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 struct squashfs_ctxt {
27 struct disk_partition cur_part_info;
28 struct blk_desc *cur_dev;
29 struct squashfs_super_block *sblk;
32 static struct squashfs_ctxt ctxt;
34 static int sqfs_disk_read(__u32 block, __u32 nr_blocks, void *buf)
41 ret = blk_dread(ctxt.cur_dev, ctxt.cur_part_info.start + block,
50 static int sqfs_read_sblk(struct squashfs_super_block **sblk)
52 *sblk = malloc_cache_aligned(ctxt.cur_dev->blksz);
56 if (sqfs_disk_read(0, 1, *sblk) != 1) {
64 static int sqfs_count_tokens(const char *filename)
66 int token_count = 1, l;
68 for (l = 1; l < strlen(filename); l++) {
69 if (filename[l] == '/')
73 /* Ignore trailing '/' in path */
74 if (filename[strlen(filename) - 1] == '/')
84 * Calculates how many blocks are needed for the buffer used in sqfs_disk_read.
85 * The memory section (e.g. inode table) start offset and its end (i.e. the next
86 * table start) must be specified. It also calculates the offset from which to
87 * start reading the buffer.
89 static int sqfs_calc_n_blks(__le64 start, __le64 end, u64 *offset)
91 u64 start_, table_size;
93 table_size = le64_to_cpu(end) - le64_to_cpu(start);
94 start_ = le64_to_cpu(start) / ctxt.cur_dev->blksz;
95 *offset = le64_to_cpu(start) - (start_ * ctxt.cur_dev->blksz);
97 return DIV_ROUND_UP(table_size + *offset, ctxt.cur_dev->blksz);
101 * Retrieves fragment block entry and returns true if the fragment block is
104 static int sqfs_frag_lookup(u32 inode_fragment_index,
105 struct squashfs_fragment_block_entry *e)
107 u64 start, n_blks, src_len, table_offset, start_block;
108 unsigned char *metadata_buffer, *metadata, *table;
109 struct squashfs_fragment_block_entry *entries;
110 struct squashfs_super_block *sblk = ctxt.sblk;
111 unsigned long dest_len;
112 int block, offset, ret;
113 u16 header, comp_type;
115 comp_type = get_unaligned_le16(&sblk->compression);
117 if (inode_fragment_index >= get_unaligned_le32(&sblk->fragments))
120 start = get_unaligned_le64(&sblk->fragment_table_start) /
122 n_blks = sqfs_calc_n_blks(sblk->fragment_table_start,
123 sblk->export_table_start,
126 /* Allocate a proper sized buffer to store the fragment index table */
127 table = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
131 if (sqfs_disk_read(start, n_blks, table) < 0) {
136 block = SQFS_FRAGMENT_INDEX(inode_fragment_index);
137 offset = SQFS_FRAGMENT_INDEX_OFFSET(inode_fragment_index);
140 * Get the start offset of the metadata block that contains the right
141 * fragment block entry
143 start_block = get_unaligned_le64(table + table_offset + block *
146 start = start_block / ctxt.cur_dev->blksz;
147 n_blks = sqfs_calc_n_blks(cpu_to_le64(start_block),
148 sblk->fragment_table_start, &table_offset);
150 metadata_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
151 if (!metadata_buffer) {
156 if (sqfs_disk_read(start, n_blks, metadata_buffer) < 0) {
161 /* Every metadata block starts with a 16-bit header */
162 header = get_unaligned_le16(metadata_buffer + table_offset);
163 metadata = metadata_buffer + table_offset + SQFS_HEADER_SIZE;
165 entries = malloc(SQFS_METADATA_BLOCK_SIZE);
171 if (SQFS_COMPRESSED_METADATA(header)) {
172 src_len = SQFS_METADATA_SIZE(header);
173 dest_len = SQFS_METADATA_BLOCK_SIZE;
174 ret = sqfs_decompress(comp_type, entries, &dest_len, metadata,
181 memcpy(entries, metadata, SQFS_METADATA_SIZE(header));
184 *e = entries[offset];
185 ret = SQFS_COMPRESSED_BLOCK(e->size);
190 free(metadata_buffer);
198 * The entry name is a flexible array member, and we don't know its size before
199 * actually reading the entry. So we need a first copy to retrieve this size so
200 * we can finally copy the whole struct.
202 static int sqfs_read_entry(struct squashfs_directory_entry **dest, void *src)
204 struct squashfs_directory_entry *tmp;
208 sz = get_unaligned_le16(src + sizeof(*tmp) - sizeof(u16));
210 * 'src' points to the begin of a directory entry, and 'sz' gets its
211 * 'name_size' member's value. name_size is actually the string
212 * length - 1, so adding 2 compensates this difference and adds space
213 * for the trailling null byte.
215 *dest = malloc(sizeof(*tmp) + sz + 2);
219 memcpy(*dest, src, sizeof(*tmp) + sz + 1);
220 (*dest)->name[sz + 1] = '\0';
225 static int sqfs_get_tokens_length(char **tokens, int count)
230 * 1 is added to the result of strlen to consider the slash separator
231 * between the tokens.
233 for (i = 0; i < count; i++)
234 length += strlen(tokens[i]) + 1;
239 /* Takes a token list and returns a single string with '/' as separator. */
240 static char *sqfs_concat_tokens(char **token_list, int token_count)
243 int i, length = 0, offset = 0;
245 length = sqfs_get_tokens_length(token_list, token_count);
247 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++)
315 * Remove last 'updir + 1' tokens from the base path tokens list. This leaves us
316 * with a token list containing only the tokens needed to form the resolved
317 * path, and returns the decremented size of the token list.
319 static int sqfs_clean_base_path(char **base, int count, int updir)
323 for (i = count - updir - 1; i < count; i++)
326 return count - updir - 1;
330 * Given the base ("current dir.") path and the relative one, generate the
333 static char *sqfs_get_abs_path(const char *base, const char *rel)
335 char **base_tokens, **rel_tokens, *resolved = NULL;
336 int ret, bc, rc, i, updir = 0, resolved_size = 0, offset = 0;
338 /* Memory allocation for the token lists */
339 bc = sqfs_count_tokens(base);
340 rc = sqfs_count_tokens(rel);
341 if (bc < 1 || rc < 1)
344 base_tokens = malloc(bc * sizeof(char *));
348 rel_tokens = malloc(rc * sizeof(char *));
352 /* Fill token lists */
353 ret = sqfs_tokenize(base_tokens, bc, base);
357 sqfs_tokenize(rel_tokens, rc, rel);
361 /* count '..' occurrences in target path */
362 for (i = 0; i < rc; i++) {
363 if (!strcmp(rel_tokens[i], ".."))
367 /* Remove the last token and the '..' occurrences */
368 bc = sqfs_clean_base_path(base_tokens, bc, updir);
372 /* Calculate resolved path size */
376 resolved_size += sqfs_get_tokens_length(base_tokens, bc) +
377 sqfs_get_tokens_length(rel_tokens, rc);
379 resolved = malloc(resolved_size + 1);
381 goto free_r_tokens_loop;
383 /* Set resolved path */
384 memset(resolved, '\0', resolved_size + 1);
385 offset += sqfs_join(base_tokens, resolved + offset, 0, bc, '/');
386 resolved[offset++] = '/';
387 offset += sqfs_join(rel_tokens, resolved + offset, updir, rc, '/');
390 for (i = 0; i < rc; i++)
392 for (i = 0; i < bc; i++)
393 free(base_tokens[i]);
402 static char *sqfs_resolve_symlink(struct squashfs_symlink_inode *sym,
403 const char *base_path)
405 char *resolved, *target;
408 sz = get_unaligned_le32(&sym->symlink_size);
409 target = malloc(sz + 1);
414 * There is no trailling null byte in the symlink's target path, so a
415 * copy is made and a '\0' is added at its end.
418 /* Get target name (relative path) */
419 strncpy(target, sym->symlink, sz);
421 /* Relative -> absolute path conversion */
422 resolved = sqfs_get_abs_path(base_path, target);
430 * m_list contains each metadata block's position, and m_count is the number of
431 * elements of m_list. Those metadata blocks come from the compressed directory
434 static int sqfs_search_dir(struct squashfs_dir_stream *dirs, char **token_list,
435 int token_count, u32 *m_list, int m_count)
437 struct squashfs_super_block *sblk = ctxt.sblk;
438 char *path, *target, **sym_tokens, *res, *rem;
439 int j, ret, new_inode_number, offset;
440 struct squashfs_symlink_inode *sym;
441 struct squashfs_ldir_inode *ldir;
442 struct squashfs_dir_inode *dir;
443 struct fs_dir_stream *dirsp;
444 struct fs_dirent *dent;
445 unsigned char *table;
447 dirsp = (struct fs_dir_stream *)dirs;
449 /* Start by root inode */
450 table = sqfs_find_inode(dirs->inode_table, le32_to_cpu(sblk->inodes),
451 sblk->inodes, sblk->block_size);
453 /* root is a regular directory, not an extended one */
454 dir = (struct squashfs_dir_inode *)table;
456 /* get directory offset in directory table */
457 offset = sqfs_dir_offset(table, m_list, m_count);
458 dirs->table = &dirs->dir_table[offset];
460 /* Setup directory header */
461 dirs->dir_header = malloc(SQFS_DIR_HEADER_SIZE);
462 if (!dirs->dir_header)
465 memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE);
467 /* Initialize squashfs_dir_stream members */
468 dirs->table += SQFS_DIR_HEADER_SIZE;
469 dirs->size = get_unaligned_le16(&dir->file_size) - SQFS_DIR_HEADER_SIZE;
470 dirs->entry_count = dirs->dir_header->count + 1;
472 /* No path given -> root directory */
473 if (!strcmp(token_list[0], "/")) {
474 dirs->table = &dirs->dir_table[offset];
475 memcpy(&dirs->i_dir, dir, sizeof(*dir));
479 for (j = 0; j < token_count; j++) {
480 if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) {
481 printf("** Cannot find directory. **\n");
485 while (!sqfs_readdir(dirsp, &dent)) {
486 ret = strcmp(dent->name, token_list[j]);
493 printf("** Cannot find directory. **\n");
497 /* Redefine inode as the found token */
498 new_inode_number = dirs->entry->inode_offset +
499 dirs->dir_header->inode_number;
501 /* Get reference to inode in the inode table */
502 table = sqfs_find_inode(dirs->inode_table, new_inode_number,
503 sblk->inodes, sblk->block_size);
504 dir = (struct squashfs_dir_inode *)table;
506 /* Check for symbolic link and inode type sanity */
507 if (get_unaligned_le16(&dir->inode_type) == SQFS_SYMLINK_TYPE) {
508 sym = (struct squashfs_symlink_inode *)table;
509 /* Get first j + 1 tokens */
510 path = sqfs_concat_tokens(token_list, j + 1);
511 /* Resolve for these tokens */
512 target = sqfs_resolve_symlink(sym, path);
513 /* Join remaining tokens */
514 rem = sqfs_concat_tokens(token_list + j + 1, token_count -
516 /* Concatenate remaining tokens and symlink's target */
517 res = malloc(strlen(rem) + strlen(target) + 1);
519 res[strlen(target)] = '/';
520 strcpy(res + strlen(target) + 1, rem);
521 token_count = sqfs_count_tokens(res);
526 sym_tokens = malloc(token_count * sizeof(char *));
530 /* Fill tokens list */
531 ret = sqfs_tokenize(sym_tokens, token_count, res);
536 ret = sqfs_search_dir(dirs, sym_tokens, token_count,
539 } else if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) {
540 printf("** Cannot find directory. **\n");
545 /* Check if it is an extended dir. */
546 if (get_unaligned_le16(&dir->inode_type) == SQFS_LDIR_TYPE)
547 ldir = (struct squashfs_ldir_inode *)table;
549 /* Get dir. offset into the directory table */
550 offset = sqfs_dir_offset(table, m_list, m_count);
551 dirs->table = &dirs->dir_table[offset];
553 /* Copy directory header */
554 memcpy(dirs->dir_header, &dirs->dir_table[offset],
555 SQFS_DIR_HEADER_SIZE);
557 /* Check for empty directory */
558 if (sqfs_is_empty_dir(table)) {
559 printf("Empty directory.\n");
561 return SQFS_EMPTY_DIR;
564 dirs->table += SQFS_DIR_HEADER_SIZE;
565 dirs->size = get_unaligned_le16(&dir->file_size);
566 dirs->entry_count = dirs->dir_header->count + 1;
567 dirs->size -= SQFS_DIR_HEADER_SIZE;
571 offset = sqfs_dir_offset(table, m_list, m_count);
572 dirs->table = &dirs->dir_table[offset];
574 if (get_unaligned_le16(&dir->inode_type) == SQFS_DIR_TYPE)
575 memcpy(&dirs->i_dir, dir, sizeof(*dir));
577 memcpy(&dirs->i_ldir, ldir, sizeof(*ldir));
583 * Inode and directory tables are stored as a series of metadata blocks, and
584 * given the compressed size of this table, we can calculate how much metadata
585 * blocks are needed to store the result of the decompression, since a
586 * decompressed metadata block should have a size of 8KiB.
588 static int sqfs_count_metablks(void *table, u32 offset, int table_size)
590 int count = 0, cur_size = 0, ret;
595 ret = sqfs_read_metablock(table, offset + cur_size, &comp,
599 cur_size += data_size + SQFS_HEADER_SIZE;
601 } while (cur_size < table_size);
607 * Storing the metadata blocks header's positions will be useful while looking
608 * for an entry in the directory table, using the reference (index and offset)
609 * given by its inode.
611 static int sqfs_get_metablk_pos(u32 *pos_list, void *table, u32 offset,
614 u32 data_size, cur_size = 0;
621 for (j = 0; j < metablks_count; j++) {
622 ret = sqfs_read_metablock(table, offset + cur_size, &comp,
627 cur_size += data_size + SQFS_HEADER_SIZE;
628 pos_list[j] = cur_size;
634 static int sqfs_read_inode_table(unsigned char **inode_table)
636 struct squashfs_super_block *sblk = ctxt.sblk;
637 u64 start, n_blks, table_offset, table_size;
638 int j, ret = 0, metablks_count, comp_type;
639 unsigned char *src_table, *itb;
640 u32 src_len, dest_offset = 0;
641 unsigned long dest_len;
644 comp_type = get_unaligned_le16(&sblk->compression);
645 table_size = get_unaligned_le64(&sblk->directory_table_start) -
646 get_unaligned_le64(&sblk->inode_table_start);
647 start = get_unaligned_le64(&sblk->inode_table_start) /
649 n_blks = sqfs_calc_n_blks(sblk->inode_table_start,
650 sblk->directory_table_start, &table_offset);
652 /* Allocate a proper sized buffer (itb) to store the inode table */
653 itb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
657 if (sqfs_disk_read(start, n_blks, itb) < 0) {
662 /* Parse inode table (metadata block) header */
663 ret = sqfs_read_metablock(itb, table_offset, &compressed, &src_len);
669 /* Calculate size to store the whole decompressed table */
670 metablks_count = sqfs_count_metablks(itb, table_offset, table_size);
671 if (metablks_count < 1) {
676 *inode_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE);
682 src_table = itb + table_offset + SQFS_HEADER_SIZE;
684 /* Extract compressed Inode table */
685 for (j = 0; j < metablks_count; j++) {
686 sqfs_read_metablock(itb, table_offset, &compressed, &src_len);
688 dest_len = SQFS_METADATA_BLOCK_SIZE;
689 ret = sqfs_decompress(comp_type, *inode_table +
690 dest_offset, &dest_len,
698 memcpy(*inode_table + (j * SQFS_METADATA_BLOCK_SIZE),
703 * Offsets to the decompression destination, to the metadata
704 * buffer 'itb' and to the decompression source, respectively.
706 dest_offset += dest_len;
707 table_offset += src_len + SQFS_HEADER_SIZE;
708 src_table += src_len + SQFS_HEADER_SIZE;
717 static int sqfs_read_directory_table(unsigned char **dir_table, u32 **pos_list)
719 u64 start, n_blks, table_offset, table_size;
720 int j, ret = 0, metablks_count = -1, comp_type;
721 struct squashfs_super_block *sblk = ctxt.sblk;
722 unsigned char *src_table, *dtb;
723 u32 src_len, dest_offset = 0;
724 unsigned long dest_len;
727 comp_type = get_unaligned_le16(&sblk->compression);
729 /* DIRECTORY TABLE */
730 table_size = get_unaligned_le64(&sblk->fragment_table_start) -
731 get_unaligned_le64(&sblk->directory_table_start);
732 start = get_unaligned_le64(&sblk->directory_table_start) /
734 n_blks = sqfs_calc_n_blks(sblk->directory_table_start,
735 sblk->fragment_table_start, &table_offset);
737 /* Allocate a proper sized buffer (dtb) to store the directory table */
738 dtb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
742 if (sqfs_disk_read(start, n_blks, dtb) < 0)
745 /* Parse directory table (metadata block) header */
746 ret = sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
750 /* Calculate total size to store the whole decompressed table */
751 metablks_count = sqfs_count_metablks(dtb, table_offset, table_size);
752 if (metablks_count < 1)
755 *dir_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE);
759 *pos_list = malloc(metablks_count * sizeof(u32));
765 ret = sqfs_get_metablk_pos(*pos_list, dtb, table_offset,
774 src_table = dtb + table_offset + SQFS_HEADER_SIZE;
776 /* Extract compressed Directory table */
778 for (j = 0; j < metablks_count; j++) {
779 sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
781 dest_len = SQFS_METADATA_BLOCK_SIZE;
782 ret = sqfs_decompress(comp_type, *dir_table +
783 (j * SQFS_METADATA_BLOCK_SIZE),
784 &dest_len, src_table, src_len);
791 if (dest_len < SQFS_METADATA_BLOCK_SIZE) {
792 dest_offset += dest_len;
796 memcpy(*dir_table + (j * SQFS_METADATA_BLOCK_SIZE),
801 * Offsets to the decompression destination, to the metadata
802 * buffer 'dtb' and to the decompression source, respectively.
804 dest_offset += dest_len;
805 table_offset += src_len + SQFS_HEADER_SIZE;
806 src_table += src_len + SQFS_HEADER_SIZE;
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, ret = 0, metablks_count;
819 struct squashfs_dir_stream *dirs;
820 char **token_list, *path;
821 u32 *pos_list = NULL;
823 dirs = malloc(sizeof(*dirs));
827 ret = sqfs_read_inode_table(&inode_table);
831 metablks_count = sqfs_read_directory_table(&dir_table, &pos_list);
832 if (metablks_count < 1)
835 /* Tokenize filename */
836 token_count = sqfs_count_tokens(filename);
840 path = strdup(filename);
844 token_list = malloc(token_count * sizeof(char *));
850 /* Fill tokens list */
851 ret = sqfs_tokenize(token_list, token_count, path);
855 * ldir's (extended directory) size is greater than dir, so it works as
856 * a general solution for the malloc size, since 'i' is a union.
858 dirs->inode_table = inode_table;
859 dirs->dir_table = dir_table;
860 ret = sqfs_search_dir(dirs, token_list, token_count, pos_list,
865 if (le16_to_cpu(dirs->i_dir.inode_type) == SQFS_DIR_TYPE)
866 dirs->size = le16_to_cpu(dirs->i_dir.file_size);
868 dirs->size = le32_to_cpu(dirs->i_ldir.file_size);
870 /* Setup directory header */
871 memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE);
872 dirs->entry_count = dirs->dir_header->count + 1;
873 dirs->size -= SQFS_DIR_HEADER_SIZE;
877 dirs->table += SQFS_DIR_HEADER_SIZE;
879 *dirsp = (struct fs_dir_stream *)dirs;
882 for (j = 0; j < token_count; j++)
892 int sqfs_readdir(struct fs_dir_stream *fs_dirs, struct fs_dirent **dentp)
894 struct squashfs_super_block *sblk = ctxt.sblk;
895 struct squashfs_dir_stream *dirs;
896 struct squashfs_lreg_inode *lreg;
897 struct squashfs_base_inode *base;
898 struct squashfs_reg_inode *reg;
899 int i_number, offset = 0, ret;
900 struct fs_dirent *dent;
903 dirs = (struct squashfs_dir_stream *)fs_dirs;
906 return -SQFS_STOP_READDIR;
911 if (!dirs->entry_count) {
912 if (dirs->size > SQFS_DIR_HEADER_SIZE) {
913 dirs->size -= SQFS_DIR_HEADER_SIZE;
917 return -SQFS_STOP_READDIR;
920 if (dirs->size > SQFS_EMPTY_FILE_SIZE) {
921 /* Read follow-up (emitted) dir. header */
922 memcpy(dirs->dir_header, dirs->table,
923 SQFS_DIR_HEADER_SIZE);
924 dirs->entry_count = dirs->dir_header->count + 1;
925 ret = sqfs_read_entry(&dirs->entry, dirs->table +
926 SQFS_DIR_HEADER_SIZE);
928 return -SQFS_STOP_READDIR;
930 dirs->table += SQFS_DIR_HEADER_SIZE;
933 ret = sqfs_read_entry(&dirs->entry, dirs->table);
935 return -SQFS_STOP_READDIR;
938 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
939 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
942 base = (struct squashfs_base_inode *)ipos;
944 /* Set entry type and size */
945 switch (dirs->entry->type) {
948 dent->type = FS_DT_DIR;
953 * Entries do not differentiate extended from regular types, so
954 * it needs to be verified manually.
956 if (get_unaligned_le16(&base->inode_type) == SQFS_LREG_TYPE) {
957 lreg = (struct squashfs_lreg_inode *)ipos;
958 dent->size = get_unaligned_le64(&lreg->file_size);
960 reg = (struct squashfs_reg_inode *)ipos;
961 dent->size = get_unaligned_le32(®->file_size);
964 dent->type = FS_DT_REG;
966 case SQFS_BLKDEV_TYPE:
967 case SQFS_CHRDEV_TYPE:
968 case SQFS_LBLKDEV_TYPE:
969 case SQFS_LCHRDEV_TYPE:
971 case SQFS_SOCKET_TYPE:
972 case SQFS_LFIFO_TYPE:
973 case SQFS_LSOCKET_TYPE:
974 dent->type = SQFS_MISC_ENTRY_TYPE;
976 case SQFS_SYMLINK_TYPE:
977 case SQFS_LSYMLINK_TYPE:
978 dent->type = FS_DT_LNK;
981 return -SQFS_STOP_READDIR;
985 strncpy(dent->name, dirs->entry->name, dirs->entry->name_size + 1);
986 dent->name[dirs->entry->name_size + 1] = '\0';
988 offset = dirs->entry->name_size + 1 + SQFS_ENTRY_BASE_LENGTH;
991 /* Decrement size to be read */
992 if (dirs->size > offset)
993 dirs->size -= offset;
997 /* Keep a reference to the current entry before incrementing it */
998 dirs->table += offset;
1005 int sqfs_probe(struct blk_desc *fs_dev_desc, struct disk_partition *fs_partition)
1007 struct squashfs_super_block *sblk;
1010 ctxt.cur_dev = fs_dev_desc;
1011 ctxt.cur_part_info = *fs_partition;
1013 ret = sqfs_read_sblk(&sblk);
1017 /* Make sure it has a valid SquashFS magic number*/
1018 if (get_unaligned_le32(&sblk->s_magic) != SQFS_MAGIC_NUMBER) {
1019 printf("Bad magic number for SquashFS image.\n");
1020 ctxt.cur_dev = NULL;
1029 static char *sqfs_basename(char *path)
1033 fname = path + strlen(path) - 1;
1034 while (fname >= path) {
1035 if (*fname == '/') {
1046 static char *sqfs_dirname(char *path)
1050 fname = sqfs_basename(path);
1058 * Takes a path to file and splits it in two parts: the filename itself and the
1059 * directory's path, e.g.:
1060 * path: /path/to/file.txt
1064 static int sqfs_split_path(char **file, char **dir, const char *path)
1066 char *dirc, *basec, *bname, *dname, *tmp_path;
1069 /* check for first slash in path*/
1070 if (path[0] == '/') {
1071 tmp_path = strdup(path);
1075 tmp_path = malloc(strlen(path) + 2);
1079 strcpy(tmp_path + 1, path);
1082 /* String duplicates */
1083 dirc = strdup(tmp_path);
1089 basec = strdup(tmp_path);
1095 dname = sqfs_dirname(dirc);
1096 bname = sqfs_basename(basec);
1098 *file = strdup(bname);
1105 if (*dname == '\0') {
1115 *dir = strdup(dname);
1132 static int sqfs_get_regfile_info(struct squashfs_reg_inode *reg,
1133 struct squashfs_file_info *finfo,
1134 struct squashfs_fragment_block_entry *fentry,
1137 int datablk_count = 0, ret;
1139 finfo->size = get_unaligned_le32(®->file_size);
1140 finfo->offset = get_unaligned_le32(®->offset);
1141 finfo->start = get_unaligned_le32(®->start_block);
1142 finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(®->fragment));
1145 datablk_count = finfo->size / le32_to_cpu(blksz);
1146 ret = sqfs_frag_lookup(get_unaligned_le32(®->fragment),
1152 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1155 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1156 if (!finfo->blk_sizes)
1159 return datablk_count;
1162 static int sqfs_get_lregfile_info(struct squashfs_lreg_inode *lreg,
1163 struct squashfs_file_info *finfo,
1164 struct squashfs_fragment_block_entry *fentry,
1167 int datablk_count = 0, ret;
1169 finfo->size = get_unaligned_le64(&lreg->file_size);
1170 finfo->offset = get_unaligned_le32(&lreg->offset);
1171 finfo->start = get_unaligned_le64(&lreg->start_block);
1172 finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(&lreg->fragment));
1175 datablk_count = finfo->size / le32_to_cpu(blksz);
1176 ret = sqfs_frag_lookup(get_unaligned_le32(&lreg->fragment),
1182 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1185 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1186 if (!finfo->blk_sizes)
1189 return datablk_count;
1192 int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
1195 char *dir, *fragment_block, *datablock = NULL, *data_buffer = NULL;
1196 char *fragment, *file, *resolved, *data;
1197 u64 start, n_blks, table_size, data_offset, table_offset;
1198 int ret, j, i_number, comp_type, datablk_count = 0;
1199 struct squashfs_super_block *sblk = ctxt.sblk;
1200 struct squashfs_fragment_block_entry frag_entry;
1201 struct squashfs_file_info finfo = {0};
1202 struct squashfs_symlink_inode *symlink;
1203 struct fs_dir_stream *dirsp = NULL;
1204 struct squashfs_dir_stream *dirs;
1205 struct squashfs_lreg_inode *lreg;
1206 struct squashfs_base_inode *base;
1207 struct squashfs_reg_inode *reg;
1208 unsigned long dest_len;
1209 struct fs_dirent *dent;
1210 unsigned char *ipos;
1214 comp_type = get_unaligned_le16(&sblk->compression);
1217 * sqfs_opendir will uncompress inode and directory tables, and will
1218 * return a pointer to the directory that contains the requested file.
1220 sqfs_split_path(&file, &dir, filename);
1221 ret = sqfs_opendir(dir, &dirsp);
1223 sqfs_closedir(dirsp);
1227 dirs = (struct squashfs_dir_stream *)dirsp;
1229 /* For now, only regular files are able to be loaded */
1230 while (!sqfs_readdir(dirsp, &dent)) {
1231 ret = strcmp(dent->name, file);
1239 printf("File not found.\n");
1241 sqfs_closedir(dirsp);
1246 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1247 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1250 base = (struct squashfs_base_inode *)ipos;
1251 switch (get_unaligned_le16(&base->inode_type)) {
1253 reg = (struct squashfs_reg_inode *)ipos;
1254 datablk_count = sqfs_get_regfile_info(reg, &finfo, &frag_entry,
1256 if (datablk_count < 0) {
1261 memcpy(finfo.blk_sizes, ipos + sizeof(*reg),
1262 datablk_count * sizeof(u32));
1264 case SQFS_LREG_TYPE:
1265 lreg = (struct squashfs_lreg_inode *)ipos;
1266 datablk_count = sqfs_get_lregfile_info(lreg, &finfo,
1269 if (datablk_count < 0) {
1274 memcpy(finfo.blk_sizes, ipos + sizeof(*lreg),
1275 datablk_count * sizeof(u32));
1277 case SQFS_SYMLINK_TYPE:
1278 case SQFS_LSYMLINK_TYPE:
1279 symlink = (struct squashfs_symlink_inode *)ipos;
1280 resolved = sqfs_resolve_symlink(symlink, filename);
1281 ret = sqfs_read(resolved, buf, offset, len, actread);
1284 case SQFS_BLKDEV_TYPE:
1285 case SQFS_CHRDEV_TYPE:
1286 case SQFS_LBLKDEV_TYPE:
1287 case SQFS_LCHRDEV_TYPE:
1288 case SQFS_FIFO_TYPE:
1289 case SQFS_SOCKET_TYPE:
1290 case SQFS_LFIFO_TYPE:
1291 case SQFS_LSOCKET_TYPE:
1293 printf("Unsupported entry type\n");
1298 /* If the user specifies a length, check its sanity */
1300 if (len > finfo.size) {
1308 if (datablk_count) {
1309 data_offset = finfo.start;
1310 datablock = malloc(get_unaligned_le32(&sblk->block_size));
1317 for (j = 0; j < datablk_count; j++) {
1318 start = data_offset / ctxt.cur_dev->blksz;
1319 table_size = SQFS_BLOCK_SIZE(finfo.blk_sizes[j]);
1320 table_offset = data_offset - (start * ctxt.cur_dev->blksz);
1321 n_blks = DIV_ROUND_UP(table_size + table_offset,
1322 ctxt.cur_dev->blksz);
1324 data_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
1331 ret = sqfs_disk_read(start, n_blks, data_buffer);
1334 * Possible causes: too many data blocks or too large
1335 * SquashFS block size. Tip: re-compile the SquashFS
1336 * image with mksquashfs's -b <block_size> option.
1338 printf("Error: too many data blocks to be read.\n");
1342 data = data_buffer + table_offset;
1345 if (SQFS_COMPRESSED_BLOCK(finfo.blk_sizes[j])) {
1346 dest_len = get_unaligned_le32(&sblk->block_size);
1347 ret = sqfs_decompress(comp_type, datablock, &dest_len,
1352 memcpy(buf + offset + *actread, datablock, dest_len);
1353 *actread += dest_len;
1355 memcpy(buf + offset + *actread, data, table_size);
1356 *actread += table_size;
1359 data_offset += table_size;
1362 free(finfo.blk_sizes);
1365 * There is no need to continue if the file is not fragmented.
1372 start = frag_entry.start / ctxt.cur_dev->blksz;
1373 table_size = SQFS_BLOCK_SIZE(frag_entry.size);
1374 table_offset = frag_entry.start - (start * ctxt.cur_dev->blksz);
1375 n_blks = DIV_ROUND_UP(table_size + table_offset, ctxt.cur_dev->blksz);
1377 fragment = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
1384 ret = sqfs_disk_read(start, n_blks, fragment);
1388 /* File compressed and fragmented */
1389 if (finfo.frag && finfo.comp) {
1390 dest_len = get_unaligned_le32(&sblk->block_size);
1391 fragment_block = malloc(dest_len);
1392 if (!fragment_block) {
1397 ret = sqfs_decompress(comp_type, fragment_block, &dest_len,
1398 (void *)fragment + table_offset,
1401 free(fragment_block);
1405 for (j = offset + *actread; j < finfo.size; j++) {
1406 memcpy(buf + j, &fragment_block[finfo.offset + j], 1);
1410 free(fragment_block);
1412 } else if (finfo.frag && !finfo.comp) {
1413 fragment_block = (void *)fragment + table_offset;
1415 for (j = offset + *actread; j < finfo.size; j++) {
1416 memcpy(buf + j, &fragment_block[finfo.offset + j], 1);
1436 int sqfs_size(const char *filename, loff_t *size)
1438 struct squashfs_super_block *sblk = ctxt.sblk;
1439 struct squashfs_symlink_inode *symlink;
1440 struct fs_dir_stream *dirsp = NULL;
1441 struct squashfs_base_inode *base;
1442 struct squashfs_dir_stream *dirs;
1443 struct squashfs_lreg_inode *lreg;
1444 struct squashfs_reg_inode *reg;
1445 char *dir, *file, *resolved;
1446 struct fs_dirent *dent;
1447 unsigned char *ipos;
1450 sqfs_split_path(&file, &dir, filename);
1452 * sqfs_opendir will uncompress inode and directory tables, and will
1453 * return a pointer to the directory that contains the requested file.
1455 ret = sqfs_opendir(dir, &dirsp);
1457 sqfs_closedir(dirsp);
1462 dirs = (struct squashfs_dir_stream *)dirsp;
1464 while (!sqfs_readdir(dirsp, &dent)) {
1465 ret = strcmp(dent->name, file);
1472 printf("File not found.\n");
1478 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1479 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1483 base = (struct squashfs_base_inode *)ipos;
1484 switch (get_unaligned_le16(&base->inode_type)) {
1486 reg = (struct squashfs_reg_inode *)ipos;
1487 *size = get_unaligned_le32(®->file_size);
1489 case SQFS_LREG_TYPE:
1490 lreg = (struct squashfs_lreg_inode *)ipos;
1491 *size = get_unaligned_le64(&lreg->file_size);
1493 case SQFS_SYMLINK_TYPE:
1494 case SQFS_LSYMLINK_TYPE:
1495 symlink = (struct squashfs_symlink_inode *)ipos;
1496 resolved = sqfs_resolve_symlink(symlink, filename);
1497 ret = sqfs_size(resolved, size);
1500 case SQFS_BLKDEV_TYPE:
1501 case SQFS_CHRDEV_TYPE:
1502 case SQFS_LBLKDEV_TYPE:
1503 case SQFS_LCHRDEV_TYPE:
1504 case SQFS_FIFO_TYPE:
1505 case SQFS_SOCKET_TYPE:
1506 case SQFS_LFIFO_TYPE:
1507 case SQFS_LSOCKET_TYPE:
1509 printf("Unable to recover entry's size.\n");
1519 sqfs_closedir(dirsp);
1524 void sqfs_close(void)
1527 ctxt.cur_dev = NULL;
1530 void sqfs_closedir(struct fs_dir_stream *dirs)
1532 struct squashfs_dir_stream *sqfs_dirs;
1534 sqfs_dirs = (struct squashfs_dir_stream *)dirs;
1535 free(sqfs_dirs->inode_table);
1536 free(sqfs_dirs->dir_table);
1537 free(sqfs_dirs->dir_header);