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);
245 result[length] = '\0';
247 for (i = 0; i < token_count; i++) {
248 strcpy(result + offset, token_list[i]);
249 offset += strlen(token_list[i]);
250 result[offset++] = '/';
257 * Differently from sqfs_concat_tokens, sqfs_join writes the result into a
258 * previously allocated string, and returns the number of bytes written.
260 static int sqfs_join(char **strings, char *dest, int start, int end,
265 for (i = start; i < end; i++) {
266 strcpy(dest + offset, strings[i]);
267 offset += strlen(strings[i]);
269 dest[offset++] = separator;
276 * Fills the given token list using its size (count) and a source string (str)
278 static int sqfs_tokenize(char **tokens, int count, const char *str)
287 if (!strcmp(strc, "/")) {
288 tokens[0] = strdup(strc);
294 for (j = 0; j < count; j++) {
295 aux = strtok(!j ? strc : NULL, "/");
296 tokens[j] = strdup(aux);
298 for (i = 0; i < j; i++)
313 * Remove last 'updir + 1' tokens from the base path tokens list. This leaves us
314 * with a token list containing only the tokens needed to form the resolved
315 * path, and returns the decremented size of the token list.
317 static int sqfs_clean_base_path(char **base, int count, int updir)
321 for (i = count - updir - 1; i < count; i++)
324 return count - updir - 1;
328 * Given the base ("current dir.") path and the relative one, generate the
331 static char *sqfs_get_abs_path(const char *base, const char *rel)
333 char **base_tokens, **rel_tokens, *resolved = NULL;
334 int ret, bc, rc, i, updir = 0, resolved_size = 0, offset = 0;
336 /* Memory allocation for the token lists */
337 bc = sqfs_count_tokens(base);
338 rc = sqfs_count_tokens(rel);
339 if (bc < 1 || rc < 1)
342 base_tokens = malloc(bc * sizeof(char *));
346 rel_tokens = malloc(rc * sizeof(char *));
350 /* Fill token lists */
351 ret = sqfs_tokenize(base_tokens, bc, base);
355 sqfs_tokenize(rel_tokens, rc, rel);
359 /* count '..' occurrences in target path */
360 for (i = 0; i < rc; i++) {
361 if (!strcmp(rel_tokens[i], ".."))
365 /* Remove the last token and the '..' occurrences */
366 bc = sqfs_clean_base_path(base_tokens, bc, updir);
370 /* Calculate resolved path size */
374 resolved_size += sqfs_get_tokens_length(base_tokens, bc) +
375 sqfs_get_tokens_length(rel_tokens, rc);
377 resolved = malloc(resolved_size + 1);
379 goto free_r_tokens_loop;
381 /* Set resolved path */
382 memset(resolved, '\0', resolved_size + 1);
383 offset += sqfs_join(base_tokens, resolved + offset, 0, bc, '/');
384 resolved[offset++] = '/';
385 offset += sqfs_join(rel_tokens, resolved + offset, updir, rc, '/');
388 for (i = 0; i < rc; i++)
390 for (i = 0; i < bc; i++)
391 free(base_tokens[i]);
400 static char *sqfs_resolve_symlink(struct squashfs_symlink_inode *sym,
401 const char *base_path)
403 char *resolved, *target;
406 sz = get_unaligned_le32(&sym->symlink_size);
407 target = malloc(sz + 1);
412 * There is no trailling null byte in the symlink's target path, so a
413 * copy is made and a '\0' is added at its end.
416 /* Get target name (relative path) */
417 strncpy(target, sym->symlink, sz);
419 /* Relative -> absolute path conversion */
420 resolved = sqfs_get_abs_path(base_path, target);
428 * m_list contains each metadata block's position, and m_count is the number of
429 * elements of m_list. Those metadata blocks come from the compressed directory
432 static int sqfs_search_dir(struct squashfs_dir_stream *dirs, char **token_list,
433 int token_count, u32 *m_list, int m_count)
435 struct squashfs_super_block *sblk = ctxt.sblk;
436 char *path, *target, **sym_tokens, *res, *rem;
437 int j, ret, new_inode_number, offset;
438 struct squashfs_symlink_inode *sym;
439 struct squashfs_ldir_inode *ldir;
440 struct squashfs_dir_inode *dir;
441 struct fs_dir_stream *dirsp;
442 struct fs_dirent *dent;
443 unsigned char *table;
445 dirsp = (struct fs_dir_stream *)dirs;
447 /* Start by root inode */
448 table = sqfs_find_inode(dirs->inode_table, le32_to_cpu(sblk->inodes),
449 sblk->inodes, sblk->block_size);
451 dir = (struct squashfs_dir_inode *)table;
452 ldir = (struct squashfs_ldir_inode *)table;
454 /* get directory offset in directory table */
455 offset = sqfs_dir_offset(table, m_list, m_count);
456 dirs->table = &dirs->dir_table[offset];
458 /* Setup directory header */
459 dirs->dir_header = malloc(SQFS_DIR_HEADER_SIZE);
460 if (!dirs->dir_header)
463 memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE);
465 /* Initialize squashfs_dir_stream members */
466 dirs->table += SQFS_DIR_HEADER_SIZE;
467 dirs->size = get_unaligned_le16(&dir->file_size) - SQFS_DIR_HEADER_SIZE;
468 dirs->entry_count = dirs->dir_header->count + 1;
470 /* No path given -> root directory */
471 if (!strcmp(token_list[0], "/")) {
472 dirs->table = &dirs->dir_table[offset];
473 memcpy(&dirs->i_dir, dir, sizeof(*dir));
477 for (j = 0; j < token_count; j++) {
478 if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) {
479 printf("** Cannot find directory. **\n");
483 while (!sqfs_readdir(dirsp, &dent)) {
484 ret = strcmp(dent->name, token_list[j]);
491 printf("** Cannot find directory. **\n");
495 /* Redefine inode as the found token */
496 new_inode_number = dirs->entry->inode_offset +
497 dirs->dir_header->inode_number;
499 /* Get reference to inode in the inode table */
500 table = sqfs_find_inode(dirs->inode_table, new_inode_number,
501 sblk->inodes, sblk->block_size);
502 dir = (struct squashfs_dir_inode *)table;
504 /* Check for symbolic link and inode type sanity */
505 if (get_unaligned_le16(&dir->inode_type) == SQFS_SYMLINK_TYPE) {
506 sym = (struct squashfs_symlink_inode *)table;
507 /* Get first j + 1 tokens */
508 path = sqfs_concat_tokens(token_list, j + 1);
509 /* Resolve for these tokens */
510 target = sqfs_resolve_symlink(sym, path);
511 /* Join remaining tokens */
512 rem = sqfs_concat_tokens(token_list + j + 1, token_count -
514 /* Concatenate remaining tokens and symlink's target */
515 res = malloc(strlen(rem) + strlen(target) + 1);
517 res[strlen(target)] = '/';
518 strcpy(res + strlen(target) + 1, rem);
519 token_count = sqfs_count_tokens(res);
524 sym_tokens = malloc(token_count * sizeof(char *));
528 /* Fill tokens list */
529 ret = sqfs_tokenize(sym_tokens, token_count, res);
534 ret = sqfs_search_dir(dirs, sym_tokens, token_count,
537 } else if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) {
538 printf("** Cannot find directory. **\n");
543 /* Check if it is an extended dir. */
544 if (get_unaligned_le16(&dir->inode_type) == SQFS_LDIR_TYPE)
545 ldir = (struct squashfs_ldir_inode *)table;
547 /* Get dir. offset into the directory table */
548 offset = sqfs_dir_offset(table, m_list, m_count);
549 dirs->table = &dirs->dir_table[offset];
551 /* Copy directory header */
552 memcpy(dirs->dir_header, &dirs->dir_table[offset],
553 SQFS_DIR_HEADER_SIZE);
555 /* Check for empty directory */
556 if (sqfs_is_empty_dir(table)) {
557 printf("Empty directory.\n");
559 return SQFS_EMPTY_DIR;
562 dirs->table += SQFS_DIR_HEADER_SIZE;
563 dirs->size = get_unaligned_le16(&dir->file_size);
564 dirs->entry_count = dirs->dir_header->count + 1;
565 dirs->size -= SQFS_DIR_HEADER_SIZE;
569 offset = sqfs_dir_offset(table, m_list, m_count);
570 dirs->table = &dirs->dir_table[offset];
572 if (get_unaligned_le16(&dir->inode_type) == SQFS_DIR_TYPE)
573 memcpy(&dirs->i_dir, dir, sizeof(*dir));
575 memcpy(&dirs->i_ldir, ldir, sizeof(*ldir));
581 * Inode and directory tables are stored as a series of metadata blocks, and
582 * given the compressed size of this table, we can calculate how much metadata
583 * blocks are needed to store the result of the decompression, since a
584 * decompressed metadata block should have a size of 8KiB.
586 static int sqfs_count_metablks(void *table, u32 offset, int table_size)
588 int count = 0, cur_size = 0, ret;
593 ret = sqfs_read_metablock(table, offset + cur_size, &comp,
597 cur_size += data_size + SQFS_HEADER_SIZE;
599 } while (cur_size < table_size);
605 * Storing the metadata blocks header's positions will be useful while looking
606 * for an entry in the directory table, using the reference (index and offset)
607 * given by its inode.
609 static int sqfs_get_metablk_pos(u32 *pos_list, void *table, u32 offset,
612 u32 data_size, cur_size = 0;
619 for (j = 0; j < metablks_count; j++) {
620 ret = sqfs_read_metablock(table, offset + cur_size, &comp,
625 cur_size += data_size + SQFS_HEADER_SIZE;
626 pos_list[j] = cur_size;
632 static int sqfs_read_inode_table(unsigned char **inode_table)
634 struct squashfs_super_block *sblk = ctxt.sblk;
635 u64 start, n_blks, table_offset, table_size;
636 int j, ret = 0, metablks_count;
637 unsigned char *src_table, *itb;
638 u32 src_len, dest_offset = 0;
639 unsigned long dest_len = 0;
642 table_size = get_unaligned_le64(&sblk->directory_table_start) -
643 get_unaligned_le64(&sblk->inode_table_start);
644 start = get_unaligned_le64(&sblk->inode_table_start) /
646 n_blks = sqfs_calc_n_blks(sblk->inode_table_start,
647 sblk->directory_table_start, &table_offset);
649 /* Allocate a proper sized buffer (itb) to store the inode table */
650 itb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
654 if (sqfs_disk_read(start, n_blks, itb) < 0) {
659 /* Parse inode table (metadata block) header */
660 ret = sqfs_read_metablock(itb, table_offset, &compressed, &src_len);
666 /* Calculate size to store the whole decompressed table */
667 metablks_count = sqfs_count_metablks(itb, table_offset, table_size);
668 if (metablks_count < 1) {
673 *inode_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE);
679 src_table = itb + table_offset + SQFS_HEADER_SIZE;
681 /* Extract compressed Inode table */
682 for (j = 0; j < metablks_count; j++) {
683 sqfs_read_metablock(itb, table_offset, &compressed, &src_len);
685 dest_len = SQFS_METADATA_BLOCK_SIZE;
686 ret = sqfs_decompress(&ctxt, *inode_table +
687 dest_offset, &dest_len,
694 dest_offset += dest_len;
696 memcpy(*inode_table + (j * SQFS_METADATA_BLOCK_SIZE),
701 * Offsets to the decompression destination, to the metadata
702 * buffer 'itb' and to the decompression source, respectively.
705 table_offset += src_len + SQFS_HEADER_SIZE;
706 src_table += src_len + SQFS_HEADER_SIZE;
715 static int sqfs_read_directory_table(unsigned char **dir_table, u32 **pos_list)
717 u64 start, n_blks, table_offset, table_size;
718 struct squashfs_super_block *sblk = ctxt.sblk;
719 int j, ret = 0, metablks_count = -1;
720 unsigned char *src_table, *dtb;
721 u32 src_len, dest_offset = 0;
722 unsigned long dest_len = 0;
727 /* DIRECTORY TABLE */
728 table_size = get_unaligned_le64(&sblk->fragment_table_start) -
729 get_unaligned_le64(&sblk->directory_table_start);
730 start = get_unaligned_le64(&sblk->directory_table_start) /
732 n_blks = sqfs_calc_n_blks(sblk->directory_table_start,
733 sblk->fragment_table_start, &table_offset);
735 /* Allocate a proper sized buffer (dtb) to store the directory table */
736 dtb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
740 if (sqfs_disk_read(start, n_blks, dtb) < 0)
743 /* Parse directory table (metadata block) header */
744 ret = sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
748 /* Calculate total size to store the whole decompressed table */
749 metablks_count = sqfs_count_metablks(dtb, table_offset, table_size);
750 if (metablks_count < 1)
753 *dir_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE);
757 *pos_list = malloc(metablks_count * sizeof(u32));
761 ret = sqfs_get_metablk_pos(*pos_list, dtb, table_offset,
768 src_table = dtb + table_offset + SQFS_HEADER_SIZE;
770 /* Extract compressed Directory table */
772 for (j = 0; j < metablks_count; j++) {
773 sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
775 dest_len = SQFS_METADATA_BLOCK_SIZE;
776 ret = sqfs_decompress(&ctxt, *dir_table +
777 (j * SQFS_METADATA_BLOCK_SIZE),
778 &dest_len, src_table, src_len);
784 if (dest_len < SQFS_METADATA_BLOCK_SIZE) {
785 dest_offset += dest_len;
789 dest_offset += dest_len;
791 memcpy(*dir_table + (j * SQFS_METADATA_BLOCK_SIZE),
796 * Offsets to the decompression destination, to the metadata
797 * buffer 'dtb' and to the decompression source, respectively.
799 table_offset += src_len + SQFS_HEADER_SIZE;
800 src_table += src_len + SQFS_HEADER_SIZE;
804 if (metablks_count < 1) {
812 return metablks_count;
815 int sqfs_opendir(const char *filename, struct fs_dir_stream **dirsp)
817 unsigned char *inode_table = NULL, *dir_table = NULL;
818 int j, token_count = 0, ret = 0, metablks_count;
819 struct squashfs_dir_stream *dirs;
820 char **token_list = NULL, *path = NULL;
821 u32 *pos_list = NULL;
823 dirs = malloc(sizeof(*dirs));
827 /* these should be set to NULL to prevent dangling pointers */
828 dirs->dir_header = NULL;
831 dirs->inode_table = NULL;
832 dirs->dir_table = NULL;
834 ret = sqfs_read_inode_table(&inode_table);
840 metablks_count = sqfs_read_directory_table(&dir_table, &pos_list);
841 if (metablks_count < 1) {
846 /* Tokenize filename */
847 token_count = sqfs_count_tokens(filename);
848 if (token_count < 0) {
853 path = strdup(filename);
859 token_list = malloc(token_count * sizeof(char *));
865 /* Fill tokens list */
866 ret = sqfs_tokenize(token_list, token_count, path);
870 * ldir's (extended directory) size is greater than dir, so it works as
871 * a general solution for the malloc size, since 'i' is a union.
873 dirs->inode_table = inode_table;
874 dirs->dir_table = dir_table;
875 ret = sqfs_search_dir(dirs, token_list, token_count, pos_list,
880 if (le16_to_cpu(dirs->i_dir.inode_type) == SQFS_DIR_TYPE)
881 dirs->size = le16_to_cpu(dirs->i_dir.file_size);
883 dirs->size = le32_to_cpu(dirs->i_ldir.file_size);
885 /* Setup directory header */
886 memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE);
887 dirs->entry_count = dirs->dir_header->count + 1;
888 dirs->size -= SQFS_DIR_HEADER_SIZE;
892 dirs->table += SQFS_DIR_HEADER_SIZE;
894 *dirsp = (struct fs_dir_stream *)dirs;
897 for (j = 0; j < token_count; j++)
910 int sqfs_readdir(struct fs_dir_stream *fs_dirs, struct fs_dirent **dentp)
912 struct squashfs_super_block *sblk = ctxt.sblk;
913 struct squashfs_dir_stream *dirs;
914 struct squashfs_lreg_inode *lreg;
915 struct squashfs_base_inode *base;
916 struct squashfs_reg_inode *reg;
917 int i_number, offset = 0, ret;
918 struct fs_dirent *dent;
921 dirs = (struct squashfs_dir_stream *)fs_dirs;
924 return -SQFS_STOP_READDIR;
929 if (!dirs->entry_count) {
930 if (dirs->size > SQFS_DIR_HEADER_SIZE) {
931 dirs->size -= SQFS_DIR_HEADER_SIZE;
935 return -SQFS_STOP_READDIR;
938 if (dirs->size > SQFS_EMPTY_FILE_SIZE) {
939 /* Read follow-up (emitted) dir. header */
940 memcpy(dirs->dir_header, dirs->table,
941 SQFS_DIR_HEADER_SIZE);
942 dirs->entry_count = dirs->dir_header->count + 1;
943 ret = sqfs_read_entry(&dirs->entry, dirs->table +
944 SQFS_DIR_HEADER_SIZE);
946 return -SQFS_STOP_READDIR;
948 dirs->table += SQFS_DIR_HEADER_SIZE;
951 ret = sqfs_read_entry(&dirs->entry, dirs->table);
953 return -SQFS_STOP_READDIR;
956 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
957 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
960 base = (struct squashfs_base_inode *)ipos;
962 /* Set entry type and size */
963 switch (dirs->entry->type) {
966 dent->type = FS_DT_DIR;
971 * Entries do not differentiate extended from regular types, so
972 * it needs to be verified manually.
974 if (get_unaligned_le16(&base->inode_type) == SQFS_LREG_TYPE) {
975 lreg = (struct squashfs_lreg_inode *)ipos;
976 dent->size = get_unaligned_le64(&lreg->file_size);
978 reg = (struct squashfs_reg_inode *)ipos;
979 dent->size = get_unaligned_le32(®->file_size);
982 dent->type = FS_DT_REG;
984 case SQFS_BLKDEV_TYPE:
985 case SQFS_CHRDEV_TYPE:
986 case SQFS_LBLKDEV_TYPE:
987 case SQFS_LCHRDEV_TYPE:
989 case SQFS_SOCKET_TYPE:
990 case SQFS_LFIFO_TYPE:
991 case SQFS_LSOCKET_TYPE:
992 dent->type = SQFS_MISC_ENTRY_TYPE;
994 case SQFS_SYMLINK_TYPE:
995 case SQFS_LSYMLINK_TYPE:
996 dent->type = FS_DT_LNK;
999 return -SQFS_STOP_READDIR;
1002 /* Set entry name */
1003 strncpy(dent->name, dirs->entry->name, dirs->entry->name_size + 1);
1004 dent->name[dirs->entry->name_size + 1] = '\0';
1006 offset = dirs->entry->name_size + 1 + SQFS_ENTRY_BASE_LENGTH;
1007 dirs->entry_count--;
1009 /* Decrement size to be read */
1010 if (dirs->size > offset)
1011 dirs->size -= offset;
1015 /* Keep a reference to the current entry before incrementing it */
1016 dirs->table += offset;
1023 int sqfs_probe(struct blk_desc *fs_dev_desc, struct disk_partition *fs_partition)
1025 struct squashfs_super_block *sblk;
1028 ctxt.cur_dev = fs_dev_desc;
1029 ctxt.cur_part_info = *fs_partition;
1031 ret = sqfs_read_sblk(&sblk);
1035 /* Make sure it has a valid SquashFS magic number*/
1036 if (get_unaligned_le32(&sblk->s_magic) != SQFS_MAGIC_NUMBER) {
1037 printf("Bad magic number for SquashFS image.\n");
1038 ctxt.cur_dev = NULL;
1044 ret = sqfs_decompressor_init(&ctxt);
1047 ctxt.cur_dev = NULL;
1055 static char *sqfs_basename(char *path)
1059 fname = path + strlen(path) - 1;
1060 while (fname >= path) {
1061 if (*fname == '/') {
1072 static char *sqfs_dirname(char *path)
1076 fname = sqfs_basename(path);
1084 * Takes a path to file and splits it in two parts: the filename itself and the
1085 * directory's path, e.g.:
1086 * path: /path/to/file.txt
1090 static int sqfs_split_path(char **file, char **dir, const char *path)
1092 char *dirc, *basec, *bname, *dname, *tmp_path;
1103 /* check for first slash in path*/
1104 if (path[0] == '/') {
1105 tmp_path = strdup(path);
1111 tmp_path = malloc(strlen(path) + 2);
1117 strcpy(tmp_path + 1, path);
1120 /* String duplicates */
1121 dirc = strdup(tmp_path);
1127 basec = strdup(tmp_path);
1133 dname = sqfs_dirname(dirc);
1134 bname = sqfs_basename(basec);
1136 *file = strdup(bname);
1143 if (*dname == '\0') {
1153 *dir = strdup(dname);
1174 static int sqfs_get_regfile_info(struct squashfs_reg_inode *reg,
1175 struct squashfs_file_info *finfo,
1176 struct squashfs_fragment_block_entry *fentry,
1179 int datablk_count = 0, ret;
1181 finfo->size = get_unaligned_le32(®->file_size);
1182 finfo->offset = get_unaligned_le32(®->offset);
1183 finfo->start = get_unaligned_le32(®->start_block);
1184 finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(®->fragment));
1186 if (finfo->frag && finfo->offset == 0xFFFFFFFF)
1189 if (finfo->size < 1 || finfo->start == 0xFFFFFFFF)
1193 datablk_count = finfo->size / le32_to_cpu(blksz);
1194 ret = sqfs_frag_lookup(get_unaligned_le32(®->fragment),
1199 if (fentry->size < 1 || fentry->start == 0x7FFFFFFF)
1202 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1205 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1206 if (!finfo->blk_sizes)
1209 return datablk_count;
1212 static int sqfs_get_lregfile_info(struct squashfs_lreg_inode *lreg,
1213 struct squashfs_file_info *finfo,
1214 struct squashfs_fragment_block_entry *fentry,
1217 int datablk_count = 0, ret;
1219 finfo->size = get_unaligned_le64(&lreg->file_size);
1220 finfo->offset = get_unaligned_le32(&lreg->offset);
1221 finfo->start = get_unaligned_le64(&lreg->start_block);
1222 finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(&lreg->fragment));
1224 if (finfo->frag && finfo->offset == 0xFFFFFFFF)
1227 if (finfo->size < 1 || finfo->start == 0x7FFFFFFF)
1231 datablk_count = finfo->size / le32_to_cpu(blksz);
1232 ret = sqfs_frag_lookup(get_unaligned_le32(&lreg->fragment),
1237 if (fentry->size < 1 || fentry->start == 0x7FFFFFFF)
1240 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1243 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1244 if (!finfo->blk_sizes)
1247 return datablk_count;
1250 int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
1253 char *dir, *fragment_block, *datablock = NULL, *data_buffer = NULL;
1254 char *fragment, *file, *resolved, *data;
1255 u64 start, n_blks, table_size, data_offset, table_offset;
1256 int ret, j, i_number, datablk_count = 0;
1257 struct squashfs_super_block *sblk = ctxt.sblk;
1258 struct squashfs_fragment_block_entry frag_entry;
1259 struct squashfs_file_info finfo = {0};
1260 struct squashfs_symlink_inode *symlink;
1261 struct fs_dir_stream *dirsp = NULL;
1262 struct squashfs_dir_stream *dirs;
1263 struct squashfs_lreg_inode *lreg;
1264 struct squashfs_base_inode *base;
1265 struct squashfs_reg_inode *reg;
1266 unsigned long dest_len;
1267 struct fs_dirent *dent;
1268 unsigned char *ipos;
1273 * sqfs_opendir will uncompress inode and directory tables, and will
1274 * return a pointer to the directory that contains the requested file.
1276 sqfs_split_path(&file, &dir, filename);
1277 ret = sqfs_opendir(dir, &dirsp);
1279 sqfs_closedir(dirsp);
1283 dirs = (struct squashfs_dir_stream *)dirsp;
1285 /* For now, only regular files are able to be loaded */
1286 while (!sqfs_readdir(dirsp, &dent)) {
1287 ret = strcmp(dent->name, file);
1295 printf("File not found.\n");
1297 sqfs_closedir(dirsp);
1302 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1303 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1306 base = (struct squashfs_base_inode *)ipos;
1307 switch (get_unaligned_le16(&base->inode_type)) {
1309 reg = (struct squashfs_reg_inode *)ipos;
1310 datablk_count = sqfs_get_regfile_info(reg, &finfo, &frag_entry,
1312 if (datablk_count < 0) {
1317 memcpy(finfo.blk_sizes, ipos + sizeof(*reg),
1318 datablk_count * sizeof(u32));
1320 case SQFS_LREG_TYPE:
1321 lreg = (struct squashfs_lreg_inode *)ipos;
1322 datablk_count = sqfs_get_lregfile_info(lreg, &finfo,
1325 if (datablk_count < 0) {
1330 memcpy(finfo.blk_sizes, ipos + sizeof(*lreg),
1331 datablk_count * sizeof(u32));
1333 case SQFS_SYMLINK_TYPE:
1334 case SQFS_LSYMLINK_TYPE:
1335 symlink = (struct squashfs_symlink_inode *)ipos;
1336 resolved = sqfs_resolve_symlink(symlink, filename);
1337 ret = sqfs_read(resolved, buf, offset, len, actread);
1340 case SQFS_BLKDEV_TYPE:
1341 case SQFS_CHRDEV_TYPE:
1342 case SQFS_LBLKDEV_TYPE:
1343 case SQFS_LCHRDEV_TYPE:
1344 case SQFS_FIFO_TYPE:
1345 case SQFS_SOCKET_TYPE:
1346 case SQFS_LFIFO_TYPE:
1347 case SQFS_LSOCKET_TYPE:
1349 printf("Unsupported entry type\n");
1354 /* If the user specifies a length, check its sanity */
1356 if (len > finfo.size) {
1364 if (datablk_count) {
1365 data_offset = finfo.start;
1366 datablock = malloc(get_unaligned_le32(&sblk->block_size));
1373 for (j = 0; j < datablk_count; j++) {
1374 start = data_offset / ctxt.cur_dev->blksz;
1375 table_size = SQFS_BLOCK_SIZE(finfo.blk_sizes[j]);
1376 table_offset = data_offset - (start * ctxt.cur_dev->blksz);
1377 n_blks = DIV_ROUND_UP(table_size + table_offset,
1378 ctxt.cur_dev->blksz);
1380 data_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
1387 ret = sqfs_disk_read(start, n_blks, data_buffer);
1390 * Possible causes: too many data blocks or too large
1391 * SquashFS block size. Tip: re-compile the SquashFS
1392 * image with mksquashfs's -b <block_size> option.
1394 printf("Error: too many data blocks to be read.\n");
1398 data = data_buffer + table_offset;
1401 if (SQFS_COMPRESSED_BLOCK(finfo.blk_sizes[j])) {
1402 dest_len = get_unaligned_le32(&sblk->block_size);
1403 ret = sqfs_decompress(&ctxt, datablock, &dest_len,
1408 memcpy(buf + offset + *actread, datablock, dest_len);
1409 *actread += dest_len;
1411 memcpy(buf + offset + *actread, data, table_size);
1412 *actread += table_size;
1415 data_offset += table_size;
1418 free(finfo.blk_sizes);
1421 * There is no need to continue if the file is not fragmented.
1428 start = frag_entry.start / ctxt.cur_dev->blksz;
1429 table_size = SQFS_BLOCK_SIZE(frag_entry.size);
1430 table_offset = frag_entry.start - (start * ctxt.cur_dev->blksz);
1431 n_blks = DIV_ROUND_UP(table_size + table_offset, ctxt.cur_dev->blksz);
1433 fragment = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
1440 ret = sqfs_disk_read(start, n_blks, fragment);
1444 /* File compressed and fragmented */
1445 if (finfo.frag && finfo.comp) {
1446 dest_len = get_unaligned_le32(&sblk->block_size);
1447 fragment_block = malloc(dest_len);
1448 if (!fragment_block) {
1453 ret = sqfs_decompress(&ctxt, fragment_block, &dest_len,
1454 (void *)fragment + table_offset,
1457 free(fragment_block);
1461 for (j = offset + *actread; j < finfo.size; j++) {
1462 memcpy(buf + j, &fragment_block[finfo.offset + j], 1);
1466 free(fragment_block);
1468 } else if (finfo.frag && !finfo.comp) {
1469 fragment_block = (void *)fragment + table_offset;
1471 for (j = offset + *actread; j < finfo.size; j++) {
1472 memcpy(buf + j, &fragment_block[finfo.offset + j], 1);
1492 int sqfs_size(const char *filename, loff_t *size)
1494 struct squashfs_super_block *sblk = ctxt.sblk;
1495 struct squashfs_symlink_inode *symlink;
1496 struct fs_dir_stream *dirsp = NULL;
1497 struct squashfs_base_inode *base;
1498 struct squashfs_dir_stream *dirs;
1499 struct squashfs_lreg_inode *lreg;
1500 struct squashfs_reg_inode *reg;
1501 char *dir, *file, *resolved;
1502 struct fs_dirent *dent;
1503 unsigned char *ipos;
1506 sqfs_split_path(&file, &dir, filename);
1508 * sqfs_opendir will uncompress inode and directory tables, and will
1509 * return a pointer to the directory that contains the requested file.
1511 ret = sqfs_opendir(dir, &dirsp);
1513 sqfs_closedir(dirsp);
1518 dirs = (struct squashfs_dir_stream *)dirsp;
1520 while (!sqfs_readdir(dirsp, &dent)) {
1521 ret = strcmp(dent->name, file);
1528 printf("File not found.\n");
1534 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1535 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1539 base = (struct squashfs_base_inode *)ipos;
1540 switch (get_unaligned_le16(&base->inode_type)) {
1542 reg = (struct squashfs_reg_inode *)ipos;
1543 *size = get_unaligned_le32(®->file_size);
1545 case SQFS_LREG_TYPE:
1546 lreg = (struct squashfs_lreg_inode *)ipos;
1547 *size = get_unaligned_le64(&lreg->file_size);
1549 case SQFS_SYMLINK_TYPE:
1550 case SQFS_LSYMLINK_TYPE:
1551 symlink = (struct squashfs_symlink_inode *)ipos;
1552 resolved = sqfs_resolve_symlink(symlink, filename);
1553 ret = sqfs_size(resolved, size);
1556 case SQFS_BLKDEV_TYPE:
1557 case SQFS_CHRDEV_TYPE:
1558 case SQFS_LBLKDEV_TYPE:
1559 case SQFS_LCHRDEV_TYPE:
1560 case SQFS_FIFO_TYPE:
1561 case SQFS_SOCKET_TYPE:
1562 case SQFS_LFIFO_TYPE:
1563 case SQFS_LSOCKET_TYPE:
1565 printf("Unable to recover entry's size.\n");
1575 sqfs_closedir(dirsp);
1580 void sqfs_close(void)
1583 ctxt.cur_dev = NULL;
1584 sqfs_decompressor_cleanup(&ctxt);
1587 void sqfs_closedir(struct fs_dir_stream *dirs)
1589 struct squashfs_dir_stream *sqfs_dirs;
1591 sqfs_dirs = (struct squashfs_dir_stream *)dirs;
1592 free(sqfs_dirs->inode_table);
1593 free(sqfs_dirs->dir_table);
1594 free(sqfs_dirs->dir_header);