blob: 1714306e747c05de3914eeef73dbddf3365a20f4 [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;
Joao Marcos Costab87fd012020-08-18 17:17:22 +0200107 u16 header;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200108
109 if (inode_fragment_index >= get_unaligned_le32(&sblk->fragments))
110 return -EINVAL;
111
112 start = get_unaligned_le64(&sblk->fragment_table_start) /
113 ctxt.cur_dev->blksz;
114 n_blks = sqfs_calc_n_blks(sblk->fragment_table_start,
115 sblk->export_table_start,
116 &table_offset);
117
118 /* Allocate a proper sized buffer to store the fragment index table */
119 table = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
120 if (!table)
121 return -ENOMEM;
122
123 if (sqfs_disk_read(start, n_blks, table) < 0) {
124 free(table);
125 return -EINVAL;
126 }
127
128 block = SQFS_FRAGMENT_INDEX(inode_fragment_index);
129 offset = SQFS_FRAGMENT_INDEX_OFFSET(inode_fragment_index);
130
131 /*
132 * Get the start offset of the metadata block that contains the right
133 * fragment block entry
134 */
135 start_block = get_unaligned_le64(table + table_offset + block *
136 sizeof(u64));
137
138 start = start_block / ctxt.cur_dev->blksz;
139 n_blks = sqfs_calc_n_blks(cpu_to_le64(start_block),
140 sblk->fragment_table_start, &table_offset);
141
142 metadata_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
143 if (!metadata_buffer) {
144 ret = -ENOMEM;
145 goto free_table;
146 }
147
148 if (sqfs_disk_read(start, n_blks, metadata_buffer) < 0) {
149 ret = -EINVAL;
150 goto free_buffer;
151 }
152
153 /* Every metadata block starts with a 16-bit header */
154 header = get_unaligned_le16(metadata_buffer + table_offset);
155 metadata = metadata_buffer + table_offset + SQFS_HEADER_SIZE;
156
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +0200157 if (!metadata || !header) {
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200158 ret = -ENOMEM;
159 goto free_buffer;
160 }
161
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200162 entries = malloc(SQFS_METADATA_BLOCK_SIZE);
163 if (!entries) {
164 ret = -ENOMEM;
165 goto free_buffer;
166 }
167
168 if (SQFS_COMPRESSED_METADATA(header)) {
169 src_len = SQFS_METADATA_SIZE(header);
170 dest_len = SQFS_METADATA_BLOCK_SIZE;
Joao Marcos Costab87fd012020-08-18 17:17:22 +0200171 ret = sqfs_decompress(&ctxt, entries, &dest_len, metadata,
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200172 src_len);
173 if (ret) {
174 ret = -EINVAL;
175 goto free_entries;
176 }
177 } else {
178 memcpy(entries, metadata, SQFS_METADATA_SIZE(header));
179 }
180
181 *e = entries[offset];
182 ret = SQFS_COMPRESSED_BLOCK(e->size);
183
184free_entries:
185 free(entries);
186free_buffer:
187 free(metadata_buffer);
188free_table:
189 free(table);
190
191 return ret;
192}
193
194/*
195 * The entry name is a flexible array member, and we don't know its size before
196 * actually reading the entry. So we need a first copy to retrieve this size so
197 * we can finally copy the whole struct.
198 */
199static int sqfs_read_entry(struct squashfs_directory_entry **dest, void *src)
200{
201 struct squashfs_directory_entry *tmp;
202 u16 sz;
203
204 tmp = src;
205 sz = get_unaligned_le16(src + sizeof(*tmp) - sizeof(u16));
206 /*
207 * 'src' points to the begin of a directory entry, and 'sz' gets its
208 * 'name_size' member's value. name_size is actually the string
209 * length - 1, so adding 2 compensates this difference and adds space
210 * for the trailling null byte.
211 */
212 *dest = malloc(sizeof(*tmp) + sz + 2);
213 if (!*dest)
214 return -ENOMEM;
215
216 memcpy(*dest, src, sizeof(*tmp) + sz + 1);
217 (*dest)->name[sz + 1] = '\0';
218
219 return 0;
220}
221
222static int sqfs_get_tokens_length(char **tokens, int count)
223{
224 int length = 0, i;
225
226 /*
227 * 1 is added to the result of strlen to consider the slash separator
228 * between the tokens.
229 */
230 for (i = 0; i < count; i++)
231 length += strlen(tokens[i]) + 1;
232
233 return length;
234}
235
236/* Takes a token list and returns a single string with '/' as separator. */
237static char *sqfs_concat_tokens(char **token_list, int token_count)
238{
239 char *result;
240 int i, length = 0, offset = 0;
241
242 length = sqfs_get_tokens_length(token_list, token_count);
243
244 result = malloc(length + 1);
245 result[length] = '\0';
246
247 for (i = 0; i < token_count; i++) {
248 strcpy(result + offset, token_list[i]);
249 offset += strlen(token_list[i]);
250 result[offset++] = '/';
251 }
252
253 return result;
254}
255
256/*
257 * Differently from sqfs_concat_tokens, sqfs_join writes the result into a
258 * previously allocated string, and returns the number of bytes written.
259 */
260static int sqfs_join(char **strings, char *dest, int start, int end,
261 char separator)
262{
263 int i, offset = 0;
264
265 for (i = start; i < end; i++) {
266 strcpy(dest + offset, strings[i]);
267 offset += strlen(strings[i]);
268 if (i < end - 1)
269 dest[offset++] = separator;
270 }
271
272 return offset;
273}
274
275/*
276 * Fills the given token list using its size (count) and a source string (str)
277 */
278static int sqfs_tokenize(char **tokens, int count, const char *str)
279{
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200280 int i, j, ret = 0;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200281 char *aux, *strc;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200282
283 strc = strdup(str);
284 if (!strc)
285 return -ENOMEM;
286
287 if (!strcmp(strc, "/")) {
288 tokens[0] = strdup(strc);
289 if (!tokens[0]) {
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200290 ret = -ENOMEM;
291 goto free_strc;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200292 }
293 } else {
294 for (j = 0; j < count; j++) {
295 aux = strtok(!j ? strc : NULL, "/");
296 tokens[j] = strdup(aux);
297 if (!tokens[j]) {
298 for (i = 0; i < j; i++)
299 free(tokens[i]);
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200300 ret = -ENOMEM;
301 goto free_strc;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200302 }
303 }
304 }
305
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200306free_strc:
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200307 free(strc);
308
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200309 return ret;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200310}
311
312/*
313 * Remove last 'updir + 1' tokens from the base path tokens list. This leaves us
314 * with a token list containing only the tokens needed to form the resolved
315 * path, and returns the decremented size of the token list.
316 */
317static int sqfs_clean_base_path(char **base, int count, int updir)
318{
319 int i;
320
321 for (i = count - updir - 1; i < count; i++)
322 free(base[i]);
323
324 return count - updir - 1;
325}
326
327/*
328 * Given the base ("current dir.") path and the relative one, generate the
329 * absolute path.
330 */
331static char *sqfs_get_abs_path(const char *base, const char *rel)
332{
333 char **base_tokens, **rel_tokens, *resolved = NULL;
334 int ret, bc, rc, i, updir = 0, resolved_size = 0, offset = 0;
335
336 /* Memory allocation for the token lists */
337 bc = sqfs_count_tokens(base);
338 rc = sqfs_count_tokens(rel);
339 if (bc < 1 || rc < 1)
340 return NULL;
341
342 base_tokens = malloc(bc * sizeof(char *));
343 if (!base_tokens)
344 return NULL;
345
346 rel_tokens = malloc(rc * sizeof(char *));
347 if (!rel_tokens)
348 goto free_b_tokens;
349
350 /* Fill token lists */
351 ret = sqfs_tokenize(base_tokens, bc, base);
352 if (ret)
353 goto free_r_tokens;
354
355 sqfs_tokenize(rel_tokens, rc, rel);
356 if (ret)
357 goto free_r_tokens;
358
359 /* count '..' occurrences in target path */
360 for (i = 0; i < rc; i++) {
361 if (!strcmp(rel_tokens[i], ".."))
362 updir++;
363 }
364
365 /* Remove the last token and the '..' occurrences */
366 bc = sqfs_clean_base_path(base_tokens, bc, updir);
367 if (bc < 0)
368 goto free_r_tokens;
369
370 /* Calculate resolved path size */
371 if (!bc)
372 resolved_size++;
373
374 resolved_size += sqfs_get_tokens_length(base_tokens, bc) +
375 sqfs_get_tokens_length(rel_tokens, rc);
376
377 resolved = malloc(resolved_size + 1);
378 if (!resolved)
379 goto free_r_tokens_loop;
380
381 /* Set resolved path */
382 memset(resolved, '\0', resolved_size + 1);
383 offset += sqfs_join(base_tokens, resolved + offset, 0, bc, '/');
384 resolved[offset++] = '/';
385 offset += sqfs_join(rel_tokens, resolved + offset, updir, rc, '/');
386
387free_r_tokens_loop:
388 for (i = 0; i < rc; i++)
389 free(rel_tokens[i]);
390 for (i = 0; i < bc; i++)
391 free(base_tokens[i]);
392free_r_tokens:
393 free(rel_tokens);
394free_b_tokens:
395 free(base_tokens);
396
397 return resolved;
398}
399
400static char *sqfs_resolve_symlink(struct squashfs_symlink_inode *sym,
401 const char *base_path)
402{
403 char *resolved, *target;
404 u32 sz;
405
406 sz = get_unaligned_le32(&sym->symlink_size);
407 target = malloc(sz + 1);
408 if (!target)
409 return NULL;
410
411 /*
412 * There is no trailling null byte in the symlink's target path, so a
413 * copy is made and a '\0' is added at its end.
414 */
415 target[sz] = '\0';
416 /* Get target name (relative path) */
417 strncpy(target, sym->symlink, sz);
418
419 /* Relative -> absolute path conversion */
420 resolved = sqfs_get_abs_path(base_path, target);
421
422 free(target);
423
424 return resolved;
425}
426
427/*
428 * m_list contains each metadata block's position, and m_count is the number of
429 * elements of m_list. Those metadata blocks come from the compressed directory
430 * table.
431 */
432static int sqfs_search_dir(struct squashfs_dir_stream *dirs, char **token_list,
433 int token_count, u32 *m_list, int m_count)
434{
435 struct squashfs_super_block *sblk = ctxt.sblk;
436 char *path, *target, **sym_tokens, *res, *rem;
Richard Genoud2762f652020-11-03 12:11:06 +0100437 int j, ret = 0, new_inode_number, offset;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200438 struct squashfs_symlink_inode *sym;
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +0200439 struct squashfs_ldir_inode *ldir;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200440 struct squashfs_dir_inode *dir;
441 struct fs_dir_stream *dirsp;
442 struct fs_dirent *dent;
443 unsigned char *table;
444
Richard Genoud2762f652020-11-03 12:11:06 +0100445 res = NULL;
446 rem = NULL;
447 path = NULL;
448 target = NULL;
449 sym_tokens = NULL;
450
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200451 dirsp = (struct fs_dir_stream *)dirs;
452
453 /* Start by root inode */
454 table = sqfs_find_inode(dirs->inode_table, le32_to_cpu(sblk->inodes),
455 sblk->inodes, sblk->block_size);
456
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200457 dir = (struct squashfs_dir_inode *)table;
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +0200458 ldir = (struct squashfs_ldir_inode *)table;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200459
460 /* get directory offset in directory table */
461 offset = sqfs_dir_offset(table, m_list, m_count);
462 dirs->table = &dirs->dir_table[offset];
463
464 /* Setup directory header */
465 dirs->dir_header = malloc(SQFS_DIR_HEADER_SIZE);
466 if (!dirs->dir_header)
467 return -ENOMEM;
468
469 memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE);
470
471 /* Initialize squashfs_dir_stream members */
472 dirs->table += SQFS_DIR_HEADER_SIZE;
473 dirs->size = get_unaligned_le16(&dir->file_size) - SQFS_DIR_HEADER_SIZE;
474 dirs->entry_count = dirs->dir_header->count + 1;
475
476 /* No path given -> root directory */
477 if (!strcmp(token_list[0], "/")) {
478 dirs->table = &dirs->dir_table[offset];
479 memcpy(&dirs->i_dir, dir, sizeof(*dir));
480 return 0;
481 }
482
483 for (j = 0; j < token_count; j++) {
484 if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) {
485 printf("** Cannot find directory. **\n");
Richard Genoud2762f652020-11-03 12:11:06 +0100486 ret = -EINVAL;
487 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200488 }
489
490 while (!sqfs_readdir(dirsp, &dent)) {
491 ret = strcmp(dent->name, token_list[j]);
492 if (!ret)
493 break;
494 free(dirs->entry);
Richard Genoude405fc42020-11-03 12:11:05 +0100495 dirs->entry = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200496 }
497
498 if (ret) {
499 printf("** Cannot find directory. **\n");
Richard Genoud2762f652020-11-03 12:11:06 +0100500 ret = -EINVAL;
501 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200502 }
503
504 /* Redefine inode as the found token */
505 new_inode_number = dirs->entry->inode_offset +
506 dirs->dir_header->inode_number;
507
508 /* Get reference to inode in the inode table */
509 table = sqfs_find_inode(dirs->inode_table, new_inode_number,
510 sblk->inodes, sblk->block_size);
511 dir = (struct squashfs_dir_inode *)table;
512
513 /* Check for symbolic link and inode type sanity */
514 if (get_unaligned_le16(&dir->inode_type) == SQFS_SYMLINK_TYPE) {
515 sym = (struct squashfs_symlink_inode *)table;
516 /* Get first j + 1 tokens */
517 path = sqfs_concat_tokens(token_list, j + 1);
Richard Genoud2762f652020-11-03 12:11:06 +0100518 if (!path) {
519 ret = -ENOMEM;
520 goto out;
521 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200522 /* Resolve for these tokens */
523 target = sqfs_resolve_symlink(sym, path);
Richard Genoud2762f652020-11-03 12:11:06 +0100524 if (!target) {
525 ret = -ENOMEM;
526 goto out;
527 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200528 /* Join remaining tokens */
529 rem = sqfs_concat_tokens(token_list + j + 1, token_count -
530 j - 1);
Richard Genoud2762f652020-11-03 12:11:06 +0100531 if (!rem) {
532 ret = -ENOMEM;
533 goto out;
534 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200535 /* Concatenate remaining tokens and symlink's target */
536 res = malloc(strlen(rem) + strlen(target) + 1);
Richard Genoud2762f652020-11-03 12:11:06 +0100537 if (!res) {
538 ret = -ENOMEM;
539 goto out;
540 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200541 strcpy(res, target);
542 res[strlen(target)] = '/';
543 strcpy(res + strlen(target) + 1, rem);
544 token_count = sqfs_count_tokens(res);
545
Richard Genoud2762f652020-11-03 12:11:06 +0100546 if (token_count < 0) {
547 ret = -EINVAL;
548 goto out;
549 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200550
551 sym_tokens = malloc(token_count * sizeof(char *));
Richard Genoud2762f652020-11-03 12:11:06 +0100552 if (!sym_tokens) {
553 ret = -EINVAL;
554 goto out;
555 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200556
557 /* Fill tokens list */
558 ret = sqfs_tokenize(sym_tokens, token_count, res);
Richard Genoud2762f652020-11-03 12:11:06 +0100559 if (ret) {
560 ret = -EINVAL;
561 goto out;
562 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200563 free(dirs->entry);
Richard Genoude405fc42020-11-03 12:11:05 +0100564 dirs->entry = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200565
566 ret = sqfs_search_dir(dirs, sym_tokens, token_count,
567 m_list, m_count);
Richard Genoud2762f652020-11-03 12:11:06 +0100568 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200569 } else if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) {
570 printf("** Cannot find directory. **\n");
571 free(dirs->entry);
Richard Genoude405fc42020-11-03 12:11:05 +0100572 dirs->entry = NULL;
Richard Genoud2762f652020-11-03 12:11:06 +0100573 ret = -EINVAL;
574 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200575 }
576
577 /* Check if it is an extended dir. */
578 if (get_unaligned_le16(&dir->inode_type) == SQFS_LDIR_TYPE)
579 ldir = (struct squashfs_ldir_inode *)table;
580
581 /* Get dir. offset into the directory table */
582 offset = sqfs_dir_offset(table, m_list, m_count);
583 dirs->table = &dirs->dir_table[offset];
584
585 /* Copy directory header */
586 memcpy(dirs->dir_header, &dirs->dir_table[offset],
587 SQFS_DIR_HEADER_SIZE);
588
589 /* Check for empty directory */
590 if (sqfs_is_empty_dir(table)) {
591 printf("Empty directory.\n");
592 free(dirs->entry);
Richard Genoude405fc42020-11-03 12:11:05 +0100593 dirs->entry = NULL;
Richard Genoud2762f652020-11-03 12:11:06 +0100594 ret = SQFS_EMPTY_DIR;
595 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200596 }
597
598 dirs->table += SQFS_DIR_HEADER_SIZE;
599 dirs->size = get_unaligned_le16(&dir->file_size);
600 dirs->entry_count = dirs->dir_header->count + 1;
601 dirs->size -= SQFS_DIR_HEADER_SIZE;
602 free(dirs->entry);
Richard Genoude405fc42020-11-03 12:11:05 +0100603 dirs->entry = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200604 }
605
606 offset = sqfs_dir_offset(table, m_list, m_count);
607 dirs->table = &dirs->dir_table[offset];
608
609 if (get_unaligned_le16(&dir->inode_type) == SQFS_DIR_TYPE)
610 memcpy(&dirs->i_dir, dir, sizeof(*dir));
611 else
612 memcpy(&dirs->i_ldir, ldir, sizeof(*ldir));
613
Richard Genoud2762f652020-11-03 12:11:06 +0100614out:
615 free(res);
616 free(rem);
617 free(path);
618 free(target);
619 free(sym_tokens);
620 return ret;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200621}
622
623/*
624 * Inode and directory tables are stored as a series of metadata blocks, and
625 * given the compressed size of this table, we can calculate how much metadata
626 * blocks are needed to store the result of the decompression, since a
627 * decompressed metadata block should have a size of 8KiB.
628 */
629static int sqfs_count_metablks(void *table, u32 offset, int table_size)
630{
631 int count = 0, cur_size = 0, ret;
632 u32 data_size;
633 bool comp;
634
635 do {
636 ret = sqfs_read_metablock(table, offset + cur_size, &comp,
637 &data_size);
638 if (ret)
639 return -EINVAL;
640 cur_size += data_size + SQFS_HEADER_SIZE;
641 count++;
642 } while (cur_size < table_size);
643
644 return count;
645}
646
647/*
648 * Storing the metadata blocks header's positions will be useful while looking
649 * for an entry in the directory table, using the reference (index and offset)
650 * given by its inode.
651 */
652static int sqfs_get_metablk_pos(u32 *pos_list, void *table, u32 offset,
653 int metablks_count)
654{
655 u32 data_size, cur_size = 0;
656 int j, ret = 0;
657 bool comp;
658
659 if (!metablks_count)
660 return -EINVAL;
661
662 for (j = 0; j < metablks_count; j++) {
663 ret = sqfs_read_metablock(table, offset + cur_size, &comp,
664 &data_size);
665 if (ret)
666 return -EINVAL;
667
668 cur_size += data_size + SQFS_HEADER_SIZE;
669 pos_list[j] = cur_size;
670 }
671
672 return ret;
673}
674
675static int sqfs_read_inode_table(unsigned char **inode_table)
676{
677 struct squashfs_super_block *sblk = ctxt.sblk;
678 u64 start, n_blks, table_offset, table_size;
Joao Marcos Costab87fd012020-08-18 17:17:22 +0200679 int j, ret = 0, metablks_count;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200680 unsigned char *src_table, *itb;
681 u32 src_len, dest_offset = 0;
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200682 unsigned long dest_len = 0;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200683 bool compressed;
684
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200685 table_size = get_unaligned_le64(&sblk->directory_table_start) -
686 get_unaligned_le64(&sblk->inode_table_start);
687 start = get_unaligned_le64(&sblk->inode_table_start) /
688 ctxt.cur_dev->blksz;
689 n_blks = sqfs_calc_n_blks(sblk->inode_table_start,
690 sblk->directory_table_start, &table_offset);
691
692 /* Allocate a proper sized buffer (itb) to store the inode table */
693 itb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
694 if (!itb)
695 return -ENOMEM;
696
697 if (sqfs_disk_read(start, n_blks, itb) < 0) {
698 ret = -EINVAL;
699 goto free_itb;
700 }
701
702 /* Parse inode table (metadata block) header */
703 ret = sqfs_read_metablock(itb, table_offset, &compressed, &src_len);
704 if (ret) {
705 ret = -EINVAL;
706 goto free_itb;
707 }
708
709 /* Calculate size to store the whole decompressed table */
710 metablks_count = sqfs_count_metablks(itb, table_offset, table_size);
711 if (metablks_count < 1) {
712 ret = -EINVAL;
713 goto free_itb;
714 }
715
716 *inode_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE);
717 if (!*inode_table) {
718 ret = -ENOMEM;
719 goto free_itb;
720 }
721
722 src_table = itb + table_offset + SQFS_HEADER_SIZE;
723
724 /* Extract compressed Inode table */
725 for (j = 0; j < metablks_count; j++) {
726 sqfs_read_metablock(itb, table_offset, &compressed, &src_len);
727 if (compressed) {
728 dest_len = SQFS_METADATA_BLOCK_SIZE;
Joao Marcos Costab87fd012020-08-18 17:17:22 +0200729 ret = sqfs_decompress(&ctxt, *inode_table +
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200730 dest_offset, &dest_len,
731 src_table, src_len);
732 if (ret) {
733 free(*inode_table);
734 goto free_itb;
735 }
736
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200737 dest_offset += dest_len;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200738 } else {
739 memcpy(*inode_table + (j * SQFS_METADATA_BLOCK_SIZE),
740 src_table, src_len);
741 }
742
743 /*
744 * Offsets to the decompression destination, to the metadata
745 * buffer 'itb' and to the decompression source, respectively.
746 */
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200747
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200748 table_offset += src_len + SQFS_HEADER_SIZE;
749 src_table += src_len + SQFS_HEADER_SIZE;
750 }
751
752free_itb:
753 free(itb);
754
755 return ret;
756}
757
758static int sqfs_read_directory_table(unsigned char **dir_table, u32 **pos_list)
759{
760 u64 start, n_blks, table_offset, table_size;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200761 struct squashfs_super_block *sblk = ctxt.sblk;
Joao Marcos Costab87fd012020-08-18 17:17:22 +0200762 int j, ret = 0, metablks_count = -1;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200763 unsigned char *src_table, *dtb;
764 u32 src_len, dest_offset = 0;
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200765 unsigned long dest_len = 0;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200766 bool compressed;
767
Richard Genoud2f9c9852020-11-03 12:11:04 +0100768 *dir_table = NULL;
769 *pos_list = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200770 /* DIRECTORY TABLE */
771 table_size = get_unaligned_le64(&sblk->fragment_table_start) -
772 get_unaligned_le64(&sblk->directory_table_start);
773 start = get_unaligned_le64(&sblk->directory_table_start) /
774 ctxt.cur_dev->blksz;
775 n_blks = sqfs_calc_n_blks(sblk->directory_table_start,
776 sblk->fragment_table_start, &table_offset);
777
778 /* Allocate a proper sized buffer (dtb) to store the directory table */
779 dtb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
780 if (!dtb)
781 return -ENOMEM;
782
783 if (sqfs_disk_read(start, n_blks, dtb) < 0)
Richard Genoud2f9c9852020-11-03 12:11:04 +0100784 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200785
786 /* Parse directory table (metadata block) header */
787 ret = sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
788 if (ret)
Richard Genoud2f9c9852020-11-03 12:11:04 +0100789 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200790
791 /* Calculate total size to store the whole decompressed table */
792 metablks_count = sqfs_count_metablks(dtb, table_offset, table_size);
793 if (metablks_count < 1)
Richard Genoud2f9c9852020-11-03 12:11:04 +0100794 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200795
796 *dir_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE);
797 if (!*dir_table)
Richard Genoud2f9c9852020-11-03 12:11:04 +0100798 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200799
800 *pos_list = malloc(metablks_count * sizeof(u32));
Richard Genoud2f9c9852020-11-03 12:11:04 +0100801 if (!*pos_list)
802 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200803
804 ret = sqfs_get_metablk_pos(*pos_list, dtb, table_offset,
805 metablks_count);
806 if (ret) {
807 metablks_count = -1;
Richard Genoud2f9c9852020-11-03 12:11:04 +0100808 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200809 }
810
811 src_table = dtb + table_offset + SQFS_HEADER_SIZE;
812
813 /* Extract compressed Directory table */
814 dest_offset = 0;
815 for (j = 0; j < metablks_count; j++) {
816 sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
817 if (compressed) {
818 dest_len = SQFS_METADATA_BLOCK_SIZE;
Joao Marcos Costab87fd012020-08-18 17:17:22 +0200819 ret = sqfs_decompress(&ctxt, *dir_table +
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200820 (j * SQFS_METADATA_BLOCK_SIZE),
821 &dest_len, src_table, src_len);
822 if (ret) {
823 metablks_count = -1;
Richard Genoud2f9c9852020-11-03 12:11:04 +0100824 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200825 }
826
827 if (dest_len < SQFS_METADATA_BLOCK_SIZE) {
828 dest_offset += dest_len;
829 break;
830 }
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200831
832 dest_offset += dest_len;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200833 } else {
834 memcpy(*dir_table + (j * SQFS_METADATA_BLOCK_SIZE),
835 src_table, src_len);
836 }
837
838 /*
839 * Offsets to the decompression destination, to the metadata
840 * buffer 'dtb' and to the decompression source, respectively.
841 */
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200842 table_offset += src_len + SQFS_HEADER_SIZE;
843 src_table += src_len + SQFS_HEADER_SIZE;
844 }
845
Richard Genoud2f9c9852020-11-03 12:11:04 +0100846out:
847 if (metablks_count < 1) {
848 free(*dir_table);
849 free(*pos_list);
850 *dir_table = NULL;
851 *pos_list = NULL;
852 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200853 free(dtb);
854
855 return metablks_count;
856}
857
858int sqfs_opendir(const char *filename, struct fs_dir_stream **dirsp)
859{
860 unsigned char *inode_table = NULL, *dir_table = NULL;
Richard Genoud32bea5b2020-11-03 12:11:01 +0100861 int j, token_count = 0, ret = 0, metablks_count;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200862 struct squashfs_dir_stream *dirs;
Richard Genoud32bea5b2020-11-03 12:11:01 +0100863 char **token_list = NULL, *path = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200864 u32 *pos_list = NULL;
865
866 dirs = malloc(sizeof(*dirs));
867 if (!dirs)
868 return -EINVAL;
869
Richard Genoud557f08f2020-11-03 12:11:00 +0100870 /* these should be set to NULL to prevent dangling pointers */
871 dirs->dir_header = NULL;
872 dirs->entry = NULL;
873 dirs->table = NULL;
874 dirs->inode_table = NULL;
875 dirs->dir_table = NULL;
876
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200877 ret = sqfs_read_inode_table(&inode_table);
Richard Genoud557f08f2020-11-03 12:11:00 +0100878 if (ret) {
879 ret = -EINVAL;
Richard Genoud32bea5b2020-11-03 12:11:01 +0100880 goto out;
Richard Genoud557f08f2020-11-03 12:11:00 +0100881 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200882
883 metablks_count = sqfs_read_directory_table(&dir_table, &pos_list);
Richard Genoud557f08f2020-11-03 12:11:00 +0100884 if (metablks_count < 1) {
885 ret = -EINVAL;
Richard Genoud32bea5b2020-11-03 12:11:01 +0100886 goto out;
Richard Genoud557f08f2020-11-03 12:11:00 +0100887 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200888
889 /* Tokenize filename */
890 token_count = sqfs_count_tokens(filename);
Richard Genoud557f08f2020-11-03 12:11:00 +0100891 if (token_count < 0) {
892 ret = -EINVAL;
Richard Genoud32bea5b2020-11-03 12:11:01 +0100893 goto out;
Richard Genoud557f08f2020-11-03 12:11:00 +0100894 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200895
896 path = strdup(filename);
Richard Genoud557f08f2020-11-03 12:11:00 +0100897 if (!path) {
898 ret = -EINVAL;
Richard Genoud32bea5b2020-11-03 12:11:01 +0100899 goto out;
Richard Genoud557f08f2020-11-03 12:11:00 +0100900 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200901
902 token_list = malloc(token_count * sizeof(char *));
903 if (!token_list) {
904 ret = -EINVAL;
Richard Genoud32bea5b2020-11-03 12:11:01 +0100905 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200906 }
907
908 /* Fill tokens list */
909 ret = sqfs_tokenize(token_list, token_count, path);
910 if (ret)
Richard Genoud32bea5b2020-11-03 12:11:01 +0100911 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200912 /*
913 * ldir's (extended directory) size is greater than dir, so it works as
914 * a general solution for the malloc size, since 'i' is a union.
915 */
916 dirs->inode_table = inode_table;
917 dirs->dir_table = dir_table;
918 ret = sqfs_search_dir(dirs, token_list, token_count, pos_list,
919 metablks_count);
920 if (ret)
Richard Genoud32bea5b2020-11-03 12:11:01 +0100921 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200922
923 if (le16_to_cpu(dirs->i_dir.inode_type) == SQFS_DIR_TYPE)
924 dirs->size = le16_to_cpu(dirs->i_dir.file_size);
925 else
926 dirs->size = le32_to_cpu(dirs->i_ldir.file_size);
927
928 /* Setup directory header */
929 memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE);
930 dirs->entry_count = dirs->dir_header->count + 1;
931 dirs->size -= SQFS_DIR_HEADER_SIZE;
932
933 /* Setup entry */
934 dirs->entry = NULL;
935 dirs->table += SQFS_DIR_HEADER_SIZE;
936
937 *dirsp = (struct fs_dir_stream *)dirs;
938
Richard Genoud32bea5b2020-11-03 12:11:01 +0100939out:
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200940 for (j = 0; j < token_count; j++)
941 free(token_list[j]);
942 free(token_list);
943 free(pos_list);
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200944 free(path);
Richard Genoud32bea5b2020-11-03 12:11:01 +0100945 if (ret) {
Richard Genoud557f08f2020-11-03 12:11:00 +0100946 free(inode_table);
Richard Genoud557f08f2020-11-03 12:11:00 +0100947 free(dirs);
Richard Genoud32bea5b2020-11-03 12:11:01 +0100948 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200949
950 return ret;
951}
952
953int sqfs_readdir(struct fs_dir_stream *fs_dirs, struct fs_dirent **dentp)
954{
955 struct squashfs_super_block *sblk = ctxt.sblk;
956 struct squashfs_dir_stream *dirs;
957 struct squashfs_lreg_inode *lreg;
958 struct squashfs_base_inode *base;
959 struct squashfs_reg_inode *reg;
960 int i_number, offset = 0, ret;
961 struct fs_dirent *dent;
962 unsigned char *ipos;
963
964 dirs = (struct squashfs_dir_stream *)fs_dirs;
965 if (!dirs->size) {
966 *dentp = NULL;
967 return -SQFS_STOP_READDIR;
968 }
969
970 dent = &dirs->dentp;
971
972 if (!dirs->entry_count) {
973 if (dirs->size > SQFS_DIR_HEADER_SIZE) {
974 dirs->size -= SQFS_DIR_HEADER_SIZE;
975 } else {
976 *dentp = NULL;
977 dirs->size = 0;
978 return -SQFS_STOP_READDIR;
979 }
980
981 if (dirs->size > SQFS_EMPTY_FILE_SIZE) {
982 /* Read follow-up (emitted) dir. header */
983 memcpy(dirs->dir_header, dirs->table,
984 SQFS_DIR_HEADER_SIZE);
985 dirs->entry_count = dirs->dir_header->count + 1;
986 ret = sqfs_read_entry(&dirs->entry, dirs->table +
987 SQFS_DIR_HEADER_SIZE);
988 if (ret)
989 return -SQFS_STOP_READDIR;
990
991 dirs->table += SQFS_DIR_HEADER_SIZE;
992 }
993 } else {
994 ret = sqfs_read_entry(&dirs->entry, dirs->table);
995 if (ret)
996 return -SQFS_STOP_READDIR;
997 }
998
999 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1000 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1001 sblk->block_size);
1002
1003 base = (struct squashfs_base_inode *)ipos;
1004
1005 /* Set entry type and size */
1006 switch (dirs->entry->type) {
1007 case SQFS_DIR_TYPE:
1008 case SQFS_LDIR_TYPE:
1009 dent->type = FS_DT_DIR;
1010 break;
1011 case SQFS_REG_TYPE:
1012 case SQFS_LREG_TYPE:
1013 /*
1014 * Entries do not differentiate extended from regular types, so
1015 * it needs to be verified manually.
1016 */
1017 if (get_unaligned_le16(&base->inode_type) == SQFS_LREG_TYPE) {
1018 lreg = (struct squashfs_lreg_inode *)ipos;
1019 dent->size = get_unaligned_le64(&lreg->file_size);
1020 } else {
1021 reg = (struct squashfs_reg_inode *)ipos;
1022 dent->size = get_unaligned_le32(&reg->file_size);
1023 }
1024
1025 dent->type = FS_DT_REG;
1026 break;
1027 case SQFS_BLKDEV_TYPE:
1028 case SQFS_CHRDEV_TYPE:
1029 case SQFS_LBLKDEV_TYPE:
1030 case SQFS_LCHRDEV_TYPE:
1031 case SQFS_FIFO_TYPE:
1032 case SQFS_SOCKET_TYPE:
1033 case SQFS_LFIFO_TYPE:
1034 case SQFS_LSOCKET_TYPE:
1035 dent->type = SQFS_MISC_ENTRY_TYPE;
1036 break;
1037 case SQFS_SYMLINK_TYPE:
1038 case SQFS_LSYMLINK_TYPE:
1039 dent->type = FS_DT_LNK;
1040 break;
1041 default:
1042 return -SQFS_STOP_READDIR;
1043 }
1044
1045 /* Set entry name */
1046 strncpy(dent->name, dirs->entry->name, dirs->entry->name_size + 1);
1047 dent->name[dirs->entry->name_size + 1] = '\0';
1048
1049 offset = dirs->entry->name_size + 1 + SQFS_ENTRY_BASE_LENGTH;
1050 dirs->entry_count--;
1051
1052 /* Decrement size to be read */
1053 if (dirs->size > offset)
1054 dirs->size -= offset;
1055 else
1056 dirs->size = 0;
1057
1058 /* Keep a reference to the current entry before incrementing it */
1059 dirs->table += offset;
1060
1061 *dentp = dent;
1062
1063 return 0;
1064}
1065
1066int sqfs_probe(struct blk_desc *fs_dev_desc, struct disk_partition *fs_partition)
1067{
1068 struct squashfs_super_block *sblk;
1069 int ret;
1070
1071 ctxt.cur_dev = fs_dev_desc;
1072 ctxt.cur_part_info = *fs_partition;
1073
1074 ret = sqfs_read_sblk(&sblk);
1075 if (ret)
1076 return ret;
1077
1078 /* Make sure it has a valid SquashFS magic number*/
1079 if (get_unaligned_le32(&sblk->s_magic) != SQFS_MAGIC_NUMBER) {
1080 printf("Bad magic number for SquashFS image.\n");
1081 ctxt.cur_dev = NULL;
1082 return -EINVAL;
1083 }
1084
1085 ctxt.sblk = sblk;
1086
Joao Marcos Costa877576c2020-08-18 17:17:21 +02001087 ret = sqfs_decompressor_init(&ctxt);
1088
1089 if (ret) {
1090 ctxt.cur_dev = NULL;
1091 free(ctxt.sblk);
1092 return -EINVAL;
1093 }
1094
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001095 return 0;
1096}
1097
1098static char *sqfs_basename(char *path)
1099{
1100 char *fname;
1101
1102 fname = path + strlen(path) - 1;
1103 while (fname >= path) {
1104 if (*fname == '/') {
1105 fname++;
1106 break;
1107 }
1108
1109 fname--;
1110 }
1111
1112 return fname;
1113}
1114
1115static char *sqfs_dirname(char *path)
1116{
1117 char *fname;
1118
1119 fname = sqfs_basename(path);
1120 --fname;
1121 *fname = '\0';
1122
1123 return path;
1124}
1125
1126/*
1127 * Takes a path to file and splits it in two parts: the filename itself and the
1128 * directory's path, e.g.:
1129 * path: /path/to/file.txt
1130 * file: file.txt
1131 * dir: /path/to
1132 */
1133static int sqfs_split_path(char **file, char **dir, const char *path)
1134{
1135 char *dirc, *basec, *bname, *dname, *tmp_path;
1136 int ret = 0;
1137
Richard Genoud8cf2f022020-11-03 12:11:03 +01001138 *file = NULL;
1139 *dir = NULL;
1140 dirc = NULL;
1141 basec = NULL;
1142 bname = NULL;
1143 dname = NULL;
1144 tmp_path = NULL;
1145
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001146 /* check for first slash in path*/
1147 if (path[0] == '/') {
1148 tmp_path = strdup(path);
Richard Genoud8cf2f022020-11-03 12:11:03 +01001149 if (!tmp_path) {
1150 ret = -ENOMEM;
1151 goto out;
1152 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001153 } else {
1154 tmp_path = malloc(strlen(path) + 2);
Richard Genoud8cf2f022020-11-03 12:11:03 +01001155 if (!tmp_path) {
1156 ret = -ENOMEM;
1157 goto out;
1158 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001159 tmp_path[0] = '/';
1160 strcpy(tmp_path + 1, path);
1161 }
1162
1163 /* String duplicates */
1164 dirc = strdup(tmp_path);
1165 if (!dirc) {
1166 ret = -ENOMEM;
Richard Genoud8cf2f022020-11-03 12:11:03 +01001167 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001168 }
1169
1170 basec = strdup(tmp_path);
1171 if (!basec) {
1172 ret = -ENOMEM;
Richard Genoud8cf2f022020-11-03 12:11:03 +01001173 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001174 }
1175
1176 dname = sqfs_dirname(dirc);
1177 bname = sqfs_basename(basec);
1178
1179 *file = strdup(bname);
1180
1181 if (!*file) {
1182 ret = -ENOMEM;
Richard Genoud8cf2f022020-11-03 12:11:03 +01001183 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001184 }
1185
1186 if (*dname == '\0') {
1187 *dir = malloc(2);
1188 if (!*dir) {
1189 ret = -ENOMEM;
Richard Genoud8cf2f022020-11-03 12:11:03 +01001190 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001191 }
1192
1193 (*dir)[0] = '/';
1194 (*dir)[1] = '\0';
1195 } else {
1196 *dir = strdup(dname);
1197 if (!*dir) {
1198 ret = -ENOMEM;
Richard Genoud8cf2f022020-11-03 12:11:03 +01001199 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001200 }
1201 }
1202
Richard Genoud8cf2f022020-11-03 12:11:03 +01001203out:
1204 if (ret) {
1205 free(*file);
1206 free(*dir);
1207 *dir = NULL;
1208 *file = NULL;
1209 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001210 free(basec);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001211 free(dirc);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001212 free(tmp_path);
1213
1214 return ret;
1215}
1216
1217static int sqfs_get_regfile_info(struct squashfs_reg_inode *reg,
1218 struct squashfs_file_info *finfo,
1219 struct squashfs_fragment_block_entry *fentry,
1220 __le32 blksz)
1221{
1222 int datablk_count = 0, ret;
1223
1224 finfo->size = get_unaligned_le32(&reg->file_size);
1225 finfo->offset = get_unaligned_le32(&reg->offset);
1226 finfo->start = get_unaligned_le32(&reg->start_block);
1227 finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(&reg->fragment));
1228
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +02001229 if (finfo->frag && finfo->offset == 0xFFFFFFFF)
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +02001230 return -EINVAL;
1231
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +02001232 if (finfo->size < 1 || finfo->start == 0xFFFFFFFF)
1233 return -EINVAL;
1234
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001235 if (finfo->frag) {
1236 datablk_count = finfo->size / le32_to_cpu(blksz);
1237 ret = sqfs_frag_lookup(get_unaligned_le32(&reg->fragment),
1238 fentry);
1239 if (ret < 0)
1240 return -EINVAL;
1241 finfo->comp = true;
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +02001242 if (fentry->size < 1 || fentry->start == 0x7FFFFFFF)
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +02001243 return -EINVAL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001244 } else {
1245 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1246 }
1247
1248 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1249 if (!finfo->blk_sizes)
1250 return -ENOMEM;
1251
1252 return datablk_count;
1253}
1254
1255static int sqfs_get_lregfile_info(struct squashfs_lreg_inode *lreg,
1256 struct squashfs_file_info *finfo,
1257 struct squashfs_fragment_block_entry *fentry,
1258 __le32 blksz)
1259{
1260 int datablk_count = 0, ret;
1261
1262 finfo->size = get_unaligned_le64(&lreg->file_size);
1263 finfo->offset = get_unaligned_le32(&lreg->offset);
1264 finfo->start = get_unaligned_le64(&lreg->start_block);
1265 finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(&lreg->fragment));
1266
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +02001267 if (finfo->frag && finfo->offset == 0xFFFFFFFF)
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +02001268 return -EINVAL;
1269
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +02001270 if (finfo->size < 1 || finfo->start == 0x7FFFFFFF)
1271 return -EINVAL;
1272
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001273 if (finfo->frag) {
1274 datablk_count = finfo->size / le32_to_cpu(blksz);
1275 ret = sqfs_frag_lookup(get_unaligned_le32(&lreg->fragment),
1276 fentry);
1277 if (ret < 0)
1278 return -EINVAL;
1279 finfo->comp = true;
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +02001280 if (fentry->size < 1 || fentry->start == 0x7FFFFFFF)
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +02001281 return -EINVAL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001282 } else {
1283 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1284 }
1285
1286 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1287 if (!finfo->blk_sizes)
1288 return -ENOMEM;
1289
1290 return datablk_count;
1291}
1292
1293int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
1294 loff_t *actread)
1295{
1296 char *dir, *fragment_block, *datablock = NULL, *data_buffer = NULL;
1297 char *fragment, *file, *resolved, *data;
1298 u64 start, n_blks, table_size, data_offset, table_offset;
Joao Marcos Costab87fd012020-08-18 17:17:22 +02001299 int ret, j, i_number, datablk_count = 0;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001300 struct squashfs_super_block *sblk = ctxt.sblk;
1301 struct squashfs_fragment_block_entry frag_entry;
1302 struct squashfs_file_info finfo = {0};
1303 struct squashfs_symlink_inode *symlink;
1304 struct fs_dir_stream *dirsp = NULL;
1305 struct squashfs_dir_stream *dirs;
1306 struct squashfs_lreg_inode *lreg;
1307 struct squashfs_base_inode *base;
1308 struct squashfs_reg_inode *reg;
1309 unsigned long dest_len;
1310 struct fs_dirent *dent;
1311 unsigned char *ipos;
1312
1313 *actread = 0;
1314
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001315 /*
1316 * sqfs_opendir will uncompress inode and directory tables, and will
1317 * return a pointer to the directory that contains the requested file.
1318 */
1319 sqfs_split_path(&file, &dir, filename);
1320 ret = sqfs_opendir(dir, &dirsp);
1321 if (ret) {
1322 sqfs_closedir(dirsp);
1323 goto free_paths;
1324 }
1325
1326 dirs = (struct squashfs_dir_stream *)dirsp;
1327
1328 /* For now, only regular files are able to be loaded */
1329 while (!sqfs_readdir(dirsp, &dent)) {
1330 ret = strcmp(dent->name, file);
1331 if (!ret)
1332 break;
1333
1334 free(dirs->entry);
1335 }
1336
1337 if (ret) {
1338 printf("File not found.\n");
1339 *actread = 0;
1340 sqfs_closedir(dirsp);
1341 ret = -ENOENT;
1342 goto free_paths;
1343 }
1344
1345 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1346 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1347 sblk->block_size);
1348
1349 base = (struct squashfs_base_inode *)ipos;
1350 switch (get_unaligned_le16(&base->inode_type)) {
1351 case SQFS_REG_TYPE:
1352 reg = (struct squashfs_reg_inode *)ipos;
1353 datablk_count = sqfs_get_regfile_info(reg, &finfo, &frag_entry,
1354 sblk->block_size);
1355 if (datablk_count < 0) {
1356 ret = -EINVAL;
1357 goto free_paths;
1358 }
1359
1360 memcpy(finfo.blk_sizes, ipos + sizeof(*reg),
1361 datablk_count * sizeof(u32));
1362 break;
1363 case SQFS_LREG_TYPE:
1364 lreg = (struct squashfs_lreg_inode *)ipos;
1365 datablk_count = sqfs_get_lregfile_info(lreg, &finfo,
1366 &frag_entry,
1367 sblk->block_size);
1368 if (datablk_count < 0) {
1369 ret = -EINVAL;
1370 goto free_paths;
1371 }
1372
1373 memcpy(finfo.blk_sizes, ipos + sizeof(*lreg),
1374 datablk_count * sizeof(u32));
1375 break;
1376 case SQFS_SYMLINK_TYPE:
1377 case SQFS_LSYMLINK_TYPE:
1378 symlink = (struct squashfs_symlink_inode *)ipos;
1379 resolved = sqfs_resolve_symlink(symlink, filename);
1380 ret = sqfs_read(resolved, buf, offset, len, actread);
1381 free(resolved);
1382 goto free_paths;
1383 case SQFS_BLKDEV_TYPE:
1384 case SQFS_CHRDEV_TYPE:
1385 case SQFS_LBLKDEV_TYPE:
1386 case SQFS_LCHRDEV_TYPE:
1387 case SQFS_FIFO_TYPE:
1388 case SQFS_SOCKET_TYPE:
1389 case SQFS_LFIFO_TYPE:
1390 case SQFS_LSOCKET_TYPE:
1391 default:
1392 printf("Unsupported entry type\n");
1393 ret = -EINVAL;
1394 goto free_paths;
1395 }
1396
1397 /* If the user specifies a length, check its sanity */
1398 if (len) {
1399 if (len > finfo.size) {
1400 ret = -EINVAL;
1401 goto free_paths;
1402 }
1403
1404 finfo.size = len;
1405 }
1406
1407 if (datablk_count) {
1408 data_offset = finfo.start;
1409 datablock = malloc(get_unaligned_le32(&sblk->block_size));
1410 if (!datablock) {
1411 ret = -ENOMEM;
1412 goto free_paths;
1413 }
1414 }
1415
1416 for (j = 0; j < datablk_count; j++) {
1417 start = data_offset / ctxt.cur_dev->blksz;
1418 table_size = SQFS_BLOCK_SIZE(finfo.blk_sizes[j]);
1419 table_offset = data_offset - (start * ctxt.cur_dev->blksz);
1420 n_blks = DIV_ROUND_UP(table_size + table_offset,
1421 ctxt.cur_dev->blksz);
1422
1423 data_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
1424
1425 if (!data_buffer) {
1426 ret = -ENOMEM;
1427 goto free_datablk;
1428 }
1429
1430 ret = sqfs_disk_read(start, n_blks, data_buffer);
1431 if (ret < 0) {
1432 /*
1433 * Possible causes: too many data blocks or too large
1434 * SquashFS block size. Tip: re-compile the SquashFS
1435 * image with mksquashfs's -b <block_size> option.
1436 */
1437 printf("Error: too many data blocks to be read.\n");
1438 goto free_buffer;
1439 }
1440
1441 data = data_buffer + table_offset;
1442
1443 /* Load the data */
1444 if (SQFS_COMPRESSED_BLOCK(finfo.blk_sizes[j])) {
1445 dest_len = get_unaligned_le32(&sblk->block_size);
Joao Marcos Costab87fd012020-08-18 17:17:22 +02001446 ret = sqfs_decompress(&ctxt, datablock, &dest_len,
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001447 data, table_size);
1448 if (ret)
1449 goto free_buffer;
1450
1451 memcpy(buf + offset + *actread, datablock, dest_len);
1452 *actread += dest_len;
1453 } else {
1454 memcpy(buf + offset + *actread, data, table_size);
1455 *actread += table_size;
1456 }
1457
1458 data_offset += table_size;
1459 }
1460
1461 free(finfo.blk_sizes);
1462
1463 /*
1464 * There is no need to continue if the file is not fragmented.
1465 */
1466 if (!finfo.frag) {
1467 ret = 0;
1468 goto free_buffer;
1469 }
1470
1471 start = frag_entry.start / ctxt.cur_dev->blksz;
1472 table_size = SQFS_BLOCK_SIZE(frag_entry.size);
1473 table_offset = frag_entry.start - (start * ctxt.cur_dev->blksz);
1474 n_blks = DIV_ROUND_UP(table_size + table_offset, ctxt.cur_dev->blksz);
1475
1476 fragment = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
1477
1478 if (!fragment) {
1479 ret = -ENOMEM;
1480 goto free_buffer;
1481 }
1482
1483 ret = sqfs_disk_read(start, n_blks, fragment);
1484 if (ret < 0)
1485 goto free_fragment;
1486
1487 /* File compressed and fragmented */
1488 if (finfo.frag && finfo.comp) {
1489 dest_len = get_unaligned_le32(&sblk->block_size);
1490 fragment_block = malloc(dest_len);
1491 if (!fragment_block) {
1492 ret = -ENOMEM;
1493 goto free_fragment;
1494 }
1495
Joao Marcos Costab87fd012020-08-18 17:17:22 +02001496 ret = sqfs_decompress(&ctxt, fragment_block, &dest_len,
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001497 (void *)fragment + table_offset,
1498 frag_entry.size);
1499 if (ret) {
1500 free(fragment_block);
1501 goto free_fragment;
1502 }
1503
1504 for (j = offset + *actread; j < finfo.size; j++) {
1505 memcpy(buf + j, &fragment_block[finfo.offset + j], 1);
1506 (*actread)++;
1507 }
1508
1509 free(fragment_block);
1510
1511 } else if (finfo.frag && !finfo.comp) {
1512 fragment_block = (void *)fragment + table_offset;
1513
1514 for (j = offset + *actread; j < finfo.size; j++) {
1515 memcpy(buf + j, &fragment_block[finfo.offset + j], 1);
1516 (*actread)++;
1517 }
1518 }
1519
1520free_fragment:
1521 free(fragment);
1522free_buffer:
1523 if (datablk_count)
1524 free(data_buffer);
1525free_datablk:
1526 if (datablk_count)
1527 free(datablock);
1528free_paths:
1529 free(file);
1530 free(dir);
1531
1532 return ret;
1533}
1534
1535int sqfs_size(const char *filename, loff_t *size)
1536{
1537 struct squashfs_super_block *sblk = ctxt.sblk;
1538 struct squashfs_symlink_inode *symlink;
1539 struct fs_dir_stream *dirsp = NULL;
1540 struct squashfs_base_inode *base;
1541 struct squashfs_dir_stream *dirs;
1542 struct squashfs_lreg_inode *lreg;
1543 struct squashfs_reg_inode *reg;
1544 char *dir, *file, *resolved;
1545 struct fs_dirent *dent;
1546 unsigned char *ipos;
1547 int ret, i_number;
1548
1549 sqfs_split_path(&file, &dir, filename);
1550 /*
1551 * sqfs_opendir will uncompress inode and directory tables, and will
1552 * return a pointer to the directory that contains the requested file.
1553 */
1554 ret = sqfs_opendir(dir, &dirsp);
1555 if (ret) {
1556 sqfs_closedir(dirsp);
1557 ret = -EINVAL;
1558 goto free_strings;
1559 }
1560
1561 dirs = (struct squashfs_dir_stream *)dirsp;
1562
1563 while (!sqfs_readdir(dirsp, &dent)) {
1564 ret = strcmp(dent->name, file);
1565 if (!ret)
1566 break;
1567 free(dirs->entry);
1568 }
1569
1570 if (ret) {
1571 printf("File not found.\n");
1572 *size = 0;
1573 ret = -EINVAL;
1574 goto free_strings;
1575 }
1576
1577 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1578 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1579 sblk->block_size);
1580 free(dirs->entry);
1581
1582 base = (struct squashfs_base_inode *)ipos;
1583 switch (get_unaligned_le16(&base->inode_type)) {
1584 case SQFS_REG_TYPE:
1585 reg = (struct squashfs_reg_inode *)ipos;
1586 *size = get_unaligned_le32(&reg->file_size);
1587 break;
1588 case SQFS_LREG_TYPE:
1589 lreg = (struct squashfs_lreg_inode *)ipos;
1590 *size = get_unaligned_le64(&lreg->file_size);
1591 break;
1592 case SQFS_SYMLINK_TYPE:
1593 case SQFS_LSYMLINK_TYPE:
1594 symlink = (struct squashfs_symlink_inode *)ipos;
1595 resolved = sqfs_resolve_symlink(symlink, filename);
1596 ret = sqfs_size(resolved, size);
1597 free(resolved);
1598 break;
1599 case SQFS_BLKDEV_TYPE:
1600 case SQFS_CHRDEV_TYPE:
1601 case SQFS_LBLKDEV_TYPE:
1602 case SQFS_LCHRDEV_TYPE:
1603 case SQFS_FIFO_TYPE:
1604 case SQFS_SOCKET_TYPE:
1605 case SQFS_LFIFO_TYPE:
1606 case SQFS_LSOCKET_TYPE:
1607 default:
1608 printf("Unable to recover entry's size.\n");
1609 *size = 0;
1610 ret = -EINVAL;
1611 break;
1612 }
1613
1614free_strings:
1615 free(dir);
1616 free(file);
1617
1618 sqfs_closedir(dirsp);
1619
1620 return ret;
1621}
1622
1623void sqfs_close(void)
1624{
1625 free(ctxt.sblk);
1626 ctxt.cur_dev = NULL;
Joao Marcos Costa877576c2020-08-18 17:17:21 +02001627 sqfs_decompressor_cleanup(&ctxt);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001628}
1629
1630void sqfs_closedir(struct fs_dir_stream *dirs)
1631{
1632 struct squashfs_dir_stream *sqfs_dirs;
1633
1634 sqfs_dirs = (struct squashfs_dir_stream *)dirs;
1635 free(sqfs_dirs->inode_table);
1636 free(sqfs_dirs->dir_table);
1637 free(sqfs_dirs->dir_header);
Richard Genoud2575eb22020-11-03 12:11:02 +01001638 free(sqfs_dirs);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001639}