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