[coreboot-gerrit] Patch set updated for coreboot: cbfs: Add "struct" file type and associated helpers

Julius Werner (jwerner@chromium.org) gerrit at coreboot.org
Sat Aug 27 00:35:26 CEST 2016


Julius Werner (jwerner at chromium.org) just uploaded a new patch set to gerrit, which you can find at https://review.coreboot.org/16272

-gerrit

commit 70c02ab4e785332fbc0c8b190b5e9812e4a17411
Author: Julius Werner <jwerner at chromium.org>
Date:   Fri Aug 19 15:43:06 2016 -0700

    cbfs: Add "struct" file type and associated helpers
    
    This patch adds functionality to compile a C data structure into a raw
    binary file, add it to CBFS and allow coreboot to load it at runtime.
    This is useful in all cases where we need to be able to have several
    larger data sets available in an image, but will only require a small
    subset of them at boot (a classic example would be DRAM parameters) or
    only require it in certain boot modes. This allows us to load less data
    from flash and increase boot speed compared to solutions that compile
    all data sets into a stage.
    
    Each structure has to be defined in a separate .c file which contains no
    functions and only a single global variable. The data type must be
    serialization safe (composed of only fixed-width types, paying attention
    to padding). It must be added to CBFS in a Makefile with the 'struct'
    file processor.
    
    Change-Id: Iab65c0b6ebea235089f741eaa8098743e54d6ccc
    Signed-off-by: Julius Werner <jwerner at chromium.org>
---
 Makefile.inc                                      | 12 ++++++++++++
 payloads/libpayload/include/cbfs_core.h           |  1 +
 src/commonlib/include/commonlib/cbfs_serialized.h |  1 +
 src/include/cbfs.h                                |  4 ++++
 src/lib/cbfs.c                                    | 21 +++++++++++++++++++++
 util/cbfstool/cbfs.h                              |  2 ++
 6 files changed, 41 insertions(+)

diff --git a/Makefile.inc b/Makefile.inc
index ff77747..6225152 100644
--- a/Makefile.inc
+++ b/Makefile.inc
@@ -278,6 +278,18 @@ cbfs-files-processor-defconfig= \
 		\mv -f $(2).tmp $(2))
 
 #######################################################################
+# Compile a C file with a bare struct definition into binary
+# arg1: C source file
+# arg2: binary file
+cbfs-files-processor-struct= \
+	$(eval $(2): $(1) $(obj)/build.h $(KCONFIG_AUTOHEADER); \
+		printf "    CC+STRIP   $(@)\n"; \
+		$(CC_ramstage) -MMD $(CPPFLAGS_ramstage) $(CFLAGS_ramstage) $(ramstage-c-ccopts) -include $(KCONFIG_AUTOHEADER) -MT $(2) -o $(2).tmp -c $(1) && \
+		$(OBJCOPY_ramstage) -O binary $(2).tmp $(2); \
+		rm -f $(2).tmp) \
+	$(eval DEPENDENCIES += $(2).d)
+
+#######################################################################
 # Add handler for arbitrary files in CBFS
 $(call add-special-class,cbfs-files)
 cbfs-files-handler= \
diff --git a/payloads/libpayload/include/cbfs_core.h b/payloads/libpayload/include/cbfs_core.h
index f94056d..f45139e 100644
--- a/payloads/libpayload/include/cbfs_core.h
+++ b/payloads/libpayload/include/cbfs_core.h
@@ -73,6 +73,7 @@
 #define CBFS_TYPE_VSA        0x51
 #define CBFS_TYPE_MBI        0x52
 #define CBFS_TYPE_MICROCODE  0x53
+#define CBFS_TYPE_STRUCT     0x70
 #define CBFS_COMPONENT_CMOS_DEFAULT 0xaa
 #define CBFS_COMPONENT_CMOS_LAYOUT 0x01aa
 
diff --git a/src/commonlib/include/commonlib/cbfs_serialized.h b/src/commonlib/include/commonlib/cbfs_serialized.h
index 1e394d2..706f175 100644
--- a/src/commonlib/include/commonlib/cbfs_serialized.h
+++ b/src/commonlib/include/commonlib/cbfs_serialized.h
@@ -77,6 +77,7 @@
 #define CBFS_TYPE_MRC        0x61
 #define CBFS_TYPE_MMA        0x62
 #define CBFS_TYPE_EFI        0x63
+#define CBFS_TYPE_STRUCT     0x70
 #define CBFS_COMPONENT_CMOS_DEFAULT 0xaa
 #define CBFS_TYPE_SPD          0xab
 #define CBFS_TYPE_MRC_CACHE    0xac
diff --git a/src/include/cbfs.h b/src/include/cbfs.h
index 6d9dd42..6063dd6 100644
--- a/src/include/cbfs.h
+++ b/src/include/cbfs.h
@@ -34,6 +34,10 @@ int cbfs_boot_locate(struct cbfsf *fh, const char *name, uint32_t *type);
  * leaking mappings are a no-op. Returns NULL on error, else returns
  * the mapping and sets the size of the file. */
 void *cbfs_boot_map_with_leak(const char *name, uint32_t type, size_t *size);
+/* Load a struct file from CBFS into a buffer. Returns amount of loaded
+ * bytes on success or 0 on error. File will get decompressed as necessary.
+ * Same decompression requirements as cbfs_load_and_decompress(). */
+size_t cbfs_boot_load_struct(const char *name, void *buf, size_t buf_size);
 
 /* Load |in_size| bytes from |rdev| at |offset| to the |buffer_size| bytes
  * large |buffer|, decompressing it according to |compression| in the process.
diff --git a/src/lib/cbfs.c b/src/lib/cbfs.c
index 7318c87..5a2f63f 100644
--- a/src/lib/cbfs.c
+++ b/src/lib/cbfs.c
@@ -79,6 +79,8 @@ size_t cbfs_load_and_decompress(const struct region_device *rdev, size_t offset,
 
 	switch (compression) {
 	case CBFS_COMPRESS_NONE:
+		if (buffer_size < in_size)
+			return 0;
 		if (rdev_readat(rdev, buffer, offset, in_size) != in_size)
 			return 0;
 		return in_size;
@@ -166,6 +168,25 @@ void *cbfs_boot_load_stage_by_name(const char *name)
 	return prog_entry(&stage);
 }
 
+size_t cbfs_boot_load_struct(const char *name, void *buf, size_t buf_size)
+{
+	struct cbfsf fh;
+	uint32_t compression_algo;
+	size_t decompressed_size;
+	uint32_t type = CBFS_TYPE_STRUCT;
+
+	if (cbfs_boot_locate(&fh, name, &type) < 0)
+		return 0;
+
+	if (cbfsf_decompression_info(&fh, &compression_algo,
+				     &decompressed_size) < 0
+				     || decompressed_size > buf_size)
+		return 0;
+
+	return cbfs_load_and_decompress(&fh.data, 0, region_device_sz(&fh.data),
+					buf, buf_size, compression_algo);
+}
+
 int cbfs_prog_stage_load(struct prog *pstage)
 {
 	struct cbfs_stage stage;
diff --git a/util/cbfstool/cbfs.h b/util/cbfstool/cbfs.h
index 36b9fc0..f1ae09d 100644
--- a/util/cbfstool/cbfs.h
+++ b/util/cbfstool/cbfs.h
@@ -186,6 +186,7 @@ struct cbfs_payload {
 #define CBFS_COMPONENT_MRC        0x61
 #define CBFS_COMPONENT_MMA	  0x62
 #define CBFS_COMPONENT_EFI	  0x63
+#define CBFS_COMPONENT_STRUCT	  0x70
 #define CBFS_COMPONENT_CMOS_DEFAULT 0xaa
 #define CBFS_COMPONENT_SPD          0xab
 #define CBFS_COMPONENT_MRC_CACHE    0xac
@@ -227,6 +228,7 @@ static struct typedesc_t filetypes[] unused = {
 	{CBFS_COMPONENT_MRC_CACHE, "mrc_cache"},
 	{CBFS_COMPONENT_MMA, "mma"},
 	{CBFS_COMPONENT_EFI, "efi"},
+	{CBFS_COMPONENT_STRUCT, "struct"},
 	{CBFS_COMPONENT_DELETED, "deleted"},
 	{CBFS_COMPONENT_NULL, "null"}
 };



More information about the coreboot-gerrit mailing list