Nico Huber has uploaded this change for review. ( https://review.coreboot.org/21788
Change subject: Add a convenient libflashrom interface
......................................................................
Add a convenient libflashrom interface
This adds a minimal libflashrom interface based on the draft in the
wiki. While the glue code in libflashrom.c is build on top of the
existing code instead on overhauling it, the interface in libflashrom.h
is supposed to be stable. So we can keep the interface and adapt
internals later if favoured, without breaking clients.
A new make target, libinstall, is also added. It installs libflashrom.a
and libflashrom.h in lib/ and include/ dirs respectively.
Hooking this into the build would break linking of the CLI and is post-
poned until that got fixed.
v2: Rebase and fixes by Anton Kochkov.
v3: o fl_image_*() rewritten with layout support (touch only included regions).
o Moved read/erase/write/verify operations to flashrom.c.
o Added layout pointer and flags to the flash context.
v4: Removed libflashrom.o from LIB_OBJS until CLI is adapted.
v5: o Incorporated David's comments.
o Added `fl_flashprog_t` as dummy parameter to hide the fact that
we have global state all around, and for future-proofness ofc.
v6: o Change namespace prefix to flashrom_.
o Remove typedefs.
Original-Change-Id: I00f169990830aa17b7dfae5eb74010d40c476181
Original-Reviewed-on: https://review.coreboot.org/17946
Original-Tested-by: build bot (Jenkins) <no-reply(a)coreboot.org>
Original-Reviewed-by: David Hendricks <david.hendricks(a)gmail.com>
Change-Id: Ibe14c89c96e45ed7679332b0f5cf44b1c9f1d9ee
Signed-off-by: Nico Huber <nico.huber(a)secunet.com>
---
M Makefile
M flash.h
M flashrom.c
A libflashrom.c
A libflashrom.h
5 files changed, 724 insertions(+), 7 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/88/21788/1
diff --git a/Makefile b/Makefile
index 785e4ee..181fd99 100644
--- a/Makefile
+++ b/Makefile
@@ -1370,6 +1370,12 @@
$(INSTALL) -m 0755 $(PROGRAM)$(EXEC_SUFFIX) $(DESTDIR)$(PREFIX)/sbin
$(INSTALL) -m 0644 $(PROGRAM).8 $(DESTDIR)$(MANDIR)/man8
+libinstall: libflashrom.a libflashrom.h
+ mkdir -p $(DESTDIR)$(PREFIX)/lib
+ $(INSTALL) -m 0644 libflashrom.a $(DESTDIR)$(PREFIX)/lib
+ mkdir -p $(DESTDIR)$(PREFIX)/include
+ $(INSTALL) -m 0644 libflashrom.h $(DESTDIR)$(PREFIX)/include
+
_export: $(PROGRAM).8
@rm -rf "$(EXPORTDIR)/flashrom-$(RELEASENAME)"
@mkdir -p "$(EXPORTDIR)/flashrom-$(RELEASENAME)"
diff --git a/flash.h b/flash.h
index b383eda..1da7e41 100644
--- a/flash.h
+++ b/flash.h
@@ -138,7 +138,8 @@
#define TEST_BAD_PRE (struct tested){ .probe = BAD, .read = BAD, .erase = BAD, .write = NT }
#define TEST_BAD_PREW (struct tested){ .probe = BAD, .read = BAD, .erase = BAD, .write = BAD }
-struct flashctx;
+struct flashrom_flashctx;
+#define flashctx flashrom_flashctx /* TODO: Agree on a name and convert all occurences. */
typedef int (erasefunc_t)(struct flashctx *flash, unsigned int addr, unsigned int blocklen);
struct flashchip {
@@ -204,7 +205,7 @@
enum write_granularity gran;
};
-struct flashctx {
+struct flashrom_flashctx {
struct flashchip *chip;
/* FIXME: The memory mappings should be saved in a more structured way. */
/* The physical_* fields store the respective addresses in the physical address space of the CPU. */
@@ -218,6 +219,12 @@
struct registered_master *mst;
const struct flashrom_layout *layout;
struct single_layout fallback_layout;
+ struct {
+ bool force;
+ bool force_boardmismatch;
+ bool verify_after_write;
+ bool verify_whole_chip;
+ } flags;
};
/* Timing used in probe routines. ZERO is -2 to differentiate between an unset
diff --git a/flashrom.c b/flashrom.c
index cbaa257..cb5b412 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -409,6 +409,7 @@
#define SHUTDOWN_MAXFN 32
static int shutdown_fn_count = 0;
+/** @private */
struct shutdown_func_data {
int (*func) (void *data);
void *data;
@@ -1835,7 +1836,7 @@
* @return 0 on success,
* 1 if all available erase functions failed.
*/
-int erase_by_layout(struct flashctx *const flashctx)
+static int erase_by_layout(struct flashctx *const flashctx)
{
struct walk_info info = { 0 };
return walk_by_layout(flashctx, &info, &erase_block);
@@ -1946,8 +1947,8 @@
* @return 0 on success,
* 1 if anything has gone wrong.
*/
-int write_by_layout(struct flashctx *const flashctx,
- void *const curcontents, const void *const newcontents)
+static int write_by_layout(struct flashctx *const flashctx,
+ void *const curcontents, const void *const newcontents)
{
struct walk_info info;
info.curcontents = curcontents;
@@ -1968,8 +1969,8 @@
* 1 if reading failed,
* 3 if the contents don't match.
*/
-int verify_by_layout(struct flashctx *const flashctx,
- void *const curcontents, const uint8_t *const newcontents)
+static int verify_by_layout(struct flashctx *const flashctx,
+ void *const curcontents, const uint8_t *const newcontents)
{
const struct flashrom_layout *const layout = get_layout(flashctx);
@@ -2484,3 +2485,301 @@
free(newcontents);
return ret;
}
+
+/** @private */
+static int prepare_flash_access(struct flashctx *const flash,
+ const bool read_it, const bool write_it,
+ const bool erase_it, const bool verify_it)
+{
+ if (chip_safety_check(flash, flash->flags.force, read_it, write_it, erase_it, verify_it)) {
+ msg_cerr("Aborting.\n");
+ return 1;
+ }
+
+ if (flash->layout == get_global_layout() && normalize_romentries(flash)) {
+ msg_cerr("Requested regions can not be handled. Aborting.\n");
+ return 1;
+ }
+
+ if (map_flash(flash) != 0)
+ return 1;
+
+ /* Given the existence of read locks, we want to unlock for read,
+ erase and write. */
+ if (flash->chip->unlock)
+ flash->chip->unlock(flash);
+
+ return 0;
+}
+
+/** @private */
+static void finalize_flash_access(struct flashctx *const flash)
+{
+ unmap_flash(flash);
+}
+
+/**
+ * @addtogroup flashrom-flash
+ * @{
+ */
+
+/**
+ * @brief Erase the specified ROM chip.
+ *
+ * If a layout is set in the given flash context, only included regions
+ * will be erased.
+ *
+ * @param flashctx The context of the flash chip to erase.
+ * @return 0 on success.
+ */
+int flashrom_flash_erase(struct flashctx *const flashctx)
+{
+ if (prepare_flash_access(flashctx, false, false, true, false))
+ return 1;
+
+ const int ret = erase_by_layout(flashctx);
+
+ finalize_flash_access(flashctx);
+
+ return ret;
+}
+
+/** @} */ /* end flashrom-flash */
+
+/**
+ * @defgroup flashrom-ops Operations
+ * @{
+ */
+
+/**
+ * @brief Read the current image from the specified ROM chip.
+ *
+ * If a layout is set in the specified flash context, only included regions
+ * will be read.
+ *
+ * @param flashctx The context of the flash chip.
+ * @param buffer Target buffer to write image to.
+ * @param buffer_len Size of target buffer in bytes.
+ * @return 0 on success,
+ * 2 if buffer_len is too short for the flash chip's contents,
+ * or 1 on any other failure.
+ */
+int flashrom_image_read(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len)
+{
+ const size_t flash_size = flashctx->chip->total_size * 1024;
+
+ if (flash_size > buffer_len)
+ return 2;
+
+ if (prepare_flash_access(flashctx, true, false, false, false))
+ return 1;
+
+ msg_cinfo("Reading flash... ");
+
+ int ret = 1;
+ if (read_by_layout(flashctx, buffer)) {
+ msg_cerr("Read operation failed!\n");
+ msg_cinfo("FAILED.\n");
+ goto _finalize_ret;
+ }
+ msg_cinfo("done.\n");
+ ret = 0;
+
+_finalize_ret:
+ finalize_flash_access(flashctx);
+ return ret;
+}
+
+static void combine_image_by_layout(const struct flashctx *const flashctx,
+ uint8_t *const newcontents, const uint8_t *const oldcontents)
+{
+ const struct flashrom_layout *const layout = get_layout(flashctx);
+
+ size_t i;
+ for (i = 0; i < layout->num_entries; ++i) {
+ if (layout->entries[i].included)
+ continue;
+
+ const chipoff_t region_start = layout->entries[i].start;
+ const chipsize_t region_len = layout->entries[i].end - layout->entries[i].start + 1;
+
+ memcpy(newcontents + region_start, oldcontents + region_start, region_len);
+ }
+}
+
+/**
+ * @brief Write the specified image to the ROM chip.
+ *
+ * If a layout is set in the specified flash context, only erase blocks
+ * containing included regions will be touched.
+ *
+ * @param flashctx The context of the flash chip.
+ * @param buffer Source buffer to read image from (may be altered for full verification).
+ * @param buffer_len Size of source buffer in bytes.
+ * @return 0 on success,
+ * 4 if buffer_len doesn't match the size of the flash chip,
+ * 3 if write was tried but nothing has changed,
+ * 2 if write failed and flash contents changed,
+ * or 1 on any other failure.
+ */
+int flashrom_image_write(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len)
+{
+ const size_t flash_size = flashctx->chip->total_size * 1024;
+ const bool verify_all = flashctx->flags.verify_whole_chip;
+ const bool verify = flashctx->flags.verify_after_write;
+
+ if (buffer_len != flash_size)
+ return 4;
+
+ int ret = 1;
+
+ uint8_t *const newcontents = buffer;
+ uint8_t *const curcontents = malloc(flash_size);
+ uint8_t *oldcontents = NULL;
+ if (verify_all)
+ oldcontents = malloc(flash_size);
+ if (!curcontents || (verify_all && !oldcontents)) {
+ msg_gerr("Out of memory!\n");
+ goto _free_ret;
+ }
+
+#if CONFIG_INTERNAL == 1
+ if (programmer == PROGRAMMER_INTERNAL && cb_check_image(newcontents, flash_size) < 0) {
+ if (flashctx->flags.force_boardmismatch) {
+ msg_pinfo("Proceeding anyway because user forced us to.\n");
+ } else {
+ msg_perr("Aborting. You can override this with "
+ "-p internal:boardmismatch=force.\n");
+ goto _free_ret;
+ }
+ }
+#endif
+
+ if (prepare_flash_access(flashctx, false, true, false, verify))
+ goto _free_ret;
+
+ /*
+ * Read the whole chip to be able to check whether regions need to be
+ * erased and to give better diagnostics in case write fails.
+ * The alternative is to read only the regions which are to be
+ * preserved, but in that case we might perform unneeded erase which
+ * takes time as well.
+ */
+ msg_cinfo("Reading old flash chip contents... ");
+ if (verify_all) {
+ if (flashctx->chip->read(flashctx, oldcontents, 0, flash_size)) {
+ msg_cinfo("FAILED.\n");
+ goto _finalize_ret;
+ }
+ memcpy(curcontents, oldcontents, flash_size);
+ } else {
+ if (read_by_layout(flashctx, curcontents)) {
+ msg_cinfo("FAILED.\n");
+ goto _finalize_ret;
+ }
+ }
+ msg_cinfo("done.\n");
+
+ if (write_by_layout(flashctx, curcontents, newcontents)) {
+ msg_cerr("Uh oh. Erase/write failed. ");
+ ret = 2;
+ if (verify_all) {
+ msg_cerr("Checking if anything has changed.\n");
+ msg_cinfo("Reading current flash chip contents... ");
+ if (!flashctx->chip->read(flashctx, curcontents, 0, flash_size)) {
+ msg_cinfo("done.\n");
+ if (!memcmp(oldcontents, curcontents, flash_size)) {
+ nonfatal_help_message();
+ goto _finalize_ret;
+ }
+ msg_cerr("Apparently at least some data has changed.\n");
+ } else
+ msg_cerr("Can't even read anymore!\n");
+ emergency_help_message();
+ goto _finalize_ret;
+ } else {
+ msg_cerr("\n");
+ }
+ emergency_help_message();
+ goto _finalize_ret;
+ }
+
+ /* Verify only if we actually changed something. */
+ if (verify && !all_skipped) {
+ const struct flashrom_layout *const layout_bak = flashctx->layout;
+
+ msg_cinfo("Verifying flash... ");
+
+ /* Work around chips which need some time to calm down. */
+ programmer_delay(1000*1000);
+
+ if (verify_all) {
+ combine_image_by_layout(flashctx, newcontents, oldcontents);
+ flashctx->layout = NULL;
+ }
+ ret = verify_by_layout(flashctx, curcontents, newcontents);
+ flashctx->layout = layout_bak;
+ /* If we tried to write, and verification now fails, we
+ might have an emergency situation. */
+ if (ret)
+ emergency_help_message();
+ else
+ msg_cinfo("VERIFIED.\n");
+ } else {
+ /* We didn't change anything. */
+ ret = 0;
+ }
+
+_finalize_ret:
+ finalize_flash_access(flashctx);
+_free_ret:
+ free(oldcontents);
+ free(curcontents);
+ return ret;
+}
+
+/**
+ * @brief Verify the ROM chip's contents with the specified image.
+ *
+ * If a layout is set in the specified flash context, only included regions
+ * will be verified.
+ *
+ * @param flashctx The context of the flash chip.
+ * @param buffer Source buffer to verify with.
+ * @param buffer_len Size of source buffer in bytes.
+ * @return 0 on success,
+ * 3 if the chip's contents don't match,
+ * 2 if buffer_len doesn't match the size of the flash chip,
+ * or 1 on any other failure.
+ */
+int flashrom_image_verify(struct flashctx *const flashctx, const void *const buffer, const size_t buffer_len)
+{
+ const size_t flash_size = flashctx->chip->total_size * 1024;
+
+ if (buffer_len != flash_size)
+ return 2;
+
+ const uint8_t *const newcontents = buffer;
+ uint8_t *const curcontents = malloc(flash_size);
+ if (!curcontents) {
+ msg_gerr("Out of memory!\n");
+ return 1;
+ }
+
+ int ret = 1;
+
+ if (prepare_flash_access(flashctx, false, false, false, true))
+ goto _free_ret;
+
+ msg_cinfo("Verifying flash... ");
+ ret = verify_by_layout(flashctx, curcontents, newcontents);
+ if (!ret)
+ msg_cinfo("VERIFIED.\n");
+
+ finalize_flash_access(flashctx);
+_free_ret:
+ free(curcontents);
+ return ret;
+}
+
+/** @} */ /* end flashrom-ops */
diff --git a/libflashrom.c b/libflashrom.c
new file mode 100644
index 0000000..f17b627
--- /dev/null
+++ b/libflashrom.c
@@ -0,0 +1,334 @@
+/*
+ * This file is part of the flashrom project.
+ *
+ * Copyright (C) 2012, 2016 secunet Security Networks AG
+ * (Written by Nico Huber <nico.huber(a)secunet.com> for secunet)
+ *
+ * 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; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * 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
+ */
+/**
+ * @mainpage
+ *
+ * Have a look at the Modules section for a function reference.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdarg.h>
+
+#include "flash.h"
+#include "programmer.h"
+#include "layout.h"
+#include "libflashrom.h"
+
+/**
+ * @defgroup flashrom-general General
+ * @{
+ */
+
+/** Pointer to log callback function. */
+static flashrom_log_callback *global_log_callback = NULL;
+
+/**
+ * @brief Initialize libflashrom.
+ *
+ * @param perform_selfcheck If not zero, perform a self check.
+ * @return 0 on success
+ */
+int flashrom_init(const int perform_selfcheck)
+{
+ if (perform_selfcheck && selfcheck())
+ return 1;
+ myusec_calibrate_delay();
+ return 0;
+}
+
+/**
+ * @brief Shut down libflashrom.
+ * @return 0 on success
+ */
+int flashrom_shutdown(void)
+{
+ return 0; /* TODO: nothing to do? */
+}
+
+/* TODO: flashrom_set_loglevel()? do we need it?
+ For now, let the user decide in his callback. */
+
+/**
+ * @brief Set the log callback function.
+ *
+ * Set a callback function which will be invoked whenever libflashrom wants
+ * to output messages. This allows frontends to do whatever they see fit with
+ * such messages, e.g. write them to syslog, or to file, or print them in a
+ * GUI window, etc.
+ *
+ * @param log_callback Pointer to the new log callback function.
+ */
+void flashrom_set_log_callback(flashrom_log_callback *const log_callback)
+{
+ global_log_callback = log_callback;
+}
+/** @private */
+int print(const enum msglevel level, const char *const fmt, ...)
+{
+ if (global_log_callback) {
+ int ret;
+ va_list args;
+ va_start(args, fmt);
+ ret = global_log_callback((enum flashrom_log_level)level, fmt, args);
+ va_end(args);
+ return ret;
+ }
+ return 0;
+}
+
+/** @} */ /* end flashrom-general */
+
+
+
+/**
+ * @defgroup flashrom-query Querying
+ * @{
+ */
+
+/* TBD */
+
+/** @} */ /* end flashrom-query */
+
+
+
+/**
+ * @defgroup flashrom-prog Programmers
+ * @{
+ */
+
+/**
+ * @brief Initialize the specified programmer.
+ *
+ * Currently, only one programmer may be initialized at a time.
+ *
+ * @param[out] flashprog Points to a pointer of type struct flashrom_programmer
+ * that will be set if programmer initialization succeeds.
+ * *flashprog has to be shutdown by the caller with @ref
+ * flashrom_programmer_shutdown.
+ * @param[in] prog_name Name of the programmer to initialize.
+ * @param[in] prog_param Pointer to programmer specific parameters.
+ * @return 0 on success
+ */
+int flashrom_programmer_init(struct flashrom_programmer **const flashprog,
+ const char *const prog_name, const char *const prog_param)
+{
+ unsigned prog;
+
+ for (prog = 0; prog < PROGRAMMER_INVALID; prog++) {
+ if (strcmp(prog_name, programmer_table[prog].name) == 0)
+ break;
+ }
+ if (prog >= PROGRAMMER_INVALID) {
+ msg_ginfo("Error: Unknown programmer \"%s\". Valid choices are:\n", prog_name);
+ list_programmers_linebreak(0, 80, 0);
+ return 1;
+ }
+ return programmer_init(prog, prog_param);
+}
+
+/**
+ * @brief Shut down the initialized programmer.
+ *
+ * @param flashprog The programmer to shut down.
+ * @return 0 on success
+ */
+int flashrom_programmer_shutdown(struct flashrom_programmer *const flashprog)
+{
+ return programmer_shutdown();
+}
+
+/* TODO: flashrom_programmer_capabilities()? */
+
+/** @} */ /* end flashrom-prog */
+
+
+
+/**
+ * @defgroup flashrom-flash Flash chips
+ * @{
+ */
+
+/**
+ * @brief Probe for a flash chip.
+ *
+ * Probes for a flash chip and returns a flash context, that can be used
+ * later with flash chip and @ref flashrom-ops "image operations", if
+ * exactly one matching chip is found.
+ *
+ * @param[out] flashctx Points to a pointer of type struct flashrom_flashctx
+ * that will be set if exactly one chip is found. *flashctx
+ * has to be freed by the caller with @ref flashrom_flash_release.
+ * @param[in] flashprog The flash programmer used to access the chip.
+ * @param[in] chip_name Name of a chip to probe for, or NULL to probe for
+ * all known chips.
+ * @return 0 on success,
+ * 3 if multiple chips were found,
+ * 2 if no chip was found,
+ * or 1 on any other error.
+ */
+int flashrom_flash_probe(struct flashrom_flashctx **const flashctx,
+ const struct flashrom_programmer *const flashprog,
+ const char *const chip_name)
+{
+ int i, ret = 2;
+ struct flashrom_flashctx second_flashctx = { 0, };
+
+ chip_to_probe = chip_name; /* chip_to_probe is global in flashrom.c */
+
+ *flashctx = malloc(sizeof(**flashctx));
+ if (!*flashctx)
+ return 1;
+ memset(*flashctx, 0, sizeof(**flashctx));
+
+ for (i = 0; i < registered_master_count; ++i) {
+ int flash_idx = -1;
+ if (!ret || (flash_idx = probe_flash(®istered_masters[i], 0, *flashctx, 0)) != -1) {
+ ret = 0;
+ /* We found one chip, now check that there is no second match. */
+ if (probe_flash(®istered_masters[i], flash_idx + 1, &second_flashctx, 0) != -1) {
+ ret = 3;
+ break;
+ }
+ }
+ }
+ if (ret) {
+ free(*flashctx);
+ *flashctx = NULL;
+ }
+ return ret;
+}
+
+/**
+ * @brief Returns the size of the specified flash chip in bytes.
+ *
+ * @param flashctx The queried flash context.
+ * @return Size of flash chip in bytes.
+ */
+size_t flashrom_flash_getsize(const struct flashrom_flashctx *const flashctx)
+{
+ return flashctx->chip->total_size * 1024;
+}
+
+/**
+ * @brief Free a flash context.
+ *
+ * @param flashctx Flash context to free.
+ */
+void flashrom_flash_release(struct flashrom_flashctx *const flashctx)
+{
+ free(flashctx);
+}
+
+/**
+ * @brief Set a flag in the given flash context.
+ *
+ * @param flashctx Flash context to alter.
+ * @param flag Flag that is to be set / cleared.
+ * @param value Value to set.
+ */
+void flashrom_flag_set(struct flashrom_flashctx *const flashctx,
+ const enum flashrom_flag flag, const bool value)
+{
+ switch (flag) {
+ case FLASHROM_FLAG_FORCE: flashctx->flags.force = value; break;
+ case FLASHROM_FLAG_FORCE_BOARDMISMATCH: flashctx->flags.force_boardmismatch = value; break;
+ case FLASHROM_FLAG_VERIFY_AFTER_WRITE: flashctx->flags.verify_after_write = value; break;
+ case FLASHROM_FLAG_VERIFY_WHOLE_CHIP: flashctx->flags.verify_whole_chip = value; break;
+ }
+}
+
+/**
+ * @brief Return the current value of a flag in the given flash context.
+ *
+ * @param flashctx Flash context to read from.
+ * @param flag Flag to be read.
+ * @return Current value of the flag.
+ */
+bool flashrom_flag_get(const struct flashrom_flashctx *const flashctx, const enum flashrom_flag flag)
+{
+ switch (flag) {
+ case FLASHROM_FLAG_FORCE: return flashctx->flags.force;
+ case FLASHROM_FLAG_FORCE_BOARDMISMATCH: return flashctx->flags.force_boardmismatch;
+ case FLASHROM_FLAG_VERIFY_AFTER_WRITE: return flashctx->flags.verify_after_write;
+ case FLASHROM_FLAG_VERIFY_WHOLE_CHIP: return flashctx->flags.verify_whole_chip;
+ default: return false;
+ }
+}
+
+/** @} */ /* end flashrom-flash */
+
+
+
+/**
+ * @defgroup flashrom-layout Layout handling
+ * @{
+ */
+
+/**
+ * @brief Mark given region as included.
+ *
+ * @param layout The layout to alter.
+ * @param name The name of the region to include.
+ *
+ * @return 0 on success,
+ * 1 if the given name can't be found.
+ */
+int flashrom_layout_include_region(struct flashrom_layout *const layout, const char *name)
+{
+ size_t i;
+ for (i = 0; i < layout->num_entries; ++i) {
+ if (!strcmp(layout->entries[i].name, name)) {
+ layout->entries[i].included = true;
+ return 0;
+ }
+ }
+ return 1;
+}
+
+/**
+ * @brief Free a layout.
+ *
+ * @param layout Layout to free.
+ */
+void flashrom_layout_release(struct flashrom_layout *const layout)
+{
+ if (layout == get_global_layout())
+ return;
+
+ free(layout);
+}
+
+/**
+ * @brief Set the active layout for a flash context.
+ *
+ * Note: This just sets a pointer. The caller must not release the layout
+ * as long as he uses it through the given flash context.
+ *
+ * @param flashctx Flash context whose layout will be set.
+ * @param layout Layout to bet set.
+ */
+void flashrom_layout_set(struct flashrom_flashctx *const flashctx, const struct flashrom_layout *const layout)
+{
+ flashctx->layout = layout;
+}
+
+/** @} */ /* end flashrom-layout */
diff --git a/libflashrom.h b/libflashrom.h
new file mode 100644
index 0000000..9fb5a47
--- /dev/null
+++ b/libflashrom.h
@@ -0,0 +1,71 @@
+/*
+ * This file is part of the flashrom project.
+ *
+ * Copyright (C) 2012 secunet Security Networks AG
+ * (Written by Nico Huber <nico.huber(a)secunet.com> for secunet)
+ *
+ * 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; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * 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 __LIBFLASHROM_H__
+#define __LIBFLASHROM_H__ 1
+
+#include <stdarg.h>
+
+int flashrom_init(int perform_selfcheck);
+int flashrom_shutdown(void);
+/** @ingroup flashrom-general */
+enum flashrom_log_level { /* This has to match enum msglevel. */
+ FLASHROM_MSG_ERROR = 0,
+ FLASHROM_MSG_WARN = 1,
+ FLASHROM_MSG_INFO = 2,
+ FLASHROM_MSG_DEBUG = 3,
+ FLASHROM_MSG_DEBUG2 = 4,
+ FLASHROM_MSG_SPEW = 5,
+};
+/** @ingroup flashrom-general */
+typedef int(flashrom_log_callback)(enum flashrom_log_level, const char *format, va_list);
+void flashrom_set_log_callback(flashrom_log_callback *);
+
+struct flashrom_programmer;
+int flashrom_programmer_init(struct flashrom_programmer **, const char *prog_name, const char *prog_params);
+int flashrom_programmer_shutdown(struct flashrom_programmer *);
+
+struct flashrom_flashctx;
+int flashrom_flash_probe(struct flashrom_flashctx **, const struct flashrom_programmer *, const char *chip_name);
+size_t flashrom_flash_getsize(const struct flashrom_flashctx *);
+int flashrom_flash_erase(struct flashrom_flashctx *);
+void flashrom_flash_release(struct flashrom_flashctx *);
+
+/** @ingroup flashrom-flash */
+enum flashrom_flag {
+ FLASHROM_FLAG_FORCE,
+ FLASHROM_FLAG_FORCE_BOARDMISMATCH,
+ FLASHROM_FLAG_VERIFY_AFTER_WRITE,
+ FLASHROM_FLAG_VERIFY_WHOLE_CHIP,
+};
+void flashrom_flag_set(struct flashrom_flashctx *, enum flashrom_flag, bool value);
+bool flashrom_flag_get(const struct flashrom_flashctx *, enum flashrom_flag);
+
+int flashrom_image_read(struct flashrom_flashctx *, void *buffer, size_t buffer_len);
+int flashrom_image_write(struct flashrom_flashctx *, void *buffer, size_t buffer_len);
+int flashrom_image_verify(struct flashrom_flashctx *, const void *buffer, size_t buffer_len);
+
+struct flashrom_layout;
+int flashrom_layout_include_region(struct flashrom_layout *, const char *name);
+void flashrom_layout_release(struct flashrom_layout *);
+void flashrom_layout_set(struct flashrom_flashctx *, const struct flashrom_layout *);
+
+#endif /* !__LIBFLASHROM_H__ */
--
To view, visit https://review.coreboot.org/21788
To unsubscribe, visit https://review.coreboot.org/settings
Gerrit-Project: flashrom
Gerrit-Branch: stable
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ibe14c89c96e45ed7679332b0f5cf44b1c9f1d9ee
Gerrit-Change-Number: 21788
Gerrit-PatchSet: 1
Gerrit-Owner: Nico Huber <nico.h(a)gmx.de>
Nico Huber has uploaded this change for review. ( https://review.coreboot.org/21787
Change subject: Add functions to read/erase/write/verify by layout
......................................................................
Add functions to read/erase/write/verify by layout
Inspired by Lynxis' related work, this implements a foundation for
layout based flash access.
All operations iterate over the given layout regions. Erase and write
then walk, per region, over all erase blocks in an inner loop (which
might not be what we want, see note on optimization below). Special care
has been taken that flash content is merged properly, in case an erase
block is only partially covered by a layout region or even affects mul-
tiple regions.
A note on performance: In the case an erase block affects multiple
regions, it will probably be read, erased and written for each region.
Another approach would be to walk all erase blocks once and check for
each erase block which regions it touches (i.e. for each erase block,
merge data pontentially from the flash and all layout regions, then
flash the combined data). That might result in cleaner code. I haven't
tried it yet, though.
Original-Change-Id: Ic6194cea4c4c430e0cf9d586052508a865b09c86
Original-Reviewed-on: https://review.coreboot.org/17945
Original-Tested-by: build bot (Jenkins) <no-reply(a)coreboot.org>
Original-Reviewed-by: David Hendricks <david.hendricks(a)gmail.com>
Change-Id: Id7d0397f29027ff16f2d5bf25811fd03d371a16f
Signed-off-by: Nico Huber <nico.huber(a)secunet.com>
---
M flash.h
M flashrom.c
2 files changed, 352 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/87/21787/1
diff --git a/flash.h b/flash.h
index bf381cf..b383eda 100644
--- a/flash.h
+++ b/flash.h
@@ -216,6 +216,8 @@
uintptr_t physical_registers;
chipaddr virtual_registers;
struct registered_master *mst;
+ const struct flashrom_layout *layout;
+ struct single_layout fallback_layout;
};
/* Timing used in probe routines. ZERO is -2 to differentiate between an unset
diff --git a/flashrom.c b/flashrom.c
index 1a43303..cbaa257 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -5,6 +5,8 @@
* Copyright (C) 2004 Tyan Corp <yhlu(a)tyan.com>
* Copyright (C) 2005-2008 coresystems GmbH
* Copyright (C) 2008,2009 Carl-Daniel Hailfinger
+ * Copyright (C) 2016 secunet Security Networks AG
+ * (Written by Nico Huber <nico.huber(a)secunet.com> for secunet)
*
* 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
@@ -1245,6 +1247,14 @@
if (!flash->chip)
return -1;
+ /* Fill fallback layout covering the whole chip. */
+ struct single_layout *const fallback = &flash->fallback_layout;
+ fallback->base.entries = &fallback->entry;
+ fallback->base.num_entries = 1;
+ fallback->entry.start = 0;
+ fallback->entry.end = flash->chip->total_size * 1024 - 1;
+ fallback->entry.included = true;
+ strcpy(fallback->entry.name, "complete flash");
tmp = flashbuses_to_text(flash->chip->bustype);
msg_cinfo("%s %s flash chip \"%s\" (%d kB, %s) ", force ? "Assuming" : "Found",
@@ -1640,6 +1650,346 @@
return ret;
}
+static const struct flashrom_layout *get_layout(const struct flashctx *const flashctx)
+{
+ if (flashctx->layout && flashctx->layout->num_entries)
+ return flashctx->layout;
+ else
+ return &flashctx->fallback_layout.base;
+}
+
+/**
+ * @brief Reads the included layout regions into a buffer.
+ *
+ * If there is no layout set in the given flash context, the whole chip will
+ * be read.
+ *
+ * @param flashctx Flash context to be used.
+ * @param buffer Buffer of full chip size to read into.
+ * @return 0 on success,
+ * 1 if any read fails.
+ */
+static int read_by_layout(struct flashctx *const flashctx, uint8_t *const buffer)
+{
+ const struct flashrom_layout *const layout = get_layout(flashctx);
+
+ size_t i;
+ for (i = 0; i < layout->num_entries; ++i) {
+ if (!layout->entries[i].included)
+ continue;
+
+ const chipoff_t region_start = layout->entries[i].start;
+ const chipsize_t region_len = layout->entries[i].end - layout->entries[i].start + 1;
+
+ if (flashctx->chip->read(flashctx, buffer + region_start, region_start, region_len))
+ return 1;
+ }
+ return 0;
+}
+
+typedef int (*erasefn_t)(struct flashctx *, unsigned int addr, unsigned int len);
+/**
+ * @private
+ *
+ * For read-erase-write, `curcontents` and `newcontents` shall point
+ * to buffers of the chip's size. Both are supposed to be prefilled
+ * with at least the included layout regions of the current flash
+ * contents (`curcontents`) and the data to be written to the flash
+ * (`newcontents`).
+ *
+ * For erase, `curcontents` and `newcontents` shall be NULL-pointers.
+ *
+ * The `chipoff_t` values are used internally by `walk_by_layout()`.
+ */
+struct walk_info {
+ uint8_t *curcontents;
+ const uint8_t *newcontents;
+ chipoff_t region_start;
+ chipoff_t region_end;
+ chipoff_t erase_start;
+ chipoff_t erase_end;
+};
+/* returns 0 on success, 1 to retry with another erase function, 2 for immediate abort */
+typedef int (*per_blockfn_t)(struct flashctx *, const struct walk_info *, erasefn_t);
+
+static int walk_eraseblocks(struct flashctx *const flashctx,
+ struct walk_info *const info,
+ const size_t erasefunction, const per_blockfn_t per_blockfn)
+{
+ int ret;
+ size_t i, j;
+ bool first = true;
+ struct block_eraser *const eraser = &flashctx->chip->block_erasers[erasefunction];
+
+ info->erase_start = 0;
+ for (i = 0; i < NUM_ERASEREGIONS; ++i) {
+ /* count==0 for all automatically initialized array
+ members so the loop below won't be executed for them. */
+ for (j = 0; j < eraser->eraseblocks[i].count; ++j, info->erase_start = info->erase_end + 1) {
+ info->erase_end = info->erase_start + eraser->eraseblocks[i].size - 1;
+
+ /* Skip any eraseblock that is completely outside the current region. */
+ if (info->erase_end < info->region_start)
+ continue;
+ if (info->region_end < info->erase_start)
+ break;
+
+ /* Print this for every block except the first one. */
+ if (first)
+ first = false;
+ else
+ msg_cdbg(", ");
+ msg_cdbg("0x%06x-0x%06x:", info->erase_start, info->erase_end);
+
+ ret = per_blockfn(flashctx, info, eraser->block_erase);
+ if (ret)
+ return ret;
+ }
+ if (info->region_end < info->erase_start)
+ break;
+ }
+ msg_cdbg("\n");
+ return 0;
+}
+
+static int walk_by_layout(struct flashctx *const flashctx, struct walk_info *const info,
+ const per_blockfn_t per_blockfn)
+{
+ const struct flashrom_layout *const layout = get_layout(flashctx);
+
+ all_skipped = true;
+ msg_cinfo("Erasing and writing flash chip... ");
+
+ size_t i;
+ for (i = 0; i < layout->num_entries; ++i) {
+ if (!layout->entries[i].included)
+ continue;
+
+ info->region_start = layout->entries[i].start;
+ info->region_end = layout->entries[i].end;
+
+ size_t j;
+ int error = 1; /* retry as long as it's 1 */
+ for (j = 0; j < NUM_ERASEFUNCTIONS; ++j) {
+ if (j != 0)
+ msg_cinfo("Looking for another erase function.\n");
+ msg_cdbg("Trying erase function %zi... ", j);
+ if (check_block_eraser(flashctx, j, 1))
+ continue;
+
+ error = walk_eraseblocks(flashctx, info, j, per_blockfn);
+ if (error != 1)
+ break;
+
+ if (info->curcontents) {
+ msg_cinfo("Reading current flash chip contents... ");
+ if (read_by_layout(flashctx, info->curcontents)) {
+ /* Now we are truly screwed. Read failed as well. */
+ msg_cerr("Can't read anymore! Aborting.\n");
+ /* We have no idea about the flash chip contents, so
+ retrying with another erase function is pointless. */
+ error = 2;
+ break;
+ }
+ msg_cinfo("done. ");
+ }
+ }
+ if (error == 1)
+ msg_cinfo("No usable erase functions left.\n");
+ if (error) {
+ msg_cerr("FAILED!\n");
+ return 1;
+ }
+ }
+ if (all_skipped)
+ msg_cinfo("\nWarning: Chip content is identical to the requested image.\n");
+ msg_cinfo("Erase/write done.\n");
+ return 0;
+}
+
+static int erase_block(struct flashctx *const flashctx,
+ const struct walk_info *const info, const erasefn_t erasefn)
+{
+ const unsigned int erase_len = info->erase_end + 1 - info->erase_start;
+
+ all_skipped = false;
+
+ msg_cdbg("E");
+ if (erasefn(flashctx, info->erase_start, erase_len))
+ return 1;
+ if (check_erased_range(flashctx, info->erase_start, erase_len)) {
+ msg_cerr("ERASE FAILED!\n");
+ return 1;
+ }
+ return 0;
+}
+
+/**
+ * @brief Erases the included layout regions.
+ *
+ * If there is no layout set in the given flash context, the whole chip will
+ * be erased.
+ *
+ * @param flashctx Flash context to be used.
+ * @param buffer Buffer of full chip size to read into.
+ * @return 0 on success,
+ * 1 if all available erase functions failed.
+ */
+int erase_by_layout(struct flashctx *const flashctx)
+{
+ struct walk_info info = { 0 };
+ return walk_by_layout(flashctx, &info, &erase_block);
+}
+
+static int read_erase_write_block(struct flashctx *const flashctx,
+ const struct walk_info *const info, const erasefn_t erasefn)
+{
+ const chipsize_t erase_len = info->erase_end + 1 - info->erase_start;
+ const bool region_unaligned = info->region_start > info->erase_start ||
+ info->erase_end > info->region_end;
+ const uint8_t *newcontents = NULL;
+ int ret = 2;
+
+ /*
+ * If the region is not erase-block aligned, merge current flash con-
+ * tents into `info->curcontents` and a new buffer `newc`. The former
+ * is necessary since we have no guarantee that the full erase block
+ * was already read into `info->curcontents`. For the latter a new
+ * buffer is used since `info->newcontents` might contain data for
+ * other unaligned regions that touch this erase block too.
+ */
+ if (region_unaligned) {
+ msg_cdbg("R");
+ uint8_t *const newc = malloc(erase_len);
+ if (!newc) {
+ msg_cerr("Out of memory!\n");
+ return 1;
+ }
+ memcpy(newc, info->newcontents + info->erase_start, erase_len);
+
+ /* Merge data preceding the current region. */
+ if (info->region_start > info->erase_start) {
+ const chipoff_t start = info->erase_start;
+ const chipsize_t len = info->region_start - info->erase_start;
+ if (flashctx->chip->read(flashctx, newc, start, len)) {
+ msg_cerr("Can't read! Aborting.\n");
+ goto _free_ret;
+ }
+ memcpy(info->curcontents + start, newc, len);
+ }
+ /* Merge data following the current region. */
+ if (info->erase_end > info->region_end) {
+ const chipoff_t start = info->region_end + 1;
+ const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
+ const chipsize_t len = info->erase_end - info->region_end;
+ if (flashctx->chip->read(flashctx, newc + rel_start, start, len)) {
+ msg_cerr("Can't read! Aborting.\n");
+ goto _free_ret;
+ }
+ memcpy(info->curcontents + start, newc + rel_start, len);
+ }
+
+ newcontents = newc;
+ } else {
+ newcontents = info->newcontents + info->erase_start;
+ }
+
+ ret = 1;
+ bool skipped = true;
+ uint8_t *const curcontents = info->curcontents + info->erase_start;
+ if (need_erase(curcontents, newcontents, erase_len, flashctx->chip->gran)) {
+ if (erase_block(flashctx, info, erasefn))
+ goto _free_ret;
+ /* Erase was successful. Adjust curcontents. */
+ memset(curcontents, 0xff, erase_len);
+ skipped = false;
+ }
+
+ unsigned int starthere = 0, lenhere = 0, writecount = 0;
+ /* get_next_write() sets starthere to a new value after the call. */
+ while ((lenhere = get_next_write(curcontents + starthere, newcontents + starthere,
+ erase_len - starthere, &starthere, flashctx->chip->gran))) {
+ if (!writecount++)
+ msg_cdbg("W");
+ /* Needs the partial write function signature. */
+ if (flashctx->chip->write(flashctx, newcontents + starthere,
+ info->erase_start + starthere, lenhere))
+ goto _free_ret;
+ starthere += lenhere;
+ skipped = false;
+ }
+ if (skipped)
+ msg_cdbg("S");
+ else
+ all_skipped = false;
+
+ /* Update curcontents, other regions with overlapping erase blocks
+ might rely on this. */
+ memcpy(curcontents, newcontents, erase_len);
+ ret = 0;
+
+_free_ret:
+ if (region_unaligned)
+ free((void *)newcontents);
+ return ret;
+}
+
+/**
+ * @brief Writes the included layout regions from a given image.
+ *
+ * If there is no layout set in the given flash context, the whole image
+ * will be written.
+ *
+ * @param flashctx Flash context to be used.
+ * @param curcontents A buffer of full chip size with current chip contents of included regions.
+ * @param newcontents The new image to be written.
+ * @return 0 on success,
+ * 1 if anything has gone wrong.
+ */
+int write_by_layout(struct flashctx *const flashctx,
+ void *const curcontents, const void *const newcontents)
+{
+ struct walk_info info;
+ info.curcontents = curcontents;
+ info.newcontents = newcontents;
+ return walk_by_layout(flashctx, &info, read_erase_write_block);
+}
+
+/**
+ * @brief Compares the included layout regions with content from a buffer.
+ *
+ * If there is no layout set in the given flash context, the whole chip's
+ * contents will be compared.
+ *
+ * @param flashctx Flash context to be used.
+ * @param curcontents A buffer of full chip size to read current chip contents into.
+ * @param newcontents The new image to compare to.
+ * @return 0 on success,
+ * 1 if reading failed,
+ * 3 if the contents don't match.
+ */
+int verify_by_layout(struct flashctx *const flashctx,
+ void *const curcontents, const uint8_t *const newcontents)
+{
+ const struct flashrom_layout *const layout = get_layout(flashctx);
+
+ size_t i;
+ for (i = 0; i < layout->num_entries; ++i) {
+ if (!layout->entries[i].included)
+ continue;
+
+ const chipoff_t region_start = layout->entries[i].start;
+ const chipsize_t region_len = layout->entries[i].end - layout->entries[i].start + 1;
+
+ if (flashctx->chip->read(flashctx, curcontents + region_start, region_start, region_len))
+ return 1;
+ if (compare_range(newcontents + region_start, curcontents + region_start,
+ region_start, region_len))
+ return 3;
+ }
+ return 0;
+}
+
static void nonfatal_help_message(void)
{
msg_gerr("Good, writing to the flash chip apparently didn't do anything.\n");
--
To view, visit https://review.coreboot.org/21787
To unsubscribe, visit https://review.coreboot.org/settings
Gerrit-Project: flashrom
Gerrit-Branch: stable
Gerrit-MessageType: newchange
Gerrit-Change-Id: Id7d0397f29027ff16f2d5bf25811fd03d371a16f
Gerrit-Change-Number: 21787
Gerrit-PatchSet: 1
Gerrit-Owner: Nico Huber <nico.h(a)gmx.de>
Nico Huber has uploaded this change for review. ( https://review.coreboot.org/21786
Change subject: Give layouts their own type
......................................................................
Give layouts their own type
Introduce `struct flashrom_layout` and refactor layout.c a little, so
we can reuse the layout from there and have other sources of layouts
beside it.
I didn't want to clutter up flash.h any more. So things went into a new
layout.h.
Original-Change-Id: Icea1a58c283131cc9c5fde6f16d783538dc1a4c7
Original-Reviewed-on: https://review.coreboot.org/17944
Original-Tested-by: build bot (Jenkins) <no-reply(a)coreboot.org>
Original-Reviewed-by: David Hendricks <david.hendricks(a)gmail.com>
Original-Reviewed-by: Philippe Mathieu-Daudé <philippe.mathieu.daude(a)gmail.com>
Change-Id: Ia8eb8f05a532a3cf4d1fb921d5629678dea67375
Signed-off-by: Nico Huber <nico.huber(a)secunet.com>
---
M flash.h
M layout.c
A layout.h
3 files changed, 102 insertions(+), 50 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/86/21786/1
diff --git a/flash.h b/flash.h
index da049d1..bf381cf 100644
--- a/flash.h
+++ b/flash.h
@@ -37,6 +37,8 @@
#undef max
#endif
+#include "layout.h"
+
#define ERROR_PTR ((void*)-1)
/* Error codes */
@@ -46,14 +48,6 @@
/* TODO: check using code for correct usage of types */
typedef uintptr_t chipaddr;
#define PRIxPTR_WIDTH ((int)(sizeof(uintptr_t)*2))
-
-/* Types and macros regarding the maximum flash space size supported by generic code. */
-typedef uint32_t chipoff_t; /* Able to store any addressable offset within a supported flash memory. */
-typedef uint32_t chipsize_t; /* Able to store the number of bytes of any supported flash memory. */
-#define FL_MAX_CHIPOFF_BITS (24)
-#define FL_MAX_CHIPOFF ((chipoff_t)(1ULL<<FL_MAX_CHIPOFF_BITS)-1)
-#define PRIxCHIPOFF "06"PRIx32
-#define PRIuCHIPSIZE PRIu32
int register_shutdown(int (*function) (void *data), void *data);
int shutdown_free(void *data);
diff --git a/layout.c b/layout.c
index f71eeaa..3d09011 100644
--- a/layout.c
+++ b/layout.c
@@ -25,24 +25,20 @@
#include <limits.h>
#include "flash.h"
#include "programmer.h"
+#include "layout.h"
-#define MAX_ROMLAYOUT 32
-
-typedef struct {
- chipoff_t start;
- chipoff_t end;
- unsigned int included;
- char name[256];
-} romentry_t;
-
-/* rom_entries store the entries specified in a layout file and associated run-time data */
-static romentry_t rom_entries[MAX_ROMLAYOUT];
-static int num_rom_entries = 0; /* the number of successfully parsed rom_entries */
+struct romentry entries[MAX_ROMLAYOUT];
+static struct flashrom_layout layout = { entries, 0 };
/* include_args holds the arguments specified at the command line with -i. They must be processed at some point
- * so that desired regions are marked as "included" in the rom_entries list. */
+ * so that desired regions are marked as "included" in the layout. */
static char *include_args[MAX_ROMLAYOUT];
static int num_include_args = 0; /* the number of valid include_args. */
+
+const struct flashrom_layout *get_global_layout(void)
+{
+ return &layout;
+}
#ifndef __LIBPAYLOAD__
int read_romlayout(const char *name)
@@ -62,13 +58,13 @@
while (!feof(romlayout)) {
char *tstr1, *tstr2;
- if (num_rom_entries >= MAX_ROMLAYOUT) {
+ if (layout.num_entries >= MAX_ROMLAYOUT) {
msg_gerr("Maximum number of ROM images (%i) in layout "
"file reached.\n", MAX_ROMLAYOUT);
(void)fclose(romlayout);
return 1;
}
- if (2 != fscanf(romlayout, "%255s %255s\n", tempstr, rom_entries[num_rom_entries].name))
+ if (2 != fscanf(romlayout, "%255s %255s\n", tempstr, layout.entries[layout.num_entries].name))
continue;
#if 0
// fscanf does not like arbitrary comments like that :( later
@@ -83,16 +79,16 @@
(void)fclose(romlayout);
return 1;
}
- rom_entries[num_rom_entries].start = strtol(tstr1, (char **)NULL, 16);
- rom_entries[num_rom_entries].end = strtol(tstr2, (char **)NULL, 16);
- rom_entries[num_rom_entries].included = 0;
- num_rom_entries++;
+ layout.entries[layout.num_entries].start = strtol(tstr1, (char **)NULL, 16);
+ layout.entries[layout.num_entries].end = strtol(tstr2, (char **)NULL, 16);
+ layout.entries[layout.num_entries].included = 0;
+ layout.num_entries++;
}
- for (i = 0; i < num_rom_entries; i++) {
+ for (i = 0; i < layout.num_entries; i++) {
msg_gdbg("romlayout %08x - %08x named %s\n",
- rom_entries[i].start,
- rom_entries[i].end, rom_entries[i].name);
+ layout.entries[i].start,
+ layout.entries[i].end, layout.entries[i].name);
}
(void)fclose(romlayout);
@@ -140,13 +136,13 @@
{
int i;
- if (num_rom_entries == 0)
+ if (layout.num_entries == 0)
return -1;
msg_gspew("Looking for region \"%s\"... ", name);
- for (i = 0; i < num_rom_entries; i++) {
- if (!strcmp(rom_entries[i].name, name)) {
- rom_entries[i].included = 1;
+ for (i = 0; i < layout.num_entries; i++) {
+ if (!strcmp(layout.entries[i].name, name)) {
+ layout.entries[i].included = 1;
msg_gspew("found.\n");
return i;
}
@@ -167,7 +163,7 @@
return 0;
/* User has specified an area, but no layout file is loaded. */
- if (num_rom_entries == 0) {
+ if (layout.num_entries == 0) {
msg_gerr("Region requested (with -i \"%s\"), "
"but no layout data is available.\n",
include_args[0]);
@@ -200,22 +196,22 @@
}
num_include_args = 0;
- for (i = 0; i < num_rom_entries; i++) {
- rom_entries[i].included = 0;
+ for (i = 0; i < layout.num_entries; i++) {
+ layout.entries[i].included = 0;
}
- num_rom_entries = 0;
+ layout.num_entries = 0;
}
-romentry_t *get_next_included_romentry(unsigned int start)
+struct romentry *get_next_included_romentry(unsigned int start)
{
int i;
unsigned int best_start = UINT_MAX;
- romentry_t *best_entry = NULL;
- romentry_t *cur;
+ struct romentry *best_entry = NULL;
+ struct romentry *cur;
/* First come, first serve for overlapping regions. */
- for (i = 0; i < num_rom_entries; i++) {
- cur = &rom_entries[i];
+ for (i = 0; i < layout.num_entries; i++) {
+ cur = &layout.entries[i];
if (!cur->included)
continue;
/* Already past the current entry? */
@@ -240,16 +236,16 @@
int ret = 0;
int i;
- for (i = 0; i < num_rom_entries; i++) {
- if (rom_entries[i].start >= total_size || rom_entries[i].end >= total_size) {
+ for (i = 0; i < layout.num_entries; i++) {
+ if (layout.entries[i].start >= total_size || layout.entries[i].end >= total_size) {
msg_gwarn("Warning: Address range of region \"%s\" exceeds the current chip's "
- "address space.\n", rom_entries[i].name);
- if (rom_entries[i].included)
+ "address space.\n", layout.entries[i].name);
+ if (layout.entries[i].included)
ret = 1;
}
- if (rom_entries[i].start > rom_entries[i].end) {
+ if (layout.entries[i].start > layout.entries[i].end) {
msg_gerr("Error: Size of the address range of region \"%s\" is not positive.\n",
- rom_entries[i].name);
+ layout.entries[i].name);
ret = 1;
}
}
@@ -281,7 +277,7 @@
int build_new_image(struct flashctx *flash, bool oldcontents_valid, uint8_t *oldcontents, uint8_t *newcontents)
{
unsigned int start = 0;
- romentry_t *entry;
+ struct romentry *entry;
unsigned int size = flash->chip->total_size * 1024;
/* If no regions were specified for inclusion, assume
diff --git a/layout.h b/layout.h
new file mode 100644
index 0000000..349cebc
--- /dev/null
+++ b/layout.h
@@ -0,0 +1,62 @@
+/*
+ * This file is part of the flashrom project.
+ *
+ * Copyright (C) 2005-2008 coresystems GmbH
+ * (Written by Stefan Reinauer <stepan(a)coresystems.de> for coresystems GmbH)
+ * Copyright (C) 2011-2013 Stefan Tauner
+ * Copyright (C) 2016 secunet Security Networks AG
+ * (Written by Nico Huber <nico.huber(a)secunet.com> for secunet)
+ *
+ * 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; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * 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 __LAYOUT_H__
+#define __LAYOUT_H__ 1
+
+#include <stddef.h>
+#include <stdint.h>
+
+/* Types and macros regarding the maximum flash space size supported by generic code. */
+typedef uint32_t chipoff_t; /* Able to store any addressable offset within a supported flash memory. */
+typedef uint32_t chipsize_t; /* Able to store the number of bytes of any supported flash memory. */
+#define FL_MAX_CHIPOFF_BITS (24)
+#define FL_MAX_CHIPOFF ((chipoff_t)(1ULL<<FL_MAX_CHIPOFF_BITS)-1)
+#define PRIxCHIPOFF "06"PRIx32
+#define PRIuCHIPSIZE PRIu32
+
+#define MAX_ROMLAYOUT 32
+
+struct romentry {
+ chipoff_t start;
+ chipoff_t end;
+ bool included;
+ char name[256];
+};
+
+struct flashrom_layout {
+ /* entries store the entries specified in a layout file and associated run-time data */
+ struct romentry *entries;
+ /* the number of successfully parsed entries */
+ size_t num_entries;
+};
+
+struct single_layout {
+ struct flashrom_layout base;
+ struct romentry entry;
+};
+
+const struct flashrom_layout *get_global_layout(void);
+
+#endif /* !__LAYOUT_H__ */
--
To view, visit https://review.coreboot.org/21786
To unsubscribe, visit https://review.coreboot.org/settings
Gerrit-Project: flashrom
Gerrit-Branch: stable
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia8eb8f05a532a3cf4d1fb921d5629678dea67375
Gerrit-Change-Number: 21786
Gerrit-PatchSet: 1
Gerrit-Owner: Nico Huber <nico.h(a)gmx.de>
Nico Huber has uploaded this change for review. ( https://review.coreboot.org/21781
Change subject: dediprog: Fix bug where too many transfers would be queued
......................................................................
dediprog: Fix bug where too many transfers would be queued
We didn't check the total number of queued transfers in the inner most
loop. Up to DEDIPROG_ASYNC_TRANSFERS - 1 invalid transfers could be
queued therefore. So add another check on the total number.
Original-Change-Id: I91a8de47db7107455f5fc63ab2f13a0bd50c5b63
Original-Acked-by: David Hendricks <david.hendricks(a)gmail.com>
Original-Reviewed-on: https://review.coreboot.org/19351
Original-Tested-by: build bot (Jenkins)
Original-Reviewed-by: Nico Huber <nico.h(a)gmx.de>
Change-Id: Ie0d516f0fb2923a772a0ca7020ca5118ab260dc5
Signed-off-by: Nico Huber <nico.huber(a)secunet.com>
---
M dediprog.c
1 file changed, 3 insertions(+), 1 deletion(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/81/21781/1
diff --git a/dediprog.c b/dediprog.c
index b7276e5..6f82772 100644
--- a/dediprog.c
+++ b/dediprog.c
@@ -462,7 +462,9 @@
/* Now transfer requested chunks using libusb's asynchronous interface. */
while (!status.error && (status.queued_idx < count)) {
- while ((status.queued_idx - status.finished_idx) < DEDIPROG_ASYNC_TRANSFERS) {
+ while ((status.queued_idx < count) &&
+ (status.queued_idx - status.finished_idx) < DEDIPROG_ASYNC_TRANSFERS)
+ {
transfer = transfers[status.queued_idx % DEDIPROG_ASYNC_TRANSFERS];
libusb_fill_bulk_transfer(transfer, dediprog_handle, 0x80 | dediprog_in_endpoint,
(unsigned char *)buf + status.queued_idx * chunksize, chunksize,
--
To view, visit https://review.coreboot.org/21781
To unsubscribe, visit https://review.coreboot.org/settings
Gerrit-Project: flashrom
Gerrit-Branch: stable
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie0d516f0fb2923a772a0ca7020ca5118ab260dc5
Gerrit-Change-Number: 21781
Gerrit-PatchSet: 1
Gerrit-Owner: Nico Huber <nico.h(a)gmx.de>