blob: 5d9c52af80baeb4168cd2b169d8a8b3b27214ace [file] [log] [blame]
Joao Marcos Costac5100612020-07-30 15:33:47 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2020 Bootlin
4 *
5 * Author: Joao Marcos Costa <joaomarcos.costa@bootlin.com>
6 *
7 * sqfs.c: SquashFS filesystem implementation
8 */
9
10#include <asm/unaligned.h>
11#include <errno.h>
12#include <fs.h>
13#include <linux/types.h>
14#include <linux/byteorder/little_endian.h>
15#include <linux/byteorder/generic.h>
16#include <memalign.h>
17#include <stdlib.h>
18#include <string.h>
19#include <squashfs.h>
20#include <part.h>
21
22#include "sqfs_decompressor.h"
23#include "sqfs_filesystem.h"
24#include "sqfs_utils.h"
25
Joao Marcos Costac5100612020-07-30 15:33:47 +020026static struct squashfs_ctxt ctxt;
27
28static int sqfs_disk_read(__u32 block, __u32 nr_blocks, void *buf)
29{
30 ulong ret;
31
32 if (!ctxt.cur_dev)
33 return -1;
34
35 ret = blk_dread(ctxt.cur_dev, ctxt.cur_part_info.start + block,
36 nr_blocks, buf);
37
38 if (ret != nr_blocks)
39 return -1;
40
41 return ret;
42}
43
44static int sqfs_read_sblk(struct squashfs_super_block **sblk)
45{
46 *sblk = malloc_cache_aligned(ctxt.cur_dev->blksz);
47 if (!*sblk)
48 return -ENOMEM;
49
50 if (sqfs_disk_read(0, 1, *sblk) != 1) {
51 free(*sblk);
Richard Genoud7e932ac2020-11-24 18:07:52 +010052 sblk = NULL;
Joao Marcos Costac5100612020-07-30 15:33:47 +020053 return -EINVAL;
54 }
55
56 return 0;
57}
58
59static int sqfs_count_tokens(const char *filename)
60{
61 int token_count = 1, l;
62
63 for (l = 1; l < strlen(filename); l++) {
64 if (filename[l] == '/')
65 token_count++;
66 }
67
68 /* Ignore trailing '/' in path */
69 if (filename[strlen(filename) - 1] == '/')
70 token_count--;
71
72 if (!token_count)
73 token_count = 1;
74
75 return token_count;
76}
77
78/*
79 * Calculates how many blocks are needed for the buffer used in sqfs_disk_read.
80 * The memory section (e.g. inode table) start offset and its end (i.e. the next
81 * table start) must be specified. It also calculates the offset from which to
82 * start reading the buffer.
83 */
84static int sqfs_calc_n_blks(__le64 start, __le64 end, u64 *offset)
85{
86 u64 start_, table_size;
87
88 table_size = le64_to_cpu(end) - le64_to_cpu(start);
89 start_ = le64_to_cpu(start) / ctxt.cur_dev->blksz;
90 *offset = le64_to_cpu(start) - (start_ * ctxt.cur_dev->blksz);
91
92 return DIV_ROUND_UP(table_size + *offset, ctxt.cur_dev->blksz);
93}
94
95/*
96 * Retrieves fragment block entry and returns true if the fragment block is
97 * compressed
98 */
99static int sqfs_frag_lookup(u32 inode_fragment_index,
100 struct squashfs_fragment_block_entry *e)
101{
102 u64 start, n_blks, src_len, table_offset, start_block;
103 unsigned char *metadata_buffer, *metadata, *table;
104 struct squashfs_fragment_block_entry *entries;
105 struct squashfs_super_block *sblk = ctxt.sblk;
106 unsigned long dest_len;
107 int block, offset, ret;
Joao Marcos Costacdc11442020-08-18 17:17:22 +0200108 u16 header;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200109
Richard Genoudc9b8e862020-11-03 12:11:15 +0100110 metadata_buffer = NULL;
111 entries = NULL;
112 table = NULL;
113
Joao Marcos Costac5100612020-07-30 15:33:47 +0200114 if (inode_fragment_index >= get_unaligned_le32(&sblk->fragments))
115 return -EINVAL;
116
117 start = get_unaligned_le64(&sblk->fragment_table_start) /
118 ctxt.cur_dev->blksz;
119 n_blks = sqfs_calc_n_blks(sblk->fragment_table_start,
120 sblk->export_table_start,
121 &table_offset);
122
123 /* Allocate a proper sized buffer to store the fragment index table */
124 table = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
Richard Genoudc9b8e862020-11-03 12:11:15 +0100125 if (!table) {
126 ret = -ENOMEM;
127 goto out;
128 }
Joao Marcos Costac5100612020-07-30 15:33:47 +0200129
130 if (sqfs_disk_read(start, n_blks, table) < 0) {
Richard Genoudc9b8e862020-11-03 12:11:15 +0100131 ret = -EINVAL;
132 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200133 }
134
135 block = SQFS_FRAGMENT_INDEX(inode_fragment_index);
136 offset = SQFS_FRAGMENT_INDEX_OFFSET(inode_fragment_index);
137
138 /*
139 * Get the start offset of the metadata block that contains the right
140 * fragment block entry
141 */
142 start_block = get_unaligned_le64(table + table_offset + block *
143 sizeof(u64));
144
145 start = start_block / ctxt.cur_dev->blksz;
146 n_blks = sqfs_calc_n_blks(cpu_to_le64(start_block),
147 sblk->fragment_table_start, &table_offset);
148
149 metadata_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
150 if (!metadata_buffer) {
151 ret = -ENOMEM;
Richard Genoudc9b8e862020-11-03 12:11:15 +0100152 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200153 }
154
155 if (sqfs_disk_read(start, n_blks, metadata_buffer) < 0) {
156 ret = -EINVAL;
Richard Genoudc9b8e862020-11-03 12:11:15 +0100157 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200158 }
159
160 /* Every metadata block starts with a 16-bit header */
161 header = get_unaligned_le16(metadata_buffer + table_offset);
162 metadata = metadata_buffer + table_offset + SQFS_HEADER_SIZE;
163
Joao Marcos Costaa7dc37d2020-09-11 12:21:06 +0200164 if (!metadata || !header) {
Joao Marcos Costac9875a52020-08-19 18:28:41 +0200165 ret = -ENOMEM;
Richard Genoudc9b8e862020-11-03 12:11:15 +0100166 goto out;
Joao Marcos Costac9875a52020-08-19 18:28:41 +0200167 }
168
Joao Marcos Costac5100612020-07-30 15:33:47 +0200169 entries = malloc(SQFS_METADATA_BLOCK_SIZE);
170 if (!entries) {
171 ret = -ENOMEM;
Richard Genoudc9b8e862020-11-03 12:11:15 +0100172 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200173 }
174
175 if (SQFS_COMPRESSED_METADATA(header)) {
176 src_len = SQFS_METADATA_SIZE(header);
177 dest_len = SQFS_METADATA_BLOCK_SIZE;
Joao Marcos Costacdc11442020-08-18 17:17:22 +0200178 ret = sqfs_decompress(&ctxt, entries, &dest_len, metadata,
Joao Marcos Costac5100612020-07-30 15:33:47 +0200179 src_len);
180 if (ret) {
181 ret = -EINVAL;
Richard Genoudc9b8e862020-11-03 12:11:15 +0100182 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200183 }
184 } else {
185 memcpy(entries, metadata, SQFS_METADATA_SIZE(header));
186 }
187
188 *e = entries[offset];
189 ret = SQFS_COMPRESSED_BLOCK(e->size);
190
Richard Genoudc9b8e862020-11-03 12:11:15 +0100191out:
Joao Marcos Costac5100612020-07-30 15:33:47 +0200192 free(entries);
Joao Marcos Costac5100612020-07-30 15:33:47 +0200193 free(metadata_buffer);
Joao Marcos Costac5100612020-07-30 15:33:47 +0200194 free(table);
195
196 return ret;
197}
198
199/*
200 * The entry name is a flexible array member, and we don't know its size before
201 * actually reading the entry. So we need a first copy to retrieve this size so
202 * we can finally copy the whole struct.
203 */
204static int sqfs_read_entry(struct squashfs_directory_entry **dest, void *src)
205{
206 struct squashfs_directory_entry *tmp;
207 u16 sz;
208
209 tmp = src;
210 sz = get_unaligned_le16(src + sizeof(*tmp) - sizeof(u16));
211 /*
212 * 'src' points to the begin of a directory entry, and 'sz' gets its
213 * 'name_size' member's value. name_size is actually the string
214 * length - 1, so adding 2 compensates this difference and adds space
215 * for the trailling null byte.
216 */
217 *dest = malloc(sizeof(*tmp) + sz + 2);
218 if (!*dest)
219 return -ENOMEM;
220
221 memcpy(*dest, src, sizeof(*tmp) + sz + 1);
222 (*dest)->name[sz + 1] = '\0';
223
224 return 0;
225}
226
227static int sqfs_get_tokens_length(char **tokens, int count)
228{
229 int length = 0, i;
230
231 /*
232 * 1 is added to the result of strlen to consider the slash separator
233 * between the tokens.
234 */
235 for (i = 0; i < count; i++)
236 length += strlen(tokens[i]) + 1;
237
238 return length;
239}
240
241/* Takes a token list and returns a single string with '/' as separator. */
242static char *sqfs_concat_tokens(char **token_list, int token_count)
243{
244 char *result;
245 int i, length = 0, offset = 0;
246
247 length = sqfs_get_tokens_length(token_list, token_count);
248
249 result = malloc(length + 1);
Richard Genouddc3312c2020-11-03 12:11:08 +0100250 if (!result)
251 return NULL;
252
Joao Marcos Costac5100612020-07-30 15:33:47 +0200253 result[length] = '\0';
254
255 for (i = 0; i < token_count; i++) {
256 strcpy(result + offset, token_list[i]);
257 offset += strlen(token_list[i]);
258 result[offset++] = '/';
259 }
260
261 return result;
262}
263
264/*
265 * Differently from sqfs_concat_tokens, sqfs_join writes the result into a
266 * previously allocated string, and returns the number of bytes written.
267 */
268static int sqfs_join(char **strings, char *dest, int start, int end,
269 char separator)
270{
271 int i, offset = 0;
272
273 for (i = start; i < end; i++) {
274 strcpy(dest + offset, strings[i]);
275 offset += strlen(strings[i]);
276 if (i < end - 1)
277 dest[offset++] = separator;
278 }
279
280 return offset;
281}
282
283/*
284 * Fills the given token list using its size (count) and a source string (str)
285 */
286static int sqfs_tokenize(char **tokens, int count, const char *str)
287{
Joao Marcos Costac9875a52020-08-19 18:28:41 +0200288 int i, j, ret = 0;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200289 char *aux, *strc;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200290
291 strc = strdup(str);
292 if (!strc)
293 return -ENOMEM;
294
295 if (!strcmp(strc, "/")) {
296 tokens[0] = strdup(strc);
297 if (!tokens[0]) {
Joao Marcos Costac9875a52020-08-19 18:28:41 +0200298 ret = -ENOMEM;
299 goto free_strc;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200300 }
301 } else {
302 for (j = 0; j < count; j++) {
303 aux = strtok(!j ? strc : NULL, "/");
304 tokens[j] = strdup(aux);
305 if (!tokens[j]) {
306 for (i = 0; i < j; i++)
307 free(tokens[i]);
Joao Marcos Costac9875a52020-08-19 18:28:41 +0200308 ret = -ENOMEM;
309 goto free_strc;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200310 }
311 }
312 }
313
Joao Marcos Costac9875a52020-08-19 18:28:41 +0200314free_strc:
Joao Marcos Costac5100612020-07-30 15:33:47 +0200315 free(strc);
316
Joao Marcos Costac9875a52020-08-19 18:28:41 +0200317 return ret;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200318}
319
320/*
321 * Remove last 'updir + 1' tokens from the base path tokens list. This leaves us
322 * with a token list containing only the tokens needed to form the resolved
323 * path, and returns the decremented size of the token list.
324 */
325static int sqfs_clean_base_path(char **base, int count, int updir)
326{
327 int i;
328
329 for (i = count - updir - 1; i < count; i++)
330 free(base[i]);
331
332 return count - updir - 1;
333}
334
335/*
336 * Given the base ("current dir.") path and the relative one, generate the
337 * absolute path.
338 */
339static char *sqfs_get_abs_path(const char *base, const char *rel)
340{
341 char **base_tokens, **rel_tokens, *resolved = NULL;
342 int ret, bc, rc, i, updir = 0, resolved_size = 0, offset = 0;
343
Richard Genoud33686802020-11-03 12:11:17 +0100344 base_tokens = NULL;
345 rel_tokens = NULL;
346
Joao Marcos Costac5100612020-07-30 15:33:47 +0200347 /* Memory allocation for the token lists */
348 bc = sqfs_count_tokens(base);
349 rc = sqfs_count_tokens(rel);
350 if (bc < 1 || rc < 1)
351 return NULL;
352
Richard Genoud33686802020-11-03 12:11:17 +0100353 base_tokens = calloc(bc, sizeof(char *));
Joao Marcos Costac5100612020-07-30 15:33:47 +0200354 if (!base_tokens)
355 return NULL;
356
Richard Genoud33686802020-11-03 12:11:17 +0100357 rel_tokens = calloc(rc, sizeof(char *));
Joao Marcos Costac5100612020-07-30 15:33:47 +0200358 if (!rel_tokens)
Richard Genoud33686802020-11-03 12:11:17 +0100359 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200360
361 /* Fill token lists */
362 ret = sqfs_tokenize(base_tokens, bc, base);
363 if (ret)
Richard Genoud33686802020-11-03 12:11:17 +0100364 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200365
Richard Genoud53db0e22020-11-03 12:11:16 +0100366 ret = sqfs_tokenize(rel_tokens, rc, rel);
Joao Marcos Costac5100612020-07-30 15:33:47 +0200367 if (ret)
Richard Genoud33686802020-11-03 12:11:17 +0100368 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200369
370 /* count '..' occurrences in target path */
371 for (i = 0; i < rc; i++) {
372 if (!strcmp(rel_tokens[i], ".."))
373 updir++;
374 }
375
376 /* Remove the last token and the '..' occurrences */
377 bc = sqfs_clean_base_path(base_tokens, bc, updir);
378 if (bc < 0)
Richard Genoud33686802020-11-03 12:11:17 +0100379 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200380
381 /* Calculate resolved path size */
382 if (!bc)
383 resolved_size++;
384
385 resolved_size += sqfs_get_tokens_length(base_tokens, bc) +
386 sqfs_get_tokens_length(rel_tokens, rc);
387
388 resolved = malloc(resolved_size + 1);
389 if (!resolved)
Richard Genoud33686802020-11-03 12:11:17 +0100390 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200391
392 /* Set resolved path */
393 memset(resolved, '\0', resolved_size + 1);
394 offset += sqfs_join(base_tokens, resolved + offset, 0, bc, '/');
395 resolved[offset++] = '/';
396 offset += sqfs_join(rel_tokens, resolved + offset, updir, rc, '/');
397
Richard Genoud33686802020-11-03 12:11:17 +0100398out:
399 if (rel_tokens)
400 for (i = 0; i < rc; i++)
401 free(rel_tokens[i]);
402 if (base_tokens)
403 for (i = 0; i < bc; i++)
404 free(base_tokens[i]);
405
Joao Marcos Costac5100612020-07-30 15:33:47 +0200406 free(rel_tokens);
Joao Marcos Costac5100612020-07-30 15:33:47 +0200407 free(base_tokens);
408
409 return resolved;
410}
411
412static char *sqfs_resolve_symlink(struct squashfs_symlink_inode *sym,
413 const char *base_path)
414{
415 char *resolved, *target;
416 u32 sz;
417
418 sz = get_unaligned_le32(&sym->symlink_size);
419 target = malloc(sz + 1);
420 if (!target)
421 return NULL;
422
423 /*
424 * There is no trailling null byte in the symlink's target path, so a
425 * copy is made and a '\0' is added at its end.
426 */
427 target[sz] = '\0';
428 /* Get target name (relative path) */
429 strncpy(target, sym->symlink, sz);
430
431 /* Relative -> absolute path conversion */
432 resolved = sqfs_get_abs_path(base_path, target);
433
434 free(target);
435
436 return resolved;
437}
438
439/*
440 * m_list contains each metadata block's position, and m_count is the number of
441 * elements of m_list. Those metadata blocks come from the compressed directory
442 * table.
443 */
444static int sqfs_search_dir(struct squashfs_dir_stream *dirs, char **token_list,
445 int token_count, u32 *m_list, int m_count)
446{
447 struct squashfs_super_block *sblk = ctxt.sblk;
448 char *path, *target, **sym_tokens, *res, *rem;
Richard Genoudcd545912020-11-03 12:11:06 +0100449 int j, ret = 0, new_inode_number, offset;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200450 struct squashfs_symlink_inode *sym;
Joao Marcos Costaa7dc37d2020-09-11 12:21:06 +0200451 struct squashfs_ldir_inode *ldir;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200452 struct squashfs_dir_inode *dir;
453 struct fs_dir_stream *dirsp;
454 struct fs_dirent *dent;
455 unsigned char *table;
456
Richard Genoudcd545912020-11-03 12:11:06 +0100457 res = NULL;
458 rem = NULL;
459 path = NULL;
460 target = NULL;
461 sym_tokens = NULL;
462
Joao Marcos Costac5100612020-07-30 15:33:47 +0200463 dirsp = (struct fs_dir_stream *)dirs;
464
465 /* Start by root inode */
466 table = sqfs_find_inode(dirs->inode_table, le32_to_cpu(sblk->inodes),
467 sblk->inodes, sblk->block_size);
468
Joao Marcos Costac5100612020-07-30 15:33:47 +0200469 dir = (struct squashfs_dir_inode *)table;
Joao Marcos Costaa7dc37d2020-09-11 12:21:06 +0200470 ldir = (struct squashfs_ldir_inode *)table;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200471
472 /* get directory offset in directory table */
473 offset = sqfs_dir_offset(table, m_list, m_count);
474 dirs->table = &dirs->dir_table[offset];
475
476 /* Setup directory header */
477 dirs->dir_header = malloc(SQFS_DIR_HEADER_SIZE);
478 if (!dirs->dir_header)
479 return -ENOMEM;
480
481 memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE);
482
483 /* Initialize squashfs_dir_stream members */
484 dirs->table += SQFS_DIR_HEADER_SIZE;
485 dirs->size = get_unaligned_le16(&dir->file_size) - SQFS_DIR_HEADER_SIZE;
486 dirs->entry_count = dirs->dir_header->count + 1;
487
488 /* No path given -> root directory */
489 if (!strcmp(token_list[0], "/")) {
490 dirs->table = &dirs->dir_table[offset];
491 memcpy(&dirs->i_dir, dir, sizeof(*dir));
492 return 0;
493 }
494
495 for (j = 0; j < token_count; j++) {
496 if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) {
497 printf("** Cannot find directory. **\n");
Richard Genoudcd545912020-11-03 12:11:06 +0100498 ret = -EINVAL;
499 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200500 }
501
502 while (!sqfs_readdir(dirsp, &dent)) {
503 ret = strcmp(dent->name, token_list[j]);
504 if (!ret)
505 break;
506 free(dirs->entry);
Richard Genoud01e71ec2020-11-03 12:11:05 +0100507 dirs->entry = NULL;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200508 }
509
510 if (ret) {
511 printf("** Cannot find directory. **\n");
Richard Genoudcd545912020-11-03 12:11:06 +0100512 ret = -EINVAL;
513 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200514 }
515
516 /* Redefine inode as the found token */
517 new_inode_number = dirs->entry->inode_offset +
518 dirs->dir_header->inode_number;
519
520 /* Get reference to inode in the inode table */
521 table = sqfs_find_inode(dirs->inode_table, new_inode_number,
522 sblk->inodes, sblk->block_size);
523 dir = (struct squashfs_dir_inode *)table;
524
525 /* Check for symbolic link and inode type sanity */
526 if (get_unaligned_le16(&dir->inode_type) == SQFS_SYMLINK_TYPE) {
527 sym = (struct squashfs_symlink_inode *)table;
528 /* Get first j + 1 tokens */
529 path = sqfs_concat_tokens(token_list, j + 1);
Richard Genoudcd545912020-11-03 12:11:06 +0100530 if (!path) {
531 ret = -ENOMEM;
532 goto out;
533 }
Joao Marcos Costac5100612020-07-30 15:33:47 +0200534 /* Resolve for these tokens */
535 target = sqfs_resolve_symlink(sym, path);
Richard Genoudcd545912020-11-03 12:11:06 +0100536 if (!target) {
537 ret = -ENOMEM;
538 goto out;
539 }
Joao Marcos Costac5100612020-07-30 15:33:47 +0200540 /* Join remaining tokens */
541 rem = sqfs_concat_tokens(token_list + j + 1, token_count -
542 j - 1);
Richard Genoudcd545912020-11-03 12:11:06 +0100543 if (!rem) {
544 ret = -ENOMEM;
545 goto out;
546 }
Joao Marcos Costac5100612020-07-30 15:33:47 +0200547 /* Concatenate remaining tokens and symlink's target */
548 res = malloc(strlen(rem) + strlen(target) + 1);
Richard Genoudcd545912020-11-03 12:11:06 +0100549 if (!res) {
550 ret = -ENOMEM;
551 goto out;
552 }
Joao Marcos Costac5100612020-07-30 15:33:47 +0200553 strcpy(res, target);
554 res[strlen(target)] = '/';
555 strcpy(res + strlen(target) + 1, rem);
556 token_count = sqfs_count_tokens(res);
557
Richard Genoudcd545912020-11-03 12:11:06 +0100558 if (token_count < 0) {
559 ret = -EINVAL;
560 goto out;
561 }
Joao Marcos Costac5100612020-07-30 15:33:47 +0200562
563 sym_tokens = malloc(token_count * sizeof(char *));
Richard Genoudcd545912020-11-03 12:11:06 +0100564 if (!sym_tokens) {
565 ret = -EINVAL;
566 goto out;
567 }
Joao Marcos Costac5100612020-07-30 15:33:47 +0200568
569 /* Fill tokens list */
570 ret = sqfs_tokenize(sym_tokens, token_count, res);
Richard Genoudcd545912020-11-03 12:11:06 +0100571 if (ret) {
572 ret = -EINVAL;
573 goto out;
574 }
Joao Marcos Costac5100612020-07-30 15:33:47 +0200575 free(dirs->entry);
Richard Genoud01e71ec2020-11-03 12:11:05 +0100576 dirs->entry = NULL;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200577
578 ret = sqfs_search_dir(dirs, sym_tokens, token_count,
579 m_list, m_count);
Richard Genoudcd545912020-11-03 12:11:06 +0100580 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200581 } else if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) {
582 printf("** Cannot find directory. **\n");
583 free(dirs->entry);
Richard Genoud01e71ec2020-11-03 12:11:05 +0100584 dirs->entry = NULL;
Richard Genoudcd545912020-11-03 12:11:06 +0100585 ret = -EINVAL;
586 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200587 }
588
589 /* Check if it is an extended dir. */
590 if (get_unaligned_le16(&dir->inode_type) == SQFS_LDIR_TYPE)
591 ldir = (struct squashfs_ldir_inode *)table;
592
593 /* Get dir. offset into the directory table */
594 offset = sqfs_dir_offset(table, m_list, m_count);
595 dirs->table = &dirs->dir_table[offset];
596
597 /* Copy directory header */
598 memcpy(dirs->dir_header, &dirs->dir_table[offset],
599 SQFS_DIR_HEADER_SIZE);
600
601 /* Check for empty directory */
602 if (sqfs_is_empty_dir(table)) {
603 printf("Empty directory.\n");
604 free(dirs->entry);
Richard Genoud01e71ec2020-11-03 12:11:05 +0100605 dirs->entry = NULL;
Richard Genoudcd545912020-11-03 12:11:06 +0100606 ret = SQFS_EMPTY_DIR;
607 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200608 }
609
610 dirs->table += SQFS_DIR_HEADER_SIZE;
611 dirs->size = get_unaligned_le16(&dir->file_size);
612 dirs->entry_count = dirs->dir_header->count + 1;
613 dirs->size -= SQFS_DIR_HEADER_SIZE;
614 free(dirs->entry);
Richard Genoud01e71ec2020-11-03 12:11:05 +0100615 dirs->entry = NULL;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200616 }
617
618 offset = sqfs_dir_offset(table, m_list, m_count);
619 dirs->table = &dirs->dir_table[offset];
620
621 if (get_unaligned_le16(&dir->inode_type) == SQFS_DIR_TYPE)
622 memcpy(&dirs->i_dir, dir, sizeof(*dir));
623 else
624 memcpy(&dirs->i_ldir, ldir, sizeof(*ldir));
625
Richard Genoudcd545912020-11-03 12:11:06 +0100626out:
627 free(res);
628 free(rem);
629 free(path);
630 free(target);
631 free(sym_tokens);
632 return ret;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200633}
634
635/*
636 * Inode and directory tables are stored as a series of metadata blocks, and
637 * given the compressed size of this table, we can calculate how much metadata
638 * blocks are needed to store the result of the decompression, since a
639 * decompressed metadata block should have a size of 8KiB.
640 */
641static int sqfs_count_metablks(void *table, u32 offset, int table_size)
642{
643 int count = 0, cur_size = 0, ret;
644 u32 data_size;
645 bool comp;
646
647 do {
648 ret = sqfs_read_metablock(table, offset + cur_size, &comp,
649 &data_size);
650 if (ret)
651 return -EINVAL;
652 cur_size += data_size + SQFS_HEADER_SIZE;
653 count++;
654 } while (cur_size < table_size);
655
656 return count;
657}
658
659/*
660 * Storing the metadata blocks header's positions will be useful while looking
661 * for an entry in the directory table, using the reference (index and offset)
662 * given by its inode.
663 */
664static int sqfs_get_metablk_pos(u32 *pos_list, void *table, u32 offset,
665 int metablks_count)
666{
667 u32 data_size, cur_size = 0;
668 int j, ret = 0;
669 bool comp;
670
671 if (!metablks_count)
672 return -EINVAL;
673
674 for (j = 0; j < metablks_count; j++) {
675 ret = sqfs_read_metablock(table, offset + cur_size, &comp,
676 &data_size);
677 if (ret)
678 return -EINVAL;
679
680 cur_size += data_size + SQFS_HEADER_SIZE;
681 pos_list[j] = cur_size;
682 }
683
684 return ret;
685}
686
687static int sqfs_read_inode_table(unsigned char **inode_table)
688{
689 struct squashfs_super_block *sblk = ctxt.sblk;
690 u64 start, n_blks, table_offset, table_size;
Joao Marcos Costacdc11442020-08-18 17:17:22 +0200691 int j, ret = 0, metablks_count;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200692 unsigned char *src_table, *itb;
693 u32 src_len, dest_offset = 0;
Joao Marcos Costac9875a52020-08-19 18:28:41 +0200694 unsigned long dest_len = 0;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200695 bool compressed;
696
Joao Marcos Costac5100612020-07-30 15:33:47 +0200697 table_size = get_unaligned_le64(&sblk->directory_table_start) -
698 get_unaligned_le64(&sblk->inode_table_start);
699 start = get_unaligned_le64(&sblk->inode_table_start) /
700 ctxt.cur_dev->blksz;
701 n_blks = sqfs_calc_n_blks(sblk->inode_table_start,
702 sblk->directory_table_start, &table_offset);
703
704 /* Allocate a proper sized buffer (itb) to store the inode table */
705 itb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
706 if (!itb)
707 return -ENOMEM;
708
709 if (sqfs_disk_read(start, n_blks, itb) < 0) {
710 ret = -EINVAL;
711 goto free_itb;
712 }
713
714 /* Parse inode table (metadata block) header */
715 ret = sqfs_read_metablock(itb, table_offset, &compressed, &src_len);
716 if (ret) {
717 ret = -EINVAL;
718 goto free_itb;
719 }
720
721 /* Calculate size to store the whole decompressed table */
722 metablks_count = sqfs_count_metablks(itb, table_offset, table_size);
723 if (metablks_count < 1) {
724 ret = -EINVAL;
725 goto free_itb;
726 }
727
728 *inode_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE);
729 if (!*inode_table) {
730 ret = -ENOMEM;
Lars Weber1e69db52022-01-13 14:28:45 +0100731 printf("Error: failed to allocate squashfs inode_table of size %i, increasing CONFIG_SYS_MALLOC_LEN could help\n",
732 metablks_count * SQFS_METADATA_BLOCK_SIZE);
Joao Marcos Costac5100612020-07-30 15:33:47 +0200733 goto free_itb;
734 }
735
736 src_table = itb + table_offset + SQFS_HEADER_SIZE;
737
738 /* Extract compressed Inode table */
739 for (j = 0; j < metablks_count; j++) {
740 sqfs_read_metablock(itb, table_offset, &compressed, &src_len);
741 if (compressed) {
742 dest_len = SQFS_METADATA_BLOCK_SIZE;
Joao Marcos Costacdc11442020-08-18 17:17:22 +0200743 ret = sqfs_decompress(&ctxt, *inode_table +
Joao Marcos Costac5100612020-07-30 15:33:47 +0200744 dest_offset, &dest_len,
745 src_table, src_len);
746 if (ret) {
747 free(*inode_table);
Richard Genoud4c83d272020-11-03 12:11:07 +0100748 *inode_table = NULL;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200749 goto free_itb;
750 }
751
Joao Marcos Costac9875a52020-08-19 18:28:41 +0200752 dest_offset += dest_len;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200753 } else {
754 memcpy(*inode_table + (j * SQFS_METADATA_BLOCK_SIZE),
755 src_table, src_len);
756 }
757
758 /*
759 * Offsets to the decompression destination, to the metadata
760 * buffer 'itb' and to the decompression source, respectively.
761 */
Joao Marcos Costac9875a52020-08-19 18:28:41 +0200762
Joao Marcos Costac5100612020-07-30 15:33:47 +0200763 table_offset += src_len + SQFS_HEADER_SIZE;
764 src_table += src_len + SQFS_HEADER_SIZE;
765 }
766
767free_itb:
768 free(itb);
769
770 return ret;
771}
772
773static int sqfs_read_directory_table(unsigned char **dir_table, u32 **pos_list)
774{
775 u64 start, n_blks, table_offset, table_size;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200776 struct squashfs_super_block *sblk = ctxt.sblk;
Joao Marcos Costacdc11442020-08-18 17:17:22 +0200777 int j, ret = 0, metablks_count = -1;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200778 unsigned char *src_table, *dtb;
779 u32 src_len, dest_offset = 0;
Joao Marcos Costac9875a52020-08-19 18:28:41 +0200780 unsigned long dest_len = 0;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200781 bool compressed;
782
Richard Genoud7d23b2c2020-11-03 12:11:04 +0100783 *dir_table = NULL;
784 *pos_list = NULL;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200785 /* DIRECTORY TABLE */
786 table_size = get_unaligned_le64(&sblk->fragment_table_start) -
787 get_unaligned_le64(&sblk->directory_table_start);
788 start = get_unaligned_le64(&sblk->directory_table_start) /
789 ctxt.cur_dev->blksz;
790 n_blks = sqfs_calc_n_blks(sblk->directory_table_start,
791 sblk->fragment_table_start, &table_offset);
792
793 /* Allocate a proper sized buffer (dtb) to store the directory table */
794 dtb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
795 if (!dtb)
796 return -ENOMEM;
797
798 if (sqfs_disk_read(start, n_blks, dtb) < 0)
Richard Genoud7d23b2c2020-11-03 12:11:04 +0100799 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200800
801 /* Parse directory table (metadata block) header */
802 ret = sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
803 if (ret)
Richard Genoud7d23b2c2020-11-03 12:11:04 +0100804 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200805
806 /* Calculate total size to store the whole decompressed table */
807 metablks_count = sqfs_count_metablks(dtb, table_offset, table_size);
808 if (metablks_count < 1)
Richard Genoud7d23b2c2020-11-03 12:11:04 +0100809 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200810
811 *dir_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE);
812 if (!*dir_table)
Richard Genoud7d23b2c2020-11-03 12:11:04 +0100813 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200814
815 *pos_list = malloc(metablks_count * sizeof(u32));
Richard Genoud7d23b2c2020-11-03 12:11:04 +0100816 if (!*pos_list)
817 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200818
819 ret = sqfs_get_metablk_pos(*pos_list, dtb, table_offset,
820 metablks_count);
821 if (ret) {
822 metablks_count = -1;
Richard Genoud7d23b2c2020-11-03 12:11:04 +0100823 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200824 }
825
826 src_table = dtb + table_offset + SQFS_HEADER_SIZE;
827
828 /* Extract compressed Directory table */
829 dest_offset = 0;
830 for (j = 0; j < metablks_count; j++) {
831 sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
832 if (compressed) {
833 dest_len = SQFS_METADATA_BLOCK_SIZE;
Joao Marcos Costacdc11442020-08-18 17:17:22 +0200834 ret = sqfs_decompress(&ctxt, *dir_table +
Joao Marcos Costac5100612020-07-30 15:33:47 +0200835 (j * SQFS_METADATA_BLOCK_SIZE),
836 &dest_len, src_table, src_len);
837 if (ret) {
838 metablks_count = -1;
Richard Genoud7d23b2c2020-11-03 12:11:04 +0100839 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200840 }
841
842 if (dest_len < SQFS_METADATA_BLOCK_SIZE) {
843 dest_offset += dest_len;
844 break;
845 }
Joao Marcos Costac9875a52020-08-19 18:28:41 +0200846
847 dest_offset += dest_len;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200848 } else {
849 memcpy(*dir_table + (j * SQFS_METADATA_BLOCK_SIZE),
850 src_table, src_len);
851 }
852
853 /*
854 * Offsets to the decompression destination, to the metadata
855 * buffer 'dtb' and to the decompression source, respectively.
856 */
Joao Marcos Costac5100612020-07-30 15:33:47 +0200857 table_offset += src_len + SQFS_HEADER_SIZE;
858 src_table += src_len + SQFS_HEADER_SIZE;
859 }
860
Richard Genoud7d23b2c2020-11-03 12:11:04 +0100861out:
862 if (metablks_count < 1) {
863 free(*dir_table);
864 free(*pos_list);
865 *dir_table = NULL;
866 *pos_list = NULL;
867 }
Joao Marcos Costac5100612020-07-30 15:33:47 +0200868 free(dtb);
869
870 return metablks_count;
871}
872
873int sqfs_opendir(const char *filename, struct fs_dir_stream **dirsp)
874{
875 unsigned char *inode_table = NULL, *dir_table = NULL;
Richard Genoudea1b1652020-11-03 12:11:01 +0100876 int j, token_count = 0, ret = 0, metablks_count;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200877 struct squashfs_dir_stream *dirs;
Richard Genoudea1b1652020-11-03 12:11:01 +0100878 char **token_list = NULL, *path = NULL;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200879 u32 *pos_list = NULL;
880
Heinrich Schuchardt53ba2c22021-05-17 08:21:39 +0200881 dirs = calloc(1, sizeof(*dirs));
Joao Marcos Costac5100612020-07-30 15:33:47 +0200882 if (!dirs)
883 return -EINVAL;
884
Richard Genoudf2687682020-11-03 12:11:00 +0100885 /* these should be set to NULL to prevent dangling pointers */
886 dirs->dir_header = NULL;
887 dirs->entry = NULL;
888 dirs->table = NULL;
889 dirs->inode_table = NULL;
890 dirs->dir_table = NULL;
891
Joao Marcos Costac5100612020-07-30 15:33:47 +0200892 ret = sqfs_read_inode_table(&inode_table);
Richard Genoudf2687682020-11-03 12:11:00 +0100893 if (ret) {
894 ret = -EINVAL;
Richard Genoudea1b1652020-11-03 12:11:01 +0100895 goto out;
Richard Genoudf2687682020-11-03 12:11:00 +0100896 }
Joao Marcos Costac5100612020-07-30 15:33:47 +0200897
898 metablks_count = sqfs_read_directory_table(&dir_table, &pos_list);
Richard Genoudf2687682020-11-03 12:11:00 +0100899 if (metablks_count < 1) {
900 ret = -EINVAL;
Richard Genoudea1b1652020-11-03 12:11:01 +0100901 goto out;
Richard Genoudf2687682020-11-03 12:11:00 +0100902 }
Joao Marcos Costac5100612020-07-30 15:33:47 +0200903
904 /* Tokenize filename */
905 token_count = sqfs_count_tokens(filename);
Richard Genoudf2687682020-11-03 12:11:00 +0100906 if (token_count < 0) {
907 ret = -EINVAL;
Richard Genoudea1b1652020-11-03 12:11:01 +0100908 goto out;
Richard Genoudf2687682020-11-03 12:11:00 +0100909 }
Joao Marcos Costac5100612020-07-30 15:33:47 +0200910
911 path = strdup(filename);
Richard Genoudf2687682020-11-03 12:11:00 +0100912 if (!path) {
913 ret = -EINVAL;
Richard Genoudea1b1652020-11-03 12:11:01 +0100914 goto out;
Richard Genoudf2687682020-11-03 12:11:00 +0100915 }
Joao Marcos Costac5100612020-07-30 15:33:47 +0200916
917 token_list = malloc(token_count * sizeof(char *));
918 if (!token_list) {
919 ret = -EINVAL;
Richard Genoudea1b1652020-11-03 12:11:01 +0100920 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200921 }
922
923 /* Fill tokens list */
924 ret = sqfs_tokenize(token_list, token_count, path);
925 if (ret)
Richard Genoudea1b1652020-11-03 12:11:01 +0100926 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200927 /*
928 * ldir's (extended directory) size is greater than dir, so it works as
929 * a general solution for the malloc size, since 'i' is a union.
930 */
931 dirs->inode_table = inode_table;
932 dirs->dir_table = dir_table;
933 ret = sqfs_search_dir(dirs, token_list, token_count, pos_list,
934 metablks_count);
935 if (ret)
Richard Genoudea1b1652020-11-03 12:11:01 +0100936 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +0200937
938 if (le16_to_cpu(dirs->i_dir.inode_type) == SQFS_DIR_TYPE)
939 dirs->size = le16_to_cpu(dirs->i_dir.file_size);
940 else
941 dirs->size = le32_to_cpu(dirs->i_ldir.file_size);
942
943 /* Setup directory header */
944 memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE);
945 dirs->entry_count = dirs->dir_header->count + 1;
946 dirs->size -= SQFS_DIR_HEADER_SIZE;
947
948 /* Setup entry */
949 dirs->entry = NULL;
950 dirs->table += SQFS_DIR_HEADER_SIZE;
951
952 *dirsp = (struct fs_dir_stream *)dirs;
953
Richard Genoudea1b1652020-11-03 12:11:01 +0100954out:
Joao Marcos Costac5100612020-07-30 15:33:47 +0200955 for (j = 0; j < token_count; j++)
956 free(token_list[j]);
957 free(token_list);
958 free(pos_list);
Joao Marcos Costac5100612020-07-30 15:33:47 +0200959 free(path);
Richard Genoudea1b1652020-11-03 12:11:01 +0100960 if (ret) {
Richard Genoudf2687682020-11-03 12:11:00 +0100961 free(inode_table);
Richard Genoudf2687682020-11-03 12:11:00 +0100962 free(dirs);
Richard Genoudea1b1652020-11-03 12:11:01 +0100963 }
Joao Marcos Costac5100612020-07-30 15:33:47 +0200964
965 return ret;
966}
967
968int sqfs_readdir(struct fs_dir_stream *fs_dirs, struct fs_dirent **dentp)
969{
970 struct squashfs_super_block *sblk = ctxt.sblk;
971 struct squashfs_dir_stream *dirs;
972 struct squashfs_lreg_inode *lreg;
973 struct squashfs_base_inode *base;
974 struct squashfs_reg_inode *reg;
975 int i_number, offset = 0, ret;
976 struct fs_dirent *dent;
977 unsigned char *ipos;
978
979 dirs = (struct squashfs_dir_stream *)fs_dirs;
980 if (!dirs->size) {
981 *dentp = NULL;
982 return -SQFS_STOP_READDIR;
983 }
984
985 dent = &dirs->dentp;
986
987 if (!dirs->entry_count) {
988 if (dirs->size > SQFS_DIR_HEADER_SIZE) {
989 dirs->size -= SQFS_DIR_HEADER_SIZE;
990 } else {
991 *dentp = NULL;
992 dirs->size = 0;
993 return -SQFS_STOP_READDIR;
994 }
995
996 if (dirs->size > SQFS_EMPTY_FILE_SIZE) {
997 /* Read follow-up (emitted) dir. header */
998 memcpy(dirs->dir_header, dirs->table,
999 SQFS_DIR_HEADER_SIZE);
1000 dirs->entry_count = dirs->dir_header->count + 1;
1001 ret = sqfs_read_entry(&dirs->entry, dirs->table +
1002 SQFS_DIR_HEADER_SIZE);
1003 if (ret)
1004 return -SQFS_STOP_READDIR;
1005
1006 dirs->table += SQFS_DIR_HEADER_SIZE;
1007 }
1008 } else {
1009 ret = sqfs_read_entry(&dirs->entry, dirs->table);
1010 if (ret)
1011 return -SQFS_STOP_READDIR;
1012 }
1013
1014 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1015 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1016 sblk->block_size);
1017
1018 base = (struct squashfs_base_inode *)ipos;
1019
1020 /* Set entry type and size */
1021 switch (dirs->entry->type) {
1022 case SQFS_DIR_TYPE:
1023 case SQFS_LDIR_TYPE:
1024 dent->type = FS_DT_DIR;
1025 break;
1026 case SQFS_REG_TYPE:
1027 case SQFS_LREG_TYPE:
1028 /*
1029 * Entries do not differentiate extended from regular types, so
1030 * it needs to be verified manually.
1031 */
1032 if (get_unaligned_le16(&base->inode_type) == SQFS_LREG_TYPE) {
1033 lreg = (struct squashfs_lreg_inode *)ipos;
1034 dent->size = get_unaligned_le64(&lreg->file_size);
1035 } else {
1036 reg = (struct squashfs_reg_inode *)ipos;
1037 dent->size = get_unaligned_le32(&reg->file_size);
1038 }
1039
1040 dent->type = FS_DT_REG;
1041 break;
1042 case SQFS_BLKDEV_TYPE:
1043 case SQFS_CHRDEV_TYPE:
1044 case SQFS_LBLKDEV_TYPE:
1045 case SQFS_LCHRDEV_TYPE:
1046 case SQFS_FIFO_TYPE:
1047 case SQFS_SOCKET_TYPE:
1048 case SQFS_LFIFO_TYPE:
1049 case SQFS_LSOCKET_TYPE:
1050 dent->type = SQFS_MISC_ENTRY_TYPE;
1051 break;
1052 case SQFS_SYMLINK_TYPE:
1053 case SQFS_LSYMLINK_TYPE:
1054 dent->type = FS_DT_LNK;
1055 break;
1056 default:
1057 return -SQFS_STOP_READDIR;
1058 }
1059
1060 /* Set entry name */
1061 strncpy(dent->name, dirs->entry->name, dirs->entry->name_size + 1);
1062 dent->name[dirs->entry->name_size + 1] = '\0';
1063
1064 offset = dirs->entry->name_size + 1 + SQFS_ENTRY_BASE_LENGTH;
1065 dirs->entry_count--;
1066
1067 /* Decrement size to be read */
1068 if (dirs->size > offset)
1069 dirs->size -= offset;
1070 else
1071 dirs->size = 0;
1072
1073 /* Keep a reference to the current entry before incrementing it */
1074 dirs->table += offset;
1075
1076 *dentp = dent;
1077
1078 return 0;
1079}
1080
1081int sqfs_probe(struct blk_desc *fs_dev_desc, struct disk_partition *fs_partition)
1082{
1083 struct squashfs_super_block *sblk;
1084 int ret;
1085
1086 ctxt.cur_dev = fs_dev_desc;
1087 ctxt.cur_part_info = *fs_partition;
1088
1089 ret = sqfs_read_sblk(&sblk);
1090 if (ret)
Richard Genoud56cf1ce2020-11-03 12:11:21 +01001091 goto error;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001092
1093 /* Make sure it has a valid SquashFS magic number*/
1094 if (get_unaligned_le32(&sblk->s_magic) != SQFS_MAGIC_NUMBER) {
Simon Glassad6ddc52021-08-18 21:40:27 -06001095 debug("Bad magic number for SquashFS image.\n");
Richard Genoudccd4c082020-11-03 12:11:19 +01001096 ret = -EINVAL;
1097 goto error;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001098 }
1099
1100 ctxt.sblk = sblk;
1101
Joao Marcos Costa10f7cf52020-08-18 17:17:21 +02001102 ret = sqfs_decompressor_init(&ctxt);
Joao Marcos Costa10f7cf52020-08-18 17:17:21 +02001103 if (ret) {
Richard Genoudccd4c082020-11-03 12:11:19 +01001104 goto error;
Joao Marcos Costa10f7cf52020-08-18 17:17:21 +02001105 }
1106
Joao Marcos Costac5100612020-07-30 15:33:47 +02001107 return 0;
Richard Genoudccd4c082020-11-03 12:11:19 +01001108error:
1109 ctxt.cur_dev = NULL;
1110 free(ctxt.sblk);
1111 ctxt.sblk = NULL;
1112 return ret;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001113}
1114
1115static char *sqfs_basename(char *path)
1116{
1117 char *fname;
1118
1119 fname = path + strlen(path) - 1;
1120 while (fname >= path) {
1121 if (*fname == '/') {
1122 fname++;
1123 break;
1124 }
1125
1126 fname--;
1127 }
1128
1129 return fname;
1130}
1131
1132static char *sqfs_dirname(char *path)
1133{
1134 char *fname;
1135
1136 fname = sqfs_basename(path);
1137 --fname;
1138 *fname = '\0';
1139
1140 return path;
1141}
1142
1143/*
1144 * Takes a path to file and splits it in two parts: the filename itself and the
1145 * directory's path, e.g.:
1146 * path: /path/to/file.txt
1147 * file: file.txt
1148 * dir: /path/to
1149 */
1150static int sqfs_split_path(char **file, char **dir, const char *path)
1151{
1152 char *dirc, *basec, *bname, *dname, *tmp_path;
1153 int ret = 0;
1154
Richard Genoud54874772020-11-03 12:11:03 +01001155 *file = NULL;
1156 *dir = NULL;
1157 dirc = NULL;
1158 basec = NULL;
1159 bname = NULL;
1160 dname = NULL;
1161 tmp_path = NULL;
1162
Joao Marcos Costac5100612020-07-30 15:33:47 +02001163 /* check for first slash in path*/
1164 if (path[0] == '/') {
1165 tmp_path = strdup(path);
Richard Genoud54874772020-11-03 12:11:03 +01001166 if (!tmp_path) {
1167 ret = -ENOMEM;
1168 goto out;
1169 }
Joao Marcos Costac5100612020-07-30 15:33:47 +02001170 } else {
1171 tmp_path = malloc(strlen(path) + 2);
Richard Genoud54874772020-11-03 12:11:03 +01001172 if (!tmp_path) {
1173 ret = -ENOMEM;
1174 goto out;
1175 }
Joao Marcos Costac5100612020-07-30 15:33:47 +02001176 tmp_path[0] = '/';
1177 strcpy(tmp_path + 1, path);
1178 }
1179
1180 /* String duplicates */
1181 dirc = strdup(tmp_path);
1182 if (!dirc) {
1183 ret = -ENOMEM;
Richard Genoud54874772020-11-03 12:11:03 +01001184 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001185 }
1186
1187 basec = strdup(tmp_path);
1188 if (!basec) {
1189 ret = -ENOMEM;
Richard Genoud54874772020-11-03 12:11:03 +01001190 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001191 }
1192
1193 dname = sqfs_dirname(dirc);
1194 bname = sqfs_basename(basec);
1195
1196 *file = strdup(bname);
1197
1198 if (!*file) {
1199 ret = -ENOMEM;
Richard Genoud54874772020-11-03 12:11:03 +01001200 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001201 }
1202
1203 if (*dname == '\0') {
1204 *dir = malloc(2);
1205 if (!*dir) {
1206 ret = -ENOMEM;
Richard Genoud54874772020-11-03 12:11:03 +01001207 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001208 }
1209
1210 (*dir)[0] = '/';
1211 (*dir)[1] = '\0';
1212 } else {
1213 *dir = strdup(dname);
1214 if (!*dir) {
1215 ret = -ENOMEM;
Richard Genoud54874772020-11-03 12:11:03 +01001216 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001217 }
1218 }
1219
Richard Genoud54874772020-11-03 12:11:03 +01001220out:
1221 if (ret) {
1222 free(*file);
1223 free(*dir);
1224 *dir = NULL;
1225 *file = NULL;
1226 }
Joao Marcos Costac5100612020-07-30 15:33:47 +02001227 free(basec);
Joao Marcos Costac5100612020-07-30 15:33:47 +02001228 free(dirc);
Joao Marcos Costac5100612020-07-30 15:33:47 +02001229 free(tmp_path);
1230
1231 return ret;
1232}
1233
1234static int sqfs_get_regfile_info(struct squashfs_reg_inode *reg,
1235 struct squashfs_file_info *finfo,
1236 struct squashfs_fragment_block_entry *fentry,
1237 __le32 blksz)
1238{
1239 int datablk_count = 0, ret;
1240
1241 finfo->size = get_unaligned_le32(&reg->file_size);
1242 finfo->offset = get_unaligned_le32(&reg->offset);
1243 finfo->start = get_unaligned_le32(&reg->start_block);
1244 finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(&reg->fragment));
1245
Joao Marcos Costaa7dc37d2020-09-11 12:21:06 +02001246 if (finfo->frag && finfo->offset == 0xFFFFFFFF)
1247 return -EINVAL;
1248
1249 if (finfo->size < 1 || finfo->start == 0xFFFFFFFF)
Joao Marcos Costac9875a52020-08-19 18:28:41 +02001250 return -EINVAL;
1251
Joao Marcos Costac5100612020-07-30 15:33:47 +02001252 if (finfo->frag) {
1253 datablk_count = finfo->size / le32_to_cpu(blksz);
1254 ret = sqfs_frag_lookup(get_unaligned_le32(&reg->fragment),
1255 fentry);
1256 if (ret < 0)
1257 return -EINVAL;
Joao Marcos Costa0008d802021-05-17 18:20:38 -03001258 finfo->comp = ret;
Joao Marcos Costaa7dc37d2020-09-11 12:21:06 +02001259 if (fentry->size < 1 || fentry->start == 0x7FFFFFFF)
Joao Marcos Costac9875a52020-08-19 18:28:41 +02001260 return -EINVAL;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001261 } else {
1262 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1263 }
1264
1265 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1266 if (!finfo->blk_sizes)
1267 return -ENOMEM;
1268
1269 return datablk_count;
1270}
1271
1272static int sqfs_get_lregfile_info(struct squashfs_lreg_inode *lreg,
1273 struct squashfs_file_info *finfo,
1274 struct squashfs_fragment_block_entry *fentry,
1275 __le32 blksz)
1276{
1277 int datablk_count = 0, ret;
1278
1279 finfo->size = get_unaligned_le64(&lreg->file_size);
1280 finfo->offset = get_unaligned_le32(&lreg->offset);
1281 finfo->start = get_unaligned_le64(&lreg->start_block);
1282 finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(&lreg->fragment));
1283
Joao Marcos Costaa7dc37d2020-09-11 12:21:06 +02001284 if (finfo->frag && finfo->offset == 0xFFFFFFFF)
1285 return -EINVAL;
1286
1287 if (finfo->size < 1 || finfo->start == 0x7FFFFFFF)
Joao Marcos Costac9875a52020-08-19 18:28:41 +02001288 return -EINVAL;
1289
Joao Marcos Costac5100612020-07-30 15:33:47 +02001290 if (finfo->frag) {
1291 datablk_count = finfo->size / le32_to_cpu(blksz);
1292 ret = sqfs_frag_lookup(get_unaligned_le32(&lreg->fragment),
1293 fentry);
1294 if (ret < 0)
1295 return -EINVAL;
Joao Marcos Costa0008d802021-05-17 18:20:38 -03001296 finfo->comp = ret;
Joao Marcos Costaa7dc37d2020-09-11 12:21:06 +02001297 if (fentry->size < 1 || fentry->start == 0x7FFFFFFF)
Joao Marcos Costac9875a52020-08-19 18:28:41 +02001298 return -EINVAL;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001299 } else {
1300 datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
1301 }
1302
1303 finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
1304 if (!finfo->blk_sizes)
1305 return -ENOMEM;
1306
1307 return datablk_count;
1308}
1309
1310int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
1311 loff_t *actread)
1312{
Richard Genoud571b67e2020-11-03 12:11:18 +01001313 char *dir = NULL, *fragment_block, *datablock = NULL, *data_buffer = NULL;
1314 char *fragment = NULL, *file = NULL, *resolved, *data;
Campbell Suter9dba07f2020-11-23 15:40:03 +13001315 u64 start, n_blks, table_size, data_offset, table_offset, sparse_size;
Joao Marcos Costacdc11442020-08-18 17:17:22 +02001316 int ret, j, i_number, datablk_count = 0;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001317 struct squashfs_super_block *sblk = ctxt.sblk;
1318 struct squashfs_fragment_block_entry frag_entry;
1319 struct squashfs_file_info finfo = {0};
1320 struct squashfs_symlink_inode *symlink;
1321 struct fs_dir_stream *dirsp = NULL;
1322 struct squashfs_dir_stream *dirs;
1323 struct squashfs_lreg_inode *lreg;
1324 struct squashfs_base_inode *base;
1325 struct squashfs_reg_inode *reg;
1326 unsigned long dest_len;
1327 struct fs_dirent *dent;
1328 unsigned char *ipos;
1329
1330 *actread = 0;
1331
Richard Genoud21b1b3b2020-11-03 12:11:24 +01001332 if (offset) {
1333 /*
1334 * TODO: implement reading at an offset in file
1335 */
1336 printf("Error: reading at a specific offset in a squashfs file is not supported yet.\n");
1337 return -EINVAL;
1338 }
1339
Joao Marcos Costac5100612020-07-30 15:33:47 +02001340 /*
1341 * sqfs_opendir will uncompress inode and directory tables, and will
1342 * return a pointer to the directory that contains the requested file.
1343 */
1344 sqfs_split_path(&file, &dir, filename);
1345 ret = sqfs_opendir(dir, &dirsp);
1346 if (ret) {
Richard Genoud571b67e2020-11-03 12:11:18 +01001347 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001348 }
1349
1350 dirs = (struct squashfs_dir_stream *)dirsp;
1351
1352 /* For now, only regular files are able to be loaded */
1353 while (!sqfs_readdir(dirsp, &dent)) {
1354 ret = strcmp(dent->name, file);
1355 if (!ret)
1356 break;
1357
1358 free(dirs->entry);
Richard Genoudd1d8d752020-11-03 12:11:11 +01001359 dirs->entry = NULL;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001360 }
1361
1362 if (ret) {
1363 printf("File not found.\n");
1364 *actread = 0;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001365 ret = -ENOENT;
Richard Genoud571b67e2020-11-03 12:11:18 +01001366 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001367 }
1368
1369 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1370 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1371 sblk->block_size);
1372
1373 base = (struct squashfs_base_inode *)ipos;
1374 switch (get_unaligned_le16(&base->inode_type)) {
1375 case SQFS_REG_TYPE:
1376 reg = (struct squashfs_reg_inode *)ipos;
1377 datablk_count = sqfs_get_regfile_info(reg, &finfo, &frag_entry,
1378 sblk->block_size);
1379 if (datablk_count < 0) {
1380 ret = -EINVAL;
Richard Genoud571b67e2020-11-03 12:11:18 +01001381 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001382 }
1383
1384 memcpy(finfo.blk_sizes, ipos + sizeof(*reg),
1385 datablk_count * sizeof(u32));
1386 break;
1387 case SQFS_LREG_TYPE:
1388 lreg = (struct squashfs_lreg_inode *)ipos;
1389 datablk_count = sqfs_get_lregfile_info(lreg, &finfo,
1390 &frag_entry,
1391 sblk->block_size);
1392 if (datablk_count < 0) {
1393 ret = -EINVAL;
Richard Genoud571b67e2020-11-03 12:11:18 +01001394 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001395 }
1396
1397 memcpy(finfo.blk_sizes, ipos + sizeof(*lreg),
1398 datablk_count * sizeof(u32));
1399 break;
1400 case SQFS_SYMLINK_TYPE:
1401 case SQFS_LSYMLINK_TYPE:
1402 symlink = (struct squashfs_symlink_inode *)ipos;
1403 resolved = sqfs_resolve_symlink(symlink, filename);
1404 ret = sqfs_read(resolved, buf, offset, len, actread);
1405 free(resolved);
Richard Genoud571b67e2020-11-03 12:11:18 +01001406 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001407 case SQFS_BLKDEV_TYPE:
1408 case SQFS_CHRDEV_TYPE:
1409 case SQFS_LBLKDEV_TYPE:
1410 case SQFS_LCHRDEV_TYPE:
1411 case SQFS_FIFO_TYPE:
1412 case SQFS_SOCKET_TYPE:
1413 case SQFS_LFIFO_TYPE:
1414 case SQFS_LSOCKET_TYPE:
1415 default:
1416 printf("Unsupported entry type\n");
1417 ret = -EINVAL;
Richard Genoud571b67e2020-11-03 12:11:18 +01001418 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001419 }
1420
1421 /* If the user specifies a length, check its sanity */
1422 if (len) {
1423 if (len > finfo.size) {
1424 ret = -EINVAL;
Richard Genoud571b67e2020-11-03 12:11:18 +01001425 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001426 }
1427
1428 finfo.size = len;
Richard Genoudcbd5e402020-11-03 12:11:23 +01001429 } else {
1430 len = finfo.size;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001431 }
1432
1433 if (datablk_count) {
1434 data_offset = finfo.start;
1435 datablock = malloc(get_unaligned_le32(&sblk->block_size));
1436 if (!datablock) {
1437 ret = -ENOMEM;
Richard Genoud571b67e2020-11-03 12:11:18 +01001438 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001439 }
1440 }
1441
1442 for (j = 0; j < datablk_count; j++) {
1443 start = data_offset / ctxt.cur_dev->blksz;
1444 table_size = SQFS_BLOCK_SIZE(finfo.blk_sizes[j]);
1445 table_offset = data_offset - (start * ctxt.cur_dev->blksz);
1446 n_blks = DIV_ROUND_UP(table_size + table_offset,
1447 ctxt.cur_dev->blksz);
1448
Campbell Suter9dba07f2020-11-23 15:40:03 +13001449 /* Don't load any data for sparse blocks */
1450 if (finfo.blk_sizes[j] == 0) {
1451 n_blks = 0;
1452 table_offset = 0;
1453 data_buffer = NULL;
1454 data = NULL;
1455 } else {
1456 data_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
Joao Marcos Costac5100612020-07-30 15:33:47 +02001457
Campbell Suter9dba07f2020-11-23 15:40:03 +13001458 if (!data_buffer) {
1459 ret = -ENOMEM;
1460 goto out;
1461 }
1462
1463 ret = sqfs_disk_read(start, n_blks, data_buffer);
1464 if (ret < 0) {
1465 /*
1466 * Possible causes: too many data blocks or too large
1467 * SquashFS block size. Tip: re-compile the SquashFS
1468 * image with mksquashfs's -b <block_size> option.
1469 */
1470 printf("Error: too many data blocks to be read.\n");
1471 goto out;
1472 }
1473
1474 data = data_buffer + table_offset;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001475 }
1476
Joao Marcos Costac5100612020-07-30 15:33:47 +02001477 /* Load the data */
Campbell Suter9dba07f2020-11-23 15:40:03 +13001478 if (finfo.blk_sizes[j] == 0) {
1479 /* This is a sparse block */
1480 sparse_size = get_unaligned_le32(&sblk->block_size);
1481 if ((*actread + sparse_size) > len)
1482 sparse_size = len - *actread;
1483 memset(buf + *actread, 0, sparse_size);
1484 *actread += sparse_size;
1485 } else if (SQFS_COMPRESSED_BLOCK(finfo.blk_sizes[j])) {
Joao Marcos Costac5100612020-07-30 15:33:47 +02001486 dest_len = get_unaligned_le32(&sblk->block_size);
Joao Marcos Costacdc11442020-08-18 17:17:22 +02001487 ret = sqfs_decompress(&ctxt, datablock, &dest_len,
Joao Marcos Costac5100612020-07-30 15:33:47 +02001488 data, table_size);
1489 if (ret)
Richard Genoud571b67e2020-11-03 12:11:18 +01001490 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001491
Richard Genoudcbd5e402020-11-03 12:11:23 +01001492 if ((*actread + dest_len) > len)
1493 dest_len = len - *actread;
Richard Genoud21b1b3b2020-11-03 12:11:24 +01001494 memcpy(buf + *actread, datablock, dest_len);
Joao Marcos Costac5100612020-07-30 15:33:47 +02001495 *actread += dest_len;
1496 } else {
Richard Genoudcbd5e402020-11-03 12:11:23 +01001497 if ((*actread + table_size) > len)
1498 table_size = len - *actread;
Richard Genoud21b1b3b2020-11-03 12:11:24 +01001499 memcpy(buf + *actread, data, table_size);
Joao Marcos Costac5100612020-07-30 15:33:47 +02001500 *actread += table_size;
1501 }
1502
1503 data_offset += table_size;
Campbell Suter9dba07f2020-11-23 15:40:03 +13001504 if (data_buffer)
1505 free(data_buffer);
Richard Genoud555459e2020-11-03 12:11:14 +01001506 data_buffer = NULL;
Richard Genoudcbd5e402020-11-03 12:11:23 +01001507 if (*actread >= len)
1508 break;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001509 }
1510
Joao Marcos Costac5100612020-07-30 15:33:47 +02001511 /*
1512 * There is no need to continue if the file is not fragmented.
1513 */
1514 if (!finfo.frag) {
1515 ret = 0;
Richard Genoud571b67e2020-11-03 12:11:18 +01001516 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001517 }
1518
1519 start = frag_entry.start / ctxt.cur_dev->blksz;
1520 table_size = SQFS_BLOCK_SIZE(frag_entry.size);
1521 table_offset = frag_entry.start - (start * ctxt.cur_dev->blksz);
1522 n_blks = DIV_ROUND_UP(table_size + table_offset, ctxt.cur_dev->blksz);
1523
1524 fragment = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
1525
1526 if (!fragment) {
1527 ret = -ENOMEM;
Richard Genoud571b67e2020-11-03 12:11:18 +01001528 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001529 }
1530
1531 ret = sqfs_disk_read(start, n_blks, fragment);
1532 if (ret < 0)
Richard Genoud571b67e2020-11-03 12:11:18 +01001533 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001534
1535 /* File compressed and fragmented */
1536 if (finfo.frag && finfo.comp) {
1537 dest_len = get_unaligned_le32(&sblk->block_size);
1538 fragment_block = malloc(dest_len);
1539 if (!fragment_block) {
1540 ret = -ENOMEM;
Richard Genoud571b67e2020-11-03 12:11:18 +01001541 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001542 }
1543
Joao Marcos Costacdc11442020-08-18 17:17:22 +02001544 ret = sqfs_decompress(&ctxt, fragment_block, &dest_len,
Joao Marcos Costac5100612020-07-30 15:33:47 +02001545 (void *)fragment + table_offset,
1546 frag_entry.size);
1547 if (ret) {
1548 free(fragment_block);
Richard Genoud571b67e2020-11-03 12:11:18 +01001549 goto out;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001550 }
1551
Joao Marcos Costa0008d802021-05-17 18:20:38 -03001552 memcpy(buf + *actread, &fragment_block[finfo.offset], finfo.size - *actread);
1553 *actread = finfo.size;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001554
1555 free(fragment_block);
1556
1557 } else if (finfo.frag && !finfo.comp) {
1558 fragment_block = (void *)fragment + table_offset;
1559
Joao Marcos Costa0008d802021-05-17 18:20:38 -03001560 memcpy(buf + *actread, &fragment_block[finfo.offset], finfo.size - *actread);
1561 *actread = finfo.size;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001562 }
1563
Richard Genoud571b67e2020-11-03 12:11:18 +01001564out:
Joao Marcos Costac5100612020-07-30 15:33:47 +02001565 free(fragment);
Richard Genoud571b67e2020-11-03 12:11:18 +01001566 if (datablk_count) {
Joao Marcos Costac5100612020-07-30 15:33:47 +02001567 free(data_buffer);
Joao Marcos Costac5100612020-07-30 15:33:47 +02001568 free(datablock);
Richard Genoud571b67e2020-11-03 12:11:18 +01001569 }
Joao Marcos Costac5100612020-07-30 15:33:47 +02001570 free(file);
1571 free(dir);
Richard Genoud571b67e2020-11-03 12:11:18 +01001572 free(finfo.blk_sizes);
Richard Genoud7ce97452020-11-03 12:11:13 +01001573 sqfs_closedir(dirsp);
Joao Marcos Costac5100612020-07-30 15:33:47 +02001574
1575 return ret;
1576}
1577
1578int sqfs_size(const char *filename, loff_t *size)
1579{
1580 struct squashfs_super_block *sblk = ctxt.sblk;
1581 struct squashfs_symlink_inode *symlink;
1582 struct fs_dir_stream *dirsp = NULL;
1583 struct squashfs_base_inode *base;
1584 struct squashfs_dir_stream *dirs;
1585 struct squashfs_lreg_inode *lreg;
1586 struct squashfs_reg_inode *reg;
1587 char *dir, *file, *resolved;
1588 struct fs_dirent *dent;
1589 unsigned char *ipos;
1590 int ret, i_number;
1591
1592 sqfs_split_path(&file, &dir, filename);
1593 /*
1594 * sqfs_opendir will uncompress inode and directory tables, and will
1595 * return a pointer to the directory that contains the requested file.
1596 */
1597 ret = sqfs_opendir(dir, &dirsp);
1598 if (ret) {
Joao Marcos Costac5100612020-07-30 15:33:47 +02001599 ret = -EINVAL;
1600 goto free_strings;
1601 }
1602
1603 dirs = (struct squashfs_dir_stream *)dirsp;
1604
1605 while (!sqfs_readdir(dirsp, &dent)) {
1606 ret = strcmp(dent->name, file);
1607 if (!ret)
1608 break;
1609 free(dirs->entry);
Richard Genoud508a9dc2020-11-03 12:11:09 +01001610 dirs->entry = NULL;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001611 }
1612
1613 if (ret) {
1614 printf("File not found.\n");
1615 *size = 0;
1616 ret = -EINVAL;
1617 goto free_strings;
1618 }
1619
1620 i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
1621 ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
1622 sblk->block_size);
1623 free(dirs->entry);
Richard Genoud508a9dc2020-11-03 12:11:09 +01001624 dirs->entry = NULL;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001625
1626 base = (struct squashfs_base_inode *)ipos;
1627 switch (get_unaligned_le16(&base->inode_type)) {
1628 case SQFS_REG_TYPE:
1629 reg = (struct squashfs_reg_inode *)ipos;
1630 *size = get_unaligned_le32(&reg->file_size);
1631 break;
1632 case SQFS_LREG_TYPE:
1633 lreg = (struct squashfs_lreg_inode *)ipos;
1634 *size = get_unaligned_le64(&lreg->file_size);
1635 break;
1636 case SQFS_SYMLINK_TYPE:
1637 case SQFS_LSYMLINK_TYPE:
1638 symlink = (struct squashfs_symlink_inode *)ipos;
1639 resolved = sqfs_resolve_symlink(symlink, filename);
1640 ret = sqfs_size(resolved, size);
1641 free(resolved);
1642 break;
1643 case SQFS_BLKDEV_TYPE:
1644 case SQFS_CHRDEV_TYPE:
1645 case SQFS_LBLKDEV_TYPE:
1646 case SQFS_LCHRDEV_TYPE:
1647 case SQFS_FIFO_TYPE:
1648 case SQFS_SOCKET_TYPE:
1649 case SQFS_LFIFO_TYPE:
1650 case SQFS_LSOCKET_TYPE:
1651 default:
1652 printf("Unable to recover entry's size.\n");
1653 *size = 0;
1654 ret = -EINVAL;
1655 break;
1656 }
1657
1658free_strings:
1659 free(dir);
1660 free(file);
1661
1662 sqfs_closedir(dirsp);
1663
1664 return ret;
1665}
1666
Richard Genouddd4866b2020-11-03 12:11:26 +01001667int sqfs_exists(const char *filename)
1668{
1669 struct fs_dir_stream *dirsp = NULL;
1670 struct squashfs_dir_stream *dirs;
1671 char *dir, *file;
1672 struct fs_dirent *dent;
1673 int ret;
1674
1675 sqfs_split_path(&file, &dir, filename);
1676 /*
1677 * sqfs_opendir will uncompress inode and directory tables, and will
1678 * return a pointer to the directory that contains the requested file.
1679 */
1680 ret = sqfs_opendir(dir, &dirsp);
1681 if (ret) {
1682 ret = -EINVAL;
1683 goto free_strings;
1684 }
1685
1686 dirs = (struct squashfs_dir_stream *)dirsp;
1687
1688 while (!sqfs_readdir(dirsp, &dent)) {
1689 ret = strcmp(dent->name, file);
1690 if (!ret)
1691 break;
1692 free(dirs->entry);
1693 dirs->entry = NULL;
1694 }
1695
1696 sqfs_closedir(dirsp);
1697
1698free_strings:
1699 free(dir);
1700 free(file);
1701
1702 return ret == 0;
1703}
1704
Joao Marcos Costac5100612020-07-30 15:33:47 +02001705void sqfs_close(void)
1706{
Joao Marcos Costa10f7cf52020-08-18 17:17:21 +02001707 sqfs_decompressor_cleanup(&ctxt);
Richard Genoud7e932ac2020-11-24 18:07:52 +01001708 free(ctxt.sblk);
1709 ctxt.sblk = NULL;
1710 ctxt.cur_dev = NULL;
Joao Marcos Costac5100612020-07-30 15:33:47 +02001711}
1712
1713void sqfs_closedir(struct fs_dir_stream *dirs)
1714{
1715 struct squashfs_dir_stream *sqfs_dirs;
1716
Heinrich Schuchardt220fa472021-02-01 03:28:48 +01001717 if (!dirs)
1718 return;
1719
Joao Marcos Costac5100612020-07-30 15:33:47 +02001720 sqfs_dirs = (struct squashfs_dir_stream *)dirs;
1721 free(sqfs_dirs->inode_table);
1722 free(sqfs_dirs->dir_table);
1723 free(sqfs_dirs->dir_header);
Richard Genoud87d11e02020-11-03 12:11:02 +01001724 free(sqfs_dirs);
Joao Marcos Costac5100612020-07-30 15:33:47 +02001725}