aboutsummaryrefslogtreecommitdiff
path: root/util/cbfstool/common.c
diff options
context:
space:
mode:
authorGabe Black <gabeblack@google.com>2012-01-27 00:33:47 -0800
committerStefan Reinauer <stefan.reinauer@coreboot.org>2012-04-02 18:39:08 +0200
commite1bb49e2ece1eda6a3d566b48b7a8e50a3469f63 (patch)
treec26d5c701a82d620aa87dfae071571faee200a3d /util/cbfstool/common.c
parentdd30acdd590f6a39a5d3c4a38e3d949e73b5e2fa (diff)
Add a "remove" command to cbfstool
This command removes the first file it finds with the given name by changing its type to CBFS_COMPONENT_NULL and setting the first character of its name to a null terminator. If the "files" immediately before or after the target file are already marked as empty, they're all merged together into one large file. Change-Id: Idc6b2a4c355c3f039c2ccae81866e3ed6035539b Signed-off-by: Gabe Black <gabeblack@google.com> Reviewed-by: Ronald G. Minnich <rminnich@google.com> Reviewed-on: http://review.coreboot.org/814 Tested-by: build bot (Jenkins) Reviewed-by: Ronald G. Minnich <rminnich@gmail.com>
Diffstat (limited to 'util/cbfstool/common.c')
-rw-r--r--util/cbfstool/common.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/util/cbfstool/common.c b/util/cbfstool/common.c
index df744217d4..49da7aea6c 100644
--- a/util/cbfstool/common.c
+++ b/util/cbfstool/common.c
@@ -350,6 +350,62 @@ int add_file_to_cbfs(void *content, uint32_t contentsize, uint32_t location)
return 1;
}
+
+static struct cbfs_file *merge_adjacent_files(struct cbfs_file *first,
+ struct cbfs_file *second)
+{
+ uint32_t new_length =
+ ntohl(first->len) + ntohl(second->len) + ntohl(second->offset);
+ first->len = htonl(new_length);
+ first->checksum = 0; // FIXME?
+ return first;
+}
+
+static struct cbfs_file *next_file(struct cbfs_file *prev)
+{
+ uint32_t pos = (prev == NULL) ? phys_start :
+ ALIGN(virt_to_phys(prev) + ntohl(prev->len) + ntohl(prev->offset),
+ align);
+
+ for (; pos < phys_end; pos += align) {
+ if (cbfs_file_header(pos))
+ return (struct cbfs_file *)phys_to_virt(pos);
+ }
+ return NULL;
+}
+
+
+int remove_file_from_cbfs(const char *filename)
+{
+ struct cbfs_file *prev = NULL;
+ struct cbfs_file *cur = next_file(prev);
+ struct cbfs_file *next = next_file(cur);
+ for (; cur; prev = cur, cur = next, next = next_file(next)) {
+
+ /* Check if this is the file to remove. */
+ char *name = (char *)cur + sizeof(*cur);
+ if (strcmp(name, filename))
+ continue;
+
+ /* Mark the file as free space and erase its name. */
+ cur->type = CBFS_COMPONENT_NULL;
+ name[0] = '\0';
+
+ /* Merge it with the previous file if possible. */
+ if (prev && prev->type == CBFS_COMPONENT_NULL)
+ cur = merge_adjacent_files(prev, cur);
+
+ /* Merge it with the next file if possible. */
+ if (next && next->type == CBFS_COMPONENT_NULL)
+ merge_adjacent_files(cur, next);
+
+ return 0;
+ }
+ printf("CBFS file %s not found.\n", filename);
+ return 1;
+}
+
+
/* returns new data block with cbfs_file header, suitable to dump into the ROM. location returns
the new location that points to the cbfs_file header */
void *create_cbfs_file(const char *filename, void *data, uint32_t * datasize,