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