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 | |
| 8 | #include <asm/unaligned.h> |
Pali Rohár | 11f5c0c | 2021-03-03 10:52:36 +0100 | [diff] [blame] | 9 | #include <compiler.h> |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 10 | #include <errno.h> |
| 11 | #include <stdint.h> |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | |
| 16 | #include "sqfs_decompressor.h" |
| 17 | #include "sqfs_filesystem.h" |
| 18 | #include "sqfs_utils.h" |
| 19 | |
| 20 | int sqfs_inode_size(struct squashfs_base_inode *inode, u32 blk_size) |
| 21 | { |
| 22 | switch (get_unaligned_le16(&inode->inode_type)) { |
| 23 | case SQFS_DIR_TYPE: |
| 24 | return sizeof(struct squashfs_dir_inode); |
| 25 | |
| 26 | case SQFS_REG_TYPE: { |
| 27 | struct squashfs_reg_inode *reg = |
| 28 | (struct squashfs_reg_inode *)inode; |
| 29 | u32 fragment = get_unaligned_le32(®->fragment); |
| 30 | u32 file_size = get_unaligned_le32(®->file_size); |
| 31 | unsigned int blk_list_size; |
| 32 | |
| 33 | if (SQFS_IS_FRAGMENTED(fragment)) |
| 34 | blk_list_size = file_size / blk_size; |
| 35 | else |
| 36 | blk_list_size = DIV_ROUND_UP(file_size, blk_size); |
| 37 | |
| 38 | return sizeof(*reg) + blk_list_size * sizeof(u32); |
| 39 | } |
| 40 | |
| 41 | case SQFS_LDIR_TYPE: { |
| 42 | struct squashfs_ldir_inode *ldir = |
| 43 | (struct squashfs_ldir_inode *)inode; |
| 44 | u16 i_count = get_unaligned_le16(&ldir->i_count); |
| 45 | unsigned int index_list_size = 0, l = 0; |
| 46 | struct squashfs_directory_index *di; |
| 47 | u32 sz; |
| 48 | |
| 49 | if (i_count == 0) |
| 50 | return sizeof(*ldir); |
| 51 | |
| 52 | di = ldir->index; |
Gerard Koskamp | 6f15370 | 2020-10-30 13:41:58 +0000 | [diff] [blame] | 53 | while (l < i_count) { |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 54 | sz = get_unaligned_le32(&di->size) + 1; |
| 55 | index_list_size += sz; |
| 56 | di = (void *)di + sizeof(*di) + sz; |
| 57 | l++; |
| 58 | } |
| 59 | |
| 60 | return sizeof(*ldir) + index_list_size + |
Gerard Koskamp | 6f15370 | 2020-10-30 13:41:58 +0000 | [diff] [blame] | 61 | i_count * SQFS_DIR_INDEX_BASE_LENGTH; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | case SQFS_LREG_TYPE: { |
| 65 | struct squashfs_lreg_inode *lreg = |
| 66 | (struct squashfs_lreg_inode *)inode; |
| 67 | u32 fragment = get_unaligned_le32(&lreg->fragment); |
| 68 | u64 file_size = get_unaligned_le64(&lreg->file_size); |
| 69 | unsigned int blk_list_size; |
| 70 | |
| 71 | if (fragment == 0xFFFFFFFF) |
| 72 | blk_list_size = DIV_ROUND_UP(file_size, blk_size); |
| 73 | else |
| 74 | blk_list_size = file_size / blk_size; |
| 75 | |
| 76 | return sizeof(*lreg) + blk_list_size * sizeof(u32); |
| 77 | } |
| 78 | |
| 79 | case SQFS_SYMLINK_TYPE: |
| 80 | case SQFS_LSYMLINK_TYPE: { |
Richard Weinberger | 16a19a6 | 2024-08-02 18:36:45 +0200 | [diff] [blame] | 81 | int size; |
| 82 | |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 83 | struct squashfs_symlink_inode *symlink = |
| 84 | (struct squashfs_symlink_inode *)inode; |
| 85 | |
Richard Weinberger | 16a19a6 | 2024-08-02 18:36:45 +0200 | [diff] [blame] | 86 | if (__builtin_add_overflow(sizeof(*symlink), |
| 87 | get_unaligned_le32(&symlink->symlink_size), &size)) |
| 88 | return -EINVAL; |
| 89 | |
| 90 | return size; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | case SQFS_BLKDEV_TYPE: |
| 94 | case SQFS_CHRDEV_TYPE: |
| 95 | return sizeof(struct squashfs_dev_inode); |
| 96 | case SQFS_LBLKDEV_TYPE: |
| 97 | case SQFS_LCHRDEV_TYPE: |
| 98 | return sizeof(struct squashfs_ldev_inode); |
| 99 | case SQFS_FIFO_TYPE: |
| 100 | case SQFS_SOCKET_TYPE: |
| 101 | return sizeof(struct squashfs_ipc_inode); |
| 102 | case SQFS_LFIFO_TYPE: |
| 103 | case SQFS_LSOCKET_TYPE: |
| 104 | return sizeof(struct squashfs_lipc_inode); |
| 105 | default: |
| 106 | printf("Error while searching inode: unknown type.\n"); |
| 107 | return -EINVAL; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * Given the uncompressed inode table, the inode to be found and the number of |
| 113 | * inodes in the table, return inode position in case of success. |
| 114 | */ |
| 115 | void *sqfs_find_inode(void *inode_table, int inode_number, __le32 inode_count, |
| 116 | __le32 block_size) |
| 117 | { |
| 118 | struct squashfs_base_inode *base; |
| 119 | unsigned int offset = 0, k; |
| 120 | int sz; |
| 121 | |
| 122 | if (!inode_table) { |
| 123 | printf("%s: Invalid pointer to inode table.\n", __func__); |
| 124 | return NULL; |
| 125 | } |
| 126 | |
| 127 | for (k = 0; k < le32_to_cpu(inode_count); k++) { |
| 128 | base = inode_table + offset; |
| 129 | if (get_unaligned_le32(&base->inode_number) == inode_number) |
| 130 | return inode_table + offset; |
| 131 | |
| 132 | sz = sqfs_inode_size(base, le32_to_cpu(block_size)); |
| 133 | if (sz < 0) |
| 134 | return NULL; |
| 135 | |
| 136 | offset += sz; |
| 137 | } |
| 138 | |
| 139 | printf("Inode not found.\n"); |
| 140 | |
| 141 | return NULL; |
| 142 | } |
| 143 | |
| 144 | int sqfs_read_metablock(unsigned char *file_mapping, int offset, |
| 145 | bool *compressed, u32 *data_size) |
| 146 | { |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 147 | const unsigned char *data; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 148 | u16 header; |
| 149 | |
Heinrich Schuchardt | 3abb7a4 | 2020-09-23 21:13:51 +0200 | [diff] [blame] | 150 | if (!file_mapping) |
Joao Marcos Costa | f1dfe4a | 2020-09-11 12:21:06 +0200 | [diff] [blame] | 151 | return -EFAULT; |
Heinrich Schuchardt | 3abb7a4 | 2020-09-23 21:13:51 +0200 | [diff] [blame] | 152 | data = file_mapping + offset; |
Joao Marcos Costa | f1dfe4a | 2020-09-11 12:21:06 +0200 | [diff] [blame] | 153 | |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 154 | header = get_unaligned((u16 *)data); |
Joao Marcos Costa | f1dfe4a | 2020-09-11 12:21:06 +0200 | [diff] [blame] | 155 | if (!header) |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 156 | return -EINVAL; |
| 157 | |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 158 | *compressed = SQFS_COMPRESSED_METADATA(header); |
| 159 | *data_size = SQFS_METADATA_SIZE(header); |
| 160 | |
| 161 | if (*data_size > SQFS_METADATA_BLOCK_SIZE) { |
| 162 | printf("Invalid metatada block size: %d bytes.\n", *data_size); |
| 163 | return -EINVAL; |
| 164 | } |
| 165 | |
| 166 | return 0; |
| 167 | } |