1
0
mirror of https://review.coreboot.org/flashrom.git synced 2025-04-28 23:43:42 +02:00

Revert "flashrom.c: Remove programmer_param global state"

This reverts commit 3b8b93e17f6ad861acb2a0810ae1dcf03285fb10.

Invoking flashrom with no parameters crashes when calling strdup(NULL)
in programmer_init().

Change-Id: I3b689ad4bdd0c9c3b11f30becafc878c78630f0b
Signed-off-by: Thomas Heijligen <thomas.heijligen@secunet.com>
Reviewed-on: https://review.coreboot.org/c/flashrom/+/67621
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Angel Pons <th3fanbus@gmail.com>
Reviewed-by: Felix Singer <felixsinger@posteo.net>
Reviewed-by: Nico Huber <nico.h@gmx.de>
Reviewed-by: Jonathon Hall <jonathon.hall@puri.sm>
Reviewed-by: Anastasia Klimchuk <aklm@chromium.org>
Reviewed-by: Edward O'Callaghan <quasisec@chromium.org>
This commit is contained in:
Thomas Heijligen 2022-09-13 19:14:32 +02:00 committed by Felix Singer
parent 74698d2165
commit 50ea017af6

View File

@ -38,6 +38,7 @@ const char flashrom_version[] = FLASHROM_VERSION;
const char *chip_to_probe = NULL; const char *chip_to_probe = NULL;
static const struct programmer_entry *programmer = NULL; static const struct programmer_entry *programmer = NULL;
static const char *programmer_param = NULL;
/* /*
* Programmers supporting multiple buses can have differing size limits on * Programmers supporting multiple buses can have differing size limits on
@ -125,28 +126,10 @@ static int deregister_chip_restore(struct flashctx *flash)
return rc; return rc;
} }
static int get_param_residue(const char *prog_params, int ret)
{
if (prog_params && strlen(prog_params)) {
if (ret != 0) {
/* It is quite possible that any unhandled programmer parameter would have been valid,
* but an error in actual programmer init happened before the parameter was evaluated.
*/
msg_pwarn("Unhandled programmer parameters (possibly due to another failure): %s\n", prog_params);
} else {
/* Actual programmer init was successful, but the user specified an invalid or unusable
* (for the current programmer configuration) parameter.
*/
msg_perr("Unhandled programmer parameters: %s\n", prog_params);
msg_perr("Aborting.\n");
ret = ERROR_FATAL;
}
}
return ret;
}
int programmer_init(const struct programmer_entry *prog, const char *param) int programmer_init(const struct programmer_entry *prog, const char *param)
{ {
int ret;
if (prog == NULL) { if (prog == NULL) {
msg_perr("Invalid programmer specified!\n"); msg_perr("Invalid programmer specified!\n");
return -1; return -1;
@ -167,11 +150,25 @@ int programmer_init(const struct programmer_entry *prog, const char *param)
/* Default to allowing writes. Broken programmers set this to 0. */ /* Default to allowing writes. Broken programmers set this to 0. */
programmer_may_write = true; programmer_may_write = true;
msg_pdbg("Initializing %s programmer\n", prog->name); programmer_param = param;
const struct programmer_cfg cfg = { .params = strdup(param) }; msg_pdbg("Initializing %s programmer\n", programmer->name);
int ret = prog->init(&cfg); ret = programmer->init(NULL);
ret = get_param_residue(cfg.params, ret); if (programmer_param && strlen(programmer_param)) {
free(cfg.params); if (ret != 0) {
/* It is quite possible that any unhandled programmer parameter would have been valid,
* but an error in actual programmer init happened before the parameter was evaluated.
*/
msg_pwarn("Unhandled programmer parameters (possibly due to another failure): %s\n",
programmer_param);
} else {
/* Actual programmer init was successful, but the user specified an invalid or unusable
* (for the current programmer configuration) parameter.
*/
msg_perr("Unhandled programmer parameters: %s\n", programmer_param);
msg_perr("Aborting.\n");
ret = ERROR_FATAL;
}
}
return ret; return ret;
} }
@ -191,6 +188,7 @@ int programmer_shutdown(void)
ret |= shutdown_fn[i].func(shutdown_fn[i].data); ret |= shutdown_fn[i].func(shutdown_fn[i].data);
} }
programmer_param = NULL;
registered_master_count = 0; registered_master_count = 0;
return ret; return ret;
@ -229,7 +227,7 @@ int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start,
* needle and remove everything from the first occurrence of needle to the next * needle and remove everything from the first occurrence of needle to the next
* delimiter from haystack. * delimiter from haystack.
*/ */
static char *extract_param(char *const *haystack, const char *needle, const char *delim) static char *extract_param(const char *const *haystack, const char *needle, const char *delim)
{ {
char *param_pos, *opt_pos, *rest; char *param_pos, *opt_pos, *rest;
char *opt = NULL; char *opt = NULL;
@ -287,7 +285,7 @@ static char *extract_param(char *const *haystack, const char *needle, const char
char *extract_programmer_param_str(const struct programmer_cfg *cfg, const char *param_name) char *extract_programmer_param_str(const struct programmer_cfg *cfg, const char *param_name)
{ {
return extract_param(&cfg->params, param_name, ","); return extract_param(&programmer_param, param_name, ",");
} }
static int check_block_eraser(const struct flashctx *flash, int k, int log) static int check_block_eraser(const struct flashctx *flash, int k, int log)