Edward O'Callaghan has uploaded this change for review.

View Change

programmer: Introduce programmer alias mechanism

Originally introduced in the downstream commit
ba0827acee4da6740133345f43aae386923bbaad .

This was originally authored by David Hendricks so that
the various flash peripheral in ChromeOS devices can be
flashed. For example,
flashrom -p ec -r ec.bin
flashrom -p host -r host.bin

Note that this requires,
`CONFIG_DEFAULT_PROGRAMMER=PROGRAMMER_INTERNAL make`
to be defined as build-time.

BUG=b:148755493
BRANCH=none
TEST=ran ./flashrom -p host --flash-name

Change-Id: I73766451cd8900a9e0fe08efc7a4d81b2a35ac8d
Signed-off-by: Edward O'Callaghan <quasisec@google.com>
---
M cli_classic.c
M it85spi.c
M linux_mtd.c
M linux_spi.c
M programmer.c
M programmer.h
6 files changed, 98 insertions(+), 1 deletion(-)

git pull ssh://review.coreboot.org:29418/flashrom refs/changes/71/38671/1
diff --git a/cli_classic.c b/cli_classic.c
index 73cc417..d58562b 100644
--- a/cli_classic.c
+++ b/cli_classic.c
@@ -329,13 +329,51 @@
break;
}
}
- if (prog == PROGRAMMER_INVALID) {
+
+ for (i = 0; aliases[i].name; i++) {
+ name = aliases[i].name;
+ namelen = strlen(aliases[i].name);
+
+ if (strncmp(optarg, name, namelen))
+ continue;
+
+ switch (optarg[namelen]) {
+ case ':':
+ pparam = strdup(optarg + namelen + 1);
+ if (!strlen(pparam)) {
+ free(pparam);
+ pparam = NULL;
+ }
+ break;
+ case '\0':
+ break;
+ default:
+ /* The continue refers to the for-loop.
+ * It is here to be able to
+ * differentiate between foo and foobar.
+ */
+ continue;
+ }
+
+ alias = &aliases[i];
+ msg_gdbg("Programmer alias: \"%s\", parameter: "
+ " \"%s\",\n", alias->name, pparam);
+ break;
+ }
+
+ if ((prog == PROGRAMMER_INVALID) && !alias) {
fprintf(stderr, "Error: Unknown programmer \"%s\". Valid choices are:\n",
optarg);
list_programmers_linebreak(0, 80, 0);
msg_ginfo(".\n");
cli_classic_abort_usage(NULL);
}
+
+ if ((prog != PROGRAMMER_INVALID) && alias) {
+ fprintf(stderr, "Error: Alias cannot be used "
+ "with programmer name.\n");
+ cli_classic_abort_usage(NULL);
+ }
break;
case 'R':
/* print_version() is always called during startup. */
diff --git a/it85spi.c b/it85spi.c
index 5ce9193..47e2337 100644
--- a/it85spi.c
+++ b/it85spi.c
@@ -289,6 +289,9 @@
{
int ret;

+ if (!programming_ec())
+ return 1;
+
if (!(internal_buses_supported & BUS_FWH)) {
msg_pdbg("%s():%d buses not support FWH\n", __func__, __LINE__);
return 1;
diff --git a/linux_mtd.c b/linux_mtd.c
index d2df95e..bee7cc5 100644
--- a/linux_mtd.c
+++ b/linux_mtd.c
@@ -363,6 +363,9 @@
int dev_num = 0;
int ret = 1;

+ if (!programming_host())
+ return 1;
+
param = extract_programmer_param("dev");
if (param) {
char *endptr;
diff --git a/linux_spi.c b/linux_spi.c
index aa73c18..221a36e 100644
--- a/linux_spi.c
+++ b/linux_spi.c
@@ -78,6 +78,14 @@
const uint8_t mode = SPI_MODE_0;
const uint8_t bits = 8;

+ /*
+ * FIXME: There might be other programmers with flash memory (such as
+ * an EC) connected via SPI. For now we rely on the device's driver to
+ * distinguish it and assume generic SPI implies host.
+ */
+ if (!programming_host())
+ return 1;
+
p = extract_programmer_param("spispeed");
if (p && strlen(p)) {
speed_hz = (uint32_t)strtoul(p, &endp, 10) * 1000;
diff --git a/programmer.c b/programmer.c
index f4b4384..ffad784 100644
--- a/programmer.c
+++ b/programmer.c
@@ -128,6 +128,13 @@
return 0;
}

+struct programmer_alias aliases[] = {
+ { "ec", ALIAS_EC },
+ { "host", ALIAS_HOST },
+ { NULL },
+};
+struct programmer_alias *alias;
+
enum chipbustype get_buses_supported(void)
{
int i;
diff --git a/programmer.h b/programmer.h
index 3cf53b9..a7bd947 100644
--- a/programmer.h
+++ b/programmer.h
@@ -130,6 +130,44 @@
PROGRAMMER_INVALID /* This must always be the last entry. */
};

+enum alias_type {
+ ALIAS_NONE = 0, /* no alias (default) */
+ ALIAS_EC, /* embedded controller */
+ ALIAS_HOST, /* chipset / PCH / SoC / etc. */
+};
+
+struct programmer_alias {
+ const char *name;
+ enum alias_type type;
+};
+
+extern struct programmer_alias *alias;
+extern struct programmer_alias aliases[];
+
+/**
+ * This function returns 'true' if current flashrom invocation is programming
+ * NONE.
+ */
+static inline int programming_none(void) {
+ return alias && (alias->type == ALIAS_NONE);
+}
+
+/**
+ * This function returns 'true' if current flashrom invocation is programming
+ * the EC.
+ */
+static inline int programming_ec(void) {
+ return alias && (alias->type == ALIAS_EC);
+}
+
+/**
+ * This function returns 'true' if current flashrom invocation is programming
+ * the HOST.
+ */
+static inline int programming_host(void) {
+ return alias && (alias->type == ALIAS_HOST);
+}
+
enum programmer_type {
PCI = 1, /* to detect uninitialized values */
USB,

To view, visit change 38671. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: flashrom
Gerrit-Branch: master
Gerrit-Change-Id: I73766451cd8900a9e0fe08efc7a4d81b2a35ac8d
Gerrit-Change-Number: 38671
Gerrit-PatchSet: 1
Gerrit-Owner: Edward O'Callaghan <quasisec@chromium.org>
Gerrit-MessageType: newchange