blob: 72181f38332970dc7e62623ddcf98635e9670b86 [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);
Richard Genouda62528d2020-11-03 12:11:07 +0100734 *inode_table = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200735 goto free_itb;
736 }
737
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200738 dest_offset += dest_len;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200739 } else {
740 memcpy(*inode_table + (j * SQFS_METADATA_BLOCK_SIZE),
741 src_table, src_len);
742 }
743
744 /*
745 * Offsets to the decompression destination, to the metadata
746 * buffer 'itb' and to the decompression source, respectively.
747 */
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200748
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200749 table_offset += src_len + SQFS_HEADER_SIZE;
750 src_table += src_len + SQFS_HEADER_SIZE;
751 }
752
753free_itb:
754 free(itb);
755
756 return ret;
757}
758
759static int sqfs_read_directory_table(unsigned char **dir_table, u32 **pos_list)
760{
761 u64 start, n_blks, table_offset, table_size;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200762 struct squashfs_super_block *sblk = ctxt.sblk;
Joao Marcos Costab87fd012020-08-18 17:17:22 +0200763 int j, ret = 0, metablks_count = -1;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200764 unsigned char *src_table, *dtb;
765 u32 src_len, dest_offset = 0;
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200766 unsigned long dest_len = 0;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200767 bool compressed;
768
Richard Genoud2f9c9852020-11-03 12:11:04 +0100769 *dir_table = NULL;
770 *pos_list = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200771 /* DIRECTORY TABLE */
772 table_size = get_unaligned_le64(&sblk->fragment_table_start) -
773 get_unaligned_le64(&sblk->directory_table_start);
774 start = get_unaligned_le64(&sblk->directory_table_start) /
775 ctxt.cur_dev->blksz;
776 n_blks = sqfs_calc_n_blks(sblk->directory_table_start,
777 sblk->fragment_table_start, &table_offset);
778
779 /* Allocate a proper sized buffer (dtb) to store the directory table */
780 dtb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
781 if (!dtb)
782 return -ENOMEM;
783
784 if (sqfs_disk_read(start, n_blks, dtb) < 0)
Richard Genoud2f9c9852020-11-03 12:11:04 +0100785 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200786
787 /* Parse directory table (metadata block) header */
788 ret = sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
789 if (ret)
Richard Genoud2f9c9852020-11-03 12:11:04 +0100790 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200791
792 /* Calculate total size to store the whole decompressed table */
793 metablks_count = sqfs_count_metablks(dtb, table_offset, table_size);
794 if (metablks_count < 1)
Richard Genoud2f9c9852020-11-03 12:11:04 +0100795 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200796
797 *dir_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE);
798 if (!*dir_table)
Richard Genoud2f9c9852020-11-03 12:11:04 +0100799 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200800
801 *pos_list = malloc(metablks_count * sizeof(u32));
Richard Genoud2f9c9852020-11-03 12:11:04 +0100802 if (!*pos_list)
803 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200804
805 ret = sqfs_get_metablk_pos(*pos_list, dtb, table_offset,
806 metablks_count);
807 if (ret) {
808 metablks_count = -1;
Richard Genoud2f9c9852020-11-03 12:11:04 +0100809 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200810 }
811
812 src_table = dtb + table_offset + SQFS_HEADER_SIZE;
813
814 /* Extract compressed Directory table */
815 dest_offset = 0;
816 for (j = 0; j < metablks_count; j++) {
817 sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
818 if (compressed) {
819 dest_len = SQFS_METADATA_BLOCK_SIZE;
Joao Marcos Costab87fd012020-08-18 17:17:22 +0200820 ret = sqfs_decompress(&ctxt, *dir_table +
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200821 (j * SQFS_METADATA_BLOCK_SIZE),
822 &dest_len, src_table, src_len);
823 if (ret) {
824 metablks_count = -1;
Richard Genoud2f9c9852020-11-03 12:11:04 +0100825 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200826 }
827
828 if (dest_len < SQFS_METADATA_BLOCK_SIZE) {
829 dest_offset += dest_len;
830 break;
831 }
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200832
833 dest_offset += dest_len;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200834 } else {
835 memcpy(*dir_table + (j * SQFS_METADATA_BLOCK_SIZE),
836 src_table, src_len);
837 }
838
839 /*
840 * Offsets to the decompression destination, to the metadata
841 * buffer 'dtb' and to the decompression source, respectively.
842 */
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200843 table_offset += src_len + SQFS_HEADER_SIZE;
844 src_table += src_len + SQFS_HEADER_SIZE;
845 }
846
Richard Genoud2f9c9852020-11-03 12:11:04 +0100847out:
848 if (metablks_count < 1) {
849 free(*dir_table);
850 free(*pos_list);
851 *dir_table = NULL;
852 *pos_list = NULL;
853 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200854 free(dtb);
855
856 return metablks_count;
857}
858
859int sqfs_opendir(const char *filename, struct fs_dir_stream **dirsp)
860{
861 unsigned char *inode_table = NULL, *dir_table = NULL;
Richard Genoud32bea5b2020-11-03 12:11:01 +0100862 int j, token_count = 0, ret = 0, metablks_count;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200863 struct squashfs_dir_stream *dirs;
Richard Genoud32bea5b2020-11-03 12:11:01 +0100864 char **token_list = NULL, *path = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200865 u32 *pos_list = NULL;
866
867 dirs = malloc(sizeof(*dirs));
868 if (!dirs)
869 return -EINVAL;
870
Richard Genoud557f08f2020-11-03 12:11:00 +0100871 /* these should be set to NULL to prevent dangling pointers */
872 dirs->dir_header = NULL;
873 dirs->entry = NULL;
874 dirs->table = NULL;
875 dirs->inode_table = NULL;
876 dirs->dir_table = NULL;
877
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200878 ret = sqfs_read_inode_table(&inode_table);
Richard Genoud557f08f2020-11-03 12:11:00 +0100879 if (ret) {
880 ret = -EINVAL;
Richard Genoud32bea5b2020-11-03 12:11:01 +0100881 goto out;
Richard Genoud557f08f2020-11-03 12:11:00 +0100882 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200883
884 metablks_count = sqfs_read_directory_table(&dir_table, &pos_list);
Richard Genoud557f08f2020-11-03 12:11:00 +0100885 if (metablks_count < 1) {
886 ret = -EINVAL;
Richard Genoud32bea5b2020-11-03 12:11:01 +0100887 goto out;
Richard Genoud557f08f2020-11-03 12:11:00 +0100888 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200889
890 /* Tokenize filename */
891 token_count = sqfs_count_tokens(filename);
Richard Genoud557f08f2020-11-03 12:11:00 +0100892 if (token_count < 0) {
893 ret = -EINVAL;
Richard Genoud32bea5b2020-11-03 12:11:01 +0100894 goto out;
Richard Genoud557f08f2020-11-03 12:11:00 +0100895 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200896
897 path = strdup(filename);
Richard Genoud557f08f2020-11-03 12:11:00 +0100898 if (!path) {
899 ret = -EINVAL;
Richard Genoud32bea5b2020-11-03 12:11:01 +0100900 goto out;
Richard Genoud557f08f2020-11-03 12:11:00 +0100901 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200902
903 token_list = malloc(token_count * sizeof(char *));
904 if (!token_list) {
905 ret = -EINVAL;
Richard Genoud32bea5b2020-11-03 12:11:01 +0100906 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200907 }
908
909 /* Fill tokens list */
910 ret = sqfs_tokenize(token_list, token_count, path);
911 if (ret)
Richard Genoud32bea5b2020-11-03 12:11:01 +0100912 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200913 /*
914 * ldir's (extended directory) size is greater than dir, so it works as
915 * a general solution for the malloc size, since 'i' is a union.
916 */
917 dirs->inode_table = inode_table;
918 dirs->dir_table = dir_table;
919 ret = sqfs_search_dir(dirs, token_list, token_count, pos_list,
920 metablks_count);
921 if (ret)
Richard Genoud32bea5b2020-11-03 12:11:01 +0100922 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200923
924 if (le16_to_cpu(dirs->i_dir.inode_type) == SQFS_DIR_TYPE)
925 dirs->size = le16_to_cpu(dirs->i_dir.file_size);
926 else
927 dirs->size = le32_to_cpu(dirs->i_ldir.file_size);
928
929 /* Setup directory header */
930 memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE);
931 dirs->entry_count = dirs->dir_header->count + 1;
932 dirs->size -= SQFS_DIR_HEADER_SIZE;
933
934 /* Setup entry */
935 dirs->entry = NULL;
936 dirs->table += SQFS_DIR_HEADER_SIZE;
937
938 *dirsp = (struct fs_dir_stream *)dirs;
939
Richard Genoud32bea5b2020-11-03 12:11:01 +0100940out:
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200941 for (j = 0; j < token_count; j++)
942 free(token_list[j]);
943 free(token_list);
944 free(pos_list);
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200945 free(path);
Richard Genoud32bea5b2020-11-03 12:11:01 +0100946 if (ret) {
Richard Genoud557f08f2020-11-03 12:11:00 +0100947 free(inode_table);
Richard Genoud557f08f2020-11-03 12:11:00 +0100948 free(dirs);
Richard Genoud32bea5b2020-11-03 12:11:01 +0100949 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200950
951 return ret;
952}
953
954int sqfs_readdir(struct fs_dir_stream *fs_dirs, struct fs_dirent **dentp)
955{
956 struct squashfs_super_block *sblk = ctxt.sblk;
957 struct squashfs_dir_stream *dirs;
958 struct squashfs_lreg_inode *lreg;
959 struct squashfs_base_inode *base;
960 struct squashfs_reg_inode *reg;
961 int i_number, offset = 0, ret;
962 struct fs_dirent *dent;
963 unsigned char *ipos;
964
965 dirs = (struct squashfs_dir_stream *)fs_dirs;
966 if (!dirs->size) {
967 *dentp = NULL;
968 return -SQFS_STOP_READDIR;
969 }
970
971 dent = &dirs->dentp;
972
973 if (!dirs->entry_count) {
974 if (dirs->size > SQFS_DIR_HEADER_SIZE) {
975 dirs->size -= SQFS_DIR_HEADER_SIZE;
976 } else {
977 *dentp = NULL;
978 dirs->size = 0;
979 return -SQFS_STOP_READDIR;
980 }
981
982 if (dirs->size > SQFS_EMPTY_FILE_SIZE) {
983 /* Read follow-up (emitted) dir. header */
984 memcpy(dirs->dir_header, dirs->table,
985 SQFS_DIR_HEADER_SIZE);
986 dirs->entry_count = dirs->dir_header->count + 1;
987 ret = sqfs_read_entry(&dirs->entry, dirs->table +
988 SQFS_DIR_HEADER_SIZE);
989 if (ret)
990 return -SQFS_STOP_READDIR;
991
992 dirs->table += SQFS_DIR_HEADER_SIZE;
993 }
994 } else {
995 ret = sqfs_read_entry(&dirs->entry, dirs->table);
996 if (ret)
997 return -SQFS_STOP_READDIR;
998 }
999
1000 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1001 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1002 sblk->block_size);
1003
1004 base = (struct squashfs_base_inode *)ipos;
1005
1006 /* Set entry type and size */
1007 switch (dirs->entry->type) {
1008 case SQFS_DIR_TYPE:
1009 case SQFS_LDIR_TYPE:
1010 dent->type = FS_DT_DIR;
1011 break;
1012 case SQFS_REG_TYPE:
1013 case SQFS_LREG_TYPE:
1014 /*
1015 * Entries do not differentiate extended from regular types, so
1016 * it needs to be verified manually.
1017 */
1018 if (get_unaligned_le16(&base->inode_type) == SQFS_LREG_TYPE) {
1019 lreg = (struct squashfs_lreg_inode *)ipos;
1020 dent->size = get_unaligned_le64(&lreg->file_size);
1021 } else {
1022 reg = (struct squashfs_reg_inode *)ipos;
1023 dent->size = get_unaligned_le32(&reg->file_size);
1024 }
1025
1026 dent->type = FS_DT_REG;
1027 break;
1028 case SQFS_BLKDEV_TYPE:
1029 case SQFS_CHRDEV_TYPE:
1030 case SQFS_LBLKDEV_TYPE:
1031 case SQFS_LCHRDEV_TYPE:
1032 case SQFS_FIFO_TYPE:
1033 case SQFS_SOCKET_TYPE:
1034 case SQFS_LFIFO_TYPE:
1035 case SQFS_LSOCKET_TYPE:
1036 dent->type = SQFS_MISC_ENTRY_TYPE;
1037 break;
1038 case SQFS_SYMLINK_TYPE:
1039 case SQFS_LSYMLINK_TYPE:
1040 dent->type = FS_DT_LNK;
1041 break;
1042 default:
1043 return -SQFS_STOP_READDIR;
1044 }
1045
1046 /* Set entry name */
1047 strncpy(dent->name, dirs->entry->name, dirs->entry->name_size + 1);
1048 dent->name[dirs->entry->name_size + 1] = '\0';
1049
1050 offset = dirs->entry->name_size + 1 + SQFS_ENTRY_BASE_LENGTH;
1051 dirs->entry_count--;
1052
1053 /* Decrement size to be read */
1054 if (dirs->size > offset)
1055 dirs->size -= offset;
1056 else
1057 dirs->size = 0;
1058
1059 /* Keep a reference to the current entry before incrementing it */
1060 dirs->table += offset;
1061
1062 *dentp = dent;
1063
1064 return 0;
1065}
1066
1067int sqfs_probe(struct blk_desc *fs_dev_desc, struct disk_partition *fs_partition)
1068{
1069 struct squashfs_super_block *sblk;
1070 int ret;
1071
1072 ctxt.cur_dev = fs_dev_desc;
1073 ctxt.cur_part_info = *fs_partition;
1074
1075 ret = sqfs_read_sblk(&sblk);
1076 if (ret)
1077 return ret;
1078
1079 /* Make sure it has a valid SquashFS magic number*/
1080 if (get_unaligned_le32(&sblk->s_magic) != SQFS_MAGIC_NUMBER) {
1081 printf("Bad magic number for SquashFS image.\n");
1082 ctxt.cur_dev = NULL;
1083 return -EINVAL;
1084 }
1085
1086 ctxt.sblk = sblk;
1087
Joao Marcos Costa877576c2020-08-18 17:17:21 +02001088 ret = sqfs_decompressor_init(&ctxt);
1089
1090 if (ret) {
1091 ctxt.cur_dev = NULL;
1092 free(ctxt.sblk);
1093 return -EINVAL;
1094 }
1095
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001096 return 0;
1097}
1098
1099static char *sqfs_basename(char *path)
1100{
1101 char *fname;
1102
1103 fname = path + strlen(path) - 1;
1104 while (fname >= path) {
1105 if (*fname == '/') {
1106 fname++;
1107 break;
1108 }
1109
1110 fname--;
1111 }
1112
1113 return fname;
1114}
1115
1116static char *sqfs_dirname(char *path)
1117{
1118 char *fname;
1119
1120 fname = sqfs_basename(path);
1121 --fname;
1122 *fname = '\0';
1123
1124 return path;
1125}
1126
1127/*
1128 * Takes a path to file and splits it in two parts: the filename itself and the
1129 * directory's path, e.g.:
1130 * path: /path/to/file.txt
1131 * file: file.txt
1132 * dir: /path/to
1133 */
1134static int sqfs_split_path(char **file, char **dir, const char *path)
1135{
1136 char *dirc, *basec, *bname, *dname, *tmp_path;
1137 int ret = 0;
1138
Richard Genoud8cf2f022020-11-03 12:11:03 +01001139 *file = NULL;
1140 *dir = NULL;
1141 dirc = NULL;
1142 basec = NULL;
1143 bname = NULL;
1144 dname = NULL;
1145 tmp_path = NULL;
1146
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001147 /* check for first slash in path*/
1148 if (path[0] == '/') {
1149 tmp_path = strdup(path);
Richard Genoud8cf2f022020-11-03 12:11:03 +01001150 if (!tmp_path) {
1151 ret = -ENOMEM;
1152 goto out;
1153 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001154 } else {
1155 tmp_path = malloc(strlen(path) + 2);
Richard Genoud8cf2f022020-11-03 12:11:03 +01001156 if (!tmp_path) {
1157 ret = -ENOMEM;
1158 goto out;
1159 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001160 tmp_path[0] = '/';
1161 strcpy(tmp_path + 1, path);
1162 }
1163
1164 /* String duplicates */
1165 dirc = strdup(tmp_path);
1166 if (!dirc) {
1167 ret = -ENOMEM;
Richard Genoud8cf2f022020-11-03 12:11:03 +01001168 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001169 }
1170
1171 basec = strdup(tmp_path);
1172 if (!basec) {
1173 ret = -ENOMEM;
Richard Genoud8cf2f022020-11-03 12:11:03 +01001174 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001175 }
1176
1177 dname = sqfs_dirname(dirc);
1178 bname = sqfs_basename(basec);
1179
1180 *file = strdup(bname);
1181
1182 if (!*file) {
1183 ret = -ENOMEM;
Richard Genoud8cf2f022020-11-03 12:11:03 +01001184 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001185 }
1186
1187 if (*dname == '\0') {
1188 *dir = malloc(2);
1189 if (!*dir) {
1190 ret = -ENOMEM;
Richard Genoud8cf2f022020-11-03 12:11:03 +01001191 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001192 }
1193
1194 (*dir)[0] = '/';
1195 (*dir)[1] = '\0';
1196 } else {
1197 *dir = strdup(dname);
1198 if (!*dir) {
1199 ret = -ENOMEM;
Richard Genoud8cf2f022020-11-03 12:11:03 +01001200 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001201 }
1202 }
1203
Richard Genoud8cf2f022020-11-03 12:11:03 +01001204out:
1205 if (ret) {
1206 free(*file);
1207 free(*dir);
1208 *dir = NULL;
1209 *file = NULL;
1210 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001211 free(basec);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001212 free(dirc);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001213 free(tmp_path);
1214
1215 return ret;
1216}
1217
1218static int sqfs_get_regfile_info(struct squashfs_reg_inode *reg,
1219 struct squashfs_file_info *finfo,
1220 struct squashfs_fragment_block_entry *fentry,
1221 __le32 blksz)
1222{
1223 int datablk_count = 0, ret;
1224
1225 finfo->size = get_unaligned_le32(&reg->file_size);
1226 finfo->offset = get_unaligned_le32(&reg->offset);
1227 finfo->start = get_unaligned_le32(&reg->start_block);
1228 finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(&reg->fragment));
1229
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +02001230 if (finfo->frag && finfo->offset == 0xFFFFFFFF)
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +02001231 return -EINVAL;
1232
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +02001233 if (finfo->size < 1 || finfo->start == 0xFFFFFFFF)
1234 return -EINVAL;
1235
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001236 if (finfo->frag) {
1237 datablk_count = finfo->size / le32_to_cpu(blksz);
1238 ret = sqfs_frag_lookup(get_unaligned_le32(&reg->fragment),
1239 fentry);
1240 if (ret < 0)
1241 return -EINVAL;
1242 finfo->comp = true;
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +02001243 if (fentry->size < 1 || fentry->start == 0x7FFFFFFF)
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +02001244 return -EINVAL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001245 } else {
1246 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1247 }
1248
1249 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1250 if (!finfo->blk_sizes)
1251 return -ENOMEM;
1252
1253 return datablk_count;
1254}
1255
1256static int sqfs_get_lregfile_info(struct squashfs_lreg_inode *lreg,
1257 struct squashfs_file_info *finfo,
1258 struct squashfs_fragment_block_entry *fentry,
1259 __le32 blksz)
1260{
1261 int datablk_count = 0, ret;
1262
1263 finfo->size = get_unaligned_le64(&lreg->file_size);
1264 finfo->offset = get_unaligned_le32(&lreg->offset);
1265 finfo->start = get_unaligned_le64(&lreg->start_block);
1266 finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(&lreg->fragment));
1267
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +02001268 if (finfo->frag && finfo->offset == 0xFFFFFFFF)
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +02001269 return -EINVAL;
1270
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +02001271 if (finfo->size < 1 || finfo->start == 0x7FFFFFFF)
1272 return -EINVAL;
1273
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001274 if (finfo->frag) {
1275 datablk_count = finfo->size / le32_to_cpu(blksz);
1276 ret = sqfs_frag_lookup(get_unaligned_le32(&lreg->fragment),
1277 fentry);
1278 if (ret < 0)
1279 return -EINVAL;
1280 finfo->comp = true;
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +02001281 if (fentry->size < 1 || fentry->start == 0x7FFFFFFF)
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +02001282 return -EINVAL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001283 } else {
1284 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1285 }
1286
1287 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1288 if (!finfo->blk_sizes)
1289 return -ENOMEM;
1290
1291 return datablk_count;
1292}
1293
1294int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
1295 loff_t *actread)
1296{
1297 char *dir, *fragment_block, *datablock = NULL, *data_buffer = NULL;
1298 char *fragment, *file, *resolved, *data;
1299 u64 start, n_blks, table_size, data_offset, table_offset;
Joao Marcos Costab87fd012020-08-18 17:17:22 +02001300 int ret, j, i_number, datablk_count = 0;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001301 struct squashfs_super_block *sblk = ctxt.sblk;
1302 struct squashfs_fragment_block_entry frag_entry;
1303 struct squashfs_file_info finfo = {0};
1304 struct squashfs_symlink_inode *symlink;
1305 struct fs_dir_stream *dirsp = NULL;
1306 struct squashfs_dir_stream *dirs;
1307 struct squashfs_lreg_inode *lreg;
1308 struct squashfs_base_inode *base;
1309 struct squashfs_reg_inode *reg;
1310 unsigned long dest_len;
1311 struct fs_dirent *dent;
1312 unsigned char *ipos;
1313
1314 *actread = 0;
1315
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001316 /*
1317 * sqfs_opendir will uncompress inode and directory tables, and will
1318 * return a pointer to the directory that contains the requested file.
1319 */
1320 sqfs_split_path(&file, &dir, filename);
1321 ret = sqfs_opendir(dir, &dirsp);
1322 if (ret) {
1323 sqfs_closedir(dirsp);
1324 goto free_paths;
1325 }
1326
1327 dirs = (struct squashfs_dir_stream *)dirsp;
1328
1329 /* For now, only regular files are able to be loaded */
1330 while (!sqfs_readdir(dirsp, &dent)) {
1331 ret = strcmp(dent->name, file);
1332 if (!ret)
1333 break;
1334
1335 free(dirs->entry);
1336 }
1337
1338 if (ret) {
1339 printf("File not found.\n");
1340 *actread = 0;
1341 sqfs_closedir(dirsp);
1342 ret = -ENOENT;
1343 goto free_paths;
1344 }
1345
1346 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1347 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1348 sblk->block_size);
1349
1350 base = (struct squashfs_base_inode *)ipos;
1351 switch (get_unaligned_le16(&base->inode_type)) {
1352 case SQFS_REG_TYPE:
1353 reg = (struct squashfs_reg_inode *)ipos;
1354 datablk_count = sqfs_get_regfile_info(reg, &finfo, &frag_entry,
1355 sblk->block_size);
1356 if (datablk_count < 0) {
1357 ret = -EINVAL;
1358 goto free_paths;
1359 }
1360
1361 memcpy(finfo.blk_sizes, ipos + sizeof(*reg),
1362 datablk_count * sizeof(u32));
1363 break;
1364 case SQFS_LREG_TYPE:
1365 lreg = (struct squashfs_lreg_inode *)ipos;
1366 datablk_count = sqfs_get_lregfile_info(lreg, &finfo,
1367 &frag_entry,
1368 sblk->block_size);
1369 if (datablk_count < 0) {
1370 ret = -EINVAL;
1371 goto free_paths;
1372 }
1373
1374 memcpy(finfo.blk_sizes, ipos + sizeof(*lreg),
1375 datablk_count * sizeof(u32));
1376 break;
1377 case SQFS_SYMLINK_TYPE:
1378 case SQFS_LSYMLINK_TYPE:
1379 symlink = (struct squashfs_symlink_inode *)ipos;
1380 resolved = sqfs_resolve_symlink(symlink, filename);
1381 ret = sqfs_read(resolved, buf, offset, len, actread);
1382 free(resolved);
1383 goto free_paths;
1384 case SQFS_BLKDEV_TYPE:
1385 case SQFS_CHRDEV_TYPE:
1386 case SQFS_LBLKDEV_TYPE:
1387 case SQFS_LCHRDEV_TYPE:
1388 case SQFS_FIFO_TYPE:
1389 case SQFS_SOCKET_TYPE:
1390 case SQFS_LFIFO_TYPE:
1391 case SQFS_LSOCKET_TYPE:
1392 default:
1393 printf("Unsupported entry type\n");
1394 ret = -EINVAL;
1395 goto free_paths;
1396 }
1397
1398 /* If the user specifies a length, check its sanity */
1399 if (len) {
1400 if (len > finfo.size) {
1401 ret = -EINVAL;
1402 goto free_paths;
1403 }
1404
1405 finfo.size = len;
1406 }
1407
1408 if (datablk_count) {
1409 data_offset = finfo.start;
1410 datablock = malloc(get_unaligned_le32(&sblk->block_size));
1411 if (!datablock) {
1412 ret = -ENOMEM;
1413 goto free_paths;
1414 }
1415 }
1416
1417 for (j = 0; j < datablk_count; j++) {
1418 start = data_offset / ctxt.cur_dev->blksz;
1419 table_size = SQFS_BLOCK_SIZE(finfo.blk_sizes[j]);
1420 table_offset = data_offset - (start * ctxt.cur_dev->blksz);
1421 n_blks = DIV_ROUND_UP(table_size + table_offset,
1422 ctxt.cur_dev->blksz);
1423
1424 data_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
1425
1426 if (!data_buffer) {
1427 ret = -ENOMEM;
1428 goto free_datablk;
1429 }
1430
1431 ret = sqfs_disk_read(start, n_blks, data_buffer);
1432 if (ret < 0) {
1433 /*
1434 * Possible causes: too many data blocks or too large
1435 * SquashFS block size. Tip: re-compile the SquashFS
1436 * image with mksquashfs's -b <block_size> option.
1437 */
1438 printf("Error: too many data blocks to be read.\n");
1439 goto free_buffer;
1440 }
1441
1442 data = data_buffer + table_offset;
1443
1444 /* Load the data */
1445 if (SQFS_COMPRESSED_BLOCK(finfo.blk_sizes[j])) {
1446 dest_len = get_unaligned_le32(&sblk->block_size);
Joao Marcos Costab87fd012020-08-18 17:17:22 +02001447 ret = sqfs_decompress(&ctxt, datablock, &dest_len,
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001448 data, table_size);
1449 if (ret)
1450 goto free_buffer;
1451
1452 memcpy(buf + offset + *actread, datablock, dest_len);
1453 *actread += dest_len;
1454 } else {
1455 memcpy(buf + offset + *actread, data, table_size);
1456 *actread += table_size;
1457 }
1458
1459 data_offset += table_size;
1460 }
1461
1462 free(finfo.blk_sizes);
1463
1464 /*
1465 * There is no need to continue if the file is not fragmented.
1466 */
1467 if (!finfo.frag) {
1468 ret = 0;
1469 goto free_buffer;
1470 }
1471
1472 start = frag_entry.start / ctxt.cur_dev->blksz;
1473 table_size = SQFS_BLOCK_SIZE(frag_entry.size);
1474 table_offset = frag_entry.start - (start * ctxt.cur_dev->blksz);
1475 n_blks = DIV_ROUND_UP(table_size + table_offset, ctxt.cur_dev->blksz);
1476
1477 fragment = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
1478
1479 if (!fragment) {
1480 ret = -ENOMEM;
1481 goto free_buffer;
1482 }
1483
1484 ret = sqfs_disk_read(start, n_blks, fragment);
1485 if (ret < 0)
1486 goto free_fragment;
1487
1488 /* File compressed and fragmented */
1489 if (finfo.frag && finfo.comp) {
1490 dest_len = get_unaligned_le32(&sblk->block_size);
1491 fragment_block = malloc(dest_len);
1492 if (!fragment_block) {
1493 ret = -ENOMEM;
1494 goto free_fragment;
1495 }
1496
Joao Marcos Costab87fd012020-08-18 17:17:22 +02001497 ret = sqfs_decompress(&ctxt, fragment_block, &dest_len,
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001498 (void *)fragment + table_offset,
1499 frag_entry.size);
1500 if (ret) {
1501 free(fragment_block);
1502 goto free_fragment;
1503 }
1504
1505 for (j = offset + *actread; j < finfo.size; j++) {
1506 memcpy(buf + j, &fragment_block[finfo.offset + j], 1);
1507 (*actread)++;
1508 }
1509
1510 free(fragment_block);
1511
1512 } else if (finfo.frag && !finfo.comp) {
1513 fragment_block = (void *)fragment + table_offset;
1514
1515 for (j = offset + *actread; j < finfo.size; j++) {
1516 memcpy(buf + j, &fragment_block[finfo.offset + j], 1);
1517 (*actread)++;
1518 }
1519 }
1520
1521free_fragment:
1522 free(fragment);
1523free_buffer:
1524 if (datablk_count)
1525 free(data_buffer);
1526free_datablk:
1527 if (datablk_count)
1528 free(datablock);
1529free_paths:
1530 free(file);
1531 free(dir);
1532
1533 return ret;
1534}
1535
1536int sqfs_size(const char *filename, loff_t *size)
1537{
1538 struct squashfs_super_block *sblk = ctxt.sblk;
1539 struct squashfs_symlink_inode *symlink;
1540 struct fs_dir_stream *dirsp = NULL;
1541 struct squashfs_base_inode *base;
1542 struct squashfs_dir_stream *dirs;
1543 struct squashfs_lreg_inode *lreg;
1544 struct squashfs_reg_inode *reg;
1545 char *dir, *file, *resolved;
1546 struct fs_dirent *dent;
1547 unsigned char *ipos;
1548 int ret, i_number;
1549
1550 sqfs_split_path(&file, &dir, filename);
1551 /*
1552 * sqfs_opendir will uncompress inode and directory tables, and will
1553 * return a pointer to the directory that contains the requested file.
1554 */
1555 ret = sqfs_opendir(dir, &dirsp);
1556 if (ret) {
1557 sqfs_closedir(dirsp);
1558 ret = -EINVAL;
1559 goto free_strings;
1560 }
1561
1562 dirs = (struct squashfs_dir_stream *)dirsp;
1563
1564 while (!sqfs_readdir(dirsp, &dent)) {
1565 ret = strcmp(dent->name, file);
1566 if (!ret)
1567 break;
1568 free(dirs->entry);
1569 }
1570
1571 if (ret) {
1572 printf("File not found.\n");
1573 *size = 0;
1574 ret = -EINVAL;
1575 goto free_strings;
1576 }
1577
1578 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1579 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1580 sblk->block_size);
1581 free(dirs->entry);
1582
1583 base = (struct squashfs_base_inode *)ipos;
1584 switch (get_unaligned_le16(&base->inode_type)) {
1585 case SQFS_REG_TYPE:
1586 reg = (struct squashfs_reg_inode *)ipos;
1587 *size = get_unaligned_le32(&reg->file_size);
1588 break;
1589 case SQFS_LREG_TYPE:
1590 lreg = (struct squashfs_lreg_inode *)ipos;
1591 *size = get_unaligned_le64(&lreg->file_size);
1592 break;
1593 case SQFS_SYMLINK_TYPE:
1594 case SQFS_LSYMLINK_TYPE:
1595 symlink = (struct squashfs_symlink_inode *)ipos;
1596 resolved = sqfs_resolve_symlink(symlink, filename);
1597 ret = sqfs_size(resolved, size);
1598 free(resolved);
1599 break;
1600 case SQFS_BLKDEV_TYPE:
1601 case SQFS_CHRDEV_TYPE:
1602 case SQFS_LBLKDEV_TYPE:
1603 case SQFS_LCHRDEV_TYPE:
1604 case SQFS_FIFO_TYPE:
1605 case SQFS_SOCKET_TYPE:
1606 case SQFS_LFIFO_TYPE:
1607 case SQFS_LSOCKET_TYPE:
1608 default:
1609 printf("Unable to recover entry's size.\n");
1610 *size = 0;
1611 ret = -EINVAL;
1612 break;
1613 }
1614
1615free_strings:
1616 free(dir);
1617 free(file);
1618
1619 sqfs_closedir(dirsp);
1620
1621 return ret;
1622}
1623
1624void sqfs_close(void)
1625{
1626 free(ctxt.sblk);
1627 ctxt.cur_dev = NULL;
Joao Marcos Costa877576c2020-08-18 17:17:21 +02001628 sqfs_decompressor_cleanup(&ctxt);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001629}
1630
1631void sqfs_closedir(struct fs_dir_stream *dirs)
1632{
1633 struct squashfs_dir_stream *sqfs_dirs;
1634
1635 sqfs_dirs = (struct squashfs_dir_stream *)dirs;
1636 free(sqfs_dirs->inode_table);
1637 free(sqfs_dirs->dir_table);
1638 free(sqfs_dirs->dir_header);
Richard Genoud2575eb22020-11-03 12:11:02 +01001639 free(sqfs_dirs);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001640}