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