blob: bd81542cc7c910dde4be4a7fc7fd09c2e4e48e30 [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);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800115 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800116 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;
Hung-Te Linb02c8732013-03-15 17:40:08 +0800145 size_t entry_header_len;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800146
147 DEBUG("cbfs_image_create: bootblock=0x%x+0x%zx, "
148 "header=0x%x+0x%zx, entries_offset=0x%x\n",
149 bootblock_offset, bootblock->size,
150 header_offset, sizeof(*header), entries_offset);
151
152 if (buffer_create(&image->buffer, size, "(new)") != 0)
153 return -1;
154 image->header = NULL;
155 memset(image->buffer.data, CBFS_CONTENT_DEFAULT_VALUE, size);
156
157 // Adjust legcay top-aligned address to ROM offset.
158 if (IS_TOP_ALIGNED_ADDRESS(entries_offset))
159 entries_offset += (int32_t)size;
160 if (IS_TOP_ALIGNED_ADDRESS(bootblock_offset))
161 bootblock_offset += (int32_t)size;
162 if (IS_TOP_ALIGNED_ADDRESS(header_offset))
163 header_offset += (int32_t) size;
164
165 DEBUG("cbfs_create_image: (real offset) bootblock=0x%x, "
166 "header=0x%x, entries_offset=0x%x\n",
167 bootblock_offset, header_offset, entries_offset);
168
169 if (align == 0)
170 align = 64; // default align size.
171
172 // Prepare bootblock
173 if (bootblock_offset + bootblock->size > size) {
174 ERROR("Bootblock (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
175 bootblock_offset, bootblock->size, size);
176 return -1;
177 }
Hung-Te Linc5ff6482013-02-06 12:41:49 +0800178 if (entries_offset > bootblock_offset &&
179 entries_offset < bootblock->size) {
180 ERROR("Bootblock (0x%x+0x%zx) overlap CBFS data (0x%x)\n",
181 bootblock_offset, bootblock->size, entries_offset);
182 return -1;
183 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800184 memcpy(image->buffer.data + bootblock_offset, bootblock->data,
185 bootblock->size);
186
187 // Prepare header
188 if (header_offset + sizeof(*header) > size) {
189 ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
190 header_offset, sizeof(*header), size);
191 return -1;
192 }
193 header = (struct cbfs_header *)(image->buffer.data + header_offset);
194 image->header = header;
195 header->magic = htonl(CBFS_HEADER_MAGIC);
196 header->version = htonl(CBFS_HEADER_VERSION);
197 header->romsize = htonl(size);
198 header->bootblocksize = htonl(bootblock->size);
199 header->align = htonl(align);
200 header->offset = htonl(entries_offset);
201 header->architecture = htonl(arch);
202
203 // Prepare entries
204 if (align_up(entries_offset, align) != entries_offset) {
205 ERROR("Offset (0x%x) must be aligned to 0x%x.\n",
206 entries_offset, align);
207 return -1;
208 }
Hung-Te Linb02c8732013-03-15 17:40:08 +0800209 entry_header_len = cbfs_calculate_file_header_size("");
210 if (entries_offset + entry_header_len > size) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800211 ERROR("Offset (0x%x+0x%zx) exceed ROM size(0x%zx)\n",
Hung-Te Linb02c8732013-03-15 17:40:08 +0800212 entries_offset, entry_header_len, size);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800213 return -1;
214 }
215 entry = (struct cbfs_file *)(image->buffer.data + entries_offset);
216 // To calculate available length, find
217 // e = min(bootblock, header, size) where e > entries_offset.
218 cbfs_len = size;
219 if (bootblock_offset > entries_offset && bootblock_offset < cbfs_len)
220 cbfs_len = bootblock_offset;
221 if (header_offset > entries_offset && header_offset < cbfs_len)
222 cbfs_len = header_offset;
Hung-Te Linb02c8732013-03-15 17:40:08 +0800223 cbfs_len -= entries_offset + align + entry_header_len;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800224 cbfs_create_empty_entry(image, entry, cbfs_len, "");
225 LOG("Created CBFS image (capacity = %d bytes)\n", cbfs_len);
226 return 0;
227}
228
Hung-Te Lineab2c812013-01-29 01:56:17 +0800229int cbfs_image_from_file(struct cbfs_image *image, const char *filename) {
230 if (buffer_from_file(&image->buffer, filename) != 0)
231 return -1;
232 DEBUG("read_cbfs_image: %s (%zd bytes)\n", image->buffer.name,
233 image->buffer.size);
234 image->header = cbfs_find_header(image->buffer.data,
235 image->buffer.size);
236 if (!image->header) {
237 ERROR("%s does not have CBFS master header.\n", filename);
238 cbfs_image_delete(image);
239 return -1;
240 }
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800241 cbfs_fix_legacy_size(image);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800242
243 return 0;
244}
245
246int cbfs_image_write_file(struct cbfs_image *image, const char *filename) {
247 assert(image && image->buffer.data);
248 return buffer_write_file(&image->buffer, filename);
249}
250
251int cbfs_image_delete(struct cbfs_image *image) {
252 buffer_delete(&image->buffer);
253 image->header = NULL;
254 return 0;
255}
256
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800257/* Tries to add an entry with its data (CBFS_SUBHEADER) at given offset. */
258static int cbfs_add_entry_at(struct cbfs_image *image,
259 struct cbfs_file *entry,
260 uint32_t size,
261 const char *name,
262 uint32_t type,
263 const void *data,
264 uint32_t content_offset) {
265 struct cbfs_file *next = cbfs_find_next_entry(image, entry);
266 uint32_t addr = cbfs_get_entry_addr(image, entry),
267 addr_next = cbfs_get_entry_addr(image, next);
268 uint32_t header_size = cbfs_calculate_file_header_size(name),
269 min_entry_size = cbfs_calculate_file_header_size("");
270 uint32_t len, target;
271 uint32_t align = ntohl(image->header->align);
272
273 target = content_offset - header_size;
274 if (target % align)
275 target -= target % align;
276 if (target < addr) {
277 ERROR("No space to hold cbfs_file header.");
278 return -1;
279 }
280
281 // Process buffer BEFORE content_offset.
282 if (target - addr > min_entry_size) {
283 DEBUG("|min|...|header|content|... <create new entry>\n");
284 len = target - addr - min_entry_size;
285 cbfs_create_empty_entry(image, entry, len, "");
286 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
287 entry = cbfs_find_next_entry(image, entry);
288 addr = cbfs_get_entry_addr(image, entry);
289 }
290
291 len = size + (content_offset - addr - header_size);
292 cbfs_create_empty_entry(image, entry, len, name);
293 if (len != size) {
294 DEBUG("|..|header|content|... <use offset to create entry>\n");
295 DEBUG("before: offset=0x%x, len=0x%x\n",
296 ntohl(entry->offset), ntohl(entry->len));
297 // TODO reset expanded name buffer to 0xFF.
298 entry->offset = htonl(ntohl(entry->offset) + (len - size));
299 entry->len = htonl(size);
300 DEBUG("after: offset=0x%x, len=0x%x\n",
301 ntohl(entry->offset), ntohl(entry->len));
302 }
303
304 // Ready to fill data into entry.
305 assert(ntohl(entry->len) == size);
306 entry->type = htonl(type);
307 DEBUG("content_offset: 0x%x, entry location: %x\n",
308 content_offset, (int)((char*)CBFS_SUBHEADER(entry) -
309 image->buffer.data));
310 assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data ==
311 content_offset);
312 memcpy(CBFS_SUBHEADER(entry), data, size);
313 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
314
315 // Process buffer AFTER entry.
316 entry = cbfs_find_next_entry(image, entry);
317 addr = cbfs_get_entry_addr(image, entry);
318 assert(addr < addr_next);
319
320 if (addr_next - addr < min_entry_size) {
321 DEBUG("No space after content to keep CBFS structure.\n");
322 return -1;
323 }
324
325 len = addr_next - addr - min_entry_size;
326 cbfs_create_empty_entry(image, entry, len, "");
327 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
328 return 0;
329}
330
331int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
332 const char *name, uint32_t type, uint32_t content_offset) {
333 uint32_t entry_type;
334 uint32_t addr, addr_next;
335 struct cbfs_file *entry, *next;
336 uint32_t header_size, need_size, new_size;
337
338 header_size = cbfs_calculate_file_header_size(name);
339
340 need_size = header_size + buffer->size;
341 DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n",
342 name, content_offset, header_size, buffer->size, need_size);
343
344 if (IS_TOP_ALIGNED_ADDRESS(content_offset)) {
345 // legacy cbfstool takes top-aligned address.
346 uint32_t romsize = ntohl(image->header->romsize);
347 INFO("Converting top-aligned address 0x%x to offset: 0x%x\n",
348 content_offset, content_offset + romsize);
349 content_offset += romsize;
350 }
351
352 // Merge empty entries.
353 DEBUG("(trying to merge empty entries...)\n");
354 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
355
356 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800357 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800358 entry = cbfs_find_next_entry(image, entry)) {
359
360 entry_type = ntohl(entry->type);
361 if (entry_type != CBFS_COMPONENT_NULL)
362 continue;
363
364 addr = cbfs_get_entry_addr(image, entry);
365 next = cbfs_find_next_entry(image, entry);
366 addr_next = cbfs_get_entry_addr(image, next);
367
368 DEBUG("cbfs_add_entry: space at 0x%x+0x%x(%d) bytes\n",
369 addr, addr_next - addr, addr_next - addr);
370 if (addr + need_size > addr_next)
371 continue;
372
373 // Can we simply put object here?
374 if (!content_offset || content_offset == addr + header_size) {
375 DEBUG("Filling new entry data (%zd bytes).\n",
376 buffer->size);
377 cbfs_create_empty_entry(image, entry, buffer->size,
378 name);
379 entry->type = htonl(type);
380 memcpy(CBFS_SUBHEADER(entry), buffer->data, buffer->size);
381 if (verbose)
382 cbfs_print_entry_info(image, entry, stderr);
383
384 // setup new entry
385 DEBUG("Seting new empty entry.\n");
386 entry = cbfs_find_next_entry(image, entry);
387 new_size = (cbfs_get_entry_addr(image, next) -
388 cbfs_get_entry_addr(image, entry));
389 new_size -= cbfs_calculate_file_header_size("");
390 DEBUG("new size: %d\n", new_size);
391 cbfs_create_empty_entry(image, entry, new_size, "");
392 if (verbose)
393 cbfs_print_entry_info(image, entry, stderr);
394 return 0;
395 }
396
397 // We need to put content here, and the case is really
398 // complicated...
399 assert(content_offset);
400 if (addr_next < content_offset) {
401 DEBUG("Not for specified offset yet");
402 continue;
403 } else if (addr > content_offset) {
404 DEBUG("Exceed specified content_offset.");
405 break;
406 } else if (addr + header_size > content_offset) {
407 ERROR("Not enough space for header.\n");
408 break;
409 } else if (content_offset + buffer->size > addr_next) {
410 ERROR("Not enough space for content.\n");
411 break;
412 }
413
414 // TODO there are more few tricky cases that we may
415 // want to fit by altering offset.
416 DEBUG("section 0x%x+0x%x for content_offset 0x%x.\n",
417 addr, addr_next - addr, content_offset);
418
419 if (cbfs_add_entry_at(image, entry, buffer->size, name, type,
420 buffer->data, content_offset) == 0) {
421 return 0;
422 }
423 break;
424 }
425
426 ERROR("Could not add [%s, %zd bytes (%zd KB)@0x%x]; too big?\n",
427 buffer->name, buffer->size, buffer->size / 1024, content_offset);
428 return -1;
429}
430
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800431struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name) {
432 struct cbfs_file *entry;
433 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800434 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800435 entry = cbfs_find_next_entry(image, entry)) {
436 if (strcasecmp(CBFS_NAME(entry), name) == 0) {
437 DEBUG("cbfs_get_entry: found %s\n", name);
438 return entry;
439 }
440 }
441 return NULL;
442}
443
444int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
445 const char *filename) {
446 struct cbfs_file *entry = cbfs_get_entry(image, entry_name);
447 struct buffer buffer;
448 if (!entry) {
449 ERROR("File not found: %s\n", entry_name);
450 return -1;
451 }
452 LOG("Found file %.30s at 0x%x, type %.12s, size %d\n",
453 entry_name, cbfs_get_entry_addr(image, entry),
454 get_cbfs_entry_type_name(ntohl(entry->type)), ntohl(entry->len));
455
456 if (ntohl(entry->type) != CBFS_COMPONENT_RAW) {
457 WARN("Only 'raw' files are safe to extract.\n");
458 }
459
460 buffer.data = CBFS_SUBHEADER(entry);
461 buffer.size = ntohl(entry->len);
462 buffer.name = "(cbfs_export_entry)";
463 if (buffer_write_file(&buffer, filename) != 0) {
464 ERROR("Failed to write %s into %s.\n",
465 entry_name, filename);
466 return -1;
467 }
468 INFO("Successfully dumped the file to: %s\n", filename);
469 return 0;
470}
471
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800472int cbfs_remove_entry(struct cbfs_image *image, const char *name) {
473 struct cbfs_file *entry, *next;
474 size_t len;
475 entry = cbfs_get_entry(image, name);
476 if (!entry) {
477 ERROR("CBFS file %s not found.\n", name);
478 return -1;
479 }
480 next = cbfs_find_next_entry(image, entry);
481 assert(next);
482 DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n",
483 CBFS_NAME(entry), cbfs_get_entry_addr(image, entry));
484 entry->type = htonl(CBFS_COMPONENT_DELETED);
485 len = (cbfs_get_entry_addr(image, next) -
486 cbfs_get_entry_addr(image, entry));
487 entry->offset = htonl(cbfs_calculate_file_header_size(""));
488 entry->len = htonl(len - ntohl(entry->offset));
489 memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry));
490 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE,
491 ntohl(entry->len));
492 return 0;
493}
494
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800495int cbfs_print_header_info(struct cbfs_image *image) {
496 char *name = strdup(image->buffer.name);
497 assert(image && image->header);
498 printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n"
499 "alignment: %d bytes\n\n",
500 basename(name),
501 image->buffer.size / 1024,
502 ntohl(image->header->bootblocksize),
503 ntohl(image->header->romsize),
504 ntohl(image->header->offset),
505 ntohl(image->header->align));
506 free(name);
507 return 0;
508}
509
510static int cbfs_print_stage_info(struct cbfs_stage *stage, FILE* fp) {
511 fprintf(fp,
512 " %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", "
513 "length: %d/%d\n",
514 lookup_name_by_type(types_cbfs_compression,
515 stage->compression, "(unknown)"),
516 stage->entry,
517 stage->load,
518 stage->len,
519 stage->memlen);
520 return 0;
521}
522
523static int cbfs_print_payload_segment_info(struct cbfs_payload_segment *payload,
524 FILE *fp)
525{
526 switch(payload->type) {
527 case PAYLOAD_SEGMENT_CODE:
528 case PAYLOAD_SEGMENT_DATA:
529 fprintf(fp, " %s (%s compression, offset: 0x%x, "
530 "load: 0x%" PRIx64 ", length: %d/%d)\n",
531 (payload->type == PAYLOAD_SEGMENT_CODE ?
532 "code " : "data"),
533 lookup_name_by_type(types_cbfs_compression,
534 ntohl(payload->compression),
535 "(unknown)"),
536 ntohl(payload->offset),
537 ntohll(payload->load_addr),
538 ntohl(payload->len), ntohl(payload->mem_len));
539 break;
540
541 case PAYLOAD_SEGMENT_ENTRY:
542 fprintf(fp, " entry (0x%" PRIx64 ")\n",
543 ntohll(payload->load_addr));
544 break;
545
546 case PAYLOAD_SEGMENT_BSS:
547 fprintf(fp, " BSS (address 0x%016" PRIx64 ", "
548 "length 0x%x)\n",
549 ntohll(payload->load_addr),
550 ntohl(payload->len));
551 break;
552
553 case PAYLOAD_SEGMENT_PARAMS:
554 fprintf(fp, " parameters\n");
555 break;
556
557 default:
558 fprintf(fp, " 0x%x (%s compression, offset: 0x%x, "
559 "load: 0x%" PRIx64 ", length: %d/%d\n",
560 payload->type,
561 lookup_name_by_type(types_cbfs_compression,
562 payload->compression,
563 "(unknown)"),
564 ntohl(payload->offset),
565 ntohll(payload->load_addr),
566 ntohl(payload->len),
567 ntohl(payload->mem_len));
568 break;
569 }
570 return 0;
571}
572
573int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
574 void *arg) {
575 const char *name = CBFS_NAME(entry);
576 struct cbfs_payload_segment *payload;
577 FILE *fp = (FILE *)arg;
578
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800579 if (!cbfs_is_valid_entry(image, entry)) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800580 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
581 cbfs_get_entry_addr(image, entry));
582 return -1;
583 }
584 if (!fp)
585 fp = stdout;
586
587 fprintf(fp, "%-30s 0x%-8x %-12s %d\n",
588 *name ? name : "(empty)",
589 cbfs_get_entry_addr(image, entry),
590 get_cbfs_entry_type_name(ntohl(entry->type)),
591 ntohl(entry->len));
592
593 if (!verbose)
594 return 0;
595
596 DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n",
597 cbfs_get_entry_addr(image, entry), ntohl(entry->offset),
598 cbfs_get_entry_addr(image, entry) + ntohl(entry->offset),
599 ntohl(entry->len));
600
601 /* note the components of the subheader may be in host order ... */
602 switch (ntohl(entry->type)) {
603 case CBFS_COMPONENT_STAGE:
604 cbfs_print_stage_info((struct cbfs_stage *)
605 CBFS_SUBHEADER(entry), fp);
606 break;
607
608 case CBFS_COMPONENT_PAYLOAD:
609 payload = (struct cbfs_payload_segment *)
610 CBFS_SUBHEADER(entry);
611 while (payload) {
612 cbfs_print_payload_segment_info(payload, fp);
613 if (payload->type == PAYLOAD_SEGMENT_ENTRY)
614 break;
615 else
616 payload ++;
617 }
618 break;
619 default:
620 break;
621 }
622 return 0;
623}
624
625int cbfs_print_directory(struct cbfs_image *image) {
626 cbfs_print_header_info(image);
627 printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
628 cbfs_walk(image, cbfs_print_entry_info, NULL);
629 return 0;
630}
631
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800632int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
633 void *arg) {
634 struct cbfs_file *next;
635 uint32_t type, addr, last_addr;
636
637 type = ntohl(entry->type);
638 if (type == CBFS_COMPONENT_DELETED) {
639 // Ready to be recycled.
640 type = CBFS_COMPONENT_NULL;
641 entry->type = htonl(type);
642 }
643 if (type != CBFS_COMPONENT_NULL)
644 return 0;
645
646 next = cbfs_find_next_entry(image, entry);
647
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800648 while (next && cbfs_is_valid_entry(image, next)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800649 type = ntohl(next->type);
650 if (type == CBFS_COMPONENT_DELETED) {
651 type = CBFS_COMPONENT_NULL;
652 next->type = htonl(type);
653 }
654 if (type != CBFS_COMPONENT_NULL)
655 return 0;
656
657 addr = cbfs_get_entry_addr(image, entry);
658 last_addr = cbfs_get_entry_addr(
659 image, cbfs_find_next_entry(image, next));
660
661 // Now, we find two deleted/empty entries; try to merge now.
662 DEBUG("join_empty_entry: combine 0x%x+0x%x and 0x%x+0x%x.\n",
663 cbfs_get_entry_addr(image, entry), ntohl(entry->len),
664 cbfs_get_entry_addr(image, next), ntohl(next->len));
665 cbfs_create_empty_entry(image, entry,
666 (last_addr - addr -
667 cbfs_calculate_file_header_size("")),
668 "");
669 DEBUG("new empty entry: length=0x%x\n", ntohl(entry->len));
670 next = cbfs_find_next_entry(image, entry);
671 }
672 return 0;
673}
674
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800675int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback,
676 void *arg) {
677 int count = 0;
678 struct cbfs_file *entry;
679 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800680 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800681 entry = cbfs_find_next_entry(image, entry)) {
682 count ++;
683 if (callback(image, entry, arg) != 0)
684 break;
685 }
686 return count;
687}
688
Hung-Te Lineab2c812013-01-29 01:56:17 +0800689struct cbfs_header *cbfs_find_header(char *data, size_t size) {
690 size_t offset;
691 int found = 0;
692 uint32_t x86sig;
693 struct cbfs_header *header, *result = NULL;
694
695 // Try x86 style (check signature in bottom) header first.
696 x86sig = *(uint32_t *)(data + size - sizeof(uint32_t));
697 offset = (x86sig + (uint32_t)size);
698 DEBUG("x86sig: 0x%x, offset: 0x%zx\n", x86sig, offset);
699 if (offset >= size - sizeof(*header) ||
700 ntohl(((struct cbfs_header *)(data + offset))->magic) !=
701 CBFS_HEADER_MAGIC)
702 offset = 0;
703
704 for (; offset + sizeof(*header) < size; offset++) {
705 header = (struct cbfs_header *)(data + offset);
706 if (ntohl(header->magic) !=(CBFS_HEADER_MAGIC))
707 continue;
708 if (ntohl(header->version) != CBFS_HEADER_VERSION1 &&
709 ntohl(header->version) != CBFS_HEADER_VERSION2) {
710 // Probably not a real CBFS header?
711 continue;
712 }
713 found++;
714 result = header;
715 }
716 if (found > 1) {
717 ERROR("multiple (%d) CBFS headers found!\n",
718 found);
719 result = NULL;
720 }
721 return result;
722}
723
724
725struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image) {
726 assert(image && image->header);
727 return (struct cbfs_file *)(image->buffer.data +
728 ntohl(image->header->offset));
729}
730
731struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
732 struct cbfs_file *entry) {
733 uint32_t addr = cbfs_get_entry_addr(image, entry);
734 int align = ntohl(image->header->align);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800735 assert(entry && cbfs_is_valid_entry(image, entry));
Hung-Te Lineab2c812013-01-29 01:56:17 +0800736 addr += ntohl(entry->offset) + ntohl(entry->len);
737 addr = align_up(addr, align);
738 return (struct cbfs_file *)(image->buffer.data + addr);
739}
740
741uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry) {
742 assert(image && image->buffer.data && entry);
743 return (int32_t)((char *)entry - image->buffer.data);
744}
745
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800746int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry) {
747 return (entry &&
748 (char *)entry >= image->buffer.data &&
749 (char *)entry + sizeof(entry->magic) <
750 image->buffer.data + image->buffer.size &&
751 memcmp(entry->magic, CBFS_FILE_MAGIC,
752 sizeof(entry->magic)) == 0);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800753}
754
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800755int cbfs_init_entry(struct cbfs_file *entry,
756 struct buffer *buffer) {
757 memset(entry, 0, sizeof(*entry));
758 memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
759 entry->len = htonl(buffer->size);
760 entry->offset = htonl(sizeof(*entry) + strlen(buffer->name) + 1);
761 return 0;
762}
763
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800764int cbfs_create_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
765 size_t len, const char *name) {
766 memset(entry, CBFS_CONTENT_DEFAULT_VALUE, sizeof(*entry));
767 memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
768 entry->type = htonl(CBFS_COMPONENT_NULL);
769 entry->len = htonl(len);
770 entry->checksum = 0; // TODO Build a checksum algorithm.
771 entry->offset = htonl(cbfs_calculate_file_header_size(name));
772 memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry));
773 strcpy(CBFS_NAME(entry), name);
774 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len);
775 return 0;
776}
777
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800778/* Finds a place to hold whole data in same memory page.
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800779 */
780static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page) {
781 if (!page)
782 return 1;
783 return (start / page) == (start + size - 1) / page;
784}
785
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800786/* Tests if data can fit in a range by given offset:
787 * start ->| header_len | offset (+ size) |<- end
788 */
789static int is_in_range(uint32_t start, uint32_t end, uint32_t header_len,
790 uint32_t offset, uint32_t size) {
791 return (offset >= start + header_len && offset + size <= end);
792}
793
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800794int32_t cbfs_locate_entry(struct cbfs_image *image, const char *name,
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800795 uint32_t size, uint32_t page_size, uint32_t align) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800796 struct cbfs_file *entry;
797 size_t need_len;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800798 uint32_t addr, addr_next, addr2, addr3, offset, header_len;
799
800 /* Default values: allow fitting anywhere in ROM. */
801 if (!page_size)
802 page_size = ntohl(image->header->romsize);
803 if (!align)
804 align = 1;
805
806 if (size > page_size)
807 ERROR("Input file size (%d) greater than page size (%d).\n",
808 size, page_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800809
810 if (page_size % ntohl(image->header->align))
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800811 WARN("%s: Page size (%#x) not aligned with CBFS image (%#x).\n",
812 __func__, page_size, ntohl(image->header->align));
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800813
814 /* TODO Old cbfstool always assume input is a stage file (and adding
815 * sizeof(cbfs_stage) for header. We should fix that by adding "-t"
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800816 * (type) param in future. For right now, we assume cbfs_stage is the
817 * largest structure and add it into header size. */
818 assert(sizeof(struct cbfs_stage) >= sizeof(struct cbfs_payload));
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800819 header_len = (cbfs_calculate_file_header_size(name) +
820 sizeof(struct cbfs_stage));
821 need_len = header_len + size;
822
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800823 // Merge empty entries to build get max available space.
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800824 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
825
826 /* Three cases of content location on memory page:
827 * case 1.
828 * | PAGE 1 | PAGE 2 |
829 * | <header><content>| Fit. Return start of content.
830 *
831 * case 2.
832 * | PAGE 1 | PAGE 2 |
833 * | <header><content> | Fits when we shift content to align
834 * shift-> | <header>|<content> | at starting of PAGE 2.
835 *
836 * case 3. (large content filling whole page)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800837 * | PAGE 1 | PAGE 2 | PAGE 3 |
838 * | <header>< content > | Can't fit. If we shift content to
839 * |trial-> <header>< content > | PAGE 2, header can't fit in free
840 * | shift-> <header><content> space, so we must use PAGE 3.
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800841 *
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800842 * The returned address can be then used as "base-address" (-b) in add-*
843 * commands (will be re-calculated and positioned by cbfs_add_entry_at).
844 * For stage targets, the address is also used to re-link stage before
845 * being added into CBFS.
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800846 */
847 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800848 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800849 entry = cbfs_find_next_entry(image, entry)) {
850
851 uint32_t type = ntohl(entry->type);
852 if (type != CBFS_COMPONENT_NULL)
853 continue;
854
855 addr = cbfs_get_entry_addr(image, entry);
856 addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry(
857 image, entry));
858 if (addr_next - addr < need_len)
859 continue;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800860
861 offset = align_up(addr + header_len, align);
862 if (is_in_same_page(offset, size, page_size) &&
863 is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800864 DEBUG("cbfs_locate_entry: FIT (PAGE1).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800865 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800866 }
867
868 addr2 = align_up(addr, page_size);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800869 offset = align_up(addr2, align);
870 if (is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800871 DEBUG("cbfs_locate_entry: OVERLAP (PAGE2).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800872 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800873 }
874
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800875 /* Assume page_size >= header_len so adding one page will
876 * definitely provide the space for header. */
877 assert(page_size >= header_len);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800878 addr3 = addr2 + page_size;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800879 offset = align_up(addr3, align);
880 if (is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800881 DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800882 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800883 }
884 }
885 return -1;
886}