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>
13 #include <linux/types.h>
14 #include <linux/byteorder/little_endian.h>
15 #include <linux/byteorder/generic.h>
22 #include "sqfs_decompressor.h"
23 #include "sqfs_filesystem.h"
24 #include "sqfs_utils.h"
26 static struct squashfs_ctxt ctxt;
28 static int sqfs_disk_read(__u32 block, __u32 nr_blocks, void *buf)
35 ret = blk_dread(ctxt.cur_dev, ctxt.cur_part_info.start + block,
44 static int sqfs_read_sblk(struct squashfs_super_block **sblk)
46 *sblk = malloc_cache_aligned(ctxt.cur_dev->blksz);
50 if (sqfs_disk_read(0, 1, *sblk) != 1) {
58 static int sqfs_count_tokens(const char *filename)
60 int token_count = 1, l;
62 for (l = 1; l < strlen(filename); l++) {
63 if (filename[l] == '/')
67 /* Ignore trailing '/' in path */
68 if (filename[strlen(filename) - 1] == '/')
78 * Calculates how many blocks are needed for the buffer used in sqfs_disk_read.
79 * The memory section (e.g. inode table) start offset and its end (i.e. the next
80 * table start) must be specified. It also calculates the offset from which to
81 * start reading the buffer.
83 static int sqfs_calc_n_blks(__le64 start, __le64 end, u64 *offset)
85 u64 start_, table_size;
87 table_size = le64_to_cpu(end) - le64_to_cpu(start);
88 start_ = le64_to_cpu(start) / ctxt.cur_dev->blksz;
89 *offset = le64_to_cpu(start) - (start_ * ctxt.cur_dev->blksz);
91 return DIV_ROUND_UP(table_size + *offset, ctxt.cur_dev->blksz);
95 * Retrieves fragment block entry and returns true if the fragment block is
98 static int sqfs_frag_lookup(u32 inode_fragment_index,
99 struct squashfs_fragment_block_entry *e)
101 u64 start, n_blks, src_len, table_offset, start_block;
102 unsigned char *metadata_buffer, *metadata, *table;
103 struct squashfs_fragment_block_entry *entries;
104 struct squashfs_super_block *sblk = ctxt.sblk;
105 unsigned long dest_len;
106 int block, offset, ret;
109 metadata_buffer = NULL;
113 if (inode_fragment_index >= get_unaligned_le32(&sblk->fragments))
116 start = get_unaligned_le64(&sblk->fragment_table_start) /
118 n_blks = sqfs_calc_n_blks(sblk->fragment_table_start,
119 sblk->export_table_start,
122 /* Allocate a proper sized buffer to store the fragment index table */
123 table = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
129 if (sqfs_disk_read(start, n_blks, table) < 0) {
134 block = SQFS_FRAGMENT_INDEX(inode_fragment_index);
135 offset = SQFS_FRAGMENT_INDEX_OFFSET(inode_fragment_index);
138 * Get the start offset of the metadata block that contains the right
139 * fragment block entry
141 start_block = get_unaligned_le64(table + table_offset + block *
144 start = start_block / ctxt.cur_dev->blksz;
145 n_blks = sqfs_calc_n_blks(cpu_to_le64(start_block),
146 sblk->fragment_table_start, &table_offset);
148 metadata_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
149 if (!metadata_buffer) {
154 if (sqfs_disk_read(start, n_blks, metadata_buffer) < 0) {
159 /* Every metadata block starts with a 16-bit header */
160 header = get_unaligned_le16(metadata_buffer + table_offset);
161 metadata = metadata_buffer + table_offset + SQFS_HEADER_SIZE;
163 if (!metadata || !header) {
168 entries = malloc(SQFS_METADATA_BLOCK_SIZE);
174 if (SQFS_COMPRESSED_METADATA(header)) {
175 src_len = SQFS_METADATA_SIZE(header);
176 dest_len = SQFS_METADATA_BLOCK_SIZE;
177 ret = sqfs_decompress(&ctxt, entries, &dest_len, metadata,
184 memcpy(entries, metadata, SQFS_METADATA_SIZE(header));
187 *e = entries[offset];
188 ret = SQFS_COMPRESSED_BLOCK(e->size);
192 free(metadata_buffer);
199 * The entry name is a flexible array member, and we don't know its size before
200 * actually reading the entry. So we need a first copy to retrieve this size so
201 * we can finally copy the whole struct.
203 static int sqfs_read_entry(struct squashfs_directory_entry **dest, void *src)
205 struct squashfs_directory_entry *tmp;
209 sz = get_unaligned_le16(src + sizeof(*tmp) - sizeof(u16));
211 * 'src' points to the begin of a directory entry, and 'sz' gets its
212 * 'name_size' member's value. name_size is actually the string
213 * length - 1, so adding 2 compensates this difference and adds space
214 * for the trailling null byte.
216 *dest = malloc(sizeof(*tmp) + sz + 2);
220 memcpy(*dest, src, sizeof(*tmp) + sz + 1);
221 (*dest)->name[sz + 1] = '\0';
226 static int sqfs_get_tokens_length(char **tokens, int count)
231 * 1 is added to the result of strlen to consider the slash separator
232 * between the tokens.
234 for (i = 0; i < count; i++)
235 length += strlen(tokens[i]) + 1;
240 /* Takes a token list and returns a single string with '/' as separator. */
241 static char *sqfs_concat_tokens(char **token_list, int token_count)
244 int i, length = 0, offset = 0;
246 length = sqfs_get_tokens_length(token_list, token_count);
248 result = malloc(length + 1);
252 result[length] = '\0';
254 for (i = 0; i < token_count; i++) {
255 strcpy(result + offset, token_list[i]);
256 offset += strlen(token_list[i]);
257 result[offset++] = '/';
264 * Differently from sqfs_concat_tokens, sqfs_join writes the result into a
265 * previously allocated string, and returns the number of bytes written.
267 static int sqfs_join(char **strings, char *dest, int start, int end,
272 for (i = start; i < end; i++) {
273 strcpy(dest + offset, strings[i]);
274 offset += strlen(strings[i]);
276 dest[offset++] = separator;
283 * Fills the given token list using its size (count) and a source string (str)
285 static int sqfs_tokenize(char **tokens, int count, const char *str)
294 if (!strcmp(strc, "/")) {
295 tokens[0] = strdup(strc);
301 for (j = 0; j < count; j++) {
302 aux = strtok(!j ? strc : NULL, "/");
303 tokens[j] = strdup(aux);
305 for (i = 0; i < j; i++)
320 * Remove last 'updir + 1' tokens from the base path tokens list. This leaves us
321 * with a token list containing only the tokens needed to form the resolved
322 * path, and returns the decremented size of the token list.
324 static int sqfs_clean_base_path(char **base, int count, int updir)
328 for (i = count - updir - 1; i < count; i++)
331 return count - updir - 1;
335 * Given the base ("current dir.") path and the relative one, generate the
338 static char *sqfs_get_abs_path(const char *base, const char *rel)
340 char **base_tokens, **rel_tokens, *resolved = NULL;
341 int ret, bc, rc, i, updir = 0, resolved_size = 0, offset = 0;
346 /* Memory allocation for the token lists */
347 bc = sqfs_count_tokens(base);
348 rc = sqfs_count_tokens(rel);
349 if (bc < 1 || rc < 1)
352 base_tokens = calloc(bc, sizeof(char *));
356 rel_tokens = calloc(rc, sizeof(char *));
360 /* Fill token lists */
361 ret = sqfs_tokenize(base_tokens, bc, base);
365 ret = sqfs_tokenize(rel_tokens, rc, rel);
369 /* count '..' occurrences in target path */
370 for (i = 0; i < rc; i++) {
371 if (!strcmp(rel_tokens[i], ".."))
375 /* Remove the last token and the '..' occurrences */
376 bc = sqfs_clean_base_path(base_tokens, bc, updir);
380 /* Calculate resolved path size */
384 resolved_size += sqfs_get_tokens_length(base_tokens, bc) +
385 sqfs_get_tokens_length(rel_tokens, rc);
387 resolved = malloc(resolved_size + 1);
391 /* Set resolved path */
392 memset(resolved, '\0', resolved_size + 1);
393 offset += sqfs_join(base_tokens, resolved + offset, 0, bc, '/');
394 resolved[offset++] = '/';
395 offset += sqfs_join(rel_tokens, resolved + offset, updir, rc, '/');
399 for (i = 0; i < rc; i++)
402 for (i = 0; i < bc; i++)
403 free(base_tokens[i]);
411 static char *sqfs_resolve_symlink(struct squashfs_symlink_inode *sym,
412 const char *base_path)
414 char *resolved, *target;
417 sz = get_unaligned_le32(&sym->symlink_size);
418 target = malloc(sz + 1);
423 * There is no trailling null byte in the symlink's target path, so a
424 * copy is made and a '\0' is added at its end.
427 /* Get target name (relative path) */
428 strncpy(target, sym->symlink, sz);
430 /* Relative -> absolute path conversion */
431 resolved = sqfs_get_abs_path(base_path, target);
439 * m_list contains each metadata block's position, and m_count is the number of
440 * elements of m_list. Those metadata blocks come from the compressed directory
443 static int sqfs_search_dir(struct squashfs_dir_stream *dirs, char **token_list,
444 int token_count, u32 *m_list, int m_count)
446 struct squashfs_super_block *sblk = ctxt.sblk;
447 char *path, *target, **sym_tokens, *res, *rem;
448 int j, ret = 0, new_inode_number, offset;
449 struct squashfs_symlink_inode *sym;
450 struct squashfs_ldir_inode *ldir;
451 struct squashfs_dir_inode *dir;
452 struct fs_dir_stream *dirsp;
453 struct fs_dirent *dent;
454 unsigned char *table;
462 dirsp = (struct fs_dir_stream *)dirs;
464 /* Start by root inode */
465 table = sqfs_find_inode(dirs->inode_table, le32_to_cpu(sblk->inodes),
466 sblk->inodes, sblk->block_size);
468 dir = (struct squashfs_dir_inode *)table;
469 ldir = (struct squashfs_ldir_inode *)table;
471 /* get directory offset in directory table */
472 offset = sqfs_dir_offset(table, m_list, m_count);
473 dirs->table = &dirs->dir_table[offset];
475 /* Setup directory header */
476 dirs->dir_header = malloc(SQFS_DIR_HEADER_SIZE);
477 if (!dirs->dir_header)
480 memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE);
482 /* Initialize squashfs_dir_stream members */
483 dirs->table += SQFS_DIR_HEADER_SIZE;
484 dirs->size = get_unaligned_le16(&dir->file_size) - SQFS_DIR_HEADER_SIZE;
485 dirs->entry_count = dirs->dir_header->count + 1;
487 /* No path given -> root directory */
488 if (!strcmp(token_list[0], "/")) {
489 dirs->table = &dirs->dir_table[offset];
490 memcpy(&dirs->i_dir, dir, sizeof(*dir));
494 for (j = 0; j < token_count; j++) {
495 if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) {
496 printf("** Cannot find directory. **\n");
501 while (!sqfs_readdir(dirsp, &dent)) {
502 ret = strcmp(dent->name, token_list[j]);
510 printf("** Cannot find directory. **\n");
515 /* Redefine inode as the found token */
516 new_inode_number = dirs->entry->inode_offset +
517 dirs->dir_header->inode_number;
519 /* Get reference to inode in the inode table */
520 table = sqfs_find_inode(dirs->inode_table, new_inode_number,
521 sblk->inodes, sblk->block_size);
522 dir = (struct squashfs_dir_inode *)table;
524 /* Check for symbolic link and inode type sanity */
525 if (get_unaligned_le16(&dir->inode_type) == SQFS_SYMLINK_TYPE) {
526 sym = (struct squashfs_symlink_inode *)table;
527 /* Get first j + 1 tokens */
528 path = sqfs_concat_tokens(token_list, j + 1);
533 /* Resolve for these tokens */
534 target = sqfs_resolve_symlink(sym, path);
539 /* Join remaining tokens */
540 rem = sqfs_concat_tokens(token_list + j + 1, token_count -
546 /* Concatenate remaining tokens and symlink's target */
547 res = malloc(strlen(rem) + strlen(target) + 1);
553 res[strlen(target)] = '/';
554 strcpy(res + strlen(target) + 1, rem);
555 token_count = sqfs_count_tokens(res);
557 if (token_count < 0) {
562 sym_tokens = malloc(token_count * sizeof(char *));
568 /* Fill tokens list */
569 ret = sqfs_tokenize(sym_tokens, token_count, res);
577 ret = sqfs_search_dir(dirs, sym_tokens, token_count,
580 } else if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) {
581 printf("** Cannot find directory. **\n");
588 /* Check if it is an extended dir. */
589 if (get_unaligned_le16(&dir->inode_type) == SQFS_LDIR_TYPE)
590 ldir = (struct squashfs_ldir_inode *)table;
592 /* Get dir. offset into the directory table */
593 offset = sqfs_dir_offset(table, m_list, m_count);
594 dirs->table = &dirs->dir_table[offset];
596 /* Copy directory header */
597 memcpy(dirs->dir_header, &dirs->dir_table[offset],
598 SQFS_DIR_HEADER_SIZE);
600 /* Check for empty directory */
601 if (sqfs_is_empty_dir(table)) {
602 printf("Empty directory.\n");
605 ret = SQFS_EMPTY_DIR;
609 dirs->table += SQFS_DIR_HEADER_SIZE;
610 dirs->size = get_unaligned_le16(&dir->file_size);
611 dirs->entry_count = dirs->dir_header->count + 1;
612 dirs->size -= SQFS_DIR_HEADER_SIZE;
617 offset = sqfs_dir_offset(table, m_list, m_count);
618 dirs->table = &dirs->dir_table[offset];
620 if (get_unaligned_le16(&dir->inode_type) == SQFS_DIR_TYPE)
621 memcpy(&dirs->i_dir, dir, sizeof(*dir));
623 memcpy(&dirs->i_ldir, ldir, sizeof(*ldir));
635 * Inode and directory tables are stored as a series of metadata blocks, and
636 * given the compressed size of this table, we can calculate how much metadata
637 * blocks are needed to store the result of the decompression, since a
638 * decompressed metadata block should have a size of 8KiB.
640 static int sqfs_count_metablks(void *table, u32 offset, int table_size)
642 int count = 0, cur_size = 0, ret;
647 ret = sqfs_read_metablock(table, offset + cur_size, &comp,
651 cur_size += data_size + SQFS_HEADER_SIZE;
653 } while (cur_size < table_size);
659 * Storing the metadata blocks header's positions will be useful while looking
660 * for an entry in the directory table, using the reference (index and offset)
661 * given by its inode.
663 static int sqfs_get_metablk_pos(u32 *pos_list, void *table, u32 offset,
666 u32 data_size, cur_size = 0;
673 for (j = 0; j < metablks_count; j++) {
674 ret = sqfs_read_metablock(table, offset + cur_size, &comp,
679 cur_size += data_size + SQFS_HEADER_SIZE;
680 pos_list[j] = cur_size;
686 static int sqfs_read_inode_table(unsigned char **inode_table)
688 struct squashfs_super_block *sblk = ctxt.sblk;
689 u64 start, n_blks, table_offset, table_size;
690 int j, ret = 0, metablks_count;
691 unsigned char *src_table, *itb;
692 u32 src_len, dest_offset = 0;
693 unsigned long dest_len = 0;
696 table_size = get_unaligned_le64(&sblk->directory_table_start) -
697 get_unaligned_le64(&sblk->inode_table_start);
698 start = get_unaligned_le64(&sblk->inode_table_start) /
700 n_blks = sqfs_calc_n_blks(sblk->inode_table_start,
701 sblk->directory_table_start, &table_offset);
703 /* Allocate a proper sized buffer (itb) to store the inode table */
704 itb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
708 if (sqfs_disk_read(start, n_blks, itb) < 0) {
713 /* Parse inode table (metadata block) header */
714 ret = sqfs_read_metablock(itb, table_offset, &compressed, &src_len);
720 /* Calculate size to store the whole decompressed table */
721 metablks_count = sqfs_count_metablks(itb, table_offset, table_size);
722 if (metablks_count < 1) {
727 *inode_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE);
733 src_table = itb + table_offset + SQFS_HEADER_SIZE;
735 /* Extract compressed Inode table */
736 for (j = 0; j < metablks_count; j++) {
737 sqfs_read_metablock(itb, table_offset, &compressed, &src_len);
739 dest_len = SQFS_METADATA_BLOCK_SIZE;
740 ret = sqfs_decompress(&ctxt, *inode_table +
741 dest_offset, &dest_len,
749 dest_offset += dest_len;
751 memcpy(*inode_table + (j * SQFS_METADATA_BLOCK_SIZE),
756 * Offsets to the decompression destination, to the metadata
757 * buffer 'itb' and to the decompression source, respectively.
760 table_offset += src_len + SQFS_HEADER_SIZE;
761 src_table += src_len + SQFS_HEADER_SIZE;
770 static int sqfs_read_directory_table(unsigned char **dir_table, u32 **pos_list)
772 u64 start, n_blks, table_offset, table_size;
773 struct squashfs_super_block *sblk = ctxt.sblk;
774 int j, ret = 0, metablks_count = -1;
775 unsigned char *src_table, *dtb;
776 u32 src_len, dest_offset = 0;
777 unsigned long dest_len = 0;
782 /* DIRECTORY TABLE */
783 table_size = get_unaligned_le64(&sblk->fragment_table_start) -
784 get_unaligned_le64(&sblk->directory_table_start);
785 start = get_unaligned_le64(&sblk->directory_table_start) /
787 n_blks = sqfs_calc_n_blks(sblk->directory_table_start,
788 sblk->fragment_table_start, &table_offset);
790 /* Allocate a proper sized buffer (dtb) to store the directory table */
791 dtb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
795 if (sqfs_disk_read(start, n_blks, dtb) < 0)
798 /* Parse directory table (metadata block) header */
799 ret = sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
803 /* Calculate total size to store the whole decompressed table */
804 metablks_count = sqfs_count_metablks(dtb, table_offset, table_size);
805 if (metablks_count < 1)
808 *dir_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE);
812 *pos_list = malloc(metablks_count * sizeof(u32));
816 ret = sqfs_get_metablk_pos(*pos_list, dtb, table_offset,
823 src_table = dtb + table_offset + SQFS_HEADER_SIZE;
825 /* Extract compressed Directory table */
827 for (j = 0; j < metablks_count; j++) {
828 sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
830 dest_len = SQFS_METADATA_BLOCK_SIZE;
831 ret = sqfs_decompress(&ctxt, *dir_table +
832 (j * SQFS_METADATA_BLOCK_SIZE),
833 &dest_len, src_table, src_len);
839 if (dest_len < SQFS_METADATA_BLOCK_SIZE) {
840 dest_offset += dest_len;
844 dest_offset += dest_len;
846 memcpy(*dir_table + (j * SQFS_METADATA_BLOCK_SIZE),
851 * Offsets to the decompression destination, to the metadata
852 * buffer 'dtb' and to the decompression source, respectively.
854 table_offset += src_len + SQFS_HEADER_SIZE;
855 src_table += src_len + SQFS_HEADER_SIZE;
859 if (metablks_count < 1) {
867 return metablks_count;
870 int sqfs_opendir(const char *filename, struct fs_dir_stream **dirsp)
872 unsigned char *inode_table = NULL, *dir_table = NULL;
873 int j, token_count, ret = 0, metablks_count;
874 struct squashfs_dir_stream *dirs;
875 char **token_list, *path;
876 u32 *pos_list = NULL;
878 dirs = malloc(sizeof(*dirs));
882 /* these should be set to NULL to prevent dangling pointers */
883 dirs->dir_header = NULL;
886 dirs->inode_table = NULL;
887 dirs->dir_table = NULL;
889 ret = sqfs_read_inode_table(&inode_table);
895 metablks_count = sqfs_read_directory_table(&dir_table, &pos_list);
896 if (metablks_count < 1) {
898 goto free_inode_table;
901 /* Tokenize filename */
902 token_count = sqfs_count_tokens(filename);
903 if (token_count < 0) {
905 goto free_inode_table;
908 path = strdup(filename);
911 goto free_inode_table;
914 token_list = malloc(token_count * sizeof(char *));
920 /* Fill tokens list */
921 ret = sqfs_tokenize(token_list, token_count, path);
925 * ldir's (extended directory) size is greater than dir, so it works as
926 * a general solution for the malloc size, since 'i' is a union.
928 dirs->inode_table = inode_table;
929 dirs->dir_table = dir_table;
930 ret = sqfs_search_dir(dirs, token_list, token_count, pos_list,
935 if (le16_to_cpu(dirs->i_dir.inode_type) == SQFS_DIR_TYPE)
936 dirs->size = le16_to_cpu(dirs->i_dir.file_size);
938 dirs->size = le32_to_cpu(dirs->i_ldir.file_size);
940 /* Setup directory header */
941 memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE);
942 dirs->entry_count = dirs->dir_header->count + 1;
943 dirs->size -= SQFS_DIR_HEADER_SIZE;
947 dirs->table += SQFS_DIR_HEADER_SIZE;
949 *dirsp = (struct fs_dir_stream *)dirs;
952 for (j = 0; j < token_count; j++)
968 int sqfs_readdir(struct fs_dir_stream *fs_dirs, struct fs_dirent **dentp)
970 struct squashfs_super_block *sblk = ctxt.sblk;
971 struct squashfs_dir_stream *dirs;
972 struct squashfs_lreg_inode *lreg;
973 struct squashfs_base_inode *base;
974 struct squashfs_reg_inode *reg;
975 int i_number, offset = 0, ret;
976 struct fs_dirent *dent;
979 dirs = (struct squashfs_dir_stream *)fs_dirs;
982 return -SQFS_STOP_READDIR;
987 if (!dirs->entry_count) {
988 if (dirs->size > SQFS_DIR_HEADER_SIZE) {
989 dirs->size -= SQFS_DIR_HEADER_SIZE;
993 return -SQFS_STOP_READDIR;
996 if (dirs->size > SQFS_EMPTY_FILE_SIZE) {
997 /* Read follow-up (emitted) dir. header */
998 memcpy(dirs->dir_header, dirs->table,
999 SQFS_DIR_HEADER_SIZE);
1000 dirs->entry_count = dirs->dir_header->count + 1;
1001 ret = sqfs_read_entry(&dirs->entry, dirs->table +
1002 SQFS_DIR_HEADER_SIZE);
1004 return -SQFS_STOP_READDIR;
1006 dirs->table += SQFS_DIR_HEADER_SIZE;
1009 ret = sqfs_read_entry(&dirs->entry, dirs->table);
1011 return -SQFS_STOP_READDIR;
1014 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1015 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1018 base = (struct squashfs_base_inode *)ipos;
1020 /* Set entry type and size */
1021 switch (dirs->entry->type) {
1023 case SQFS_LDIR_TYPE:
1024 dent->type = FS_DT_DIR;
1027 case SQFS_LREG_TYPE:
1029 * Entries do not differentiate extended from regular types, so
1030 * it needs to be verified manually.
1032 if (get_unaligned_le16(&base->inode_type) == SQFS_LREG_TYPE) {
1033 lreg = (struct squashfs_lreg_inode *)ipos;
1034 dent->size = get_unaligned_le64(&lreg->file_size);
1036 reg = (struct squashfs_reg_inode *)ipos;
1037 dent->size = get_unaligned_le32(®->file_size);
1040 dent->type = FS_DT_REG;
1042 case SQFS_BLKDEV_TYPE:
1043 case SQFS_CHRDEV_TYPE:
1044 case SQFS_LBLKDEV_TYPE:
1045 case SQFS_LCHRDEV_TYPE:
1046 case SQFS_FIFO_TYPE:
1047 case SQFS_SOCKET_TYPE:
1048 case SQFS_LFIFO_TYPE:
1049 case SQFS_LSOCKET_TYPE:
1050 dent->type = SQFS_MISC_ENTRY_TYPE;
1052 case SQFS_SYMLINK_TYPE:
1053 case SQFS_LSYMLINK_TYPE:
1054 dent->type = FS_DT_LNK;
1057 return -SQFS_STOP_READDIR;
1060 /* Set entry name */
1061 strncpy(dent->name, dirs->entry->name, dirs->entry->name_size + 1);
1062 dent->name[dirs->entry->name_size + 1] = '\0';
1064 offset = dirs->entry->name_size + 1 + SQFS_ENTRY_BASE_LENGTH;
1065 dirs->entry_count--;
1067 /* Decrement size to be read */
1068 if (dirs->size > offset)
1069 dirs->size -= offset;
1073 /* Keep a reference to the current entry before incrementing it */
1074 dirs->table += offset;
1081 int sqfs_probe(struct blk_desc *fs_dev_desc, struct disk_partition *fs_partition)
1083 struct squashfs_super_block *sblk;
1086 ctxt.cur_dev = fs_dev_desc;
1087 ctxt.cur_part_info = *fs_partition;
1089 ret = sqfs_read_sblk(&sblk);
1093 /* Make sure it has a valid SquashFS magic number*/
1094 if (get_unaligned_le32(&sblk->s_magic) != SQFS_MAGIC_NUMBER) {
1095 printf("Bad magic number for SquashFS image.\n");
1096 ctxt.cur_dev = NULL;
1102 ret = sqfs_decompressor_init(&ctxt);
1105 ctxt.cur_dev = NULL;
1113 static char *sqfs_basename(char *path)
1117 fname = path + strlen(path) - 1;
1118 while (fname >= path) {
1119 if (*fname == '/') {
1130 static char *sqfs_dirname(char *path)
1134 fname = sqfs_basename(path);
1142 * Takes a path to file and splits it in two parts: the filename itself and the
1143 * directory's path, e.g.:
1144 * path: /path/to/file.txt
1148 static int sqfs_split_path(char **file, char **dir, const char *path)
1150 char *dirc, *basec, *bname, *dname, *tmp_path;
1161 /* check for first slash in path*/
1162 if (path[0] == '/') {
1163 tmp_path = strdup(path);
1169 tmp_path = malloc(strlen(path) + 2);
1175 strcpy(tmp_path + 1, path);
1178 /* String duplicates */
1179 dirc = strdup(tmp_path);
1185 basec = strdup(tmp_path);
1191 dname = sqfs_dirname(dirc);
1192 bname = sqfs_basename(basec);
1194 *file = strdup(bname);
1201 if (*dname == '\0') {
1211 *dir = strdup(dname);
1232 static int sqfs_get_regfile_info(struct squashfs_reg_inode *reg,
1233 struct squashfs_file_info *finfo,
1234 struct squashfs_fragment_block_entry *fentry,
1237 int datablk_count = 0, ret;
1239 finfo->size = get_unaligned_le32(®->file_size);
1240 finfo->offset = get_unaligned_le32(®->offset);
1241 finfo->start = get_unaligned_le32(®->start_block);
1242 finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(®->fragment));
1244 if (finfo->frag && finfo->offset == 0xFFFFFFFF)
1247 if (finfo->size < 1 || finfo->start == 0xFFFFFFFF)
1251 datablk_count = finfo->size / le32_to_cpu(blksz);
1252 ret = sqfs_frag_lookup(get_unaligned_le32(®->fragment),
1257 if (fentry->size < 1 || fentry->start == 0x7FFFFFFF)
1260 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1263 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1264 if (!finfo->blk_sizes)
1267 return datablk_count;
1270 static int sqfs_get_lregfile_info(struct squashfs_lreg_inode *lreg,
1271 struct squashfs_file_info *finfo,
1272 struct squashfs_fragment_block_entry *fentry,
1275 int datablk_count = 0, ret;
1277 finfo->size = get_unaligned_le64(&lreg->file_size);
1278 finfo->offset = get_unaligned_le32(&lreg->offset);
1279 finfo->start = get_unaligned_le64(&lreg->start_block);
1280 finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(&lreg->fragment));
1282 if (finfo->frag && finfo->offset == 0xFFFFFFFF)
1285 if (finfo->size < 1 || finfo->start == 0x7FFFFFFF)
1289 datablk_count = finfo->size / le32_to_cpu(blksz);
1290 ret = sqfs_frag_lookup(get_unaligned_le32(&lreg->fragment),
1295 if (fentry->size < 1 || fentry->start == 0x7FFFFFFF)
1298 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1301 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1302 if (!finfo->blk_sizes)
1305 return datablk_count;
1308 int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
1311 char *dir, *fragment_block, *datablock = NULL, *data_buffer = NULL;
1312 char *fragment, *file, *resolved, *data;
1313 u64 start, n_blks, table_size, data_offset, table_offset;
1314 int ret, j, i_number, datablk_count = 0;
1315 struct squashfs_super_block *sblk = ctxt.sblk;
1316 struct squashfs_fragment_block_entry frag_entry;
1317 struct squashfs_file_info finfo = {0};
1318 struct squashfs_symlink_inode *symlink;
1319 struct fs_dir_stream *dirsp = NULL;
1320 struct squashfs_dir_stream *dirs;
1321 struct squashfs_lreg_inode *lreg;
1322 struct squashfs_base_inode *base;
1323 struct squashfs_reg_inode *reg;
1324 unsigned long dest_len;
1325 struct fs_dirent *dent;
1326 unsigned char *ipos;
1331 * sqfs_opendir will uncompress inode and directory tables, and will
1332 * return a pointer to the directory that contains the requested file.
1334 sqfs_split_path(&file, &dir, filename);
1335 ret = sqfs_opendir(dir, &dirsp);
1340 dirs = (struct squashfs_dir_stream *)dirsp;
1342 /* For now, only regular files are able to be loaded */
1343 while (!sqfs_readdir(dirsp, &dent)) {
1344 ret = strcmp(dent->name, file);
1353 printf("File not found.\n");
1359 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1360 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1363 base = (struct squashfs_base_inode *)ipos;
1364 switch (get_unaligned_le16(&base->inode_type)) {
1366 reg = (struct squashfs_reg_inode *)ipos;
1367 datablk_count = sqfs_get_regfile_info(reg, &finfo, &frag_entry,
1369 if (datablk_count < 0) {
1374 memcpy(finfo.blk_sizes, ipos + sizeof(*reg),
1375 datablk_count * sizeof(u32));
1377 case SQFS_LREG_TYPE:
1378 lreg = (struct squashfs_lreg_inode *)ipos;
1379 datablk_count = sqfs_get_lregfile_info(lreg, &finfo,
1382 if (datablk_count < 0) {
1387 memcpy(finfo.blk_sizes, ipos + sizeof(*lreg),
1388 datablk_count * sizeof(u32));
1390 case SQFS_SYMLINK_TYPE:
1391 case SQFS_LSYMLINK_TYPE:
1392 symlink = (struct squashfs_symlink_inode *)ipos;
1393 resolved = sqfs_resolve_symlink(symlink, filename);
1394 ret = sqfs_read(resolved, buf, offset, len, actread);
1397 case SQFS_BLKDEV_TYPE:
1398 case SQFS_CHRDEV_TYPE:
1399 case SQFS_LBLKDEV_TYPE:
1400 case SQFS_LCHRDEV_TYPE:
1401 case SQFS_FIFO_TYPE:
1402 case SQFS_SOCKET_TYPE:
1403 case SQFS_LFIFO_TYPE:
1404 case SQFS_LSOCKET_TYPE:
1406 printf("Unsupported entry type\n");
1411 /* If the user specifies a length, check its sanity */
1413 if (len > finfo.size) {
1421 if (datablk_count) {
1422 data_offset = finfo.start;
1423 datablock = malloc(get_unaligned_le32(&sblk->block_size));
1430 for (j = 0; j < datablk_count; j++) {
1431 start = data_offset / ctxt.cur_dev->blksz;
1432 table_size = SQFS_BLOCK_SIZE(finfo.blk_sizes[j]);
1433 table_offset = data_offset - (start * ctxt.cur_dev->blksz);
1434 n_blks = DIV_ROUND_UP(table_size + table_offset,
1435 ctxt.cur_dev->blksz);
1437 data_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
1444 ret = sqfs_disk_read(start, n_blks, data_buffer);
1447 * Possible causes: too many data blocks or too large
1448 * SquashFS block size. Tip: re-compile the SquashFS
1449 * image with mksquashfs's -b <block_size> option.
1451 printf("Error: too many data blocks to be read.\n");
1455 data = data_buffer + table_offset;
1458 if (SQFS_COMPRESSED_BLOCK(finfo.blk_sizes[j])) {
1459 dest_len = get_unaligned_le32(&sblk->block_size);
1460 ret = sqfs_decompress(&ctxt, datablock, &dest_len,
1465 memcpy(buf + offset + *actread, datablock, dest_len);
1466 *actread += dest_len;
1468 memcpy(buf + offset + *actread, data, table_size);
1469 *actread += table_size;
1472 data_offset += table_size;
1477 free(finfo.blk_sizes);
1480 * There is no need to continue if the file is not fragmented.
1487 start = frag_entry.start / ctxt.cur_dev->blksz;
1488 table_size = SQFS_BLOCK_SIZE(frag_entry.size);
1489 table_offset = frag_entry.start - (start * ctxt.cur_dev->blksz);
1490 n_blks = DIV_ROUND_UP(table_size + table_offset, ctxt.cur_dev->blksz);
1492 fragment = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
1499 ret = sqfs_disk_read(start, n_blks, fragment);
1503 /* File compressed and fragmented */
1504 if (finfo.frag && finfo.comp) {
1505 dest_len = get_unaligned_le32(&sblk->block_size);
1506 fragment_block = malloc(dest_len);
1507 if (!fragment_block) {
1512 ret = sqfs_decompress(&ctxt, fragment_block, &dest_len,
1513 (void *)fragment + table_offset,
1516 free(fragment_block);
1520 for (j = offset + *actread; j < finfo.size; j++) {
1521 memcpy(buf + j, &fragment_block[finfo.offset + j], 1);
1525 free(fragment_block);
1527 } else if (finfo.frag && !finfo.comp) {
1528 fragment_block = (void *)fragment + table_offset;
1530 for (j = offset + *actread; j < finfo.size; j++) {
1531 memcpy(buf + j, &fragment_block[finfo.offset + j], 1);
1547 sqfs_closedir(dirsp);
1552 int sqfs_size(const char *filename, loff_t *size)
1554 struct squashfs_super_block *sblk = ctxt.sblk;
1555 struct squashfs_symlink_inode *symlink;
1556 struct fs_dir_stream *dirsp = NULL;
1557 struct squashfs_base_inode *base;
1558 struct squashfs_dir_stream *dirs;
1559 struct squashfs_lreg_inode *lreg;
1560 struct squashfs_reg_inode *reg;
1561 char *dir, *file, *resolved;
1562 struct fs_dirent *dent;
1563 unsigned char *ipos;
1566 sqfs_split_path(&file, &dir, filename);
1568 * sqfs_opendir will uncompress inode and directory tables, and will
1569 * return a pointer to the directory that contains the requested file.
1571 ret = sqfs_opendir(dir, &dirsp);
1577 dirs = (struct squashfs_dir_stream *)dirsp;
1579 while (!sqfs_readdir(dirsp, &dent)) {
1580 ret = strcmp(dent->name, file);
1588 printf("File not found.\n");
1594 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1595 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1600 base = (struct squashfs_base_inode *)ipos;
1601 switch (get_unaligned_le16(&base->inode_type)) {
1603 reg = (struct squashfs_reg_inode *)ipos;
1604 *size = get_unaligned_le32(®->file_size);
1606 case SQFS_LREG_TYPE:
1607 lreg = (struct squashfs_lreg_inode *)ipos;
1608 *size = get_unaligned_le64(&lreg->file_size);
1610 case SQFS_SYMLINK_TYPE:
1611 case SQFS_LSYMLINK_TYPE:
1612 symlink = (struct squashfs_symlink_inode *)ipos;
1613 resolved = sqfs_resolve_symlink(symlink, filename);
1614 ret = sqfs_size(resolved, size);
1617 case SQFS_BLKDEV_TYPE:
1618 case SQFS_CHRDEV_TYPE:
1619 case SQFS_LBLKDEV_TYPE:
1620 case SQFS_LCHRDEV_TYPE:
1621 case SQFS_FIFO_TYPE:
1622 case SQFS_SOCKET_TYPE:
1623 case SQFS_LFIFO_TYPE:
1624 case SQFS_LSOCKET_TYPE:
1626 printf("Unable to recover entry's size.\n");
1636 sqfs_closedir(dirsp);
1641 void sqfs_close(void)
1644 ctxt.cur_dev = NULL;
1645 sqfs_decompressor_cleanup(&ctxt);
1648 void sqfs_closedir(struct fs_dir_stream *dirs)
1650 struct squashfs_dir_stream *sqfs_dirs;
1652 sqfs_dirs = (struct squashfs_dir_stream *)dirs;
1653 free(sqfs_dirs->inode_table);
1654 free(sqfs_dirs->dir_table);
1655 free(sqfs_dirs->dir_header);