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 | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 101 | int cbfs_image_from_file(struct cbfs_image *image, const char *filename) { |
| 102 | if (buffer_from_file(&image->buffer, filename) != 0) |
| 103 | return -1; |
| 104 | DEBUG("read_cbfs_image: %s (%zd bytes)\n", image->buffer.name, |
| 105 | image->buffer.size); |
| 106 | image->header = cbfs_find_header(image->buffer.data, |
| 107 | image->buffer.size); |
| 108 | if (!image->header) { |
| 109 | ERROR("%s does not have CBFS master header.\n", filename); |
| 110 | cbfs_image_delete(image); |
| 111 | return -1; |
| 112 | } |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | int cbfs_image_write_file(struct cbfs_image *image, const char *filename) { |
| 118 | assert(image && image->buffer.data); |
| 119 | return buffer_write_file(&image->buffer, filename); |
| 120 | } |
| 121 | |
| 122 | int cbfs_image_delete(struct cbfs_image *image) { |
| 123 | buffer_delete(&image->buffer); |
| 124 | image->header = NULL; |
| 125 | return 0; |
| 126 | } |
| 127 | |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame^] | 128 | struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name) { |
| 129 | struct cbfs_file *entry; |
| 130 | for (entry = cbfs_find_first_entry(image); |
| 131 | entry && cbfs_is_valid_entry(entry); |
| 132 | entry = cbfs_find_next_entry(image, entry)) { |
| 133 | if (strcasecmp(CBFS_NAME(entry), name) == 0) { |
| 134 | DEBUG("cbfs_get_entry: found %s\n", name); |
| 135 | return entry; |
| 136 | } |
| 137 | } |
| 138 | return NULL; |
| 139 | } |
| 140 | |
| 141 | int cbfs_export_entry(struct cbfs_image *image, const char *entry_name, |
| 142 | const char *filename) { |
| 143 | struct cbfs_file *entry = cbfs_get_entry(image, entry_name); |
| 144 | struct buffer buffer; |
| 145 | if (!entry) { |
| 146 | ERROR("File not found: %s\n", entry_name); |
| 147 | return -1; |
| 148 | } |
| 149 | LOG("Found file %.30s at 0x%x, type %.12s, size %d\n", |
| 150 | entry_name, cbfs_get_entry_addr(image, entry), |
| 151 | get_cbfs_entry_type_name(ntohl(entry->type)), ntohl(entry->len)); |
| 152 | |
| 153 | if (ntohl(entry->type) != CBFS_COMPONENT_RAW) { |
| 154 | WARN("Only 'raw' files are safe to extract.\n"); |
| 155 | } |
| 156 | |
| 157 | buffer.data = CBFS_SUBHEADER(entry); |
| 158 | buffer.size = ntohl(entry->len); |
| 159 | buffer.name = "(cbfs_export_entry)"; |
| 160 | if (buffer_write_file(&buffer, filename) != 0) { |
| 161 | ERROR("Failed to write %s into %s.\n", |
| 162 | entry_name, filename); |
| 163 | return -1; |
| 164 | } |
| 165 | INFO("Successfully dumped the file to: %s\n", filename); |
| 166 | return 0; |
| 167 | } |
| 168 | |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 169 | int cbfs_print_header_info(struct cbfs_image *image) { |
| 170 | char *name = strdup(image->buffer.name); |
| 171 | assert(image && image->header); |
| 172 | printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n" |
| 173 | "alignment: %d bytes\n\n", |
| 174 | basename(name), |
| 175 | image->buffer.size / 1024, |
| 176 | ntohl(image->header->bootblocksize), |
| 177 | ntohl(image->header->romsize), |
| 178 | ntohl(image->header->offset), |
| 179 | ntohl(image->header->align)); |
| 180 | free(name); |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | static int cbfs_print_stage_info(struct cbfs_stage *stage, FILE* fp) { |
| 185 | fprintf(fp, |
| 186 | " %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", " |
| 187 | "length: %d/%d\n", |
| 188 | lookup_name_by_type(types_cbfs_compression, |
| 189 | stage->compression, "(unknown)"), |
| 190 | stage->entry, |
| 191 | stage->load, |
| 192 | stage->len, |
| 193 | stage->memlen); |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | static int cbfs_print_payload_segment_info(struct cbfs_payload_segment *payload, |
| 198 | FILE *fp) |
| 199 | { |
| 200 | switch(payload->type) { |
| 201 | case PAYLOAD_SEGMENT_CODE: |
| 202 | case PAYLOAD_SEGMENT_DATA: |
| 203 | fprintf(fp, " %s (%s compression, offset: 0x%x, " |
| 204 | "load: 0x%" PRIx64 ", length: %d/%d)\n", |
| 205 | (payload->type == PAYLOAD_SEGMENT_CODE ? |
| 206 | "code " : "data"), |
| 207 | lookup_name_by_type(types_cbfs_compression, |
| 208 | ntohl(payload->compression), |
| 209 | "(unknown)"), |
| 210 | ntohl(payload->offset), |
| 211 | ntohll(payload->load_addr), |
| 212 | ntohl(payload->len), ntohl(payload->mem_len)); |
| 213 | break; |
| 214 | |
| 215 | case PAYLOAD_SEGMENT_ENTRY: |
| 216 | fprintf(fp, " entry (0x%" PRIx64 ")\n", |
| 217 | ntohll(payload->load_addr)); |
| 218 | break; |
| 219 | |
| 220 | case PAYLOAD_SEGMENT_BSS: |
| 221 | fprintf(fp, " BSS (address 0x%016" PRIx64 ", " |
| 222 | "length 0x%x)\n", |
| 223 | ntohll(payload->load_addr), |
| 224 | ntohl(payload->len)); |
| 225 | break; |
| 226 | |
| 227 | case PAYLOAD_SEGMENT_PARAMS: |
| 228 | fprintf(fp, " parameters\n"); |
| 229 | break; |
| 230 | |
| 231 | default: |
| 232 | fprintf(fp, " 0x%x (%s compression, offset: 0x%x, " |
| 233 | "load: 0x%" PRIx64 ", length: %d/%d\n", |
| 234 | payload->type, |
| 235 | lookup_name_by_type(types_cbfs_compression, |
| 236 | payload->compression, |
| 237 | "(unknown)"), |
| 238 | ntohl(payload->offset), |
| 239 | ntohll(payload->load_addr), |
| 240 | ntohl(payload->len), |
| 241 | ntohl(payload->mem_len)); |
| 242 | break; |
| 243 | } |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry, |
| 248 | void *arg) { |
| 249 | const char *name = CBFS_NAME(entry); |
| 250 | struct cbfs_payload_segment *payload; |
| 251 | FILE *fp = (FILE *)arg; |
| 252 | |
| 253 | if (!cbfs_is_valid_entry(entry)) { |
| 254 | ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n", |
| 255 | cbfs_get_entry_addr(image, entry)); |
| 256 | return -1; |
| 257 | } |
| 258 | if (!fp) |
| 259 | fp = stdout; |
| 260 | |
| 261 | fprintf(fp, "%-30s 0x%-8x %-12s %d\n", |
| 262 | *name ? name : "(empty)", |
| 263 | cbfs_get_entry_addr(image, entry), |
| 264 | get_cbfs_entry_type_name(ntohl(entry->type)), |
| 265 | ntohl(entry->len)); |
| 266 | |
| 267 | if (!verbose) |
| 268 | return 0; |
| 269 | |
| 270 | DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n", |
| 271 | cbfs_get_entry_addr(image, entry), ntohl(entry->offset), |
| 272 | cbfs_get_entry_addr(image, entry) + ntohl(entry->offset), |
| 273 | ntohl(entry->len)); |
| 274 | |
| 275 | /* note the components of the subheader may be in host order ... */ |
| 276 | switch (ntohl(entry->type)) { |
| 277 | case CBFS_COMPONENT_STAGE: |
| 278 | cbfs_print_stage_info((struct cbfs_stage *) |
| 279 | CBFS_SUBHEADER(entry), fp); |
| 280 | break; |
| 281 | |
| 282 | case CBFS_COMPONENT_PAYLOAD: |
| 283 | payload = (struct cbfs_payload_segment *) |
| 284 | CBFS_SUBHEADER(entry); |
| 285 | while (payload) { |
| 286 | cbfs_print_payload_segment_info(payload, fp); |
| 287 | if (payload->type == PAYLOAD_SEGMENT_ENTRY) |
| 288 | break; |
| 289 | else |
| 290 | payload ++; |
| 291 | } |
| 292 | break; |
| 293 | default: |
| 294 | break; |
| 295 | } |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | int cbfs_print_directory(struct cbfs_image *image) { |
| 300 | cbfs_print_header_info(image); |
| 301 | printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type"); |
| 302 | cbfs_walk(image, cbfs_print_entry_info, NULL); |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback, |
| 307 | void *arg) { |
| 308 | int count = 0; |
| 309 | struct cbfs_file *entry; |
| 310 | for (entry = cbfs_find_first_entry(image); |
| 311 | entry && cbfs_is_valid_entry(entry); |
| 312 | entry = cbfs_find_next_entry(image, entry)) { |
| 313 | count ++; |
| 314 | if (callback(image, entry, arg) != 0) |
| 315 | break; |
| 316 | } |
| 317 | return count; |
| 318 | } |
| 319 | |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 320 | struct cbfs_header *cbfs_find_header(char *data, size_t size) { |
| 321 | size_t offset; |
| 322 | int found = 0; |
| 323 | uint32_t x86sig; |
| 324 | struct cbfs_header *header, *result = NULL; |
| 325 | |
| 326 | // Try x86 style (check signature in bottom) header first. |
| 327 | x86sig = *(uint32_t *)(data + size - sizeof(uint32_t)); |
| 328 | offset = (x86sig + (uint32_t)size); |
| 329 | DEBUG("x86sig: 0x%x, offset: 0x%zx\n", x86sig, offset); |
| 330 | if (offset >= size - sizeof(*header) || |
| 331 | ntohl(((struct cbfs_header *)(data + offset))->magic) != |
| 332 | CBFS_HEADER_MAGIC) |
| 333 | offset = 0; |
| 334 | |
| 335 | for (; offset + sizeof(*header) < size; offset++) { |
| 336 | header = (struct cbfs_header *)(data + offset); |
| 337 | if (ntohl(header->magic) !=(CBFS_HEADER_MAGIC)) |
| 338 | continue; |
| 339 | if (ntohl(header->version) != CBFS_HEADER_VERSION1 && |
| 340 | ntohl(header->version) != CBFS_HEADER_VERSION2) { |
| 341 | // Probably not a real CBFS header? |
| 342 | continue; |
| 343 | } |
| 344 | found++; |
| 345 | result = header; |
| 346 | } |
| 347 | if (found > 1) { |
| 348 | ERROR("multiple (%d) CBFS headers found!\n", |
| 349 | found); |
| 350 | result = NULL; |
| 351 | } |
| 352 | return result; |
| 353 | } |
| 354 | |
| 355 | |
| 356 | struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image) { |
| 357 | assert(image && image->header); |
| 358 | return (struct cbfs_file *)(image->buffer.data + |
| 359 | ntohl(image->header->offset)); |
| 360 | } |
| 361 | |
| 362 | struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image, |
| 363 | struct cbfs_file *entry) { |
| 364 | uint32_t addr = cbfs_get_entry_addr(image, entry); |
| 365 | int align = ntohl(image->header->align); |
| 366 | assert(entry && cbfs_is_valid_entry(entry)); |
| 367 | addr += ntohl(entry->offset) + ntohl(entry->len); |
| 368 | addr = align_up(addr, align); |
| 369 | return (struct cbfs_file *)(image->buffer.data + addr); |
| 370 | } |
| 371 | |
| 372 | uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry) { |
| 373 | assert(image && image->buffer.data && entry); |
| 374 | return (int32_t)((char *)entry - image->buffer.data); |
| 375 | } |
| 376 | |
| 377 | int cbfs_is_valid_entry(struct cbfs_file *entry) { |
| 378 | return (entry &&memcmp(entry->magic, CBFS_FILE_MAGIC, |
| 379 | sizeof(entry->magic)) == 0); |
| 380 | } |
| 381 | |