[coreboot] Patch set updated for coreboot: aff1e4e Improve CBFS tool to support non-x86 platform better.

Hung-Te Lin (hungte@chromium.org) gerrit at coreboot.org
Sat Jan 26 12:21:18 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 aff1e4eb99db0d2b8a3d4802469afdf729da330b
Author: Hung-Te Lin <hungte at chromium.org>
Date:   Sat Jan 26 02:03:28 2013 +0800

    Improve CBFS tool to support non-x86 platform better.
    
    CBFS tool was written with lots of global variables and implemented with
    assumptions based on x86 architecture. To support other platforms better, we
    should rewrite using logical offset.
    
    Verified to work on ARM (google/snow).
    Does not work on x86 yet.
    
    DO NOT SUBMIT. WIP.
    
    Change-Id: I28c737c8f290e51332119188248ac9e28042024c
    Signed-off-by: Hung-Te Lin <hungte at chromium.org>
---
 src/arch/armv7/Makefile.inc    |    4 +-
 util/cbfstool/cbfs-mkpayload.c |   82 +++-
 util/cbfstool/cbfs-mkstage.c   |   22 +-
 util/cbfstool/cbfs.h           |    3 +
 util/cbfstool/cbfstool.c       |  586 +++++++++-------------
 util/cbfstool/common.c         | 1066 +++++++++++++++++++++-------------------
 util/cbfstool/common.h         |  170 +++++--
 7 files changed, 989 insertions(+), 944 deletions(-)

diff --git a/src/arch/armv7/Makefile.inc b/src/arch/armv7/Makefile.inc
index 872365c..d32b530 100644
--- a/src/arch/armv7/Makefile.inc
+++ b/src/arch/armv7/Makefile.inc
@@ -46,9 +46,11 @@ 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.
 $(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
+		-B $(objcbfs)/bootblock.bin -a 64 -A 0x0000 -H 0x2040 \
+		-o 0x5000
 	$(prebuild-files) true
 	mv $@.tmp $@
 else
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..645f505 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,
+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;
 	}
 
diff --git a/util/cbfstool/cbfs.h b/util/cbfstool/cbfs.h
index 3dbeefd..35d0670 100644
--- a/util/cbfstool/cbfs.h
+++ b/util/cbfstool/cbfs.h
@@ -42,6 +42,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 +108,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..04f48df 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
@@ -26,7 +27,6 @@
 #include <unistd.h>
 #include <getopt.h>
 #include "common.h"
-#include "cbfs.h"
 
 struct command {
 	const char *name;
@@ -35,23 +35,34 @@ struct command {
 };
 
 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;
+struct param {
+	char *cbfs_name;
+	char *rom_name;
+	char *rom_filename;
+	char *rom_bootblock;
+	uint32_t rom_type;
+	uint32_t rom_baseaddress;
+	uint32_t rom_loadaddress;
+	uint32_t rom_bootaddress;
+	uint32_t rom_headeraddress;
+	uint32_t rom_entrypoint;
+	uint32_t rom_size;
+	uint32_t rom_alignment;
+	uint32_t rom_offset;
+	comp_algo rom_algo;
+} param;
+
+typedef int (*convert_buffer_t)(struct buffer *buffer);
+
+static int cbfs_add_file(const char *rom_filename,
+			 const char *rom_name,
+			 uint32_t rom_type,
+			 convert_buffer_t convert,
+			 const char *cbfs_name) {
+	struct cbfs_image image;
+	struct buffer buffer;
+	int result = 1;
 
 	if (!rom_filename) {
 		fprintf(stderr, "E: You need to specify -f/--filename.\n");
@@ -68,404 +79,286 @@ static int cbfs_add(void)
 		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 (buffer_from_file(&buffer, rom_filename) != 0) {
+		fprintf(stderr, "E: Could not load file '%s'.\n", rom_filename);
+		cbfs_image_delete(&image);
 		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);
-		return 1;
-	}
+	do {
+		if (convert && convert(&buffer) != 0) {
+			fprintf(stderr, "E: Failed to parse file '%s'.\n",
+				rom_filename);
+			break;
+		}
+		if (cbfs_add_entry(&image, rom_name, rom_type,
+				   &buffer) != 0)
+			break;
+		if (cbfs_image_write_file(&image, cbfs_name) != 0)
+			break;
+
+		result = 0;
+	} while (0);
+
+	buffer_delete(&buffer);
+	cbfs_image_delete(&image);
+	return result;
+}
 
-	free(cbfsfile);
-	free(rom);
+static int cbfs_convert_mkstage(struct buffer *buffer) {
+	char *name;
+	char *output;
+	int result = parse_elf_to_stage(buffer->data, &output, param.rom_algo,
+					&param.rom_baseaddress);
+	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;
 }
 
-static int cbfs_add_payload(void)
-{
-	int32_t filesize = 0;
-	void *rom, *filedata, *cbfsfile;
-	unsigned char *payload;
-
-	if (!rom_filename) {
-		fprintf(stderr, "E: You need to specify -f/--filename.\n");
-		return 1;
-	}
-
-	if (!rom_name) {
-		fprintf(stderr, "E: You need to specify -n/--name.\n");
-		return 1;
-	}
-
-	rom = loadrom(cbfs_name);
-	if (rom == NULL) {
-		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);
-		return 1;
-	}
-
-	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;
-	}
-
-	cbfsfile = create_cbfs_file(rom_name, payload, &filesize,
-				CBFS_COMPONENT_PAYLOAD, &rom_baseaddress);
-
-	free(filedata);
-	free(payload);
-
-	if (add_file_to_cbfs(cbfsfile, filesize, rom_baseaddress)) {
-		fprintf(stderr, "E: Adding payload '%s' failed.\n",
-			rom_filename);
-		free(cbfsfile);
-		free(rom);
-		return 1;
-	}
-
-	if (writerom(cbfs_name, rom, romsize)) {
-		free(cbfsfile);
-		free(rom);
-		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.rom_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;
+}
 
-	free(cbfsfile);
-	free(rom);
+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.rom_loadaddress,
+					      param.rom_entrypoint,
+					      param.rom_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;
 }
 
-static int cbfs_add_stage(void)
+static int cbfs_add(void)
 {
-	uint32_t filesize = 0;
-	void *rom, *filedata, *cbfsfile;
-	unsigned char *stage;
-
-	if (!rom_filename) {
-		fprintf(stderr, "E: You need to specify -f/--filename.\n");
-		return 1;
-	}
-
-	if (!rom_name) {
-		fprintf(stderr, "E: You need to specify -n/--name.\n");
-		return 1;
-	}
-
-	rom = loadrom(cbfs_name);
-	if (rom == NULL) {
-		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);
-		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);
+	// TODO where is rom_baseaddress?
+	return cbfs_add_file(param.rom_filename,
+			     param.rom_name,
+			     param.rom_type,
+			     NULL,
+			     param.cbfs_name);
+}
 
-	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;
-	}
+static int cbfs_add_payload(void)
+{
+	return cbfs_add_file(param.rom_filename,
+			     param.rom_name,
+			     CBFS_COMPONENT_PAYLOAD,
+			     cbfs_convert_elf_mkpayload,
+			     param.cbfs_name);
+}
 
-	free(cbfsfile);
-	free(rom);
-	return 0;
+static int cbfs_add_stage(void)
+{
+	return cbfs_add_file(param.rom_filename,
+			     param.rom_name,
+			     CBFS_COMPONENT_STAGE,
+			     cbfs_convert_mkstage,
+			     param.cbfs_name);
 }
 
 static int cbfs_add_flat_binary(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;
-	}
-
-	if (!rom_name) {
-		fprintf(stderr, "E: You need to specify -n/--name.\n");
-		return 1;
-	}
-
-	if (rom_loadaddress == 0) {
+	if (param.rom_loadaddress == 0) {
 		fprintf(stderr, "E: You need to specify a valid "
 			"-l/--load-address.\n");
 		return 1;
 	}
-
-	if (rom_entrypoint == 0) {
+	if (param.rom_entrypoint == 0) {
 		fprintf(stderr, "E: You need to specify a valid "
 			"-e/--entry-point.\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);
-		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;
-	}
-
-	/* 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;
-	}
-
-	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);
-	}
-
-	/* 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);
-		return 1;
-	}
-	if (writerom(cbfs_name, rom, romsize)) {
-		free(cbfsfile);
-		free(rom);
-		return 1;
-	}
-
-	free(cbfsfile);
-	free(rom);
-	return 0;
+	return cbfs_add_file(param.rom_filename,
+			     param.rom_name,
+			     CBFS_COMPONENT_PAYLOAD,
+			     cbfs_convert_flat_mkpayload,
+			     param.cbfs_name);
 }
 
 static int cbfs_remove(void)
 {
-	void *rom;
+	int result = 1;
+	struct cbfs_image image;
 
-	if (!rom_name) {
+	if (!param.rom_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;
-	}
-
-	if (remove_file_from_cbfs(rom_name)) {
-		fprintf(stderr, "E: Removing file '%s' failed.\n",
-			rom_name);
-		free(rom);
+			param.cbfs_name);
 		return 1;
 	}
 
-	if (writerom(cbfs_name, rom, romsize)) {
-		free(rom);
-		return 1;
-	}
+	do {
+		if (cbfs_remove_entry(&image, param.rom_name) != 0) {
+			fprintf(stderr, "E: Removing file '%s' failed.\n",
+				param.rom_name);
+			break;
+		}
+		if (cbfs_image_write_file(&image, param.cbfs_name) != 0)
+			break;
+		result = 0;
+	} while (0);
 
-	free(rom);
-	return 0;
+	cbfs_image_delete(&image);
+	return result;
 }
 
 static int cbfs_create(void)
 {
-	if (rom_size == 0) {
+	struct cbfs_image image;
+	struct buffer bootblock;
+
+	if (param.rom_size == 0) {
 		fprintf(stderr, "E: You need to specify a valid -s/--size.\n");
 		return 1;
 	}
 
-	if (!rom_bootblock) {
+	if (!param.rom_bootblock) {
 		fprintf(stderr, "E: You need to specify -b/--bootblock.\n");
 		return 1;
 	}
 
-	if (arch == CBFS_ARCHITECTURE_UNKNOWN) {
-		fprintf(stderr, "E: You need to specify -m/--machine arch\n");
+	if (buffer_from_file(&bootblock, param.rom_bootblock) != 0) {
 		return 1;
 	}
 
-	return create_cbfs_image(cbfs_name, rom_size, rom_bootblock,
-						rom_alignment, rom_offset);
+	if (cbfs_create_image(&image,
+			      param.rom_size,
+			      &bootblock,
+			      param.rom_alignment,
+			      param.rom_bootaddress,
+			      param.rom_headeraddress,
+			      param.rom_offset) != 0) {
+		fprintf(stderr, "E: Failed to create %s.\n", param.cbfs_name);
+		return 1;
+	}
+	if (cbfs_image_write_file(&image, param.cbfs_name) != 0) {
+		fprintf(stderr, "E: Failed to write %s.\n", param.cbfs_name);
+		return 1;
+	}
+	cbfs_image_delete(&image);
+	return 0;
+
 }
 
 static int cbfs_locate(void)
 {
+#if 0
+	struct cbfs_image image;
 	uint32_t filesize, location;
 
-	if (!rom_filename) {
+	if (!param.rom_filename) {
 		fprintf(stderr, "E: You need to specify -f/--filename.\n");
 		return 1;
 	}
 
-	if (!rom_name) {
+	if (!param.rom_name) {
 		fprintf(stderr, "E: You need to specify -n/--name.\n");
 		return 1;
 	}
 
-	filesize = getfilesize(rom_filename);
+	filesize = getfilesize(param.rom_filename);
 
-	location = cbfs_find_location(cbfs_name, filesize,
-					rom_name, rom_alignment);
+	location = cbfs_find_location(param.cbfs_name, filesize,
+					param.rom_name, param.rom_alignment);
 
 	printf("%x\n", location);
 	return location == 0 ? 1 : 0;
+#endif
+	return 0;
 }
 
 static int cbfs_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;
 	}
+	cbfs_print_header_info(&image);
+	printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
+	cbfs_walk(&image, cbfs_print_entry_info, &verbose);
 
-	print_cbfs_directory(cbfs_name);
-
-	free(rom);
+	cbfs_image_delete(&image);
 	return 0;
 }
 
 static int cbfs_extract(void)
 {
-	void *rom;
-	int ret;
+	int result = 0;
+	struct cbfs_image image;
 
-	if (!rom_filename) {
+	if (!param.rom_filename) {
 		fprintf(stderr, "E: You need to specify -f/--filename.\n");
 		return 1;
 	}
 
-	if (!rom_name) {
+	if (!param.rom_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.rom_name,
+				     param.rom_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?", cbfs_add},
+	{"add-payload", "vf:n:t:c:b:h?", cbfs_add_payload},
+	{"add-stage", "vf:n:t:c:b:h?", cbfs_add_stage},
+	{"add-flat-binary", "vf:n:l:e:c:b:h?", cbfs_add_flat_binary},
+	{"remove", "vn:h?", cbfs_remove},
+	{"create", "vs:B:A:H:a:o:m:h?", cbfs_create},
+	{"locate", "vf:n:a:h?", cbfs_locate},
+	{"print", "vh?", cbfs_print},
+	{"extract", "vn:f:h?", cbfs_extract},
 };
 
 static struct option long_options[] = {
@@ -477,6 +370,8 @@ static struct option long_options[] = {
 	{"entry-point",  required_argument, 0, 'e' },
 	{"size",         required_argument, 0, 's' },
 	{"bootblock",    required_argument, 0, 'B' },
+	{"boot-address", required_argument, 0, 'A' },
+	{"header-address",required_argument,0, 'H' },
 	{"alignment",    required_argument, 0, 'a' },
 	{"offset",       required_argument, 0, 'o' },
 	{"file",         required_argument, 0, 'f' },
@@ -491,8 +386,9 @@ 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"
+	     "  -v             Verbose output\n"
 	     "COMMANDs:\n"
 	     " add -f FILE -n NAME -t TYPE [-b base-address]               "
 			"Add a component\n"
@@ -505,11 +401,12 @@ 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 [-A boot-address] \\\n"
+	     "        [-H header-address] -m ARCH [-a align] [-o offset]   "
 			"Create a ROM file\n"
 	     " locate -f FILE -n NAME -a align                             "
 			"Find a place for a file of that size\n"
-	     " print                                                       "
+	     " print	                                                   "
 			"Show the contents of the ROM\n"
 	     " extract -n NAME -f FILE                                     "
 			"Extracts a raw payload from ROM\n"
@@ -518,19 +415,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 +428,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 +455,68 @@ int main(int argc, char **argv)
 
 			switch(c) {
 			case 'n':
-				rom_name = optarg;
+				param.rom_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)
+				param.rom_type = get_cbfs_entry_type(optarg);
+				if (param.rom_type == (uint32_t)-1)
+					param.rom_type = strtoul(optarg, NULL,
+								 0);
+				if (param.rom_type == 0)
 					printf("W: Unknown type '%s' ignored\n",
-							optarg);
+					       optarg);
 				break;
 			case 'c':
 				if (!strncasecmp(optarg, "lzma", 5))
-					rom_algo = CBFS_COMPRESS_LZMA;
+					param.rom_algo = CBFS_COMPRESS_LZMA;
 				else if (!strncasecmp(optarg, "none", 5))
-					rom_algo = CBFS_COMPRESS_NONE;
+					param.rom_algo = CBFS_COMPRESS_NONE;
 				else
 					printf("W: Unknown compression '%s'"
-							" ignored.\n", optarg);
+					       " ignored.\n", optarg);
 				break;
 			case 'b':
-				rom_baseaddress = strtoul(optarg, NULL, 0);
+				param.rom_baseaddress = strtoul(optarg, NULL, 0);
 				break;
 			case 'l':
-				rom_loadaddress = strtoul(optarg, NULL, 0);
+				param.rom_loadaddress = strtoul(optarg, NULL, 0);
 
 				break;
 			case 'e':
-				rom_entrypoint = strtoul(optarg, NULL, 0);
+				param.rom_entrypoint = strtoul(optarg, NULL, 0);
 				break;
 			case 's':
-				rom_size = strtoul(optarg, &suffix, 0);
+				param.rom_size = strtoul(optarg, &suffix, 0);
 				if (tolower(suffix[0])=='k') {
-					rom_size *= 1024;
+					param.rom_size *= 1024;
 				}
 				if (tolower(suffix[0])=='m') {
-					rom_size *= 1024 * 1024;
+					param.rom_size *= 1024 * 1024;
 				}
 			case 'B':
-				rom_bootblock = optarg;
+				param.rom_bootblock = optarg;
+				break;
+			case 'A':
+				param.rom_bootaddress = strtoul(optarg, NULL, 0);
+				break;
+			case 'H':
+				param.rom_headeraddress = strtoul(
+						optarg, NULL, 0);
 				break;
 			case 'a':
-				rom_alignment = strtoul(optarg, NULL, 0);
+				param.rom_alignment = strtoul(optarg, NULL, 0);
 				break;
 			case 'o':
-				rom_offset = strtoul(optarg, NULL, 0);
+				param.rom_offset = strtoul(optarg, NULL, 0);
 				break;
 			case 'f':
-				rom_filename = optarg;
+				param.rom_filename = optarg;
 				break;
 			case 'v':
 				verbose++;
 				break;
 			case 'm':
-				arch = string_to_arch(optarg);
+				// arch = string_to_arch(optarg);
 				break;
 			case 'h':
 			case '?':
diff --git a/util/cbfstool/common.c b/util/cbfstool/common.c
index 97bf168..e6a6e51 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,215 +20,99 @@
  * 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...)
-
-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;
-
-	fseek(file, 0, SEEK_END);
-	size = ftell(file);
-	fclose(file);
-	return size;
-}
-
-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;
-
-	if (!fread(content, *romsize_p, 1, file)) {
-		fprintf(stderr, "E: Failed to read %s\n", filename);
-		return NULL;
+	static const uint32_t inttest = 0x12345678;
+	uint8_t inttest_lsb = *(uint8_t *)&inttest;
+	if (inttest_lsb == 0x12) {
+		return 1;
 	}
-	fclose(file);
-	return content;
+	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;
-
-static struct {
-	uint32_t arch;
-	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;
+#define CBFS_FILENAME_ALIGN	(16)
 
-	for (i = 0; i < ARRAY_SIZE(arch_names); i++) {
-		if (!strcasecmp(arch_string, arch_names[i].name)) {
-			ret = arch_names[i].arch;
-			break;
-		}
-	}
+/* Buffer and file I/O */
 
-	return ret;
+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);
 }
 
-const char *arch_to_string(uint32_t a)
-{
-	int i;
-	const char *ret = NULL;
-
-	for (i = 0; i < ARRAY_SIZE(arch_names); i++) {
-		if (a == arch_names[i].arch) {
-			ret = arch_names[i].name;
-			break;
-		}
+int buffer_from_file(struct buffer *buffer, const char *filename) {
+	FILE *fp = fopen(filename, "rb");
+	if (!fp) {
+		perror(filename);
+		return -1;
 	}
-
-	return ret;
-
-}
-
-int find_master_header(void *romarea, size_t size)
-{
-	size_t offset;
-
-	if (master_header)
-		return 0;
-
-	for (offset = 0; offset < size - sizeof(struct cbfs_header); offset++) {
-		struct cbfs_header *tmp = romarea + offset;
-
-		if (tmp->magic == ntohl(CBFS_HEADER_MAGIC)) {
-			master_header = tmp;
-			break;
-		}
+	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;
 	}
-
-	return master_header ? 0 : 1;
+	fclose(fp);
+	return 0;
 }
 
-void recalculate_rom_geometry(void *romarea)
-{
-	if (find_master_header(romarea, romsize)) {
-		fprintf(stderr, "E: Cannot find master header\n");
-		exit(1);
-	}
-
-	/* 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);
+int buffer_write_file(struct buffer *buffer, const char *filename) {
+	FILE *fp = fopen(filename, "wb");
+	if (!fp) {
+		perror(filename);
+		return -1;
 	}
-
-	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);
+	assert(buffer && buffer->data);
+	if (fwrite(buffer->data, 1, buffer->size, fp) != buffer->size) {
+		ERROR("incomplete write: %s\n", filename);
+		fclose(fp);
+		return -1;
 	}
-
-	align = ntohl(master_header->align);
-}
-
-void *loadrom(const char *filename)
-{
-	void *romarea = loadfile(filename, &romsize, 0, SEEK_SET);
-	if (romarea == NULL)
-		return NULL;
-	recalculate_rom_geometry(romarea);
-	return romarea;
+	fclose(fp);
+	return 0;
 }
 
-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;
+int buffer_delete(struct buffer *buffer) {
+	assert(buffer);
+	if (buffer->name) {
+		free(buffer->name);
+		buffer->name = NULL;
 	}
-
-	if (fwrite(start, size, 1, file) != 1) {
-		fprintf(stderr, "Could not write to '%s': ", filename);
-		perror("");
-		return 1;
+	if (buffer->data) {
+		free(buffer->data);
+		buffer->data = NULL;
 	}
-
-	fclose(file);
+	buffer->size = 0;
 	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;
-}
+/* Type and format */
 
-int iself(unsigned char *input)
-{
-	Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input;
+int is_elf_object(const void *input) {
+	Elf32_Ehdr *ehdr = (Elf32_Ehdr *)input;
 	return !memcmp(ehdr->e_ident, ELFMAG, 4);
 }
 
 static struct filetypes_t {
 	uint32_t type;
 	const char *name;
-} filetypes[] = {
+} cbfs_filetypes[] = {
 	{CBFS_COMPONENT_STAGE, "stage"},
 	{CBFS_COMPONENT_PAYLOAD, "payload"},
 	{CBFS_COMPONENT_OPTIONROM, "optionrom"},
@@ -236,206 +121,152 @@ static struct filetypes_t {
 	{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_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);
+uint32_t get_cbfs_entry_type(const char *name) {
+	size_t i;
+	for (i = 0; i < ARRAY_SIZE(cbfs_filetypes); i++)
+		if (strcmp(cbfs_filetypes[i].name, name) == 0)
+			return cbfs_filetypes[i].type;
+	return -1;
+}
 
-	for (i=0; i<number; i++) {
-		printf(" %s%c", filetypes[i].name, (i==(number-1))?'\n':',');
-		if ((i%8) == 7)
+const char * get_cbfs_entry_type_name(uint32_t type) {
+	size_t i;
+	for (i = 0; i < ARRAY_SIZE(cbfs_filetypes); i++)
+		if (cbfs_filetypes[i].type == type)
+			return cbfs_filetypes[i].name;
+	return "(unknown)";
+}
+
+void print_all_cbfs_entry_types(void) {
+	size_t i, total = ARRAY_SIZE(cbfs_filetypes);
+
+	for (i = 0; i < total; i++) {
+		printf(" %s%c", cbfs_filetypes[i].name,
+		       (i + 1 == total) ? '\n' : ',');
+		if ((i + 1) % 8 == 0)
 			printf("\n");
 	}
 }
 
-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";
+/* CBFS image processing */
+int cbfs_image_create(struct cbfs_image *image, size_t size) {
+	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, -1, size);
+	return 0;
 }
 
-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_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;
+	}
+	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;
-				}
+int cbfs_image_write_file(struct cbfs_image *image, const char *filename) {
+	assert(image && image->buffer.data);
+	return buffer_write_file(&image->buffer, filename);
+}
 
-				if(payload->type == PAYLOAD_SEGMENT_ENTRY)
-					payload=NULL;
-				else
-					payload++;
-			}
-			break;
-		}
-		default:
-			break;
-		}
-		current =
-		    ALIGN(current + ntohl(thisfile->len) +
-			  ntohl(thisfile->offset), align);
-	}
+int cbfs_image_delete(struct cbfs_image *image) {
+	buffer_delete(&image->buffer);
+	image->header = NULL;
+	return 0;
 }
 
-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;
-		}
+static uint32_t align_up(uint32_t value, uint32_t align) {
+	if (value % align)
+		value += align - (value % align);
+	return value;
+}
+
+int cbfs_add_entry(struct cbfs_image *image, const char *name,
+		   uint32_t type, struct buffer *buffer) {
+	uint32_t entry_type, entry_offset, entry_capacity;
+	uint32_t addr;
+	struct cbfs_file *entry, old_entry;
+	uint32_t need_size, new_size;
 
-		// 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);
+	need_size = align_up(strlen(name) + 1, CBFS_FILENAME_ALIGN);
+	need_size += sizeof(*entry);
+	need_size += buffer->size;
+	need_size = align_up(need_size, ntohl(image->header->align));
+
+	DEBUG("cbfs_add_entry('%s', %zd) => need_size = %u\n",
+	      name, buffer->size, need_size);
+
+	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);
+		entry_offset = ntohl(entry->offset);
+		entry_capacity = entry_offset + ntohl(entry->len);
 
-		if (ntohl(thisfile->type) != CBFS_COMPONENT_RAW)
-		{
-			fprintf(stderr, "W: Only 'raw' files are safe to extract.\n");
-		}
+		DEBUG("cbfs_add_entry: space %s at 0x%x, %d bytes\n",
+		      get_cbfs_entry_type_name(entry_type), addr,
+		      entry_capacity);
 
-		fwrite(((char *)thisfile)
-				+ ntohl(thisfile->offset), length, 1, outfile);
+		DEBUG("capacity=%d, need_size=%d\n", entry_capacity, need_size);
 
-		fclose(outfile);
-		printf("Successfully dumped the file.\n");
+		if (entry_capacity < need_size)
+			continue;
 
-		// We'll only dump one file.
+		// fill entry
+		DEBUG("filling new entry data (%zd bytes).\n", buffer->size);
+		memcpy(&old_entry, entry, sizeof(old_entry));
+		entry->len = htonl(buffer->size);
+		entry->type = htonl(type);
+		entry->offset = htonl(align_up(sizeof(*entry) + strlen(name) + 1,
+					       CBFS_FILENAME_ALIGN));
+		strcpy(CBFS_NAME(entry), name);
+		memcpy(CBFS_SUBHEADER(entry), buffer->data, buffer->size);
+		if (verbose)
+			cbfs_print_entry_info(image, entry, NULL);
+
+		// setup new entry
+		entry = cbfs_find_next_entry(image, entry);
+		assert(entry);
+		new_size = ntohl(old_entry.len);
+		new_size -= (cbfs_get_entry_addr(image, entry) - addr);
+		old_entry.len = htonl(new_size);
+		memcpy(entry, &old_entry, sizeof(old_entry));
+		*CBFS_NAME(entry) = 0;
+		if (verbose)
+			cbfs_print_entry_info(image, entry, NULL);
 		return 0;
-	}
-	fprintf(stderr, "E: File %s not found.\n", payloadname);
-	return 1;
-}
 
+		// assume NULL is 
+		// assume we always have good NULL to add.
 
-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 *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);
+		// See if we can update current record.
+		//if (buffer->size <= len && 
+		 //   (location == 0 || current == location))
+	}
+
+#if 0
 			/* if this is the right size, and if specified, the right location, use it */
 			if ((contentsize <= length)
 			    && ((location == 0) || (current == location))) {
@@ -490,66 +321,400 @@ int add_file_to_cbfs(void *content, uint32_t contentsize, uint32_t location)
 		    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;
+#endif
+	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;
+	char *entry_name;
+	size_t len, name_len;
+	entry = cbfs_get_entry(image, name);
+	if (!entry) {
+		ERROR("CBFS file %s not found.\n", name);
+		return -1;
+	}
+	entry_name = CBFS_NAME(entry);
+	name_len = ntohl(entry->offset) - sizeof(*entry);
+	DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n",
+	      name, cbfs_get_entry_addr(image, entry));
+	// entry->type = htonl(CBFS_COMPONENT_NULL);
+	entry->type = htonl(CBFS_COMPONENT_DELETED);
+	// adjust name & offset.
+	// TODO offset(name) must be aligned at 16.
+	memset(entry_name, 0, name_len);
+	len = ntohl(entry->len) + name_len;
+	entry->offset = htonl(sizeof(*entry) + CBFS_FILENAME_ALIGN);
+	// TODO entry->len can expand to next aligned address.
+	entry->len = htonl(len - ntohl(entry->offset));
+
+	// TODO(hungte) Merge with previous and next file if possible.
+	return 0;
+}
 
-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;
+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 (strcmp(CBFS_NAME(entry), name) == 0) {
+			DEBUG("cbfs_get_entry: found %s\n", name);
+			return entry;
+		}
+	}
+	return NULL;
 }
 
-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);
+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));
 
-	for (; pos < phys_end; pos += align) {
-		if (cbfs_file_header(pos))
-			return (struct cbfs_file *)phys_to_virt(pos);
+	if (ntohl(entry->type) != CBFS_COMPONENT_RAW) {
+		WARN("Only 'raw' files are safe to extract.\n");
 	}
-	return NULL;
+
+	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;
+	}
+	printf("Successfully dumped the file to: %s\n", filename);
+	return 0;
+}
+
+/* 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;
+}
+
+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",
+	       stage->compression == CBFS_COMPRESS_LZMA ? "LZMA" : "no",
+	       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"),
+			       (payload->compression == CBFS_COMPRESS_LZMA ?
+				"LZMA" : "no"),
+			       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;
+
+		case PAYLOAD_SEGMENT_BSS:
+			printf("    BSS (address 0x%016" PRIx64 ", "
+			       "length 0x%x)\n",
+			       ntohll(payload->load_addr),
+			       ntohl(payload->len));
+			break;
+
+		case PAYLOAD_SEGMENT_PARAMS:
+			printf("    parameters\n");
+			break;
 
-		/* Merge it with the previous file if possible. */
-		if (prev && prev->type == CBFS_COMPONENT_NULL)
-			cur = merge_adjacent_files(prev, cur);
+		default:
+			printf("    %x (%s compression, offset: 0x%x, "
+			       "load: 0x%" PRIx64 ", length: %d/%d\n",
+			       payload->type,
+			       (payload->compression == CBFS_COMPRESS_LZMA ?
+			       "LZMA" : "no"),
+			       ntohl(payload->offset),
+			       ntohll(payload->load_addr),
+			       ntohl(payload->len),
+			       ntohl(payload->mem_len));
+			break;
+	}
+	return 0;
+}
+
+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;
+	int *verbose = (int*)arg;
+	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;
+	}
 
-		/* Merge it with the next file if possible. */
-		if (next && next->type == CBFS_COMPONENT_NULL)
-			merge_adjacent_files(cur, next);
+	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 && *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;
+
+		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 ++;
+			}
+			break;
+		default:
+			break;
 	}
-	fprintf(stderr, "E: CBFS file %s not found.\n", filename);
-	return 1;
+	return 0;
 }
 
+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;
+}
+
+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;
+}
+
+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));
+}
+
+struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
+				       struct cbfs_file *entry) {
+	// TODO check if next entry is valid in cbfs_image?
+	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);
+}
+
+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);
+}
+
+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) {
+	memset(entry, -1, sizeof(*entry));
+	memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
+	entry->type = htonl(CBFS_COMPONENT_NULL);
+	entry->len = htonl(len);
+	entry->checksum = 0;  // FIXME Get a checksum algorithm.
+	entry->offset = htonl(sizeof(*entry) + CBFS_FILENAME_ALIGN);
+	memset(CBFS_NAME(entry), 0, CBFS_FILENAME_ALIGN);
+	return 0;
+}
+
+int cbfs_create_image(struct cbfs_image *image,
+		      uint32_t size,
+		      struct buffer *bootblock,
+		      uint32_t align,
+		      int32_t bootblock_offset,
+		      int32_t header_offset,
+		      int32_t entries_offset)
+{
+	struct cbfs_header *header;
+	struct cbfs_file *entry;
+	uint32_t cbfs_len;
+	if (cbfs_image_create(image, size) != 0)
+		return -1;
+	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);
+
+	// Adjust relative offset values (<0) to ROM address (>=0).
+	if (entries_offset < 0)
+		entries_offset += (int32_t)size;
+	if (bootblock_offset < 0)
+		bootblock_offset += (int32_t)size;
+	if (header_offset < 0)
+		header_offset += (int32_t) size;
+
+	DEBUG("cbfs_create_image: (real offset) bootblock=0x%x, "
+	      "header=0x%x, entries_offset=0x%x\n",
+	      bootblock_offset, header_offset, entries_offset);
+
+	if (align == 0)
+		align = 64;  // default align size.
+
+	// Prepare bootblock
+	if (bootblock_offset + bootblock->size > size) {
+		ERROR("Bootblock (0x%x+0x%zx) exceed ROM size (0x%x)\n",
+		      bootblock_offset, bootblock->size, size);
+		return -1;
+	}
+	memcpy(image->buffer.data + bootblock_offset, bootblock->data,
+	       bootblock->size);
+
+	// Prepare header
+	if (header_offset + sizeof(*header) > size) {
+		ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%x)\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);
+	header->architecture = ntohl(CBFS_ARCHITECTURE_ARMV7);
+	// TODO update x86 signature
+
+	// 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%x)\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.
+	uint32_t end = size;
+	if (bootblock_offset > entries_offset && bootblock_offset < end)
+		end = bootblock_offset;
+	if (header_offset > entries_offset && header_offset < end)
+		end = header_offset;
+	cbfs_len = end - entries_offset;
+	cbfs_create_entry(image, entry, cbfs_len);
+	LOG("Created CBFS image (capacity = %d bytes)\n", cbfs_len);
+	return 0;
+}
+
+
+
+#if 0
+
+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;
+}
 
 /* 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 */
@@ -589,126 +754,6 @@ void *create_cbfs_file(const char *filename, void *data, uint32_t * datasize,
 	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);
-	}
-	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;
-	}
-
-	// 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;
-			}
-			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);
-	}
-
-	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)));
@@ -783,3 +828,4 @@ uint32_t cbfs_find_location(const char *romfile, uint32_t filesize,
 	free(rom);
 	return ret;
 }
+#endif
diff --git a/util/cbfstool/common.h b/util/cbfstool/common.h
index 853e529..61ebfdc 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,144 @@
 #define __CBFSTOOL_COMMON_H
 
 #include <stdint.h>
+#include "cbfs.h"
+
+/* Message output */
+extern int verbose;
+#define DEBUG(x...) { if (verbose > 1) fprintf(stderr, "DEBUG: " x); }
+#define ERROR(x...) { fprintf(stderr, "ERROR: " x); }
+#define WARN(x...) { fprintf(stderr, "WARN: " x); }
+#define LOG(x...) { if (verbose > 0) printf(x); }
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+
+/* 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 */
 
-typedef void (*comp_func_ptr) (char *, int, char *, int *);
-typedef enum { CBFS_COMPRESS_NONE = 0, CBFS_COMPRESS_LZMA = 1 } comp_algo;
+/* Returns 1 if input points to an ELF object, otherwise 0. */
+int is_elf_object(const void *input);
 
-comp_func_ptr compression_function(comp_algo algo);
+/* Returns CBFS entry type value for given name, or (uint32_t)-1 as unknown. */
+uint32_t get_cbfs_entry_type(const char *name);
+
+/* Gets the name of given CBFS entry type value, or "(unknown)". */
+const char *get_cbfs_entry_type_name(uint32_t type);
+
+/* Prints all known CBFS entry types. */
+void print_all_cbfs_entry_types();
+
+
+/* CBFS image processing */
+
+struct cbfs_image {
+	struct buffer buffer;
+	struct cbfs_header *header;
+};
+
+/* Creates an empty CBFS image. Returns 0 on success, otherwise non-zero. */
+int cbfs_image_create(struct cbfs_image *image, size_t size);
 
-uint64_t intfiletype(const char *name);
+/* 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);
 
-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);
+/* 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);
 
-void *create_cbfs_file(const char *filename, void *data, uint32_t * datasize,
-		       uint32_t type, uint32_t * location);
+/* Releases the CBFS image. Returns 0 on success, otherwise non-zero. */
+int cbfs_image_delete(struct cbfs_image *image);
 
-int create_cbfs_image(const char *romfile, uint32_t romsize,
-		      const char *bootblock, uint32_t align, uint32_t offs);
+/* Adds an entry to CBFS image.  Returns 0 on success, otherwise non-zero. */
+int cbfs_add_entry(struct cbfs_image *image, const char *name,
+		   uint32_t type, struct buffer *buffer);
 
-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);
+/* Removes an entry from CBFS image. Returns 0 on success, otherwise non-zero. */
+int cbfs_remove_entry(struct cbfs_image *image, const char *name);
 
-uint32_t cbfs_find_location(const char *romfile, uint32_t filesize,
-			    const char *filename, uint32_t align);
+/* 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);
 
-void print_supported_filetypes(void);
+/* 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);
 
-#define ARRAY_SIZE(a) (int)(sizeof(a) / sizeof((a)[0]))
+/* Creates a CBFS image. */
+int cbfs_create_image(struct cbfs_image *image,
+		      uint32_t size,
+		      struct buffer *bootblock,
+		      uint32_t align,
+		      int32_t bootblock_offset,
+		      int32_t header_offset,
+		      int32_t entries_offset);
+
+/* 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);
+
+/* 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 */
+struct cbfs_header *cbfs_find_header(char *data, size_t size);
+struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image);
+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_header_info(struct cbfs_image *image);
+int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
+			  void *arg);
+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