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;
343 /* Memory allocation for the token lists */
344 bc = sqfs_count_tokens(base);
345 rc = sqfs_count_tokens(rel);
346 if (bc < 1 || rc < 1)
349 base_tokens = malloc(bc * sizeof(char *));
353 rel_tokens = malloc(rc * sizeof(char *));
357 /* Fill token lists */
358 ret = sqfs_tokenize(base_tokens, bc, base);
362 sqfs_tokenize(rel_tokens, rc, rel);
366 /* count '..' occurrences in target path */
367 for (i = 0; i < rc; i++) {
368 if (!strcmp(rel_tokens[i], ".."))
372 /* Remove the last token and the '..' occurrences */
373 bc = sqfs_clean_base_path(base_tokens, bc, updir);
377 /* Calculate resolved path size */
381 resolved_size += sqfs_get_tokens_length(base_tokens, bc) +
382 sqfs_get_tokens_length(rel_tokens, rc);
384 resolved = malloc(resolved_size + 1);
386 goto free_r_tokens_loop;
388 /* Set resolved path */
389 memset(resolved, '\0', resolved_size + 1);
390 offset += sqfs_join(base_tokens, resolved + offset, 0, bc, '/');
391 resolved[offset++] = '/';
392 offset += sqfs_join(rel_tokens, resolved + offset, updir, rc, '/');
395 for (i = 0; i < rc; i++)
397 for (i = 0; i < bc; i++)
398 free(base_tokens[i]);
407 static char *sqfs_resolve_symlink(struct squashfs_symlink_inode *sym,
408 const char *base_path)
410 char *resolved, *target;
413 sz = get_unaligned_le32(&sym->symlink_size);
414 target = malloc(sz + 1);
419 * There is no trailling null byte in the symlink's target path, so a
420 * copy is made and a '\0' is added at its end.
423 /* Get target name (relative path) */
424 strncpy(target, sym->symlink, sz);
426 /* Relative -> absolute path conversion */
427 resolved = sqfs_get_abs_path(base_path, target);
435 * m_list contains each metadata block's position, and m_count is the number of
436 * elements of m_list. Those metadata blocks come from the compressed directory
439 static int sqfs_search_dir(struct squashfs_dir_stream *dirs, char **token_list,
440 int token_count, u32 *m_list, int m_count)
442 struct squashfs_super_block *sblk = ctxt.sblk;
443 char *path, *target, **sym_tokens, *res, *rem;
444 int j, ret = 0, new_inode_number, offset;
445 struct squashfs_symlink_inode *sym;
446 struct squashfs_ldir_inode *ldir;
447 struct squashfs_dir_inode *dir;
448 struct fs_dir_stream *dirsp;
449 struct fs_dirent *dent;
450 unsigned char *table;
458 dirsp = (struct fs_dir_stream *)dirs;
460 /* Start by root inode */
461 table = sqfs_find_inode(dirs->inode_table, le32_to_cpu(sblk->inodes),
462 sblk->inodes, sblk->block_size);
464 dir = (struct squashfs_dir_inode *)table;
465 ldir = (struct squashfs_ldir_inode *)table;
467 /* get directory offset in directory table */
468 offset = sqfs_dir_offset(table, m_list, m_count);
469 dirs->table = &dirs->dir_table[offset];
471 /* Setup directory header */
472 dirs->dir_header = malloc(SQFS_DIR_HEADER_SIZE);
473 if (!dirs->dir_header)
476 memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE);
478 /* Initialize squashfs_dir_stream members */
479 dirs->table += SQFS_DIR_HEADER_SIZE;
480 dirs->size = get_unaligned_le16(&dir->file_size) - SQFS_DIR_HEADER_SIZE;
481 dirs->entry_count = dirs->dir_header->count + 1;
483 /* No path given -> root directory */
484 if (!strcmp(token_list[0], "/")) {
485 dirs->table = &dirs->dir_table[offset];
486 memcpy(&dirs->i_dir, dir, sizeof(*dir));
490 for (j = 0; j < token_count; j++) {
491 if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) {
492 printf("** Cannot find directory. **\n");
497 while (!sqfs_readdir(dirsp, &dent)) {
498 ret = strcmp(dent->name, token_list[j]);
506 printf("** Cannot find directory. **\n");
511 /* Redefine inode as the found token */
512 new_inode_number = dirs->entry->inode_offset +
513 dirs->dir_header->inode_number;
515 /* Get reference to inode in the inode table */
516 table = sqfs_find_inode(dirs->inode_table, new_inode_number,
517 sblk->inodes, sblk->block_size);
518 dir = (struct squashfs_dir_inode *)table;
520 /* Check for symbolic link and inode type sanity */
521 if (get_unaligned_le16(&dir->inode_type) == SQFS_SYMLINK_TYPE) {
522 sym = (struct squashfs_symlink_inode *)table;
523 /* Get first j + 1 tokens */
524 path = sqfs_concat_tokens(token_list, j + 1);
529 /* Resolve for these tokens */
530 target = sqfs_resolve_symlink(sym, path);
535 /* Join remaining tokens */
536 rem = sqfs_concat_tokens(token_list + j + 1, token_count -
542 /* Concatenate remaining tokens and symlink's target */
543 res = malloc(strlen(rem) + strlen(target) + 1);
549 res[strlen(target)] = '/';
550 strcpy(res + strlen(target) + 1, rem);
551 token_count = sqfs_count_tokens(res);
553 if (token_count < 0) {
558 sym_tokens = malloc(token_count * sizeof(char *));
564 /* Fill tokens list */
565 ret = sqfs_tokenize(sym_tokens, token_count, res);
573 ret = sqfs_search_dir(dirs, sym_tokens, token_count,
576 } else if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) {
577 printf("** Cannot find directory. **\n");
584 /* Check if it is an extended dir. */
585 if (get_unaligned_le16(&dir->inode_type) == SQFS_LDIR_TYPE)
586 ldir = (struct squashfs_ldir_inode *)table;
588 /* Get dir. offset into the directory table */
589 offset = sqfs_dir_offset(table, m_list, m_count);
590 dirs->table = &dirs->dir_table[offset];
592 /* Copy directory header */
593 memcpy(dirs->dir_header, &dirs->dir_table[offset],
594 SQFS_DIR_HEADER_SIZE);
596 /* Check for empty directory */
597 if (sqfs_is_empty_dir(table)) {
598 printf("Empty directory.\n");
601 ret = SQFS_EMPTY_DIR;
605 dirs->table += SQFS_DIR_HEADER_SIZE;
606 dirs->size = get_unaligned_le16(&dir->file_size);
607 dirs->entry_count = dirs->dir_header->count + 1;
608 dirs->size -= SQFS_DIR_HEADER_SIZE;
613 offset = sqfs_dir_offset(table, m_list, m_count);
614 dirs->table = &dirs->dir_table[offset];
616 if (get_unaligned_le16(&dir->inode_type) == SQFS_DIR_TYPE)
617 memcpy(&dirs->i_dir, dir, sizeof(*dir));
619 memcpy(&dirs->i_ldir, ldir, sizeof(*ldir));
631 * Inode and directory tables are stored as a series of metadata blocks, and
632 * given the compressed size of this table, we can calculate how much metadata
633 * blocks are needed to store the result of the decompression, since a
634 * decompressed metadata block should have a size of 8KiB.
636 static int sqfs_count_metablks(void *table, u32 offset, int table_size)
638 int count = 0, cur_size = 0, ret;
643 ret = sqfs_read_metablock(table, offset + cur_size, &comp,
647 cur_size += data_size + SQFS_HEADER_SIZE;
649 } while (cur_size < table_size);
655 * Storing the metadata blocks header's positions will be useful while looking
656 * for an entry in the directory table, using the reference (index and offset)
657 * given by its inode.
659 static int sqfs_get_metablk_pos(u32 *pos_list, void *table, u32 offset,
662 u32 data_size, cur_size = 0;
669 for (j = 0; j < metablks_count; j++) {
670 ret = sqfs_read_metablock(table, offset + cur_size, &comp,
675 cur_size += data_size + SQFS_HEADER_SIZE;
676 pos_list[j] = cur_size;
682 static int sqfs_read_inode_table(unsigned char **inode_table)
684 struct squashfs_super_block *sblk = ctxt.sblk;
685 u64 start, n_blks, table_offset, table_size;
686 int j, ret = 0, metablks_count;
687 unsigned char *src_table, *itb;
688 u32 src_len, dest_offset = 0;
689 unsigned long dest_len = 0;
692 table_size = get_unaligned_le64(&sblk->directory_table_start) -
693 get_unaligned_le64(&sblk->inode_table_start);
694 start = get_unaligned_le64(&sblk->inode_table_start) /
696 n_blks = sqfs_calc_n_blks(sblk->inode_table_start,
697 sblk->directory_table_start, &table_offset);
699 /* Allocate a proper sized buffer (itb) to store the inode table */
700 itb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
704 if (sqfs_disk_read(start, n_blks, itb) < 0) {
709 /* Parse inode table (metadata block) header */
710 ret = sqfs_read_metablock(itb, table_offset, &compressed, &src_len);
716 /* Calculate size to store the whole decompressed table */
717 metablks_count = sqfs_count_metablks(itb, table_offset, table_size);
718 if (metablks_count < 1) {
723 *inode_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE);
729 src_table = itb + table_offset + SQFS_HEADER_SIZE;
731 /* Extract compressed Inode table */
732 for (j = 0; j < metablks_count; j++) {
733 sqfs_read_metablock(itb, table_offset, &compressed, &src_len);
735 dest_len = SQFS_METADATA_BLOCK_SIZE;
736 ret = sqfs_decompress(&ctxt, *inode_table +
737 dest_offset, &dest_len,
745 dest_offset += dest_len;
747 memcpy(*inode_table + (j * SQFS_METADATA_BLOCK_SIZE),
752 * Offsets to the decompression destination, to the metadata
753 * buffer 'itb' and to the decompression source, respectively.
756 table_offset += src_len + SQFS_HEADER_SIZE;
757 src_table += src_len + SQFS_HEADER_SIZE;
766 static int sqfs_read_directory_table(unsigned char **dir_table, u32 **pos_list)
768 u64 start, n_blks, table_offset, table_size;
769 struct squashfs_super_block *sblk = ctxt.sblk;
770 int j, ret = 0, metablks_count = -1;
771 unsigned char *src_table, *dtb;
772 u32 src_len, dest_offset = 0;
773 unsigned long dest_len = 0;
778 /* DIRECTORY TABLE */
779 table_size = get_unaligned_le64(&sblk->fragment_table_start) -
780 get_unaligned_le64(&sblk->directory_table_start);
781 start = get_unaligned_le64(&sblk->directory_table_start) /
783 n_blks = sqfs_calc_n_blks(sblk->directory_table_start,
784 sblk->fragment_table_start, &table_offset);
786 /* Allocate a proper sized buffer (dtb) to store the directory table */
787 dtb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
791 if (sqfs_disk_read(start, n_blks, dtb) < 0)
794 /* Parse directory table (metadata block) header */
795 ret = sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
799 /* Calculate total size to store the whole decompressed table */
800 metablks_count = sqfs_count_metablks(dtb, table_offset, table_size);
801 if (metablks_count < 1)
804 *dir_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE);
808 *pos_list = malloc(metablks_count * sizeof(u32));
812 ret = sqfs_get_metablk_pos(*pos_list, dtb, table_offset,
819 src_table = dtb + table_offset + SQFS_HEADER_SIZE;
821 /* Extract compressed Directory table */
823 for (j = 0; j < metablks_count; j++) {
824 sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
826 dest_len = SQFS_METADATA_BLOCK_SIZE;
827 ret = sqfs_decompress(&ctxt, *dir_table +
828 (j * SQFS_METADATA_BLOCK_SIZE),
829 &dest_len, src_table, src_len);
835 if (dest_len < SQFS_METADATA_BLOCK_SIZE) {
836 dest_offset += dest_len;
840 dest_offset += dest_len;
842 memcpy(*dir_table + (j * SQFS_METADATA_BLOCK_SIZE),
847 * Offsets to the decompression destination, to the metadata
848 * buffer 'dtb' and to the decompression source, respectively.
850 table_offset += src_len + SQFS_HEADER_SIZE;
851 src_table += src_len + SQFS_HEADER_SIZE;
855 if (metablks_count < 1) {
863 return metablks_count;
866 int sqfs_opendir(const char *filename, struct fs_dir_stream **dirsp)
868 unsigned char *inode_table = NULL, *dir_table = NULL;
869 int j, token_count, ret = 0, metablks_count;
870 struct squashfs_dir_stream *dirs;
871 char **token_list, *path;
872 u32 *pos_list = NULL;
874 dirs = malloc(sizeof(*dirs));
878 /* these should be set to NULL to prevent dangling pointers */
879 dirs->dir_header = NULL;
882 dirs->inode_table = NULL;
883 dirs->dir_table = NULL;
885 ret = sqfs_read_inode_table(&inode_table);
891 metablks_count = sqfs_read_directory_table(&dir_table, &pos_list);
892 if (metablks_count < 1) {
894 goto free_inode_table;
897 /* Tokenize filename */
898 token_count = sqfs_count_tokens(filename);
899 if (token_count < 0) {
901 goto free_inode_table;
904 path = strdup(filename);
907 goto free_inode_table;
910 token_list = malloc(token_count * sizeof(char *));
916 /* Fill tokens list */
917 ret = sqfs_tokenize(token_list, token_count, path);
921 * ldir's (extended directory) size is greater than dir, so it works as
922 * a general solution for the malloc size, since 'i' is a union.
924 dirs->inode_table = inode_table;
925 dirs->dir_table = dir_table;
926 ret = sqfs_search_dir(dirs, token_list, token_count, pos_list,
931 if (le16_to_cpu(dirs->i_dir.inode_type) == SQFS_DIR_TYPE)
932 dirs->size = le16_to_cpu(dirs->i_dir.file_size);
934 dirs->size = le32_to_cpu(dirs->i_ldir.file_size);
936 /* Setup directory header */
937 memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE);
938 dirs->entry_count = dirs->dir_header->count + 1;
939 dirs->size -= SQFS_DIR_HEADER_SIZE;
943 dirs->table += SQFS_DIR_HEADER_SIZE;
945 *dirsp = (struct fs_dir_stream *)dirs;
948 for (j = 0; j < token_count; j++)
964 int sqfs_readdir(struct fs_dir_stream *fs_dirs, struct fs_dirent **dentp)
966 struct squashfs_super_block *sblk = ctxt.sblk;
967 struct squashfs_dir_stream *dirs;
968 struct squashfs_lreg_inode *lreg;
969 struct squashfs_base_inode *base;
970 struct squashfs_reg_inode *reg;
971 int i_number, offset = 0, ret;
972 struct fs_dirent *dent;
975 dirs = (struct squashfs_dir_stream *)fs_dirs;
978 return -SQFS_STOP_READDIR;
983 if (!dirs->entry_count) {
984 if (dirs->size > SQFS_DIR_HEADER_SIZE) {
985 dirs->size -= SQFS_DIR_HEADER_SIZE;
989 return -SQFS_STOP_READDIR;
992 if (dirs->size > SQFS_EMPTY_FILE_SIZE) {
993 /* Read follow-up (emitted) dir. header */
994 memcpy(dirs->dir_header, dirs->table,
995 SQFS_DIR_HEADER_SIZE);
996 dirs->entry_count = dirs->dir_header->count + 1;
997 ret = sqfs_read_entry(&dirs->entry, dirs->table +
998 SQFS_DIR_HEADER_SIZE);
1000 return -SQFS_STOP_READDIR;
1002 dirs->table += SQFS_DIR_HEADER_SIZE;
1005 ret = sqfs_read_entry(&dirs->entry, dirs->table);
1007 return -SQFS_STOP_READDIR;
1010 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1011 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1014 base = (struct squashfs_base_inode *)ipos;
1016 /* Set entry type and size */
1017 switch (dirs->entry->type) {
1019 case SQFS_LDIR_TYPE:
1020 dent->type = FS_DT_DIR;
1023 case SQFS_LREG_TYPE:
1025 * Entries do not differentiate extended from regular types, so
1026 * it needs to be verified manually.
1028 if (get_unaligned_le16(&base->inode_type) == SQFS_LREG_TYPE) {
1029 lreg = (struct squashfs_lreg_inode *)ipos;
1030 dent->size = get_unaligned_le64(&lreg->file_size);
1032 reg = (struct squashfs_reg_inode *)ipos;
1033 dent->size = get_unaligned_le32(®->file_size);
1036 dent->type = FS_DT_REG;
1038 case SQFS_BLKDEV_TYPE:
1039 case SQFS_CHRDEV_TYPE:
1040 case SQFS_LBLKDEV_TYPE:
1041 case SQFS_LCHRDEV_TYPE:
1042 case SQFS_FIFO_TYPE:
1043 case SQFS_SOCKET_TYPE:
1044 case SQFS_LFIFO_TYPE:
1045 case SQFS_LSOCKET_TYPE:
1046 dent->type = SQFS_MISC_ENTRY_TYPE;
1048 case SQFS_SYMLINK_TYPE:
1049 case SQFS_LSYMLINK_TYPE:
1050 dent->type = FS_DT_LNK;
1053 return -SQFS_STOP_READDIR;
1056 /* Set entry name */
1057 strncpy(dent->name, dirs->entry->name, dirs->entry->name_size + 1);
1058 dent->name[dirs->entry->name_size + 1] = '\0';
1060 offset = dirs->entry->name_size + 1 + SQFS_ENTRY_BASE_LENGTH;
1061 dirs->entry_count--;
1063 /* Decrement size to be read */
1064 if (dirs->size > offset)
1065 dirs->size -= offset;
1069 /* Keep a reference to the current entry before incrementing it */
1070 dirs->table += offset;
1077 int sqfs_probe(struct blk_desc *fs_dev_desc, struct disk_partition *fs_partition)
1079 struct squashfs_super_block *sblk;
1082 ctxt.cur_dev = fs_dev_desc;
1083 ctxt.cur_part_info = *fs_partition;
1085 ret = sqfs_read_sblk(&sblk);
1089 /* Make sure it has a valid SquashFS magic number*/
1090 if (get_unaligned_le32(&sblk->s_magic) != SQFS_MAGIC_NUMBER) {
1091 printf("Bad magic number for SquashFS image.\n");
1092 ctxt.cur_dev = NULL;
1098 ret = sqfs_decompressor_init(&ctxt);
1101 ctxt.cur_dev = NULL;
1109 static char *sqfs_basename(char *path)
1113 fname = path + strlen(path) - 1;
1114 while (fname >= path) {
1115 if (*fname == '/') {
1126 static char *sqfs_dirname(char *path)
1130 fname = sqfs_basename(path);
1138 * Takes a path to file and splits it in two parts: the filename itself and the
1139 * directory's path, e.g.:
1140 * path: /path/to/file.txt
1144 static int sqfs_split_path(char **file, char **dir, const char *path)
1146 char *dirc, *basec, *bname, *dname, *tmp_path;
1157 /* check for first slash in path*/
1158 if (path[0] == '/') {
1159 tmp_path = strdup(path);
1165 tmp_path = malloc(strlen(path) + 2);
1171 strcpy(tmp_path + 1, path);
1174 /* String duplicates */
1175 dirc = strdup(tmp_path);
1181 basec = strdup(tmp_path);
1187 dname = sqfs_dirname(dirc);
1188 bname = sqfs_basename(basec);
1190 *file = strdup(bname);
1197 if (*dname == '\0') {
1207 *dir = strdup(dname);
1228 static int sqfs_get_regfile_info(struct squashfs_reg_inode *reg,
1229 struct squashfs_file_info *finfo,
1230 struct squashfs_fragment_block_entry *fentry,
1233 int datablk_count = 0, ret;
1235 finfo->size = get_unaligned_le32(®->file_size);
1236 finfo->offset = get_unaligned_le32(®->offset);
1237 finfo->start = get_unaligned_le32(®->start_block);
1238 finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(®->fragment));
1240 if (finfo->frag && finfo->offset == 0xFFFFFFFF)
1243 if (finfo->size < 1 || finfo->start == 0xFFFFFFFF)
1247 datablk_count = finfo->size / le32_to_cpu(blksz);
1248 ret = sqfs_frag_lookup(get_unaligned_le32(®->fragment),
1253 if (fentry->size < 1 || fentry->start == 0x7FFFFFFF)
1256 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1259 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1260 if (!finfo->blk_sizes)
1263 return datablk_count;
1266 static int sqfs_get_lregfile_info(struct squashfs_lreg_inode *lreg,
1267 struct squashfs_file_info *finfo,
1268 struct squashfs_fragment_block_entry *fentry,
1271 int datablk_count = 0, ret;
1273 finfo->size = get_unaligned_le64(&lreg->file_size);
1274 finfo->offset = get_unaligned_le32(&lreg->offset);
1275 finfo->start = get_unaligned_le64(&lreg->start_block);
1276 finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(&lreg->fragment));
1278 if (finfo->frag && finfo->offset == 0xFFFFFFFF)
1281 if (finfo->size < 1 || finfo->start == 0x7FFFFFFF)
1285 datablk_count = finfo->size / le32_to_cpu(blksz);
1286 ret = sqfs_frag_lookup(get_unaligned_le32(&lreg->fragment),
1291 if (fentry->size < 1 || fentry->start == 0x7FFFFFFF)
1294 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1297 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1298 if (!finfo->blk_sizes)
1301 return datablk_count;
1304 int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
1307 char *dir, *fragment_block, *datablock = NULL, *data_buffer = NULL;
1308 char *fragment, *file, *resolved, *data;
1309 u64 start, n_blks, table_size, data_offset, table_offset;
1310 int ret, j, i_number, datablk_count = 0;
1311 struct squashfs_super_block *sblk = ctxt.sblk;
1312 struct squashfs_fragment_block_entry frag_entry;
1313 struct squashfs_file_info finfo = {0};
1314 struct squashfs_symlink_inode *symlink;
1315 struct fs_dir_stream *dirsp = NULL;
1316 struct squashfs_dir_stream *dirs;
1317 struct squashfs_lreg_inode *lreg;
1318 struct squashfs_base_inode *base;
1319 struct squashfs_reg_inode *reg;
1320 unsigned long dest_len;
1321 struct fs_dirent *dent;
1322 unsigned char *ipos;
1327 * sqfs_opendir will uncompress inode and directory tables, and will
1328 * return a pointer to the directory that contains the requested file.
1330 sqfs_split_path(&file, &dir, filename);
1331 ret = sqfs_opendir(dir, &dirsp);
1336 dirs = (struct squashfs_dir_stream *)dirsp;
1338 /* For now, only regular files are able to be loaded */
1339 while (!sqfs_readdir(dirsp, &dent)) {
1340 ret = strcmp(dent->name, file);
1349 printf("File not found.\n");
1355 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1356 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1359 base = (struct squashfs_base_inode *)ipos;
1360 switch (get_unaligned_le16(&base->inode_type)) {
1362 reg = (struct squashfs_reg_inode *)ipos;
1363 datablk_count = sqfs_get_regfile_info(reg, &finfo, &frag_entry,
1365 if (datablk_count < 0) {
1370 memcpy(finfo.blk_sizes, ipos + sizeof(*reg),
1371 datablk_count * sizeof(u32));
1373 case SQFS_LREG_TYPE:
1374 lreg = (struct squashfs_lreg_inode *)ipos;
1375 datablk_count = sqfs_get_lregfile_info(lreg, &finfo,
1378 if (datablk_count < 0) {
1383 memcpy(finfo.blk_sizes, ipos + sizeof(*lreg),
1384 datablk_count * sizeof(u32));
1386 case SQFS_SYMLINK_TYPE:
1387 case SQFS_LSYMLINK_TYPE:
1388 symlink = (struct squashfs_symlink_inode *)ipos;
1389 resolved = sqfs_resolve_symlink(symlink, filename);
1390 ret = sqfs_read(resolved, buf, offset, len, actread);
1393 case SQFS_BLKDEV_TYPE:
1394 case SQFS_CHRDEV_TYPE:
1395 case SQFS_LBLKDEV_TYPE:
1396 case SQFS_LCHRDEV_TYPE:
1397 case SQFS_FIFO_TYPE:
1398 case SQFS_SOCKET_TYPE:
1399 case SQFS_LFIFO_TYPE:
1400 case SQFS_LSOCKET_TYPE:
1402 printf("Unsupported entry type\n");
1407 /* If the user specifies a length, check its sanity */
1409 if (len > finfo.size) {
1417 if (datablk_count) {
1418 data_offset = finfo.start;
1419 datablock = malloc(get_unaligned_le32(&sblk->block_size));
1426 for (j = 0; j < datablk_count; j++) {
1427 start = data_offset / ctxt.cur_dev->blksz;
1428 table_size = SQFS_BLOCK_SIZE(finfo.blk_sizes[j]);
1429 table_offset = data_offset - (start * ctxt.cur_dev->blksz);
1430 n_blks = DIV_ROUND_UP(table_size + table_offset,
1431 ctxt.cur_dev->blksz);
1433 data_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
1440 ret = sqfs_disk_read(start, n_blks, data_buffer);
1443 * Possible causes: too many data blocks or too large
1444 * SquashFS block size. Tip: re-compile the SquashFS
1445 * image with mksquashfs's -b <block_size> option.
1447 printf("Error: too many data blocks to be read.\n");
1451 data = data_buffer + table_offset;
1454 if (SQFS_COMPRESSED_BLOCK(finfo.blk_sizes[j])) {
1455 dest_len = get_unaligned_le32(&sblk->block_size);
1456 ret = sqfs_decompress(&ctxt, datablock, &dest_len,
1461 memcpy(buf + offset + *actread, datablock, dest_len);
1462 *actread += dest_len;
1464 memcpy(buf + offset + *actread, data, table_size);
1465 *actread += table_size;
1468 data_offset += table_size;
1473 free(finfo.blk_sizes);
1476 * There is no need to continue if the file is not fragmented.
1483 start = frag_entry.start / ctxt.cur_dev->blksz;
1484 table_size = SQFS_BLOCK_SIZE(frag_entry.size);
1485 table_offset = frag_entry.start - (start * ctxt.cur_dev->blksz);
1486 n_blks = DIV_ROUND_UP(table_size + table_offset, ctxt.cur_dev->blksz);
1488 fragment = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
1495 ret = sqfs_disk_read(start, n_blks, fragment);
1499 /* File compressed and fragmented */
1500 if (finfo.frag && finfo.comp) {
1501 dest_len = get_unaligned_le32(&sblk->block_size);
1502 fragment_block = malloc(dest_len);
1503 if (!fragment_block) {
1508 ret = sqfs_decompress(&ctxt, fragment_block, &dest_len,
1509 (void *)fragment + table_offset,
1512 free(fragment_block);
1516 for (j = offset + *actread; j < finfo.size; j++) {
1517 memcpy(buf + j, &fragment_block[finfo.offset + j], 1);
1521 free(fragment_block);
1523 } else if (finfo.frag && !finfo.comp) {
1524 fragment_block = (void *)fragment + table_offset;
1526 for (j = offset + *actread; j < finfo.size; j++) {
1527 memcpy(buf + j, &fragment_block[finfo.offset + j], 1);
1543 sqfs_closedir(dirsp);
1548 int sqfs_size(const char *filename, loff_t *size)
1550 struct squashfs_super_block *sblk = ctxt.sblk;
1551 struct squashfs_symlink_inode *symlink;
1552 struct fs_dir_stream *dirsp = NULL;
1553 struct squashfs_base_inode *base;
1554 struct squashfs_dir_stream *dirs;
1555 struct squashfs_lreg_inode *lreg;
1556 struct squashfs_reg_inode *reg;
1557 char *dir, *file, *resolved;
1558 struct fs_dirent *dent;
1559 unsigned char *ipos;
1562 sqfs_split_path(&file, &dir, filename);
1564 * sqfs_opendir will uncompress inode and directory tables, and will
1565 * return a pointer to the directory that contains the requested file.
1567 ret = sqfs_opendir(dir, &dirsp);
1573 dirs = (struct squashfs_dir_stream *)dirsp;
1575 while (!sqfs_readdir(dirsp, &dent)) {
1576 ret = strcmp(dent->name, file);
1584 printf("File not found.\n");
1590 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1591 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1596 base = (struct squashfs_base_inode *)ipos;
1597 switch (get_unaligned_le16(&base->inode_type)) {
1599 reg = (struct squashfs_reg_inode *)ipos;
1600 *size = get_unaligned_le32(®->file_size);
1602 case SQFS_LREG_TYPE:
1603 lreg = (struct squashfs_lreg_inode *)ipos;
1604 *size = get_unaligned_le64(&lreg->file_size);
1606 case SQFS_SYMLINK_TYPE:
1607 case SQFS_LSYMLINK_TYPE:
1608 symlink = (struct squashfs_symlink_inode *)ipos;
1609 resolved = sqfs_resolve_symlink(symlink, filename);
1610 ret = sqfs_size(resolved, size);
1613 case SQFS_BLKDEV_TYPE:
1614 case SQFS_CHRDEV_TYPE:
1615 case SQFS_LBLKDEV_TYPE:
1616 case SQFS_LCHRDEV_TYPE:
1617 case SQFS_FIFO_TYPE:
1618 case SQFS_SOCKET_TYPE:
1619 case SQFS_LFIFO_TYPE:
1620 case SQFS_LSOCKET_TYPE:
1622 printf("Unable to recover entry's size.\n");
1632 sqfs_closedir(dirsp);
1637 void sqfs_close(void)
1640 ctxt.cur_dev = NULL;
1641 sqfs_decompressor_cleanup(&ctxt);
1644 void sqfs_closedir(struct fs_dir_stream *dirs)
1646 struct squashfs_dir_stream *sqfs_dirs;
1648 sqfs_dirs = (struct squashfs_dir_stream *)dirs;
1649 free(sqfs_dirs->inode_table);
1650 free(sqfs_dirs->dir_table);
1651 free(sqfs_dirs->dir_header);