Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1 | /* |
| 2 | * CBFS Image Manipulation |
| 3 | * |
| 4 | * Copyright (C) 2013 The Chromium OS Authors. All rights reserved. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
Patrick Georgi | b890a12 | 2015-03-26 15:17:45 +0100 | [diff] [blame] | 17 | * Foundation, Inc. |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 18 | */ |
| 19 | |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 20 | #include <inttypes.h> |
| 21 | #include <libgen.h> |
Patrick Georgi | cccc9d4 | 2015-04-28 13:09:36 +0200 | [diff] [blame] | 22 | #include <stddef.h> |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 26 | #include <strings.h> |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 27 | |
| 28 | #include "common.h" |
| 29 | #include "cbfs_image.h" |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame^] | 30 | #include "elfparsing.h" |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 31 | |
Sol Boucher | 636cc85 | 2015-04-03 09:13:04 -0700 | [diff] [blame] | 32 | /* Even though the file-adding functions---cbfs_add_entry() and |
| 33 | * cbfs_add_entry_at()---perform their sizing checks against the beginning of |
| 34 | * the subsequent section rather than a stable recorded value such as an empty |
| 35 | * file header's len field, it's possible to prove two interesting properties |
| 36 | * about their behavior: |
| 37 | * - Placing a new file within an empty entry located below an existing file |
| 38 | * entry will never leave an aligned flash address containing neither the |
| 39 | * beginning of a file header nor part of a file. |
| 40 | * - Placing a new file in an empty entry at the very end of the image such |
| 41 | * that it fits, but leaves no room for a final header, is guaranteed not to |
| 42 | * change the total amount of space for entries, even if that new file is |
| 43 | * later removed from the CBFS. |
| 44 | * These properties are somewhat nonobvious from the implementation, so the |
| 45 | * reader is encouraged to blame this comment and examine the full proofs |
| 46 | * in the commit message before making significant changes that would risk |
| 47 | * removing said guarantees. |
| 48 | */ |
| 49 | |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 50 | /* The file name align is not defined in CBFS spec -- only a preference by |
| 51 | * (old) cbfstool. */ |
| 52 | #define CBFS_FILENAME_ALIGN (16) |
| 53 | |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 54 | /* Type and format */ |
| 55 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 56 | static const struct typedesc_t types_cbfs_compression[] = { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 57 | {CBFS_COMPRESS_NONE, "none"}, |
| 58 | {CBFS_COMPRESS_LZMA, "LZMA"}, |
Sol Boucher | 5bb90e6 | 2015-05-07 21:00:05 -0700 | [diff] [blame] | 59 | {0, NULL} |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 60 | }; |
| 61 | |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 62 | 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] | 63 | const char *default_value) |
| 64 | { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 65 | int i; |
| 66 | for (i = 0; desc[i].name; i++) |
| 67 | if (desc[i].type == type) |
| 68 | return desc[i].name; |
| 69 | return default_value; |
| 70 | } |
| 71 | |
Sol Boucher | ec42486 | 2015-05-07 21:00:05 -0700 | [diff] [blame] | 72 | static int lookup_type_by_name(const struct typedesc_t *desc, const char *name) |
| 73 | { |
| 74 | int i; |
| 75 | for (i = 0; desc[i].name && strcasecmp(name, desc[i].name); ++i); |
| 76 | return desc[i].name ? (int)desc[i].type : -1; |
| 77 | } |
| 78 | |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 79 | static const char *get_cbfs_entry_type_name(uint32_t type) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 80 | { |
Patrick Georgi | dc37dab | 2015-09-09 16:46:00 +0200 | [diff] [blame] | 81 | return lookup_name_by_type(filetypes, type, "(unknown)"); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 82 | } |
| 83 | |
Sol Boucher | ec42486 | 2015-05-07 21:00:05 -0700 | [diff] [blame] | 84 | int cbfs_parse_comp_algo(const char *name) |
| 85 | { |
| 86 | return lookup_type_by_name(types_cbfs_compression, name); |
| 87 | } |
| 88 | |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 89 | static const char *get_hash_attr_name(uint16_t hash_type) |
| 90 | { |
| 91 | return lookup_name_by_type(types_cbfs_hash, hash_type, "(invalid)"); |
| 92 | } |
| 93 | |
| 94 | int cbfs_parse_hash_algo(const char *name) |
| 95 | { |
| 96 | return lookup_type_by_name(types_cbfs_hash, name); |
| 97 | } |
| 98 | |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 99 | /* CBFS image */ |
| 100 | |
Patrick Georgi | 11ee08f | 2015-08-11 15:10:02 +0200 | [diff] [blame] | 101 | size_t cbfs_calculate_file_header_size(const char *name) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 102 | { |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 103 | return (sizeof(struct cbfs_file) + |
| 104 | align_up(strlen(name) + 1, CBFS_FILENAME_ALIGN)); |
| 105 | } |
| 106 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 107 | /* Only call on legacy CBFSes possessing a master header. */ |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 108 | 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] | 109 | { |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 110 | assert(image); |
| 111 | assert(cbfs_is_legacy_cbfs(image)); |
Hung-Te Lin | 49fcd75 | 2013-01-29 03:16:20 +0800 | [diff] [blame] | 112 | // A bug in old cbfstool may produce extra few bytes (by alignment) and |
| 113 | // cause cbfstool to overwrite things after free space -- which is |
| 114 | // usually CBFS header on x86. We need to workaround that. |
| 115 | |
| 116 | struct cbfs_file *entry, *first = NULL, *last = NULL; |
| 117 | for (first = entry = cbfs_find_first_entry(image); |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 118 | entry && cbfs_is_valid_entry(image, entry); |
Hung-Te Lin | 49fcd75 | 2013-01-29 03:16:20 +0800 | [diff] [blame] | 119 | entry = cbfs_find_next_entry(image, entry)) { |
| 120 | last = entry; |
| 121 | } |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 122 | if ((char *)first < (char *)hdr_loc && |
| 123 | (char *)entry > (char *)hdr_loc) { |
Hung-Te Lin | 49fcd75 | 2013-01-29 03:16:20 +0800 | [diff] [blame] | 124 | WARN("CBFS image was created with old cbfstool with size bug. " |
| 125 | "Fixing size in last entry...\n"); |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 126 | last->len = htonl(ntohl(last->len) - image->header.align); |
Hung-Te Lin | 49fcd75 | 2013-01-29 03:16:20 +0800 | [diff] [blame] | 127 | DEBUG("Last entry has been changed from 0x%x to 0x%x.\n", |
| 128 | cbfs_get_entry_addr(image, entry), |
| 129 | cbfs_get_entry_addr(image, |
| 130 | cbfs_find_next_entry(image, last))); |
| 131 | } |
| 132 | return 0; |
| 133 | } |
| 134 | |
Ronald G. Minnich | b5adeee | 2014-01-06 08:38:15 -0800 | [diff] [blame] | 135 | void cbfs_put_header(void *dest, const struct cbfs_header *header) |
| 136 | { |
| 137 | struct buffer outheader; |
| 138 | |
| 139 | outheader.data = dest; |
| 140 | outheader.size = 0; |
| 141 | |
| 142 | xdr_be.put32(&outheader, header->magic); |
| 143 | xdr_be.put32(&outheader, header->version); |
| 144 | xdr_be.put32(&outheader, header->romsize); |
| 145 | xdr_be.put32(&outheader, header->bootblocksize); |
| 146 | xdr_be.put32(&outheader, header->align); |
| 147 | xdr_be.put32(&outheader, header->offset); |
| 148 | xdr_be.put32(&outheader, header->architecture); |
| 149 | } |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 150 | |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 151 | static void cbfs_decode_payload_segment(struct cbfs_payload_segment *output, |
| 152 | struct cbfs_payload_segment *input) |
| 153 | { |
| 154 | struct buffer seg = { |
| 155 | .data = (void *)input, |
| 156 | .size = sizeof(*input), |
| 157 | }; |
| 158 | output->type = xdr_be.get32(&seg); |
| 159 | output->compression = xdr_be.get32(&seg); |
| 160 | output->offset = xdr_be.get32(&seg); |
| 161 | output->load_addr = xdr_be.get64(&seg); |
| 162 | output->len = xdr_be.get32(&seg); |
| 163 | output->mem_len = xdr_be.get32(&seg); |
| 164 | assert(seg.size == 0); |
| 165 | } |
| 166 | |
Patrick Georgi | a71c83f | 2015-08-26 12:23:26 +0200 | [diff] [blame] | 167 | static int cbfs_file_get_compression_info(struct cbfs_file *entry, |
| 168 | uint32_t *decompressed_size) |
| 169 | { |
| 170 | unsigned int compression = CBFS_COMPRESS_NONE; |
| 171 | *decompressed_size = ntohl(entry->len); |
| 172 | for (struct cbfs_file_attribute *attr = cbfs_file_first_attr(entry); |
| 173 | attr != NULL; |
| 174 | attr = cbfs_file_next_attr(entry, attr)) { |
| 175 | if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_COMPRESSION) { |
| 176 | struct cbfs_file_attr_compression *ac = |
| 177 | (struct cbfs_file_attr_compression *)attr; |
| 178 | compression = ntohl(ac->compression); |
| 179 | if (decompressed_size) |
| 180 | *decompressed_size = |
| 181 | ntohl(ac->decompressed_size); |
| 182 | } |
| 183 | } |
| 184 | return compression; |
| 185 | } |
| 186 | |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 187 | static struct cbfs_file_attr_hash *cbfs_file_get_next_hash( |
| 188 | struct cbfs_file *entry, struct cbfs_file_attr_hash *cur) |
| 189 | { |
| 190 | struct cbfs_file_attribute *attr = (struct cbfs_file_attribute *)cur; |
| 191 | if (attr == NULL) { |
| 192 | attr = cbfs_file_first_attr(entry); |
| 193 | if (attr == NULL) |
| 194 | return NULL; |
| 195 | if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_HASH) |
| 196 | return (struct cbfs_file_attr_hash *)attr; |
| 197 | } |
| 198 | while ((attr = cbfs_file_next_attr(entry, attr)) != NULL) { |
| 199 | if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_HASH) |
| 200 | return (struct cbfs_file_attr_hash *)attr; |
| 201 | }; |
| 202 | return NULL; |
| 203 | } |
| 204 | |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 205 | void cbfs_get_header(struct cbfs_header *header, void *src) |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 206 | { |
| 207 | struct buffer outheader; |
| 208 | |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 209 | outheader.data = src; /* We're not modifying the data */ |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 210 | outheader.size = 0; |
| 211 | |
| 212 | header->magic = xdr_be.get32(&outheader); |
| 213 | header->version = xdr_be.get32(&outheader); |
| 214 | header->romsize = xdr_be.get32(&outheader); |
| 215 | header->bootblocksize = xdr_be.get32(&outheader); |
| 216 | header->align = xdr_be.get32(&outheader); |
| 217 | header->offset = xdr_be.get32(&outheader); |
| 218 | header->architecture = xdr_be.get32(&outheader); |
| 219 | } |
| 220 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 221 | int cbfs_image_create(struct cbfs_image *image, size_t entries_size) |
| 222 | { |
| 223 | assert(image); |
| 224 | assert(image->buffer.data); |
| 225 | |
| 226 | size_t empty_header_len = cbfs_calculate_file_header_size(""); |
| 227 | uint32_t entries_offset = 0; |
| 228 | uint32_t align = CBFS_ENTRY_ALIGNMENT; |
| 229 | if (image->has_header) { |
| 230 | entries_offset = image->header.offset; |
| 231 | |
| 232 | if (entries_offset > image->buffer.size) { |
| 233 | ERROR("CBFS file entries are located outside CBFS itself\n"); |
| 234 | return -1; |
| 235 | } |
| 236 | |
| 237 | align = image->header.align; |
| 238 | } |
| 239 | |
| 240 | // This attribute must be given in order to prove that this module |
| 241 | // correctly preserves certain CBFS properties. See the block comment |
| 242 | // near the top of this file (and the associated commit message). |
| 243 | if (align < empty_header_len) { |
| 244 | ERROR("CBFS must be aligned to at least %zu bytes\n", |
| 245 | empty_header_len); |
| 246 | return -1; |
| 247 | } |
| 248 | |
| 249 | if (entries_size > image->buffer.size - entries_offset) { |
| 250 | ERROR("CBFS doesn't have enough space to fit its file entries\n"); |
| 251 | return -1; |
| 252 | } |
| 253 | |
| 254 | if (empty_header_len > entries_size) { |
| 255 | ERROR("CBFS is too small to fit any header\n"); |
| 256 | return -1; |
| 257 | } |
| 258 | struct cbfs_file *entry_header = |
| 259 | (struct cbfs_file *)(image->buffer.data + entries_offset); |
| 260 | // This alignment is necessary in order to prove that this module |
| 261 | // correctly preserves certain CBFS properties. See the block comment |
| 262 | // near the top of this file (and the associated commit message). |
| 263 | entries_size -= entries_size % align; |
| 264 | |
| 265 | size_t capacity = entries_size - empty_header_len; |
| 266 | LOG("Created CBFS (capacity = %zu bytes)\n", capacity); |
Patrick Georgi | edf25d9 | 2015-08-12 09:12:06 +0200 | [diff] [blame] | 267 | return cbfs_create_empty_entry(entry_header, CBFS_COMPONENT_NULL, |
| 268 | capacity, ""); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | int cbfs_legacy_image_create(struct cbfs_image *image, |
| 272 | uint32_t architecture, |
| 273 | uint32_t align, |
| 274 | struct buffer *bootblock, |
| 275 | uint32_t bootblock_offset, |
| 276 | uint32_t header_offset, |
| 277 | uint32_t entries_offset) |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 278 | { |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 279 | assert(image); |
| 280 | assert(image->buffer.data); |
| 281 | assert(bootblock); |
| 282 | |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 283 | int32_t *rel_offset; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 284 | uint32_t cbfs_len; |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 285 | void *header_loc; |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 286 | size_t size = image->buffer.size; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 287 | |
| 288 | DEBUG("cbfs_image_create: bootblock=0x%x+0x%zx, " |
| 289 | "header=0x%x+0x%zx, entries_offset=0x%x\n", |
Sol Boucher | 5bad395 | 2015-05-05 20:35:26 -0700 | [diff] [blame] | 290 | bootblock_offset, bootblock->size, header_offset, |
| 291 | sizeof(image->header), entries_offset); |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 292 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 293 | // Adjust legacy top-aligned address to ROM offset. |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 294 | if (IS_TOP_ALIGNED_ADDRESS(entries_offset)) |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 295 | entries_offset = size + (int32_t)entries_offset; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 296 | if (IS_TOP_ALIGNED_ADDRESS(bootblock_offset)) |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 297 | bootblock_offset = size + (int32_t)bootblock_offset; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 298 | if (IS_TOP_ALIGNED_ADDRESS(header_offset)) |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 299 | header_offset = size + (int32_t)header_offset; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 300 | |
| 301 | DEBUG("cbfs_create_image: (real offset) bootblock=0x%x, " |
| 302 | "header=0x%x, entries_offset=0x%x\n", |
| 303 | bootblock_offset, header_offset, entries_offset); |
| 304 | |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 305 | // Prepare bootblock |
| 306 | if (bootblock_offset + bootblock->size > size) { |
| 307 | ERROR("Bootblock (0x%x+0x%zx) exceed ROM size (0x%zx)\n", |
| 308 | bootblock_offset, bootblock->size, size); |
| 309 | return -1; |
| 310 | } |
Hung-Te Lin | c5ff648 | 2013-02-06 12:41:49 +0800 | [diff] [blame] | 311 | if (entries_offset > bootblock_offset && |
| 312 | entries_offset < bootblock->size) { |
| 313 | ERROR("Bootblock (0x%x+0x%zx) overlap CBFS data (0x%x)\n", |
| 314 | bootblock_offset, bootblock->size, entries_offset); |
| 315 | return -1; |
| 316 | } |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 317 | memcpy(image->buffer.data + bootblock_offset, bootblock->data, |
| 318 | bootblock->size); |
| 319 | |
| 320 | // Prepare header |
Sol Boucher | 5bad395 | 2015-05-05 20:35:26 -0700 | [diff] [blame] | 321 | if (header_offset + sizeof(image->header) > size - sizeof(int32_t)) { |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 322 | ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%zx)\n", |
Sol Boucher | 5bad395 | 2015-05-05 20:35:26 -0700 | [diff] [blame] | 323 | header_offset, sizeof(image->header), size); |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 324 | return -1; |
| 325 | } |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 326 | image->header.magic = CBFS_HEADER_MAGIC; |
| 327 | image->header.version = CBFS_HEADER_VERSION; |
| 328 | image->header.romsize = size; |
| 329 | image->header.bootblocksize = bootblock->size; |
| 330 | image->header.align = align; |
| 331 | image->header.offset = entries_offset; |
| 332 | image->header.architecture = architecture; |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 333 | |
| 334 | header_loc = (image->buffer.data + header_offset); |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 335 | cbfs_put_header(header_loc, &image->header); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 336 | image->has_header = true; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 337 | |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 338 | // The last 4 byte of the image contain the relative offset from the end |
| 339 | // of the image to the master header as a 32-bit signed integer. x86 |
| 340 | // relies on this also being its (memory-mapped, top-aligned) absolute |
| 341 | // 32-bit address by virtue of how two's complement numbers work. |
| 342 | assert(size % sizeof(int32_t) == 0); |
| 343 | rel_offset = (int32_t *)(image->buffer.data + size - sizeof(int32_t)); |
| 344 | *rel_offset = header_offset - size; |
| 345 | |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 346 | // Prepare entries |
| 347 | if (align_up(entries_offset, align) != entries_offset) { |
| 348 | ERROR("Offset (0x%x) must be aligned to 0x%x.\n", |
| 349 | entries_offset, align); |
| 350 | return -1; |
| 351 | } |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 352 | // To calculate available length, find |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 353 | // e = min(bootblock, header, rel_offset) where e > entries_offset. |
| 354 | cbfs_len = size - sizeof(int32_t); |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 355 | if (bootblock_offset > entries_offset && bootblock_offset < cbfs_len) |
| 356 | cbfs_len = bootblock_offset; |
| 357 | if (header_offset > entries_offset && header_offset < cbfs_len) |
| 358 | cbfs_len = header_offset; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 359 | |
| 360 | if (cbfs_image_create(image, cbfs_len - entries_offset)) |
| 361 | return -1; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 362 | return 0; |
| 363 | } |
| 364 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 365 | int cbfs_image_from_buffer(struct cbfs_image *out, struct buffer *in, |
| 366 | uint32_t offset) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 367 | { |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 368 | assert(out); |
| 369 | assert(in); |
| 370 | assert(in->data); |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 371 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 372 | buffer_clone(&out->buffer, in); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 373 | out->has_header = false; |
| 374 | |
Patrick Georgi | 2f953d3 | 2015-09-11 18:34:39 +0200 | [diff] [blame] | 375 | if (cbfs_is_valid_cbfs(out)) { |
| 376 | return 0; |
| 377 | } |
| 378 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 379 | void *header_loc = cbfs_find_header(in->data, in->size, offset); |
| 380 | if (header_loc) { |
| 381 | cbfs_get_header(&out->header, header_loc); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 382 | out->has_header = true; |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 383 | cbfs_fix_legacy_size(out, header_loc); |
Patrick Georgi | 2f953d3 | 2015-09-11 18:34:39 +0200 | [diff] [blame] | 384 | return 0; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 385 | } else if (offset != ~0u) { |
| 386 | ERROR("The -H switch is only valid on legacy images having CBFS master headers.\n"); |
| 387 | return 1; |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 388 | } |
Patrick Georgi | 2f953d3 | 2015-09-11 18:34:39 +0200 | [diff] [blame] | 389 | ERROR("Selected image region is not a valid CBFS.\n"); |
| 390 | return 1; |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 391 | } |
| 392 | |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 393 | int cbfs_copy_instance(struct cbfs_image *image, size_t copy_offset, |
| 394 | size_t copy_size) |
| 395 | { |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 396 | assert(image); |
| 397 | if (!cbfs_is_legacy_cbfs(image)) |
| 398 | return -1; |
| 399 | |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 400 | struct cbfs_file *src_entry, *dst_entry; |
| 401 | struct cbfs_header *copy_header; |
| 402 | size_t align, entry_offset; |
| 403 | ssize_t last_entry_size; |
| 404 | |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 405 | size_t cbfs_offset, cbfs_end; |
| 406 | size_t copy_end = copy_offset + copy_size; |
| 407 | |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 408 | align = image->header.align; |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 409 | |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 410 | cbfs_offset = image->header.offset; |
| 411 | cbfs_end = image->header.romsize; |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 412 | |
| 413 | if (copy_end > image->buffer.size) { |
| 414 | ERROR("Copy offset out of range: [%zx:%zx)\n", |
| 415 | copy_offset, copy_end); |
| 416 | return 1; |
| 417 | } |
| 418 | |
Sol Boucher | 297c88c | 2015-05-05 15:35:18 -0700 | [diff] [blame] | 419 | /* Range check requested copy region with source cbfs. */ |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 420 | if ((copy_offset >= cbfs_offset && copy_offset < cbfs_end) || |
| 421 | (copy_end >= cbfs_offset && copy_end <= cbfs_end)) { |
| 422 | ERROR("New image would overlap old one.\n"); |
| 423 | return 1; |
| 424 | } |
| 425 | |
| 426 | /* This will work, let's create a copy. */ |
| 427 | copy_header = (struct cbfs_header *)(image->buffer.data + copy_offset); |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 428 | cbfs_put_header(copy_header, &image->header); |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 429 | |
| 430 | copy_header->bootblocksize = 0; |
| 431 | /* Romsize is a misnomer. It's the absolute limit of cbfs content.*/ |
| 432 | copy_header->romsize = htonl(copy_end); |
| 433 | entry_offset = align_up(copy_offset + sizeof(*copy_header), align); |
| 434 | copy_header->offset = htonl(entry_offset); |
| 435 | dst_entry = (struct cbfs_file *)(image->buffer.data + entry_offset); |
| 436 | |
| 437 | /* Copy non-empty files */ |
| 438 | for (src_entry = cbfs_find_first_entry(image); |
| 439 | src_entry && cbfs_is_valid_entry(image, src_entry); |
| 440 | src_entry = cbfs_find_next_entry(image, src_entry)) { |
| 441 | size_t entry_size; |
| 442 | |
| 443 | if ((src_entry->type == htonl(CBFS_COMPONENT_NULL)) || |
| 444 | (src_entry->type == htonl(CBFS_COMPONENT_DELETED))) |
| 445 | continue; |
| 446 | |
| 447 | entry_size = htonl(src_entry->len) + htonl(src_entry->offset); |
| 448 | memcpy(dst_entry, src_entry, entry_size); |
| 449 | dst_entry = (struct cbfs_file *)( |
| 450 | (uintptr_t)dst_entry + align_up(entry_size, align)); |
| 451 | |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 452 | if ((size_t)((char *)dst_entry - image->buffer.data) >= |
| 453 | copy_end) { |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 454 | ERROR("Ran out of room in copy region.\n"); |
| 455 | return 1; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | /* Last entry size is all the room above it. */ |
| 460 | last_entry_size = copy_end - ((char *)dst_entry - image->buffer.data) |
| 461 | - cbfs_calculate_file_header_size(""); |
| 462 | |
| 463 | if (last_entry_size < 0) |
| 464 | WARN("No room to create the last entry!\n") |
| 465 | else |
Patrick Georgi | edf25d9 | 2015-08-12 09:12:06 +0200 | [diff] [blame] | 466 | cbfs_create_empty_entry(dst_entry, CBFS_COMPONENT_NULL, |
| 467 | last_entry_size, ""); |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 468 | |
| 469 | return 0; |
| 470 | } |
| 471 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 472 | int cbfs_image_delete(struct cbfs_image *image) |
| 473 | { |
Edward O'Callaghan | a0f9ece | 2014-03-09 00:05:18 +1100 | [diff] [blame] | 474 | if (image == NULL) |
| 475 | return 0; |
| 476 | |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 477 | buffer_delete(&image->buffer); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 478 | return 0; |
| 479 | } |
| 480 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 481 | /* Tries to add an entry with its data (CBFS_SUBHEADER) at given offset. */ |
| 482 | static int cbfs_add_entry_at(struct cbfs_image *image, |
| 483 | struct cbfs_file *entry, |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 484 | const void *data, |
Patrick Georgi | 7fd1418 | 2015-08-11 15:55:16 +0200 | [diff] [blame] | 485 | uint32_t content_offset, |
Patrick Georgi | d5a4afa | 2015-08-25 22:27:57 +0200 | [diff] [blame] | 486 | const struct cbfs_file *header) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 487 | { |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 488 | struct cbfs_file *next = cbfs_find_next_entry(image, entry); |
| 489 | uint32_t addr = cbfs_get_entry_addr(image, entry), |
| 490 | addr_next = cbfs_get_entry_addr(image, next); |
Patrick Georgi | 7fd1418 | 2015-08-11 15:55:16 +0200 | [diff] [blame] | 491 | uint32_t min_entry_size = cbfs_calculate_file_header_size(""); |
Patrick Georgi | 4eb8abe | 2015-08-25 12:24:49 +0200 | [diff] [blame] | 492 | uint32_t len, header_offset; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 493 | uint32_t align = image->has_header ? image->header.align : |
| 494 | CBFS_ENTRY_ALIGNMENT; |
Patrick Georgi | d5a4afa | 2015-08-25 22:27:57 +0200 | [diff] [blame] | 495 | uint32_t header_size = ntohl(header->offset); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 496 | |
Patrick Georgi | 4eb8abe | 2015-08-25 12:24:49 +0200 | [diff] [blame] | 497 | header_offset = content_offset - header_size; |
| 498 | if (header_offset % align) |
| 499 | header_offset -= header_offset % align; |
| 500 | if (header_offset < addr) { |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 501 | ERROR("No space to hold cbfs_file header."); |
| 502 | return -1; |
| 503 | } |
| 504 | |
| 505 | // Process buffer BEFORE content_offset. |
Patrick Georgi | 4eb8abe | 2015-08-25 12:24:49 +0200 | [diff] [blame] | 506 | if (header_offset - addr > min_entry_size) { |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 507 | DEBUG("|min|...|header|content|... <create new entry>\n"); |
Patrick Georgi | 4eb8abe | 2015-08-25 12:24:49 +0200 | [diff] [blame] | 508 | len = header_offset - addr - min_entry_size; |
Patrick Georgi | edf25d9 | 2015-08-12 09:12:06 +0200 | [diff] [blame] | 509 | cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, len, ""); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 510 | if (verbose > 1) cbfs_print_entry_info(image, entry, stderr); |
| 511 | entry = cbfs_find_next_entry(image, entry); |
| 512 | addr = cbfs_get_entry_addr(image, entry); |
| 513 | } |
| 514 | |
Patrick Georgi | 7a33b53 | 2015-08-25 13:00:04 +0200 | [diff] [blame] | 515 | len = content_offset - addr - header_size; |
Patrick Georgi | a60e7b6 | 2015-08-25 22:26:02 +0200 | [diff] [blame] | 516 | memcpy(entry, header, header_size); |
Patrick Georgi | 7a33b53 | 2015-08-25 13:00:04 +0200 | [diff] [blame] | 517 | if (len != 0) { |
| 518 | /* the header moved backwards a bit to accomodate cbfs_file |
| 519 | * alignment requirements, so patch up ->offset to still point |
| 520 | * to file data. |
| 521 | */ |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 522 | DEBUG("|..|header|content|... <use offset to create entry>\n"); |
Patrick Georgi | ae7efb9 | 2015-08-25 13:11:28 +0200 | [diff] [blame] | 523 | DEBUG("before: offset=0x%x\n", ntohl(entry->offset)); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 524 | // TODO reset expanded name buffer to 0xFF. |
Patrick Georgi | 7a33b53 | 2015-08-25 13:00:04 +0200 | [diff] [blame] | 525 | entry->offset = htonl(ntohl(entry->offset) + len); |
Patrick Georgi | ae7efb9 | 2015-08-25 13:11:28 +0200 | [diff] [blame] | 526 | DEBUG("after: offset=0x%x\n", ntohl(entry->len)); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | // Ready to fill data into entry. |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 530 | DEBUG("content_offset: 0x%x, entry location: %x\n", |
| 531 | content_offset, (int)((char*)CBFS_SUBHEADER(entry) - |
| 532 | image->buffer.data)); |
| 533 | assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data == |
Patrick Georgi | cccc9d4 | 2015-04-28 13:09:36 +0200 | [diff] [blame] | 534 | (ptrdiff_t)content_offset); |
Patrick Georgi | 19c80b2 | 2015-08-25 13:16:04 +0200 | [diff] [blame] | 535 | memcpy(CBFS_SUBHEADER(entry), data, ntohl(entry->len)); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 536 | if (verbose > 1) cbfs_print_entry_info(image, entry, stderr); |
| 537 | |
| 538 | // Process buffer AFTER entry. |
| 539 | entry = cbfs_find_next_entry(image, entry); |
| 540 | addr = cbfs_get_entry_addr(image, entry); |
Sol Boucher | 0572565 | 2015-04-02 20:58:26 -0700 | [diff] [blame] | 541 | if (addr == addr_next) |
| 542 | return 0; |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 543 | |
Sol Boucher | 0572565 | 2015-04-02 20:58:26 -0700 | [diff] [blame] | 544 | assert(addr < addr_next); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 545 | if (addr_next - addr < min_entry_size) { |
Sol Boucher | 636cc85 | 2015-04-03 09:13:04 -0700 | [diff] [blame] | 546 | DEBUG("No need for new \"empty\" entry\n"); |
| 547 | /* No need to increase the size of the just |
| 548 | * stored file to extend to next file. Alignment |
| 549 | * of next file takes care of this. |
| 550 | */ |
| 551 | return 0; |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | len = addr_next - addr - min_entry_size; |
Patrick Georgi | edf25d9 | 2015-08-12 09:12:06 +0200 | [diff] [blame] | 555 | cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, len, ""); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 556 | if (verbose > 1) cbfs_print_entry_info(image, entry, stderr); |
| 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer, |
Patrick Georgi | e590358 | 2015-08-25 13:53:42 +0200 | [diff] [blame] | 561 | uint32_t content_offset, |
Patrick Georgi | f5252f3 | 2015-08-25 22:27:57 +0200 | [diff] [blame] | 562 | struct cbfs_file *header) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 563 | { |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 564 | assert(image); |
| 565 | assert(buffer); |
| 566 | assert(buffer->data); |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 567 | assert(!IS_TOP_ALIGNED_ADDRESS(content_offset)); |
| 568 | |
Patrick Georgi | a60e7b6 | 2015-08-25 22:26:02 +0200 | [diff] [blame] | 569 | const char *name = header->filename; |
Patrick Georgi | e590358 | 2015-08-25 13:53:42 +0200 | [diff] [blame] | 570 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 571 | uint32_t entry_type; |
| 572 | uint32_t addr, addr_next; |
| 573 | struct cbfs_file *entry, *next; |
Patrick Georgi | dd2d3f9 | 2015-08-12 12:29:20 +0200 | [diff] [blame] | 574 | uint32_t need_size; |
Patrick Georgi | f5252f3 | 2015-08-25 22:27:57 +0200 | [diff] [blame] | 575 | uint32_t header_size = ntohl(header->offset); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 576 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 577 | need_size = header_size + buffer->size; |
| 578 | DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n", |
| 579 | name, content_offset, header_size, buffer->size, need_size); |
| 580 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 581 | // Merge empty entries. |
| 582 | DEBUG("(trying to merge empty entries...)\n"); |
| 583 | cbfs_walk(image, cbfs_merge_empty_entry, NULL); |
| 584 | |
| 585 | for (entry = cbfs_find_first_entry(image); |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 586 | entry && cbfs_is_valid_entry(image, entry); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 587 | entry = cbfs_find_next_entry(image, entry)) { |
| 588 | |
| 589 | entry_type = ntohl(entry->type); |
| 590 | if (entry_type != CBFS_COMPONENT_NULL) |
| 591 | continue; |
| 592 | |
| 593 | addr = cbfs_get_entry_addr(image, entry); |
| 594 | next = cbfs_find_next_entry(image, entry); |
| 595 | addr_next = cbfs_get_entry_addr(image, next); |
| 596 | |
| 597 | DEBUG("cbfs_add_entry: space at 0x%x+0x%x(%d) bytes\n", |
| 598 | addr, addr_next - addr, addr_next - addr); |
Aaron Durbin | 1ebc7e9 | 2014-01-21 15:28:38 -0600 | [diff] [blame] | 599 | |
| 600 | /* Will the file fit? Don't yet worry if we have space for a new |
| 601 | * "empty" entry. We take care of that later. |
| 602 | */ |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 603 | if (addr + need_size > addr_next) |
| 604 | continue; |
| 605 | |
Patrick Georgi | aa44dbd | 2015-08-12 12:05:21 +0200 | [diff] [blame] | 606 | // Test for complicated cases |
| 607 | if (content_offset > 0) { |
| 608 | if (addr_next < content_offset) { |
| 609 | DEBUG("Not for specified offset yet"); |
| 610 | continue; |
| 611 | } else if (addr > content_offset) { |
| 612 | DEBUG("Exceed specified content_offset."); |
| 613 | break; |
| 614 | } else if (addr + header_size > content_offset) { |
| 615 | ERROR("Not enough space for header.\n"); |
| 616 | break; |
| 617 | } else if (content_offset + buffer->size > addr_next) { |
| 618 | ERROR("Not enough space for content.\n"); |
| 619 | break; |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | // TODO there are more few tricky cases that we may |
| 624 | // want to fit by altering offset. |
| 625 | |
Patrick Georgi | dd2d3f9 | 2015-08-12 12:29:20 +0200 | [diff] [blame] | 626 | if (content_offset == 0) { |
| 627 | // we tested every condition earlier under which |
| 628 | // placing the file there might fail |
| 629 | content_offset = addr + header_size; |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 630 | } |
| 631 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 632 | DEBUG("section 0x%x+0x%x for content_offset 0x%x.\n", |
| 633 | addr, addr_next - addr, content_offset); |
| 634 | |
Patrick Georgi | d5a4afa | 2015-08-25 22:27:57 +0200 | [diff] [blame] | 635 | if (cbfs_add_entry_at(image, entry, buffer->data, |
| 636 | content_offset, header) == 0) { |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 637 | return 0; |
| 638 | } |
| 639 | break; |
| 640 | } |
| 641 | |
| 642 | ERROR("Could not add [%s, %zd bytes (%zd KB)@0x%x]; too big?\n", |
| 643 | buffer->name, buffer->size, buffer->size / 1024, content_offset); |
| 644 | return -1; |
| 645 | } |
| 646 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 647 | struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name) |
| 648 | { |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 649 | struct cbfs_file *entry; |
| 650 | for (entry = cbfs_find_first_entry(image); |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 651 | entry && cbfs_is_valid_entry(image, entry); |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 652 | entry = cbfs_find_next_entry(image, entry)) { |
Patrick Georgi | c569b8b | 2015-07-15 16:42:38 +0200 | [diff] [blame] | 653 | if (strcasecmp(entry->filename, name) == 0) { |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 654 | DEBUG("cbfs_get_entry: found %s\n", name); |
| 655 | return entry; |
| 656 | } |
| 657 | } |
| 658 | return NULL; |
| 659 | } |
| 660 | |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame^] | 661 | static int cbfs_stage_decompress(struct cbfs_stage *stage, struct buffer *buff) |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 662 | { |
| 663 | struct buffer reader; |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 664 | char *orig_buffer; |
| 665 | char *new_buffer; |
| 666 | size_t new_buff_sz; |
| 667 | decomp_func_ptr decompress; |
| 668 | |
| 669 | buffer_clone(&reader, buff); |
| 670 | |
| 671 | /* The stage metadata is in little endian. */ |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame^] | 672 | stage->compression = xdr_le.get32(&reader); |
| 673 | stage->entry = xdr_le.get64(&reader); |
| 674 | stage->load = xdr_le.get64(&reader); |
| 675 | stage->len = xdr_le.get32(&reader); |
| 676 | stage->memlen = xdr_le.get32(&reader); |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 677 | |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame^] | 678 | /* Create a buffer just with the uncompressed program now that the |
| 679 | * struct cbfs_stage has been peeled off. */ |
| 680 | if (stage->compression == CBFS_COMPRESS_NONE) { |
| 681 | new_buff_sz = buffer_size(buff) - sizeof(struct cbfs_stage); |
| 682 | |
| 683 | orig_buffer = buffer_get(buff); |
| 684 | new_buffer = calloc(1, new_buff_sz); |
| 685 | memcpy(new_buffer, orig_buffer + sizeof(struct cbfs_stage), |
| 686 | new_buff_sz); |
| 687 | buffer_init(buff, buff->name, new_buffer, new_buff_sz); |
| 688 | free(orig_buffer); |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 689 | return 0; |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame^] | 690 | } |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 691 | |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame^] | 692 | decompress = decompression_function(stage->compression); |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 693 | if (decompress == NULL) |
| 694 | return -1; |
| 695 | |
| 696 | orig_buffer = buffer_get(buff); |
| 697 | |
| 698 | /* This can be too big of a buffer needed, but there's no current |
| 699 | * field indicating decompressed size of data. */ |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame^] | 700 | new_buff_sz = stage->memlen; |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 701 | new_buffer = calloc(1, new_buff_sz); |
| 702 | |
| 703 | if (decompress(orig_buffer + sizeof(struct cbfs_stage), |
| 704 | (int)(buffer_size(buff) - sizeof(struct cbfs_stage)), |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame^] | 705 | new_buffer, (int)new_buff_sz, &new_buff_sz)) { |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 706 | ERROR("Couldn't decompress stage.\n"); |
| 707 | free(new_buffer); |
| 708 | return -1; |
| 709 | } |
| 710 | |
| 711 | /* Include correct size for full stage info. */ |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 712 | buffer_init(buff, buff->name, new_buffer, new_buff_sz); |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 713 | |
| 714 | /* True decompressed size is just the data size -- no metadata. */ |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame^] | 715 | stage->len = new_buff_sz; |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 716 | /* Stage is not compressed. */ |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame^] | 717 | stage->compression = CBFS_COMPRESS_NONE; |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 718 | |
| 719 | free(orig_buffer); |
| 720 | |
| 721 | return 0; |
| 722 | } |
| 723 | |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame^] | 724 | static int init_elf_from_arch(Elf64_Ehdr *ehdr, uint32_t cbfs_arch) |
| 725 | { |
| 726 | int endian; |
| 727 | int nbits; |
| 728 | int machine; |
| 729 | |
| 730 | switch (cbfs_arch) { |
| 731 | case CBFS_ARCHITECTURE_X86: |
| 732 | endian = ELFDATA2LSB; |
| 733 | nbits = ELFCLASS32; |
| 734 | machine = EM_386; |
| 735 | break; |
| 736 | case CBFS_ARCHITECTURE_ARM: |
| 737 | endian = ELFDATA2LSB; |
| 738 | nbits = ELFCLASS32; |
| 739 | machine = EM_ARM; |
| 740 | break; |
| 741 | case CBFS_ARCHITECTURE_AARCH64: |
| 742 | endian = ELFDATA2LSB; |
| 743 | nbits = ELFCLASS64; |
| 744 | machine = EM_AARCH64; |
| 745 | break; |
| 746 | case CBFS_ARCHITECTURE_MIPS: |
| 747 | endian = ELFDATA2LSB; |
| 748 | nbits = ELFCLASS32; |
| 749 | machine = EM_MIPS; |
| 750 | break; |
| 751 | case CBFS_ARCHITECTURE_RISCV: |
| 752 | endian = ELFDATA2LSB; |
| 753 | nbits = ELFCLASS32; |
| 754 | machine = EM_RISCV; |
| 755 | break; |
| 756 | default: |
| 757 | ERROR("Unsupported arch: %x\n", cbfs_arch); |
| 758 | return -1; |
| 759 | } |
| 760 | |
| 761 | elf_init_eheader(ehdr, machine, nbits, endian); |
| 762 | return 0; |
| 763 | } |
| 764 | |
| 765 | static int cbfs_stage_make_elf(struct buffer *buff, uint32_t arch) |
| 766 | { |
| 767 | Elf64_Ehdr ehdr; |
| 768 | Elf64_Shdr shdr; |
| 769 | struct cbfs_stage stage; |
| 770 | struct elf_writer *ew; |
| 771 | struct buffer elf_out; |
| 772 | size_t empty_sz; |
| 773 | |
| 774 | if (cbfs_stage_decompress(&stage, buff)) { |
| 775 | ERROR("Failed to decompress stage.\n"); |
| 776 | return -1; |
| 777 | } |
| 778 | |
| 779 | if (init_elf_from_arch(&ehdr, arch)) |
| 780 | return -1; |
| 781 | |
| 782 | ehdr.e_entry = stage.entry; |
| 783 | |
| 784 | ew = elf_writer_init(&ehdr); |
| 785 | if (ew == NULL) { |
| 786 | ERROR("Unable to init ELF writer.\n"); |
| 787 | return -1; |
| 788 | } |
| 789 | |
| 790 | memset(&shdr, 0, sizeof(shdr)); |
| 791 | shdr.sh_type = SHT_PROGBITS; |
| 792 | shdr.sh_flags = SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR; |
| 793 | shdr.sh_addr = stage.load; |
| 794 | shdr.sh_size = stage.len; |
| 795 | empty_sz = stage.memlen - stage.len; |
| 796 | |
| 797 | if (elf_writer_add_section(ew, &shdr, buff, ".program")) { |
| 798 | ERROR("Unable to add ELF section: .program\n"); |
| 799 | elf_writer_destroy(ew); |
| 800 | return -1; |
| 801 | } |
| 802 | |
| 803 | if (empty_sz != 0) { |
| 804 | struct buffer b; |
| 805 | |
| 806 | buffer_init(&b, NULL, NULL, 0); |
| 807 | memset(&shdr, 0, sizeof(shdr)); |
| 808 | shdr.sh_type = SHT_NOBITS; |
| 809 | shdr.sh_flags = SHF_WRITE | SHF_ALLOC; |
| 810 | shdr.sh_addr = stage.load + stage.len; |
| 811 | shdr.sh_size = empty_sz; |
| 812 | if (elf_writer_add_section(ew, &shdr, &b, ".empty")) { |
| 813 | ERROR("Unable to add ELF section: .empty\n"); |
| 814 | elf_writer_destroy(ew); |
| 815 | return -1; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | if (elf_writer_serialize(ew, &elf_out)) { |
| 820 | ERROR("Unable to create ELF file from stage.\n"); |
| 821 | elf_writer_destroy(ew); |
| 822 | return -1; |
| 823 | } |
| 824 | |
| 825 | /* Flip buffer with the created ELF one. */ |
| 826 | buffer_delete(buff); |
| 827 | *buff = elf_out; |
| 828 | |
| 829 | elf_writer_destroy(ew); |
| 830 | |
| 831 | return 0; |
| 832 | } |
| 833 | |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 834 | int cbfs_export_entry(struct cbfs_image *image, const char *entry_name, |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame^] | 835 | const char *filename, uint32_t arch) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 836 | { |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 837 | struct cbfs_file *entry = cbfs_get_entry(image, entry_name); |
| 838 | struct buffer buffer; |
| 839 | if (!entry) { |
| 840 | ERROR("File not found: %s\n", entry_name); |
| 841 | return -1; |
| 842 | } |
Patrick Georgi | 23aeaff | 2015-08-26 13:01:10 +0200 | [diff] [blame] | 843 | |
| 844 | unsigned int decompressed_size = 0; |
| 845 | unsigned int compression = cbfs_file_get_compression_info(entry, |
| 846 | &decompressed_size); |
| 847 | |
| 848 | decomp_func_ptr decompress = decompression_function(compression); |
| 849 | if (!decompress) { |
| 850 | ERROR("looking up decompression routine failed\n"); |
| 851 | return -1; |
| 852 | } |
| 853 | |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 854 | LOG("Found file %.30s at 0x%x, type %.12s, size %d\n", |
| 855 | entry_name, cbfs_get_entry_addr(image, entry), |
Patrick Georgi | 23aeaff | 2015-08-26 13:01:10 +0200 | [diff] [blame] | 856 | get_cbfs_entry_type_name(ntohl(entry->type)), decompressed_size); |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 857 | |
Patrick Georgi | 011b0b3 | 2015-08-26 12:16:54 +0200 | [diff] [blame] | 858 | if (ntohl(entry->type) == CBFS_COMPONENT_PAYLOAD) { |
| 859 | WARN("Payloads are extracted in SELF format.\n"); |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 860 | } |
| 861 | |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 862 | buffer_init(&buffer, strdup("(cbfs_export_entry)"), NULL, 0); |
| 863 | |
Patrick Georgi | 23aeaff | 2015-08-26 13:01:10 +0200 | [diff] [blame] | 864 | buffer.data = malloc(decompressed_size); |
| 865 | buffer.size = decompressed_size; |
| 866 | if (decompress(CBFS_SUBHEADER(entry), ntohl(entry->len), |
Aaron Durbin | 5213c53 | 2015-10-23 17:38:40 -0500 | [diff] [blame] | 867 | buffer.data, buffer.size, NULL)) { |
Patrick Georgi | 23aeaff | 2015-08-26 13:01:10 +0200 | [diff] [blame] | 868 | ERROR("decompression failed for %s\n", entry_name); |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 869 | buffer_delete(&buffer); |
Patrick Georgi | 23aeaff | 2015-08-26 13:01:10 +0200 | [diff] [blame] | 870 | return -1; |
| 871 | } |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 872 | |
| 873 | /* |
| 874 | * The stage metadata is never compressed proper for cbfs_stage |
| 875 | * files. The contents of the stage data can be though. Therefore |
| 876 | * one has to do a second pass for stages to potentially decompress |
| 877 | * the stage data to make it more meaningful. |
| 878 | */ |
| 879 | if (ntohl(entry->type) == CBFS_COMPONENT_STAGE) { |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame^] | 880 | if (cbfs_stage_make_elf(&buffer, arch)) { |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 881 | buffer_delete(&buffer); |
| 882 | return -1; |
| 883 | } |
| 884 | } |
| 885 | |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 886 | if (buffer_write_file(&buffer, filename) != 0) { |
| 887 | ERROR("Failed to write %s into %s.\n", |
| 888 | entry_name, filename); |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 889 | buffer_delete(&buffer); |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 890 | return -1; |
| 891 | } |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 892 | |
| 893 | buffer_delete(&buffer); |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 894 | INFO("Successfully dumped the file to: %s\n", filename); |
| 895 | return 0; |
| 896 | } |
| 897 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 898 | int cbfs_remove_entry(struct cbfs_image *image, const char *name) |
| 899 | { |
Patrick Georgi | 4d1c5aa | 2015-07-17 22:07:26 +0200 | [diff] [blame] | 900 | struct cbfs_file *entry; |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 901 | entry = cbfs_get_entry(image, name); |
| 902 | if (!entry) { |
| 903 | ERROR("CBFS file %s not found.\n", name); |
| 904 | return -1; |
| 905 | } |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 906 | DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n", |
Patrick Georgi | c569b8b | 2015-07-15 16:42:38 +0200 | [diff] [blame] | 907 | entry->filename, cbfs_get_entry_addr(image, entry)); |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 908 | entry->type = htonl(CBFS_COMPONENT_DELETED); |
Patrick Georgi | 4d1c5aa | 2015-07-17 22:07:26 +0200 | [diff] [blame] | 909 | cbfs_walk(image, cbfs_merge_empty_entry, NULL); |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 910 | return 0; |
| 911 | } |
| 912 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 913 | int cbfs_print_header_info(struct cbfs_image *image) |
| 914 | { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 915 | char *name = strdup(image->buffer.name); |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 916 | assert(image); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 917 | printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n" |
Stefan Reinauer | 8f50e53 | 2013-11-13 14:34:57 -0800 | [diff] [blame] | 918 | "alignment: %d bytes, architecture: %s\n\n", |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 919 | basename(name), |
| 920 | image->buffer.size / 1024, |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 921 | image->header.bootblocksize, |
| 922 | image->header.romsize, |
| 923 | image->header.offset, |
| 924 | image->header.align, |
| 925 | arch_to_string(image->header.architecture)); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 926 | free(name); |
| 927 | return 0; |
| 928 | } |
| 929 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 930 | static int cbfs_print_stage_info(struct cbfs_stage *stage, FILE* fp) |
| 931 | { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 932 | fprintf(fp, |
| 933 | " %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", " |
| 934 | "length: %d/%d\n", |
| 935 | lookup_name_by_type(types_cbfs_compression, |
| 936 | stage->compression, "(unknown)"), |
| 937 | stage->entry, |
| 938 | stage->load, |
| 939 | stage->len, |
| 940 | stage->memlen); |
| 941 | return 0; |
| 942 | } |
| 943 | |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 944 | static int cbfs_print_decoded_payload_segment_info( |
| 945 | struct cbfs_payload_segment *seg, FILE *fp) |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 946 | { |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 947 | /* The input (seg) must be already decoded by |
| 948 | * cbfs_decode_payload_segment. |
| 949 | */ |
| 950 | switch (seg->type) { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 951 | case PAYLOAD_SEGMENT_CODE: |
| 952 | case PAYLOAD_SEGMENT_DATA: |
| 953 | fprintf(fp, " %s (%s compression, offset: 0x%x, " |
| 954 | "load: 0x%" PRIx64 ", length: %d/%d)\n", |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 955 | (seg->type == PAYLOAD_SEGMENT_CODE ? |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 956 | "code " : "data"), |
| 957 | lookup_name_by_type(types_cbfs_compression, |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 958 | seg->compression, |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 959 | "(unknown)"), |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 960 | seg->offset, seg->load_addr, seg->len, |
| 961 | seg->mem_len); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 962 | break; |
| 963 | |
| 964 | case PAYLOAD_SEGMENT_ENTRY: |
| 965 | fprintf(fp, " entry (0x%" PRIx64 ")\n", |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 966 | seg->load_addr); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 967 | break; |
| 968 | |
| 969 | case PAYLOAD_SEGMENT_BSS: |
| 970 | fprintf(fp, " BSS (address 0x%016" PRIx64 ", " |
| 971 | "length 0x%x)\n", |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 972 | seg->load_addr, seg->len); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 973 | break; |
| 974 | |
| 975 | case PAYLOAD_SEGMENT_PARAMS: |
| 976 | fprintf(fp, " parameters\n"); |
| 977 | break; |
| 978 | |
| 979 | default: |
| 980 | fprintf(fp, " 0x%x (%s compression, offset: 0x%x, " |
| 981 | "load: 0x%" PRIx64 ", length: %d/%d\n", |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 982 | seg->type, |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 983 | lookup_name_by_type(types_cbfs_compression, |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 984 | seg->compression, |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 985 | "(unknown)"), |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 986 | seg->offset, seg->load_addr, seg->len, |
| 987 | seg->mem_len); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 988 | break; |
| 989 | } |
| 990 | return 0; |
| 991 | } |
| 992 | |
| 993 | 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] | 994 | void *arg) |
| 995 | { |
Patrick Georgi | c569b8b | 2015-07-15 16:42:38 +0200 | [diff] [blame] | 996 | const char *name = entry->filename; |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 997 | struct cbfs_payload_segment *payload; |
| 998 | FILE *fp = (FILE *)arg; |
| 999 | |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 1000 | if (!cbfs_is_valid_entry(image, entry)) { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1001 | ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n", |
| 1002 | cbfs_get_entry_addr(image, entry)); |
| 1003 | return -1; |
| 1004 | } |
| 1005 | if (!fp) |
| 1006 | fp = stdout; |
| 1007 | |
Patrick Georgi | c82725c | 2015-08-26 12:13:03 +0200 | [diff] [blame] | 1008 | unsigned int decompressed_size = 0; |
Patrick Georgi | a71c83f | 2015-08-26 12:23:26 +0200 | [diff] [blame] | 1009 | unsigned int compression = cbfs_file_get_compression_info(entry, |
| 1010 | &decompressed_size); |
Patrick Georgi | c82725c | 2015-08-26 12:13:03 +0200 | [diff] [blame] | 1011 | |
| 1012 | if (compression == CBFS_COMPRESS_NONE) { |
| 1013 | fprintf(fp, "%-30s 0x%-8x %-12s %d\n", |
| 1014 | *name ? name : "(empty)", |
| 1015 | cbfs_get_entry_addr(image, entry), |
| 1016 | get_cbfs_entry_type_name(ntohl(entry->type)), |
| 1017 | ntohl(entry->len)); |
| 1018 | } else { |
| 1019 | fprintf(fp, "%-30s 0x%-8x %-12s %d (%d after %s decompression)\n", |
| 1020 | *name ? name : "(empty)", |
| 1021 | cbfs_get_entry_addr(image, entry), |
| 1022 | get_cbfs_entry_type_name(ntohl(entry->type)), |
| 1023 | ntohl(entry->len), |
| 1024 | decompressed_size, |
| 1025 | lookup_name_by_type(types_cbfs_compression, |
| 1026 | compression, "(unknown)") |
| 1027 | ); |
| 1028 | } |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1029 | |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 1030 | struct cbfs_file_attr_hash *hash = NULL; |
| 1031 | while ((hash = cbfs_file_get_next_hash(entry, hash)) != NULL) { |
| 1032 | unsigned int hash_type = ntohl(hash->hash_type); |
| 1033 | if (hash_type > CBFS_NUM_SUPPORTED_HASHES) { |
| 1034 | fprintf(fp, "invalid hash type %d\n", hash_type); |
| 1035 | break; |
| 1036 | } |
| 1037 | size_t hash_len = widths_cbfs_hash[hash_type]; |
| 1038 | char *hash_str = bintohex(hash->hash_data, hash_len); |
| 1039 | uint8_t local_hash[hash_len]; |
| 1040 | if (vb2_digest_buffer(CBFS_SUBHEADER(entry), |
| 1041 | ntohl(entry->len), hash_type, local_hash, |
| 1042 | hash_len) != VB2_SUCCESS) { |
| 1043 | fprintf(fp, "failed to hash '%s'\n", name); |
| 1044 | break; |
| 1045 | } |
| 1046 | int valid = memcmp(local_hash, hash->hash_data, hash_len) == 0; |
| 1047 | const char *valid_str = valid ? "valid" : "invalid"; |
| 1048 | |
| 1049 | fprintf(fp, " hash %s:%s %s\n", |
| 1050 | get_hash_attr_name(hash_type), |
| 1051 | hash_str, valid_str); |
| 1052 | free(hash_str); |
| 1053 | } |
| 1054 | |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1055 | if (!verbose) |
| 1056 | return 0; |
| 1057 | |
| 1058 | DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n", |
| 1059 | cbfs_get_entry_addr(image, entry), ntohl(entry->offset), |
| 1060 | cbfs_get_entry_addr(image, entry) + ntohl(entry->offset), |
| 1061 | ntohl(entry->len)); |
| 1062 | |
| 1063 | /* note the components of the subheader may be in host order ... */ |
| 1064 | switch (ntohl(entry->type)) { |
| 1065 | case CBFS_COMPONENT_STAGE: |
| 1066 | cbfs_print_stage_info((struct cbfs_stage *) |
| 1067 | CBFS_SUBHEADER(entry), fp); |
| 1068 | break; |
| 1069 | |
| 1070 | case CBFS_COMPONENT_PAYLOAD: |
Paul Menzel | 831bbe8 | 2015-08-08 20:20:57 +0200 | [diff] [blame] | 1071 | payload = (struct cbfs_payload_segment *) |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1072 | CBFS_SUBHEADER(entry); |
| 1073 | while (payload) { |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 1074 | struct cbfs_payload_segment seg; |
| 1075 | cbfs_decode_payload_segment(&seg, payload); |
| 1076 | cbfs_print_decoded_payload_segment_info( |
| 1077 | &seg, fp); |
| 1078 | if (seg.type == PAYLOAD_SEGMENT_ENTRY) |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1079 | break; |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 1080 | else |
Aaron Durbin | ca63027 | 2014-08-05 10:48:20 -0500 | [diff] [blame] | 1081 | payload ++; |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1082 | } |
| 1083 | break; |
| 1084 | default: |
| 1085 | break; |
| 1086 | } |
| 1087 | return 0; |
| 1088 | } |
| 1089 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1090 | int cbfs_print_directory(struct cbfs_image *image) |
| 1091 | { |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1092 | if (cbfs_is_legacy_cbfs(image)) |
| 1093 | cbfs_print_header_info(image); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1094 | printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type"); |
| 1095 | cbfs_walk(image, cbfs_print_entry_info, NULL); |
| 1096 | return 0; |
| 1097 | } |
| 1098 | |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1099 | 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] | 1100 | unused void *arg) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1101 | { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1102 | struct cbfs_file *next; |
Aaron Durbin | 285111f | 2015-08-08 20:25:17 +0200 | [diff] [blame] | 1103 | uint8_t *name; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1104 | uint32_t type, addr, last_addr; |
| 1105 | |
| 1106 | type = ntohl(entry->type); |
| 1107 | if (type == CBFS_COMPONENT_DELETED) { |
| 1108 | // Ready to be recycled. |
| 1109 | type = CBFS_COMPONENT_NULL; |
| 1110 | entry->type = htonl(type); |
Aaron Durbin | 285111f | 2015-08-08 20:25:17 +0200 | [diff] [blame] | 1111 | // Place NUL byte as first byte of name to be viewed as "empty". |
| 1112 | name = (void *)&entry[1]; |
| 1113 | *name = '\0'; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1114 | } |
| 1115 | if (type != CBFS_COMPONENT_NULL) |
| 1116 | return 0; |
| 1117 | |
| 1118 | next = cbfs_find_next_entry(image, entry); |
| 1119 | |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 1120 | while (next && cbfs_is_valid_entry(image, next)) { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1121 | type = ntohl(next->type); |
| 1122 | if (type == CBFS_COMPONENT_DELETED) { |
| 1123 | type = CBFS_COMPONENT_NULL; |
| 1124 | next->type = htonl(type); |
| 1125 | } |
| 1126 | if (type != CBFS_COMPONENT_NULL) |
| 1127 | return 0; |
| 1128 | |
| 1129 | addr = cbfs_get_entry_addr(image, entry); |
| 1130 | last_addr = cbfs_get_entry_addr( |
| 1131 | image, cbfs_find_next_entry(image, next)); |
| 1132 | |
| 1133 | // Now, we find two deleted/empty entries; try to merge now. |
| 1134 | DEBUG("join_empty_entry: combine 0x%x+0x%x and 0x%x+0x%x.\n", |
| 1135 | cbfs_get_entry_addr(image, entry), ntohl(entry->len), |
| 1136 | cbfs_get_entry_addr(image, next), ntohl(next->len)); |
Patrick Georgi | edf25d9 | 2015-08-12 09:12:06 +0200 | [diff] [blame] | 1137 | cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1138 | (last_addr - addr - |
| 1139 | cbfs_calculate_file_header_size("")), |
| 1140 | ""); |
| 1141 | DEBUG("new empty entry: length=0x%x\n", ntohl(entry->len)); |
| 1142 | next = cbfs_find_next_entry(image, entry); |
| 1143 | } |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1147 | int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback, |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1148 | void *arg) |
| 1149 | { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1150 | int count = 0; |
| 1151 | struct cbfs_file *entry; |
| 1152 | for (entry = cbfs_find_first_entry(image); |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 1153 | entry && cbfs_is_valid_entry(image, entry); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1154 | entry = cbfs_find_next_entry(image, entry)) { |
| 1155 | count ++; |
| 1156 | if (callback(image, entry, arg) != 0) |
| 1157 | break; |
| 1158 | } |
| 1159 | return count; |
| 1160 | } |
| 1161 | |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 1162 | static int cbfs_header_valid(struct cbfs_header *header, size_t size) |
| 1163 | { |
| 1164 | if ((ntohl(header->magic) == CBFS_HEADER_MAGIC) && |
| 1165 | ((ntohl(header->version) == CBFS_HEADER_VERSION1) || |
| 1166 | (ntohl(header->version) == CBFS_HEADER_VERSION2)) && |
| 1167 | (ntohl(header->romsize) <= size) && |
| 1168 | (ntohl(header->offset) < ntohl(header->romsize))) |
| 1169 | return 1; |
| 1170 | return 0; |
| 1171 | } |
| 1172 | |
| 1173 | struct cbfs_header *cbfs_find_header(char *data, size_t size, |
| 1174 | uint32_t forced_offset) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1175 | { |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1176 | size_t offset; |
| 1177 | int found = 0; |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1178 | int32_t rel_offset; |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1179 | struct cbfs_header *header, *result = NULL; |
| 1180 | |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 1181 | if (forced_offset < (size - sizeof(struct cbfs_header))) { |
| 1182 | /* Check if the forced header is valid. */ |
| 1183 | header = (struct cbfs_header *)(data + forced_offset); |
| 1184 | if (cbfs_header_valid(header, size)) |
| 1185 | return header; |
| 1186 | return NULL; |
| 1187 | } |
| 1188 | |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1189 | // Try finding relative offset of master header at end of file first. |
| 1190 | rel_offset = *(int32_t *)(data + size - sizeof(int32_t)); |
| 1191 | offset = size + rel_offset; |
| 1192 | DEBUG("relative offset: %#zx(-%#zx), offset: %#zx\n", |
| 1193 | (size_t)rel_offset, (size_t)-rel_offset, offset); |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 1194 | |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1195 | if (offset >= size - sizeof(*header) || |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 1196 | !cbfs_header_valid((struct cbfs_header *)(data + offset), size)) { |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1197 | // Some use cases append non-CBFS data to the end of the ROM. |
| 1198 | DEBUG("relative offset seems wrong, scanning whole image...\n"); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1199 | offset = 0; |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1200 | } |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1201 | |
| 1202 | for (; offset + sizeof(*header) < size; offset++) { |
| 1203 | header = (struct cbfs_header *)(data + offset); |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 1204 | if (!cbfs_header_valid(header, size)) |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1205 | continue; |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1206 | if (!found++) |
| 1207 | result = header; |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1208 | } |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1209 | if (found > 1) |
| 1210 | // Top-aligned images usually have a working relative offset |
| 1211 | // field, so this is more likely to happen on bottom-aligned |
| 1212 | // ones (where the first header is the "outermost" one) |
| 1213 | WARN("Multiple (%d) CBFS headers found, using the first one.\n", |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1214 | found); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1215 | return result; |
| 1216 | } |
| 1217 | |
| 1218 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1219 | struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image) |
| 1220 | { |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 1221 | assert(image); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1222 | return image->has_header ? (struct cbfs_file *)(image->buffer.data + |
| 1223 | image->header.offset) : |
| 1224 | (struct cbfs_file *)image->buffer.data; |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image, |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1228 | struct cbfs_file *entry) |
| 1229 | { |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1230 | uint32_t addr = cbfs_get_entry_addr(image, entry); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1231 | int align = image->has_header ? image->header.align : |
| 1232 | CBFS_ENTRY_ALIGNMENT; |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 1233 | assert(entry && cbfs_is_valid_entry(image, entry)); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1234 | addr += ntohl(entry->offset) + ntohl(entry->len); |
| 1235 | addr = align_up(addr, align); |
| 1236 | return (struct cbfs_file *)(image->buffer.data + addr); |
| 1237 | } |
| 1238 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1239 | uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry) |
| 1240 | { |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1241 | assert(image && image->buffer.data && entry); |
| 1242 | return (int32_t)((char *)entry - image->buffer.data); |
| 1243 | } |
| 1244 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1245 | int cbfs_is_valid_cbfs(struct cbfs_image *image) |
| 1246 | { |
| 1247 | return buffer_check_magic(&image->buffer, CBFS_FILE_MAGIC, |
| 1248 | strlen(CBFS_FILE_MAGIC)); |
| 1249 | } |
| 1250 | |
| 1251 | int cbfs_is_legacy_cbfs(struct cbfs_image *image) |
| 1252 | { |
| 1253 | return image->has_header; |
| 1254 | } |
| 1255 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1256 | int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry) |
| 1257 | { |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1258 | uint32_t offset = cbfs_get_entry_addr(image, entry); |
| 1259 | |
| 1260 | if (offset >= image->buffer.size) |
| 1261 | return 0; |
| 1262 | |
| 1263 | struct buffer entry_data; |
| 1264 | buffer_clone(&entry_data, &image->buffer); |
| 1265 | buffer_seek(&entry_data, offset); |
| 1266 | return buffer_check_magic(&entry_data, CBFS_FILE_MAGIC, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1267 | strlen(CBFS_FILE_MAGIC)); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1268 | } |
| 1269 | |
Patrick Georgi | 57edf16 | 2015-08-12 09:20:11 +0200 | [diff] [blame] | 1270 | struct cbfs_file *cbfs_create_file_header(int type, |
Vadim Bendebury | 45e5997 | 2014-12-23 15:59:57 -0800 | [diff] [blame] | 1271 | size_t len, const char *name) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1272 | { |
Patrick Georgi | 2c61506 | 2015-07-15 20:49:00 +0200 | [diff] [blame] | 1273 | struct cbfs_file *entry = malloc(MAX_CBFS_FILE_HEADER_BUFFER); |
| 1274 | memset(entry, CBFS_CONTENT_DEFAULT_VALUE, MAX_CBFS_FILE_HEADER_BUFFER); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1275 | memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic)); |
Patrick Georgi | edf25d9 | 2015-08-12 09:12:06 +0200 | [diff] [blame] | 1276 | entry->type = htonl(type); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1277 | entry->len = htonl(len); |
Patrick Georgi | 0d618af | 2015-07-15 18:28:23 +0200 | [diff] [blame] | 1278 | entry->attributes_offset = 0; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1279 | entry->offset = htonl(cbfs_calculate_file_header_size(name)); |
Patrick Georgi | c569b8b | 2015-07-15 16:42:38 +0200 | [diff] [blame] | 1280 | memset(entry->filename, 0, ntohl(entry->offset) - sizeof(*entry)); |
| 1281 | strcpy(entry->filename, name); |
Patrick Georgi | 57edf16 | 2015-08-12 09:20:11 +0200 | [diff] [blame] | 1282 | return entry; |
| 1283 | } |
| 1284 | |
| 1285 | int cbfs_create_empty_entry(struct cbfs_file *entry, int type, |
| 1286 | size_t len, const char *name) |
| 1287 | { |
| 1288 | struct cbfs_file *tmp = cbfs_create_file_header(type, len, name); |
| 1289 | memcpy(entry, tmp, ntohl(tmp->offset)); |
| 1290 | free(tmp); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1291 | memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len); |
| 1292 | return 0; |
| 1293 | } |
| 1294 | |
Patrick Georgi | 2c61506 | 2015-07-15 20:49:00 +0200 | [diff] [blame] | 1295 | struct cbfs_file_attribute *cbfs_file_first_attr(struct cbfs_file *file) |
| 1296 | { |
| 1297 | /* attributes_offset should be 0 when there is no attribute, but all |
| 1298 | * values that point into the cbfs_file header are invalid, too. */ |
| 1299 | if (ntohl(file->attributes_offset) <= sizeof(*file)) |
| 1300 | return NULL; |
| 1301 | |
| 1302 | /* There needs to be enough space for the file header and one |
| 1303 | * attribute header for this to make sense. */ |
| 1304 | if (ntohl(file->offset) <= |
| 1305 | sizeof(*file) + sizeof(struct cbfs_file_attribute)) |
| 1306 | return NULL; |
| 1307 | |
| 1308 | return (struct cbfs_file_attribute *) |
| 1309 | (((uint8_t *)file) + ntohl(file->attributes_offset)); |
| 1310 | } |
| 1311 | |
| 1312 | struct cbfs_file_attribute *cbfs_file_next_attr(struct cbfs_file *file, |
| 1313 | struct cbfs_file_attribute *attr) |
| 1314 | { |
| 1315 | /* ex falso sequitur quodlibet */ |
| 1316 | if (attr == NULL) |
| 1317 | return NULL; |
| 1318 | |
| 1319 | /* Is there enough space for another attribute? */ |
| 1320 | if ((uint8_t *)attr + ntohl(attr->len) + |
| 1321 | sizeof(struct cbfs_file_attribute) >= |
| 1322 | (uint8_t *)file + ntohl(file->offset)) |
| 1323 | return NULL; |
| 1324 | |
| 1325 | struct cbfs_file_attribute *next = (struct cbfs_file_attribute *) |
| 1326 | (((uint8_t *)attr) + ntohl(attr->len)); |
| 1327 | /* If any, "unused" attributes must come last. */ |
| 1328 | if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED) |
| 1329 | return NULL; |
| 1330 | if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED2) |
| 1331 | return NULL; |
| 1332 | |
| 1333 | return next; |
| 1334 | } |
| 1335 | |
| 1336 | struct cbfs_file_attribute *cbfs_add_file_attr(struct cbfs_file *header, |
| 1337 | uint32_t tag, |
| 1338 | uint32_t size) |
| 1339 | { |
| 1340 | struct cbfs_file_attribute *attr, *next; |
| 1341 | next = cbfs_file_first_attr(header); |
| 1342 | do { |
| 1343 | attr = next; |
| 1344 | next = cbfs_file_next_attr(header, attr); |
| 1345 | } while (next != NULL); |
| 1346 | uint32_t header_size = ntohl(header->offset) + size; |
| 1347 | if (header_size > MAX_CBFS_FILE_HEADER_BUFFER) { |
| 1348 | DEBUG("exceeding allocated space for cbfs_file headers"); |
| 1349 | return NULL; |
| 1350 | } |
| 1351 | /* attr points to the last valid attribute now. |
| 1352 | * If NULL, we have to create the first one. */ |
| 1353 | if (attr == NULL) { |
| 1354 | /* New attributes start where the header ends. |
| 1355 | * header->offset is later set to accomodate the |
| 1356 | * additional structure. |
| 1357 | * No endianess translation necessary here, because both |
| 1358 | * fields are encoded the same way. */ |
| 1359 | header->attributes_offset = header->offset; |
| 1360 | attr = (struct cbfs_file_attribute *) |
| 1361 | (((uint8_t *)header) + |
| 1362 | ntohl(header->attributes_offset)); |
| 1363 | } else { |
| 1364 | attr = (struct cbfs_file_attribute *) |
| 1365 | (((uint8_t *)attr) + |
| 1366 | ntohl(attr->len)); |
| 1367 | } |
| 1368 | header->offset = htonl(header_size); |
| 1369 | memset(attr, CBFS_CONTENT_DEFAULT_VALUE, size); |
| 1370 | attr->tag = htonl(tag); |
| 1371 | attr->len = htonl(size); |
| 1372 | return attr; |
| 1373 | } |
| 1374 | |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 1375 | int cbfs_add_file_hash(struct cbfs_file *header, struct buffer *buffer, |
| 1376 | enum vb2_hash_algorithm hash_type) |
| 1377 | { |
| 1378 | if (hash_type >= CBFS_NUM_SUPPORTED_HASHES) |
| 1379 | return -1; |
| 1380 | |
| 1381 | unsigned hash_size = widths_cbfs_hash[hash_type]; |
| 1382 | if (hash_size == 0) |
| 1383 | return -1; |
| 1384 | |
| 1385 | struct cbfs_file_attr_hash *attrs = |
| 1386 | (struct cbfs_file_attr_hash *)cbfs_add_file_attr(header, |
| 1387 | CBFS_FILE_ATTR_TAG_HASH, |
| 1388 | sizeof(struct cbfs_file_attr_hash) + hash_size); |
| 1389 | |
| 1390 | if (attrs == NULL) |
| 1391 | return -1; |
| 1392 | |
| 1393 | attrs->hash_type = htonl(hash_type); |
| 1394 | if (vb2_digest_buffer(buffer_get(buffer), buffer_size(buffer), |
| 1395 | hash_type, attrs->hash_data, hash_size) != VB2_SUCCESS) |
| 1396 | return -1; |
| 1397 | |
| 1398 | return 0; |
| 1399 | } |
| 1400 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1401 | /* Finds a place to hold whole data in same memory page. */ |
| 1402 | static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page) |
| 1403 | { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1404 | if (!page) |
| 1405 | return 1; |
| 1406 | return (start / page) == (start + size - 1) / page; |
| 1407 | } |
| 1408 | |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1409 | /* Tests if data can fit in a range by given offset: |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1410 | * start ->| metadata_size | offset (+ size) |<- end |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1411 | */ |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1412 | static int is_in_range(size_t start, size_t end, size_t metadata_size, |
| 1413 | size_t offset, size_t size) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1414 | { |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1415 | return (offset >= start + metadata_size && offset + size <= end); |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1416 | } |
| 1417 | |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1418 | int32_t cbfs_locate_entry(struct cbfs_image *image, size_t size, |
| 1419 | size_t page_size, size_t align, size_t metadata_size) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1420 | { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1421 | struct cbfs_file *entry; |
| 1422 | size_t need_len; |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1423 | size_t addr, addr_next, addr2, addr3, offset; |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1424 | |
| 1425 | /* Default values: allow fitting anywhere in ROM. */ |
| 1426 | if (!page_size) |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1427 | page_size = image->has_header ? image->header.romsize : |
| 1428 | image->buffer.size; |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1429 | if (!align) |
| 1430 | align = 1; |
| 1431 | |
| 1432 | if (size > page_size) |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1433 | ERROR("Input file size (%zd) greater than page size (%zd).\n", |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1434 | size, page_size); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1435 | |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1436 | size_t image_align = image->has_header ? image->header.align : |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1437 | CBFS_ENTRY_ALIGNMENT; |
| 1438 | if (page_size % image_align) |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1439 | WARN("%s: Page size (%#zx) not aligned with CBFS image (%#zx).\n", |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1440 | __func__, page_size, image_align); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1441 | |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1442 | need_len = metadata_size + size; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1443 | |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1444 | // Merge empty entries to build get max available space. |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1445 | cbfs_walk(image, cbfs_merge_empty_entry, NULL); |
| 1446 | |
| 1447 | /* Three cases of content location on memory page: |
| 1448 | * case 1. |
| 1449 | * | PAGE 1 | PAGE 2 | |
| 1450 | * | <header><content>| Fit. Return start of content. |
| 1451 | * |
| 1452 | * case 2. |
| 1453 | * | PAGE 1 | PAGE 2 | |
| 1454 | * | <header><content> | Fits when we shift content to align |
| 1455 | * shift-> | <header>|<content> | at starting of PAGE 2. |
| 1456 | * |
| 1457 | * case 3. (large content filling whole page) |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1458 | * | PAGE 1 | PAGE 2 | PAGE 3 | |
| 1459 | * | <header>< content > | Can't fit. If we shift content to |
| 1460 | * |trial-> <header>< content > | PAGE 2, header can't fit in free |
| 1461 | * | shift-> <header><content> space, so we must use PAGE 3. |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1462 | * |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1463 | * The returned address can be then used as "base-address" (-b) in add-* |
| 1464 | * commands (will be re-calculated and positioned by cbfs_add_entry_at). |
| 1465 | * For stage targets, the address is also used to re-link stage before |
| 1466 | * being added into CBFS. |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1467 | */ |
| 1468 | for (entry = cbfs_find_first_entry(image); |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 1469 | entry && cbfs_is_valid_entry(image, entry); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1470 | entry = cbfs_find_next_entry(image, entry)) { |
| 1471 | |
| 1472 | uint32_t type = ntohl(entry->type); |
| 1473 | if (type != CBFS_COMPONENT_NULL) |
| 1474 | continue; |
| 1475 | |
| 1476 | addr = cbfs_get_entry_addr(image, entry); |
| 1477 | addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry( |
| 1478 | image, entry)); |
| 1479 | if (addr_next - addr < need_len) |
| 1480 | continue; |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1481 | |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1482 | offset = align_up(addr + metadata_size, align); |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1483 | if (is_in_same_page(offset, size, page_size) && |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1484 | is_in_range(addr, addr_next, metadata_size, offset, size)) { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1485 | DEBUG("cbfs_locate_entry: FIT (PAGE1)."); |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1486 | return offset; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | addr2 = align_up(addr, page_size); |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1490 | offset = align_up(addr2, align); |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1491 | if (is_in_range(addr, addr_next, metadata_size, offset, size)) { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1492 | DEBUG("cbfs_locate_entry: OVERLAP (PAGE2)."); |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1493 | return offset; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1494 | } |
| 1495 | |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1496 | /* Assume page_size >= metadata_size so adding one page will |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1497 | * definitely provide the space for header. */ |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1498 | assert(page_size >= metadata_size); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1499 | addr3 = addr2 + page_size; |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1500 | offset = align_up(addr3, align); |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1501 | if (is_in_range(addr, addr_next, metadata_size, offset, size)) { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1502 | DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3)."); |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1503 | return offset; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1504 | } |
| 1505 | } |
| 1506 | return -1; |
| 1507 | } |