Patrick Georgi | ea063cb | 2020-05-08 19:28:13 +0200 | [diff] [blame] | 1 | /* CBFS Image Manipulation */ |
Patrick Georgi | 7333a11 | 2020-05-08 20:48:04 +0200 | [diff] [blame] | 2 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 3 | |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 4 | #include <inttypes.h> |
| 5 | #include <libgen.h> |
Patrick Georgi | cccc9d4 | 2015-04-28 13:09:36 +0200 | [diff] [blame] | 6 | #include <stddef.h> |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 10 | #include <strings.h> |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 11 | #include <commonlib/endian.h> |
Joel Kitching | 2eb89c8 | 2019-04-25 17:45:12 +0800 | [diff] [blame] | 12 | #include <vb2_sha.h> |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 13 | |
| 14 | #include "common.h" |
| 15 | #include "cbfs_image.h" |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 16 | #include "elfparsing.h" |
Aaron Durbin | 694fd13 | 2015-10-28 11:39:34 -0500 | [diff] [blame] | 17 | #include "rmodule.h" |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 18 | |
Sol Boucher | 636cc85 | 2015-04-03 09:13:04 -0700 | [diff] [blame] | 19 | /* Even though the file-adding functions---cbfs_add_entry() and |
| 20 | * cbfs_add_entry_at()---perform their sizing checks against the beginning of |
| 21 | * the subsequent section rather than a stable recorded value such as an empty |
| 22 | * file header's len field, it's possible to prove two interesting properties |
| 23 | * about their behavior: |
| 24 | * - Placing a new file within an empty entry located below an existing file |
| 25 | * entry will never leave an aligned flash address containing neither the |
| 26 | * beginning of a file header nor part of a file. |
| 27 | * - Placing a new file in an empty entry at the very end of the image such |
| 28 | * that it fits, but leaves no room for a final header, is guaranteed not to |
| 29 | * change the total amount of space for entries, even if that new file is |
| 30 | * later removed from the CBFS. |
| 31 | * These properties are somewhat nonobvious from the implementation, so the |
| 32 | * reader is encouraged to blame this comment and examine the full proofs |
| 33 | * in the commit message before making significant changes that would risk |
| 34 | * removing said guarantees. |
| 35 | */ |
| 36 | |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 37 | /* The file name align is not defined in CBFS spec -- only a preference by |
| 38 | * (old) cbfstool. */ |
| 39 | #define CBFS_FILENAME_ALIGN (16) |
| 40 | |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 41 | static const char *lookup_name_by_type(const struct typedesc_t *desc, uint32_t type, |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 42 | const char *default_value) |
| 43 | { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 44 | int i; |
| 45 | for (i = 0; desc[i].name; i++) |
| 46 | if (desc[i].type == type) |
| 47 | return desc[i].name; |
| 48 | return default_value; |
| 49 | } |
| 50 | |
Sol Boucher | ec42486 | 2015-05-07 21:00:05 -0700 | [diff] [blame] | 51 | static int lookup_type_by_name(const struct typedesc_t *desc, const char *name) |
| 52 | { |
| 53 | int i; |
| 54 | for (i = 0; desc[i].name && strcasecmp(name, desc[i].name); ++i); |
| 55 | return desc[i].name ? (int)desc[i].type : -1; |
| 56 | } |
| 57 | |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 58 | static const char *get_cbfs_entry_type_name(uint32_t type) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 59 | { |
Patrick Georgi | dc37dab | 2015-09-09 16:46:00 +0200 | [diff] [blame] | 60 | return lookup_name_by_type(filetypes, type, "(unknown)"); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 61 | } |
| 62 | |
Sol Boucher | ec42486 | 2015-05-07 21:00:05 -0700 | [diff] [blame] | 63 | int cbfs_parse_comp_algo(const char *name) |
| 64 | { |
| 65 | return lookup_type_by_name(types_cbfs_compression, name); |
| 66 | } |
| 67 | |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 68 | static const char *get_hash_attr_name(uint16_t hash_type) |
| 69 | { |
| 70 | return lookup_name_by_type(types_cbfs_hash, hash_type, "(invalid)"); |
| 71 | } |
| 72 | |
| 73 | int cbfs_parse_hash_algo(const char *name) |
| 74 | { |
| 75 | return lookup_type_by_name(types_cbfs_hash, name); |
| 76 | } |
| 77 | |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 78 | /* CBFS image */ |
| 79 | |
Patrick Georgi | 11ee08f | 2015-08-11 15:10:02 +0200 | [diff] [blame] | 80 | size_t cbfs_calculate_file_header_size(const char *name) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 81 | { |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 82 | return (sizeof(struct cbfs_file) + |
| 83 | align_up(strlen(name) + 1, CBFS_FILENAME_ALIGN)); |
| 84 | } |
| 85 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 86 | /* Only call on legacy CBFSes possessing a master header. */ |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 87 | static int cbfs_fix_legacy_size(struct cbfs_image *image, char *hdr_loc) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 88 | { |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 89 | assert(image); |
| 90 | assert(cbfs_is_legacy_cbfs(image)); |
Hung-Te Lin | 49fcd75 | 2013-01-29 03:16:20 +0800 | [diff] [blame] | 91 | // A bug in old cbfstool may produce extra few bytes (by alignment) and |
| 92 | // cause cbfstool to overwrite things after free space -- which is |
| 93 | // usually CBFS header on x86. We need to workaround that. |
Patrick Georgi | 343ea08 | 2016-02-10 18:07:52 +0100 | [diff] [blame] | 94 | // Except when we run across a file that contains the actual header, |
| 95 | // in which case this image is a safe, new-style |
| 96 | // `cbfstool add-master-header` based image. |
Hung-Te Lin | 49fcd75 | 2013-01-29 03:16:20 +0800 | [diff] [blame] | 97 | |
| 98 | struct cbfs_file *entry, *first = NULL, *last = NULL; |
| 99 | for (first = entry = cbfs_find_first_entry(image); |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 100 | entry && cbfs_is_valid_entry(image, entry); |
Hung-Te Lin | 49fcd75 | 2013-01-29 03:16:20 +0800 | [diff] [blame] | 101 | entry = cbfs_find_next_entry(image, entry)) { |
Patrick Georgi | 343ea08 | 2016-02-10 18:07:52 +0100 | [diff] [blame] | 102 | /* Is the header guarded by a CBFS file entry? Then exit */ |
| 103 | if (((char *)entry) + ntohl(entry->offset) == hdr_loc) { |
| 104 | return 0; |
| 105 | } |
Hung-Te Lin | 49fcd75 | 2013-01-29 03:16:20 +0800 | [diff] [blame] | 106 | last = entry; |
| 107 | } |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 108 | if ((char *)first < (char *)hdr_loc && |
| 109 | (char *)entry > (char *)hdr_loc) { |
Hung-Te Lin | 49fcd75 | 2013-01-29 03:16:20 +0800 | [diff] [blame] | 110 | WARN("CBFS image was created with old cbfstool with size bug. " |
| 111 | "Fixing size in last entry...\n"); |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 112 | last->len = htonl(ntohl(last->len) - image->header.align); |
Hung-Te Lin | 49fcd75 | 2013-01-29 03:16:20 +0800 | [diff] [blame] | 113 | DEBUG("Last entry has been changed from 0x%x to 0x%x.\n", |
| 114 | cbfs_get_entry_addr(image, entry), |
| 115 | cbfs_get_entry_addr(image, |
| 116 | cbfs_find_next_entry(image, last))); |
| 117 | } |
| 118 | return 0; |
| 119 | } |
| 120 | |
Ronald G. Minnich | b5adeee | 2014-01-06 08:38:15 -0800 | [diff] [blame] | 121 | void cbfs_put_header(void *dest, const struct cbfs_header *header) |
| 122 | { |
| 123 | struct buffer outheader; |
| 124 | |
| 125 | outheader.data = dest; |
| 126 | outheader.size = 0; |
| 127 | |
| 128 | xdr_be.put32(&outheader, header->magic); |
| 129 | xdr_be.put32(&outheader, header->version); |
| 130 | xdr_be.put32(&outheader, header->romsize); |
| 131 | xdr_be.put32(&outheader, header->bootblocksize); |
| 132 | xdr_be.put32(&outheader, header->align); |
| 133 | xdr_be.put32(&outheader, header->offset); |
| 134 | xdr_be.put32(&outheader, header->architecture); |
| 135 | } |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 136 | |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 137 | static void cbfs_decode_payload_segment(struct cbfs_payload_segment *output, |
| 138 | struct cbfs_payload_segment *input) |
| 139 | { |
| 140 | struct buffer seg = { |
| 141 | .data = (void *)input, |
| 142 | .size = sizeof(*input), |
| 143 | }; |
| 144 | output->type = xdr_be.get32(&seg); |
| 145 | output->compression = xdr_be.get32(&seg); |
| 146 | output->offset = xdr_be.get32(&seg); |
| 147 | output->load_addr = xdr_be.get64(&seg); |
| 148 | output->len = xdr_be.get32(&seg); |
| 149 | output->mem_len = xdr_be.get32(&seg); |
| 150 | assert(seg.size == 0); |
| 151 | } |
| 152 | |
Patrick Georgi | a71c83f | 2015-08-26 12:23:26 +0200 | [diff] [blame] | 153 | static int cbfs_file_get_compression_info(struct cbfs_file *entry, |
| 154 | uint32_t *decompressed_size) |
| 155 | { |
| 156 | unsigned int compression = CBFS_COMPRESS_NONE; |
Patrick Georgi | a2ce710 | 2016-12-14 16:08:52 +0100 | [diff] [blame] | 157 | if (decompressed_size) |
| 158 | *decompressed_size = ntohl(entry->len); |
Patrick Georgi | a71c83f | 2015-08-26 12:23:26 +0200 | [diff] [blame] | 159 | for (struct cbfs_file_attribute *attr = cbfs_file_first_attr(entry); |
| 160 | attr != NULL; |
| 161 | attr = cbfs_file_next_attr(entry, attr)) { |
| 162 | if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_COMPRESSION) { |
| 163 | struct cbfs_file_attr_compression *ac = |
| 164 | (struct cbfs_file_attr_compression *)attr; |
| 165 | compression = ntohl(ac->compression); |
| 166 | if (decompressed_size) |
| 167 | *decompressed_size = |
| 168 | ntohl(ac->decompressed_size); |
| 169 | } |
| 170 | } |
| 171 | return compression; |
| 172 | } |
| 173 | |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 174 | static struct cbfs_file_attr_hash *cbfs_file_get_next_hash( |
| 175 | struct cbfs_file *entry, struct cbfs_file_attr_hash *cur) |
| 176 | { |
| 177 | struct cbfs_file_attribute *attr = (struct cbfs_file_attribute *)cur; |
| 178 | if (attr == NULL) { |
| 179 | attr = cbfs_file_first_attr(entry); |
| 180 | if (attr == NULL) |
| 181 | return NULL; |
| 182 | if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_HASH) |
| 183 | return (struct cbfs_file_attr_hash *)attr; |
| 184 | } |
| 185 | while ((attr = cbfs_file_next_attr(entry, attr)) != NULL) { |
| 186 | if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_HASH) |
| 187 | return (struct cbfs_file_attr_hash *)attr; |
| 188 | }; |
| 189 | return NULL; |
| 190 | } |
| 191 | |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 192 | void cbfs_get_header(struct cbfs_header *header, void *src) |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 193 | { |
| 194 | struct buffer outheader; |
| 195 | |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 196 | outheader.data = src; /* We're not modifying the data */ |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 197 | outheader.size = 0; |
| 198 | |
| 199 | header->magic = xdr_be.get32(&outheader); |
| 200 | header->version = xdr_be.get32(&outheader); |
| 201 | header->romsize = xdr_be.get32(&outheader); |
| 202 | header->bootblocksize = xdr_be.get32(&outheader); |
| 203 | header->align = xdr_be.get32(&outheader); |
| 204 | header->offset = xdr_be.get32(&outheader); |
| 205 | header->architecture = xdr_be.get32(&outheader); |
| 206 | } |
| 207 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 208 | int cbfs_image_create(struct cbfs_image *image, size_t entries_size) |
| 209 | { |
| 210 | assert(image); |
| 211 | assert(image->buffer.data); |
| 212 | |
| 213 | size_t empty_header_len = cbfs_calculate_file_header_size(""); |
| 214 | uint32_t entries_offset = 0; |
| 215 | uint32_t align = CBFS_ENTRY_ALIGNMENT; |
| 216 | if (image->has_header) { |
| 217 | entries_offset = image->header.offset; |
| 218 | |
| 219 | if (entries_offset > image->buffer.size) { |
| 220 | ERROR("CBFS file entries are located outside CBFS itself\n"); |
| 221 | return -1; |
| 222 | } |
| 223 | |
| 224 | align = image->header.align; |
| 225 | } |
| 226 | |
| 227 | // This attribute must be given in order to prove that this module |
| 228 | // correctly preserves certain CBFS properties. See the block comment |
| 229 | // near the top of this file (and the associated commit message). |
| 230 | if (align < empty_header_len) { |
| 231 | ERROR("CBFS must be aligned to at least %zu bytes\n", |
| 232 | empty_header_len); |
| 233 | return -1; |
| 234 | } |
| 235 | |
| 236 | if (entries_size > image->buffer.size - entries_offset) { |
| 237 | ERROR("CBFS doesn't have enough space to fit its file entries\n"); |
| 238 | return -1; |
| 239 | } |
| 240 | |
| 241 | if (empty_header_len > entries_size) { |
| 242 | ERROR("CBFS is too small to fit any header\n"); |
| 243 | return -1; |
| 244 | } |
| 245 | struct cbfs_file *entry_header = |
| 246 | (struct cbfs_file *)(image->buffer.data + entries_offset); |
| 247 | // This alignment is necessary in order to prove that this module |
| 248 | // correctly preserves certain CBFS properties. See the block comment |
| 249 | // near the top of this file (and the associated commit message). |
| 250 | entries_size -= entries_size % align; |
| 251 | |
| 252 | size_t capacity = entries_size - empty_header_len; |
| 253 | LOG("Created CBFS (capacity = %zu bytes)\n", capacity); |
Patrick Georgi | edf25d9 | 2015-08-12 09:12:06 +0200 | [diff] [blame] | 254 | return cbfs_create_empty_entry(entry_header, CBFS_COMPONENT_NULL, |
| 255 | capacity, ""); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | int cbfs_legacy_image_create(struct cbfs_image *image, |
| 259 | uint32_t architecture, |
| 260 | uint32_t align, |
| 261 | struct buffer *bootblock, |
| 262 | uint32_t bootblock_offset, |
| 263 | uint32_t header_offset, |
| 264 | uint32_t entries_offset) |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 265 | { |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 266 | assert(image); |
| 267 | assert(image->buffer.data); |
| 268 | assert(bootblock); |
| 269 | |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 270 | int32_t *rel_offset; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 271 | uint32_t cbfs_len; |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 272 | void *header_loc; |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 273 | size_t size = image->buffer.size; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 274 | |
| 275 | DEBUG("cbfs_image_create: bootblock=0x%x+0x%zx, " |
| 276 | "header=0x%x+0x%zx, entries_offset=0x%x\n", |
Sol Boucher | 5bad395 | 2015-05-05 20:35:26 -0700 | [diff] [blame] | 277 | bootblock_offset, bootblock->size, header_offset, |
| 278 | sizeof(image->header), entries_offset); |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 279 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 280 | // Adjust legacy top-aligned address to ROM offset. |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 281 | if (IS_TOP_ALIGNED_ADDRESS(entries_offset)) |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 282 | entries_offset = size + (int32_t)entries_offset; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 283 | if (IS_TOP_ALIGNED_ADDRESS(bootblock_offset)) |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 284 | bootblock_offset = size + (int32_t)bootblock_offset; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 285 | if (IS_TOP_ALIGNED_ADDRESS(header_offset)) |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 286 | header_offset = size + (int32_t)header_offset; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 287 | |
| 288 | DEBUG("cbfs_create_image: (real offset) bootblock=0x%x, " |
| 289 | "header=0x%x, entries_offset=0x%x\n", |
| 290 | bootblock_offset, header_offset, entries_offset); |
| 291 | |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 292 | // Prepare bootblock |
| 293 | if (bootblock_offset + bootblock->size > size) { |
| 294 | ERROR("Bootblock (0x%x+0x%zx) exceed ROM size (0x%zx)\n", |
| 295 | bootblock_offset, bootblock->size, size); |
| 296 | return -1; |
| 297 | } |
Hung-Te Lin | c5ff648 | 2013-02-06 12:41:49 +0800 | [diff] [blame] | 298 | if (entries_offset > bootblock_offset && |
| 299 | entries_offset < bootblock->size) { |
| 300 | ERROR("Bootblock (0x%x+0x%zx) overlap CBFS data (0x%x)\n", |
| 301 | bootblock_offset, bootblock->size, entries_offset); |
| 302 | return -1; |
| 303 | } |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 304 | memcpy(image->buffer.data + bootblock_offset, bootblock->data, |
| 305 | bootblock->size); |
| 306 | |
| 307 | // Prepare header |
Sol Boucher | 5bad395 | 2015-05-05 20:35:26 -0700 | [diff] [blame] | 308 | if (header_offset + sizeof(image->header) > size - sizeof(int32_t)) { |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 309 | ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%zx)\n", |
Sol Boucher | 5bad395 | 2015-05-05 20:35:26 -0700 | [diff] [blame] | 310 | header_offset, sizeof(image->header), size); |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 311 | return -1; |
| 312 | } |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 313 | image->header.magic = CBFS_HEADER_MAGIC; |
| 314 | image->header.version = CBFS_HEADER_VERSION; |
| 315 | image->header.romsize = size; |
| 316 | image->header.bootblocksize = bootblock->size; |
| 317 | image->header.align = align; |
| 318 | image->header.offset = entries_offset; |
| 319 | image->header.architecture = architecture; |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 320 | |
| 321 | header_loc = (image->buffer.data + header_offset); |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 322 | cbfs_put_header(header_loc, &image->header); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 323 | image->has_header = true; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 324 | |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 325 | // The last 4 byte of the image contain the relative offset from the end |
| 326 | // of the image to the master header as a 32-bit signed integer. x86 |
| 327 | // relies on this also being its (memory-mapped, top-aligned) absolute |
| 328 | // 32-bit address by virtue of how two's complement numbers work. |
| 329 | assert(size % sizeof(int32_t) == 0); |
| 330 | rel_offset = (int32_t *)(image->buffer.data + size - sizeof(int32_t)); |
| 331 | *rel_offset = header_offset - size; |
| 332 | |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 333 | // Prepare entries |
| 334 | if (align_up(entries_offset, align) != entries_offset) { |
| 335 | ERROR("Offset (0x%x) must be aligned to 0x%x.\n", |
| 336 | entries_offset, align); |
| 337 | return -1; |
| 338 | } |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 339 | // To calculate available length, find |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 340 | // e = min(bootblock, header, rel_offset) where e > entries_offset. |
| 341 | cbfs_len = size - sizeof(int32_t); |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 342 | if (bootblock_offset > entries_offset && bootblock_offset < cbfs_len) |
| 343 | cbfs_len = bootblock_offset; |
| 344 | if (header_offset > entries_offset && header_offset < cbfs_len) |
| 345 | cbfs_len = header_offset; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 346 | |
| 347 | if (cbfs_image_create(image, cbfs_len - entries_offset)) |
| 348 | return -1; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 349 | return 0; |
| 350 | } |
| 351 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 352 | int cbfs_image_from_buffer(struct cbfs_image *out, struct buffer *in, |
| 353 | uint32_t offset) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 354 | { |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 355 | assert(out); |
| 356 | assert(in); |
| 357 | assert(in->data); |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 358 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 359 | buffer_clone(&out->buffer, in); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 360 | out->has_header = false; |
| 361 | |
Patrick Georgi | 2f953d3 | 2015-09-11 18:34:39 +0200 | [diff] [blame] | 362 | if (cbfs_is_valid_cbfs(out)) { |
| 363 | return 0; |
| 364 | } |
| 365 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 366 | void *header_loc = cbfs_find_header(in->data, in->size, offset); |
| 367 | if (header_loc) { |
| 368 | cbfs_get_header(&out->header, header_loc); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 369 | out->has_header = true; |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 370 | cbfs_fix_legacy_size(out, header_loc); |
Patrick Georgi | 2f953d3 | 2015-09-11 18:34:39 +0200 | [diff] [blame] | 371 | return 0; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 372 | } else if (offset != ~0u) { |
| 373 | ERROR("The -H switch is only valid on legacy images having CBFS master headers.\n"); |
| 374 | return 1; |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 375 | } |
Patrick Georgi | 2f953d3 | 2015-09-11 18:34:39 +0200 | [diff] [blame] | 376 | ERROR("Selected image region is not a valid CBFS.\n"); |
| 377 | return 1; |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 378 | } |
| 379 | |
Patrick Georgi | 214e4af | 2015-11-20 19:22:50 +0100 | [diff] [blame] | 380 | int cbfs_copy_instance(struct cbfs_image *image, struct buffer *dst) |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 381 | { |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 382 | assert(image); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 383 | |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 384 | struct cbfs_file *src_entry, *dst_entry; |
Patrick Georgi | bd0bb23 | 2015-11-20 21:48:18 +0100 | [diff] [blame] | 385 | size_t align; |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 386 | ssize_t last_entry_size; |
| 387 | |
Patrick Georgi | 214e4af | 2015-11-20 19:22:50 +0100 | [diff] [blame] | 388 | size_t copy_end = buffer_size(dst); |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 389 | |
Patrick Georgi | bd0bb23 | 2015-11-20 21:48:18 +0100 | [diff] [blame] | 390 | align = CBFS_ENTRY_ALIGNMENT; |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 391 | |
Patrick Georgi | bd0bb23 | 2015-11-20 21:48:18 +0100 | [diff] [blame] | 392 | dst_entry = (struct cbfs_file *)buffer_get(dst); |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 393 | |
| 394 | /* Copy non-empty files */ |
| 395 | for (src_entry = cbfs_find_first_entry(image); |
| 396 | src_entry && cbfs_is_valid_entry(image, src_entry); |
| 397 | src_entry = cbfs_find_next_entry(image, src_entry)) { |
| 398 | size_t entry_size; |
| 399 | |
| 400 | if ((src_entry->type == htonl(CBFS_COMPONENT_NULL)) || |
Patrick Georgi | bd0bb23 | 2015-11-20 21:48:18 +0100 | [diff] [blame] | 401 | (src_entry->type == htonl(CBFS_COMPONENT_CBFSHEADER)) || |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 402 | (src_entry->type == htonl(CBFS_COMPONENT_DELETED))) |
| 403 | continue; |
| 404 | |
| 405 | entry_size = htonl(src_entry->len) + htonl(src_entry->offset); |
| 406 | memcpy(dst_entry, src_entry, entry_size); |
| 407 | dst_entry = (struct cbfs_file *)( |
| 408 | (uintptr_t)dst_entry + align_up(entry_size, align)); |
| 409 | |
Richard Spiegel | b59c1f4 | 2018-11-02 11:14:38 -0700 | [diff] [blame] | 410 | if ((size_t)((uint8_t *)dst_entry - (uint8_t *)buffer_get(dst)) |
| 411 | >= copy_end) { |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 412 | ERROR("Ran out of room in copy region.\n"); |
| 413 | return 1; |
| 414 | } |
| 415 | } |
| 416 | |
Patrick Georgi | bd0bb23 | 2015-11-20 21:48:18 +0100 | [diff] [blame] | 417 | /* Last entry size is all the room above it, except for top 4 bytes |
| 418 | * which may be used by the master header pointer. This messes with |
| 419 | * the ability to stash something "top-aligned" into the region, but |
| 420 | * keeps things simpler. */ |
Richard Spiegel | b59c1f4 | 2018-11-02 11:14:38 -0700 | [diff] [blame] | 421 | last_entry_size = copy_end - |
| 422 | ((uint8_t *)dst_entry - (uint8_t *)buffer_get(dst)) - |
| 423 | cbfs_calculate_file_header_size("") - sizeof(int32_t); |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 424 | |
| 425 | if (last_entry_size < 0) |
| 426 | WARN("No room to create the last entry!\n") |
| 427 | else |
Patrick Georgi | edf25d9 | 2015-08-12 09:12:06 +0200 | [diff] [blame] | 428 | cbfs_create_empty_entry(dst_entry, CBFS_COMPONENT_NULL, |
| 429 | last_entry_size, ""); |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 430 | |
| 431 | return 0; |
| 432 | } |
| 433 | |
Patrick Georgi | 5d982d7 | 2017-09-19 14:39:58 +0200 | [diff] [blame] | 434 | int cbfs_expand_to_region(struct buffer *region) |
| 435 | { |
| 436 | if (buffer_get(region) == NULL) |
| 437 | return 1; |
| 438 | |
| 439 | struct cbfs_image image; |
| 440 | memset(&image, 0, sizeof(image)); |
| 441 | if (cbfs_image_from_buffer(&image, region, 0)) { |
| 442 | ERROR("reading CBFS failed!\n"); |
| 443 | return 1; |
| 444 | } |
| 445 | |
| 446 | uint32_t region_sz = buffer_size(region); |
| 447 | |
| 448 | struct cbfs_file *entry; |
| 449 | for (entry = buffer_get(region); |
| 450 | cbfs_is_valid_entry(&image, entry); |
| 451 | entry = cbfs_find_next_entry(&image, entry)) { |
| 452 | /* just iterate through */ |
| 453 | } |
| 454 | |
| 455 | /* entry now points to the first aligned address after the last valid |
| 456 | * file header. That's either outside the image or exactly the place |
| 457 | * where we need to create a new file. |
| 458 | */ |
Richard Spiegel | b59c1f4 | 2018-11-02 11:14:38 -0700 | [diff] [blame] | 459 | int last_entry_size = region_sz - |
| 460 | ((uint8_t *)entry - (uint8_t *)buffer_get(region)) - |
| 461 | cbfs_calculate_file_header_size("") - sizeof(int32_t); |
Patrick Georgi | 5d982d7 | 2017-09-19 14:39:58 +0200 | [diff] [blame] | 462 | |
| 463 | if (last_entry_size > 0) { |
| 464 | cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, |
| 465 | last_entry_size, ""); |
| 466 | /* If the last entry was an empty file, merge them. */ |
| 467 | cbfs_walk(&image, cbfs_merge_empty_entry, NULL); |
| 468 | } |
| 469 | |
| 470 | return 0; |
| 471 | } |
| 472 | |
Patrick Georgi | 12631a4 | 2017-09-20 11:59:18 +0200 | [diff] [blame] | 473 | int cbfs_truncate_space(struct buffer *region, uint32_t *size) |
| 474 | { |
| 475 | if (buffer_get(region) == NULL) |
| 476 | return 1; |
| 477 | |
| 478 | struct cbfs_image image; |
| 479 | memset(&image, 0, sizeof(image)); |
| 480 | if (cbfs_image_from_buffer(&image, region, 0)) { |
| 481 | ERROR("reading CBFS failed!\n"); |
| 482 | return 1; |
| 483 | } |
| 484 | |
| 485 | struct cbfs_file *entry, *trailer; |
| 486 | for (trailer = entry = buffer_get(region); |
| 487 | cbfs_is_valid_entry(&image, entry); |
| 488 | trailer = entry, |
| 489 | entry = cbfs_find_next_entry(&image, entry)) { |
| 490 | /* just iterate through */ |
| 491 | } |
| 492 | |
| 493 | /* trailer now points to the last valid CBFS entry's header. |
| 494 | * If that file is empty, remove it and report its header's offset as |
| 495 | * maximum size. |
| 496 | */ |
| 497 | if ((strlen(trailer->filename) != 0) && |
| 498 | (trailer->type != htonl(CBFS_COMPONENT_NULL)) && |
| 499 | (trailer->type != htonl(CBFS_COMPONENT_DELETED))) { |
| 500 | /* nothing to truncate. Return de-facto CBFS size in case it |
| 501 | * was already truncated. */ |
Richard Spiegel | b59c1f4 | 2018-11-02 11:14:38 -0700 | [diff] [blame] | 502 | *size = (uint8_t *)entry - (uint8_t *)buffer_get(region); |
Patrick Georgi | 12631a4 | 2017-09-20 11:59:18 +0200 | [diff] [blame] | 503 | return 0; |
| 504 | } |
Richard Spiegel | b59c1f4 | 2018-11-02 11:14:38 -0700 | [diff] [blame] | 505 | *size = (uint8_t *)trailer - (uint8_t *)buffer_get(region); |
Patrick Georgi | 12631a4 | 2017-09-20 11:59:18 +0200 | [diff] [blame] | 506 | memset(trailer, 0xff, buffer_size(region) - *size); |
| 507 | |
| 508 | return 0; |
| 509 | } |
| 510 | |
Aaron Durbin | 71c60ca | 2016-01-26 17:08:56 -0600 | [diff] [blame] | 511 | static size_t cbfs_file_entry_metadata_size(const struct cbfs_file *f) |
| 512 | { |
| 513 | return ntohl(f->offset); |
| 514 | } |
| 515 | |
| 516 | static size_t cbfs_file_entry_data_size(const struct cbfs_file *f) |
| 517 | { |
| 518 | return ntohl(f->len); |
| 519 | } |
| 520 | |
| 521 | static size_t cbfs_file_entry_size(const struct cbfs_file *f) |
| 522 | { |
| 523 | return cbfs_file_entry_metadata_size(f) + cbfs_file_entry_data_size(f); |
| 524 | } |
| 525 | |
| 526 | int cbfs_compact_instance(struct cbfs_image *image) |
| 527 | { |
| 528 | assert(image); |
| 529 | |
| 530 | struct cbfs_file *prev; |
| 531 | struct cbfs_file *cur; |
| 532 | |
| 533 | /* The prev entry will always be an empty entry. */ |
| 534 | prev = NULL; |
| 535 | |
| 536 | /* |
| 537 | * Note: this function does not honor alignment or fixed location files. |
| 538 | * It's behavior is akin to cbfs_copy_instance() in that it expects |
| 539 | * the caller to understand the ramifications of compacting a |
| 540 | * fragmented CBFS image. |
| 541 | */ |
| 542 | |
| 543 | for (cur = cbfs_find_first_entry(image); |
| 544 | cur && cbfs_is_valid_entry(image, cur); |
| 545 | cur = cbfs_find_next_entry(image, cur)) { |
| 546 | size_t prev_size; |
| 547 | size_t cur_size; |
| 548 | size_t empty_metadata_size; |
| 549 | size_t spill_size; |
| 550 | uint32_t type = htonl(cur->type); |
| 551 | |
| 552 | /* Current entry is empty. Kepp track of it. */ |
| 553 | if ((type == htonl(CBFS_COMPONENT_NULL)) || |
| 554 | (type == htonl(CBFS_COMPONENT_DELETED))) { |
| 555 | prev = cur; |
| 556 | continue; |
| 557 | } |
| 558 | |
| 559 | /* Need to ensure the previous entry is an empty one. */ |
| 560 | if (prev == NULL) |
| 561 | continue; |
| 562 | |
| 563 | /* At this point prev is an empty entry. Put the non-empty |
Patrick Georgi | 01cfecc | 2020-01-29 13:31:16 +0100 | [diff] [blame] | 564 | * file in prev's location. Then add a new empty entry. This |
Aaron Durbin | 71c60ca | 2016-01-26 17:08:56 -0600 | [diff] [blame] | 565 | * essentialy bubbles empty entries towards the end. */ |
| 566 | |
| 567 | prev_size = cbfs_file_entry_size(prev); |
| 568 | cur_size = cbfs_file_entry_size(cur); |
| 569 | |
| 570 | /* |
| 571 | * Adjust the empty file size by the actual space occupied |
| 572 | * bewtween the beginning of the empty file and the non-empty |
| 573 | * file. |
| 574 | */ |
| 575 | prev_size += (cbfs_get_entry_addr(image, cur) - |
| 576 | cbfs_get_entry_addr(image, prev)) - prev_size; |
| 577 | |
| 578 | /* Move the non-empty file over the empty file. */ |
| 579 | memmove(prev, cur, cur_size); |
| 580 | |
| 581 | /* |
| 582 | * Get location of the empty file. Note that since prev was |
| 583 | * overwritten with the non-empty file the previously moved |
| 584 | * file needs to be used to calculate the empty file's location. |
| 585 | */ |
| 586 | cur = cbfs_find_next_entry(image, prev); |
| 587 | |
| 588 | /* |
| 589 | * The total space to work with for swapping the 2 entries |
| 590 | * consists of the 2 files' sizes combined. However, the |
| 591 | * cbfs_file entries start on CBFS_ALIGNMENT boundaries. |
| 592 | * Because of this the empty file size may end up smaller |
| 593 | * because of the non-empty file's metadata and data length. |
| 594 | * |
| 595 | * Calculate the spill size which is the amount of data lost |
| 596 | * due to the alignment constraints after moving the non-empty |
| 597 | * file. |
| 598 | */ |
| 599 | spill_size = (cbfs_get_entry_addr(image, cur) - |
| 600 | cbfs_get_entry_addr(image, prev)) - cur_size; |
| 601 | |
| 602 | empty_metadata_size = cbfs_calculate_file_header_size(""); |
| 603 | |
| 604 | /* Check if new empty size can contain the metadata. */ |
| 605 | if (empty_metadata_size + spill_size > prev_size) { |
| 606 | ERROR("Unable to swap '%s' with prev empty entry.\n", |
| 607 | prev->filename); |
| 608 | return 1; |
| 609 | } |
| 610 | |
| 611 | /* Update the empty file's size. */ |
| 612 | prev_size -= spill_size + empty_metadata_size; |
| 613 | |
| 614 | /* Create new empty file. */ |
| 615 | cbfs_create_empty_entry(cur, CBFS_COMPONENT_NULL, |
| 616 | prev_size, ""); |
| 617 | |
| 618 | /* Merge any potential empty entries together. */ |
| 619 | cbfs_walk(image, cbfs_merge_empty_entry, NULL); |
| 620 | |
| 621 | /* |
| 622 | * Since current switched to an empty file keep track of it. |
| 623 | * Even if any empty files were merged the empty entry still |
| 624 | * starts at previously calculated location. |
| 625 | */ |
| 626 | prev = cur; |
| 627 | } |
| 628 | |
| 629 | return 0; |
| 630 | } |
| 631 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 632 | int cbfs_image_delete(struct cbfs_image *image) |
| 633 | { |
Edward O'Callaghan | a0f9ece | 2014-03-09 00:05:18 +1100 | [diff] [blame] | 634 | if (image == NULL) |
| 635 | return 0; |
| 636 | |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 637 | buffer_delete(&image->buffer); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 638 | return 0; |
| 639 | } |
| 640 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 641 | /* Tries to add an entry with its data (CBFS_SUBHEADER) at given offset. */ |
| 642 | static int cbfs_add_entry_at(struct cbfs_image *image, |
| 643 | struct cbfs_file *entry, |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 644 | const void *data, |
Patrick Georgi | 7fd1418 | 2015-08-11 15:55:16 +0200 | [diff] [blame] | 645 | uint32_t content_offset, |
Philipp Deppenwiese | 7ba5871 | 2018-11-20 13:54:49 +0100 | [diff] [blame] | 646 | const struct cbfs_file *header, |
| 647 | const size_t len_align) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 648 | { |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 649 | struct cbfs_file *next = cbfs_find_next_entry(image, entry); |
| 650 | uint32_t addr = cbfs_get_entry_addr(image, entry), |
| 651 | addr_next = cbfs_get_entry_addr(image, next); |
Patrick Georgi | 7fd1418 | 2015-08-11 15:55:16 +0200 | [diff] [blame] | 652 | uint32_t min_entry_size = cbfs_calculate_file_header_size(""); |
Patrick Georgi | 4eb8abe | 2015-08-25 12:24:49 +0200 | [diff] [blame] | 653 | uint32_t len, header_offset; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 654 | uint32_t align = image->has_header ? image->header.align : |
| 655 | CBFS_ENTRY_ALIGNMENT; |
Patrick Georgi | d5a4afa | 2015-08-25 22:27:57 +0200 | [diff] [blame] | 656 | uint32_t header_size = ntohl(header->offset); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 657 | |
Patrick Georgi | 4eb8abe | 2015-08-25 12:24:49 +0200 | [diff] [blame] | 658 | header_offset = content_offset - header_size; |
| 659 | if (header_offset % align) |
| 660 | header_offset -= header_offset % align; |
| 661 | if (header_offset < addr) { |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 662 | ERROR("No space to hold cbfs_file header."); |
| 663 | return -1; |
| 664 | } |
| 665 | |
| 666 | // Process buffer BEFORE content_offset. |
Patrick Georgi | 4eb8abe | 2015-08-25 12:24:49 +0200 | [diff] [blame] | 667 | if (header_offset - addr > min_entry_size) { |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 668 | DEBUG("|min|...|header|content|... <create new entry>\n"); |
Patrick Georgi | 4eb8abe | 2015-08-25 12:24:49 +0200 | [diff] [blame] | 669 | len = header_offset - addr - min_entry_size; |
Patrick Georgi | edf25d9 | 2015-08-12 09:12:06 +0200 | [diff] [blame] | 670 | cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, len, ""); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 671 | if (verbose > 1) cbfs_print_entry_info(image, entry, stderr); |
| 672 | entry = cbfs_find_next_entry(image, entry); |
| 673 | addr = cbfs_get_entry_addr(image, entry); |
| 674 | } |
| 675 | |
Patrick Georgi | 7a33b53 | 2015-08-25 13:00:04 +0200 | [diff] [blame] | 676 | len = content_offset - addr - header_size; |
Patrick Georgi | a60e7b6 | 2015-08-25 22:26:02 +0200 | [diff] [blame] | 677 | memcpy(entry, header, header_size); |
Patrick Georgi | 7a33b53 | 2015-08-25 13:00:04 +0200 | [diff] [blame] | 678 | if (len != 0) { |
Elyes HAOUAS | 3db0198 | 2018-08-23 18:08:20 +0200 | [diff] [blame] | 679 | /* the header moved backwards a bit to accommodate cbfs_file |
Patrick Georgi | 7a33b53 | 2015-08-25 13:00:04 +0200 | [diff] [blame] | 680 | * alignment requirements, so patch up ->offset to still point |
| 681 | * to file data. |
| 682 | */ |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 683 | DEBUG("|..|header|content|... <use offset to create entry>\n"); |
Patrick Georgi | ae7efb9 | 2015-08-25 13:11:28 +0200 | [diff] [blame] | 684 | DEBUG("before: offset=0x%x\n", ntohl(entry->offset)); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 685 | // TODO reset expanded name buffer to 0xFF. |
Patrick Georgi | 7a33b53 | 2015-08-25 13:00:04 +0200 | [diff] [blame] | 686 | entry->offset = htonl(ntohl(entry->offset) + len); |
Patrick Georgi | ae7efb9 | 2015-08-25 13:11:28 +0200 | [diff] [blame] | 687 | DEBUG("after: offset=0x%x\n", ntohl(entry->len)); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | // Ready to fill data into entry. |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 691 | DEBUG("content_offset: 0x%x, entry location: %x\n", |
| 692 | content_offset, (int)((char*)CBFS_SUBHEADER(entry) - |
| 693 | image->buffer.data)); |
| 694 | assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data == |
Patrick Georgi | cccc9d4 | 2015-04-28 13:09:36 +0200 | [diff] [blame] | 695 | (ptrdiff_t)content_offset); |
Patrick Georgi | 19c80b2 | 2015-08-25 13:16:04 +0200 | [diff] [blame] | 696 | memcpy(CBFS_SUBHEADER(entry), data, ntohl(entry->len)); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 697 | if (verbose > 1) cbfs_print_entry_info(image, entry, stderr); |
| 698 | |
Philipp Deppenwiese | 7ba5871 | 2018-11-20 13:54:49 +0100 | [diff] [blame] | 699 | // Align the length to a multiple of len_align |
| 700 | if (len_align && |
| 701 | ((ntohl(entry->offset) + ntohl(entry->len)) % len_align)) { |
| 702 | size_t off = (ntohl(entry->offset) + ntohl(entry->len)) % len_align; |
| 703 | entry->len = htonl(ntohl(entry->len) + len_align - off); |
| 704 | } |
| 705 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 706 | // Process buffer AFTER entry. |
| 707 | entry = cbfs_find_next_entry(image, entry); |
| 708 | addr = cbfs_get_entry_addr(image, entry); |
Sol Boucher | 0572565 | 2015-04-02 20:58:26 -0700 | [diff] [blame] | 709 | if (addr == addr_next) |
| 710 | return 0; |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 711 | |
Sol Boucher | 0572565 | 2015-04-02 20:58:26 -0700 | [diff] [blame] | 712 | assert(addr < addr_next); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 713 | if (addr_next - addr < min_entry_size) { |
Sol Boucher | 636cc85 | 2015-04-03 09:13:04 -0700 | [diff] [blame] | 714 | DEBUG("No need for new \"empty\" entry\n"); |
| 715 | /* No need to increase the size of the just |
| 716 | * stored file to extend to next file. Alignment |
| 717 | * of next file takes care of this. |
| 718 | */ |
| 719 | return 0; |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | len = addr_next - addr - min_entry_size; |
Patrick Georgi | 29a04d9 | 2015-11-20 23:23:44 +0100 | [diff] [blame] | 723 | /* keep space for master header pointer */ |
Richard Spiegel | b59c1f4 | 2018-11-02 11:14:38 -0700 | [diff] [blame] | 724 | if ((uint8_t *)entry + min_entry_size + len > |
| 725 | (uint8_t *)buffer_get(&image->buffer) + |
| 726 | buffer_size(&image->buffer) - sizeof(int32_t)) { |
Patrick Georgi | 29a04d9 | 2015-11-20 23:23:44 +0100 | [diff] [blame] | 727 | len -= sizeof(int32_t); |
| 728 | } |
Patrick Georgi | edf25d9 | 2015-08-12 09:12:06 +0200 | [diff] [blame] | 729 | cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, len, ""); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 730 | if (verbose > 1) cbfs_print_entry_info(image, entry, stderr); |
| 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer, |
Patrick Georgi | e590358 | 2015-08-25 13:53:42 +0200 | [diff] [blame] | 735 | uint32_t content_offset, |
Philipp Deppenwiese | 7ba5871 | 2018-11-20 13:54:49 +0100 | [diff] [blame] | 736 | struct cbfs_file *header, |
| 737 | const size_t len_align) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 738 | { |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 739 | assert(image); |
| 740 | assert(buffer); |
| 741 | assert(buffer->data); |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 742 | assert(!IS_TOP_ALIGNED_ADDRESS(content_offset)); |
| 743 | |
Patrick Georgi | a60e7b6 | 2015-08-25 22:26:02 +0200 | [diff] [blame] | 744 | const char *name = header->filename; |
Patrick Georgi | e590358 | 2015-08-25 13:53:42 +0200 | [diff] [blame] | 745 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 746 | uint32_t entry_type; |
| 747 | uint32_t addr, addr_next; |
| 748 | struct cbfs_file *entry, *next; |
Patrick Georgi | dd2d3f9 | 2015-08-12 12:29:20 +0200 | [diff] [blame] | 749 | uint32_t need_size; |
Patrick Georgi | f5252f3 | 2015-08-25 22:27:57 +0200 | [diff] [blame] | 750 | uint32_t header_size = ntohl(header->offset); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 751 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 752 | need_size = header_size + buffer->size; |
| 753 | DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n", |
| 754 | name, content_offset, header_size, buffer->size, need_size); |
| 755 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 756 | // Merge empty entries. |
| 757 | DEBUG("(trying to merge empty entries...)\n"); |
| 758 | cbfs_walk(image, cbfs_merge_empty_entry, NULL); |
| 759 | |
| 760 | for (entry = cbfs_find_first_entry(image); |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 761 | entry && cbfs_is_valid_entry(image, entry); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 762 | entry = cbfs_find_next_entry(image, entry)) { |
| 763 | |
| 764 | entry_type = ntohl(entry->type); |
| 765 | if (entry_type != CBFS_COMPONENT_NULL) |
| 766 | continue; |
| 767 | |
| 768 | addr = cbfs_get_entry_addr(image, entry); |
| 769 | next = cbfs_find_next_entry(image, entry); |
| 770 | addr_next = cbfs_get_entry_addr(image, next); |
| 771 | |
| 772 | DEBUG("cbfs_add_entry: space at 0x%x+0x%x(%d) bytes\n", |
| 773 | addr, addr_next - addr, addr_next - addr); |
Aaron Durbin | 1ebc7e9 | 2014-01-21 15:28:38 -0600 | [diff] [blame] | 774 | |
| 775 | /* Will the file fit? Don't yet worry if we have space for a new |
| 776 | * "empty" entry. We take care of that later. |
| 777 | */ |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 778 | if (addr + need_size > addr_next) |
| 779 | continue; |
| 780 | |
Patrick Georgi | aa44dbd | 2015-08-12 12:05:21 +0200 | [diff] [blame] | 781 | // Test for complicated cases |
| 782 | if (content_offset > 0) { |
| 783 | if (addr_next < content_offset) { |
| 784 | DEBUG("Not for specified offset yet"); |
| 785 | continue; |
| 786 | } else if (addr > content_offset) { |
| 787 | DEBUG("Exceed specified content_offset."); |
| 788 | break; |
| 789 | } else if (addr + header_size > content_offset) { |
| 790 | ERROR("Not enough space for header.\n"); |
| 791 | break; |
| 792 | } else if (content_offset + buffer->size > addr_next) { |
| 793 | ERROR("Not enough space for content.\n"); |
| 794 | break; |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | // TODO there are more few tricky cases that we may |
| 799 | // want to fit by altering offset. |
| 800 | |
Patrick Georgi | dd2d3f9 | 2015-08-12 12:29:20 +0200 | [diff] [blame] | 801 | if (content_offset == 0) { |
| 802 | // we tested every condition earlier under which |
| 803 | // placing the file there might fail |
| 804 | content_offset = addr + header_size; |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 805 | } |
| 806 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 807 | DEBUG("section 0x%x+0x%x for content_offset 0x%x.\n", |
| 808 | addr, addr_next - addr, content_offset); |
| 809 | |
Patrick Georgi | d5a4afa | 2015-08-25 22:27:57 +0200 | [diff] [blame] | 810 | if (cbfs_add_entry_at(image, entry, buffer->data, |
Philipp Deppenwiese | 7ba5871 | 2018-11-20 13:54:49 +0100 | [diff] [blame] | 811 | content_offset, header, len_align) == 0) { |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 812 | return 0; |
| 813 | } |
| 814 | break; |
| 815 | } |
| 816 | |
| 817 | ERROR("Could not add [%s, %zd bytes (%zd KB)@0x%x]; too big?\n", |
| 818 | buffer->name, buffer->size, buffer->size / 1024, content_offset); |
| 819 | return -1; |
| 820 | } |
| 821 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 822 | struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name) |
| 823 | { |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 824 | struct cbfs_file *entry; |
| 825 | for (entry = cbfs_find_first_entry(image); |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 826 | entry && cbfs_is_valid_entry(image, entry); |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 827 | entry = cbfs_find_next_entry(image, entry)) { |
Patrick Georgi | c569b8b | 2015-07-15 16:42:38 +0200 | [diff] [blame] | 828 | if (strcasecmp(entry->filename, name) == 0) { |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 829 | DEBUG("cbfs_get_entry: found %s\n", name); |
| 830 | return entry; |
| 831 | } |
| 832 | } |
| 833 | return NULL; |
| 834 | } |
| 835 | |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 836 | static int cbfs_stage_decompress(struct cbfs_stage *stage, struct buffer *buff) |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 837 | { |
| 838 | struct buffer reader; |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 839 | char *orig_buffer; |
| 840 | char *new_buffer; |
| 841 | size_t new_buff_sz; |
| 842 | decomp_func_ptr decompress; |
| 843 | |
| 844 | buffer_clone(&reader, buff); |
| 845 | |
| 846 | /* The stage metadata is in little endian. */ |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 847 | stage->compression = xdr_le.get32(&reader); |
| 848 | stage->entry = xdr_le.get64(&reader); |
| 849 | stage->load = xdr_le.get64(&reader); |
| 850 | stage->len = xdr_le.get32(&reader); |
| 851 | stage->memlen = xdr_le.get32(&reader); |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 852 | |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 853 | /* Create a buffer just with the uncompressed program now that the |
| 854 | * struct cbfs_stage has been peeled off. */ |
| 855 | if (stage->compression == CBFS_COMPRESS_NONE) { |
| 856 | new_buff_sz = buffer_size(buff) - sizeof(struct cbfs_stage); |
| 857 | |
| 858 | orig_buffer = buffer_get(buff); |
| 859 | new_buffer = calloc(1, new_buff_sz); |
| 860 | memcpy(new_buffer, orig_buffer + sizeof(struct cbfs_stage), |
| 861 | new_buff_sz); |
| 862 | buffer_init(buff, buff->name, new_buffer, new_buff_sz); |
| 863 | free(orig_buffer); |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 864 | return 0; |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 865 | } |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 866 | |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 867 | decompress = decompression_function(stage->compression); |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 868 | if (decompress == NULL) |
| 869 | return -1; |
| 870 | |
| 871 | orig_buffer = buffer_get(buff); |
| 872 | |
| 873 | /* This can be too big of a buffer needed, but there's no current |
| 874 | * field indicating decompressed size of data. */ |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 875 | new_buff_sz = stage->memlen; |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 876 | new_buffer = calloc(1, new_buff_sz); |
| 877 | |
| 878 | if (decompress(orig_buffer + sizeof(struct cbfs_stage), |
| 879 | (int)(buffer_size(buff) - sizeof(struct cbfs_stage)), |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 880 | new_buffer, (int)new_buff_sz, &new_buff_sz)) { |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 881 | ERROR("Couldn't decompress stage.\n"); |
| 882 | free(new_buffer); |
| 883 | return -1; |
| 884 | } |
| 885 | |
| 886 | /* Include correct size for full stage info. */ |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 887 | buffer_init(buff, buff->name, new_buffer, new_buff_sz); |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 888 | |
| 889 | /* True decompressed size is just the data size -- no metadata. */ |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 890 | stage->len = new_buff_sz; |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 891 | /* Stage is not compressed. */ |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 892 | stage->compression = CBFS_COMPRESS_NONE; |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 893 | |
| 894 | free(orig_buffer); |
| 895 | |
| 896 | return 0; |
| 897 | } |
| 898 | |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 899 | static int cbfs_payload_decompress(struct cbfs_payload_segment *segments, |
| 900 | struct buffer *buff, int num_seg) |
| 901 | { |
| 902 | struct buffer new_buffer; |
| 903 | struct buffer seg_buffer; |
| 904 | size_t new_buff_sz; |
| 905 | char *in_ptr; |
| 906 | char *out_ptr; |
| 907 | size_t new_offset; |
| 908 | decomp_func_ptr decompress; |
| 909 | |
| 910 | new_offset = num_seg * sizeof(*segments); |
| 911 | new_buff_sz = num_seg * sizeof(*segments); |
| 912 | |
| 913 | /* Find out and allocate the amount of memory occupied |
| 914 | * by the binary data */ |
| 915 | for (int i = 0; i < num_seg; i++) |
| 916 | new_buff_sz += segments[i].mem_len; |
| 917 | |
Furquan Shaikh | 58644a0 | 2016-08-05 08:27:18 -0700 | [diff] [blame] | 918 | if (buffer_create(&new_buffer, new_buff_sz, "decompressed_buff")) |
| 919 | return -1; |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 920 | |
| 921 | in_ptr = buffer_get(buff) + new_offset; |
| 922 | out_ptr = buffer_get(&new_buffer) + new_offset; |
| 923 | |
| 924 | for (int i = 0; i < num_seg; i++) { |
| 925 | struct buffer tbuff; |
| 926 | size_t decomp_size; |
| 927 | |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 928 | /* Segments BSS and ENTRY do not have binary data. */ |
| 929 | if (segments[i].type == PAYLOAD_SEGMENT_BSS || |
| 930 | segments[i].type == PAYLOAD_SEGMENT_ENTRY) { |
| 931 | continue; |
| 932 | } else if (segments[i].type == PAYLOAD_SEGMENT_PARAMS) { |
| 933 | memcpy(out_ptr, in_ptr, segments[i].len); |
| 934 | segments[i].offset = new_offset; |
| 935 | new_offset += segments[i].len; |
| 936 | in_ptr += segments[i].len; |
| 937 | out_ptr += segments[i].len; |
| 938 | segments[i].compression = CBFS_COMPRESS_NONE; |
| 939 | continue; |
| 940 | } |
| 941 | |
Joel Kitching | 72d77a9 | 2018-07-18 13:23:52 +0800 | [diff] [blame] | 942 | /* The payload uses an unknown compression algorithm. */ |
| 943 | decompress = decompression_function(segments[i].compression); |
| 944 | if (decompress == NULL) { |
| 945 | ERROR("Unknown decompression algorithm: %u\n", |
| 946 | segments[i].compression); |
| 947 | return -1; |
| 948 | } |
| 949 | |
Furquan Shaikh | 58644a0 | 2016-08-05 08:27:18 -0700 | [diff] [blame] | 950 | if (buffer_create(&tbuff, segments[i].mem_len, "segment")) { |
| 951 | buffer_delete(&new_buffer); |
| 952 | return -1; |
| 953 | } |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 954 | |
| 955 | if (decompress(in_ptr, segments[i].len, buffer_get(&tbuff), |
| 956 | (int) buffer_size(&tbuff), |
| 957 | &decomp_size)) { |
| 958 | ERROR("Couldn't decompress payload segment %u\n", i); |
| 959 | buffer_delete(&new_buffer); |
Furquan Shaikh | 9844d56 | 2016-08-05 08:32:23 -0700 | [diff] [blame] | 960 | buffer_delete(&tbuff); |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 961 | return -1; |
| 962 | } |
| 963 | |
| 964 | memcpy(out_ptr, buffer_get(&tbuff), decomp_size); |
| 965 | |
| 966 | in_ptr += segments[i].len; |
| 967 | |
| 968 | /* Update the offset of the segment. */ |
| 969 | segments[i].offset = new_offset; |
| 970 | /* True decompressed size is just the data size. No metadata */ |
| 971 | segments[i].len = decomp_size; |
| 972 | /* Segment is not compressed. */ |
| 973 | segments[i].compression = CBFS_COMPRESS_NONE; |
| 974 | |
| 975 | /* Update the offset and output buffer pointer. */ |
| 976 | new_offset += decomp_size; |
| 977 | out_ptr += decomp_size; |
| 978 | |
| 979 | buffer_delete(&tbuff); |
| 980 | } |
| 981 | |
| 982 | buffer_splice(&seg_buffer, &new_buffer, 0, 0); |
| 983 | xdr_segs(&seg_buffer, segments, num_seg); |
| 984 | |
| 985 | buffer_delete(buff); |
| 986 | *buff = new_buffer; |
| 987 | |
| 988 | return 0; |
| 989 | } |
| 990 | |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 991 | static int init_elf_from_arch(Elf64_Ehdr *ehdr, uint32_t cbfs_arch) |
| 992 | { |
| 993 | int endian; |
| 994 | int nbits; |
| 995 | int machine; |
| 996 | |
| 997 | switch (cbfs_arch) { |
| 998 | case CBFS_ARCHITECTURE_X86: |
| 999 | endian = ELFDATA2LSB; |
| 1000 | nbits = ELFCLASS32; |
| 1001 | machine = EM_386; |
| 1002 | break; |
| 1003 | case CBFS_ARCHITECTURE_ARM: |
| 1004 | endian = ELFDATA2LSB; |
| 1005 | nbits = ELFCLASS32; |
| 1006 | machine = EM_ARM; |
| 1007 | break; |
| 1008 | case CBFS_ARCHITECTURE_AARCH64: |
| 1009 | endian = ELFDATA2LSB; |
| 1010 | nbits = ELFCLASS64; |
| 1011 | machine = EM_AARCH64; |
| 1012 | break; |
| 1013 | case CBFS_ARCHITECTURE_MIPS: |
| 1014 | endian = ELFDATA2LSB; |
| 1015 | nbits = ELFCLASS32; |
| 1016 | machine = EM_MIPS; |
| 1017 | break; |
| 1018 | case CBFS_ARCHITECTURE_RISCV: |
| 1019 | endian = ELFDATA2LSB; |
| 1020 | nbits = ELFCLASS32; |
| 1021 | machine = EM_RISCV; |
| 1022 | break; |
| 1023 | default: |
| 1024 | ERROR("Unsupported arch: %x\n", cbfs_arch); |
| 1025 | return -1; |
| 1026 | } |
| 1027 | |
| 1028 | elf_init_eheader(ehdr, machine, nbits, endian); |
| 1029 | return 0; |
| 1030 | } |
| 1031 | |
| 1032 | static int cbfs_stage_make_elf(struct buffer *buff, uint32_t arch) |
| 1033 | { |
| 1034 | Elf64_Ehdr ehdr; |
| 1035 | Elf64_Shdr shdr; |
| 1036 | struct cbfs_stage stage; |
| 1037 | struct elf_writer *ew; |
| 1038 | struct buffer elf_out; |
| 1039 | size_t empty_sz; |
Aaron Durbin | 694fd13 | 2015-10-28 11:39:34 -0500 | [diff] [blame] | 1040 | int rmod_ret; |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 1041 | |
Antonello Dettori | 0b80628 | 2016-06-26 00:24:25 +0200 | [diff] [blame] | 1042 | if (arch == CBFS_ARCHITECTURE_UNKNOWN) { |
| 1043 | ERROR("You need to specify -m ARCH.\n"); |
| 1044 | return -1; |
| 1045 | } |
| 1046 | |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 1047 | if (cbfs_stage_decompress(&stage, buff)) { |
| 1048 | ERROR("Failed to decompress stage.\n"); |
| 1049 | return -1; |
| 1050 | } |
| 1051 | |
| 1052 | if (init_elf_from_arch(&ehdr, arch)) |
| 1053 | return -1; |
| 1054 | |
| 1055 | ehdr.e_entry = stage.entry; |
| 1056 | |
Aaron Durbin | 694fd13 | 2015-10-28 11:39:34 -0500 | [diff] [blame] | 1057 | /* Attempt rmodule translation first. */ |
| 1058 | rmod_ret = rmodule_stage_to_elf(&ehdr, buff); |
| 1059 | |
| 1060 | if (rmod_ret < 0) { |
| 1061 | ERROR("rmodule parsing failed\n"); |
| 1062 | return -1; |
| 1063 | } else if (rmod_ret == 0) |
| 1064 | return 0; |
| 1065 | |
| 1066 | /* Rmodule couldn't do anything with the data. Continue on with SELF. */ |
| 1067 | |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 1068 | ew = elf_writer_init(&ehdr); |
| 1069 | if (ew == NULL) { |
| 1070 | ERROR("Unable to init ELF writer.\n"); |
| 1071 | return -1; |
| 1072 | } |
| 1073 | |
| 1074 | memset(&shdr, 0, sizeof(shdr)); |
| 1075 | shdr.sh_type = SHT_PROGBITS; |
| 1076 | shdr.sh_flags = SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR; |
| 1077 | shdr.sh_addr = stage.load; |
| 1078 | shdr.sh_size = stage.len; |
| 1079 | empty_sz = stage.memlen - stage.len; |
| 1080 | |
| 1081 | if (elf_writer_add_section(ew, &shdr, buff, ".program")) { |
| 1082 | ERROR("Unable to add ELF section: .program\n"); |
| 1083 | elf_writer_destroy(ew); |
| 1084 | return -1; |
| 1085 | } |
| 1086 | |
| 1087 | if (empty_sz != 0) { |
| 1088 | struct buffer b; |
| 1089 | |
| 1090 | buffer_init(&b, NULL, NULL, 0); |
| 1091 | memset(&shdr, 0, sizeof(shdr)); |
| 1092 | shdr.sh_type = SHT_NOBITS; |
| 1093 | shdr.sh_flags = SHF_WRITE | SHF_ALLOC; |
| 1094 | shdr.sh_addr = stage.load + stage.len; |
| 1095 | shdr.sh_size = empty_sz; |
| 1096 | if (elf_writer_add_section(ew, &shdr, &b, ".empty")) { |
| 1097 | ERROR("Unable to add ELF section: .empty\n"); |
| 1098 | elf_writer_destroy(ew); |
| 1099 | return -1; |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | if (elf_writer_serialize(ew, &elf_out)) { |
| 1104 | ERROR("Unable to create ELF file from stage.\n"); |
| 1105 | elf_writer_destroy(ew); |
| 1106 | return -1; |
| 1107 | } |
| 1108 | |
| 1109 | /* Flip buffer with the created ELF one. */ |
| 1110 | buffer_delete(buff); |
| 1111 | *buff = elf_out; |
| 1112 | |
| 1113 | elf_writer_destroy(ew); |
| 1114 | |
| 1115 | return 0; |
| 1116 | } |
| 1117 | |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1118 | static int cbfs_payload_make_elf(struct buffer *buff, uint32_t arch) |
| 1119 | { |
| 1120 | Elf64_Ehdr ehdr; |
| 1121 | Elf64_Shdr shdr; |
Furquan Shaikh | 7b40517 | 2016-08-05 08:20:37 -0700 | [diff] [blame] | 1122 | struct cbfs_payload_segment *segs = NULL; |
Jonathan Neuschäfer | 5de5458 | 2016-08-09 15:01:58 +0200 | [diff] [blame] | 1123 | struct elf_writer *ew = NULL; |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1124 | struct buffer elf_out; |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1125 | int segments = 0; |
Furquan Shaikh | 7b40517 | 2016-08-05 08:20:37 -0700 | [diff] [blame] | 1126 | int retval = -1; |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1127 | |
Antonello Dettori | 0b80628 | 2016-06-26 00:24:25 +0200 | [diff] [blame] | 1128 | if (arch == CBFS_ARCHITECTURE_UNKNOWN) { |
| 1129 | ERROR("You need to specify -m ARCH.\n"); |
Furquan Shaikh | 7b40517 | 2016-08-05 08:20:37 -0700 | [diff] [blame] | 1130 | goto out; |
Antonello Dettori | 0b80628 | 2016-06-26 00:24:25 +0200 | [diff] [blame] | 1131 | } |
| 1132 | |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1133 | /* Count the number of segments inside buffer */ |
| 1134 | while (true) { |
| 1135 | uint32_t payload_type = 0; |
| 1136 | |
| 1137 | struct cbfs_payload_segment *seg; |
| 1138 | |
| 1139 | seg = buffer_get(buff); |
| 1140 | payload_type = read_be32(&seg[segments].type); |
| 1141 | |
| 1142 | if (payload_type == PAYLOAD_SEGMENT_CODE) { |
| 1143 | segments++; |
| 1144 | } else if (payload_type == PAYLOAD_SEGMENT_DATA) { |
| 1145 | segments++; |
| 1146 | } else if (payload_type == PAYLOAD_SEGMENT_BSS) { |
| 1147 | segments++; |
| 1148 | } else if (payload_type == PAYLOAD_SEGMENT_PARAMS) { |
| 1149 | segments++; |
| 1150 | } else if (payload_type == PAYLOAD_SEGMENT_ENTRY) { |
| 1151 | /* The last segment in a payload is always ENTRY as |
| 1152 | * specified by the parse_elf_to_payload() function. |
| 1153 | * Therefore there is no need to continue looking for |
| 1154 | * segments.*/ |
| 1155 | segments++; |
| 1156 | break; |
| 1157 | } else { |
| 1158 | ERROR("Unknown payload segment type: %x\n", |
| 1159 | payload_type); |
Furquan Shaikh | 7b40517 | 2016-08-05 08:20:37 -0700 | [diff] [blame] | 1160 | goto out; |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | segs = malloc(segments * sizeof(*segs)); |
| 1165 | |
| 1166 | /* Decode xdr segments */ |
| 1167 | for (int i = 0; i < segments; i++) { |
| 1168 | struct cbfs_payload_segment *serialized_seg = buffer_get(buff); |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1169 | xdr_get_seg(&segs[i], &serialized_seg[i]); |
| 1170 | } |
| 1171 | |
| 1172 | if (cbfs_payload_decompress(segs, buff, segments)) { |
| 1173 | ERROR("Failed to decompress payload.\n"); |
Furquan Shaikh | 7b40517 | 2016-08-05 08:20:37 -0700 | [diff] [blame] | 1174 | goto out; |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | if (init_elf_from_arch(&ehdr, arch)) |
Furquan Shaikh | 7b40517 | 2016-08-05 08:20:37 -0700 | [diff] [blame] | 1178 | goto out; |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1179 | |
| 1180 | ehdr.e_entry = segs[segments-1].load_addr; |
| 1181 | |
| 1182 | ew = elf_writer_init(&ehdr); |
| 1183 | if (ew == NULL) { |
| 1184 | ERROR("Unable to init ELF writer.\n"); |
Furquan Shaikh | 7b40517 | 2016-08-05 08:20:37 -0700 | [diff] [blame] | 1185 | goto out; |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | for (int i = 0; i < segments; i++) { |
| 1189 | struct buffer tbuff; |
Furquan Shaikh | f3bba44 | 2016-08-05 08:12:31 -0700 | [diff] [blame] | 1190 | size_t empty_sz = 0; |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1191 | |
| 1192 | memset(&shdr, 0, sizeof(shdr)); |
| 1193 | char *name = NULL; |
| 1194 | |
| 1195 | if (segs[i].type == PAYLOAD_SEGMENT_CODE) { |
| 1196 | shdr.sh_type = SHT_PROGBITS; |
| 1197 | shdr.sh_flags = SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR; |
| 1198 | shdr.sh_addr = segs[i].load_addr; |
| 1199 | shdr.sh_size = segs[i].len; |
| 1200 | empty_sz = segs[i].mem_len - segs[i].len; |
| 1201 | name = strdup(".text"); |
| 1202 | buffer_splice(&tbuff, buff, segs[i].offset, |
| 1203 | segs[i].len); |
| 1204 | } else if (segs[i].type == PAYLOAD_SEGMENT_DATA) { |
| 1205 | shdr.sh_type = SHT_PROGBITS; |
| 1206 | shdr.sh_flags = SHF_ALLOC | SHF_WRITE; |
| 1207 | shdr.sh_addr = segs[i].load_addr; |
| 1208 | shdr.sh_size = segs[i].len; |
| 1209 | empty_sz = segs[i].mem_len - segs[i].len; |
| 1210 | name = strdup(".data"); |
| 1211 | buffer_splice(&tbuff, buff, segs[i].offset, |
| 1212 | segs[i].len); |
| 1213 | } else if (segs[i].type == PAYLOAD_SEGMENT_BSS) { |
| 1214 | shdr.sh_type = SHT_NOBITS; |
| 1215 | shdr.sh_flags = SHF_ALLOC | SHF_WRITE; |
| 1216 | shdr.sh_addr = segs[i].load_addr; |
| 1217 | shdr.sh_size = segs[i].len; |
| 1218 | name = strdup(".bss"); |
| 1219 | buffer_splice(&tbuff, buff, 0, 0); |
| 1220 | } else if (segs[i].type == PAYLOAD_SEGMENT_PARAMS) { |
| 1221 | shdr.sh_type = SHT_NOTE; |
| 1222 | shdr.sh_flags = 0; |
| 1223 | shdr.sh_size = segs[i].len; |
| 1224 | name = strdup(".note.pinfo"); |
| 1225 | buffer_splice(&tbuff, buff, segs[i].offset, |
| 1226 | segs[i].len); |
| 1227 | } else if (segs[i].type == PAYLOAD_SEGMENT_ENTRY) { |
| 1228 | break; |
Patrick Georgi | dce629b | 2017-01-13 13:30:54 +0100 | [diff] [blame] | 1229 | } else { |
| 1230 | ERROR("unknown ELF segment type\n"); |
| 1231 | goto out; |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1232 | } |
| 1233 | |
Patrick Georgi | dce629b | 2017-01-13 13:30:54 +0100 | [diff] [blame] | 1234 | if (!name) { |
| 1235 | ERROR("out of memory\n"); |
| 1236 | goto out; |
| 1237 | } |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1238 | |
| 1239 | if (elf_writer_add_section(ew, &shdr, &tbuff, name)) { |
| 1240 | ERROR("Unable to add ELF section: %s\n", name); |
Patrick Georgi | dce629b | 2017-01-13 13:30:54 +0100 | [diff] [blame] | 1241 | free(name); |
Furquan Shaikh | 7b40517 | 2016-08-05 08:20:37 -0700 | [diff] [blame] | 1242 | goto out; |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1243 | } |
Patrick Georgi | dce629b | 2017-01-13 13:30:54 +0100 | [diff] [blame] | 1244 | free(name); |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1245 | |
| 1246 | if (empty_sz != 0) { |
| 1247 | struct buffer b; |
| 1248 | |
| 1249 | buffer_init(&b, NULL, NULL, 0); |
| 1250 | memset(&shdr, 0, sizeof(shdr)); |
| 1251 | shdr.sh_type = SHT_NOBITS; |
| 1252 | shdr.sh_flags = SHF_WRITE | SHF_ALLOC; |
| 1253 | shdr.sh_addr = segs[i].load_addr + segs[i].len; |
| 1254 | shdr.sh_size = empty_sz; |
| 1255 | name = strdup(".empty"); |
Patrick Georgi | dce629b | 2017-01-13 13:30:54 +0100 | [diff] [blame] | 1256 | if (!name) { |
| 1257 | ERROR("out of memory\n"); |
Furquan Shaikh | 7b40517 | 2016-08-05 08:20:37 -0700 | [diff] [blame] | 1258 | goto out; |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1259 | } |
Patrick Georgi | dce629b | 2017-01-13 13:30:54 +0100 | [diff] [blame] | 1260 | if (elf_writer_add_section(ew, &shdr, &b, name)) { |
| 1261 | ERROR("Unable to add ELF section: %s\n", name); |
| 1262 | free(name); |
| 1263 | goto out; |
| 1264 | } |
| 1265 | free(name); |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1266 | } |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | if (elf_writer_serialize(ew, &elf_out)) { |
| 1270 | ERROR("Unable to create ELF file from stage.\n"); |
Furquan Shaikh | 7b40517 | 2016-08-05 08:20:37 -0700 | [diff] [blame] | 1271 | goto out; |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | /* Flip buffer with the created ELF one. */ |
| 1275 | buffer_delete(buff); |
| 1276 | *buff = elf_out; |
Furquan Shaikh | 7b40517 | 2016-08-05 08:20:37 -0700 | [diff] [blame] | 1277 | retval = 0; |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1278 | |
Furquan Shaikh | 7b40517 | 2016-08-05 08:20:37 -0700 | [diff] [blame] | 1279 | out: |
| 1280 | free(segs); |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1281 | elf_writer_destroy(ew); |
Furquan Shaikh | 7b40517 | 2016-08-05 08:20:37 -0700 | [diff] [blame] | 1282 | return retval; |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1283 | } |
| 1284 | |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 1285 | int cbfs_export_entry(struct cbfs_image *image, const char *entry_name, |
Joel Kitching | 21fdd89 | 2018-08-09 17:49:52 +0800 | [diff] [blame] | 1286 | const char *filename, uint32_t arch, bool do_processing) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1287 | { |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 1288 | struct cbfs_file *entry = cbfs_get_entry(image, entry_name); |
| 1289 | struct buffer buffer; |
| 1290 | if (!entry) { |
| 1291 | ERROR("File not found: %s\n", entry_name); |
| 1292 | return -1; |
| 1293 | } |
Patrick Georgi | 23aeaff | 2015-08-26 13:01:10 +0200 | [diff] [blame] | 1294 | |
Joel Kitching | 21fdd89 | 2018-08-09 17:49:52 +0800 | [diff] [blame] | 1295 | unsigned int compressed_size = ntohl(entry->len); |
Patrick Georgi | 23aeaff | 2015-08-26 13:01:10 +0200 | [diff] [blame] | 1296 | unsigned int decompressed_size = 0; |
| 1297 | unsigned int compression = cbfs_file_get_compression_info(entry, |
| 1298 | &decompressed_size); |
Werner Zeh | bbf1df7 | 2018-11-21 13:07:50 +0100 | [diff] [blame] | 1299 | unsigned int buffer_len; |
Joel Kitching | 21fdd89 | 2018-08-09 17:49:52 +0800 | [diff] [blame] | 1300 | decomp_func_ptr decompress; |
Patrick Georgi | 23aeaff | 2015-08-26 13:01:10 +0200 | [diff] [blame] | 1301 | |
Joel Kitching | 21fdd89 | 2018-08-09 17:49:52 +0800 | [diff] [blame] | 1302 | if (do_processing) { |
| 1303 | decompress = decompression_function(compression); |
| 1304 | if (!decompress) { |
| 1305 | ERROR("looking up decompression routine failed\n"); |
| 1306 | return -1; |
| 1307 | } |
Werner Zeh | bbf1df7 | 2018-11-21 13:07:50 +0100 | [diff] [blame] | 1308 | buffer_len = decompressed_size; |
Joel Kitching | 21fdd89 | 2018-08-09 17:49:52 +0800 | [diff] [blame] | 1309 | } else { |
| 1310 | /* Force nop decompression */ |
| 1311 | decompress = decompression_function(CBFS_COMPRESS_NONE); |
Werner Zeh | bbf1df7 | 2018-11-21 13:07:50 +0100 | [diff] [blame] | 1312 | buffer_len = compressed_size; |
Patrick Georgi | 23aeaff | 2015-08-26 13:01:10 +0200 | [diff] [blame] | 1313 | } |
| 1314 | |
Joel Kitching | 21fdd89 | 2018-08-09 17:49:52 +0800 | [diff] [blame] | 1315 | LOG("Found file %.30s at 0x%x, type %.12s, compressed %d, size %d\n", |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 1316 | entry_name, cbfs_get_entry_addr(image, entry), |
Joel Kitching | 21fdd89 | 2018-08-09 17:49:52 +0800 | [diff] [blame] | 1317 | get_cbfs_entry_type_name(ntohl(entry->type)), compressed_size, |
| 1318 | decompressed_size); |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 1319 | |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 1320 | buffer_init(&buffer, strdup("(cbfs_export_entry)"), NULL, 0); |
Werner Zeh | bbf1df7 | 2018-11-21 13:07:50 +0100 | [diff] [blame] | 1321 | buffer.data = malloc(buffer_len); |
| 1322 | buffer.size = buffer_len; |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 1323 | |
Joel Kitching | 21fdd89 | 2018-08-09 17:49:52 +0800 | [diff] [blame] | 1324 | if (decompress(CBFS_SUBHEADER(entry), compressed_size, |
| 1325 | buffer.data, buffer.size, NULL)) { |
Patrick Georgi | 23aeaff | 2015-08-26 13:01:10 +0200 | [diff] [blame] | 1326 | ERROR("decompression failed for %s\n", entry_name); |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 1327 | buffer_delete(&buffer); |
Patrick Georgi | 23aeaff | 2015-08-26 13:01:10 +0200 | [diff] [blame] | 1328 | return -1; |
| 1329 | } |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 1330 | |
| 1331 | /* |
| 1332 | * The stage metadata is never compressed proper for cbfs_stage |
| 1333 | * files. The contents of the stage data can be though. Therefore |
| 1334 | * one has to do a second pass for stages to potentially decompress |
| 1335 | * the stage data to make it more meaningful. |
| 1336 | */ |
Joel Kitching | 21fdd89 | 2018-08-09 17:49:52 +0800 | [diff] [blame] | 1337 | if (do_processing) { |
| 1338 | int (*make_elf)(struct buffer *, uint32_t) = NULL; |
| 1339 | switch (ntohl(entry->type)) { |
| 1340 | case CBFS_COMPONENT_STAGE: |
| 1341 | make_elf = cbfs_stage_make_elf; |
| 1342 | break; |
| 1343 | case CBFS_COMPONENT_SELF: |
| 1344 | make_elf = cbfs_payload_make_elf; |
| 1345 | break; |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 1346 | } |
Joel Kitching | 21fdd89 | 2018-08-09 17:49:52 +0800 | [diff] [blame] | 1347 | if (make_elf && make_elf(&buffer, arch)) { |
| 1348 | ERROR("Failed to write %s into %s.\n", |
| 1349 | entry_name, filename); |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1350 | buffer_delete(&buffer); |
| 1351 | return -1; |
| 1352 | } |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 1353 | } |
| 1354 | |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 1355 | if (buffer_write_file(&buffer, filename) != 0) { |
| 1356 | ERROR("Failed to write %s into %s.\n", |
| 1357 | entry_name, filename); |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 1358 | buffer_delete(&buffer); |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 1359 | return -1; |
| 1360 | } |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 1361 | |
| 1362 | buffer_delete(&buffer); |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 1363 | INFO("Successfully dumped the file to: %s\n", filename); |
| 1364 | return 0; |
| 1365 | } |
| 1366 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1367 | int cbfs_remove_entry(struct cbfs_image *image, const char *name) |
| 1368 | { |
Patrick Georgi | 4d1c5aa | 2015-07-17 22:07:26 +0200 | [diff] [blame] | 1369 | struct cbfs_file *entry; |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 1370 | entry = cbfs_get_entry(image, name); |
| 1371 | if (!entry) { |
| 1372 | ERROR("CBFS file %s not found.\n", name); |
| 1373 | return -1; |
| 1374 | } |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 1375 | DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n", |
Patrick Georgi | c569b8b | 2015-07-15 16:42:38 +0200 | [diff] [blame] | 1376 | entry->filename, cbfs_get_entry_addr(image, entry)); |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 1377 | entry->type = htonl(CBFS_COMPONENT_DELETED); |
Patrick Georgi | 4d1c5aa | 2015-07-17 22:07:26 +0200 | [diff] [blame] | 1378 | cbfs_walk(image, cbfs_merge_empty_entry, NULL); |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 1379 | return 0; |
| 1380 | } |
| 1381 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1382 | int cbfs_print_header_info(struct cbfs_image *image) |
| 1383 | { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1384 | char *name = strdup(image->buffer.name); |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 1385 | assert(image); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1386 | printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n" |
Stefan Reinauer | 8f50e53 | 2013-11-13 14:34:57 -0800 | [diff] [blame] | 1387 | "alignment: %d bytes, architecture: %s\n\n", |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1388 | basename(name), |
| 1389 | image->buffer.size / 1024, |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 1390 | image->header.bootblocksize, |
| 1391 | image->header.romsize, |
| 1392 | image->header.offset, |
| 1393 | image->header.align, |
| 1394 | arch_to_string(image->header.architecture)); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1395 | free(name); |
| 1396 | return 0; |
| 1397 | } |
| 1398 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1399 | static int cbfs_print_stage_info(struct cbfs_stage *stage, FILE* fp) |
| 1400 | { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1401 | fprintf(fp, |
| 1402 | " %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", " |
| 1403 | "length: %d/%d\n", |
| 1404 | lookup_name_by_type(types_cbfs_compression, |
| 1405 | stage->compression, "(unknown)"), |
| 1406 | stage->entry, |
| 1407 | stage->load, |
| 1408 | stage->len, |
| 1409 | stage->memlen); |
| 1410 | return 0; |
| 1411 | } |
| 1412 | |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 1413 | static int cbfs_print_decoded_payload_segment_info( |
| 1414 | struct cbfs_payload_segment *seg, FILE *fp) |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1415 | { |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 1416 | /* The input (seg) must be already decoded by |
| 1417 | * cbfs_decode_payload_segment. |
| 1418 | */ |
| 1419 | switch (seg->type) { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1420 | case PAYLOAD_SEGMENT_CODE: |
| 1421 | case PAYLOAD_SEGMENT_DATA: |
| 1422 | fprintf(fp, " %s (%s compression, offset: 0x%x, " |
| 1423 | "load: 0x%" PRIx64 ", length: %d/%d)\n", |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 1424 | (seg->type == PAYLOAD_SEGMENT_CODE ? |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1425 | "code " : "data"), |
| 1426 | lookup_name_by_type(types_cbfs_compression, |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 1427 | seg->compression, |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1428 | "(unknown)"), |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 1429 | seg->offset, seg->load_addr, seg->len, |
| 1430 | seg->mem_len); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1431 | break; |
| 1432 | |
| 1433 | case PAYLOAD_SEGMENT_ENTRY: |
| 1434 | fprintf(fp, " entry (0x%" PRIx64 ")\n", |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 1435 | seg->load_addr); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1436 | break; |
| 1437 | |
| 1438 | case PAYLOAD_SEGMENT_BSS: |
| 1439 | fprintf(fp, " BSS (address 0x%016" PRIx64 ", " |
| 1440 | "length 0x%x)\n", |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 1441 | seg->load_addr, seg->len); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1442 | break; |
| 1443 | |
| 1444 | case PAYLOAD_SEGMENT_PARAMS: |
| 1445 | fprintf(fp, " parameters\n"); |
| 1446 | break; |
| 1447 | |
| 1448 | default: |
| 1449 | fprintf(fp, " 0x%x (%s compression, offset: 0x%x, " |
| 1450 | "load: 0x%" PRIx64 ", length: %d/%d\n", |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 1451 | seg->type, |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1452 | lookup_name_by_type(types_cbfs_compression, |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 1453 | seg->compression, |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1454 | "(unknown)"), |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 1455 | seg->offset, seg->load_addr, seg->len, |
| 1456 | seg->mem_len); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1457 | break; |
| 1458 | } |
| 1459 | return 0; |
| 1460 | } |
| 1461 | |
| 1462 | int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry, |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1463 | void *arg) |
| 1464 | { |
Patrick Georgi | c569b8b | 2015-07-15 16:42:38 +0200 | [diff] [blame] | 1465 | const char *name = entry->filename; |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1466 | struct cbfs_payload_segment *payload; |
| 1467 | FILE *fp = (FILE *)arg; |
| 1468 | |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 1469 | if (!cbfs_is_valid_entry(image, entry)) { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1470 | ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n", |
| 1471 | cbfs_get_entry_addr(image, entry)); |
| 1472 | return -1; |
| 1473 | } |
| 1474 | if (!fp) |
| 1475 | fp = stdout; |
| 1476 | |
Patrick Georgi | c82725c | 2015-08-26 12:13:03 +0200 | [diff] [blame] | 1477 | unsigned int decompressed_size = 0; |
Patrick Georgi | a71c83f | 2015-08-26 12:23:26 +0200 | [diff] [blame] | 1478 | unsigned int compression = cbfs_file_get_compression_info(entry, |
| 1479 | &decompressed_size); |
Daisuke Nojiri | 0c2f0c1 | 2017-10-26 17:40:41 -0700 | [diff] [blame] | 1480 | const char *compression_name = lookup_name_by_type( |
| 1481 | types_cbfs_compression, compression, "????"); |
Patrick Georgi | c82725c | 2015-08-26 12:13:03 +0200 | [diff] [blame] | 1482 | |
Daisuke Nojiri | 0c2f0c1 | 2017-10-26 17:40:41 -0700 | [diff] [blame] | 1483 | if (compression == CBFS_COMPRESS_NONE) |
| 1484 | fprintf(fp, "%-30s 0x%-8x %-12s %8d %-4s\n", |
Patrick Georgi | c82725c | 2015-08-26 12:13:03 +0200 | [diff] [blame] | 1485 | *name ? name : "(empty)", |
| 1486 | cbfs_get_entry_addr(image, entry), |
| 1487 | get_cbfs_entry_type_name(ntohl(entry->type)), |
| 1488 | ntohl(entry->len), |
Daisuke Nojiri | 0c2f0c1 | 2017-10-26 17:40:41 -0700 | [diff] [blame] | 1489 | compression_name |
Patrick Georgi | c82725c | 2015-08-26 12:13:03 +0200 | [diff] [blame] | 1490 | ); |
Daisuke Nojiri | 0c2f0c1 | 2017-10-26 17:40:41 -0700 | [diff] [blame] | 1491 | else |
| 1492 | fprintf(fp, "%-30s 0x%-8x %-12s %8d %-4s (%d decompressed)\n", |
| 1493 | *name ? name : "(empty)", |
| 1494 | cbfs_get_entry_addr(image, entry), |
| 1495 | get_cbfs_entry_type_name(ntohl(entry->type)), |
| 1496 | ntohl(entry->len), |
| 1497 | compression_name, |
| 1498 | decompressed_size |
| 1499 | ); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1500 | |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 1501 | struct cbfs_file_attr_hash *hash = NULL; |
| 1502 | while ((hash = cbfs_file_get_next_hash(entry, hash)) != NULL) { |
| 1503 | unsigned int hash_type = ntohl(hash->hash_type); |
Furquan Shaikh | 1d56eef | 2016-12-02 09:24:50 -0800 | [diff] [blame] | 1504 | if (hash_type >= CBFS_NUM_SUPPORTED_HASHES) { |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 1505 | fprintf(fp, "invalid hash type %d\n", hash_type); |
| 1506 | break; |
| 1507 | } |
| 1508 | size_t hash_len = widths_cbfs_hash[hash_type]; |
| 1509 | char *hash_str = bintohex(hash->hash_data, hash_len); |
| 1510 | uint8_t local_hash[hash_len]; |
| 1511 | if (vb2_digest_buffer(CBFS_SUBHEADER(entry), |
| 1512 | ntohl(entry->len), hash_type, local_hash, |
| 1513 | hash_len) != VB2_SUCCESS) { |
| 1514 | fprintf(fp, "failed to hash '%s'\n", name); |
Patrick Georgi | 862df92 | 2016-12-14 16:10:00 +0100 | [diff] [blame] | 1515 | free(hash_str); |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 1516 | break; |
| 1517 | } |
| 1518 | int valid = memcmp(local_hash, hash->hash_data, hash_len) == 0; |
| 1519 | const char *valid_str = valid ? "valid" : "invalid"; |
| 1520 | |
| 1521 | fprintf(fp, " hash %s:%s %s\n", |
| 1522 | get_hash_attr_name(hash_type), |
| 1523 | hash_str, valid_str); |
| 1524 | free(hash_str); |
| 1525 | } |
| 1526 | |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1527 | if (!verbose) |
| 1528 | return 0; |
| 1529 | |
| 1530 | DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n", |
| 1531 | cbfs_get_entry_addr(image, entry), ntohl(entry->offset), |
| 1532 | cbfs_get_entry_addr(image, entry) + ntohl(entry->offset), |
| 1533 | ntohl(entry->len)); |
| 1534 | |
| 1535 | /* note the components of the subheader may be in host order ... */ |
| 1536 | switch (ntohl(entry->type)) { |
| 1537 | case CBFS_COMPONENT_STAGE: |
| 1538 | cbfs_print_stage_info((struct cbfs_stage *) |
| 1539 | CBFS_SUBHEADER(entry), fp); |
| 1540 | break; |
| 1541 | |
Patrick Rudolph | 4f5bed5 | 2018-05-02 09:44:08 +0200 | [diff] [blame] | 1542 | case CBFS_COMPONENT_SELF: |
Paul Menzel | 831bbe8 | 2015-08-08 20:20:57 +0200 | [diff] [blame] | 1543 | payload = (struct cbfs_payload_segment *) |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1544 | CBFS_SUBHEADER(entry); |
| 1545 | while (payload) { |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 1546 | struct cbfs_payload_segment seg; |
| 1547 | cbfs_decode_payload_segment(&seg, payload); |
| 1548 | cbfs_print_decoded_payload_segment_info( |
| 1549 | &seg, fp); |
| 1550 | if (seg.type == PAYLOAD_SEGMENT_ENTRY) |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1551 | break; |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 1552 | else |
Aaron Durbin | ca63027 | 2014-08-05 10:48:20 -0500 | [diff] [blame] | 1553 | payload ++; |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1554 | } |
| 1555 | break; |
| 1556 | default: |
| 1557 | break; |
| 1558 | } |
| 1559 | return 0; |
| 1560 | } |
| 1561 | |
Aaron Durbin | 5dc628a | 2016-01-26 15:35:34 -0600 | [diff] [blame] | 1562 | static int cbfs_print_parseable_entry_info(struct cbfs_image *image, |
| 1563 | struct cbfs_file *entry, void *arg) |
| 1564 | { |
| 1565 | FILE *fp = (FILE *)arg; |
| 1566 | const char *name; |
| 1567 | const char *type; |
| 1568 | size_t offset; |
| 1569 | size_t metadata_size; |
| 1570 | size_t data_size; |
| 1571 | const char *sep = "\t"; |
| 1572 | |
| 1573 | if (!cbfs_is_valid_entry(image, entry)) { |
| 1574 | ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n", |
| 1575 | cbfs_get_entry_addr(image, entry)); |
| 1576 | return -1; |
| 1577 | } |
| 1578 | |
| 1579 | name = entry->filename; |
| 1580 | if (*name == '\0') |
| 1581 | name = "(empty)"; |
| 1582 | type = get_cbfs_entry_type_name(ntohl(entry->type)), |
| 1583 | metadata_size = ntohl(entry->offset); |
| 1584 | data_size = ntohl(entry->len); |
| 1585 | offset = cbfs_get_entry_addr(image, entry); |
| 1586 | |
| 1587 | fprintf(fp, "%s%s", name, sep); |
| 1588 | fprintf(fp, "0x%zx%s", offset, sep); |
| 1589 | fprintf(fp, "%s%s", type, sep); |
| 1590 | fprintf(fp, "0x%zx%s", metadata_size, sep); |
| 1591 | fprintf(fp, "0x%zx%s", data_size, sep); |
| 1592 | fprintf(fp, "0x%zx\n", metadata_size + data_size); |
| 1593 | |
| 1594 | return 0; |
| 1595 | } |
| 1596 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1597 | int cbfs_print_directory(struct cbfs_image *image) |
| 1598 | { |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1599 | if (cbfs_is_legacy_cbfs(image)) |
| 1600 | cbfs_print_header_info(image); |
Daisuke Nojiri | 0c2f0c1 | 2017-10-26 17:40:41 -0700 | [diff] [blame] | 1601 | printf("%-30s %-10s %-12s Size Comp\n", "Name", "Offset", "Type"); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1602 | cbfs_walk(image, cbfs_print_entry_info, NULL); |
| 1603 | return 0; |
| 1604 | } |
| 1605 | |
Aaron Durbin | 5dc628a | 2016-01-26 15:35:34 -0600 | [diff] [blame] | 1606 | int cbfs_print_parseable_directory(struct cbfs_image *image) |
| 1607 | { |
Furquan Shaikh | 161d233 | 2016-05-26 14:41:02 -0700 | [diff] [blame] | 1608 | size_t i; |
Aaron Durbin | 5dc628a | 2016-01-26 15:35:34 -0600 | [diff] [blame] | 1609 | const char *header[] = { |
| 1610 | "Name", |
| 1611 | "Offset", |
| 1612 | "Type", |
| 1613 | "Metadata Size", |
| 1614 | "Data Size", |
| 1615 | "Total Size", |
| 1616 | }; |
| 1617 | const char *sep = "\t"; |
| 1618 | |
| 1619 | for (i = 0; i < ARRAY_SIZE(header) - 1; i++) |
| 1620 | fprintf(stdout, "%s%s", header[i], sep); |
| 1621 | fprintf(stdout, "%s\n", header[i]); |
| 1622 | cbfs_walk(image, cbfs_print_parseable_entry_info, stdout); |
| 1623 | return 0; |
| 1624 | } |
| 1625 | |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1626 | int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry, |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 1627 | unused void *arg) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1628 | { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1629 | struct cbfs_file *next; |
Daisuke Nojiri | 2b59c61 | 2018-10-02 12:59:58 -0700 | [diff] [blame] | 1630 | uint32_t next_addr = 0; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1631 | |
Daisuke Nojiri | 2b59c61 | 2018-10-02 12:59:58 -0700 | [diff] [blame] | 1632 | /* We don't return here even if this entry is already empty because we |
| 1633 | want to merge the empty entries following after it. */ |
| 1634 | |
| 1635 | /* Loop until non-empty entry is found, starting from the current entry. |
| 1636 | After the loop, next_addr points to the next non-empty entry. */ |
| 1637 | next = entry; |
| 1638 | while (ntohl(next->type) == CBFS_COMPONENT_DELETED || |
| 1639 | ntohl(next->type) == CBFS_COMPONENT_NULL) { |
| 1640 | next = cbfs_find_next_entry(image, next); |
| 1641 | if (!next) |
| 1642 | break; |
| 1643 | next_addr = cbfs_get_entry_addr(image, next); |
| 1644 | if (!cbfs_is_valid_entry(image, next)) |
| 1645 | /* 'next' could be the end of cbfs */ |
| 1646 | break; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1647 | } |
Daisuke Nojiri | 2b59c61 | 2018-10-02 12:59:58 -0700 | [diff] [blame] | 1648 | |
| 1649 | if (!next_addr) |
| 1650 | /* Nothing to empty */ |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1651 | return 0; |
| 1652 | |
Daisuke Nojiri | 2b59c61 | 2018-10-02 12:59:58 -0700 | [diff] [blame] | 1653 | /* We can return here if we find only a single empty entry. |
| 1654 | For simplicity, we just proceed (and make it empty again). */ |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1655 | |
Daisuke Nojiri | 2b59c61 | 2018-10-02 12:59:58 -0700 | [diff] [blame] | 1656 | /* We're creating one empty entry for combined empty spaces */ |
| 1657 | uint32_t addr = cbfs_get_entry_addr(image, entry); |
| 1658 | size_t len = next_addr - addr - cbfs_calculate_file_header_size(""); |
| 1659 | DEBUG("join_empty_entry: [0x%x, 0x%x) len=%zu\n", addr, next_addr, len); |
| 1660 | cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, len, ""); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1661 | |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1662 | return 0; |
| 1663 | } |
| 1664 | |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1665 | int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback, |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1666 | void *arg) |
| 1667 | { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1668 | int count = 0; |
| 1669 | struct cbfs_file *entry; |
| 1670 | for (entry = cbfs_find_first_entry(image); |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 1671 | entry && cbfs_is_valid_entry(image, entry); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1672 | entry = cbfs_find_next_entry(image, entry)) { |
| 1673 | count ++; |
| 1674 | if (callback(image, entry, arg) != 0) |
| 1675 | break; |
| 1676 | } |
| 1677 | return count; |
| 1678 | } |
| 1679 | |
Patrick Georgi | 7db2b6c | 2015-11-11 15:35:24 +0100 | [diff] [blame] | 1680 | static int cbfs_header_valid(struct cbfs_header *header) |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 1681 | { |
| 1682 | if ((ntohl(header->magic) == CBFS_HEADER_MAGIC) && |
| 1683 | ((ntohl(header->version) == CBFS_HEADER_VERSION1) || |
| 1684 | (ntohl(header->version) == CBFS_HEADER_VERSION2)) && |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 1685 | (ntohl(header->offset) < ntohl(header->romsize))) |
| 1686 | return 1; |
| 1687 | return 0; |
| 1688 | } |
| 1689 | |
| 1690 | struct cbfs_header *cbfs_find_header(char *data, size_t size, |
| 1691 | uint32_t forced_offset) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1692 | { |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1693 | size_t offset; |
| 1694 | int found = 0; |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1695 | int32_t rel_offset; |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1696 | struct cbfs_header *header, *result = NULL; |
| 1697 | |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 1698 | if (forced_offset < (size - sizeof(struct cbfs_header))) { |
| 1699 | /* Check if the forced header is valid. */ |
| 1700 | header = (struct cbfs_header *)(data + forced_offset); |
Patrick Georgi | 7db2b6c | 2015-11-11 15:35:24 +0100 | [diff] [blame] | 1701 | if (cbfs_header_valid(header)) |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 1702 | return header; |
| 1703 | return NULL; |
| 1704 | } |
| 1705 | |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1706 | // Try finding relative offset of master header at end of file first. |
| 1707 | rel_offset = *(int32_t *)(data + size - sizeof(int32_t)); |
| 1708 | offset = size + rel_offset; |
| 1709 | DEBUG("relative offset: %#zx(-%#zx), offset: %#zx\n", |
| 1710 | (size_t)rel_offset, (size_t)-rel_offset, offset); |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 1711 | |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1712 | if (offset >= size - sizeof(*header) || |
Patrick Georgi | 7db2b6c | 2015-11-11 15:35:24 +0100 | [diff] [blame] | 1713 | !cbfs_header_valid((struct cbfs_header *)(data + offset))) { |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1714 | // Some use cases append non-CBFS data to the end of the ROM. |
| 1715 | DEBUG("relative offset seems wrong, scanning whole image...\n"); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1716 | offset = 0; |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1717 | } |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1718 | |
| 1719 | for (; offset + sizeof(*header) < size; offset++) { |
| 1720 | header = (struct cbfs_header *)(data + offset); |
Patrick Georgi | 7db2b6c | 2015-11-11 15:35:24 +0100 | [diff] [blame] | 1721 | if (!cbfs_header_valid(header)) |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1722 | continue; |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1723 | if (!found++) |
| 1724 | result = header; |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1725 | } |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1726 | if (found > 1) |
| 1727 | // Top-aligned images usually have a working relative offset |
| 1728 | // field, so this is more likely to happen on bottom-aligned |
| 1729 | // ones (where the first header is the "outermost" one) |
| 1730 | WARN("Multiple (%d) CBFS headers found, using the first one.\n", |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1731 | found); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1732 | return result; |
| 1733 | } |
| 1734 | |
| 1735 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1736 | struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image) |
| 1737 | { |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 1738 | assert(image); |
Patrick Georgi | 7db2b6c | 2015-11-11 15:35:24 +0100 | [diff] [blame] | 1739 | if (image->has_header) |
| 1740 | /* header.offset is relative to start of flash, not |
| 1741 | * start of region, so use it with the full image. |
| 1742 | */ |
| 1743 | return (struct cbfs_file *) |
| 1744 | (buffer_get_original_backing(&image->buffer) + |
| 1745 | image->header.offset); |
| 1746 | else |
| 1747 | return (struct cbfs_file *)buffer_get(&image->buffer); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1748 | } |
| 1749 | |
| 1750 | struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image, |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1751 | struct cbfs_file *entry) |
| 1752 | { |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1753 | uint32_t addr = cbfs_get_entry_addr(image, entry); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1754 | int align = image->has_header ? image->header.align : |
| 1755 | CBFS_ENTRY_ALIGNMENT; |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 1756 | assert(entry && cbfs_is_valid_entry(image, entry)); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1757 | addr += ntohl(entry->offset) + ntohl(entry->len); |
| 1758 | addr = align_up(addr, align); |
| 1759 | return (struct cbfs_file *)(image->buffer.data + addr); |
| 1760 | } |
| 1761 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1762 | uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry) |
| 1763 | { |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1764 | assert(image && image->buffer.data && entry); |
| 1765 | return (int32_t)((char *)entry - image->buffer.data); |
| 1766 | } |
| 1767 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1768 | int cbfs_is_valid_cbfs(struct cbfs_image *image) |
| 1769 | { |
| 1770 | return buffer_check_magic(&image->buffer, CBFS_FILE_MAGIC, |
| 1771 | strlen(CBFS_FILE_MAGIC)); |
| 1772 | } |
| 1773 | |
| 1774 | int cbfs_is_legacy_cbfs(struct cbfs_image *image) |
| 1775 | { |
| 1776 | return image->has_header; |
| 1777 | } |
| 1778 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1779 | int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry) |
| 1780 | { |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1781 | uint32_t offset = cbfs_get_entry_addr(image, entry); |
| 1782 | |
| 1783 | if (offset >= image->buffer.size) |
| 1784 | return 0; |
| 1785 | |
| 1786 | struct buffer entry_data; |
| 1787 | buffer_clone(&entry_data, &image->buffer); |
| 1788 | buffer_seek(&entry_data, offset); |
| 1789 | return buffer_check_magic(&entry_data, CBFS_FILE_MAGIC, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1790 | strlen(CBFS_FILE_MAGIC)); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1791 | } |
| 1792 | |
Patrick Georgi | 57edf16 | 2015-08-12 09:20:11 +0200 | [diff] [blame] | 1793 | struct cbfs_file *cbfs_create_file_header(int type, |
Vadim Bendebury | 45e5997 | 2014-12-23 15:59:57 -0800 | [diff] [blame] | 1794 | size_t len, const char *name) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1795 | { |
Patrick Georgi | 2c61506 | 2015-07-15 20:49:00 +0200 | [diff] [blame] | 1796 | struct cbfs_file *entry = malloc(MAX_CBFS_FILE_HEADER_BUFFER); |
| 1797 | memset(entry, CBFS_CONTENT_DEFAULT_VALUE, MAX_CBFS_FILE_HEADER_BUFFER); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1798 | memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic)); |
Patrick Georgi | edf25d9 | 2015-08-12 09:12:06 +0200 | [diff] [blame] | 1799 | entry->type = htonl(type); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1800 | entry->len = htonl(len); |
Patrick Georgi | 0d618af | 2015-07-15 18:28:23 +0200 | [diff] [blame] | 1801 | entry->attributes_offset = 0; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1802 | entry->offset = htonl(cbfs_calculate_file_header_size(name)); |
Patrick Georgi | c569b8b | 2015-07-15 16:42:38 +0200 | [diff] [blame] | 1803 | memset(entry->filename, 0, ntohl(entry->offset) - sizeof(*entry)); |
| 1804 | strcpy(entry->filename, name); |
Patrick Georgi | 57edf16 | 2015-08-12 09:20:11 +0200 | [diff] [blame] | 1805 | return entry; |
| 1806 | } |
| 1807 | |
| 1808 | int cbfs_create_empty_entry(struct cbfs_file *entry, int type, |
| 1809 | size_t len, const char *name) |
| 1810 | { |
| 1811 | struct cbfs_file *tmp = cbfs_create_file_header(type, len, name); |
| 1812 | memcpy(entry, tmp, ntohl(tmp->offset)); |
| 1813 | free(tmp); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1814 | memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len); |
| 1815 | return 0; |
| 1816 | } |
| 1817 | |
Patrick Georgi | 2c61506 | 2015-07-15 20:49:00 +0200 | [diff] [blame] | 1818 | struct cbfs_file_attribute *cbfs_file_first_attr(struct cbfs_file *file) |
| 1819 | { |
| 1820 | /* attributes_offset should be 0 when there is no attribute, but all |
| 1821 | * values that point into the cbfs_file header are invalid, too. */ |
| 1822 | if (ntohl(file->attributes_offset) <= sizeof(*file)) |
| 1823 | return NULL; |
| 1824 | |
| 1825 | /* There needs to be enough space for the file header and one |
| 1826 | * attribute header for this to make sense. */ |
| 1827 | if (ntohl(file->offset) <= |
| 1828 | sizeof(*file) + sizeof(struct cbfs_file_attribute)) |
| 1829 | return NULL; |
| 1830 | |
| 1831 | return (struct cbfs_file_attribute *) |
| 1832 | (((uint8_t *)file) + ntohl(file->attributes_offset)); |
| 1833 | } |
| 1834 | |
| 1835 | struct cbfs_file_attribute *cbfs_file_next_attr(struct cbfs_file *file, |
| 1836 | struct cbfs_file_attribute *attr) |
| 1837 | { |
| 1838 | /* ex falso sequitur quodlibet */ |
| 1839 | if (attr == NULL) |
| 1840 | return NULL; |
| 1841 | |
| 1842 | /* Is there enough space for another attribute? */ |
| 1843 | if ((uint8_t *)attr + ntohl(attr->len) + |
Patrick Rudolph | e28fa40 | 2019-02-14 11:19:07 +0100 | [diff] [blame] | 1844 | sizeof(struct cbfs_file_attribute) > |
Patrick Georgi | 2c61506 | 2015-07-15 20:49:00 +0200 | [diff] [blame] | 1845 | (uint8_t *)file + ntohl(file->offset)) |
| 1846 | return NULL; |
| 1847 | |
| 1848 | struct cbfs_file_attribute *next = (struct cbfs_file_attribute *) |
| 1849 | (((uint8_t *)attr) + ntohl(attr->len)); |
| 1850 | /* If any, "unused" attributes must come last. */ |
| 1851 | if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED) |
| 1852 | return NULL; |
| 1853 | if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED2) |
| 1854 | return NULL; |
| 1855 | |
| 1856 | return next; |
| 1857 | } |
| 1858 | |
| 1859 | struct cbfs_file_attribute *cbfs_add_file_attr(struct cbfs_file *header, |
| 1860 | uint32_t tag, |
| 1861 | uint32_t size) |
| 1862 | { |
| 1863 | struct cbfs_file_attribute *attr, *next; |
| 1864 | next = cbfs_file_first_attr(header); |
| 1865 | do { |
| 1866 | attr = next; |
| 1867 | next = cbfs_file_next_attr(header, attr); |
| 1868 | } while (next != NULL); |
| 1869 | uint32_t header_size = ntohl(header->offset) + size; |
| 1870 | if (header_size > MAX_CBFS_FILE_HEADER_BUFFER) { |
| 1871 | DEBUG("exceeding allocated space for cbfs_file headers"); |
| 1872 | return NULL; |
| 1873 | } |
| 1874 | /* attr points to the last valid attribute now. |
| 1875 | * If NULL, we have to create the first one. */ |
| 1876 | if (attr == NULL) { |
| 1877 | /* New attributes start where the header ends. |
Elyes HAOUAS | 3db0198 | 2018-08-23 18:08:20 +0200 | [diff] [blame] | 1878 | * header->offset is later set to accommodate the |
Patrick Georgi | 2c61506 | 2015-07-15 20:49:00 +0200 | [diff] [blame] | 1879 | * additional structure. |
Elyes HAOUAS | 3db0198 | 2018-08-23 18:08:20 +0200 | [diff] [blame] | 1880 | * No endianness translation necessary here, because both |
Patrick Georgi | 2c61506 | 2015-07-15 20:49:00 +0200 | [diff] [blame] | 1881 | * fields are encoded the same way. */ |
| 1882 | header->attributes_offset = header->offset; |
| 1883 | attr = (struct cbfs_file_attribute *) |
| 1884 | (((uint8_t *)header) + |
| 1885 | ntohl(header->attributes_offset)); |
| 1886 | } else { |
| 1887 | attr = (struct cbfs_file_attribute *) |
| 1888 | (((uint8_t *)attr) + |
| 1889 | ntohl(attr->len)); |
| 1890 | } |
| 1891 | header->offset = htonl(header_size); |
| 1892 | memset(attr, CBFS_CONTENT_DEFAULT_VALUE, size); |
| 1893 | attr->tag = htonl(tag); |
| 1894 | attr->len = htonl(size); |
| 1895 | return attr; |
| 1896 | } |
| 1897 | |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 1898 | int cbfs_add_file_hash(struct cbfs_file *header, struct buffer *buffer, |
| 1899 | enum vb2_hash_algorithm hash_type) |
| 1900 | { |
zbao | 37450ff | 2015-11-05 14:35:57 +0800 | [diff] [blame] | 1901 | uint32_t hash_index = hash_type; |
| 1902 | |
| 1903 | if (hash_index >= CBFS_NUM_SUPPORTED_HASHES) |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 1904 | return -1; |
| 1905 | |
| 1906 | unsigned hash_size = widths_cbfs_hash[hash_type]; |
| 1907 | if (hash_size == 0) |
| 1908 | return -1; |
| 1909 | |
| 1910 | struct cbfs_file_attr_hash *attrs = |
| 1911 | (struct cbfs_file_attr_hash *)cbfs_add_file_attr(header, |
| 1912 | CBFS_FILE_ATTR_TAG_HASH, |
| 1913 | sizeof(struct cbfs_file_attr_hash) + hash_size); |
| 1914 | |
| 1915 | if (attrs == NULL) |
| 1916 | return -1; |
| 1917 | |
| 1918 | attrs->hash_type = htonl(hash_type); |
| 1919 | if (vb2_digest_buffer(buffer_get(buffer), buffer_size(buffer), |
| 1920 | hash_type, attrs->hash_data, hash_size) != VB2_SUCCESS) |
| 1921 | return -1; |
| 1922 | |
| 1923 | return 0; |
| 1924 | } |
| 1925 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1926 | /* Finds a place to hold whole data in same memory page. */ |
| 1927 | static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page) |
| 1928 | { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1929 | if (!page) |
| 1930 | return 1; |
| 1931 | return (start / page) == (start + size - 1) / page; |
| 1932 | } |
| 1933 | |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1934 | /* Tests if data can fit in a range by given offset: |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1935 | * start ->| metadata_size | offset (+ size) |<- end |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1936 | */ |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1937 | static int is_in_range(size_t start, size_t end, size_t metadata_size, |
| 1938 | size_t offset, size_t size) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1939 | { |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1940 | return (offset >= start + metadata_size && offset + size <= end); |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1941 | } |
| 1942 | |
Werner Zeh | 95bfcae | 2016-01-25 12:47:20 +0100 | [diff] [blame] | 1943 | static size_t absolute_align(const struct cbfs_image *image, size_t val, |
| 1944 | size_t align) |
| 1945 | { |
| 1946 | const size_t region_offset = buffer_offset(&image->buffer); |
| 1947 | /* To perform alignment on absolute address, take the region offset */ |
| 1948 | /* of the image into account. */ |
| 1949 | return align_up(val + region_offset, align) - region_offset; |
| 1950 | |
| 1951 | } |
| 1952 | |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1953 | int32_t cbfs_locate_entry(struct cbfs_image *image, size_t size, |
| 1954 | size_t page_size, size_t align, size_t metadata_size) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1955 | { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1956 | struct cbfs_file *entry; |
| 1957 | size_t need_len; |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1958 | size_t addr, addr_next, addr2, addr3, offset; |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1959 | |
| 1960 | /* Default values: allow fitting anywhere in ROM. */ |
| 1961 | if (!page_size) |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1962 | page_size = image->has_header ? image->header.romsize : |
| 1963 | image->buffer.size; |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1964 | if (!align) |
| 1965 | align = 1; |
| 1966 | |
| 1967 | if (size > page_size) |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1968 | ERROR("Input file size (%zd) greater than page size (%zd).\n", |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1969 | size, page_size); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1970 | |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1971 | size_t image_align = image->has_header ? image->header.align : |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1972 | CBFS_ENTRY_ALIGNMENT; |
| 1973 | if (page_size % image_align) |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1974 | WARN("%s: Page size (%#zx) not aligned with CBFS image (%#zx).\n", |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1975 | __func__, page_size, image_align); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1976 | |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1977 | need_len = metadata_size + size; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1978 | |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1979 | // Merge empty entries to build get max available space. |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1980 | cbfs_walk(image, cbfs_merge_empty_entry, NULL); |
| 1981 | |
| 1982 | /* Three cases of content location on memory page: |
| 1983 | * case 1. |
| 1984 | * | PAGE 1 | PAGE 2 | |
| 1985 | * | <header><content>| Fit. Return start of content. |
| 1986 | * |
| 1987 | * case 2. |
| 1988 | * | PAGE 1 | PAGE 2 | |
| 1989 | * | <header><content> | Fits when we shift content to align |
| 1990 | * shift-> | <header>|<content> | at starting of PAGE 2. |
| 1991 | * |
| 1992 | * case 3. (large content filling whole page) |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1993 | * | PAGE 1 | PAGE 2 | PAGE 3 | |
| 1994 | * | <header>< content > | Can't fit. If we shift content to |
| 1995 | * |trial-> <header>< content > | PAGE 2, header can't fit in free |
| 1996 | * | shift-> <header><content> space, so we must use PAGE 3. |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1997 | * |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1998 | * The returned address can be then used as "base-address" (-b) in add-* |
| 1999 | * commands (will be re-calculated and positioned by cbfs_add_entry_at). |
| 2000 | * For stage targets, the address is also used to re-link stage before |
| 2001 | * being added into CBFS. |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 2002 | */ |
| 2003 | for (entry = cbfs_find_first_entry(image); |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 2004 | entry && cbfs_is_valid_entry(image, entry); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 2005 | entry = cbfs_find_next_entry(image, entry)) { |
| 2006 | |
| 2007 | uint32_t type = ntohl(entry->type); |
| 2008 | if (type != CBFS_COMPONENT_NULL) |
| 2009 | continue; |
| 2010 | |
| 2011 | addr = cbfs_get_entry_addr(image, entry); |
| 2012 | addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry( |
| 2013 | image, entry)); |
| 2014 | if (addr_next - addr < need_len) |
| 2015 | continue; |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 2016 | |
Werner Zeh | 95bfcae | 2016-01-25 12:47:20 +0100 | [diff] [blame] | 2017 | offset = absolute_align(image, addr + metadata_size, align); |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 2018 | if (is_in_same_page(offset, size, page_size) && |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 2019 | is_in_range(addr, addr_next, metadata_size, offset, size)) { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 2020 | DEBUG("cbfs_locate_entry: FIT (PAGE1)."); |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 2021 | return offset; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 2022 | } |
| 2023 | |
| 2024 | addr2 = align_up(addr, page_size); |
Werner Zeh | 95bfcae | 2016-01-25 12:47:20 +0100 | [diff] [blame] | 2025 | offset = absolute_align(image, addr2, align); |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 2026 | if (is_in_range(addr, addr_next, metadata_size, offset, size)) { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 2027 | DEBUG("cbfs_locate_entry: OVERLAP (PAGE2)."); |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 2028 | return offset; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 2029 | } |
| 2030 | |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 2031 | /* Assume page_size >= metadata_size so adding one page will |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 2032 | * definitely provide the space for header. */ |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 2033 | assert(page_size >= metadata_size); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 2034 | addr3 = addr2 + page_size; |
Werner Zeh | 95bfcae | 2016-01-25 12:47:20 +0100 | [diff] [blame] | 2035 | offset = absolute_align(image, addr3, align); |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 2036 | if (is_in_range(addr, addr_next, metadata_size, offset, size)) { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 2037 | DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3)."); |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 2038 | return offset; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 2039 | } |
| 2040 | } |
| 2041 | return -1; |
| 2042 | } |