summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNikolai Artemiev <nartemiev@google.com>2022-08-30 11:11:13 +1000
committerEdward O'Callaghan <quasisec@chromium.org>2022-10-28 00:41:32 +0000
commit2e00f736714c3117354106c502a8c89b23896b2b (patch)
treefa51e602529c445891de23cafb36a1f4f42b6434
parentbd8a02de6cd9a6fb909be7e246ab7bb0dc558798 (diff)
downloadflashrom-2e00f736714c3117354106c502a8c89b23896b2b.tar.gz
flashrom-2e00f736714c3117354106c502a8c89b23896b2b.tar.bz2
flashrom-2e00f736714c3117354106c502a8c89b23896b2b.zip
tree/: Convert flashchip decode range func ptr to enum
Replace the `decode_range` function pointer in `struct flashchip` to an enum value. The enum value can be used to find the corresponding function pointer by passing it to `lookup_decode_range_func_ptr()`. Removing function pointers like `decode_range` makes it possible to represent chip data in a declarative format that does not have to be stored as C source code. BUG=b:242479049 BRANCH=none TEST=ninja && ninja test Signed-off-by: Nikolai Artemiev <nartemiev@google.com> Change-Id: If6d08d414d3d1ddadc95ca1d407fc87c23ab543d Reviewed-on: https://review.coreboot.org/c/flashrom/+/67195 Reviewed-by: Edward O'Callaghan <quasisec@chromium.org> Reviewed-by: Felix Singer <felixsinger@posteo.net> Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
-rw-r--r--flashchips.c44
-rw-r--r--include/flash.h14
-rw-r--r--tests/chip_wp.c2
-rw-r--r--writeprotect.c19
4 files changed, 51 insertions, 28 deletions
diff --git a/flashchips.c b/flashchips.c
index 41e4c42c2..cbfbc7b15 100644
--- a/flashchips.c
+++ b/flashchips.c
@@ -2451,7 +2451,7 @@ const struct flashchip flashchips[] = {
.sec = {STATUS1, 6, RW},
.cmp = {STATUS2, 6, RW},
},
- .decode_range = decode_range_spi25,
+ .decode_range = DECODE_RANGE_SPI25,
},
{
@@ -5183,7 +5183,7 @@ const struct flashchip flashchips[] = {
.bp = {{STATUS1, 2, RW}, {STATUS1, 3, RW}, {STATUS1, 4, RW}},
.tb = {STATUS1, 5, RW}, /* Called BP3 in datasheet, acts like TB */
},
- .decode_range = decode_range_spi25,
+ .decode_range = DECODE_RANGE_SPI25,
},
{
@@ -6371,7 +6371,7 @@ const struct flashchip flashchips[] = {
.sec = {STATUS1, 6, RW}, /* Called BP4 in datasheet, acts like SEC */
.cmp = {STATUS2, 6, RW},
},
- .decode_range = decode_range_spi25,
+ .decode_range = DECODE_RANGE_SPI25,
},
{
@@ -6537,7 +6537,7 @@ const struct flashchip flashchips[] = {
.sec = {STATUS1, 6, RW}, /* Called BP4 in datasheet, acts like SEC */
.cmp = {STATUS2, 6, RW},
},
- .decode_range = decode_range_spi25,
+ .decode_range = DECODE_RANGE_SPI25,
},
{
@@ -6665,7 +6665,7 @@ const struct flashchip flashchips[] = {
.sec = {STATUS1, 6, RW}, /* Called BP4 in datasheet, acts like SEC */
.cmp = {STATUS2, 6, RW},
},
- .decode_range = decode_range_spi25,
+ .decode_range = DECODE_RANGE_SPI25,
},
{
@@ -6798,7 +6798,7 @@ const struct flashchip flashchips[] = {
.bp = {{STATUS1, 2, RW}, {STATUS1, 3, RW}, {STATUS1, 4, RW}, {STATUS1, 5, RW}},
.tb = {STATUS1, 6, RW},
},
- .decode_range = decode_range_spi25,
+ .decode_range = DECODE_RANGE_SPI25,
},
{
@@ -6847,7 +6847,7 @@ const struct flashchip flashchips[] = {
.sec = {STATUS1, 6, RW}, /* Called BP4 in datasheet, acts like SEC */
.cmp = {STATUS2, 6, RW},
},
- .decode_range = decode_range_spi25,
+ .decode_range = DECODE_RANGE_SPI25,
},
{
@@ -6969,7 +6969,7 @@ const struct flashchip flashchips[] = {
.sec = {STATUS1, 6, RW}, /* Called BP4 in datasheet, acts like SEC */
.cmp = {STATUS2, 6, RW},
},
- .decode_range = decode_range_spi25,
+ .decode_range = DECODE_RANGE_SPI25,
},
{
@@ -16236,7 +16236,7 @@ const struct flashchip flashchips[] = {
.cmp = {STATUS2, 6, RW},
.wps = {STATUS3, 2, RW},
},
- .decode_range = decode_range_spi25,
+ .decode_range = DECODE_RANGE_SPI25,
},
{
@@ -16729,7 +16729,7 @@ const struct flashchip flashchips[] = {
.cmp = {STATUS2, 6, RW},
.wps = {STATUS3, 2, RW},
},
- .decode_range = decode_range_spi25,
+ .decode_range = DECODE_RANGE_SPI25,
},
{
@@ -17347,7 +17347,7 @@ const struct flashchip flashchips[] = {
.cmp = {STATUS2, 6, RW},
.wps = {STATUS3, 2, RW},
},
- .decode_range = decode_range_spi25,
+ .decode_range = DECODE_RANGE_SPI25,
},
{
@@ -17395,7 +17395,7 @@ const struct flashchip flashchips[] = {
.sec = {STATUS1, 6, RW},
.cmp = {STATUS2, 6, RW},
},
- .decode_range = decode_range_spi25,
+ .decode_range = DECODE_RANGE_SPI25,
},
{
@@ -17445,7 +17445,7 @@ const struct flashchip flashchips[] = {
.sec = {STATUS1, 6, RW},
.cmp = {STATUS2, 6, RW},
},
- .decode_range = decode_range_spi25,
+ .decode_range = DECODE_RANGE_SPI25,
},
{
@@ -17493,7 +17493,7 @@ const struct flashchip flashchips[] = {
.sec = {STATUS1, 6, RW},
.cmp = {STATUS2, 6, RW},
},
- .decode_range = decode_range_spi25,
+ .decode_range = DECODE_RANGE_SPI25,
},
{
@@ -17663,7 +17663,7 @@ const struct flashchip flashchips[] = {
.tb = {STATUS1, 6, RW},
.cmp = {STATUS2, 6, RW},
},
- .decode_range = decode_range_spi25,
+ .decode_range = DECODE_RANGE_SPI25,
},
{
@@ -17718,7 +17718,7 @@ const struct flashchip flashchips[] = {
.tb = {STATUS1, 6, RW},
.cmp = {STATUS2, 6, RW},
},
- .decode_range = decode_range_spi25,
+ .decode_range = DECODE_RANGE_SPI25,
},
{
@@ -17773,7 +17773,7 @@ const struct flashchip flashchips[] = {
.tb = {STATUS1, 6, RW},
.cmp = {STATUS2, 6, RW},
},
- .decode_range = decode_range_spi25,
+ .decode_range = DECODE_RANGE_SPI25,
},
{
@@ -18238,7 +18238,7 @@ const struct flashchip flashchips[] = {
.sec = {STATUS1, 6, RW},
.cmp = {STATUS2, 6, RW},
},
- .decode_range = decode_range_spi25,
+ .decode_range = DECODE_RANGE_SPI25,
},
{
@@ -18290,7 +18290,7 @@ const struct flashchip flashchips[] = {
.cmp = {STATUS2, 6, RW},
.wps = {STATUS3, 2, RW},
},
- .decode_range = decode_range_spi25,
+ .decode_range = DECODE_RANGE_SPI25,
},
{
@@ -18380,7 +18380,7 @@ const struct flashchip flashchips[] = {
.sec = {STATUS1, 6, RW},
.cmp = {STATUS2, 6, RW},
},
- .decode_range = decode_range_spi25,
+ .decode_range = DECODE_RANGE_SPI25,
},
{
@@ -19752,7 +19752,7 @@ const struct flashchip flashchips[] = {
.sec = {STATUS1, 6, RW},
.cmp = {STATUS2, 6, RW},
},
- .decode_range = decode_range_spi25,
+ .decode_range = DECODE_RANGE_SPI25,
},
{
@@ -19842,7 +19842,7 @@ const struct flashchip flashchips[] = {
.bp = {{STATUS1, 2, RW}, {STATUS1, 3, RW}, {STATUS1, 4, RW}, {STATUS1, 5, RW}},
.tb = {STATUS1, 6, RW},
},
- .decode_range = decode_range_spi25,
+ .decode_range = DECODE_RANGE_SPI25,
},
{
diff --git a/include/flash.h b/include/flash.h
index 633601db8..32290e4d4 100644
--- a/include/flash.h
+++ b/include/flash.h
@@ -211,6 +211,12 @@ struct reg_bit_info {
struct wp_bits;
+enum decode_range_func {
+ NO_DECODE_RANGE_FUNC = 0, /* 0 indicates no range decode funciton is set. */
+ DECODE_RANGE_SPI25 = 1,
+};
+typedef void (decode_range_func_t)(size_t *start, size_t *len, const struct wp_bits *, size_t chip_len);
+
struct flashchip {
const char *vendor;
const char *name;
@@ -321,9 +327,11 @@ struct flashchip {
struct reg_bit_info wps;
} reg_bits;
- /* Function that takes a set of WP config bits (e.g. BP, SEC, TB, etc) */
- /* and determines what protection range they select. */
- void (*decode_range)(size_t *start, size_t *len, const struct wp_bits *, size_t chip_len);
+ /*
+ * Function that takes a set of WP config bits (e.g. BP, SEC, TB, etc)
+ * and determines what protection range they select.
+ */
+ enum decode_range_func decode_range;
};
typedef int (*chip_restore_fn_cb_t)(struct flashctx *flash, uint8_t status);
diff --git a/tests/chip_wp.c b/tests/chip_wp.c
index 95e6b05ce..e4eff9907 100644
--- a/tests/chip_wp.c
+++ b/tests/chip_wp.c
@@ -97,7 +97,7 @@ static const struct flashchip chip_W25Q128_V = {
.cmp = {STATUS2, 6, RW},
.wps = {STATUS3, 2, RW},
},
- .decode_range = decode_range_spi25,
+ .decode_range = DECODE_RANGE_SPI25,
};
/* Trying to set an unsupported WP range fails */
diff --git a/writeprotect.c b/writeprotect.c
index 57d0f9bd6..dcffe0308 100644
--- a/writeprotect.c
+++ b/writeprotect.c
@@ -174,11 +174,26 @@ static enum flashrom_wp_result write_wp_bits(struct flashctx *flash, struct wp_b
return FLASHROM_WP_OK;
}
+static decode_range_func_t *lookup_decode_range_func_ptr(const struct flashchip *chip)
+{
+ switch (chip->decode_range) {
+ case DECODE_RANGE_SPI25: return &decode_range_spi25;
+ /* default: total function, 0 indicates no decode range function set. */
+ case NO_DECODE_RANGE_FUNC: return NULL;
+ };
+
+ return NULL;
+}
+
+
/** Get the range selected by a WP configuration. */
static enum flashrom_wp_result get_wp_range(struct wp_range *range, struct flashctx *flash, const struct wp_bits *bits)
{
- flash->chip->decode_range(&range->start, &range->len, bits, flashrom_flash_getsize(flash));
+ decode_range_func_t *decode_range = lookup_decode_range_func_ptr(flash->chip);
+ if (decode_range == NULL)
+ return FLASHROM_WP_ERR_OTHER;
+ decode_range(&range->start, &range->len, bits, flashrom_flash_getsize(flash));
return FLASHROM_WP_OK;
}
@@ -424,7 +439,7 @@ static int set_wp_mode(struct wp_bits *bits, const enum flashrom_wp_mode mode)
static bool chip_supported(struct flashctx *flash)
{
- return (flash->chip != NULL) && (flash->chip->decode_range != NULL);
+ return (flash->chip != NULL) && (flash->chip->decode_range != NO_DECODE_RANGE_FUNC);
}