Lachlan Bishop has uploaded this change for review. ( https://review.coreboot.org/c/flashrom/+/45230 )
Change subject: dummyflasher.c: Factor out global state ......................................................................
dummyflasher.c: Factor out global state
Moves global state into spi_master data.
Change-Id: I972b085875f1277d9ff33326669d2676a3bcd3aa Signed-off-by: Lachlan Bishop lxb@google.com --- M dummyflasher.c 1 file changed, 189 insertions(+), 169 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/30/45230/1
diff --git a/dummyflasher.c b/dummyflasher.c index 85908af..236861d 100644 --- a/dummyflasher.c +++ b/dummyflasher.c @@ -47,24 +47,29 @@ EMULATE_WINBOND_W25Q128FV, EMULATE_VARIABLE_SIZE, }; -static enum emu_chip emu_chip = EMULATE_NONE; -static char *emu_persistent_image = NULL; -static unsigned int emu_chip_size = 0; -static int emu_modified = 0; /* is the image modified since reading it? */ -#if EMULATE_SPI_CHIP -static unsigned int emu_max_byteprogram_size = 0; -static unsigned int emu_max_aai_size = 0; -static unsigned int emu_jedec_se_size = 0; -static unsigned int emu_jedec_be_52_size = 0; -static unsigned int emu_jedec_be_d8_size = 0; -static unsigned int emu_jedec_ce_60_size = 0; -static unsigned int emu_jedec_ce_c7_size = 0; -static unsigned char spi_blacklist[256]; -static unsigned char spi_ignorelist[256]; -static unsigned int spi_blacklist_size = 0; -static unsigned int spi_ignorelist_size = 0; -static uint8_t emu_status = 0;
+struct emu_data { + enum emu_chip emu_chip; + char *emu_persistent_image; + unsigned int emu_chip_size; + int emu_modified; /* is the image modified since reading it? */ +#if EMULATE_SPI_CHIP + unsigned int emu_max_byteprogram_size; + unsigned int emu_max_aai_size; + unsigned int emu_jedec_se_size; + unsigned int emu_jedec_be_52_size; + unsigned int emu_jedec_be_d8_size; + unsigned int emu_jedec_ce_60_size; + unsigned int emu_jedec_ce_c7_size; + unsigned char spi_blacklist[256]; + unsigned char spi_ignorelist[256]; + unsigned int spi_blacklist_size; + unsigned int spi_ignorelist_size; + uint8_t emu_status; +#endif +}; + +#if EMULATE_SPI_CHIP /* A legit complete SFDP table based on the MX25L6436E (rev. 1.8) datasheet. */ static const uint8_t sfdp_table[] = { 0x53, 0x46, 0x44, 0x50, // @0x00: SFDP signature @@ -109,7 +114,7 @@ static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr); static void dummy_chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len);
-static const struct spi_master spi_master_dummyflasher = { +static struct spi_master spi_master_dummyflasher = { .features = SPI_MASTER_4BA, .max_data_read = MAX_DATA_READ_UNLIMITED, .max_data_write = MAX_DATA_UNSPECIFIED, @@ -137,12 +142,15 @@ { msg_pspew("%s\n", __func__); #if EMULATE_CHIP - if (emu_chip != EMULATE_NONE) { - if (emu_persistent_image && emu_modified) { - msg_pdbg("Writing %s\n", emu_persistent_image); - write_buf_to_file(flashchip_contents, emu_chip_size, emu_persistent_image); - free(emu_persistent_image); - emu_persistent_image = NULL; + struct emu_data *emu_data = (struct emu_data *)data; + if (emu_data->emu_chip != EMULATE_NONE) { + if (emu_data->emu_persistent_image && emu_data->emu_modified) { + msg_pdbg("Writing %s\n", emu_data->emu_persistent_image); + write_buf_to_file(flashchip_contents, + emu_data->emu_chip_size, + emu_data->emu_persistent_image); + free(emu_data->emu_persistent_image); + emu_data->emu_persistent_image = NULL; } free(flashchip_contents); } @@ -163,6 +171,14 @@ struct stat image_stat; #endif
+ struct emu_data *data = calloc(1, sizeof(struct emu_data)); + data->emu_chip = EMULATE_NONE; + if (!data) { + msg_perr("Out of memory!\n"); + return 1; + } + spi_master_dummyflasher.data = data; + msg_pspew("%s\n", __func__);
bustext = extract_programmer_param("bus"); @@ -215,8 +231,8 @@ free(tmp); return 1; } - spi_blacklist_size = i / 2; - for (i = 0; i < spi_blacklist_size * 2; i++) { + data->spi_blacklist_size = i / 2; + for (i = 0; i < data->spi_blacklist_size * 2; i++) { if (!isxdigit((unsigned char)tmp[i])) { msg_perr("Invalid char "%c" in SPI command " "blacklist\n", tmp[i]); @@ -224,18 +240,18 @@ return 1; } } - for (i = 0; i < spi_blacklist_size; i++) { + for (i = 0; i < data->spi_blacklist_size; i++) { unsigned int tmp2; /* SCNx8 is apparently not supported by MSVC (and thus * MinGW), so work around it with an extra variable */ sscanf(tmp + i * 2, "%2x", &tmp2); - spi_blacklist[i] = (uint8_t)tmp2; + data->spi_blacklist[i] = (uint8_t)tmp2; } msg_pdbg("SPI blacklist is "); - for (i = 0; i < spi_blacklist_size; i++) - msg_pdbg("%02x ", spi_blacklist[i]); - msg_pdbg(", size %u\n", spi_blacklist_size); + for (i = 0; i < data->spi_blacklist_size; i++) + msg_pdbg("%02x ", data->spi_blacklist[i]); + msg_pdbg(", size %u\n", data->spi_blacklist_size); } free(tmp);
@@ -251,8 +267,8 @@ free(tmp); return 1; } - spi_ignorelist_size = i / 2; - for (i = 0; i < spi_ignorelist_size * 2; i++) { + data->spi_ignorelist_size = i / 2; + for (i = 0; i < data->spi_ignorelist_size * 2; i++) { if (!isxdigit((unsigned char)tmp[i])) { msg_perr("Invalid char "%c" in SPI command " "ignorelist\n", tmp[i]); @@ -260,18 +276,18 @@ return 1; } } - for (i = 0; i < spi_ignorelist_size; i++) { + for (i = 0; i < data->spi_ignorelist_size; i++) { unsigned int tmp2; /* SCNx8 is apparently not supported by MSVC (and thus * MinGW), so work around it with an extra variable */ sscanf(tmp + i * 2, "%2x", &tmp2); - spi_ignorelist[i] = (uint8_t)tmp2; + data->spi_ignorelist[i] = (uint8_t)tmp2; } msg_pdbg("SPI ignorelist is "); - for (i = 0; i < spi_ignorelist_size; i++) - msg_pdbg("%02x ", spi_ignorelist[i]); - msg_pdbg(", size %u\n", spi_ignorelist_size); + for (i = 0; i < data->spi_ignorelist_size; i++) + msg_pdbg("%02x ", data->spi_ignorelist[i]); + msg_pdbg(", size %u\n", data->spi_ignorelist_size); } free(tmp);
@@ -301,67 +317,67 @@ } #if EMULATE_SPI_CHIP if (!strcmp(tmp, "M25P10.RES")) { - emu_chip = EMULATE_ST_M25P10_RES; - emu_chip_size = 128 * 1024; - emu_max_byteprogram_size = 128; - emu_max_aai_size = 0; - emu_jedec_se_size = 0; - emu_jedec_be_52_size = 0; - emu_jedec_be_d8_size = 32 * 1024; - emu_jedec_ce_60_size = 0; - emu_jedec_ce_c7_size = emu_chip_size; + data->emu_chip = EMULATE_ST_M25P10_RES; + data->emu_chip_size = 128 * 1024; + data->emu_max_byteprogram_size = 128; + data->emu_max_aai_size = 0; + data->emu_jedec_se_size = 0; + data->emu_jedec_be_52_size = 0; + data->emu_jedec_be_d8_size = 32 * 1024; + data->emu_jedec_ce_60_size = 0; + data->emu_jedec_ce_c7_size = data->emu_chip_size; msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page " "write)\n"); } if (!strcmp(tmp, "SST25VF040.REMS")) { - emu_chip = EMULATE_SST_SST25VF040_REMS; - emu_chip_size = 512 * 1024; - emu_max_byteprogram_size = 1; - emu_max_aai_size = 0; - emu_jedec_se_size = 4 * 1024; - emu_jedec_be_52_size = 32 * 1024; - emu_jedec_be_d8_size = 0; - emu_jedec_ce_60_size = emu_chip_size; - emu_jedec_ce_c7_size = 0; + data->emu_chip = EMULATE_SST_SST25VF040_REMS; + data->emu_chip_size = 512 * 1024; + data->emu_max_byteprogram_size = 1; + data->emu_max_aai_size = 0; + data->emu_jedec_se_size = 4 * 1024; + data->emu_jedec_be_52_size = 32 * 1024; + data->emu_jedec_be_d8_size = 0; + data->emu_jedec_ce_60_size = data->emu_chip_size; + data->emu_jedec_ce_c7_size = 0; msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, " "byte write)\n"); } if (!strcmp(tmp, "SST25VF032B")) { - emu_chip = EMULATE_SST_SST25VF032B; - emu_chip_size = 4 * 1024 * 1024; - emu_max_byteprogram_size = 1; - emu_max_aai_size = 2; - emu_jedec_se_size = 4 * 1024; - emu_jedec_be_52_size = 32 * 1024; - emu_jedec_be_d8_size = 64 * 1024; - emu_jedec_ce_60_size = emu_chip_size; - emu_jedec_ce_c7_size = emu_chip_size; + data->emu_chip = EMULATE_SST_SST25VF032B; + data->emu_chip_size = 4 * 1024 * 1024; + data->emu_max_byteprogram_size = 1; + data->emu_max_aai_size = 2; + data->emu_jedec_se_size = 4 * 1024; + data->emu_jedec_be_52_size = 32 * 1024; + data->emu_jedec_be_d8_size = 64 * 1024; + data->emu_jedec_ce_60_size = data->emu_chip_size; + data->emu_jedec_ce_c7_size = data->emu_chip_size; msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI " "write)\n"); } if (!strcmp(tmp, "MX25L6436")) { - emu_chip = EMULATE_MACRONIX_MX25L6436; - emu_chip_size = 8 * 1024 * 1024; - emu_max_byteprogram_size = 256; - emu_max_aai_size = 0; - emu_jedec_se_size = 4 * 1024; - emu_jedec_be_52_size = 32 * 1024; - emu_jedec_be_d8_size = 64 * 1024; - emu_jedec_ce_60_size = emu_chip_size; - emu_jedec_ce_c7_size = emu_chip_size; + data->emu_chip = EMULATE_MACRONIX_MX25L6436; + data->emu_chip_size = 8 * 1024 * 1024; + data->emu_max_byteprogram_size = 256; + data->emu_max_aai_size = 0; + data->emu_jedec_se_size = 4 * 1024; + data->emu_jedec_be_52_size = 32 * 1024; + data->emu_jedec_be_d8_size = 64 * 1024; + data->emu_jedec_ce_60_size = data->emu_chip_size; + data->emu_jedec_ce_c7_size = data->emu_chip_size; msg_pdbg("Emulating Macronix MX25L6436 SPI flash chip (RDID, " "SFDP)\n"); } if (!strcmp(tmp, "W25Q128FV")) { - emu_chip = EMULATE_WINBOND_W25Q128FV; - emu_chip_size = 16 * 1024 * 1024; - emu_max_byteprogram_size = 256; - emu_max_aai_size = 0; - emu_jedec_se_size = 4 * 1024; - emu_jedec_be_52_size = 32 * 1024; - emu_jedec_be_d8_size = 64 * 1024; - emu_jedec_ce_60_size = emu_chip_size; - emu_jedec_ce_c7_size = emu_chip_size; + data->emu_chip = EMULATE_WINBOND_W25Q128FV; + data->emu_chip_size = 16 * 1024 * 1024; + data->emu_max_byteprogram_size = 256; + data->emu_max_aai_size = 0; + data->emu_jedec_se_size = 4 * 1024; + data->emu_jedec_be_52_size = 32 * 1024; + data->emu_jedec_be_d8_size = 64 * 1024; + data->emu_jedec_ce_60_size = data->emu_chip_size; + data->emu_jedec_ce_c7_size = data->emu_chip_size; msg_pdbg("Emulating Winbond W25Q128FV SPI flash chip (RDID)\n"); }
@@ -369,26 +385,26 @@ * flashrom -p dummy:emulate=VARIABLE_SIZE,size=4194304 */ if (!strcmp(tmp, "VARIABLE_SIZE")) { - emu_chip = EMULATE_VARIABLE_SIZE; - emu_chip_size = size; - emu_max_byteprogram_size = 256; - emu_max_aai_size = 0; - emu_jedec_se_size = 4 * 1024; - emu_jedec_be_52_size = 32 * 1024; - emu_jedec_be_d8_size = 64 * 1024; - emu_jedec_ce_60_size = emu_chip_size; - emu_jedec_ce_c7_size = emu_chip_size; + data->emu_chip = EMULATE_VARIABLE_SIZE; + data->emu_chip_size = size; + data->emu_max_byteprogram_size = 256; + data->emu_max_aai_size = 0; + data->emu_jedec_se_size = 4 * 1024; + data->emu_jedec_be_52_size = 32 * 1024; + data->emu_jedec_be_d8_size = 64 * 1024; + data->emu_jedec_ce_60_size = data->emu_chip_size; + data->emu_jedec_ce_c7_size = data->emu_chip_size; msg_pdbg("Emulating generic SPI flash chip (size=%d bytes)\n", - emu_chip_size); + data->emu_chip_size); } #endif - if (emu_chip == EMULATE_NONE) { + if (data->emu_chip == EMULATE_NONE) { msg_perr("Invalid chip specified for emulation: %s\n", tmp); free(tmp); return 1; } free(tmp); - flashchip_contents = malloc(emu_chip_size); + flashchip_contents = malloc(data->emu_chip_size); if (!flashchip_contents) { msg_perr("Out of memory!\n"); return 1; @@ -399,7 +415,7 @@ if (status) { char *endptr; errno = 0; - emu_status = strtoul(status, &endptr, 0); + data->emu_status = strtoul(status, &endptr, 0); free(status); if (errno != 0 || status == endptr) { msg_perr("Error: initial status register specified, " @@ -407,30 +423,30 @@ return 1; } msg_pdbg("Initial status register is set to 0x%02x.\n", - emu_status); + data->emu_status); } #endif
- msg_pdbg("Filling fake flash chip with 0xff, size %i\n", emu_chip_size); - memset(flashchip_contents, 0xff, emu_chip_size); + msg_pdbg("Filling fake flash chip with 0xff, size %i\n", data->emu_chip_size); + memset(flashchip_contents, 0xff, data->emu_chip_size);
/* Will be freed by shutdown function if necessary. */ - emu_persistent_image = extract_programmer_param("image"); - if (!emu_persistent_image) { + data->emu_persistent_image = extract_programmer_param("image"); + if (!data->emu_persistent_image) { /* Nothing else to do. */ goto dummy_init_out; } /* We will silently (in default verbosity) ignore the file if it does not exist (yet) or the size does * not match the emulated chip. */ - if (!stat(emu_persistent_image, &image_stat)) { + if (!stat(data->emu_persistent_image, &image_stat)) { msg_pdbg("Found persistent image %s, %jd B ", - emu_persistent_image, (intmax_t)image_stat.st_size); - if ((uintmax_t)image_stat.st_size == emu_chip_size) { + data->emu_persistent_image, (intmax_t)image_stat.st_size); + if ((uintmax_t)image_stat.st_size == data->emu_chip_size) { msg_pdbg("matches.\n"); - msg_pdbg("Reading %s\n", emu_persistent_image); - if (read_buf_from_file(flashchip_contents, emu_chip_size, - emu_persistent_image)) { - msg_perr("Unable to read %s\n", emu_persistent_image); + msg_pdbg("Reading %s\n", data->emu_persistent_image); + if (read_buf_from_file(flashchip_contents, data->emu_chip_size, + data->emu_persistent_image)) { + msg_perr("Unable to read %s\n", data->emu_persistent_image); free(flashchip_contents); return 1; } @@ -441,15 +457,16 @@ #endif
dummy_init_out: - if (register_shutdown(dummy_shutdown, NULL)) { + if (register_shutdown(dummy_shutdown, data)) { free(flashchip_contents); return 1; } if (dummy_buses_supported & (BUS_PARALLEL | BUS_LPC | BUS_FWH)) register_par_master(&par_master_dummy, dummy_buses_supported & (BUS_PARALLEL | BUS_LPC | BUS_FWH)); - if (dummy_buses_supported & BUS_SPI) + if (dummy_buses_supported & BUS_SPI) { register_spi_master(&spi_master_dummyflasher); + }
return 0; } @@ -521,7 +538,8 @@ static int emulate_spi_chip_response(unsigned int writecnt, unsigned int readcnt, const unsigned char *writearr, - unsigned char *readarr) + unsigned char *readarr, + struct emu_data *emu_data) { unsigned int offs, i, toread; static int unsigned aai_offs; @@ -535,17 +553,17 @@ return 1; } /* spi_blacklist has precedence over spi_ignorelist. */ - for (i = 0; i < spi_blacklist_size; i++) { - if (writearr[0] == spi_blacklist[i]) { + for (i = 0; i < emu_data->spi_blacklist_size; i++) { + if (writearr[0] == emu_data->spi_blacklist[i]) { msg_pdbg("Refusing blacklisted SPI command 0x%02x\n", - spi_blacklist[i]); + emu_data->spi_blacklist[i]); return SPI_INVALID_OPCODE; } } - for (i = 0; i < spi_ignorelist_size; i++) { - if (writearr[0] == spi_ignorelist[i]) { + for (i = 0; i < emu_data->spi_ignorelist_size; i++) { + if (writearr[0] == emu_data->spi_ignorelist[i]) { msg_cdbg("Ignoring ignorelisted SPI command 0x%02x\n", - spi_ignorelist[i]); + emu_data->spi_ignorelist[i]); /* Return success because the command does not fail, * it is simply ignored. */ @@ -553,7 +571,7 @@ } }
- if (emu_max_aai_size && (emu_status & SPI_SR_AAI)) { + if (emu_data->emu_max_aai_size && (emu_data->emu_status & SPI_SR_AAI)) { if (writearr[0] != JEDEC_AAI_WORD_PROGRAM && writearr[0] != JEDEC_WRDI && writearr[0] != JEDEC_RDSR) { @@ -570,7 +588,7 @@ /* offs calculation is only needed for SST chips which treat RES like REMS. */ offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; offs += writecnt - JEDEC_REMS_OUTSIZE; - switch (emu_chip) { + switch (emu_data->emu_chip) { case EMULATE_ST_M25P10_RES: if (readcnt > 0) memset(readarr, 0x10, readcnt); @@ -601,7 +619,7 @@ break; offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; offs += writecnt - JEDEC_REMS_OUTSIZE; - switch (emu_chip) { + switch (emu_data->emu_chip) { case EMULATE_SST_SST25VF040_REMS: for (i = 0; i < readcnt; i++) readarr[i] = sst25vf040_rems_response[(offs + i) % 2]; @@ -623,7 +641,7 @@ } break; case JEDEC_RDID: - switch (emu_chip) { + switch (emu_data->emu_chip) { case EMULATE_SST_SST25VF032B: if (readcnt > 0) readarr[0] = 0xbf; @@ -663,70 +681,70 @@ } break; case JEDEC_RDSR: - memset(readarr, emu_status, readcnt); + memset(readarr, emu_data->emu_status, readcnt); break; /* FIXME: this should be chip-specific. */ case JEDEC_EWSR: case JEDEC_WREN: - emu_status |= SPI_SR_WEL; + emu_data->emu_status |= SPI_SR_WEL; break; case JEDEC_WRSR: - if (!(emu_status & SPI_SR_WEL)) { + if (!(emu_data->emu_status & SPI_SR_WEL)) { msg_perr("WRSR attempted, but WEL is 0!\n"); break; } /* FIXME: add some reasonable simulation of the busy flag */ - emu_status = writearr[1] & ~SPI_SR_WIP; - msg_pdbg2("WRSR wrote 0x%02x.\n", emu_status); + emu_data->emu_status = writearr[1] & ~SPI_SR_WIP; + msg_pdbg2("WRSR wrote 0x%02x.\n", emu_data->emu_status); break; case JEDEC_READ: offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; /* Truncate to emu_chip_size. */ - offs %= emu_chip_size; + offs %= emu_data->emu_chip_size; if (readcnt > 0) memcpy(readarr, flashchip_contents + offs, readcnt); break; case JEDEC_READ_4BA: offs = writearr[1] << 24 | writearr[2] << 16 | writearr[3] << 8 | writearr[4]; /* Truncate to emu_chip_size. */ - offs %= emu_chip_size; + offs %= emu_data->emu_chip_size; if (readcnt > 0) memcpy(readarr, flashchip_contents + offs, readcnt); break; case JEDEC_BYTE_PROGRAM: offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; /* Truncate to emu_chip_size. */ - offs %= emu_chip_size; + offs %= emu_data->emu_chip_size; if (writecnt < 5) { msg_perr("BYTE PROGRAM size too short!\n"); return 1; } - if (writecnt - 4 > emu_max_byteprogram_size) { + if (writecnt - 4 > emu_data->emu_max_byteprogram_size) { msg_perr("Max BYTE PROGRAM size exceeded!\n"); return 1; } memcpy(flashchip_contents + offs, writearr + 4, writecnt - 4); - emu_modified = 1; + emu_data->emu_modified = 1; break; case JEDEC_BYTE_PROGRAM_4BA: offs = writearr[1] << 24 | writearr[2] << 16 | writearr[3] << 8 | writearr[4]; /* Truncate to emu_chip_size. */ - offs %= emu_chip_size; + offs %= emu_data->emu_chip_size; if (writecnt < 6) { msg_perr("BYTE PROGRAM size too short!\n"); return 1; } - if (writecnt - 5 > emu_max_byteprogram_size) { + if (writecnt - 5 > emu_data->emu_max_byteprogram_size) { msg_perr("Max BYTE PROGRAM size exceeded!\n"); return 1; } memcpy(flashchip_contents + offs, writearr + 5, writecnt - 5); - emu_modified = 1; + emu_data->emu_modified = 1; break; case JEDEC_AAI_WORD_PROGRAM: - if (!emu_max_aai_size) + if (!emu_data->emu_max_aai_size) break; - if (!(emu_status & SPI_SR_AAI)) { + if (!(emu_data->emu_status & SPI_SR_AAI)) { if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) { msg_perr("Initial AAI WORD PROGRAM size too " "short!\n"); @@ -737,11 +755,11 @@ "long!\n"); return 1; } - emu_status |= SPI_SR_AAI; + emu_data->emu_status |= SPI_SR_AAI; aai_offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; /* Truncate to emu_chip_size. */ - aai_offs %= emu_chip_size; + aai_offs %= emu_data->emu_chip_size; memcpy(flashchip_contents + aai_offs, writearr + 4, 2); aai_offs += 2; } else { @@ -758,14 +776,14 @@ memcpy(flashchip_contents + aai_offs, writearr + 1, 2); aai_offs += 2; } - emu_modified = 1; + emu_data->emu_modified = 1; break; case JEDEC_WRDI: - if (emu_max_aai_size) - emu_status &= ~SPI_SR_AAI; + if (emu_data->emu_max_aai_size) + emu_data->emu_status &= ~SPI_SR_AAI; break; case JEDEC_SE: - if (!emu_jedec_se_size) + if (!emu_data->emu_jedec_se_size) break; if (writecnt != JEDEC_SE_OUTSIZE) { msg_perr("SECTOR ERASE 0x20 outsize invalid!\n"); @@ -776,14 +794,14 @@ return 1; } offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; - if (offs & (emu_jedec_se_size - 1)) + if (offs & (emu_data->emu_jedec_se_size - 1)) msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs); - offs &= ~(emu_jedec_se_size - 1); - memset(flashchip_contents + offs, 0xff, emu_jedec_se_size); - emu_modified = 1; + offs &= ~(emu_data->emu_jedec_se_size - 1); + memset(flashchip_contents + offs, 0xff, emu_data->emu_jedec_se_size); + emu_data->emu_modified = 1; break; case JEDEC_BE_52: - if (!emu_jedec_be_52_size) + if (!emu_data->emu_jedec_be_52_size) break; if (writecnt != JEDEC_BE_52_OUTSIZE) { msg_perr("BLOCK ERASE 0x52 outsize invalid!\n"); @@ -794,14 +812,14 @@ return 1; } offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; - if (offs & (emu_jedec_be_52_size - 1)) + if (offs & (emu_data->emu_jedec_be_52_size - 1)) msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs); - offs &= ~(emu_jedec_be_52_size - 1); - memset(flashchip_contents + offs, 0xff, emu_jedec_be_52_size); - emu_modified = 1; + offs &= ~(emu_data->emu_jedec_be_52_size - 1); + memset(flashchip_contents + offs, 0xff, emu_data->emu_jedec_be_52_size); + emu_data->emu_modified = 1; break; case JEDEC_BE_D8: - if (!emu_jedec_be_d8_size) + if (!emu_data->emu_jedec_be_d8_size) break; if (writecnt != JEDEC_BE_D8_OUTSIZE) { msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n"); @@ -812,14 +830,14 @@ return 1; } offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; - if (offs & (emu_jedec_be_d8_size - 1)) + if (offs & (emu_data->emu_jedec_be_d8_size - 1)) msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs); - offs &= ~(emu_jedec_be_d8_size - 1); - memset(flashchip_contents + offs, 0xff, emu_jedec_be_d8_size); - emu_modified = 1; + offs &= ~(emu_data->emu_jedec_be_d8_size - 1); + memset(flashchip_contents + offs, 0xff, emu_data->emu_jedec_be_d8_size); + emu_data->emu_modified = 1; break; case JEDEC_CE_60: - if (!emu_jedec_ce_60_size) + if (!emu_data->emu_jedec_ce_60_size) break; if (writecnt != JEDEC_CE_60_OUTSIZE) { msg_perr("CHIP ERASE 0x60 outsize invalid!\n"); @@ -831,11 +849,11 @@ } /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */ /* emu_jedec_ce_60_size is emu_chip_size. */ - memset(flashchip_contents, 0xff, emu_jedec_ce_60_size); - emu_modified = 1; + memset(flashchip_contents, 0xff, emu_data->emu_jedec_ce_60_size); + emu_data->emu_modified = 1; break; case JEDEC_CE_C7: - if (!emu_jedec_ce_c7_size) + if (!emu_data->emu_jedec_ce_c7_size) break; if (writecnt != JEDEC_CE_C7_OUTSIZE) { msg_perr("CHIP ERASE 0xc7 outsize invalid!\n"); @@ -847,11 +865,11 @@ } /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */ /* emu_jedec_ce_c7_size is emu_chip_size. */ - memset(flashchip_contents, 0xff, emu_jedec_ce_c7_size); - emu_modified = 1; + memset(flashchip_contents, 0xff, emu_data->emu_jedec_ce_c7_size); + emu_data->emu_modified = 1; break; case JEDEC_SFDP: - if (emu_chip != EMULATE_MACRONIX_MX25L6436) + if (emu_data->emu_chip != EMULATE_MACRONIX_MX25L6436) break; if (writecnt < 4) break; @@ -890,7 +908,7 @@ break; } if (writearr[0] != JEDEC_WREN && writearr[0] != JEDEC_EWSR) - emu_status &= ~SPI_SR_WEL; + emu_data->emu_status &= ~SPI_SR_WEL; return 0; } #endif @@ -901,6 +919,7 @@ unsigned char *readarr) { unsigned int i; + struct emu_data *emu_data = (struct emu_data *)flash->mst->par.data;
msg_pspew("%s:", __func__);
@@ -911,7 +930,7 @@ /* Response for unknown commands and missing chip is 0xff. */ memset(readarr, 0xff, readcnt); #if EMULATE_SPI_CHIP - switch (emu_chip) { + switch (emu_data->emu_chip) { case EMULATE_ST_M25P10_RES: case EMULATE_SST_SST25VF040_REMS: case EMULATE_SST_SST25VF032B: @@ -919,7 +938,7 @@ case EMULATE_WINBOND_W25Q128FV: case EMULATE_VARIABLE_SIZE: if (emulate_spi_chip_response(writecnt, readcnt, writearr, - readarr)) { + readarr, emu_data)) { msg_pdbg("Invalid command sent to flash chip!\n"); return 1; } @@ -945,9 +964,10 @@ int probe_variable_size(struct flashctx *flash) { unsigned int i; + const struct emu_data *emu_data = (struct emu_data *)flash->mst->par.data;
/* Skip the probing if we don't emulate this chip. */ - if (emu_chip != EMULATE_VARIABLE_SIZE) + if (emu_data->emu_chip != EMULATE_VARIABLE_SIZE) return 0;
/* @@ -961,7 +981,7 @@ * * Search "total_size * 1024" in code. */ - flash->chip->total_size = emu_chip_size / 1024; + flash->chip->total_size = emu_data->emu_chip_size / 1024; msg_cdbg("%s: set flash->total_size to %dK bytes.\n", __func__, flash->chip->total_size);
@@ -972,7 +992,7 @@ break;
eraser->eraseblocks[0].count = 1; - eraser->eraseblocks[0].size = emu_chip_size; + eraser->eraseblocks[0].size = emu_data->emu_chip_size; msg_cdbg("%s: eraser.size=%d, .count=%d\n", __func__, eraser->eraseblocks[0].size, eraser->eraseblocks[0].count);