blob: 2f38cec0b4015d6f117cf9b5a252e74c4e60dc1f [file] [log] [blame]
Hung-Te Lineab2c812013-01-29 01:56:17 +08001/*
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 Lin3bb035b2013-01-29 02:15:49 +080020#include <inttypes.h>
21#include <libgen.h>
Hung-Te Lineab2c812013-01-29 01:56:17 +080022#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
36static 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 Lin3bb035b2013-01-29 02:15:49 +080042/* Type and format */
43
44struct typedesc_t {
45 uint32_t type;
46 const char *name;
47};
48
49static 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
65static struct typedesc_t types_cbfs_compression[] = {
66 {CBFS_COMPRESS_NONE, "none"},
67 {CBFS_COMPRESS_LZMA, "LZMA"},
68 {0, NULL},
69};
70
71uint32_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
80const 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
89uint32_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
93const char *get_cbfs_entry_type_name(uint32_t type) {
94 return lookup_name_by_type(types_cbfs_entry, type, "(unknown)");
95}
96
97uint32_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 Linc03d9b02013-01-29 02:38:40 +0800101/* CBFS image */
102
103static 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 Lin49fcd752013-01-29 03:16:20 +0800108static 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 Lineab2c812013-01-29 01:56:17 +0800133int cbfs_image_from_file(struct cbfs_image *image, const char *filename) {
134 if (buffer_from_file(&image->buffer, filename) != 0)
135 return -1;
136 DEBUG("read_cbfs_image: %s (%zd bytes)\n", image->buffer.name,
137 image->buffer.size);
138 image->header = cbfs_find_header(image->buffer.data,
139 image->buffer.size);
140 if (!image->header) {
141 ERROR("%s does not have CBFS master header.\n", filename);
142 cbfs_image_delete(image);
143 return -1;
144 }
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800145 cbfs_fix_legacy_size(image);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800146
147 return 0;
148}
149
150int cbfs_image_write_file(struct cbfs_image *image, const char *filename) {
151 assert(image && image->buffer.data);
152 return buffer_write_file(&image->buffer, filename);
153}
154
155int cbfs_image_delete(struct cbfs_image *image) {
156 buffer_delete(&image->buffer);
157 image->header = NULL;
158 return 0;
159}
160
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800161struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name) {
162 struct cbfs_file *entry;
163 for (entry = cbfs_find_first_entry(image);
164 entry && cbfs_is_valid_entry(entry);
165 entry = cbfs_find_next_entry(image, entry)) {
166 if (strcasecmp(CBFS_NAME(entry), name) == 0) {
167 DEBUG("cbfs_get_entry: found %s\n", name);
168 return entry;
169 }
170 }
171 return NULL;
172}
173
174int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
175 const char *filename) {
176 struct cbfs_file *entry = cbfs_get_entry(image, entry_name);
177 struct buffer buffer;
178 if (!entry) {
179 ERROR("File not found: %s\n", entry_name);
180 return -1;
181 }
182 LOG("Found file %.30s at 0x%x, type %.12s, size %d\n",
183 entry_name, cbfs_get_entry_addr(image, entry),
184 get_cbfs_entry_type_name(ntohl(entry->type)), ntohl(entry->len));
185
186 if (ntohl(entry->type) != CBFS_COMPONENT_RAW) {
187 WARN("Only 'raw' files are safe to extract.\n");
188 }
189
190 buffer.data = CBFS_SUBHEADER(entry);
191 buffer.size = ntohl(entry->len);
192 buffer.name = "(cbfs_export_entry)";
193 if (buffer_write_file(&buffer, filename) != 0) {
194 ERROR("Failed to write %s into %s.\n",
195 entry_name, filename);
196 return -1;
197 }
198 INFO("Successfully dumped the file to: %s\n", filename);
199 return 0;
200}
201
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800202int cbfs_remove_entry(struct cbfs_image *image, const char *name) {
203 struct cbfs_file *entry, *next;
204 size_t len;
205 entry = cbfs_get_entry(image, name);
206 if (!entry) {
207 ERROR("CBFS file %s not found.\n", name);
208 return -1;
209 }
210 next = cbfs_find_next_entry(image, entry);
211 assert(next);
212 DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n",
213 CBFS_NAME(entry), cbfs_get_entry_addr(image, entry));
214 entry->type = htonl(CBFS_COMPONENT_DELETED);
215 len = (cbfs_get_entry_addr(image, next) -
216 cbfs_get_entry_addr(image, entry));
217 entry->offset = htonl(cbfs_calculate_file_header_size(""));
218 entry->len = htonl(len - ntohl(entry->offset));
219 memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry));
220 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE,
221 ntohl(entry->len));
222 return 0;
223}
224
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800225int cbfs_print_header_info(struct cbfs_image *image) {
226 char *name = strdup(image->buffer.name);
227 assert(image && image->header);
228 printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n"
229 "alignment: %d bytes\n\n",
230 basename(name),
231 image->buffer.size / 1024,
232 ntohl(image->header->bootblocksize),
233 ntohl(image->header->romsize),
234 ntohl(image->header->offset),
235 ntohl(image->header->align));
236 free(name);
237 return 0;
238}
239
240static int cbfs_print_stage_info(struct cbfs_stage *stage, FILE* fp) {
241 fprintf(fp,
242 " %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", "
243 "length: %d/%d\n",
244 lookup_name_by_type(types_cbfs_compression,
245 stage->compression, "(unknown)"),
246 stage->entry,
247 stage->load,
248 stage->len,
249 stage->memlen);
250 return 0;
251}
252
253static int cbfs_print_payload_segment_info(struct cbfs_payload_segment *payload,
254 FILE *fp)
255{
256 switch(payload->type) {
257 case PAYLOAD_SEGMENT_CODE:
258 case PAYLOAD_SEGMENT_DATA:
259 fprintf(fp, " %s (%s compression, offset: 0x%x, "
260 "load: 0x%" PRIx64 ", length: %d/%d)\n",
261 (payload->type == PAYLOAD_SEGMENT_CODE ?
262 "code " : "data"),
263 lookup_name_by_type(types_cbfs_compression,
264 ntohl(payload->compression),
265 "(unknown)"),
266 ntohl(payload->offset),
267 ntohll(payload->load_addr),
268 ntohl(payload->len), ntohl(payload->mem_len));
269 break;
270
271 case PAYLOAD_SEGMENT_ENTRY:
272 fprintf(fp, " entry (0x%" PRIx64 ")\n",
273 ntohll(payload->load_addr));
274 break;
275
276 case PAYLOAD_SEGMENT_BSS:
277 fprintf(fp, " BSS (address 0x%016" PRIx64 ", "
278 "length 0x%x)\n",
279 ntohll(payload->load_addr),
280 ntohl(payload->len));
281 break;
282
283 case PAYLOAD_SEGMENT_PARAMS:
284 fprintf(fp, " parameters\n");
285 break;
286
287 default:
288 fprintf(fp, " 0x%x (%s compression, offset: 0x%x, "
289 "load: 0x%" PRIx64 ", length: %d/%d\n",
290 payload->type,
291 lookup_name_by_type(types_cbfs_compression,
292 payload->compression,
293 "(unknown)"),
294 ntohl(payload->offset),
295 ntohll(payload->load_addr),
296 ntohl(payload->len),
297 ntohl(payload->mem_len));
298 break;
299 }
300 return 0;
301}
302
303int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
304 void *arg) {
305 const char *name = CBFS_NAME(entry);
306 struct cbfs_payload_segment *payload;
307 FILE *fp = (FILE *)arg;
308
309 if (!cbfs_is_valid_entry(entry)) {
310 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
311 cbfs_get_entry_addr(image, entry));
312 return -1;
313 }
314 if (!fp)
315 fp = stdout;
316
317 fprintf(fp, "%-30s 0x%-8x %-12s %d\n",
318 *name ? name : "(empty)",
319 cbfs_get_entry_addr(image, entry),
320 get_cbfs_entry_type_name(ntohl(entry->type)),
321 ntohl(entry->len));
322
323 if (!verbose)
324 return 0;
325
326 DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n",
327 cbfs_get_entry_addr(image, entry), ntohl(entry->offset),
328 cbfs_get_entry_addr(image, entry) + ntohl(entry->offset),
329 ntohl(entry->len));
330
331 /* note the components of the subheader may be in host order ... */
332 switch (ntohl(entry->type)) {
333 case CBFS_COMPONENT_STAGE:
334 cbfs_print_stage_info((struct cbfs_stage *)
335 CBFS_SUBHEADER(entry), fp);
336 break;
337
338 case CBFS_COMPONENT_PAYLOAD:
339 payload = (struct cbfs_payload_segment *)
340 CBFS_SUBHEADER(entry);
341 while (payload) {
342 cbfs_print_payload_segment_info(payload, fp);
343 if (payload->type == PAYLOAD_SEGMENT_ENTRY)
344 break;
345 else
346 payload ++;
347 }
348 break;
349 default:
350 break;
351 }
352 return 0;
353}
354
355int cbfs_print_directory(struct cbfs_image *image) {
356 cbfs_print_header_info(image);
357 printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
358 cbfs_walk(image, cbfs_print_entry_info, NULL);
359 return 0;
360}
361
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800362int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
363 void *arg) {
364 struct cbfs_file *next;
365 uint32_t type, addr, last_addr;
366
367 type = ntohl(entry->type);
368 if (type == CBFS_COMPONENT_DELETED) {
369 // Ready to be recycled.
370 type = CBFS_COMPONENT_NULL;
371 entry->type = htonl(type);
372 }
373 if (type != CBFS_COMPONENT_NULL)
374 return 0;
375
376 next = cbfs_find_next_entry(image, entry);
377
378 while (next && cbfs_is_valid_entry(next)) {
379 type = ntohl(next->type);
380 if (type == CBFS_COMPONENT_DELETED) {
381 type = CBFS_COMPONENT_NULL;
382 next->type = htonl(type);
383 }
384 if (type != CBFS_COMPONENT_NULL)
385 return 0;
386
387 addr = cbfs_get_entry_addr(image, entry);
388 last_addr = cbfs_get_entry_addr(
389 image, cbfs_find_next_entry(image, next));
390
391 // Now, we find two deleted/empty entries; try to merge now.
392 DEBUG("join_empty_entry: combine 0x%x+0x%x and 0x%x+0x%x.\n",
393 cbfs_get_entry_addr(image, entry), ntohl(entry->len),
394 cbfs_get_entry_addr(image, next), ntohl(next->len));
395 cbfs_create_empty_entry(image, entry,
396 (last_addr - addr -
397 cbfs_calculate_file_header_size("")),
398 "");
399 DEBUG("new empty entry: length=0x%x\n", ntohl(entry->len));
400 next = cbfs_find_next_entry(image, entry);
401 }
402 return 0;
403}
404
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800405int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback,
406 void *arg) {
407 int count = 0;
408 struct cbfs_file *entry;
409 for (entry = cbfs_find_first_entry(image);
410 entry && cbfs_is_valid_entry(entry);
411 entry = cbfs_find_next_entry(image, entry)) {
412 count ++;
413 if (callback(image, entry, arg) != 0)
414 break;
415 }
416 return count;
417}
418
Hung-Te Lineab2c812013-01-29 01:56:17 +0800419struct cbfs_header *cbfs_find_header(char *data, size_t size) {
420 size_t offset;
421 int found = 0;
422 uint32_t x86sig;
423 struct cbfs_header *header, *result = NULL;
424
425 // Try x86 style (check signature in bottom) header first.
426 x86sig = *(uint32_t *)(data + size - sizeof(uint32_t));
427 offset = (x86sig + (uint32_t)size);
428 DEBUG("x86sig: 0x%x, offset: 0x%zx\n", x86sig, offset);
429 if (offset >= size - sizeof(*header) ||
430 ntohl(((struct cbfs_header *)(data + offset))->magic) !=
431 CBFS_HEADER_MAGIC)
432 offset = 0;
433
434 for (; offset + sizeof(*header) < size; offset++) {
435 header = (struct cbfs_header *)(data + offset);
436 if (ntohl(header->magic) !=(CBFS_HEADER_MAGIC))
437 continue;
438 if (ntohl(header->version) != CBFS_HEADER_VERSION1 &&
439 ntohl(header->version) != CBFS_HEADER_VERSION2) {
440 // Probably not a real CBFS header?
441 continue;
442 }
443 found++;
444 result = header;
445 }
446 if (found > 1) {
447 ERROR("multiple (%d) CBFS headers found!\n",
448 found);
449 result = NULL;
450 }
451 return result;
452}
453
454
455struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image) {
456 assert(image && image->header);
457 return (struct cbfs_file *)(image->buffer.data +
458 ntohl(image->header->offset));
459}
460
461struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
462 struct cbfs_file *entry) {
463 uint32_t addr = cbfs_get_entry_addr(image, entry);
464 int align = ntohl(image->header->align);
465 assert(entry && cbfs_is_valid_entry(entry));
466 addr += ntohl(entry->offset) + ntohl(entry->len);
467 addr = align_up(addr, align);
468 return (struct cbfs_file *)(image->buffer.data + addr);
469}
470
471uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry) {
472 assert(image && image->buffer.data && entry);
473 return (int32_t)((char *)entry - image->buffer.data);
474}
475
476int cbfs_is_valid_entry(struct cbfs_file *entry) {
477 return (entry &&memcmp(entry->magic, CBFS_FILE_MAGIC,
478 sizeof(entry->magic)) == 0);
479}
480
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800481int cbfs_create_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
482 size_t len, const char *name) {
483 memset(entry, CBFS_CONTENT_DEFAULT_VALUE, sizeof(*entry));
484 memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
485 entry->type = htonl(CBFS_COMPONENT_NULL);
486 entry->len = htonl(len);
487 entry->checksum = 0; // TODO Build a checksum algorithm.
488 entry->offset = htonl(cbfs_calculate_file_header_size(name));
489 memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry));
490 strcpy(CBFS_NAME(entry), name);
491 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len);
492 return 0;
493}
494
495/* Finds a place to hold whole stage data in same memory page.
496 */
497static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page) {
498 if (!page)
499 return 1;
500 return (start / page) == (start + size - 1) / page;
501}
502
503int32_t cbfs_locate_entry(struct cbfs_image *image, const char *name,
504 uint32_t size, uint32_t page_size) {
505 struct cbfs_file *entry;
506 size_t need_len;
507 uint32_t addr, addr_next, addr2, addr3, header_len;
508 assert(size < page_size);
509
510 if (page_size % ntohl(image->header->align))
511 WARN("locate_entry: page does not align with CBFS image.\n");
512
513 /* TODO Old cbfstool always assume input is a stage file (and adding
514 * sizeof(cbfs_stage) for header. We should fix that by adding "-t"
515 * (type) param in future. For right now, follow old behavior. */
516 header_len = (cbfs_calculate_file_header_size(name) +
517 sizeof(struct cbfs_stage));
518 need_len = header_len + size;
519
520 // Merge empty entries to build get max available pages.
521 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
522
523 /* Three cases of content location on memory page:
524 * case 1.
525 * | PAGE 1 | PAGE 2 |
526 * | <header><content>| Fit. Return start of content.
527 *
528 * case 2.
529 * | PAGE 1 | PAGE 2 |
530 * | <header><content> | Fits when we shift content to align
531 * shift-> | <header>|<content> | at starting of PAGE 2.
532 *
533 * case 3. (large content filling whole page)
534 * | PAGE 1 | PAGE 2 | PAGE 3|
535 * | <header>< content > | | Can't fit. If we shift content to
536 * | { free space . } PAGE 2, header can't fit in free
537 * | shift-> <header><content> space, so we must use PAGE 3.
538 *
539 * The returned address will be used to re-link stage file, and then
540 * assigned to add-stage command (-b), which will be then re-calculated
541 * by ELF loader and positioned by cbfs_add_entry.
542 */
543 for (entry = cbfs_find_first_entry(image);
544 entry && cbfs_is_valid_entry(entry);
545 entry = cbfs_find_next_entry(image, entry)) {
546
547 uint32_t type = ntohl(entry->type);
548 if (type != CBFS_COMPONENT_NULL)
549 continue;
550
551 addr = cbfs_get_entry_addr(image, entry);
552 addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry(
553 image, entry));
554 if (addr_next - addr < need_len)
555 continue;
556 if (is_in_same_page(addr + header_len, size, page_size)) {
557 DEBUG("cbfs_locate_entry: FIT (PAGE1).");
558 return addr + header_len;
559 }
560
561 addr2 = align_up(addr, page_size);
562 if (addr2 < addr_next && addr_next - addr2 >= size &&
563 addr2 - addr >= header_len) {
564 DEBUG("cbfs_locate_entry: OVERLAP (PAGE2).");
565 return addr2;
566 }
567
568 addr3 = addr2 + page_size;
569 if (addr3 < addr_next && addr_next - addr3 >= size &&
570 addr3 - addr >= header_len) {
571 DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3).");
572 return addr3;
573 }
574 }
575 return -1;
576}