Edward O'Callaghan has uploaded this change for review.

View Change

flashrom.c: Rewrite flashbuses_to_text()

The previous implementation was not total on the function
domain and had many edge cases.

BUG=issues/408
TEST=`./flashrom -p internal --flash-name -VVV | grep proto`.

Change-Id: Iafa757603ce262faf976b98c8bbf0ba4b780c3cb
Signed-off-by: Edward O'Callaghan <quasisec@google.com>
---
M flashrom.c
1 file changed, 32 insertions(+), 19 deletions(-)

git pull ssh://review.coreboot.org:29418/flashrom refs/changes/19/69219/1
diff --git a/flashrom.c b/flashrom.c
index f0049b2..594186e 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -771,30 +771,27 @@
*/
char *flashbuses_to_text(enum chipbustype bustype)
{
- char *ret = calloc(1, 1);
/*
* FIXME: Once all chipsets and flash chips have been updated, NONSPI
* will cease to exist and should be eliminated here as well.
*/
- if (bustype == BUS_NONSPI) {
- ret = strcat_realloc(ret, "Non-SPI, ");
- } else {
- if (bustype & BUS_PARALLEL)
- ret = strcat_realloc(ret, "Parallel, ");
- if (bustype & BUS_LPC)
- ret = strcat_realloc(ret, "LPC, ");
- if (bustype & BUS_FWH)
- ret = strcat_realloc(ret, "FWH, ");
- if (bustype & BUS_SPI)
- ret = strcat_realloc(ret, "SPI, ");
- if (bustype & BUS_PROG)
- ret = strcat_realloc(ret, "Programmer-specific, ");
- if (bustype == BUS_NONE)
- ret = strcat_realloc(ret, "None, ");
+ static const char *const bustypes[] = {
+ [BUS_NONSPI] = "Non-SPI",
+ [BUS_PARALLEL] = "Parallel",
+ [BUS_LPC] = "LPC",
+ [BUS_FWH] = "FWH",
+ [BUS_SPI] = "SPI",
+ [BUS_PROG] = "Programmer-specific",
+ [BUS_NONE] = "None",
+ };
+ char *ret = calloc(1, 1);
+ for (unsigned int i = 0; i < ARRAY_SIZE(bustypes); i++) {
+ if (bustype & i) {
+ if (strlen(ret) > 1)
+ ret = strcat_realloc(ret, ", ");
+ ret = strcat_realloc(ret, bustypes[i]);
+ }
}
- /* Kill last comma. */
- ret[strlen(ret) - 2] = '\0';
- ret = realloc(ret, strlen(ret) + 1);
return ret;
}


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

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