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