summaryrefslogtreecommitdiffstats
path: root/flashrom.c
diff options
context:
space:
mode:
Diffstat (limited to 'flashrom.c')
-rw-r--r--flashrom.c40
1 files changed, 37 insertions, 3 deletions
diff --git a/flashrom.c b/flashrom.c
index c8c7dd564..3ddc4b03c 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -729,6 +729,40 @@ static int init_default_layout(struct flashctx *flash)
return 0;
}
+/* special unit-test hook */
+write_func_t *g_test_write_injector;
+
+static write_func_t *lookup_write_func_ptr(const struct flashchip *chip)
+{
+ switch (chip->write) {
+ case WRITE_JEDEC: return &write_jedec;
+ case WRITE_JEDEC1: return &write_jedec_1;
+ case WRITE_OPAQUE: return &write_opaque;
+ case SPI_CHIP_WRITE1: return &spi_chip_write_1;
+ case SPI_CHIP_WRITE256: return &spi_chip_write_256;
+ case SPI_WRITE_AAI: return &spi_aai_write;
+ case SPI_WRITE_AT45DB: return &spi_write_at45db;
+ case WRITE_28SF040: return &write_28sf040;
+ case WRITE_82802AB: return &write_82802ab;
+ case WRITE_EN29LV640B: return &write_en29lv640b;
+ case EDI_CHIP_WRITE: return &edi_chip_write;
+ case TEST_WRITE_INJECTOR: return g_test_write_injector;
+ /* default: total function, 0 indicates no write function set.
+ * We explicitly do not want a default catch-all case in the switch
+ * to ensure unhandled enum's are compiler warnings.
+ */
+ case NO_WRITE_FUNC: return NULL;
+ };
+
+ return NULL;
+}
+
+static int write_flash(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
+{
+ write_func_t *write_func = lookup_write_func_ptr(flash->chip);
+ return write_func(flash, buf, start, len);
+}
+
typedef int (probe_func_t)(struct flashctx *flash);
static probe_func_t *lookup_probe_func_ptr(const struct flashchip *chip)
@@ -1161,7 +1195,7 @@ static int erase_block(struct flashctx *const flashctx,
if (!writecount++)
msg_cdbg("W");
/* Needs the partial write function signature. */
- if (flashctx->chip->write(flashctx, backup_contents + starthere,
+ if (write_flash(flashctx, backup_contents + starthere,
info->erase_start + starthere, lenhere))
goto _free_ret;
starthere += lenhere;
@@ -1265,7 +1299,7 @@ static int read_erase_write_block(struct flashctx *const flashctx,
if (!writecount++)
msg_cdbg("W");
/* Needs the partial write function signature. */
- if (flashctx->chip->write(flashctx, newcontents + starthere,
+ if (write_flash(flashctx, newcontents + starthere,
info->erase_start + starthere, lenhere))
goto _free_ret;
starthere += lenhere;
@@ -1555,7 +1589,7 @@ static int chip_safety_check(const struct flashctx *flash, int force,
return 1;
msg_cerr("Continuing anyway.\n");
}
- if (!chip->write) {
+ if (!lookup_write_func_ptr(chip)) {
msg_cerr("flashrom has no write function for this "
"flash chip.\n");
return 1;