blob: af7ff80a7bdfe9dcc75462eab65a5a70dc717b04 [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>
Sean Nyekjaera8eb9622022-05-12 20:37:14 +020011#include <div64.h>
Joao Marcos Costa29da3742020-07-30 15:33:47 +020012#include <errno.h>
13#include <fs.h>
14#include <linux/types.h>
Pali Rohár501073d2022-04-06 23:31:53 +020015#include <asm/byteorder.h>
Miquel Raynaledaf6042022-06-27 12:20:03 +020016#include <linux/compat.h>
Joao Marcos Costa29da3742020-07-30 15:33:47 +020017#include <memalign.h>
18#include <stdlib.h>
19#include <string.h>
20#include <squashfs.h>
21#include <part.h>
22
23#include "sqfs_decompressor.h"
24#include "sqfs_filesystem.h"
25#include "sqfs_utils.h"
26
Richard Weinberger8d42c7b2024-08-02 18:36:47 +020027#define MAX_SYMLINK_NEST 8
28
Joao Marcos Costa29da3742020-07-30 15:33:47 +020029static struct squashfs_ctxt ctxt;
Richard Weinberger8d42c7b2024-08-02 18:36:47 +020030static int symlinknest;
31
32static int sqfs_readdir_nest(struct fs_dir_stream *fs_dirs, struct fs_dirent **dentp);
Joao Marcos Costa29da3742020-07-30 15:33:47 +020033
34static int sqfs_disk_read(__u32 block, __u32 nr_blocks, void *buf)
35{
36 ulong ret;
37
38 if (!ctxt.cur_dev)
39 return -1;
40
41 ret = blk_dread(ctxt.cur_dev, ctxt.cur_part_info.start + block,
42 nr_blocks, buf);
43
44 if (ret != nr_blocks)
45 return -1;
46
47 return ret;
48}
49
50static int sqfs_read_sblk(struct squashfs_super_block **sblk)
51{
52 *sblk = malloc_cache_aligned(ctxt.cur_dev->blksz);
53 if (!*sblk)
54 return -ENOMEM;
55
56 if (sqfs_disk_read(0, 1, *sblk) != 1) {
57 free(*sblk);
Heinrich Schuchardt8cace452022-05-10 21:53:25 +020058 *sblk = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +020059 return -EINVAL;
60 }
61
62 return 0;
63}
64
65static int sqfs_count_tokens(const char *filename)
66{
67 int token_count = 1, l;
68
69 for (l = 1; l < strlen(filename); l++) {
70 if (filename[l] == '/')
71 token_count++;
72 }
73
74 /* Ignore trailing '/' in path */
75 if (filename[strlen(filename) - 1] == '/')
76 token_count--;
77
78 if (!token_count)
79 token_count = 1;
80
81 return token_count;
82}
83
84/*
85 * Calculates how many blocks are needed for the buffer used in sqfs_disk_read.
86 * The memory section (e.g. inode table) start offset and its end (i.e. the next
87 * table start) must be specified. It also calculates the offset from which to
88 * start reading the buffer.
89 */
90static int sqfs_calc_n_blks(__le64 start, __le64 end, u64 *offset)
91{
92 u64 start_, table_size;
93
94 table_size = le64_to_cpu(end) - le64_to_cpu(start);
Kasper Revsbech42d59662022-12-01 16:30:32 +010095 start_ = lldiv(le64_to_cpu(start), ctxt.cur_dev->blksz);
Joao Marcos Costa29da3742020-07-30 15:33:47 +020096 *offset = le64_to_cpu(start) - (start_ * ctxt.cur_dev->blksz);
97
98 return DIV_ROUND_UP(table_size + *offset, ctxt.cur_dev->blksz);
99}
100
101/*
102 * Retrieves fragment block entry and returns true if the fragment block is
103 * compressed
104 */
105static int sqfs_frag_lookup(u32 inode_fragment_index,
106 struct squashfs_fragment_block_entry *e)
107{
David Oberhollenzerddf1dfb2022-12-25 11:05:24 +0100108 u64 start, end, exp_tbl, n_blks, src_len, table_offset, start_block;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200109 unsigned char *metadata_buffer, *metadata, *table;
110 struct squashfs_fragment_block_entry *entries;
111 struct squashfs_super_block *sblk = ctxt.sblk;
112 unsigned long dest_len;
113 int block, offset, ret;
Joao Marcos Costab87fd012020-08-18 17:17:22 +0200114 u16 header;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200115
Richard Genoud07726e12020-11-03 12:11:15 +0100116 metadata_buffer = NULL;
117 entries = NULL;
118 table = NULL;
119
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200120 if (inode_fragment_index >= get_unaligned_le32(&sblk->fragments))
121 return -EINVAL;
122
David Oberhollenzerddf1dfb2022-12-25 11:05:24 +0100123 start = get_unaligned_le64(&sblk->fragment_table_start);
124 end = get_unaligned_le64(&sblk->id_table_start);
125 exp_tbl = get_unaligned_le64(&sblk->export_table_start);
126
127 if (exp_tbl > start && exp_tbl < end)
128 end = exp_tbl;
129
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200130 n_blks = sqfs_calc_n_blks(sblk->fragment_table_start,
David Oberhollenzerddf1dfb2022-12-25 11:05:24 +0100131 cpu_to_le64(end), &table_offset);
132
133 start /= ctxt.cur_dev->blksz;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200134
135 /* Allocate a proper sized buffer to store the fragment index table */
136 table = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
Richard Genoud07726e12020-11-03 12:11:15 +0100137 if (!table) {
138 ret = -ENOMEM;
139 goto out;
140 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200141
142 if (sqfs_disk_read(start, n_blks, table) < 0) {
Richard Genoud07726e12020-11-03 12:11:15 +0100143 ret = -EINVAL;
144 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200145 }
146
147 block = SQFS_FRAGMENT_INDEX(inode_fragment_index);
148 offset = SQFS_FRAGMENT_INDEX_OFFSET(inode_fragment_index);
149
150 /*
151 * Get the start offset of the metadata block that contains the right
152 * fragment block entry
153 */
154 start_block = get_unaligned_le64(table + table_offset + block *
155 sizeof(u64));
156
157 start = start_block / ctxt.cur_dev->blksz;
158 n_blks = sqfs_calc_n_blks(cpu_to_le64(start_block),
159 sblk->fragment_table_start, &table_offset);
160
161 metadata_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
162 if (!metadata_buffer) {
163 ret = -ENOMEM;
Richard Genoud07726e12020-11-03 12:11:15 +0100164 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200165 }
166
167 if (sqfs_disk_read(start, n_blks, metadata_buffer) < 0) {
168 ret = -EINVAL;
Richard Genoud07726e12020-11-03 12:11:15 +0100169 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200170 }
171
172 /* Every metadata block starts with a 16-bit header */
173 header = get_unaligned_le16(metadata_buffer + table_offset);
174 metadata = metadata_buffer + table_offset + SQFS_HEADER_SIZE;
175
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +0200176 if (!metadata || !header) {
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200177 ret = -ENOMEM;
Richard Genoud07726e12020-11-03 12:11:15 +0100178 goto out;
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200179 }
180
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200181 entries = malloc(SQFS_METADATA_BLOCK_SIZE);
182 if (!entries) {
183 ret = -ENOMEM;
Richard Genoud07726e12020-11-03 12:11:15 +0100184 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200185 }
186
187 if (SQFS_COMPRESSED_METADATA(header)) {
188 src_len = SQFS_METADATA_SIZE(header);
189 dest_len = SQFS_METADATA_BLOCK_SIZE;
Joao Marcos Costab87fd012020-08-18 17:17:22 +0200190 ret = sqfs_decompress(&ctxt, entries, &dest_len, metadata,
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200191 src_len);
192 if (ret) {
193 ret = -EINVAL;
Richard Genoud07726e12020-11-03 12:11:15 +0100194 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200195 }
196 } else {
197 memcpy(entries, metadata, SQFS_METADATA_SIZE(header));
198 }
199
200 *e = entries[offset];
201 ret = SQFS_COMPRESSED_BLOCK(e->size);
202
Richard Genoud07726e12020-11-03 12:11:15 +0100203out:
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200204 free(entries);
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200205 free(metadata_buffer);
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200206 free(table);
207
208 return ret;
209}
210
211/*
212 * The entry name is a flexible array member, and we don't know its size before
213 * actually reading the entry. So we need a first copy to retrieve this size so
214 * we can finally copy the whole struct.
215 */
216static int sqfs_read_entry(struct squashfs_directory_entry **dest, void *src)
217{
218 struct squashfs_directory_entry *tmp;
219 u16 sz;
220
221 tmp = src;
222 sz = get_unaligned_le16(src + sizeof(*tmp) - sizeof(u16));
223 /*
224 * 'src' points to the begin of a directory entry, and 'sz' gets its
225 * 'name_size' member's value. name_size is actually the string
226 * length - 1, so adding 2 compensates this difference and adds space
227 * for the trailling null byte.
228 */
229 *dest = malloc(sizeof(*tmp) + sz + 2);
230 if (!*dest)
231 return -ENOMEM;
232
233 memcpy(*dest, src, sizeof(*tmp) + sz + 1);
234 (*dest)->name[sz + 1] = '\0';
235
236 return 0;
237}
238
239static int sqfs_get_tokens_length(char **tokens, int count)
240{
241 int length = 0, i;
242
243 /*
244 * 1 is added to the result of strlen to consider the slash separator
245 * between the tokens.
246 */
247 for (i = 0; i < count; i++)
248 length += strlen(tokens[i]) + 1;
249
250 return length;
251}
252
253/* Takes a token list and returns a single string with '/' as separator. */
254static char *sqfs_concat_tokens(char **token_list, int token_count)
255{
256 char *result;
257 int i, length = 0, offset = 0;
258
259 length = sqfs_get_tokens_length(token_list, token_count);
260
261 result = malloc(length + 1);
Richard Genoud489e7ae2020-11-03 12:11:08 +0100262 if (!result)
263 return NULL;
264
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200265 result[length] = '\0';
266
267 for (i = 0; i < token_count; i++) {
268 strcpy(result + offset, token_list[i]);
269 offset += strlen(token_list[i]);
270 result[offset++] = '/';
271 }
272
273 return result;
274}
275
276/*
277 * Differently from sqfs_concat_tokens, sqfs_join writes the result into a
278 * previously allocated string, and returns the number of bytes written.
279 */
280static int sqfs_join(char **strings, char *dest, int start, int end,
281 char separator)
282{
283 int i, offset = 0;
284
285 for (i = start; i < end; i++) {
286 strcpy(dest + offset, strings[i]);
287 offset += strlen(strings[i]);
288 if (i < end - 1)
289 dest[offset++] = separator;
290 }
291
292 return offset;
293}
294
295/*
296 * Fills the given token list using its size (count) and a source string (str)
297 */
298static int sqfs_tokenize(char **tokens, int count, const char *str)
299{
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200300 int i, j, ret = 0;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200301 char *aux, *strc;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200302
303 strc = strdup(str);
304 if (!strc)
305 return -ENOMEM;
306
307 if (!strcmp(strc, "/")) {
308 tokens[0] = strdup(strc);
309 if (!tokens[0]) {
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200310 ret = -ENOMEM;
311 goto free_strc;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200312 }
313 } else {
314 for (j = 0; j < count; j++) {
315 aux = strtok(!j ? strc : NULL, "/");
316 tokens[j] = strdup(aux);
317 if (!tokens[j]) {
318 for (i = 0; i < j; i++)
319 free(tokens[i]);
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200320 ret = -ENOMEM;
321 goto free_strc;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200322 }
323 }
324 }
325
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200326free_strc:
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200327 free(strc);
328
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200329 return ret;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200330}
331
332/*
333 * Remove last 'updir + 1' tokens from the base path tokens list. This leaves us
334 * with a token list containing only the tokens needed to form the resolved
335 * path, and returns the decremented size of the token list.
336 */
337static int sqfs_clean_base_path(char **base, int count, int updir)
338{
339 int i;
340
341 for (i = count - updir - 1; i < count; i++)
342 free(base[i]);
343
344 return count - updir - 1;
345}
346
347/*
348 * Given the base ("current dir.") path and the relative one, generate the
349 * absolute path.
350 */
351static char *sqfs_get_abs_path(const char *base, const char *rel)
352{
353 char **base_tokens, **rel_tokens, *resolved = NULL;
354 int ret, bc, rc, i, updir = 0, resolved_size = 0, offset = 0;
355
Richard Genoudcf9072a2020-11-03 12:11:17 +0100356 base_tokens = NULL;
357 rel_tokens = NULL;
358
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200359 /* Memory allocation for the token lists */
360 bc = sqfs_count_tokens(base);
361 rc = sqfs_count_tokens(rel);
362 if (bc < 1 || rc < 1)
363 return NULL;
364
Richard Genoudcf9072a2020-11-03 12:11:17 +0100365 base_tokens = calloc(bc, sizeof(char *));
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200366 if (!base_tokens)
367 return NULL;
368
Richard Genoudcf9072a2020-11-03 12:11:17 +0100369 rel_tokens = calloc(rc, sizeof(char *));
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200370 if (!rel_tokens)
Richard Genoudcf9072a2020-11-03 12:11:17 +0100371 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200372
373 /* Fill token lists */
374 ret = sqfs_tokenize(base_tokens, bc, base);
375 if (ret)
Richard Genoudcf9072a2020-11-03 12:11:17 +0100376 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200377
Richard Genoud9cb30362020-11-03 12:11:16 +0100378 ret = sqfs_tokenize(rel_tokens, rc, rel);
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200379 if (ret)
Richard Genoudcf9072a2020-11-03 12:11:17 +0100380 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200381
382 /* count '..' occurrences in target path */
383 for (i = 0; i < rc; i++) {
384 if (!strcmp(rel_tokens[i], ".."))
385 updir++;
386 }
387
388 /* Remove the last token and the '..' occurrences */
389 bc = sqfs_clean_base_path(base_tokens, bc, updir);
390 if (bc < 0)
Richard Genoudcf9072a2020-11-03 12:11:17 +0100391 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200392
393 /* Calculate resolved path size */
394 if (!bc)
395 resolved_size++;
396
397 resolved_size += sqfs_get_tokens_length(base_tokens, bc) +
398 sqfs_get_tokens_length(rel_tokens, rc);
399
400 resolved = malloc(resolved_size + 1);
401 if (!resolved)
Richard Genoudcf9072a2020-11-03 12:11:17 +0100402 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200403
404 /* Set resolved path */
405 memset(resolved, '\0', resolved_size + 1);
406 offset += sqfs_join(base_tokens, resolved + offset, 0, bc, '/');
407 resolved[offset++] = '/';
408 offset += sqfs_join(rel_tokens, resolved + offset, updir, rc, '/');
409
Richard Genoudcf9072a2020-11-03 12:11:17 +0100410out:
411 if (rel_tokens)
412 for (i = 0; i < rc; i++)
413 free(rel_tokens[i]);
414 if (base_tokens)
415 for (i = 0; i < bc; i++)
416 free(base_tokens[i]);
417
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200418 free(rel_tokens);
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200419 free(base_tokens);
420
421 return resolved;
422}
423
424static char *sqfs_resolve_symlink(struct squashfs_symlink_inode *sym,
425 const char *base_path)
426{
427 char *resolved, *target;
428 u32 sz;
429
Richard Weinberger18ef5f32024-08-02 18:36:44 +0200430 if (__builtin_add_overflow(get_unaligned_le32(&sym->symlink_size), 1, &sz))
431 return NULL;
432
433 target = malloc(sz);
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200434 if (!target)
435 return NULL;
436
437 /*
438 * There is no trailling null byte in the symlink's target path, so a
439 * copy is made and a '\0' is added at its end.
440 */
Richard Weinberger18ef5f32024-08-02 18:36:44 +0200441 target[sz - 1] = '\0';
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200442 /* Get target name (relative path) */
Richard Weinberger18ef5f32024-08-02 18:36:44 +0200443 strncpy(target, sym->symlink, sz - 1);
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200444
445 /* Relative -> absolute path conversion */
446 resolved = sqfs_get_abs_path(base_path, target);
447
448 free(target);
449
450 return resolved;
451}
452
453/*
454 * m_list contains each metadata block's position, and m_count is the number of
455 * elements of m_list. Those metadata blocks come from the compressed directory
456 * table.
457 */
458static int sqfs_search_dir(struct squashfs_dir_stream *dirs, char **token_list,
459 int token_count, u32 *m_list, int m_count)
460{
461 struct squashfs_super_block *sblk = ctxt.sblk;
462 char *path, *target, **sym_tokens, *res, *rem;
Richard Genoud2762f652020-11-03 12:11:06 +0100463 int j, ret = 0, new_inode_number, offset;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200464 struct squashfs_symlink_inode *sym;
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +0200465 struct squashfs_ldir_inode *ldir;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200466 struct squashfs_dir_inode *dir;
467 struct fs_dir_stream *dirsp;
468 struct fs_dirent *dent;
469 unsigned char *table;
470
Richard Genoud2762f652020-11-03 12:11:06 +0100471 res = NULL;
472 rem = NULL;
473 path = NULL;
474 target = NULL;
475 sym_tokens = NULL;
476
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200477 dirsp = (struct fs_dir_stream *)dirs;
478
479 /* Start by root inode */
480 table = sqfs_find_inode(dirs->inode_table, le32_to_cpu(sblk->inodes),
481 sblk->inodes, sblk->block_size);
Richard Weinbergere8db3d92024-08-02 18:36:46 +0200482 if (!table)
483 return -EINVAL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200484
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200485 dir = (struct squashfs_dir_inode *)table;
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +0200486 ldir = (struct squashfs_ldir_inode *)table;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200487
488 /* get directory offset in directory table */
489 offset = sqfs_dir_offset(table, m_list, m_count);
490 dirs->table = &dirs->dir_table[offset];
491
492 /* Setup directory header */
493 dirs->dir_header = malloc(SQFS_DIR_HEADER_SIZE);
494 if (!dirs->dir_header)
495 return -ENOMEM;
496
497 memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE);
498
499 /* Initialize squashfs_dir_stream members */
500 dirs->table += SQFS_DIR_HEADER_SIZE;
501 dirs->size = get_unaligned_le16(&dir->file_size) - SQFS_DIR_HEADER_SIZE;
502 dirs->entry_count = dirs->dir_header->count + 1;
503
504 /* No path given -> root directory */
505 if (!strcmp(token_list[0], "/")) {
506 dirs->table = &dirs->dir_table[offset];
507 memcpy(&dirs->i_dir, dir, sizeof(*dir));
508 return 0;
509 }
510
511 for (j = 0; j < token_count; j++) {
512 if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) {
513 printf("** Cannot find directory. **\n");
Richard Genoud2762f652020-11-03 12:11:06 +0100514 ret = -EINVAL;
515 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200516 }
517
Richard Weinberger8d42c7b2024-08-02 18:36:47 +0200518 while (!sqfs_readdir_nest(dirsp, &dent)) {
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200519 ret = strcmp(dent->name, token_list[j]);
520 if (!ret)
521 break;
522 free(dirs->entry);
Richard Genoude405fc42020-11-03 12:11:05 +0100523 dirs->entry = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200524 }
525
526 if (ret) {
527 printf("** Cannot find directory. **\n");
Richard Genoud2762f652020-11-03 12:11:06 +0100528 ret = -EINVAL;
529 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200530 }
531
532 /* Redefine inode as the found token */
533 new_inode_number = dirs->entry->inode_offset +
534 dirs->dir_header->inode_number;
535
536 /* Get reference to inode in the inode table */
537 table = sqfs_find_inode(dirs->inode_table, new_inode_number,
538 sblk->inodes, sblk->block_size);
Richard Weinbergere8db3d92024-08-02 18:36:46 +0200539 if (!table)
540 return -EINVAL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200541 dir = (struct squashfs_dir_inode *)table;
542
543 /* Check for symbolic link and inode type sanity */
544 if (get_unaligned_le16(&dir->inode_type) == SQFS_SYMLINK_TYPE) {
Richard Weinberger8d42c7b2024-08-02 18:36:47 +0200545 if (++symlinknest == MAX_SYMLINK_NEST) {
546 ret = -ELOOP;
547 goto out;
548 }
549
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200550 sym = (struct squashfs_symlink_inode *)table;
551 /* Get first j + 1 tokens */
552 path = sqfs_concat_tokens(token_list, j + 1);
Richard Genoud2762f652020-11-03 12:11:06 +0100553 if (!path) {
554 ret = -ENOMEM;
555 goto out;
556 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200557 /* Resolve for these tokens */
558 target = sqfs_resolve_symlink(sym, path);
Richard Genoud2762f652020-11-03 12:11:06 +0100559 if (!target) {
560 ret = -ENOMEM;
561 goto out;
562 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200563 /* Join remaining tokens */
564 rem = sqfs_concat_tokens(token_list + j + 1, token_count -
565 j - 1);
Richard Genoud2762f652020-11-03 12:11:06 +0100566 if (!rem) {
567 ret = -ENOMEM;
568 goto out;
569 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200570 /* Concatenate remaining tokens and symlink's target */
571 res = malloc(strlen(rem) + strlen(target) + 1);
Richard Genoud2762f652020-11-03 12:11:06 +0100572 if (!res) {
573 ret = -ENOMEM;
574 goto out;
575 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200576 strcpy(res, target);
577 res[strlen(target)] = '/';
578 strcpy(res + strlen(target) + 1, rem);
579 token_count = sqfs_count_tokens(res);
580
Richard Genoud2762f652020-11-03 12:11:06 +0100581 if (token_count < 0) {
582 ret = -EINVAL;
583 goto out;
584 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200585
586 sym_tokens = malloc(token_count * sizeof(char *));
Richard Genoud2762f652020-11-03 12:11:06 +0100587 if (!sym_tokens) {
588 ret = -EINVAL;
589 goto out;
590 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200591
592 /* Fill tokens list */
593 ret = sqfs_tokenize(sym_tokens, token_count, res);
Richard Genoud2762f652020-11-03 12:11:06 +0100594 if (ret) {
595 ret = -EINVAL;
596 goto out;
597 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200598 free(dirs->entry);
Richard Genoude405fc42020-11-03 12:11:05 +0100599 dirs->entry = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200600
601 ret = sqfs_search_dir(dirs, sym_tokens, token_count,
602 m_list, m_count);
Richard Genoud2762f652020-11-03 12:11:06 +0100603 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200604 } else if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) {
605 printf("** Cannot find directory. **\n");
606 free(dirs->entry);
Richard Genoude405fc42020-11-03 12:11:05 +0100607 dirs->entry = NULL;
Richard Genoud2762f652020-11-03 12:11:06 +0100608 ret = -EINVAL;
609 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200610 }
611
612 /* Check if it is an extended dir. */
613 if (get_unaligned_le16(&dir->inode_type) == SQFS_LDIR_TYPE)
614 ldir = (struct squashfs_ldir_inode *)table;
615
616 /* Get dir. offset into the directory table */
617 offset = sqfs_dir_offset(table, m_list, m_count);
618 dirs->table = &dirs->dir_table[offset];
619
620 /* Copy directory header */
621 memcpy(dirs->dir_header, &dirs->dir_table[offset],
622 SQFS_DIR_HEADER_SIZE);
623
624 /* Check for empty directory */
625 if (sqfs_is_empty_dir(table)) {
626 printf("Empty directory.\n");
627 free(dirs->entry);
Richard Genoude405fc42020-11-03 12:11:05 +0100628 dirs->entry = NULL;
Richard Genoud2762f652020-11-03 12:11:06 +0100629 ret = SQFS_EMPTY_DIR;
630 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200631 }
632
633 dirs->table += SQFS_DIR_HEADER_SIZE;
634 dirs->size = get_unaligned_le16(&dir->file_size);
635 dirs->entry_count = dirs->dir_header->count + 1;
636 dirs->size -= SQFS_DIR_HEADER_SIZE;
637 free(dirs->entry);
Richard Genoude405fc42020-11-03 12:11:05 +0100638 dirs->entry = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200639 }
640
641 offset = sqfs_dir_offset(table, m_list, m_count);
642 dirs->table = &dirs->dir_table[offset];
643
644 if (get_unaligned_le16(&dir->inode_type) == SQFS_DIR_TYPE)
645 memcpy(&dirs->i_dir, dir, sizeof(*dir));
646 else
647 memcpy(&dirs->i_ldir, ldir, sizeof(*ldir));
648
Richard Genoud2762f652020-11-03 12:11:06 +0100649out:
650 free(res);
651 free(rem);
652 free(path);
653 free(target);
654 free(sym_tokens);
655 return ret;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200656}
657
658/*
659 * Inode and directory tables are stored as a series of metadata blocks, and
660 * given the compressed size of this table, we can calculate how much metadata
661 * blocks are needed to store the result of the decompression, since a
662 * decompressed metadata block should have a size of 8KiB.
663 */
664static int sqfs_count_metablks(void *table, u32 offset, int table_size)
665{
666 int count = 0, cur_size = 0, ret;
667 u32 data_size;
668 bool comp;
669
670 do {
671 ret = sqfs_read_metablock(table, offset + cur_size, &comp,
672 &data_size);
673 if (ret)
674 return -EINVAL;
675 cur_size += data_size + SQFS_HEADER_SIZE;
676 count++;
677 } while (cur_size < table_size);
678
679 return count;
680}
681
682/*
683 * Storing the metadata blocks header's positions will be useful while looking
684 * for an entry in the directory table, using the reference (index and offset)
685 * given by its inode.
686 */
687static int sqfs_get_metablk_pos(u32 *pos_list, void *table, u32 offset,
688 int metablks_count)
689{
690 u32 data_size, cur_size = 0;
691 int j, ret = 0;
692 bool comp;
693
694 if (!metablks_count)
695 return -EINVAL;
696
697 for (j = 0; j < metablks_count; j++) {
698 ret = sqfs_read_metablock(table, offset + cur_size, &comp,
699 &data_size);
700 if (ret)
701 return -EINVAL;
702
703 cur_size += data_size + SQFS_HEADER_SIZE;
704 pos_list[j] = cur_size;
705 }
706
707 return ret;
708}
709
710static int sqfs_read_inode_table(unsigned char **inode_table)
711{
712 struct squashfs_super_block *sblk = ctxt.sblk;
713 u64 start, n_blks, table_offset, table_size;
Joao Marcos Costab87fd012020-08-18 17:17:22 +0200714 int j, ret = 0, metablks_count;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200715 unsigned char *src_table, *itb;
716 u32 src_len, dest_offset = 0;
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200717 unsigned long dest_len = 0;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200718 bool compressed;
719
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200720 table_size = get_unaligned_le64(&sblk->directory_table_start) -
721 get_unaligned_le64(&sblk->inode_table_start);
722 start = get_unaligned_le64(&sblk->inode_table_start) /
723 ctxt.cur_dev->blksz;
724 n_blks = sqfs_calc_n_blks(sblk->inode_table_start,
725 sblk->directory_table_start, &table_offset);
726
727 /* Allocate a proper sized buffer (itb) to store the inode table */
728 itb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
729 if (!itb)
730 return -ENOMEM;
731
732 if (sqfs_disk_read(start, n_blks, itb) < 0) {
733 ret = -EINVAL;
734 goto free_itb;
735 }
736
737 /* Parse inode table (metadata block) header */
738 ret = sqfs_read_metablock(itb, table_offset, &compressed, &src_len);
739 if (ret) {
740 ret = -EINVAL;
741 goto free_itb;
742 }
743
744 /* Calculate size to store the whole decompressed table */
745 metablks_count = sqfs_count_metablks(itb, table_offset, table_size);
746 if (metablks_count < 1) {
747 ret = -EINVAL;
748 goto free_itb;
749 }
750
Miquel Raynaledaf6042022-06-27 12:20:03 +0200751 *inode_table = kcalloc(metablks_count, SQFS_METADATA_BLOCK_SIZE,
752 GFP_KERNEL);
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200753 if (!*inode_table) {
754 ret = -ENOMEM;
Lars Weber205783f2022-01-13 14:28:45 +0100755 printf("Error: failed to allocate squashfs inode_table of size %i, increasing CONFIG_SYS_MALLOC_LEN could help\n",
756 metablks_count * SQFS_METADATA_BLOCK_SIZE);
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200757 goto free_itb;
758 }
759
760 src_table = itb + table_offset + SQFS_HEADER_SIZE;
761
762 /* Extract compressed Inode table */
763 for (j = 0; j < metablks_count; j++) {
764 sqfs_read_metablock(itb, table_offset, &compressed, &src_len);
765 if (compressed) {
766 dest_len = SQFS_METADATA_BLOCK_SIZE;
Joao Marcos Costab87fd012020-08-18 17:17:22 +0200767 ret = sqfs_decompress(&ctxt, *inode_table +
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200768 dest_offset, &dest_len,
769 src_table, src_len);
770 if (ret) {
771 free(*inode_table);
Richard Genouda62528d2020-11-03 12:11:07 +0100772 *inode_table = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200773 goto free_itb;
774 }
775
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200776 dest_offset += dest_len;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200777 } else {
778 memcpy(*inode_table + (j * SQFS_METADATA_BLOCK_SIZE),
779 src_table, src_len);
780 }
781
782 /*
783 * Offsets to the decompression destination, to the metadata
784 * buffer 'itb' and to the decompression source, respectively.
785 */
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200786
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200787 table_offset += src_len + SQFS_HEADER_SIZE;
788 src_table += src_len + SQFS_HEADER_SIZE;
789 }
790
791free_itb:
792 free(itb);
793
794 return ret;
795}
796
797static int sqfs_read_directory_table(unsigned char **dir_table, u32 **pos_list)
798{
799 u64 start, n_blks, table_offset, table_size;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200800 struct squashfs_super_block *sblk = ctxt.sblk;
Joao Marcos Costab87fd012020-08-18 17:17:22 +0200801 int j, ret = 0, metablks_count = -1;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200802 unsigned char *src_table, *dtb;
803 u32 src_len, dest_offset = 0;
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200804 unsigned long dest_len = 0;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200805 bool compressed;
806
Richard Genoud2f9c9852020-11-03 12:11:04 +0100807 *dir_table = NULL;
808 *pos_list = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200809 /* DIRECTORY TABLE */
810 table_size = get_unaligned_le64(&sblk->fragment_table_start) -
811 get_unaligned_le64(&sblk->directory_table_start);
812 start = get_unaligned_le64(&sblk->directory_table_start) /
813 ctxt.cur_dev->blksz;
814 n_blks = sqfs_calc_n_blks(sblk->directory_table_start,
815 sblk->fragment_table_start, &table_offset);
816
817 /* Allocate a proper sized buffer (dtb) to store the directory table */
818 dtb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
819 if (!dtb)
820 return -ENOMEM;
821
822 if (sqfs_disk_read(start, n_blks, dtb) < 0)
Richard Genoud2f9c9852020-11-03 12:11:04 +0100823 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200824
825 /* Parse directory table (metadata block) header */
826 ret = sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
827 if (ret)
Richard Genoud2f9c9852020-11-03 12:11:04 +0100828 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200829
830 /* Calculate total size to store the whole decompressed table */
831 metablks_count = sqfs_count_metablks(dtb, table_offset, table_size);
832 if (metablks_count < 1)
Richard Genoud2f9c9852020-11-03 12:11:04 +0100833 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200834
835 *dir_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE);
836 if (!*dir_table)
Richard Genoud2f9c9852020-11-03 12:11:04 +0100837 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200838
839 *pos_list = malloc(metablks_count * sizeof(u32));
Richard Genoud2f9c9852020-11-03 12:11:04 +0100840 if (!*pos_list)
841 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200842
843 ret = sqfs_get_metablk_pos(*pos_list, dtb, table_offset,
844 metablks_count);
845 if (ret) {
846 metablks_count = -1;
Richard Genoud2f9c9852020-11-03 12:11:04 +0100847 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200848 }
849
850 src_table = dtb + table_offset + SQFS_HEADER_SIZE;
851
852 /* Extract compressed Directory table */
853 dest_offset = 0;
854 for (j = 0; j < metablks_count; j++) {
855 sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
856 if (compressed) {
857 dest_len = SQFS_METADATA_BLOCK_SIZE;
Joao Marcos Costab87fd012020-08-18 17:17:22 +0200858 ret = sqfs_decompress(&ctxt, *dir_table +
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200859 (j * SQFS_METADATA_BLOCK_SIZE),
860 &dest_len, src_table, src_len);
861 if (ret) {
862 metablks_count = -1;
Richard Genoud2f9c9852020-11-03 12:11:04 +0100863 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200864 }
865
866 if (dest_len < SQFS_METADATA_BLOCK_SIZE) {
867 dest_offset += dest_len;
868 break;
869 }
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200870
871 dest_offset += dest_len;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200872 } else {
873 memcpy(*dir_table + (j * SQFS_METADATA_BLOCK_SIZE),
874 src_table, src_len);
875 }
876
877 /*
878 * Offsets to the decompression destination, to the metadata
879 * buffer 'dtb' and to the decompression source, respectively.
880 */
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200881 table_offset += src_len + SQFS_HEADER_SIZE;
882 src_table += src_len + SQFS_HEADER_SIZE;
883 }
884
Richard Genoud2f9c9852020-11-03 12:11:04 +0100885out:
886 if (metablks_count < 1) {
887 free(*dir_table);
888 free(*pos_list);
889 *dir_table = NULL;
890 *pos_list = NULL;
891 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200892 free(dtb);
893
894 return metablks_count;
895}
896
Richard Weinberger8d42c7b2024-08-02 18:36:47 +0200897static int sqfs_opendir_nest(const char *filename, struct fs_dir_stream **dirsp)
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200898{
899 unsigned char *inode_table = NULL, *dir_table = NULL;
Richard Genoud32bea5b2020-11-03 12:11:01 +0100900 int j, token_count = 0, ret = 0, metablks_count;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200901 struct squashfs_dir_stream *dirs;
Richard Genoud32bea5b2020-11-03 12:11:01 +0100902 char **token_list = NULL, *path = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200903 u32 *pos_list = NULL;
904
Heinrich Schuchardte086af82021-05-17 08:21:39 +0200905 dirs = calloc(1, sizeof(*dirs));
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200906 if (!dirs)
907 return -EINVAL;
908
Richard Genoud557f08f2020-11-03 12:11:00 +0100909 /* these should be set to NULL to prevent dangling pointers */
910 dirs->dir_header = NULL;
911 dirs->entry = NULL;
912 dirs->table = NULL;
913 dirs->inode_table = NULL;
914 dirs->dir_table = NULL;
915
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200916 ret = sqfs_read_inode_table(&inode_table);
Richard Genoud557f08f2020-11-03 12:11:00 +0100917 if (ret) {
918 ret = -EINVAL;
Richard Genoud32bea5b2020-11-03 12:11:01 +0100919 goto out;
Richard Genoud557f08f2020-11-03 12:11:00 +0100920 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200921
922 metablks_count = sqfs_read_directory_table(&dir_table, &pos_list);
Richard Genoud557f08f2020-11-03 12:11:00 +0100923 if (metablks_count < 1) {
924 ret = -EINVAL;
Richard Genoud32bea5b2020-11-03 12:11:01 +0100925 goto out;
Richard Genoud557f08f2020-11-03 12:11:00 +0100926 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200927
928 /* Tokenize filename */
929 token_count = sqfs_count_tokens(filename);
Richard Genoud557f08f2020-11-03 12:11:00 +0100930 if (token_count < 0) {
931 ret = -EINVAL;
Richard Genoud32bea5b2020-11-03 12:11:01 +0100932 goto out;
Richard Genoud557f08f2020-11-03 12:11:00 +0100933 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200934
935 path = strdup(filename);
Richard Genoud557f08f2020-11-03 12:11:00 +0100936 if (!path) {
937 ret = -EINVAL;
Richard Genoud32bea5b2020-11-03 12:11:01 +0100938 goto out;
Richard Genoud557f08f2020-11-03 12:11:00 +0100939 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200940
941 token_list = malloc(token_count * sizeof(char *));
942 if (!token_list) {
943 ret = -EINVAL;
Richard Genoud32bea5b2020-11-03 12:11:01 +0100944 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200945 }
946
947 /* Fill tokens list */
948 ret = sqfs_tokenize(token_list, token_count, path);
949 if (ret)
Richard Genoud32bea5b2020-11-03 12:11:01 +0100950 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200951 /*
952 * ldir's (extended directory) size is greater than dir, so it works as
953 * a general solution for the malloc size, since 'i' is a union.
954 */
955 dirs->inode_table = inode_table;
956 dirs->dir_table = dir_table;
957 ret = sqfs_search_dir(dirs, token_list, token_count, pos_list,
958 metablks_count);
959 if (ret)
Richard Genoud32bea5b2020-11-03 12:11:01 +0100960 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200961
962 if (le16_to_cpu(dirs->i_dir.inode_type) == SQFS_DIR_TYPE)
963 dirs->size = le16_to_cpu(dirs->i_dir.file_size);
964 else
965 dirs->size = le32_to_cpu(dirs->i_ldir.file_size);
966
967 /* Setup directory header */
968 memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE);
969 dirs->entry_count = dirs->dir_header->count + 1;
970 dirs->size -= SQFS_DIR_HEADER_SIZE;
971
972 /* Setup entry */
973 dirs->entry = NULL;
974 dirs->table += SQFS_DIR_HEADER_SIZE;
975
976 *dirsp = (struct fs_dir_stream *)dirs;
977
Richard Genoud32bea5b2020-11-03 12:11:01 +0100978out:
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200979 for (j = 0; j < token_count; j++)
980 free(token_list[j]);
981 free(token_list);
982 free(pos_list);
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200983 free(path);
Richard Genoud32bea5b2020-11-03 12:11:01 +0100984 if (ret) {
Richard Genoud557f08f2020-11-03 12:11:00 +0100985 free(inode_table);
Richard Genoud557f08f2020-11-03 12:11:00 +0100986 free(dirs);
Richard Genoud32bea5b2020-11-03 12:11:01 +0100987 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200988
989 return ret;
990}
991
Richard Weinberger8d42c7b2024-08-02 18:36:47 +0200992int sqfs_opendir(const char *filename, struct fs_dir_stream **dirsp)
993{
994 symlinknest = 0;
995 return sqfs_opendir_nest(filename, dirsp);
996}
997
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200998int sqfs_readdir(struct fs_dir_stream *fs_dirs, struct fs_dirent **dentp)
999{
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001000 symlinknest = 0;
1001 return sqfs_readdir_nest(fs_dirs, dentp);
1002}
1003
1004static int sqfs_readdir_nest(struct fs_dir_stream *fs_dirs, struct fs_dirent **dentp)
1005{
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001006 struct squashfs_super_block *sblk = ctxt.sblk;
1007 struct squashfs_dir_stream *dirs;
1008 struct squashfs_lreg_inode *lreg;
1009 struct squashfs_base_inode *base;
1010 struct squashfs_reg_inode *reg;
1011 int i_number, offset = 0, ret;
1012 struct fs_dirent *dent;
1013 unsigned char *ipos;
Miquel Raynala8e95fd2022-06-09 16:02:06 +02001014 u16 name_size;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001015
1016 dirs = (struct squashfs_dir_stream *)fs_dirs;
1017 if (!dirs->size) {
1018 *dentp = NULL;
1019 return -SQFS_STOP_READDIR;
1020 }
1021
1022 dent = &dirs->dentp;
1023
1024 if (!dirs->entry_count) {
1025 if (dirs->size > SQFS_DIR_HEADER_SIZE) {
1026 dirs->size -= SQFS_DIR_HEADER_SIZE;
1027 } else {
1028 *dentp = NULL;
1029 dirs->size = 0;
1030 return -SQFS_STOP_READDIR;
1031 }
1032
1033 if (dirs->size > SQFS_EMPTY_FILE_SIZE) {
1034 /* Read follow-up (emitted) dir. header */
1035 memcpy(dirs->dir_header, dirs->table,
1036 SQFS_DIR_HEADER_SIZE);
1037 dirs->entry_count = dirs->dir_header->count + 1;
1038 ret = sqfs_read_entry(&dirs->entry, dirs->table +
1039 SQFS_DIR_HEADER_SIZE);
1040 if (ret)
1041 return -SQFS_STOP_READDIR;
1042
1043 dirs->table += SQFS_DIR_HEADER_SIZE;
1044 }
1045 } else {
1046 ret = sqfs_read_entry(&dirs->entry, dirs->table);
1047 if (ret)
1048 return -SQFS_STOP_READDIR;
1049 }
1050
1051 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1052 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1053 sblk->block_size);
Richard Weinbergere8db3d92024-08-02 18:36:46 +02001054 if (!ipos)
1055 return -SQFS_STOP_READDIR;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001056
1057 base = (struct squashfs_base_inode *)ipos;
1058
1059 /* Set entry type and size */
1060 switch (dirs->entry->type) {
1061 case SQFS_DIR_TYPE:
1062 case SQFS_LDIR_TYPE:
1063 dent->type = FS_DT_DIR;
1064 break;
1065 case SQFS_REG_TYPE:
1066 case SQFS_LREG_TYPE:
1067 /*
1068 * Entries do not differentiate extended from regular types, so
1069 * it needs to be verified manually.
1070 */
1071 if (get_unaligned_le16(&base->inode_type) == SQFS_LREG_TYPE) {
1072 lreg = (struct squashfs_lreg_inode *)ipos;
1073 dent->size = get_unaligned_le64(&lreg->file_size);
1074 } else {
1075 reg = (struct squashfs_reg_inode *)ipos;
1076 dent->size = get_unaligned_le32(&reg->file_size);
1077 }
1078
1079 dent->type = FS_DT_REG;
1080 break;
1081 case SQFS_BLKDEV_TYPE:
1082 case SQFS_CHRDEV_TYPE:
1083 case SQFS_LBLKDEV_TYPE:
1084 case SQFS_LCHRDEV_TYPE:
1085 case SQFS_FIFO_TYPE:
1086 case SQFS_SOCKET_TYPE:
1087 case SQFS_LFIFO_TYPE:
1088 case SQFS_LSOCKET_TYPE:
1089 dent->type = SQFS_MISC_ENTRY_TYPE;
1090 break;
1091 case SQFS_SYMLINK_TYPE:
1092 case SQFS_LSYMLINK_TYPE:
1093 dent->type = FS_DT_LNK;
1094 break;
1095 default:
1096 return -SQFS_STOP_READDIR;
1097 }
1098
Miquel Raynala8e95fd2022-06-09 16:02:06 +02001099 /* Set entry name (capped at FS_DIRENT_NAME_LEN which is a U-Boot limitation) */
1100 name_size = min_t(u16, dirs->entry->name_size + 1, FS_DIRENT_NAME_LEN - 1);
1101 strncpy(dent->name, dirs->entry->name, name_size);
1102 dent->name[name_size] = '\0';
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001103
1104 offset = dirs->entry->name_size + 1 + SQFS_ENTRY_BASE_LENGTH;
1105 dirs->entry_count--;
1106
1107 /* Decrement size to be read */
1108 if (dirs->size > offset)
1109 dirs->size -= offset;
1110 else
1111 dirs->size = 0;
1112
1113 /* Keep a reference to the current entry before incrementing it */
1114 dirs->table += offset;
1115
1116 *dentp = dent;
1117
1118 return 0;
1119}
1120
1121int sqfs_probe(struct blk_desc *fs_dev_desc, struct disk_partition *fs_partition)
1122{
1123 struct squashfs_super_block *sblk;
1124 int ret;
1125
1126 ctxt.cur_dev = fs_dev_desc;
1127 ctxt.cur_part_info = *fs_partition;
1128
1129 ret = sqfs_read_sblk(&sblk);
1130 if (ret)
Richard Genoudcd72bc32020-11-03 12:11:21 +01001131 goto error;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001132
1133 /* Make sure it has a valid SquashFS magic number*/
1134 if (get_unaligned_le32(&sblk->s_magic) != SQFS_MAGIC_NUMBER) {
Simon Glass9936fe32021-08-18 21:40:27 -06001135 debug("Bad magic number for SquashFS image.\n");
Richard Genoudac7c4db2020-11-03 12:11:19 +01001136 ret = -EINVAL;
1137 goto error;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001138 }
1139
1140 ctxt.sblk = sblk;
1141
Joao Marcos Costa877576c2020-08-18 17:17:21 +02001142 ret = sqfs_decompressor_init(&ctxt);
Joao Marcos Costa877576c2020-08-18 17:17:21 +02001143 if (ret) {
Richard Genoudac7c4db2020-11-03 12:11:19 +01001144 goto error;
Joao Marcos Costa877576c2020-08-18 17:17:21 +02001145 }
1146
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001147 return 0;
Richard Genoudac7c4db2020-11-03 12:11:19 +01001148error:
1149 ctxt.cur_dev = NULL;
1150 free(ctxt.sblk);
1151 ctxt.sblk = NULL;
1152 return ret;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001153}
1154
1155static char *sqfs_basename(char *path)
1156{
1157 char *fname;
1158
1159 fname = path + strlen(path) - 1;
1160 while (fname >= path) {
1161 if (*fname == '/') {
1162 fname++;
1163 break;
1164 }
1165
1166 fname--;
1167 }
1168
1169 return fname;
1170}
1171
1172static char *sqfs_dirname(char *path)
1173{
1174 char *fname;
1175
1176 fname = sqfs_basename(path);
1177 --fname;
1178 *fname = '\0';
1179
1180 return path;
1181}
1182
1183/*
1184 * Takes a path to file and splits it in two parts: the filename itself and the
1185 * directory's path, e.g.:
1186 * path: /path/to/file.txt
1187 * file: file.txt
1188 * dir: /path/to
1189 */
1190static int sqfs_split_path(char **file, char **dir, const char *path)
1191{
1192 char *dirc, *basec, *bname, *dname, *tmp_path;
1193 int ret = 0;
1194
Richard Genoud8cf2f022020-11-03 12:11:03 +01001195 *file = NULL;
1196 *dir = NULL;
1197 dirc = NULL;
1198 basec = NULL;
1199 bname = NULL;
1200 dname = NULL;
1201 tmp_path = NULL;
1202
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001203 /* check for first slash in path*/
1204 if (path[0] == '/') {
1205 tmp_path = strdup(path);
Richard Genoud8cf2f022020-11-03 12:11:03 +01001206 if (!tmp_path) {
1207 ret = -ENOMEM;
1208 goto out;
1209 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001210 } else {
1211 tmp_path = malloc(strlen(path) + 2);
Richard Genoud8cf2f022020-11-03 12:11:03 +01001212 if (!tmp_path) {
1213 ret = -ENOMEM;
1214 goto out;
1215 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001216 tmp_path[0] = '/';
1217 strcpy(tmp_path + 1, path);
1218 }
1219
1220 /* String duplicates */
1221 dirc = strdup(tmp_path);
1222 if (!dirc) {
1223 ret = -ENOMEM;
Richard Genoud8cf2f022020-11-03 12:11:03 +01001224 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001225 }
1226
1227 basec = strdup(tmp_path);
1228 if (!basec) {
1229 ret = -ENOMEM;
Richard Genoud8cf2f022020-11-03 12:11:03 +01001230 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001231 }
1232
1233 dname = sqfs_dirname(dirc);
1234 bname = sqfs_basename(basec);
1235
1236 *file = strdup(bname);
1237
1238 if (!*file) {
1239 ret = -ENOMEM;
Richard Genoud8cf2f022020-11-03 12:11:03 +01001240 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001241 }
1242
1243 if (*dname == '\0') {
1244 *dir = malloc(2);
1245 if (!*dir) {
1246 ret = -ENOMEM;
Richard Genoud8cf2f022020-11-03 12:11:03 +01001247 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001248 }
1249
1250 (*dir)[0] = '/';
1251 (*dir)[1] = '\0';
1252 } else {
1253 *dir = strdup(dname);
1254 if (!*dir) {
1255 ret = -ENOMEM;
Richard Genoud8cf2f022020-11-03 12:11:03 +01001256 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001257 }
1258 }
1259
Richard Genoud8cf2f022020-11-03 12:11:03 +01001260out:
1261 if (ret) {
1262 free(*file);
1263 free(*dir);
1264 *dir = NULL;
1265 *file = NULL;
1266 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001267 free(basec);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001268 free(dirc);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001269 free(tmp_path);
1270
1271 return ret;
1272}
1273
1274static int sqfs_get_regfile_info(struct squashfs_reg_inode *reg,
1275 struct squashfs_file_info *finfo,
1276 struct squashfs_fragment_block_entry *fentry,
1277 __le32 blksz)
1278{
1279 int datablk_count = 0, ret;
1280
1281 finfo->size = get_unaligned_le32(&reg->file_size);
1282 finfo->offset = get_unaligned_le32(&reg->offset);
1283 finfo->start = get_unaligned_le32(&reg->start_block);
1284 finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(&reg->fragment));
1285
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +02001286 if (finfo->frag && finfo->offset == 0xFFFFFFFF)
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +02001287 return -EINVAL;
1288
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +02001289 if (finfo->size < 1 || finfo->start == 0xFFFFFFFF)
1290 return -EINVAL;
1291
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001292 if (finfo->frag) {
1293 datablk_count = finfo->size / le32_to_cpu(blksz);
1294 ret = sqfs_frag_lookup(get_unaligned_le32(&reg->fragment),
1295 fentry);
1296 if (ret < 0)
1297 return -EINVAL;
Joao Marcos Costa8a724fa2021-05-17 18:20:38 -03001298 finfo->comp = ret;
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +02001299 if (fentry->size < 1 || fentry->start == 0x7FFFFFFF)
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +02001300 return -EINVAL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001301 } else {
1302 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1303 }
1304
1305 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1306 if (!finfo->blk_sizes)
1307 return -ENOMEM;
1308
1309 return datablk_count;
1310}
1311
1312static int sqfs_get_lregfile_info(struct squashfs_lreg_inode *lreg,
1313 struct squashfs_file_info *finfo,
1314 struct squashfs_fragment_block_entry *fentry,
1315 __le32 blksz)
1316{
1317 int datablk_count = 0, ret;
1318
1319 finfo->size = get_unaligned_le64(&lreg->file_size);
1320 finfo->offset = get_unaligned_le32(&lreg->offset);
1321 finfo->start = get_unaligned_le64(&lreg->start_block);
1322 finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(&lreg->fragment));
1323
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +02001324 if (finfo->frag && finfo->offset == 0xFFFFFFFF)
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +02001325 return -EINVAL;
1326
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +02001327 if (finfo->size < 1 || finfo->start == 0x7FFFFFFF)
1328 return -EINVAL;
1329
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001330 if (finfo->frag) {
1331 datablk_count = finfo->size / le32_to_cpu(blksz);
1332 ret = sqfs_frag_lookup(get_unaligned_le32(&lreg->fragment),
1333 fentry);
1334 if (ret < 0)
1335 return -EINVAL;
Joao Marcos Costa8a724fa2021-05-17 18:20:38 -03001336 finfo->comp = ret;
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +02001337 if (fentry->size < 1 || fentry->start == 0x7FFFFFFF)
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +02001338 return -EINVAL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001339 } else {
1340 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1341 }
1342
1343 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1344 if (!finfo->blk_sizes)
1345 return -ENOMEM;
1346
1347 return datablk_count;
1348}
1349
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001350static int sqfs_read_nest(const char *filename, void *buf, loff_t offset,
1351 loff_t len, loff_t *actread)
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001352{
Heinrich Schuchardt8e709432022-04-11 22:54:44 +02001353 char *dir = NULL, *fragment_block, *datablock = NULL;
Richard Genoud0caee472020-11-03 12:11:18 +01001354 char *fragment = NULL, *file = NULL, *resolved, *data;
Campbell Suter46446632020-11-23 15:40:03 +13001355 u64 start, n_blks, table_size, data_offset, table_offset, sparse_size;
Joao Marcos Costab87fd012020-08-18 17:17:22 +02001356 int ret, j, i_number, datablk_count = 0;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001357 struct squashfs_super_block *sblk = ctxt.sblk;
1358 struct squashfs_fragment_block_entry frag_entry;
1359 struct squashfs_file_info finfo = {0};
1360 struct squashfs_symlink_inode *symlink;
1361 struct fs_dir_stream *dirsp = NULL;
1362 struct squashfs_dir_stream *dirs;
1363 struct squashfs_lreg_inode *lreg;
1364 struct squashfs_base_inode *base;
1365 struct squashfs_reg_inode *reg;
1366 unsigned long dest_len;
1367 struct fs_dirent *dent;
1368 unsigned char *ipos;
1369
1370 *actread = 0;
1371
Richard Genoudcfcb2242020-11-03 12:11:24 +01001372 if (offset) {
1373 /*
1374 * TODO: implement reading at an offset in file
1375 */
1376 printf("Error: reading at a specific offset in a squashfs file is not supported yet.\n");
1377 return -EINVAL;
1378 }
1379
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001380 /*
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001381 * sqfs_opendir_nest will uncompress inode and directory tables, and will
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001382 * return a pointer to the directory that contains the requested file.
1383 */
1384 sqfs_split_path(&file, &dir, filename);
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001385 ret = sqfs_opendir_nest(dir, &dirsp);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001386 if (ret) {
Richard Genoud0caee472020-11-03 12:11:18 +01001387 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001388 }
1389
1390 dirs = (struct squashfs_dir_stream *)dirsp;
1391
1392 /* For now, only regular files are able to be loaded */
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001393 while (!sqfs_readdir_nest(dirsp, &dent)) {
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001394 ret = strcmp(dent->name, file);
1395 if (!ret)
1396 break;
1397
1398 free(dirs->entry);
Richard Genoud0b8aa992020-11-03 12:11:11 +01001399 dirs->entry = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001400 }
1401
1402 if (ret) {
1403 printf("File not found.\n");
1404 *actread = 0;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001405 ret = -ENOENT;
Richard Genoud0caee472020-11-03 12:11:18 +01001406 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001407 }
1408
1409 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1410 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1411 sblk->block_size);
Richard Weinbergere8db3d92024-08-02 18:36:46 +02001412 if (!ipos) {
1413 ret = -EINVAL;
1414 goto out;
1415 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001416
1417 base = (struct squashfs_base_inode *)ipos;
1418 switch (get_unaligned_le16(&base->inode_type)) {
1419 case SQFS_REG_TYPE:
1420 reg = (struct squashfs_reg_inode *)ipos;
1421 datablk_count = sqfs_get_regfile_info(reg, &finfo, &frag_entry,
1422 sblk->block_size);
1423 if (datablk_count < 0) {
1424 ret = -EINVAL;
Richard Genoud0caee472020-11-03 12:11:18 +01001425 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001426 }
1427
1428 memcpy(finfo.blk_sizes, ipos + sizeof(*reg),
1429 datablk_count * sizeof(u32));
1430 break;
1431 case SQFS_LREG_TYPE:
1432 lreg = (struct squashfs_lreg_inode *)ipos;
1433 datablk_count = sqfs_get_lregfile_info(lreg, &finfo,
1434 &frag_entry,
1435 sblk->block_size);
1436 if (datablk_count < 0) {
1437 ret = -EINVAL;
Richard Genoud0caee472020-11-03 12:11:18 +01001438 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001439 }
1440
1441 memcpy(finfo.blk_sizes, ipos + sizeof(*lreg),
1442 datablk_count * sizeof(u32));
1443 break;
1444 case SQFS_SYMLINK_TYPE:
1445 case SQFS_LSYMLINK_TYPE:
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001446 if (++symlinknest == MAX_SYMLINK_NEST) {
1447 ret = -ELOOP;
1448 goto out;
1449 }
1450
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001451 symlink = (struct squashfs_symlink_inode *)ipos;
1452 resolved = sqfs_resolve_symlink(symlink, filename);
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001453 ret = sqfs_read_nest(resolved, buf, offset, len, actread);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001454 free(resolved);
Richard Genoud0caee472020-11-03 12:11:18 +01001455 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001456 case SQFS_BLKDEV_TYPE:
1457 case SQFS_CHRDEV_TYPE:
1458 case SQFS_LBLKDEV_TYPE:
1459 case SQFS_LCHRDEV_TYPE:
1460 case SQFS_FIFO_TYPE:
1461 case SQFS_SOCKET_TYPE:
1462 case SQFS_LFIFO_TYPE:
1463 case SQFS_LSOCKET_TYPE:
1464 default:
1465 printf("Unsupported entry type\n");
1466 ret = -EINVAL;
Richard Genoud0caee472020-11-03 12:11:18 +01001467 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001468 }
1469
1470 /* If the user specifies a length, check its sanity */
1471 if (len) {
1472 if (len > finfo.size) {
1473 ret = -EINVAL;
Richard Genoud0caee472020-11-03 12:11:18 +01001474 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001475 }
1476
1477 finfo.size = len;
Richard Genoud6abdf012020-11-03 12:11:23 +01001478 } else {
1479 len = finfo.size;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001480 }
1481
1482 if (datablk_count) {
1483 data_offset = finfo.start;
1484 datablock = malloc(get_unaligned_le32(&sblk->block_size));
1485 if (!datablock) {
1486 ret = -ENOMEM;
Richard Genoud0caee472020-11-03 12:11:18 +01001487 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001488 }
1489 }
1490
1491 for (j = 0; j < datablk_count; j++) {
Heinrich Schuchardt8e709432022-04-11 22:54:44 +02001492 char *data_buffer;
1493
Sean Nyekjaera8eb9622022-05-12 20:37:14 +02001494 start = lldiv(data_offset, ctxt.cur_dev->blksz);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001495 table_size = SQFS_BLOCK_SIZE(finfo.blk_sizes[j]);
1496 table_offset = data_offset - (start * ctxt.cur_dev->blksz);
1497 n_blks = DIV_ROUND_UP(table_size + table_offset,
1498 ctxt.cur_dev->blksz);
1499
Campbell Suter46446632020-11-23 15:40:03 +13001500 /* Don't load any data for sparse blocks */
1501 if (finfo.blk_sizes[j] == 0) {
1502 n_blks = 0;
1503 table_offset = 0;
1504 data_buffer = NULL;
1505 data = NULL;
1506 } else {
1507 data_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001508
Campbell Suter46446632020-11-23 15:40:03 +13001509 if (!data_buffer) {
1510 ret = -ENOMEM;
1511 goto out;
1512 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001513
Campbell Suter46446632020-11-23 15:40:03 +13001514 ret = sqfs_disk_read(start, n_blks, data_buffer);
1515 if (ret < 0) {
1516 /*
1517 * Possible causes: too many data blocks or too large
1518 * SquashFS block size. Tip: re-compile the SquashFS
1519 * image with mksquashfs's -b <block_size> option.
1520 */
1521 printf("Error: too many data blocks to be read.\n");
1522 goto out;
1523 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001524
Campbell Suter46446632020-11-23 15:40:03 +13001525 data = data_buffer + table_offset;
1526 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001527
1528 /* Load the data */
Campbell Suter46446632020-11-23 15:40:03 +13001529 if (finfo.blk_sizes[j] == 0) {
1530 /* This is a sparse block */
1531 sparse_size = get_unaligned_le32(&sblk->block_size);
1532 if ((*actread + sparse_size) > len)
1533 sparse_size = len - *actread;
1534 memset(buf + *actread, 0, sparse_size);
1535 *actread += sparse_size;
1536 } else if (SQFS_COMPRESSED_BLOCK(finfo.blk_sizes[j])) {
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001537 dest_len = get_unaligned_le32(&sblk->block_size);
Joao Marcos Costab87fd012020-08-18 17:17:22 +02001538 ret = sqfs_decompress(&ctxt, datablock, &dest_len,
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001539 data, table_size);
1540 if (ret)
Richard Genoud0caee472020-11-03 12:11:18 +01001541 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001542
Richard Genoud6abdf012020-11-03 12:11:23 +01001543 if ((*actread + dest_len) > len)
1544 dest_len = len - *actread;
Richard Genoudcfcb2242020-11-03 12:11:24 +01001545 memcpy(buf + *actread, datablock, dest_len);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001546 *actread += dest_len;
1547 } else {
Richard Genoud6abdf012020-11-03 12:11:23 +01001548 if ((*actread + table_size) > len)
1549 table_size = len - *actread;
Richard Genoudcfcb2242020-11-03 12:11:24 +01001550 memcpy(buf + *actread, data, table_size);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001551 *actread += table_size;
1552 }
1553
1554 data_offset += table_size;
Heinrich Schuchardt8e709432022-04-11 22:54:44 +02001555 free(data_buffer);
Richard Genoud6abdf012020-11-03 12:11:23 +01001556 if (*actread >= len)
1557 break;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001558 }
1559
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001560 /*
1561 * There is no need to continue if the file is not fragmented.
1562 */
1563 if (!finfo.frag) {
1564 ret = 0;
Richard Genoud0caee472020-11-03 12:11:18 +01001565 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001566 }
1567
Sean Nyekjaera8eb9622022-05-12 20:37:14 +02001568 start = lldiv(frag_entry.start, ctxt.cur_dev->blksz);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001569 table_size = SQFS_BLOCK_SIZE(frag_entry.size);
1570 table_offset = frag_entry.start - (start * ctxt.cur_dev->blksz);
1571 n_blks = DIV_ROUND_UP(table_size + table_offset, ctxt.cur_dev->blksz);
1572
1573 fragment = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
1574
1575 if (!fragment) {
1576 ret = -ENOMEM;
Richard Genoud0caee472020-11-03 12:11:18 +01001577 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001578 }
1579
1580 ret = sqfs_disk_read(start, n_blks, fragment);
1581 if (ret < 0)
Richard Genoud0caee472020-11-03 12:11:18 +01001582 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001583
1584 /* File compressed and fragmented */
1585 if (finfo.frag && finfo.comp) {
1586 dest_len = get_unaligned_le32(&sblk->block_size);
1587 fragment_block = malloc(dest_len);
1588 if (!fragment_block) {
1589 ret = -ENOMEM;
Richard Genoud0caee472020-11-03 12:11:18 +01001590 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001591 }
1592
Joao Marcos Costab87fd012020-08-18 17:17:22 +02001593 ret = sqfs_decompress(&ctxt, fragment_block, &dest_len,
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001594 (void *)fragment + table_offset,
1595 frag_entry.size);
1596 if (ret) {
1597 free(fragment_block);
Richard Genoud0caee472020-11-03 12:11:18 +01001598 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001599 }
1600
Joao Marcos Costa8a724fa2021-05-17 18:20:38 -03001601 memcpy(buf + *actread, &fragment_block[finfo.offset], finfo.size - *actread);
1602 *actread = finfo.size;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001603
1604 free(fragment_block);
1605
1606 } else if (finfo.frag && !finfo.comp) {
1607 fragment_block = (void *)fragment + table_offset;
1608
Joao Marcos Costa8a724fa2021-05-17 18:20:38 -03001609 memcpy(buf + *actread, &fragment_block[finfo.offset], finfo.size - *actread);
1610 *actread = finfo.size;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001611 }
1612
Richard Genoud0caee472020-11-03 12:11:18 +01001613out:
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001614 free(fragment);
Heinrich Schuchardt8e709432022-04-11 22:54:44 +02001615 free(datablock);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001616 free(file);
1617 free(dir);
Richard Genoud0caee472020-11-03 12:11:18 +01001618 free(finfo.blk_sizes);
Richard Genoud84a5f572020-11-03 12:11:13 +01001619 sqfs_closedir(dirsp);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001620
1621 return ret;
1622}
1623
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001624int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
1625 loff_t *actread)
1626{
1627 symlinknest = 0;
1628 return sqfs_read_nest(filename, buf, offset, len, actread);
1629}
1630
1631static int sqfs_size_nest(const char *filename, loff_t *size)
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001632{
1633 struct squashfs_super_block *sblk = ctxt.sblk;
1634 struct squashfs_symlink_inode *symlink;
1635 struct fs_dir_stream *dirsp = NULL;
1636 struct squashfs_base_inode *base;
1637 struct squashfs_dir_stream *dirs;
1638 struct squashfs_lreg_inode *lreg;
1639 struct squashfs_reg_inode *reg;
1640 char *dir, *file, *resolved;
1641 struct fs_dirent *dent;
1642 unsigned char *ipos;
1643 int ret, i_number;
1644
1645 sqfs_split_path(&file, &dir, filename);
1646 /*
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001647 * sqfs_opendir_nest will uncompress inode and directory tables, and will
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001648 * return a pointer to the directory that contains the requested file.
1649 */
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001650 ret = sqfs_opendir_nest(dir, &dirsp);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001651 if (ret) {
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001652 ret = -EINVAL;
1653 goto free_strings;
1654 }
1655
1656 dirs = (struct squashfs_dir_stream *)dirsp;
1657
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001658 while (!sqfs_readdir_nest(dirsp, &dent)) {
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001659 ret = strcmp(dent->name, file);
1660 if (!ret)
1661 break;
1662 free(dirs->entry);
Richard Genoudc3dadcb2020-11-03 12:11:09 +01001663 dirs->entry = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001664 }
1665
1666 if (ret) {
1667 printf("File not found.\n");
1668 *size = 0;
1669 ret = -EINVAL;
1670 goto free_strings;
1671 }
1672
1673 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1674 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1675 sblk->block_size);
Richard Weinbergere8db3d92024-08-02 18:36:46 +02001676
1677 if (!ipos) {
1678 *size = 0;
1679 ret = -EINVAL;
1680 goto free_strings;
1681 }
1682
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001683 free(dirs->entry);
Richard Genoudc3dadcb2020-11-03 12:11:09 +01001684 dirs->entry = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001685
1686 base = (struct squashfs_base_inode *)ipos;
1687 switch (get_unaligned_le16(&base->inode_type)) {
1688 case SQFS_REG_TYPE:
1689 reg = (struct squashfs_reg_inode *)ipos;
1690 *size = get_unaligned_le32(&reg->file_size);
1691 break;
1692 case SQFS_LREG_TYPE:
1693 lreg = (struct squashfs_lreg_inode *)ipos;
1694 *size = get_unaligned_le64(&lreg->file_size);
1695 break;
1696 case SQFS_SYMLINK_TYPE:
1697 case SQFS_LSYMLINK_TYPE:
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001698 if (++symlinknest == MAX_SYMLINK_NEST) {
1699 *size = 0;
1700 return -ELOOP;
1701 }
1702
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001703 symlink = (struct squashfs_symlink_inode *)ipos;
1704 resolved = sqfs_resolve_symlink(symlink, filename);
1705 ret = sqfs_size(resolved, size);
1706 free(resolved);
1707 break;
1708 case SQFS_BLKDEV_TYPE:
1709 case SQFS_CHRDEV_TYPE:
1710 case SQFS_LBLKDEV_TYPE:
1711 case SQFS_LCHRDEV_TYPE:
1712 case SQFS_FIFO_TYPE:
1713 case SQFS_SOCKET_TYPE:
1714 case SQFS_LFIFO_TYPE:
1715 case SQFS_LSOCKET_TYPE:
1716 default:
1717 printf("Unable to recover entry's size.\n");
1718 *size = 0;
1719 ret = -EINVAL;
1720 break;
1721 }
1722
1723free_strings:
1724 free(dir);
1725 free(file);
1726
1727 sqfs_closedir(dirsp);
1728
1729 return ret;
1730}
1731
Richard Genoudcfca20c2020-11-03 12:11:26 +01001732int sqfs_exists(const char *filename)
1733{
1734 struct fs_dir_stream *dirsp = NULL;
1735 struct squashfs_dir_stream *dirs;
1736 char *dir, *file;
1737 struct fs_dirent *dent;
1738 int ret;
1739
1740 sqfs_split_path(&file, &dir, filename);
1741 /*
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001742 * sqfs_opendir_nest will uncompress inode and directory tables, and will
Richard Genoudcfca20c2020-11-03 12:11:26 +01001743 * return a pointer to the directory that contains the requested file.
1744 */
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001745 symlinknest = 0;
1746 ret = sqfs_opendir_nest(dir, &dirsp);
Richard Genoudcfca20c2020-11-03 12:11:26 +01001747 if (ret) {
1748 ret = -EINVAL;
1749 goto free_strings;
1750 }
1751
1752 dirs = (struct squashfs_dir_stream *)dirsp;
1753
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001754 while (!sqfs_readdir_nest(dirsp, &dent)) {
Richard Genoudcfca20c2020-11-03 12:11:26 +01001755 ret = strcmp(dent->name, file);
1756 if (!ret)
1757 break;
1758 free(dirs->entry);
1759 dirs->entry = NULL;
1760 }
1761
1762 sqfs_closedir(dirsp);
1763
1764free_strings:
1765 free(dir);
1766 free(file);
1767
1768 return ret == 0;
1769}
1770
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001771int sqfs_size(const char *filename, loff_t *size)
1772{
1773 symlinknest = 0;
1774 return sqfs_size_nest(filename, size);
1775}
1776
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001777void sqfs_close(void)
1778{
Richard Genoud1d95a1e2020-11-24 18:07:52 +01001779 sqfs_decompressor_cleanup(&ctxt);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001780 free(ctxt.sblk);
Richard Genoud1d95a1e2020-11-24 18:07:52 +01001781 ctxt.sblk = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001782 ctxt.cur_dev = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001783}
1784
1785void sqfs_closedir(struct fs_dir_stream *dirs)
1786{
1787 struct squashfs_dir_stream *sqfs_dirs;
1788
Heinrich Schuchardt836bba42021-02-01 03:28:48 +01001789 if (!dirs)
1790 return;
1791
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001792 sqfs_dirs = (struct squashfs_dir_stream *)dirs;
1793 free(sqfs_dirs->inode_table);
1794 free(sqfs_dirs->dir_table);
1795 free(sqfs_dirs->dir_header);
Richard Genoud2575eb22020-11-03 12:11:02 +01001796 free(sqfs_dirs);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001797}