[coreboot] New patch to review for coreboot: 41c6065 Add a "remove" command to cbfstool

Stefan Reinauer (stefan.reinauer@coreboot.org) gerrit at coreboot.org
Fri Mar 30 21:30:37 CEST 2012


Stefan Reinauer (stefan.reinauer at coreboot.org) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/814

-gerrit

commit 41c6065a48ad409b8d2dbf9d54e42df263c2939f
Author: Gabe Black <gabeblack at google.com>
Date:   Fri Jan 27 00:33:47 2012 -0800

    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 at google.com>
    Reviewed-by: Ronald G. Minnich <rminnich at google.com>
---
 util/cbfstool/cbfstool.c |   30 ++++++++++++++++++++++++
 util/cbfstool/common.c   |   56 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 86 insertions(+), 0 deletions(-)

diff --git a/util/cbfstool/cbfstool.c b/util/cbfstool/cbfstool.c
index 939221e..9dbdc1c 100644
--- a/util/cbfstool/cbfstool.c
+++ b/util/cbfstool/cbfstool.c
@@ -29,6 +29,7 @@ typedef enum {
 	CMD_ADD,
 	CMD_ADD_PAYLOAD,
 	CMD_ADD_STAGE,
+	CMD_REMOVE,
 	CMD_CREATE,
 	CMD_LOCATE,
 	CMD_PRINT,
@@ -195,6 +196,33 @@ static int cbfs_add_stage(int argc, char **argv)
 	return 0;
 }
 
+static int cbfs_remove(int argc, char **argv)
+{
+	char *romname = argv[1];
+	char *cmd = argv[2];
+	void *rom = loadrom(romname);
+
+	if (rom == NULL) {
+		printf("Could not load ROM image '%s'.\n", romname);
+		return 1;
+	}
+
+	if (argc < 4) {
+		printf("not enough arguments to '%s'.\n", cmd);
+		return 1;
+	}
+
+	char *cbfsname = argv[3];
+
+	if (remove_file_from_cbfs(cbfsname)) {
+		printf("Removing file '%s' failed.\n", cbfsname);
+		return 1;
+	}
+	if (writerom(romname, rom, romsize))
+		return 1;
+	return 0;
+}
+
 static int cbfs_create(int argc, char **argv)
 {
 	char *romname = argv[1];
@@ -275,6 +303,7 @@ static const struct command commands[] = {
 	{CMD_ADD, "add", cbfs_add},
 	{CMD_ADD_PAYLOAD, "add-payload", cbfs_add_payload},
 	{CMD_ADD_STAGE, "add-stage", cbfs_add_stage},
+	{CMD_REMOVE, "remove", cbfs_remove},
 	{CMD_CREATE, "create", cbfs_create},
 	{CMD_LOCATE, "locate", cbfs_locate},
 	{CMD_PRINT, "print", cbfs_print},
@@ -292,6 +321,7 @@ static void usage(void)
 	     " add FILE NAME TYPE [base address]    Add a component\n"
 	     " add-payload FILE NAME [COMP] [base]  Add a payload to the ROM\n"
 	     " add-stage FILE NAME [COMP] [base]    Add a stage to the ROM\n"
+	     " remove FILE NAME                     Remove a component\n"
 	     " create SIZE BOOTBLOCK [ALIGN]        Create a ROM file\n"
 	     " locate FILE NAME ALIGN               Find a place for a file of that size\n"
 	     " print                                Show the contents of the ROM\n"
diff --git a/util/cbfstool/common.c b/util/cbfstool/common.c
index df74421..49da7ae 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,




More information about the coreboot mailing list