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