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) {
59 static int sqfs_count_tokens(const char *filename)
61 int token_count = 1, l;
63 for (l = 1; l < strlen(filename); l++) {
64 if (filename[l] == '/')
68 /* Ignore trailing '/' in path */
69 if (filename[strlen(filename) - 1] == '/')
79 * Calculates how many blocks are needed for the buffer used in sqfs_disk_read.
80 * The memory section (e.g. inode table) start offset and its end (i.e. the next
81 * table start) must be specified. It also calculates the offset from which to
82 * start reading the buffer.
84 static int sqfs_calc_n_blks(__le64 start, __le64 end, u64 *offset)
86 u64 start_, table_size;
88 table_size = le64_to_cpu(end) - le64_to_cpu(start);
89 start_ = le64_to_cpu(start) / ctxt.cur_dev->blksz;
90 *offset = le64_to_cpu(start) - (start_ * ctxt.cur_dev->blksz);
92 return DIV_ROUND_UP(table_size + *offset, ctxt.cur_dev->blksz);
96 * Retrieves fragment block entry and returns true if the fragment block is
99 static int sqfs_frag_lookup(u32 inode_fragment_index,
100 struct squashfs_fragment_block_entry *e)
102 u64 start, n_blks, src_len, table_offset, start_block;
103 unsigned char *metadata_buffer, *metadata, *table;
104 struct squashfs_fragment_block_entry *entries;
105 struct squashfs_super_block *sblk = ctxt.sblk;
106 unsigned long dest_len;
107 int block, offset, ret;
110 metadata_buffer = NULL;
114 if (inode_fragment_index >= get_unaligned_le32(&sblk->fragments))
117 start = get_unaligned_le64(&sblk->fragment_table_start) /
119 n_blks = sqfs_calc_n_blks(sblk->fragment_table_start,
120 sblk->export_table_start,
123 /* Allocate a proper sized buffer to store the fragment index table */
124 table = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
130 if (sqfs_disk_read(start, n_blks, table) < 0) {
135 block = SQFS_FRAGMENT_INDEX(inode_fragment_index);
136 offset = SQFS_FRAGMENT_INDEX_OFFSET(inode_fragment_index);
139 * Get the start offset of the metadata block that contains the right
140 * fragment block entry
142 start_block = get_unaligned_le64(table + table_offset + block *
145 start = start_block / ctxt.cur_dev->blksz;
146 n_blks = sqfs_calc_n_blks(cpu_to_le64(start_block),
147 sblk->fragment_table_start, &table_offset);
149 metadata_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
150 if (!metadata_buffer) {
155 if (sqfs_disk_read(start, n_blks, metadata_buffer) < 0) {
160 /* Every metadata block starts with a 16-bit header */
161 header = get_unaligned_le16(metadata_buffer + table_offset);
162 metadata = metadata_buffer + table_offset + SQFS_HEADER_SIZE;
164 if (!metadata || !header) {
169 entries = malloc(SQFS_METADATA_BLOCK_SIZE);
175 if (SQFS_COMPRESSED_METADATA(header)) {
176 src_len = SQFS_METADATA_SIZE(header);
177 dest_len = SQFS_METADATA_BLOCK_SIZE;
178 ret = sqfs_decompress(&ctxt, entries, &dest_len, metadata,
185 memcpy(entries, metadata, SQFS_METADATA_SIZE(header));
188 *e = entries[offset];
189 ret = SQFS_COMPRESSED_BLOCK(e->size);
193 free(metadata_buffer);
200 * The entry name is a flexible array member, and we don't know its size before
201 * actually reading the entry. So we need a first copy to retrieve this size so
202 * we can finally copy the whole struct.
204 static int sqfs_read_entry(struct squashfs_directory_entry **dest, void *src)
206 struct squashfs_directory_entry *tmp;
210 sz = get_unaligned_le16(src + sizeof(*tmp) - sizeof(u16));
212 * 'src' points to the begin of a directory entry, and 'sz' gets its
213 * 'name_size' member's value. name_size is actually the string
214 * length - 1, so adding 2 compensates this difference and adds space
215 * for the trailling null byte.
217 *dest = malloc(sizeof(*tmp) + sz + 2);
221 memcpy(*dest, src, sizeof(*tmp) + sz + 1);
222 (*dest)->name[sz + 1] = '\0';
227 static int sqfs_get_tokens_length(char **tokens, int count)
232 * 1 is added to the result of strlen to consider the slash separator
233 * between the tokens.
235 for (i = 0; i < count; i++)
236 length += strlen(tokens[i]) + 1;
241 /* Takes a token list and returns a single string with '/' as separator. */
242 static char *sqfs_concat_tokens(char **token_list, int token_count)
245 int i, length = 0, offset = 0;
247 length = sqfs_get_tokens_length(token_list, token_count);
249 result = malloc(length + 1);
253 result[length] = '\0';
255 for (i = 0; i < token_count; i++) {
256 strcpy(result + offset, token_list[i]);
257 offset += strlen(token_list[i]);
258 result[offset++] = '/';
265 * Differently from sqfs_concat_tokens, sqfs_join writes the result into a
266 * previously allocated string, and returns the number of bytes written.
268 static int sqfs_join(char **strings, char *dest, int start, int end,
273 for (i = start; i < end; i++) {
274 strcpy(dest + offset, strings[i]);
275 offset += strlen(strings[i]);
277 dest[offset++] = separator;
284 * Fills the given token list using its size (count) and a source string (str)
286 static int sqfs_tokenize(char **tokens, int count, const char *str)
295 if (!strcmp(strc, "/")) {
296 tokens[0] = strdup(strc);
302 for (j = 0; j < count; j++) {
303 aux = strtok(!j ? strc : NULL, "/");
304 tokens[j] = strdup(aux);
306 for (i = 0; i < j; i++)
321 * Remove last 'updir + 1' tokens from the base path tokens list. This leaves us
322 * with a token list containing only the tokens needed to form the resolved
323 * path, and returns the decremented size of the token list.
325 static int sqfs_clean_base_path(char **base, int count, int updir)
329 for (i = count - updir - 1; i < count; i++)
332 return count - updir - 1;
336 * Given the base ("current dir.") path and the relative one, generate the
339 static char *sqfs_get_abs_path(const char *base, const char *rel)
341 char **base_tokens, **rel_tokens, *resolved = NULL;
342 int ret, bc, rc, i, updir = 0, resolved_size = 0, offset = 0;
347 /* Memory allocation for the token lists */
348 bc = sqfs_count_tokens(base);
349 rc = sqfs_count_tokens(rel);
350 if (bc < 1 || rc < 1)
353 base_tokens = calloc(bc, sizeof(char *));
357 rel_tokens = calloc(rc, sizeof(char *));
361 /* Fill token lists */
362 ret = sqfs_tokenize(base_tokens, bc, base);
366 ret = sqfs_tokenize(rel_tokens, rc, rel);
370 /* count '..' occurrences in target path */
371 for (i = 0; i < rc; i++) {
372 if (!strcmp(rel_tokens[i], ".."))
376 /* Remove the last token and the '..' occurrences */
377 bc = sqfs_clean_base_path(base_tokens, bc, updir);
381 /* Calculate resolved path size */
385 resolved_size += sqfs_get_tokens_length(base_tokens, bc) +
386 sqfs_get_tokens_length(rel_tokens, rc);
388 resolved = malloc(resolved_size + 1);
392 /* Set resolved path */
393 memset(resolved, '\0', resolved_size + 1);
394 offset += sqfs_join(base_tokens, resolved + offset, 0, bc, '/');
395 resolved[offset++] = '/';
396 offset += sqfs_join(rel_tokens, resolved + offset, updir, rc, '/');
400 for (i = 0; i < rc; i++)
403 for (i = 0; i < bc; i++)
404 free(base_tokens[i]);
412 static char *sqfs_resolve_symlink(struct squashfs_symlink_inode *sym,
413 const char *base_path)
415 char *resolved, *target;
418 sz = get_unaligned_le32(&sym->symlink_size);
419 target = malloc(sz + 1);
424 * There is no trailling null byte in the symlink's target path, so a
425 * copy is made and a '\0' is added at its end.
428 /* Get target name (relative path) */
429 strncpy(target, sym->symlink, sz);
431 /* Relative -> absolute path conversion */
432 resolved = sqfs_get_abs_path(base_path, target);
440 * m_list contains each metadata block's position, and m_count is the number of
441 * elements of m_list. Those metadata blocks come from the compressed directory
444 static int sqfs_search_dir(struct squashfs_dir_stream *dirs, char **token_list,
445 int token_count, u32 *m_list, int m_count)
447 struct squashfs_super_block *sblk = ctxt.sblk;
448 char *path, *target, **sym_tokens, *res, *rem;
449 int j, ret = 0, new_inode_number, offset;
450 struct squashfs_symlink_inode *sym;
451 struct squashfs_ldir_inode *ldir;
452 struct squashfs_dir_inode *dir;
453 struct fs_dir_stream *dirsp;
454 struct fs_dirent *dent;
455 unsigned char *table;
463 dirsp = (struct fs_dir_stream *)dirs;
465 /* Start by root inode */
466 table = sqfs_find_inode(dirs->inode_table, le32_to_cpu(sblk->inodes),
467 sblk->inodes, sblk->block_size);
469 dir = (struct squashfs_dir_inode *)table;
470 ldir = (struct squashfs_ldir_inode *)table;
472 /* get directory offset in directory table */
473 offset = sqfs_dir_offset(table, m_list, m_count);
474 dirs->table = &dirs->dir_table[offset];
476 /* Setup directory header */
477 dirs->dir_header = malloc(SQFS_DIR_HEADER_SIZE);
478 if (!dirs->dir_header)
481 memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE);
483 /* Initialize squashfs_dir_stream members */
484 dirs->table += SQFS_DIR_HEADER_SIZE;
485 dirs->size = get_unaligned_le16(&dir->file_size) - SQFS_DIR_HEADER_SIZE;
486 dirs->entry_count = dirs->dir_header->count + 1;
488 /* No path given -> root directory */
489 if (!strcmp(token_list[0], "/")) {
490 dirs->table = &dirs->dir_table[offset];
491 memcpy(&dirs->i_dir, dir, sizeof(*dir));
495 for (j = 0; j < token_count; j++) {
496 if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) {
497 printf("** Cannot find directory. **\n");
502 while (!sqfs_readdir(dirsp, &dent)) {
503 ret = strcmp(dent->name, token_list[j]);
511 printf("** Cannot find directory. **\n");
516 /* Redefine inode as the found token */
517 new_inode_number = dirs->entry->inode_offset +
518 dirs->dir_header->inode_number;
520 /* Get reference to inode in the inode table */
521 table = sqfs_find_inode(dirs->inode_table, new_inode_number,
522 sblk->inodes, sblk->block_size);
523 dir = (struct squashfs_dir_inode *)table;
525 /* Check for symbolic link and inode type sanity */
526 if (get_unaligned_le16(&dir->inode_type) == SQFS_SYMLINK_TYPE) {
527 sym = (struct squashfs_symlink_inode *)table;
528 /* Get first j + 1 tokens */
529 path = sqfs_concat_tokens(token_list, j + 1);
534 /* Resolve for these tokens */
535 target = sqfs_resolve_symlink(sym, path);
540 /* Join remaining tokens */
541 rem = sqfs_concat_tokens(token_list + j + 1, token_count -
547 /* Concatenate remaining tokens and symlink's target */
548 res = malloc(strlen(rem) + strlen(target) + 1);
554 res[strlen(target)] = '/';
555 strcpy(res + strlen(target) + 1, rem);
556 token_count = sqfs_count_tokens(res);
558 if (token_count < 0) {
563 sym_tokens = malloc(token_count * sizeof(char *));
569 /* Fill tokens list */
570 ret = sqfs_tokenize(sym_tokens, token_count, res);
578 ret = sqfs_search_dir(dirs, sym_tokens, token_count,
581 } else if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) {
582 printf("** Cannot find directory. **\n");
589 /* Check if it is an extended dir. */
590 if (get_unaligned_le16(&dir->inode_type) == SQFS_LDIR_TYPE)
591 ldir = (struct squashfs_ldir_inode *)table;
593 /* Get dir. offset into the directory table */
594 offset = sqfs_dir_offset(table, m_list, m_count);
595 dirs->table = &dirs->dir_table[offset];
597 /* Copy directory header */
598 memcpy(dirs->dir_header, &dirs->dir_table[offset],
599 SQFS_DIR_HEADER_SIZE);
601 /* Check for empty directory */
602 if (sqfs_is_empty_dir(table)) {
603 printf("Empty directory.\n");
606 ret = SQFS_EMPTY_DIR;
610 dirs->table += SQFS_DIR_HEADER_SIZE;
611 dirs->size = get_unaligned_le16(&dir->file_size);
612 dirs->entry_count = dirs->dir_header->count + 1;
613 dirs->size -= SQFS_DIR_HEADER_SIZE;
618 offset = sqfs_dir_offset(table, m_list, m_count);
619 dirs->table = &dirs->dir_table[offset];
621 if (get_unaligned_le16(&dir->inode_type) == SQFS_DIR_TYPE)
622 memcpy(&dirs->i_dir, dir, sizeof(*dir));
624 memcpy(&dirs->i_ldir, ldir, sizeof(*ldir));
636 * Inode and directory tables are stored as a series of metadata blocks, and
637 * given the compressed size of this table, we can calculate how much metadata
638 * blocks are needed to store the result of the decompression, since a
639 * decompressed metadata block should have a size of 8KiB.
641 static int sqfs_count_metablks(void *table, u32 offset, int table_size)
643 int count = 0, cur_size = 0, ret;
648 ret = sqfs_read_metablock(table, offset + cur_size, &comp,
652 cur_size += data_size + SQFS_HEADER_SIZE;
654 } while (cur_size < table_size);
660 * Storing the metadata blocks header's positions will be useful while looking
661 * for an entry in the directory table, using the reference (index and offset)
662 * given by its inode.
664 static int sqfs_get_metablk_pos(u32 *pos_list, void *table, u32 offset,
667 u32 data_size, cur_size = 0;
674 for (j = 0; j < metablks_count; j++) {
675 ret = sqfs_read_metablock(table, offset + cur_size, &comp,
680 cur_size += data_size + SQFS_HEADER_SIZE;
681 pos_list[j] = cur_size;
687 static int sqfs_read_inode_table(unsigned char **inode_table)
689 struct squashfs_super_block *sblk = ctxt.sblk;
690 u64 start, n_blks, table_offset, table_size;
691 int j, ret = 0, metablks_count;
692 unsigned char *src_table, *itb;
693 u32 src_len, dest_offset = 0;
694 unsigned long dest_len = 0;
697 table_size = get_unaligned_le64(&sblk->directory_table_start) -
698 get_unaligned_le64(&sblk->inode_table_start);
699 start = get_unaligned_le64(&sblk->inode_table_start) /
701 n_blks = sqfs_calc_n_blks(sblk->inode_table_start,
702 sblk->directory_table_start, &table_offset);
704 /* Allocate a proper sized buffer (itb) to store the inode table */
705 itb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
709 if (sqfs_disk_read(start, n_blks, itb) < 0) {
714 /* Parse inode table (metadata block) header */
715 ret = sqfs_read_metablock(itb, table_offset, &compressed, &src_len);
721 /* Calculate size to store the whole decompressed table */
722 metablks_count = sqfs_count_metablks(itb, table_offset, table_size);
723 if (metablks_count < 1) {
728 *inode_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE);
734 src_table = itb + table_offset + SQFS_HEADER_SIZE;
736 /* Extract compressed Inode table */
737 for (j = 0; j < metablks_count; j++) {
738 sqfs_read_metablock(itb, table_offset, &compressed, &src_len);
740 dest_len = SQFS_METADATA_BLOCK_SIZE;
741 ret = sqfs_decompress(&ctxt, *inode_table +
742 dest_offset, &dest_len,
750 dest_offset += dest_len;
752 memcpy(*inode_table + (j * SQFS_METADATA_BLOCK_SIZE),
757 * Offsets to the decompression destination, to the metadata
758 * buffer 'itb' and to the decompression source, respectively.
761 table_offset += src_len + SQFS_HEADER_SIZE;
762 src_table += src_len + SQFS_HEADER_SIZE;
771 static int sqfs_read_directory_table(unsigned char **dir_table, u32 **pos_list)
773 u64 start, n_blks, table_offset, table_size;
774 struct squashfs_super_block *sblk = ctxt.sblk;
775 int j, ret = 0, metablks_count = -1;
776 unsigned char *src_table, *dtb;
777 u32 src_len, dest_offset = 0;
778 unsigned long dest_len = 0;
783 /* DIRECTORY TABLE */
784 table_size = get_unaligned_le64(&sblk->fragment_table_start) -
785 get_unaligned_le64(&sblk->directory_table_start);
786 start = get_unaligned_le64(&sblk->directory_table_start) /
788 n_blks = sqfs_calc_n_blks(sblk->directory_table_start,
789 sblk->fragment_table_start, &table_offset);
791 /* Allocate a proper sized buffer (dtb) to store the directory table */
792 dtb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
796 if (sqfs_disk_read(start, n_blks, dtb) < 0)
799 /* Parse directory table (metadata block) header */
800 ret = sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
804 /* Calculate total size to store the whole decompressed table */
805 metablks_count = sqfs_count_metablks(dtb, table_offset, table_size);
806 if (metablks_count < 1)
809 *dir_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE);
813 *pos_list = malloc(metablks_count * sizeof(u32));
817 ret = sqfs_get_metablk_pos(*pos_list, dtb, table_offset,
824 src_table = dtb + table_offset + SQFS_HEADER_SIZE;
826 /* Extract compressed Directory table */
828 for (j = 0; j < metablks_count; j++) {
829 sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
831 dest_len = SQFS_METADATA_BLOCK_SIZE;
832 ret = sqfs_decompress(&ctxt, *dir_table +
833 (j * SQFS_METADATA_BLOCK_SIZE),
834 &dest_len, src_table, src_len);
840 if (dest_len < SQFS_METADATA_BLOCK_SIZE) {
841 dest_offset += dest_len;
845 dest_offset += dest_len;
847 memcpy(*dir_table + (j * SQFS_METADATA_BLOCK_SIZE),
852 * Offsets to the decompression destination, to the metadata
853 * buffer 'dtb' and to the decompression source, respectively.
855 table_offset += src_len + SQFS_HEADER_SIZE;
856 src_table += src_len + SQFS_HEADER_SIZE;
860 if (metablks_count < 1) {
868 return metablks_count;
871 int sqfs_opendir(const char *filename, struct fs_dir_stream **dirsp)
873 unsigned char *inode_table = NULL, *dir_table = NULL;
874 int j, token_count = 0, ret = 0, metablks_count;
875 struct squashfs_dir_stream *dirs;
876 char **token_list = NULL, *path = NULL;
877 u32 *pos_list = NULL;
879 dirs = calloc(1, sizeof(*dirs));
883 /* these should be set to NULL to prevent dangling pointers */
884 dirs->dir_header = NULL;
887 dirs->inode_table = NULL;
888 dirs->dir_table = NULL;
890 ret = sqfs_read_inode_table(&inode_table);
896 metablks_count = sqfs_read_directory_table(&dir_table, &pos_list);
897 if (metablks_count < 1) {
902 /* Tokenize filename */
903 token_count = sqfs_count_tokens(filename);
904 if (token_count < 0) {
909 path = strdup(filename);
915 token_list = malloc(token_count * sizeof(char *));
921 /* Fill tokens list */
922 ret = sqfs_tokenize(token_list, token_count, path);
926 * ldir's (extended directory) size is greater than dir, so it works as
927 * a general solution for the malloc size, since 'i' is a union.
929 dirs->inode_table = inode_table;
930 dirs->dir_table = dir_table;
931 ret = sqfs_search_dir(dirs, token_list, token_count, pos_list,
936 if (le16_to_cpu(dirs->i_dir.inode_type) == SQFS_DIR_TYPE)
937 dirs->size = le16_to_cpu(dirs->i_dir.file_size);
939 dirs->size = le32_to_cpu(dirs->i_ldir.file_size);
941 /* Setup directory header */
942 memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE);
943 dirs->entry_count = dirs->dir_header->count + 1;
944 dirs->size -= SQFS_DIR_HEADER_SIZE;
948 dirs->table += SQFS_DIR_HEADER_SIZE;
950 *dirsp = (struct fs_dir_stream *)dirs;
953 for (j = 0; j < token_count; j++)
966 int sqfs_readdir(struct fs_dir_stream *fs_dirs, struct fs_dirent **dentp)
968 struct squashfs_super_block *sblk = ctxt.sblk;
969 struct squashfs_dir_stream *dirs;
970 struct squashfs_lreg_inode *lreg;
971 struct squashfs_base_inode *base;
972 struct squashfs_reg_inode *reg;
973 int i_number, offset = 0, ret;
974 struct fs_dirent *dent;
977 dirs = (struct squashfs_dir_stream *)fs_dirs;
980 return -SQFS_STOP_READDIR;
985 if (!dirs->entry_count) {
986 if (dirs->size > SQFS_DIR_HEADER_SIZE) {
987 dirs->size -= SQFS_DIR_HEADER_SIZE;
991 return -SQFS_STOP_READDIR;
994 if (dirs->size > SQFS_EMPTY_FILE_SIZE) {
995 /* Read follow-up (emitted) dir. header */
996 memcpy(dirs->dir_header, dirs->table,
997 SQFS_DIR_HEADER_SIZE);
998 dirs->entry_count = dirs->dir_header->count + 1;
999 ret = sqfs_read_entry(&dirs->entry, dirs->table +
1000 SQFS_DIR_HEADER_SIZE);
1002 return -SQFS_STOP_READDIR;
1004 dirs->table += SQFS_DIR_HEADER_SIZE;
1007 ret = sqfs_read_entry(&dirs->entry, dirs->table);
1009 return -SQFS_STOP_READDIR;
1012 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1013 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1016 base = (struct squashfs_base_inode *)ipos;
1018 /* Set entry type and size */
1019 switch (dirs->entry->type) {
1021 case SQFS_LDIR_TYPE:
1022 dent->type = FS_DT_DIR;
1025 case SQFS_LREG_TYPE:
1027 * Entries do not differentiate extended from regular types, so
1028 * it needs to be verified manually.
1030 if (get_unaligned_le16(&base->inode_type) == SQFS_LREG_TYPE) {
1031 lreg = (struct squashfs_lreg_inode *)ipos;
1032 dent->size = get_unaligned_le64(&lreg->file_size);
1034 reg = (struct squashfs_reg_inode *)ipos;
1035 dent->size = get_unaligned_le32(®->file_size);
1038 dent->type = FS_DT_REG;
1040 case SQFS_BLKDEV_TYPE:
1041 case SQFS_CHRDEV_TYPE:
1042 case SQFS_LBLKDEV_TYPE:
1043 case SQFS_LCHRDEV_TYPE:
1044 case SQFS_FIFO_TYPE:
1045 case SQFS_SOCKET_TYPE:
1046 case SQFS_LFIFO_TYPE:
1047 case SQFS_LSOCKET_TYPE:
1048 dent->type = SQFS_MISC_ENTRY_TYPE;
1050 case SQFS_SYMLINK_TYPE:
1051 case SQFS_LSYMLINK_TYPE:
1052 dent->type = FS_DT_LNK;
1055 return -SQFS_STOP_READDIR;
1058 /* Set entry name */
1059 strncpy(dent->name, dirs->entry->name, dirs->entry->name_size + 1);
1060 dent->name[dirs->entry->name_size + 1] = '\0';
1062 offset = dirs->entry->name_size + 1 + SQFS_ENTRY_BASE_LENGTH;
1063 dirs->entry_count--;
1065 /* Decrement size to be read */
1066 if (dirs->size > offset)
1067 dirs->size -= offset;
1071 /* Keep a reference to the current entry before incrementing it */
1072 dirs->table += offset;
1079 int sqfs_probe(struct blk_desc *fs_dev_desc, struct disk_partition *fs_partition)
1081 struct squashfs_super_block *sblk;
1084 ctxt.cur_dev = fs_dev_desc;
1085 ctxt.cur_part_info = *fs_partition;
1087 ret = sqfs_read_sblk(&sblk);
1091 /* Make sure it has a valid SquashFS magic number*/
1092 if (get_unaligned_le32(&sblk->s_magic) != SQFS_MAGIC_NUMBER) {
1093 debug("Bad magic number for SquashFS image.\n");
1100 ret = sqfs_decompressor_init(&ctxt);
1107 ctxt.cur_dev = NULL;
1113 static char *sqfs_basename(char *path)
1117 fname = path + strlen(path) - 1;
1118 while (fname >= path) {
1119 if (*fname == '/') {
1130 static char *sqfs_dirname(char *path)
1134 fname = sqfs_basename(path);
1142 * Takes a path to file and splits it in two parts: the filename itself and the
1143 * directory's path, e.g.:
1144 * path: /path/to/file.txt
1148 static int sqfs_split_path(char **file, char **dir, const char *path)
1150 char *dirc, *basec, *bname, *dname, *tmp_path;
1161 /* check for first slash in path*/
1162 if (path[0] == '/') {
1163 tmp_path = strdup(path);
1169 tmp_path = malloc(strlen(path) + 2);
1175 strcpy(tmp_path + 1, path);
1178 /* String duplicates */
1179 dirc = strdup(tmp_path);
1185 basec = strdup(tmp_path);
1191 dname = sqfs_dirname(dirc);
1192 bname = sqfs_basename(basec);
1194 *file = strdup(bname);
1201 if (*dname == '\0') {
1211 *dir = strdup(dname);
1232 static int sqfs_get_regfile_info(struct squashfs_reg_inode *reg,
1233 struct squashfs_file_info *finfo,
1234 struct squashfs_fragment_block_entry *fentry,
1237 int datablk_count = 0, ret;
1239 finfo->size = get_unaligned_le32(®->file_size);
1240 finfo->offset = get_unaligned_le32(®->offset);
1241 finfo->start = get_unaligned_le32(®->start_block);
1242 finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(®->fragment));
1244 if (finfo->frag && finfo->offset == 0xFFFFFFFF)
1247 if (finfo->size < 1 || finfo->start == 0xFFFFFFFF)
1251 datablk_count = finfo->size / le32_to_cpu(blksz);
1252 ret = sqfs_frag_lookup(get_unaligned_le32(®->fragment),
1257 if (fentry->size < 1 || fentry->start == 0x7FFFFFFF)
1260 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1263 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1264 if (!finfo->blk_sizes)
1267 return datablk_count;
1270 static int sqfs_get_lregfile_info(struct squashfs_lreg_inode *lreg,
1271 struct squashfs_file_info *finfo,
1272 struct squashfs_fragment_block_entry *fentry,
1275 int datablk_count = 0, ret;
1277 finfo->size = get_unaligned_le64(&lreg->file_size);
1278 finfo->offset = get_unaligned_le32(&lreg->offset);
1279 finfo->start = get_unaligned_le64(&lreg->start_block);
1280 finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(&lreg->fragment));
1282 if (finfo->frag && finfo->offset == 0xFFFFFFFF)
1285 if (finfo->size < 1 || finfo->start == 0x7FFFFFFF)
1289 datablk_count = finfo->size / le32_to_cpu(blksz);
1290 ret = sqfs_frag_lookup(get_unaligned_le32(&lreg->fragment),
1295 if (fentry->size < 1 || fentry->start == 0x7FFFFFFF)
1298 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1301 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1302 if (!finfo->blk_sizes)
1305 return datablk_count;
1308 int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
1311 char *dir = NULL, *fragment_block, *datablock = NULL, *data_buffer = NULL;
1312 char *fragment = NULL, *file = NULL, *resolved, *data;
1313 u64 start, n_blks, table_size, data_offset, table_offset, sparse_size;
1314 int ret, j, i_number, datablk_count = 0;
1315 struct squashfs_super_block *sblk = ctxt.sblk;
1316 struct squashfs_fragment_block_entry frag_entry;
1317 struct squashfs_file_info finfo = {0};
1318 struct squashfs_symlink_inode *symlink;
1319 struct fs_dir_stream *dirsp = NULL;
1320 struct squashfs_dir_stream *dirs;
1321 struct squashfs_lreg_inode *lreg;
1322 struct squashfs_base_inode *base;
1323 struct squashfs_reg_inode *reg;
1324 unsigned long dest_len;
1325 struct fs_dirent *dent;
1326 unsigned char *ipos;
1332 * TODO: implement reading at an offset in file
1334 printf("Error: reading at a specific offset in a squashfs file is not supported yet.\n");
1339 * sqfs_opendir will uncompress inode and directory tables, and will
1340 * return a pointer to the directory that contains the requested file.
1342 sqfs_split_path(&file, &dir, filename);
1343 ret = sqfs_opendir(dir, &dirsp);
1348 dirs = (struct squashfs_dir_stream *)dirsp;
1350 /* For now, only regular files are able to be loaded */
1351 while (!sqfs_readdir(dirsp, &dent)) {
1352 ret = strcmp(dent->name, file);
1361 printf("File not found.\n");
1367 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1368 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1371 base = (struct squashfs_base_inode *)ipos;
1372 switch (get_unaligned_le16(&base->inode_type)) {
1374 reg = (struct squashfs_reg_inode *)ipos;
1375 datablk_count = sqfs_get_regfile_info(reg, &finfo, &frag_entry,
1377 if (datablk_count < 0) {
1382 memcpy(finfo.blk_sizes, ipos + sizeof(*reg),
1383 datablk_count * sizeof(u32));
1385 case SQFS_LREG_TYPE:
1386 lreg = (struct squashfs_lreg_inode *)ipos;
1387 datablk_count = sqfs_get_lregfile_info(lreg, &finfo,
1390 if (datablk_count < 0) {
1395 memcpy(finfo.blk_sizes, ipos + sizeof(*lreg),
1396 datablk_count * sizeof(u32));
1398 case SQFS_SYMLINK_TYPE:
1399 case SQFS_LSYMLINK_TYPE:
1400 symlink = (struct squashfs_symlink_inode *)ipos;
1401 resolved = sqfs_resolve_symlink(symlink, filename);
1402 ret = sqfs_read(resolved, buf, offset, len, actread);
1405 case SQFS_BLKDEV_TYPE:
1406 case SQFS_CHRDEV_TYPE:
1407 case SQFS_LBLKDEV_TYPE:
1408 case SQFS_LCHRDEV_TYPE:
1409 case SQFS_FIFO_TYPE:
1410 case SQFS_SOCKET_TYPE:
1411 case SQFS_LFIFO_TYPE:
1412 case SQFS_LSOCKET_TYPE:
1414 printf("Unsupported entry type\n");
1419 /* If the user specifies a length, check its sanity */
1421 if (len > finfo.size) {
1431 if (datablk_count) {
1432 data_offset = finfo.start;
1433 datablock = malloc(get_unaligned_le32(&sblk->block_size));
1440 for (j = 0; j < datablk_count; j++) {
1441 start = data_offset / ctxt.cur_dev->blksz;
1442 table_size = SQFS_BLOCK_SIZE(finfo.blk_sizes[j]);
1443 table_offset = data_offset - (start * ctxt.cur_dev->blksz);
1444 n_blks = DIV_ROUND_UP(table_size + table_offset,
1445 ctxt.cur_dev->blksz);
1447 /* Don't load any data for sparse blocks */
1448 if (finfo.blk_sizes[j] == 0) {
1454 data_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
1461 ret = sqfs_disk_read(start, n_blks, data_buffer);
1464 * Possible causes: too many data blocks or too large
1465 * SquashFS block size. Tip: re-compile the SquashFS
1466 * image with mksquashfs's -b <block_size> option.
1468 printf("Error: too many data blocks to be read.\n");
1472 data = data_buffer + table_offset;
1476 if (finfo.blk_sizes[j] == 0) {
1477 /* This is a sparse block */
1478 sparse_size = get_unaligned_le32(&sblk->block_size);
1479 if ((*actread + sparse_size) > len)
1480 sparse_size = len - *actread;
1481 memset(buf + *actread, 0, sparse_size);
1482 *actread += sparse_size;
1483 } else if (SQFS_COMPRESSED_BLOCK(finfo.blk_sizes[j])) {
1484 dest_len = get_unaligned_le32(&sblk->block_size);
1485 ret = sqfs_decompress(&ctxt, datablock, &dest_len,
1490 if ((*actread + dest_len) > len)
1491 dest_len = len - *actread;
1492 memcpy(buf + *actread, datablock, dest_len);
1493 *actread += dest_len;
1495 if ((*actread + table_size) > len)
1496 table_size = len - *actread;
1497 memcpy(buf + *actread, data, table_size);
1498 *actread += table_size;
1501 data_offset += table_size;
1505 if (*actread >= len)
1510 * There is no need to continue if the file is not fragmented.
1517 start = frag_entry.start / ctxt.cur_dev->blksz;
1518 table_size = SQFS_BLOCK_SIZE(frag_entry.size);
1519 table_offset = frag_entry.start - (start * ctxt.cur_dev->blksz);
1520 n_blks = DIV_ROUND_UP(table_size + table_offset, ctxt.cur_dev->blksz);
1522 fragment = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
1529 ret = sqfs_disk_read(start, n_blks, fragment);
1533 /* File compressed and fragmented */
1534 if (finfo.frag && finfo.comp) {
1535 dest_len = get_unaligned_le32(&sblk->block_size);
1536 fragment_block = malloc(dest_len);
1537 if (!fragment_block) {
1542 ret = sqfs_decompress(&ctxt, fragment_block, &dest_len,
1543 (void *)fragment + table_offset,
1546 free(fragment_block);
1550 memcpy(buf + *actread, &fragment_block[finfo.offset], finfo.size - *actread);
1551 *actread = finfo.size;
1553 free(fragment_block);
1555 } else if (finfo.frag && !finfo.comp) {
1556 fragment_block = (void *)fragment + table_offset;
1558 memcpy(buf + *actread, &fragment_block[finfo.offset], finfo.size - *actread);
1559 *actread = finfo.size;
1564 if (datablk_count) {
1570 free(finfo.blk_sizes);
1571 sqfs_closedir(dirsp);
1576 int sqfs_size(const char *filename, loff_t *size)
1578 struct squashfs_super_block *sblk = ctxt.sblk;
1579 struct squashfs_symlink_inode *symlink;
1580 struct fs_dir_stream *dirsp = NULL;
1581 struct squashfs_base_inode *base;
1582 struct squashfs_dir_stream *dirs;
1583 struct squashfs_lreg_inode *lreg;
1584 struct squashfs_reg_inode *reg;
1585 char *dir, *file, *resolved;
1586 struct fs_dirent *dent;
1587 unsigned char *ipos;
1590 sqfs_split_path(&file, &dir, filename);
1592 * sqfs_opendir will uncompress inode and directory tables, and will
1593 * return a pointer to the directory that contains the requested file.
1595 ret = sqfs_opendir(dir, &dirsp);
1601 dirs = (struct squashfs_dir_stream *)dirsp;
1603 while (!sqfs_readdir(dirsp, &dent)) {
1604 ret = strcmp(dent->name, file);
1612 printf("File not found.\n");
1618 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1619 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1624 base = (struct squashfs_base_inode *)ipos;
1625 switch (get_unaligned_le16(&base->inode_type)) {
1627 reg = (struct squashfs_reg_inode *)ipos;
1628 *size = get_unaligned_le32(®->file_size);
1630 case SQFS_LREG_TYPE:
1631 lreg = (struct squashfs_lreg_inode *)ipos;
1632 *size = get_unaligned_le64(&lreg->file_size);
1634 case SQFS_SYMLINK_TYPE:
1635 case SQFS_LSYMLINK_TYPE:
1636 symlink = (struct squashfs_symlink_inode *)ipos;
1637 resolved = sqfs_resolve_symlink(symlink, filename);
1638 ret = sqfs_size(resolved, size);
1641 case SQFS_BLKDEV_TYPE:
1642 case SQFS_CHRDEV_TYPE:
1643 case SQFS_LBLKDEV_TYPE:
1644 case SQFS_LCHRDEV_TYPE:
1645 case SQFS_FIFO_TYPE:
1646 case SQFS_SOCKET_TYPE:
1647 case SQFS_LFIFO_TYPE:
1648 case SQFS_LSOCKET_TYPE:
1650 printf("Unable to recover entry's size.\n");
1660 sqfs_closedir(dirsp);
1665 int sqfs_exists(const char *filename)
1667 struct fs_dir_stream *dirsp = NULL;
1668 struct squashfs_dir_stream *dirs;
1670 struct fs_dirent *dent;
1673 sqfs_split_path(&file, &dir, filename);
1675 * sqfs_opendir will uncompress inode and directory tables, and will
1676 * return a pointer to the directory that contains the requested file.
1678 ret = sqfs_opendir(dir, &dirsp);
1684 dirs = (struct squashfs_dir_stream *)dirsp;
1686 while (!sqfs_readdir(dirsp, &dent)) {
1687 ret = strcmp(dent->name, file);
1694 sqfs_closedir(dirsp);
1703 void sqfs_close(void)
1705 sqfs_decompressor_cleanup(&ctxt);
1708 ctxt.cur_dev = NULL;
1711 void sqfs_closedir(struct fs_dir_stream *dirs)
1713 struct squashfs_dir_stream *sqfs_dirs;
1718 sqfs_dirs = (struct squashfs_dir_stream *)dirs;
1719 free(sqfs_dirs->inode_table);
1720 free(sqfs_dirs->dir_table);
1721 free(sqfs_dirs->dir_header);