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