blob: 598b42cd341422313cb35f42e40be2199ca98b77 [file] [log] [blame]
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2020 Bootlin
4 *
5 * Author: Joao Marcos Costa <joaomarcos.costa@bootlin.com>
6 *
7 * sqfs.c: SquashFS filesystem implementation
8 */
9
10#include <asm/unaligned.h>
11#include <errno.h>
12#include <fs.h>
13#include <linux/types.h>
14#include <linux/byteorder/little_endian.h>
15#include <linux/byteorder/generic.h>
16#include <memalign.h>
17#include <stdlib.h>
18#include <string.h>
19#include <squashfs.h>
20#include <part.h>
21
22#include "sqfs_decompressor.h"
23#include "sqfs_filesystem.h"
24#include "sqfs_utils.h"
25
Joao Marcos Costa29da3742020-07-30 15:33:47 +020026static struct squashfs_ctxt ctxt;
27
28static int sqfs_disk_read(__u32 block, __u32 nr_blocks, void *buf)
29{
30 ulong ret;
31
32 if (!ctxt.cur_dev)
33 return -1;
34
35 ret = blk_dread(ctxt.cur_dev, ctxt.cur_part_info.start + block,
36 nr_blocks, buf);
37
38 if (ret != nr_blocks)
39 return -1;
40
41 return ret;
42}
43
44static int sqfs_read_sblk(struct squashfs_super_block **sblk)
45{
46 *sblk = malloc_cache_aligned(ctxt.cur_dev->blksz);
47 if (!*sblk)
48 return -ENOMEM;
49
50 if (sqfs_disk_read(0, 1, *sblk) != 1) {
51 free(*sblk);
52 return -EINVAL;
53 }
54
55 return 0;
56}
57
58static int sqfs_count_tokens(const char *filename)
59{
60 int token_count = 1, l;
61
62 for (l = 1; l < strlen(filename); l++) {
63 if (filename[l] == '/')
64 token_count++;
65 }
66
67 /* Ignore trailing '/' in path */
68 if (filename[strlen(filename) - 1] == '/')
69 token_count--;
70
71 if (!token_count)
72 token_count = 1;
73
74 return token_count;
75}
76
77/*
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.
82 */
83static int sqfs_calc_n_blks(__le64 start, __le64 end, u64 *offset)
84{
85 u64 start_, table_size;
86
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);
90
91 return DIV_ROUND_UP(table_size + *offset, ctxt.cur_dev->blksz);
92}
93
94/*
95 * Retrieves fragment block entry and returns true if the fragment block is
96 * compressed
97 */
98static int sqfs_frag_lookup(u32 inode_fragment_index,
99 struct squashfs_fragment_block_entry *e)
100{
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;
107 u16 header, comp_type;
108
109 comp_type = get_unaligned_le16(&sblk->compression);
110
111 if (inode_fragment_index >= get_unaligned_le32(&sblk->fragments))
112 return -EINVAL;
113
114 start = get_unaligned_le64(&sblk->fragment_table_start) /
115 ctxt.cur_dev->blksz;
116 n_blks = sqfs_calc_n_blks(sblk->fragment_table_start,
117 sblk->export_table_start,
118 &table_offset);
119
120 /* Allocate a proper sized buffer to store the fragment index table */
121 table = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
122 if (!table)
123 return -ENOMEM;
124
125 if (sqfs_disk_read(start, n_blks, table) < 0) {
126 free(table);
127 return -EINVAL;
128 }
129
130 block = SQFS_FRAGMENT_INDEX(inode_fragment_index);
131 offset = SQFS_FRAGMENT_INDEX_OFFSET(inode_fragment_index);
132
133 /*
134 * Get the start offset of the metadata block that contains the right
135 * fragment block entry
136 */
137 start_block = get_unaligned_le64(table + table_offset + block *
138 sizeof(u64));
139
140 start = start_block / ctxt.cur_dev->blksz;
141 n_blks = sqfs_calc_n_blks(cpu_to_le64(start_block),
142 sblk->fragment_table_start, &table_offset);
143
144 metadata_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
145 if (!metadata_buffer) {
146 ret = -ENOMEM;
147 goto free_table;
148 }
149
150 if (sqfs_disk_read(start, n_blks, metadata_buffer) < 0) {
151 ret = -EINVAL;
152 goto free_buffer;
153 }
154
155 /* Every metadata block starts with a 16-bit header */
156 header = get_unaligned_le16(metadata_buffer + table_offset);
157 metadata = metadata_buffer + table_offset + SQFS_HEADER_SIZE;
158
159 entries = malloc(SQFS_METADATA_BLOCK_SIZE);
160 if (!entries) {
161 ret = -ENOMEM;
162 goto free_buffer;
163 }
164
165 if (SQFS_COMPRESSED_METADATA(header)) {
166 src_len = SQFS_METADATA_SIZE(header);
167 dest_len = SQFS_METADATA_BLOCK_SIZE;
168 ret = sqfs_decompress(comp_type, entries, &dest_len, metadata,
169 src_len);
170 if (ret) {
171 ret = -EINVAL;
172 goto free_entries;
173 }
174 } else {
175 memcpy(entries, metadata, SQFS_METADATA_SIZE(header));
176 }
177
178 *e = entries[offset];
179 ret = SQFS_COMPRESSED_BLOCK(e->size);
180
181free_entries:
182 free(entries);
183free_buffer:
184 free(metadata_buffer);
185free_table:
186 free(table);
187
188 return ret;
189}
190
191/*
192 * The entry name is a flexible array member, and we don't know its size before
193 * actually reading the entry. So we need a first copy to retrieve this size so
194 * we can finally copy the whole struct.
195 */
196static int sqfs_read_entry(struct squashfs_directory_entry **dest, void *src)
197{
198 struct squashfs_directory_entry *tmp;
199 u16 sz;
200
201 tmp = src;
202 sz = get_unaligned_le16(src + sizeof(*tmp) - sizeof(u16));
203 /*
204 * 'src' points to the begin of a directory entry, and 'sz' gets its
205 * 'name_size' member's value. name_size is actually the string
206 * length - 1, so adding 2 compensates this difference and adds space
207 * for the trailling null byte.
208 */
209 *dest = malloc(sizeof(*tmp) + sz + 2);
210 if (!*dest)
211 return -ENOMEM;
212
213 memcpy(*dest, src, sizeof(*tmp) + sz + 1);
214 (*dest)->name[sz + 1] = '\0';
215
216 return 0;
217}
218
219static int sqfs_get_tokens_length(char **tokens, int count)
220{
221 int length = 0, i;
222
223 /*
224 * 1 is added to the result of strlen to consider the slash separator
225 * between the tokens.
226 */
227 for (i = 0; i < count; i++)
228 length += strlen(tokens[i]) + 1;
229
230 return length;
231}
232
233/* Takes a token list and returns a single string with '/' as separator. */
234static char *sqfs_concat_tokens(char **token_list, int token_count)
235{
236 char *result;
237 int i, length = 0, offset = 0;
238
239 length = sqfs_get_tokens_length(token_list, token_count);
240
241 result = malloc(length + 1);
242 result[length] = '\0';
243
244 for (i = 0; i < token_count; i++) {
245 strcpy(result + offset, token_list[i]);
246 offset += strlen(token_list[i]);
247 result[offset++] = '/';
248 }
249
250 return result;
251}
252
253/*
254 * Differently from sqfs_concat_tokens, sqfs_join writes the result into a
255 * previously allocated string, and returns the number of bytes written.
256 */
257static int sqfs_join(char **strings, char *dest, int start, int end,
258 char separator)
259{
260 int i, offset = 0;
261
262 for (i = start; i < end; i++) {
263 strcpy(dest + offset, strings[i]);
264 offset += strlen(strings[i]);
265 if (i < end - 1)
266 dest[offset++] = separator;
267 }
268
269 return offset;
270}
271
272/*
273 * Fills the given token list using its size (count) and a source string (str)
274 */
275static int sqfs_tokenize(char **tokens, int count, const char *str)
276{
277 char *aux, *strc;
278 int i, j;
279
280 strc = strdup(str);
281 if (!strc)
282 return -ENOMEM;
283
284 if (!strcmp(strc, "/")) {
285 tokens[0] = strdup(strc);
286 if (!tokens[0]) {
287 free(strc);
288 return -ENOMEM;
289 }
290 } else {
291 for (j = 0; j < count; j++) {
292 aux = strtok(!j ? strc : NULL, "/");
293 tokens[j] = strdup(aux);
294 if (!tokens[j]) {
295 for (i = 0; i < j; i++)
296 free(tokens[i]);
297 free(strc);
298 return -ENOMEM;
299 }
300 }
301 }
302
303 free(strc);
304
305 return 0;
306}
307
308/*
309 * Remove last 'updir + 1' tokens from the base path tokens list. This leaves us
310 * with a token list containing only the tokens needed to form the resolved
311 * path, and returns the decremented size of the token list.
312 */
313static int sqfs_clean_base_path(char **base, int count, int updir)
314{
315 int i;
316
317 for (i = count - updir - 1; i < count; i++)
318 free(base[i]);
319
320 return count - updir - 1;
321}
322
323/*
324 * Given the base ("current dir.") path and the relative one, generate the
325 * absolute path.
326 */
327static char *sqfs_get_abs_path(const char *base, const char *rel)
328{
329 char **base_tokens, **rel_tokens, *resolved = NULL;
330 int ret, bc, rc, i, updir = 0, resolved_size = 0, offset = 0;
331
332 /* Memory allocation for the token lists */
333 bc = sqfs_count_tokens(base);
334 rc = sqfs_count_tokens(rel);
335 if (bc < 1 || rc < 1)
336 return NULL;
337
338 base_tokens = malloc(bc * sizeof(char *));
339 if (!base_tokens)
340 return NULL;
341
342 rel_tokens = malloc(rc * sizeof(char *));
343 if (!rel_tokens)
344 goto free_b_tokens;
345
346 /* Fill token lists */
347 ret = sqfs_tokenize(base_tokens, bc, base);
348 if (ret)
349 goto free_r_tokens;
350
351 sqfs_tokenize(rel_tokens, rc, rel);
352 if (ret)
353 goto free_r_tokens;
354
355 /* count '..' occurrences in target path */
356 for (i = 0; i < rc; i++) {
357 if (!strcmp(rel_tokens[i], ".."))
358 updir++;
359 }
360
361 /* Remove the last token and the '..' occurrences */
362 bc = sqfs_clean_base_path(base_tokens, bc, updir);
363 if (bc < 0)
364 goto free_r_tokens;
365
366 /* Calculate resolved path size */
367 if (!bc)
368 resolved_size++;
369
370 resolved_size += sqfs_get_tokens_length(base_tokens, bc) +
371 sqfs_get_tokens_length(rel_tokens, rc);
372
373 resolved = malloc(resolved_size + 1);
374 if (!resolved)
375 goto free_r_tokens_loop;
376
377 /* Set resolved path */
378 memset(resolved, '\0', resolved_size + 1);
379 offset += sqfs_join(base_tokens, resolved + offset, 0, bc, '/');
380 resolved[offset++] = '/';
381 offset += sqfs_join(rel_tokens, resolved + offset, updir, rc, '/');
382
383free_r_tokens_loop:
384 for (i = 0; i < rc; i++)
385 free(rel_tokens[i]);
386 for (i = 0; i < bc; i++)
387 free(base_tokens[i]);
388free_r_tokens:
389 free(rel_tokens);
390free_b_tokens:
391 free(base_tokens);
392
393 return resolved;
394}
395
396static char *sqfs_resolve_symlink(struct squashfs_symlink_inode *sym,
397 const char *base_path)
398{
399 char *resolved, *target;
400 u32 sz;
401
402 sz = get_unaligned_le32(&sym->symlink_size);
403 target = malloc(sz + 1);
404 if (!target)
405 return NULL;
406
407 /*
408 * There is no trailling null byte in the symlink's target path, so a
409 * copy is made and a '\0' is added at its end.
410 */
411 target[sz] = '\0';
412 /* Get target name (relative path) */
413 strncpy(target, sym->symlink, sz);
414
415 /* Relative -> absolute path conversion */
416 resolved = sqfs_get_abs_path(base_path, target);
417
418 free(target);
419
420 return resolved;
421}
422
423/*
424 * m_list contains each metadata block's position, and m_count is the number of
425 * elements of m_list. Those metadata blocks come from the compressed directory
426 * table.
427 */
428static int sqfs_search_dir(struct squashfs_dir_stream *dirs, char **token_list,
429 int token_count, u32 *m_list, int m_count)
430{
431 struct squashfs_super_block *sblk = ctxt.sblk;
432 char *path, *target, **sym_tokens, *res, *rem;
433 int j, ret, new_inode_number, offset;
434 struct squashfs_symlink_inode *sym;
435 struct squashfs_ldir_inode *ldir;
436 struct squashfs_dir_inode *dir;
437 struct fs_dir_stream *dirsp;
438 struct fs_dirent *dent;
439 unsigned char *table;
440
441 dirsp = (struct fs_dir_stream *)dirs;
442
443 /* Start by root inode */
444 table = sqfs_find_inode(dirs->inode_table, le32_to_cpu(sblk->inodes),
445 sblk->inodes, sblk->block_size);
446
447 /* root is a regular directory, not an extended one */
448 dir = (struct squashfs_dir_inode *)table;
449
450 /* get directory offset in directory table */
451 offset = sqfs_dir_offset(table, m_list, m_count);
452 dirs->table = &dirs->dir_table[offset];
453
454 /* Setup directory header */
455 dirs->dir_header = malloc(SQFS_DIR_HEADER_SIZE);
456 if (!dirs->dir_header)
457 return -ENOMEM;
458
459 memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE);
460
461 /* Initialize squashfs_dir_stream members */
462 dirs->table += SQFS_DIR_HEADER_SIZE;
463 dirs->size = get_unaligned_le16(&dir->file_size) - SQFS_DIR_HEADER_SIZE;
464 dirs->entry_count = dirs->dir_header->count + 1;
465
466 /* No path given -> root directory */
467 if (!strcmp(token_list[0], "/")) {
468 dirs->table = &dirs->dir_table[offset];
469 memcpy(&dirs->i_dir, dir, sizeof(*dir));
470 return 0;
471 }
472
473 for (j = 0; j < token_count; j++) {
474 if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) {
475 printf("** Cannot find directory. **\n");
476 return -EINVAL;
477 }
478
479 while (!sqfs_readdir(dirsp, &dent)) {
480 ret = strcmp(dent->name, token_list[j]);
481 if (!ret)
482 break;
483 free(dirs->entry);
484 }
485
486 if (ret) {
487 printf("** Cannot find directory. **\n");
488 return -EINVAL;
489 }
490
491 /* Redefine inode as the found token */
492 new_inode_number = dirs->entry->inode_offset +
493 dirs->dir_header->inode_number;
494
495 /* Get reference to inode in the inode table */
496 table = sqfs_find_inode(dirs->inode_table, new_inode_number,
497 sblk->inodes, sblk->block_size);
498 dir = (struct squashfs_dir_inode *)table;
499
500 /* Check for symbolic link and inode type sanity */
501 if (get_unaligned_le16(&dir->inode_type) == SQFS_SYMLINK_TYPE) {
502 sym = (struct squashfs_symlink_inode *)table;
503 /* Get first j + 1 tokens */
504 path = sqfs_concat_tokens(token_list, j + 1);
505 /* Resolve for these tokens */
506 target = sqfs_resolve_symlink(sym, path);
507 /* Join remaining tokens */
508 rem = sqfs_concat_tokens(token_list + j + 1, token_count -
509 j - 1);
510 /* Concatenate remaining tokens and symlink's target */
511 res = malloc(strlen(rem) + strlen(target) + 1);
512 strcpy(res, target);
513 res[strlen(target)] = '/';
514 strcpy(res + strlen(target) + 1, rem);
515 token_count = sqfs_count_tokens(res);
516
517 if (token_count < 0)
518 return -EINVAL;
519
520 sym_tokens = malloc(token_count * sizeof(char *));
521 if (!sym_tokens)
522 return -EINVAL;
523
524 /* Fill tokens list */
525 ret = sqfs_tokenize(sym_tokens, token_count, res);
526 if (ret)
527 return -EINVAL;
528 free(dirs->entry);
529
530 ret = sqfs_search_dir(dirs, sym_tokens, token_count,
531 m_list, m_count);
532 return ret;
533 } else if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) {
534 printf("** Cannot find directory. **\n");
535 free(dirs->entry);
536 return -EINVAL;
537 }
538
539 /* Check if it is an extended dir. */
540 if (get_unaligned_le16(&dir->inode_type) == SQFS_LDIR_TYPE)
541 ldir = (struct squashfs_ldir_inode *)table;
542
543 /* Get dir. offset into the directory table */
544 offset = sqfs_dir_offset(table, m_list, m_count);
545 dirs->table = &dirs->dir_table[offset];
546
547 /* Copy directory header */
548 memcpy(dirs->dir_header, &dirs->dir_table[offset],
549 SQFS_DIR_HEADER_SIZE);
550
551 /* Check for empty directory */
552 if (sqfs_is_empty_dir(table)) {
553 printf("Empty directory.\n");
554 free(dirs->entry);
555 return SQFS_EMPTY_DIR;
556 }
557
558 dirs->table += SQFS_DIR_HEADER_SIZE;
559 dirs->size = get_unaligned_le16(&dir->file_size);
560 dirs->entry_count = dirs->dir_header->count + 1;
561 dirs->size -= SQFS_DIR_HEADER_SIZE;
562 free(dirs->entry);
563 }
564
565 offset = sqfs_dir_offset(table, m_list, m_count);
566 dirs->table = &dirs->dir_table[offset];
567
568 if (get_unaligned_le16(&dir->inode_type) == SQFS_DIR_TYPE)
569 memcpy(&dirs->i_dir, dir, sizeof(*dir));
570 else
571 memcpy(&dirs->i_ldir, ldir, sizeof(*ldir));
572
573 return 0;
574}
575
576/*
577 * Inode and directory tables are stored as a series of metadata blocks, and
578 * given the compressed size of this table, we can calculate how much metadata
579 * blocks are needed to store the result of the decompression, since a
580 * decompressed metadata block should have a size of 8KiB.
581 */
582static int sqfs_count_metablks(void *table, u32 offset, int table_size)
583{
584 int count = 0, cur_size = 0, ret;
585 u32 data_size;
586 bool comp;
587
588 do {
589 ret = sqfs_read_metablock(table, offset + cur_size, &comp,
590 &data_size);
591 if (ret)
592 return -EINVAL;
593 cur_size += data_size + SQFS_HEADER_SIZE;
594 count++;
595 } while (cur_size < table_size);
596
597 return count;
598}
599
600/*
601 * Storing the metadata blocks header's positions will be useful while looking
602 * for an entry in the directory table, using the reference (index and offset)
603 * given by its inode.
604 */
605static int sqfs_get_metablk_pos(u32 *pos_list, void *table, u32 offset,
606 int metablks_count)
607{
608 u32 data_size, cur_size = 0;
609 int j, ret = 0;
610 bool comp;
611
612 if (!metablks_count)
613 return -EINVAL;
614
615 for (j = 0; j < metablks_count; j++) {
616 ret = sqfs_read_metablock(table, offset + cur_size, &comp,
617 &data_size);
618 if (ret)
619 return -EINVAL;
620
621 cur_size += data_size + SQFS_HEADER_SIZE;
622 pos_list[j] = cur_size;
623 }
624
625 return ret;
626}
627
628static int sqfs_read_inode_table(unsigned char **inode_table)
629{
630 struct squashfs_super_block *sblk = ctxt.sblk;
631 u64 start, n_blks, table_offset, table_size;
632 int j, ret = 0, metablks_count, comp_type;
633 unsigned char *src_table, *itb;
634 u32 src_len, dest_offset = 0;
635 unsigned long dest_len;
636 bool compressed;
637
638 comp_type = get_unaligned_le16(&sblk->compression);
639 table_size = get_unaligned_le64(&sblk->directory_table_start) -
640 get_unaligned_le64(&sblk->inode_table_start);
641 start = get_unaligned_le64(&sblk->inode_table_start) /
642 ctxt.cur_dev->blksz;
643 n_blks = sqfs_calc_n_blks(sblk->inode_table_start,
644 sblk->directory_table_start, &table_offset);
645
646 /* Allocate a proper sized buffer (itb) to store the inode table */
647 itb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
648 if (!itb)
649 return -ENOMEM;
650
651 if (sqfs_disk_read(start, n_blks, itb) < 0) {
652 ret = -EINVAL;
653 goto free_itb;
654 }
655
656 /* Parse inode table (metadata block) header */
657 ret = sqfs_read_metablock(itb, table_offset, &compressed, &src_len);
658 if (ret) {
659 ret = -EINVAL;
660 goto free_itb;
661 }
662
663 /* Calculate size to store the whole decompressed table */
664 metablks_count = sqfs_count_metablks(itb, table_offset, table_size);
665 if (metablks_count < 1) {
666 ret = -EINVAL;
667 goto free_itb;
668 }
669
670 *inode_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE);
671 if (!*inode_table) {
672 ret = -ENOMEM;
673 goto free_itb;
674 }
675
676 src_table = itb + table_offset + SQFS_HEADER_SIZE;
677
678 /* Extract compressed Inode table */
679 for (j = 0; j < metablks_count; j++) {
680 sqfs_read_metablock(itb, table_offset, &compressed, &src_len);
681 if (compressed) {
682 dest_len = SQFS_METADATA_BLOCK_SIZE;
683 ret = sqfs_decompress(comp_type, *inode_table +
684 dest_offset, &dest_len,
685 src_table, src_len);
686 if (ret) {
687 free(*inode_table);
688 goto free_itb;
689 }
690
691 } else {
692 memcpy(*inode_table + (j * SQFS_METADATA_BLOCK_SIZE),
693 src_table, src_len);
694 }
695
696 /*
697 * Offsets to the decompression destination, to the metadata
698 * buffer 'itb' and to the decompression source, respectively.
699 */
700 dest_offset += dest_len;
701 table_offset += src_len + SQFS_HEADER_SIZE;
702 src_table += src_len + SQFS_HEADER_SIZE;
703 }
704
705free_itb:
706 free(itb);
707
708 return ret;
709}
710
711static int sqfs_read_directory_table(unsigned char **dir_table, u32 **pos_list)
712{
713 u64 start, n_blks, table_offset, table_size;
714 int j, ret = 0, metablks_count = -1, comp_type;
715 struct squashfs_super_block *sblk = ctxt.sblk;
716 unsigned char *src_table, *dtb;
717 u32 src_len, dest_offset = 0;
718 unsigned long dest_len;
719 bool compressed;
720
721 comp_type = get_unaligned_le16(&sblk->compression);
722
723 /* DIRECTORY TABLE */
724 table_size = get_unaligned_le64(&sblk->fragment_table_start) -
725 get_unaligned_le64(&sblk->directory_table_start);
726 start = get_unaligned_le64(&sblk->directory_table_start) /
727 ctxt.cur_dev->blksz;
728 n_blks = sqfs_calc_n_blks(sblk->directory_table_start,
729 sblk->fragment_table_start, &table_offset);
730
731 /* Allocate a proper sized buffer (dtb) to store the directory table */
732 dtb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
733 if (!dtb)
734 return -ENOMEM;
735
736 if (sqfs_disk_read(start, n_blks, dtb) < 0)
737 goto free_dtb;
738
739 /* Parse directory table (metadata block) header */
740 ret = sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
741 if (ret)
742 goto free_dtb;
743
744 /* Calculate total size to store the whole decompressed table */
745 metablks_count = sqfs_count_metablks(dtb, table_offset, table_size);
746 if (metablks_count < 1)
747 goto free_dtb;
748
749 *dir_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE);
750 if (!*dir_table)
751 goto free_dtb;
752
753 *pos_list = malloc(metablks_count * sizeof(u32));
754 if (!*pos_list) {
755 free(*dir_table);
756 goto free_dtb;
757 }
758
759 ret = sqfs_get_metablk_pos(*pos_list, dtb, table_offset,
760 metablks_count);
761 if (ret) {
762 metablks_count = -1;
763 free(*dir_table);
764 free(*pos_list);
765 goto free_dtb;
766 }
767
768 src_table = dtb + table_offset + SQFS_HEADER_SIZE;
769
770 /* Extract compressed Directory table */
771 dest_offset = 0;
772 for (j = 0; j < metablks_count; j++) {
773 sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
774 if (compressed) {
775 dest_len = SQFS_METADATA_BLOCK_SIZE;
776 ret = sqfs_decompress(comp_type, *dir_table +
777 (j * SQFS_METADATA_BLOCK_SIZE),
778 &dest_len, src_table, src_len);
779 if (ret) {
780 metablks_count = -1;
781 free(*dir_table);
782 goto free_dtb;
783 }
784
785 if (dest_len < SQFS_METADATA_BLOCK_SIZE) {
786 dest_offset += dest_len;
787 break;
788 }
789 } else {
790 memcpy(*dir_table + (j * SQFS_METADATA_BLOCK_SIZE),
791 src_table, src_len);
792 }
793
794 /*
795 * Offsets to the decompression destination, to the metadata
796 * buffer 'dtb' and to the decompression source, respectively.
797 */
798 dest_offset += dest_len;
799 table_offset += src_len + SQFS_HEADER_SIZE;
800 src_table += src_len + SQFS_HEADER_SIZE;
801 }
802
803free_dtb:
804 free(dtb);
805
806 return metablks_count;
807}
808
809int sqfs_opendir(const char *filename, struct fs_dir_stream **dirsp)
810{
811 unsigned char *inode_table = NULL, *dir_table = NULL;
812 int j, token_count, ret = 0, metablks_count;
813 struct squashfs_dir_stream *dirs;
814 char **token_list, *path;
815 u32 *pos_list = NULL;
816
817 dirs = malloc(sizeof(*dirs));
818 if (!dirs)
819 return -EINVAL;
820
821 ret = sqfs_read_inode_table(&inode_table);
822 if (ret)
823 return -EINVAL;
824
825 metablks_count = sqfs_read_directory_table(&dir_table, &pos_list);
826 if (metablks_count < 1)
827 return -EINVAL;
828
829 /* Tokenize filename */
830 token_count = sqfs_count_tokens(filename);
831 if (token_count < 0)
832 return -EINVAL;
833
834 path = strdup(filename);
835 if (!path)
836 return -ENOMEM;
837
838 token_list = malloc(token_count * sizeof(char *));
839 if (!token_list) {
840 ret = -EINVAL;
841 goto free_path;
842 }
843
844 /* Fill tokens list */
845 ret = sqfs_tokenize(token_list, token_count, path);
846 if (ret)
847 goto free_tokens;
848 /*
849 * ldir's (extended directory) size is greater than dir, so it works as
850 * a general solution for the malloc size, since 'i' is a union.
851 */
852 dirs->inode_table = inode_table;
853 dirs->dir_table = dir_table;
854 ret = sqfs_search_dir(dirs, token_list, token_count, pos_list,
855 metablks_count);
856 if (ret)
857 goto free_tokens;
858
859 if (le16_to_cpu(dirs->i_dir.inode_type) == SQFS_DIR_TYPE)
860 dirs->size = le16_to_cpu(dirs->i_dir.file_size);
861 else
862 dirs->size = le32_to_cpu(dirs->i_ldir.file_size);
863
864 /* Setup directory header */
865 memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE);
866 dirs->entry_count = dirs->dir_header->count + 1;
867 dirs->size -= SQFS_DIR_HEADER_SIZE;
868
869 /* Setup entry */
870 dirs->entry = NULL;
871 dirs->table += SQFS_DIR_HEADER_SIZE;
872
873 *dirsp = (struct fs_dir_stream *)dirs;
874
875free_tokens:
876 for (j = 0; j < token_count; j++)
877 free(token_list[j]);
878 free(token_list);
879 free(pos_list);
880free_path:
881 free(path);
882
883 return ret;
884}
885
886int sqfs_readdir(struct fs_dir_stream *fs_dirs, struct fs_dirent **dentp)
887{
888 struct squashfs_super_block *sblk = ctxt.sblk;
889 struct squashfs_dir_stream *dirs;
890 struct squashfs_lreg_inode *lreg;
891 struct squashfs_base_inode *base;
892 struct squashfs_reg_inode *reg;
893 int i_number, offset = 0, ret;
894 struct fs_dirent *dent;
895 unsigned char *ipos;
896
897 dirs = (struct squashfs_dir_stream *)fs_dirs;
898 if (!dirs->size) {
899 *dentp = NULL;
900 return -SQFS_STOP_READDIR;
901 }
902
903 dent = &dirs->dentp;
904
905 if (!dirs->entry_count) {
906 if (dirs->size > SQFS_DIR_HEADER_SIZE) {
907 dirs->size -= SQFS_DIR_HEADER_SIZE;
908 } else {
909 *dentp = NULL;
910 dirs->size = 0;
911 return -SQFS_STOP_READDIR;
912 }
913
914 if (dirs->size > SQFS_EMPTY_FILE_SIZE) {
915 /* Read follow-up (emitted) dir. header */
916 memcpy(dirs->dir_header, dirs->table,
917 SQFS_DIR_HEADER_SIZE);
918 dirs->entry_count = dirs->dir_header->count + 1;
919 ret = sqfs_read_entry(&dirs->entry, dirs->table +
920 SQFS_DIR_HEADER_SIZE);
921 if (ret)
922 return -SQFS_STOP_READDIR;
923
924 dirs->table += SQFS_DIR_HEADER_SIZE;
925 }
926 } else {
927 ret = sqfs_read_entry(&dirs->entry, dirs->table);
928 if (ret)
929 return -SQFS_STOP_READDIR;
930 }
931
932 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
933 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
934 sblk->block_size);
935
936 base = (struct squashfs_base_inode *)ipos;
937
938 /* Set entry type and size */
939 switch (dirs->entry->type) {
940 case SQFS_DIR_TYPE:
941 case SQFS_LDIR_TYPE:
942 dent->type = FS_DT_DIR;
943 break;
944 case SQFS_REG_TYPE:
945 case SQFS_LREG_TYPE:
946 /*
947 * Entries do not differentiate extended from regular types, so
948 * it needs to be verified manually.
949 */
950 if (get_unaligned_le16(&base->inode_type) == SQFS_LREG_TYPE) {
951 lreg = (struct squashfs_lreg_inode *)ipos;
952 dent->size = get_unaligned_le64(&lreg->file_size);
953 } else {
954 reg = (struct squashfs_reg_inode *)ipos;
955 dent->size = get_unaligned_le32(&reg->file_size);
956 }
957
958 dent->type = FS_DT_REG;
959 break;
960 case SQFS_BLKDEV_TYPE:
961 case SQFS_CHRDEV_TYPE:
962 case SQFS_LBLKDEV_TYPE:
963 case SQFS_LCHRDEV_TYPE:
964 case SQFS_FIFO_TYPE:
965 case SQFS_SOCKET_TYPE:
966 case SQFS_LFIFO_TYPE:
967 case SQFS_LSOCKET_TYPE:
968 dent->type = SQFS_MISC_ENTRY_TYPE;
969 break;
970 case SQFS_SYMLINK_TYPE:
971 case SQFS_LSYMLINK_TYPE:
972 dent->type = FS_DT_LNK;
973 break;
974 default:
975 return -SQFS_STOP_READDIR;
976 }
977
978 /* Set entry name */
979 strncpy(dent->name, dirs->entry->name, dirs->entry->name_size + 1);
980 dent->name[dirs->entry->name_size + 1] = '\0';
981
982 offset = dirs->entry->name_size + 1 + SQFS_ENTRY_BASE_LENGTH;
983 dirs->entry_count--;
984
985 /* Decrement size to be read */
986 if (dirs->size > offset)
987 dirs->size -= offset;
988 else
989 dirs->size = 0;
990
991 /* Keep a reference to the current entry before incrementing it */
992 dirs->table += offset;
993
994 *dentp = dent;
995
996 return 0;
997}
998
999int sqfs_probe(struct blk_desc *fs_dev_desc, struct disk_partition *fs_partition)
1000{
1001 struct squashfs_super_block *sblk;
1002 int ret;
1003
1004 ctxt.cur_dev = fs_dev_desc;
1005 ctxt.cur_part_info = *fs_partition;
1006
1007 ret = sqfs_read_sblk(&sblk);
1008 if (ret)
1009 return ret;
1010
1011 /* Make sure it has a valid SquashFS magic number*/
1012 if (get_unaligned_le32(&sblk->s_magic) != SQFS_MAGIC_NUMBER) {
1013 printf("Bad magic number for SquashFS image.\n");
1014 ctxt.cur_dev = NULL;
1015 return -EINVAL;
1016 }
1017
1018 ctxt.sblk = sblk;
1019
Joao Marcos Costa877576c2020-08-18 17:17:21 +02001020 ret = sqfs_decompressor_init(&ctxt);
1021
1022 if (ret) {
1023 ctxt.cur_dev = NULL;
1024 free(ctxt.sblk);
1025 return -EINVAL;
1026 }
1027
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001028 return 0;
1029}
1030
1031static char *sqfs_basename(char *path)
1032{
1033 char *fname;
1034
1035 fname = path + strlen(path) - 1;
1036 while (fname >= path) {
1037 if (*fname == '/') {
1038 fname++;
1039 break;
1040 }
1041
1042 fname--;
1043 }
1044
1045 return fname;
1046}
1047
1048static char *sqfs_dirname(char *path)
1049{
1050 char *fname;
1051
1052 fname = sqfs_basename(path);
1053 --fname;
1054 *fname = '\0';
1055
1056 return path;
1057}
1058
1059/*
1060 * Takes a path to file and splits it in two parts: the filename itself and the
1061 * directory's path, e.g.:
1062 * path: /path/to/file.txt
1063 * file: file.txt
1064 * dir: /path/to
1065 */
1066static int sqfs_split_path(char **file, char **dir, const char *path)
1067{
1068 char *dirc, *basec, *bname, *dname, *tmp_path;
1069 int ret = 0;
1070
1071 /* check for first slash in path*/
1072 if (path[0] == '/') {
1073 tmp_path = strdup(path);
1074 if (!tmp_path)
1075 return -ENOMEM;
1076 } else {
1077 tmp_path = malloc(strlen(path) + 2);
1078 if (!tmp_path)
1079 return -ENOMEM;
1080 tmp_path[0] = '/';
1081 strcpy(tmp_path + 1, path);
1082 }
1083
1084 /* String duplicates */
1085 dirc = strdup(tmp_path);
1086 if (!dirc) {
1087 ret = -ENOMEM;
1088 goto free_tmp;
1089 }
1090
1091 basec = strdup(tmp_path);
1092 if (!basec) {
1093 ret = -ENOMEM;
1094 goto free_dirc;
1095 }
1096
1097 dname = sqfs_dirname(dirc);
1098 bname = sqfs_basename(basec);
1099
1100 *file = strdup(bname);
1101
1102 if (!*file) {
1103 ret = -ENOMEM;
1104 goto free_basec;
1105 }
1106
1107 if (*dname == '\0') {
1108 *dir = malloc(2);
1109 if (!*dir) {
1110 ret = -ENOMEM;
1111 goto free_basec;
1112 }
1113
1114 (*dir)[0] = '/';
1115 (*dir)[1] = '\0';
1116 } else {
1117 *dir = strdup(dname);
1118 if (!*dir) {
1119 ret = -ENOMEM;
1120 goto free_basec;
1121 }
1122 }
1123
1124free_basec:
1125 free(basec);
1126free_dirc:
1127 free(dirc);
1128free_tmp:
1129 free(tmp_path);
1130
1131 return ret;
1132}
1133
1134static int sqfs_get_regfile_info(struct squashfs_reg_inode *reg,
1135 struct squashfs_file_info *finfo,
1136 struct squashfs_fragment_block_entry *fentry,
1137 __le32 blksz)
1138{
1139 int datablk_count = 0, ret;
1140
1141 finfo->size = get_unaligned_le32(&reg->file_size);
1142 finfo->offset = get_unaligned_le32(&reg->offset);
1143 finfo->start = get_unaligned_le32(&reg->start_block);
1144 finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(&reg->fragment));
1145
1146 if (finfo->frag) {
1147 datablk_count = finfo->size / le32_to_cpu(blksz);
1148 ret = sqfs_frag_lookup(get_unaligned_le32(&reg->fragment),
1149 fentry);
1150 if (ret < 0)
1151 return -EINVAL;
1152 finfo->comp = true;
1153 } else {
1154 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1155 }
1156
1157 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1158 if (!finfo->blk_sizes)
1159 return -ENOMEM;
1160
1161 return datablk_count;
1162}
1163
1164static int sqfs_get_lregfile_info(struct squashfs_lreg_inode *lreg,
1165 struct squashfs_file_info *finfo,
1166 struct squashfs_fragment_block_entry *fentry,
1167 __le32 blksz)
1168{
1169 int datablk_count = 0, ret;
1170
1171 finfo->size = get_unaligned_le64(&lreg->file_size);
1172 finfo->offset = get_unaligned_le32(&lreg->offset);
1173 finfo->start = get_unaligned_le64(&lreg->start_block);
1174 finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(&lreg->fragment));
1175
1176 if (finfo->frag) {
1177 datablk_count = finfo->size / le32_to_cpu(blksz);
1178 ret = sqfs_frag_lookup(get_unaligned_le32(&lreg->fragment),
1179 fentry);
1180 if (ret < 0)
1181 return -EINVAL;
1182 finfo->comp = true;
1183 } else {
1184 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1185 }
1186
1187 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1188 if (!finfo->blk_sizes)
1189 return -ENOMEM;
1190
1191 return datablk_count;
1192}
1193
1194int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
1195 loff_t *actread)
1196{
1197 char *dir, *fragment_block, *datablock = NULL, *data_buffer = NULL;
1198 char *fragment, *file, *resolved, *data;
1199 u64 start, n_blks, table_size, data_offset, table_offset;
1200 int ret, j, i_number, comp_type, datablk_count = 0;
1201 struct squashfs_super_block *sblk = ctxt.sblk;
1202 struct squashfs_fragment_block_entry frag_entry;
1203 struct squashfs_file_info finfo = {0};
1204 struct squashfs_symlink_inode *symlink;
1205 struct fs_dir_stream *dirsp = NULL;
1206 struct squashfs_dir_stream *dirs;
1207 struct squashfs_lreg_inode *lreg;
1208 struct squashfs_base_inode *base;
1209 struct squashfs_reg_inode *reg;
1210 unsigned long dest_len;
1211 struct fs_dirent *dent;
1212 unsigned char *ipos;
1213
1214 *actread = 0;
1215
1216 comp_type = get_unaligned_le16(&sblk->compression);
1217
1218 /*
1219 * sqfs_opendir will uncompress inode and directory tables, and will
1220 * return a pointer to the directory that contains the requested file.
1221 */
1222 sqfs_split_path(&file, &dir, filename);
1223 ret = sqfs_opendir(dir, &dirsp);
1224 if (ret) {
1225 sqfs_closedir(dirsp);
1226 goto free_paths;
1227 }
1228
1229 dirs = (struct squashfs_dir_stream *)dirsp;
1230
1231 /* For now, only regular files are able to be loaded */
1232 while (!sqfs_readdir(dirsp, &dent)) {
1233 ret = strcmp(dent->name, file);
1234 if (!ret)
1235 break;
1236
1237 free(dirs->entry);
1238 }
1239
1240 if (ret) {
1241 printf("File not found.\n");
1242 *actread = 0;
1243 sqfs_closedir(dirsp);
1244 ret = -ENOENT;
1245 goto free_paths;
1246 }
1247
1248 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1249 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1250 sblk->block_size);
1251
1252 base = (struct squashfs_base_inode *)ipos;
1253 switch (get_unaligned_le16(&base->inode_type)) {
1254 case SQFS_REG_TYPE:
1255 reg = (struct squashfs_reg_inode *)ipos;
1256 datablk_count = sqfs_get_regfile_info(reg, &finfo, &frag_entry,
1257 sblk->block_size);
1258 if (datablk_count < 0) {
1259 ret = -EINVAL;
1260 goto free_paths;
1261 }
1262
1263 memcpy(finfo.blk_sizes, ipos + sizeof(*reg),
1264 datablk_count * sizeof(u32));
1265 break;
1266 case SQFS_LREG_TYPE:
1267 lreg = (struct squashfs_lreg_inode *)ipos;
1268 datablk_count = sqfs_get_lregfile_info(lreg, &finfo,
1269 &frag_entry,
1270 sblk->block_size);
1271 if (datablk_count < 0) {
1272 ret = -EINVAL;
1273 goto free_paths;
1274 }
1275
1276 memcpy(finfo.blk_sizes, ipos + sizeof(*lreg),
1277 datablk_count * sizeof(u32));
1278 break;
1279 case SQFS_SYMLINK_TYPE:
1280 case SQFS_LSYMLINK_TYPE:
1281 symlink = (struct squashfs_symlink_inode *)ipos;
1282 resolved = sqfs_resolve_symlink(symlink, filename);
1283 ret = sqfs_read(resolved, buf, offset, len, actread);
1284 free(resolved);
1285 goto free_paths;
1286 case SQFS_BLKDEV_TYPE:
1287 case SQFS_CHRDEV_TYPE:
1288 case SQFS_LBLKDEV_TYPE:
1289 case SQFS_LCHRDEV_TYPE:
1290 case SQFS_FIFO_TYPE:
1291 case SQFS_SOCKET_TYPE:
1292 case SQFS_LFIFO_TYPE:
1293 case SQFS_LSOCKET_TYPE:
1294 default:
1295 printf("Unsupported entry type\n");
1296 ret = -EINVAL;
1297 goto free_paths;
1298 }
1299
1300 /* If the user specifies a length, check its sanity */
1301 if (len) {
1302 if (len > finfo.size) {
1303 ret = -EINVAL;
1304 goto free_paths;
1305 }
1306
1307 finfo.size = len;
1308 }
1309
1310 if (datablk_count) {
1311 data_offset = finfo.start;
1312 datablock = malloc(get_unaligned_le32(&sblk->block_size));
1313 if (!datablock) {
1314 ret = -ENOMEM;
1315 goto free_paths;
1316 }
1317 }
1318
1319 for (j = 0; j < datablk_count; j++) {
1320 start = data_offset / ctxt.cur_dev->blksz;
1321 table_size = SQFS_BLOCK_SIZE(finfo.blk_sizes[j]);
1322 table_offset = data_offset - (start * ctxt.cur_dev->blksz);
1323 n_blks = DIV_ROUND_UP(table_size + table_offset,
1324 ctxt.cur_dev->blksz);
1325
1326 data_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
1327
1328 if (!data_buffer) {
1329 ret = -ENOMEM;
1330 goto free_datablk;
1331 }
1332
1333 ret = sqfs_disk_read(start, n_blks, data_buffer);
1334 if (ret < 0) {
1335 /*
1336 * Possible causes: too many data blocks or too large
1337 * SquashFS block size. Tip: re-compile the SquashFS
1338 * image with mksquashfs's -b <block_size> option.
1339 */
1340 printf("Error: too many data blocks to be read.\n");
1341 goto free_buffer;
1342 }
1343
1344 data = data_buffer + table_offset;
1345
1346 /* Load the data */
1347 if (SQFS_COMPRESSED_BLOCK(finfo.blk_sizes[j])) {
1348 dest_len = get_unaligned_le32(&sblk->block_size);
1349 ret = sqfs_decompress(comp_type, datablock, &dest_len,
1350 data, table_size);
1351 if (ret)
1352 goto free_buffer;
1353
1354 memcpy(buf + offset + *actread, datablock, dest_len);
1355 *actread += dest_len;
1356 } else {
1357 memcpy(buf + offset + *actread, data, table_size);
1358 *actread += table_size;
1359 }
1360
1361 data_offset += table_size;
1362 }
1363
1364 free(finfo.blk_sizes);
1365
1366 /*
1367 * There is no need to continue if the file is not fragmented.
1368 */
1369 if (!finfo.frag) {
1370 ret = 0;
1371 goto free_buffer;
1372 }
1373
1374 start = frag_entry.start / ctxt.cur_dev->blksz;
1375 table_size = SQFS_BLOCK_SIZE(frag_entry.size);
1376 table_offset = frag_entry.start - (start * ctxt.cur_dev->blksz);
1377 n_blks = DIV_ROUND_UP(table_size + table_offset, ctxt.cur_dev->blksz);
1378
1379 fragment = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
1380
1381 if (!fragment) {
1382 ret = -ENOMEM;
1383 goto free_buffer;
1384 }
1385
1386 ret = sqfs_disk_read(start, n_blks, fragment);
1387 if (ret < 0)
1388 goto free_fragment;
1389
1390 /* File compressed and fragmented */
1391 if (finfo.frag && finfo.comp) {
1392 dest_len = get_unaligned_le32(&sblk->block_size);
1393 fragment_block = malloc(dest_len);
1394 if (!fragment_block) {
1395 ret = -ENOMEM;
1396 goto free_fragment;
1397 }
1398
1399 ret = sqfs_decompress(comp_type, fragment_block, &dest_len,
1400 (void *)fragment + table_offset,
1401 frag_entry.size);
1402 if (ret) {
1403 free(fragment_block);
1404 goto free_fragment;
1405 }
1406
1407 for (j = offset + *actread; j < finfo.size; j++) {
1408 memcpy(buf + j, &fragment_block[finfo.offset + j], 1);
1409 (*actread)++;
1410 }
1411
1412 free(fragment_block);
1413
1414 } else if (finfo.frag && !finfo.comp) {
1415 fragment_block = (void *)fragment + table_offset;
1416
1417 for (j = offset + *actread; j < finfo.size; j++) {
1418 memcpy(buf + j, &fragment_block[finfo.offset + j], 1);
1419 (*actread)++;
1420 }
1421 }
1422
1423free_fragment:
1424 free(fragment);
1425free_buffer:
1426 if (datablk_count)
1427 free(data_buffer);
1428free_datablk:
1429 if (datablk_count)
1430 free(datablock);
1431free_paths:
1432 free(file);
1433 free(dir);
1434
1435 return ret;
1436}
1437
1438int sqfs_size(const char *filename, loff_t *size)
1439{
1440 struct squashfs_super_block *sblk = ctxt.sblk;
1441 struct squashfs_symlink_inode *symlink;
1442 struct fs_dir_stream *dirsp = NULL;
1443 struct squashfs_base_inode *base;
1444 struct squashfs_dir_stream *dirs;
1445 struct squashfs_lreg_inode *lreg;
1446 struct squashfs_reg_inode *reg;
1447 char *dir, *file, *resolved;
1448 struct fs_dirent *dent;
1449 unsigned char *ipos;
1450 int ret, i_number;
1451
1452 sqfs_split_path(&file, &dir, filename);
1453 /*
1454 * sqfs_opendir will uncompress inode and directory tables, and will
1455 * return a pointer to the directory that contains the requested file.
1456 */
1457 ret = sqfs_opendir(dir, &dirsp);
1458 if (ret) {
1459 sqfs_closedir(dirsp);
1460 ret = -EINVAL;
1461 goto free_strings;
1462 }
1463
1464 dirs = (struct squashfs_dir_stream *)dirsp;
1465
1466 while (!sqfs_readdir(dirsp, &dent)) {
1467 ret = strcmp(dent->name, file);
1468 if (!ret)
1469 break;
1470 free(dirs->entry);
1471 }
1472
1473 if (ret) {
1474 printf("File not found.\n");
1475 *size = 0;
1476 ret = -EINVAL;
1477 goto free_strings;
1478 }
1479
1480 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1481 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1482 sblk->block_size);
1483 free(dirs->entry);
1484
1485 base = (struct squashfs_base_inode *)ipos;
1486 switch (get_unaligned_le16(&base->inode_type)) {
1487 case SQFS_REG_TYPE:
1488 reg = (struct squashfs_reg_inode *)ipos;
1489 *size = get_unaligned_le32(&reg->file_size);
1490 break;
1491 case SQFS_LREG_TYPE:
1492 lreg = (struct squashfs_lreg_inode *)ipos;
1493 *size = get_unaligned_le64(&lreg->file_size);
1494 break;
1495 case SQFS_SYMLINK_TYPE:
1496 case SQFS_LSYMLINK_TYPE:
1497 symlink = (struct squashfs_symlink_inode *)ipos;
1498 resolved = sqfs_resolve_symlink(symlink, filename);
1499 ret = sqfs_size(resolved, size);
1500 free(resolved);
1501 break;
1502 case SQFS_BLKDEV_TYPE:
1503 case SQFS_CHRDEV_TYPE:
1504 case SQFS_LBLKDEV_TYPE:
1505 case SQFS_LCHRDEV_TYPE:
1506 case SQFS_FIFO_TYPE:
1507 case SQFS_SOCKET_TYPE:
1508 case SQFS_LFIFO_TYPE:
1509 case SQFS_LSOCKET_TYPE:
1510 default:
1511 printf("Unable to recover entry's size.\n");
1512 *size = 0;
1513 ret = -EINVAL;
1514 break;
1515 }
1516
1517free_strings:
1518 free(dir);
1519 free(file);
1520
1521 sqfs_closedir(dirsp);
1522
1523 return ret;
1524}
1525
1526void sqfs_close(void)
1527{
1528 free(ctxt.sblk);
1529 ctxt.cur_dev = NULL;
Joao Marcos Costa877576c2020-08-18 17:17:21 +02001530 sqfs_decompressor_cleanup(&ctxt);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001531}
1532
1533void sqfs_closedir(struct fs_dir_stream *dirs)
1534{
1535 struct squashfs_dir_stream *sqfs_dirs;
1536
1537 sqfs_dirs = (struct squashfs_dir_stream *)dirs;
1538 free(sqfs_dirs->inode_table);
1539 free(sqfs_dirs->dir_table);
1540 free(sqfs_dirs->dir_header);
1541}