Add support to get layout from fmap (e.g. coreboot rom)
Flashmap, or simply fmap, is a binary data format for describing
region offsets, sizes, and certain attributes and is widely used by
coreboot. This patch adds support for the fmap data format version 1.1
and adds --fmap and --fmap-file arguments.
Using --fmap will make flashrom to search the ROM content for fmap
data. Using --fmap-file will make flashrom search a supplied file
for fmap data.
An example of how to update the COREBOOT region of a ROM:
flashrom -p programmer --fmap -w coreboot.rom -i COREBOOT
flashrom -p programmer --fmap-file coreboot.rom -w coreboot.rom -i COREBOOT
The fmap functions are mostly copied from cbfstool.
Currently it is made mutually exclusive with other layout options until
we are more clever about this input.
Change-Id: I0e7fad38ed79a84d41358e1f175c36d255786c12
Signed-off-by: Arthur Heymans <arthur@aheymans.xyz>
Signed-off-by: David Hendricks <dhendricks@fb.com>
Reviewed-on: https://review.coreboot.org/23203
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Tested-by: David Hendricks <david.hendricks@gmail.com>
Reviewed-by: Werner Zeh <werner.zeh@siemens.com>
Reviewed-by: Nico Huber <nico.h@gmx.de>
diff --git a/libflashrom.c b/libflashrom.c
index 34e881a..f90a22c 100644
--- a/libflashrom.c
+++ b/libflashrom.c
@@ -25,6 +25,7 @@
#include <stdarg.h>
#include "flash.h"
+#include "fmap.h"
#include "programmer.h"
#include "layout.h"
#include "hwaccess.h"
@@ -382,6 +383,124 @@
#endif
}
+static int flashrom_layout_parse_fmap(struct flashrom_layout **layout,
+ struct flashctx *const flashctx, const struct fmap *const fmap)
+{
+ int i;
+ struct flashrom_layout *l = get_global_layout();
+
+ if (!fmap || !l)
+ return 1;
+
+ if (l->num_entries + fmap->nareas > MAX_ROMLAYOUT) {
+ msg_gerr("Cannot add fmap entries to layout - Too many entries.\n");
+ return 1;
+ }
+
+ for (i = 0; i < fmap->nareas; i++) {
+ l->entries[l->num_entries].start = fmap->areas[i].offset;
+ l->entries[l->num_entries].end = fmap->areas[i].offset + fmap->areas[i].size - 1;
+ l->entries[l->num_entries].included = false;
+ memset(l->entries[l->num_entries].name, 0, sizeof(l->entries[i].name));
+ memcpy(l->entries[l->num_entries].name, fmap->areas[i].name,
+ min(FMAP_STRLEN, sizeof(l->entries[i].name)));
+ msg_gdbg("fmap %08x - %08x named %s\n",
+ l->entries[l->num_entries].start,
+ l->entries[l->num_entries].end,
+ l->entries[l->num_entries].name);
+ l->num_entries++;
+ }
+
+ *layout = l;
+ return 0;
+}
+
+/**
+ * @brief Read a layout by searching the flash chip for fmap.
+ *
+ * @param[out] layout Points to a struct flashrom_layout pointer that
+ * gets set if the fmap is read and parsed successfully.
+ * @param[in] flashctx Flash context
+ * @param[in] offset Offset to begin searching for fmap.
+ * @param[in] offset Length of address space to search.
+ *
+ * @return 0 on success,
+ * 3 if fmap parsing isn't implemented for the host,
+ * 2 if the fmap couldn't be read,
+ * 1 on any other error.
+ */
+int flashrom_layout_read_fmap_from_rom(struct flashrom_layout **const layout,
+ struct flashctx *const flashctx, off_t offset, size_t len)
+{
+#ifndef __FLASHROM_LITTLE_ENDIAN__
+ return 3;
+#else
+ struct fmap *fmap = NULL;
+ int ret = 0;
+
+ msg_gdbg("Attempting to read fmap from ROM content.\n");
+ if (fmap_read_from_rom(&fmap, flashctx, offset, len)) {
+ msg_gerr("Failed to read fmap from ROM.\n");
+ return 1;
+ }
+
+ msg_gdbg("Adding fmap layout to global layout.\n");
+ if (flashrom_layout_parse_fmap(layout, flashctx, fmap)) {
+ msg_gerr("Failed to add fmap regions to layout.\n");
+ ret = 1;
+ }
+
+ free(fmap);
+ return ret;
+#endif
+}
+
+/**
+ * @brief Read a layout by searching buffer for fmap.
+ *
+ * @param[out] layout Points to a struct flashrom_layout pointer that
+ * gets set if the fmap is read and parsed successfully.
+ * @param[in] flashctx Flash context
+ * @param[in] buffer Buffer to search in
+ * @param[in] size Size of buffer to search
+ *
+ * @return 0 on success,
+ * 3 if fmap parsing isn't implemented for the host,
+ * 2 if the fmap couldn't be read,
+ * 1 on any other error.
+ */
+int flashrom_layout_read_fmap_from_buffer(struct flashrom_layout **const layout,
+ struct flashctx *const flashctx, const uint8_t *const buf, size_t size)
+{
+#ifndef __FLASHROM_LITTLE_ENDIAN__
+ return 3;
+#else
+ struct fmap *fmap = NULL;
+ int ret = 1;
+
+ if (!buf || !size)
+ goto _ret;
+
+ msg_gdbg("Attempting to read fmap from buffer.\n");
+ if (fmap_read_from_buffer(&fmap, buf, size)) {
+ msg_gerr("Failed to read fmap from buffer.\n");
+ goto _ret;
+ }
+
+ msg_gdbg("Adding fmap layout to global layout.\n");
+ if (flashrom_layout_parse_fmap(layout, flashctx, fmap)) {
+ msg_gerr("Failed to add fmap regions to layout.\n");
+ goto _free_ret;
+ }
+
+ ret = 0;
+_free_ret:
+ free(fmap);
+_ret:
+ return ret;
+#endif
+}
+
/**
* @brief Free a layout.
*