[coreboot] Patch set updated for coreboot: 8b36963 Improve CBFS tool to support platform without top-aligned mapping.

Hung-Te Lin (hungte@chromium.org) gerrit at coreboot.org
Sun Jan 27 10:03:21 CET 2013


Hung-Te Lin (hungte at chromium.org) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/2194

-gerrit

commit 8b369635ce3b23edf32db9751793ffab2900e5f0
Author: Hung-Te Lin <hungte at chromium.org>
Date:   Sat Jan 26 02:03:28 2013 +0800

    Improve CBFS tool to support platform without top-aligned mapping.
    
    CBFS tool was written with lots of global variables and designed with assumption
    based on x86 architecture. To support other platforms better, we should rewrite
    using logical offset instead of top-aligned memory mapped address.
    
    Also fixed a calculation error in size of last entry (which will cause
    overwriting header data when you really used every bits in CBFS) in old CBFS
    tool.
    
    Usage changes:
    
     - "locate" is changed to "locate-stage" (which is the real behavior in old
       cbfstool) and prefix output with "0x".  The output is now platform
       independent offset.  To retrieve x86 top-aligned virtual address, add "-T".
       "-a" for locate-stage is ignored because it can't be assigned when you really
       do add-stage.
    
     - "create" takes two extra optional params:
        "-b": baseeaddress (offset) for bootblock. When omitted, put bootblock in
              end of ROM.
        "-H": header offset. When omitted, put header right before bootblock,
              and put a top-aligned virtual address reference in end of ROM.
    
     - All address (-b, -H, -o) can now be ROM offset or x86 top-aligned address
       ( > 0x80000000).
    
     - "-m" (architecture) is deprecated because we won't want to list hundreds of
       architecture variants (like ARM soc family).
    
     - "-v" (verbose) now really works.
    
    Verified to work on ARM (google/snow) and X86 (qemu/x86).
    
    Change-Id: I28c737c8f290e51332119188248ac9e28042024c
    Signed-off-by: Hung-Te Lin <hungte at chromium.org>
---
 src/arch/armv7/Makefile.inc    |   13 +-
 src/arch/x86/Makefile.inc      |   12 +-
 util/cbfstool/cbfs-mkpayload.c |   82 ++-
 util/cbfstool/cbfs-mkstage.c   |   39 +-
 util/cbfstool/cbfs.h           |    7 +-
 util/cbfstool/cbfstool.c       |  681 ++++++++++-----------
 util/cbfstool/common.c         | 1315 ++++++++++++++++++++--------------------
 util/cbfstool/common.h         |  198 ++++--
 8 files changed, 1233 insertions(+), 1114 deletions(-)

diff --git a/src/arch/armv7/Makefile.inc b/src/arch/armv7/Makefile.inc
index 872365c..55de481 100644
--- a/src/arch/armv7/Makefile.inc
+++ b/src/arch/armv7/Makefile.inc
@@ -46,9 +46,12 @@ prebuild-files = \
 	$(if $(call extract_nth,4,$(file)),-b $(call extract_nth,4,$(file))) &&)
 prebuilt-files = $(foreach file,$(cbfs-files), $(call extract_nth,1,$(file)))
 
+# TODO(hungte) Change CBFS command to use per-board params. Also we don't need
+# to prefix 0x2000 bytes of zero in boot block anymore!
 $(obj)/coreboot.pre1: $(objcbfs)/bootblock.bin $$(prebuilt-files) $(CBFSTOOL)
-	$(CBFSTOOL) $@.tmp create -m armv7 -s $(CONFIG_COREBOOT_ROMSIZE_KB)K \
-		-B $(objcbfs)/bootblock.bin -a 64
+	$(CBFSTOOL) $@.tmp create -s $(CONFIG_COREBOOT_ROMSIZE_KB)K \
+		-B $(objcbfs)/bootblock.bin -a 64 -b 0x0000 -H 0x2040 \
+		-o 0x5000
 	$(prebuild-files) true
 	mv $@.tmp $@
 else
@@ -314,11 +317,9 @@ $(objgenerated)/romstage_xip.ld: $(objgenerated)/romstage_null.ld $(objcbfs)/bas
 $(objcbfs)/base_xip.txt: $(obj)/coreboot.pre1 $(objcbfs)/romstage_null.bin
 	@printf "    generating base_xip.txt\n"
 	rm -f $@
-	$(CBFSTOOL) $(obj)/coreboot.pre1 locate -f $(objcbfs)/romstage_null.bin -n $(CONFIG_CBFS_PREFIX)/romstage -a $(CONFIG_XIP_ROM_SIZE) > $@.tmp \
+	$(CBFSTOOL) $(obj)/coreboot.pre1 locate-stage -f $(objcbfs)/romstage_null.bin -n $(CONFIG_CBFS_PREFIX)/romstage -a $(CONFIG_XIP_ROM_SIZE) > $@.tmp \
 	 || { echo "The romstage is larger than XIP size. Please expand the CONFIG_XIP_ROM_SIZE" ; exit 1; }
-	sed -e 's/^/0x/g' $@.tmp > $@.tmp2
-	rm $@.tmp
-	mv $@.tmp2 $@
+	mv $@.tmp $@
 
 $(objgenerated)/crt0.romstage.S: $$(crt0s)
 	@printf "    GEN        $(subst $(obj)/,,$(@))\n"
diff --git a/src/arch/x86/Makefile.inc b/src/arch/x86/Makefile.inc
index ea86f8f..30f9008 100644
--- a/src/arch/x86/Makefile.inc
+++ b/src/arch/x86/Makefile.inc
@@ -65,7 +65,7 @@ prebuild-files = \
 prebuilt-files = $(foreach file,$(cbfs-files), $(call extract_nth,1,$(file)))
 
 $(obj)/coreboot.pre1: $(objcbfs)/bootblock.bin $$(prebuilt-files) $(CBFSTOOL)
-	$(CBFSTOOL) $@.tmp create -m x86 -s $(CONFIG_COREBOOT_ROMSIZE_KB)K \
+	$(CBFSTOOL) $@.tmp create -s $(CONFIG_COREBOOT_ROMSIZE_KB)K \
 		-B $(objcbfs)/bootblock.bin -a 64 \
 		-o $$(( $(CONFIG_ROM_SIZE) - $(CONFIG_CBFS_SIZE) ))
 	$(prebuild-files) true
@@ -274,6 +274,9 @@ endif
 $(obj)/coreboot.pre: $(objcbfs)/romstage_xip.elf $(obj)/coreboot.pre1 $(CBFSTOOL)
 	@printf "    CBFS       $(subst $(obj)/,,$(@))\n"
 	cp $(obj)/coreboot.pre1 $@.tmp
+	echo $(CBFSTOOL) $@.tmp add-stage -f $(objcbfs)/romstage_xip.elf \
+		-n $(CONFIG_CBFS_PREFIX)/romstage -c none \
+		-b $(shell cat $(objcbfs)/base_xip.txt) >>cbfs.log
 	$(CBFSTOOL) $@.tmp add-stage -f $(objcbfs)/romstage_xip.elf \
 		-n $(CONFIG_CBFS_PREFIX)/romstage -c none \
 		-b $(shell cat $(objcbfs)/base_xip.txt)
@@ -375,11 +378,10 @@ $(objgenerated)/romstage_xip.ld: $(objgenerated)/romstage_null.ld $(objcbfs)/bas
 
 $(objcbfs)/base_xip.txt: $(obj)/coreboot.pre1 $(objcbfs)/romstage_null.bin
 	rm -f $@
-	$(CBFSTOOL) $(obj)/coreboot.pre1 locate -f $(objcbfs)/romstage_null.bin -n $(CONFIG_CBFS_PREFIX)/romstage -a $(CONFIG_XIP_ROM_SIZE) > $@.tmp \
+	echo $(CBFSTOOL) $(obj)/coreboot.pre1 locate-stage -T -f $(objcbfs)/romstage_null.bin -n $(CONFIG_CBFS_PREFIX)/romstage -a $(CONFIG_XIP_ROM_SIZE) >> cbfs.log
+	$(CBFSTOOL) $(obj)/coreboot.pre1 locate-stage -T -f $(objcbfs)/romstage_null.bin -n $(CONFIG_CBFS_PREFIX)/romstage -a $(CONFIG_XIP_ROM_SIZE) > $@.tmp \
 	 || { echo "The romstage is larger than XIP size. Please expand the CONFIG_XIP_ROM_SIZE" ; exit 1; }
-	sed -e 's/^/0x/g' $@.tmp > $@.tmp2
-	rm $@.tmp
-	mv $@.tmp2 $@
+	mv $@.tmp $@
 
 $(objgenerated)/crt0.romstage.S: $$(crt0s)
 	@printf "    GEN        $(subst $(obj)/,,$(@))\n"
diff --git a/util/cbfstool/cbfs-mkpayload.c b/util/cbfstool/cbfs-mkpayload.c
index 0d3986e..3c19d55 100644
--- a/util/cbfstool/cbfs-mkpayload.c
+++ b/util/cbfstool/cbfs-mkpayload.c
@@ -22,24 +22,18 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <unistd.h>
-#include "elf.h"
-#include <fcntl.h>
-#include <getopt.h>
-#include <sys/stat.h>
 
 #include "common.h"
-#include "cbfs.h"
+#include "elf.h"
 
-int parse_elf_to_payload(unsigned char *input, unsigned char **output,
-			 comp_algo algo)
+int parse_elf_to_payload(char *input, char **output, comp_algo algo)
 {
 	Elf32_Phdr *phdr;
 	Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input;
 	Elf32_Shdr *shdr;
 	char *header;
 	char *strtab;
-	unsigned char *sptr;
+	char *sptr;
 	int headers;
 	int segments = 1;
 	int isize = 0, osize = 0;
@@ -47,21 +41,16 @@ int parse_elf_to_payload(unsigned char *input, unsigned char **output,
 	struct cbfs_payload_segment *segs;
 	int i;
 
-	if(!iself(input)){
+	if(!is_elf_object(input)){
 		fprintf(stderr, "E: The payload file is not in ELF format!\n");
 		return -1;
 	}
 
-	if (!((ehdr->e_machine == EM_ARM) && (arch == CBFS_ARCHITECTURE_ARMV7)) &&
-	    !((ehdr->e_machine == EM_386) && (arch == CBFS_ARCHITECTURE_X86))) {
-		fprintf(stderr, "E: The payload file has the wrong architecture\n");
-		return -1;
-	}
-
 	comp_func_ptr compress = compression_function(algo);
 	if (!compress)
 		return -1;
 
+	DEBUG("start: parse_elf_to_payload\n");
 	headers = ehdr->e_phnum;
 	header = (char *)ehdr;
 
@@ -156,9 +145,9 @@ int parse_elf_to_payload(unsigned char *input, unsigned char **output,
 		if (phdr[i].p_filesz == 0) {
 			segs[segments].type = PAYLOAD_SEGMENT_BSS;
 			segs[segments].load_addr =
-			    (uint64_t)htonll(phdr[i].p_paddr);
+			    htonll(phdr[i].p_paddr);
 			segs[segments].mem_len =
-			    (uint32_t)htonl(phdr[i].p_memsz);
+			    htonl(phdr[i].p_memsz);
 			segs[segments].offset = htonl(doffset);
 
 			segments++;
@@ -169,8 +158,8 @@ int parse_elf_to_payload(unsigned char *input, unsigned char **output,
 			segs[segments].type = PAYLOAD_SEGMENT_CODE;
 		else
 			segs[segments].type = PAYLOAD_SEGMENT_DATA;
-		segs[segments].load_addr = (uint64_t)htonll(phdr[i].p_paddr);
-		segs[segments].mem_len = (uint32_t)htonl(phdr[i].p_memsz);
+		segs[segments].load_addr = htonll(phdr[i].p_paddr);
+		segs[segments].mem_len = htonl(phdr[i].p_memsz);
 		segs[segments].compression = htonl(algo);
 		segs[segments].offset = htonl(doffset);
 
@@ -197,7 +186,7 @@ int parse_elf_to_payload(unsigned char *input, unsigned char **output,
 	}
 
 	segs[segments].type = PAYLOAD_SEGMENT_ENTRY;
-	segs[segments++].load_addr = (uint64_t)htonll(ehdr->e_entry);
+	segs[segments++].load_addr = htonll(ehdr->e_entry);
 
 	*output = sptr;
 
@@ -206,3 +195,54 @@ int parse_elf_to_payload(unsigned char *input, unsigned char **output,
       err:
 	return -1;
 }
+
+int parse_flat_binary_to_payload(char *input,
+				 char **output,
+				 uint32_t input_size,
+				 uint32_t loadaddress,
+				 uint32_t entrypoint,
+				 comp_algo algo)
+{
+	comp_func_ptr compress;
+	char *payload;
+	struct cbfs_payload_segment *segs;
+	int doffset, len = 0;
+
+	compress = compression_function(algo);
+	if (!compress)
+		return 1;
+
+	DEBUG("start: parse_flat_binary_to_payload\n");
+	/* FIXME compressed file size might be bigger than original file */
+	payload = calloc((2 * sizeof(*segs)) + input_size, 1);
+	if (payload == NULL) {
+		fprintf(stderr, "E: Could not allocate memory.\n");
+		return 1;
+	}
+
+	segs = (struct cbfs_payload_segment *)payload;
+	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, input_size, (char *)(payload + doffset), &len);
+	segs[0].compression = htonl(algo);
+	segs[0].len = htonl(len);
+
+	if ((unsigned int)len >= input_size) {
+		segs[0].compression = 0;
+		segs[0].len = htonl(input_size);
+		memcpy(payload + doffset, input, input_size);
+	}
+
+	/* prepare entry point segment */
+	segs[1].type = PAYLOAD_SEGMENT_ENTRY;
+	segs[1].load_addr = htonll(entrypoint);
+
+	*output = payload;
+	return doffset + ntohl(segs[0].len);
+}
diff --git a/util/cbfstool/cbfs-mkstage.c b/util/cbfstool/cbfs-mkstage.c
index 0541d4b..9e47047 100644
--- a/util/cbfstool/cbfs-mkstage.c
+++ b/util/cbfstool/cbfs-mkstage.c
@@ -23,14 +23,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <unistd.h>
-#include "elf.h"
-#include <fcntl.h>
-#include <getopt.h>
-#include <sys/stat.h>
 
 #include "common.h"
-#include "cbfs.h"
+#include "elf.h"
 
 static unsigned int idemp(unsigned int x)
 {
@@ -48,13 +43,13 @@ static unsigned int swap32(unsigned int x)
 unsigned int (*elf32_to_native) (unsigned int) = idemp;
 
 /* returns size of result, or -1 if error */
-int parse_elf_to_stage(unsigned char *input, unsigned char **output,
-		       comp_algo algo, uint32_t * location)
+int parse_elf_to_stage(char *input, char **output,
+		       comp_algo algo, uint32_t location)
 {
 	Elf32_Phdr *phdr;
 	Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input;
 	char *header, *buffer;
-	unsigned char *out;
+	char *out;
 
 	int headers;
 	int i;
@@ -67,21 +62,16 @@ int parse_elf_to_stage(unsigned char *input, unsigned char **output,
 	if (!compress)
 		return -1;
 
-	if (!iself(input)) {
+	DEBUG("start: parse_elf_to_stage(location=0x%x)\n", location);
+	if (!is_elf_object(input)) {
 		fprintf(stderr, "E: The stage file is not in ELF format!\n");
 		return -1;
 	}
 
-	if (!((ehdr->e_machine == EM_ARM) && (arch == CBFS_ARCHITECTURE_ARMV7)) &&
-	    !((ehdr->e_machine == EM_386) && (arch == CBFS_ARCHITECTURE_X86))) {
-		fprintf(stderr, "E: The stage file has the wrong architecture\n");
-		return -1;
-	}
-
 	if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
 		elf_bigendian = 1;
 	}
-	if (elf_bigendian != host_bigendian) {
+	if (elf_bigendian != is_big_endian()) {
 		elf32_to_native = swap32;
 	}
 
@@ -125,8 +115,8 @@ int parse_elf_to_stage(unsigned char *input, unsigned char **output,
 			mem_end = mend;
 	}
 
-	if (data_start < *location) {
-		data_start = *location;
+	if (data_start < location) {
+		data_start = location;
 	}
 
 	if (data_end <= data_start) {
@@ -155,9 +145,9 @@ int parse_elf_to_stage(unsigned char *input, unsigned char **output,
 			continue;
 
 		l_start = elf32_to_native(phdr[i].p_paddr);
-		if (l_start < *location) {
-			l_offset = *location - l_start;
-			l_start = *location;
+		if (l_start < location) {
+			l_offset = location - l_start;
+			l_start = location;
 		}
 
 		memcpy(buffer + (l_start - data_start),
@@ -184,10 +174,7 @@ int parse_elf_to_stage(unsigned char *input, unsigned char **output,
 		 (char *)(out + sizeof(struct cbfs_stage)), (int *)&stage->len);
 
 	free(buffer);
-
 	*output = out;
 
-	if (*location)
-		*location -= sizeof(struct cbfs_stage);
-	return sizeof(struct cbfs_stage) + stage->len;
+	return sizeof(*stage) + stage->len;
 }
diff --git a/util/cbfstool/cbfs.h b/util/cbfstool/cbfs.h
index 3dbeefd..26ab62f 100644
--- a/util/cbfstool/cbfs.h
+++ b/util/cbfstool/cbfs.h
@@ -25,7 +25,9 @@
 #define CBFS_HEADPTR_ADDR_X86 0xFFFFFFFC
 #define CBFS_HEADER_VERSION1 0x31313131
 #define CBFS_HEADER_VERSION2 0x31313132
-#define CBFS_HEADER_VERSION  CBFS_HEADER_VERSION2
+// TODO(hungte) We probably don't need "architecture" anymore - remove in future
+// Right now, to make old cbfstool happy, declare version as 1.
+#define CBFS_HEADER_VERSION  CBFS_HEADER_VERSION1
 
 struct cbfs_header {
 	uint32_t magic;
@@ -42,6 +44,8 @@ struct cbfs_header {
 #define CBFS_ARCHITECTURE_X86      0x00000001
 #define CBFS_ARCHITECTURE_ARMV7    0x00000010
 
+#define CBFS_FILE_MAGIC "LARCHIVE"
+
 struct cbfs_file {
 	uint8_t magic[8];
 	uint32_t len;
@@ -106,6 +110,7 @@ struct cbfs_payload {
 #define CBFS_COMPONENT_NULL 0xFFFFFFFF
 
 int cbfs_file_header(unsigned long physaddr);
+#define CBFS_NAME(_c) (((char *) (_c)) + sizeof(struct cbfs_file))
 #define CBFS_SUBHEADER(_p) ( (void *) ((((uint8_t *) (_p)) + ntohl((_p)->offset))) )
 
 struct cbfs_file *cbfs_create_empty_file(uint32_t physaddr, uint32_t size);
diff --git a/util/cbfstool/cbfstool.c b/util/cbfstool/cbfstool.c
index 4202801..d3853ef 100644
--- a/util/cbfstool/cbfstool.c
+++ b/util/cbfstool/cbfstool.c
@@ -4,6 +4,7 @@
  * Copyright (C) 2009 coresystems GmbH
  *                 written by Patrick Georgi <patrick.georgi at coresystems.de>
  * Copyright (C) 2012 Google, Inc.
+ * Copyright (C) 2013 The ChromiumOS Authors.  All rights reserved.
  *
  * 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
@@ -19,6 +20,7 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
  */
 
+#include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -26,7 +28,8 @@
 #include <unistd.h>
 #include <getopt.h>
 #include "common.h"
-#include "cbfs.h"
+
+int verbose = 0;
 
 struct command {
 	const char *name;
@@ -34,438 +37,396 @@ struct command {
 	int (*function) (void);
 };
 
-int verbose = 0;
-static char *cbfs_name = NULL;
-static char *rom_name = NULL;
-static char *rom_filename = NULL;
-static char *rom_bootblock = NULL;
-static uint32_t rom_type = 0;
-static uint32_t rom_baseaddress = 0;
-static uint32_t rom_loadaddress = 0;
-static uint32_t rom_entrypoint = 0;
-static uint32_t rom_size = 0;
-static uint32_t rom_alignment = 0;
-static uint32_t rom_offset = 0;
-static comp_algo rom_algo = CBFS_COMPRESS_NONE;
-
-static int cbfs_add(void)
-{
-	uint32_t filesize = 0;
-	void *rom, *filedata, *cbfsfile;
-
-	if (!rom_filename) {
+struct param {
+	char *cbfs_name;
+	char *name;
+	char *filename;
+	char *bootblock_file;
+	uint32_t type;
+	uint32_t base_address;
+	uint32_t base_address_assigned;
+	uint32_t load_address;
+	uint32_t header_offset;
+	uint32_t header_offset_assigned;
+	uint32_t entrypoint;
+	uint32_t size;
+	uint32_t alignment;
+	uint32_t offset;
+	uint32_t top_aligned;
+	comp_algo algo;
+} param;
+
+typedef int (*convert_buffer_t)(struct buffer *buffer);
+
+static int cbfstool_add_file(const char *cbfs_name,
+			     const char *filename,
+			     const char *name,
+			     uint32_t type,
+			     uint32_t offset,
+			     convert_buffer_t convert) {
+	struct cbfs_image image;
+	struct buffer buffer;
+	int result = 1;
+
+	if (!filename) {
 		fprintf(stderr, "E: You need to specify -f/--filename.\n");
 		return 1;
 	}
 
-	if (!rom_name) {
+	if (!name) {
 		fprintf(stderr, "E: You need to specify -n/--name.\n");
 		return 1;
 	}
 
-	if (rom_type == 0) {
+	if (type == 0) {
 		fprintf(stderr, "E: You need to specify a valid -t/--type.\n");
 		return 1;
 	}
 
-	rom = loadrom(cbfs_name);
-	if (rom == NULL) {
+	if (cbfs_image_from_file(&image, cbfs_name) != 0) {
 		fprintf(stderr, "E: Could not load ROM image '%s'.\n",
 			cbfs_name);
 		return 1;
 	}
 
-	filedata = loadfile(rom_filename, &filesize, 0, SEEK_SET);
-	if (filedata == NULL) {
-		fprintf(stderr, "E: Could not load file '%s'.\n",
-			rom_filename);
-		free(rom);
+	if (cbfs_get_entry(&image, name)) {
+		fprintf(stderr, "E: '%s' already in ROM image.\n", name);
 		return 1;
 	}
 
-	cbfsfile = create_cbfs_file(rom_name, filedata, &filesize,
-					rom_type, &rom_baseaddress);
-	free(filedata);
-
-	if (add_file_to_cbfs(cbfsfile, filesize, rom_baseaddress)) {
-		fprintf(stderr, "E: Adding file '%s' failed.\n", rom_filename);
-		free(cbfsfile);
-		free(rom);
-		return 1;
-	}
-	if (writerom(cbfs_name, rom, romsize)) {
-		free(cbfsfile);
-		free(rom);
+	if (buffer_from_file(&buffer, filename) != 0) {
+		fprintf(stderr, "E: Could not load file '%s'.\n", filename);
+		cbfs_image_delete(&image);
 		return 1;
 	}
 
-	free(cbfsfile);
-	free(rom);
-	return 0;
-}
+	do {
+		if (convert && convert(&buffer) != 0) {
+			fprintf(stderr, "E: Failed to parse file '%s'.\n",
+				filename);
+			break;
+		}
+		if (cbfs_add_entry(&image, &buffer, name, type, offset) != 0)
+			break;
+		if (cbfs_image_write_file(&image, cbfs_name) != 0)
+			break;
 
-static int cbfs_add_payload(void)
-{
-	int32_t filesize = 0;
-	void *rom, *filedata, *cbfsfile;
-	unsigned char *payload;
+		result = 0;
+	} while (0);
 
-	if (!rom_filename) {
-		fprintf(stderr, "E: You need to specify -f/--filename.\n");
-		return 1;
-	}
+	buffer_delete(&buffer);
+	cbfs_image_delete(&image);
+	return result;
+}
 
-	if (!rom_name) {
-		fprintf(stderr, "E: You need to specify -n/--name.\n");
-		return 1;
-	}
+static int cbfs_convert_mkstage(struct buffer *buffer) {
+	char *name;
+	char *output;
+	int result = parse_elf_to_stage(buffer->data, &output, param.algo,
+					param.base_address);
+	if (result < 0)
+		return -1;
+	name = strdup(buffer->name);
+	buffer_delete(buffer);
+	buffer->name = name;
+	buffer->size = (size_t)result;
+	buffer->data = (char*)output;
+	return 0;
+}
 
-	rom = loadrom(cbfs_name);
-	if (rom == NULL) {
-		fprintf(stderr, "E: Could not load ROM image '%s'.\n",
-			cbfs_name);
-		return 1;
-	}
+static int cbfs_convert_elf_mkpayload(struct buffer *buffer) {
+	char *name;
+	char *output;
+	int result = parse_elf_to_payload(buffer->data, &output,
+					  param.algo);
+	if (result < 0)
+		return -1;
+	name = strdup(buffer->name);
+	buffer_delete(buffer);
+	buffer->name = name;
+	buffer->size = (size_t)result;
+	buffer->data = (char*)output;
+	return 0;
+}
 
-	filedata = loadfile(rom_filename, &filesize, 0, SEEK_SET);
-	if (filedata == NULL) {
-		fprintf(stderr, "E: Could not load file '%s'.\n",
-			rom_filename);
-		free(rom);
-		return 1;
-	}
+static int cbfs_convert_flat_mkpayload(struct buffer *buffer) {
+	char *name;
+	char *output;
+	int result;
+	result = parse_flat_binary_to_payload(buffer->data, &output,
+					      buffer->size,
+					      param.load_address,
+					      param.entrypoint,
+					      param.algo);
+	if (result < 0)
+		return -1;
+	name = strdup(buffer->name);
+	buffer_delete(buffer);
+	buffer->name = name;
+	buffer->size = (size_t)result;
+	buffer->data = (char*)output;
+	return 0;
+}
 
-	filesize = parse_elf_to_payload(filedata, &payload, rom_algo);
-	if (filesize <= 0) {
-		fprintf(stderr, "E: Adding payload '%s' failed.\n",
-			rom_filename);
-		free(rom);
-		return 1;
-	}
+static int cbfstool_add(void)
+{
+	return cbfstool_add_file(param.cbfs_name,
+				 param.filename,
+				 param.name,
+				 param.type,
+				 param.base_address,
+				 NULL);
+}
 
-	cbfsfile = create_cbfs_file(rom_name, payload, &filesize,
-				CBFS_COMPONENT_PAYLOAD, &rom_baseaddress);
 
-	free(filedata);
-	free(payload);
+static int cbfstool_add_payload(void)
+{
+	return cbfstool_add_file(param.cbfs_name,
+				 param.filename,
+				 param.name,
+				 CBFS_COMPONENT_PAYLOAD,
+				 param.base_address,
+				 cbfs_convert_elf_mkpayload);
+}
+
+static int cbfstool_add_stage(void)
+{
+	return cbfstool_add_file(param.cbfs_name,
+				 param.filename,
+				 param.name,
+				 CBFS_COMPONENT_STAGE,
+				 param.base_address,
+				 cbfs_convert_mkstage);
+}
 
-	if (add_file_to_cbfs(cbfsfile, filesize, rom_baseaddress)) {
-		fprintf(stderr, "E: Adding payload '%s' failed.\n",
-			rom_filename);
-		free(cbfsfile);
-		free(rom);
+static int cbfstool_add_flat_binary(void)
+{
+	if (param.load_address == 0) {
+		fprintf(stderr, "E: You need to specify a valid "
+			"-l/--load-address.\n");
 		return 1;
 	}
-
-	if (writerom(cbfs_name, rom, romsize)) {
-		free(cbfsfile);
-		free(rom);
+	if (param.entrypoint == 0) {
+		fprintf(stderr, "E: You need to specify a valid "
+			"-e/--entry-point.\n");
 		return 1;
 	}
-
-	free(cbfsfile);
-	free(rom);
-	return 0;
+	return cbfstool_add_file(param.cbfs_name,
+				 param.filename,
+				 param.name,
+				 CBFS_COMPONENT_PAYLOAD,
+				 param.base_address,
+				 cbfs_convert_flat_mkpayload);
 }
 
-static int cbfs_add_stage(void)
+static int cbfstool_remove(void)
 {
-	uint32_t filesize = 0;
-	void *rom, *filedata, *cbfsfile;
-	unsigned char *stage;
+	int result = 1;
+	struct cbfs_image image;
 
-	if (!rom_filename) {
-		fprintf(stderr, "E: You need to specify -f/--filename.\n");
-		return 1;
-	}
-
-	if (!rom_name) {
+	if (!param.name) {
 		fprintf(stderr, "E: You need to specify -n/--name.\n");
 		return 1;
 	}
 
-	rom = loadrom(cbfs_name);
-	if (rom == NULL) {
+	if (cbfs_image_from_file(&image, param.cbfs_name) != 0) {
 		fprintf(stderr, "E: Could not load ROM image '%s'.\n",
-			cbfs_name);
+			param.cbfs_name);
 		return 1;
 	}
 
-	filedata = loadfile(rom_filename, &filesize, 0, SEEK_SET);
-	if (filedata == NULL) {
-		fprintf(stderr, "E: Could not load file '%s'.\n",
-			rom_filename);
-		free(rom);
-		return 1;
-	}
-
-	filesize = parse_elf_to_stage(filedata, &stage, rom_algo, &rom_baseaddress);
-
-	cbfsfile = create_cbfs_file(rom_name, stage, &filesize,
-				CBFS_COMPONENT_STAGE, &rom_baseaddress);
-
-	free(filedata);
-	free(stage);
-
-	if (add_file_to_cbfs(cbfsfile, filesize, rom_baseaddress)) {
-		fprintf(stderr, "E: Adding stage '%s' failed.\n",
-			rom_filename);
-		free(cbfsfile);
-		free(rom);
-		return 1;
-	}
-
-	if (writerom(cbfs_name, rom, romsize)) {
-		free(cbfsfile);
-		free(rom);
-		return 1;
-	}
+	do {
+		if (cbfs_remove_entry(&image, param.name) != 0) {
+			fprintf(stderr, "E: Removing file '%s' failed.\n",
+				param.name);
+			break;
+		}
+		if (cbfs_image_write_file(&image, param.cbfs_name) != 0)
+			break;
+		result = 0;
+	} while (0);
 
-	free(cbfsfile);
-	free(rom);
-	return 0;
+	cbfs_image_delete(&image);
+	return result;
 }
 
-static int cbfs_add_flat_binary(void)
+static int cbfstool_create(void)
 {
-	uint32_t filesize = 0;
-	uint32_t final_size;
-	void *rom, *filedata, *cbfsfile;
-	unsigned char *payload;
-	comp_func_ptr compress;
-	struct cbfs_payload_segment *segs;
-	int doffset, len = 0;
-
-	if (!rom_filename) {
-		fprintf(stderr, "E: You need to specify -f/--filename.\n");
-		return 1;
-	}
+	struct cbfs_image image;
+	struct buffer bootblock;
 
-	if (!rom_name) {
-		fprintf(stderr, "E: You need to specify -n/--name.\n");
-		return 1;
-	}
-
-	if (rom_loadaddress == 0) {
-		fprintf(stderr, "E: You need to specify a valid "
-			"-l/--load-address.\n");
-		return 1;
-	}
-
-	if (rom_entrypoint == 0) {
-		fprintf(stderr, "E: You need to specify a valid "
-			"-e/--entry-point.\n");
+	if (param.size == 0) {
+		fprintf(stderr, "E: You need to specify a valid -s/--size.\n");
 		return 1;
 	}
 
-	compress = compression_function(rom_algo);
-	if (!compress)
-		return 1;
-
-	rom = loadrom(cbfs_name);
-	if (rom == NULL) {
-		fprintf(stderr, "E: Could not load ROM image '%s'.\n",
-			cbfs_name);
+	if (!param.bootblock_file) {
+		fprintf(stderr, "E: You need to specify -b/--bootblock.\n");
 		return 1;
 	}
 
-	filedata = loadfile(rom_filename, &filesize, 0, SEEK_SET);
-	if (filedata == NULL) {
-		fprintf(stderr, "E: Could not load file '%s'.\n",
-			rom_filename);
-		free(rom);
+	if (buffer_from_file(&bootblock, param.bootblock_file) != 0) {
 		return 1;
 	}
 
-	/* FIXME compressed file size might be bigger than original file */
-	payload = calloc((2 * sizeof(struct cbfs_payload_segment)) + filesize, 1);
-	if (payload == NULL) {
-		fprintf(stderr, "E: Could not allocate memory.\n");
-		free(filedata);
-		free(rom);
-		return 1;
+	// Setup default boot offset and header offset.
+	if (!param.base_address_assigned) {
+		// put boot block before end of ROM.
+		param.base_address = param.size - bootblock.size;
+		DEBUG("bootblock in end of ROM.\n");
 	}
-
-	segs = (struct cbfs_payload_segment *)payload;
-	doffset = (2 * sizeof(struct cbfs_payload_segment));
-
-	/* Prepare code segment */
-	segs[0].type = PAYLOAD_SEGMENT_CODE;
-	segs[0].load_addr = (uint64_t)htonll(rom_loadaddress);
-	segs[0].mem_len = (uint32_t)htonl(filesize);
-	segs[0].offset = (uint32_t)htonl(doffset);
-
-	compress(filedata, filesize, (char *)(payload + doffset), &len);
-	segs[0].compression = htonl(rom_algo);
-	segs[0].len = htonl(len);
-
-	if ((unsigned int)len >= filesize) {
-		segs[0].compression = 0;
-		segs[0].len = htonl(filesize);
-		memcpy((char *)(payload + doffset), filedata, filesize);
+	if (!param.header_offset_assigned) {
+		// Put header before bootblock, and make a reference in end of
+		// bootblock.
+		param.header_offset = (
+				param.base_address -
+				sizeof(struct cbfs_header));
+		if (bootblock.size >= sizeof(uint32_t)) {
+			// TODO this only works for 32b top-aligned system now...
+			uint32_t ptr = param.header_offset - param.size;
+			uint32_t *sig = (uint32_t*)(bootblock.data +
+						    bootblock.size -
+						    sizeof(ptr));
+			*sig = ptr;
+			DEBUG("CBFS header reference in end of bootblock.\n");
+		}
 	}
 
-	/* prepare entry point segment */
-	segs[1].type = PAYLOAD_SEGMENT_ENTRY;
-	segs[1].load_addr = (uint64_t)htonll(rom_entrypoint);
-
-	final_size = doffset + ntohl(segs[0].len);
-	cbfsfile =
-	    create_cbfs_file(rom_name, payload, &final_size,
-			     CBFS_COMPONENT_PAYLOAD, &rom_baseaddress);
-
-	free(filedata);
-	free(payload);
-
-	if (add_file_to_cbfs(cbfsfile, final_size, rom_baseaddress)) {
-		fprintf(stderr, "E: Adding payload '%s' failed.\n",
-			rom_filename);
-		free(cbfsfile);
-		free(rom);
+	if (cbfs_image_create(&image,
+			      param.size,
+			      param.alignment,
+			      &bootblock,
+			      param.base_address,
+			      param.header_offset,
+			      param.offset) != 0) {
+		fprintf(stderr, "E: Failed to create %s.\n", param.cbfs_name);
 		return 1;
 	}
-	if (writerom(cbfs_name, rom, romsize)) {
-		free(cbfsfile);
-		free(rom);
+	if (cbfs_image_write_file(&image, param.cbfs_name) != 0) {
+		fprintf(stderr, "E: Failed to write %s.\n", param.cbfs_name);
 		return 1;
 	}
-
-	free(cbfsfile);
-	free(rom);
+	cbfs_image_delete(&image);
 	return 0;
+
 }
 
-static int cbfs_remove(void)
+static int cbfstool_locate_stage(void)
 {
-	void *rom;
+	struct cbfs_image image;
+	struct cbfs_file *entry;
+	struct buffer buffer;
+	size_t stage_size;
+	uint32_t address;
 
-	if (!rom_name) {
-		fprintf(stderr, "E: You need to specify -n/--name.\n");
+	if (!param.filename) {
+		fprintf(stderr, "E: You need to specify -f/--filename.\n");
 		return 1;
 	}
 
-	rom = loadrom(cbfs_name);
-	if (rom == NULL) {
-		fprintf(stderr, "E: Could not load ROM image '%s'.\n",
-			cbfs_name);
+	if (!param.name) {
+		fprintf(stderr, "E: You need to specify -n/--name.\n");
 		return 1;
 	}
 
-	if (remove_file_from_cbfs(rom_name)) {
-		fprintf(stderr, "E: Removing file '%s' failed.\n",
-			rom_name);
-		free(rom);
-		return 1;
-	}
+	if (param.alignment)
+		WARN("-a is deprecated because you can't align in add-stage.\n");
 
-	if (writerom(cbfs_name, rom, romsize)) {
-		free(rom);
+	if (cbfs_image_from_file(&image, param.cbfs_name) != 0) {
+		fprintf(stderr, "E: Failed to load %s.\n", param.cbfs_name);
 		return 1;
 	}
 
-	free(rom);
-	return 0;
-}
-
-static int cbfs_create(void)
-{
-	if (rom_size == 0) {
-		fprintf(stderr, "E: You need to specify a valid -s/--size.\n");
+	if (buffer_from_file(&buffer, param.filename) != 0) {
+		fprintf(stderr, "E: Cannot load %s.\n", param.filename);
+		cbfs_image_delete(&image);
 		return 1;
 	}
 
-	if (!rom_bootblock) {
-		fprintf(stderr, "E: You need to specify -b/--bootblock.\n");
-		return 1;
-	}
+	// Increase buffer for cbfs_stage header.
+	stage_size = buffer.size + sizeof(struct cbfs_stage);
+	buffer_delete(&buffer);
+	buffer_create(&buffer, stage_size, param.name);
 
-	if (arch == CBFS_ARCHITECTURE_UNKNOWN) {
-		fprintf(stderr, "E: You need to specify -m/--machine arch\n");
+	if (cbfs_get_entry(&image, param.name)) {
+		fprintf(stderr, "E: '%s' already in CBFS.\n", param.name);
+		buffer_delete(&buffer);
+		cbfs_image_delete(&image);
 		return 1;
 	}
 
-	return create_cbfs_image(cbfs_name, rom_size, rom_bootblock,
-						rom_alignment, rom_offset);
-}
-
-static int cbfs_locate(void)
-{
-	uint32_t filesize, location;
-
-	if (!rom_filename) {
-		fprintf(stderr, "E: You need to specify -f/--filename.\n");
+	if (cbfs_add_entry(&image, &buffer, param.name,
+			   CBFS_COMPONENT_STAGE, 0) != 0) {
+		buffer_delete(&buffer);
+		cbfs_image_delete(&image);
 		return 1;
 	}
+	entry = cbfs_get_entry(&image, param.name);
+	assert(entry);
+	assert(cbfs_is_valid_entry(entry));
+	address = (cbfs_get_entry_addr(&image, entry) + ntohl(entry->offset) +
+		   sizeof(struct cbfs_stage));
 
-	if (!rom_name) {
-		fprintf(stderr, "E: You need to specify -n/--name.\n");
-		return 1;
+	if (param.top_aligned) {
+		address = address - ntohl(image.header->romsize);
 	}
-
-	filesize = getfilesize(rom_filename);
-
-	location = cbfs_find_location(cbfs_name, filesize,
-					rom_name, rom_alignment);
-
-	printf("%x\n", location);
-	return location == 0 ? 1 : 0;
+	buffer_delete(&buffer);
+	cbfs_image_delete(&image);
+	printf("0x%x\n", address);
+	return 0;
 }
 
-static int cbfs_print(void)
+static int cbfstool_print(void)
 {
-	void *rom;
-
-	rom = loadrom(cbfs_name);
-	if (rom == NULL) {
+	struct cbfs_image image;
+	if (cbfs_image_from_file(&image, param.cbfs_name) != 0) {
 		fprintf(stderr, "E: Could not load ROM image '%s'.\n",
-			cbfs_name);
+			param.cbfs_name);
 		return 1;
 	}
-
-	print_cbfs_directory(cbfs_name);
-
-	free(rom);
+	cbfs_print_directory(&image);
+	cbfs_image_delete(&image);
 	return 0;
 }
 
-static int cbfs_extract(void)
+static int cbfstool_extract(void)
 {
-	void *rom;
-	int ret;
+	int result = 0;
+	struct cbfs_image image;
 
-	if (!rom_filename) {
+	if (!param.filename) {
 		fprintf(stderr, "E: You need to specify -f/--filename.\n");
 		return 1;
 	}
 
-	if (!rom_name) {
+	if (!param.name) {
 		fprintf(stderr, "E: You need to specify -n/--name.\n");
 		return 1;
 	}
 
-	rom = loadrom(cbfs_name);
-	if (rom == NULL) {
+	if (cbfs_image_from_file(&image, param.cbfs_name) != 0) {
 		fprintf(stderr, "E: Could not load ROM image '%s'.\n",
-			cbfs_name);
-		return 1;
+			param.cbfs_name);
+		result = 1;
+	} else if (cbfs_export_entry(&image, param.name,
+				     param.filename) != 0) {
+		result = 1;
 	}
 
-	ret = extract_file_from_cbfs(cbfs_name, rom_name, rom_filename);
-
-	free(rom);
-	return ret;
+	cbfs_image_delete(&image);
+	return result;
 }
 
 static const struct command commands[] = {
-	{"add", "f:n:t:b:h?", cbfs_add},
-	{"add-payload", "f:n:t:c:b:h?", cbfs_add_payload},
-	{"add-stage", "f:n:t:c:b:h?", cbfs_add_stage},
-	{"add-flat-binary", "f:n:l:e:c:b:h?", cbfs_add_flat_binary},
-	{"remove", "n:h?", cbfs_remove},
-	{"create", "s:B:a:o:m:h?", cbfs_create},
-	{"locate", "f:n:a:h?", cbfs_locate},
-	{"print", "h?", cbfs_print},
-	{"extract", "n:f:h?", cbfs_extract},
+	{"add", "vf:n:t:b:h?", cbfstool_add},
+	{"add-payload", "vf:n:t:c:b:h?", cbfstool_add_payload},
+	{"add-stage", "vf:n:t:c:b:h?", cbfstool_add_stage},
+	{"add-flat-binary", "vf:n:l:e:c:b:h?", cbfstool_add_flat_binary},
+	{"remove", "vn:h?", cbfstool_remove},
+	{"create", "vs:B:b:H:a:o:m:h?", cbfstool_create},
+	{"locate-stage", "vTf:n:a:h?", cbfstool_locate_stage},
+	{"print", "vh?", cbfstool_print},
+	{"extract", "vn:f:h?", cbfstool_extract},
 };
 
 static struct option long_options[] = {
@@ -477,10 +438,12 @@ static struct option long_options[] = {
 	{"entry-point",  required_argument, 0, 'e' },
 	{"size",         required_argument, 0, 's' },
 	{"bootblock",    required_argument, 0, 'B' },
+	{"header-offset",required_argument, 0, 'H' },
 	{"alignment",    required_argument, 0, 'a' },
 	{"offset",       required_argument, 0, 'o' },
 	{"file",         required_argument, 0, 'f' },
 	{"arch",         required_argument, 0, 'm' },
+	{"top-aligned",  no_argument,       0, 'T' },
 	{"verbose",      no_argument,       0, 'v' },
 	{"help",         no_argument,       0, 'h' },
 	{NULL,           0,                 0,  0  }
@@ -491,10 +454,12 @@ static void usage(char *name)
 	printf
 	    ("cbfstool: Management utility for CBFS formatted ROM images\n\n"
 	     "USAGE:\n" " %s [-h]\n"
-	     " %s FILE COMMAND [PARAMETERS]...\n\n" "OPTIONs:\n"
-	     "  -h		Display this help message\n\n"
+	     " %s FILE COMMAND [-v] [PARAMETERS]...\n\n" "OPTIONs:\n"
+	     "  -h             Display this help message\n\n"
+	     "  -T             Display top-aligned address instead of offset\n"
+	     "  -v             Verbose output\n"
 	     "COMMANDs:\n"
-	     " add -f FILE -n NAME -t TYPE [-b base-address]               "
+	     " add -f FILE -n NAME -t TYPE [-b base]                       "
 			"Add a component\n"
 	     " add-payload -f FILE -n NAME [-c compression] [-b base]      "
 			"Add a payload to the ROM\n"
@@ -505,10 +470,11 @@ static void usage(char *name)
 			"Add a 32bit flat mode binary\n"
 	     " remove -n NAME                                              "
 			"Remove a component\n"
-	     " create -s size -B bootblock -m ARCH [-a align] [-o offset]  "
+	     " create -s size -B bootblock [-b base] [-a align] \\\n"
+	     "         [-o offset] [-H header-offset]                      "
 			"Create a ROM file\n"
-	     " locate -f FILE -n NAME -a align                             "
-			"Find a place for a file of that size\n"
+	     " locate-stage -f FILE -n NAME [-T]                           "
+			"Find the entry if the stage component is added\n"
 	     " print                                                       "
 			"Show the contents of the ROM\n"
 	     " extract -n NAME -f FILE                                     "
@@ -518,19 +484,7 @@ static void usage(char *name)
 	     "  armv7, x86\n"
 	     "TYPEs:\n", name, name
 	    );
-	print_supported_filetypes();
-}
-
-/* Small, OS/libc independent runtime check for endianess */
-int host_bigendian = 0;
-
-static void which_endian(void)
-{
-	static const uint32_t inttest = 0x12345678;
-	uint8_t inttest_lsb = *(uint8_t *)&inttest;
-	if (inttest_lsb == 0x12) {
-		host_bigendian = 1;
-	}
+	print_all_cbfs_entry_types();
 }
 
 int main(int argc, char **argv)
@@ -543,9 +497,7 @@ int main(int argc, char **argv)
 		return 1;
 	}
 
-	which_endian();
-
-	cbfs_name = argv[1];
+	param.cbfs_name = argv[1];
 	char *cmd = argv[2];
 	optind += 2;
 
@@ -572,61 +524,72 @@ int main(int argc, char **argv)
 
 			switch(c) {
 			case 'n':
-				rom_name = optarg;
+				param.name = optarg;
 				break;
 			case 't':
-				if (intfiletype(optarg) != ((uint64_t) - 1))
-					rom_type = intfiletype(optarg);
-				else
-					rom_type = strtoul(optarg, NULL, 0);
-				if (rom_type == 0)
+				// zero should be reserved for types.
+				param.type = get_cbfs_entry_type(
+						optarg,
+						strtoul(optarg, NULL, 0));
+				if (param.type == 0)
 					printf("W: Unknown type '%s' ignored\n",
-							optarg);
+					       optarg);
 				break;
 			case 'c':
-				if (!strncasecmp(optarg, "lzma", 5))
-					rom_algo = CBFS_COMPRESS_LZMA;
-				else if (!strncasecmp(optarg, "none", 5))
-					rom_algo = CBFS_COMPRESS_NONE;
-				else
+				param.algo = get_cbfs_compression(
+						optarg, -1);
+				if (param.algo == -1) {
 					printf("W: Unknown compression '%s'"
-							" ignored.\n", optarg);
+					       " ignored.\n", optarg);
+					param.algo = 0;
+				}
 				break;
 			case 'b':
-				rom_baseaddress = strtoul(optarg, NULL, 0);
+				// base_address may be zero on non-x86, so we
+				// need an explicit "base_address_assigned".
+				param.base_address = strtoul(optarg, NULL, 0);
+				param.base_address_assigned = 1;
 				break;
 			case 'l':
-				rom_loadaddress = strtoul(optarg, NULL, 0);
+				param.load_address = strtoul(optarg, NULL, 0);
 
 				break;
 			case 'e':
-				rom_entrypoint = strtoul(optarg, NULL, 0);
+				param.entrypoint = strtoul(optarg, NULL, 0);
 				break;
 			case 's':
-				rom_size = strtoul(optarg, &suffix, 0);
+				param.size = strtoul(optarg, &suffix, 0);
 				if (tolower(suffix[0])=='k') {
-					rom_size *= 1024;
+					param.size *= 1024;
 				}
 				if (tolower(suffix[0])=='m') {
-					rom_size *= 1024 * 1024;
+					param.size *= 1024 * 1024;
 				}
 			case 'B':
-				rom_bootblock = optarg;
+				param.bootblock_file = optarg;
+				break;
+			case 'H':
+				param.header_offset = strtoul(
+						optarg, NULL, 0);
+				param.header_offset_assigned = 1;
 				break;
 			case 'a':
-				rom_alignment = strtoul(optarg, NULL, 0);
+				param.alignment = strtoul(optarg, NULL, 0);
 				break;
 			case 'o':
-				rom_offset = strtoul(optarg, NULL, 0);
+				param.offset = strtoul(optarg, NULL, 0);
 				break;
 			case 'f':
-				rom_filename = optarg;
+				param.filename = optarg;
+				break;
+			case 'T':
+				param.top_aligned = 1;
 				break;
 			case 'v':
 				verbose++;
 				break;
 			case 'm':
-				arch = string_to_arch(optarg);
+				INFO("Architecture (-m) is deprecated.\n");
 				break;
 			case 'h':
 			case '?':
diff --git a/util/cbfstool/common.c b/util/cbfstool/common.c
index 97bf168..dd53d33 100644
--- a/util/cbfstool/common.c
+++ b/util/cbfstool/common.c
@@ -4,6 +4,7 @@
  * Copyright (C) 2009 coresystems GmbH
  *                 written by Patrick Georgi <patrick.georgi at coresystems.de>
  * Copyright (C) 2012 Google, Inc.
+ * Copyright (C) 2013 The ChromiumOS Authors.  All rights reserved.
  *
  * 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
@@ -19,767 +20,785 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
  */
 
+#include <assert.h>
+#include <inttypes.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <libgen.h>
+
 #include "common.h"
-#include "cbfs.h"
 #include "elf.h"
 
-#define dprintf(x...)
+/* Utilities */
 
-size_t getfilesize(const char *filename)
+/* Small, OS/libc independent runtime check for endianess */
+int is_big_endian(void)
 {
-	size_t size;
-	FILE *file = fopen(filename, "rb");
-	if (file == NULL)
-		return -1;
+	static const uint32_t inttest = 0x12345678;
+	uint8_t inttest_lsb = *(uint8_t *)&inttest;
+	if (inttest_lsb == 0x12) {
+		return 1;
+	}
+	return 0;
+}
 
-	fseek(file, 0, SEEK_END);
-	size = ftell(file);
-	fclose(file);
-	return size;
+uint32_t align_up(uint32_t value, uint32_t align) {
+	if (value % align)
+		value += align - (value % align);
+	return value;
 }
 
-void *loadfile(const char *filename, uint32_t * romsize_p, void *content,
-	       int place)
-{
-	FILE *file = fopen(filename, "rb");
-	if (file == NULL)
-		return NULL;
-
-	fseek(file, 0, SEEK_END);
-	*romsize_p = ftell(file);
-	fseek(file, 0, SEEK_SET);
-	if (!content) {
-		content = malloc(*romsize_p);
-		if (!content) {
-			fprintf(stderr, "E: Could not get %d bytes for file %s\n",
-			       *romsize_p, filename);
-			exit(1);
-		}
-	} else if (place == SEEK_END)
-		content -= *romsize_p;
+/* Buffer and file I/O */
+
+int buffer_create(struct buffer *buffer, size_t size, const char *name) {
+	buffer->name = strdup(name);
+	buffer->size = size;
+	buffer->data = (char*)malloc(buffer->size);
+	return (buffer->data == NULL);
+}
+
+int buffer_from_file(struct buffer *buffer, const char *filename) {
+	FILE *fp = fopen(filename, "rb");
+	if (!fp) {
+		perror(filename);
+		return -1;
+	}
+	fseek(fp, 0, SEEK_END);
+	buffer->size = ftell(fp);
+	buffer->name = strdup(filename);
+	rewind(fp);
+	buffer->data = (char*)malloc(buffer->size);
+	assert(buffer->data);
+	if (fread(buffer->data, 1, buffer->size, fp) != buffer->size) {
+		ERROR("incomplete read: %s\n", filename);
+		fclose(fp);
+		return -1;
+	}
+	fclose(fp);
+	return 0;
+}
 
-	if (!fread(content, *romsize_p, 1, file)) {
-		fprintf(stderr, "E: Failed to read %s\n", filename);
-		return NULL;
+int buffer_write_file(struct buffer *buffer, const char *filename) {
+	FILE *fp = fopen(filename, "wb");
+	if (!fp) {
+		perror(filename);
+		return -1;
+	}
+	assert(buffer && buffer->data);
+	if (fwrite(buffer->data, 1, buffer->size, fp) != buffer->size) {
+		ERROR("incomplete write: %s\n", filename);
+		fclose(fp);
+		return -1;
 	}
-	fclose(file);
-	return content;
+	fclose(fp);
+	return 0;
 }
 
-static struct cbfs_header *master_header;
-static uint32_t phys_start, phys_end, align;
-uint32_t romsize;
-void *offset;
-uint32_t arch = CBFS_ARCHITECTURE_UNKNOWN;
+int buffer_delete(struct buffer *buffer) {
+	assert(buffer);
+	if (buffer->name) {
+		free(buffer->name);
+		buffer->name = NULL;
+	}
+	if (buffer->data) {
+		free(buffer->data);
+		buffer->data = NULL;
+	}
+	buffer->size = 0;
+	return 0;
+}
 
-static struct {
-	uint32_t arch;
+/* Type and format */
+
+int is_elf_object(const void *input) {
+	Elf32_Ehdr *ehdr = (Elf32_Ehdr *)input;
+	return !memcmp(ehdr->e_ident, ELFMAG, 4);
+}
+
+struct typedesc_t {
+	uint32_t type;
 	const char *name;
-} arch_names[] = {
-	{ CBFS_ARCHITECTURE_ARMV7, "armv7" },
-	{ CBFS_ARCHITECTURE_X86, "x86" },
-	{ CBFS_ARCHITECTURE_UNKNOWN, "unknown" }
 };
 
-uint32_t string_to_arch(const char *arch_string)
-{
-	int i;
-	uint32_t ret = CBFS_ARCHITECTURE_UNKNOWN;
+static struct typedesc_t types_cbfs_entry[] = {
+	{CBFS_COMPONENT_STAGE, "stage"},
+	{CBFS_COMPONENT_PAYLOAD, "payload"},
+	{CBFS_COMPONENT_OPTIONROM, "optionrom"},
+	{CBFS_COMPONENT_BOOTSPLASH, "bootsplash"},
+	{CBFS_COMPONENT_RAW, "raw"},
+	{CBFS_COMPONENT_VSA, "vsa"},
+	{CBFS_COMPONENT_MBI, "mbi"},
+	{CBFS_COMPONENT_MICROCODE, "microcode"},
+	{CBFS_COMPONENT_CMOS_DEFAULT, "cmos_default"},
+	{CBFS_COMPONENT_CMOS_LAYOUT, "cmos_layout"},
+	{CBFS_COMPONENT_DELETED, "deleted"},
+	{CBFS_COMPONENT_NULL, "null"},
+	{0, NULL},
+};
 
-	for (i = 0; i < ARRAY_SIZE(arch_names); i++) {
-		if (!strcasecmp(arch_string, arch_names[i].name)) {
-			ret = arch_names[i].arch;
-			break;
-		}
-	}
+static struct typedesc_t types_cbfs_compression[] = {
+	{CBFS_COMPRESS_NONE, "none"},
+	{CBFS_COMPRESS_LZMA, "LZMA"},
+	{0, NULL},
+};
 
-	return ret;
+uint32_t lookup_type_by_name(struct typedesc_t *desc, const char *name,
+			     uint32_t default_value) {
+	int i;
+	for (i = 0; desc[i].name; i++)
+		if (strcmp(desc[i].name, name) == 0)
+			return desc[i].type;
+	return default_value;
 }
 
-const char *arch_to_string(uint32_t a)
-{
+const char *lookup_name_by_type(struct typedesc_t *desc, uint32_t type,
+				const char *default_value) {
 	int i;
-	const char *ret = NULL;
+	for (i = 0; desc[i].name; i++)
+		if (desc[i].type == type)
+			return desc[i].name;
+	return default_value;
+}
 
-	for (i = 0; i < ARRAY_SIZE(arch_names); i++) {
-		if (a == arch_names[i].arch) {
-			ret = arch_names[i].name;
-			break;
-		}
-	}
+uint32_t get_cbfs_entry_type(const char *name, uint32_t default_value) {
+	return lookup_type_by_name(types_cbfs_entry, name, default_value);
+}
 
-	return ret;
+const char *get_cbfs_entry_type_name(uint32_t type) {
+	return lookup_name_by_type(types_cbfs_entry, type, "(unknown)");
+}
 
+uint32_t get_cbfs_compression(const char *name, uint32_t unknown) {
+	return lookup_type_by_name(types_cbfs_compression, name, unknown);
 }
 
-int find_master_header(void *romarea, size_t size)
-{
-	size_t offset;
+void print_all_cbfs_entry_types(void) {
+	size_t i, total = ARRAY_SIZE(types_cbfs_entry);
 
-	if (master_header)
-		return 0;
+	for (i = 0; i < total; i++) {
+		printf(" %s%c", types_cbfs_entry[i].name,
+		       (i + 1 == total) ? '\n' : ',');
+		if ((i + 1) % 8 == 0)
+			printf("\n");
+	}
+}
 
-	for (offset = 0; offset < size - sizeof(struct cbfs_header); offset++) {
-		struct cbfs_header *tmp = romarea + offset;
+/* CBFS image processing */
 
-		if (tmp->magic == ntohl(CBFS_HEADER_MAGIC)) {
-			master_header = tmp;
-			break;
-		}
-	}
+/* The file name align is not defined in CBFS spec -- only a preference by
+ * (old) cbfstool. */
+#define CBFS_FILENAME_ALIGN	(16)
 
-	return master_header ? 0 : 1;
-}
+/* To make CBFS more friendly to ROM, fill -1 (0xFF) instead of zero. */
+#define CBFS_CONTENT_DEFAULT_VALUE	(-1)
 
-void recalculate_rom_geometry(void *romarea)
+int cbfs_image_create(struct cbfs_image *image,
+		      size_t size,
+		      uint32_t align,
+		      struct buffer *bootblock,
+		      int32_t bootblock_offset,
+		      int32_t header_offset,
+		      int32_t entries_offset)
 {
-	if (find_master_header(romarea, romsize)) {
-		fprintf(stderr, "E: Cannot find master header\n");
-		exit(1);
-	}
+	struct cbfs_header *header;
+	struct cbfs_file *entry;
+	uint32_t cbfs_len;
 
-	/* Update old headers */
-	if (master_header->version == CBFS_HEADER_VERSION1 &&
-	    ntohl(master_header->architecture) == CBFS_ARCHITECTURE_UNKNOWN) {
-		dprintf("Updating CBFS master header to version 2\n");
-		master_header->architecture = htonl(CBFS_ARCHITECTURE_X86);
-	}
+	DEBUG("cbfs_create_image: bootblock=0x%x+0x%zx, "
+	      "header=0x%x+0x%zx, entries_offset=0x%x\n",
+	      bootblock_offset, bootblock->size,
+	      header_offset, sizeof(*header), entries_offset);
 
-	arch = ntohl(master_header->architecture);
-
-	switch (arch) {
-	case CBFS_ARCHITECTURE_ARMV7:
-		offset = romarea;
-		phys_start = (0 + ntohl(master_header->offset)) & 0xffffffff;
-		phys_end = romsize & 0xffffffff;
-		break;
-	case CBFS_ARCHITECTURE_X86:
-		offset = romarea + romsize - 0x100000000ULL;
-		phys_start = (0 - romsize + ntohl(master_header->offset)) &
-				0xffffffff;
-		phys_end = (0 - ntohl(master_header->bootblocksize) -
-		     sizeof(struct cbfs_header)) & 0xffffffff;
-		break;
-	default:
-		fprintf(stderr, "E: Unknown architecture\n");
-		exit(1);
+	if (buffer_create(&image->buffer, size, "(new)") != 0) {
+		ERROR("Cannot allocate memory for %zd bytes.\n", size);
+		return -1;
 	}
+	image->header = NULL;
+	memset(image->buffer.data, CBFS_CONTENT_DEFAULT_VALUE, size);
 
-	align = ntohl(master_header->align);
-}
+	// Adjust legcay top-aligned address to ROM offset.
+	if (IS_TOP_ALIGNED_ADDRESS(entries_offset))
+		entries_offset += (int32_t)size;
+	if (IS_TOP_ALIGNED_ADDRESS(bootblock_offset))
+		bootblock_offset += (int32_t)size;
+	if (IS_TOP_ALIGNED_ADDRESS(header_offset))
+		header_offset += (int32_t) size;
 
-void *loadrom(const char *filename)
-{
-	void *romarea = loadfile(filename, &romsize, 0, SEEK_SET);
-	if (romarea == NULL)
-		return NULL;
-	recalculate_rom_geometry(romarea);
-	return romarea;
-}
+	DEBUG("cbfs_create_image: (real offset) bootblock=0x%x, "
+	      "header=0x%x, entries_offset=0x%x\n",
+	      bootblock_offset, header_offset, entries_offset);
 
-int writerom(const char *filename, void *start, uint32_t size)
-{
-	FILE *file = fopen(filename, "wb");
-	if (!file) {
-		fprintf(stderr, "Could not open '%s' for writing: ", filename);
-		perror("");
-		return 1;
-	}
+	if (align == 0)
+		align = 64;  // default align size.
 
-	if (fwrite(start, size, 1, file) != 1) {
-		fprintf(stderr, "Could not write to '%s': ", filename);
-		perror("");
-		return 1;
+	// Prepare bootblock
+	if (bootblock_offset + bootblock->size > size) {
+		ERROR("Bootblock (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
+		      bootblock_offset, bootblock->size, size);
+		return -1;
 	}
+	memcpy(image->buffer.data + bootblock_offset, bootblock->data,
+	       bootblock->size);
 
-	fclose(file);
+	// Prepare header
+	if (header_offset + sizeof(*header) > size) {
+		ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
+		      header_offset, sizeof(*header), size);
+		return -1;
+	}
+	header = (struct cbfs_header*)(image->buffer.data + header_offset);
+	image->header = header;
+	header->magic = htonl(CBFS_HEADER_MAGIC);
+	header->version = htonl(CBFS_HEADER_VERSION);
+	header->romsize = htonl(size);
+	header->bootblocksize = htonl(bootblock->size);
+	header->align = htonl(align);
+	header->offset = htonl(entries_offset);
+
+	// Prepare entries
+	if (align_up(entries_offset, align) != entries_offset) {
+		ERROR("Offset (0x%x) must be aligned to 0x%x.\n",
+		      entries_offset, align);
+		return -1;
+	}
+	if (entries_offset + sizeof(*entry) > size) {
+		ERROR("Offset (0x%x+0x%zx) exceed ROM size(0x%zx)\n",
+		      entries_offset, sizeof(*entry), size);
+		return -1;
+	}
+	entry = (struct cbfs_file*)(image->buffer.data + entries_offset);
+	// To calculate available length, find
+	//   e = min(bootblock, header, size) where e > entries_offset.
+	cbfs_len = size;
+	if (bootblock_offset > entries_offset && bootblock_offset < cbfs_len)
+		cbfs_len = bootblock_offset;
+	if (header_offset > entries_offset && header_offset < cbfs_len)
+		cbfs_len = header_offset;
+	cbfs_len -= entries_offset + align;
+	cbfs_create_entry(image, entry, cbfs_len, "");
+	LOG("Created CBFS image (capacity = %d bytes)\n", cbfs_len);
 	return 0;
 }
 
-int cbfs_file_header(unsigned long physaddr)
-{
-	/* maybe improve this test */
-	return (strncmp(phys_to_virt(physaddr), "LARCHIVE", 8) == 0);
-}
-
-struct cbfs_file *cbfs_create_empty_file(uint32_t physaddr, uint32_t size)
-{
-	struct cbfs_file *nextfile = (struct cbfs_file *)phys_to_virt(physaddr);
-	strncpy((char *)(nextfile->magic), "LARCHIVE", 8);
-	nextfile->len = htonl(size);
-	nextfile->type = htonl(0xffffffff);
-	nextfile->checksum = 0;	// FIXME?
-	nextfile->offset = htonl(sizeof(struct cbfs_file) + 16);
-	memset(((void *)nextfile) + sizeof(struct cbfs_file), 0, 16);
-	return nextfile;
-}
+static int cbfs_fix_legacy_size(struct cbfs_image *image) {
+	// A bug in old CBFStool may produce extra few bytes (by alignment) and
+	// cause cbfstool to overwrite things after free space -- which is
+	// usually CBFS header on x86. We need to workaround that.
 
-int iself(unsigned char *input)
-{
-	Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input;
-	return !memcmp(ehdr->e_ident, ELFMAG, 4);
+	struct cbfs_file *entry, *first = NULL, *last = NULL;
+	for (first = entry = cbfs_find_first_entry(image);
+	     entry && cbfs_is_valid_entry(entry);
+	     entry = cbfs_find_next_entry(image, entry)) {
+		last = entry;
+	}
+	if ((char*)first < (char*)image->header &&
+	    (char*)entry > (char*)image->header) {
+		WARN("CBFS image was created with old cbfstool with size bug. "
+		     "Fixing size in last entry...\n");
+		last->len = htonl(ntohl(last->len) -
+				  ntohl(image->header->align));
+		DEBUG("Last entry has been changed from 0x%x to 0x%x.\n",
+		      cbfs_get_entry_addr(image, entry),
+		      cbfs_get_entry_addr(image,
+					  cbfs_find_next_entry(image, last)));
+	}
+	return 0;
 }
 
-static struct filetypes_t {
-	uint32_t type;
-	const char *name;
-} filetypes[] = {
-	{CBFS_COMPONENT_STAGE, "stage"},
-	{CBFS_COMPONENT_PAYLOAD, "payload"},
-	{CBFS_COMPONENT_OPTIONROM, "optionrom"},
-	{CBFS_COMPONENT_BOOTSPLASH, "bootsplash"},
-	{CBFS_COMPONENT_RAW, "raw"},
-	{CBFS_COMPONENT_VSA, "vsa"},
-	{CBFS_COMPONENT_MBI, "mbi"},
-	{CBFS_COMPONENT_MICROCODE, "microcode"},
-	{CBFS_COMPONENT_CMOS_DEFAULT, "cmos default"},
-	{CBFS_COMPONENT_CMOS_LAYOUT, "cmos layout"},
-	{CBFS_COMPONENT_DELETED, "deleted"},
-	{CBFS_COMPONENT_NULL, "null"}
-};
-
-void print_supported_filetypes(void)
-{
-	int i, number = ARRAY_SIZE(filetypes);
-
-	for (i=0; i<number; i++) {
-		printf(" %s%c", filetypes[i].name, (i==(number-1))?'\n':',');
-		if ((i%8) == 7)
-			printf("\n");
+int cbfs_image_from_file(struct cbfs_image *image, const char *filename) {
+	if (buffer_from_file(&image->buffer, filename) != 0)
+		return -1;
+	DEBUG("read_cbfs_image: %s (%zd bytes)\n", image->buffer.name,
+	      image->buffer.size);
+	image->header = cbfs_find_header(image->buffer.data,
+					 image->buffer.size);
+	if (!image->header) {
+		ERROR("%s does not have CBFS master header.\n", filename);
+		cbfs_image_delete(image);
+		return -1;
 	}
+
+	cbfs_fix_legacy_size(image);
+	return 0;
 }
 
-const char *strfiletype(uint32_t number)
-{
-	size_t i;
-	for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++)
-		if (filetypes[i].type == number)
-			return filetypes[i].name;
-	return "unknown";
+int cbfs_image_write_file(struct cbfs_image *image, const char *filename) {
+	assert(image && image->buffer.data);
+	return buffer_write_file(&image->buffer, filename);
 }
 
-uint64_t intfiletype(const char *name)
-{
-	size_t i;
-	for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++)
-		if (strcmp(filetypes[i].name, name) == 0)
-			return filetypes[i].type;
-	return -1;
+int cbfs_image_delete(struct cbfs_image *image) {
+	buffer_delete(&image->buffer);
+	image->header = NULL;
+	return 0;
 }
 
-void print_cbfs_directory(const char *filename)
-{
-	printf
-		("%s: %d kB, bootblocksize %d, romsize %d, offset 0x%x\n"
-		 "alignment: %d bytes, architecture: %s\n\n",
-		 basename((char *)filename), romsize / 1024, ntohl(master_header->bootblocksize),
-		 romsize, ntohl(master_header->offset), align, arch_to_string(arch));
-	printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
-	uint32_t current = phys_start;
-	while (current < phys_end) {
-		if (!cbfs_file_header(current)) {
-			current += align;
-			continue;
-		}
-		struct cbfs_file *thisfile =
-			(struct cbfs_file *)phys_to_virt(current);
-		uint32_t length = ntohl(thisfile->len);
-		char *fname = (char *)(phys_to_virt(current) + sizeof(struct cbfs_file));
-		if (strlen(fname) == 0)
-			fname = "(empty)";
-
-		printf("%-30s 0x%-8x %-12s %d\n", fname,
-		       current - phys_start + ntohl(master_header->offset),
-		       strfiletype(ntohl(thisfile->type)), length);
-
-		/* note the components of the subheader are in host order ... */
-		switch (ntohl(thisfile->type)) {
-		case CBFS_COMPONENT_STAGE:
-		{
-			struct cbfs_stage *stage = CBFS_SUBHEADER(thisfile);
-			dprintf("    %s compression, entry: 0x%llx, load: 0x%llx, length: %d/%d\n",
-			       stage->compression == CBFS_COMPRESS_LZMA ? "LZMA" : "no",
-			       (unsigned long long)stage->entry,
-			       (unsigned long long)stage->load,
-			       stage->len,
-			       stage->memlen);
-			break;
-		}
-		case CBFS_COMPONENT_PAYLOAD:
-		{
-			struct cbfs_payload_segment *payload = CBFS_SUBHEADER(thisfile);
-			while(payload) {
-				switch(payload->type) {
-				case PAYLOAD_SEGMENT_CODE:
-				case PAYLOAD_SEGMENT_DATA:
-					dprintf("    %s (%s compression, offset: 0x%x, load: 0x%llx, length: %d/%d)\n",
-						payload->type == PAYLOAD_SEGMENT_CODE ? "code " : "data" ,
-						payload->compression == CBFS_COMPRESS_LZMA ? "LZMA" : "no",
-						ntohl(payload->offset),
-						(unsigned long long)ntohll(payload->load_addr),
-						ntohl(payload->len), ntohl(payload->mem_len));
-					break;
-				case PAYLOAD_SEGMENT_ENTRY:
-					dprintf("    entry (0x%llx)\n", (unsigned long long)ntohll(payload->load_addr));
-					break;
-				case PAYLOAD_SEGMENT_BSS:
-					dprintf("    BSS (address 0x%016llx, length 0x%x)\n", (unsigned long long)ntohll(payload->load_addr), ntohl(payload->len));
-					break;
-				case PAYLOAD_SEGMENT_PARAMS:
-					dprintf("    parameters\n");
-					break;
-				default:
-					dprintf("    %x (%s compression, offset: 0x%x, load: 0x%llx, length: %d/%d\n",
-						payload->type,
-						payload->compression == CBFS_COMPRESS_LZMA ? "LZMA" : "no",
-						ntohl(payload->offset),
-						(unsigned long long)ntohll(payload->load_addr),
-						ntohl(payload->len),
-						ntohl(payload->mem_len));
-					break;
-				}
+/* Splits an empty entry at given offset. */
+int cbfs_split_empty_entry(struct cbfs_image *image,
+			   struct cbfs_file *entry,
+			   uint32_t offset) {
+	struct cbfs_file *next = cbfs_find_next_entry(image, entry);
+	uint32_t addr = cbfs_get_entry_addr(image, entry),
+		 addr_next = cbfs_get_entry_addr(image, next);
+	size_t min_entry_size = sizeof(*entry) + CBFS_FILENAME_ALIGN;
+	size_t len;
+
+	assert(addr < offset && addr_next <= offset);
+	if (offset - addr < min_entry_size) {
+		ERROR("No space between previous entry 0x%x and offset 0x%x.\n",
+		      addr, offset);
+		return -1;
+	}
+	if (addr_next - offset < min_entry_size) {
+		ERROR("No space between offset 0x%x and entry 0x%x\n",
+		      offset, addr_next);
+		return -1;
+	}
+	cbfs_create_entry(image, entry, 0, "");
+	len = offset - addr - ntohl(entry->offset);
+	entry->len = htonl(len);
+	if (verbose > 1)
+		cbfs_print_entry_info(image, entry, NULL);
+
+	entry = cbfs_find_next_entry(image, entry);
+	addr = cbfs_get_entry_addr(image, entry);
+	cbfs_create_entry(image, entry, 0, "");
+	len = addr_next - offset - ntohl(entry->offset);
+	entry->len = htonl(len);
+	if (verbose > 1)
+		cbfs_print_entry_info(image, entry, NULL);
+	assert(cbfs_get_entry_addr(image, cbfs_find_next_entry(image, entry)) ==
+	       addr_next);
+	return 0;
+}
 
-				if(payload->type == PAYLOAD_SEGMENT_ENTRY)
-					payload=NULL;
-				else
-					payload++;
-			}
-			break;
+int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
+		   const char *name, uint32_t type, uint32_t offset) {
+	uint32_t entry_type, entry_capacity;
+	uint32_t addr;
+	struct cbfs_file *entry, *next;
+	uint32_t need_size, new_size;
+
+	need_size = (sizeof(*entry) + buffer->size +
+		     align_up(strlen(name) + 1, CBFS_FILENAME_ALIGN));
+	DEBUG("cbfs_add_entry('%s', %zd) => need_size = %u\n",
+	      name, buffer->size, need_size);
+
+	// Check if offset is valid.
+	if (offset) {
+		uint32_t align = ntohl(image->header->align),
+			 romsize = ntohl(image->header->romsize);
+		if (offset % align != 0) {
+			ERROR("Target address (0x%x) is not aligned to CBFS "
+			      "image align (0x%x). It's impossible to add entry "
+			      "in that location.\n", offset, align);
+			return -1;
 		}
-		default:
-			break;
+		// legacy cbfstool takes top-aligned address.
+		if (IS_TOP_ALIGNED_ADDRESS(offset)) {
+			INFO("Converting top-aligned address 0x%x to offset: "
+			     " 0x%x\n", offset, offset - romsize);
+			offset += romsize;
 		}
-		current =
-		    ALIGN(current + ntohl(thisfile->len) +
-			  ntohl(thisfile->offset), align);
 	}
-}
 
-int extract_file_from_cbfs(const char *filename, const char *payloadname, const char *outpath)
-{
-	FILE *outfile = NULL;
-	uint32_t current = phys_start;
-	while (current < phys_end) {
-		if (!cbfs_file_header(current)) {
-			current += align;
-			continue;
-		}
+	// Merge empty entries.
+	DEBUG("(trying to merge empty entries...)\n");
+	cbfs_walk(image, cbfs_merge_empty_entry, NULL);
 
-		// Locate the file start struct
-		struct cbfs_file *thisfile =
-		    (struct cbfs_file *)phys_to_virt(current);
-		// And its length
-		uint32_t length = ntohl(thisfile->len);
-		// Locate the file name
-		char *fname = (char *)(phys_to_virt(current) + sizeof(struct cbfs_file));
-
-		// It's not the file we are looking for..
-		if (strcmp(fname, payloadname) != 0)
-		{
-			current =
-			   ALIGN(current + ntohl(thisfile->len) +
-				  ntohl(thisfile->offset), align);
+	for (entry = cbfs_find_first_entry(image);
+	     entry && cbfs_is_valid_entry(entry);
+	     entry = cbfs_find_next_entry(image, entry)) {
+
+		entry_type = ntohl(entry->type);
+		if (entry_type != CBFS_COMPONENT_DELETED &&
+		    entry_type != CBFS_COMPONENT_NULL)
 			continue;
-		}
 
-		// Else, it's our file.
-		printf("Found file %.30s at 0x%x, type %.12s, size %d\n", fname,
-		       current - phys_start, strfiletype(ntohl(thisfile->type)),
-		       length);
-
-		// If we are not dumping to stdout, open the out file.
-		outfile = fopen(outpath, "wb");
-		if (!outfile)
-		{
-			fprintf(stderr, "E: Could not open the file %s for writing.\n", outpath);
-			return 1;
+		addr = cbfs_get_entry_addr(image, entry);
+		if (offset && addr > offset) {
+			ERROR("Not available to add component in 0x%x.\n",
+			      offset);
+			return -1;
 		}
 
-		if (ntohl(thisfile->type) != CBFS_COMPONENT_RAW)
-		{
-			fprintf(stderr, "W: Only 'raw' files are safe to extract.\n");
+		next = cbfs_find_next_entry(image, entry);
+		entry_capacity = cbfs_get_entry_addr(image, next) - addr;
+		if (offset && (addr + entry_capacity) <= offset) {
+			DEBUG("Skip entry 0x%x before specified offset 0x%x.\n",
+			      addr, offset);
+			continue;
 		}
 
-		fwrite(((char *)thisfile)
-				+ ntohl(thisfile->offset), length, 1, outfile);
+		DEBUG("cbfs_add_entry: space %s at 0x%x, %d bytes\n",
+		      get_cbfs_entry_type_name(entry_type), addr,
+		      entry_capacity);
+		if (entry_capacity < need_size)
+			continue;
 
-		fclose(outfile);
-		printf("Successfully dumped the file.\n");
+		// Should we create an empty partition before target offset?
+		if (offset && addr != offset) {
+			DEBUG("Split empty entry at 0x%x\n", addr);
+			if (cbfs_split_empty_entry(image, entry, offset) != 0) {
+				ERROR("Can't split empty entry for 0x%x\n",
+				      offset);
+				return -1;
+			}
+			assert(cbfs_get_entry_addr(
+					image,
+					cbfs_find_next_entry(image, entry)) ==
+			       addr);
+			continue;
+		}
 
-		// We'll only dump one file.
+		DEBUG("Filling new entry data (%zd bytes).\n", buffer->size);
+		cbfs_create_entry(image, entry, buffer->size, name);
+		entry->type = htonl(type);
+		memcpy(CBFS_SUBHEADER(entry), buffer->data, buffer->size);
+		if (verbose)
+			cbfs_print_entry_info(image, entry, NULL);
+
+		// setup new entry
+		DEBUG("Seting new empty entry.\n");
+		entry = cbfs_find_next_entry(image, entry);
+		new_size = (cbfs_get_entry_addr(image, next) -
+			    cbfs_get_entry_addr(image, entry));
+		new_size -= sizeof(*entry) + CBFS_FILENAME_ALIGN;
+		DEBUG("new size: %d\n", new_size);
+		cbfs_create_entry(image, entry, new_size, "");
+		if (verbose)
+			cbfs_print_entry_info(image, entry, NULL);
 		return 0;
 	}
-	fprintf(stderr, "E: File %s not found.\n", payloadname);
-	return 1;
+
+	ERROR("Could not add [%s, %zd bytes (%zd KB)]; probably too big?\n",
+	      buffer->name, buffer->size, buffer->size / 1024);
+	return 0;
 }
 
+int cbfs_remove_entry(struct cbfs_image *image, const char *name) {
+	struct cbfs_file *entry, *next;
+	size_t len;
+	entry = cbfs_get_entry(image, name);
+	if (!entry) {
+		ERROR("CBFS file %s not found.\n", name);
+		return -1;
+	}
+	next = cbfs_find_next_entry(image, entry);
+	assert(next);
+	DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n",
+	      CBFS_NAME(entry), cbfs_get_entry_addr(image, entry));
+	entry->type = htonl(CBFS_COMPONENT_DELETED);
+	len = (cbfs_get_entry_addr(image, next) -
+	       cbfs_get_entry_addr(image, entry));
+	entry->offset = htonl(sizeof(*entry) + CBFS_FILENAME_ALIGN);
+	entry->len = htonl(len - ntohl(entry->offset));
+	memset(CBFS_NAME(entry), 0, CBFS_FILENAME_ALIGN);
+	memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE,
+	       ntohl(entry->len));
+	return 0;
+}
 
-int add_file_to_cbfs(void *content, uint32_t contentsize, uint32_t location)
-{
-	uint32_t current = phys_start;
-	while (current < phys_end) {
-		if (!cbfs_file_header(current)) {
-			current += align;
-			continue;
+struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name) {
+	struct cbfs_file *entry;
+	for (entry = cbfs_find_first_entry(image);
+	     entry && cbfs_is_valid_entry(entry);
+	     entry = cbfs_find_next_entry(image, entry)) {
+		if (strcasecmp(CBFS_NAME(entry), name) == 0) {
+			DEBUG("cbfs_get_entry: found %s\n", name);
+			return entry;
 		}
-		struct cbfs_file *thisfile =
-		    (struct cbfs_file *)phys_to_virt(current);
-		uint32_t length = ntohl(thisfile->len);
-
-		dprintf("at %x, %x bytes\n", current, length);
-		/* Is this a free chunk? */
-		if ((thisfile->type == CBFS_COMPONENT_DELETED)
-		    || (thisfile->type == CBFS_COMPONENT_NULL)) {
-			dprintf("null||deleted at %x, %x bytes\n", current,
-				length);
-			/* if this is the right size, and if specified, the right location, use it */
-			if ((contentsize <= length)
-			    && ((location == 0) || (current == location))) {
-				if (contentsize < length) {
-					dprintf
-					    ("this chunk is %x bytes, we need %x. create a new chunk at %x with %x bytes\n",
-					     length, contentsize,
-					     ALIGN(current + contentsize,
-						   align),
-					     length - contentsize);
-					uint32_t start =
-					    ALIGN(current + contentsize, align);
-					uint32_t size =
-					    current + ntohl(thisfile->offset)
-					    + length - start - 16 -
-					    sizeof(struct cbfs_file);
-					cbfs_create_empty_file(start, size);
-				}
-				dprintf("copying data\n");
-				memcpy(phys_to_virt(current), content,
-				       contentsize);
-				return 0;
-			}
-			if (location != 0) {
-				/* CBFS has the constraint that the chain always moves up in memory. so once
-				   we're past the place we seek, we don't need to look any further */
-				if (current > location) {
-					fprintf
-					    (stderr, "E: The requested space is not available\n");
-					return 1;
-				}
-
-				/* Is the requested location inside the current chunk? */
-				if ((current < location)
-				    && ((location + contentsize) <=
-					(current + length))) {
-					/* Split it up. In the next iteration the code will be at the right place. */
-					dprintf("split up. new length: %x\n",
-						location - current -
-						ntohl(thisfile->offset));
-					thisfile->len =
-					    htonl(location - current -
-						  ntohl(thisfile->offset));
-					cbfs_create_empty_file(location,
-								   length -
-								   (location -
-								    current));
-				}
-			}
-		}
-		current =
-		    ALIGN(current + ntohl(thisfile->len) +
-			  ntohl(thisfile->offset), align);
 	}
-	fprintf(stderr, "E: Could not add the file to CBFS, it's probably too big.\n");
-	fprintf(stderr, "E: File size: %d bytes (%d KB).\n", contentsize, contentsize/1024);
-	return 1;
+	return NULL;
 }
 
+int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
+		      const char *filename) {
+	struct cbfs_file *entry = cbfs_get_entry(image, entry_name);
+	struct buffer buffer;
+	if (!entry) {
+		ERROR("File not found: %s\n", entry_name);
+		return -1;
+	}
+	LOG("Found file %.30s at 0x%x, type %.12s, size %d\n",
+	    entry_name, cbfs_get_entry_addr(image, entry),
+	    get_cbfs_entry_type_name(ntohl(entry->type)), ntohl(entry->len));
 
-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;
+	if (ntohl(entry->type) != CBFS_COMPONENT_RAW) {
+		WARN("Only 'raw' files are safe to extract.\n");
+	}
+
+	buffer.data = CBFS_SUBHEADER(entry);
+	buffer.size = ntohl(entry->len);
+	buffer.name = "(cbfs_export_entry)";
+	if (buffer_write_file(&buffer, filename) != 0) {
+		ERROR("Failed to write %s into %s.\n",
+		      entry_name, filename);
+		return -1;
+	}
+	INFO("Successfully dumped the file to: %s\n", filename);
+	return 0;
 }
 
-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);
+/* basename(3) may modify buffer, so we want a tiny alternative. */
+static const char *simple_basename(const char *name) {
+	const char *slash = strrchr(name, '/');
+	if (slash)
+		return slash + 1;
+	else
+		return name;
+}
 
-	for (; pos < phys_end; pos += align) {
-		if (cbfs_file_header(pos))
-			return (struct cbfs_file *)phys_to_virt(pos);
-	}
-	return NULL;
+int cbfs_print_header_info(struct cbfs_image *image) {
+	assert(image && image->header);
+	printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n"
+	       "alignment: %d bytes\n\n",
+	       simple_basename(image->buffer.name),
+	       image->buffer.size / 1024,
+	       ntohl(image->header->bootblocksize),
+	       ntohl(image->header->romsize),
+	       ntohl(image->header->offset),
+	       ntohl(image->header->align));
+	return 0;
 }
 
+static int cbfs_print_stage_info(struct cbfs_stage *stage) {
+	printf("    %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", "
+	       "length: %d/%d\n",
+	       lookup_name_by_type(types_cbfs_compression,
+				   stage->compression, "(unknown)"),
+	       stage->entry,
+	       stage->load,
+	       stage->len,
+	       stage->memlen);
+	return 0;
+}
 
-int remove_file_from_cbfs(const char *filename)
+static int cbfs_print_payload_segment_info(struct cbfs_payload_segment *payload)
 {
-	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;
+	switch(payload->type) {
+		case PAYLOAD_SEGMENT_CODE:
+		case PAYLOAD_SEGMENT_DATA:
+			printf("    %s (%s compression, offset: 0x%x, "
+			       "load: 0x%" PRIx64 ", length: %d/%d)\n",
+			       (payload->type == PAYLOAD_SEGMENT_CODE ?
+				"code " : "data"),
+			       lookup_name_by_type(types_cbfs_compression,
+						   payload->compression,
+						   "(unknown)"),
+			       ntohl(payload->offset),
+			       ntohll(payload->load_addr),
+			       ntohl(payload->len), ntohl(payload->mem_len));
+			break;
 
-		/* Mark the file as free space and erase its name. */
-		cur->type = CBFS_COMPONENT_NULL;
-		name[0] = '\0';
+		case PAYLOAD_SEGMENT_ENTRY:
+			printf("    entry (0x%" PRIx64 ")\n",
+			       ntohll(payload->load_addr));
+			break;
 
-		/* Merge it with the previous file if possible. */
-		if (prev && prev->type == CBFS_COMPONENT_NULL)
-			cur = merge_adjacent_files(prev, cur);
+		case PAYLOAD_SEGMENT_BSS:
+			printf("    BSS (address 0x%016" PRIx64 ", "
+			       "length 0x%x)\n",
+			       ntohll(payload->load_addr),
+			       ntohl(payload->len));
+			break;
 
-		/* Merge it with the next file if possible. */
-		if (next && next->type == CBFS_COMPONENT_NULL)
-			merge_adjacent_files(cur, next);
+		case PAYLOAD_SEGMENT_PARAMS:
+			printf("    parameters\n");
+			break;
 
-		return 0;
+		default:
+			printf("   0x%x (%s compression, offset: 0x%x, "
+			       "load: 0x%" PRIx64 ", length: %d/%d\n",
+			       payload->type,
+			       lookup_name_by_type(types_cbfs_compression,
+						   payload->compression,
+						   "(unknown)"),
+			       ntohl(payload->offset),
+			       ntohll(payload->load_addr),
+			       ntohl(payload->len),
+			       ntohl(payload->mem_len));
+			break;
 	}
-	fprintf(stderr, "E: CBFS file %s not found.\n", filename);
-	return 1;
+	return 0;
 }
 
-
-/* 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,
-		       uint32_t type, uint32_t * location)
-{
-	uint32_t filename_len = ALIGN(strlen(filename) + 1, 16);
-	uint32_t headersize = sizeof(struct cbfs_file) + filename_len;
-	if ((location != 0) && (*location != 0)) {
-		uint32_t offset = *location % align;
-		/* If offset >= (headersize % align), we can stuff the header into the offset.
-		   Otherwise the header has to be aligned itself, and put before the offset data */
-		if (offset >= (headersize % align)) {
-			offset -= (headersize % align);
-		} else {
-			offset += align - (headersize % align);
-		}
-		headersize += offset;
-		*location -= headersize;
-	}
-	void *newdata = malloc(*datasize + headersize);
-	if (!newdata) {
-		fprintf(stderr, "E: Could not get %d bytes for CBFS file.\n", *datasize +
-		       headersize);
-		exit(1);
-	}
-	memset(newdata, 0xff, *datasize + headersize);
-	struct cbfs_file *nextfile = (struct cbfs_file *)newdata;
-	strncpy((char *)(nextfile->magic), "LARCHIVE", 8);
-	nextfile->len = htonl(*datasize);
-	nextfile->type = htonl(type);
-	nextfile->checksum = 0;	// FIXME?
-	nextfile->offset = htonl(headersize);
-	strcpy(newdata + sizeof(struct cbfs_file), filename);
-	memcpy(newdata + headersize, data, *datasize);
-	*datasize += headersize;
-	return newdata;
-}
-
-int create_cbfs_image(const char *romfile, uint32_t _romsize,
-		const char *bootblock, uint32_t align, uint32_t offs)
-{
-	uint32_t bootblocksize = 0;
-	struct cbfs_header *master_header;
-	unsigned char *romarea, *bootblk;
-
-	romsize = _romsize;
-	romarea = malloc(romsize);
-	if (!romarea) {
-		fprintf(stderr, "E: Could not get %d bytes of memory"
-			" for CBFS image.\n", romsize);
-		exit(1);
+int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
+			  void *arg) {
+	const char *name = CBFS_NAME(entry);
+	struct cbfs_payload_segment *payload;
+	if (!cbfs_is_valid_entry(entry)) {
+		ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
+		      cbfs_get_entry_addr(image, entry));
+		return -1;
 	}
-	memset(romarea, 0xff, romsize);
 
-	if (align == 0)
-		align = 64;
-
-	bootblk = loadfile(bootblock, &bootblocksize,
-				romarea + romsize, SEEK_END);
-	if (!bootblk) {
-		fprintf(stderr, "E: Could not load bootblock %s.\n",
-			bootblock);
-		free(romarea);
-		return 1;
-	}
+	printf("%-30s 0x%-8x %-12s %d\n",
+	       *name ? name : "(empty)",
+	       cbfs_get_entry_addr(image, entry),
+	       get_cbfs_entry_type_name(ntohl(entry->type)),
+	       ntohl(entry->len));
+
+	if (!verbose)
+		return 0;
+
+	/* note the components of the subheader may be in host order ... */
+	switch (ntohl(entry->type)) {
+		case CBFS_COMPONENT_STAGE:
+			cbfs_print_stage_info((struct cbfs_stage*)
+					      CBFS_SUBHEADER(entry));
+			break;
 
-	// TODO(hungte) Replace magic numbers by named constants.
-	switch (arch) {
-	case CBFS_ARCHITECTURE_ARMV7:
-		/* Set up physical/virtual mapping */
-		offset = romarea;
-
-		/*
-		 * The initial jump instruction and bootblock will be placed
-		 * before and after the master header, respectively. The
-		 * bootblock image must contain a blank, aligned region large
-		 * enough for the master header to fit.
-		 *
-		 * An anchor string must be left such that when cbfstool is run
-		 * we can find it and insert the master header at the next
-		 * aligned boundary.
-		 */
-		loadfile(bootblock, &bootblocksize, romarea + offs, SEEK_SET);
-
-		unsigned char *p = romarea + offs;
-		while (1) {
-			/* FIXME: assumes little endian... */
-			if (*(uint32_t *)p == 0xdeadbeef)
-				break;
-			if (p >= (romarea + _romsize)) {
-				fprintf(stderr, "E: Could not determine CBFS "
-					"header location.\n", bootblock);
-				return 1;
+		case CBFS_COMPONENT_PAYLOAD:
+			payload  = (struct cbfs_payload_segment*)
+					CBFS_SUBHEADER(entry);
+			while (payload) {
+				cbfs_print_payload_segment_info(payload);
+				if (payload->type == PAYLOAD_SEGMENT_ENTRY)
+					break;
+				else
+					payload ++;
 			}
-			p += (sizeof(unsigned int));
-		}
-		unsigned int u = ALIGN((unsigned int)(p - romarea), align);
-		master_header = (struct cbfs_header *)(romarea + u);
-
-		master_header->magic = ntohl(CBFS_HEADER_MAGIC);
-		master_header->version = ntohl(CBFS_HEADER_VERSION);
-		master_header->romsize = htonl(romsize);
-		master_header->bootblocksize = htonl(bootblocksize);
-		master_header->align = htonl(align);
-		master_header->offset = htonl(
-				ALIGN((0x40 + bootblocksize), align));
-		master_header->architecture = htonl(CBFS_ARCHITECTURE_ARMV7);
-
-		((uint32_t *) phys_to_virt(0x4 + offs))[0] =
-				virt_to_phys(master_header);
-
-		recalculate_rom_geometry(romarea);
-
-		cbfs_create_empty_file(
-				offs + ALIGN((0x40 + bootblocksize), align),
-				romsize - offs - sizeof(struct cbfs_file) -
-				ALIGN((bootblocksize + 0x40), align));
-		break;
-
-	case CBFS_ARCHITECTURE_X86:
-		// Set up physical/virtual mapping
-		offset = romarea + romsize - 0x100000000ULL;
-
-		loadfile(bootblock, &bootblocksize, romarea + romsize,
-			 SEEK_END);
-		master_header = (struct cbfs_header *)(romarea + romsize -
-				  bootblocksize - sizeof(struct cbfs_header));
-
-		master_header->magic = ntohl(CBFS_HEADER_MAGIC);
-		master_header->version = ntohl(CBFS_HEADER_VERSION);
-		master_header->romsize = htonl(romsize);
-		master_header->bootblocksize = htonl(bootblocksize);
-		master_header->align = htonl(align);
-		master_header->offset = htonl(offs);
-		master_header->architecture = htonl(CBFS_ARCHITECTURE_X86);
-
-		((uint32_t *) phys_to_virt(CBFS_HEADPTR_ADDR_X86))[0] =
-		    virt_to_phys(master_header);
-
-		recalculate_rom_geometry(romarea);
-
-		cbfs_create_empty_file((0 - romsize + offs) & 0xffffffff,
-				       romsize - offs - bootblocksize -
-				       sizeof(struct cbfs_header) -
-				       sizeof(struct cbfs_file) - 16);
-		break;
-
-	default:
-		// Should not happen.
-		fprintf(stderr, "E: You found a bug in cbfstool.\n");
-		exit(1);
+			break;
+		default:
+			break;
 	}
-
-	writerom(romfile, romarea, romsize);
-	free(romarea);
 	return 0;
 }
 
-static int in_segment(int addr, int size, int gran)
-{
-	return ((addr & ~(gran - 1)) == ((addr + size) & ~(gran - 1)));
+int cbfs_print_directory(struct cbfs_image *image) {
+	cbfs_print_header_info(image);
+	printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
+	cbfs_walk(image, cbfs_print_entry_info, NULL);
+	return 0;
 }
 
-uint32_t cbfs_find_location(const char *romfile, uint32_t filesize,
-			    const char *filename, uint32_t alignment)
-{
-	void *rom;
-	size_t filename_size, headersize, totalsize;
-	int ret = 0;
-	uint32_t current;
-
-	rom = loadrom(romfile);
-	if (rom == NULL) {
-		fprintf(stderr, "E: Could not load ROM image '%s'.\n",
-			romfile);
+int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
+			   void *arg) {
+	struct cbfs_file *next, *last;
+	uint32_t type;
+	size_t newlen;
+	type = ntohl(entry->type);
+	if (type != CBFS_COMPONENT_NULL && type != CBFS_COMPONENT_DELETED)
+		return 0;
+	next = cbfs_find_next_entry(image, entry);
+	if (!next || !cbfs_is_valid_entry(next))
+		return -1;
+	last = cbfs_find_next_entry(image, next);
+	assert(last);
+	type = ntohl(next->type);
+	if (type != CBFS_COMPONENT_NULL && type != CBFS_COMPONENT_DELETED)
 		return 0;
-	}
 
-	filename_size = strlen(filename);
-	headersize = sizeof(struct cbfs_file) + ALIGN(filename_size + 1, 16) +
-			sizeof(struct cbfs_stage);
-	totalsize = headersize + filesize;
+	// Now, we find two deleted/empty entries; try to merge now.
+	DEBUG("cbfs_join_empty_entry: combine 0x%x+0x%x and 0x%x+0x%x.\n",
+	      cbfs_get_entry_addr(image, entry), ntohl(entry->len),
+	      cbfs_get_entry_addr(image, next), ntohl(next->len));
+	entry->type = htonl(CBFS_COMPONENT_NULL);
+	newlen = (cbfs_get_entry_addr(image, last) -
+		  cbfs_get_entry_addr(image, entry) - ntohl(entry->offset));
+	entry->len = htonl(newlen);
+	DEBUG("new empty entry: length=0x%zx\n", newlen);
+	memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, newlen);
+	// TODO we should try again until no more entries are found...
+	return 0;
+}
 
-	current = phys_start;
-	while (current < phys_end) {
-		uint32_t top;
-		struct cbfs_file *thisfile;
+int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback,
+	      void *arg) {
+	int count = 0;
+	struct cbfs_file *entry;
+	for (entry = cbfs_find_first_entry(image);
+	     entry && cbfs_is_valid_entry(entry);
+	     entry = cbfs_find_next_entry(image, entry)) {
+		count ++;
+		if (callback(image, entry, arg) != 0)
+			break;
+	}
+	return count;
+}
 
-		if (!cbfs_file_header(current)) {
-			current += align;
+struct cbfs_header *cbfs_find_header(char *data, size_t size) {
+	size_t offset;
+	int found = 0;
+	uint32_t x86sig;
+	struct cbfs_header *header, *result = NULL;
+
+	// Try x86 style (check signature in bottom) header first.
+	x86sig = *(uint32_t*)(data + size - sizeof(uint32_t));
+	offset = (x86sig + (uint32_t)size);
+	DEBUG("x86sig: 0x%x, offset: 0x%zx\n", x86sig, offset);
+	if (offset >= size - sizeof(*header) ||
+	    ntohl(((struct cbfs_header*)(data + offset))->magic) !=
+	    CBFS_HEADER_MAGIC)
+		offset = 0;
+
+	for (; offset + sizeof(*header) < size; offset++) {
+		header = (struct cbfs_header*)(data + offset);
+		if (ntohl(header->magic) !=(CBFS_HEADER_MAGIC))
+		    continue;
+		if (ntohl(header->version) != CBFS_HEADER_VERSION1 &&
+		    ntohl(header->version) != CBFS_HEADER_VERSION2) {
+			// Probably not a real CBFS header?
 			continue;
 		}
+		found++;
+		result = header;
+	}
+	if (found > 1) {
+		ERROR("multiple (%d) CBFS headers found!\n",
+		       found);
+		result = NULL;
+	}
+	return result;
+}
 
-		thisfile = (struct cbfs_file *)phys_to_virt(current);
+struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image) {
+	assert(image && image->header);
+	return (struct cbfs_file*)(image->buffer.data +
+				   ntohl(image->header->offset));
+}
 
-		top = current + ntohl(thisfile->len) + ntohl(thisfile->offset);
+struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
+				       struct cbfs_file *entry) {
+	uint32_t addr = cbfs_get_entry_addr(image, entry);
+	int align = ntohl(image->header->align);
+	assert(entry && cbfs_is_valid_entry(entry));
+	addr += ntohl(entry->offset) + ntohl(entry->len);
+	addr = align_up(addr, align);
+	return (struct cbfs_file*)(image->buffer.data + addr);
+}
 
-		if (((ntohl(thisfile->type) == 0x0)
-		     || (ntohl(thisfile->type) == 0xffffffff))
-		    && (ntohl(thisfile->len) + ntohl(thisfile->offset) >=
-			totalsize)) {
-			if (in_segment
-			    (current + headersize, filesize, alignment)) {
-				ret = current + headersize;
-				break;
-			}
-			if ((ALIGN(current, alignment) + filesize < top)
-			    && (ALIGN(current, alignment) - headersize >
-				current)
-			    && in_segment(ALIGN(current, alignment), filesize,
-					  alignment)) {
-				ret = ALIGN(current, alignment);
-				break;
-			}
-			if ((ALIGN(current, alignment) + alignment + filesize <
-			     top)
-			    && (ALIGN(current, alignment) + alignment -
-				headersize > current)
-			    && in_segment(ALIGN(current, alignment) + alignment,
-					  filesize, alignment)) {
-				ret = ALIGN(current, alignment) + alignment;
-				break;
-			}
-		}
-		current =
-		    ALIGN(current + ntohl(thisfile->len) +
-			  ntohl(thisfile->offset), align);
-	}
+uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry) {
+	assert(image && image->buffer.data && entry);
+	return (int32_t)((char*)entry - image->buffer.data);
+}
+
+int cbfs_is_valid_entry(struct cbfs_file *entry) {
+	return (entry &&memcmp(entry->magic, CBFS_FILE_MAGIC,
+			       sizeof(entry->magic)) == 0);
+}
 
-	free(rom);
-	return ret;
+int cbfs_init_entry(struct cbfs_file *entry,
+		    struct buffer *buffer) {
+	memset(entry, 0, sizeof(*entry));
+	memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
+	entry->len = htonl(buffer->size);
+	entry->offset = htonl(sizeof(*entry) + strlen(buffer->name) + 1);
+	return 0;
+}
+
+int cbfs_create_entry(struct cbfs_image *image, struct cbfs_file *entry,
+		      size_t len, const char *name) {
+	memset(entry, CBFS_CONTENT_DEFAULT_VALUE, sizeof(*entry));
+	memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
+	entry->type = htonl(CBFS_COMPONENT_NULL);
+	entry->len = htonl(len);
+	entry->checksum = 0;  // TODO Build a checksum algorithm.
+	entry->offset = htonl(sizeof(*entry) + align_up(strlen(name) + 1,
+							CBFS_FILENAME_ALIGN));
+	memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry));
+	strcpy(CBFS_NAME(entry), name);
+	memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len);
+	return 0;
 }
diff --git a/util/cbfstool/common.h b/util/cbfstool/common.h
index 853e529..77bd100 100644
--- a/util/cbfstool/common.h
+++ b/util/cbfstool/common.h
@@ -2,6 +2,7 @@
  * Copyright (C) 2009 coresystems GmbH
  *                 written by Patrick Georgi <patrick.georgi at coresystems.de>
  * Copyright (C) 2012 Google, Inc.
+ * Copyright (C) 2013 The ChromiumOS Authors.  All rights reserved.
  *
  * 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
@@ -21,71 +22,172 @@
 #define __CBFSTOOL_COMMON_H
 
 #include <stdint.h>
+#include "cbfs.h"
+
+/* Message output */
+extern int verbose;
+#define ERROR(x...) { fprintf(stderr, "ERROR: " x); }
+#define WARN(x...) { fprintf(stderr, "WARN: " x); }
+#define LOG(x...) { fprintf(stderr, x); }
+#define INFO(x...) { if (verbose > 0) fprintf(stderr, "INFO: " x); }
+#define DEBUG(x...) { if (verbose > 1) fprintf(stderr, "DEBUG: " x); }
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+#define IS_TOP_ALIGNED_ADDRESS(x)	((uint32_t)(x) > 0x80000000)
+
+/* Endianess */
+#include <arpa/inet.h>	// for ntohl, htonl
 #include "swab.h"
-#ifndef __APPLE__
-#define ntohl(x)  (host_bigendian?(x):swab32(x))
-#define htonl(x)  (host_bigendian?(x):swab32(x))
-#endif
-#define ntohll(x) (host_bigendian?(x):swab64(x))
-#define htonll(x) (host_bigendian?(x):swab64(x))
+int is_big_endian(void);
+#define ntohll(x)	(is_big_endian() ? (x) : swab64(x))
+#define htonll(x)	(is_big_endian() ? (x) : swab64(x))
 
-extern void *offset;
-extern uint32_t romsize;
-extern int host_bigendian;
-extern uint32_t arch;
+/* Buffer and file I/O */
+struct buffer {
+	char *name;
+	char *data;
+	size_t size;
+};
 
-const char *arch_to_string(uint32_t a);
-uint32_t string_to_arch(const char *arch_string);
+/* Creates an empty memory buffer with given size.
+ * Returns 0 on success, otherwise non-zero. */
+int buffer_create(struct buffer *buffer, size_t size, const char *name);
 
-static inline void *phys_to_virt(uint32_t addr)
-{
-	return offset + addr;
-}
+/* Loads a file into memory buffer. Returns 0 on success, otherwise non-zero. */
+int buffer_from_file(struct buffer *buffer, const char *filename);
 
-static inline uint32_t virt_to_phys(void *addr)
-{
-	return (unsigned long)(addr - offset) & 0xffffffff;
-}
+/* Writes memory buffer content into file.
+ * Returns 0 on success, otherwise non-zero. */
+int buffer_write_file(struct buffer *buffer, const char *filename);
 
-#define ALIGN(val, by) (((val) + (by)-1)&~((by)-1))
+/* Destroys a memory buffer. Returns 0 on success, otherwise non-zero. */
+int buffer_delete(struct buffer *buffer);
 
-size_t getfilesize(const char *filename);
-void *loadfile(const char *filename, uint32_t * romsize_p, void *content,
-	       int place);
-void *loadrom(const char *filename);
-int writerom(const char *filename, void *start, uint32_t size);
 
-int iself(unsigned char *input);
+/* Type and format */
+
+/* Returns 1 if input points to an ELF object, otherwise 0. */
+int is_elf_object(const void *input);
+
+/* Returns the value from CBFS entry type name, or default_value if
+ * the name is not found. */
+uint32_t get_cbfs_entry_type(const char *name, uint32_t default_value);
+/* Returns the compression name, or default_value if not found. */
+uint32_t get_cbfs_compression(const char *name, uint32_t default_value);
 
-typedef void (*comp_func_ptr) (char *, int, char *, int *);
-typedef enum { CBFS_COMPRESS_NONE = 0, CBFS_COMPRESS_LZMA = 1 } comp_algo;
+/* Prints all known CBFS entry types. */
+void print_all_cbfs_entry_types();
 
-comp_func_ptr compression_function(comp_algo algo);
 
-uint64_t intfiletype(const char *name);
+/* CBFS image processing */
+
+struct cbfs_image {
+	struct buffer buffer;
+	struct cbfs_header *header;
+};
+
+/* Creates an empty CBFS image by given size, and description to its content
+ * (bootblock, align, header location, starting offset of CBFS entries.
+ * The output image will contain a valid cbfs_header, with one cbfs_file
+ * entry with type CBFS_COMPONENT_NULL, with max available size.
+ * Returns 0 on success, otherwise none-zero. */
+int cbfs_image_create(struct cbfs_image *image,
+		      size_t size,
+		      uint32_t align,
+		      struct buffer *bootblock,
+		      int32_t bootblock_offset,
+		      int32_t header_offset,
+		      int32_t entries_offset);
+
+
+/* Loads a CBFS image from file. Returns 0 on success, otherwise non-zero. */
+int cbfs_image_from_file(struct cbfs_image *image, const char *filename);
+
+/* Writes a CBFS image into file. Returns 0 on success, otherwise non-zero. */
+int cbfs_image_write_file(struct cbfs_image *image, const char *filename);
+
+/* Releases the CBFS image. Returns 0 on success, otherwise non-zero. */
+int cbfs_image_delete(struct cbfs_image *image);
 
-int parse_elf_to_payload(unsigned char *input, unsigned char **output,
-			 comp_algo algo);
-int parse_elf_to_stage(unsigned char *input, unsigned char **output,
-		       comp_algo algo, uint32_t * location);
+/* Adds an entry to CBFS image by given name and type. If offset is non-zero,
+ * try to add at assigned location.
+ * Returns 0 on success, otherwise non-zero. */
+int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
+		   const char *name, uint32_t type, uint32_t offset);
 
-void *create_cbfs_file(const char *filename, void *data, uint32_t * datasize,
-		       uint32_t type, uint32_t * location);
+/* Removes an entry from CBFS image. Returns 0 on success, otherwise non-zero. */
+int cbfs_remove_entry(struct cbfs_image *image, const char *name);
 
-int create_cbfs_image(const char *romfile, uint32_t romsize,
-		      const char *bootblock, uint32_t align, uint32_t offs);
+/* Returns a pointer to entry by name, or NULL if name is not found. */
+struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name);
 
-int add_file_to_cbfs(void *content, uint32_t contentsize, uint32_t location);
-int remove_file_from_cbfs(const char *filename);
-void print_cbfs_directory(const char *filename);
-int extract_file_from_cbfs(const char *filename, const char *payloadname, const char *outpath);
-int remove_file_from_cbfs(const char *filename);
+/* Exports an entry to external file.
+ * Returns 0 on success, otherwise (ex, not found) non-zero. */
+int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
+		      const char *filename);
 
-uint32_t cbfs_find_location(const char *romfile, uint32_t filesize,
-			    const char *filename, uint32_t align);
+/* Initializes a new entry with size and name in CBFS image.
+ * Returns 0 on success, otherwise (ex, not found) non-zero. */
+int cbfs_create_entry(struct cbfs_image *image, struct cbfs_file *entry,
+		      size_t len, const char *name);
 
-void print_supported_filetypes(void);
+/* Callback function used by cbfs_walk.
+ * Returns 0 on success, or non-zero to stop further iteration. */
+typedef int (*cbfs_entry_callback)(struct cbfs_image *image,
+				   struct cbfs_file *file,
+				   void *arg);
 
-#define ARRAY_SIZE(a) (int)(sizeof(a) / sizeof((a)[0]))
+/* Iterates through all entries in CBFS image, and invoke with callback.
+ * Stops if callback returns non-zero values.
+ * Returns number of entries invoked. */
+int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback, void *arg);
+
+/* Primitive CBFS utilities */
+
+/* Returns a pointer to the only valid CBFS header in give buffer, otherwise
+ * NULL (including when multiple headers were found). If there is a X86 ROM
+ * style signature (pointer at 0xfffffffc) found in ROM, it will be selected as
+ * the only header.*/
+struct cbfs_header *cbfs_find_header(char *data, size_t size);
+
+/* Returns the first cbfs_file entry in CBFS image by CBFS header (no matter if
+ * the entry has valid content or not), otherwise NULL. */
+struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image);
+
+/* Returns next cbfs_file entry (no matter if its content is valid or not), or
+ * NULL on failure. */
+struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
+				       struct cbfs_file *entry);
+
+/* Returns ROM address (offset) of entry.
+ * This is different from entry->offset (pointer to content). */
+uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry);
+
+int cbfs_print_directory(struct cbfs_image *image);
+int cbfs_print_header_info(struct cbfs_image *image);
+int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
+			  void *arg);
+int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
+			   void *arg);
+
+/* Returns 1 if entry has valid data (by checking magic number), otherwise 0. */
+int cbfs_is_valid_entry(struct cbfs_file *entry);
+
+/* Utilities */
+
+/* compress.c */
+typedef void (*comp_func_ptr) (char *, int, char *, int *);
+typedef enum {
+	CBFS_COMPRESS_NONE = 0,
+	CBFS_COMPRESS_LZMA
+} comp_algo;
+comp_func_ptr compression_function(comp_algo algo);
 
+/* cbfs-mkpayload.c */
+int parse_elf_to_payload(char *input, char **output, comp_algo algo);
+int parse_flat_binary_to_payload(char *input, char **output,
+				 uint32_t input_size, uint32_t loadaddress,
+				 uint32_t entrypoint, comp_algo algo);
+/* cbfs-mkstage.c */
+int parse_elf_to_stage(char *input, char **output, comp_algo algo,
+		       uint32_t location);
 #endif



More information about the coreboot mailing list