blob: 8fac6c6c5a98cf148ee194221a4a32810c517842 [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
952 token_list = malloc(token_count * sizeof(char *));
953 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:
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200990 for (j = 0; j < token_count; j++)
991 free(token_list[j]);
992 free(token_list);
993 free(pos_list);
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200994 free(path);
Richard Genoud32bea5b2020-11-03 12:11:01 +0100995 if (ret) {
Richard Genoud557f08f2020-11-03 12:11:00 +0100996 free(inode_table);
Richard Genoud557f08f2020-11-03 12:11:00 +0100997 free(dirs);
Richard Genoud32bea5b2020-11-03 12:11:01 +0100998 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +0200999
1000 return ret;
1001}
1002
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001003int sqfs_opendir(const char *filename, struct fs_dir_stream **dirsp)
1004{
1005 symlinknest = 0;
1006 return sqfs_opendir_nest(filename, dirsp);
1007}
1008
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001009int sqfs_readdir(struct fs_dir_stream *fs_dirs, struct fs_dirent **dentp)
1010{
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001011 symlinknest = 0;
1012 return sqfs_readdir_nest(fs_dirs, dentp);
1013}
1014
1015static int sqfs_readdir_nest(struct fs_dir_stream *fs_dirs, struct fs_dirent **dentp)
1016{
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001017 struct squashfs_super_block *sblk = ctxt.sblk;
1018 struct squashfs_dir_stream *dirs;
1019 struct squashfs_lreg_inode *lreg;
1020 struct squashfs_base_inode *base;
1021 struct squashfs_reg_inode *reg;
1022 int i_number, offset = 0, ret;
1023 struct fs_dirent *dent;
1024 unsigned char *ipos;
Miquel Raynala8e95fd2022-06-09 16:02:06 +02001025 u16 name_size;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001026
1027 dirs = (struct squashfs_dir_stream *)fs_dirs;
1028 if (!dirs->size) {
1029 *dentp = NULL;
1030 return -SQFS_STOP_READDIR;
1031 }
1032
1033 dent = &dirs->dentp;
1034
1035 if (!dirs->entry_count) {
1036 if (dirs->size > SQFS_DIR_HEADER_SIZE) {
1037 dirs->size -= SQFS_DIR_HEADER_SIZE;
1038 } else {
1039 *dentp = NULL;
1040 dirs->size = 0;
1041 return -SQFS_STOP_READDIR;
1042 }
1043
1044 if (dirs->size > SQFS_EMPTY_FILE_SIZE) {
1045 /* Read follow-up (emitted) dir. header */
1046 memcpy(dirs->dir_header, dirs->table,
1047 SQFS_DIR_HEADER_SIZE);
1048 dirs->entry_count = dirs->dir_header->count + 1;
1049 ret = sqfs_read_entry(&dirs->entry, dirs->table +
1050 SQFS_DIR_HEADER_SIZE);
1051 if (ret)
1052 return -SQFS_STOP_READDIR;
1053
1054 dirs->table += SQFS_DIR_HEADER_SIZE;
1055 }
1056 } else {
1057 ret = sqfs_read_entry(&dirs->entry, dirs->table);
1058 if (ret)
1059 return -SQFS_STOP_READDIR;
1060 }
1061
1062 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1063 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1064 sblk->block_size);
Richard Weinbergere8db3d92024-08-02 18:36:46 +02001065 if (!ipos)
1066 return -SQFS_STOP_READDIR;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001067
1068 base = (struct squashfs_base_inode *)ipos;
1069
1070 /* Set entry type and size */
1071 switch (dirs->entry->type) {
1072 case SQFS_DIR_TYPE:
1073 case SQFS_LDIR_TYPE:
1074 dent->type = FS_DT_DIR;
1075 break;
1076 case SQFS_REG_TYPE:
1077 case SQFS_LREG_TYPE:
1078 /*
1079 * Entries do not differentiate extended from regular types, so
1080 * it needs to be verified manually.
1081 */
1082 if (get_unaligned_le16(&base->inode_type) == SQFS_LREG_TYPE) {
1083 lreg = (struct squashfs_lreg_inode *)ipos;
1084 dent->size = get_unaligned_le64(&lreg->file_size);
1085 } else {
1086 reg = (struct squashfs_reg_inode *)ipos;
1087 dent->size = get_unaligned_le32(&reg->file_size);
1088 }
1089
1090 dent->type = FS_DT_REG;
1091 break;
1092 case SQFS_BLKDEV_TYPE:
1093 case SQFS_CHRDEV_TYPE:
1094 case SQFS_LBLKDEV_TYPE:
1095 case SQFS_LCHRDEV_TYPE:
1096 case SQFS_FIFO_TYPE:
1097 case SQFS_SOCKET_TYPE:
1098 case SQFS_LFIFO_TYPE:
1099 case SQFS_LSOCKET_TYPE:
1100 dent->type = SQFS_MISC_ENTRY_TYPE;
1101 break;
1102 case SQFS_SYMLINK_TYPE:
1103 case SQFS_LSYMLINK_TYPE:
1104 dent->type = FS_DT_LNK;
1105 break;
1106 default:
1107 return -SQFS_STOP_READDIR;
1108 }
1109
Miquel Raynala8e95fd2022-06-09 16:02:06 +02001110 /* Set entry name (capped at FS_DIRENT_NAME_LEN which is a U-Boot limitation) */
1111 name_size = min_t(u16, dirs->entry->name_size + 1, FS_DIRENT_NAME_LEN - 1);
1112 strncpy(dent->name, dirs->entry->name, name_size);
1113 dent->name[name_size] = '\0';
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001114
1115 offset = dirs->entry->name_size + 1 + SQFS_ENTRY_BASE_LENGTH;
1116 dirs->entry_count--;
1117
1118 /* Decrement size to be read */
1119 if (dirs->size > offset)
1120 dirs->size -= offset;
1121 else
1122 dirs->size = 0;
1123
1124 /* Keep a reference to the current entry before incrementing it */
1125 dirs->table += offset;
1126
1127 *dentp = dent;
1128
1129 return 0;
1130}
1131
1132int sqfs_probe(struct blk_desc *fs_dev_desc, struct disk_partition *fs_partition)
1133{
1134 struct squashfs_super_block *sblk;
1135 int ret;
1136
1137 ctxt.cur_dev = fs_dev_desc;
1138 ctxt.cur_part_info = *fs_partition;
1139
1140 ret = sqfs_read_sblk(&sblk);
1141 if (ret)
Richard Genoudcd72bc32020-11-03 12:11:21 +01001142 goto error;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001143
1144 /* Make sure it has a valid SquashFS magic number*/
1145 if (get_unaligned_le32(&sblk->s_magic) != SQFS_MAGIC_NUMBER) {
Simon Glass9936fe32021-08-18 21:40:27 -06001146 debug("Bad magic number for SquashFS image.\n");
Richard Genoudac7c4db2020-11-03 12:11:19 +01001147 ret = -EINVAL;
1148 goto error;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001149 }
1150
1151 ctxt.sblk = sblk;
1152
Joao Marcos Costa877576c2020-08-18 17:17:21 +02001153 ret = sqfs_decompressor_init(&ctxt);
Joao Marcos Costa877576c2020-08-18 17:17:21 +02001154 if (ret) {
Richard Genoudac7c4db2020-11-03 12:11:19 +01001155 goto error;
Joao Marcos Costa877576c2020-08-18 17:17:21 +02001156 }
1157
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001158 return 0;
Richard Genoudac7c4db2020-11-03 12:11:19 +01001159error:
1160 ctxt.cur_dev = NULL;
1161 free(ctxt.sblk);
1162 ctxt.sblk = NULL;
1163 return ret;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001164}
1165
1166static char *sqfs_basename(char *path)
1167{
1168 char *fname;
1169
1170 fname = path + strlen(path) - 1;
1171 while (fname >= path) {
1172 if (*fname == '/') {
1173 fname++;
1174 break;
1175 }
1176
1177 fname--;
1178 }
1179
1180 return fname;
1181}
1182
1183static char *sqfs_dirname(char *path)
1184{
1185 char *fname;
1186
1187 fname = sqfs_basename(path);
1188 --fname;
1189 *fname = '\0';
1190
1191 return path;
1192}
1193
1194/*
1195 * Takes a path to file and splits it in two parts: the filename itself and the
1196 * directory's path, e.g.:
1197 * path: /path/to/file.txt
1198 * file: file.txt
1199 * dir: /path/to
1200 */
1201static int sqfs_split_path(char **file, char **dir, const char *path)
1202{
1203 char *dirc, *basec, *bname, *dname, *tmp_path;
1204 int ret = 0;
1205
Richard Genoud8cf2f022020-11-03 12:11:03 +01001206 *file = NULL;
1207 *dir = NULL;
1208 dirc = NULL;
1209 basec = NULL;
1210 bname = NULL;
1211 dname = NULL;
1212 tmp_path = NULL;
1213
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001214 /* check for first slash in path*/
1215 if (path[0] == '/') {
1216 tmp_path = strdup(path);
Richard Genoud8cf2f022020-11-03 12:11:03 +01001217 if (!tmp_path) {
1218 ret = -ENOMEM;
1219 goto out;
1220 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001221 } else {
1222 tmp_path = malloc(strlen(path) + 2);
Richard Genoud8cf2f022020-11-03 12:11:03 +01001223 if (!tmp_path) {
1224 ret = -ENOMEM;
1225 goto out;
1226 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001227 tmp_path[0] = '/';
1228 strcpy(tmp_path + 1, path);
1229 }
1230
1231 /* String duplicates */
1232 dirc = strdup(tmp_path);
1233 if (!dirc) {
1234 ret = -ENOMEM;
Richard Genoud8cf2f022020-11-03 12:11:03 +01001235 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001236 }
1237
1238 basec = strdup(tmp_path);
1239 if (!basec) {
1240 ret = -ENOMEM;
Richard Genoud8cf2f022020-11-03 12:11:03 +01001241 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001242 }
1243
1244 dname = sqfs_dirname(dirc);
1245 bname = sqfs_basename(basec);
1246
1247 *file = strdup(bname);
1248
1249 if (!*file) {
1250 ret = -ENOMEM;
Richard Genoud8cf2f022020-11-03 12:11:03 +01001251 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001252 }
1253
1254 if (*dname == '\0') {
1255 *dir = malloc(2);
1256 if (!*dir) {
1257 ret = -ENOMEM;
Richard Genoud8cf2f022020-11-03 12:11:03 +01001258 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001259 }
1260
1261 (*dir)[0] = '/';
1262 (*dir)[1] = '\0';
1263 } else {
1264 *dir = strdup(dname);
1265 if (!*dir) {
1266 ret = -ENOMEM;
Richard Genoud8cf2f022020-11-03 12:11:03 +01001267 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001268 }
1269 }
1270
Richard Genoud8cf2f022020-11-03 12:11:03 +01001271out:
1272 if (ret) {
1273 free(*file);
1274 free(*dir);
1275 *dir = NULL;
1276 *file = NULL;
1277 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001278 free(basec);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001279 free(dirc);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001280 free(tmp_path);
1281
1282 return ret;
1283}
1284
1285static int sqfs_get_regfile_info(struct squashfs_reg_inode *reg,
1286 struct squashfs_file_info *finfo,
1287 struct squashfs_fragment_block_entry *fentry,
1288 __le32 blksz)
1289{
1290 int datablk_count = 0, ret;
1291
1292 finfo->size = get_unaligned_le32(&reg->file_size);
1293 finfo->offset = get_unaligned_le32(&reg->offset);
1294 finfo->start = get_unaligned_le32(&reg->start_block);
1295 finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(&reg->fragment));
1296
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +02001297 if (finfo->frag && finfo->offset == 0xFFFFFFFF)
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +02001298 return -EINVAL;
1299
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +02001300 if (finfo->size < 1 || finfo->start == 0xFFFFFFFF)
1301 return -EINVAL;
1302
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001303 if (finfo->frag) {
1304 datablk_count = finfo->size / le32_to_cpu(blksz);
1305 ret = sqfs_frag_lookup(get_unaligned_le32(&reg->fragment),
1306 fentry);
1307 if (ret < 0)
1308 return -EINVAL;
Joao Marcos Costa8a724fa2021-05-17 18:20:38 -03001309 finfo->comp = ret;
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +02001310 if (fentry->size < 1 || fentry->start == 0x7FFFFFFF)
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +02001311 return -EINVAL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001312 } else {
1313 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1314 }
1315
1316 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1317 if (!finfo->blk_sizes)
1318 return -ENOMEM;
1319
1320 return datablk_count;
1321}
1322
1323static int sqfs_get_lregfile_info(struct squashfs_lreg_inode *lreg,
1324 struct squashfs_file_info *finfo,
1325 struct squashfs_fragment_block_entry *fentry,
1326 __le32 blksz)
1327{
1328 int datablk_count = 0, ret;
1329
1330 finfo->size = get_unaligned_le64(&lreg->file_size);
1331 finfo->offset = get_unaligned_le32(&lreg->offset);
1332 finfo->start = get_unaligned_le64(&lreg->start_block);
1333 finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(&lreg->fragment));
1334
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +02001335 if (finfo->frag && finfo->offset == 0xFFFFFFFF)
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +02001336 return -EINVAL;
1337
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +02001338 if (finfo->size < 1 || finfo->start == 0x7FFFFFFF)
1339 return -EINVAL;
1340
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001341 if (finfo->frag) {
1342 datablk_count = finfo->size / le32_to_cpu(blksz);
1343 ret = sqfs_frag_lookup(get_unaligned_le32(&lreg->fragment),
1344 fentry);
1345 if (ret < 0)
1346 return -EINVAL;
Joao Marcos Costa8a724fa2021-05-17 18:20:38 -03001347 finfo->comp = ret;
Joao Marcos Costaf1dfe4a2020-09-11 12:21:06 +02001348 if (fentry->size < 1 || fentry->start == 0x7FFFFFFF)
Joao Marcos Costab7fb3c32020-08-19 18:28:41 +02001349 return -EINVAL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001350 } else {
1351 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1352 }
1353
1354 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1355 if (!finfo->blk_sizes)
1356 return -ENOMEM;
1357
1358 return datablk_count;
1359}
1360
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001361static int sqfs_read_nest(const char *filename, void *buf, loff_t offset,
1362 loff_t len, loff_t *actread)
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001363{
Heinrich Schuchardt8e709432022-04-11 22:54:44 +02001364 char *dir = NULL, *fragment_block, *datablock = NULL;
Richard Genoud0caee472020-11-03 12:11:18 +01001365 char *fragment = NULL, *file = NULL, *resolved, *data;
Campbell Suter46446632020-11-23 15:40:03 +13001366 u64 start, n_blks, table_size, data_offset, table_offset, sparse_size;
Joao Marcos Costab87fd012020-08-18 17:17:22 +02001367 int ret, j, i_number, datablk_count = 0;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001368 struct squashfs_super_block *sblk = ctxt.sblk;
1369 struct squashfs_fragment_block_entry frag_entry;
1370 struct squashfs_file_info finfo = {0};
1371 struct squashfs_symlink_inode *symlink;
1372 struct fs_dir_stream *dirsp = NULL;
1373 struct squashfs_dir_stream *dirs;
1374 struct squashfs_lreg_inode *lreg;
1375 struct squashfs_base_inode *base;
1376 struct squashfs_reg_inode *reg;
1377 unsigned long dest_len;
1378 struct fs_dirent *dent;
1379 unsigned char *ipos;
Joao Marcos Costa652277d2025-02-19 11:16:33 +01001380 size_t buf_size;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001381
1382 *actread = 0;
1383
Richard Genoudcfcb2242020-11-03 12:11:24 +01001384 if (offset) {
1385 /*
1386 * TODO: implement reading at an offset in file
1387 */
1388 printf("Error: reading at a specific offset in a squashfs file is not supported yet.\n");
1389 return -EINVAL;
1390 }
1391
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001392 /*
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001393 * sqfs_opendir_nest will uncompress inode and directory tables, and will
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001394 * return a pointer to the directory that contains the requested file.
1395 */
1396 sqfs_split_path(&file, &dir, filename);
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001397 ret = sqfs_opendir_nest(dir, &dirsp);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001398 if (ret) {
Richard Genoud0caee472020-11-03 12:11:18 +01001399 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001400 }
1401
1402 dirs = (struct squashfs_dir_stream *)dirsp;
1403
1404 /* For now, only regular files are able to be loaded */
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001405 while (!sqfs_readdir_nest(dirsp, &dent)) {
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001406 ret = strcmp(dent->name, file);
1407 if (!ret)
1408 break;
1409
1410 free(dirs->entry);
Richard Genoud0b8aa992020-11-03 12:11:11 +01001411 dirs->entry = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001412 }
1413
1414 if (ret) {
1415 printf("File not found.\n");
1416 *actread = 0;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001417 ret = -ENOENT;
Richard Genoud0caee472020-11-03 12:11:18 +01001418 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001419 }
1420
1421 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1422 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1423 sblk->block_size);
Richard Weinbergere8db3d92024-08-02 18:36:46 +02001424 if (!ipos) {
1425 ret = -EINVAL;
1426 goto out;
1427 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001428
1429 base = (struct squashfs_base_inode *)ipos;
1430 switch (get_unaligned_le16(&base->inode_type)) {
1431 case SQFS_REG_TYPE:
1432 reg = (struct squashfs_reg_inode *)ipos;
1433 datablk_count = sqfs_get_regfile_info(reg, &finfo, &frag_entry,
1434 sblk->block_size);
1435 if (datablk_count < 0) {
1436 ret = -EINVAL;
Richard Genoud0caee472020-11-03 12:11:18 +01001437 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001438 }
1439
1440 memcpy(finfo.blk_sizes, ipos + sizeof(*reg),
1441 datablk_count * sizeof(u32));
1442 break;
1443 case SQFS_LREG_TYPE:
1444 lreg = (struct squashfs_lreg_inode *)ipos;
1445 datablk_count = sqfs_get_lregfile_info(lreg, &finfo,
1446 &frag_entry,
1447 sblk->block_size);
1448 if (datablk_count < 0) {
1449 ret = -EINVAL;
Richard Genoud0caee472020-11-03 12:11:18 +01001450 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001451 }
1452
1453 memcpy(finfo.blk_sizes, ipos + sizeof(*lreg),
1454 datablk_count * sizeof(u32));
1455 break;
1456 case SQFS_SYMLINK_TYPE:
1457 case SQFS_LSYMLINK_TYPE:
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001458 if (++symlinknest == MAX_SYMLINK_NEST) {
1459 ret = -ELOOP;
1460 goto out;
1461 }
1462
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001463 symlink = (struct squashfs_symlink_inode *)ipos;
1464 resolved = sqfs_resolve_symlink(symlink, filename);
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001465 ret = sqfs_read_nest(resolved, buf, offset, len, actread);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001466 free(resolved);
Richard Genoud0caee472020-11-03 12:11:18 +01001467 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001468 case SQFS_BLKDEV_TYPE:
1469 case SQFS_CHRDEV_TYPE:
1470 case SQFS_LBLKDEV_TYPE:
1471 case SQFS_LCHRDEV_TYPE:
1472 case SQFS_FIFO_TYPE:
1473 case SQFS_SOCKET_TYPE:
1474 case SQFS_LFIFO_TYPE:
1475 case SQFS_LSOCKET_TYPE:
1476 default:
1477 printf("Unsupported entry type\n");
1478 ret = -EINVAL;
Richard Genoud0caee472020-11-03 12:11:18 +01001479 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001480 }
1481
1482 /* If the user specifies a length, check its sanity */
1483 if (len) {
1484 if (len > finfo.size) {
1485 ret = -EINVAL;
Richard Genoud0caee472020-11-03 12:11:18 +01001486 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001487 }
1488
1489 finfo.size = len;
Richard Genoud6abdf012020-11-03 12:11:23 +01001490 } else {
1491 len = finfo.size;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001492 }
1493
1494 if (datablk_count) {
1495 data_offset = finfo.start;
1496 datablock = malloc(get_unaligned_le32(&sblk->block_size));
1497 if (!datablock) {
1498 ret = -ENOMEM;
Richard Genoud0caee472020-11-03 12:11:18 +01001499 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001500 }
1501 }
1502
1503 for (j = 0; j < datablk_count; j++) {
Heinrich Schuchardt8e709432022-04-11 22:54:44 +02001504 char *data_buffer;
1505
Sean Nyekjaera8eb9622022-05-12 20:37:14 +02001506 start = lldiv(data_offset, ctxt.cur_dev->blksz);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001507 table_size = SQFS_BLOCK_SIZE(finfo.blk_sizes[j]);
1508 table_offset = data_offset - (start * ctxt.cur_dev->blksz);
1509 n_blks = DIV_ROUND_UP(table_size + table_offset,
1510 ctxt.cur_dev->blksz);
1511
Campbell Suter46446632020-11-23 15:40:03 +13001512 /* Don't load any data for sparse blocks */
1513 if (finfo.blk_sizes[j] == 0) {
1514 n_blks = 0;
1515 table_offset = 0;
1516 data_buffer = NULL;
1517 data = NULL;
1518 } else {
1519 data_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001520
Campbell Suter46446632020-11-23 15:40:03 +13001521 if (!data_buffer) {
1522 ret = -ENOMEM;
1523 goto out;
1524 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001525
Campbell Suter46446632020-11-23 15:40:03 +13001526 ret = sqfs_disk_read(start, n_blks, data_buffer);
1527 if (ret < 0) {
1528 /*
1529 * Possible causes: too many data blocks or too large
1530 * SquashFS block size. Tip: re-compile the SquashFS
1531 * image with mksquashfs's -b <block_size> option.
1532 */
1533 printf("Error: too many data blocks to be read.\n");
1534 goto out;
1535 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001536
Campbell Suter46446632020-11-23 15:40:03 +13001537 data = data_buffer + table_offset;
1538 }
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001539
1540 /* Load the data */
Campbell Suter46446632020-11-23 15:40:03 +13001541 if (finfo.blk_sizes[j] == 0) {
1542 /* This is a sparse block */
1543 sparse_size = get_unaligned_le32(&sblk->block_size);
1544 if ((*actread + sparse_size) > len)
1545 sparse_size = len - *actread;
1546 memset(buf + *actread, 0, sparse_size);
1547 *actread += sparse_size;
1548 } else if (SQFS_COMPRESSED_BLOCK(finfo.blk_sizes[j])) {
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001549 dest_len = get_unaligned_le32(&sblk->block_size);
Joao Marcos Costab87fd012020-08-18 17:17:22 +02001550 ret = sqfs_decompress(&ctxt, datablock, &dest_len,
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001551 data, table_size);
1552 if (ret)
Richard Genoud0caee472020-11-03 12:11:18 +01001553 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001554
Richard Genoud6abdf012020-11-03 12:11:23 +01001555 if ((*actread + dest_len) > len)
1556 dest_len = len - *actread;
Richard Genoudcfcb2242020-11-03 12:11:24 +01001557 memcpy(buf + *actread, datablock, dest_len);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001558 *actread += dest_len;
1559 } else {
Richard Genoud6abdf012020-11-03 12:11:23 +01001560 if ((*actread + table_size) > len)
1561 table_size = len - *actread;
Richard Genoudcfcb2242020-11-03 12:11:24 +01001562 memcpy(buf + *actread, data, table_size);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001563 *actread += table_size;
1564 }
1565
1566 data_offset += table_size;
Heinrich Schuchardt8e709432022-04-11 22:54:44 +02001567 free(data_buffer);
Richard Genoud6abdf012020-11-03 12:11:23 +01001568 if (*actread >= len)
1569 break;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001570 }
1571
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001572 /*
1573 * There is no need to continue if the file is not fragmented.
1574 */
1575 if (!finfo.frag) {
1576 ret = 0;
Richard Genoud0caee472020-11-03 12:11:18 +01001577 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001578 }
1579
Sean Nyekjaera8eb9622022-05-12 20:37:14 +02001580 start = lldiv(frag_entry.start, ctxt.cur_dev->blksz);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001581 table_size = SQFS_BLOCK_SIZE(frag_entry.size);
1582 table_offset = frag_entry.start - (start * ctxt.cur_dev->blksz);
1583 n_blks = DIV_ROUND_UP(table_size + table_offset, ctxt.cur_dev->blksz);
1584
Joao Marcos Costa652277d2025-02-19 11:16:33 +01001585 if (__builtin_mul_overflow(n_blks, ctxt.cur_dev->blksz, &buf_size))
1586 return -EINVAL;
1587
1588 fragment = malloc_cache_aligned(buf_size);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001589
1590 if (!fragment) {
1591 ret = -ENOMEM;
Richard Genoud0caee472020-11-03 12:11:18 +01001592 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001593 }
1594
1595 ret = sqfs_disk_read(start, n_blks, fragment);
1596 if (ret < 0)
Richard Genoud0caee472020-11-03 12:11:18 +01001597 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001598
1599 /* File compressed and fragmented */
1600 if (finfo.frag && finfo.comp) {
1601 dest_len = get_unaligned_le32(&sblk->block_size);
1602 fragment_block = malloc(dest_len);
1603 if (!fragment_block) {
1604 ret = -ENOMEM;
Richard Genoud0caee472020-11-03 12:11:18 +01001605 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001606 }
1607
Joao Marcos Costab87fd012020-08-18 17:17:22 +02001608 ret = sqfs_decompress(&ctxt, fragment_block, &dest_len,
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001609 (void *)fragment + table_offset,
1610 frag_entry.size);
1611 if (ret) {
1612 free(fragment_block);
Richard Genoud0caee472020-11-03 12:11:18 +01001613 goto out;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001614 }
1615
Joao Marcos Costa8a724fa2021-05-17 18:20:38 -03001616 memcpy(buf + *actread, &fragment_block[finfo.offset], finfo.size - *actread);
1617 *actread = finfo.size;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001618
1619 free(fragment_block);
1620
1621 } else if (finfo.frag && !finfo.comp) {
1622 fragment_block = (void *)fragment + table_offset;
1623
Joao Marcos Costa8a724fa2021-05-17 18:20:38 -03001624 memcpy(buf + *actread, &fragment_block[finfo.offset], finfo.size - *actread);
1625 *actread = finfo.size;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001626 }
1627
Richard Genoud0caee472020-11-03 12:11:18 +01001628out:
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001629 free(fragment);
Heinrich Schuchardt8e709432022-04-11 22:54:44 +02001630 free(datablock);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001631 free(file);
1632 free(dir);
Richard Genoud0caee472020-11-03 12:11:18 +01001633 free(finfo.blk_sizes);
Richard Genoud84a5f572020-11-03 12:11:13 +01001634 sqfs_closedir(dirsp);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001635
1636 return ret;
1637}
1638
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001639int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
1640 loff_t *actread)
1641{
1642 symlinknest = 0;
1643 return sqfs_read_nest(filename, buf, offset, len, actread);
1644}
1645
1646static int sqfs_size_nest(const char *filename, loff_t *size)
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001647{
1648 struct squashfs_super_block *sblk = ctxt.sblk;
1649 struct squashfs_symlink_inode *symlink;
1650 struct fs_dir_stream *dirsp = NULL;
1651 struct squashfs_base_inode *base;
1652 struct squashfs_dir_stream *dirs;
1653 struct squashfs_lreg_inode *lreg;
1654 struct squashfs_reg_inode *reg;
1655 char *dir, *file, *resolved;
1656 struct fs_dirent *dent;
1657 unsigned char *ipos;
1658 int ret, i_number;
1659
1660 sqfs_split_path(&file, &dir, filename);
1661 /*
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001662 * sqfs_opendir_nest will uncompress inode and directory tables, and will
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001663 * return a pointer to the directory that contains the requested file.
1664 */
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001665 ret = sqfs_opendir_nest(dir, &dirsp);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001666 if (ret) {
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001667 ret = -EINVAL;
1668 goto free_strings;
1669 }
1670
1671 dirs = (struct squashfs_dir_stream *)dirsp;
1672
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001673 while (!sqfs_readdir_nest(dirsp, &dent)) {
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001674 ret = strcmp(dent->name, file);
1675 if (!ret)
1676 break;
1677 free(dirs->entry);
Richard Genoudc3dadcb2020-11-03 12:11:09 +01001678 dirs->entry = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001679 }
1680
1681 if (ret) {
1682 printf("File not found.\n");
1683 *size = 0;
1684 ret = -EINVAL;
1685 goto free_strings;
1686 }
1687
1688 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1689 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1690 sblk->block_size);
Richard Weinbergere8db3d92024-08-02 18:36:46 +02001691
1692 if (!ipos) {
1693 *size = 0;
1694 ret = -EINVAL;
1695 goto free_strings;
1696 }
1697
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001698 free(dirs->entry);
Richard Genoudc3dadcb2020-11-03 12:11:09 +01001699 dirs->entry = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001700
1701 base = (struct squashfs_base_inode *)ipos;
1702 switch (get_unaligned_le16(&base->inode_type)) {
1703 case SQFS_REG_TYPE:
1704 reg = (struct squashfs_reg_inode *)ipos;
1705 *size = get_unaligned_le32(&reg->file_size);
1706 break;
1707 case SQFS_LREG_TYPE:
1708 lreg = (struct squashfs_lreg_inode *)ipos;
1709 *size = get_unaligned_le64(&lreg->file_size);
1710 break;
1711 case SQFS_SYMLINK_TYPE:
1712 case SQFS_LSYMLINK_TYPE:
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001713 if (++symlinknest == MAX_SYMLINK_NEST) {
1714 *size = 0;
1715 return -ELOOP;
1716 }
1717
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001718 symlink = (struct squashfs_symlink_inode *)ipos;
1719 resolved = sqfs_resolve_symlink(symlink, filename);
1720 ret = sqfs_size(resolved, size);
1721 free(resolved);
1722 break;
1723 case SQFS_BLKDEV_TYPE:
1724 case SQFS_CHRDEV_TYPE:
1725 case SQFS_LBLKDEV_TYPE:
1726 case SQFS_LCHRDEV_TYPE:
1727 case SQFS_FIFO_TYPE:
1728 case SQFS_SOCKET_TYPE:
1729 case SQFS_LFIFO_TYPE:
1730 case SQFS_LSOCKET_TYPE:
1731 default:
1732 printf("Unable to recover entry's size.\n");
1733 *size = 0;
1734 ret = -EINVAL;
1735 break;
1736 }
1737
1738free_strings:
1739 free(dir);
1740 free(file);
1741
1742 sqfs_closedir(dirsp);
1743
1744 return ret;
1745}
1746
Richard Genoudcfca20c2020-11-03 12:11:26 +01001747int sqfs_exists(const char *filename)
1748{
1749 struct fs_dir_stream *dirsp = NULL;
1750 struct squashfs_dir_stream *dirs;
1751 char *dir, *file;
1752 struct fs_dirent *dent;
1753 int ret;
1754
1755 sqfs_split_path(&file, &dir, filename);
1756 /*
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001757 * sqfs_opendir_nest will uncompress inode and directory tables, and will
Richard Genoudcfca20c2020-11-03 12:11:26 +01001758 * return a pointer to the directory that contains the requested file.
1759 */
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001760 symlinknest = 0;
1761 ret = sqfs_opendir_nest(dir, &dirsp);
Richard Genoudcfca20c2020-11-03 12:11:26 +01001762 if (ret) {
1763 ret = -EINVAL;
1764 goto free_strings;
1765 }
1766
1767 dirs = (struct squashfs_dir_stream *)dirsp;
1768
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001769 while (!sqfs_readdir_nest(dirsp, &dent)) {
Richard Genoudcfca20c2020-11-03 12:11:26 +01001770 ret = strcmp(dent->name, file);
1771 if (!ret)
1772 break;
1773 free(dirs->entry);
1774 dirs->entry = NULL;
1775 }
1776
1777 sqfs_closedir(dirsp);
1778
1779free_strings:
1780 free(dir);
1781 free(file);
1782
1783 return ret == 0;
1784}
1785
Richard Weinberger8d42c7b2024-08-02 18:36:47 +02001786int sqfs_size(const char *filename, loff_t *size)
1787{
1788 symlinknest = 0;
1789 return sqfs_size_nest(filename, size);
1790}
1791
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001792void sqfs_close(void)
1793{
Richard Genoud1d95a1e2020-11-24 18:07:52 +01001794 sqfs_decompressor_cleanup(&ctxt);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001795 free(ctxt.sblk);
Richard Genoud1d95a1e2020-11-24 18:07:52 +01001796 ctxt.sblk = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001797 ctxt.cur_dev = NULL;
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001798}
1799
1800void sqfs_closedir(struct fs_dir_stream *dirs)
1801{
1802 struct squashfs_dir_stream *sqfs_dirs;
1803
Heinrich Schuchardt836bba42021-02-01 03:28:48 +01001804 if (!dirs)
1805 return;
1806
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001807 sqfs_dirs = (struct squashfs_dir_stream *)dirs;
1808 free(sqfs_dirs->inode_table);
1809 free(sqfs_dirs->dir_table);
1810 free(sqfs_dirs->dir_header);
Richard Genoud2575eb22020-11-03 12:11:02 +01001811 free(sqfs_dirs);
Joao Marcos Costa29da3742020-07-30 15:33:47 +02001812}