Edward O'Callaghan has uploaded this change for review. ( https://review.coreboot.org/c/flashrom/+/67360 )
Change subject: libflashrom: Fix flashrom_programmer_init() ......................................................................
libflashrom: Fix flashrom_programmer_init()
Change-Id: Ibd61333c0f74086186a0f2f1f2c9cf780364f9d6 Signed-off-by: Edward O'Callaghan quasisec@google.com --- M flashrom.c M libflashrom.c 2 files changed, 32 insertions(+), 8 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/60/67360/1
diff --git a/flashrom.c b/flashrom.c index ba363b2..4706d89 100644 --- a/flashrom.c +++ b/flashrom.c @@ -146,8 +146,8 @@ programmer_may_write = 1;
programmer_param = param; - msg_pdbg("Initializing %s programmer\n", programmer->name); - ret = programmer->init(); + msg_pdbg("Initializing %s programmer\n", prog->name); + ret = prog->init(); if (programmer_param && strlen(programmer_param)) { if (ret != 0) { /* It is quite possible that any unhandled programmer parameter would have been valid, diff --git a/libflashrom.c b/libflashrom.c index 3e28938..2febf30 100644 --- a/libflashrom.c +++ b/libflashrom.c @@ -29,6 +29,10 @@ #include "writeprotect.h"
+struct flashrom_programmer { + struct programmer_entry prog; /* the programmer_entry must be the first field! */ +}; + /** Pointer to log callback function. */ static flashrom_log_callback *global_log_callback = NULL;
@@ -193,18 +197,28 @@ int flashrom_programmer_init(struct flashrom_programmer **const flashprog, const char *const prog_name, const char *const prog_param) { - unsigned prog; + unsigned prog_idx;
- for (prog = 0; prog < programmer_table_size; prog++) { - if (strcmp(prog_name, programmer_table[prog]->name) == 0) + for (prog_idx = 0; prog_idx < programmer_table_size; prog_idx++) { + if (strcmp(prog_name, programmer_table[prog_idx]->name) == 0) break; } - if (prog >= programmer_table_size) { - msg_ginfo("Error: Unknown programmer "%s". Valid choices are:\n", prog_name); + if (prog_idx >= programmer_table_size) { + msg_gerr("%s: Error: Unknown programmer "%s". Valid choices are:\n", __func__, prog_name); list_programmers_linebreak(0, 80, 0); return 1; } - return programmer_init(programmer_table[prog], prog_param); + + if (!flashprog) { + msg_gerr("%s: call site did not allocate a valid flashrom_programmer entry!\n", __func__); + return -2; /* no valid flashrom_programmer allocated. */ + } + + /* Take a mutable copy out of the table of available programmers. */ + struct programmer_entry *pentry = &((*flashprog)->prog); + memcpy(pentry, programmer_table[prog_idx], sizeof(struct programmer_entry)); + + return programmer_init(pentry, prog_param); }
int flashrom_programmer_shutdown(struct flashrom_programmer *const flashprog)