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