summaryrefslogtreecommitdiff
path: root/util/cbfstool/cbfs_image.c
diff options
context:
space:
mode:
authorSol Boucher <solb@chromium.org>2015-03-25 13:40:08 -0700
committerPatrick Georgi <pgeorgi@google.com>2015-05-08 20:25:20 +0200
commite3260a042f438cedb446c5e7b3dc6f55a3b9aa95 (patch)
tree56149aa77d5dfcb9eb57081e890711a1e3470d40 /util/cbfstool/cbfs_image.c
parent64c6cd73f3567e893a9cde7d08eb5505c1a19c62 (diff)
downloadcoreboot-e3260a042f438cedb446c5e7b3dc6f55a3b9aa95.tar.xz
cbfstool: Restructure around support for reading/writing portions of files
The buffer API that cbfstool uses to read and write files only directly supports one-shot operations on whole files. This adds an intermediate partitioned_file module that sits on top of the buffer system and has an awareness of FMAP entries. It provides an easy way to get a buffer for an individual region of a larger image file based on FMAP section name, as well as incrementally write those smaller buffers back to the backing file at the appropriate offset. The module has two distinct modes of operation: - For new images whose layout is described exclusively by an FMAP section, all the aforementioned functionality will be available. - For images in the current format, where the CBFS master header serves as the root of knowledge of the image's size and layout, the module falls back to a legacy operation mode, where it only allows manipulation of the entire image as one unit, but exposes this support through the same interface by mapping the region named SECTION_NAME_PRIMARY_CBFS ("COREBOOT") to the whole file. The tool is presently only ported onto the new module running in legacy mode: higher-level support for true "partitioned" images will be forthcoming. However, as part of this change, the crusty cbfs_image_from_file() and cbfs_image_write_file() abstractions are removed and replaced with a single cbfs_image function, cbfs_image_from_buffer(), as well as centralized image reading/writing directly in cbfstool's main() function. This reduces the boilerplate required to implement each new action, makes the create action much more similar to the others, and will make implementing additional actions and adding in support for the new format much easier. BUG=chromium:470407 TEST=Build panther and nyan_big coreboot.rom images with and without this patch and diff their hexdumps. Ensure that no differences occur at different locations from the diffs between subsequent builds of an identical source tree. Then flash a full new build onto nyan_big and watch it boot normally. BRANCH=None Change-Id: I25578c7b223bc8434c3074cb0dd8894534f8c500 Signed-off-by: Sol Boucher <solb@chromium.org> Original-Commit-Id: 7e1c96a48e7a27fc6b90289d35e6e169d5e7ad20 Original-Change-Id: Ia4a1a4c48df42b9ec2d6b9471b3a10eb7b24bb39 Original-Signed-off-by: Sol Boucher <solb@chromium.org> Original-Reviewed-on: https://chromium-review.googlesource.com/265581 Original-Reviewed-by: Aaron Durbin <adurbin@chromium.org> Reviewed-on: http://review.coreboot.org/10134 Tested-by: build bot (Jenkins) Reviewed-by: Patrick Georgi <pgeorgi@google.com>
Diffstat (limited to 'util/cbfstool/cbfs_image.c')
-rw-r--r--util/cbfstool/cbfs_image.c75
1 files changed, 32 insertions, 43 deletions
diff --git a/util/cbfstool/cbfs_image.c b/util/cbfstool/cbfs_image.c
index 16dc3dcab4..d07f80660f 100644
--- a/util/cbfstool/cbfs_image.c
+++ b/util/cbfstool/cbfs_image.c
@@ -184,18 +184,22 @@ void cbfs_get_header(struct cbfs_header *header, void *src)
int cbfs_image_create(struct cbfs_image *image,
uint32_t architecture,
- size_t size,
uint32_t align,
struct buffer *bootblock,
uint32_t bootblock_offset,
uint32_t header_offset,
uint32_t entries_offset)
{
+ assert(image);
+ assert(image->buffer.data);
+ assert(bootblock);
+
struct cbfs_file *entry;
int32_t *rel_offset;
uint32_t cbfs_len;
- size_t entry_header_len;
void *header_loc;
+ size_t empty_header_len;
+ size_t size = image->buffer.size;
DEBUG("cbfs_image_create: bootblock=0x%x+0x%zx, "
"header=0x%x+0x%zx, entries_offset=0x%x\n",
@@ -205,17 +209,13 @@ int cbfs_image_create(struct cbfs_image *image,
// This attribute must be given in order to prove that this module
// correctly preserves certain CBFS properties. See the block comment
// near the top of this file (and the associated commit message).
- size_t empty_header_len = cbfs_calculate_file_header_size("");
+ empty_header_len = cbfs_calculate_file_header_size("");
if (align < empty_header_len) {
ERROR("CBFS must be aligned to at least %zu bytes\n",
empty_header_len);
return -1;
}
- if (buffer_create(&image->buffer, size, "(new)") != 0)
- return -1;
- memset(image->buffer.data, CBFS_CONTENT_DEFAULT_VALUE, size);
-
// Adjust legcay top-aligned address to ROM offset.
if (IS_TOP_ALIGNED_ADDRESS(entries_offset))
entries_offset = size + (int32_t)entries_offset;
@@ -274,10 +274,9 @@ int cbfs_image_create(struct cbfs_image *image,
entries_offset, align);
return -1;
}
- entry_header_len = cbfs_calculate_file_header_size("");
- if (entries_offset + entry_header_len > size) {
+ if (entries_offset + empty_header_len > size) {
ERROR("Offset (0x%x+0x%zx) exceed ROM size(0x%zx)\n",
- entries_offset, entry_header_len, size);
+ entries_offset, empty_header_len, size);
return -1;
}
entry = (struct cbfs_file *)(image->buffer.data + entries_offset);
@@ -292,34 +291,26 @@ int cbfs_image_create(struct cbfs_image *image,
// correctly preserves certain CBFS properties. See the block comment
// near the top of this file (and the associated commit message).
cbfs_len -= cbfs_len % align;
- cbfs_len -= entries_offset + entry_header_len;
+ cbfs_len -= entries_offset + empty_header_len;
cbfs_create_empty_entry(entry, cbfs_len, "");
LOG("Created CBFS image (capacity = %d bytes)\n", cbfs_len);
return 0;
}
-int cbfs_image_from_file(struct cbfs_image *image,
- const char *filename, uint32_t offset)
+int cbfs_image_from_buffer(struct cbfs_image *out, struct buffer *in,
+ uint32_t offset)
{
- void *header_loc;
-
- if (buffer_from_file(&image->buffer, filename) != 0)
- return -1;
- DEBUG("read_cbfs_image: %s (%zd bytes)\n", image->buffer.name,
- image->buffer.size);
- header_loc = cbfs_find_header(image->buffer.data,
- image->buffer.size,
- offset);
- if (!header_loc) {
- ERROR("%s does not have CBFS master header.\n", filename);
- cbfs_image_delete(image);
- return -1;
+ assert(out);
+ assert(in);
+ assert(in->data);
+
+ buffer_clone(&out->buffer, in);
+ void *header_loc = cbfs_find_header(in->data, in->size, offset);
+ if (header_loc) {
+ cbfs_get_header(&out->header, header_loc);
+ cbfs_fix_legacy_size(out, header_loc);
}
-
- cbfs_get_header(&image->header, header_loc);
- cbfs_fix_legacy_size(image, header_loc);
-
- return 0;
+ return !header_loc;
}
int cbfs_copy_instance(struct cbfs_image *image, size_t copy_offset,
@@ -396,12 +387,6 @@ int cbfs_copy_instance(struct cbfs_image *image, size_t copy_offset,
return 0;
}
-int cbfs_image_write_file(struct cbfs_image *image, const char *filename)
-{
- assert(image && image->buffer.data);
- return buffer_write_file(&image->buffer, filename);
-}
-
int cbfs_image_delete(struct cbfs_image *image)
{
if (image == NULL)
@@ -961,12 +946,16 @@ uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry)
int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry)
{
- return (entry &&
- (char *)entry >= image->buffer.data &&
- (char *)entry + sizeof(entry->magic) <
- image->buffer.data + image->buffer.size &&
- memcmp(entry->magic, CBFS_FILE_MAGIC,
- sizeof(entry->magic)) == 0);
+ uint32_t offset = cbfs_get_entry_addr(image, entry);
+
+ if (offset >= image->buffer.size)
+ return 0;
+
+ struct buffer entry_data;
+ buffer_clone(&entry_data, &image->buffer);
+ buffer_seek(&entry_data, offset);
+ return buffer_check_magic(&entry_data, CBFS_FILE_MAGIC,
+ strlen(CBFS_FILE_MAGIC));
}
int cbfs_create_empty_entry(struct cbfs_file *entry,