1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020 Bootlin
5 * Author: Joao Marcos Costa <joaomarcos.costa@bootlin.com>
7 * sqfs.c: SquashFS filesystem implementation
10 #include <asm/unaligned.h>
14 #include <linux/types.h>
15 #include <asm/byteorder.h>
16 #include <linux/compat.h>
23 #include "sqfs_decompressor.h"
24 #include "sqfs_filesystem.h"
25 #include "sqfs_utils.h"
27 #define MAX_SYMLINK_NEST 8
29 static struct squashfs_ctxt ctxt;
30 static int symlinknest;
32 static int sqfs_readdir_nest(struct fs_dir_stream *fs_dirs, struct fs_dirent **dentp);
34 static int sqfs_disk_read(__u32 block, __u32 nr_blocks, void *buf)
41 ret = blk_dread(ctxt.cur_dev, ctxt.cur_part_info.start + block,
50 static int sqfs_read_sblk(struct squashfs_super_block **sblk)
52 *sblk = malloc_cache_aligned(ctxt.cur_dev->blksz);
56 if (sqfs_disk_read(0, 1, *sblk) != 1) {
65 static int sqfs_count_tokens(const char *filename)
67 int token_count = 1, l;
69 for (l = 1; l < strlen(filename); l++) {
70 if (filename[l] == '/')
74 /* Ignore trailing '/' in path */
75 if (filename[strlen(filename) - 1] == '/')
85 * Calculates how many blocks are needed for the buffer used in sqfs_disk_read.
86 * The memory section (e.g. inode table) start offset and its end (i.e. the next
87 * table start) must be specified. It also calculates the offset from which to
88 * start reading the buffer.
90 static int sqfs_calc_n_blks(__le64 start, __le64 end, u64 *offset)
92 u64 start_, table_size;
94 table_size = le64_to_cpu(end) - le64_to_cpu(start);
95 start_ = lldiv(le64_to_cpu(start), ctxt.cur_dev->blksz);
96 *offset = le64_to_cpu(start) - (start_ * ctxt.cur_dev->blksz);
98 return DIV_ROUND_UP(table_size + *offset, ctxt.cur_dev->blksz);
102 * Retrieves fragment block entry and returns true if the fragment block is
105 static int sqfs_frag_lookup(u32 inode_fragment_index,
106 struct squashfs_fragment_block_entry *e)
108 u64 start, end, exp_tbl, n_blks, src_len, table_offset, start_block;
109 unsigned char *metadata_buffer, *metadata, *table;
110 struct squashfs_fragment_block_entry *entries;
111 struct squashfs_super_block *sblk = ctxt.sblk;
112 unsigned long dest_len;
113 int block, offset, ret;
116 metadata_buffer = NULL;
120 if (inode_fragment_index >= get_unaligned_le32(&sblk->fragments))
123 start = get_unaligned_le64(&sblk->fragment_table_start);
124 end = get_unaligned_le64(&sblk->id_table_start);
125 exp_tbl = get_unaligned_le64(&sblk->export_table_start);
127 if (exp_tbl > start && exp_tbl < end)
130 n_blks = sqfs_calc_n_blks(sblk->fragment_table_start,
131 cpu_to_le64(end), &table_offset);
133 start /= ctxt.cur_dev->blksz;
135 /* Allocate a proper sized buffer to store the fragment index table */
136 table = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
142 if (sqfs_disk_read(start, n_blks, table) < 0) {
147 block = SQFS_FRAGMENT_INDEX(inode_fragment_index);
148 offset = SQFS_FRAGMENT_INDEX_OFFSET(inode_fragment_index);
151 * Get the start offset of the metadata block that contains the right
152 * fragment block entry
154 start_block = get_unaligned_le64(table + table_offset + block *
157 start = start_block / ctxt.cur_dev->blksz;
158 n_blks = sqfs_calc_n_blks(cpu_to_le64(start_block),
159 sblk->fragment_table_start, &table_offset);
161 metadata_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
162 if (!metadata_buffer) {
167 if (sqfs_disk_read(start, n_blks, metadata_buffer) < 0) {
172 /* Every metadata block starts with a 16-bit header */
173 header = get_unaligned_le16(metadata_buffer + table_offset);
174 metadata = metadata_buffer + table_offset + SQFS_HEADER_SIZE;
176 if (!metadata || !header) {
181 entries = malloc(SQFS_METADATA_BLOCK_SIZE);
187 if (SQFS_COMPRESSED_METADATA(header)) {
188 src_len = SQFS_METADATA_SIZE(header);
189 dest_len = SQFS_METADATA_BLOCK_SIZE;
190 ret = sqfs_decompress(&ctxt, entries, &dest_len, metadata,
197 memcpy(entries, metadata, SQFS_METADATA_SIZE(header));
200 *e = entries[offset];
201 ret = SQFS_COMPRESSED_BLOCK(e->size);
205 free(metadata_buffer);
212 * The entry name is a flexible array member, and we don't know its size before
213 * actually reading the entry. So we need a first copy to retrieve this size so
214 * we can finally copy the whole struct.
216 static int sqfs_read_entry(struct squashfs_directory_entry **dest, void *src)
218 struct squashfs_directory_entry *tmp;
222 sz = get_unaligned_le16(src + sizeof(*tmp) - sizeof(u16));
224 * 'src' points to the begin of a directory entry, and 'sz' gets its
225 * 'name_size' member's value. name_size is actually the string
226 * length - 1, so adding 2 compensates this difference and adds space
227 * for the trailling null byte.
229 *dest = malloc(sizeof(*tmp) + sz + 2);
233 memcpy(*dest, src, sizeof(*tmp) + sz + 1);
234 (*dest)->name[sz + 1] = '\0';
239 static int sqfs_get_tokens_length(char **tokens, int count)
244 * 1 is added to the result of strlen to consider the slash separator
245 * between the tokens.
247 for (i = 0; i < count; i++)
248 length += strlen(tokens[i]) + 1;
253 /* Takes a token list and returns a single string with '/' as separator. */
254 static char *sqfs_concat_tokens(char **token_list, int token_count)
257 int i, length = 0, offset = 0;
259 length = sqfs_get_tokens_length(token_list, token_count);
261 result = malloc(length + 1);
265 result[length] = '\0';
267 for (i = 0; i < token_count; i++) {
268 strcpy(result + offset, token_list[i]);
269 offset += strlen(token_list[i]);
270 result[offset++] = '/';
277 * Differently from sqfs_concat_tokens, sqfs_join writes the result into a
278 * previously allocated string, and returns the number of bytes written.
280 static int sqfs_join(char **strings, char *dest, int start, int end,
285 for (i = start; i < end; i++) {
286 strcpy(dest + offset, strings[i]);
287 offset += strlen(strings[i]);
289 dest[offset++] = separator;
296 * Fills the given token list using its size (count) and a source string (str)
298 static int sqfs_tokenize(char **tokens, int count, const char *str)
307 if (!strcmp(strc, "/")) {
308 tokens[0] = strdup(strc);
314 for (j = 0; j < count; j++) {
315 aux = strtok(!j ? strc : NULL, "/");
316 tokens[j] = strdup(aux);
318 for (i = 0; i < j; i++)
333 * Remove last 'updir + 1' tokens from the base path tokens list. This leaves us
334 * with a token list containing only the tokens needed to form the resolved
335 * path, and returns the decremented size of the token list.
337 static int sqfs_clean_base_path(char **base, int count, int updir)
341 for (i = count - updir - 1; i < count; i++)
344 return count - updir - 1;
348 * Given the base ("current dir.") path and the relative one, generate the
351 static char *sqfs_get_abs_path(const char *base, const char *rel)
353 char **base_tokens, **rel_tokens, *resolved = NULL;
354 int ret, bc, rc, i, updir = 0, resolved_size = 0, offset = 0;
359 /* Memory allocation for the token lists */
360 bc = sqfs_count_tokens(base);
361 rc = sqfs_count_tokens(rel);
362 if (bc < 1 || rc < 1)
365 base_tokens = calloc(bc, sizeof(char *));
369 rel_tokens = calloc(rc, sizeof(char *));
373 /* Fill token lists */
374 ret = sqfs_tokenize(base_tokens, bc, base);
378 ret = sqfs_tokenize(rel_tokens, rc, rel);
382 /* count '..' occurrences in target path */
383 for (i = 0; i < rc; i++) {
384 if (!strcmp(rel_tokens[i], ".."))
388 /* Remove the last token and the '..' occurrences */
389 bc = sqfs_clean_base_path(base_tokens, bc, updir);
393 /* Calculate resolved path size */
397 resolved_size += sqfs_get_tokens_length(base_tokens, bc) +
398 sqfs_get_tokens_length(rel_tokens, rc);
400 resolved = malloc(resolved_size + 1);
404 /* Set resolved path */
405 memset(resolved, '\0', resolved_size + 1);
406 offset += sqfs_join(base_tokens, resolved + offset, 0, bc, '/');
407 resolved[offset++] = '/';
408 offset += sqfs_join(rel_tokens, resolved + offset, updir, rc, '/');
412 for (i = 0; i < rc; i++)
415 for (i = 0; i < bc; i++)
416 free(base_tokens[i]);
424 static char *sqfs_resolve_symlink(struct squashfs_symlink_inode *sym,
425 const char *base_path)
427 char *resolved, *target;
430 if (__builtin_add_overflow(get_unaligned_le32(&sym->symlink_size), 1, &sz))
438 * There is no trailling null byte in the symlink's target path, so a
439 * copy is made and a '\0' is added at its end.
441 target[sz - 1] = '\0';
442 /* Get target name (relative path) */
443 strncpy(target, sym->symlink, sz - 1);
445 /* Relative -> absolute path conversion */
446 resolved = sqfs_get_abs_path(base_path, target);
454 * m_list contains each metadata block's position, and m_count is the number of
455 * elements of m_list. Those metadata blocks come from the compressed directory
458 static int sqfs_search_dir(struct squashfs_dir_stream *dirs, char **token_list,
459 int token_count, u32 *m_list, int m_count)
461 struct squashfs_super_block *sblk = ctxt.sblk;
462 char *path, *target, **sym_tokens, *res, *rem;
463 int j, ret = 0, new_inode_number, offset;
464 struct squashfs_symlink_inode *sym;
465 struct squashfs_ldir_inode *ldir;
466 struct squashfs_dir_inode *dir;
467 struct fs_dir_stream *dirsp;
468 struct fs_dirent *dent;
469 unsigned char *table;
477 dirsp = (struct fs_dir_stream *)dirs;
479 /* Start by root inode */
480 table = sqfs_find_inode(dirs->inode_table, le32_to_cpu(sblk->inodes),
481 sblk->inodes, sblk->block_size);
485 dir = (struct squashfs_dir_inode *)table;
486 ldir = (struct squashfs_ldir_inode *)table;
488 /* get directory offset in directory table */
489 offset = sqfs_dir_offset(table, m_list, m_count);
490 dirs->table = &dirs->dir_table[offset];
492 /* Setup directory header */
493 dirs->dir_header = malloc(SQFS_DIR_HEADER_SIZE);
494 if (!dirs->dir_header)
497 memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE);
499 /* Initialize squashfs_dir_stream members */
500 dirs->table += SQFS_DIR_HEADER_SIZE;
501 dirs->size = get_unaligned_le16(&dir->file_size) - SQFS_DIR_HEADER_SIZE;
502 dirs->entry_count = dirs->dir_header->count + 1;
504 /* No path given -> root directory */
505 if (!strcmp(token_list[0], "/")) {
506 dirs->table = &dirs->dir_table[offset];
507 memcpy(&dirs->i_dir, dir, sizeof(*dir));
511 for (j = 0; j < token_count; j++) {
512 if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) {
513 printf("** Cannot find directory. **\n");
518 while (!sqfs_readdir_nest(dirsp, &dent)) {
519 ret = strcmp(dent->name, token_list[j]);
527 printf("** Cannot find directory. **\n");
532 /* Redefine inode as the found token */
533 new_inode_number = dirs->entry->inode_offset +
534 dirs->dir_header->inode_number;
536 /* Get reference to inode in the inode table */
537 table = sqfs_find_inode(dirs->inode_table, new_inode_number,
538 sblk->inodes, sblk->block_size);
541 dir = (struct squashfs_dir_inode *)table;
543 /* Check for symbolic link and inode type sanity */
544 if (get_unaligned_le16(&dir->inode_type) == SQFS_SYMLINK_TYPE) {
545 if (++symlinknest == MAX_SYMLINK_NEST) {
550 sym = (struct squashfs_symlink_inode *)table;
551 /* Get first j + 1 tokens */
552 path = sqfs_concat_tokens(token_list, j + 1);
557 /* Resolve for these tokens */
558 target = sqfs_resolve_symlink(sym, path);
563 /* Join remaining tokens */
564 rem = sqfs_concat_tokens(token_list + j + 1, token_count -
571 * Concatenate remaining tokens and symlink's target.
572 * Allocate enough space for rem, target, '/' and '\0'.
574 res = malloc(strlen(rem) + strlen(target) + 2);
580 res[strlen(target)] = '/';
581 strcpy(res + strlen(target) + 1, rem);
582 token_count = sqfs_count_tokens(res);
584 if (token_count < 0) {
589 sym_tokens = malloc(token_count * sizeof(char *));
595 /* Fill tokens list */
596 ret = sqfs_tokenize(sym_tokens, token_count, res);
604 ret = sqfs_search_dir(dirs, sym_tokens, token_count,
607 } else if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) {
608 printf("** Cannot find directory. **\n");
615 /* Check if it is an extended dir. */
616 if (get_unaligned_le16(&dir->inode_type) == SQFS_LDIR_TYPE)
617 ldir = (struct squashfs_ldir_inode *)table;
619 /* Get dir. offset into the directory table */
620 offset = sqfs_dir_offset(table, m_list, m_count);
621 dirs->table = &dirs->dir_table[offset];
623 /* Copy directory header */
624 memcpy(dirs->dir_header, &dirs->dir_table[offset],
625 SQFS_DIR_HEADER_SIZE);
627 /* Check for empty directory */
628 if (sqfs_is_empty_dir(table)) {
629 printf("Empty directory.\n");
632 ret = SQFS_EMPTY_DIR;
636 dirs->table += SQFS_DIR_HEADER_SIZE;
637 dirs->size = get_unaligned_le16(&dir->file_size);
638 dirs->entry_count = dirs->dir_header->count + 1;
639 dirs->size -= SQFS_DIR_HEADER_SIZE;
644 offset = sqfs_dir_offset(table, m_list, m_count);
645 dirs->table = &dirs->dir_table[offset];
647 if (get_unaligned_le16(&dir->inode_type) == SQFS_DIR_TYPE)
648 memcpy(&dirs->i_dir, dir, sizeof(*dir));
650 memcpy(&dirs->i_ldir, ldir, sizeof(*ldir));
662 * Inode and directory tables are stored as a series of metadata blocks, and
663 * given the compressed size of this table, we can calculate how much metadata
664 * blocks are needed to store the result of the decompression, since a
665 * decompressed metadata block should have a size of 8KiB.
667 static int sqfs_count_metablks(void *table, u32 offset, int table_size)
669 int count = 0, cur_size = 0, ret;
674 ret = sqfs_read_metablock(table, offset + cur_size, &comp,
678 cur_size += data_size + SQFS_HEADER_SIZE;
680 } while (cur_size < table_size);
686 * Storing the metadata blocks header's positions will be useful while looking
687 * for an entry in the directory table, using the reference (index and offset)
688 * given by its inode.
690 static int sqfs_get_metablk_pos(u32 *pos_list, void *table, u32 offset,
693 u32 data_size, cur_size = 0;
700 for (j = 0; j < metablks_count; j++) {
701 ret = sqfs_read_metablock(table, offset + cur_size, &comp,
706 cur_size += data_size + SQFS_HEADER_SIZE;
707 pos_list[j] = cur_size;
713 static int sqfs_read_inode_table(unsigned char **inode_table)
715 struct squashfs_super_block *sblk = ctxt.sblk;
716 u64 start, n_blks, table_offset, table_size;
717 int j, ret = 0, metablks_count;
718 unsigned char *src_table, *itb;
719 u32 src_len, dest_offset = 0;
720 unsigned long dest_len = 0;
723 table_size = get_unaligned_le64(&sblk->directory_table_start) -
724 get_unaligned_le64(&sblk->inode_table_start);
725 start = get_unaligned_le64(&sblk->inode_table_start) /
727 n_blks = sqfs_calc_n_blks(sblk->inode_table_start,
728 sblk->directory_table_start, &table_offset);
730 /* Allocate a proper sized buffer (itb) to store the inode table */
731 itb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
735 if (sqfs_disk_read(start, n_blks, itb) < 0) {
740 /* Parse inode table (metadata block) header */
741 ret = sqfs_read_metablock(itb, table_offset, &compressed, &src_len);
747 /* Calculate size to store the whole decompressed table */
748 metablks_count = sqfs_count_metablks(itb, table_offset, table_size);
749 if (metablks_count < 1) {
754 *inode_table = kcalloc(metablks_count, SQFS_METADATA_BLOCK_SIZE,
758 printf("Error: failed to allocate squashfs inode_table of size %i, increasing CONFIG_SYS_MALLOC_LEN could help\n",
759 metablks_count * SQFS_METADATA_BLOCK_SIZE);
763 src_table = itb + table_offset + SQFS_HEADER_SIZE;
765 /* Extract compressed Inode table */
766 for (j = 0; j < metablks_count; j++) {
767 sqfs_read_metablock(itb, table_offset, &compressed, &src_len);
769 dest_len = SQFS_METADATA_BLOCK_SIZE;
770 ret = sqfs_decompress(&ctxt, *inode_table +
771 dest_offset, &dest_len,
779 dest_offset += dest_len;
781 memcpy(*inode_table + (j * SQFS_METADATA_BLOCK_SIZE),
786 * Offsets to the decompression destination, to the metadata
787 * buffer 'itb' and to the decompression source, respectively.
790 table_offset += src_len + SQFS_HEADER_SIZE;
791 src_table += src_len + SQFS_HEADER_SIZE;
800 static int sqfs_read_directory_table(unsigned char **dir_table, u32 **pos_list)
802 u64 start, n_blks, table_offset, table_size;
803 struct squashfs_super_block *sblk = ctxt.sblk;
804 int j, ret = 0, metablks_count = -1;
805 unsigned char *src_table, *dtb;
806 u32 src_len, dest_offset = 0;
807 unsigned long dest_len = 0;
812 /* DIRECTORY TABLE */
813 table_size = get_unaligned_le64(&sblk->fragment_table_start) -
814 get_unaligned_le64(&sblk->directory_table_start);
815 start = get_unaligned_le64(&sblk->directory_table_start) /
817 n_blks = sqfs_calc_n_blks(sblk->directory_table_start,
818 sblk->fragment_table_start, &table_offset);
820 /* Allocate a proper sized buffer (dtb) to store the directory table */
821 dtb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
825 if (sqfs_disk_read(start, n_blks, dtb) < 0)
828 /* Parse directory table (metadata block) header */
829 ret = sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
833 /* Calculate total size to store the whole decompressed table */
834 metablks_count = sqfs_count_metablks(dtb, table_offset, table_size);
835 if (metablks_count < 1)
838 *dir_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE);
842 *pos_list = malloc(metablks_count * sizeof(u32));
846 ret = sqfs_get_metablk_pos(*pos_list, dtb, table_offset,
853 src_table = dtb + table_offset + SQFS_HEADER_SIZE;
855 /* Extract compressed Directory table */
857 for (j = 0; j < metablks_count; j++) {
858 sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
860 dest_len = SQFS_METADATA_BLOCK_SIZE;
861 ret = sqfs_decompress(&ctxt, *dir_table +
862 (j * SQFS_METADATA_BLOCK_SIZE),
863 &dest_len, src_table, src_len);
869 if (dest_len < SQFS_METADATA_BLOCK_SIZE) {
870 dest_offset += dest_len;
874 dest_offset += dest_len;
876 memcpy(*dir_table + (j * SQFS_METADATA_BLOCK_SIZE),
881 * Offsets to the decompression destination, to the metadata
882 * buffer 'dtb' and to the decompression source, respectively.
884 table_offset += src_len + SQFS_HEADER_SIZE;
885 src_table += src_len + SQFS_HEADER_SIZE;
889 if (metablks_count < 1) {
897 return metablks_count;
900 static int sqfs_opendir_nest(const char *filename, struct fs_dir_stream **dirsp)
902 unsigned char *inode_table = NULL, *dir_table = NULL;
903 int j, token_count = 0, ret = 0, metablks_count;
904 struct squashfs_dir_stream *dirs;
905 char **token_list = NULL, *path = NULL;
906 u32 *pos_list = NULL;
908 dirs = calloc(1, sizeof(*dirs));
912 /* these should be set to NULL to prevent dangling pointers */
913 dirs->dir_header = NULL;
916 dirs->inode_table = NULL;
917 dirs->dir_table = NULL;
919 ret = sqfs_read_inode_table(&inode_table);
925 metablks_count = sqfs_read_directory_table(&dir_table, &pos_list);
926 if (metablks_count < 1) {
931 /* Tokenize filename */
932 token_count = sqfs_count_tokens(filename);
933 if (token_count < 0) {
938 path = strdup(filename);
944 token_list = malloc(token_count * sizeof(char *));
950 /* Fill tokens list */
951 ret = sqfs_tokenize(token_list, token_count, path);
955 * ldir's (extended directory) size is greater than dir, so it works as
956 * a general solution for the malloc size, since 'i' is a union.
958 dirs->inode_table = inode_table;
959 dirs->dir_table = dir_table;
960 ret = sqfs_search_dir(dirs, token_list, token_count, pos_list,
965 if (le16_to_cpu(dirs->i_dir.inode_type) == SQFS_DIR_TYPE)
966 dirs->size = le16_to_cpu(dirs->i_dir.file_size);
968 dirs->size = le32_to_cpu(dirs->i_ldir.file_size);
970 /* Setup directory header */
971 memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE);
972 dirs->entry_count = dirs->dir_header->count + 1;
973 dirs->size -= SQFS_DIR_HEADER_SIZE;
977 dirs->table += SQFS_DIR_HEADER_SIZE;
979 *dirsp = (struct fs_dir_stream *)dirs;
982 for (j = 0; j < token_count; j++)
995 int sqfs_opendir(const char *filename, struct fs_dir_stream **dirsp)
998 return sqfs_opendir_nest(filename, dirsp);
1001 int sqfs_readdir(struct fs_dir_stream *fs_dirs, struct fs_dirent **dentp)
1004 return sqfs_readdir_nest(fs_dirs, dentp);
1007 static int sqfs_readdir_nest(struct fs_dir_stream *fs_dirs, struct fs_dirent **dentp)
1009 struct squashfs_super_block *sblk = ctxt.sblk;
1010 struct squashfs_dir_stream *dirs;
1011 struct squashfs_lreg_inode *lreg;
1012 struct squashfs_base_inode *base;
1013 struct squashfs_reg_inode *reg;
1014 int i_number, offset = 0, ret;
1015 struct fs_dirent *dent;
1016 unsigned char *ipos;
1019 dirs = (struct squashfs_dir_stream *)fs_dirs;
1022 return -SQFS_STOP_READDIR;
1025 dent = &dirs->dentp;
1027 if (!dirs->entry_count) {
1028 if (dirs->size > SQFS_DIR_HEADER_SIZE) {
1029 dirs->size -= SQFS_DIR_HEADER_SIZE;
1033 return -SQFS_STOP_READDIR;
1036 if (dirs->size > SQFS_EMPTY_FILE_SIZE) {
1037 /* Read follow-up (emitted) dir. header */
1038 memcpy(dirs->dir_header, dirs->table,
1039 SQFS_DIR_HEADER_SIZE);
1040 dirs->entry_count = dirs->dir_header->count + 1;
1041 ret = sqfs_read_entry(&dirs->entry, dirs->table +
1042 SQFS_DIR_HEADER_SIZE);
1044 return -SQFS_STOP_READDIR;
1046 dirs->table += SQFS_DIR_HEADER_SIZE;
1049 ret = sqfs_read_entry(&dirs->entry, dirs->table);
1051 return -SQFS_STOP_READDIR;
1054 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1055 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1058 return -SQFS_STOP_READDIR;
1060 base = (struct squashfs_base_inode *)ipos;
1062 /* Set entry type and size */
1063 switch (dirs->entry->type) {
1065 case SQFS_LDIR_TYPE:
1066 dent->type = FS_DT_DIR;
1069 case SQFS_LREG_TYPE:
1071 * Entries do not differentiate extended from regular types, so
1072 * it needs to be verified manually.
1074 if (get_unaligned_le16(&base->inode_type) == SQFS_LREG_TYPE) {
1075 lreg = (struct squashfs_lreg_inode *)ipos;
1076 dent->size = get_unaligned_le64(&lreg->file_size);
1078 reg = (struct squashfs_reg_inode *)ipos;
1079 dent->size = get_unaligned_le32(®->file_size);
1082 dent->type = FS_DT_REG;
1084 case SQFS_BLKDEV_TYPE:
1085 case SQFS_CHRDEV_TYPE:
1086 case SQFS_LBLKDEV_TYPE:
1087 case SQFS_LCHRDEV_TYPE:
1088 case SQFS_FIFO_TYPE:
1089 case SQFS_SOCKET_TYPE:
1090 case SQFS_LFIFO_TYPE:
1091 case SQFS_LSOCKET_TYPE:
1092 dent->type = SQFS_MISC_ENTRY_TYPE;
1094 case SQFS_SYMLINK_TYPE:
1095 case SQFS_LSYMLINK_TYPE:
1096 dent->type = FS_DT_LNK;
1099 return -SQFS_STOP_READDIR;
1102 /* Set entry name (capped at FS_DIRENT_NAME_LEN which is a U-Boot limitation) */
1103 name_size = min_t(u16, dirs->entry->name_size + 1, FS_DIRENT_NAME_LEN - 1);
1104 strncpy(dent->name, dirs->entry->name, name_size);
1105 dent->name[name_size] = '\0';
1107 offset = dirs->entry->name_size + 1 + SQFS_ENTRY_BASE_LENGTH;
1108 dirs->entry_count--;
1110 /* Decrement size to be read */
1111 if (dirs->size > offset)
1112 dirs->size -= offset;
1116 /* Keep a reference to the current entry before incrementing it */
1117 dirs->table += offset;
1124 int sqfs_probe(struct blk_desc *fs_dev_desc, struct disk_partition *fs_partition)
1126 struct squashfs_super_block *sblk;
1129 ctxt.cur_dev = fs_dev_desc;
1130 ctxt.cur_part_info = *fs_partition;
1132 ret = sqfs_read_sblk(&sblk);
1136 /* Make sure it has a valid SquashFS magic number*/
1137 if (get_unaligned_le32(&sblk->s_magic) != SQFS_MAGIC_NUMBER) {
1138 debug("Bad magic number for SquashFS image.\n");
1145 ret = sqfs_decompressor_init(&ctxt);
1152 ctxt.cur_dev = NULL;
1158 static char *sqfs_basename(char *path)
1162 fname = path + strlen(path) - 1;
1163 while (fname >= path) {
1164 if (*fname == '/') {
1175 static char *sqfs_dirname(char *path)
1179 fname = sqfs_basename(path);
1187 * Takes a path to file and splits it in two parts: the filename itself and the
1188 * directory's path, e.g.:
1189 * path: /path/to/file.txt
1193 static int sqfs_split_path(char **file, char **dir, const char *path)
1195 char *dirc, *basec, *bname, *dname, *tmp_path;
1206 /* check for first slash in path*/
1207 if (path[0] == '/') {
1208 tmp_path = strdup(path);
1214 tmp_path = malloc(strlen(path) + 2);
1220 strcpy(tmp_path + 1, path);
1223 /* String duplicates */
1224 dirc = strdup(tmp_path);
1230 basec = strdup(tmp_path);
1236 dname = sqfs_dirname(dirc);
1237 bname = sqfs_basename(basec);
1239 *file = strdup(bname);
1246 if (*dname == '\0') {
1256 *dir = strdup(dname);
1277 static int sqfs_get_regfile_info(struct squashfs_reg_inode *reg,
1278 struct squashfs_file_info *finfo,
1279 struct squashfs_fragment_block_entry *fentry,
1282 int datablk_count = 0, ret;
1284 finfo->size = get_unaligned_le32(®->file_size);
1285 finfo->offset = get_unaligned_le32(®->offset);
1286 finfo->start = get_unaligned_le32(®->start_block);
1287 finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(®->fragment));
1289 if (finfo->frag && finfo->offset == 0xFFFFFFFF)
1292 if (finfo->size < 1 || finfo->start == 0xFFFFFFFF)
1296 datablk_count = finfo->size / le32_to_cpu(blksz);
1297 ret = sqfs_frag_lookup(get_unaligned_le32(®->fragment),
1302 if (fentry->size < 1 || fentry->start == 0x7FFFFFFF)
1305 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1308 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1309 if (!finfo->blk_sizes)
1312 return datablk_count;
1315 static int sqfs_get_lregfile_info(struct squashfs_lreg_inode *lreg,
1316 struct squashfs_file_info *finfo,
1317 struct squashfs_fragment_block_entry *fentry,
1320 int datablk_count = 0, ret;
1322 finfo->size = get_unaligned_le64(&lreg->file_size);
1323 finfo->offset = get_unaligned_le32(&lreg->offset);
1324 finfo->start = get_unaligned_le64(&lreg->start_block);
1325 finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(&lreg->fragment));
1327 if (finfo->frag && finfo->offset == 0xFFFFFFFF)
1330 if (finfo->size < 1 || finfo->start == 0x7FFFFFFF)
1334 datablk_count = finfo->size / le32_to_cpu(blksz);
1335 ret = sqfs_frag_lookup(get_unaligned_le32(&lreg->fragment),
1340 if (fentry->size < 1 || fentry->start == 0x7FFFFFFF)
1343 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1346 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1347 if (!finfo->blk_sizes)
1350 return datablk_count;
1353 static int sqfs_read_nest(const char *filename, void *buf, loff_t offset,
1354 loff_t len, loff_t *actread)
1356 char *dir = NULL, *fragment_block, *datablock = NULL;
1357 char *fragment = NULL, *file = NULL, *resolved, *data;
1358 u64 start, n_blks, table_size, data_offset, table_offset, sparse_size;
1359 int ret, j, i_number, datablk_count = 0;
1360 struct squashfs_super_block *sblk = ctxt.sblk;
1361 struct squashfs_fragment_block_entry frag_entry;
1362 struct squashfs_file_info finfo = {0};
1363 struct squashfs_symlink_inode *symlink;
1364 struct fs_dir_stream *dirsp = NULL;
1365 struct squashfs_dir_stream *dirs;
1366 struct squashfs_lreg_inode *lreg;
1367 struct squashfs_base_inode *base;
1368 struct squashfs_reg_inode *reg;
1369 unsigned long dest_len;
1370 struct fs_dirent *dent;
1371 unsigned char *ipos;
1377 * TODO: implement reading at an offset in file
1379 printf("Error: reading at a specific offset in a squashfs file is not supported yet.\n");
1384 * sqfs_opendir_nest will uncompress inode and directory tables, and will
1385 * return a pointer to the directory that contains the requested file.
1387 sqfs_split_path(&file, &dir, filename);
1388 ret = sqfs_opendir_nest(dir, &dirsp);
1393 dirs = (struct squashfs_dir_stream *)dirsp;
1395 /* For now, only regular files are able to be loaded */
1396 while (!sqfs_readdir_nest(dirsp, &dent)) {
1397 ret = strcmp(dent->name, file);
1406 printf("File not found.\n");
1412 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1413 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1420 base = (struct squashfs_base_inode *)ipos;
1421 switch (get_unaligned_le16(&base->inode_type)) {
1423 reg = (struct squashfs_reg_inode *)ipos;
1424 datablk_count = sqfs_get_regfile_info(reg, &finfo, &frag_entry,
1426 if (datablk_count < 0) {
1431 memcpy(finfo.blk_sizes, ipos + sizeof(*reg),
1432 datablk_count * sizeof(u32));
1434 case SQFS_LREG_TYPE:
1435 lreg = (struct squashfs_lreg_inode *)ipos;
1436 datablk_count = sqfs_get_lregfile_info(lreg, &finfo,
1439 if (datablk_count < 0) {
1444 memcpy(finfo.blk_sizes, ipos + sizeof(*lreg),
1445 datablk_count * sizeof(u32));
1447 case SQFS_SYMLINK_TYPE:
1448 case SQFS_LSYMLINK_TYPE:
1449 if (++symlinknest == MAX_SYMLINK_NEST) {
1454 symlink = (struct squashfs_symlink_inode *)ipos;
1455 resolved = sqfs_resolve_symlink(symlink, filename);
1456 ret = sqfs_read_nest(resolved, buf, offset, len, actread);
1459 case SQFS_BLKDEV_TYPE:
1460 case SQFS_CHRDEV_TYPE:
1461 case SQFS_LBLKDEV_TYPE:
1462 case SQFS_LCHRDEV_TYPE:
1463 case SQFS_FIFO_TYPE:
1464 case SQFS_SOCKET_TYPE:
1465 case SQFS_LFIFO_TYPE:
1466 case SQFS_LSOCKET_TYPE:
1468 printf("Unsupported entry type\n");
1473 /* If the user specifies a length, check its sanity */
1475 if (len > finfo.size) {
1485 if (datablk_count) {
1486 data_offset = finfo.start;
1487 datablock = malloc(get_unaligned_le32(&sblk->block_size));
1494 for (j = 0; j < datablk_count; j++) {
1497 start = lldiv(data_offset, ctxt.cur_dev->blksz);
1498 table_size = SQFS_BLOCK_SIZE(finfo.blk_sizes[j]);
1499 table_offset = data_offset - (start * ctxt.cur_dev->blksz);
1500 n_blks = DIV_ROUND_UP(table_size + table_offset,
1501 ctxt.cur_dev->blksz);
1503 /* Don't load any data for sparse blocks */
1504 if (finfo.blk_sizes[j] == 0) {
1510 data_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
1517 ret = sqfs_disk_read(start, n_blks, data_buffer);
1520 * Possible causes: too many data blocks or too large
1521 * SquashFS block size. Tip: re-compile the SquashFS
1522 * image with mksquashfs's -b <block_size> option.
1524 printf("Error: too many data blocks to be read.\n");
1528 data = data_buffer + table_offset;
1532 if (finfo.blk_sizes[j] == 0) {
1533 /* This is a sparse block */
1534 sparse_size = get_unaligned_le32(&sblk->block_size);
1535 if ((*actread + sparse_size) > len)
1536 sparse_size = len - *actread;
1537 memset(buf + *actread, 0, sparse_size);
1538 *actread += sparse_size;
1539 } else if (SQFS_COMPRESSED_BLOCK(finfo.blk_sizes[j])) {
1540 dest_len = get_unaligned_le32(&sblk->block_size);
1541 ret = sqfs_decompress(&ctxt, datablock, &dest_len,
1546 if ((*actread + dest_len) > len)
1547 dest_len = len - *actread;
1548 memcpy(buf + *actread, datablock, dest_len);
1549 *actread += dest_len;
1551 if ((*actread + table_size) > len)
1552 table_size = len - *actread;
1553 memcpy(buf + *actread, data, table_size);
1554 *actread += table_size;
1557 data_offset += table_size;
1559 if (*actread >= len)
1564 * There is no need to continue if the file is not fragmented.
1571 start = lldiv(frag_entry.start, ctxt.cur_dev->blksz);
1572 table_size = SQFS_BLOCK_SIZE(frag_entry.size);
1573 table_offset = frag_entry.start - (start * ctxt.cur_dev->blksz);
1574 n_blks = DIV_ROUND_UP(table_size + table_offset, ctxt.cur_dev->blksz);
1576 fragment = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
1583 ret = sqfs_disk_read(start, n_blks, fragment);
1587 /* File compressed and fragmented */
1588 if (finfo.frag && finfo.comp) {
1589 dest_len = get_unaligned_le32(&sblk->block_size);
1590 fragment_block = malloc(dest_len);
1591 if (!fragment_block) {
1596 ret = sqfs_decompress(&ctxt, fragment_block, &dest_len,
1597 (void *)fragment + table_offset,
1600 free(fragment_block);
1604 memcpy(buf + *actread, &fragment_block[finfo.offset], finfo.size - *actread);
1605 *actread = finfo.size;
1607 free(fragment_block);
1609 } else if (finfo.frag && !finfo.comp) {
1610 fragment_block = (void *)fragment + table_offset;
1612 memcpy(buf + *actread, &fragment_block[finfo.offset], finfo.size - *actread);
1613 *actread = finfo.size;
1621 free(finfo.blk_sizes);
1622 sqfs_closedir(dirsp);
1627 int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
1631 return sqfs_read_nest(filename, buf, offset, len, actread);
1634 static int sqfs_size_nest(const char *filename, loff_t *size)
1636 struct squashfs_super_block *sblk = ctxt.sblk;
1637 struct squashfs_symlink_inode *symlink;
1638 struct fs_dir_stream *dirsp = NULL;
1639 struct squashfs_base_inode *base;
1640 struct squashfs_dir_stream *dirs;
1641 struct squashfs_lreg_inode *lreg;
1642 struct squashfs_reg_inode *reg;
1643 char *dir, *file, *resolved;
1644 struct fs_dirent *dent;
1645 unsigned char *ipos;
1648 sqfs_split_path(&file, &dir, filename);
1650 * sqfs_opendir_nest will uncompress inode and directory tables, and will
1651 * return a pointer to the directory that contains the requested file.
1653 ret = sqfs_opendir_nest(dir, &dirsp);
1659 dirs = (struct squashfs_dir_stream *)dirsp;
1661 while (!sqfs_readdir_nest(dirsp, &dent)) {
1662 ret = strcmp(dent->name, file);
1670 printf("File not found.\n");
1676 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1677 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1689 base = (struct squashfs_base_inode *)ipos;
1690 switch (get_unaligned_le16(&base->inode_type)) {
1692 reg = (struct squashfs_reg_inode *)ipos;
1693 *size = get_unaligned_le32(®->file_size);
1695 case SQFS_LREG_TYPE:
1696 lreg = (struct squashfs_lreg_inode *)ipos;
1697 *size = get_unaligned_le64(&lreg->file_size);
1699 case SQFS_SYMLINK_TYPE:
1700 case SQFS_LSYMLINK_TYPE:
1701 if (++symlinknest == MAX_SYMLINK_NEST) {
1706 symlink = (struct squashfs_symlink_inode *)ipos;
1707 resolved = sqfs_resolve_symlink(symlink, filename);
1708 ret = sqfs_size(resolved, size);
1711 case SQFS_BLKDEV_TYPE:
1712 case SQFS_CHRDEV_TYPE:
1713 case SQFS_LBLKDEV_TYPE:
1714 case SQFS_LCHRDEV_TYPE:
1715 case SQFS_FIFO_TYPE:
1716 case SQFS_SOCKET_TYPE:
1717 case SQFS_LFIFO_TYPE:
1718 case SQFS_LSOCKET_TYPE:
1720 printf("Unable to recover entry's size.\n");
1730 sqfs_closedir(dirsp);
1735 int sqfs_exists(const char *filename)
1737 struct fs_dir_stream *dirsp = NULL;
1738 struct squashfs_dir_stream *dirs;
1740 struct fs_dirent *dent;
1743 sqfs_split_path(&file, &dir, filename);
1745 * sqfs_opendir_nest will uncompress inode and directory tables, and will
1746 * return a pointer to the directory that contains the requested file.
1749 ret = sqfs_opendir_nest(dir, &dirsp);
1755 dirs = (struct squashfs_dir_stream *)dirsp;
1757 while (!sqfs_readdir_nest(dirsp, &dent)) {
1758 ret = strcmp(dent->name, file);
1765 sqfs_closedir(dirsp);
1774 int sqfs_size(const char *filename, loff_t *size)
1777 return sqfs_size_nest(filename, size);
1780 void sqfs_close(void)
1782 sqfs_decompressor_cleanup(&ctxt);
1785 ctxt.cur_dev = NULL;
1788 void sqfs_closedir(struct fs_dir_stream *dirs)
1790 struct squashfs_dir_stream *sqfs_dirs;
1795 sqfs_dirs = (struct squashfs_dir_stream *)dirs;
1796 free(sqfs_dirs->inode_table);
1797 free(sqfs_dirs->dir_table);
1798 free(sqfs_dirs->dir_header);