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