[coreboot] [PATCH] cbfstool "add-lzma" method

Kevin O'Connor kevin at koconnor.net
Sun Sep 5 20:21:08 CEST 2010


This patch enhances cbfstool so that it can support "cbfstool ROM
add-lzma FILE NAME" calls.  This is useful for callers that wish to
add an lzma compressed raw file.

Right now, SeaBIOS supports lzma raw files for things like floppy
images.  Today, adding one of these files requires a two step process
- for example:

$ lzma -zc /path/to/myfloppy.img > myfloppy.img.lzma
$ cbfstool coreboot.rom add myfloppy.img.lzma floppyimg/MyFloppy.lzma raw

Unfortunately, various versions of "lzma" are quirky and the above can
be troublesome.  With the patch below, a user need only execute:

$ cbfstool coreboot.rom add-lzma myfloppy.img floppyimg/MyFloppy.lzma

Signed-off-by: Kevin O'Connor <kevin at koconnor.net>


Index: util/cbfstool/cbfs.h
===================================================================
--- util/cbfstool/cbfs.h	(revision 5777)
+++ util/cbfstool/cbfs.h	(working copy)
@@ -73,6 +73,7 @@
 #define CBFS_COMPONENT_OPTIONROM  0x30
 #define CBFS_COMPONENT_BOOTSPLASH 0x40
 #define CBFS_COMPONENT_RAW        0x50
+#define CBFS_COMPONENT_LZMA_RAW   0x40000050
 #define CBFS_COMPONENT_VSA        0x51
 #define CBFS_COMPONENT_MBI        0x52
 #define CBFS_COMPONENT_MICROCODE  0x53
Index: util/cbfstool/common.c
===================================================================
--- util/cbfstool/common.c	(revision 5777)
+++ util/cbfstool/common.c	(working copy)
@@ -142,6 +142,7 @@
 	{CBFS_COMPONENT_OPTIONROM, "optionrom"},
 	{CBFS_COMPONENT_BOOTSPLASH, "bootsplash"},
 	{CBFS_COMPONENT_RAW, "raw"},
+	{CBFS_COMPONENT_LZMA_RAW, "lzma-raw"},
 	{CBFS_COMPONENT_VSA, "vsa"},
 	{CBFS_COMPONENT_MBI, "mbi"},
 	{CBFS_COMPONENT_MICROCODE, "microcode"},
Index: util/cbfstool/cbfstool.c
===================================================================
--- util/cbfstool/cbfstool.c	(revision 5777)
+++ util/cbfstool/cbfstool.c	(working copy)
@@ -18,6 +18,7 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
  */
 
+#include <stdlib.h>
 #include <stdio.h>
 #include <stdint.h>
 #include <string.h>
@@ -28,6 +29,7 @@
 	CMD_ADD,
 	CMD_ADD_PAYLOAD,
 	CMD_ADD_STAGE,
+	CMD_ADD_LZMA,
 	CMD_CREATE,
 	CMD_LOCATE,
 	CMD_PRINT
@@ -187,6 +189,73 @@
 	return 0;
 }
 
+static int cbfs_add_lzma(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 < 5) {
+		printf("not enough arguments to '%s'.\n", cmd);
+		return 1;
+	}
+
+	char *filename = argv[3];
+	char *cbfsname = argv[4];
+
+	uint32_t filesize = 0;
+	void *filedata = loadfile(filename, &filesize, 0, SEEK_SET);
+	if (filedata == NULL) {
+		printf("Could not load file '%s'.\n", filename);
+		return 1;
+	}
+
+	uint32_t base = 0;
+	void *cbfsfile = NULL;
+
+	uint32_t type = CBFS_COMPONENT_LZMA_RAW;
+	if (argc > 5) {
+		if (intfiletype(argv[5]) != ((uint64_t) - 1))
+			type = intfiletype(argv[5]);
+		else
+			type = strtoul(argv[5], NULL, 0);
+	}
+	if (argc > 6) {
+		base = strtoul(argv[6], NULL, 0);
+	}
+	uint32_t compresssize = filesize;
+	void *compressed_data;
+	for (;;) {
+		compressed_data = malloc(compresssize);
+		if (! compressed_data) {
+			printf("Could not allocate %d space\n", compresssize);
+			return 1;
+		}
+		uint32_t actualsize = compresssize;
+		extern void do_lzma_compress(char *in, int in_len,
+					     char *out, int *out_len);
+		do_lzma_compress(filedata, filesize, compressed_data,
+				 &compresssize);
+		if (compresssize <= actualsize)
+			break;
+		// Try again with required size.
+		free(compressed_data);
+	}
+
+	cbfsfile = create_cbfs_file(cbfsname, compressed_data,
+				    &compresssize, type, &base);
+	if (add_file_to_cbfs(cbfsfile, compresssize, base))
+		return 1;
+	if (writerom(romname, rom, romsize))
+		return 1;
+	return 0;
+}
+
 static int cbfs_create(int argc, char **argv)
 {
 	char *romname = argv[1];
@@ -249,6 +318,7 @@
 	{CMD_ADD, "add", cbfs_add},
 	{CMD_ADD_PAYLOAD, "add-payload", cbfs_add_payload},
 	{CMD_ADD_STAGE, "add-stage", cbfs_add_stage},
+	{CMD_ADD_LZMA, "add-lzma", cbfs_add_lzma},
 	{CMD_CREATE, "create", cbfs_create},
 	{CMD_LOCATE, "locate", cbfs_locate},
 	{CMD_PRINT, "print", cbfs_print}
@@ -265,6 +335,7 @@
 	     " 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"
+	     " add-lzma FILE NAME [TYPE] [base]     Lzma compress and add\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\n"




More information about the coreboot mailing list