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 if (inode_fragment_index >= get_unaligned_le32(&sblk->fragments))
112 start = get_unaligned_le64(&sblk->fragment_table_start) /
114 n_blks = sqfs_calc_n_blks(sblk->fragment_table_start,
115 sblk->export_table_start,
118 /* Allocate a proper sized buffer to store the fragment index table */
119 table = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
123 if (sqfs_disk_read(start, n_blks, table) < 0) {
128 block = SQFS_FRAGMENT_INDEX(inode_fragment_index);
129 offset = SQFS_FRAGMENT_INDEX_OFFSET(inode_fragment_index);
132 * Get the start offset of the metadata block that contains the right
133 * fragment block entry
135 start_block = get_unaligned_le64(table + table_offset + block *
138 start = start_block / ctxt.cur_dev->blksz;
139 n_blks = sqfs_calc_n_blks(cpu_to_le64(start_block),
140 sblk->fragment_table_start, &table_offset);
142 metadata_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
143 if (!metadata_buffer) {
148 if (sqfs_disk_read(start, n_blks, metadata_buffer) < 0) {
153 /* Every metadata block starts with a 16-bit header */
154 header = get_unaligned_le16(metadata_buffer + table_offset);
155 metadata = metadata_buffer + table_offset + SQFS_HEADER_SIZE;
157 if (!metadata || !header) {
162 entries = malloc(SQFS_METADATA_BLOCK_SIZE);
168 if (SQFS_COMPRESSED_METADATA(header)) {
169 src_len = SQFS_METADATA_SIZE(header);
170 dest_len = SQFS_METADATA_BLOCK_SIZE;
171 ret = sqfs_decompress(&ctxt, entries, &dest_len, metadata,
178 memcpy(entries, metadata, SQFS_METADATA_SIZE(header));
181 *e = entries[offset];
182 ret = SQFS_COMPRESSED_BLOCK(e->size);
187 free(metadata_buffer);
195 * The entry name is a flexible array member, and we don't know its size before
196 * actually reading the entry. So we need a first copy to retrieve this size so
197 * we can finally copy the whole struct.
199 static int sqfs_read_entry(struct squashfs_directory_entry **dest, void *src)
201 struct squashfs_directory_entry *tmp;
205 sz = get_unaligned_le16(src + sizeof(*tmp) - sizeof(u16));
207 * 'src' points to the begin of a directory entry, and 'sz' gets its
208 * 'name_size' member's value. name_size is actually the string
209 * length - 1, so adding 2 compensates this difference and adds space
210 * for the trailling null byte.
212 *dest = malloc(sizeof(*tmp) + sz + 2);
216 memcpy(*dest, src, sizeof(*tmp) + sz + 1);
217 (*dest)->name[sz + 1] = '\0';
222 static int sqfs_get_tokens_length(char **tokens, int count)
227 * 1 is added to the result of strlen to consider the slash separator
228 * between the tokens.
230 for (i = 0; i < count; i++)
231 length += strlen(tokens[i]) + 1;
236 /* Takes a token list and returns a single string with '/' as separator. */
237 static char *sqfs_concat_tokens(char **token_list, int token_count)
240 int i, length = 0, offset = 0;
242 length = sqfs_get_tokens_length(token_list, token_count);
244 result = malloc(length + 1);
248 result[length] = '\0';
250 for (i = 0; i < token_count; i++) {
251 strcpy(result + offset, token_list[i]);
252 offset += strlen(token_list[i]);
253 result[offset++] = '/';
260 * Differently from sqfs_concat_tokens, sqfs_join writes the result into a
261 * previously allocated string, and returns the number of bytes written.
263 static int sqfs_join(char **strings, char *dest, int start, int end,
268 for (i = start; i < end; i++) {
269 strcpy(dest + offset, strings[i]);
270 offset += strlen(strings[i]);
272 dest[offset++] = separator;
279 * Fills the given token list using its size (count) and a source string (str)
281 static int sqfs_tokenize(char **tokens, int count, const char *str)
290 if (!strcmp(strc, "/")) {
291 tokens[0] = strdup(strc);
297 for (j = 0; j < count; j++) {
298 aux = strtok(!j ? strc : NULL, "/");
299 tokens[j] = strdup(aux);
301 for (i = 0; i < j; i++)
316 * Remove last 'updir + 1' tokens from the base path tokens list. This leaves us
317 * with a token list containing only the tokens needed to form the resolved
318 * path, and returns the decremented size of the token list.
320 static int sqfs_clean_base_path(char **base, int count, int updir)
324 for (i = count - updir - 1; i < count; i++)
327 return count - updir - 1;
331 * Given the base ("current dir.") path and the relative one, generate the
334 static char *sqfs_get_abs_path(const char *base, const char *rel)
336 char **base_tokens, **rel_tokens, *resolved = NULL;
337 int ret, bc, rc, i, updir = 0, resolved_size = 0, offset = 0;
339 /* Memory allocation for the token lists */
340 bc = sqfs_count_tokens(base);
341 rc = sqfs_count_tokens(rel);
342 if (bc < 1 || rc < 1)
345 base_tokens = malloc(bc * sizeof(char *));
349 rel_tokens = malloc(rc * sizeof(char *));
353 /* Fill token lists */
354 ret = sqfs_tokenize(base_tokens, bc, base);
358 sqfs_tokenize(rel_tokens, rc, rel);
362 /* count '..' occurrences in target path */
363 for (i = 0; i < rc; i++) {
364 if (!strcmp(rel_tokens[i], ".."))
368 /* Remove the last token and the '..' occurrences */
369 bc = sqfs_clean_base_path(base_tokens, bc, updir);
373 /* Calculate resolved path size */
377 resolved_size += sqfs_get_tokens_length(base_tokens, bc) +
378 sqfs_get_tokens_length(rel_tokens, rc);
380 resolved = malloc(resolved_size + 1);
382 goto free_r_tokens_loop;
384 /* Set resolved path */
385 memset(resolved, '\0', resolved_size + 1);
386 offset += sqfs_join(base_tokens, resolved + offset, 0, bc, '/');
387 resolved[offset++] = '/';
388 offset += sqfs_join(rel_tokens, resolved + offset, updir, rc, '/');
391 for (i = 0; i < rc; i++)
393 for (i = 0; i < bc; i++)
394 free(base_tokens[i]);
403 static char *sqfs_resolve_symlink(struct squashfs_symlink_inode *sym,
404 const char *base_path)
406 char *resolved, *target;
409 sz = get_unaligned_le32(&sym->symlink_size);
410 target = malloc(sz + 1);
415 * There is no trailling null byte in the symlink's target path, so a
416 * copy is made and a '\0' is added at its end.
419 /* Get target name (relative path) */
420 strncpy(target, sym->symlink, sz);
422 /* Relative -> absolute path conversion */
423 resolved = sqfs_get_abs_path(base_path, target);
431 * m_list contains each metadata block's position, and m_count is the number of
432 * elements of m_list. Those metadata blocks come from the compressed directory
435 static int sqfs_search_dir(struct squashfs_dir_stream *dirs, char **token_list,
436 int token_count, u32 *m_list, int m_count)
438 struct squashfs_super_block *sblk = ctxt.sblk;
439 char *path, *target, **sym_tokens, *res, *rem;
440 int j, ret = 0, new_inode_number, offset;
441 struct squashfs_symlink_inode *sym;
442 struct squashfs_ldir_inode *ldir;
443 struct squashfs_dir_inode *dir;
444 struct fs_dir_stream *dirsp;
445 struct fs_dirent *dent;
446 unsigned char *table;
454 dirsp = (struct fs_dir_stream *)dirs;
456 /* Start by root inode */
457 table = sqfs_find_inode(dirs->inode_table, le32_to_cpu(sblk->inodes),
458 sblk->inodes, sblk->block_size);
460 dir = (struct squashfs_dir_inode *)table;
461 ldir = (struct squashfs_ldir_inode *)table;
463 /* get directory offset in directory table */
464 offset = sqfs_dir_offset(table, m_list, m_count);
465 dirs->table = &dirs->dir_table[offset];
467 /* Setup directory header */
468 dirs->dir_header = malloc(SQFS_DIR_HEADER_SIZE);
469 if (!dirs->dir_header)
472 memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE);
474 /* Initialize squashfs_dir_stream members */
475 dirs->table += SQFS_DIR_HEADER_SIZE;
476 dirs->size = get_unaligned_le16(&dir->file_size) - SQFS_DIR_HEADER_SIZE;
477 dirs->entry_count = dirs->dir_header->count + 1;
479 /* No path given -> root directory */
480 if (!strcmp(token_list[0], "/")) {
481 dirs->table = &dirs->dir_table[offset];
482 memcpy(&dirs->i_dir, dir, sizeof(*dir));
486 for (j = 0; j < token_count; j++) {
487 if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) {
488 printf("** Cannot find directory. **\n");
493 while (!sqfs_readdir(dirsp, &dent)) {
494 ret = strcmp(dent->name, token_list[j]);
502 printf("** Cannot find directory. **\n");
507 /* Redefine inode as the found token */
508 new_inode_number = dirs->entry->inode_offset +
509 dirs->dir_header->inode_number;
511 /* Get reference to inode in the inode table */
512 table = sqfs_find_inode(dirs->inode_table, new_inode_number,
513 sblk->inodes, sblk->block_size);
514 dir = (struct squashfs_dir_inode *)table;
516 /* Check for symbolic link and inode type sanity */
517 if (get_unaligned_le16(&dir->inode_type) == SQFS_SYMLINK_TYPE) {
518 sym = (struct squashfs_symlink_inode *)table;
519 /* Get first j + 1 tokens */
520 path = sqfs_concat_tokens(token_list, j + 1);
525 /* Resolve for these tokens */
526 target = sqfs_resolve_symlink(sym, path);
531 /* Join remaining tokens */
532 rem = sqfs_concat_tokens(token_list + j + 1, token_count -
538 /* Concatenate remaining tokens and symlink's target */
539 res = malloc(strlen(rem) + strlen(target) + 1);
545 res[strlen(target)] = '/';
546 strcpy(res + strlen(target) + 1, rem);
547 token_count = sqfs_count_tokens(res);
549 if (token_count < 0) {
554 sym_tokens = malloc(token_count * sizeof(char *));
560 /* Fill tokens list */
561 ret = sqfs_tokenize(sym_tokens, token_count, res);
569 ret = sqfs_search_dir(dirs, sym_tokens, token_count,
572 } else if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) {
573 printf("** Cannot find directory. **\n");
580 /* Check if it is an extended dir. */
581 if (get_unaligned_le16(&dir->inode_type) == SQFS_LDIR_TYPE)
582 ldir = (struct squashfs_ldir_inode *)table;
584 /* Get dir. offset into the directory table */
585 offset = sqfs_dir_offset(table, m_list, m_count);
586 dirs->table = &dirs->dir_table[offset];
588 /* Copy directory header */
589 memcpy(dirs->dir_header, &dirs->dir_table[offset],
590 SQFS_DIR_HEADER_SIZE);
592 /* Check for empty directory */
593 if (sqfs_is_empty_dir(table)) {
594 printf("Empty directory.\n");
597 ret = SQFS_EMPTY_DIR;
601 dirs->table += SQFS_DIR_HEADER_SIZE;
602 dirs->size = get_unaligned_le16(&dir->file_size);
603 dirs->entry_count = dirs->dir_header->count + 1;
604 dirs->size -= SQFS_DIR_HEADER_SIZE;
609 offset = sqfs_dir_offset(table, m_list, m_count);
610 dirs->table = &dirs->dir_table[offset];
612 if (get_unaligned_le16(&dir->inode_type) == SQFS_DIR_TYPE)
613 memcpy(&dirs->i_dir, dir, sizeof(*dir));
615 memcpy(&dirs->i_ldir, ldir, sizeof(*ldir));
627 * Inode and directory tables are stored as a series of metadata blocks, and
628 * given the compressed size of this table, we can calculate how much metadata
629 * blocks are needed to store the result of the decompression, since a
630 * decompressed metadata block should have a size of 8KiB.
632 static int sqfs_count_metablks(void *table, u32 offset, int table_size)
634 int count = 0, cur_size = 0, ret;
639 ret = sqfs_read_metablock(table, offset + cur_size, &comp,
643 cur_size += data_size + SQFS_HEADER_SIZE;
645 } while (cur_size < table_size);
651 * Storing the metadata blocks header's positions will be useful while looking
652 * for an entry in the directory table, using the reference (index and offset)
653 * given by its inode.
655 static int sqfs_get_metablk_pos(u32 *pos_list, void *table, u32 offset,
658 u32 data_size, cur_size = 0;
665 for (j = 0; j < metablks_count; j++) {
666 ret = sqfs_read_metablock(table, offset + cur_size, &comp,
671 cur_size += data_size + SQFS_HEADER_SIZE;
672 pos_list[j] = cur_size;
678 static int sqfs_read_inode_table(unsigned char **inode_table)
680 struct squashfs_super_block *sblk = ctxt.sblk;
681 u64 start, n_blks, table_offset, table_size;
682 int j, ret = 0, metablks_count;
683 unsigned char *src_table, *itb;
684 u32 src_len, dest_offset = 0;
685 unsigned long dest_len = 0;
688 table_size = get_unaligned_le64(&sblk->directory_table_start) -
689 get_unaligned_le64(&sblk->inode_table_start);
690 start = get_unaligned_le64(&sblk->inode_table_start) /
692 n_blks = sqfs_calc_n_blks(sblk->inode_table_start,
693 sblk->directory_table_start, &table_offset);
695 /* Allocate a proper sized buffer (itb) to store the inode table */
696 itb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
700 if (sqfs_disk_read(start, n_blks, itb) < 0) {
705 /* Parse inode table (metadata block) header */
706 ret = sqfs_read_metablock(itb, table_offset, &compressed, &src_len);
712 /* Calculate size to store the whole decompressed table */
713 metablks_count = sqfs_count_metablks(itb, table_offset, table_size);
714 if (metablks_count < 1) {
719 *inode_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE);
725 src_table = itb + table_offset + SQFS_HEADER_SIZE;
727 /* Extract compressed Inode table */
728 for (j = 0; j < metablks_count; j++) {
729 sqfs_read_metablock(itb, table_offset, &compressed, &src_len);
731 dest_len = SQFS_METADATA_BLOCK_SIZE;
732 ret = sqfs_decompress(&ctxt, *inode_table +
733 dest_offset, &dest_len,
741 dest_offset += dest_len;
743 memcpy(*inode_table + (j * SQFS_METADATA_BLOCK_SIZE),
748 * Offsets to the decompression destination, to the metadata
749 * buffer 'itb' and to the decompression source, respectively.
752 table_offset += src_len + SQFS_HEADER_SIZE;
753 src_table += src_len + SQFS_HEADER_SIZE;
762 static int sqfs_read_directory_table(unsigned char **dir_table, u32 **pos_list)
764 u64 start, n_blks, table_offset, table_size;
765 struct squashfs_super_block *sblk = ctxt.sblk;
766 int j, ret = 0, metablks_count = -1;
767 unsigned char *src_table, *dtb;
768 u32 src_len, dest_offset = 0;
769 unsigned long dest_len = 0;
774 /* DIRECTORY TABLE */
775 table_size = get_unaligned_le64(&sblk->fragment_table_start) -
776 get_unaligned_le64(&sblk->directory_table_start);
777 start = get_unaligned_le64(&sblk->directory_table_start) /
779 n_blks = sqfs_calc_n_blks(sblk->directory_table_start,
780 sblk->fragment_table_start, &table_offset);
782 /* Allocate a proper sized buffer (dtb) to store the directory table */
783 dtb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
787 if (sqfs_disk_read(start, n_blks, dtb) < 0)
790 /* Parse directory table (metadata block) header */
791 ret = sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
795 /* Calculate total size to store the whole decompressed table */
796 metablks_count = sqfs_count_metablks(dtb, table_offset, table_size);
797 if (metablks_count < 1)
800 *dir_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE);
804 *pos_list = malloc(metablks_count * sizeof(u32));
808 ret = sqfs_get_metablk_pos(*pos_list, dtb, table_offset,
815 src_table = dtb + table_offset + SQFS_HEADER_SIZE;
817 /* Extract compressed Directory table */
819 for (j = 0; j < metablks_count; j++) {
820 sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
822 dest_len = SQFS_METADATA_BLOCK_SIZE;
823 ret = sqfs_decompress(&ctxt, *dir_table +
824 (j * SQFS_METADATA_BLOCK_SIZE),
825 &dest_len, src_table, src_len);
831 if (dest_len < SQFS_METADATA_BLOCK_SIZE) {
832 dest_offset += dest_len;
836 dest_offset += dest_len;
838 memcpy(*dir_table + (j * SQFS_METADATA_BLOCK_SIZE),
843 * Offsets to the decompression destination, to the metadata
844 * buffer 'dtb' and to the decompression source, respectively.
846 table_offset += src_len + SQFS_HEADER_SIZE;
847 src_table += src_len + SQFS_HEADER_SIZE;
851 if (metablks_count < 1) {
859 return metablks_count;
862 int sqfs_opendir(const char *filename, struct fs_dir_stream **dirsp)
864 unsigned char *inode_table = NULL, *dir_table = NULL;
865 int j, token_count, ret = 0, metablks_count;
866 struct squashfs_dir_stream *dirs;
867 char **token_list, *path;
868 u32 *pos_list = NULL;
870 dirs = malloc(sizeof(*dirs));
874 /* these should be set to NULL to prevent dangling pointers */
875 dirs->dir_header = NULL;
878 dirs->inode_table = NULL;
879 dirs->dir_table = NULL;
881 ret = sqfs_read_inode_table(&inode_table);
887 metablks_count = sqfs_read_directory_table(&dir_table, &pos_list);
888 if (metablks_count < 1) {
890 goto free_inode_table;
893 /* Tokenize filename */
894 token_count = sqfs_count_tokens(filename);
895 if (token_count < 0) {
897 goto free_inode_table;
900 path = strdup(filename);
903 goto free_inode_table;
906 token_list = malloc(token_count * sizeof(char *));
912 /* Fill tokens list */
913 ret = sqfs_tokenize(token_list, token_count, path);
917 * ldir's (extended directory) size is greater than dir, so it works as
918 * a general solution for the malloc size, since 'i' is a union.
920 dirs->inode_table = inode_table;
921 dirs->dir_table = dir_table;
922 ret = sqfs_search_dir(dirs, token_list, token_count, pos_list,
927 if (le16_to_cpu(dirs->i_dir.inode_type) == SQFS_DIR_TYPE)
928 dirs->size = le16_to_cpu(dirs->i_dir.file_size);
930 dirs->size = le32_to_cpu(dirs->i_ldir.file_size);
932 /* Setup directory header */
933 memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE);
934 dirs->entry_count = dirs->dir_header->count + 1;
935 dirs->size -= SQFS_DIR_HEADER_SIZE;
939 dirs->table += SQFS_DIR_HEADER_SIZE;
941 *dirsp = (struct fs_dir_stream *)dirs;
944 for (j = 0; j < token_count; j++)
960 int sqfs_readdir(struct fs_dir_stream *fs_dirs, struct fs_dirent **dentp)
962 struct squashfs_super_block *sblk = ctxt.sblk;
963 struct squashfs_dir_stream *dirs;
964 struct squashfs_lreg_inode *lreg;
965 struct squashfs_base_inode *base;
966 struct squashfs_reg_inode *reg;
967 int i_number, offset = 0, ret;
968 struct fs_dirent *dent;
971 dirs = (struct squashfs_dir_stream *)fs_dirs;
974 return -SQFS_STOP_READDIR;
979 if (!dirs->entry_count) {
980 if (dirs->size > SQFS_DIR_HEADER_SIZE) {
981 dirs->size -= SQFS_DIR_HEADER_SIZE;
985 return -SQFS_STOP_READDIR;
988 if (dirs->size > SQFS_EMPTY_FILE_SIZE) {
989 /* Read follow-up (emitted) dir. header */
990 memcpy(dirs->dir_header, dirs->table,
991 SQFS_DIR_HEADER_SIZE);
992 dirs->entry_count = dirs->dir_header->count + 1;
993 ret = sqfs_read_entry(&dirs->entry, dirs->table +
994 SQFS_DIR_HEADER_SIZE);
996 return -SQFS_STOP_READDIR;
998 dirs->table += SQFS_DIR_HEADER_SIZE;
1001 ret = sqfs_read_entry(&dirs->entry, dirs->table);
1003 return -SQFS_STOP_READDIR;
1006 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1007 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1010 base = (struct squashfs_base_inode *)ipos;
1012 /* Set entry type and size */
1013 switch (dirs->entry->type) {
1015 case SQFS_LDIR_TYPE:
1016 dent->type = FS_DT_DIR;
1019 case SQFS_LREG_TYPE:
1021 * Entries do not differentiate extended from regular types, so
1022 * it needs to be verified manually.
1024 if (get_unaligned_le16(&base->inode_type) == SQFS_LREG_TYPE) {
1025 lreg = (struct squashfs_lreg_inode *)ipos;
1026 dent->size = get_unaligned_le64(&lreg->file_size);
1028 reg = (struct squashfs_reg_inode *)ipos;
1029 dent->size = get_unaligned_le32(®->file_size);
1032 dent->type = FS_DT_REG;
1034 case SQFS_BLKDEV_TYPE:
1035 case SQFS_CHRDEV_TYPE:
1036 case SQFS_LBLKDEV_TYPE:
1037 case SQFS_LCHRDEV_TYPE:
1038 case SQFS_FIFO_TYPE:
1039 case SQFS_SOCKET_TYPE:
1040 case SQFS_LFIFO_TYPE:
1041 case SQFS_LSOCKET_TYPE:
1042 dent->type = SQFS_MISC_ENTRY_TYPE;
1044 case SQFS_SYMLINK_TYPE:
1045 case SQFS_LSYMLINK_TYPE:
1046 dent->type = FS_DT_LNK;
1049 return -SQFS_STOP_READDIR;
1052 /* Set entry name */
1053 strncpy(dent->name, dirs->entry->name, dirs->entry->name_size + 1);
1054 dent->name[dirs->entry->name_size + 1] = '\0';
1056 offset = dirs->entry->name_size + 1 + SQFS_ENTRY_BASE_LENGTH;
1057 dirs->entry_count--;
1059 /* Decrement size to be read */
1060 if (dirs->size > offset)
1061 dirs->size -= offset;
1065 /* Keep a reference to the current entry before incrementing it */
1066 dirs->table += offset;
1073 int sqfs_probe(struct blk_desc *fs_dev_desc, struct disk_partition *fs_partition)
1075 struct squashfs_super_block *sblk;
1078 ctxt.cur_dev = fs_dev_desc;
1079 ctxt.cur_part_info = *fs_partition;
1081 ret = sqfs_read_sblk(&sblk);
1085 /* Make sure it has a valid SquashFS magic number*/
1086 if (get_unaligned_le32(&sblk->s_magic) != SQFS_MAGIC_NUMBER) {
1087 printf("Bad magic number for SquashFS image.\n");
1088 ctxt.cur_dev = NULL;
1094 ret = sqfs_decompressor_init(&ctxt);
1097 ctxt.cur_dev = NULL;
1105 static char *sqfs_basename(char *path)
1109 fname = path + strlen(path) - 1;
1110 while (fname >= path) {
1111 if (*fname == '/') {
1122 static char *sqfs_dirname(char *path)
1126 fname = sqfs_basename(path);
1134 * Takes a path to file and splits it in two parts: the filename itself and the
1135 * directory's path, e.g.:
1136 * path: /path/to/file.txt
1140 static int sqfs_split_path(char **file, char **dir, const char *path)
1142 char *dirc, *basec, *bname, *dname, *tmp_path;
1153 /* check for first slash in path*/
1154 if (path[0] == '/') {
1155 tmp_path = strdup(path);
1161 tmp_path = malloc(strlen(path) + 2);
1167 strcpy(tmp_path + 1, path);
1170 /* String duplicates */
1171 dirc = strdup(tmp_path);
1177 basec = strdup(tmp_path);
1183 dname = sqfs_dirname(dirc);
1184 bname = sqfs_basename(basec);
1186 *file = strdup(bname);
1193 if (*dname == '\0') {
1203 *dir = strdup(dname);
1224 static int sqfs_get_regfile_info(struct squashfs_reg_inode *reg,
1225 struct squashfs_file_info *finfo,
1226 struct squashfs_fragment_block_entry *fentry,
1229 int datablk_count = 0, ret;
1231 finfo->size = get_unaligned_le32(®->file_size);
1232 finfo->offset = get_unaligned_le32(®->offset);
1233 finfo->start = get_unaligned_le32(®->start_block);
1234 finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(®->fragment));
1236 if (finfo->frag && finfo->offset == 0xFFFFFFFF)
1239 if (finfo->size < 1 || finfo->start == 0xFFFFFFFF)
1243 datablk_count = finfo->size / le32_to_cpu(blksz);
1244 ret = sqfs_frag_lookup(get_unaligned_le32(®->fragment),
1249 if (fentry->size < 1 || fentry->start == 0x7FFFFFFF)
1252 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1255 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1256 if (!finfo->blk_sizes)
1259 return datablk_count;
1262 static int sqfs_get_lregfile_info(struct squashfs_lreg_inode *lreg,
1263 struct squashfs_file_info *finfo,
1264 struct squashfs_fragment_block_entry *fentry,
1267 int datablk_count = 0, ret;
1269 finfo->size = get_unaligned_le64(&lreg->file_size);
1270 finfo->offset = get_unaligned_le32(&lreg->offset);
1271 finfo->start = get_unaligned_le64(&lreg->start_block);
1272 finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(&lreg->fragment));
1274 if (finfo->frag && finfo->offset == 0xFFFFFFFF)
1277 if (finfo->size < 1 || finfo->start == 0x7FFFFFFF)
1281 datablk_count = finfo->size / le32_to_cpu(blksz);
1282 ret = sqfs_frag_lookup(get_unaligned_le32(&lreg->fragment),
1287 if (fentry->size < 1 || fentry->start == 0x7FFFFFFF)
1290 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1293 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1294 if (!finfo->blk_sizes)
1297 return datablk_count;
1300 int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
1303 char *dir, *fragment_block, *datablock = NULL, *data_buffer = NULL;
1304 char *fragment, *file, *resolved, *data;
1305 u64 start, n_blks, table_size, data_offset, table_offset;
1306 int ret, j, i_number, datablk_count = 0;
1307 struct squashfs_super_block *sblk = ctxt.sblk;
1308 struct squashfs_fragment_block_entry frag_entry;
1309 struct squashfs_file_info finfo = {0};
1310 struct squashfs_symlink_inode *symlink;
1311 struct fs_dir_stream *dirsp = NULL;
1312 struct squashfs_dir_stream *dirs;
1313 struct squashfs_lreg_inode *lreg;
1314 struct squashfs_base_inode *base;
1315 struct squashfs_reg_inode *reg;
1316 unsigned long dest_len;
1317 struct fs_dirent *dent;
1318 unsigned char *ipos;
1323 * sqfs_opendir will uncompress inode and directory tables, and will
1324 * return a pointer to the directory that contains the requested file.
1326 sqfs_split_path(&file, &dir, filename);
1327 ret = sqfs_opendir(dir, &dirsp);
1332 dirs = (struct squashfs_dir_stream *)dirsp;
1334 /* For now, only regular files are able to be loaded */
1335 while (!sqfs_readdir(dirsp, &dent)) {
1336 ret = strcmp(dent->name, file);
1345 printf("File not found.\n");
1351 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1352 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1355 base = (struct squashfs_base_inode *)ipos;
1356 switch (get_unaligned_le16(&base->inode_type)) {
1358 reg = (struct squashfs_reg_inode *)ipos;
1359 datablk_count = sqfs_get_regfile_info(reg, &finfo, &frag_entry,
1361 if (datablk_count < 0) {
1366 memcpy(finfo.blk_sizes, ipos + sizeof(*reg),
1367 datablk_count * sizeof(u32));
1369 case SQFS_LREG_TYPE:
1370 lreg = (struct squashfs_lreg_inode *)ipos;
1371 datablk_count = sqfs_get_lregfile_info(lreg, &finfo,
1374 if (datablk_count < 0) {
1379 memcpy(finfo.blk_sizes, ipos + sizeof(*lreg),
1380 datablk_count * sizeof(u32));
1382 case SQFS_SYMLINK_TYPE:
1383 case SQFS_LSYMLINK_TYPE:
1384 symlink = (struct squashfs_symlink_inode *)ipos;
1385 resolved = sqfs_resolve_symlink(symlink, filename);
1386 ret = sqfs_read(resolved, buf, offset, len, actread);
1389 case SQFS_BLKDEV_TYPE:
1390 case SQFS_CHRDEV_TYPE:
1391 case SQFS_LBLKDEV_TYPE:
1392 case SQFS_LCHRDEV_TYPE:
1393 case SQFS_FIFO_TYPE:
1394 case SQFS_SOCKET_TYPE:
1395 case SQFS_LFIFO_TYPE:
1396 case SQFS_LSOCKET_TYPE:
1398 printf("Unsupported entry type\n");
1403 /* If the user specifies a length, check its sanity */
1405 if (len > finfo.size) {
1413 if (datablk_count) {
1414 data_offset = finfo.start;
1415 datablock = malloc(get_unaligned_le32(&sblk->block_size));
1422 for (j = 0; j < datablk_count; j++) {
1423 start = data_offset / ctxt.cur_dev->blksz;
1424 table_size = SQFS_BLOCK_SIZE(finfo.blk_sizes[j]);
1425 table_offset = data_offset - (start * ctxt.cur_dev->blksz);
1426 n_blks = DIV_ROUND_UP(table_size + table_offset,
1427 ctxt.cur_dev->blksz);
1429 data_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
1436 ret = sqfs_disk_read(start, n_blks, data_buffer);
1439 * Possible causes: too many data blocks or too large
1440 * SquashFS block size. Tip: re-compile the SquashFS
1441 * image with mksquashfs's -b <block_size> option.
1443 printf("Error: too many data blocks to be read.\n");
1447 data = data_buffer + table_offset;
1450 if (SQFS_COMPRESSED_BLOCK(finfo.blk_sizes[j])) {
1451 dest_len = get_unaligned_le32(&sblk->block_size);
1452 ret = sqfs_decompress(&ctxt, datablock, &dest_len,
1457 memcpy(buf + offset + *actread, datablock, dest_len);
1458 *actread += dest_len;
1460 memcpy(buf + offset + *actread, data, table_size);
1461 *actread += table_size;
1464 data_offset += table_size;
1469 free(finfo.blk_sizes);
1472 * There is no need to continue if the file is not fragmented.
1479 start = frag_entry.start / ctxt.cur_dev->blksz;
1480 table_size = SQFS_BLOCK_SIZE(frag_entry.size);
1481 table_offset = frag_entry.start - (start * ctxt.cur_dev->blksz);
1482 n_blks = DIV_ROUND_UP(table_size + table_offset, ctxt.cur_dev->blksz);
1484 fragment = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
1491 ret = sqfs_disk_read(start, n_blks, fragment);
1495 /* File compressed and fragmented */
1496 if (finfo.frag && finfo.comp) {
1497 dest_len = get_unaligned_le32(&sblk->block_size);
1498 fragment_block = malloc(dest_len);
1499 if (!fragment_block) {
1504 ret = sqfs_decompress(&ctxt, fragment_block, &dest_len,
1505 (void *)fragment + table_offset,
1508 free(fragment_block);
1512 for (j = offset + *actread; j < finfo.size; j++) {
1513 memcpy(buf + j, &fragment_block[finfo.offset + j], 1);
1517 free(fragment_block);
1519 } else if (finfo.frag && !finfo.comp) {
1520 fragment_block = (void *)fragment + table_offset;
1522 for (j = offset + *actread; j < finfo.size; j++) {
1523 memcpy(buf + j, &fragment_block[finfo.offset + j], 1);
1539 sqfs_closedir(dirsp);
1544 int sqfs_size(const char *filename, loff_t *size)
1546 struct squashfs_super_block *sblk = ctxt.sblk;
1547 struct squashfs_symlink_inode *symlink;
1548 struct fs_dir_stream *dirsp = NULL;
1549 struct squashfs_base_inode *base;
1550 struct squashfs_dir_stream *dirs;
1551 struct squashfs_lreg_inode *lreg;
1552 struct squashfs_reg_inode *reg;
1553 char *dir, *file, *resolved;
1554 struct fs_dirent *dent;
1555 unsigned char *ipos;
1558 sqfs_split_path(&file, &dir, filename);
1560 * sqfs_opendir will uncompress inode and directory tables, and will
1561 * return a pointer to the directory that contains the requested file.
1563 ret = sqfs_opendir(dir, &dirsp);
1569 dirs = (struct squashfs_dir_stream *)dirsp;
1571 while (!sqfs_readdir(dirsp, &dent)) {
1572 ret = strcmp(dent->name, file);
1580 printf("File not found.\n");
1586 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1587 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1592 base = (struct squashfs_base_inode *)ipos;
1593 switch (get_unaligned_le16(&base->inode_type)) {
1595 reg = (struct squashfs_reg_inode *)ipos;
1596 *size = get_unaligned_le32(®->file_size);
1598 case SQFS_LREG_TYPE:
1599 lreg = (struct squashfs_lreg_inode *)ipos;
1600 *size = get_unaligned_le64(&lreg->file_size);
1602 case SQFS_SYMLINK_TYPE:
1603 case SQFS_LSYMLINK_TYPE:
1604 symlink = (struct squashfs_symlink_inode *)ipos;
1605 resolved = sqfs_resolve_symlink(symlink, filename);
1606 ret = sqfs_size(resolved, size);
1609 case SQFS_BLKDEV_TYPE:
1610 case SQFS_CHRDEV_TYPE:
1611 case SQFS_LBLKDEV_TYPE:
1612 case SQFS_LCHRDEV_TYPE:
1613 case SQFS_FIFO_TYPE:
1614 case SQFS_SOCKET_TYPE:
1615 case SQFS_LFIFO_TYPE:
1616 case SQFS_LSOCKET_TYPE:
1618 printf("Unable to recover entry's size.\n");
1628 sqfs_closedir(dirsp);
1633 void sqfs_close(void)
1636 ctxt.cur_dev = NULL;
1637 sqfs_decompressor_cleanup(&ctxt);
1640 void sqfs_closedir(struct fs_dir_stream *dirs)
1642 struct squashfs_dir_stream *sqfs_dirs;
1644 sqfs_dirs = (struct squashfs_dir_stream *)dirs;
1645 free(sqfs_dirs->inode_table);
1646 free(sqfs_dirs->dir_table);
1647 free(sqfs_dirs->dir_header);