Angel Pons has uploaded this change for review.

View Change

Introduce `extract_programmer_param_toggle` helper

This is useful to retrieve programmer params which can only be 0 or 1.

Change-Id: I2a27f36b0fbafc2113387ad82321f3d026811f5e
Signed-off-by: Angel Pons <th3fanbus@gmail.com>
---
M flashrom.c
M programmer.h
M realtek_mst_i2c_spi.c
3 files changed, 45 insertions(+), 27 deletions(-)

git pull ssh://review.coreboot.org:29418/flashrom refs/changes/17/46717/1
diff --git a/flashrom.c b/flashrom.c
index c18a04f..d574902 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -789,6 +789,39 @@
return extract_param(&programmer_param, param_name, ",");
}

+/*
+ * Extract a 'toggle' (on/off) value from a programmer parameter.
+ * Returns 0 on success, -1 on bad usage (in code or at runtime).
+ * If the parameter was not specified, 'value' remains unchanged.
+ */
+int extract_programmer_param_toggle(const char *param_name, int *value)
+{
+ if (!param_name || !value)
+ return -1;
+
+ char *param_str = extract_programmer_param(param_name);
+
+ if (!param_str)
+ return 0;
+
+ const char c = param_str[0];
+
+ free(param_str);
+
+ if (c == '1') {
+ *value = 1;
+ return 0;
+ }
+
+ if (c == '0') {
+ *value = 0;
+ return 0;
+ }
+
+ msg_perr("Incorrect toggle parameter format, %s = 1 or 0.\n", param_name);
+ return -1;
+}
+
/* Returns the number of well-defined erasers for a chip. */
static unsigned int count_usable_erasers(const struct flashctx *flash)
{
diff --git a/programmer.h b/programmer.h
index 9a7892d..34aac88 100644
--- a/programmer.h
+++ b/programmer.h
@@ -612,6 +612,7 @@
extern unsigned long flashbase;
unsigned int count_max_decode_exceedings(const struct flashctx *flash);
char *extract_programmer_param(const char *param_name);
+int extract_programmer_param_toggle(const char *param_name, int *value);

/* spi.c */
#define MAX_DATA_UNSPECIFIED 0
diff --git a/realtek_mst_i2c_spi.c b/realtek_mst_i2c_spi.c
index 538b07a..574087b 100644
--- a/realtek_mst_i2c_spi.c
+++ b/realtek_mst_i2c_spi.c
@@ -434,7 +434,7 @@

static int get_params(int *i2c_bus, int *reset, int *enter_isp)
{
- char *bus_str = NULL, *reset_str = NULL, *isp_str = NULL;
+ char *bus_str = NULL;
int ret = SPI_GENERIC_ERROR;

bus_str = extract_programmer_param("bus");
@@ -464,33 +464,17 @@
msg_perr("%s: Bus number not specified.\n", __func__);
}

- reset_str = extract_programmer_param("reset-mcu");
- if (reset_str) {
- if (reset_str[0] == '1')
- *reset = 1;
- else if (reset_str[0] == '0')
- *reset = 0;
- else {
- msg_perr("%s: Incorrect param format, reset-mcu=1 or 0.\n", __func__);
- ret = SPI_GENERIC_ERROR;
- }
- } else
- *reset = 0; /* Default behaviour is no MCU reset on tear-down. */
- free(reset_str);
+ /* Default behaviour is no MCU reset on tear-down. */
+ *reset = 0;
+ ret = extract_programmer_param_toggle("reset-mcu", reset);
+ if (ret)
+ goto _get_params_failed;

- isp_str = extract_programmer_param("enter-isp");
- if (isp_str) {
- if (isp_str[0] == '1')
- *enter_isp = 1;
- else if (isp_str[0] == '0')
- *enter_isp = 0;
- else {
- msg_perr("%s: Incorrect param format, enter-isp=1 or 0.\n", __func__);
- ret = SPI_GENERIC_ERROR;
- }
- } else
- *enter_isp = 1; /* Default behaviour is enter ISP on setup. */
- free(isp_str);
+ /* Default behaviour is enter ISP on setup. */
+ *enter_isp = 1;
+ ret = extract_programmer_param_toggle("enter-isp", enter_isp);
+ if (ret)
+ goto _get_params_failed;

_get_params_failed:
if (bus_str)

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

Gerrit-Project: flashrom
Gerrit-Branch: master
Gerrit-Change-Id: I2a27f36b0fbafc2113387ad82321f3d026811f5e
Gerrit-Change-Number: 46717
Gerrit-PatchSet: 1
Gerrit-Owner: Angel Pons <th3fanbus@gmail.com>
Gerrit-MessageType: newchange