drivers/i2c/gpiomux: Add chip driver for multiplexed I2C bus
[coreboot.git] / util / cbfstool / cbfs_image.c
bloba042f0dac8cc9a62e9d735e45243f39adc46d85f
1 /* CBFS Image Manipulation */
2 /* SPDX-License-Identifier: GPL-2.0-only */
4 #include <inttypes.h>
5 #include <libgen.h>
6 #include <stddef.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <strings.h>
11 #include <commonlib/endian.h>
12 #include <vb2_sha.h>
14 #include "common.h"
15 #include "cbfs_image.h"
16 #include "elfparsing.h"
17 #include "rmodule.h"
19 /* Even though the file-adding functions---cbfs_add_entry() and
20 * cbfs_add_entry_at()---perform their sizing checks against the beginning of
21 * the subsequent section rather than a stable recorded value such as an empty
22 * file header's len field, it's possible to prove two interesting properties
23 * about their behavior:
24 * - Placing a new file within an empty entry located below an existing file
25 * entry will never leave an aligned flash address containing neither the
26 * beginning of a file header nor part of a file.
27 * - Placing a new file in an empty entry at the very end of the image such
28 * that it fits, but leaves no room for a final header, is guaranteed not to
29 * change the total amount of space for entries, even if that new file is
30 * later removed from the CBFS.
31 * These properties are somewhat nonobvious from the implementation, so the
32 * reader is encouraged to blame this comment and examine the full proofs
33 * in the commit message before making significant changes that would risk
34 * removing said guarantees.
37 /* The file name align is not defined in CBFS spec -- only a preference by
38 * (old) cbfstool. */
39 #define CBFS_FILENAME_ALIGN (16)
41 static const char *lookup_name_by_type(const struct typedesc_t *desc, uint32_t type,
42 const char *default_value)
44 int i;
45 for (i = 0; desc[i].name; i++)
46 if (desc[i].type == type)
47 return desc[i].name;
48 return default_value;
51 static int lookup_type_by_name(const struct typedesc_t *desc, const char *name)
53 int i;
54 for (i = 0; desc[i].name && strcasecmp(name, desc[i].name); ++i);
55 return desc[i].name ? (int)desc[i].type : -1;
58 static const char *get_cbfs_entry_type_name(uint32_t type)
60 return lookup_name_by_type(filetypes, type, "(unknown)");
63 int cbfs_parse_comp_algo(const char *name)
65 return lookup_type_by_name(types_cbfs_compression, name);
68 static const char *get_hash_attr_name(uint16_t hash_type)
70 return lookup_name_by_type(types_cbfs_hash, hash_type, "(invalid)");
73 int cbfs_parse_hash_algo(const char *name)
75 return lookup_type_by_name(types_cbfs_hash, name);
78 /* CBFS image */
80 size_t cbfs_calculate_file_header_size(const char *name)
82 return (sizeof(struct cbfs_file) +
83 align_up(strlen(name) + 1, CBFS_FILENAME_ALIGN));
86 /* Only call on legacy CBFSes possessing a master header. */
87 static int cbfs_fix_legacy_size(struct cbfs_image *image, char *hdr_loc)
89 assert(image);
90 assert(cbfs_is_legacy_cbfs(image));
91 // A bug in old cbfstool may produce extra few bytes (by alignment) and
92 // cause cbfstool to overwrite things after free space -- which is
93 // usually CBFS header on x86. We need to workaround that.
94 // Except when we run across a file that contains the actual header,
95 // in which case this image is a safe, new-style
96 // `cbfstool add-master-header` based image.
98 struct cbfs_file *entry, *first = NULL, *last = NULL;
99 for (first = entry = cbfs_find_first_entry(image);
100 entry && cbfs_is_valid_entry(image, entry);
101 entry = cbfs_find_next_entry(image, entry)) {
102 /* Is the header guarded by a CBFS file entry? Then exit */
103 if (((char *)entry) + ntohl(entry->offset) == hdr_loc) {
104 return 0;
106 last = entry;
108 if ((char *)first < (char *)hdr_loc &&
109 (char *)entry > (char *)hdr_loc) {
110 WARN("CBFS image was created with old cbfstool with size bug. "
111 "Fixing size in last entry...\n");
112 last->len = htonl(ntohl(last->len) - image->header.align);
113 DEBUG("Last entry has been changed from 0x%x to 0x%x.\n",
114 cbfs_get_entry_addr(image, entry),
115 cbfs_get_entry_addr(image,
116 cbfs_find_next_entry(image, last)));
118 return 0;
121 void cbfs_put_header(void *dest, const struct cbfs_header *header)
123 struct buffer outheader;
125 outheader.data = dest;
126 outheader.size = 0;
128 xdr_be.put32(&outheader, header->magic);
129 xdr_be.put32(&outheader, header->version);
130 xdr_be.put32(&outheader, header->romsize);
131 xdr_be.put32(&outheader, header->bootblocksize);
132 xdr_be.put32(&outheader, header->align);
133 xdr_be.put32(&outheader, header->offset);
134 xdr_be.put32(&outheader, header->architecture);
137 static void cbfs_decode_payload_segment(struct cbfs_payload_segment *output,
138 struct cbfs_payload_segment *input)
140 struct buffer seg = {
141 .data = (void *)input,
142 .size = sizeof(*input),
144 output->type = xdr_be.get32(&seg);
145 output->compression = xdr_be.get32(&seg);
146 output->offset = xdr_be.get32(&seg);
147 output->load_addr = xdr_be.get64(&seg);
148 output->len = xdr_be.get32(&seg);
149 output->mem_len = xdr_be.get32(&seg);
150 assert(seg.size == 0);
153 static int cbfs_file_get_compression_info(struct cbfs_file *entry,
154 uint32_t *decompressed_size)
156 unsigned int compression = CBFS_COMPRESS_NONE;
157 if (decompressed_size)
158 *decompressed_size = ntohl(entry->len);
159 for (struct cbfs_file_attribute *attr = cbfs_file_first_attr(entry);
160 attr != NULL;
161 attr = cbfs_file_next_attr(entry, attr)) {
162 if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_COMPRESSION) {
163 struct cbfs_file_attr_compression *ac =
164 (struct cbfs_file_attr_compression *)attr;
165 compression = ntohl(ac->compression);
166 if (decompressed_size)
167 *decompressed_size =
168 ntohl(ac->decompressed_size);
171 return compression;
174 static struct cbfs_file_attr_hash *cbfs_file_get_next_hash(
175 struct cbfs_file *entry, struct cbfs_file_attr_hash *cur)
177 struct cbfs_file_attribute *attr = (struct cbfs_file_attribute *)cur;
178 if (attr == NULL) {
179 attr = cbfs_file_first_attr(entry);
180 if (attr == NULL)
181 return NULL;
182 if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_HASH)
183 return (struct cbfs_file_attr_hash *)attr;
185 while ((attr = cbfs_file_next_attr(entry, attr)) != NULL) {
186 if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_HASH)
187 return (struct cbfs_file_attr_hash *)attr;
189 return NULL;
192 void cbfs_get_header(struct cbfs_header *header, void *src)
194 struct buffer outheader;
196 outheader.data = src; /* We're not modifying the data */
197 outheader.size = 0;
199 header->magic = xdr_be.get32(&outheader);
200 header->version = xdr_be.get32(&outheader);
201 header->romsize = xdr_be.get32(&outheader);
202 header->bootblocksize = xdr_be.get32(&outheader);
203 header->align = xdr_be.get32(&outheader);
204 header->offset = xdr_be.get32(&outheader);
205 header->architecture = xdr_be.get32(&outheader);
208 int cbfs_image_create(struct cbfs_image *image, size_t entries_size)
210 assert(image);
211 assert(image->buffer.data);
213 size_t empty_header_len = cbfs_calculate_file_header_size("");
214 uint32_t entries_offset = 0;
215 uint32_t align = CBFS_ENTRY_ALIGNMENT;
216 if (image->has_header) {
217 entries_offset = image->header.offset;
219 if (entries_offset > image->buffer.size) {
220 ERROR("CBFS file entries are located outside CBFS itself\n");
221 return -1;
224 align = image->header.align;
227 // This attribute must be given in order to prove that this module
228 // correctly preserves certain CBFS properties. See the block comment
229 // near the top of this file (and the associated commit message).
230 if (align < empty_header_len) {
231 ERROR("CBFS must be aligned to at least %zu bytes\n",
232 empty_header_len);
233 return -1;
236 if (entries_size > image->buffer.size - entries_offset) {
237 ERROR("CBFS doesn't have enough space to fit its file entries\n");
238 return -1;
241 if (empty_header_len > entries_size) {
242 ERROR("CBFS is too small to fit any header\n");
243 return -1;
245 struct cbfs_file *entry_header =
246 (struct cbfs_file *)(image->buffer.data + entries_offset);
247 // This alignment is necessary in order to prove that this module
248 // correctly preserves certain CBFS properties. See the block comment
249 // near the top of this file (and the associated commit message).
250 entries_size -= entries_size % align;
252 size_t capacity = entries_size - empty_header_len;
253 LOG("Created CBFS (capacity = %zu bytes)\n", capacity);
254 return cbfs_create_empty_entry(entry_header, CBFS_COMPONENT_NULL,
255 capacity, "");
258 int cbfs_legacy_image_create(struct cbfs_image *image,
259 uint32_t architecture,
260 uint32_t align,
261 struct buffer *bootblock,
262 uint32_t bootblock_offset,
263 uint32_t header_offset,
264 uint32_t entries_offset)
266 assert(image);
267 assert(image->buffer.data);
268 assert(bootblock);
270 int32_t *rel_offset;
271 uint32_t cbfs_len;
272 void *header_loc;
273 size_t size = image->buffer.size;
275 DEBUG("cbfs_image_create: bootblock=0x%x+0x%zx, "
276 "header=0x%x+0x%zx, entries_offset=0x%x\n",
277 bootblock_offset, bootblock->size, header_offset,
278 sizeof(image->header), entries_offset);
280 // Adjust legacy top-aligned address to ROM offset.
281 if (IS_TOP_ALIGNED_ADDRESS(entries_offset))
282 entries_offset = size + (int32_t)entries_offset;
283 if (IS_TOP_ALIGNED_ADDRESS(bootblock_offset))
284 bootblock_offset = size + (int32_t)bootblock_offset;
285 if (IS_TOP_ALIGNED_ADDRESS(header_offset))
286 header_offset = size + (int32_t)header_offset;
288 DEBUG("cbfs_create_image: (real offset) bootblock=0x%x, "
289 "header=0x%x, entries_offset=0x%x\n",
290 bootblock_offset, header_offset, entries_offset);
292 // Prepare bootblock
293 if (bootblock_offset + bootblock->size > size) {
294 ERROR("Bootblock (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
295 bootblock_offset, bootblock->size, size);
296 return -1;
298 if (entries_offset > bootblock_offset &&
299 entries_offset < bootblock->size) {
300 ERROR("Bootblock (0x%x+0x%zx) overlap CBFS data (0x%x)\n",
301 bootblock_offset, bootblock->size, entries_offset);
302 return -1;
304 memcpy(image->buffer.data + bootblock_offset, bootblock->data,
305 bootblock->size);
307 // Prepare header
308 if (header_offset + sizeof(image->header) > size - sizeof(int32_t)) {
309 ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
310 header_offset, sizeof(image->header), size);
311 return -1;
313 image->header.magic = CBFS_HEADER_MAGIC;
314 image->header.version = CBFS_HEADER_VERSION;
315 image->header.romsize = size;
316 image->header.bootblocksize = bootblock->size;
317 image->header.align = align;
318 image->header.offset = entries_offset;
319 image->header.architecture = architecture;
321 header_loc = (image->buffer.data + header_offset);
322 cbfs_put_header(header_loc, &image->header);
323 image->has_header = true;
325 // The last 4 byte of the image contain the relative offset from the end
326 // of the image to the master header as a 32-bit signed integer. x86
327 // relies on this also being its (memory-mapped, top-aligned) absolute
328 // 32-bit address by virtue of how two's complement numbers work.
329 assert(size % sizeof(int32_t) == 0);
330 rel_offset = (int32_t *)(image->buffer.data + size - sizeof(int32_t));
331 *rel_offset = header_offset - size;
333 // Prepare entries
334 if (align_up(entries_offset, align) != entries_offset) {
335 ERROR("Offset (0x%x) must be aligned to 0x%x.\n",
336 entries_offset, align);
337 return -1;
339 // To calculate available length, find
340 // e = min(bootblock, header, rel_offset) where e > entries_offset.
341 cbfs_len = size - sizeof(int32_t);
342 if (bootblock_offset > entries_offset && bootblock_offset < cbfs_len)
343 cbfs_len = bootblock_offset;
344 if (header_offset > entries_offset && header_offset < cbfs_len)
345 cbfs_len = header_offset;
347 if (cbfs_image_create(image, cbfs_len - entries_offset))
348 return -1;
349 return 0;
352 int cbfs_image_from_buffer(struct cbfs_image *out, struct buffer *in,
353 uint32_t offset)
355 assert(out);
356 assert(in);
357 assert(in->data);
359 buffer_clone(&out->buffer, in);
360 out->has_header = false;
362 if (cbfs_is_valid_cbfs(out)) {
363 return 0;
366 void *header_loc = cbfs_find_header(in->data, in->size, offset);
367 if (header_loc) {
368 cbfs_get_header(&out->header, header_loc);
369 out->has_header = true;
370 cbfs_fix_legacy_size(out, header_loc);
371 return 0;
372 } else if (offset != ~0u) {
373 ERROR("The -H switch is only valid on legacy images having CBFS master headers.\n");
374 return 1;
376 ERROR("Selected image region is not a valid CBFS.\n");
377 return 1;
380 int cbfs_copy_instance(struct cbfs_image *image, struct buffer *dst)
382 assert(image);
384 struct cbfs_file *src_entry, *dst_entry;
385 size_t align;
386 ssize_t last_entry_size;
388 size_t copy_end = buffer_size(dst);
390 align = CBFS_ENTRY_ALIGNMENT;
392 dst_entry = (struct cbfs_file *)buffer_get(dst);
394 /* Copy non-empty files */
395 for (src_entry = cbfs_find_first_entry(image);
396 src_entry && cbfs_is_valid_entry(image, src_entry);
397 src_entry = cbfs_find_next_entry(image, src_entry)) {
398 size_t entry_size;
400 if ((src_entry->type == htonl(CBFS_COMPONENT_NULL)) ||
401 (src_entry->type == htonl(CBFS_COMPONENT_CBFSHEADER)) ||
402 (src_entry->type == htonl(CBFS_COMPONENT_DELETED)))
403 continue;
405 entry_size = htonl(src_entry->len) + htonl(src_entry->offset);
406 memcpy(dst_entry, src_entry, entry_size);
407 dst_entry = (struct cbfs_file *)(
408 (uintptr_t)dst_entry + align_up(entry_size, align));
410 if ((size_t)((uint8_t *)dst_entry - (uint8_t *)buffer_get(dst))
411 >= copy_end) {
412 ERROR("Ran out of room in copy region.\n");
413 return 1;
417 /* Last entry size is all the room above it, except for top 4 bytes
418 * which may be used by the master header pointer. This messes with
419 * the ability to stash something "top-aligned" into the region, but
420 * keeps things simpler. */
421 last_entry_size = copy_end -
422 ((uint8_t *)dst_entry - (uint8_t *)buffer_get(dst)) -
423 cbfs_calculate_file_header_size("") - sizeof(int32_t);
425 if (last_entry_size < 0)
426 WARN("No room to create the last entry!\n")
427 else
428 cbfs_create_empty_entry(dst_entry, CBFS_COMPONENT_NULL,
429 last_entry_size, "");
431 return 0;
434 int cbfs_expand_to_region(struct buffer *region)
436 if (buffer_get(region) == NULL)
437 return 1;
439 struct cbfs_image image;
440 memset(&image, 0, sizeof(image));
441 if (cbfs_image_from_buffer(&image, region, 0)) {
442 ERROR("reading CBFS failed!\n");
443 return 1;
446 uint32_t region_sz = buffer_size(region);
448 struct cbfs_file *entry;
449 for (entry = buffer_get(region);
450 cbfs_is_valid_entry(&image, entry);
451 entry = cbfs_find_next_entry(&image, entry)) {
452 /* just iterate through */
455 /* entry now points to the first aligned address after the last valid
456 * file header. That's either outside the image or exactly the place
457 * where we need to create a new file.
459 int last_entry_size = region_sz -
460 ((uint8_t *)entry - (uint8_t *)buffer_get(region)) -
461 cbfs_calculate_file_header_size("") - sizeof(int32_t);
463 if (last_entry_size > 0) {
464 cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL,
465 last_entry_size, "");
466 /* If the last entry was an empty file, merge them. */
467 cbfs_walk(&image, cbfs_merge_empty_entry, NULL);
470 return 0;
473 int cbfs_truncate_space(struct buffer *region, uint32_t *size)
475 if (buffer_get(region) == NULL)
476 return 1;
478 struct cbfs_image image;
479 memset(&image, 0, sizeof(image));
480 if (cbfs_image_from_buffer(&image, region, 0)) {
481 ERROR("reading CBFS failed!\n");
482 return 1;
485 struct cbfs_file *entry, *trailer;
486 for (trailer = entry = buffer_get(region);
487 cbfs_is_valid_entry(&image, entry);
488 trailer = entry,
489 entry = cbfs_find_next_entry(&image, entry)) {
490 /* just iterate through */
493 /* trailer now points to the last valid CBFS entry's header.
494 * If that file is empty, remove it and report its header's offset as
495 * maximum size.
497 if ((strlen(trailer->filename) != 0) &&
498 (trailer->type != htonl(CBFS_COMPONENT_NULL)) &&
499 (trailer->type != htonl(CBFS_COMPONENT_DELETED))) {
500 /* nothing to truncate. Return de-facto CBFS size in case it
501 * was already truncated. */
502 *size = (uint8_t *)entry - (uint8_t *)buffer_get(region);
503 return 0;
505 *size = (uint8_t *)trailer - (uint8_t *)buffer_get(region);
506 memset(trailer, 0xff, buffer_size(region) - *size);
508 return 0;
511 static size_t cbfs_file_entry_metadata_size(const struct cbfs_file *f)
513 return ntohl(f->offset);
516 static size_t cbfs_file_entry_data_size(const struct cbfs_file *f)
518 return ntohl(f->len);
521 static size_t cbfs_file_entry_size(const struct cbfs_file *f)
523 return cbfs_file_entry_metadata_size(f) + cbfs_file_entry_data_size(f);
526 int cbfs_compact_instance(struct cbfs_image *image)
528 assert(image);
530 struct cbfs_file *prev;
531 struct cbfs_file *cur;
533 /* The prev entry will always be an empty entry. */
534 prev = NULL;
537 * Note: this function does not honor alignment or fixed location files.
538 * It's behavior is akin to cbfs_copy_instance() in that it expects
539 * the caller to understand the ramifications of compacting a
540 * fragmented CBFS image.
543 for (cur = cbfs_find_first_entry(image);
544 cur && cbfs_is_valid_entry(image, cur);
545 cur = cbfs_find_next_entry(image, cur)) {
546 size_t prev_size;
547 size_t cur_size;
548 size_t empty_metadata_size;
549 size_t spill_size;
550 uint32_t type = htonl(cur->type);
552 /* Current entry is empty. Kepp track of it. */
553 if ((type == htonl(CBFS_COMPONENT_NULL)) ||
554 (type == htonl(CBFS_COMPONENT_DELETED))) {
555 prev = cur;
556 continue;
559 /* Need to ensure the previous entry is an empty one. */
560 if (prev == NULL)
561 continue;
563 /* At this point prev is an empty entry. Put the non-empty
564 * file in prev's location. Then add a new empty entry. This
565 * essentialy bubbles empty entries towards the end. */
567 prev_size = cbfs_file_entry_size(prev);
568 cur_size = cbfs_file_entry_size(cur);
571 * Adjust the empty file size by the actual space occupied
572 * bewtween the beginning of the empty file and the non-empty
573 * file.
575 prev_size += (cbfs_get_entry_addr(image, cur) -
576 cbfs_get_entry_addr(image, prev)) - prev_size;
578 /* Move the non-empty file over the empty file. */
579 memmove(prev, cur, cur_size);
582 * Get location of the empty file. Note that since prev was
583 * overwritten with the non-empty file the previously moved
584 * file needs to be used to calculate the empty file's location.
586 cur = cbfs_find_next_entry(image, prev);
589 * The total space to work with for swapping the 2 entries
590 * consists of the 2 files' sizes combined. However, the
591 * cbfs_file entries start on CBFS_ALIGNMENT boundaries.
592 * Because of this the empty file size may end up smaller
593 * because of the non-empty file's metadata and data length.
595 * Calculate the spill size which is the amount of data lost
596 * due to the alignment constraints after moving the non-empty
597 * file.
599 spill_size = (cbfs_get_entry_addr(image, cur) -
600 cbfs_get_entry_addr(image, prev)) - cur_size;
602 empty_metadata_size = cbfs_calculate_file_header_size("");
604 /* Check if new empty size can contain the metadata. */
605 if (empty_metadata_size + spill_size > prev_size) {
606 ERROR("Unable to swap '%s' with prev empty entry.\n",
607 prev->filename);
608 return 1;
611 /* Update the empty file's size. */
612 prev_size -= spill_size + empty_metadata_size;
614 /* Create new empty file. */
615 cbfs_create_empty_entry(cur, CBFS_COMPONENT_NULL,
616 prev_size, "");
618 /* Merge any potential empty entries together. */
619 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
622 * Since current switched to an empty file keep track of it.
623 * Even if any empty files were merged the empty entry still
624 * starts at previously calculated location.
626 prev = cur;
629 return 0;
632 int cbfs_image_delete(struct cbfs_image *image)
634 if (image == NULL)
635 return 0;
637 buffer_delete(&image->buffer);
638 return 0;
641 /* Tries to add an entry with its data (CBFS_SUBHEADER) at given offset. */
642 static int cbfs_add_entry_at(struct cbfs_image *image,
643 struct cbfs_file *entry,
644 const void *data,
645 uint32_t content_offset,
646 const struct cbfs_file *header,
647 const size_t len_align)
649 struct cbfs_file *next = cbfs_find_next_entry(image, entry);
650 uint32_t addr = cbfs_get_entry_addr(image, entry),
651 addr_next = cbfs_get_entry_addr(image, next);
652 uint32_t min_entry_size = cbfs_calculate_file_header_size("");
653 uint32_t len, header_offset;
654 uint32_t align = image->has_header ? image->header.align :
655 CBFS_ENTRY_ALIGNMENT;
656 uint32_t header_size = ntohl(header->offset);
658 header_offset = content_offset - header_size;
659 if (header_offset % align)
660 header_offset -= header_offset % align;
661 if (header_offset < addr) {
662 ERROR("No space to hold cbfs_file header.");
663 return -1;
666 // Process buffer BEFORE content_offset.
667 if (header_offset - addr > min_entry_size) {
668 DEBUG("|min|...|header|content|... <create new entry>\n");
669 len = header_offset - addr - min_entry_size;
670 cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, len, "");
671 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
672 entry = cbfs_find_next_entry(image, entry);
673 addr = cbfs_get_entry_addr(image, entry);
676 len = content_offset - addr - header_size;
677 memcpy(entry, header, header_size);
678 if (len != 0) {
679 /* the header moved backwards a bit to accommodate cbfs_file
680 * alignment requirements, so patch up ->offset to still point
681 * to file data.
683 DEBUG("|..|header|content|... <use offset to create entry>\n");
684 DEBUG("before: offset=0x%x\n", ntohl(entry->offset));
685 // TODO reset expanded name buffer to 0xFF.
686 entry->offset = htonl(ntohl(entry->offset) + len);
687 DEBUG("after: offset=0x%x\n", ntohl(entry->len));
690 // Ready to fill data into entry.
691 DEBUG("content_offset: 0x%x, entry location: %x\n",
692 content_offset, (int)((char*)CBFS_SUBHEADER(entry) -
693 image->buffer.data));
694 assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data ==
695 (ptrdiff_t)content_offset);
696 memcpy(CBFS_SUBHEADER(entry), data, ntohl(entry->len));
697 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
699 // Align the length to a multiple of len_align
700 if (len_align &&
701 ((ntohl(entry->offset) + ntohl(entry->len)) % len_align)) {
702 size_t off = (ntohl(entry->offset) + ntohl(entry->len)) % len_align;
703 entry->len = htonl(ntohl(entry->len) + len_align - off);
706 // Process buffer AFTER entry.
707 entry = cbfs_find_next_entry(image, entry);
708 addr = cbfs_get_entry_addr(image, entry);
709 if (addr == addr_next)
710 return 0;
712 assert(addr < addr_next);
713 if (addr_next - addr < min_entry_size) {
714 DEBUG("No need for new \"empty\" entry\n");
715 /* No need to increase the size of the just
716 * stored file to extend to next file. Alignment
717 * of next file takes care of this.
719 return 0;
722 len = addr_next - addr - min_entry_size;
723 /* keep space for master header pointer */
724 if ((uint8_t *)entry + min_entry_size + len >
725 (uint8_t *)buffer_get(&image->buffer) +
726 buffer_size(&image->buffer) - sizeof(int32_t)) {
727 len -= sizeof(int32_t);
729 cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, len, "");
730 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
731 return 0;
734 int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
735 uint32_t content_offset,
736 struct cbfs_file *header,
737 const size_t len_align)
739 assert(image);
740 assert(buffer);
741 assert(buffer->data);
742 assert(!IS_TOP_ALIGNED_ADDRESS(content_offset));
744 const char *name = header->filename;
746 uint32_t entry_type;
747 uint32_t addr, addr_next;
748 struct cbfs_file *entry, *next;
749 uint32_t need_size;
750 uint32_t header_size = ntohl(header->offset);
752 need_size = header_size + buffer->size;
753 DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n",
754 name, content_offset, header_size, buffer->size, need_size);
756 // Merge empty entries.
757 DEBUG("(trying to merge empty entries...)\n");
758 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
760 for (entry = cbfs_find_first_entry(image);
761 entry && cbfs_is_valid_entry(image, entry);
762 entry = cbfs_find_next_entry(image, entry)) {
764 entry_type = ntohl(entry->type);
765 if (entry_type != CBFS_COMPONENT_NULL)
766 continue;
768 addr = cbfs_get_entry_addr(image, entry);
769 next = cbfs_find_next_entry(image, entry);
770 addr_next = cbfs_get_entry_addr(image, next);
772 DEBUG("cbfs_add_entry: space at 0x%x+0x%x(%d) bytes\n",
773 addr, addr_next - addr, addr_next - addr);
775 /* Will the file fit? Don't yet worry if we have space for a new
776 * "empty" entry. We take care of that later.
778 if (addr + need_size > addr_next)
779 continue;
781 // Test for complicated cases
782 if (content_offset > 0) {
783 if (addr_next < content_offset) {
784 DEBUG("Not for specified offset yet");
785 continue;
786 } else if (addr > content_offset) {
787 DEBUG("Exceed specified content_offset.");
788 break;
789 } else if (addr + header_size > content_offset) {
790 ERROR("Not enough space for header.\n");
791 break;
792 } else if (content_offset + buffer->size > addr_next) {
793 ERROR("Not enough space for content.\n");
794 break;
798 // TODO there are more few tricky cases that we may
799 // want to fit by altering offset.
801 if (content_offset == 0) {
802 // we tested every condition earlier under which
803 // placing the file there might fail
804 content_offset = addr + header_size;
807 DEBUG("section 0x%x+0x%x for content_offset 0x%x.\n",
808 addr, addr_next - addr, content_offset);
810 if (cbfs_add_entry_at(image, entry, buffer->data,
811 content_offset, header, len_align) == 0) {
812 return 0;
814 break;
817 ERROR("Could not add [%s, %zd bytes (%zd KB)@0x%x]; too big?\n",
818 buffer->name, buffer->size, buffer->size / 1024, content_offset);
819 return -1;
822 struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name)
824 struct cbfs_file *entry;
825 for (entry = cbfs_find_first_entry(image);
826 entry && cbfs_is_valid_entry(image, entry);
827 entry = cbfs_find_next_entry(image, entry)) {
828 if (strcasecmp(entry->filename, name) == 0) {
829 DEBUG("cbfs_get_entry: found %s\n", name);
830 return entry;
833 return NULL;
836 static int cbfs_stage_decompress(struct cbfs_stage *stage, struct buffer *buff)
838 struct buffer reader;
839 char *orig_buffer;
840 char *new_buffer;
841 size_t new_buff_sz;
842 decomp_func_ptr decompress;
844 buffer_clone(&reader, buff);
846 /* The stage metadata is in little endian. */
847 stage->compression = xdr_le.get32(&reader);
848 stage->entry = xdr_le.get64(&reader);
849 stage->load = xdr_le.get64(&reader);
850 stage->len = xdr_le.get32(&reader);
851 stage->memlen = xdr_le.get32(&reader);
853 /* Create a buffer just with the uncompressed program now that the
854 * struct cbfs_stage has been peeled off. */
855 if (stage->compression == CBFS_COMPRESS_NONE) {
856 new_buff_sz = buffer_size(buff) - sizeof(struct cbfs_stage);
858 orig_buffer = buffer_get(buff);
859 new_buffer = calloc(1, new_buff_sz);
860 memcpy(new_buffer, orig_buffer + sizeof(struct cbfs_stage),
861 new_buff_sz);
862 buffer_init(buff, buff->name, new_buffer, new_buff_sz);
863 free(orig_buffer);
864 return 0;
867 decompress = decompression_function(stage->compression);
868 if (decompress == NULL)
869 return -1;
871 orig_buffer = buffer_get(buff);
873 /* This can be too big of a buffer needed, but there's no current
874 * field indicating decompressed size of data. */
875 new_buff_sz = stage->memlen;
876 new_buffer = calloc(1, new_buff_sz);
878 if (decompress(orig_buffer + sizeof(struct cbfs_stage),
879 (int)(buffer_size(buff) - sizeof(struct cbfs_stage)),
880 new_buffer, (int)new_buff_sz, &new_buff_sz)) {
881 ERROR("Couldn't decompress stage.\n");
882 free(new_buffer);
883 return -1;
886 /* Include correct size for full stage info. */
887 buffer_init(buff, buff->name, new_buffer, new_buff_sz);
889 /* True decompressed size is just the data size -- no metadata. */
890 stage->len = new_buff_sz;
891 /* Stage is not compressed. */
892 stage->compression = CBFS_COMPRESS_NONE;
894 free(orig_buffer);
896 return 0;
899 static int cbfs_payload_decompress(struct cbfs_payload_segment *segments,
900 struct buffer *buff, int num_seg)
902 struct buffer new_buffer;
903 struct buffer seg_buffer;
904 size_t new_buff_sz;
905 char *in_ptr;
906 char *out_ptr;
907 size_t new_offset;
908 decomp_func_ptr decompress;
910 new_offset = num_seg * sizeof(*segments);
911 new_buff_sz = num_seg * sizeof(*segments);
913 /* Find out and allocate the amount of memory occupied
914 * by the binary data */
915 for (int i = 0; i < num_seg; i++)
916 new_buff_sz += segments[i].mem_len;
918 if (buffer_create(&new_buffer, new_buff_sz, "decompressed_buff"))
919 return -1;
921 in_ptr = buffer_get(buff) + new_offset;
922 out_ptr = buffer_get(&new_buffer) + new_offset;
924 for (int i = 0; i < num_seg; i++) {
925 struct buffer tbuff;
926 size_t decomp_size;
928 /* Segments BSS and ENTRY do not have binary data. */
929 if (segments[i].type == PAYLOAD_SEGMENT_BSS ||
930 segments[i].type == PAYLOAD_SEGMENT_ENTRY) {
931 continue;
932 } else if (segments[i].type == PAYLOAD_SEGMENT_PARAMS) {
933 memcpy(out_ptr, in_ptr, segments[i].len);
934 segments[i].offset = new_offset;
935 new_offset += segments[i].len;
936 in_ptr += segments[i].len;
937 out_ptr += segments[i].len;
938 segments[i].compression = CBFS_COMPRESS_NONE;
939 continue;
942 /* The payload uses an unknown compression algorithm. */
943 decompress = decompression_function(segments[i].compression);
944 if (decompress == NULL) {
945 ERROR("Unknown decompression algorithm: %u\n",
946 segments[i].compression);
947 return -1;
950 if (buffer_create(&tbuff, segments[i].mem_len, "segment")) {
951 buffer_delete(&new_buffer);
952 return -1;
955 if (decompress(in_ptr, segments[i].len, buffer_get(&tbuff),
956 (int) buffer_size(&tbuff),
957 &decomp_size)) {
958 ERROR("Couldn't decompress payload segment %u\n", i);
959 buffer_delete(&new_buffer);
960 buffer_delete(&tbuff);
961 return -1;
964 memcpy(out_ptr, buffer_get(&tbuff), decomp_size);
966 in_ptr += segments[i].len;
968 /* Update the offset of the segment. */
969 segments[i].offset = new_offset;
970 /* True decompressed size is just the data size. No metadata */
971 segments[i].len = decomp_size;
972 /* Segment is not compressed. */
973 segments[i].compression = CBFS_COMPRESS_NONE;
975 /* Update the offset and output buffer pointer. */
976 new_offset += decomp_size;
977 out_ptr += decomp_size;
979 buffer_delete(&tbuff);
982 buffer_splice(&seg_buffer, &new_buffer, 0, 0);
983 xdr_segs(&seg_buffer, segments, num_seg);
985 buffer_delete(buff);
986 *buff = new_buffer;
988 return 0;
991 static int init_elf_from_arch(Elf64_Ehdr *ehdr, uint32_t cbfs_arch)
993 int endian;
994 int nbits;
995 int machine;
997 switch (cbfs_arch) {
998 case CBFS_ARCHITECTURE_X86:
999 endian = ELFDATA2LSB;
1000 nbits = ELFCLASS32;
1001 machine = EM_386;
1002 break;
1003 case CBFS_ARCHITECTURE_ARM:
1004 endian = ELFDATA2LSB;
1005 nbits = ELFCLASS32;
1006 machine = EM_ARM;
1007 break;
1008 case CBFS_ARCHITECTURE_AARCH64:
1009 endian = ELFDATA2LSB;
1010 nbits = ELFCLASS64;
1011 machine = EM_AARCH64;
1012 break;
1013 case CBFS_ARCHITECTURE_MIPS:
1014 endian = ELFDATA2LSB;
1015 nbits = ELFCLASS32;
1016 machine = EM_MIPS;
1017 break;
1018 case CBFS_ARCHITECTURE_RISCV:
1019 endian = ELFDATA2LSB;
1020 nbits = ELFCLASS32;
1021 machine = EM_RISCV;
1022 break;
1023 default:
1024 ERROR("Unsupported arch: %x\n", cbfs_arch);
1025 return -1;
1028 elf_init_eheader(ehdr, machine, nbits, endian);
1029 return 0;
1032 static int cbfs_stage_make_elf(struct buffer *buff, uint32_t arch)
1034 Elf64_Ehdr ehdr;
1035 Elf64_Shdr shdr;
1036 struct cbfs_stage stage;
1037 struct elf_writer *ew;
1038 struct buffer elf_out;
1039 size_t empty_sz;
1040 int rmod_ret;
1042 if (arch == CBFS_ARCHITECTURE_UNKNOWN) {
1043 ERROR("You need to specify -m ARCH.\n");
1044 return -1;
1047 if (cbfs_stage_decompress(&stage, buff)) {
1048 ERROR("Failed to decompress stage.\n");
1049 return -1;
1052 if (init_elf_from_arch(&ehdr, arch))
1053 return -1;
1055 ehdr.e_entry = stage.entry;
1057 /* Attempt rmodule translation first. */
1058 rmod_ret = rmodule_stage_to_elf(&ehdr, buff);
1060 if (rmod_ret < 0) {
1061 ERROR("rmodule parsing failed\n");
1062 return -1;
1063 } else if (rmod_ret == 0)
1064 return 0;
1066 /* Rmodule couldn't do anything with the data. Continue on with SELF. */
1068 ew = elf_writer_init(&ehdr);
1069 if (ew == NULL) {
1070 ERROR("Unable to init ELF writer.\n");
1071 return -1;
1074 memset(&shdr, 0, sizeof(shdr));
1075 shdr.sh_type = SHT_PROGBITS;
1076 shdr.sh_flags = SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR;
1077 shdr.sh_addr = stage.load;
1078 shdr.sh_size = stage.len;
1079 empty_sz = stage.memlen - stage.len;
1081 if (elf_writer_add_section(ew, &shdr, buff, ".program")) {
1082 ERROR("Unable to add ELF section: .program\n");
1083 elf_writer_destroy(ew);
1084 return -1;
1087 if (empty_sz != 0) {
1088 struct buffer b;
1090 buffer_init(&b, NULL, NULL, 0);
1091 memset(&shdr, 0, sizeof(shdr));
1092 shdr.sh_type = SHT_NOBITS;
1093 shdr.sh_flags = SHF_WRITE | SHF_ALLOC;
1094 shdr.sh_addr = stage.load + stage.len;
1095 shdr.sh_size = empty_sz;
1096 if (elf_writer_add_section(ew, &shdr, &b, ".empty")) {
1097 ERROR("Unable to add ELF section: .empty\n");
1098 elf_writer_destroy(ew);
1099 return -1;
1103 if (elf_writer_serialize(ew, &elf_out)) {
1104 ERROR("Unable to create ELF file from stage.\n");
1105 elf_writer_destroy(ew);
1106 return -1;
1109 /* Flip buffer with the created ELF one. */
1110 buffer_delete(buff);
1111 *buff = elf_out;
1113 elf_writer_destroy(ew);
1115 return 0;
1118 static int cbfs_payload_make_elf(struct buffer *buff, uint32_t arch)
1120 Elf64_Ehdr ehdr;
1121 Elf64_Shdr shdr;
1122 struct cbfs_payload_segment *segs = NULL;
1123 struct elf_writer *ew = NULL;
1124 struct buffer elf_out;
1125 int segments = 0;
1126 int retval = -1;
1128 if (arch == CBFS_ARCHITECTURE_UNKNOWN) {
1129 ERROR("You need to specify -m ARCH.\n");
1130 goto out;
1133 /* Count the number of segments inside buffer */
1134 while (true) {
1135 uint32_t payload_type = 0;
1137 struct cbfs_payload_segment *seg;
1139 seg = buffer_get(buff);
1140 payload_type = read_be32(&seg[segments].type);
1142 if (payload_type == PAYLOAD_SEGMENT_CODE) {
1143 segments++;
1144 } else if (payload_type == PAYLOAD_SEGMENT_DATA) {
1145 segments++;
1146 } else if (payload_type == PAYLOAD_SEGMENT_BSS) {
1147 segments++;
1148 } else if (payload_type == PAYLOAD_SEGMENT_PARAMS) {
1149 segments++;
1150 } else if (payload_type == PAYLOAD_SEGMENT_ENTRY) {
1151 /* The last segment in a payload is always ENTRY as
1152 * specified by the parse_elf_to_payload() function.
1153 * Therefore there is no need to continue looking for
1154 * segments.*/
1155 segments++;
1156 break;
1157 } else {
1158 ERROR("Unknown payload segment type: %x\n",
1159 payload_type);
1160 goto out;
1164 segs = malloc(segments * sizeof(*segs));
1166 /* Decode xdr segments */
1167 for (int i = 0; i < segments; i++) {
1168 struct cbfs_payload_segment *serialized_seg = buffer_get(buff);
1169 xdr_get_seg(&segs[i], &serialized_seg[i]);
1172 if (cbfs_payload_decompress(segs, buff, segments)) {
1173 ERROR("Failed to decompress payload.\n");
1174 goto out;
1177 if (init_elf_from_arch(&ehdr, arch))
1178 goto out;
1180 ehdr.e_entry = segs[segments-1].load_addr;
1182 ew = elf_writer_init(&ehdr);
1183 if (ew == NULL) {
1184 ERROR("Unable to init ELF writer.\n");
1185 goto out;
1188 for (int i = 0; i < segments; i++) {
1189 struct buffer tbuff;
1190 size_t empty_sz = 0;
1192 memset(&shdr, 0, sizeof(shdr));
1193 char *name = NULL;
1195 if (segs[i].type == PAYLOAD_SEGMENT_CODE) {
1196 shdr.sh_type = SHT_PROGBITS;
1197 shdr.sh_flags = SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR;
1198 shdr.sh_addr = segs[i].load_addr;
1199 shdr.sh_size = segs[i].len;
1200 empty_sz = segs[i].mem_len - segs[i].len;
1201 name = strdup(".text");
1202 buffer_splice(&tbuff, buff, segs[i].offset,
1203 segs[i].len);
1204 } else if (segs[i].type == PAYLOAD_SEGMENT_DATA) {
1205 shdr.sh_type = SHT_PROGBITS;
1206 shdr.sh_flags = SHF_ALLOC | SHF_WRITE;
1207 shdr.sh_addr = segs[i].load_addr;
1208 shdr.sh_size = segs[i].len;
1209 empty_sz = segs[i].mem_len - segs[i].len;
1210 name = strdup(".data");
1211 buffer_splice(&tbuff, buff, segs[i].offset,
1212 segs[i].len);
1213 } else if (segs[i].type == PAYLOAD_SEGMENT_BSS) {
1214 shdr.sh_type = SHT_NOBITS;
1215 shdr.sh_flags = SHF_ALLOC | SHF_WRITE;
1216 shdr.sh_addr = segs[i].load_addr;
1217 shdr.sh_size = segs[i].len;
1218 name = strdup(".bss");
1219 buffer_splice(&tbuff, buff, 0, 0);
1220 } else if (segs[i].type == PAYLOAD_SEGMENT_PARAMS) {
1221 shdr.sh_type = SHT_NOTE;
1222 shdr.sh_flags = 0;
1223 shdr.sh_size = segs[i].len;
1224 name = strdup(".note.pinfo");
1225 buffer_splice(&tbuff, buff, segs[i].offset,
1226 segs[i].len);
1227 } else if (segs[i].type == PAYLOAD_SEGMENT_ENTRY) {
1228 break;
1229 } else {
1230 ERROR("unknown ELF segment type\n");
1231 goto out;
1234 if (!name) {
1235 ERROR("out of memory\n");
1236 goto out;
1239 if (elf_writer_add_section(ew, &shdr, &tbuff, name)) {
1240 ERROR("Unable to add ELF section: %s\n", name);
1241 free(name);
1242 goto out;
1244 free(name);
1246 if (empty_sz != 0) {
1247 struct buffer b;
1249 buffer_init(&b, NULL, NULL, 0);
1250 memset(&shdr, 0, sizeof(shdr));
1251 shdr.sh_type = SHT_NOBITS;
1252 shdr.sh_flags = SHF_WRITE | SHF_ALLOC;
1253 shdr.sh_addr = segs[i].load_addr + segs[i].len;
1254 shdr.sh_size = empty_sz;
1255 name = strdup(".empty");
1256 if (!name) {
1257 ERROR("out of memory\n");
1258 goto out;
1260 if (elf_writer_add_section(ew, &shdr, &b, name)) {
1261 ERROR("Unable to add ELF section: %s\n", name);
1262 free(name);
1263 goto out;
1265 free(name);
1269 if (elf_writer_serialize(ew, &elf_out)) {
1270 ERROR("Unable to create ELF file from stage.\n");
1271 goto out;
1274 /* Flip buffer with the created ELF one. */
1275 buffer_delete(buff);
1276 *buff = elf_out;
1277 retval = 0;
1279 out:
1280 free(segs);
1281 elf_writer_destroy(ew);
1282 return retval;
1285 int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
1286 const char *filename, uint32_t arch, bool do_processing)
1288 struct cbfs_file *entry = cbfs_get_entry(image, entry_name);
1289 struct buffer buffer;
1290 if (!entry) {
1291 ERROR("File not found: %s\n", entry_name);
1292 return -1;
1295 unsigned int compressed_size = ntohl(entry->len);
1296 unsigned int decompressed_size = 0;
1297 unsigned int compression = cbfs_file_get_compression_info(entry,
1298 &decompressed_size);
1299 unsigned int buffer_len;
1300 decomp_func_ptr decompress;
1302 if (do_processing) {
1303 decompress = decompression_function(compression);
1304 if (!decompress) {
1305 ERROR("looking up decompression routine failed\n");
1306 return -1;
1308 buffer_len = decompressed_size;
1309 } else {
1310 /* Force nop decompression */
1311 decompress = decompression_function(CBFS_COMPRESS_NONE);
1312 buffer_len = compressed_size;
1315 LOG("Found file %.30s at 0x%x, type %.12s, compressed %d, size %d\n",
1316 entry_name, cbfs_get_entry_addr(image, entry),
1317 get_cbfs_entry_type_name(ntohl(entry->type)), compressed_size,
1318 decompressed_size);
1320 buffer_init(&buffer, strdup("(cbfs_export_entry)"), NULL, 0);
1321 buffer.data = malloc(buffer_len);
1322 buffer.size = buffer_len;
1324 if (decompress(CBFS_SUBHEADER(entry), compressed_size,
1325 buffer.data, buffer.size, NULL)) {
1326 ERROR("decompression failed for %s\n", entry_name);
1327 buffer_delete(&buffer);
1328 return -1;
1332 * The stage metadata is never compressed proper for cbfs_stage
1333 * files. The contents of the stage data can be though. Therefore
1334 * one has to do a second pass for stages to potentially decompress
1335 * the stage data to make it more meaningful.
1337 if (do_processing) {
1338 int (*make_elf)(struct buffer *, uint32_t) = NULL;
1339 switch (ntohl(entry->type)) {
1340 case CBFS_COMPONENT_STAGE:
1341 make_elf = cbfs_stage_make_elf;
1342 break;
1343 case CBFS_COMPONENT_SELF:
1344 make_elf = cbfs_payload_make_elf;
1345 break;
1347 if (make_elf && make_elf(&buffer, arch)) {
1348 ERROR("Failed to write %s into %s.\n",
1349 entry_name, filename);
1350 buffer_delete(&buffer);
1351 return -1;
1355 if (buffer_write_file(&buffer, filename) != 0) {
1356 ERROR("Failed to write %s into %s.\n",
1357 entry_name, filename);
1358 buffer_delete(&buffer);
1359 return -1;
1362 buffer_delete(&buffer);
1363 INFO("Successfully dumped the file to: %s\n", filename);
1364 return 0;
1367 int cbfs_remove_entry(struct cbfs_image *image, const char *name)
1369 struct cbfs_file *entry;
1370 entry = cbfs_get_entry(image, name);
1371 if (!entry) {
1372 ERROR("CBFS file %s not found.\n", name);
1373 return -1;
1375 DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n",
1376 entry->filename, cbfs_get_entry_addr(image, entry));
1377 entry->type = htonl(CBFS_COMPONENT_DELETED);
1378 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
1379 return 0;
1382 int cbfs_print_header_info(struct cbfs_image *image)
1384 char *name = strdup(image->buffer.name);
1385 assert(image);
1386 printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n"
1387 "alignment: %d bytes, architecture: %s\n\n",
1388 basename(name),
1389 image->buffer.size / 1024,
1390 image->header.bootblocksize,
1391 image->header.romsize,
1392 image->header.offset,
1393 image->header.align,
1394 arch_to_string(image->header.architecture));
1395 free(name);
1396 return 0;
1399 static int cbfs_print_stage_info(struct cbfs_stage *stage, FILE* fp)
1401 fprintf(fp,
1402 " %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", "
1403 "length: %d/%d\n",
1404 lookup_name_by_type(types_cbfs_compression,
1405 stage->compression, "(unknown)"),
1406 stage->entry,
1407 stage->load,
1408 stage->len,
1409 stage->memlen);
1410 return 0;
1413 static int cbfs_print_decoded_payload_segment_info(
1414 struct cbfs_payload_segment *seg, FILE *fp)
1416 /* The input (seg) must be already decoded by
1417 * cbfs_decode_payload_segment.
1419 switch (seg->type) {
1420 case PAYLOAD_SEGMENT_CODE:
1421 case PAYLOAD_SEGMENT_DATA:
1422 fprintf(fp, " %s (%s compression, offset: 0x%x, "
1423 "load: 0x%" PRIx64 ", length: %d/%d)\n",
1424 (seg->type == PAYLOAD_SEGMENT_CODE ?
1425 "code " : "data"),
1426 lookup_name_by_type(types_cbfs_compression,
1427 seg->compression,
1428 "(unknown)"),
1429 seg->offset, seg->load_addr, seg->len,
1430 seg->mem_len);
1431 break;
1433 case PAYLOAD_SEGMENT_ENTRY:
1434 fprintf(fp, " entry (0x%" PRIx64 ")\n",
1435 seg->load_addr);
1436 break;
1438 case PAYLOAD_SEGMENT_BSS:
1439 fprintf(fp, " BSS (address 0x%016" PRIx64 ", "
1440 "length 0x%x)\n",
1441 seg->load_addr, seg->len);
1442 break;
1444 case PAYLOAD_SEGMENT_PARAMS:
1445 fprintf(fp, " parameters\n");
1446 break;
1448 default:
1449 fprintf(fp, " 0x%x (%s compression, offset: 0x%x, "
1450 "load: 0x%" PRIx64 ", length: %d/%d\n",
1451 seg->type,
1452 lookup_name_by_type(types_cbfs_compression,
1453 seg->compression,
1454 "(unknown)"),
1455 seg->offset, seg->load_addr, seg->len,
1456 seg->mem_len);
1457 break;
1459 return 0;
1462 int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
1463 void *arg)
1465 const char *name = entry->filename;
1466 struct cbfs_payload_segment *payload;
1467 FILE *fp = (FILE *)arg;
1469 if (!cbfs_is_valid_entry(image, entry)) {
1470 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
1471 cbfs_get_entry_addr(image, entry));
1472 return -1;
1474 if (!fp)
1475 fp = stdout;
1477 unsigned int decompressed_size = 0;
1478 unsigned int compression = cbfs_file_get_compression_info(entry,
1479 &decompressed_size);
1480 const char *compression_name = lookup_name_by_type(
1481 types_cbfs_compression, compression, "????");
1483 if (compression == CBFS_COMPRESS_NONE)
1484 fprintf(fp, "%-30s 0x%-8x %-12s %8d %-4s\n",
1485 *name ? name : "(empty)",
1486 cbfs_get_entry_addr(image, entry),
1487 get_cbfs_entry_type_name(ntohl(entry->type)),
1488 ntohl(entry->len),
1489 compression_name
1491 else
1492 fprintf(fp, "%-30s 0x%-8x %-12s %8d %-4s (%d decompressed)\n",
1493 *name ? name : "(empty)",
1494 cbfs_get_entry_addr(image, entry),
1495 get_cbfs_entry_type_name(ntohl(entry->type)),
1496 ntohl(entry->len),
1497 compression_name,
1498 decompressed_size
1501 struct cbfs_file_attr_hash *hash = NULL;
1502 while ((hash = cbfs_file_get_next_hash(entry, hash)) != NULL) {
1503 unsigned int hash_type = ntohl(hash->hash_type);
1504 if (hash_type >= CBFS_NUM_SUPPORTED_HASHES) {
1505 fprintf(fp, "invalid hash type %d\n", hash_type);
1506 break;
1508 size_t hash_len = widths_cbfs_hash[hash_type];
1509 char *hash_str = bintohex(hash->hash_data, hash_len);
1510 uint8_t local_hash[hash_len];
1511 if (vb2_digest_buffer(CBFS_SUBHEADER(entry),
1512 ntohl(entry->len), hash_type, local_hash,
1513 hash_len) != VB2_SUCCESS) {
1514 fprintf(fp, "failed to hash '%s'\n", name);
1515 free(hash_str);
1516 break;
1518 int valid = memcmp(local_hash, hash->hash_data, hash_len) == 0;
1519 const char *valid_str = valid ? "valid" : "invalid";
1521 fprintf(fp, " hash %s:%s %s\n",
1522 get_hash_attr_name(hash_type),
1523 hash_str, valid_str);
1524 free(hash_str);
1527 if (!verbose)
1528 return 0;
1530 DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n",
1531 cbfs_get_entry_addr(image, entry), ntohl(entry->offset),
1532 cbfs_get_entry_addr(image, entry) + ntohl(entry->offset),
1533 ntohl(entry->len));
1535 /* note the components of the subheader may be in host order ... */
1536 switch (ntohl(entry->type)) {
1537 case CBFS_COMPONENT_STAGE:
1538 cbfs_print_stage_info((struct cbfs_stage *)
1539 CBFS_SUBHEADER(entry), fp);
1540 break;
1542 case CBFS_COMPONENT_SELF:
1543 payload = (struct cbfs_payload_segment *)
1544 CBFS_SUBHEADER(entry);
1545 while (payload) {
1546 struct cbfs_payload_segment seg;
1547 cbfs_decode_payload_segment(&seg, payload);
1548 cbfs_print_decoded_payload_segment_info(
1549 &seg, fp);
1550 if (seg.type == PAYLOAD_SEGMENT_ENTRY)
1551 break;
1552 else
1553 payload ++;
1555 break;
1556 default:
1557 break;
1559 return 0;
1562 static int cbfs_print_parseable_entry_info(struct cbfs_image *image,
1563 struct cbfs_file *entry, void *arg)
1565 FILE *fp = (FILE *)arg;
1566 const char *name;
1567 const char *type;
1568 size_t offset;
1569 size_t metadata_size;
1570 size_t data_size;
1571 const char *sep = "\t";
1573 if (!cbfs_is_valid_entry(image, entry)) {
1574 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
1575 cbfs_get_entry_addr(image, entry));
1576 return -1;
1579 name = entry->filename;
1580 if (*name == '\0')
1581 name = "(empty)";
1582 type = get_cbfs_entry_type_name(ntohl(entry->type)),
1583 metadata_size = ntohl(entry->offset);
1584 data_size = ntohl(entry->len);
1585 offset = cbfs_get_entry_addr(image, entry);
1587 fprintf(fp, "%s%s", name, sep);
1588 fprintf(fp, "0x%zx%s", offset, sep);
1589 fprintf(fp, "%s%s", type, sep);
1590 fprintf(fp, "0x%zx%s", metadata_size, sep);
1591 fprintf(fp, "0x%zx%s", data_size, sep);
1592 fprintf(fp, "0x%zx\n", metadata_size + data_size);
1594 return 0;
1597 int cbfs_print_directory(struct cbfs_image *image)
1599 if (cbfs_is_legacy_cbfs(image))
1600 cbfs_print_header_info(image);
1601 printf("%-30s %-10s %-12s Size Comp\n", "Name", "Offset", "Type");
1602 cbfs_walk(image, cbfs_print_entry_info, NULL);
1603 return 0;
1606 int cbfs_print_parseable_directory(struct cbfs_image *image)
1608 size_t i;
1609 const char *header[] = {
1610 "Name",
1611 "Offset",
1612 "Type",
1613 "Metadata Size",
1614 "Data Size",
1615 "Total Size",
1617 const char *sep = "\t";
1619 for (i = 0; i < ARRAY_SIZE(header) - 1; i++)
1620 fprintf(stdout, "%s%s", header[i], sep);
1621 fprintf(stdout, "%s\n", header[i]);
1622 cbfs_walk(image, cbfs_print_parseable_entry_info, stdout);
1623 return 0;
1626 int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
1627 unused void *arg)
1629 struct cbfs_file *next;
1630 uint32_t next_addr = 0;
1632 /* We don't return here even if this entry is already empty because we
1633 want to merge the empty entries following after it. */
1635 /* Loop until non-empty entry is found, starting from the current entry.
1636 After the loop, next_addr points to the next non-empty entry. */
1637 next = entry;
1638 while (ntohl(next->type) == CBFS_COMPONENT_DELETED ||
1639 ntohl(next->type) == CBFS_COMPONENT_NULL) {
1640 next = cbfs_find_next_entry(image, next);
1641 if (!next)
1642 break;
1643 next_addr = cbfs_get_entry_addr(image, next);
1644 if (!cbfs_is_valid_entry(image, next))
1645 /* 'next' could be the end of cbfs */
1646 break;
1649 if (!next_addr)
1650 /* Nothing to empty */
1651 return 0;
1653 /* We can return here if we find only a single empty entry.
1654 For simplicity, we just proceed (and make it empty again). */
1656 /* We're creating one empty entry for combined empty spaces */
1657 uint32_t addr = cbfs_get_entry_addr(image, entry);
1658 size_t len = next_addr - addr - cbfs_calculate_file_header_size("");
1659 DEBUG("join_empty_entry: [0x%x, 0x%x) len=%zu\n", addr, next_addr, len);
1660 cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, len, "");
1662 return 0;
1665 int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback,
1666 void *arg)
1668 int count = 0;
1669 struct cbfs_file *entry;
1670 for (entry = cbfs_find_first_entry(image);
1671 entry && cbfs_is_valid_entry(image, entry);
1672 entry = cbfs_find_next_entry(image, entry)) {
1673 count ++;
1674 if (callback(image, entry, arg) != 0)
1675 break;
1677 return count;
1680 static int cbfs_header_valid(struct cbfs_header *header)
1682 if ((ntohl(header->magic) == CBFS_HEADER_MAGIC) &&
1683 ((ntohl(header->version) == CBFS_HEADER_VERSION1) ||
1684 (ntohl(header->version) == CBFS_HEADER_VERSION2)) &&
1685 (ntohl(header->offset) < ntohl(header->romsize)))
1686 return 1;
1687 return 0;
1690 struct cbfs_header *cbfs_find_header(char *data, size_t size,
1691 uint32_t forced_offset)
1693 size_t offset;
1694 int found = 0;
1695 int32_t rel_offset;
1696 struct cbfs_header *header, *result = NULL;
1698 if (forced_offset < (size - sizeof(struct cbfs_header))) {
1699 /* Check if the forced header is valid. */
1700 header = (struct cbfs_header *)(data + forced_offset);
1701 if (cbfs_header_valid(header))
1702 return header;
1703 return NULL;
1706 // Try finding relative offset of master header at end of file first.
1707 rel_offset = *(int32_t *)(data + size - sizeof(int32_t));
1708 offset = size + rel_offset;
1709 DEBUG("relative offset: %#zx(-%#zx), offset: %#zx\n",
1710 (size_t)rel_offset, (size_t)-rel_offset, offset);
1712 if (offset >= size - sizeof(*header) ||
1713 !cbfs_header_valid((struct cbfs_header *)(data + offset))) {
1714 // Some use cases append non-CBFS data to the end of the ROM.
1715 DEBUG("relative offset seems wrong, scanning whole image...\n");
1716 offset = 0;
1719 for (; offset + sizeof(*header) < size; offset++) {
1720 header = (struct cbfs_header *)(data + offset);
1721 if (!cbfs_header_valid(header))
1722 continue;
1723 if (!found++)
1724 result = header;
1726 if (found > 1)
1727 // Top-aligned images usually have a working relative offset
1728 // field, so this is more likely to happen on bottom-aligned
1729 // ones (where the first header is the "outermost" one)
1730 WARN("Multiple (%d) CBFS headers found, using the first one.\n",
1731 found);
1732 return result;
1736 struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image)
1738 assert(image);
1739 if (image->has_header)
1740 /* header.offset is relative to start of flash, not
1741 * start of region, so use it with the full image.
1743 return (struct cbfs_file *)
1744 (buffer_get_original_backing(&image->buffer) +
1745 image->header.offset);
1746 else
1747 return (struct cbfs_file *)buffer_get(&image->buffer);
1750 struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
1751 struct cbfs_file *entry)
1753 uint32_t addr = cbfs_get_entry_addr(image, entry);
1754 int align = image->has_header ? image->header.align :
1755 CBFS_ENTRY_ALIGNMENT;
1756 assert(entry && cbfs_is_valid_entry(image, entry));
1757 addr += ntohl(entry->offset) + ntohl(entry->len);
1758 addr = align_up(addr, align);
1759 return (struct cbfs_file *)(image->buffer.data + addr);
1762 uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry)
1764 assert(image && image->buffer.data && entry);
1765 return (int32_t)((char *)entry - image->buffer.data);
1768 int cbfs_is_valid_cbfs(struct cbfs_image *image)
1770 return buffer_check_magic(&image->buffer, CBFS_FILE_MAGIC,
1771 strlen(CBFS_FILE_MAGIC));
1774 int cbfs_is_legacy_cbfs(struct cbfs_image *image)
1776 return image->has_header;
1779 int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry)
1781 uint32_t offset = cbfs_get_entry_addr(image, entry);
1783 if (offset >= image->buffer.size)
1784 return 0;
1786 struct buffer entry_data;
1787 buffer_clone(&entry_data, &image->buffer);
1788 buffer_seek(&entry_data, offset);
1789 return buffer_check_magic(&entry_data, CBFS_FILE_MAGIC,
1790 strlen(CBFS_FILE_MAGIC));
1793 struct cbfs_file *cbfs_create_file_header(int type,
1794 size_t len, const char *name)
1796 struct cbfs_file *entry = malloc(MAX_CBFS_FILE_HEADER_BUFFER);
1797 memset(entry, CBFS_CONTENT_DEFAULT_VALUE, MAX_CBFS_FILE_HEADER_BUFFER);
1798 memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
1799 entry->type = htonl(type);
1800 entry->len = htonl(len);
1801 entry->attributes_offset = 0;
1802 entry->offset = htonl(cbfs_calculate_file_header_size(name));
1803 memset(entry->filename, 0, ntohl(entry->offset) - sizeof(*entry));
1804 strcpy(entry->filename, name);
1805 return entry;
1808 int cbfs_create_empty_entry(struct cbfs_file *entry, int type,
1809 size_t len, const char *name)
1811 struct cbfs_file *tmp = cbfs_create_file_header(type, len, name);
1812 memcpy(entry, tmp, ntohl(tmp->offset));
1813 free(tmp);
1814 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len);
1815 return 0;
1818 struct cbfs_file_attribute *cbfs_file_first_attr(struct cbfs_file *file)
1820 /* attributes_offset should be 0 when there is no attribute, but all
1821 * values that point into the cbfs_file header are invalid, too. */
1822 if (ntohl(file->attributes_offset) <= sizeof(*file))
1823 return NULL;
1825 /* There needs to be enough space for the file header and one
1826 * attribute header for this to make sense. */
1827 if (ntohl(file->offset) <=
1828 sizeof(*file) + sizeof(struct cbfs_file_attribute))
1829 return NULL;
1831 return (struct cbfs_file_attribute *)
1832 (((uint8_t *)file) + ntohl(file->attributes_offset));
1835 struct cbfs_file_attribute *cbfs_file_next_attr(struct cbfs_file *file,
1836 struct cbfs_file_attribute *attr)
1838 /* ex falso sequitur quodlibet */
1839 if (attr == NULL)
1840 return NULL;
1842 /* Is there enough space for another attribute? */
1843 if ((uint8_t *)attr + ntohl(attr->len) +
1844 sizeof(struct cbfs_file_attribute) >
1845 (uint8_t *)file + ntohl(file->offset))
1846 return NULL;
1848 struct cbfs_file_attribute *next = (struct cbfs_file_attribute *)
1849 (((uint8_t *)attr) + ntohl(attr->len));
1850 /* If any, "unused" attributes must come last. */
1851 if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED)
1852 return NULL;
1853 if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED2)
1854 return NULL;
1856 return next;
1859 struct cbfs_file_attribute *cbfs_add_file_attr(struct cbfs_file *header,
1860 uint32_t tag,
1861 uint32_t size)
1863 struct cbfs_file_attribute *attr, *next;
1864 next = cbfs_file_first_attr(header);
1865 do {
1866 attr = next;
1867 next = cbfs_file_next_attr(header, attr);
1868 } while (next != NULL);
1869 uint32_t header_size = ntohl(header->offset) + size;
1870 if (header_size > MAX_CBFS_FILE_HEADER_BUFFER) {
1871 DEBUG("exceeding allocated space for cbfs_file headers");
1872 return NULL;
1874 /* attr points to the last valid attribute now.
1875 * If NULL, we have to create the first one. */
1876 if (attr == NULL) {
1877 /* New attributes start where the header ends.
1878 * header->offset is later set to accommodate the
1879 * additional structure.
1880 * No endianness translation necessary here, because both
1881 * fields are encoded the same way. */
1882 header->attributes_offset = header->offset;
1883 attr = (struct cbfs_file_attribute *)
1884 (((uint8_t *)header) +
1885 ntohl(header->attributes_offset));
1886 } else {
1887 attr = (struct cbfs_file_attribute *)
1888 (((uint8_t *)attr) +
1889 ntohl(attr->len));
1891 header->offset = htonl(header_size);
1892 memset(attr, CBFS_CONTENT_DEFAULT_VALUE, size);
1893 attr->tag = htonl(tag);
1894 attr->len = htonl(size);
1895 return attr;
1898 int cbfs_add_file_hash(struct cbfs_file *header, struct buffer *buffer,
1899 enum vb2_hash_algorithm hash_type)
1901 uint32_t hash_index = hash_type;
1903 if (hash_index >= CBFS_NUM_SUPPORTED_HASHES)
1904 return -1;
1906 unsigned hash_size = widths_cbfs_hash[hash_type];
1907 if (hash_size == 0)
1908 return -1;
1910 struct cbfs_file_attr_hash *attrs =
1911 (struct cbfs_file_attr_hash *)cbfs_add_file_attr(header,
1912 CBFS_FILE_ATTR_TAG_HASH,
1913 sizeof(struct cbfs_file_attr_hash) + hash_size);
1915 if (attrs == NULL)
1916 return -1;
1918 attrs->hash_type = htonl(hash_type);
1919 if (vb2_digest_buffer(buffer_get(buffer), buffer_size(buffer),
1920 hash_type, attrs->hash_data, hash_size) != VB2_SUCCESS)
1921 return -1;
1923 return 0;
1926 /* Finds a place to hold whole data in same memory page. */
1927 static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page)
1929 if (!page)
1930 return 1;
1931 return (start / page) == (start + size - 1) / page;
1934 /* Tests if data can fit in a range by given offset:
1935 * start ->| metadata_size | offset (+ size) |<- end
1937 static int is_in_range(size_t start, size_t end, size_t metadata_size,
1938 size_t offset, size_t size)
1940 return (offset >= start + metadata_size && offset + size <= end);
1943 static size_t absolute_align(const struct cbfs_image *image, size_t val,
1944 size_t align)
1946 const size_t region_offset = buffer_offset(&image->buffer);
1947 /* To perform alignment on absolute address, take the region offset */
1948 /* of the image into account. */
1949 return align_up(val + region_offset, align) - region_offset;
1953 int32_t cbfs_locate_entry(struct cbfs_image *image, size_t size,
1954 size_t page_size, size_t align, size_t metadata_size)
1956 struct cbfs_file *entry;
1957 size_t need_len;
1958 size_t addr, addr_next, addr2, addr3, offset;
1960 /* Default values: allow fitting anywhere in ROM. */
1961 if (!page_size)
1962 page_size = image->has_header ? image->header.romsize :
1963 image->buffer.size;
1964 if (!align)
1965 align = 1;
1967 if (size > page_size)
1968 ERROR("Input file size (%zd) greater than page size (%zd).\n",
1969 size, page_size);
1971 size_t image_align = image->has_header ? image->header.align :
1972 CBFS_ENTRY_ALIGNMENT;
1973 if (page_size % image_align)
1974 WARN("%s: Page size (%#zx) not aligned with CBFS image (%#zx).\n",
1975 __func__, page_size, image_align);
1977 need_len = metadata_size + size;
1979 // Merge empty entries to build get max available space.
1980 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
1982 /* Three cases of content location on memory page:
1983 * case 1.
1984 * | PAGE 1 | PAGE 2 |
1985 * | <header><content>| Fit. Return start of content.
1987 * case 2.
1988 * | PAGE 1 | PAGE 2 |
1989 * | <header><content> | Fits when we shift content to align
1990 * shift-> | <header>|<content> | at starting of PAGE 2.
1992 * case 3. (large content filling whole page)
1993 * | PAGE 1 | PAGE 2 | PAGE 3 |
1994 * | <header>< content > | Can't fit. If we shift content to
1995 * |trial-> <header>< content > | PAGE 2, header can't fit in free
1996 * | shift-> <header><content> space, so we must use PAGE 3.
1998 * The returned address can be then used as "base-address" (-b) in add-*
1999 * commands (will be re-calculated and positioned by cbfs_add_entry_at).
2000 * For stage targets, the address is also used to re-link stage before
2001 * being added into CBFS.
2003 for (entry = cbfs_find_first_entry(image);
2004 entry && cbfs_is_valid_entry(image, entry);
2005 entry = cbfs_find_next_entry(image, entry)) {
2007 uint32_t type = ntohl(entry->type);
2008 if (type != CBFS_COMPONENT_NULL)
2009 continue;
2011 addr = cbfs_get_entry_addr(image, entry);
2012 addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry(
2013 image, entry));
2014 if (addr_next - addr < need_len)
2015 continue;
2017 offset = absolute_align(image, addr + metadata_size, align);
2018 if (is_in_same_page(offset, size, page_size) &&
2019 is_in_range(addr, addr_next, metadata_size, offset, size)) {
2020 DEBUG("cbfs_locate_entry: FIT (PAGE1).");
2021 return offset;
2024 addr2 = align_up(addr, page_size);
2025 offset = absolute_align(image, addr2, align);
2026 if (is_in_range(addr, addr_next, metadata_size, offset, size)) {
2027 DEBUG("cbfs_locate_entry: OVERLAP (PAGE2).");
2028 return offset;
2031 /* Assume page_size >= metadata_size so adding one page will
2032 * definitely provide the space for header. */
2033 assert(page_size >= metadata_size);
2034 addr3 = addr2 + page_size;
2035 offset = absolute_align(image, addr3, align);
2036 if (is_in_range(addr, addr_next, metadata_size, offset, size)) {
2037 DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3).");
2038 return offset;
2041 return -1;