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 | } |
| 177 | memcpy(image->buffer.data + bootblock_offset, bootblock->data, |
| 178 | bootblock->size); |
| 179 | |
| 180 | // Prepare header |
| 181 | if (header_offset + sizeof(*header) > size) { |
| 182 | ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%zx)\n", |
| 183 | header_offset, sizeof(*header), size); |
| 184 | return -1; |
| 185 | } |
| 186 | header = (struct cbfs_header *)(image->buffer.data + header_offset); |
| 187 | image->header = header; |
| 188 | header->magic = htonl(CBFS_HEADER_MAGIC); |
| 189 | header->version = htonl(CBFS_HEADER_VERSION); |
| 190 | header->romsize = htonl(size); |
| 191 | header->bootblocksize = htonl(bootblock->size); |
| 192 | header->align = htonl(align); |
| 193 | header->offset = htonl(entries_offset); |
| 194 | header->architecture = htonl(arch); |
| 195 | |
| 196 | // Prepare entries |
| 197 | if (align_up(entries_offset, align) != entries_offset) { |
| 198 | ERROR("Offset (0x%x) must be aligned to 0x%x.\n", |
| 199 | entries_offset, align); |
| 200 | return -1; |
| 201 | } |
| 202 | if (entries_offset + sizeof(*entry) > size) { |
| 203 | ERROR("Offset (0x%x+0x%zx) exceed ROM size(0x%zx)\n", |
| 204 | entries_offset, sizeof(*entry), size); |
| 205 | return -1; |
| 206 | } |
| 207 | entry = (struct cbfs_file *)(image->buffer.data + entries_offset); |
| 208 | // To calculate available length, find |
| 209 | // e = min(bootblock, header, size) where e > entries_offset. |
| 210 | cbfs_len = size; |
| 211 | if (bootblock_offset > entries_offset && bootblock_offset < cbfs_len) |
| 212 | cbfs_len = bootblock_offset; |
| 213 | if (header_offset > entries_offset && header_offset < cbfs_len) |
| 214 | cbfs_len = header_offset; |
| 215 | cbfs_len -= entries_offset + align; |
| 216 | cbfs_create_empty_entry(image, entry, cbfs_len, ""); |
| 217 | LOG("Created CBFS image (capacity = %d bytes)\n", cbfs_len); |
| 218 | return 0; |
| 219 | } |
| 220 | |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 221 | int cbfs_image_from_file(struct cbfs_image *image, const char *filename) { |
| 222 | if (buffer_from_file(&image->buffer, filename) != 0) |
| 223 | return -1; |
| 224 | DEBUG("read_cbfs_image: %s (%zd bytes)\n", image->buffer.name, |
| 225 | image->buffer.size); |
| 226 | image->header = cbfs_find_header(image->buffer.data, |
| 227 | image->buffer.size); |
| 228 | if (!image->header) { |
| 229 | ERROR("%s does not have CBFS master header.\n", filename); |
| 230 | cbfs_image_delete(image); |
| 231 | return -1; |
| 232 | } |
Hung-Te Lin | 49fcd75 | 2013-01-29 03:16:20 +0800 | [diff] [blame] | 233 | cbfs_fix_legacy_size(image); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | int cbfs_image_write_file(struct cbfs_image *image, const char *filename) { |
| 239 | assert(image && image->buffer.data); |
| 240 | return buffer_write_file(&image->buffer, filename); |
| 241 | } |
| 242 | |
| 243 | int cbfs_image_delete(struct cbfs_image *image) { |
| 244 | buffer_delete(&image->buffer); |
| 245 | image->header = NULL; |
| 246 | return 0; |
| 247 | } |
| 248 | |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 249 | struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name) { |
| 250 | struct cbfs_file *entry; |
| 251 | for (entry = cbfs_find_first_entry(image); |
| 252 | entry && cbfs_is_valid_entry(entry); |
| 253 | entry = cbfs_find_next_entry(image, entry)) { |
| 254 | if (strcasecmp(CBFS_NAME(entry), name) == 0) { |
| 255 | DEBUG("cbfs_get_entry: found %s\n", name); |
| 256 | return entry; |
| 257 | } |
| 258 | } |
| 259 | return NULL; |
| 260 | } |
| 261 | |
| 262 | int cbfs_export_entry(struct cbfs_image *image, const char *entry_name, |
| 263 | const char *filename) { |
| 264 | struct cbfs_file *entry = cbfs_get_entry(image, entry_name); |
| 265 | struct buffer buffer; |
| 266 | if (!entry) { |
| 267 | ERROR("File not found: %s\n", entry_name); |
| 268 | return -1; |
| 269 | } |
| 270 | LOG("Found file %.30s at 0x%x, type %.12s, size %d\n", |
| 271 | entry_name, cbfs_get_entry_addr(image, entry), |
| 272 | get_cbfs_entry_type_name(ntohl(entry->type)), ntohl(entry->len)); |
| 273 | |
| 274 | if (ntohl(entry->type) != CBFS_COMPONENT_RAW) { |
| 275 | WARN("Only 'raw' files are safe to extract.\n"); |
| 276 | } |
| 277 | |
| 278 | buffer.data = CBFS_SUBHEADER(entry); |
| 279 | buffer.size = ntohl(entry->len); |
| 280 | buffer.name = "(cbfs_export_entry)"; |
| 281 | if (buffer_write_file(&buffer, filename) != 0) { |
| 282 | ERROR("Failed to write %s into %s.\n", |
| 283 | entry_name, filename); |
| 284 | return -1; |
| 285 | } |
| 286 | INFO("Successfully dumped the file to: %s\n", filename); |
| 287 | return 0; |
| 288 | } |
| 289 | |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 290 | int cbfs_remove_entry(struct cbfs_image *image, const char *name) { |
| 291 | struct cbfs_file *entry, *next; |
| 292 | size_t len; |
| 293 | entry = cbfs_get_entry(image, name); |
| 294 | if (!entry) { |
| 295 | ERROR("CBFS file %s not found.\n", name); |
| 296 | return -1; |
| 297 | } |
| 298 | next = cbfs_find_next_entry(image, entry); |
| 299 | assert(next); |
| 300 | DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n", |
| 301 | CBFS_NAME(entry), cbfs_get_entry_addr(image, entry)); |
| 302 | entry->type = htonl(CBFS_COMPONENT_DELETED); |
| 303 | len = (cbfs_get_entry_addr(image, next) - |
| 304 | cbfs_get_entry_addr(image, entry)); |
| 305 | entry->offset = htonl(cbfs_calculate_file_header_size("")); |
| 306 | entry->len = htonl(len - ntohl(entry->offset)); |
| 307 | memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry)); |
| 308 | memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, |
| 309 | ntohl(entry->len)); |
| 310 | return 0; |
| 311 | } |
| 312 | |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 313 | int cbfs_print_header_info(struct cbfs_image *image) { |
| 314 | char *name = strdup(image->buffer.name); |
| 315 | assert(image && image->header); |
| 316 | printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n" |
| 317 | "alignment: %d bytes\n\n", |
| 318 | basename(name), |
| 319 | image->buffer.size / 1024, |
| 320 | ntohl(image->header->bootblocksize), |
| 321 | ntohl(image->header->romsize), |
| 322 | ntohl(image->header->offset), |
| 323 | ntohl(image->header->align)); |
| 324 | free(name); |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | static int cbfs_print_stage_info(struct cbfs_stage *stage, FILE* fp) { |
| 329 | fprintf(fp, |
| 330 | " %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", " |
| 331 | "length: %d/%d\n", |
| 332 | lookup_name_by_type(types_cbfs_compression, |
| 333 | stage->compression, "(unknown)"), |
| 334 | stage->entry, |
| 335 | stage->load, |
| 336 | stage->len, |
| 337 | stage->memlen); |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | static int cbfs_print_payload_segment_info(struct cbfs_payload_segment *payload, |
| 342 | FILE *fp) |
| 343 | { |
| 344 | switch(payload->type) { |
| 345 | case PAYLOAD_SEGMENT_CODE: |
| 346 | case PAYLOAD_SEGMENT_DATA: |
| 347 | fprintf(fp, " %s (%s compression, offset: 0x%x, " |
| 348 | "load: 0x%" PRIx64 ", length: %d/%d)\n", |
| 349 | (payload->type == PAYLOAD_SEGMENT_CODE ? |
| 350 | "code " : "data"), |
| 351 | lookup_name_by_type(types_cbfs_compression, |
| 352 | ntohl(payload->compression), |
| 353 | "(unknown)"), |
| 354 | ntohl(payload->offset), |
| 355 | ntohll(payload->load_addr), |
| 356 | ntohl(payload->len), ntohl(payload->mem_len)); |
| 357 | break; |
| 358 | |
| 359 | case PAYLOAD_SEGMENT_ENTRY: |
| 360 | fprintf(fp, " entry (0x%" PRIx64 ")\n", |
| 361 | ntohll(payload->load_addr)); |
| 362 | break; |
| 363 | |
| 364 | case PAYLOAD_SEGMENT_BSS: |
| 365 | fprintf(fp, " BSS (address 0x%016" PRIx64 ", " |
| 366 | "length 0x%x)\n", |
| 367 | ntohll(payload->load_addr), |
| 368 | ntohl(payload->len)); |
| 369 | break; |
| 370 | |
| 371 | case PAYLOAD_SEGMENT_PARAMS: |
| 372 | fprintf(fp, " parameters\n"); |
| 373 | break; |
| 374 | |
| 375 | default: |
| 376 | fprintf(fp, " 0x%x (%s compression, offset: 0x%x, " |
| 377 | "load: 0x%" PRIx64 ", length: %d/%d\n", |
| 378 | payload->type, |
| 379 | lookup_name_by_type(types_cbfs_compression, |
| 380 | payload->compression, |
| 381 | "(unknown)"), |
| 382 | ntohl(payload->offset), |
| 383 | ntohll(payload->load_addr), |
| 384 | ntohl(payload->len), |
| 385 | ntohl(payload->mem_len)); |
| 386 | break; |
| 387 | } |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry, |
| 392 | void *arg) { |
| 393 | const char *name = CBFS_NAME(entry); |
| 394 | struct cbfs_payload_segment *payload; |
| 395 | FILE *fp = (FILE *)arg; |
| 396 | |
| 397 | if (!cbfs_is_valid_entry(entry)) { |
| 398 | ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n", |
| 399 | cbfs_get_entry_addr(image, entry)); |
| 400 | return -1; |
| 401 | } |
| 402 | if (!fp) |
| 403 | fp = stdout; |
| 404 | |
| 405 | fprintf(fp, "%-30s 0x%-8x %-12s %d\n", |
| 406 | *name ? name : "(empty)", |
| 407 | cbfs_get_entry_addr(image, entry), |
| 408 | get_cbfs_entry_type_name(ntohl(entry->type)), |
| 409 | ntohl(entry->len)); |
| 410 | |
| 411 | if (!verbose) |
| 412 | return 0; |
| 413 | |
| 414 | DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n", |
| 415 | cbfs_get_entry_addr(image, entry), ntohl(entry->offset), |
| 416 | cbfs_get_entry_addr(image, entry) + ntohl(entry->offset), |
| 417 | ntohl(entry->len)); |
| 418 | |
| 419 | /* note the components of the subheader may be in host order ... */ |
| 420 | switch (ntohl(entry->type)) { |
| 421 | case CBFS_COMPONENT_STAGE: |
| 422 | cbfs_print_stage_info((struct cbfs_stage *) |
| 423 | CBFS_SUBHEADER(entry), fp); |
| 424 | break; |
| 425 | |
| 426 | case CBFS_COMPONENT_PAYLOAD: |
| 427 | payload = (struct cbfs_payload_segment *) |
| 428 | CBFS_SUBHEADER(entry); |
| 429 | while (payload) { |
| 430 | cbfs_print_payload_segment_info(payload, fp); |
| 431 | if (payload->type == PAYLOAD_SEGMENT_ENTRY) |
| 432 | break; |
| 433 | else |
| 434 | payload ++; |
| 435 | } |
| 436 | break; |
| 437 | default: |
| 438 | break; |
| 439 | } |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | int cbfs_print_directory(struct cbfs_image *image) { |
| 444 | cbfs_print_header_info(image); |
| 445 | printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type"); |
| 446 | cbfs_walk(image, cbfs_print_entry_info, NULL); |
| 447 | return 0; |
| 448 | } |
| 449 | |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 450 | int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry, |
| 451 | void *arg) { |
| 452 | struct cbfs_file *next; |
| 453 | uint32_t type, addr, last_addr; |
| 454 | |
| 455 | type = ntohl(entry->type); |
| 456 | if (type == CBFS_COMPONENT_DELETED) { |
| 457 | // Ready to be recycled. |
| 458 | type = CBFS_COMPONENT_NULL; |
| 459 | entry->type = htonl(type); |
| 460 | } |
| 461 | if (type != CBFS_COMPONENT_NULL) |
| 462 | return 0; |
| 463 | |
| 464 | next = cbfs_find_next_entry(image, entry); |
| 465 | |
| 466 | while (next && cbfs_is_valid_entry(next)) { |
| 467 | type = ntohl(next->type); |
| 468 | if (type == CBFS_COMPONENT_DELETED) { |
| 469 | type = CBFS_COMPONENT_NULL; |
| 470 | next->type = htonl(type); |
| 471 | } |
| 472 | if (type != CBFS_COMPONENT_NULL) |
| 473 | return 0; |
| 474 | |
| 475 | addr = cbfs_get_entry_addr(image, entry); |
| 476 | last_addr = cbfs_get_entry_addr( |
| 477 | image, cbfs_find_next_entry(image, next)); |
| 478 | |
| 479 | // Now, we find two deleted/empty entries; try to merge now. |
| 480 | DEBUG("join_empty_entry: combine 0x%x+0x%x and 0x%x+0x%x.\n", |
| 481 | cbfs_get_entry_addr(image, entry), ntohl(entry->len), |
| 482 | cbfs_get_entry_addr(image, next), ntohl(next->len)); |
| 483 | cbfs_create_empty_entry(image, entry, |
| 484 | (last_addr - addr - |
| 485 | cbfs_calculate_file_header_size("")), |
| 486 | ""); |
| 487 | DEBUG("new empty entry: length=0x%x\n", ntohl(entry->len)); |
| 488 | next = cbfs_find_next_entry(image, entry); |
| 489 | } |
| 490 | return 0; |
| 491 | } |
| 492 | |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 493 | int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback, |
| 494 | void *arg) { |
| 495 | int count = 0; |
| 496 | struct cbfs_file *entry; |
| 497 | for (entry = cbfs_find_first_entry(image); |
| 498 | entry && cbfs_is_valid_entry(entry); |
| 499 | entry = cbfs_find_next_entry(image, entry)) { |
| 500 | count ++; |
| 501 | if (callback(image, entry, arg) != 0) |
| 502 | break; |
| 503 | } |
| 504 | return count; |
| 505 | } |
| 506 | |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 507 | struct cbfs_header *cbfs_find_header(char *data, size_t size) { |
| 508 | size_t offset; |
| 509 | int found = 0; |
| 510 | uint32_t x86sig; |
| 511 | struct cbfs_header *header, *result = NULL; |
| 512 | |
| 513 | // Try x86 style (check signature in bottom) header first. |
| 514 | x86sig = *(uint32_t *)(data + size - sizeof(uint32_t)); |
| 515 | offset = (x86sig + (uint32_t)size); |
| 516 | DEBUG("x86sig: 0x%x, offset: 0x%zx\n", x86sig, offset); |
| 517 | if (offset >= size - sizeof(*header) || |
| 518 | ntohl(((struct cbfs_header *)(data + offset))->magic) != |
| 519 | CBFS_HEADER_MAGIC) |
| 520 | offset = 0; |
| 521 | |
| 522 | for (; offset + sizeof(*header) < size; offset++) { |
| 523 | header = (struct cbfs_header *)(data + offset); |
| 524 | if (ntohl(header->magic) !=(CBFS_HEADER_MAGIC)) |
| 525 | continue; |
| 526 | if (ntohl(header->version) != CBFS_HEADER_VERSION1 && |
| 527 | ntohl(header->version) != CBFS_HEADER_VERSION2) { |
| 528 | // Probably not a real CBFS header? |
| 529 | continue; |
| 530 | } |
| 531 | found++; |
| 532 | result = header; |
| 533 | } |
| 534 | if (found > 1) { |
| 535 | ERROR("multiple (%d) CBFS headers found!\n", |
| 536 | found); |
| 537 | result = NULL; |
| 538 | } |
| 539 | return result; |
| 540 | } |
| 541 | |
| 542 | |
| 543 | struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image) { |
| 544 | assert(image && image->header); |
| 545 | return (struct cbfs_file *)(image->buffer.data + |
| 546 | ntohl(image->header->offset)); |
| 547 | } |
| 548 | |
| 549 | struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image, |
| 550 | struct cbfs_file *entry) { |
| 551 | uint32_t addr = cbfs_get_entry_addr(image, entry); |
| 552 | int align = ntohl(image->header->align); |
| 553 | assert(entry && cbfs_is_valid_entry(entry)); |
| 554 | addr += ntohl(entry->offset) + ntohl(entry->len); |
| 555 | addr = align_up(addr, align); |
| 556 | return (struct cbfs_file *)(image->buffer.data + addr); |
| 557 | } |
| 558 | |
| 559 | uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry) { |
| 560 | assert(image && image->buffer.data && entry); |
| 561 | return (int32_t)((char *)entry - image->buffer.data); |
| 562 | } |
| 563 | |
| 564 | int cbfs_is_valid_entry(struct cbfs_file *entry) { |
| 565 | return (entry &&memcmp(entry->magic, CBFS_FILE_MAGIC, |
| 566 | sizeof(entry->magic)) == 0); |
| 567 | } |
| 568 | |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 569 | int cbfs_create_empty_entry(struct cbfs_image *image, struct cbfs_file *entry, |
| 570 | size_t len, const char *name) { |
| 571 | memset(entry, CBFS_CONTENT_DEFAULT_VALUE, sizeof(*entry)); |
| 572 | memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic)); |
| 573 | entry->type = htonl(CBFS_COMPONENT_NULL); |
| 574 | entry->len = htonl(len); |
| 575 | entry->checksum = 0; // TODO Build a checksum algorithm. |
| 576 | entry->offset = htonl(cbfs_calculate_file_header_size(name)); |
| 577 | memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry)); |
| 578 | strcpy(CBFS_NAME(entry), name); |
| 579 | memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len); |
| 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | /* Finds a place to hold whole stage data in same memory page. |
| 584 | */ |
| 585 | static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page) { |
| 586 | if (!page) |
| 587 | return 1; |
| 588 | return (start / page) == (start + size - 1) / page; |
| 589 | } |
| 590 | |
| 591 | int32_t cbfs_locate_entry(struct cbfs_image *image, const char *name, |
| 592 | uint32_t size, uint32_t page_size) { |
| 593 | struct cbfs_file *entry; |
| 594 | size_t need_len; |
| 595 | uint32_t addr, addr_next, addr2, addr3, header_len; |
| 596 | assert(size < page_size); |
| 597 | |
| 598 | if (page_size % ntohl(image->header->align)) |
| 599 | WARN("locate_entry: page does not align with CBFS image.\n"); |
| 600 | |
| 601 | /* TODO Old cbfstool always assume input is a stage file (and adding |
| 602 | * sizeof(cbfs_stage) for header. We should fix that by adding "-t" |
| 603 | * (type) param in future. For right now, follow old behavior. */ |
| 604 | header_len = (cbfs_calculate_file_header_size(name) + |
| 605 | sizeof(struct cbfs_stage)); |
| 606 | need_len = header_len + size; |
| 607 | |
| 608 | // Merge empty entries to build get max available pages. |
| 609 | cbfs_walk(image, cbfs_merge_empty_entry, NULL); |
| 610 | |
| 611 | /* Three cases of content location on memory page: |
| 612 | * case 1. |
| 613 | * | PAGE 1 | PAGE 2 | |
| 614 | * | <header><content>| Fit. Return start of content. |
| 615 | * |
| 616 | * case 2. |
| 617 | * | PAGE 1 | PAGE 2 | |
| 618 | * | <header><content> | Fits when we shift content to align |
| 619 | * shift-> | <header>|<content> | at starting of PAGE 2. |
| 620 | * |
| 621 | * case 3. (large content filling whole page) |
| 622 | * | PAGE 1 | PAGE 2 | PAGE 3| |
| 623 | * | <header>< content > | | Can't fit. If we shift content to |
| 624 | * | { free space . } PAGE 2, header can't fit in free |
| 625 | * | shift-> <header><content> space, so we must use PAGE 3. |
| 626 | * |
| 627 | * The returned address will be used to re-link stage file, and then |
| 628 | * assigned to add-stage command (-b), which will be then re-calculated |
| 629 | * by ELF loader and positioned by cbfs_add_entry. |
| 630 | */ |
| 631 | for (entry = cbfs_find_first_entry(image); |
| 632 | entry && cbfs_is_valid_entry(entry); |
| 633 | entry = cbfs_find_next_entry(image, entry)) { |
| 634 | |
| 635 | uint32_t type = ntohl(entry->type); |
| 636 | if (type != CBFS_COMPONENT_NULL) |
| 637 | continue; |
| 638 | |
| 639 | addr = cbfs_get_entry_addr(image, entry); |
| 640 | addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry( |
| 641 | image, entry)); |
| 642 | if (addr_next - addr < need_len) |
| 643 | continue; |
| 644 | if (is_in_same_page(addr + header_len, size, page_size)) { |
| 645 | DEBUG("cbfs_locate_entry: FIT (PAGE1)."); |
| 646 | return addr + header_len; |
| 647 | } |
| 648 | |
| 649 | addr2 = align_up(addr, page_size); |
| 650 | if (addr2 < addr_next && addr_next - addr2 >= size && |
| 651 | addr2 - addr >= header_len) { |
| 652 | DEBUG("cbfs_locate_entry: OVERLAP (PAGE2)."); |
| 653 | return addr2; |
| 654 | } |
| 655 | |
| 656 | addr3 = addr2 + page_size; |
| 657 | if (addr3 < addr_next && addr_next - addr3 >= size && |
| 658 | addr3 - addr >= header_len) { |
| 659 | DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3)."); |
| 660 | return addr3; |
| 661 | } |
| 662 | } |
| 663 | return -1; |
| 664 | } |