| 724 | } |
| 725 | |
| 726 | int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer, |
| 727 | uint32_t content_offset, |
| 728 | struct cbfs_file *header, |
| 729 | const size_t len_align) |
| 730 | { |
| 731 | assert(image); |
| 732 | assert(buffer); |
| 733 | assert(buffer->data); |
| 734 | assert(!IS_HOST_SPACE_ADDRESS(content_offset)); |
| 735 | |
| 736 | const char *name = header->filename; |
| 737 | |
| 738 | /* This is so special rows in cbfstool print -k -v output stay unambiguous. */ |
| 739 | if (name[0] == '[') { |
| 740 | ERROR("CBFS file name `%s` must not start with `[`\n", name); |
| 741 | return -1; |
| 742 | } |
| 743 | |
| 744 | uint32_t entry_type; |
| 745 | uint32_t addr, addr_next; |
| 746 | uint32_t entry_size; |
| 747 | uint32_t max_null_entry_size = 0; |
| 748 | struct cbfs_file *entry, *next; |
| 749 | uint32_t need_size; |
| 750 | uint32_t header_size = be32toh(header->offset); |
| 751 | |
| 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); |
| 755 | |
| 756 | // Merge empty entries. |
| 757 | DEBUG("(trying to merge empty entries...)\n"); |
| 758 | cbfs_legacy_walk(image, cbfs_merge_empty_entry, NULL); |
| 759 | |
| 760 | for (entry = cbfs_find_first_entry(image); |
| 761 | entry && cbfs_is_valid_entry(image, entry); |
| 762 | entry = cbfs_find_next_entry(image, entry)) { |
| 763 | |
| 764 | entry_type = be32toh(entry->type); |
| 765 | if (entry_type != CBFS_TYPE_NULL) |
| 766 | continue; |
| 767 | |
| 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); |
| 771 | entry_size = addr_next - addr; |
| 772 | max_null_entry_size = MAX(max_null_entry_size, entry_size); |
| 773 | |
| 774 | DEBUG("%s: space at 0x%x+0x%x(%d) bytes\n", __func__, |
| 775 | addr, entry_size, entry_size); |
| 776 | |
| 777 | /* Will the file fit? Don't yet worry if we have space for a new |
| 778 | * "empty" entry. We take care of that later. |
| 779 | */ |
| 780 | if (addr + need_size > addr_next) |
| 781 | continue; |
| 782 | |
| 783 | // Test for complicated cases |
no test coverage detected