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 entries = malloc(SQFS_METADATA_BLOCK_SIZE);
163 if (SQFS_COMPRESSED_METADATA(header)) {
164 src_len = SQFS_METADATA_SIZE(header);
165 dest_len = SQFS_METADATA_BLOCK_SIZE;
166 ret = sqfs_decompress(&ctxt, entries, &dest_len, metadata,
173 memcpy(entries, metadata, SQFS_METADATA_SIZE(header));
176 *e = entries[offset];
177 ret = SQFS_COMPRESSED_BLOCK(e->size);
182 free(metadata_buffer);
190 * The entry name is a flexible array member, and we don't know its size before
191 * actually reading the entry. So we need a first copy to retrieve this size so
192 * we can finally copy the whole struct.
194 static int sqfs_read_entry(struct squashfs_directory_entry **dest, void *src)
196 struct squashfs_directory_entry *tmp;
200 sz = get_unaligned_le16(src + sizeof(*tmp) - sizeof(u16));
202 * 'src' points to the begin of a directory entry, and 'sz' gets its
203 * 'name_size' member's value. name_size is actually the string
204 * length - 1, so adding 2 compensates this difference and adds space
205 * for the trailling null byte.
207 *dest = malloc(sizeof(*tmp) + sz + 2);
211 memcpy(*dest, src, sizeof(*tmp) + sz + 1);
212 (*dest)->name[sz + 1] = '\0';
217 static int sqfs_get_tokens_length(char **tokens, int count)
222 * 1 is added to the result of strlen to consider the slash separator
223 * between the tokens.
225 for (i = 0; i < count; i++)
226 length += strlen(tokens[i]) + 1;
231 /* Takes a token list and returns a single string with '/' as separator. */
232 static char *sqfs_concat_tokens(char **token_list, int token_count)
235 int i, length = 0, offset = 0;
237 length = sqfs_get_tokens_length(token_list, token_count);
239 result = malloc(length + 1);
240 result[length] = '\0';
242 for (i = 0; i < token_count; i++) {
243 strcpy(result + offset, token_list[i]);
244 offset += strlen(token_list[i]);
245 result[offset++] = '/';
252 * Differently from sqfs_concat_tokens, sqfs_join writes the result into a
253 * previously allocated string, and returns the number of bytes written.
255 static int sqfs_join(char **strings, char *dest, int start, int end,
260 for (i = start; i < end; i++) {
261 strcpy(dest + offset, strings[i]);
262 offset += strlen(strings[i]);
264 dest[offset++] = separator;
271 * Fills the given token list using its size (count) and a source string (str)
273 static int sqfs_tokenize(char **tokens, int count, const char *str)
282 if (!strcmp(strc, "/")) {
283 tokens[0] = strdup(strc);
289 for (j = 0; j < count; j++) {
290 aux = strtok(!j ? strc : NULL, "/");
291 tokens[j] = strdup(aux);
293 for (i = 0; i < j; i++)
307 * Remove last 'updir + 1' tokens from the base path tokens list. This leaves us
308 * with a token list containing only the tokens needed to form the resolved
309 * path, and returns the decremented size of the token list.
311 static int sqfs_clean_base_path(char **base, int count, int updir)
315 for (i = count - updir - 1; i < count; i++)
318 return count - updir - 1;
322 * Given the base ("current dir.") path and the relative one, generate the
325 static char *sqfs_get_abs_path(const char *base, const char *rel)
327 char **base_tokens, **rel_tokens, *resolved = NULL;
328 int ret, bc, rc, i, updir = 0, resolved_size = 0, offset = 0;
330 /* Memory allocation for the token lists */
331 bc = sqfs_count_tokens(base);
332 rc = sqfs_count_tokens(rel);
333 if (bc < 1 || rc < 1)
336 base_tokens = malloc(bc * sizeof(char *));
340 rel_tokens = malloc(rc * sizeof(char *));
344 /* Fill token lists */
345 ret = sqfs_tokenize(base_tokens, bc, base);
349 sqfs_tokenize(rel_tokens, rc, rel);
353 /* count '..' occurrences in target path */
354 for (i = 0; i < rc; i++) {
355 if (!strcmp(rel_tokens[i], ".."))
359 /* Remove the last token and the '..' occurrences */
360 bc = sqfs_clean_base_path(base_tokens, bc, updir);
364 /* Calculate resolved path size */
368 resolved_size += sqfs_get_tokens_length(base_tokens, bc) +
369 sqfs_get_tokens_length(rel_tokens, rc);
371 resolved = malloc(resolved_size + 1);
373 goto free_r_tokens_loop;
375 /* Set resolved path */
376 memset(resolved, '\0', resolved_size + 1);
377 offset += sqfs_join(base_tokens, resolved + offset, 0, bc, '/');
378 resolved[offset++] = '/';
379 offset += sqfs_join(rel_tokens, resolved + offset, updir, rc, '/');
382 for (i = 0; i < rc; i++)
384 for (i = 0; i < bc; i++)
385 free(base_tokens[i]);
394 static char *sqfs_resolve_symlink(struct squashfs_symlink_inode *sym,
395 const char *base_path)
397 char *resolved, *target;
400 sz = get_unaligned_le32(&sym->symlink_size);
401 target = malloc(sz + 1);
406 * There is no trailling null byte in the symlink's target path, so a
407 * copy is made and a '\0' is added at its end.
410 /* Get target name (relative path) */
411 strncpy(target, sym->symlink, sz);
413 /* Relative -> absolute path conversion */
414 resolved = sqfs_get_abs_path(base_path, target);
422 * m_list contains each metadata block's position, and m_count is the number of
423 * elements of m_list. Those metadata blocks come from the compressed directory
426 static int sqfs_search_dir(struct squashfs_dir_stream *dirs, char **token_list,
427 int token_count, u32 *m_list, int m_count)
429 struct squashfs_super_block *sblk = ctxt.sblk;
430 char *path, *target, **sym_tokens, *res, *rem;
431 int j, ret, new_inode_number, offset;
432 struct squashfs_symlink_inode *sym;
433 struct squashfs_ldir_inode *ldir;
434 struct squashfs_dir_inode *dir;
435 struct fs_dir_stream *dirsp;
436 struct fs_dirent *dent;
437 unsigned char *table;
439 dirsp = (struct fs_dir_stream *)dirs;
441 /* Start by root inode */
442 table = sqfs_find_inode(dirs->inode_table, le32_to_cpu(sblk->inodes),
443 sblk->inodes, sblk->block_size);
445 /* root is a regular directory, not an extended one */
446 dir = (struct squashfs_dir_inode *)table;
448 /* get directory offset in directory table */
449 offset = sqfs_dir_offset(table, m_list, m_count);
450 dirs->table = &dirs->dir_table[offset];
452 /* Setup directory header */
453 dirs->dir_header = malloc(SQFS_DIR_HEADER_SIZE);
454 if (!dirs->dir_header)
457 memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE);
459 /* Initialize squashfs_dir_stream members */
460 dirs->table += SQFS_DIR_HEADER_SIZE;
461 dirs->size = get_unaligned_le16(&dir->file_size) - SQFS_DIR_HEADER_SIZE;
462 dirs->entry_count = dirs->dir_header->count + 1;
464 /* No path given -> root directory */
465 if (!strcmp(token_list[0], "/")) {
466 dirs->table = &dirs->dir_table[offset];
467 memcpy(&dirs->i_dir, dir, sizeof(*dir));
471 for (j = 0; j < token_count; j++) {
472 if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) {
473 printf("** Cannot find directory. **\n");
477 while (!sqfs_readdir(dirsp, &dent)) {
478 ret = strcmp(dent->name, token_list[j]);
485 printf("** Cannot find directory. **\n");
489 /* Redefine inode as the found token */
490 new_inode_number = dirs->entry->inode_offset +
491 dirs->dir_header->inode_number;
493 /* Get reference to inode in the inode table */
494 table = sqfs_find_inode(dirs->inode_table, new_inode_number,
495 sblk->inodes, sblk->block_size);
496 dir = (struct squashfs_dir_inode *)table;
498 /* Check for symbolic link and inode type sanity */
499 if (get_unaligned_le16(&dir->inode_type) == SQFS_SYMLINK_TYPE) {
500 sym = (struct squashfs_symlink_inode *)table;
501 /* Get first j + 1 tokens */
502 path = sqfs_concat_tokens(token_list, j + 1);
503 /* Resolve for these tokens */
504 target = sqfs_resolve_symlink(sym, path);
505 /* Join remaining tokens */
506 rem = sqfs_concat_tokens(token_list + j + 1, token_count -
508 /* Concatenate remaining tokens and symlink's target */
509 res = malloc(strlen(rem) + strlen(target) + 1);
511 res[strlen(target)] = '/';
512 strcpy(res + strlen(target) + 1, rem);
513 token_count = sqfs_count_tokens(res);
518 sym_tokens = malloc(token_count * sizeof(char *));
522 /* Fill tokens list */
523 ret = sqfs_tokenize(sym_tokens, token_count, res);
528 ret = sqfs_search_dir(dirs, sym_tokens, token_count,
531 } else if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) {
532 printf("** Cannot find directory. **\n");
537 /* Check if it is an extended dir. */
538 if (get_unaligned_le16(&dir->inode_type) == SQFS_LDIR_TYPE)
539 ldir = (struct squashfs_ldir_inode *)table;
541 /* Get dir. offset into the directory table */
542 offset = sqfs_dir_offset(table, m_list, m_count);
543 dirs->table = &dirs->dir_table[offset];
545 /* Copy directory header */
546 memcpy(dirs->dir_header, &dirs->dir_table[offset],
547 SQFS_DIR_HEADER_SIZE);
549 /* Check for empty directory */
550 if (sqfs_is_empty_dir(table)) {
551 printf("Empty directory.\n");
553 return SQFS_EMPTY_DIR;
556 dirs->table += SQFS_DIR_HEADER_SIZE;
557 dirs->size = get_unaligned_le16(&dir->file_size);
558 dirs->entry_count = dirs->dir_header->count + 1;
559 dirs->size -= SQFS_DIR_HEADER_SIZE;
563 offset = sqfs_dir_offset(table, m_list, m_count);
564 dirs->table = &dirs->dir_table[offset];
566 if (get_unaligned_le16(&dir->inode_type) == SQFS_DIR_TYPE)
567 memcpy(&dirs->i_dir, dir, sizeof(*dir));
569 memcpy(&dirs->i_ldir, ldir, sizeof(*ldir));
575 * Inode and directory tables are stored as a series of metadata blocks, and
576 * given the compressed size of this table, we can calculate how much metadata
577 * blocks are needed to store the result of the decompression, since a
578 * decompressed metadata block should have a size of 8KiB.
580 static int sqfs_count_metablks(void *table, u32 offset, int table_size)
582 int count = 0, cur_size = 0, ret;
587 ret = sqfs_read_metablock(table, offset + cur_size, &comp,
591 cur_size += data_size + SQFS_HEADER_SIZE;
593 } while (cur_size < table_size);
599 * Storing the metadata blocks header's positions will be useful while looking
600 * for an entry in the directory table, using the reference (index and offset)
601 * given by its inode.
603 static int sqfs_get_metablk_pos(u32 *pos_list, void *table, u32 offset,
606 u32 data_size, cur_size = 0;
613 for (j = 0; j < metablks_count; j++) {
614 ret = sqfs_read_metablock(table, offset + cur_size, &comp,
619 cur_size += data_size + SQFS_HEADER_SIZE;
620 pos_list[j] = cur_size;
626 static int sqfs_read_inode_table(unsigned char **inode_table)
628 struct squashfs_super_block *sblk = ctxt.sblk;
629 u64 start, n_blks, table_offset, table_size;
630 int j, ret = 0, metablks_count;
631 unsigned char *src_table, *itb;
632 u32 src_len, dest_offset = 0;
633 unsigned long dest_len;
636 table_size = get_unaligned_le64(&sblk->directory_table_start) -
637 get_unaligned_le64(&sblk->inode_table_start);
638 start = get_unaligned_le64(&sblk->inode_table_start) /
640 n_blks = sqfs_calc_n_blks(sblk->inode_table_start,
641 sblk->directory_table_start, &table_offset);
643 /* Allocate a proper sized buffer (itb) to store the inode table */
644 itb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
648 if (sqfs_disk_read(start, n_blks, itb) < 0) {
653 /* Parse inode table (metadata block) header */
654 ret = sqfs_read_metablock(itb, table_offset, &compressed, &src_len);
660 /* Calculate size to store the whole decompressed table */
661 metablks_count = sqfs_count_metablks(itb, table_offset, table_size);
662 if (metablks_count < 1) {
667 *inode_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE);
673 src_table = itb + table_offset + SQFS_HEADER_SIZE;
675 /* Extract compressed Inode table */
676 for (j = 0; j < metablks_count; j++) {
677 sqfs_read_metablock(itb, table_offset, &compressed, &src_len);
679 dest_len = SQFS_METADATA_BLOCK_SIZE;
680 ret = sqfs_decompress(&ctxt, *inode_table +
681 dest_offset, &dest_len,
689 memcpy(*inode_table + (j * SQFS_METADATA_BLOCK_SIZE),
694 * Offsets to the decompression destination, to the metadata
695 * buffer 'itb' and to the decompression source, respectively.
697 dest_offset += dest_len;
698 table_offset += src_len + SQFS_HEADER_SIZE;
699 src_table += src_len + SQFS_HEADER_SIZE;
708 static int sqfs_read_directory_table(unsigned char **dir_table, u32 **pos_list)
710 u64 start, n_blks, table_offset, table_size;
711 struct squashfs_super_block *sblk = ctxt.sblk;
712 int j, ret = 0, metablks_count = -1;
713 unsigned char *src_table, *dtb;
714 u32 src_len, dest_offset = 0;
715 unsigned long dest_len;
718 /* DIRECTORY TABLE */
719 table_size = get_unaligned_le64(&sblk->fragment_table_start) -
720 get_unaligned_le64(&sblk->directory_table_start);
721 start = get_unaligned_le64(&sblk->directory_table_start) /
723 n_blks = sqfs_calc_n_blks(sblk->directory_table_start,
724 sblk->fragment_table_start, &table_offset);
726 /* Allocate a proper sized buffer (dtb) to store the directory table */
727 dtb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
731 if (sqfs_disk_read(start, n_blks, dtb) < 0)
734 /* Parse directory table (metadata block) header */
735 ret = sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
739 /* Calculate total size to store the whole decompressed table */
740 metablks_count = sqfs_count_metablks(dtb, table_offset, table_size);
741 if (metablks_count < 1)
744 *dir_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE);
748 *pos_list = malloc(metablks_count * sizeof(u32));
754 ret = sqfs_get_metablk_pos(*pos_list, dtb, table_offset,
763 src_table = dtb + table_offset + SQFS_HEADER_SIZE;
765 /* Extract compressed Directory table */
767 for (j = 0; j < metablks_count; j++) {
768 sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
770 dest_len = SQFS_METADATA_BLOCK_SIZE;
771 ret = sqfs_decompress(&ctxt, *dir_table +
772 (j * SQFS_METADATA_BLOCK_SIZE),
773 &dest_len, src_table, src_len);
780 if (dest_len < SQFS_METADATA_BLOCK_SIZE) {
781 dest_offset += dest_len;
785 memcpy(*dir_table + (j * SQFS_METADATA_BLOCK_SIZE),
790 * Offsets to the decompression destination, to the metadata
791 * buffer 'dtb' and to the decompression source, respectively.
793 dest_offset += dest_len;
794 table_offset += src_len + SQFS_HEADER_SIZE;
795 src_table += src_len + SQFS_HEADER_SIZE;
801 return metablks_count;
804 int sqfs_opendir(const char *filename, struct fs_dir_stream **dirsp)
806 unsigned char *inode_table = NULL, *dir_table = NULL;
807 int j, token_count, ret = 0, metablks_count;
808 struct squashfs_dir_stream *dirs;
809 char **token_list, *path;
810 u32 *pos_list = NULL;
812 dirs = malloc(sizeof(*dirs));
816 ret = sqfs_read_inode_table(&inode_table);
820 metablks_count = sqfs_read_directory_table(&dir_table, &pos_list);
821 if (metablks_count < 1)
824 /* Tokenize filename */
825 token_count = sqfs_count_tokens(filename);
829 path = strdup(filename);
833 token_list = malloc(token_count * sizeof(char *));
839 /* Fill tokens list */
840 ret = sqfs_tokenize(token_list, token_count, path);
844 * ldir's (extended directory) size is greater than dir, so it works as
845 * a general solution for the malloc size, since 'i' is a union.
847 dirs->inode_table = inode_table;
848 dirs->dir_table = dir_table;
849 ret = sqfs_search_dir(dirs, token_list, token_count, pos_list,
854 if (le16_to_cpu(dirs->i_dir.inode_type) == SQFS_DIR_TYPE)
855 dirs->size = le16_to_cpu(dirs->i_dir.file_size);
857 dirs->size = le32_to_cpu(dirs->i_ldir.file_size);
859 /* Setup directory header */
860 memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE);
861 dirs->entry_count = dirs->dir_header->count + 1;
862 dirs->size -= SQFS_DIR_HEADER_SIZE;
866 dirs->table += SQFS_DIR_HEADER_SIZE;
868 *dirsp = (struct fs_dir_stream *)dirs;
871 for (j = 0; j < token_count; j++)
881 int sqfs_readdir(struct fs_dir_stream *fs_dirs, struct fs_dirent **dentp)
883 struct squashfs_super_block *sblk = ctxt.sblk;
884 struct squashfs_dir_stream *dirs;
885 struct squashfs_lreg_inode *lreg;
886 struct squashfs_base_inode *base;
887 struct squashfs_reg_inode *reg;
888 int i_number, offset = 0, ret;
889 struct fs_dirent *dent;
892 dirs = (struct squashfs_dir_stream *)fs_dirs;
895 return -SQFS_STOP_READDIR;
900 if (!dirs->entry_count) {
901 if (dirs->size > SQFS_DIR_HEADER_SIZE) {
902 dirs->size -= SQFS_DIR_HEADER_SIZE;
906 return -SQFS_STOP_READDIR;
909 if (dirs->size > SQFS_EMPTY_FILE_SIZE) {
910 /* Read follow-up (emitted) dir. header */
911 memcpy(dirs->dir_header, dirs->table,
912 SQFS_DIR_HEADER_SIZE);
913 dirs->entry_count = dirs->dir_header->count + 1;
914 ret = sqfs_read_entry(&dirs->entry, dirs->table +
915 SQFS_DIR_HEADER_SIZE);
917 return -SQFS_STOP_READDIR;
919 dirs->table += SQFS_DIR_HEADER_SIZE;
922 ret = sqfs_read_entry(&dirs->entry, dirs->table);
924 return -SQFS_STOP_READDIR;
927 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
928 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
931 base = (struct squashfs_base_inode *)ipos;
933 /* Set entry type and size */
934 switch (dirs->entry->type) {
937 dent->type = FS_DT_DIR;
942 * Entries do not differentiate extended from regular types, so
943 * it needs to be verified manually.
945 if (get_unaligned_le16(&base->inode_type) == SQFS_LREG_TYPE) {
946 lreg = (struct squashfs_lreg_inode *)ipos;
947 dent->size = get_unaligned_le64(&lreg->file_size);
949 reg = (struct squashfs_reg_inode *)ipos;
950 dent->size = get_unaligned_le32(®->file_size);
953 dent->type = FS_DT_REG;
955 case SQFS_BLKDEV_TYPE:
956 case SQFS_CHRDEV_TYPE:
957 case SQFS_LBLKDEV_TYPE:
958 case SQFS_LCHRDEV_TYPE:
960 case SQFS_SOCKET_TYPE:
961 case SQFS_LFIFO_TYPE:
962 case SQFS_LSOCKET_TYPE:
963 dent->type = SQFS_MISC_ENTRY_TYPE;
965 case SQFS_SYMLINK_TYPE:
966 case SQFS_LSYMLINK_TYPE:
967 dent->type = FS_DT_LNK;
970 return -SQFS_STOP_READDIR;
974 strncpy(dent->name, dirs->entry->name, dirs->entry->name_size + 1);
975 dent->name[dirs->entry->name_size + 1] = '\0';
977 offset = dirs->entry->name_size + 1 + SQFS_ENTRY_BASE_LENGTH;
980 /* Decrement size to be read */
981 if (dirs->size > offset)
982 dirs->size -= offset;
986 /* Keep a reference to the current entry before incrementing it */
987 dirs->table += offset;
994 int sqfs_probe(struct blk_desc *fs_dev_desc, struct disk_partition *fs_partition)
996 struct squashfs_super_block *sblk;
999 ctxt.cur_dev = fs_dev_desc;
1000 ctxt.cur_part_info = *fs_partition;
1002 ret = sqfs_read_sblk(&sblk);
1006 /* Make sure it has a valid SquashFS magic number*/
1007 if (get_unaligned_le32(&sblk->s_magic) != SQFS_MAGIC_NUMBER) {
1008 printf("Bad magic number for SquashFS image.\n");
1009 ctxt.cur_dev = NULL;
1015 ret = sqfs_decompressor_init(&ctxt);
1018 ctxt.cur_dev = NULL;
1026 static char *sqfs_basename(char *path)
1030 fname = path + strlen(path) - 1;
1031 while (fname >= path) {
1032 if (*fname == '/') {
1043 static char *sqfs_dirname(char *path)
1047 fname = sqfs_basename(path);
1055 * Takes a path to file and splits it in two parts: the filename itself and the
1056 * directory's path, e.g.:
1057 * path: /path/to/file.txt
1061 static int sqfs_split_path(char **file, char **dir, const char *path)
1063 char *dirc, *basec, *bname, *dname, *tmp_path;
1066 /* check for first slash in path*/
1067 if (path[0] == '/') {
1068 tmp_path = strdup(path);
1072 tmp_path = malloc(strlen(path) + 2);
1076 strcpy(tmp_path + 1, path);
1079 /* String duplicates */
1080 dirc = strdup(tmp_path);
1086 basec = strdup(tmp_path);
1092 dname = sqfs_dirname(dirc);
1093 bname = sqfs_basename(basec);
1095 *file = strdup(bname);
1102 if (*dname == '\0') {
1112 *dir = strdup(dname);
1129 static int sqfs_get_regfile_info(struct squashfs_reg_inode *reg,
1130 struct squashfs_file_info *finfo,
1131 struct squashfs_fragment_block_entry *fentry,
1134 int datablk_count = 0, ret;
1136 finfo->size = get_unaligned_le32(®->file_size);
1137 finfo->offset = get_unaligned_le32(®->offset);
1138 finfo->start = get_unaligned_le32(®->start_block);
1139 finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(®->fragment));
1142 datablk_count = finfo->size / le32_to_cpu(blksz);
1143 ret = sqfs_frag_lookup(get_unaligned_le32(®->fragment),
1149 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1152 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1153 if (!finfo->blk_sizes)
1156 return datablk_count;
1159 static int sqfs_get_lregfile_info(struct squashfs_lreg_inode *lreg,
1160 struct squashfs_file_info *finfo,
1161 struct squashfs_fragment_block_entry *fentry,
1164 int datablk_count = 0, ret;
1166 finfo->size = get_unaligned_le64(&lreg->file_size);
1167 finfo->offset = get_unaligned_le32(&lreg->offset);
1168 finfo->start = get_unaligned_le64(&lreg->start_block);
1169 finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(&lreg->fragment));
1172 datablk_count = finfo->size / le32_to_cpu(blksz);
1173 ret = sqfs_frag_lookup(get_unaligned_le32(&lreg->fragment),
1179 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1182 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1183 if (!finfo->blk_sizes)
1186 return datablk_count;
1189 int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
1192 char *dir, *fragment_block, *datablock = NULL, *data_buffer = NULL;
1193 char *fragment, *file, *resolved, *data;
1194 u64 start, n_blks, table_size, data_offset, table_offset;
1195 int ret, j, i_number, datablk_count = 0;
1196 struct squashfs_super_block *sblk = ctxt.sblk;
1197 struct squashfs_fragment_block_entry frag_entry;
1198 struct squashfs_file_info finfo = {0};
1199 struct squashfs_symlink_inode *symlink;
1200 struct fs_dir_stream *dirsp = NULL;
1201 struct squashfs_dir_stream *dirs;
1202 struct squashfs_lreg_inode *lreg;
1203 struct squashfs_base_inode *base;
1204 struct squashfs_reg_inode *reg;
1205 unsigned long dest_len;
1206 struct fs_dirent *dent;
1207 unsigned char *ipos;
1212 * sqfs_opendir will uncompress inode and directory tables, and will
1213 * return a pointer to the directory that contains the requested file.
1215 sqfs_split_path(&file, &dir, filename);
1216 ret = sqfs_opendir(dir, &dirsp);
1218 sqfs_closedir(dirsp);
1222 dirs = (struct squashfs_dir_stream *)dirsp;
1224 /* For now, only regular files are able to be loaded */
1225 while (!sqfs_readdir(dirsp, &dent)) {
1226 ret = strcmp(dent->name, file);
1234 printf("File not found.\n");
1236 sqfs_closedir(dirsp);
1241 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1242 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1245 base = (struct squashfs_base_inode *)ipos;
1246 switch (get_unaligned_le16(&base->inode_type)) {
1248 reg = (struct squashfs_reg_inode *)ipos;
1249 datablk_count = sqfs_get_regfile_info(reg, &finfo, &frag_entry,
1251 if (datablk_count < 0) {
1256 memcpy(finfo.blk_sizes, ipos + sizeof(*reg),
1257 datablk_count * sizeof(u32));
1259 case SQFS_LREG_TYPE:
1260 lreg = (struct squashfs_lreg_inode *)ipos;
1261 datablk_count = sqfs_get_lregfile_info(lreg, &finfo,
1264 if (datablk_count < 0) {
1269 memcpy(finfo.blk_sizes, ipos + sizeof(*lreg),
1270 datablk_count * sizeof(u32));
1272 case SQFS_SYMLINK_TYPE:
1273 case SQFS_LSYMLINK_TYPE:
1274 symlink = (struct squashfs_symlink_inode *)ipos;
1275 resolved = sqfs_resolve_symlink(symlink, filename);
1276 ret = sqfs_read(resolved, buf, offset, len, actread);
1279 case SQFS_BLKDEV_TYPE:
1280 case SQFS_CHRDEV_TYPE:
1281 case SQFS_LBLKDEV_TYPE:
1282 case SQFS_LCHRDEV_TYPE:
1283 case SQFS_FIFO_TYPE:
1284 case SQFS_SOCKET_TYPE:
1285 case SQFS_LFIFO_TYPE:
1286 case SQFS_LSOCKET_TYPE:
1288 printf("Unsupported entry type\n");
1293 /* If the user specifies a length, check its sanity */
1295 if (len > finfo.size) {
1303 if (datablk_count) {
1304 data_offset = finfo.start;
1305 datablock = malloc(get_unaligned_le32(&sblk->block_size));
1312 for (j = 0; j < datablk_count; j++) {
1313 start = data_offset / ctxt.cur_dev->blksz;
1314 table_size = SQFS_BLOCK_SIZE(finfo.blk_sizes[j]);
1315 table_offset = data_offset - (start * ctxt.cur_dev->blksz);
1316 n_blks = DIV_ROUND_UP(table_size + table_offset,
1317 ctxt.cur_dev->blksz);
1319 data_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
1326 ret = sqfs_disk_read(start, n_blks, data_buffer);
1329 * Possible causes: too many data blocks or too large
1330 * SquashFS block size. Tip: re-compile the SquashFS
1331 * image with mksquashfs's -b <block_size> option.
1333 printf("Error: too many data blocks to be read.\n");
1337 data = data_buffer + table_offset;
1340 if (SQFS_COMPRESSED_BLOCK(finfo.blk_sizes[j])) {
1341 dest_len = get_unaligned_le32(&sblk->block_size);
1342 ret = sqfs_decompress(&ctxt, datablock, &dest_len,
1347 memcpy(buf + offset + *actread, datablock, dest_len);
1348 *actread += dest_len;
1350 memcpy(buf + offset + *actread, data, table_size);
1351 *actread += table_size;
1354 data_offset += table_size;
1357 free(finfo.blk_sizes);
1360 * There is no need to continue if the file is not fragmented.
1367 start = frag_entry.start / ctxt.cur_dev->blksz;
1368 table_size = SQFS_BLOCK_SIZE(frag_entry.size);
1369 table_offset = frag_entry.start - (start * ctxt.cur_dev->blksz);
1370 n_blks = DIV_ROUND_UP(table_size + table_offset, ctxt.cur_dev->blksz);
1372 fragment = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
1379 ret = sqfs_disk_read(start, n_blks, fragment);
1383 /* File compressed and fragmented */
1384 if (finfo.frag && finfo.comp) {
1385 dest_len = get_unaligned_le32(&sblk->block_size);
1386 fragment_block = malloc(dest_len);
1387 if (!fragment_block) {
1392 ret = sqfs_decompress(&ctxt, fragment_block, &dest_len,
1393 (void *)fragment + table_offset,
1396 free(fragment_block);
1400 for (j = offset + *actread; j < finfo.size; j++) {
1401 memcpy(buf + j, &fragment_block[finfo.offset + j], 1);
1405 free(fragment_block);
1407 } else if (finfo.frag && !finfo.comp) {
1408 fragment_block = (void *)fragment + table_offset;
1410 for (j = offset + *actread; j < finfo.size; j++) {
1411 memcpy(buf + j, &fragment_block[finfo.offset + j], 1);
1431 int sqfs_size(const char *filename, loff_t *size)
1433 struct squashfs_super_block *sblk = ctxt.sblk;
1434 struct squashfs_symlink_inode *symlink;
1435 struct fs_dir_stream *dirsp = NULL;
1436 struct squashfs_base_inode *base;
1437 struct squashfs_dir_stream *dirs;
1438 struct squashfs_lreg_inode *lreg;
1439 struct squashfs_reg_inode *reg;
1440 char *dir, *file, *resolved;
1441 struct fs_dirent *dent;
1442 unsigned char *ipos;
1445 sqfs_split_path(&file, &dir, filename);
1447 * sqfs_opendir will uncompress inode and directory tables, and will
1448 * return a pointer to the directory that contains the requested file.
1450 ret = sqfs_opendir(dir, &dirsp);
1452 sqfs_closedir(dirsp);
1457 dirs = (struct squashfs_dir_stream *)dirsp;
1459 while (!sqfs_readdir(dirsp, &dent)) {
1460 ret = strcmp(dent->name, file);
1467 printf("File not found.\n");
1473 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1474 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1478 base = (struct squashfs_base_inode *)ipos;
1479 switch (get_unaligned_le16(&base->inode_type)) {
1481 reg = (struct squashfs_reg_inode *)ipos;
1482 *size = get_unaligned_le32(®->file_size);
1484 case SQFS_LREG_TYPE:
1485 lreg = (struct squashfs_lreg_inode *)ipos;
1486 *size = get_unaligned_le64(&lreg->file_size);
1488 case SQFS_SYMLINK_TYPE:
1489 case SQFS_LSYMLINK_TYPE:
1490 symlink = (struct squashfs_symlink_inode *)ipos;
1491 resolved = sqfs_resolve_symlink(symlink, filename);
1492 ret = sqfs_size(resolved, size);
1495 case SQFS_BLKDEV_TYPE:
1496 case SQFS_CHRDEV_TYPE:
1497 case SQFS_LBLKDEV_TYPE:
1498 case SQFS_LCHRDEV_TYPE:
1499 case SQFS_FIFO_TYPE:
1500 case SQFS_SOCKET_TYPE:
1501 case SQFS_LFIFO_TYPE:
1502 case SQFS_LSOCKET_TYPE:
1504 printf("Unable to recover entry's size.\n");
1514 sqfs_closedir(dirsp);
1519 void sqfs_close(void)
1522 ctxt.cur_dev = NULL;
1523 sqfs_decompressor_cleanup(&ctxt);
1526 void sqfs_closedir(struct fs_dir_stream *dirs)
1528 struct squashfs_dir_stream *sqfs_dirs;
1530 sqfs_dirs = (struct squashfs_dir_stream *)dirs;
1531 free(sqfs_dirs->inode_table);
1532 free(sqfs_dirs->dir_table);
1533 free(sqfs_dirs->dir_header);