Sol Boucher (solb(a)chromium.org) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/10122
-gerrit
commit dd6ecddc0d4d095773f67155ff73ab55d1a5dc28
Author: Sol Boucher <solb(a)chromium.org>
Date: Tue May 5 15:35:18 2015 -0700
cbfstool: Fix cbfs_copy_instance()'s master header endianness
The function hadn't been updated to account for the fact that we now
copy an endianness-corrected CBFS master header into a separate buffer
from the CBFS data: it still performed pointer arithmetic accross the
two buffers and wrote the copied buffer into the image without
restoring the original endianness.
Change-Id: Ieb2a001f253494cf3a90d7e19cd260791200c4d3
Signed-off-by: Sol Boucher <solb(a)chromium.org>
---
util/cbfstool/cbfs_image.c | 19 +++++--------------
1 file changed, 5 insertions(+), 14 deletions(-)
diff --git a/util/cbfstool/cbfs_image.c b/util/cbfstool/cbfs_image.c
index 8e0e9a4..457a873 100644
--- a/util/cbfstool/cbfs_image.c
+++ b/util/cbfstool/cbfs_image.c
@@ -336,17 +336,13 @@ int cbfs_copy_instance(struct cbfs_image *image, size_t copy_offset,
size_t align, entry_offset;
ssize_t last_entry_size;
- size_t header_offset, header_end;
size_t cbfs_offset, cbfs_end;
size_t copy_end = copy_offset + copy_size;
- align = htonl(image->header->align);
+ align = image->header->align;
- header_offset = (char *)image->header - image->buffer.data;
- header_end = header_offset + sizeof(image->header);
-
- cbfs_offset = htonl(image->header->offset);
- cbfs_end = htonl(image->header->romsize);
+ cbfs_offset = image->header->offset;
+ cbfs_end = image->header->romsize;
if (copy_end > image->buffer.size) {
ERROR("Copy offset out of range: [%zx:%zx)\n",
@@ -354,12 +350,7 @@ int cbfs_copy_instance(struct cbfs_image *image, size_t copy_offset,
return 1;
}
- /* Range check requested copy region with header and source cbfs. */
- if ((copy_offset >= header_offset && copy_offset < header_end) ||
- (copy_end >= header_offset && copy_end <= header_end)) {
- ERROR("New image would overlap old header.\n");
- }
-
+ /* Range check requested copy region with source cbfs. */
if ((copy_offset >= cbfs_offset && copy_offset < cbfs_end) ||
(copy_end >= cbfs_offset && copy_end <= cbfs_end)) {
ERROR("New image would overlap old one.\n");
@@ -368,7 +359,7 @@ int cbfs_copy_instance(struct cbfs_image *image, size_t copy_offset,
/* This will work, let's create a copy. */
copy_header = (struct cbfs_header *)(image->buffer.data + copy_offset);
- *copy_header = *image->header;
+ cbfs_put_header(copy_header, image->header);
copy_header->bootblocksize = 0;
/* Romsize is a misnomer. It's the absolute limit of cbfs content.*/
Sol Boucher (solb(a)chromium.org) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/9967
-gerrit
commit 166392432a2c2c208be254ea800678a89d7af0bc
Author: Sol Boucher <solb(a)chromium.org>
Date: Wed Mar 18 10:13:48 2015 -0700
fmaptool: Add listing of annotated CBFS sections and generate header
The fmd compiler now processes "(CBFS)" annotations, distilling them
into a comma-separated list of the names of sections containing
CBFSes. This list is the only thing printed to standard output to
enable easy capture and machine consumption by other tools.
Additionally, the ability to generate a tiny header with a define for
the primary CBFS's size is implemented and can be requested via a
new command-line switch.
Here's an example of how to use the new features:
$ ./fmaptool -h layout.h layout_arm_8192.fmd layout.fmap 2>/dev/null
FW_MAIN_A,FW_MAIN_B,COREBOOT
The hypothetical fmd file contains three sections annotated as (CBFS),
the names of which are printed to standard output. As before, a binary
FMAP file named layout.fmap is created; however, because the command
was invoked with -h, a header #define ing the offset of its FMAP
section (i.e. where it will be relative to the base of flash once the
boot image is assembled) is also generated.
BUG=chromium:470407
TEST=Verify that fmd files without a "COREBOOT" section or with one
that isn't annotated as "(CBFS)" are not accepted. Ensure that the
list of CBFS sections matches the descriptor file's annotations and
is led by the "COREBOOT" section. Invoke with the header generation
switch and check that output file for reasonableness.
BRANCH=None
Change-Id: I496dd937f69467bfd9233c28df59c7608e89538f
Signed-off-by: Sol Boucher <solb(a)chromium.org>
Original-Commit-Id: 9227698adecf675770b2983380eb570676c2b5d2
Original-Change-Id: I8b32f6ef19cabe2f6760106e676683c4565bbaad
Original-Signed-off-by: Sol Boucher <solb(a)chromium.org>
Original-Reviewed-on: https://chromium-review.googlesource.com/262956
Original-Reviewed-by: Duncan Laurie <dlaurie(a)chromium.org>
Original-Reviewed-by: Julius Werner <jwerner(a)chromium.org>
---
util/cbfstool/Makefile | 3 +-
util/cbfstool/Makefile.inc | 1 +
util/cbfstool/cbfs_sections.c | 122 ++++++++++++++++++++++++++++
util/cbfstool/cbfs_sections.h | 59 ++++++++++++++
util/cbfstool/fmaptool.c | 181 +++++++++++++++++++++++++++++++++++-------
5 files changed, 337 insertions(+), 29 deletions(-)
diff --git a/util/cbfstool/Makefile b/util/cbfstool/Makefile
index 2c63e05..1edb092 100644
--- a/util/cbfstool/Makefile
+++ b/util/cbfstool/Makefile
@@ -22,7 +22,8 @@ CBFSTOOL_COMMON+=lzma/C/LzFind.o lzma/C/LzmaDec.o lzma/C/LzmaEnc.o
CBFSTOOL_COMMON:=$(addprefix $(obj)/,$(CBFSTOOL_COMMON))
FMAPTOOL_BINARY:=$(obj)/fmaptool
-FMAPTOOL_COMMON:=fmap_from_fmd.o fmd.o fmd_parser.o fmd_scanner.o
+FMAPTOOL_COMMON:=cbfs_sections.o fmap_from_fmd.o
+FMAPTOOL_COMMON+=fmd.o fmd_parser.o fmd_scanner.o
# FMAP
FMAPTOOL_COMMON+=flashmap/fmap.o
FMAPTOOL_COMMON+=flashmap/kv_pair.o flashmap/valstr.o
diff --git a/util/cbfstool/Makefile.inc b/util/cbfstool/Makefile.inc
index 7fba6be..bf7e1b8 100644
--- a/util/cbfstool/Makefile.inc
+++ b/util/cbfstool/Makefile.inc
@@ -19,6 +19,7 @@ cbfsobj += cbfs-payload-linux.o
fmapobj :=
fmapobj += fmaptool.o
+fmapobj += cbfs_sections.o
fmapobj += fmap_from_fmd.o
fmapobj += fmd.o
fmapobj += fmd_parser.o
diff --git a/util/cbfstool/cbfs_sections.c b/util/cbfstool/cbfs_sections.c
new file mode 100644
index 0000000..81912d5
--- /dev/null
+++ b/util/cbfstool/cbfs_sections.c
@@ -0,0 +1,122 @@
+/*
+ * fmap_sections.c, track which sections of the image will contain CBFSes
+ *
+ * Copyright (C) 2015 Google, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
+ */
+
+#include "cbfs_sections.h"
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+
+struct descriptor_node {
+ const struct flashmap_descriptor *val;
+ struct descriptor_node *next;
+};
+
+static struct descriptor_list {
+ struct descriptor_node *head;
+ struct descriptor_node *tail;
+} cbfs_sections;
+
+static bool seen_primary_section = false;
+
+static void descriptor_list_prepend(struct descriptor_list *list,
+ struct descriptor_node *new_head)
+{
+ assert(list);
+ assert(new_head);
+
+ new_head->next = list->head;
+ list->head = new_head;
+ if (!list->tail)
+ list->tail = new_head;
+}
+
+static void descriptor_list_append(struct descriptor_list *list,
+ struct descriptor_node *new_tail)
+{
+ assert(list);
+ assert(new_tail);
+
+ if (list->tail)
+ list->tail->next = new_tail;
+ list->tail = new_tail;
+ if (!list->head)
+ list->head = new_tail;
+}
+
+/* Implementation of cbfs module's callback; invoked during fmd file parsing */
+bool fmd_process_annotation_impl(const struct flashmap_descriptor *node,
+ const char *annotation)
+{
+ if (strcmp(annotation, SECTION_ANNOTATION_CBFS) == 0 &&
+ node->list_len == 0) {
+ struct descriptor_node *list_node = malloc(sizeof(*list_node));
+ list_node->val = node;
+ list_node->next = NULL;
+
+ if (strcmp(node->name, SECTION_NAME_PRIMARY_CBFS) == 0) {
+ descriptor_list_prepend(&cbfs_sections, list_node);
+ seen_primary_section = true;
+ } else {
+ descriptor_list_append(&cbfs_sections, list_node);
+ }
+
+ return true;
+ }
+
+ return false;
+}
+
+cbfs_section_iterator_t cbfs_sections_iterator(void)
+{
+ return cbfs_sections.head;
+}
+
+bool cbfs_sections_iterator_advance(cbfs_section_iterator_t *it)
+{
+ assert(it);
+ if (!*it)
+ return false;
+
+ *it = (*it)->next;
+ return true;
+}
+
+const struct flashmap_descriptor *cbfs_sections_iterator_deref(
+ cbfs_section_iterator_t it)
+{
+ assert(it);
+ return it->val;
+}
+
+bool cbfs_sections_primary_cbfs_accounted_for(void)
+{
+ return seen_primary_section;
+}
+
+void cbfs_sections_cleanup(void)
+{
+ for (struct descriptor_node *cur = cbfs_sections.head, *next = NULL;
+ cur; cur = next) {
+ next = cur->next;
+ free(cur);
+ }
+ cbfs_sections.head = NULL;
+ cbfs_sections.tail = NULL;
+}
diff --git a/util/cbfstool/cbfs_sections.h b/util/cbfstool/cbfs_sections.h
new file mode 100644
index 0000000..5a23899
--- /dev/null
+++ b/util/cbfstool/cbfs_sections.h
@@ -0,0 +1,59 @@
+/*
+ * fmap_sections.h, track which sections of the image will contain CBFSes
+ *
+ * Copyright (C) 2015 Google, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
+ */
+
+#ifndef CBFS_SECTIONS_H_
+#define CBFS_SECTIONS_H_
+
+#include "fmd.h"
+
+#include <stdbool.h>
+
+#define SECTION_NAME_FMAP "FMAP"
+#define SECTION_NAME_PRIMARY_CBFS "COREBOOT"
+
+#define SECTION_ANNOTATION_CBFS "CBFS"
+
+typedef const struct descriptor_node *cbfs_section_iterator_t;
+
+/** @return Iterator pointing to first CBFS section, or NULL if none exist */
+cbfs_section_iterator_t cbfs_sections_iterator(void);
+
+/**
+ * Advance iterator to point to the next CBFS section.
+ * If it was already pointing to the last such section, it will be set to NULL.
+ *
+ * @param it (Non-NULL) pointer to (possibly NULL) iterator to be updated
+ * @return Whether it was successfully advanced (wasn't already NULL)
+ */
+bool cbfs_sections_iterator_advance(cbfs_section_iterator_t *it);
+
+/**
+ * @param it Iterator, which must currently be non-NULL
+ * @return Section to which it points
+ */
+const struct flashmap_descriptor *cbfs_sections_iterator_deref(
+ cbfs_section_iterator_t it);
+
+/** @return Whether a section named SECTION_NAME_PRIMARY_CBFS is in the list. */
+bool cbfs_sections_primary_cbfs_accounted_for(void);
+
+/** Reclaim the space used to store knowledge of which sections are CBFSes. */
+void cbfs_sections_cleanup(void);
+
+#endif
diff --git a/util/cbfstool/fmaptool.c b/util/cbfstool/fmaptool.c
index 961e9dc..09b68d2 100644
--- a/util/cbfstool/fmaptool.c
+++ b/util/cbfstool/fmaptool.c
@@ -18,53 +18,149 @@
*/
#include "common.h"
+#include "cbfs_sections.h"
#include "fmap_from_fmd.h"
#include <stdio.h>
#include <string.h>
+#include <unistd.h>
#define STDIN_FILENAME_SENTINEL "-"
+#define HEADER_FMAP_OFFSET "FMAP_OFFSET"
+
enum fmaptool_return {
FMAPTOOL_EXIT_SUCCESS = 0,
FMAPTOOL_EXIT_BAD_ARGS,
FMAPTOOL_EXIT_BAD_INPUT_PATH,
FMAPTOOL_EXIT_BAD_OUTPUT_PATH,
FMAPTOOL_EXIT_FAILED_DESCRIPTOR,
+ FMAPTOOL_EXIT_MISSING_FMAP_SECTION,
+ FMAPTOOL_EXIT_MISSING_PRIMARY_CBFS,
FMAPTOOL_EXIT_FAILED_FMAP_CONVERSION,
FMAPTOOL_EXIT_UNKNOWN_FMAP_SIZE,
- FMAPTOOL_EXIT_FAILED_WRITING_FILE,
+ FMAPTOOL_EXIT_FAILED_WRITING_OUTPUT,
+ FMAPTOOL_EXIT_FAILED_WRITING_HEADER,
};
-bool fmd_process_annotation_impl(unused const struct flashmap_descriptor *node,
- unused const char *annotation)
+static void usage(const char *invoked_as)
{
- // We always accept annotations, but never act on them.
+ fputs("fmaptool: Compiler for fmd (flashmap descriptor) files\n",
+ stderr);
+ fputs("\nUSAGE:\n", stderr);
+ fprintf(stderr,
+ "\t%s [-h <header output file>] <fmd input file> <binary output file>\n",
+ invoked_as);
+ fputs("\nMANDATORY ARGUMENTS:\n", stderr);
+ fprintf(stderr,
+ "<fmd input file> may be '%s' to read from standard input\n",
+ STDIN_FILENAME_SENTINEL);
+ fputs("<binary output file> must be a regular file\n", stderr);
+ fputs("\nOPTIONAL SWITCHES:\n", stderr);
+ fprintf(stderr,
+ "-h\tAlso produce a C header defining %s to the FMAP section's flash offset.\n",
+ HEADER_FMAP_OFFSET);
+ fputs("\nOUTPUT:\n", stderr);
+ fputs("A successful invocation prints a summary of work done to standard error, and a comma-separated list\n",
+ stderr);
+ fputs("of those sections that contain CBFSes, starting with the primary such section, to standard output.\n",
+ stderr);
+}
+
+static void list_cbfs_section_names(void)
+{
+ cbfs_section_iterator_t cbfs_it = cbfs_sections_iterator();
+ assert(cbfs_it);
+
+ bool subsequent = false;
+ while (cbfs_it) {
+ const char *cur_name =
+ cbfs_sections_iterator_deref(cbfs_it)->name;
+ if (cbfs_sections_iterator_advance(&cbfs_it) && subsequent)
+ putchar(',');
+ fputs(cur_name, stdout);
+ subsequent = true;
+ }
+ putchar('\n');
+}
+
+static bool write_header(const char *out_fname,
+ const struct flashmap_descriptor *root)
+{
+ assert(out_fname);
+
+ FILE *header = fopen(out_fname, "w");
+ if (!header) {
+ fprintf(stderr, "FATAL: Unable to open file '%s' for writing\n",
+ out_fname);
+ return false;
+ }
+
+ unsigned fmap_offset =
+ fmd_calc_absolute_offset(root, SECTION_NAME_FMAP);
+ assert(fmap_offset != FMD_NOTFOUND);
+
+ fputs("#ifndef FMAPTOOL_GENERATED_HEADER_H_\n", header);
+ fputs("#define FMAPTOOL_GENERATED_HEADER_H_\n\n", header);
+ fprintf(header, "#define %s %#x\n\n", HEADER_FMAP_OFFSET, fmap_offset);
+ fputs("#endif\n", header);
+
+ fclose(header);
return true;
}
+static void full_fmd_cleanup(struct flashmap_descriptor **victim)
+{
+ assert(victim);
+
+ cbfs_sections_cleanup();
+ fmd_cleanup(*victim);
+ *victim = NULL;
+}
+
int main(int argc, char **argv)
{
- if (argc != 3) {
- fputs("Convert a human-readable flashmap descriptor (fmd) file into the binary FMAP format for use in firmware images\n",
- stderr);
- fprintf(stderr,
- "USAGE: %s <fmd input file> <binary output file>\n",
- argv[0]);
- fprintf(stderr,
- "To read from standard input, provide '%s' as the input filename.\n",
- STDIN_FILENAME_SENTINEL);
+ struct {
+ // Mandatory
+ const char *fmd_filename;
+ const char *fmap_filename;
+
+ // Optional
+ const char *header_filename;
+ } args = {NULL, NULL, NULL};
+
+ bool show_usage = false;
+ int each_arg;
+ while (!show_usage && (each_arg = getopt(argc, argv, ":h:")) != -1) {
+ switch (each_arg) {
+ case 'h':
+ args.header_filename = optarg;
+ break;
+ case ':':
+ fprintf(stderr, "-%c: Expected an accompanying value\n",
+ optopt);
+ show_usage = true;
+ break;
+ default:
+ fprintf(stderr, "-%c: Unexpected command-line switch\n",
+ optopt);
+ show_usage = true;
+ }
+ }
+
+ if (show_usage || argc - optind != 2) {
+ usage(argv[0]);
return FMAPTOOL_EXIT_BAD_ARGS;
}
- const char *fmd_filename = argv[1];
- const char *fmap_filename = argv[2];
+ args.fmd_filename = argv[optind];
+ args.fmap_filename = argv[optind + 1];
FILE *fmd_file = stdin;
- if (strcmp(fmd_filename, STDIN_FILENAME_SENTINEL) != 0) {
- fmd_file = fopen(fmd_filename, "r");
+ if (strcmp(args.fmd_filename, STDIN_FILENAME_SENTINEL) != 0) {
+ fmd_file = fopen(args.fmd_filename, "r");
if (!fmd_file) {
fprintf(stderr, "FATAL: Unable to open file '%s'\n",
- fmd_filename);
+ args.fmd_filename);
return FMAPTOOL_EXIT_BAD_INPUT_PATH;
}
}
@@ -74,14 +170,32 @@ int main(int argc, char **argv)
if (!descriptor) {
fputs("FATAL: Failed while processing provided descriptor\n",
stderr);
+ full_fmd_cleanup(&descriptor);
return FMAPTOOL_EXIT_FAILED_DESCRIPTOR;
}
+ if (!fmd_find_node(descriptor, SECTION_NAME_FMAP)) {
+ fprintf(stderr,
+ "FATAL: Flashmap descriptor must have an '%s' section\n",
+ SECTION_NAME_FMAP);
+ full_fmd_cleanup(&descriptor);
+ return FMAPTOOL_EXIT_MISSING_FMAP_SECTION;
+ }
+
+ if (!cbfs_sections_primary_cbfs_accounted_for()) {
+ fprintf(stderr,
+ "FATAL: Flashmap descriptor must have a '%s' section that is annotated with '(%s)'\n",
+ SECTION_NAME_PRIMARY_CBFS,
+ SECTION_ANNOTATION_CBFS);
+ full_fmd_cleanup(&descriptor);
+ return FMAPTOOL_EXIT_MISSING_PRIMARY_CBFS;
+ }
+
struct fmap *flashmap = fmap_from_fmd(descriptor);
if (!flashmap) {
fputs("FATAL: Failed while constructing FMAP section\n",
stderr);
- fmd_cleanup(descriptor);
+ full_fmd_cleanup(&descriptor);
return FMAPTOOL_EXIT_FAILED_FMAP_CONVERSION;
}
@@ -90,16 +204,16 @@ int main(int argc, char **argv)
fputs("FATAL: Failed to determine FMAP section size\n",
stderr);
fmap_destroy(flashmap);
- fmd_cleanup(descriptor);
+ full_fmd_cleanup(&descriptor);
return FMAPTOOL_EXIT_UNKNOWN_FMAP_SIZE;
}
- FILE *fmap_file = fopen(fmap_filename, "wb");
+ FILE *fmap_file = fopen(args.fmap_filename, "wb");
if (!fmap_file) {
fprintf(stderr, "FATAL: Unable to open file '%s' for writing\n",
- fmap_filename);
+ args.fmap_filename);
fmap_destroy(flashmap);
- fmd_cleanup(descriptor);
+ full_fmd_cleanup(&descriptor);
return FMAPTOOL_EXIT_BAD_OUTPUT_PATH;
}
@@ -107,13 +221,24 @@ int main(int argc, char **argv)
fputs("FATAL: Failed to write final FMAP to file\n", stderr);
fclose(fmap_file);
fmap_destroy(flashmap);
- fmd_cleanup(descriptor);
- return FMAPTOOL_EXIT_FAILED_WRITING_FILE;
+ full_fmd_cleanup(&descriptor);
+ return FMAPTOOL_EXIT_FAILED_WRITING_OUTPUT;
}
fclose(fmap_file);
- printf("SUCCESS: Wrote %d bytes to file '%s'\n", size, fmap_filename);
-
fmap_destroy(flashmap);
- fmd_cleanup(descriptor);
+
+ if (args.header_filename &&
+ !write_header(args.header_filename, descriptor)) {
+ full_fmd_cleanup(&descriptor);
+ return FMAPTOOL_EXIT_FAILED_WRITING_HEADER;
+ }
+
+ fprintf(stderr, "SUCCESS: Wrote %d bytes to file '%s'%s\n", size,
+ args.fmap_filename,
+ args.header_filename ? " (and generated header)" : "");
+ fputs("The sections containing CBFSes are: ", stderr);
+ list_cbfs_section_names();
+
+ full_fmd_cleanup(&descriptor);
return FMAPTOOL_EXIT_SUCCESS;
}
Sol Boucher (solb(a)chromium.org) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/10136
-gerrit
commit fc9b5bb82c493fe7116ec416df141bacfd67adce
Author: Sol Boucher <solb(a)chromium.org>
Date: Thu May 7 02:39:22 2015 -0700
cbfstool: Support top-aligned addresses for new-format images
The cbfstool handling of new-style FMAP-driven "partitioned" images
originally disallowed the use of x86-style top-aligned addresses with
the add.* and layout actions because it wasn't obvious how they should
work, especially since the normal addressing is done relative to each
individual region for these types of images. Not surprisingly,
however, the x86 portions of the build system make copious use of
top-aligned addresses, so this allows their use with new images and
specifies their behavior as being relative to the *image* end---not
the region end---just as it is for legacy images.
Change-Id: Icecc843f4f8b6bb52aa0ea16df771faa278228d2
Signed-off-by: Sol Boucher <solb(a)chromium.org>
---
util/cbfstool/cbfs_image.c | 19 ++++++------------
util/cbfstool/cbfs_image.h | 4 +---
util/cbfstool/cbfstool.c | 43 ++++++++++++++++++++++++++++++----------
util/cbfstool/common.h | 2 ++
util/cbfstool/partitioned_file.c | 7 +++++++
util/cbfstool/partitioned_file.h | 3 +++
6 files changed, 52 insertions(+), 26 deletions(-)
diff --git a/util/cbfstool/cbfs_image.c b/util/cbfstool/cbfs_image.c
index 4ecb461..8fb2a60 100644
--- a/util/cbfstool/cbfs_image.c
+++ b/util/cbfstool/cbfs_image.c
@@ -526,6 +526,12 @@ static int cbfs_add_entry_at(struct cbfs_image *image,
int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
const char *name, uint32_t type, uint32_t content_offset)
{
+ assert(image);
+ assert(buffer);
+ assert(buffer->data);
+ assert(name);
+ assert(!IS_TOP_ALIGNED_ADDRESS(content_offset));
+
uint32_t entry_type;
uint32_t addr, addr_next;
struct cbfs_file *entry, *next;
@@ -537,19 +543,6 @@ int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n",
name, content_offset, header_size, buffer->size, need_size);
- if (IS_TOP_ALIGNED_ADDRESS(content_offset)) {
- if (!cbfs_is_legacy_cbfs(image)) {
- ERROR("Top-aligned offsets are only supported for legacy CBFSes (with master headers)\n");
- return -1;
- }
-
- // legacy cbfstool takes top-aligned address.
- uint32_t theromsize = image->header.romsize;
- INFO("Converting top-aligned address 0x%x to offset: 0x%x\n",
- content_offset, content_offset + theromsize);
- content_offset = theromsize + (int32_t)content_offset;
- }
-
// Merge empty entries.
DEBUG("(trying to merge empty entries...)\n");
cbfs_walk(image, cbfs_merge_empty_entry, NULL);
diff --git a/util/cbfstool/cbfs_image.h b/util/cbfstool/cbfs_image.h
index 024edc5..5ff8a9a 100644
--- a/util/cbfstool/cbfs_image.h
+++ b/util/cbfstool/cbfs_image.h
@@ -22,8 +22,6 @@
#include "common.h"
#include "cbfs.h"
-#define IS_TOP_ALIGNED_ADDRESS(x) ((uint32_t)(x) > 0x80000000)
-
/* CBFS image processing */
struct cbfs_image {
@@ -88,7 +86,7 @@ int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
/* Adds an entry to CBFS image by given name and type. If content_offset is
* non-zero, try to align "content" (CBFS_SUBHEADER(p)) at content_offset.
- * Note that top-aligned addresses are only supported for legacy CBFSes.
+ * Never pass this function a top-aligned address: convert it to an offset.
* 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 content_offset);
diff --git a/util/cbfstool/cbfstool.c b/util/cbfstool/cbfstool.c
index 29ac6b6..ea3c339 100644
--- a/util/cbfstool/cbfstool.c
+++ b/util/cbfstool/cbfstool.c
@@ -98,6 +98,21 @@ static bool region_is_modern_cbfs(const char *region)
CBFS_FILE_MAGIC, strlen(CBFS_FILE_MAGIC));
}
+/*
+ * Converts between offsets from the start of the specified image region and
+ * "top-aligned" offsets from the top of the entire flash image. Works in either
+ * direction: pass in one type of offset and receive the other type.
+ * N.B. A top-aligned offset is always a positive number, and should not be
+ * confused with a top-aliged *address*, which is its arithmetic inverse. */
+static unsigned convert_to_from_top_aligned(const struct buffer *region,
+ unsigned offset)
+{
+ assert(region);
+
+ size_t image_size = partitioned_file_total_size(param.image_file);
+ return image_size - region->offset - offset;
+}
+
typedef int (*convert_buffer_t)(struct buffer *buffer, uint32_t *offset);
static int cbfs_add_integer_component(const char *name,
@@ -129,6 +144,10 @@ static int cbfs_add_integer_component(const char *name,
goto done;
}
+ if (IS_TOP_ALIGNED_ADDRESS(offset))
+ offset = convert_to_from_top_aligned(param.image_region,
+ -offset);
+
if (cbfs_add_entry(&image, &buffer, name, CBFS_COMPONENT_RAW, offset) !=
0) {
ERROR("Failed to add %llu into ROM image as '%s'.\n",
@@ -187,6 +206,10 @@ static int cbfs_add_component(const char *filename,
return 1;
}
+ if (IS_TOP_ALIGNED_ADDRESS(offset))
+ offset = convert_to_from_top_aligned(param.image_region,
+ -offset);
+
if (cbfs_add_entry(&image, &buffer, name, type, offset) != 0) {
ERROR("Failed to add '%s' into ROM image.\n", filename);
buffer_delete(&buffer);
@@ -439,11 +462,6 @@ static int cbfs_locate(void)
param.headeroffset))
return 1;
- if (!cbfs_is_legacy_cbfs(&image) && param.top_aligned) {
- ERROR("The -T switch is only valid on legacy images having CBFS master headers\n");
- return 1;
- }
-
if (cbfs_get_entry(&image, param.name))
WARN("'%s' already in CBFS.\n", param.name);
@@ -464,7 +482,8 @@ static int cbfs_locate(void)
}
if (param.top_aligned)
- address = address - image.header.romsize;
+ address = -convert_to_from_top_aligned(param.image_region,
+ address);
printf("0x%x\n", address);
return 0;
@@ -806,7 +825,7 @@ static void usage(char *name)
"USAGE:\n" " %s [-h]\n"
" %s FILE COMMAND [-v] [PARAMETERS]...\n\n" "OPTIONs:\n"
" -H header_offset Do not search for header; use this offset*\n"
- " -T Output top-aligned memory address*\n"
+ " -T Output top-aligned memory address\n"
" -u Accept short data; fill upward/from bottom\n"
" -d Accept short data; fill downward/from top\n"
" -v Provide verbose output\n"
@@ -857,8 +876,8 @@ static void usage(char *name)
"Updates the FIT table with microcode entries\n"
"\n"
"OFFSETs:\n"
- " Numbers accompanying -b, -H, and -o switches may be provided\n"
- " in two possible formats*: if their value is greater than\n"
+ " Numbers accompanying -b, -H, and -o switches* may be provided\n"
+ " in two possible formats: if their value is greater than\n"
" 0x80000000, they are interpreted as a top-aligned x86 memory\n"
" address; otherwise, they are treated as an offset into flash.\n"
"ARCHes:\n"
@@ -878,7 +897,11 @@ static void usage(char *name)
" that, when working with such images, the -F and -r switches\n"
" default to '%s' for convenience, and both the -b switch to\n"
" CBFS operations and the output of the locate action become\n"
- " relative to the selected CBFS region's lowest address.\n",
+ " relative to the selected CBFS region's lowest address.\n"
+ " The one exception to this rule is the top-aligned address,\n"
+ " which is always relative to the end of the entire image\n"
+ " rather than relative to the local region; this is true for\n"
+ " for both input (sufficiently large) and output (-T) data.\n",
SECTION_NAME_FMAP, SECTION_NAME_PRIMARY_CBFS,
SECTION_NAME_PRIMARY_CBFS
);
diff --git a/util/cbfstool/common.h b/util/cbfstool/common.h
index 831d3eb..392ec80 100644
--- a/util/cbfstool/common.h
+++ b/util/cbfstool/common.h
@@ -50,6 +50,8 @@ extern int verbose;
#define MAX(x, y) ((x) > (y) ? (x) : (y))
#define MIN(x, y) ((x) < (y) ? (x) : (y))
+#define IS_TOP_ALIGNED_ADDRESS(x) ((uint32_t)(x) > 0x80000000)
+
#define unused __attribute__((unused))
static inline uint32_t align_up(uint32_t value, uint32_t align)
diff --git a/util/cbfstool/partitioned_file.c b/util/cbfstool/partitioned_file.c
index 6473963..0b25272 100644
--- a/util/cbfstool/partitioned_file.c
+++ b/util/cbfstool/partitioned_file.c
@@ -290,6 +290,13 @@ bool partitioned_file_is_partitioned(const partitioned_file_t *file)
return partitioned_file_get_fmap(file) != NULL;
}
+size_t partitioned_file_total_size(const partitioned_file_t *file)
+{
+ assert(file);
+
+ return file->buffer.size;
+}
+
bool partitioned_file_region_check_magic(const partitioned_file_t *file,
const char *region, const char *magic, size_t magic_len)
{
diff --git a/util/cbfstool/partitioned_file.h b/util/cbfstool/partitioned_file.h
index 97d1b57..92f228e 100644
--- a/util/cbfstool/partitioned_file.h
+++ b/util/cbfstool/partitioned_file.h
@@ -133,6 +133,9 @@ void partitioned_file_close(partitioned_file_t *file);
/** @return Whether the file is partitioned (i.e. not flat). */
bool partitioned_file_is_partitioned(const partitioned_file_t *file);
+/** @return The image's overall filesize, regardless of whether it's flat. */
+size_t partitioned_file_total_size(const partitioned_file_t *file);
+
/** @return Whether the specified region begins with the magic bytes. */
bool partitioned_file_region_check_magic(const partitioned_file_t *file,
const char *region, const char *magic, size_t magic_len);
Sol Boucher (solb(a)chromium.org) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/10131
-gerrit
commit 7b9a0713e0670fa88bbeab493a0f3f87488ecdc2
Author: Sol Boucher <solb(a)chromium.org>
Date: Tue May 5 20:35:26 2015 -0700
cbfstool: Eliminate useless cbfs_image_create() local variable
The only operation performed on this struct turned out to be sizeof...
Change-Id: I619db60ed2e7ef6c196dd2600dc83bad2fdc6a55
Signed-off-by: Sol Boucher <solb(a)chromium.org>
---
util/cbfstool/cbfs_image.c | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)
diff --git a/util/cbfstool/cbfs_image.c b/util/cbfstool/cbfs_image.c
index 3df291a..16dc3dc 100644
--- a/util/cbfstool/cbfs_image.c
+++ b/util/cbfstool/cbfs_image.c
@@ -191,7 +191,6 @@ int cbfs_image_create(struct cbfs_image *image,
uint32_t header_offset,
uint32_t entries_offset)
{
- struct cbfs_header header;
struct cbfs_file *entry;
int32_t *rel_offset;
uint32_t cbfs_len;
@@ -200,8 +199,8 @@ int cbfs_image_create(struct cbfs_image *image,
DEBUG("cbfs_image_create: 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);
+ bootblock_offset, bootblock->size, header_offset,
+ sizeof(image->header), entries_offset);
// This attribute must be given in order to prove that this module
// correctly preserves certain CBFS properties. See the block comment
@@ -245,9 +244,9 @@ int cbfs_image_create(struct cbfs_image *image,
bootblock->size);
// Prepare header
- if (header_offset + sizeof(header) > size - sizeof(int32_t)) {
+ if (header_offset + sizeof(image->header) > size - sizeof(int32_t)) {
ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
- header_offset, sizeof(header), size);
+ header_offset, sizeof(image->header), size);
return -1;
}
image->header.magic = CBFS_HEADER_MAGIC;
Sol Boucher (solb(a)chromium.org) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/9940
-gerrit
commit f712faa907a97a3c0012dabe357ab2b87bfdc5dd
Author: Sol Boucher <solb(a)chromium.org>
Date: Fri Feb 27 17:07:30 2015 -0800
cbfstool: Import minimal set of files from flashmap
flashmap was developed in a separate repository until now.
Import the files from the 2012 version of the project [1].
[1] https://code.google.com/p/flashmap
BUG=chromium:461875
TEST=None
BRANCH=None
Change-Id: Ida33f81509abc1cf2e532435adbbf31919d96bd8
Signed-off-by: Sol Boucher <solb(a)chromium.org>
Original-Commit-Id: f44e1d1864babe244f07ca49655f0b80b84e890d
Original-Change-Id: Ibf191d34df738449c9b9d7ebccca3d7f4150d4d3
Original-Signed-off-by: Sol Boucher <solb(a)chromium.org>
Original-Reviewed-on: https://chromium-review.googlesource.com/254801
Original-Reviewed-by: Julius Werner <jwerner(a)chromium.org>
---
util/cbfstool/flashmap/fmap.c | 727 +++++++++++++++++++++++++++++++++++++++
util/cbfstool/flashmap/fmap.h | 192 +++++++++++
util/cbfstool/flashmap/kv_pair.c | 242 +++++++++++++
util/cbfstool/flashmap/kv_pair.h | 153 ++++++++
util/cbfstool/flashmap/valstr.c | 65 ++++
util/cbfstool/flashmap/valstr.h | 78 +++++
6 files changed, 1457 insertions(+)
diff --git a/util/cbfstool/flashmap/fmap.c b/util/cbfstool/flashmap/fmap.c
new file mode 100644
index 0000000..8850a92
--- /dev/null
+++ b/util/cbfstool/flashmap/fmap.c
@@ -0,0 +1,727 @@
+/* Copyright 2010, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Alternatively, this software may be distributed under the terms of the
+ * GNU General Public License ("GPL") version 2 as published by the Free
+ * Software Foundation.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <errno.h>
+#include <inttypes.h>
+#include <limits.h>
+#include <assert.h>
+
+#include <fmap.h>
+#include <valstr.h>
+
+#include "kv_pair.h"
+#include "mincrypt/sha.h"
+
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+
+const struct valstr flag_lut[] = {
+ { FMAP_AREA_STATIC, "static" },
+ { FMAP_AREA_COMPRESSED, "compressed" },
+ { FMAP_AREA_RO, "ro" },
+};
+
+/* returns size of fmap data structure if successful, <0 to indicate error */
+int fmap_size(struct fmap *fmap)
+{
+ if (!fmap)
+ return -1;
+
+ return sizeof(*fmap) + (fmap->nareas * sizeof(struct fmap_area));
+}
+
+/* brute force linear search */
+static long int fmap_lsearch(const uint8_t *image, size_t len)
+{
+ long int offset;
+ int fmap_found = 0;
+
+ for (offset = 0; offset < len - strlen(FMAP_SIGNATURE); offset++) {
+ if (!memcmp(&image[offset],
+ FMAP_SIGNATURE,
+ strlen(FMAP_SIGNATURE))) {
+ fmap_found = 1;
+ break;
+ }
+ }
+
+ if (!fmap_found)
+ return -1;
+
+ if (offset + fmap_size((struct fmap *)&image[offset]) > len)
+ return -1;
+
+ return offset;
+}
+
+/* if image length is a power of 2, use binary search */
+static long int fmap_bsearch(const uint8_t *image, size_t len)
+{
+ long int offset = -1;
+ int fmap_found = 0, stride;
+
+ /*
+ * For efficient operation, we start with the largest stride possible
+ * and then decrease the stride on each iteration. Also, check for a
+ * remainder when modding the offset with the previous stride. This
+ * makes it so that each offset is only checked once.
+ */
+ for (stride = len / 2; stride >= 1; stride /= 2) {
+ if (fmap_found)
+ break;
+
+ for (offset = 0;
+ offset < len - strlen(FMAP_SIGNATURE);
+ offset += stride) {
+ if ((offset % (stride * 2) == 0) && (offset != 0))
+ continue;
+ if (!memcmp(&image[offset],
+ FMAP_SIGNATURE,
+ strlen(FMAP_SIGNATURE))) {
+ fmap_found = 1;
+ break;
+ }
+ }
+ }
+
+ if (!fmap_found)
+ return -1;
+
+ if (offset + fmap_size((struct fmap *)&image[offset]) > len)
+ return -1;
+
+ return offset;
+}
+
+static int popcnt(unsigned int u)
+{
+ int count;
+
+ /* K&R method */
+ for (count = 0; u; count++)
+ u &= (u - 1);
+
+ return count;
+}
+
+long int fmap_find(const uint8_t *image, unsigned int image_len)
+{
+ long int ret = -1;
+
+ if ((image == NULL) || (image_len == 0))
+ return -1;
+
+ if (popcnt(image_len) == 1)
+ ret = fmap_bsearch(image, image_len);
+ else
+ ret = fmap_lsearch(image, image_len);
+
+ return ret;
+}
+
+int fmap_print(const struct fmap *fmap)
+{
+ int i;
+ struct kv_pair *kv = NULL;
+ const uint8_t *tmp;
+
+ kv = kv_pair_new();
+ if (!kv)
+ return -1;
+
+ tmp = fmap->signature;
+ kv_pair_fmt(kv, "fmap_signature",
+ "0x%02x%02x%02x%02x%02x%02x%02x%02x",
+ tmp[0], tmp[1], tmp[2], tmp[3],
+ tmp[4], tmp[5], tmp[6], tmp[7]);
+ kv_pair_fmt(kv, "fmap_ver_major", "%d", fmap->ver_major);
+ kv_pair_fmt(kv, "fmap_ver_minor","%d", fmap->ver_minor);
+ kv_pair_fmt(kv, "fmap_base", "0x%016llx",
+ (unsigned long long)fmap->base);
+ kv_pair_fmt(kv, "fmap_size", "0x%04x", fmap->size);
+ kv_pair_fmt(kv, "fmap_name", "%s", fmap->name);
+ kv_pair_fmt(kv, "fmap_nareas", "%d", fmap->nareas);
+ kv_pair_print(kv);
+ kv_pair_free(kv);
+
+ for (i = 0; i < fmap->nareas; i++) {
+ struct kv_pair *kv;
+ uint16_t flags;
+ char *str;
+
+ kv = kv_pair_new();
+ if (!kv)
+ return -1;
+
+ kv_pair_fmt(kv, "area_offset", "0x%08x",
+ fmap->areas[i].offset);
+ kv_pair_fmt(kv, "area_size", "0x%08x",
+ fmap->areas[i].size);
+ kv_pair_fmt(kv, "area_name", "%s",
+ fmap->areas[i].name);
+ kv_pair_fmt(kv, "area_flags_raw", "0x%02x",
+ fmap->areas[i].flags);
+
+ /* Print descriptive strings for flags rather than the field */
+ flags = fmap->areas[i].flags;
+ if ((str = fmap_flags_to_string(flags)) == NULL)
+ return -1;
+ kv_pair_fmt(kv, "area_flags", "%s", str );
+ free(str);
+
+ kv_pair_print(kv);
+ kv_pair_free(kv);
+ }
+
+ return 0;
+}
+
+/* get SHA1 sum of all static regions described by the flashmap and copy into
+ *digest (which will be allocated and must be freed by the caller), */
+int fmap_get_csum(const uint8_t *image, unsigned int image_len, uint8_t **digest)
+{
+ int i;
+ struct fmap *fmap;
+ int fmap_offset;
+ SHA_CTX ctx;
+
+ if ((image == NULL))
+ return -1;
+
+ if ((fmap_offset = fmap_find(image, image_len)) < 0)
+ return -1;
+ fmap = (struct fmap *)(image + fmap_offset);
+
+ SHA_init(&ctx);
+
+ /* Iterate through flash map and calculate the checksum piece-wise. */
+ for (i = 0; i < fmap->nareas; i++) {
+ /* skip non-static areas */
+ if (!(fmap->areas[i].flags & FMAP_AREA_STATIC))
+ continue;
+
+ /* sanity check the offset */
+ if (fmap->areas[i].size + fmap->areas[i].offset > image_len) {
+ fprintf(stderr,
+ "(%s) invalid parameter detected in area %d\n",
+ __func__, i);
+ return -1;
+ }
+
+ SHA_update(&ctx,
+ image + fmap->areas[i].offset,
+ fmap->areas[i].size);
+ }
+
+ SHA_final(&ctx);
+ *digest = malloc(SHA_DIGEST_SIZE);
+ memcpy(*digest, ctx.buf, SHA_DIGEST_SIZE);
+
+ return SHA_DIGEST_SIZE;
+}
+
+/* convert raw flags field to user-friendly string */
+char *fmap_flags_to_string(uint16_t flags)
+{
+ char *str = NULL;
+ int i, total_size;
+
+ str = malloc(1);
+ str[0] = '\0';
+ total_size = 1;
+
+ for (i = 0; i < sizeof(flags) * CHAR_BIT; i++) {
+ if (!flags)
+ break;
+
+ if (flags & (1 << i)) {
+ const char *tmp = val2str(1 << i, flag_lut);
+
+ total_size += strlen(tmp);
+ str = realloc(str, total_size);
+ strcat(str, tmp);
+
+ flags &= ~(1 << i);
+ if (flags) {
+ total_size++;
+ str = realloc(str, total_size);
+ strcat(str, ",");
+ }
+ }
+ }
+
+ return str;
+}
+
+/* allocate and initialize a new fmap structure */
+struct fmap *fmap_create(uint64_t base, uint32_t size, uint8_t *name)
+{
+ struct fmap *fmap;
+
+ fmap = malloc(sizeof(*fmap));
+ if (!fmap)
+ return NULL;
+
+ memset(fmap, 0, sizeof(*fmap));
+ memcpy(&fmap->signature, FMAP_SIGNATURE, strlen(FMAP_SIGNATURE));
+ fmap->ver_major = VERSION_MAJOR;
+ fmap->ver_minor = VERSION_MINOR;
+ fmap->base = base;
+ fmap->size = size;
+ memccpy(&fmap->name, name, '\0', FMAP_STRLEN);
+
+ return fmap;
+}
+
+/* free memory used by an fmap structure */
+void fmap_destroy(struct fmap *fmap) {
+ free(fmap);
+}
+
+/* append area to existing structure, return new total size if successful */
+int fmap_append_area(struct fmap **fmap,
+ uint32_t offset, uint32_t size,
+ const uint8_t *name, uint16_t flags)
+{
+ struct fmap_area *area;
+ int orig_size, new_size;
+
+ if ((fmap == NULL || *fmap == NULL) || (name == NULL))
+ return -1;
+
+ /* too many areas */
+ if ((*fmap)->nareas >= 0xffff)
+ return -1;
+
+ orig_size = fmap_size(*fmap);
+ new_size = orig_size + sizeof(*area);
+
+ *fmap = realloc(*fmap, new_size);
+ if (*fmap == NULL)
+ return -1;
+
+ area = (struct fmap_area *)((uint8_t *)*fmap + orig_size);
+ memset(area, 0, sizeof(*area));
+ memcpy(&area->offset, &offset, sizeof(area->offset));
+ memcpy(&area->size, &size, sizeof(area->size));
+ memccpy(&area->name, name, '\0', FMAP_STRLEN);
+ memcpy(&area->flags, &flags, sizeof(area->flags));
+
+ (*fmap)->nareas++;
+ return new_size;
+}
+
+struct fmap_area *fmap_find_area(struct fmap *fmap, const char *name)
+{
+ int i;
+ struct fmap_area *area = NULL;
+
+ if (!fmap || !name)
+ return NULL;
+
+ for (i = 0; i < fmap->nareas; i++) {
+ if (!strcmp((const char *)fmap->areas[i].name, name)) {
+ area = &fmap->areas[i];
+ break;
+ }
+ }
+
+ return area;
+}
+
+/*
+ * LCOV_EXCL_START
+ * Unit testing stuff done here so we do not need to expose static functions.
+ */
+static enum test_status { pass = EXIT_SUCCESS, fail = EXIT_FAILURE } status;
+static struct fmap *fmap_create_test(void)
+{
+ struct fmap *fmap;
+ uint64_t base = 0;
+ uint32_t size = 0x100000;
+ char name[] = "test_fmap";
+
+ status = fail;
+
+ fmap = fmap_create(base, size, (uint8_t *)name);
+ if (!fmap)
+ return NULL;
+
+ if (memcmp(&fmap->signature, FMAP_SIGNATURE, strlen(FMAP_SIGNATURE))) {
+ printf("FAILURE: signature is incorrect\n");
+ goto fmap_create_test_exit;
+ }
+
+ if ((fmap->ver_major != VERSION_MAJOR) ||
+ (fmap->ver_minor != VERSION_MINOR)) {
+ printf("FAILURE: version is incorrect\n");
+ goto fmap_create_test_exit;
+ }
+
+ if (fmap->base != base) {
+ printf("FAILURE: base is incorrect\n");
+ goto fmap_create_test_exit;
+ }
+
+ if (fmap->size != 0x100000) {
+ printf("FAILURE: size is incorrect\n");
+ goto fmap_create_test_exit;
+ }
+
+ if (strcmp((char *)fmap->name, "test_fmap")) {
+ printf("FAILURE: name is incorrect\n");
+ goto fmap_create_test_exit;
+ }
+
+ if (fmap->nareas != 0) {
+ printf("FAILURE: number of areas is incorrect\n");
+ goto fmap_create_test_exit;
+ }
+
+ status = pass;
+fmap_create_test_exit:
+ /* preserve fmap if all went well */
+ if (status == fail) {
+ fmap_destroy(fmap);
+ fmap = NULL;
+ }
+ return fmap;
+}
+
+static int fmap_print_test(struct fmap *fmap)
+{
+ return fmap_print(fmap);
+}
+
+static int fmap_get_csum_test(struct fmap *fmap)
+{
+ uint8_t *digest = NULL, *image = NULL;
+ /* assume 0x100-0x10100 is marked "static" and is filled with 0x00 */
+ int image_size = 0x20000;
+ uint8_t csum[SHA_DIGEST_SIZE] = {
+ 0x1a, 0xdc, 0x95, 0xbe, 0xbe, 0x9e, 0xea, 0x8c,
+ 0x11, 0x2d, 0x40, 0xcd, 0x04, 0xab, 0x7a, 0x8d,
+ 0x75, 0xc4, 0xf9, 0x61 };
+
+ status = fail;
+
+ if ((fmap_get_csum(NULL, image_size, &digest) >= 0) ||
+ (fmap_get_csum(image, image_size, NULL) >= 0)) {
+ printf("failed to abort on NULL pointer input\n");
+ goto fmap_get_csum_test_exit;
+ }
+
+ image = calloc(image_size, 1);
+ memcpy(image, fmap, fmap_size(fmap));
+
+ if (fmap_get_csum(image, image_size, &digest) != SHA_DIGEST_SIZE) {
+ printf("FAILURE: failed to calculate checksum\n");
+ goto fmap_get_csum_test_exit;
+ }
+ if (memcmp(digest, csum, SHA_DIGEST_SIZE)) {
+ printf("FAILURE: checksum is incorrect\n");
+ goto fmap_get_csum_test_exit;
+ }
+
+ status = pass;
+fmap_get_csum_test_exit:
+ free(image);
+ free(digest);
+ return status;
+}
+
+static int fmap_size_test(struct fmap *fmap)
+{
+ status = fail;
+
+ if (fmap_size(NULL) >= 0) {
+ printf("FAILURE: failed to abort on NULL pointer input\n");
+ goto fmap_size_test_exit;
+ }
+
+ status = pass;
+fmap_size_test_exit:
+ return status;
+}
+
+/* this test re-allocates the fmap, so it gets a double-pointer */
+static int fmap_append_area_test(struct fmap **fmap)
+{
+ int total_size;
+ uint16_t nareas_orig;
+ /* test_area will be used by fmap_csum_test and find_area_test */
+ struct fmap_area test_area = {
+ .offset = 0x400,
+ .size = 0x10000,
+ .name = "test_area_1",
+ .flags = FMAP_AREA_STATIC,
+ };
+
+ status = fail;
+
+ if ((fmap_append_area(NULL, 0, 0, test_area.name, 0) >= 0) ||
+ (fmap_append_area(fmap, 0, 0, NULL, 0) >= 0)) {
+ printf("FAILURE: failed to abort on NULL pointer input\n");
+ goto fmap_append_area_test_exit;
+ }
+
+ nareas_orig = (*fmap)->nareas;
+ (*fmap)->nareas = ~(0);
+ if (fmap_append_area(fmap, 0, 0, (const uint8_t *)"foo", 0) >= 0) {
+ printf("FAILURE: failed to abort with too many areas\n");
+ goto fmap_append_area_test_exit;
+ }
+ (*fmap)->nareas = nareas_orig;
+
+ total_size = sizeof(**fmap) + sizeof(test_area);
+ if (fmap_append_area(fmap,
+ test_area.offset,
+ test_area.size,
+ test_area.name,
+ test_area.flags
+ ) != total_size) {
+ printf("failed to append area\n");
+ goto fmap_append_area_test_exit;
+ }
+
+ if ((*fmap)->nareas != 1) {
+ printf("FAILURE: failed to increment number of areas\n");
+ goto fmap_append_area_test_exit;
+ }
+
+ status = pass;
+fmap_append_area_test_exit:
+ return status;
+}
+
+static int fmap_find_area_test(struct fmap *fmap)
+{
+ status = fail;
+ char area_name[] = "test_area_1";
+
+ if (fmap_find_area(NULL, area_name) ||
+ fmap_find_area(fmap, NULL)) {
+ printf("FAILURE: failed to abort on NULL pointer input\n");
+ goto fmap_find_area_test_exit;
+ }
+
+ if (fmap_find_area(fmap, area_name) == NULL) {
+ printf("FAILURE: failed to find \"%s\"\n", area_name);
+ goto fmap_find_area_test_exit;
+ }
+
+ status = pass;
+fmap_find_area_test_exit:
+ return status;
+}
+
+static int fmap_flags_to_string_test()
+{
+ char *str, *my_str;
+ int i;
+ uint16_t flags;
+
+ status = fail;
+
+ /* no area flag */
+ str = fmap_flags_to_string(0);
+ if (!str || strcmp(str, "")) {
+ printf("FAILURE: failed to return empty string when no flag"
+ "are set");
+ goto fmap_flags_to_string_test_exit;
+ }
+ free(str);
+
+ /* single area flags */
+ for (i = 0; i < ARRAY_SIZE(flag_lut); i++) {
+ if (!flag_lut[i].str)
+ continue;
+
+ if ((str = fmap_flags_to_string(flag_lut[i].val)) == NULL) {
+ printf("FAILURE: failed to translate flag to string");
+ goto fmap_flags_to_string_test_exit;
+ }
+ free(str);
+ }
+
+ /* construct our own flags field and string using all available flags
+ * and compare output with fmap_flags_to_string() */
+ my_str = calloc(256, 1);
+ flags = 0;
+ for (i = 0; i < ARRAY_SIZE(flag_lut); i++) {
+ if (!flag_lut[i].str)
+ continue;
+ else if (i > 0)
+ strcat(my_str, ",");
+
+ flags |= flag_lut[i].val;
+ strcat(my_str, flag_lut[i].str);
+ }
+
+ str = fmap_flags_to_string(flags);
+ if (strcmp(str, my_str)) {
+ printf("FAILURE: bad result from fmap_flags_to_string\n");
+ goto fmap_flags_to_string_test_exit;
+ }
+ free(my_str);
+ free(str);
+
+ status = pass;
+fmap_flags_to_string_test_exit:
+ return status;
+
+}
+
+static int fmap_find_test(struct fmap *fmap)
+{
+ uint8_t *buf;
+ size_t total_size, offset;
+
+ status = fail;
+
+ /*
+ * Note: In these tests, we'll use fmap_find() and control usage of
+ * lsearch and bsearch by using a power-of-2 total_size. For lsearch,
+ * use total_size - 1. For bsearch, use total_size.
+ */
+
+ total_size = 0x100000;
+ buf = calloc(total_size, 1);
+
+ /* test if image length is zero */
+ if (fmap_find(buf, 0) >= 0) {
+ printf("FAILURE: failed to abort on zero-length image\n");
+ goto fmap_find_test_exit;
+ }
+
+ /* test if no fmap exists */
+ if (fmap_find(buf, total_size - 1) >= 0) {
+ printf("FAILURE: lsearch returned false positive\n");
+ goto fmap_find_test_exit;
+ }
+ if (fmap_find(buf, total_size) >= 0) {
+ printf("FAILURE: bsearch returned false positive\n");
+ goto fmap_find_test_exit;
+ }
+
+ /* simple test case: fmap at (total_size / 2) + 1 */
+ offset = (total_size / 2) + 1;
+ memcpy(&buf[offset], fmap, fmap_size(fmap));
+
+ if (fmap_find(buf, total_size - 1) != offset) {
+ printf("FAILURE: lsearch failed to find fmap\n");
+ goto fmap_find_test_exit;
+ }
+ if (fmap_find(buf, total_size) != offset) {
+ printf("FAILURE: bsearch failed to find fmap\n");
+ goto fmap_find_test_exit;
+ }
+
+ /* test bsearch if offset is at 0 */
+ offset = 0;
+ memset(buf, 0, total_size);
+ memcpy(buf, fmap, fmap_size(fmap));
+ if (fmap_find(buf, total_size) != offset) {
+ printf("FAILURE: bsearch failed to find fmap at offset 0\n");
+ goto fmap_find_test_exit;
+ }
+
+ /* test overrun detection */
+ memset(buf, 0, total_size);
+ memcpy(&buf[total_size - fmap_size(fmap) + 1],
+ fmap,
+ fmap_size(fmap) + 1);
+ if (fmap_find(buf, total_size - 1) >= 0) {
+ printf("FAILURE: lsearch failed to catch overrun\n");
+ goto fmap_find_test_exit;
+ }
+ if (fmap_find(buf, total_size) >= 0) {
+ printf("FAILURE: bsearch failed to catch overrun\n");
+ goto fmap_find_test_exit;
+ }
+
+ status = pass;
+fmap_find_test_exit:
+ free(buf);
+ return status;
+}
+
+int fmap_test()
+{
+ int rc = EXIT_SUCCESS;
+ struct fmap *my_fmap;
+
+ /*
+ * This test has two parts: Creation of an fmap with one or more
+ * area(s), and other stuff. Since a valid fmap is required to run
+ * many tests, we abort if fmap creation fails in any way.
+ *
+ * Also, fmap_csum_test() makes some assumptions based on the areas
+ * appended. See fmap_append_area_test() for details.
+ */
+ if ((my_fmap = fmap_create_test()) == NULL) {
+ rc = EXIT_FAILURE;
+ goto fmap_test_exit;
+ }
+
+ if (fmap_find_test(my_fmap)) {
+ rc = EXIT_FAILURE;
+ goto fmap_test_exit;
+ }
+
+ if (fmap_append_area_test(&my_fmap)) {
+ rc = EXIT_FAILURE;
+ goto fmap_test_exit;
+ }
+
+ rc |= fmap_find_area_test(my_fmap);
+ rc |= fmap_get_csum_test(my_fmap);
+ rc |= fmap_size_test(my_fmap);
+ rc |= fmap_flags_to_string_test();
+ rc |= fmap_print_test(my_fmap);
+
+fmap_test_exit:
+ fmap_destroy(my_fmap);
+ if (rc)
+ printf("FAILED\n");
+ return rc;
+}
+/* LCOV_EXCL_STOP */
diff --git a/util/cbfstool/flashmap/fmap.h b/util/cbfstool/flashmap/fmap.h
new file mode 100644
index 0000000..9cea22d
--- /dev/null
+++ b/util/cbfstool/flashmap/fmap.h
@@ -0,0 +1,192 @@
+/*
+ * Copyright 2010, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Alternatively, this software may be distributed under the terms of the
+ * GNU General Public License ("GPL") version 2 as published by the Free
+ * Software Foundation.
+ */
+
+#ifndef FLASHMAP_LIB_FMAP_H__
+#define FLASHMAP_LIB_FMAP_H__
+
+#include <inttypes.h>
+
+#include <valstr.h>
+
+#define FMAP_SIGNATURE "__FMAP__"
+#define FMAP_VER_MAJOR 1 /* this header's FMAP minor version */
+#define FMAP_VER_MINOR 1 /* this header's FMAP minor version */
+#define FMAP_STRLEN 32 /* maximum length for strings, */
+ /* including null-terminator */
+extern const struct valstr flag_lut[16];
+enum fmap_flags {
+ FMAP_AREA_STATIC = 1 << 0,
+ FMAP_AREA_COMPRESSED = 1 << 1,
+ FMAP_AREA_RO = 1 << 2,
+};
+
+/* Mapping of volatile and static regions in firmware binary */
+struct fmap_area {
+ uint32_t offset; /* offset relative to base */
+ uint32_t size; /* size in bytes */
+ uint8_t name[FMAP_STRLEN]; /* descriptive name */
+ uint16_t flags; /* flags for this area */
+} __attribute__((packed));
+
+struct fmap {
+ uint8_t signature[8]; /* "__FMAP__" (0x5F5F464D41505F5F) */
+ uint8_t ver_major; /* major version */
+ uint8_t ver_minor; /* minor version */
+ uint64_t base; /* address of the firmware binary */
+ uint32_t size; /* size of firmware binary in bytes */
+ uint8_t name[FMAP_STRLEN]; /* name of this firmware binary */
+ uint16_t nareas; /* number of areas described by
+ fmap_areas[] below */
+ struct fmap_area areas[];
+} __attribute__((packed));
+
+/*
+ * fmap_find - find FMAP signature in a binary image
+ *
+ * @image: binary image
+ * @len: length of binary image
+ *
+ * This function does no error checking. The caller is responsible for
+ * verifying that the contents are sane.
+ *
+ * returns offset of FMAP signature to indicate success
+ * returns <0 to indicate failure
+ */
+extern long int fmap_find(const uint8_t *image, unsigned int len);
+
+/*
+ * fmap_print - Print contents of flash map data structure
+ *
+ * @map: raw map data
+ *
+ * returns 0 to indiciate success
+ * returns <0 to indicate failure
+ */
+extern int fmap_print(const struct fmap *map);
+
+/*
+ * fmap_get_csum - get the checksum of static regions of an image
+ *
+ * @image: image to checksum
+ * @len: length of image
+ * @digest: double-pointer to store location of first byte of digest
+ *
+ * fmap_get_csum() will reset, write, and finalize the digest.
+ * The location of the final digest will start at the location pointed to
+ * by digest, which will be allocated and must be freed by the caller.
+ *
+ * returns digest length if successful
+ * returns <0 to indicate error
+ */
+extern int fmap_get_csum(const uint8_t *image,
+ unsigned int image_len, uint8_t **digest);
+
+
+/*
+ * fmap_flags_to_string - convert raw flags field into user-friendly string
+ *
+ * @flags: raw flags
+ *
+ * This function returns a user-friendly comma-separated list of fmap area
+ * flags. If there are no flags (flags == 0), the string will contain only
+ * a terminating character ('\0')
+ *
+ * This function allocates memory which the caller must free.
+ *
+ * returns pointer to an allocated string if successful
+ * returns NULL to indicate failure
+ */
+char *fmap_flags_to_string(uint16_t flags);
+
+/*
+ * fmap_create - allocate and initialize a new fmap structure
+ *
+ * @base: base address of firmware within address space
+ * @size: size of the firmware (bytes)
+ * @name: name of firmware
+ *
+ * This function will allocate a flashmap header. Members of the structure
+ * which are not passed in are automatically initialized.
+ *
+ * returns pointer to newly allocated flashmap header if successful
+ * returns NULL to indicate failure
+ */
+extern struct fmap *fmap_create(uint64_t base,
+ uint32_t size, uint8_t *name);
+
+/* free memory used by an fmap structure */
+extern void fmap_destroy(struct fmap *fmap);
+
+/*
+ * fmap_size - returns size of fmap data structure (including areas)
+ *
+ * @fmap: fmap
+ *
+ * returns size of fmap structure if successful
+ * returns <0 to indicate failure
+ */
+extern int fmap_size(struct fmap *fmap);
+
+/*
+ * fmap_append_area - realloc an existing flashmap and append an area
+ *
+ * @fmap: double pointer to existing flashmap
+ * @offset: offset of area
+ * @size: size of area
+ * @name: name of area
+ * @flags: area flags
+ *
+ * returns total size of reallocated flashmap structure if successful
+ * returns <0 to indicate failure
+ */
+extern int fmap_append_area(struct fmap **fmap,
+ uint32_t offset, uint32_t size,
+ const uint8_t *name, uint16_t flags);
+
+/*
+ * fmap_find_area - find an fmap_area entry (by name) and return pointer to it
+ *
+ * @fmap: fmap structure to parse
+ * @name: name of area to find
+ *
+ * returns a pointer to the entry in the fmap structure if successful
+ * returns NULL to indicate failure or if no matching area entry is found
+ */
+extern struct fmap_area *fmap_find_area(struct fmap *fmap, const char *name);
+
+/* unit testing stuff */
+extern int fmap_test();
+
+#endif /* FLASHMAP_LIB_FMAP_H__*/
diff --git a/util/cbfstool/flashmap/kv_pair.c b/util/cbfstool/flashmap/kv_pair.c
new file mode 100644
index 0000000..2572c64
--- /dev/null
+++ b/util/cbfstool/flashmap/kv_pair.c
@@ -0,0 +1,242 @@
+/*
+ * Copyright 2010, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Alternatively, this software may be distributed under the terms of the
+ * GNU General Public License ("GPL") version 2 as published by the Free
+ * Software Foundation.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+
+#include "kv_pair.h"
+
+/* Internal variable for output style. Use accessors to get/set style. */
+static enum kv_pair_style _style;
+
+void kv_pair_set_style(enum kv_pair_style style)
+{
+ _style = style;
+}
+
+enum kv_pair_style kv_pair_get_style()
+{
+ return _style;
+}
+
+struct kv_pair *kv_pair_new(void)
+{
+ struct kv_pair *kv;
+
+ kv = calloc(0, sizeof(*kv));
+ if (!kv)
+ return NULL;
+
+ return kv;
+}
+
+struct kv_pair **kv_pair_new_array(size_t size)
+{
+ struct kv_pair **kv;
+ size_t i;
+
+ kv = (struct kv_pair **) calloc(0, sizeof(kv) * size);
+ for (i = 0; i < size; ++i) {
+ kv[i] = kv_pair_new();
+ }
+ return kv;
+}
+
+struct kv_pair *kv_pair_add(struct kv_pair *kv_list,
+ const char *key, const char *value)
+{
+ struct kv_pair *kv_new;
+ struct kv_pair *kv_ptr;
+
+ kv_new = kv_pair_new();
+ if (!kv_new)
+ return NULL;
+
+ /* save key=value strings if provided */
+ if (key) {
+ kv_new->key = strdup(key);
+ if (!kv_new->key)
+ goto kv_pair_add_failed;
+ }
+ if (value) {
+ kv_new->value = strdup(value);
+ if (!kv_new->value)
+ goto kv_pair_add_failed;
+ }
+
+ /* first in the list if no list provided */
+ if (kv_list) {
+ /* find the end of list */
+ for (kv_ptr = kv_list; kv_ptr->next != NULL;
+ kv_ptr = kv_ptr->next)
+ ;
+
+ /* link in the new pair at the end */
+ kv_ptr->next = kv_new;
+ }
+
+ /* return pointer to the new pair */
+ return kv_new;
+
+kv_pair_add_failed:
+ kv_pair_free(kv_new);
+ return NULL;
+}
+
+struct kv_pair *kv_pair_add_bool(struct kv_pair *kv_list,
+ const char *key, int value)
+{
+ const char *str;
+
+ if (value) {
+ str = "yes";
+ } else {
+ str = "no";
+ }
+ return kv_pair_add(kv_list, key, str);
+}
+
+struct kv_pair *kv_pair_fmt(struct kv_pair *kv_list,
+ const char *kv_key, const char *format, ...)
+{
+ char kv_value[KV_PAIR_MAX_VALUE_LEN];
+ va_list vptr;
+
+ memset(kv_value, 0, sizeof(kv_value));
+
+ va_start(vptr, format);
+ vsnprintf(kv_value, sizeof(kv_value), format, vptr);
+ va_end(vptr);
+
+ return kv_pair_add(kv_list, kv_key, kv_value);
+}
+
+void kv_pair_free(struct kv_pair *kv_list)
+{
+ struct kv_pair *kv_ptr = kv_list;
+ struct kv_pair *kv_next;
+
+ while (kv_ptr != NULL) {
+ /* free key/value strings */
+ if (kv_ptr->key)
+ free(kv_ptr->key);
+ if (kv_ptr->value)
+ free(kv_ptr->value);
+
+ /* free current pair move to next */
+ kv_next = kv_ptr->next;
+ free(kv_ptr);
+ kv_ptr = kv_next;
+ }
+}
+
+void kv_pair_free_array(struct kv_pair **kv_array, size_t size)
+{
+ size_t i;
+ for (i = 0; i < size; ++i) {
+ kv_pair_free(kv_array[i]);
+ }
+ free(kv_array);
+}
+
+void kv_pair_print_to_file(FILE* fp, struct kv_pair *kv_list,
+ enum kv_pair_style style)
+{
+ struct kv_pair *kv_ptr;
+
+ switch (style) {
+ case KV_STYLE_PAIR:
+ for (kv_ptr = kv_list; kv_ptr != NULL; kv_ptr = kv_ptr->next) {
+ if (kv_ptr->key && kv_ptr->value) {
+ fprintf(fp, "%s=\"%s\" ",
+ kv_ptr->key, kv_ptr->value);
+ }
+ }
+ break;
+
+ case KV_STYLE_VALUE:
+ for (kv_ptr = kv_list; kv_ptr != NULL; kv_ptr = kv_ptr->next) {
+ if (kv_ptr->value) {
+ fprintf(fp, "%s", kv_ptr->value);
+ if (kv_ptr->next)
+ fprintf(fp, " | ");
+ }
+ }
+ break;
+
+ case KV_STYLE_LONG:
+ for (kv_ptr = kv_list; kv_ptr != NULL; kv_ptr = kv_ptr->next) {
+ if (kv_ptr->key && kv_ptr->value)
+ fprintf(fp, "%-20s | %s\n",
+ kv_ptr->key, kv_ptr->value);
+ }
+ break;
+ }
+
+ fprintf(fp, "\n");
+}
+
+void kv_pair_print(struct kv_pair *kv_list)
+{
+ kv_pair_print_to_file(stdout, kv_list, kv_pair_get_style());
+}
+
+const char *kv_pair_get_value(struct kv_pair *kv_list, const char *kv_key)
+{
+ const char *kv_value = NULL;
+ struct kv_pair *kv_ptr;
+
+ for (kv_ptr = kv_list; kv_ptr != NULL; kv_ptr = kv_ptr->next) {
+ if (kv_ptr->key && strcmp(kv_ptr->key, kv_key) == 0) {
+ kv_value = kv_ptr->value;
+ break;
+ }
+ }
+ return kv_value;
+}
+
+int kv_pair_size(struct kv_pair *kv_list) {
+ struct kv_pair *kv_ptr;
+ int count;
+
+ count = 0;
+ for (kv_ptr = kv_list; kv_ptr != NULL; kv_ptr = kv_ptr->next) {
+ if (kv_ptr->key) {
+ count++;
+ }
+ }
+ return count;
+}
diff --git a/util/cbfstool/flashmap/kv_pair.h b/util/cbfstool/flashmap/kv_pair.h
new file mode 100644
index 0000000..b573530
--- /dev/null
+++ b/util/cbfstool/flashmap/kv_pair.h
@@ -0,0 +1,153 @@
+/*
+ * Copyright 2010, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Alternatively, this software may be distributed under the terms of the
+ * GNU General Public License ("GPL") version 2 as published by the Free
+ * Software Foundation.
+ */
+
+#ifndef FLASHMAP_LIB_KV_PAIR_H__
+#define FLASHMAP_LIB_KV_PAIR_H__
+
+#include <stdio.h>
+#include <inttypes.h>
+#include <string.h>
+
+/* key=value string pair list */
+#define KV_PAIR_MAX_VALUE_LEN 1024
+
+enum kv_pair_style {
+ KV_STYLE_PAIR, /* key1="value1" key2="value2" */
+ KV_STYLE_VALUE, /* | value1 | value2 | */
+ KV_STYLE_LONG, /* key1 | value1 */
+ /* key2 | value2 */
+};
+
+struct kv_pair {
+ char *key;
+ char *value;
+ struct kv_pair *next;
+};
+
+extern enum kv_pair_style kv_pair_get_style();
+
+extern void kv_pair_set_style(enum kv_pair_style style);
+
+/*
+ * kv_pair_new - create new key=value pair
+ *
+ * returns pointer to new key=value pair
+ * returns NULL to indicate error
+ */
+extern struct kv_pair *kv_pair_new(void);
+
+/*
+ * kv_pair_add - add new key=value pair to list
+ *
+ * @kv_list: key=value pair list
+ * @key: key string
+ * @value: value string
+ *
+ * returns pointer to new key=value pair
+ * returns NULL to indicate error
+ */
+extern struct kv_pair *kv_pair_add(struct kv_pair *kv_list,
+ const char *key, const char *value);
+
+/*
+ * kv_pair_add_bool - add new boolean kvpair to list
+ *
+ * @kv_list: key=value pair list
+ * @key: key string
+ * @value: value
+ *
+ * returns pointer to new key=value pair
+ * returns NULL to indicate error
+ */
+extern struct kv_pair *kv_pair_add_bool(struct kv_pair *kv_list,
+ const char *key, int value);
+
+/*
+ * kv_pair_fmt - add key=value pair based on printf format
+ * NOTE: uses variable argument list
+ *
+ * @kv_list: list of key=value pairs
+ * @kv_key: key string
+ * @format: printf-style format for value input
+ * @...: arguments to format
+ *
+ * returns pointer to new key=value pair
+ * returns NULL to indicate error
+ */
+extern struct kv_pair *kv_pair_fmt(struct kv_pair *kv_list,
+ const char *kv_key, const char *format, ...)
+ __attribute__((format(printf, 3, 4)));
+
+/*
+ * kv_pair_free - clean a key=value pair list
+ *
+ * @kv_list: pointer to key=value list
+ */
+extern void kv_pair_free(struct kv_pair *kv_list);
+
+/*
+ * kv_pair_print - print a key=value pair list
+ *
+ * @kv_list: pointer to key=value list
+ * @style: print style
+ */
+extern void kv_pair_print_to_file(FILE* fp, struct kv_pair *kv_list,
+ enum kv_pair_style style);
+
+/*
+ * kv_pair_print - print a key=value pair list to gsys output
+ *
+ * @kv_list: pointer to key=value list
+ */
+extern void kv_pair_print(struct kv_pair *kv_list);
+
+
+/*
+ * kv_pair_get_value - return first value with key match
+ *
+ * @kv_list: pointer to key=value list
+ * @kv_key: key string
+ */
+extern const char *kv_pair_get_value(struct kv_pair *kv_list,
+ const char *kv_key);
+
+/*
+ * kv_pair_size - return number of kv pairs in the chain
+ *
+ * @kv_list: pointer to key=value list
+ */
+extern int kv_pair_size(struct kv_pair *kv_list);
+
+#endif /* FLASHMAP_LIB_KV_PAIR_H__ */
diff --git a/util/cbfstool/flashmap/valstr.c b/util/cbfstool/flashmap/valstr.c
new file mode 100644
index 0000000..fa9ed82
--- /dev/null
+++ b/util/cbfstool/flashmap/valstr.c
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2010, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <inttypes.h>
+#include <string.h>
+
+#include <valstr.h>
+
+const char *val2str_default(uint32_t val, const struct valstr *vs,
+ const char *def_str)
+{
+ int i;
+
+ for (i = 0; vs[i].str; i++) {
+ if (vs[i].val == val)
+ return vs[i].str;
+ }
+
+ return def_str;
+}
+
+const char *val2str(uint32_t val, const struct valstr *vs)
+{
+ return val2str_default(val, vs, "Unknown");
+}
+
+uint32_t str2val(const char *str, const struct valstr *vs)
+{
+ int i;
+
+ for (i = 0; vs[i].str; i++) {
+ if (strcasecmp(vs[i].str, str) == 0)
+ return vs[i].val;
+ }
+
+ return vs[i].val;
+}
diff --git a/util/cbfstool/flashmap/valstr.h b/util/cbfstool/flashmap/valstr.h
new file mode 100644
index 0000000..3885ef7
--- /dev/null
+++ b/util/cbfstool/flashmap/valstr.h
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2010, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef FLASHMAP_LIB_VALSTR_H__
+#define FLASHMAP_LIB_VALSTR_H__
+
+#include <inttypes.h>
+
+/* value + string structure for common conversions */
+struct valstr {
+ uint32_t val; /* field value */
+ const char *str; /* field description */
+};
+
+/*
+ * val2str_default - convert value to string
+ *
+ * @val: value to convert
+ * @vs: value-string data
+ * @def_str: default string to return if no matching value found
+ *
+ * returns pointer to string
+ * returns def_str if no matching value found
+ */
+const char *val2str_default(uint32_t val, const struct valstr *vs,
+ const char *def_str);
+
+/*
+ * val2str - convert value to string
+ *
+ * @val: value to convert
+ * @vs: value-string data
+ *
+ * returns pointer to string
+ * returns pointer to "unknown" static string if not found
+ */
+const char *val2str(uint32_t val, const struct valstr *vs);
+
+/*
+ * str2val - convert string to value
+ *
+ * @str: string to convert
+ * @vs: value-string data
+ *
+ * returns value for string
+ * returns value for last entry in value-string data if not found
+ */
+uint32_t str2val(const char *str, const struct valstr *vs);
+
+#endif /* FLASHMAP_LIB_VALSTR_H__ */