Patrick Georgi (pgeorgi@google.com) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/10935
-gerrit
commit 484400ed61ae947c972846035b2182706dbb8f6a Author: Daisuke Nojiri dnojiri@chromium.org Date: Thu Jul 9 15:07:45 2015 -0700
cbfstool: allow compression at file header level
Currently, compression is only allowed at subheader level (e.g. cbfs_stage, cbfs_payload_segment). This change adds compression field to each file's header so that any cbfs file can be compressed.
With the necessary additions in coreboot and libpayload, the following sample code can load a compressed file:
const char *name = "foo.bmp"; struct cbfs_file *file = cbfs_get_file(media, name); void *dst = malloc(ntohl(file->uncompressed_size)); dst = cbfs_get_file_content(media, name, type, file, dst);
cbfs_stage and cbfs_payload_segment continue to support compression at subheader level because stages and payloads have to be decompressed to the load address, which is stored in the subheader. For these, file level compression should be turned off.
Change-Id: I9a00ec99dfc68ffb2771bb4a3cc5ba6ba8a326f4 Signed-off-by: Daisuke Nojiri dnojiri@chromium.org Signed-off-by: Patrick Georgi pgeorgi@chromium.org --- util/cbfstool/cbfs.h | 9 +++++++++ util/cbfstool/cbfstool.c | 37 ++++++++++++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 3 deletions(-)
diff --git a/util/cbfstool/cbfs.h b/util/cbfstool/cbfs.h index d45fc67..6eb9c8a 100644 --- a/util/cbfstool/cbfs.h +++ b/util/cbfstool/cbfs.h @@ -98,6 +98,15 @@ struct cbfs_file_attribute {
#define CBFS_FILE_ATTR_UNUSED 0 #define CBFS_FILE_ATTR_UNUSED2 0xffffffff +#define CBFS_FILE_ATTR_COMPRESSION makemagic('L', 'Z', 'C', 'B') + +struct cbfs_file_attr_compression { + uint32_t tag; + uint32_t len; + /* whole file compression format. 0 if no compression. */ + uint32_t compression; + uint32_t decompressed_size; +} __PACKED;
struct cbfs_stage { uint32_t compression; diff --git a/util/cbfstool/cbfstool.c b/util/cbfstool/cbfstool.c index 059acbf..c6680ff 100644 --- a/util/cbfstool/cbfstool.c +++ b/util/cbfstool/cbfstool.c @@ -230,6 +230,37 @@ static int cbfs_add_component(const char *filename, return 0; }
+static int cbfstool_convert_raw(struct buffer *buffer, + unused uint32_t *offset, struct cbfs_file *header) +{ + char *compressed; + int compressed_size; + + comp_func_ptr compress = compression_function(param.compression); + if (!compress) + return -1; + compressed = calloc(buffer->size, 1); + + if (compress(buffer->data, buffer->size, + compressed, &compressed_size)) { + WARN("Compression failed - disabled\n"); + } else { + struct cbfs_file_attr_compression *attrs = + (struct cbfs_file_attr_compression *) + cbfs_add_file_attr(header, CBFS_FILE_ATTR_COMPRESSION, + sizeof(struct cbfs_file_attr_compression)); + if (attrs == NULL) + return -1; + free(buffer->data); + buffer->data = compressed; + attrs->compression = htonl(param.compression); + attrs->decompressed_size = htonl(buffer->size); + buffer->size = compressed_size; + header->len = htonl(buffer->size); + } + return 0; +} + static int cbfstool_convert_mkstage(struct buffer *buffer, uint32_t *offset, struct cbfs_file *header) { @@ -301,7 +332,7 @@ static int cbfs_add(void) param.type, param.baseaddress, param.headeroffset, - NULL); + cbfstool_convert_raw); }
static int cbfs_add_stage(void) @@ -747,7 +778,7 @@ static bool cbfs_is_legacy_format(struct buffer *buffer) }
static const struct command commands[] = { - {"add", "H:r:f:n:t:b:vh?", cbfs_add, true, true}, + {"add", "H:r:f:n:t:c:b:vh?", cbfs_add, true, true}, {"add-flat-binary", "H:r:f:n:l:e:c:b:vh?", cbfs_add_flat_binary, true, true}, {"add-payload", "H:r:f:n:t:c:b:C:I:vh?", cbfs_add_payload, true, true}, @@ -862,7 +893,7 @@ static void usage(char *name) " -h Display this help message\n\n" "COMMANDs:\n" " add [-r image,regions] -f FILE -n NAME -t TYPE \\n" - " [-b base-address] " + " [-c compression] [-b base-address] " "Add a component\n" " add-payload [-r image,regions] -f FILE -n NAME \\n" " [-c compression] [-b base-address] "