blob: b7c6ee5af8392027056562eaa70102bef37cc1bc [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 Linf56c73f2013-01-29 09:45:12 +0800133int 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 Lineab2c812013-01-29 01:56:17 +0800221int 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 Lin49fcd752013-01-29 03:16:20 +0800233 cbfs_fix_legacy_size(image);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800234
235 return 0;
236}
237
238int 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
243int cbfs_image_delete(struct cbfs_image *image) {
244 buffer_delete(&image->buffer);
245 image->header = NULL;
246 return 0;
247}
248
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800249/* Tries to add an entry with its data (CBFS_SUBHEADER) at given offset. */
250static int cbfs_add_entry_at(struct cbfs_image *image,
251 struct cbfs_file *entry,
252 uint32_t size,
253 const char *name,
254 uint32_t type,
255 const void *data,
256 uint32_t content_offset) {
257 struct cbfs_file *next = cbfs_find_next_entry(image, entry);
258 uint32_t addr = cbfs_get_entry_addr(image, entry),
259 addr_next = cbfs_get_entry_addr(image, next);
260 uint32_t header_size = cbfs_calculate_file_header_size(name),
261 min_entry_size = cbfs_calculate_file_header_size("");
262 uint32_t len, target;
263 uint32_t align = ntohl(image->header->align);
264
265 target = content_offset - header_size;
266 if (target % align)
267 target -= target % align;
268 if (target < addr) {
269 ERROR("No space to hold cbfs_file header.");
270 return -1;
271 }
272
273 // Process buffer BEFORE content_offset.
274 if (target - addr > min_entry_size) {
275 DEBUG("|min|...|header|content|... <create new entry>\n");
276 len = target - addr - min_entry_size;
277 cbfs_create_empty_entry(image, entry, len, "");
278 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
279 entry = cbfs_find_next_entry(image, entry);
280 addr = cbfs_get_entry_addr(image, entry);
281 }
282
283 len = size + (content_offset - addr - header_size);
284 cbfs_create_empty_entry(image, entry, len, name);
285 if (len != size) {
286 DEBUG("|..|header|content|... <use offset to create entry>\n");
287 DEBUG("before: offset=0x%x, len=0x%x\n",
288 ntohl(entry->offset), ntohl(entry->len));
289 // TODO reset expanded name buffer to 0xFF.
290 entry->offset = htonl(ntohl(entry->offset) + (len - size));
291 entry->len = htonl(size);
292 DEBUG("after: offset=0x%x, len=0x%x\n",
293 ntohl(entry->offset), ntohl(entry->len));
294 }
295
296 // Ready to fill data into entry.
297 assert(ntohl(entry->len) == size);
298 entry->type = htonl(type);
299 DEBUG("content_offset: 0x%x, entry location: %x\n",
300 content_offset, (int)((char*)CBFS_SUBHEADER(entry) -
301 image->buffer.data));
302 assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data ==
303 content_offset);
304 memcpy(CBFS_SUBHEADER(entry), data, size);
305 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
306
307 // Process buffer AFTER entry.
308 entry = cbfs_find_next_entry(image, entry);
309 addr = cbfs_get_entry_addr(image, entry);
310 assert(addr < addr_next);
311
312 if (addr_next - addr < min_entry_size) {
313 DEBUG("No space after content to keep CBFS structure.\n");
314 return -1;
315 }
316
317 len = addr_next - addr - min_entry_size;
318 cbfs_create_empty_entry(image, entry, len, "");
319 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
320 return 0;
321}
322
323int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
324 const char *name, uint32_t type, uint32_t content_offset) {
325 uint32_t entry_type;
326 uint32_t addr, addr_next;
327 struct cbfs_file *entry, *next;
328 uint32_t header_size, need_size, new_size;
329
330 header_size = cbfs_calculate_file_header_size(name);
331
332 need_size = header_size + buffer->size;
333 DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n",
334 name, content_offset, header_size, buffer->size, need_size);
335
336 if (IS_TOP_ALIGNED_ADDRESS(content_offset)) {
337 // legacy cbfstool takes top-aligned address.
338 uint32_t romsize = ntohl(image->header->romsize);
339 INFO("Converting top-aligned address 0x%x to offset: 0x%x\n",
340 content_offset, content_offset + romsize);
341 content_offset += romsize;
342 }
343
344 // Merge empty entries.
345 DEBUG("(trying to merge empty entries...)\n");
346 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
347
348 for (entry = cbfs_find_first_entry(image);
349 entry && cbfs_is_valid_entry(entry);
350 entry = cbfs_find_next_entry(image, entry)) {
351
352 entry_type = ntohl(entry->type);
353 if (entry_type != CBFS_COMPONENT_NULL)
354 continue;
355
356 addr = cbfs_get_entry_addr(image, entry);
357 next = cbfs_find_next_entry(image, entry);
358 addr_next = cbfs_get_entry_addr(image, next);
359
360 DEBUG("cbfs_add_entry: space at 0x%x+0x%x(%d) bytes\n",
361 addr, addr_next - addr, addr_next - addr);
362 if (addr + need_size > addr_next)
363 continue;
364
365 // Can we simply put object here?
366 if (!content_offset || content_offset == addr + header_size) {
367 DEBUG("Filling new entry data (%zd bytes).\n",
368 buffer->size);
369 cbfs_create_empty_entry(image, entry, buffer->size,
370 name);
371 entry->type = htonl(type);
372 memcpy(CBFS_SUBHEADER(entry), buffer->data, buffer->size);
373 if (verbose)
374 cbfs_print_entry_info(image, entry, stderr);
375
376 // setup new entry
377 DEBUG("Seting new empty entry.\n");
378 entry = cbfs_find_next_entry(image, entry);
379 new_size = (cbfs_get_entry_addr(image, next) -
380 cbfs_get_entry_addr(image, entry));
381 new_size -= cbfs_calculate_file_header_size("");
382 DEBUG("new size: %d\n", new_size);
383 cbfs_create_empty_entry(image, entry, new_size, "");
384 if (verbose)
385 cbfs_print_entry_info(image, entry, stderr);
386 return 0;
387 }
388
389 // We need to put content here, and the case is really
390 // complicated...
391 assert(content_offset);
392 if (addr_next < content_offset) {
393 DEBUG("Not for specified offset yet");
394 continue;
395 } else if (addr > content_offset) {
396 DEBUG("Exceed specified content_offset.");
397 break;
398 } else if (addr + header_size > content_offset) {
399 ERROR("Not enough space for header.\n");
400 break;
401 } else if (content_offset + buffer->size > addr_next) {
402 ERROR("Not enough space for content.\n");
403 break;
404 }
405
406 // TODO there are more few tricky cases that we may
407 // want to fit by altering offset.
408 DEBUG("section 0x%x+0x%x for content_offset 0x%x.\n",
409 addr, addr_next - addr, content_offset);
410
411 if (cbfs_add_entry_at(image, entry, buffer->size, name, type,
412 buffer->data, content_offset) == 0) {
413 return 0;
414 }
415 break;
416 }
417
418 ERROR("Could not add [%s, %zd bytes (%zd KB)@0x%x]; too big?\n",
419 buffer->name, buffer->size, buffer->size / 1024, content_offset);
420 return -1;
421}
422
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800423struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name) {
424 struct cbfs_file *entry;
425 for (entry = cbfs_find_first_entry(image);
426 entry && cbfs_is_valid_entry(entry);
427 entry = cbfs_find_next_entry(image, entry)) {
428 if (strcasecmp(CBFS_NAME(entry), name) == 0) {
429 DEBUG("cbfs_get_entry: found %s\n", name);
430 return entry;
431 }
432 }
433 return NULL;
434}
435
436int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
437 const char *filename) {
438 struct cbfs_file *entry = cbfs_get_entry(image, entry_name);
439 struct buffer buffer;
440 if (!entry) {
441 ERROR("File not found: %s\n", entry_name);
442 return -1;
443 }
444 LOG("Found file %.30s at 0x%x, type %.12s, size %d\n",
445 entry_name, cbfs_get_entry_addr(image, entry),
446 get_cbfs_entry_type_name(ntohl(entry->type)), ntohl(entry->len));
447
448 if (ntohl(entry->type) != CBFS_COMPONENT_RAW) {
449 WARN("Only 'raw' files are safe to extract.\n");
450 }
451
452 buffer.data = CBFS_SUBHEADER(entry);
453 buffer.size = ntohl(entry->len);
454 buffer.name = "(cbfs_export_entry)";
455 if (buffer_write_file(&buffer, filename) != 0) {
456 ERROR("Failed to write %s into %s.\n",
457 entry_name, filename);
458 return -1;
459 }
460 INFO("Successfully dumped the file to: %s\n", filename);
461 return 0;
462}
463
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800464int cbfs_remove_entry(struct cbfs_image *image, const char *name) {
465 struct cbfs_file *entry, *next;
466 size_t len;
467 entry = cbfs_get_entry(image, name);
468 if (!entry) {
469 ERROR("CBFS file %s not found.\n", name);
470 return -1;
471 }
472 next = cbfs_find_next_entry(image, entry);
473 assert(next);
474 DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n",
475 CBFS_NAME(entry), cbfs_get_entry_addr(image, entry));
476 entry->type = htonl(CBFS_COMPONENT_DELETED);
477 len = (cbfs_get_entry_addr(image, next) -
478 cbfs_get_entry_addr(image, entry));
479 entry->offset = htonl(cbfs_calculate_file_header_size(""));
480 entry->len = htonl(len - ntohl(entry->offset));
481 memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry));
482 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE,
483 ntohl(entry->len));
484 return 0;
485}
486
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800487int cbfs_print_header_info(struct cbfs_image *image) {
488 char *name = strdup(image->buffer.name);
489 assert(image && image->header);
490 printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n"
491 "alignment: %d bytes\n\n",
492 basename(name),
493 image->buffer.size / 1024,
494 ntohl(image->header->bootblocksize),
495 ntohl(image->header->romsize),
496 ntohl(image->header->offset),
497 ntohl(image->header->align));
498 free(name);
499 return 0;
500}
501
502static int cbfs_print_stage_info(struct cbfs_stage *stage, FILE* fp) {
503 fprintf(fp,
504 " %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", "
505 "length: %d/%d\n",
506 lookup_name_by_type(types_cbfs_compression,
507 stage->compression, "(unknown)"),
508 stage->entry,
509 stage->load,
510 stage->len,
511 stage->memlen);
512 return 0;
513}
514
515static int cbfs_print_payload_segment_info(struct cbfs_payload_segment *payload,
516 FILE *fp)
517{
518 switch(payload->type) {
519 case PAYLOAD_SEGMENT_CODE:
520 case PAYLOAD_SEGMENT_DATA:
521 fprintf(fp, " %s (%s compression, offset: 0x%x, "
522 "load: 0x%" PRIx64 ", length: %d/%d)\n",
523 (payload->type == PAYLOAD_SEGMENT_CODE ?
524 "code " : "data"),
525 lookup_name_by_type(types_cbfs_compression,
526 ntohl(payload->compression),
527 "(unknown)"),
528 ntohl(payload->offset),
529 ntohll(payload->load_addr),
530 ntohl(payload->len), ntohl(payload->mem_len));
531 break;
532
533 case PAYLOAD_SEGMENT_ENTRY:
534 fprintf(fp, " entry (0x%" PRIx64 ")\n",
535 ntohll(payload->load_addr));
536 break;
537
538 case PAYLOAD_SEGMENT_BSS:
539 fprintf(fp, " BSS (address 0x%016" PRIx64 ", "
540 "length 0x%x)\n",
541 ntohll(payload->load_addr),
542 ntohl(payload->len));
543 break;
544
545 case PAYLOAD_SEGMENT_PARAMS:
546 fprintf(fp, " parameters\n");
547 break;
548
549 default:
550 fprintf(fp, " 0x%x (%s compression, offset: 0x%x, "
551 "load: 0x%" PRIx64 ", length: %d/%d\n",
552 payload->type,
553 lookup_name_by_type(types_cbfs_compression,
554 payload->compression,
555 "(unknown)"),
556 ntohl(payload->offset),
557 ntohll(payload->load_addr),
558 ntohl(payload->len),
559 ntohl(payload->mem_len));
560 break;
561 }
562 return 0;
563}
564
565int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
566 void *arg) {
567 const char *name = CBFS_NAME(entry);
568 struct cbfs_payload_segment *payload;
569 FILE *fp = (FILE *)arg;
570
571 if (!cbfs_is_valid_entry(entry)) {
572 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
573 cbfs_get_entry_addr(image, entry));
574 return -1;
575 }
576 if (!fp)
577 fp = stdout;
578
579 fprintf(fp, "%-30s 0x%-8x %-12s %d\n",
580 *name ? name : "(empty)",
581 cbfs_get_entry_addr(image, entry),
582 get_cbfs_entry_type_name(ntohl(entry->type)),
583 ntohl(entry->len));
584
585 if (!verbose)
586 return 0;
587
588 DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n",
589 cbfs_get_entry_addr(image, entry), ntohl(entry->offset),
590 cbfs_get_entry_addr(image, entry) + ntohl(entry->offset),
591 ntohl(entry->len));
592
593 /* note the components of the subheader may be in host order ... */
594 switch (ntohl(entry->type)) {
595 case CBFS_COMPONENT_STAGE:
596 cbfs_print_stage_info((struct cbfs_stage *)
597 CBFS_SUBHEADER(entry), fp);
598 break;
599
600 case CBFS_COMPONENT_PAYLOAD:
601 payload = (struct cbfs_payload_segment *)
602 CBFS_SUBHEADER(entry);
603 while (payload) {
604 cbfs_print_payload_segment_info(payload, fp);
605 if (payload->type == PAYLOAD_SEGMENT_ENTRY)
606 break;
607 else
608 payload ++;
609 }
610 break;
611 default:
612 break;
613 }
614 return 0;
615}
616
617int cbfs_print_directory(struct cbfs_image *image) {
618 cbfs_print_header_info(image);
619 printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
620 cbfs_walk(image, cbfs_print_entry_info, NULL);
621 return 0;
622}
623
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800624int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
625 void *arg) {
626 struct cbfs_file *next;
627 uint32_t type, addr, last_addr;
628
629 type = ntohl(entry->type);
630 if (type == CBFS_COMPONENT_DELETED) {
631 // Ready to be recycled.
632 type = CBFS_COMPONENT_NULL;
633 entry->type = htonl(type);
634 }
635 if (type != CBFS_COMPONENT_NULL)
636 return 0;
637
638 next = cbfs_find_next_entry(image, entry);
639
640 while (next && cbfs_is_valid_entry(next)) {
641 type = ntohl(next->type);
642 if (type == CBFS_COMPONENT_DELETED) {
643 type = CBFS_COMPONENT_NULL;
644 next->type = htonl(type);
645 }
646 if (type != CBFS_COMPONENT_NULL)
647 return 0;
648
649 addr = cbfs_get_entry_addr(image, entry);
650 last_addr = cbfs_get_entry_addr(
651 image, cbfs_find_next_entry(image, next));
652
653 // Now, we find two deleted/empty entries; try to merge now.
654 DEBUG("join_empty_entry: combine 0x%x+0x%x and 0x%x+0x%x.\n",
655 cbfs_get_entry_addr(image, entry), ntohl(entry->len),
656 cbfs_get_entry_addr(image, next), ntohl(next->len));
657 cbfs_create_empty_entry(image, entry,
658 (last_addr - addr -
659 cbfs_calculate_file_header_size("")),
660 "");
661 DEBUG("new empty entry: length=0x%x\n", ntohl(entry->len));
662 next = cbfs_find_next_entry(image, entry);
663 }
664 return 0;
665}
666
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800667int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback,
668 void *arg) {
669 int count = 0;
670 struct cbfs_file *entry;
671 for (entry = cbfs_find_first_entry(image);
672 entry && cbfs_is_valid_entry(entry);
673 entry = cbfs_find_next_entry(image, entry)) {
674 count ++;
675 if (callback(image, entry, arg) != 0)
676 break;
677 }
678 return count;
679}
680
Hung-Te Lineab2c812013-01-29 01:56:17 +0800681struct cbfs_header *cbfs_find_header(char *data, size_t size) {
682 size_t offset;
683 int found = 0;
684 uint32_t x86sig;
685 struct cbfs_header *header, *result = NULL;
686
687 // Try x86 style (check signature in bottom) header first.
688 x86sig = *(uint32_t *)(data + size - sizeof(uint32_t));
689 offset = (x86sig + (uint32_t)size);
690 DEBUG("x86sig: 0x%x, offset: 0x%zx\n", x86sig, offset);
691 if (offset >= size - sizeof(*header) ||
692 ntohl(((struct cbfs_header *)(data + offset))->magic) !=
693 CBFS_HEADER_MAGIC)
694 offset = 0;
695
696 for (; offset + sizeof(*header) < size; offset++) {
697 header = (struct cbfs_header *)(data + offset);
698 if (ntohl(header->magic) !=(CBFS_HEADER_MAGIC))
699 continue;
700 if (ntohl(header->version) != CBFS_HEADER_VERSION1 &&
701 ntohl(header->version) != CBFS_HEADER_VERSION2) {
702 // Probably not a real CBFS header?
703 continue;
704 }
705 found++;
706 result = header;
707 }
708 if (found > 1) {
709 ERROR("multiple (%d) CBFS headers found!\n",
710 found);
711 result = NULL;
712 }
713 return result;
714}
715
716
717struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image) {
718 assert(image && image->header);
719 return (struct cbfs_file *)(image->buffer.data +
720 ntohl(image->header->offset));
721}
722
723struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
724 struct cbfs_file *entry) {
725 uint32_t addr = cbfs_get_entry_addr(image, entry);
726 int align = ntohl(image->header->align);
727 assert(entry && cbfs_is_valid_entry(entry));
728 addr += ntohl(entry->offset) + ntohl(entry->len);
729 addr = align_up(addr, align);
730 return (struct cbfs_file *)(image->buffer.data + addr);
731}
732
733uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry) {
734 assert(image && image->buffer.data && entry);
735 return (int32_t)((char *)entry - image->buffer.data);
736}
737
738int cbfs_is_valid_entry(struct cbfs_file *entry) {
739 return (entry &&memcmp(entry->magic, CBFS_FILE_MAGIC,
740 sizeof(entry->magic)) == 0);
741}
742
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800743int cbfs_init_entry(struct cbfs_file *entry,
744 struct buffer *buffer) {
745 memset(entry, 0, sizeof(*entry));
746 memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
747 entry->len = htonl(buffer->size);
748 entry->offset = htonl(sizeof(*entry) + strlen(buffer->name) + 1);
749 return 0;
750}
751
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800752int cbfs_create_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
753 size_t len, const char *name) {
754 memset(entry, CBFS_CONTENT_DEFAULT_VALUE, sizeof(*entry));
755 memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
756 entry->type = htonl(CBFS_COMPONENT_NULL);
757 entry->len = htonl(len);
758 entry->checksum = 0; // TODO Build a checksum algorithm.
759 entry->offset = htonl(cbfs_calculate_file_header_size(name));
760 memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry));
761 strcpy(CBFS_NAME(entry), name);
762 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len);
763 return 0;
764}
765
766/* Finds a place to hold whole stage data in same memory page.
767 */
768static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page) {
769 if (!page)
770 return 1;
771 return (start / page) == (start + size - 1) / page;
772}
773
774int32_t cbfs_locate_entry(struct cbfs_image *image, const char *name,
775 uint32_t size, uint32_t page_size) {
776 struct cbfs_file *entry;
777 size_t need_len;
778 uint32_t addr, addr_next, addr2, addr3, header_len;
779 assert(size < page_size);
780
781 if (page_size % ntohl(image->header->align))
782 WARN("locate_entry: page does not align with CBFS image.\n");
783
784 /* TODO Old cbfstool always assume input is a stage file (and adding
785 * sizeof(cbfs_stage) for header. We should fix that by adding "-t"
786 * (type) param in future. For right now, follow old behavior. */
787 header_len = (cbfs_calculate_file_header_size(name) +
788 sizeof(struct cbfs_stage));
789 need_len = header_len + size;
790
791 // Merge empty entries to build get max available pages.
792 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
793
794 /* Three cases of content location on memory page:
795 * case 1.
796 * | PAGE 1 | PAGE 2 |
797 * | <header><content>| Fit. Return start of content.
798 *
799 * case 2.
800 * | PAGE 1 | PAGE 2 |
801 * | <header><content> | Fits when we shift content to align
802 * shift-> | <header>|<content> | at starting of PAGE 2.
803 *
804 * case 3. (large content filling whole page)
805 * | PAGE 1 | PAGE 2 | PAGE 3|
806 * | <header>< content > | | Can't fit. If we shift content to
807 * | { free space . } PAGE 2, header can't fit in free
808 * | shift-> <header><content> space, so we must use PAGE 3.
809 *
810 * The returned address will be used to re-link stage file, and then
811 * assigned to add-stage command (-b), which will be then re-calculated
812 * by ELF loader and positioned by cbfs_add_entry.
813 */
814 for (entry = cbfs_find_first_entry(image);
815 entry && cbfs_is_valid_entry(entry);
816 entry = cbfs_find_next_entry(image, entry)) {
817
818 uint32_t type = ntohl(entry->type);
819 if (type != CBFS_COMPONENT_NULL)
820 continue;
821
822 addr = cbfs_get_entry_addr(image, entry);
823 addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry(
824 image, entry));
825 if (addr_next - addr < need_len)
826 continue;
827 if (is_in_same_page(addr + header_len, size, page_size)) {
828 DEBUG("cbfs_locate_entry: FIT (PAGE1).");
829 return addr + header_len;
830 }
831
832 addr2 = align_up(addr, page_size);
833 if (addr2 < addr_next && addr_next - addr2 >= size &&
834 addr2 - addr >= header_len) {
835 DEBUG("cbfs_locate_entry: OVERLAP (PAGE2).");
836 return addr2;
837 }
838
839 addr3 = addr2 + page_size;
840 if (addr3 < addr_next && addr_next - addr3 >= size &&
841 addr3 - addr >= header_len) {
842 DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3).");
843 return addr3;
844 }
845 }
846 return -1;
847}