summaryrefslogtreecommitdiffstats
path: root/libflashrom.c
diff options
context:
space:
mode:
authorNico Huber <nico.huber@secunet.com>2012-12-10 13:34:10 +0000
committerDavid Hendricks <david.hendricks@gmail.com>2017-06-03 20:13:06 +0200
commit454f61338213f73ca74fda54c0bf86afb01947de (patch)
tree5c981a1a181c130467d3c37b99cdeaf686ff49c8 /libflashrom.c
parent7af0e79b44bdc86497a992a90855f284e74d73f1 (diff)
downloadflashrom-454f61338213f73ca74fda54c0bf86afb01947de.tar.gz
flashrom-454f61338213f73ca74fda54c0bf86afb01947de.tar.bz2
flashrom-454f61338213f73ca74fda54c0bf86afb01947de.zip
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. Change-Id: I00f169990830aa17b7dfae5eb74010d40c476181 Signed-off-by: Nico Huber <nico.huber@secunet.com> Reviewed-on: https://review.coreboot.org/17946 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: David Hendricks <david.hendricks@gmail.com>
Diffstat (limited to 'libflashrom.c')
-rw-r--r--libflashrom.c334
1 files changed, 334 insertions, 0 deletions
diff --git a/libflashrom.c b/libflashrom.c
new file mode 100644
index 000000000..5447bae9e
--- /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@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(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(&registered_masters[i], 0, *flashctx, 0)) != -1) {
+ ret = 0;
+ /* We found one chip, now check that there is no second match. */
+ if (probe_flash(&registered_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 */