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