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