blob: 2dcdd60f68343f801bd73e318568fb030434ed31 [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 }
Richard Weinbergerf042f2f2024-08-02 22:05:09 +0200570 /*
571 * Concatenate remaining tokens and symlink's target.
572 * Allocate enough space for rem, target, '/' and '\0'.
573 */
574 res = malloc(strlen(rem) + strlen(target) + 2);
Richard Genoud2762f652020-11-03 12:11:06 +0100575 if (!res) {
576 ret = -ENOMEM;
577 goto out;
578 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200579 strcpy(res, target);
580 res[strlen(target)] = '/';
581 strcpy(res + strlen(target) + 1, rem);
582 token_count = sqfs_count_tokens(res);
583
Richard Genoud2762f652020-11-03 12:11:06 +0100584 if (token_count < 0) {
585 ret = -EINVAL;
586 goto out;
587 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200588
589 sym_tokens = malloc(token_count * sizeof(char *));
Richard Genoud2762f652020-11-03 12:11:06 +0100590 if (!sym_tokens) {
591 ret = -EINVAL;
592 goto out;
593 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200594
595 /* Fill tokens list */
596 ret = sqfs_tokenize(sym_tokens, token_count, res);
Richard Genoud2762f652020-11-03 12:11:06 +0100597 if (ret) {
598 ret = -EINVAL;
599 goto out;
600 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200601 free(dirs->entry);
Richard Genoude405fc42020-11-03 12:11:05 +0100602 dirs->entry = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200603
604 ret = sqfs_search_dir(dirs, sym_tokens, token_count,
605 m_list, m_count);
Richard Genoud2762f652020-11-03 12:11:06 +0100606 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200607 } else if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) {
608 printf("** Cannot find directory. **\n");
609 free(dirs->entry);
Richard Genoude405fc42020-11-03 12:11:05 +0100610 dirs->entry = NULL;
Richard Genoud2762f652020-11-03 12:11:06 +0100611 ret = -EINVAL;
612 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200613 }
614
615 /* Check if it is an extended dir. */
616 if (get_unaligned_le16(&dir->inode_type) == SQFS_LDIR_TYPE)
617 ldir = (struct squashfs_ldir_inode *)table;
618
619 /* Get dir. offset into the directory table */
620 offset = sqfs_dir_offset(table, m_list, m_count);
621 dirs->table = &dirs->dir_table[offset];
622
623 /* Copy directory header */
624 memcpy(dirs->dir_header, &dirs->dir_table[offset],
625 SQFS_DIR_HEADER_SIZE);
626
627 /* Check for empty directory */
628 if (sqfs_is_empty_dir(table)) {
629 printf("Empty directory.\n");
630 free(dirs->entry);
Richard Genoude405fc42020-11-03 12:11:05 +0100631 dirs->entry = NULL;
Richard Genoud2762f652020-11-03 12:11:06 +0100632 ret = SQFS_EMPTY_DIR;
633 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200634 }
635
636 dirs->table += SQFS_DIR_HEADER_SIZE;
637 dirs->size = get_unaligned_le16(&dir->file_size);
638 dirs->entry_count = dirs->dir_header->count + 1;
639 dirs->size -= SQFS_DIR_HEADER_SIZE;
640 free(dirs->entry);
Richard Genoude405fc42020-11-03 12:11:05 +0100641 dirs->entry = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200642 }
643
644 offset = sqfs_dir_offset(table, m_list, m_count);
645 dirs->table = &dirs->dir_table[offset];
646
647 if (get_unaligned_le16(&dir->inode_type) == SQFS_DIR_TYPE)
648 memcpy(&dirs->i_dir, dir, sizeof(*dir));
649 else
650 memcpy(&dirs->i_ldir, ldir, sizeof(*ldir));
651
Richard Genoud2762f652020-11-03 12:11:06 +0100652out:
653 free(res);
654 free(rem);
655 free(path);
656 free(target);
657 free(sym_tokens);
658 return ret;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200659}
660
661/*
662 * Inode and directory tables are stored as a series of metadata blocks, and
663 * given the compressed size of this table, we can calculate how much metadata
664 * blocks are needed to store the result of the decompression, since a
665 * decompressed metadata block should have a size of 8KiB.
666 */
667static int sqfs_count_metablks(void *table, u32 offset, int table_size)
668{
669 int count = 0, cur_size = 0, ret;
670 u32 data_size;
671 bool comp;
672
673 do {
674 ret = sqfs_read_metablock(table, offset + cur_size, &comp,
675 &data_size);
676 if (ret)
677 return -EINVAL;
678 cur_size += data_size + SQFS_HEADER_SIZE;
679 count++;
680 } while (cur_size < table_size);
681
682 return count;
683}
684
685/*
686 * Storing the metadata blocks header's positions will be useful while looking
687 * for an entry in the directory table, using the reference (index and offset)
688 * given by its inode.
689 */
690static int sqfs_get_metablk_pos(u32 *pos_list, void *table, u32 offset,
691 int metablks_count)
692{
693 u32 data_size, cur_size = 0;
694 int j, ret = 0;
695 bool comp;
696
697 if (!metablks_count)
698 return -EINVAL;
699
700 for (j = 0; j < metablks_count; j++) {
701 ret = sqfs_read_metablock(table, offset + cur_size, &comp,
702 &data_size);
703 if (ret)
704 return -EINVAL;
705
706 cur_size += data_size + SQFS_HEADER_SIZE;
707 pos_list[j] = cur_size;
708 }
709
710 return ret;
711}
712
713static int sqfs_read_inode_table(unsigned char **inode_table)
714{
715 struct squashfs_super_block *sblk = ctxt.sblk;
716 u64 start, n_blks, table_offset, table_size;
Joao Marcos Costab87fd012020-08-18 17:17:22 +0200717 int j, ret = 0, metablks_count;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200718 unsigned char *src_table, *itb;
719 u32 src_len, dest_offset = 0;
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200720 unsigned long dest_len = 0;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200721 bool compressed;
Joao Marcos Costa652277d2025-02-19 11:16:33 +0100722 size_t buf_size;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200723
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200724 table_size = get_unaligned_le64(&sblk->directory_table_start) -
725 get_unaligned_le64(&sblk->inode_table_start);
726 start = get_unaligned_le64(&sblk->inode_table_start) /
727 ctxt.cur_dev->blksz;
728 n_blks = sqfs_calc_n_blks(sblk->inode_table_start,
729 sblk->directory_table_start, &table_offset);
730
731 /* Allocate a proper sized buffer (itb) to store the inode table */
Joao Marcos Costa652277d2025-02-19 11:16:33 +0100732 if (__builtin_mul_overflow(n_blks, ctxt.cur_dev->blksz, &buf_size))
733 return -EINVAL;
734
735 itb = malloc_cache_aligned(buf_size);
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200736 if (!itb)
737 return -ENOMEM;
738
739 if (sqfs_disk_read(start, n_blks, itb) < 0) {
740 ret = -EINVAL;
741 goto free_itb;
742 }
743
744 /* Parse inode table (metadata block) header */
745 ret = sqfs_read_metablock(itb, table_offset, &compressed, &src_len);
746 if (ret) {
747 ret = -EINVAL;
748 goto free_itb;
749 }
750
751 /* Calculate size to store the whole decompressed table */
752 metablks_count = sqfs_count_metablks(itb, table_offset, table_size);
753 if (metablks_count < 1) {
754 ret = -EINVAL;
755 goto free_itb;
756 }
757
Miquel Raynaledaf6042022-06-27 12:20:03 +0200758 *inode_table = kcalloc(metablks_count, SQFS_METADATA_BLOCK_SIZE,
759 GFP_KERNEL);
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200760 if (!*inode_table) {
761 ret = -ENOMEM;
Lars Weber205783f2022-01-13 14:28:45 +0100762 printf("Error: failed to allocate squashfs inode_table of size %i, increasing CONFIG_SYS_MALLOC_LEN could help\n",
763 metablks_count * SQFS_METADATA_BLOCK_SIZE);
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200764 goto free_itb;
765 }
766
767 src_table = itb + table_offset + SQFS_HEADER_SIZE;
768
769 /* Extract compressed Inode table */
770 for (j = 0; j < metablks_count; j++) {
771 sqfs_read_metablock(itb, table_offset, &compressed, &src_len);
772 if (compressed) {
773 dest_len = SQFS_METADATA_BLOCK_SIZE;
Joao Marcos Costab87fd012020-08-18 17:17:22 +0200774 ret = sqfs_decompress(&ctxt, *inode_table +
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200775 dest_offset, &dest_len,
776 src_table, src_len);
777 if (ret) {
778 free(*inode_table);
Richard Genouda62528d2020-11-03 12:11:07 +0100779 *inode_table = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200780 goto free_itb;
781 }
782
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200783 dest_offset += dest_len;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200784 } else {
785 memcpy(*inode_table + (j * SQFS_METADATA_BLOCK_SIZE),
786 src_table, src_len);
787 }
788
789 /*
790 * Offsets to the decompression destination, to the metadata
791 * buffer 'itb' and to the decompression source, respectively.
792 */
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200793
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200794 table_offset += src_len + SQFS_HEADER_SIZE;
795 src_table += src_len + SQFS_HEADER_SIZE;
796 }
797
798free_itb:
799 free(itb);
800
801 return ret;
802}
803
804static int sqfs_read_directory_table(unsigned char **dir_table, u32 **pos_list)
805{
806 u64 start, n_blks, table_offset, table_size;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200807 struct squashfs_super_block *sblk = ctxt.sblk;
Joao Marcos Costab87fd012020-08-18 17:17:22 +0200808 int j, ret = 0, metablks_count = -1;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200809 unsigned char *src_table, *dtb;
810 u32 src_len, dest_offset = 0;
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200811 unsigned long dest_len = 0;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200812 bool compressed;
Joao Marcos Costa652277d2025-02-19 11:16:33 +0100813 size_t buf_size;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200814
Richard Genoud2f9c9852020-11-03 12:11:04 +0100815 *dir_table = NULL;
816 *pos_list = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200817 /* DIRECTORY TABLE */
818 table_size = get_unaligned_le64(&sblk->fragment_table_start) -
819 get_unaligned_le64(&sblk->directory_table_start);
820 start = get_unaligned_le64(&sblk->directory_table_start) /
821 ctxt.cur_dev->blksz;
822 n_blks = sqfs_calc_n_blks(sblk->directory_table_start,
823 sblk->fragment_table_start, &table_offset);
824
825 /* Allocate a proper sized buffer (dtb) to store the directory table */
Joao Marcos Costa652277d2025-02-19 11:16:33 +0100826 if (__builtin_mul_overflow(n_blks, ctxt.cur_dev->blksz, &buf_size))
827 return -EINVAL;
828
829 dtb = malloc_cache_aligned(buf_size);
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200830 if (!dtb)
831 return -ENOMEM;
832
833 if (sqfs_disk_read(start, n_blks, dtb) < 0)
Richard Genoud2f9c9852020-11-03 12:11:04 +0100834 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200835
836 /* Parse directory table (metadata block) header */
837 ret = sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
838 if (ret)
Richard Genoud2f9c9852020-11-03 12:11:04 +0100839 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200840
841 /* Calculate total size to store the whole decompressed table */
842 metablks_count = sqfs_count_metablks(dtb, table_offset, table_size);
843 if (metablks_count < 1)
Richard Genoud2f9c9852020-11-03 12:11:04 +0100844 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200845
846 *dir_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE);
847 if (!*dir_table)
Richard Genoud2f9c9852020-11-03 12:11:04 +0100848 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200849
850 *pos_list = malloc(metablks_count * sizeof(u32));
Richard Genoud2f9c9852020-11-03 12:11:04 +0100851 if (!*pos_list)
852 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200853
854 ret = sqfs_get_metablk_pos(*pos_list, dtb, table_offset,
855 metablks_count);
856 if (ret) {
857 metablks_count = -1;
Richard Genoud2f9c9852020-11-03 12:11:04 +0100858 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200859 }
860
861 src_table = dtb + table_offset + SQFS_HEADER_SIZE;
862
863 /* Extract compressed Directory table */
864 dest_offset = 0;
865 for (j = 0; j < metablks_count; j++) {
866 sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
867 if (compressed) {
868 dest_len = SQFS_METADATA_BLOCK_SIZE;
Joao Marcos Costab87fd012020-08-18 17:17:22 +0200869 ret = sqfs_decompress(&ctxt, *dir_table +
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200870 (j * SQFS_METADATA_BLOCK_SIZE),
871 &dest_len, src_table, src_len);
872 if (ret) {
873 metablks_count = -1;
Richard Genoud2f9c9852020-11-03 12:11:04 +0100874 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200875 }
876
877 if (dest_len < SQFS_METADATA_BLOCK_SIZE) {
878 dest_offset += dest_len;
879 break;
880 }
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +0200881
882 dest_offset += dest_len;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200883 } else {
884 memcpy(*dir_table + (j * SQFS_METADATA_BLOCK_SIZE),
885 src_table, src_len);
886 }
887
888 /*
889 * Offsets to the decompression destination, to the metadata
890 * buffer 'dtb' and to the decompression source, respectively.
891 */
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200892 table_offset += src_len + SQFS_HEADER_SIZE;
893 src_table += src_len + SQFS_HEADER_SIZE;
894 }
895
Richard Genoud2f9c9852020-11-03 12:11:04 +0100896out:
897 if (metablks_count < 1) {
898 free(*dir_table);
899 free(*pos_list);
900 *dir_table = NULL;
901 *pos_list = NULL;
902 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200903 free(dtb);
904
905 return metablks_count;
906}
907
Richard Weinberger8d42c7b2024-08-02 18:36:47 +0200908static int sqfs_opendir_nest(const char *filename, struct fs_dir_stream **dirsp)
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200909{
910 unsigned char *inode_table = NULL, *dir_table = NULL;
Richard Genoud32bea5b2020-11-03 12:11:01 +0100911 int j, token_count = 0, ret = 0, metablks_count;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200912 struct squashfs_dir_stream *dirs;
Richard Genoud32bea5b2020-11-03 12:11:01 +0100913 char **token_list = NULL, *path = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200914 u32 *pos_list = NULL;
915
Heinrich Schuchardte086af82021-05-17 08:21:39 +0200916 dirs = calloc(1, sizeof(*dirs));
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200917 if (!dirs)
918 return -EINVAL;
919
Richard Genoud557f08f2020-11-03 12:11:00 +0100920 /* these should be set to NULL to prevent dangling pointers */
921 dirs->dir_header = NULL;
922 dirs->entry = NULL;
923 dirs->table = NULL;
924 dirs->inode_table = NULL;
925 dirs->dir_table = NULL;
926
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200927 ret = sqfs_read_inode_table(&inode_table);
Richard Genoud557f08f2020-11-03 12:11:00 +0100928 if (ret) {
929 ret = -EINVAL;
Richard Genoud32bea5b2020-11-03 12:11:01 +0100930 goto out;
Richard Genoud557f08f2020-11-03 12:11:00 +0100931 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200932
933 metablks_count = sqfs_read_directory_table(&dir_table, &pos_list);
Richard Genoud557f08f2020-11-03 12:11:00 +0100934 if (metablks_count < 1) {
935 ret = -EINVAL;
Richard Genoud32bea5b2020-11-03 12:11:01 +0100936 goto out;
Richard Genoud557f08f2020-11-03 12:11:00 +0100937 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200938
939 /* Tokenize filename */
940 token_count = sqfs_count_tokens(filename);
Richard Genoud557f08f2020-11-03 12:11:00 +0100941 if (token_count < 0) {
942 ret = -EINVAL;
Richard Genoud32bea5b2020-11-03 12:11:01 +0100943 goto out;
Richard Genoud557f08f2020-11-03 12:11:00 +0100944 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200945
946 path = strdup(filename);
Richard Genoud557f08f2020-11-03 12:11:00 +0100947 if (!path) {
948 ret = -EINVAL;
Richard Genoud32bea5b2020-11-03 12:11:01 +0100949 goto out;
Richard Genoud557f08f2020-11-03 12:11:00 +0100950 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200951
Heinrich Schuchardt7bb96222025-04-14 15:19:24 +0200952 token_list = calloc(token_count, sizeof(char *));
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200953 if (!token_list) {
954 ret = -EINVAL;
Richard Genoud32bea5b2020-11-03 12:11:01 +0100955 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200956 }
957
958 /* Fill tokens list */
959 ret = sqfs_tokenize(token_list, token_count, path);
960 if (ret)
Richard Genoud32bea5b2020-11-03 12:11:01 +0100961 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200962 /*
963 * ldir's (extended directory) size is greater than dir, so it works as
964 * a general solution for the malloc size, since 'i' is a union.
965 */
966 dirs->inode_table = inode_table;
967 dirs->dir_table = dir_table;
968 ret = sqfs_search_dir(dirs, token_list, token_count, pos_list,
969 metablks_count);
970 if (ret)
Richard Genoud32bea5b2020-11-03 12:11:01 +0100971 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200972
973 if (le16_to_cpu(dirs->i_dir.inode_type) == SQFS_DIR_TYPE)
974 dirs->size = le16_to_cpu(dirs->i_dir.file_size);
975 else
976 dirs->size = le32_to_cpu(dirs->i_ldir.file_size);
977
978 /* Setup directory header */
979 memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE);
980 dirs->entry_count = dirs->dir_header->count + 1;
981 dirs->size -= SQFS_DIR_HEADER_SIZE;
982
983 /* Setup entry */
984 dirs->entry = NULL;
985 dirs->table += SQFS_DIR_HEADER_SIZE;
986
987 *dirsp = (struct fs_dir_stream *)dirs;
988
Richard Genoud32bea5b2020-11-03 12:11:01 +0100989out:
Heinrich Schuchardt7bb96222025-04-14 15:19:24 +0200990 if (token_list) {
991 for (j = 0; j < token_count; j++)
992 free(token_list[j]);
993 free(token_list);
994 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200995 free(pos_list);
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200996 free(path);
Richard Genoud32bea5b2020-11-03 12:11:01 +0100997 if (ret) {
Richard Genoud557f08f2020-11-03 12:11:00 +0100998 free(inode_table);
Richard Genoud557f08f2020-11-03 12:11:00 +0100999 free(dirs);
Richard Genoud32bea5b2020-11-03 12:11:01 +01001000 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001001
1002 return ret;
1003}
1004
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001005int sqfs_opendir(const char *filename, struct fs_dir_stream **dirsp)
1006{
1007 symlinknest = 0;
1008 return sqfs_opendir_nest(filename, dirsp);
1009}
1010
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001011int sqfs_readdir(struct fs_dir_stream *fs_dirs, struct fs_dirent **dentp)
1012{
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001013 symlinknest = 0;
1014 return sqfs_readdir_nest(fs_dirs, dentp);
1015}
1016
1017static int sqfs_readdir_nest(struct fs_dir_stream *fs_dirs, struct fs_dirent **dentp)
1018{
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001019 struct squashfs_super_block *sblk = ctxt.sblk;
1020 struct squashfs_dir_stream *dirs;
1021 struct squashfs_lreg_inode *lreg;
1022 struct squashfs_base_inode *base;
1023 struct squashfs_reg_inode *reg;
1024 int i_number, offset = 0, ret;
1025 struct fs_dirent *dent;
1026 unsigned char *ipos;
Miquel Raynala8e95fd2022-06-09 16:02:06 +02001027 u16 name_size;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001028
1029 dirs = (struct squashfs_dir_stream *)fs_dirs;
1030 if (!dirs->size) {
1031 *dentp = NULL;
1032 return -SQFS_STOP_READDIR;
1033 }
1034
1035 dent = &dirs->dentp;
1036
1037 if (!dirs->entry_count) {
1038 if (dirs->size > SQFS_DIR_HEADER_SIZE) {
1039 dirs->size -= SQFS_DIR_HEADER_SIZE;
1040 } else {
1041 *dentp = NULL;
1042 dirs->size = 0;
1043 return -SQFS_STOP_READDIR;
1044 }
1045
1046 if (dirs->size > SQFS_EMPTY_FILE_SIZE) {
1047 /* Read follow-up (emitted) dir. header */
1048 memcpy(dirs->dir_header, dirs->table,
1049 SQFS_DIR_HEADER_SIZE);
1050 dirs->entry_count = dirs->dir_header->count + 1;
1051 ret = sqfs_read_entry(&dirs->entry, dirs->table +
1052 SQFS_DIR_HEADER_SIZE);
1053 if (ret)
1054 return -SQFS_STOP_READDIR;
1055
1056 dirs->table += SQFS_DIR_HEADER_SIZE;
1057 }
1058 } else {
1059 ret = sqfs_read_entry(&dirs->entry, dirs->table);
1060 if (ret)
1061 return -SQFS_STOP_READDIR;
1062 }
1063
1064 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1065 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1066 sblk->block_size);
Richard Weinbergere8db3d92024-08-02 18:36:46 +02001067 if (!ipos)
1068 return -SQFS_STOP_READDIR;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001069
1070 base = (struct squashfs_base_inode *)ipos;
1071
1072 /* Set entry type and size */
1073 switch (dirs->entry->type) {
1074 case SQFS_DIR_TYPE:
1075 case SQFS_LDIR_TYPE:
1076 dent->type = FS_DT_DIR;
1077 break;
1078 case SQFS_REG_TYPE:
1079 case SQFS_LREG_TYPE:
1080 /*
1081 * Entries do not differentiate extended from regular types, so
1082 * it needs to be verified manually.
1083 */
1084 if (get_unaligned_le16(&base->inode_type) == SQFS_LREG_TYPE) {
1085 lreg = (struct squashfs_lreg_inode *)ipos;
1086 dent->size = get_unaligned_le64(&lreg->file_size);
1087 } else {
1088 reg = (struct squashfs_reg_inode *)ipos;
1089 dent->size = get_unaligned_le32(&reg->file_size);
1090 }
1091
1092 dent->type = FS_DT_REG;
1093 break;
1094 case SQFS_BLKDEV_TYPE:
1095 case SQFS_CHRDEV_TYPE:
1096 case SQFS_LBLKDEV_TYPE:
1097 case SQFS_LCHRDEV_TYPE:
1098 case SQFS_FIFO_TYPE:
1099 case SQFS_SOCKET_TYPE:
1100 case SQFS_LFIFO_TYPE:
1101 case SQFS_LSOCKET_TYPE:
1102 dent->type = SQFS_MISC_ENTRY_TYPE;
1103 break;
1104 case SQFS_SYMLINK_TYPE:
1105 case SQFS_LSYMLINK_TYPE:
1106 dent->type = FS_DT_LNK;
1107 break;
1108 default:
1109 return -SQFS_STOP_READDIR;
1110 }
1111
Miquel Raynala8e95fd2022-06-09 16:02:06 +02001112 /* Set entry name (capped at FS_DIRENT_NAME_LEN which is a U-Boot limitation) */
1113 name_size = min_t(u16, dirs->entry->name_size + 1, FS_DIRENT_NAME_LEN - 1);
1114 strncpy(dent->name, dirs->entry->name, name_size);
1115 dent->name[name_size] = '\0';
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001116
1117 offset = dirs->entry->name_size + 1 + SQFS_ENTRY_BASE_LENGTH;
1118 dirs->entry_count--;
1119
1120 /* Decrement size to be read */
1121 if (dirs->size > offset)
1122 dirs->size -= offset;
1123 else
1124 dirs->size = 0;
1125
1126 /* Keep a reference to the current entry before incrementing it */
1127 dirs->table += offset;
1128
1129 *dentp = dent;
1130
1131 return 0;
1132}
1133
1134int sqfs_probe(struct blk_desc *fs_dev_desc, struct disk_partition *fs_partition)
1135{
1136 struct squashfs_super_block *sblk;
1137 int ret;
1138
1139 ctxt.cur_dev = fs_dev_desc;
1140 ctxt.cur_part_info = *fs_partition;
1141
1142 ret = sqfs_read_sblk(&sblk);
1143 if (ret)
Richard Genoudcd72bc32020-11-03 12:11:21 +01001144 goto error;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001145
1146 /* Make sure it has a valid SquashFS magic number*/
1147 if (get_unaligned_le32(&sblk->s_magic) != SQFS_MAGIC_NUMBER) {
Simon Glass9936fe32021-08-18 21:40:27 -06001148 debug("Bad magic number for SquashFS image.\n");
Richard Genoudac7c4db2020-11-03 12:11:19 +01001149 ret = -EINVAL;
1150 goto error;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001151 }
1152
1153 ctxt.sblk = sblk;
1154
Joao Marcos Costa877576c2020-08-18 17:17:21 +02001155 ret = sqfs_decompressor_init(&ctxt);
Joao Marcos Costa877576c2020-08-18 17:17:21 +02001156 if (ret) {
Richard Genoudac7c4db2020-11-03 12:11:19 +01001157 goto error;
Joao Marcos Costa877576c2020-08-18 17:17:21 +02001158 }
1159
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001160 return 0;
Richard Genoudac7c4db2020-11-03 12:11:19 +01001161error:
1162 ctxt.cur_dev = NULL;
1163 free(ctxt.sblk);
1164 ctxt.sblk = NULL;
1165 return ret;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001166}
1167
1168static char *sqfs_basename(char *path)
1169{
1170 char *fname;
1171
1172 fname = path + strlen(path) - 1;
1173 while (fname >= path) {
1174 if (*fname == '/') {
1175 fname++;
1176 break;
1177 }
1178
1179 fname--;
1180 }
1181
1182 return fname;
1183}
1184
1185static char *sqfs_dirname(char *path)
1186{
1187 char *fname;
1188
1189 fname = sqfs_basename(path);
1190 --fname;
1191 *fname = '\0';
1192
1193 return path;
1194}
1195
1196/*
1197 * Takes a path to file and splits it in two parts: the filename itself and the
1198 * directory's path, e.g.:
1199 * path: /path/to/file.txt
1200 * file: file.txt
1201 * dir: /path/to
1202 */
1203static int sqfs_split_path(char **file, char **dir, const char *path)
1204{
1205 char *dirc, *basec, *bname, *dname, *tmp_path;
1206 int ret = 0;
1207
Richard Genoud8cf2f022020-11-03 12:11:03 +01001208 *file = NULL;
1209 *dir = NULL;
1210 dirc = NULL;
1211 basec = NULL;
1212 bname = NULL;
1213 dname = NULL;
1214 tmp_path = NULL;
1215
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001216 /* check for first slash in path*/
1217 if (path[0] == '/') {
1218 tmp_path = strdup(path);
Richard Genoud8cf2f022020-11-03 12:11:03 +01001219 if (!tmp_path) {
1220 ret = -ENOMEM;
1221 goto out;
1222 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001223 } else {
1224 tmp_path = malloc(strlen(path) + 2);
Richard Genoud8cf2f022020-11-03 12:11:03 +01001225 if (!tmp_path) {
1226 ret = -ENOMEM;
1227 goto out;
1228 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001229 tmp_path[0] = '/';
1230 strcpy(tmp_path + 1, path);
1231 }
1232
1233 /* String duplicates */
1234 dirc = strdup(tmp_path);
1235 if (!dirc) {
1236 ret = -ENOMEM;
Richard Genoud8cf2f022020-11-03 12:11:03 +01001237 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001238 }
1239
1240 basec = strdup(tmp_path);
1241 if (!basec) {
1242 ret = -ENOMEM;
Richard Genoud8cf2f022020-11-03 12:11:03 +01001243 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001244 }
1245
1246 dname = sqfs_dirname(dirc);
1247 bname = sqfs_basename(basec);
1248
1249 *file = strdup(bname);
1250
1251 if (!*file) {
1252 ret = -ENOMEM;
Richard Genoud8cf2f022020-11-03 12:11:03 +01001253 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001254 }
1255
1256 if (*dname == '\0') {
1257 *dir = malloc(2);
1258 if (!*dir) {
1259 ret = -ENOMEM;
Richard Genoud8cf2f022020-11-03 12:11:03 +01001260 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001261 }
1262
1263 (*dir)[0] = '/';
1264 (*dir)[1] = '\0';
1265 } else {
1266 *dir = strdup(dname);
1267 if (!*dir) {
1268 ret = -ENOMEM;
Richard Genoud8cf2f022020-11-03 12:11:03 +01001269 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001270 }
1271 }
1272
Richard Genoud8cf2f022020-11-03 12:11:03 +01001273out:
1274 if (ret) {
1275 free(*file);
1276 free(*dir);
1277 *dir = NULL;
1278 *file = NULL;
1279 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001280 free(basec);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001281 free(dirc);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001282 free(tmp_path);
1283
1284 return ret;
1285}
1286
1287static int sqfs_get_regfile_info(struct squashfs_reg_inode *reg,
1288 struct squashfs_file_info *finfo,
1289 struct squashfs_fragment_block_entry *fentry,
1290 __le32 blksz)
1291{
1292 int datablk_count = 0, ret;
1293
1294 finfo->size = get_unaligned_le32(&reg->file_size);
1295 finfo->offset = get_unaligned_le32(&reg->offset);
1296 finfo->start = get_unaligned_le32(&reg->start_block);
1297 finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(&reg->fragment));
1298
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +02001299 if (finfo->frag && finfo->offset == 0xFFFFFFFF)
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +02001300 return -EINVAL;
1301
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +02001302 if (finfo->size < 1 || finfo->start == 0xFFFFFFFF)
1303 return -EINVAL;
1304
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001305 if (finfo->frag) {
1306 datablk_count = finfo->size / le32_to_cpu(blksz);
1307 ret = sqfs_frag_lookup(get_unaligned_le32(&reg->fragment),
1308 fentry);
1309 if (ret < 0)
1310 return -EINVAL;
Joao Marcos Costa8a724fa2021-05-17 18:20:38 -03001311 finfo->comp = ret;
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +02001312 if (fentry->size < 1 || fentry->start == 0x7FFFFFFF)
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +02001313 return -EINVAL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001314 } else {
1315 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1316 }
1317
1318 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1319 if (!finfo->blk_sizes)
1320 return -ENOMEM;
1321
1322 return datablk_count;
1323}
1324
1325static int sqfs_get_lregfile_info(struct squashfs_lreg_inode *lreg,
1326 struct squashfs_file_info *finfo,
1327 struct squashfs_fragment_block_entry *fentry,
1328 __le32 blksz)
1329{
1330 int datablk_count = 0, ret;
1331
1332 finfo->size = get_unaligned_le64(&lreg->file_size);
1333 finfo->offset = get_unaligned_le32(&lreg->offset);
1334 finfo->start = get_unaligned_le64(&lreg->start_block);
1335 finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(&lreg->fragment));
1336
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +02001337 if (finfo->frag && finfo->offset == 0xFFFFFFFF)
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +02001338 return -EINVAL;
1339
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +02001340 if (finfo->size < 1 || finfo->start == 0x7FFFFFFF)
1341 return -EINVAL;
1342
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001343 if (finfo->frag) {
1344 datablk_count = finfo->size / le32_to_cpu(blksz);
1345 ret = sqfs_frag_lookup(get_unaligned_le32(&lreg->fragment),
1346 fentry);
1347 if (ret < 0)
1348 return -EINVAL;
Joao Marcos Costa8a724fa2021-05-17 18:20:38 -03001349 finfo->comp = ret;
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +02001350 if (fentry->size < 1 || fentry->start == 0x7FFFFFFF)
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +02001351 return -EINVAL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001352 } else {
1353 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1354 }
1355
1356 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1357 if (!finfo->blk_sizes)
1358 return -ENOMEM;
1359
1360 return datablk_count;
1361}
1362
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001363static int sqfs_read_nest(const char *filename, void *buf, loff_t offset,
1364 loff_t len, loff_t *actread)
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001365{
Heinrich Schuchardt8e709432022-04-11 22:54:44 +02001366 char *dir = NULL, *fragment_block, *datablock = NULL;
Richard Genoud0caee472020-11-03 12:11:18 +01001367 char *fragment = NULL, *file = NULL, *resolved, *data;
Campbell Suter46446632020-11-23 15:40:03 +13001368 u64 start, n_blks, table_size, data_offset, table_offset, sparse_size;
Joao Marcos Costab87fd012020-08-18 17:17:22 +02001369 int ret, j, i_number, datablk_count = 0;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001370 struct squashfs_super_block *sblk = ctxt.sblk;
1371 struct squashfs_fragment_block_entry frag_entry;
1372 struct squashfs_file_info finfo = {0};
1373 struct squashfs_symlink_inode *symlink;
1374 struct fs_dir_stream *dirsp = NULL;
1375 struct squashfs_dir_stream *dirs;
1376 struct squashfs_lreg_inode *lreg;
1377 struct squashfs_base_inode *base;
1378 struct squashfs_reg_inode *reg;
1379 unsigned long dest_len;
1380 struct fs_dirent *dent;
1381 unsigned char *ipos;
Joao Marcos Costa652277d2025-02-19 11:16:33 +01001382 size_t buf_size;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001383
1384 *actread = 0;
1385
Richard Genoudcfcb2242020-11-03 12:11:24 +01001386 if (offset) {
1387 /*
1388 * TODO: implement reading at an offset in file
1389 */
1390 printf("Error: reading at a specific offset in a squashfs file is not supported yet.\n");
1391 return -EINVAL;
1392 }
1393
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001394 /*
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001395 * sqfs_opendir_nest will uncompress inode and directory tables, and will
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001396 * return a pointer to the directory that contains the requested file.
1397 */
1398 sqfs_split_path(&file, &dir, filename);
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001399 ret = sqfs_opendir_nest(dir, &dirsp);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001400 if (ret) {
Richard Genoud0caee472020-11-03 12:11:18 +01001401 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001402 }
1403
1404 dirs = (struct squashfs_dir_stream *)dirsp;
1405
1406 /* For now, only regular files are able to be loaded */
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001407 while (!sqfs_readdir_nest(dirsp, &dent)) {
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001408 ret = strcmp(dent->name, file);
1409 if (!ret)
1410 break;
1411
1412 free(dirs->entry);
Richard Genoud0b8aa992020-11-03 12:11:11 +01001413 dirs->entry = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001414 }
1415
1416 if (ret) {
1417 printf("File not found.\n");
1418 *actread = 0;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001419 ret = -ENOENT;
Richard Genoud0caee472020-11-03 12:11:18 +01001420 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001421 }
1422
1423 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1424 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1425 sblk->block_size);
Richard Weinbergere8db3d92024-08-02 18:36:46 +02001426 if (!ipos) {
1427 ret = -EINVAL;
1428 goto out;
1429 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001430
1431 base = (struct squashfs_base_inode *)ipos;
1432 switch (get_unaligned_le16(&base->inode_type)) {
1433 case SQFS_REG_TYPE:
1434 reg = (struct squashfs_reg_inode *)ipos;
1435 datablk_count = sqfs_get_regfile_info(reg, &finfo, &frag_entry,
1436 sblk->block_size);
1437 if (datablk_count < 0) {
1438 ret = -EINVAL;
Richard Genoud0caee472020-11-03 12:11:18 +01001439 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001440 }
1441
1442 memcpy(finfo.blk_sizes, ipos + sizeof(*reg),
1443 datablk_count * sizeof(u32));
1444 break;
1445 case SQFS_LREG_TYPE:
1446 lreg = (struct squashfs_lreg_inode *)ipos;
1447 datablk_count = sqfs_get_lregfile_info(lreg, &finfo,
1448 &frag_entry,
1449 sblk->block_size);
1450 if (datablk_count < 0) {
1451 ret = -EINVAL;
Richard Genoud0caee472020-11-03 12:11:18 +01001452 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001453 }
1454
1455 memcpy(finfo.blk_sizes, ipos + sizeof(*lreg),
1456 datablk_count * sizeof(u32));
1457 break;
1458 case SQFS_SYMLINK_TYPE:
1459 case SQFS_LSYMLINK_TYPE:
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001460 if (++symlinknest == MAX_SYMLINK_NEST) {
1461 ret = -ELOOP;
1462 goto out;
1463 }
1464
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001465 symlink = (struct squashfs_symlink_inode *)ipos;
1466 resolved = sqfs_resolve_symlink(symlink, filename);
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001467 ret = sqfs_read_nest(resolved, buf, offset, len, actread);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001468 free(resolved);
Richard Genoud0caee472020-11-03 12:11:18 +01001469 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001470 case SQFS_BLKDEV_TYPE:
1471 case SQFS_CHRDEV_TYPE:
1472 case SQFS_LBLKDEV_TYPE:
1473 case SQFS_LCHRDEV_TYPE:
1474 case SQFS_FIFO_TYPE:
1475 case SQFS_SOCKET_TYPE:
1476 case SQFS_LFIFO_TYPE:
1477 case SQFS_LSOCKET_TYPE:
1478 default:
1479 printf("Unsupported entry type\n");
1480 ret = -EINVAL;
Richard Genoud0caee472020-11-03 12:11:18 +01001481 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001482 }
1483
1484 /* If the user specifies a length, check its sanity */
1485 if (len) {
1486 if (len > finfo.size) {
1487 ret = -EINVAL;
Richard Genoud0caee472020-11-03 12:11:18 +01001488 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001489 }
1490
1491 finfo.size = len;
Richard Genoud6abdf012020-11-03 12:11:23 +01001492 } else {
1493 len = finfo.size;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001494 }
1495
1496 if (datablk_count) {
1497 data_offset = finfo.start;
1498 datablock = malloc(get_unaligned_le32(&sblk->block_size));
1499 if (!datablock) {
1500 ret = -ENOMEM;
Richard Genoud0caee472020-11-03 12:11:18 +01001501 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001502 }
1503 }
1504
1505 for (j = 0; j < datablk_count; j++) {
Heinrich Schuchardt8e709432022-04-11 22:54:44 +02001506 char *data_buffer;
1507
Sean Nyekjaera8eb9622022-05-12 20:37:14 +02001508 start = lldiv(data_offset, ctxt.cur_dev->blksz);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001509 table_size = SQFS_BLOCK_SIZE(finfo.blk_sizes[j]);
1510 table_offset = data_offset - (start * ctxt.cur_dev->blksz);
1511 n_blks = DIV_ROUND_UP(table_size + table_offset,
1512 ctxt.cur_dev->blksz);
1513
Campbell Suter46446632020-11-23 15:40:03 +13001514 /* Don't load any data for sparse blocks */
1515 if (finfo.blk_sizes[j] == 0) {
1516 n_blks = 0;
1517 table_offset = 0;
1518 data_buffer = NULL;
1519 data = NULL;
1520 } else {
1521 data_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001522
Campbell Suter46446632020-11-23 15:40:03 +13001523 if (!data_buffer) {
1524 ret = -ENOMEM;
1525 goto out;
1526 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001527
Campbell Suter46446632020-11-23 15:40:03 +13001528 ret = sqfs_disk_read(start, n_blks, data_buffer);
1529 if (ret < 0) {
1530 /*
1531 * Possible causes: too many data blocks or too large
1532 * SquashFS block size. Tip: re-compile the SquashFS
1533 * image with mksquashfs's -b <block_size> option.
1534 */
1535 printf("Error: too many data blocks to be read.\n");
1536 goto out;
1537 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001538
Campbell Suter46446632020-11-23 15:40:03 +13001539 data = data_buffer + table_offset;
1540 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001541
1542 /* Load the data */
Campbell Suter46446632020-11-23 15:40:03 +13001543 if (finfo.blk_sizes[j] == 0) {
1544 /* This is a sparse block */
1545 sparse_size = get_unaligned_le32(&sblk->block_size);
1546 if ((*actread + sparse_size) > len)
1547 sparse_size = len - *actread;
1548 memset(buf + *actread, 0, sparse_size);
1549 *actread += sparse_size;
1550 } else if (SQFS_COMPRESSED_BLOCK(finfo.blk_sizes[j])) {
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001551 dest_len = get_unaligned_le32(&sblk->block_size);
Joao Marcos Costab87fd012020-08-18 17:17:22 +02001552 ret = sqfs_decompress(&ctxt, datablock, &dest_len,
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001553 data, table_size);
1554 if (ret)
Richard Genoud0caee472020-11-03 12:11:18 +01001555 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001556
Richard Genoud6abdf012020-11-03 12:11:23 +01001557 if ((*actread + dest_len) > len)
1558 dest_len = len - *actread;
Richard Genoudcfcb2242020-11-03 12:11:24 +01001559 memcpy(buf + *actread, datablock, dest_len);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001560 *actread += dest_len;
1561 } else {
Richard Genoud6abdf012020-11-03 12:11:23 +01001562 if ((*actread + table_size) > len)
1563 table_size = len - *actread;
Richard Genoudcfcb2242020-11-03 12:11:24 +01001564 memcpy(buf + *actread, data, table_size);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001565 *actread += table_size;
1566 }
1567
1568 data_offset += table_size;
Heinrich Schuchardt8e709432022-04-11 22:54:44 +02001569 free(data_buffer);
Richard Genoud6abdf012020-11-03 12:11:23 +01001570 if (*actread >= len)
1571 break;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001572 }
1573
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001574 /*
1575 * There is no need to continue if the file is not fragmented.
1576 */
1577 if (!finfo.frag) {
1578 ret = 0;
Richard Genoud0caee472020-11-03 12:11:18 +01001579 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001580 }
1581
Sean Nyekjaera8eb9622022-05-12 20:37:14 +02001582 start = lldiv(frag_entry.start, ctxt.cur_dev->blksz);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001583 table_size = SQFS_BLOCK_SIZE(frag_entry.size);
1584 table_offset = frag_entry.start - (start * ctxt.cur_dev->blksz);
1585 n_blks = DIV_ROUND_UP(table_size + table_offset, ctxt.cur_dev->blksz);
1586
Joao Marcos Costa652277d2025-02-19 11:16:33 +01001587 if (__builtin_mul_overflow(n_blks, ctxt.cur_dev->blksz, &buf_size))
1588 return -EINVAL;
1589
1590 fragment = malloc_cache_aligned(buf_size);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001591
1592 if (!fragment) {
1593 ret = -ENOMEM;
Richard Genoud0caee472020-11-03 12:11:18 +01001594 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001595 }
1596
1597 ret = sqfs_disk_read(start, n_blks, fragment);
1598 if (ret < 0)
Richard Genoud0caee472020-11-03 12:11:18 +01001599 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001600
1601 /* File compressed and fragmented */
1602 if (finfo.frag && finfo.comp) {
1603 dest_len = get_unaligned_le32(&sblk->block_size);
1604 fragment_block = malloc(dest_len);
1605 if (!fragment_block) {
1606 ret = -ENOMEM;
Richard Genoud0caee472020-11-03 12:11:18 +01001607 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001608 }
1609
Joao Marcos Costab87fd012020-08-18 17:17:22 +02001610 ret = sqfs_decompress(&ctxt, fragment_block, &dest_len,
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001611 (void *)fragment + table_offset,
1612 frag_entry.size);
1613 if (ret) {
1614 free(fragment_block);
Richard Genoud0caee472020-11-03 12:11:18 +01001615 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001616 }
1617
Joao Marcos Costa8a724fa2021-05-17 18:20:38 -03001618 memcpy(buf + *actread, &fragment_block[finfo.offset], finfo.size - *actread);
1619 *actread = finfo.size;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001620
1621 free(fragment_block);
1622
1623 } else if (finfo.frag && !finfo.comp) {
1624 fragment_block = (void *)fragment + table_offset;
1625
Joao Marcos Costa8a724fa2021-05-17 18:20:38 -03001626 memcpy(buf + *actread, &fragment_block[finfo.offset], finfo.size - *actread);
1627 *actread = finfo.size;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001628 }
1629
Richard Genoud0caee472020-11-03 12:11:18 +01001630out:
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001631 free(fragment);
Heinrich Schuchardt8e709432022-04-11 22:54:44 +02001632 free(datablock);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001633 free(file);
1634 free(dir);
Richard Genoud0caee472020-11-03 12:11:18 +01001635 free(finfo.blk_sizes);
Richard Genoud84a5f572020-11-03 12:11:13 +01001636 sqfs_closedir(dirsp);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001637
1638 return ret;
1639}
1640
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001641int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
1642 loff_t *actread)
1643{
1644 symlinknest = 0;
1645 return sqfs_read_nest(filename, buf, offset, len, actread);
1646}
1647
1648static int sqfs_size_nest(const char *filename, loff_t *size)
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001649{
1650 struct squashfs_super_block *sblk = ctxt.sblk;
1651 struct squashfs_symlink_inode *symlink;
1652 struct fs_dir_stream *dirsp = NULL;
1653 struct squashfs_base_inode *base;
1654 struct squashfs_dir_stream *dirs;
1655 struct squashfs_lreg_inode *lreg;
1656 struct squashfs_reg_inode *reg;
1657 char *dir, *file, *resolved;
1658 struct fs_dirent *dent;
1659 unsigned char *ipos;
1660 int ret, i_number;
1661
1662 sqfs_split_path(&file, &dir, filename);
1663 /*
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001664 * sqfs_opendir_nest will uncompress inode and directory tables, and will
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001665 * return a pointer to the directory that contains the requested file.
1666 */
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001667 ret = sqfs_opendir_nest(dir, &dirsp);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001668 if (ret) {
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001669 ret = -EINVAL;
1670 goto free_strings;
1671 }
1672
1673 dirs = (struct squashfs_dir_stream *)dirsp;
1674
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001675 while (!sqfs_readdir_nest(dirsp, &dent)) {
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001676 ret = strcmp(dent->name, file);
1677 if (!ret)
1678 break;
1679 free(dirs->entry);
Richard Genoudc3dadcb2020-11-03 12:11:09 +01001680 dirs->entry = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001681 }
1682
1683 if (ret) {
1684 printf("File not found.\n");
1685 *size = 0;
1686 ret = -EINVAL;
1687 goto free_strings;
1688 }
1689
1690 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1691 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1692 sblk->block_size);
Richard Weinbergere8db3d92024-08-02 18:36:46 +02001693
1694 if (!ipos) {
1695 *size = 0;
1696 ret = -EINVAL;
1697 goto free_strings;
1698 }
1699
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001700 free(dirs->entry);
Richard Genoudc3dadcb2020-11-03 12:11:09 +01001701 dirs->entry = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001702
1703 base = (struct squashfs_base_inode *)ipos;
1704 switch (get_unaligned_le16(&base->inode_type)) {
1705 case SQFS_REG_TYPE:
1706 reg = (struct squashfs_reg_inode *)ipos;
1707 *size = get_unaligned_le32(&reg->file_size);
1708 break;
1709 case SQFS_LREG_TYPE:
1710 lreg = (struct squashfs_lreg_inode *)ipos;
1711 *size = get_unaligned_le64(&lreg->file_size);
1712 break;
1713 case SQFS_SYMLINK_TYPE:
1714 case SQFS_LSYMLINK_TYPE:
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001715 if (++symlinknest == MAX_SYMLINK_NEST) {
1716 *size = 0;
Andrea della Porta05527842025-03-02 19:29:31 +01001717 ret = -ELOOP;
1718 break;
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001719 }
1720
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001721 symlink = (struct squashfs_symlink_inode *)ipos;
1722 resolved = sqfs_resolve_symlink(symlink, filename);
1723 ret = sqfs_size(resolved, size);
1724 free(resolved);
1725 break;
1726 case SQFS_BLKDEV_TYPE:
1727 case SQFS_CHRDEV_TYPE:
1728 case SQFS_LBLKDEV_TYPE:
1729 case SQFS_LCHRDEV_TYPE:
1730 case SQFS_FIFO_TYPE:
1731 case SQFS_SOCKET_TYPE:
1732 case SQFS_LFIFO_TYPE:
1733 case SQFS_LSOCKET_TYPE:
1734 default:
1735 printf("Unable to recover entry's size.\n");
1736 *size = 0;
1737 ret = -EINVAL;
1738 break;
1739 }
1740
1741free_strings:
1742 free(dir);
1743 free(file);
1744
1745 sqfs_closedir(dirsp);
1746
1747 return ret;
1748}
1749
Richard Genoudcfca20c2020-11-03 12:11:26 +01001750int sqfs_exists(const char *filename)
1751{
1752 struct fs_dir_stream *dirsp = NULL;
1753 struct squashfs_dir_stream *dirs;
1754 char *dir, *file;
1755 struct fs_dirent *dent;
1756 int ret;
1757
1758 sqfs_split_path(&file, &dir, filename);
1759 /*
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001760 * sqfs_opendir_nest will uncompress inode and directory tables, and will
Richard Genoudcfca20c2020-11-03 12:11:26 +01001761 * return a pointer to the directory that contains the requested file.
1762 */
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001763 symlinknest = 0;
1764 ret = sqfs_opendir_nest(dir, &dirsp);
Richard Genoudcfca20c2020-11-03 12:11:26 +01001765 if (ret) {
1766 ret = -EINVAL;
1767 goto free_strings;
1768 }
1769
1770 dirs = (struct squashfs_dir_stream *)dirsp;
1771
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001772 while (!sqfs_readdir_nest(dirsp, &dent)) {
Richard Genoudcfca20c2020-11-03 12:11:26 +01001773 ret = strcmp(dent->name, file);
1774 if (!ret)
1775 break;
1776 free(dirs->entry);
1777 dirs->entry = NULL;
1778 }
1779
1780 sqfs_closedir(dirsp);
1781
1782free_strings:
1783 free(dir);
1784 free(file);
1785
1786 return ret == 0;
1787}
1788
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001789int sqfs_size(const char *filename, loff_t *size)
1790{
1791 symlinknest = 0;
1792 return sqfs_size_nest(filename, size);
1793}
1794
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001795void sqfs_close(void)
1796{
Richard Genoud1d95a1e2020-11-24 18:07:52 +01001797 sqfs_decompressor_cleanup(&ctxt);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001798 free(ctxt.sblk);
Richard Genoud1d95a1e2020-11-24 18:07:52 +01001799 ctxt.sblk = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001800 ctxt.cur_dev = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001801}
1802
1803void sqfs_closedir(struct fs_dir_stream *dirs)
1804{
1805 struct squashfs_dir_stream *sqfs_dirs;
1806
Heinrich Schuchardt836bba42021-02-01 03:28:48 +01001807 if (!dirs)
1808 return;
1809
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001810 sqfs_dirs = (struct squashfs_dir_stream *)dirs;
1811 free(sqfs_dirs->inode_table);
1812 free(sqfs_dirs->dir_table);
1813 free(sqfs_dirs->dir_header);
Richard Genoud2575eb22020-11-03 12:11:02 +01001814 free(sqfs_dirs);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001815}