blob: 340e5ebdb9dbe877eba13addbb2ce131ffbfbef4 [file] [log] [blame]
Joao Marcos Costac5100612020-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
26struct squashfs_ctxt {
27 struct disk_partition cur_part_info;
28 struct blk_desc *cur_dev;
29 struct squashfs_super_block *sblk;
30};
31
32static struct squashfs_ctxt ctxt;
33
34static int sqfs_disk_read(__u32 block, __u32 nr_blocks, void *buf)
35{
36 ulong ret;
37
38 if (!ctxt.cur_dev)
39 return -1;
40
41 ret = blk_dread(ctxt.cur_dev, ctxt.cur_part_info.start + block,
42 nr_blocks, buf);
43
44 if (ret != nr_blocks)
45 return -1;
46
47 return ret;
48}
49
50static int sqfs_read_sblk(struct squashfs_super_block **sblk)
51{
52 *sblk = malloc_cache_aligned(ctxt.cur_dev->blksz);
53 if (!*sblk)
54 return -ENOMEM;
55
56 if (sqfs_disk_read(0, 1, *sblk) != 1) {
57 free(*sblk);
58 return -EINVAL;
59 }
60
61 return 0;
62}
63
64static int sqfs_count_tokens(const char *filename)
65{
66 int token_count = 1, l;
67
68 for (l = 1; l < strlen(filename); l++) {
69 if (filename[l] == '/')
70 token_count++;
71 }
72
73 /* Ignore trailing '/' in path */
74 if (filename[strlen(filename) - 1] == '/')
75 token_count--;
76
77 if (!token_count)
78 token_count = 1;
79
80 return token_count;
81}
82
83/*
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.
88 */
89static int sqfs_calc_n_blks(__le64 start, __le64 end, u64 *offset)
90{
91 u64 start_, table_size;
92
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);
96
97 return DIV_ROUND_UP(table_size + *offset, ctxt.cur_dev->blksz);
98}
99
100/*
101 * Retrieves fragment block entry and returns true if the fragment block is
102 * compressed
103 */
104static int sqfs_frag_lookup(u32 inode_fragment_index,
105 struct squashfs_fragment_block_entry *e)
106{
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;
114
115 comp_type = get_unaligned_le16(&sblk->compression);
116
117 if (inode_fragment_index >= get_unaligned_le32(&sblk->fragments))
118 return -EINVAL;
119
120 start = get_unaligned_le64(&sblk->fragment_table_start) /
121 ctxt.cur_dev->blksz;
122 n_blks = sqfs_calc_n_blks(sblk->fragment_table_start,
123 sblk->export_table_start,
124 &table_offset);
125
126 /* Allocate a proper sized buffer to store the fragment index table */
127 table = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
128 if (!table)
129 return -ENOMEM;
130
131 if (sqfs_disk_read(start, n_blks, table) < 0) {
132 free(table);
133 return -EINVAL;
134 }
135
136 block = SQFS_FRAGMENT_INDEX(inode_fragment_index);
137 offset = SQFS_FRAGMENT_INDEX_OFFSET(inode_fragment_index);
138
139 /*
140 * Get the start offset of the metadata block that contains the right
141 * fragment block entry
142 */
143 start_block = get_unaligned_le64(table + table_offset + block *
144 sizeof(u64));
145
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);
149
150 metadata_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
151 if (!metadata_buffer) {
152 ret = -ENOMEM;
153 goto free_table;
154 }
155
156 if (sqfs_disk_read(start, n_blks, metadata_buffer) < 0) {
157 ret = -EINVAL;
158 goto free_buffer;
159 }
160
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;
164
165 entries = malloc(SQFS_METADATA_BLOCK_SIZE);
166 if (!entries) {
167 ret = -ENOMEM;
168 goto free_buffer;
169 }
170
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,
175 src_len);
176 if (ret) {
177 ret = -EINVAL;
178 goto free_entries;
179 }
180 } else {
181 memcpy(entries, metadata, SQFS_METADATA_SIZE(header));
182 }
183
184 *e = entries[offset];
185 ret = SQFS_COMPRESSED_BLOCK(e->size);
186
187free_entries:
188 free(entries);
189free_buffer:
190 free(metadata_buffer);
191free_table:
192 free(table);
193
194 return ret;
195}
196
197/*
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.
201 */
202static int sqfs_read_entry(struct squashfs_directory_entry **dest, void *src)
203{
204 struct squashfs_directory_entry *tmp;
205 u16 sz;
206
207 tmp = src;
208 sz = get_unaligned_le16(src + sizeof(*tmp) - sizeof(u16));
209 /*
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.
214 */
215 *dest = malloc(sizeof(*tmp) + sz + 2);
216 if (!*dest)
217 return -ENOMEM;
218
219 memcpy(*dest, src, sizeof(*tmp) + sz + 1);
220 (*dest)->name[sz + 1] = '\0';
221
222 return 0;
223}
224
225static int sqfs_get_tokens_length(char **tokens, int count)
226{
227 int length = 0, i;
228
229 /*
230 * 1 is added to the result of strlen to consider the slash separator
231 * between the tokens.
232 */
233 for (i = 0; i < count; i++)
234 length += strlen(tokens[i]) + 1;
235
236 return length;
237}
238
239/* Takes a token list and returns a single string with '/' as separator. */
240static char *sqfs_concat_tokens(char **token_list, int token_count)
241{
242 char *result;
243 int i, length = 0, offset = 0;
244
245 length = sqfs_get_tokens_length(token_list, token_count);
246
247 result = malloc(length + 1);
248 result[length] = '\0';
249
250 for (i = 0; i < token_count; i++) {
251 strcpy(result + offset, token_list[i]);
252 offset += strlen(token_list[i]);
253 result[offset++] = '/';
254 }
255
256 return result;
257}
258
259/*
260 * Differently from sqfs_concat_tokens, sqfs_join writes the result into a
261 * previously allocated string, and returns the number of bytes written.
262 */
263static int sqfs_join(char **strings, char *dest, int start, int end,
264 char separator)
265{
266 int i, offset = 0;
267
268 for (i = start; i < end; i++) {
269 strcpy(dest + offset, strings[i]);
270 offset += strlen(strings[i]);
271 if (i < end - 1)
272 dest[offset++] = separator;
273 }
274
275 return offset;
276}
277
278/*
279 * Fills the given token list using its size (count) and a source string (str)
280 */
281static int sqfs_tokenize(char **tokens, int count, const char *str)
282{
283 char *aux, *strc;
284 int i, j;
285
286 strc = strdup(str);
287 if (!strc)
288 return -ENOMEM;
289
290 if (!strcmp(strc, "/")) {
291 tokens[0] = strdup(strc);
292 if (!tokens[0]) {
293 free(strc);
294 return -ENOMEM;
295 }
296 } else {
297 for (j = 0; j < count; j++) {
298 aux = strtok(!j ? strc : NULL, "/");
299 tokens[j] = strdup(aux);
300 if (!tokens[j]) {
301 for (i = 0; i < j; i++)
302 free(tokens[i]);
303 free(strc);
304 return -ENOMEM;
305 }
306 }
307 }
308
309 free(strc);
310
311 return 0;
312}
313
314/*
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.
318 */
319static int sqfs_clean_base_path(char **base, int count, int updir)
320{
321 int i;
322
323 for (i = count - updir - 1; i < count; i++)
324 free(base[i]);
325
326 return count - updir - 1;
327}
328
329/*
330 * Given the base ("current dir.") path and the relative one, generate the
331 * absolute path.
332 */
333static char *sqfs_get_abs_path(const char *base, const char *rel)
334{
335 char **base_tokens, **rel_tokens, *resolved = NULL;
336 int ret, bc, rc, i, updir = 0, resolved_size = 0, offset = 0;
337
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)
342 return NULL;
343
344 base_tokens = malloc(bc * sizeof(char *));
345 if (!base_tokens)
346 return NULL;
347
348 rel_tokens = malloc(rc * sizeof(char *));
349 if (!rel_tokens)
350 goto free_b_tokens;
351
352 /* Fill token lists */
353 ret = sqfs_tokenize(base_tokens, bc, base);
354 if (ret)
355 goto free_r_tokens;
356
357 sqfs_tokenize(rel_tokens, rc, rel);
358 if (ret)
359 goto free_r_tokens;
360
361 /* count '..' occurrences in target path */
362 for (i = 0; i < rc; i++) {
363 if (!strcmp(rel_tokens[i], ".."))
364 updir++;
365 }
366
367 /* Remove the last token and the '..' occurrences */
368 bc = sqfs_clean_base_path(base_tokens, bc, updir);
369 if (bc < 0)
370 goto free_r_tokens;
371
372 /* Calculate resolved path size */
373 if (!bc)
374 resolved_size++;
375
376 resolved_size += sqfs_get_tokens_length(base_tokens, bc) +
377 sqfs_get_tokens_length(rel_tokens, rc);
378
379 resolved = malloc(resolved_size + 1);
380 if (!resolved)
381 goto free_r_tokens_loop;
382
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, '/');
388
389free_r_tokens_loop:
390 for (i = 0; i < rc; i++)
391 free(rel_tokens[i]);
392 for (i = 0; i < bc; i++)
393 free(base_tokens[i]);
394free_r_tokens:
395 free(rel_tokens);
396free_b_tokens:
397 free(base_tokens);
398
399 return resolved;
400}
401
402static char *sqfs_resolve_symlink(struct squashfs_symlink_inode *sym,
403 const char *base_path)
404{
405 char *resolved, *target;
406 u32 sz;
407
408 sz = get_unaligned_le32(&sym->symlink_size);
409 target = malloc(sz + 1);
410 if (!target)
411 return NULL;
412
413 /*
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.
416 */
417 target[sz] = '\0';
418 /* Get target name (relative path) */
419 strncpy(target, sym->symlink, sz);
420
421 /* Relative -> absolute path conversion */
422 resolved = sqfs_get_abs_path(base_path, target);
423
424 free(target);
425
426 return resolved;
427}
428
429/*
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
432 * table.
433 */
434static int sqfs_search_dir(struct squashfs_dir_stream *dirs, char **token_list,
435 int token_count, u32 *m_list, int m_count)
436{
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;
446
447 dirsp = (struct fs_dir_stream *)dirs;
448
449 /* Start by root inode */
450 table = sqfs_find_inode(dirs->inode_table, le32_to_cpu(sblk->inodes),
451 sblk->inodes, sblk->block_size);
452
453 /* root is a regular directory, not an extended one */
454 dir = (struct squashfs_dir_inode *)table;
455
456 /* get directory offset in directory table */
457 offset = sqfs_dir_offset(table, m_list, m_count);
458 dirs->table = &dirs->dir_table[offset];
459
460 /* Setup directory header */
461 dirs->dir_header = malloc(SQFS_DIR_HEADER_SIZE);
462 if (!dirs->dir_header)
463 return -ENOMEM;
464
465 memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE);
466
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;
471
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));
476 return 0;
477 }
478
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");
482 return -EINVAL;
483 }
484
485 while (!sqfs_readdir(dirsp, &dent)) {
486 ret = strcmp(dent->name, token_list[j]);
487 if (!ret)
488 break;
489 free(dirs->entry);
490 }
491
492 if (ret) {
493 printf("** Cannot find directory. **\n");
494 return -EINVAL;
495 }
496
497 /* Redefine inode as the found token */
498 new_inode_number = dirs->entry->inode_offset +
499 dirs->dir_header->inode_number;
500
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;
505
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 -
515 j - 1);
516 /* Concatenate remaining tokens and symlink's target */
517 res = malloc(strlen(rem) + strlen(target) + 1);
518 strcpy(res, target);
519 res[strlen(target)] = '/';
520 strcpy(res + strlen(target) + 1, rem);
521 token_count = sqfs_count_tokens(res);
522
523 if (token_count < 0)
524 return -EINVAL;
525
526 sym_tokens = malloc(token_count * sizeof(char *));
527 if (!sym_tokens)
528 return -EINVAL;
529
530 /* Fill tokens list */
531 ret = sqfs_tokenize(sym_tokens, token_count, res);
532 if (ret)
533 return -EINVAL;
534 free(dirs->entry);
535
536 ret = sqfs_search_dir(dirs, sym_tokens, token_count,
537 m_list, m_count);
538 return ret;
539 } else if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) {
540 printf("** Cannot find directory. **\n");
541 free(dirs->entry);
542 return -EINVAL;
543 }
544
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;
548
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];
552
553 /* Copy directory header */
554 memcpy(dirs->dir_header, &dirs->dir_table[offset],
555 SQFS_DIR_HEADER_SIZE);
556
557 /* Check for empty directory */
558 if (sqfs_is_empty_dir(table)) {
559 printf("Empty directory.\n");
560 free(dirs->entry);
561 return SQFS_EMPTY_DIR;
562 }
563
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;
568 free(dirs->entry);
569 }
570
571 offset = sqfs_dir_offset(table, m_list, m_count);
572 dirs->table = &dirs->dir_table[offset];
573
574 if (get_unaligned_le16(&dir->inode_type) == SQFS_DIR_TYPE)
575 memcpy(&dirs->i_dir, dir, sizeof(*dir));
576 else
577 memcpy(&dirs->i_ldir, ldir, sizeof(*ldir));
578
579 return 0;
580}
581
582/*
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.
587 */
588static int sqfs_count_metablks(void *table, u32 offset, int table_size)
589{
590 int count = 0, cur_size = 0, ret;
591 u32 data_size;
592 bool comp;
593
594 do {
595 ret = sqfs_read_metablock(table, offset + cur_size, &comp,
596 &data_size);
597 if (ret)
598 return -EINVAL;
599 cur_size += data_size + SQFS_HEADER_SIZE;
600 count++;
601 } while (cur_size < table_size);
602
603 return count;
604}
605
606/*
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.
610 */
611static int sqfs_get_metablk_pos(u32 *pos_list, void *table, u32 offset,
612 int metablks_count)
613{
614 u32 data_size, cur_size = 0;
615 int j, ret = 0;
616 bool comp;
617
618 if (!metablks_count)
619 return -EINVAL;
620
621 for (j = 0; j < metablks_count; j++) {
622 ret = sqfs_read_metablock(table, offset + cur_size, &comp,
623 &data_size);
624 if (ret)
625 return -EINVAL;
626
627 cur_size += data_size + SQFS_HEADER_SIZE;
628 pos_list[j] = cur_size;
629 }
630
631 return ret;
632}
633
634static int sqfs_read_inode_table(unsigned char **inode_table)
635{
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;
642 bool compressed;
643
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) /
648 ctxt.cur_dev->blksz;
649 n_blks = sqfs_calc_n_blks(sblk->inode_table_start,
650 sblk->directory_table_start, &table_offset);
651
652 /* Allocate a proper sized buffer (itb) to store the inode table */
653 itb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
654 if (!itb)
655 return -ENOMEM;
656
657 if (sqfs_disk_read(start, n_blks, itb) < 0) {
658 ret = -EINVAL;
659 goto free_itb;
660 }
661
662 /* Parse inode table (metadata block) header */
663 ret = sqfs_read_metablock(itb, table_offset, &compressed, &src_len);
664 if (ret) {
665 ret = -EINVAL;
666 goto free_itb;
667 }
668
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) {
672 ret = -EINVAL;
673 goto free_itb;
674 }
675
676 *inode_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE);
677 if (!*inode_table) {
678 ret = -ENOMEM;
679 goto free_itb;
680 }
681
682 src_table = itb + table_offset + SQFS_HEADER_SIZE;
683
684 /* Extract compressed Inode table */
685 for (j = 0; j < metablks_count; j++) {
686 sqfs_read_metablock(itb, table_offset, &compressed, &src_len);
687 if (compressed) {
688 dest_len = SQFS_METADATA_BLOCK_SIZE;
689 ret = sqfs_decompress(comp_type, *inode_table +
690 dest_offset, &dest_len,
691 src_table, src_len);
692 if (ret) {
693 free(*inode_table);
694 goto free_itb;
695 }
696
697 } else {
698 memcpy(*inode_table + (j * SQFS_METADATA_BLOCK_SIZE),
699 src_table, src_len);
700 }
701
702 /*
703 * Offsets to the decompression destination, to the metadata
704 * buffer 'itb' and to the decompression source, respectively.
705 */
706 dest_offset += dest_len;
707 table_offset += src_len + SQFS_HEADER_SIZE;
708 src_table += src_len + SQFS_HEADER_SIZE;
709 }
710
711free_itb:
712 free(itb);
713
714 return ret;
715}
716
717static int sqfs_read_directory_table(unsigned char **dir_table, u32 **pos_list)
718{
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;
725 bool compressed;
726
727 comp_type = get_unaligned_le16(&sblk->compression);
728
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) /
733 ctxt.cur_dev->blksz;
734 n_blks = sqfs_calc_n_blks(sblk->directory_table_start,
735 sblk->fragment_table_start, &table_offset);
736
737 /* Allocate a proper sized buffer (dtb) to store the directory table */
738 dtb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
739 if (!dtb)
740 return -ENOMEM;
741
742 if (sqfs_disk_read(start, n_blks, dtb) < 0)
743 goto free_dtb;
744
745 /* Parse directory table (metadata block) header */
746 ret = sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
747 if (ret)
748 goto free_dtb;
749
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)
753 goto free_dtb;
754
755 *dir_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE);
756 if (!*dir_table)
757 goto free_dtb;
758
759 *pos_list = malloc(metablks_count * sizeof(u32));
760 if (!*pos_list) {
761 free(*dir_table);
762 goto free_dtb;
763 }
764
765 ret = sqfs_get_metablk_pos(*pos_list, dtb, table_offset,
766 metablks_count);
767 if (ret) {
768 metablks_count = -1;
769 free(*dir_table);
770 free(*pos_list);
771 goto free_dtb;
772 }
773
774 src_table = dtb + table_offset + SQFS_HEADER_SIZE;
775
776 /* Extract compressed Directory table */
777 dest_offset = 0;
778 for (j = 0; j < metablks_count; j++) {
779 sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
780 if (compressed) {
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);
785 if (ret) {
786 metablks_count = -1;
787 free(*dir_table);
788 goto free_dtb;
789 }
790
791 if (dest_len < SQFS_METADATA_BLOCK_SIZE) {
792 dest_offset += dest_len;
793 break;
794 }
795 } else {
796 memcpy(*dir_table + (j * SQFS_METADATA_BLOCK_SIZE),
797 src_table, src_len);
798 }
799
800 /*
801 * Offsets to the decompression destination, to the metadata
802 * buffer 'dtb' and to the decompression source, respectively.
803 */
804 dest_offset += dest_len;
805 table_offset += src_len + SQFS_HEADER_SIZE;
806 src_table += src_len + SQFS_HEADER_SIZE;
807 }
808
809free_dtb:
810 free(dtb);
811
812 return metablks_count;
813}
814
815int sqfs_opendir(const char *filename, struct fs_dir_stream **dirsp)
816{
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;
822
823 dirs = malloc(sizeof(*dirs));
824 if (!dirs)
825 return -EINVAL;
826
827 ret = sqfs_read_inode_table(&inode_table);
828 if (ret)
829 return -EINVAL;
830
831 metablks_count = sqfs_read_directory_table(&dir_table, &pos_list);
832 if (metablks_count < 1)
833 return -EINVAL;
834
835 /* Tokenize filename */
836 token_count = sqfs_count_tokens(filename);
837 if (token_count < 0)
838 return -EINVAL;
839
840 path = strdup(filename);
841 if (!path)
842 return -ENOMEM;
843
844 token_list = malloc(token_count * sizeof(char *));
845 if (!token_list) {
846 ret = -EINVAL;
847 goto free_path;
848 }
849
850 /* Fill tokens list */
851 ret = sqfs_tokenize(token_list, token_count, path);
852 if (ret)
853 goto free_tokens;
854 /*
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.
857 */
858 dirs->inode_table = inode_table;
859 dirs->dir_table = dir_table;
860 ret = sqfs_search_dir(dirs, token_list, token_count, pos_list,
861 metablks_count);
862 if (ret)
863 goto free_tokens;
864
865 if (le16_to_cpu(dirs->i_dir.inode_type) == SQFS_DIR_TYPE)
866 dirs->size = le16_to_cpu(dirs->i_dir.file_size);
867 else
868 dirs->size = le32_to_cpu(dirs->i_ldir.file_size);
869
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;
874
875 /* Setup entry */
876 dirs->entry = NULL;
877 dirs->table += SQFS_DIR_HEADER_SIZE;
878
879 *dirsp = (struct fs_dir_stream *)dirs;
880
881free_tokens:
882 for (j = 0; j < token_count; j++)
883 free(token_list[j]);
884 free(token_list);
885 free(pos_list);
886free_path:
887 free(path);
888
889 return ret;
890}
891
892int sqfs_readdir(struct fs_dir_stream *fs_dirs, struct fs_dirent **dentp)
893{
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;
901 unsigned char *ipos;
902
903 dirs = (struct squashfs_dir_stream *)fs_dirs;
904 if (!dirs->size) {
905 *dentp = NULL;
906 return -SQFS_STOP_READDIR;
907 }
908
909 dent = &dirs->dentp;
910
911 if (!dirs->entry_count) {
912 if (dirs->size > SQFS_DIR_HEADER_SIZE) {
913 dirs->size -= SQFS_DIR_HEADER_SIZE;
914 } else {
915 *dentp = NULL;
916 dirs->size = 0;
917 return -SQFS_STOP_READDIR;
918 }
919
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);
927 if (ret)
928 return -SQFS_STOP_READDIR;
929
930 dirs->table += SQFS_DIR_HEADER_SIZE;
931 }
932 } else {
933 ret = sqfs_read_entry(&dirs->entry, dirs->table);
934 if (ret)
935 return -SQFS_STOP_READDIR;
936 }
937
938 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
939 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
940 sblk->block_size);
941
942 base = (struct squashfs_base_inode *)ipos;
943
944 /* Set entry type and size */
945 switch (dirs->entry->type) {
946 case SQFS_DIR_TYPE:
947 case SQFS_LDIR_TYPE:
948 dent->type = FS_DT_DIR;
949 break;
950 case SQFS_REG_TYPE:
951 case SQFS_LREG_TYPE:
952 /*
953 * Entries do not differentiate extended from regular types, so
954 * it needs to be verified manually.
955 */
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);
959 } else {
960 reg = (struct squashfs_reg_inode *)ipos;
961 dent->size = get_unaligned_le32(&reg->file_size);
962 }
963
964 dent->type = FS_DT_REG;
965 break;
966 case SQFS_BLKDEV_TYPE:
967 case SQFS_CHRDEV_TYPE:
968 case SQFS_LBLKDEV_TYPE:
969 case SQFS_LCHRDEV_TYPE:
970 case SQFS_FIFO_TYPE:
971 case SQFS_SOCKET_TYPE:
972 case SQFS_LFIFO_TYPE:
973 case SQFS_LSOCKET_TYPE:
974 dent->type = SQFS_MISC_ENTRY_TYPE;
975 break;
976 case SQFS_SYMLINK_TYPE:
977 case SQFS_LSYMLINK_TYPE:
978 dent->type = FS_DT_LNK;
979 break;
980 default:
981 return -SQFS_STOP_READDIR;
982 }
983
984 /* Set entry name */
985 strncpy(dent->name, dirs->entry->name, dirs->entry->name_size + 1);
986 dent->name[dirs->entry->name_size + 1] = '\0';
987
988 offset = dirs->entry->name_size + 1 + SQFS_ENTRY_BASE_LENGTH;
989 dirs->entry_count--;
990
991 /* Decrement size to be read */
992 if (dirs->size > offset)
993 dirs->size -= offset;
994 else
995 dirs->size = 0;
996
997 /* Keep a reference to the current entry before incrementing it */
998 dirs->table += offset;
999
1000 *dentp = dent;
1001
1002 return 0;
1003}
1004
1005int sqfs_probe(struct blk_desc *fs_dev_desc, struct disk_partition *fs_partition)
1006{
1007 struct squashfs_super_block *sblk;
1008 int ret;
1009
1010 ctxt.cur_dev = fs_dev_desc;
1011 ctxt.cur_part_info = *fs_partition;
1012
1013 ret = sqfs_read_sblk(&sblk);
1014 if (ret)
1015 return ret;
1016
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;
1021 return -EINVAL;
1022 }
1023
1024 ctxt.sblk = sblk;
1025
1026 return 0;
1027}
1028
1029static char *sqfs_basename(char *path)
1030{
1031 char *fname;
1032
1033 fname = path + strlen(path) - 1;
1034 while (fname >= path) {
1035 if (*fname == '/') {
1036 fname++;
1037 break;
1038 }
1039
1040 fname--;
1041 }
1042
1043 return fname;
1044}
1045
1046static char *sqfs_dirname(char *path)
1047{
1048 char *fname;
1049
1050 fname = sqfs_basename(path);
1051 --fname;
1052 *fname = '\0';
1053
1054 return path;
1055}
1056
1057/*
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
1061 * file: file.txt
1062 * dir: /path/to
1063 */
1064static int sqfs_split_path(char **file, char **dir, const char *path)
1065{
1066 char *dirc, *basec, *bname, *dname, *tmp_path;
1067 int ret = 0;
1068
1069 /* check for first slash in path*/
1070 if (path[0] == '/') {
1071 tmp_path = strdup(path);
1072 if (!tmp_path)
1073 return -ENOMEM;
1074 } else {
1075 tmp_path = malloc(strlen(path) + 2);
1076 if (!tmp_path)
1077 return -ENOMEM;
1078 tmp_path[0] = '/';
1079 strcpy(tmp_path + 1, path);
1080 }
1081
1082 /* String duplicates */
1083 dirc = strdup(tmp_path);
1084 if (!dirc) {
1085 ret = -ENOMEM;
1086 goto free_tmp;
1087 }
1088
1089 basec = strdup(tmp_path);
1090 if (!basec) {
1091 ret = -ENOMEM;
1092 goto free_dirc;
1093 }
1094
1095 dname = sqfs_dirname(dirc);
1096 bname = sqfs_basename(basec);
1097
1098 *file = strdup(bname);
1099
1100 if (!*file) {
1101 ret = -ENOMEM;
1102 goto free_basec;
1103 }
1104
1105 if (*dname == '\0') {
1106 *dir = malloc(2);
1107 if (!*dir) {
1108 ret = -ENOMEM;
1109 goto free_basec;
1110 }
1111
1112 (*dir)[0] = '/';
1113 (*dir)[1] = '\0';
1114 } else {
1115 *dir = strdup(dname);
1116 if (!*dir) {
1117 ret = -ENOMEM;
1118 goto free_basec;
1119 }
1120 }
1121
1122free_basec:
1123 free(basec);
1124free_dirc:
1125 free(dirc);
1126free_tmp:
1127 free(tmp_path);
1128
1129 return ret;
1130}
1131
1132static int sqfs_get_regfile_info(struct squashfs_reg_inode *reg,
1133 struct squashfs_file_info *finfo,
1134 struct squashfs_fragment_block_entry *fentry,
1135 __le32 blksz)
1136{
1137 int datablk_count = 0, ret;
1138
1139 finfo->size = get_unaligned_le32(&reg->file_size);
1140 finfo->offset = get_unaligned_le32(&reg->offset);
1141 finfo->start = get_unaligned_le32(&reg->start_block);
1142 finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(&reg->fragment));
1143
1144 if (finfo->frag) {
1145 datablk_count = finfo->size / le32_to_cpu(blksz);
1146 ret = sqfs_frag_lookup(get_unaligned_le32(&reg->fragment),
1147 fentry);
1148 if (ret < 0)
1149 return -EINVAL;
1150 finfo->comp = true;
1151 } else {
1152 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1153 }
1154
1155 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1156 if (!finfo->blk_sizes)
1157 return -ENOMEM;
1158
1159 return datablk_count;
1160}
1161
1162static int sqfs_get_lregfile_info(struct squashfs_lreg_inode *lreg,
1163 struct squashfs_file_info *finfo,
1164 struct squashfs_fragment_block_entry *fentry,
1165 __le32 blksz)
1166{
1167 int datablk_count = 0, ret;
1168
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));
1173
1174 if (finfo->frag) {
1175 datablk_count = finfo->size / le32_to_cpu(blksz);
1176 ret = sqfs_frag_lookup(get_unaligned_le32(&lreg->fragment),
1177 fentry);
1178 if (ret < 0)
1179 return -EINVAL;
1180 finfo->comp = true;
1181 } else {
1182 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1183 }
1184
1185 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1186 if (!finfo->blk_sizes)
1187 return -ENOMEM;
1188
1189 return datablk_count;
1190}
1191
1192int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
1193 loff_t *actread)
1194{
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;
1211
1212 *actread = 0;
1213
1214 comp_type = get_unaligned_le16(&sblk->compression);
1215
1216 /*
1217 * sqfs_opendir will uncompress inode and directory tables, and will
1218 * return a pointer to the directory that contains the requested file.
1219 */
1220 sqfs_split_path(&file, &dir, filename);
1221 ret = sqfs_opendir(dir, &dirsp);
1222 if (ret) {
1223 sqfs_closedir(dirsp);
1224 goto free_paths;
1225 }
1226
1227 dirs = (struct squashfs_dir_stream *)dirsp;
1228
1229 /* For now, only regular files are able to be loaded */
1230 while (!sqfs_readdir(dirsp, &dent)) {
1231 ret = strcmp(dent->name, file);
1232 if (!ret)
1233 break;
1234
1235 free(dirs->entry);
1236 }
1237
1238 if (ret) {
1239 printf("File not found.\n");
1240 *actread = 0;
1241 sqfs_closedir(dirsp);
1242 ret = -ENOENT;
1243 goto free_paths;
1244 }
1245
1246 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1247 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1248 sblk->block_size);
1249
1250 base = (struct squashfs_base_inode *)ipos;
1251 switch (get_unaligned_le16(&base->inode_type)) {
1252 case SQFS_REG_TYPE:
1253 reg = (struct squashfs_reg_inode *)ipos;
1254 datablk_count = sqfs_get_regfile_info(reg, &finfo, &frag_entry,
1255 sblk->block_size);
1256 if (datablk_count < 0) {
1257 ret = -EINVAL;
1258 goto free_paths;
1259 }
1260
1261 memcpy(finfo.blk_sizes, ipos + sizeof(*reg),
1262 datablk_count * sizeof(u32));
1263 break;
1264 case SQFS_LREG_TYPE:
1265 lreg = (struct squashfs_lreg_inode *)ipos;
1266 datablk_count = sqfs_get_lregfile_info(lreg, &finfo,
1267 &frag_entry,
1268 sblk->block_size);
1269 if (datablk_count < 0) {
1270 ret = -EINVAL;
1271 goto free_paths;
1272 }
1273
1274 memcpy(finfo.blk_sizes, ipos + sizeof(*lreg),
1275 datablk_count * sizeof(u32));
1276 break;
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);
1282 free(resolved);
1283 goto free_paths;
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:
1292 default:
1293 printf("Unsupported entry type\n");
1294 ret = -EINVAL;
1295 goto free_paths;
1296 }
1297
1298 /* If the user specifies a length, check its sanity */
1299 if (len) {
1300 if (len > finfo.size) {
1301 ret = -EINVAL;
1302 goto free_paths;
1303 }
1304
1305 finfo.size = len;
1306 }
1307
1308 if (datablk_count) {
1309 data_offset = finfo.start;
1310 datablock = malloc(get_unaligned_le32(&sblk->block_size));
1311 if (!datablock) {
1312 ret = -ENOMEM;
1313 goto free_paths;
1314 }
1315 }
1316
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);
1323
1324 data_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
1325
1326 if (!data_buffer) {
1327 ret = -ENOMEM;
1328 goto free_datablk;
1329 }
1330
1331 ret = sqfs_disk_read(start, n_blks, data_buffer);
1332 if (ret < 0) {
1333 /*
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.
1337 */
1338 printf("Error: too many data blocks to be read.\n");
1339 goto free_buffer;
1340 }
1341
1342 data = data_buffer + table_offset;
1343
1344 /* Load the data */
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,
1348 data, table_size);
1349 if (ret)
1350 goto free_buffer;
1351
1352 memcpy(buf + offset + *actread, datablock, dest_len);
1353 *actread += dest_len;
1354 } else {
1355 memcpy(buf + offset + *actread, data, table_size);
1356 *actread += table_size;
1357 }
1358
1359 data_offset += table_size;
1360 }
1361
1362 free(finfo.blk_sizes);
1363
1364 /*
1365 * There is no need to continue if the file is not fragmented.
1366 */
1367 if (!finfo.frag) {
1368 ret = 0;
1369 goto free_buffer;
1370 }
1371
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);
1376
1377 fragment = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
1378
1379 if (!fragment) {
1380 ret = -ENOMEM;
1381 goto free_buffer;
1382 }
1383
1384 ret = sqfs_disk_read(start, n_blks, fragment);
1385 if (ret < 0)
1386 goto free_fragment;
1387
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) {
1393 ret = -ENOMEM;
1394 goto free_fragment;
1395 }
1396
1397 ret = sqfs_decompress(comp_type, fragment_block, &dest_len,
1398 (void *)fragment + table_offset,
1399 frag_entry.size);
1400 if (ret) {
1401 free(fragment_block);
1402 goto free_fragment;
1403 }
1404
1405 for (j = offset + *actread; j < finfo.size; j++) {
1406 memcpy(buf + j, &fragment_block[finfo.offset + j], 1);
1407 (*actread)++;
1408 }
1409
1410 free(fragment_block);
1411
1412 } else if (finfo.frag && !finfo.comp) {
1413 fragment_block = (void *)fragment + table_offset;
1414
1415 for (j = offset + *actread; j < finfo.size; j++) {
1416 memcpy(buf + j, &fragment_block[finfo.offset + j], 1);
1417 (*actread)++;
1418 }
1419 }
1420
1421free_fragment:
1422 free(fragment);
1423free_buffer:
1424 if (datablk_count)
1425 free(data_buffer);
1426free_datablk:
1427 if (datablk_count)
1428 free(datablock);
1429free_paths:
1430 free(file);
1431 free(dir);
1432
1433 return ret;
1434}
1435
1436int sqfs_size(const char *filename, loff_t *size)
1437{
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;
1448 int ret, i_number;
1449
1450 sqfs_split_path(&file, &dir, filename);
1451 /*
1452 * sqfs_opendir will uncompress inode and directory tables, and will
1453 * return a pointer to the directory that contains the requested file.
1454 */
1455 ret = sqfs_opendir(dir, &dirsp);
1456 if (ret) {
1457 sqfs_closedir(dirsp);
1458 ret = -EINVAL;
1459 goto free_strings;
1460 }
1461
1462 dirs = (struct squashfs_dir_stream *)dirsp;
1463
1464 while (!sqfs_readdir(dirsp, &dent)) {
1465 ret = strcmp(dent->name, file);
1466 if (!ret)
1467 break;
1468 free(dirs->entry);
1469 }
1470
1471 if (ret) {
1472 printf("File not found.\n");
1473 *size = 0;
1474 ret = -EINVAL;
1475 goto free_strings;
1476 }
1477
1478 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1479 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1480 sblk->block_size);
1481 free(dirs->entry);
1482
1483 base = (struct squashfs_base_inode *)ipos;
1484 switch (get_unaligned_le16(&base->inode_type)) {
1485 case SQFS_REG_TYPE:
1486 reg = (struct squashfs_reg_inode *)ipos;
1487 *size = get_unaligned_le32(&reg->file_size);
1488 break;
1489 case SQFS_LREG_TYPE:
1490 lreg = (struct squashfs_lreg_inode *)ipos;
1491 *size = get_unaligned_le64(&lreg->file_size);
1492 break;
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);
1498 free(resolved);
1499 break;
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:
1508 default:
1509 printf("Unable to recover entry's size.\n");
1510 *size = 0;
1511 ret = -EINVAL;
1512 break;
1513 }
1514
1515free_strings:
1516 free(dir);
1517 free(file);
1518
1519 sqfs_closedir(dirsp);
1520
1521 return ret;
1522}
1523
1524void sqfs_close(void)
1525{
1526 free(ctxt.sblk);
1527 ctxt.cur_dev = NULL;
1528}
1529
1530void sqfs_closedir(struct fs_dir_stream *dirs)
1531{
1532 struct squashfs_dir_stream *sqfs_dirs;
1533
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);
1538}