[coreboot] New patch to review for coreboot: 95e511f cbfstool: support parsing UEFI firmware volumes

Stefan Reinauer (stefan.reinauer@coreboot.org) gerrit at coreboot.org
Tue Feb 5 00:41:31 CET 2013


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

-gerrit

commit 95e511f5504dab3fabac2f18036712f4d5e4e3fe
Author: Stefan Reinauer <reinauer at chromium.org>
Date:   Mon Feb 4 15:39:13 2013 -0800

    cbfstool: support parsing UEFI firmware volumes
    
    This removes the hack implemented in http://review.coreboot.org/#/c/2280
    (and should make using 64bit Tiano easier, but that's not yet supported)
    
    Change-Id: Ie30129c4102dfbd41584177f39057b31f5a937fd
    Signed-off-by: Stefan Reinauer <reinauer at google.com>
---
 src/arch/x86/Makefile.inc      |  14 +-----
 util/cbfstool/cbfs-mkpayload.c | 107 ++++++++++++++++++++++++++++++++++++++++-
 util/cbfstool/cbfstool.c       |  14 +++++-
 util/cbfstool/coff.h           |  86 +++++++++++++++++++++++++++++++++
 util/cbfstool/common.h         |   2 +
 util/cbfstool/fv.h             |  49 +++++++++++++++++++
 6 files changed, 257 insertions(+), 15 deletions(-)

diff --git a/src/arch/x86/Makefile.inc b/src/arch/x86/Makefile.inc
index fc1e0e0..67789ce 100644
--- a/src/arch/x86/Makefile.inc
+++ b/src/arch/x86/Makefile.inc
@@ -101,19 +101,7 @@ ifeq ($(CONFIG_PAYLOAD_FILO),y)
 endif
 ifeq ($(CONFIG_PAYLOAD_TIANOCORE),y)
 	@printf "    PAYLOAD    Tiano Core (compression: $(CBFS_PAYLOAD_COMPRESS_FLAG))\n"
-	$(eval $(shell \
-		TMPFILE=`mktemp`; \
-		head -c1200 $(CONFIG_TIANOCORE_FILE) | \
-		tail -c1100 > $$TMPFILE && \
-		LC_ALL=C objdump -x $$TMPFILE | \
-		grep .text | while read idx nam size vma lma off align; do \
-			printf "TIANO_ENTRY:=%d " 0x$$vma; \
-			expr `printf "%d - %d - 100" 0x$$vma 0x$$off`; \
-		done && \
-		rm $$TMPFILE))
-	$(eval TIANO_BASE:=$(word 2,$(TIANO_ENTRY)))
-	$(eval TIANO_ENTRY:=$(word 1,$(TIANO_ENTRY)))
-	$(CBFSTOOL) $@.tmp add-flat-binary -f $(CONFIG_TIANOCORE_FILE) -n $(CONFIG_CBFS_PREFIX)/payload -l $(TIANO_BASE) -e $(TIANO_ENTRY) -c $(CBFS_PAYLOAD_COMPRESS_FLAG)
+	$(CBFSTOOL) $@.tmp add-payload -f $(CONFIG_TIANOCORE_FILE) -n $(CONFIG_CBFS_PREFIX)/payload -c $(CBFS_PAYLOAD_COMPRESS_FLAG)
 endif
 ifeq ($(CONFIG_INCLUDE_CONFIG_FILE),y)
 	@printf "    CONFIG     $(DOTCONFIG)\n"
diff --git a/util/cbfstool/cbfs-mkpayload.c b/util/cbfstool/cbfs-mkpayload.c
index 302d506..99142f4 100644
--- a/util/cbfstool/cbfs-mkpayload.c
+++ b/util/cbfstool/cbfs-mkpayload.c
@@ -26,6 +26,8 @@
 #include "common.h"
 #include "cbfs.h"
 #include "elf.h"
+#include "fv.h"
+#include "coff.h"
 
 int parse_elf_to_payload(const struct buffer *input,
 			 struct buffer *output, comp_algo algo)
@@ -43,7 +45,7 @@ int parse_elf_to_payload(const struct buffer *input,
 	int i;
 
 	if(!iself((unsigned char *)input->data)){
-		ERROR("The payload file is not in ELF format!\n");
+		INFO("The payload file is not in ELF format!\n");
 		return -1;
 	}
 
@@ -247,3 +249,106 @@ int parse_flat_binary_to_payload(const struct buffer *input,
 
 	return 0;
 }
+
+int parse_fv_to_payload(const struct buffer *input,
+			 struct buffer *output, comp_algo algo)
+{
+	comp_func_ptr compress;
+	struct cbfs_payload_segment *segs;
+	int doffset, len = 0;
+	firmware_volume_header_t *fv;
+	ffs_file_header_t *fh;
+	common_section_header_t *cs;
+	dos_header_t *dh;
+	coff_header_t *ch;
+	pe_opt_header_t *ph;
+	int dh_offset;
+
+	uint32_t loadaddress;
+	uint32_t entrypoint;
+
+	compress = compression_function(algo);
+	if (!compress)
+		return -1;
+
+	DEBUG("start: parse_fv_to_payload\n");
+
+	fv = (firmware_volume_header_t *)input->data;
+	if (fv->signature != FV_SIGNATURE) {
+		INFO("Not a UEFI firmware volume.\n");
+		return -1;
+	}
+
+	fh = (ffs_file_header_t *)(input->data + fv->header_length);
+	if (fh->file_type != FILETYPE_SEC) {
+		ERROR("Not a usable UEFI firmware volume.\n");
+		return -1;
+	}
+
+	cs = (common_section_header_t *)&fh[1];
+	if (cs->section_type != SECTION_PE32) {
+		ERROR("Not a usable UEFI firmware volume.\n");
+		return -1;
+	}
+
+	dh = (dos_header_t *)&cs[1];
+	if (dh->signature != 0x5a4d) {
+		ERROR("Not a usable UEFI firmware volume.\n");
+		return -1;
+	}
+
+	dh_offset = (unsigned long)dh - (unsigned long)input->data;
+	DEBUG("dos header offset = %x\n", dh_offset);
+
+	ch = (coff_header_t *)(((void *)dh)+dh->e_lfanew);
+	if (ch->machine != 0x14c) {
+		ERROR("Not a usable UEFI firmware volume.\n");
+		return -1;
+	}
+
+	ph = (pe_opt_header_t *)&ch[1];
+	if (ph->signature != 267) {
+		ERROR("Not a usable UEFI firmware volume.\n");
+		return -1;
+	}
+
+	DEBUG("image base %x\n", ph->image_addr);
+	DEBUG("entry point %x\n", ph->entry_point);
+
+	loadaddress = ph->image_addr - dh_offset;
+	entrypoint = ph->image_addr + ph->entry_point;
+
+	if (buffer_create(output, (2 * sizeof(*segs) + input->size),
+			  input->name) != 0)
+		return -1;
+
+	memset(output->data, 0, output->size);
+
+	segs = (struct cbfs_payload_segment *)output->data;
+	doffset = (2 * sizeof(*segs));
+
+	/* Prepare code segment */
+	segs[0].type = PAYLOAD_SEGMENT_CODE;
+	segs[0].load_addr = htonll(loadaddress);
+	segs[0].mem_len = htonl(input->size);
+	segs[0].offset = htonl(doffset);
+
+	compress(input->data, input->size, output->data + doffset, &len);
+	segs[0].compression = htonl(algo);
+	segs[0].len = htonl(len);
+
+	if ((unsigned int)len >= input->size) {
+		WARN("Compressing data would make it bigger - disabled.\n");
+		segs[0].compression = 0;
+		segs[0].len = htonl(input->size);
+		memcpy(output->data + doffset, input->data, input->size);
+	}
+
+	/* prepare entry point segment */
+	segs[1].type = PAYLOAD_SEGMENT_ENTRY;
+	segs[1].load_addr = htonll(entrypoint);
+	output->size = doffset + ntohl(segs[0].len);
+
+	return 0;
+
+}
diff --git a/util/cbfstool/cbfstool.c b/util/cbfstool/cbfstool.c
index 3881628..97fd88d 100644
--- a/util/cbfstool/cbfstool.c
+++ b/util/cbfstool/cbfstool.c
@@ -138,8 +138,20 @@ static int cbfstool_convert_mkstage(struct buffer *buffer, uint32_t *offset) {
 
 static int cbfstool_convert_mkpayload(struct buffer *buffer, uint32_t *offset) {
 	struct buffer output;
-	if (parse_elf_to_payload(buffer, &output, param.algo) != 0)
+	int ret;
+	/* per default, try and see if payload is an ELF binary */
+	ret = parse_elf_to_payload(buffer, &output, param.algo);
+
+	/* If it's not an ELF, see if it's a UEFI FV */
+	if (ret != 0)
+		ret = parse_fv_to_payload(buffer, &output, param.algo);
+
+	/* Not a supported payload type */
+	if (ret != 0) {
+		ERROR("Not a supported payload type (ELF / FV).\n");
 		return -1;
+	}
+
 	buffer_delete(buffer);
 	// direct assign, no dupe.
 	memcpy(buffer, &output, sizeof(*buffer));
diff --git a/util/cbfstool/coff.h b/util/cbfstool/coff.h
new file mode 100644
index 0000000..ebe4538
--- /dev/null
+++ b/util/cbfstool/coff.h
@@ -0,0 +1,86 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * Copyright (C) 2013 Google, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ */
+
+typedef struct {
+	 uint16_t signature;
+	 uint16_t lastsize;
+	 uint16_t nblocks;
+	 uint16_t nreloc;
+	 uint16_t hdrsize;
+	 uint16_t minalloc;
+	 uint16_t maxalloc;
+	 uint16_t ss;
+	 uint16_t sp;
+	 uint16_t checksum;
+	 uint16_t ip;
+	 uint16_t cs;
+	 uint16_t relocpos;
+	 uint16_t noverlay;
+	 uint16_t reserved1[4];
+	 uint16_t oem_id;
+	 uint16_t oem_info;
+	 uint16_t reserved2[10];
+	 uint32_t e_lfanew;
+} dos_header_t;
+
+typedef struct {
+	uint8_t  signature[4];
+	uint16_t machine;
+	uint16_t num_sections;
+	uint32_t timestamp;
+	uint32_t symboltable;
+	uint32_t num_symbols;
+	uint16_t opt_header_size;
+	uint16_t characteristics;
+} coff_header_t;
+
+typedef struct {
+	uint16_t signature;
+	uint8_t  major_linker_version;
+	uint8_t  minor_linker_version;
+	uint32_t code_size;
+	uint32_t data_size;
+	uint32_t bss_size;
+	uint32_t entry_point;
+	uint32_t code_offset;
+	uint32_t data_offset;
+	uint32_t image_addr;
+	uint32_t section_alignment;
+	uint32_t file_alignment;
+	uint16_t major_os_version;
+	uint16_t minor_os_version;
+	uint16_t major_image_version;
+	uint16_t minor_image_version;
+	uint16_t major_subsystem_version;
+	uint16_t minor_subsystem_version;
+	uint32_t reserved;
+	uint32_t image_size;
+	uint32_t header_size;
+	uint32_t checksum;
+	uint16_t subsystem;
+	uint16_t characteristics;
+	uint32_t stack_reserve_size;
+	uint32_t stack_commit_size;
+	uint32_t heap_reserve_size;
+	uint32_t heap_commit_size;
+	uint32_t loader_flags;
+	uint32_t number_of_va_and_sizes;
+	/* data directory not needed */
+} pe_opt_header_t;
+
diff --git a/util/cbfstool/common.h b/util/cbfstool/common.h
index 16c04c9..e197143 100644
--- a/util/cbfstool/common.h
+++ b/util/cbfstool/common.h
@@ -100,6 +100,8 @@ uint64_t intfiletype(const char *name);
 /* cbfs-mkpayload.c */
 int parse_elf_to_payload(const struct buffer *input,
 			 struct buffer *output, comp_algo algo);
+int parse_fv_to_payload(const struct buffer *input,
+			 struct buffer *output, comp_algo algo);
 int parse_flat_binary_to_payload(const struct buffer *input,
 				 struct buffer *output,
 				 uint32_t loadaddress,
diff --git a/util/cbfstool/fv.h b/util/cbfstool/fv.h
new file mode 100644
index 0000000..1ea50e0
--- /dev/null
+++ b/util/cbfstool/fv.h
@@ -0,0 +1,49 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * Copyright (C) 2013 Google, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ */
+
+#define FV_SIGNATURE 0x4856465f
+typedef struct {
+	uint8_t  padding[16];
+	uint8_t  guid[16];
+	uint64_t fv_length;
+	uint32_t signature;
+	uint32_t attributes;
+	uint16_t header_length;
+	uint16_t checksum;
+	uint16_t ext_header_offs;
+	uint8_t  reserved;
+	uint8_t  revision;
+	/* not used here: block map entries */
+} firmware_volume_header_t;
+
+#define FILETYPE_SEC 0x03
+typedef struct {
+	uint8_t  name[16];
+	uint16_t integrity;
+	uint8_t  file_type;
+	uint8_t  attributes;
+	uint8_t  size[3];
+	uint8_t  state;
+} ffs_file_header_t;
+
+#define SECTION_PE32 0x10
+typedef struct {
+	uint8_t size[3];
+	uint8_t section_type;
+} common_section_header_t;



More information about the coreboot mailing list