Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1 | // 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 Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 26 | static struct squashfs_ctxt ctxt; |
| 27 | |
| 28 | static 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 | |
| 44 | static int sqfs_read_sblk(struct squashfs_super_block **sblk) |
| 45 | { |
| 46 | *sblk = malloc_cache_aligned(ctxt.cur_dev->blksz); |
| 47 | if (!*sblk) |
| 48 | return -ENOMEM; |
| 49 | |
| 50 | if (sqfs_disk_read(0, 1, *sblk) != 1) { |
| 51 | free(*sblk); |
| 52 | return -EINVAL; |
| 53 | } |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | static int sqfs_count_tokens(const char *filename) |
| 59 | { |
| 60 | int token_count = 1, l; |
| 61 | |
| 62 | for (l = 1; l < strlen(filename); l++) { |
| 63 | if (filename[l] == '/') |
| 64 | token_count++; |
| 65 | } |
| 66 | |
| 67 | /* Ignore trailing '/' in path */ |
| 68 | if (filename[strlen(filename) - 1] == '/') |
| 69 | token_count--; |
| 70 | |
| 71 | if (!token_count) |
| 72 | token_count = 1; |
| 73 | |
| 74 | return token_count; |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * Calculates how many blocks are needed for the buffer used in sqfs_disk_read. |
| 79 | * The memory section (e.g. inode table) start offset and its end (i.e. the next |
| 80 | * table start) must be specified. It also calculates the offset from which to |
| 81 | * start reading the buffer. |
| 82 | */ |
| 83 | static int sqfs_calc_n_blks(__le64 start, __le64 end, u64 *offset) |
| 84 | { |
| 85 | u64 start_, table_size; |
| 86 | |
| 87 | table_size = le64_to_cpu(end) - le64_to_cpu(start); |
| 88 | start_ = le64_to_cpu(start) / ctxt.cur_dev->blksz; |
| 89 | *offset = le64_to_cpu(start) - (start_ * ctxt.cur_dev->blksz); |
| 90 | |
| 91 | return DIV_ROUND_UP(table_size + *offset, ctxt.cur_dev->blksz); |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Retrieves fragment block entry and returns true if the fragment block is |
| 96 | * compressed |
| 97 | */ |
| 98 | static int sqfs_frag_lookup(u32 inode_fragment_index, |
| 99 | struct squashfs_fragment_block_entry *e) |
| 100 | { |
| 101 | u64 start, n_blks, src_len, table_offset, start_block; |
| 102 | unsigned char *metadata_buffer, *metadata, *table; |
| 103 | struct squashfs_fragment_block_entry *entries; |
| 104 | struct squashfs_super_block *sblk = ctxt.sblk; |
| 105 | unsigned long dest_len; |
| 106 | int block, offset, ret; |
Joao Marcos Costa | b87fd01 | 2020-08-18 17:17:22 +0200 | [diff] [blame] | 107 | u16 header; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 108 | |
| 109 | if (inode_fragment_index >= get_unaligned_le32(&sblk->fragments)) |
| 110 | return -EINVAL; |
| 111 | |
| 112 | start = get_unaligned_le64(&sblk->fragment_table_start) / |
| 113 | ctxt.cur_dev->blksz; |
| 114 | n_blks = sqfs_calc_n_blks(sblk->fragment_table_start, |
| 115 | sblk->export_table_start, |
| 116 | &table_offset); |
| 117 | |
| 118 | /* Allocate a proper sized buffer to store the fragment index table */ |
| 119 | table = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz); |
| 120 | if (!table) |
| 121 | return -ENOMEM; |
| 122 | |
| 123 | if (sqfs_disk_read(start, n_blks, table) < 0) { |
| 124 | free(table); |
| 125 | return -EINVAL; |
| 126 | } |
| 127 | |
| 128 | block = SQFS_FRAGMENT_INDEX(inode_fragment_index); |
| 129 | offset = SQFS_FRAGMENT_INDEX_OFFSET(inode_fragment_index); |
| 130 | |
| 131 | /* |
| 132 | * Get the start offset of the metadata block that contains the right |
| 133 | * fragment block entry |
| 134 | */ |
| 135 | start_block = get_unaligned_le64(table + table_offset + block * |
| 136 | sizeof(u64)); |
| 137 | |
| 138 | start = start_block / ctxt.cur_dev->blksz; |
| 139 | n_blks = sqfs_calc_n_blks(cpu_to_le64(start_block), |
| 140 | sblk->fragment_table_start, &table_offset); |
| 141 | |
| 142 | metadata_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz); |
| 143 | if (!metadata_buffer) { |
| 144 | ret = -ENOMEM; |
| 145 | goto free_table; |
| 146 | } |
| 147 | |
| 148 | if (sqfs_disk_read(start, n_blks, metadata_buffer) < 0) { |
| 149 | ret = -EINVAL; |
| 150 | goto free_buffer; |
| 151 | } |
| 152 | |
| 153 | /* Every metadata block starts with a 16-bit header */ |
| 154 | header = get_unaligned_le16(metadata_buffer + table_offset); |
| 155 | metadata = metadata_buffer + table_offset + SQFS_HEADER_SIZE; |
| 156 | |
Joao Marcos Costa | f1dfe4a | 2020-09-11 12:21:06 +0200 | [diff] [blame^] | 157 | if (!metadata || !header) { |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 158 | ret = -ENOMEM; |
| 159 | goto free_buffer; |
| 160 | } |
| 161 | |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 162 | entries = malloc(SQFS_METADATA_BLOCK_SIZE); |
| 163 | if (!entries) { |
| 164 | ret = -ENOMEM; |
| 165 | goto free_buffer; |
| 166 | } |
| 167 | |
| 168 | if (SQFS_COMPRESSED_METADATA(header)) { |
| 169 | src_len = SQFS_METADATA_SIZE(header); |
| 170 | dest_len = SQFS_METADATA_BLOCK_SIZE; |
Joao Marcos Costa | b87fd01 | 2020-08-18 17:17:22 +0200 | [diff] [blame] | 171 | ret = sqfs_decompress(&ctxt, entries, &dest_len, metadata, |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 172 | src_len); |
| 173 | if (ret) { |
| 174 | ret = -EINVAL; |
| 175 | goto free_entries; |
| 176 | } |
| 177 | } else { |
| 178 | memcpy(entries, metadata, SQFS_METADATA_SIZE(header)); |
| 179 | } |
| 180 | |
| 181 | *e = entries[offset]; |
| 182 | ret = SQFS_COMPRESSED_BLOCK(e->size); |
| 183 | |
| 184 | free_entries: |
| 185 | free(entries); |
| 186 | free_buffer: |
| 187 | free(metadata_buffer); |
| 188 | free_table: |
| 189 | free(table); |
| 190 | |
| 191 | return ret; |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * The entry name is a flexible array member, and we don't know its size before |
| 196 | * actually reading the entry. So we need a first copy to retrieve this size so |
| 197 | * we can finally copy the whole struct. |
| 198 | */ |
| 199 | static int sqfs_read_entry(struct squashfs_directory_entry **dest, void *src) |
| 200 | { |
| 201 | struct squashfs_directory_entry *tmp; |
| 202 | u16 sz; |
| 203 | |
| 204 | tmp = src; |
| 205 | sz = get_unaligned_le16(src + sizeof(*tmp) - sizeof(u16)); |
| 206 | /* |
| 207 | * 'src' points to the begin of a directory entry, and 'sz' gets its |
| 208 | * 'name_size' member's value. name_size is actually the string |
| 209 | * length - 1, so adding 2 compensates this difference and adds space |
| 210 | * for the trailling null byte. |
| 211 | */ |
| 212 | *dest = malloc(sizeof(*tmp) + sz + 2); |
| 213 | if (!*dest) |
| 214 | return -ENOMEM; |
| 215 | |
| 216 | memcpy(*dest, src, sizeof(*tmp) + sz + 1); |
| 217 | (*dest)->name[sz + 1] = '\0'; |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static int sqfs_get_tokens_length(char **tokens, int count) |
| 223 | { |
| 224 | int length = 0, i; |
| 225 | |
| 226 | /* |
| 227 | * 1 is added to the result of strlen to consider the slash separator |
| 228 | * between the tokens. |
| 229 | */ |
| 230 | for (i = 0; i < count; i++) |
| 231 | length += strlen(tokens[i]) + 1; |
| 232 | |
| 233 | return length; |
| 234 | } |
| 235 | |
| 236 | /* Takes a token list and returns a single string with '/' as separator. */ |
| 237 | static char *sqfs_concat_tokens(char **token_list, int token_count) |
| 238 | { |
| 239 | char *result; |
| 240 | int i, length = 0, offset = 0; |
| 241 | |
| 242 | length = sqfs_get_tokens_length(token_list, token_count); |
| 243 | |
| 244 | result = malloc(length + 1); |
| 245 | result[length] = '\0'; |
| 246 | |
| 247 | for (i = 0; i < token_count; i++) { |
| 248 | strcpy(result + offset, token_list[i]); |
| 249 | offset += strlen(token_list[i]); |
| 250 | result[offset++] = '/'; |
| 251 | } |
| 252 | |
| 253 | return result; |
| 254 | } |
| 255 | |
| 256 | /* |
| 257 | * Differently from sqfs_concat_tokens, sqfs_join writes the result into a |
| 258 | * previously allocated string, and returns the number of bytes written. |
| 259 | */ |
| 260 | static int sqfs_join(char **strings, char *dest, int start, int end, |
| 261 | char separator) |
| 262 | { |
| 263 | int i, offset = 0; |
| 264 | |
| 265 | for (i = start; i < end; i++) { |
| 266 | strcpy(dest + offset, strings[i]); |
| 267 | offset += strlen(strings[i]); |
| 268 | if (i < end - 1) |
| 269 | dest[offset++] = separator; |
| 270 | } |
| 271 | |
| 272 | return offset; |
| 273 | } |
| 274 | |
| 275 | /* |
| 276 | * Fills the given token list using its size (count) and a source string (str) |
| 277 | */ |
| 278 | static int sqfs_tokenize(char **tokens, int count, const char *str) |
| 279 | { |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 280 | int i, j, ret = 0; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 281 | char *aux, *strc; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 282 | |
| 283 | strc = strdup(str); |
| 284 | if (!strc) |
| 285 | return -ENOMEM; |
| 286 | |
| 287 | if (!strcmp(strc, "/")) { |
| 288 | tokens[0] = strdup(strc); |
| 289 | if (!tokens[0]) { |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 290 | ret = -ENOMEM; |
| 291 | goto free_strc; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 292 | } |
| 293 | } else { |
| 294 | for (j = 0; j < count; j++) { |
| 295 | aux = strtok(!j ? strc : NULL, "/"); |
| 296 | tokens[j] = strdup(aux); |
| 297 | if (!tokens[j]) { |
| 298 | for (i = 0; i < j; i++) |
| 299 | free(tokens[i]); |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 300 | ret = -ENOMEM; |
| 301 | goto free_strc; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 306 | free_strc: |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 307 | free(strc); |
| 308 | |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 309 | return ret; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | /* |
| 313 | * Remove last 'updir + 1' tokens from the base path tokens list. This leaves us |
| 314 | * with a token list containing only the tokens needed to form the resolved |
| 315 | * path, and returns the decremented size of the token list. |
| 316 | */ |
| 317 | static int sqfs_clean_base_path(char **base, int count, int updir) |
| 318 | { |
| 319 | int i; |
| 320 | |
| 321 | for (i = count - updir - 1; i < count; i++) |
| 322 | free(base[i]); |
| 323 | |
| 324 | return count - updir - 1; |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * Given the base ("current dir.") path and the relative one, generate the |
| 329 | * absolute path. |
| 330 | */ |
| 331 | static char *sqfs_get_abs_path(const char *base, const char *rel) |
| 332 | { |
| 333 | char **base_tokens, **rel_tokens, *resolved = NULL; |
| 334 | int ret, bc, rc, i, updir = 0, resolved_size = 0, offset = 0; |
| 335 | |
| 336 | /* Memory allocation for the token lists */ |
| 337 | bc = sqfs_count_tokens(base); |
| 338 | rc = sqfs_count_tokens(rel); |
| 339 | if (bc < 1 || rc < 1) |
| 340 | return NULL; |
| 341 | |
| 342 | base_tokens = malloc(bc * sizeof(char *)); |
| 343 | if (!base_tokens) |
| 344 | return NULL; |
| 345 | |
| 346 | rel_tokens = malloc(rc * sizeof(char *)); |
| 347 | if (!rel_tokens) |
| 348 | goto free_b_tokens; |
| 349 | |
| 350 | /* Fill token lists */ |
| 351 | ret = sqfs_tokenize(base_tokens, bc, base); |
| 352 | if (ret) |
| 353 | goto free_r_tokens; |
| 354 | |
| 355 | sqfs_tokenize(rel_tokens, rc, rel); |
| 356 | if (ret) |
| 357 | goto free_r_tokens; |
| 358 | |
| 359 | /* count '..' occurrences in target path */ |
| 360 | for (i = 0; i < rc; i++) { |
| 361 | if (!strcmp(rel_tokens[i], "..")) |
| 362 | updir++; |
| 363 | } |
| 364 | |
| 365 | /* Remove the last token and the '..' occurrences */ |
| 366 | bc = sqfs_clean_base_path(base_tokens, bc, updir); |
| 367 | if (bc < 0) |
| 368 | goto free_r_tokens; |
| 369 | |
| 370 | /* Calculate resolved path size */ |
| 371 | if (!bc) |
| 372 | resolved_size++; |
| 373 | |
| 374 | resolved_size += sqfs_get_tokens_length(base_tokens, bc) + |
| 375 | sqfs_get_tokens_length(rel_tokens, rc); |
| 376 | |
| 377 | resolved = malloc(resolved_size + 1); |
| 378 | if (!resolved) |
| 379 | goto free_r_tokens_loop; |
| 380 | |
| 381 | /* Set resolved path */ |
| 382 | memset(resolved, '\0', resolved_size + 1); |
| 383 | offset += sqfs_join(base_tokens, resolved + offset, 0, bc, '/'); |
| 384 | resolved[offset++] = '/'; |
| 385 | offset += sqfs_join(rel_tokens, resolved + offset, updir, rc, '/'); |
| 386 | |
| 387 | free_r_tokens_loop: |
| 388 | for (i = 0; i < rc; i++) |
| 389 | free(rel_tokens[i]); |
| 390 | for (i = 0; i < bc; i++) |
| 391 | free(base_tokens[i]); |
| 392 | free_r_tokens: |
| 393 | free(rel_tokens); |
| 394 | free_b_tokens: |
| 395 | free(base_tokens); |
| 396 | |
| 397 | return resolved; |
| 398 | } |
| 399 | |
| 400 | static char *sqfs_resolve_symlink(struct squashfs_symlink_inode *sym, |
| 401 | const char *base_path) |
| 402 | { |
| 403 | char *resolved, *target; |
| 404 | u32 sz; |
| 405 | |
| 406 | sz = get_unaligned_le32(&sym->symlink_size); |
| 407 | target = malloc(sz + 1); |
| 408 | if (!target) |
| 409 | return NULL; |
| 410 | |
| 411 | /* |
| 412 | * There is no trailling null byte in the symlink's target path, so a |
| 413 | * copy is made and a '\0' is added at its end. |
| 414 | */ |
| 415 | target[sz] = '\0'; |
| 416 | /* Get target name (relative path) */ |
| 417 | strncpy(target, sym->symlink, sz); |
| 418 | |
| 419 | /* Relative -> absolute path conversion */ |
| 420 | resolved = sqfs_get_abs_path(base_path, target); |
| 421 | |
| 422 | free(target); |
| 423 | |
| 424 | return resolved; |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * m_list contains each metadata block's position, and m_count is the number of |
| 429 | * elements of m_list. Those metadata blocks come from the compressed directory |
| 430 | * table. |
| 431 | */ |
| 432 | static int sqfs_search_dir(struct squashfs_dir_stream *dirs, char **token_list, |
| 433 | int token_count, u32 *m_list, int m_count) |
| 434 | { |
| 435 | struct squashfs_super_block *sblk = ctxt.sblk; |
| 436 | char *path, *target, **sym_tokens, *res, *rem; |
| 437 | int j, ret, new_inode_number, offset; |
| 438 | struct squashfs_symlink_inode *sym; |
Joao Marcos Costa | f1dfe4a | 2020-09-11 12:21:06 +0200 | [diff] [blame^] | 439 | struct squashfs_ldir_inode *ldir; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 440 | struct squashfs_dir_inode *dir; |
| 441 | struct fs_dir_stream *dirsp; |
| 442 | struct fs_dirent *dent; |
| 443 | unsigned char *table; |
| 444 | |
| 445 | dirsp = (struct fs_dir_stream *)dirs; |
| 446 | |
| 447 | /* Start by root inode */ |
| 448 | table = sqfs_find_inode(dirs->inode_table, le32_to_cpu(sblk->inodes), |
| 449 | sblk->inodes, sblk->block_size); |
| 450 | |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 451 | dir = (struct squashfs_dir_inode *)table; |
Joao Marcos Costa | f1dfe4a | 2020-09-11 12:21:06 +0200 | [diff] [blame^] | 452 | ldir = (struct squashfs_ldir_inode *)table; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 453 | |
| 454 | /* get directory offset in directory table */ |
| 455 | offset = sqfs_dir_offset(table, m_list, m_count); |
| 456 | dirs->table = &dirs->dir_table[offset]; |
| 457 | |
| 458 | /* Setup directory header */ |
| 459 | dirs->dir_header = malloc(SQFS_DIR_HEADER_SIZE); |
| 460 | if (!dirs->dir_header) |
| 461 | return -ENOMEM; |
| 462 | |
| 463 | memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE); |
| 464 | |
| 465 | /* Initialize squashfs_dir_stream members */ |
| 466 | dirs->table += SQFS_DIR_HEADER_SIZE; |
| 467 | dirs->size = get_unaligned_le16(&dir->file_size) - SQFS_DIR_HEADER_SIZE; |
| 468 | dirs->entry_count = dirs->dir_header->count + 1; |
| 469 | |
| 470 | /* No path given -> root directory */ |
| 471 | if (!strcmp(token_list[0], "/")) { |
| 472 | dirs->table = &dirs->dir_table[offset]; |
| 473 | memcpy(&dirs->i_dir, dir, sizeof(*dir)); |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | for (j = 0; j < token_count; j++) { |
| 478 | if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) { |
| 479 | printf("** Cannot find directory. **\n"); |
| 480 | return -EINVAL; |
| 481 | } |
| 482 | |
| 483 | while (!sqfs_readdir(dirsp, &dent)) { |
| 484 | ret = strcmp(dent->name, token_list[j]); |
| 485 | if (!ret) |
| 486 | break; |
| 487 | free(dirs->entry); |
| 488 | } |
| 489 | |
| 490 | if (ret) { |
| 491 | printf("** Cannot find directory. **\n"); |
| 492 | return -EINVAL; |
| 493 | } |
| 494 | |
| 495 | /* Redefine inode as the found token */ |
| 496 | new_inode_number = dirs->entry->inode_offset + |
| 497 | dirs->dir_header->inode_number; |
| 498 | |
| 499 | /* Get reference to inode in the inode table */ |
| 500 | table = sqfs_find_inode(dirs->inode_table, new_inode_number, |
| 501 | sblk->inodes, sblk->block_size); |
| 502 | dir = (struct squashfs_dir_inode *)table; |
| 503 | |
| 504 | /* Check for symbolic link and inode type sanity */ |
| 505 | if (get_unaligned_le16(&dir->inode_type) == SQFS_SYMLINK_TYPE) { |
| 506 | sym = (struct squashfs_symlink_inode *)table; |
| 507 | /* Get first j + 1 tokens */ |
| 508 | path = sqfs_concat_tokens(token_list, j + 1); |
| 509 | /* Resolve for these tokens */ |
| 510 | target = sqfs_resolve_symlink(sym, path); |
| 511 | /* Join remaining tokens */ |
| 512 | rem = sqfs_concat_tokens(token_list + j + 1, token_count - |
| 513 | j - 1); |
| 514 | /* Concatenate remaining tokens and symlink's target */ |
| 515 | res = malloc(strlen(rem) + strlen(target) + 1); |
| 516 | strcpy(res, target); |
| 517 | res[strlen(target)] = '/'; |
| 518 | strcpy(res + strlen(target) + 1, rem); |
| 519 | token_count = sqfs_count_tokens(res); |
| 520 | |
| 521 | if (token_count < 0) |
| 522 | return -EINVAL; |
| 523 | |
| 524 | sym_tokens = malloc(token_count * sizeof(char *)); |
| 525 | if (!sym_tokens) |
| 526 | return -EINVAL; |
| 527 | |
| 528 | /* Fill tokens list */ |
| 529 | ret = sqfs_tokenize(sym_tokens, token_count, res); |
| 530 | if (ret) |
| 531 | return -EINVAL; |
| 532 | free(dirs->entry); |
| 533 | |
| 534 | ret = sqfs_search_dir(dirs, sym_tokens, token_count, |
| 535 | m_list, m_count); |
| 536 | return ret; |
| 537 | } else if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) { |
| 538 | printf("** Cannot find directory. **\n"); |
| 539 | free(dirs->entry); |
| 540 | return -EINVAL; |
| 541 | } |
| 542 | |
| 543 | /* Check if it is an extended dir. */ |
| 544 | if (get_unaligned_le16(&dir->inode_type) == SQFS_LDIR_TYPE) |
| 545 | ldir = (struct squashfs_ldir_inode *)table; |
| 546 | |
| 547 | /* Get dir. offset into the directory table */ |
| 548 | offset = sqfs_dir_offset(table, m_list, m_count); |
| 549 | dirs->table = &dirs->dir_table[offset]; |
| 550 | |
| 551 | /* Copy directory header */ |
| 552 | memcpy(dirs->dir_header, &dirs->dir_table[offset], |
| 553 | SQFS_DIR_HEADER_SIZE); |
| 554 | |
| 555 | /* Check for empty directory */ |
| 556 | if (sqfs_is_empty_dir(table)) { |
| 557 | printf("Empty directory.\n"); |
| 558 | free(dirs->entry); |
| 559 | return SQFS_EMPTY_DIR; |
| 560 | } |
| 561 | |
| 562 | dirs->table += SQFS_DIR_HEADER_SIZE; |
| 563 | dirs->size = get_unaligned_le16(&dir->file_size); |
| 564 | dirs->entry_count = dirs->dir_header->count + 1; |
| 565 | dirs->size -= SQFS_DIR_HEADER_SIZE; |
| 566 | free(dirs->entry); |
| 567 | } |
| 568 | |
| 569 | offset = sqfs_dir_offset(table, m_list, m_count); |
| 570 | dirs->table = &dirs->dir_table[offset]; |
| 571 | |
| 572 | if (get_unaligned_le16(&dir->inode_type) == SQFS_DIR_TYPE) |
| 573 | memcpy(&dirs->i_dir, dir, sizeof(*dir)); |
| 574 | else |
| 575 | memcpy(&dirs->i_ldir, ldir, sizeof(*ldir)); |
| 576 | |
| 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | /* |
| 581 | * Inode and directory tables are stored as a series of metadata blocks, and |
| 582 | * given the compressed size of this table, we can calculate how much metadata |
| 583 | * blocks are needed to store the result of the decompression, since a |
| 584 | * decompressed metadata block should have a size of 8KiB. |
| 585 | */ |
| 586 | static int sqfs_count_metablks(void *table, u32 offset, int table_size) |
| 587 | { |
| 588 | int count = 0, cur_size = 0, ret; |
| 589 | u32 data_size; |
| 590 | bool comp; |
| 591 | |
| 592 | do { |
| 593 | ret = sqfs_read_metablock(table, offset + cur_size, &comp, |
| 594 | &data_size); |
| 595 | if (ret) |
| 596 | return -EINVAL; |
| 597 | cur_size += data_size + SQFS_HEADER_SIZE; |
| 598 | count++; |
| 599 | } while (cur_size < table_size); |
| 600 | |
| 601 | return count; |
| 602 | } |
| 603 | |
| 604 | /* |
| 605 | * Storing the metadata blocks header's positions will be useful while looking |
| 606 | * for an entry in the directory table, using the reference (index and offset) |
| 607 | * given by its inode. |
| 608 | */ |
| 609 | static int sqfs_get_metablk_pos(u32 *pos_list, void *table, u32 offset, |
| 610 | int metablks_count) |
| 611 | { |
| 612 | u32 data_size, cur_size = 0; |
| 613 | int j, ret = 0; |
| 614 | bool comp; |
| 615 | |
| 616 | if (!metablks_count) |
| 617 | return -EINVAL; |
| 618 | |
| 619 | for (j = 0; j < metablks_count; j++) { |
| 620 | ret = sqfs_read_metablock(table, offset + cur_size, &comp, |
| 621 | &data_size); |
| 622 | if (ret) |
| 623 | return -EINVAL; |
| 624 | |
| 625 | cur_size += data_size + SQFS_HEADER_SIZE; |
| 626 | pos_list[j] = cur_size; |
| 627 | } |
| 628 | |
| 629 | return ret; |
| 630 | } |
| 631 | |
| 632 | static int sqfs_read_inode_table(unsigned char **inode_table) |
| 633 | { |
| 634 | struct squashfs_super_block *sblk = ctxt.sblk; |
| 635 | u64 start, n_blks, table_offset, table_size; |
Joao Marcos Costa | b87fd01 | 2020-08-18 17:17:22 +0200 | [diff] [blame] | 636 | int j, ret = 0, metablks_count; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 637 | unsigned char *src_table, *itb; |
| 638 | u32 src_len, dest_offset = 0; |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 639 | unsigned long dest_len = 0; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 640 | bool compressed; |
| 641 | |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 642 | table_size = get_unaligned_le64(&sblk->directory_table_start) - |
| 643 | get_unaligned_le64(&sblk->inode_table_start); |
| 644 | start = get_unaligned_le64(&sblk->inode_table_start) / |
| 645 | ctxt.cur_dev->blksz; |
| 646 | n_blks = sqfs_calc_n_blks(sblk->inode_table_start, |
| 647 | sblk->directory_table_start, &table_offset); |
| 648 | |
| 649 | /* Allocate a proper sized buffer (itb) to store the inode table */ |
| 650 | itb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz); |
| 651 | if (!itb) |
| 652 | return -ENOMEM; |
| 653 | |
| 654 | if (sqfs_disk_read(start, n_blks, itb) < 0) { |
| 655 | ret = -EINVAL; |
| 656 | goto free_itb; |
| 657 | } |
| 658 | |
| 659 | /* Parse inode table (metadata block) header */ |
| 660 | ret = sqfs_read_metablock(itb, table_offset, &compressed, &src_len); |
| 661 | if (ret) { |
| 662 | ret = -EINVAL; |
| 663 | goto free_itb; |
| 664 | } |
| 665 | |
| 666 | /* Calculate size to store the whole decompressed table */ |
| 667 | metablks_count = sqfs_count_metablks(itb, table_offset, table_size); |
| 668 | if (metablks_count < 1) { |
| 669 | ret = -EINVAL; |
| 670 | goto free_itb; |
| 671 | } |
| 672 | |
| 673 | *inode_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE); |
| 674 | if (!*inode_table) { |
| 675 | ret = -ENOMEM; |
| 676 | goto free_itb; |
| 677 | } |
| 678 | |
| 679 | src_table = itb + table_offset + SQFS_HEADER_SIZE; |
| 680 | |
| 681 | /* Extract compressed Inode table */ |
| 682 | for (j = 0; j < metablks_count; j++) { |
| 683 | sqfs_read_metablock(itb, table_offset, &compressed, &src_len); |
| 684 | if (compressed) { |
| 685 | dest_len = SQFS_METADATA_BLOCK_SIZE; |
Joao Marcos Costa | b87fd01 | 2020-08-18 17:17:22 +0200 | [diff] [blame] | 686 | ret = sqfs_decompress(&ctxt, *inode_table + |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 687 | dest_offset, &dest_len, |
| 688 | src_table, src_len); |
| 689 | if (ret) { |
| 690 | free(*inode_table); |
| 691 | goto free_itb; |
| 692 | } |
| 693 | |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 694 | dest_offset += dest_len; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 695 | } else { |
| 696 | memcpy(*inode_table + (j * SQFS_METADATA_BLOCK_SIZE), |
| 697 | src_table, src_len); |
| 698 | } |
| 699 | |
| 700 | /* |
| 701 | * Offsets to the decompression destination, to the metadata |
| 702 | * buffer 'itb' and to the decompression source, respectively. |
| 703 | */ |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 704 | |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 705 | table_offset += src_len + SQFS_HEADER_SIZE; |
| 706 | src_table += src_len + SQFS_HEADER_SIZE; |
| 707 | } |
| 708 | |
| 709 | free_itb: |
| 710 | free(itb); |
| 711 | |
| 712 | return ret; |
| 713 | } |
| 714 | |
| 715 | static int sqfs_read_directory_table(unsigned char **dir_table, u32 **pos_list) |
| 716 | { |
| 717 | u64 start, n_blks, table_offset, table_size; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 718 | struct squashfs_super_block *sblk = ctxt.sblk; |
Joao Marcos Costa | b87fd01 | 2020-08-18 17:17:22 +0200 | [diff] [blame] | 719 | int j, ret = 0, metablks_count = -1; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 720 | unsigned char *src_table, *dtb; |
| 721 | u32 src_len, dest_offset = 0; |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 722 | unsigned long dest_len = 0; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 723 | bool compressed; |
| 724 | |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 725 | /* DIRECTORY TABLE */ |
| 726 | table_size = get_unaligned_le64(&sblk->fragment_table_start) - |
| 727 | get_unaligned_le64(&sblk->directory_table_start); |
| 728 | start = get_unaligned_le64(&sblk->directory_table_start) / |
| 729 | ctxt.cur_dev->blksz; |
| 730 | n_blks = sqfs_calc_n_blks(sblk->directory_table_start, |
| 731 | sblk->fragment_table_start, &table_offset); |
| 732 | |
| 733 | /* Allocate a proper sized buffer (dtb) to store the directory table */ |
| 734 | dtb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz); |
| 735 | if (!dtb) |
| 736 | return -ENOMEM; |
| 737 | |
| 738 | if (sqfs_disk_read(start, n_blks, dtb) < 0) |
| 739 | goto free_dtb; |
| 740 | |
| 741 | /* Parse directory table (metadata block) header */ |
| 742 | ret = sqfs_read_metablock(dtb, table_offset, &compressed, &src_len); |
| 743 | if (ret) |
| 744 | goto free_dtb; |
| 745 | |
| 746 | /* Calculate total size to store the whole decompressed table */ |
| 747 | metablks_count = sqfs_count_metablks(dtb, table_offset, table_size); |
| 748 | if (metablks_count < 1) |
| 749 | goto free_dtb; |
| 750 | |
| 751 | *dir_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE); |
| 752 | if (!*dir_table) |
| 753 | goto free_dtb; |
| 754 | |
| 755 | *pos_list = malloc(metablks_count * sizeof(u32)); |
| 756 | if (!*pos_list) { |
| 757 | free(*dir_table); |
| 758 | goto free_dtb; |
| 759 | } |
| 760 | |
| 761 | ret = sqfs_get_metablk_pos(*pos_list, dtb, table_offset, |
| 762 | metablks_count); |
| 763 | if (ret) { |
| 764 | metablks_count = -1; |
| 765 | free(*dir_table); |
| 766 | free(*pos_list); |
| 767 | goto free_dtb; |
| 768 | } |
| 769 | |
| 770 | src_table = dtb + table_offset + SQFS_HEADER_SIZE; |
| 771 | |
| 772 | /* Extract compressed Directory table */ |
| 773 | dest_offset = 0; |
| 774 | for (j = 0; j < metablks_count; j++) { |
| 775 | sqfs_read_metablock(dtb, table_offset, &compressed, &src_len); |
| 776 | if (compressed) { |
| 777 | dest_len = SQFS_METADATA_BLOCK_SIZE; |
Joao Marcos Costa | b87fd01 | 2020-08-18 17:17:22 +0200 | [diff] [blame] | 778 | ret = sqfs_decompress(&ctxt, *dir_table + |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 779 | (j * SQFS_METADATA_BLOCK_SIZE), |
| 780 | &dest_len, src_table, src_len); |
| 781 | if (ret) { |
| 782 | metablks_count = -1; |
| 783 | free(*dir_table); |
| 784 | goto free_dtb; |
| 785 | } |
| 786 | |
| 787 | if (dest_len < SQFS_METADATA_BLOCK_SIZE) { |
| 788 | dest_offset += dest_len; |
| 789 | break; |
| 790 | } |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 791 | |
| 792 | dest_offset += dest_len; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 793 | } else { |
| 794 | memcpy(*dir_table + (j * SQFS_METADATA_BLOCK_SIZE), |
| 795 | src_table, src_len); |
| 796 | } |
| 797 | |
| 798 | /* |
| 799 | * Offsets to the decompression destination, to the metadata |
| 800 | * buffer 'dtb' and to the decompression source, respectively. |
| 801 | */ |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 802 | table_offset += src_len + SQFS_HEADER_SIZE; |
| 803 | src_table += src_len + SQFS_HEADER_SIZE; |
| 804 | } |
| 805 | |
| 806 | free_dtb: |
| 807 | free(dtb); |
| 808 | |
| 809 | return metablks_count; |
| 810 | } |
| 811 | |
| 812 | int sqfs_opendir(const char *filename, struct fs_dir_stream **dirsp) |
| 813 | { |
| 814 | unsigned char *inode_table = NULL, *dir_table = NULL; |
| 815 | int j, token_count, ret = 0, metablks_count; |
| 816 | struct squashfs_dir_stream *dirs; |
| 817 | char **token_list, *path; |
| 818 | u32 *pos_list = NULL; |
| 819 | |
| 820 | dirs = malloc(sizeof(*dirs)); |
| 821 | if (!dirs) |
| 822 | return -EINVAL; |
| 823 | |
| 824 | ret = sqfs_read_inode_table(&inode_table); |
| 825 | if (ret) |
| 826 | return -EINVAL; |
| 827 | |
| 828 | metablks_count = sqfs_read_directory_table(&dir_table, &pos_list); |
| 829 | if (metablks_count < 1) |
| 830 | return -EINVAL; |
| 831 | |
| 832 | /* Tokenize filename */ |
| 833 | token_count = sqfs_count_tokens(filename); |
| 834 | if (token_count < 0) |
| 835 | return -EINVAL; |
| 836 | |
| 837 | path = strdup(filename); |
| 838 | if (!path) |
| 839 | return -ENOMEM; |
| 840 | |
| 841 | token_list = malloc(token_count * sizeof(char *)); |
| 842 | if (!token_list) { |
| 843 | ret = -EINVAL; |
| 844 | goto free_path; |
| 845 | } |
| 846 | |
| 847 | /* Fill tokens list */ |
| 848 | ret = sqfs_tokenize(token_list, token_count, path); |
| 849 | if (ret) |
| 850 | goto free_tokens; |
| 851 | /* |
| 852 | * ldir's (extended directory) size is greater than dir, so it works as |
| 853 | * a general solution for the malloc size, since 'i' is a union. |
| 854 | */ |
| 855 | dirs->inode_table = inode_table; |
| 856 | dirs->dir_table = dir_table; |
| 857 | ret = sqfs_search_dir(dirs, token_list, token_count, pos_list, |
| 858 | metablks_count); |
| 859 | if (ret) |
| 860 | goto free_tokens; |
| 861 | |
| 862 | if (le16_to_cpu(dirs->i_dir.inode_type) == SQFS_DIR_TYPE) |
| 863 | dirs->size = le16_to_cpu(dirs->i_dir.file_size); |
| 864 | else |
| 865 | dirs->size = le32_to_cpu(dirs->i_ldir.file_size); |
| 866 | |
| 867 | /* Setup directory header */ |
| 868 | memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE); |
| 869 | dirs->entry_count = dirs->dir_header->count + 1; |
| 870 | dirs->size -= SQFS_DIR_HEADER_SIZE; |
| 871 | |
| 872 | /* Setup entry */ |
| 873 | dirs->entry = NULL; |
| 874 | dirs->table += SQFS_DIR_HEADER_SIZE; |
| 875 | |
| 876 | *dirsp = (struct fs_dir_stream *)dirs; |
| 877 | |
| 878 | free_tokens: |
| 879 | for (j = 0; j < token_count; j++) |
| 880 | free(token_list[j]); |
| 881 | free(token_list); |
| 882 | free(pos_list); |
| 883 | free_path: |
| 884 | free(path); |
| 885 | |
| 886 | return ret; |
| 887 | } |
| 888 | |
| 889 | int sqfs_readdir(struct fs_dir_stream *fs_dirs, struct fs_dirent **dentp) |
| 890 | { |
| 891 | struct squashfs_super_block *sblk = ctxt.sblk; |
| 892 | struct squashfs_dir_stream *dirs; |
| 893 | struct squashfs_lreg_inode *lreg; |
| 894 | struct squashfs_base_inode *base; |
| 895 | struct squashfs_reg_inode *reg; |
| 896 | int i_number, offset = 0, ret; |
| 897 | struct fs_dirent *dent; |
| 898 | unsigned char *ipos; |
| 899 | |
| 900 | dirs = (struct squashfs_dir_stream *)fs_dirs; |
| 901 | if (!dirs->size) { |
| 902 | *dentp = NULL; |
| 903 | return -SQFS_STOP_READDIR; |
| 904 | } |
| 905 | |
| 906 | dent = &dirs->dentp; |
| 907 | |
| 908 | if (!dirs->entry_count) { |
| 909 | if (dirs->size > SQFS_DIR_HEADER_SIZE) { |
| 910 | dirs->size -= SQFS_DIR_HEADER_SIZE; |
| 911 | } else { |
| 912 | *dentp = NULL; |
| 913 | dirs->size = 0; |
| 914 | return -SQFS_STOP_READDIR; |
| 915 | } |
| 916 | |
| 917 | if (dirs->size > SQFS_EMPTY_FILE_SIZE) { |
| 918 | /* Read follow-up (emitted) dir. header */ |
| 919 | memcpy(dirs->dir_header, dirs->table, |
| 920 | SQFS_DIR_HEADER_SIZE); |
| 921 | dirs->entry_count = dirs->dir_header->count + 1; |
| 922 | ret = sqfs_read_entry(&dirs->entry, dirs->table + |
| 923 | SQFS_DIR_HEADER_SIZE); |
| 924 | if (ret) |
| 925 | return -SQFS_STOP_READDIR; |
| 926 | |
| 927 | dirs->table += SQFS_DIR_HEADER_SIZE; |
| 928 | } |
| 929 | } else { |
| 930 | ret = sqfs_read_entry(&dirs->entry, dirs->table); |
| 931 | if (ret) |
| 932 | return -SQFS_STOP_READDIR; |
| 933 | } |
| 934 | |
| 935 | i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset; |
| 936 | ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes, |
| 937 | sblk->block_size); |
| 938 | |
| 939 | base = (struct squashfs_base_inode *)ipos; |
| 940 | |
| 941 | /* Set entry type and size */ |
| 942 | switch (dirs->entry->type) { |
| 943 | case SQFS_DIR_TYPE: |
| 944 | case SQFS_LDIR_TYPE: |
| 945 | dent->type = FS_DT_DIR; |
| 946 | break; |
| 947 | case SQFS_REG_TYPE: |
| 948 | case SQFS_LREG_TYPE: |
| 949 | /* |
| 950 | * Entries do not differentiate extended from regular types, so |
| 951 | * it needs to be verified manually. |
| 952 | */ |
| 953 | if (get_unaligned_le16(&base->inode_type) == SQFS_LREG_TYPE) { |
| 954 | lreg = (struct squashfs_lreg_inode *)ipos; |
| 955 | dent->size = get_unaligned_le64(&lreg->file_size); |
| 956 | } else { |
| 957 | reg = (struct squashfs_reg_inode *)ipos; |
| 958 | dent->size = get_unaligned_le32(®->file_size); |
| 959 | } |
| 960 | |
| 961 | dent->type = FS_DT_REG; |
| 962 | break; |
| 963 | case SQFS_BLKDEV_TYPE: |
| 964 | case SQFS_CHRDEV_TYPE: |
| 965 | case SQFS_LBLKDEV_TYPE: |
| 966 | case SQFS_LCHRDEV_TYPE: |
| 967 | case SQFS_FIFO_TYPE: |
| 968 | case SQFS_SOCKET_TYPE: |
| 969 | case SQFS_LFIFO_TYPE: |
| 970 | case SQFS_LSOCKET_TYPE: |
| 971 | dent->type = SQFS_MISC_ENTRY_TYPE; |
| 972 | break; |
| 973 | case SQFS_SYMLINK_TYPE: |
| 974 | case SQFS_LSYMLINK_TYPE: |
| 975 | dent->type = FS_DT_LNK; |
| 976 | break; |
| 977 | default: |
| 978 | return -SQFS_STOP_READDIR; |
| 979 | } |
| 980 | |
| 981 | /* Set entry name */ |
| 982 | strncpy(dent->name, dirs->entry->name, dirs->entry->name_size + 1); |
| 983 | dent->name[dirs->entry->name_size + 1] = '\0'; |
| 984 | |
| 985 | offset = dirs->entry->name_size + 1 + SQFS_ENTRY_BASE_LENGTH; |
| 986 | dirs->entry_count--; |
| 987 | |
| 988 | /* Decrement size to be read */ |
| 989 | if (dirs->size > offset) |
| 990 | dirs->size -= offset; |
| 991 | else |
| 992 | dirs->size = 0; |
| 993 | |
| 994 | /* Keep a reference to the current entry before incrementing it */ |
| 995 | dirs->table += offset; |
| 996 | |
| 997 | *dentp = dent; |
| 998 | |
| 999 | return 0; |
| 1000 | } |
| 1001 | |
| 1002 | int sqfs_probe(struct blk_desc *fs_dev_desc, struct disk_partition *fs_partition) |
| 1003 | { |
| 1004 | struct squashfs_super_block *sblk; |
| 1005 | int ret; |
| 1006 | |
| 1007 | ctxt.cur_dev = fs_dev_desc; |
| 1008 | ctxt.cur_part_info = *fs_partition; |
| 1009 | |
| 1010 | ret = sqfs_read_sblk(&sblk); |
| 1011 | if (ret) |
| 1012 | return ret; |
| 1013 | |
| 1014 | /* Make sure it has a valid SquashFS magic number*/ |
| 1015 | if (get_unaligned_le32(&sblk->s_magic) != SQFS_MAGIC_NUMBER) { |
| 1016 | printf("Bad magic number for SquashFS image.\n"); |
| 1017 | ctxt.cur_dev = NULL; |
| 1018 | return -EINVAL; |
| 1019 | } |
| 1020 | |
| 1021 | ctxt.sblk = sblk; |
| 1022 | |
Joao Marcos Costa | 877576c | 2020-08-18 17:17:21 +0200 | [diff] [blame] | 1023 | ret = sqfs_decompressor_init(&ctxt); |
| 1024 | |
| 1025 | if (ret) { |
| 1026 | ctxt.cur_dev = NULL; |
| 1027 | free(ctxt.sblk); |
| 1028 | return -EINVAL; |
| 1029 | } |
| 1030 | |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1031 | return 0; |
| 1032 | } |
| 1033 | |
| 1034 | static char *sqfs_basename(char *path) |
| 1035 | { |
| 1036 | char *fname; |
| 1037 | |
| 1038 | fname = path + strlen(path) - 1; |
| 1039 | while (fname >= path) { |
| 1040 | if (*fname == '/') { |
| 1041 | fname++; |
| 1042 | break; |
| 1043 | } |
| 1044 | |
| 1045 | fname--; |
| 1046 | } |
| 1047 | |
| 1048 | return fname; |
| 1049 | } |
| 1050 | |
| 1051 | static char *sqfs_dirname(char *path) |
| 1052 | { |
| 1053 | char *fname; |
| 1054 | |
| 1055 | fname = sqfs_basename(path); |
| 1056 | --fname; |
| 1057 | *fname = '\0'; |
| 1058 | |
| 1059 | return path; |
| 1060 | } |
| 1061 | |
| 1062 | /* |
| 1063 | * Takes a path to file and splits it in two parts: the filename itself and the |
| 1064 | * directory's path, e.g.: |
| 1065 | * path: /path/to/file.txt |
| 1066 | * file: file.txt |
| 1067 | * dir: /path/to |
| 1068 | */ |
| 1069 | static int sqfs_split_path(char **file, char **dir, const char *path) |
| 1070 | { |
| 1071 | char *dirc, *basec, *bname, *dname, *tmp_path; |
| 1072 | int ret = 0; |
| 1073 | |
| 1074 | /* check for first slash in path*/ |
| 1075 | if (path[0] == '/') { |
| 1076 | tmp_path = strdup(path); |
| 1077 | if (!tmp_path) |
| 1078 | return -ENOMEM; |
| 1079 | } else { |
| 1080 | tmp_path = malloc(strlen(path) + 2); |
| 1081 | if (!tmp_path) |
| 1082 | return -ENOMEM; |
| 1083 | tmp_path[0] = '/'; |
| 1084 | strcpy(tmp_path + 1, path); |
| 1085 | } |
| 1086 | |
| 1087 | /* String duplicates */ |
| 1088 | dirc = strdup(tmp_path); |
| 1089 | if (!dirc) { |
| 1090 | ret = -ENOMEM; |
| 1091 | goto free_tmp; |
| 1092 | } |
| 1093 | |
| 1094 | basec = strdup(tmp_path); |
| 1095 | if (!basec) { |
| 1096 | ret = -ENOMEM; |
| 1097 | goto free_dirc; |
| 1098 | } |
| 1099 | |
| 1100 | dname = sqfs_dirname(dirc); |
| 1101 | bname = sqfs_basename(basec); |
| 1102 | |
| 1103 | *file = strdup(bname); |
| 1104 | |
| 1105 | if (!*file) { |
| 1106 | ret = -ENOMEM; |
| 1107 | goto free_basec; |
| 1108 | } |
| 1109 | |
| 1110 | if (*dname == '\0') { |
| 1111 | *dir = malloc(2); |
| 1112 | if (!*dir) { |
| 1113 | ret = -ENOMEM; |
| 1114 | goto free_basec; |
| 1115 | } |
| 1116 | |
| 1117 | (*dir)[0] = '/'; |
| 1118 | (*dir)[1] = '\0'; |
| 1119 | } else { |
| 1120 | *dir = strdup(dname); |
| 1121 | if (!*dir) { |
| 1122 | ret = -ENOMEM; |
| 1123 | goto free_basec; |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | free_basec: |
| 1128 | free(basec); |
| 1129 | free_dirc: |
| 1130 | free(dirc); |
| 1131 | free_tmp: |
| 1132 | free(tmp_path); |
| 1133 | |
| 1134 | return ret; |
| 1135 | } |
| 1136 | |
| 1137 | static int sqfs_get_regfile_info(struct squashfs_reg_inode *reg, |
| 1138 | struct squashfs_file_info *finfo, |
| 1139 | struct squashfs_fragment_block_entry *fentry, |
| 1140 | __le32 blksz) |
| 1141 | { |
| 1142 | int datablk_count = 0, ret; |
| 1143 | |
| 1144 | finfo->size = get_unaligned_le32(®->file_size); |
| 1145 | finfo->offset = get_unaligned_le32(®->offset); |
| 1146 | finfo->start = get_unaligned_le32(®->start_block); |
| 1147 | finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(®->fragment)); |
| 1148 | |
Joao Marcos Costa | f1dfe4a | 2020-09-11 12:21:06 +0200 | [diff] [blame^] | 1149 | if (finfo->frag && finfo->offset == 0xFFFFFFFF) |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 1150 | return -EINVAL; |
| 1151 | |
Joao Marcos Costa | f1dfe4a | 2020-09-11 12:21:06 +0200 | [diff] [blame^] | 1152 | if (finfo->size < 1 || finfo->start == 0xFFFFFFFF) |
| 1153 | return -EINVAL; |
| 1154 | |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1155 | if (finfo->frag) { |
| 1156 | datablk_count = finfo->size / le32_to_cpu(blksz); |
| 1157 | ret = sqfs_frag_lookup(get_unaligned_le32(®->fragment), |
| 1158 | fentry); |
| 1159 | if (ret < 0) |
| 1160 | return -EINVAL; |
| 1161 | finfo->comp = true; |
Joao Marcos Costa | f1dfe4a | 2020-09-11 12:21:06 +0200 | [diff] [blame^] | 1162 | if (fentry->size < 1 || fentry->start == 0x7FFFFFFF) |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 1163 | return -EINVAL; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1164 | } else { |
| 1165 | datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz)); |
| 1166 | } |
| 1167 | |
| 1168 | finfo->blk_sizes = malloc(datablk_count * sizeof(u32)); |
| 1169 | if (!finfo->blk_sizes) |
| 1170 | return -ENOMEM; |
| 1171 | |
| 1172 | return datablk_count; |
| 1173 | } |
| 1174 | |
| 1175 | static int sqfs_get_lregfile_info(struct squashfs_lreg_inode *lreg, |
| 1176 | struct squashfs_file_info *finfo, |
| 1177 | struct squashfs_fragment_block_entry *fentry, |
| 1178 | __le32 blksz) |
| 1179 | { |
| 1180 | int datablk_count = 0, ret; |
| 1181 | |
| 1182 | finfo->size = get_unaligned_le64(&lreg->file_size); |
| 1183 | finfo->offset = get_unaligned_le32(&lreg->offset); |
| 1184 | finfo->start = get_unaligned_le64(&lreg->start_block); |
| 1185 | finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(&lreg->fragment)); |
| 1186 | |
Joao Marcos Costa | f1dfe4a | 2020-09-11 12:21:06 +0200 | [diff] [blame^] | 1187 | if (finfo->frag && finfo->offset == 0xFFFFFFFF) |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 1188 | return -EINVAL; |
| 1189 | |
Joao Marcos Costa | f1dfe4a | 2020-09-11 12:21:06 +0200 | [diff] [blame^] | 1190 | if (finfo->size < 1 || finfo->start == 0x7FFFFFFF) |
| 1191 | return -EINVAL; |
| 1192 | |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1193 | if (finfo->frag) { |
| 1194 | datablk_count = finfo->size / le32_to_cpu(blksz); |
| 1195 | ret = sqfs_frag_lookup(get_unaligned_le32(&lreg->fragment), |
| 1196 | fentry); |
| 1197 | if (ret < 0) |
| 1198 | return -EINVAL; |
| 1199 | finfo->comp = true; |
Joao Marcos Costa | f1dfe4a | 2020-09-11 12:21:06 +0200 | [diff] [blame^] | 1200 | if (fentry->size < 1 || fentry->start == 0x7FFFFFFF) |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 1201 | return -EINVAL; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1202 | } else { |
| 1203 | datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz)); |
| 1204 | } |
| 1205 | |
| 1206 | finfo->blk_sizes = malloc(datablk_count * sizeof(u32)); |
| 1207 | if (!finfo->blk_sizes) |
| 1208 | return -ENOMEM; |
| 1209 | |
| 1210 | return datablk_count; |
| 1211 | } |
| 1212 | |
| 1213 | int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len, |
| 1214 | loff_t *actread) |
| 1215 | { |
| 1216 | char *dir, *fragment_block, *datablock = NULL, *data_buffer = NULL; |
| 1217 | char *fragment, *file, *resolved, *data; |
| 1218 | u64 start, n_blks, table_size, data_offset, table_offset; |
Joao Marcos Costa | b87fd01 | 2020-08-18 17:17:22 +0200 | [diff] [blame] | 1219 | int ret, j, i_number, datablk_count = 0; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1220 | struct squashfs_super_block *sblk = ctxt.sblk; |
| 1221 | struct squashfs_fragment_block_entry frag_entry; |
| 1222 | struct squashfs_file_info finfo = {0}; |
| 1223 | struct squashfs_symlink_inode *symlink; |
| 1224 | struct fs_dir_stream *dirsp = NULL; |
| 1225 | struct squashfs_dir_stream *dirs; |
| 1226 | struct squashfs_lreg_inode *lreg; |
| 1227 | struct squashfs_base_inode *base; |
| 1228 | struct squashfs_reg_inode *reg; |
| 1229 | unsigned long dest_len; |
| 1230 | struct fs_dirent *dent; |
| 1231 | unsigned char *ipos; |
| 1232 | |
| 1233 | *actread = 0; |
| 1234 | |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1235 | /* |
| 1236 | * sqfs_opendir will uncompress inode and directory tables, and will |
| 1237 | * return a pointer to the directory that contains the requested file. |
| 1238 | */ |
| 1239 | sqfs_split_path(&file, &dir, filename); |
| 1240 | ret = sqfs_opendir(dir, &dirsp); |
| 1241 | if (ret) { |
| 1242 | sqfs_closedir(dirsp); |
| 1243 | goto free_paths; |
| 1244 | } |
| 1245 | |
| 1246 | dirs = (struct squashfs_dir_stream *)dirsp; |
| 1247 | |
| 1248 | /* For now, only regular files are able to be loaded */ |
| 1249 | while (!sqfs_readdir(dirsp, &dent)) { |
| 1250 | ret = strcmp(dent->name, file); |
| 1251 | if (!ret) |
| 1252 | break; |
| 1253 | |
| 1254 | free(dirs->entry); |
| 1255 | } |
| 1256 | |
| 1257 | if (ret) { |
| 1258 | printf("File not found.\n"); |
| 1259 | *actread = 0; |
| 1260 | sqfs_closedir(dirsp); |
| 1261 | ret = -ENOENT; |
| 1262 | goto free_paths; |
| 1263 | } |
| 1264 | |
| 1265 | i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset; |
| 1266 | ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes, |
| 1267 | sblk->block_size); |
| 1268 | |
| 1269 | base = (struct squashfs_base_inode *)ipos; |
| 1270 | switch (get_unaligned_le16(&base->inode_type)) { |
| 1271 | case SQFS_REG_TYPE: |
| 1272 | reg = (struct squashfs_reg_inode *)ipos; |
| 1273 | datablk_count = sqfs_get_regfile_info(reg, &finfo, &frag_entry, |
| 1274 | sblk->block_size); |
| 1275 | if (datablk_count < 0) { |
| 1276 | ret = -EINVAL; |
| 1277 | goto free_paths; |
| 1278 | } |
| 1279 | |
| 1280 | memcpy(finfo.blk_sizes, ipos + sizeof(*reg), |
| 1281 | datablk_count * sizeof(u32)); |
| 1282 | break; |
| 1283 | case SQFS_LREG_TYPE: |
| 1284 | lreg = (struct squashfs_lreg_inode *)ipos; |
| 1285 | datablk_count = sqfs_get_lregfile_info(lreg, &finfo, |
| 1286 | &frag_entry, |
| 1287 | sblk->block_size); |
| 1288 | if (datablk_count < 0) { |
| 1289 | ret = -EINVAL; |
| 1290 | goto free_paths; |
| 1291 | } |
| 1292 | |
| 1293 | memcpy(finfo.blk_sizes, ipos + sizeof(*lreg), |
| 1294 | datablk_count * sizeof(u32)); |
| 1295 | break; |
| 1296 | case SQFS_SYMLINK_TYPE: |
| 1297 | case SQFS_LSYMLINK_TYPE: |
| 1298 | symlink = (struct squashfs_symlink_inode *)ipos; |
| 1299 | resolved = sqfs_resolve_symlink(symlink, filename); |
| 1300 | ret = sqfs_read(resolved, buf, offset, len, actread); |
| 1301 | free(resolved); |
| 1302 | goto free_paths; |
| 1303 | case SQFS_BLKDEV_TYPE: |
| 1304 | case SQFS_CHRDEV_TYPE: |
| 1305 | case SQFS_LBLKDEV_TYPE: |
| 1306 | case SQFS_LCHRDEV_TYPE: |
| 1307 | case SQFS_FIFO_TYPE: |
| 1308 | case SQFS_SOCKET_TYPE: |
| 1309 | case SQFS_LFIFO_TYPE: |
| 1310 | case SQFS_LSOCKET_TYPE: |
| 1311 | default: |
| 1312 | printf("Unsupported entry type\n"); |
| 1313 | ret = -EINVAL; |
| 1314 | goto free_paths; |
| 1315 | } |
| 1316 | |
| 1317 | /* If the user specifies a length, check its sanity */ |
| 1318 | if (len) { |
| 1319 | if (len > finfo.size) { |
| 1320 | ret = -EINVAL; |
| 1321 | goto free_paths; |
| 1322 | } |
| 1323 | |
| 1324 | finfo.size = len; |
| 1325 | } |
| 1326 | |
| 1327 | if (datablk_count) { |
| 1328 | data_offset = finfo.start; |
| 1329 | datablock = malloc(get_unaligned_le32(&sblk->block_size)); |
| 1330 | if (!datablock) { |
| 1331 | ret = -ENOMEM; |
| 1332 | goto free_paths; |
| 1333 | } |
| 1334 | } |
| 1335 | |
| 1336 | for (j = 0; j < datablk_count; j++) { |
| 1337 | start = data_offset / ctxt.cur_dev->blksz; |
| 1338 | table_size = SQFS_BLOCK_SIZE(finfo.blk_sizes[j]); |
| 1339 | table_offset = data_offset - (start * ctxt.cur_dev->blksz); |
| 1340 | n_blks = DIV_ROUND_UP(table_size + table_offset, |
| 1341 | ctxt.cur_dev->blksz); |
| 1342 | |
| 1343 | data_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz); |
| 1344 | |
| 1345 | if (!data_buffer) { |
| 1346 | ret = -ENOMEM; |
| 1347 | goto free_datablk; |
| 1348 | } |
| 1349 | |
| 1350 | ret = sqfs_disk_read(start, n_blks, data_buffer); |
| 1351 | if (ret < 0) { |
| 1352 | /* |
| 1353 | * Possible causes: too many data blocks or too large |
| 1354 | * SquashFS block size. Tip: re-compile the SquashFS |
| 1355 | * image with mksquashfs's -b <block_size> option. |
| 1356 | */ |
| 1357 | printf("Error: too many data blocks to be read.\n"); |
| 1358 | goto free_buffer; |
| 1359 | } |
| 1360 | |
| 1361 | data = data_buffer + table_offset; |
| 1362 | |
| 1363 | /* Load the data */ |
| 1364 | if (SQFS_COMPRESSED_BLOCK(finfo.blk_sizes[j])) { |
| 1365 | dest_len = get_unaligned_le32(&sblk->block_size); |
Joao Marcos Costa | b87fd01 | 2020-08-18 17:17:22 +0200 | [diff] [blame] | 1366 | ret = sqfs_decompress(&ctxt, datablock, &dest_len, |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1367 | data, table_size); |
| 1368 | if (ret) |
| 1369 | goto free_buffer; |
| 1370 | |
| 1371 | memcpy(buf + offset + *actread, datablock, dest_len); |
| 1372 | *actread += dest_len; |
| 1373 | } else { |
| 1374 | memcpy(buf + offset + *actread, data, table_size); |
| 1375 | *actread += table_size; |
| 1376 | } |
| 1377 | |
| 1378 | data_offset += table_size; |
| 1379 | } |
| 1380 | |
| 1381 | free(finfo.blk_sizes); |
| 1382 | |
| 1383 | /* |
| 1384 | * There is no need to continue if the file is not fragmented. |
| 1385 | */ |
| 1386 | if (!finfo.frag) { |
| 1387 | ret = 0; |
| 1388 | goto free_buffer; |
| 1389 | } |
| 1390 | |
| 1391 | start = frag_entry.start / ctxt.cur_dev->blksz; |
| 1392 | table_size = SQFS_BLOCK_SIZE(frag_entry.size); |
| 1393 | table_offset = frag_entry.start - (start * ctxt.cur_dev->blksz); |
| 1394 | n_blks = DIV_ROUND_UP(table_size + table_offset, ctxt.cur_dev->blksz); |
| 1395 | |
| 1396 | fragment = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz); |
| 1397 | |
| 1398 | if (!fragment) { |
| 1399 | ret = -ENOMEM; |
| 1400 | goto free_buffer; |
| 1401 | } |
| 1402 | |
| 1403 | ret = sqfs_disk_read(start, n_blks, fragment); |
| 1404 | if (ret < 0) |
| 1405 | goto free_fragment; |
| 1406 | |
| 1407 | /* File compressed and fragmented */ |
| 1408 | if (finfo.frag && finfo.comp) { |
| 1409 | dest_len = get_unaligned_le32(&sblk->block_size); |
| 1410 | fragment_block = malloc(dest_len); |
| 1411 | if (!fragment_block) { |
| 1412 | ret = -ENOMEM; |
| 1413 | goto free_fragment; |
| 1414 | } |
| 1415 | |
Joao Marcos Costa | b87fd01 | 2020-08-18 17:17:22 +0200 | [diff] [blame] | 1416 | ret = sqfs_decompress(&ctxt, fragment_block, &dest_len, |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1417 | (void *)fragment + table_offset, |
| 1418 | frag_entry.size); |
| 1419 | if (ret) { |
| 1420 | free(fragment_block); |
| 1421 | goto free_fragment; |
| 1422 | } |
| 1423 | |
| 1424 | for (j = offset + *actread; j < finfo.size; j++) { |
| 1425 | memcpy(buf + j, &fragment_block[finfo.offset + j], 1); |
| 1426 | (*actread)++; |
| 1427 | } |
| 1428 | |
| 1429 | free(fragment_block); |
| 1430 | |
| 1431 | } else if (finfo.frag && !finfo.comp) { |
| 1432 | fragment_block = (void *)fragment + table_offset; |
| 1433 | |
| 1434 | for (j = offset + *actread; j < finfo.size; j++) { |
| 1435 | memcpy(buf + j, &fragment_block[finfo.offset + j], 1); |
| 1436 | (*actread)++; |
| 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | free_fragment: |
| 1441 | free(fragment); |
| 1442 | free_buffer: |
| 1443 | if (datablk_count) |
| 1444 | free(data_buffer); |
| 1445 | free_datablk: |
| 1446 | if (datablk_count) |
| 1447 | free(datablock); |
| 1448 | free_paths: |
| 1449 | free(file); |
| 1450 | free(dir); |
| 1451 | |
| 1452 | return ret; |
| 1453 | } |
| 1454 | |
| 1455 | int sqfs_size(const char *filename, loff_t *size) |
| 1456 | { |
| 1457 | struct squashfs_super_block *sblk = ctxt.sblk; |
| 1458 | struct squashfs_symlink_inode *symlink; |
| 1459 | struct fs_dir_stream *dirsp = NULL; |
| 1460 | struct squashfs_base_inode *base; |
| 1461 | struct squashfs_dir_stream *dirs; |
| 1462 | struct squashfs_lreg_inode *lreg; |
| 1463 | struct squashfs_reg_inode *reg; |
| 1464 | char *dir, *file, *resolved; |
| 1465 | struct fs_dirent *dent; |
| 1466 | unsigned char *ipos; |
| 1467 | int ret, i_number; |
| 1468 | |
| 1469 | sqfs_split_path(&file, &dir, filename); |
| 1470 | /* |
| 1471 | * sqfs_opendir will uncompress inode and directory tables, and will |
| 1472 | * return a pointer to the directory that contains the requested file. |
| 1473 | */ |
| 1474 | ret = sqfs_opendir(dir, &dirsp); |
| 1475 | if (ret) { |
| 1476 | sqfs_closedir(dirsp); |
| 1477 | ret = -EINVAL; |
| 1478 | goto free_strings; |
| 1479 | } |
| 1480 | |
| 1481 | dirs = (struct squashfs_dir_stream *)dirsp; |
| 1482 | |
| 1483 | while (!sqfs_readdir(dirsp, &dent)) { |
| 1484 | ret = strcmp(dent->name, file); |
| 1485 | if (!ret) |
| 1486 | break; |
| 1487 | free(dirs->entry); |
| 1488 | } |
| 1489 | |
| 1490 | if (ret) { |
| 1491 | printf("File not found.\n"); |
| 1492 | *size = 0; |
| 1493 | ret = -EINVAL; |
| 1494 | goto free_strings; |
| 1495 | } |
| 1496 | |
| 1497 | i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset; |
| 1498 | ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes, |
| 1499 | sblk->block_size); |
| 1500 | free(dirs->entry); |
| 1501 | |
| 1502 | base = (struct squashfs_base_inode *)ipos; |
| 1503 | switch (get_unaligned_le16(&base->inode_type)) { |
| 1504 | case SQFS_REG_TYPE: |
| 1505 | reg = (struct squashfs_reg_inode *)ipos; |
| 1506 | *size = get_unaligned_le32(®->file_size); |
| 1507 | break; |
| 1508 | case SQFS_LREG_TYPE: |
| 1509 | lreg = (struct squashfs_lreg_inode *)ipos; |
| 1510 | *size = get_unaligned_le64(&lreg->file_size); |
| 1511 | break; |
| 1512 | case SQFS_SYMLINK_TYPE: |
| 1513 | case SQFS_LSYMLINK_TYPE: |
| 1514 | symlink = (struct squashfs_symlink_inode *)ipos; |
| 1515 | resolved = sqfs_resolve_symlink(symlink, filename); |
| 1516 | ret = sqfs_size(resolved, size); |
| 1517 | free(resolved); |
| 1518 | break; |
| 1519 | case SQFS_BLKDEV_TYPE: |
| 1520 | case SQFS_CHRDEV_TYPE: |
| 1521 | case SQFS_LBLKDEV_TYPE: |
| 1522 | case SQFS_LCHRDEV_TYPE: |
| 1523 | case SQFS_FIFO_TYPE: |
| 1524 | case SQFS_SOCKET_TYPE: |
| 1525 | case SQFS_LFIFO_TYPE: |
| 1526 | case SQFS_LSOCKET_TYPE: |
| 1527 | default: |
| 1528 | printf("Unable to recover entry's size.\n"); |
| 1529 | *size = 0; |
| 1530 | ret = -EINVAL; |
| 1531 | break; |
| 1532 | } |
| 1533 | |
| 1534 | free_strings: |
| 1535 | free(dir); |
| 1536 | free(file); |
| 1537 | |
| 1538 | sqfs_closedir(dirsp); |
| 1539 | |
| 1540 | return ret; |
| 1541 | } |
| 1542 | |
| 1543 | void sqfs_close(void) |
| 1544 | { |
| 1545 | free(ctxt.sblk); |
| 1546 | ctxt.cur_dev = NULL; |
Joao Marcos Costa | 877576c | 2020-08-18 17:17:21 +0200 | [diff] [blame] | 1547 | sqfs_decompressor_cleanup(&ctxt); |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | void sqfs_closedir(struct fs_dir_stream *dirs) |
| 1551 | { |
| 1552 | struct squashfs_dir_stream *sqfs_dirs; |
| 1553 | |
| 1554 | sqfs_dirs = (struct squashfs_dir_stream *)dirs; |
| 1555 | free(sqfs_dirs->inode_table); |
| 1556 | free(sqfs_dirs->dir_table); |
| 1557 | free(sqfs_dirs->dir_header); |
| 1558 | } |