1
0
mirror of https://review.coreboot.org/flashrom.git synced 2025-04-28 07:23:43 +02:00

dummyflasher.c: Factor out global state

Moves global state into spi_master data.

BUGS=b:140394053

Change-Id: I972b085875f1277d9ff33326669d2676a3bcd3aa
Signed-off-by: Lachlan Bishop <lxb@google.com>
Reviewed-on: https://review.coreboot.org/c/flashrom/+/45230
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Edward O'Callaghan <quasisec@chromium.org>
This commit is contained in:
Lachlan Bishop 2020-09-10 14:57:05 +10:00 committed by Edward O'Callaghan
parent 95d822e342
commit 3b8fe0f8e9

View File

@ -47,24 +47,27 @@ enum emu_chip {
EMULATE_WINBOND_W25Q128FV, EMULATE_WINBOND_W25Q128FV,
EMULATE_VARIABLE_SIZE, 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? */
uint8_t emu_status;
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;
};
#if EMULATE_SPI_CHIP
/* A legit complete SFDP table based on the MX25L6436E (rev. 1.8) datasheet. */ /* A legit complete SFDP table based on the MX25L6436E (rev. 1.8) datasheet. */
static const uint8_t sfdp_table[] = { static const uint8_t sfdp_table[] = {
0x53, 0x46, 0x44, 0x50, // @0x00: SFDP signature 0x53, 0x46, 0x44, 0x50, // @0x00: SFDP signature
@ -109,7 +112,7 @@ static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr ad
static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr); 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 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, .features = SPI_MASTER_4BA,
.max_data_read = MAX_DATA_READ_UNLIMITED, .max_data_read = MAX_DATA_READ_UNLIMITED,
.max_data_write = MAX_DATA_UNSPECIFIED, .max_data_write = MAX_DATA_UNSPECIFIED,
@ -137,12 +140,15 @@ static int dummy_shutdown(void *data)
{ {
msg_pspew("%s\n", __func__); msg_pspew("%s\n", __func__);
#if EMULATE_CHIP #if EMULATE_CHIP
if (emu_chip != EMULATE_NONE) { struct emu_data *emu_data = (struct emu_data *)data;
if (emu_persistent_image && emu_modified) { if (emu_data->emu_chip != EMULATE_NONE) {
msg_pdbg("Writing %s\n", emu_persistent_image); if (emu_data->emu_persistent_image && emu_data->emu_modified) {
write_buf_to_file(flashchip_contents, emu_chip_size, emu_persistent_image); msg_pdbg("Writing %s\n", emu_data->emu_persistent_image);
free(emu_persistent_image); write_buf_to_file(flashchip_contents,
emu_persistent_image = NULL; 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); free(flashchip_contents);
} }
@ -163,6 +169,14 @@ int dummy_init(void)
struct stat image_stat; struct stat image_stat;
#endif #endif
struct emu_data *data = calloc(1, sizeof(struct emu_data));
if (!data) {
msg_perr("Out of memory!\n");
return 1;
}
data->emu_chip = EMULATE_NONE;
spi_master_dummyflasher.data = data;
msg_pspew("%s\n", __func__); msg_pspew("%s\n", __func__);
bustext = extract_programmer_param("bus"); bustext = extract_programmer_param("bus");
@ -215,8 +229,8 @@ int dummy_init(void)
free(tmp); free(tmp);
return 1; return 1;
} }
spi_blacklist_size = i / 2; data->spi_blacklist_size = i / 2;
for (i = 0; i < spi_blacklist_size * 2; i++) { for (i = 0; i < data->spi_blacklist_size * 2; i++) {
if (!isxdigit((unsigned char)tmp[i])) { if (!isxdigit((unsigned char)tmp[i])) {
msg_perr("Invalid char \"%c\" in SPI command " msg_perr("Invalid char \"%c\" in SPI command "
"blacklist\n", tmp[i]); "blacklist\n", tmp[i]);
@ -224,18 +238,18 @@ int dummy_init(void)
return 1; return 1;
} }
} }
for (i = 0; i < spi_blacklist_size; i++) { for (i = 0; i < data->spi_blacklist_size; i++) {
unsigned int tmp2; unsigned int tmp2;
/* SCNx8 is apparently not supported by MSVC (and thus /* SCNx8 is apparently not supported by MSVC (and thus
* MinGW), so work around it with an extra variable * MinGW), so work around it with an extra variable
*/ */
sscanf(tmp + i * 2, "%2x", &tmp2); sscanf(tmp + i * 2, "%2x", &tmp2);
spi_blacklist[i] = (uint8_t)tmp2; data->spi_blacklist[i] = (uint8_t)tmp2;
} }
msg_pdbg("SPI blacklist is "); msg_pdbg("SPI blacklist is ");
for (i = 0; i < spi_blacklist_size; i++) for (i = 0; i < data->spi_blacklist_size; i++)
msg_pdbg("%02x ", spi_blacklist[i]); msg_pdbg("%02x ", data->spi_blacklist[i]);
msg_pdbg(", size %u\n", spi_blacklist_size); msg_pdbg(", size %u\n", data->spi_blacklist_size);
} }
free(tmp); free(tmp);
@ -251,8 +265,8 @@ int dummy_init(void)
free(tmp); free(tmp);
return 1; return 1;
} }
spi_ignorelist_size = i / 2; data->spi_ignorelist_size = i / 2;
for (i = 0; i < spi_ignorelist_size * 2; i++) { for (i = 0; i < data->spi_ignorelist_size * 2; i++) {
if (!isxdigit((unsigned char)tmp[i])) { if (!isxdigit((unsigned char)tmp[i])) {
msg_perr("Invalid char \"%c\" in SPI command " msg_perr("Invalid char \"%c\" in SPI command "
"ignorelist\n", tmp[i]); "ignorelist\n", tmp[i]);
@ -260,18 +274,18 @@ int dummy_init(void)
return 1; return 1;
} }
} }
for (i = 0; i < spi_ignorelist_size; i++) { for (i = 0; i < data->spi_ignorelist_size; i++) {
unsigned int tmp2; unsigned int tmp2;
/* SCNx8 is apparently not supported by MSVC (and thus /* SCNx8 is apparently not supported by MSVC (and thus
* MinGW), so work around it with an extra variable * MinGW), so work around it with an extra variable
*/ */
sscanf(tmp + i * 2, "%2x", &tmp2); sscanf(tmp + i * 2, "%2x", &tmp2);
spi_ignorelist[i] = (uint8_t)tmp2; data->spi_ignorelist[i] = (uint8_t)tmp2;
} }
msg_pdbg("SPI ignorelist is "); msg_pdbg("SPI ignorelist is ");
for (i = 0; i < spi_ignorelist_size; i++) for (i = 0; i < data->spi_ignorelist_size; i++)
msg_pdbg("%02x ", spi_ignorelist[i]); msg_pdbg("%02x ", data->spi_ignorelist[i]);
msg_pdbg(", size %u\n", spi_ignorelist_size); msg_pdbg(", size %u\n", data->spi_ignorelist_size);
} }
free(tmp); free(tmp);
@ -301,67 +315,67 @@ int dummy_init(void)
} }
#if EMULATE_SPI_CHIP #if EMULATE_SPI_CHIP
if (!strcmp(tmp, "M25P10.RES")) { if (!strcmp(tmp, "M25P10.RES")) {
emu_chip = EMULATE_ST_M25P10_RES; data->emu_chip = EMULATE_ST_M25P10_RES;
emu_chip_size = 128 * 1024; data->emu_chip_size = 128 * 1024;
emu_max_byteprogram_size = 128; data->emu_max_byteprogram_size = 128;
emu_max_aai_size = 0; data->emu_max_aai_size = 0;
emu_jedec_se_size = 0; data->emu_jedec_se_size = 0;
emu_jedec_be_52_size = 0; data->emu_jedec_be_52_size = 0;
emu_jedec_be_d8_size = 32 * 1024; data->emu_jedec_be_d8_size = 32 * 1024;
emu_jedec_ce_60_size = 0; data->emu_jedec_ce_60_size = 0;
emu_jedec_ce_c7_size = emu_chip_size; data->emu_jedec_ce_c7_size = data->emu_chip_size;
msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page " msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
"write)\n"); "write)\n");
} }
if (!strcmp(tmp, "SST25VF040.REMS")) { if (!strcmp(tmp, "SST25VF040.REMS")) {
emu_chip = EMULATE_SST_SST25VF040_REMS; data->emu_chip = EMULATE_SST_SST25VF040_REMS;
emu_chip_size = 512 * 1024; data->emu_chip_size = 512 * 1024;
emu_max_byteprogram_size = 1; data->emu_max_byteprogram_size = 1;
emu_max_aai_size = 0; data->emu_max_aai_size = 0;
emu_jedec_se_size = 4 * 1024; data->emu_jedec_se_size = 4 * 1024;
emu_jedec_be_52_size = 32 * 1024; data->emu_jedec_be_52_size = 32 * 1024;
emu_jedec_be_d8_size = 0; data->emu_jedec_be_d8_size = 0;
emu_jedec_ce_60_size = emu_chip_size; data->emu_jedec_ce_60_size = data->emu_chip_size;
emu_jedec_ce_c7_size = 0; data->emu_jedec_ce_c7_size = 0;
msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, " msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
"byte write)\n"); "byte write)\n");
} }
if (!strcmp(tmp, "SST25VF032B")) { if (!strcmp(tmp, "SST25VF032B")) {
emu_chip = EMULATE_SST_SST25VF032B; data->emu_chip = EMULATE_SST_SST25VF032B;
emu_chip_size = 4 * 1024 * 1024; data->emu_chip_size = 4 * 1024 * 1024;
emu_max_byteprogram_size = 1; data->emu_max_byteprogram_size = 1;
emu_max_aai_size = 2; data->emu_max_aai_size = 2;
emu_jedec_se_size = 4 * 1024; data->emu_jedec_se_size = 4 * 1024;
emu_jedec_be_52_size = 32 * 1024; data->emu_jedec_be_52_size = 32 * 1024;
emu_jedec_be_d8_size = 64 * 1024; data->emu_jedec_be_d8_size = 64 * 1024;
emu_jedec_ce_60_size = emu_chip_size; data->emu_jedec_ce_60_size = data->emu_chip_size;
emu_jedec_ce_c7_size = emu_chip_size; data->emu_jedec_ce_c7_size = data->emu_chip_size;
msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI " msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
"write)\n"); "write)\n");
} }
if (!strcmp(tmp, "MX25L6436")) { if (!strcmp(tmp, "MX25L6436")) {
emu_chip = EMULATE_MACRONIX_MX25L6436; data->emu_chip = EMULATE_MACRONIX_MX25L6436;
emu_chip_size = 8 * 1024 * 1024; data->emu_chip_size = 8 * 1024 * 1024;
emu_max_byteprogram_size = 256; data->emu_max_byteprogram_size = 256;
emu_max_aai_size = 0; data->emu_max_aai_size = 0;
emu_jedec_se_size = 4 * 1024; data->emu_jedec_se_size = 4 * 1024;
emu_jedec_be_52_size = 32 * 1024; data->emu_jedec_be_52_size = 32 * 1024;
emu_jedec_be_d8_size = 64 * 1024; data->emu_jedec_be_d8_size = 64 * 1024;
emu_jedec_ce_60_size = emu_chip_size; data->emu_jedec_ce_60_size = data->emu_chip_size;
emu_jedec_ce_c7_size = emu_chip_size; data->emu_jedec_ce_c7_size = data->emu_chip_size;
msg_pdbg("Emulating Macronix MX25L6436 SPI flash chip (RDID, " msg_pdbg("Emulating Macronix MX25L6436 SPI flash chip (RDID, "
"SFDP)\n"); "SFDP)\n");
} }
if (!strcmp(tmp, "W25Q128FV")) { if (!strcmp(tmp, "W25Q128FV")) {
emu_chip = EMULATE_WINBOND_W25Q128FV; data->emu_chip = EMULATE_WINBOND_W25Q128FV;
emu_chip_size = 16 * 1024 * 1024; data->emu_chip_size = 16 * 1024 * 1024;
emu_max_byteprogram_size = 256; data->emu_max_byteprogram_size = 256;
emu_max_aai_size = 0; data->emu_max_aai_size = 0;
emu_jedec_se_size = 4 * 1024; data->emu_jedec_se_size = 4 * 1024;
emu_jedec_be_52_size = 32 * 1024; data->emu_jedec_be_52_size = 32 * 1024;
emu_jedec_be_d8_size = 64 * 1024; data->emu_jedec_be_d8_size = 64 * 1024;
emu_jedec_ce_60_size = emu_chip_size; data->emu_jedec_ce_60_size = data->emu_chip_size;
emu_jedec_ce_c7_size = emu_chip_size; data->emu_jedec_ce_c7_size = data->emu_chip_size;
msg_pdbg("Emulating Winbond W25Q128FV SPI flash chip (RDID)\n"); msg_pdbg("Emulating Winbond W25Q128FV SPI flash chip (RDID)\n");
} }
@ -369,26 +383,26 @@ int dummy_init(void)
* flashrom -p dummy:emulate=VARIABLE_SIZE,size=4194304 * flashrom -p dummy:emulate=VARIABLE_SIZE,size=4194304
*/ */
if (!strcmp(tmp, "VARIABLE_SIZE")) { if (!strcmp(tmp, "VARIABLE_SIZE")) {
emu_chip = EMULATE_VARIABLE_SIZE; data->emu_chip = EMULATE_VARIABLE_SIZE;
emu_chip_size = size; data->emu_chip_size = size;
emu_max_byteprogram_size = 256; data->emu_max_byteprogram_size = 256;
emu_max_aai_size = 0; data->emu_max_aai_size = 0;
emu_jedec_se_size = 4 * 1024; data->emu_jedec_se_size = 4 * 1024;
emu_jedec_be_52_size = 32 * 1024; data->emu_jedec_be_52_size = 32 * 1024;
emu_jedec_be_d8_size = 64 * 1024; data->emu_jedec_be_d8_size = 64 * 1024;
emu_jedec_ce_60_size = emu_chip_size; data->emu_jedec_ce_60_size = data->emu_chip_size;
emu_jedec_ce_c7_size = emu_chip_size; data->emu_jedec_ce_c7_size = data->emu_chip_size;
msg_pdbg("Emulating generic SPI flash chip (size=%d bytes)\n", msg_pdbg("Emulating generic SPI flash chip (size=%d bytes)\n",
emu_chip_size); data->emu_chip_size);
} }
#endif #endif
if (emu_chip == EMULATE_NONE) { if (data->emu_chip == EMULATE_NONE) {
msg_perr("Invalid chip specified for emulation: %s\n", tmp); msg_perr("Invalid chip specified for emulation: %s\n", tmp);
free(tmp); free(tmp);
return 1; return 1;
} }
free(tmp); free(tmp);
flashchip_contents = malloc(emu_chip_size); flashchip_contents = malloc(data->emu_chip_size);
if (!flashchip_contents) { if (!flashchip_contents) {
msg_perr("Out of memory!\n"); msg_perr("Out of memory!\n");
return 1; return 1;
@ -399,7 +413,7 @@ int dummy_init(void)
if (status) { if (status) {
char *endptr; char *endptr;
errno = 0; errno = 0;
emu_status = strtoul(status, &endptr, 0); data->emu_status = strtoul(status, &endptr, 0);
free(status); free(status);
if (errno != 0 || status == endptr) { if (errno != 0 || status == endptr) {
msg_perr("Error: initial status register specified, " msg_perr("Error: initial status register specified, "
@ -407,30 +421,30 @@ int dummy_init(void)
return 1; return 1;
} }
msg_pdbg("Initial status register is set to 0x%02x.\n", msg_pdbg("Initial status register is set to 0x%02x.\n",
emu_status); data->emu_status);
} }
#endif #endif
msg_pdbg("Filling fake flash chip with 0xff, size %i\n", emu_chip_size); msg_pdbg("Filling fake flash chip with 0xff, size %i\n", data->emu_chip_size);
memset(flashchip_contents, 0xff, emu_chip_size); memset(flashchip_contents, 0xff, data->emu_chip_size);
/* Will be freed by shutdown function if necessary. */ /* Will be freed by shutdown function if necessary. */
emu_persistent_image = extract_programmer_param("image"); data->emu_persistent_image = extract_programmer_param("image");
if (!emu_persistent_image) { if (!data->emu_persistent_image) {
/* Nothing else to do. */ /* Nothing else to do. */
goto dummy_init_out; goto dummy_init_out;
} }
/* We will silently (in default verbosity) ignore the file if it does not exist (yet) or the size does /* We will silently (in default verbosity) ignore the file if it does not exist (yet) or the size does
* not match the emulated chip. */ * 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 ", msg_pdbg("Found persistent image %s, %jd B ",
emu_persistent_image, (intmax_t)image_stat.st_size); data->emu_persistent_image, (intmax_t)image_stat.st_size);
if ((uintmax_t)image_stat.st_size == emu_chip_size) { if ((uintmax_t)image_stat.st_size == data->emu_chip_size) {
msg_pdbg("matches.\n"); msg_pdbg("matches.\n");
msg_pdbg("Reading %s\n", emu_persistent_image); msg_pdbg("Reading %s\n", data->emu_persistent_image);
if (read_buf_from_file(flashchip_contents, emu_chip_size, if (read_buf_from_file(flashchip_contents, data->emu_chip_size,
emu_persistent_image)) { data->emu_persistent_image)) {
msg_perr("Unable to read %s\n", emu_persistent_image); msg_perr("Unable to read %s\n", data->emu_persistent_image);
free(flashchip_contents); free(flashchip_contents);
return 1; return 1;
} }
@ -441,8 +455,9 @@ int dummy_init(void)
#endif #endif
dummy_init_out: dummy_init_out:
if (register_shutdown(dummy_shutdown, NULL)) { if (register_shutdown(dummy_shutdown, data)) {
free(flashchip_contents); free(flashchip_contents);
free(data);
return 1; return 1;
} }
if (dummy_buses_supported & (BUS_PARALLEL | BUS_LPC | BUS_FWH)) if (dummy_buses_supported & (BUS_PARALLEL | BUS_LPC | BUS_FWH))
@ -521,7 +536,8 @@ static void dummy_chip_readn(const struct flashctx *flash, uint8_t *buf, const c
static int emulate_spi_chip_response(unsigned int writecnt, static int emulate_spi_chip_response(unsigned int writecnt,
unsigned int readcnt, unsigned int readcnt,
const unsigned char *writearr, const unsigned char *writearr,
unsigned char *readarr) unsigned char *readarr,
struct emu_data *data)
{ {
unsigned int offs, i, toread; unsigned int offs, i, toread;
static int unsigned aai_offs; static int unsigned aai_offs;
@ -535,17 +551,17 @@ static int emulate_spi_chip_response(unsigned int writecnt,
return 1; return 1;
} }
/* spi_blacklist has precedence over spi_ignorelist. */ /* spi_blacklist has precedence over spi_ignorelist. */
for (i = 0; i < spi_blacklist_size; i++) { for (i = 0; i < data->spi_blacklist_size; i++) {
if (writearr[0] == spi_blacklist[i]) { if (writearr[0] == data->spi_blacklist[i]) {
msg_pdbg("Refusing blacklisted SPI command 0x%02x\n", msg_pdbg("Refusing blacklisted SPI command 0x%02x\n",
spi_blacklist[i]); data->spi_blacklist[i]);
return SPI_INVALID_OPCODE; return SPI_INVALID_OPCODE;
} }
} }
for (i = 0; i < spi_ignorelist_size; i++) { for (i = 0; i < data->spi_ignorelist_size; i++) {
if (writearr[0] == spi_ignorelist[i]) { if (writearr[0] == data->spi_ignorelist[i]) {
msg_cdbg("Ignoring ignorelisted SPI command 0x%02x\n", msg_cdbg("Ignoring ignorelisted SPI command 0x%02x\n",
spi_ignorelist[i]); data->spi_ignorelist[i]);
/* Return success because the command does not fail, /* Return success because the command does not fail,
* it is simply ignored. * it is simply ignored.
*/ */
@ -553,7 +569,7 @@ static int emulate_spi_chip_response(unsigned int writecnt,
} }
} }
if (emu_max_aai_size && (emu_status & SPI_SR_AAI)) { if (data->emu_max_aai_size && (data->emu_status & SPI_SR_AAI)) {
if (writearr[0] != JEDEC_AAI_WORD_PROGRAM && if (writearr[0] != JEDEC_AAI_WORD_PROGRAM &&
writearr[0] != JEDEC_WRDI && writearr[0] != JEDEC_WRDI &&
writearr[0] != JEDEC_RDSR) { writearr[0] != JEDEC_RDSR) {
@ -570,7 +586,7 @@ static int emulate_spi_chip_response(unsigned int writecnt,
/* offs calculation is only needed for SST chips which treat RES like REMS. */ /* offs calculation is only needed for SST chips which treat RES like REMS. */
offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
offs += writecnt - JEDEC_REMS_OUTSIZE; offs += writecnt - JEDEC_REMS_OUTSIZE;
switch (emu_chip) { switch (data->emu_chip) {
case EMULATE_ST_M25P10_RES: case EMULATE_ST_M25P10_RES:
if (readcnt > 0) if (readcnt > 0)
memset(readarr, 0x10, readcnt); memset(readarr, 0x10, readcnt);
@ -601,7 +617,7 @@ static int emulate_spi_chip_response(unsigned int writecnt,
break; break;
offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
offs += writecnt - JEDEC_REMS_OUTSIZE; offs += writecnt - JEDEC_REMS_OUTSIZE;
switch (emu_chip) { switch (data->emu_chip) {
case EMULATE_SST_SST25VF040_REMS: case EMULATE_SST_SST25VF040_REMS:
for (i = 0; i < readcnt; i++) for (i = 0; i < readcnt; i++)
readarr[i] = sst25vf040_rems_response[(offs + i) % 2]; readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
@ -623,7 +639,7 @@ static int emulate_spi_chip_response(unsigned int writecnt,
} }
break; break;
case JEDEC_RDID: case JEDEC_RDID:
switch (emu_chip) { switch (data->emu_chip) {
case EMULATE_SST_SST25VF032B: case EMULATE_SST_SST25VF032B:
if (readcnt > 0) if (readcnt > 0)
readarr[0] = 0xbf; readarr[0] = 0xbf;
@ -663,70 +679,70 @@ static int emulate_spi_chip_response(unsigned int writecnt,
} }
break; break;
case JEDEC_RDSR: case JEDEC_RDSR:
memset(readarr, emu_status, readcnt); memset(readarr, data->emu_status, readcnt);
break; break;
/* FIXME: this should be chip-specific. */ /* FIXME: this should be chip-specific. */
case JEDEC_EWSR: case JEDEC_EWSR:
case JEDEC_WREN: case JEDEC_WREN:
emu_status |= SPI_SR_WEL; data->emu_status |= SPI_SR_WEL;
break; break;
case JEDEC_WRSR: case JEDEC_WRSR:
if (!(emu_status & SPI_SR_WEL)) { if (!(data->emu_status & SPI_SR_WEL)) {
msg_perr("WRSR attempted, but WEL is 0!\n"); msg_perr("WRSR attempted, but WEL is 0!\n");
break; break;
} }
/* FIXME: add some reasonable simulation of the busy flag */ /* FIXME: add some reasonable simulation of the busy flag */
emu_status = writearr[1] & ~SPI_SR_WIP; data->emu_status = writearr[1] & ~SPI_SR_WIP;
msg_pdbg2("WRSR wrote 0x%02x.\n", emu_status); msg_pdbg2("WRSR wrote 0x%02x.\n", data->emu_status);
break; break;
case JEDEC_READ: case JEDEC_READ:
offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
/* Truncate to emu_chip_size. */ /* Truncate to emu_chip_size. */
offs %= emu_chip_size; offs %= data->emu_chip_size;
if (readcnt > 0) if (readcnt > 0)
memcpy(readarr, flashchip_contents + offs, readcnt); memcpy(readarr, flashchip_contents + offs, readcnt);
break; break;
case JEDEC_READ_4BA: case JEDEC_READ_4BA:
offs = writearr[1] << 24 | writearr[2] << 16 | writearr[3] << 8 | writearr[4]; offs = writearr[1] << 24 | writearr[2] << 16 | writearr[3] << 8 | writearr[4];
/* Truncate to emu_chip_size. */ /* Truncate to emu_chip_size. */
offs %= emu_chip_size; offs %= data->emu_chip_size;
if (readcnt > 0) if (readcnt > 0)
memcpy(readarr, flashchip_contents + offs, readcnt); memcpy(readarr, flashchip_contents + offs, readcnt);
break; break;
case JEDEC_BYTE_PROGRAM: case JEDEC_BYTE_PROGRAM:
offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
/* Truncate to emu_chip_size. */ /* Truncate to emu_chip_size. */
offs %= emu_chip_size; offs %= data->emu_chip_size;
if (writecnt < 5) { if (writecnt < 5) {
msg_perr("BYTE PROGRAM size too short!\n"); msg_perr("BYTE PROGRAM size too short!\n");
return 1; return 1;
} }
if (writecnt - 4 > emu_max_byteprogram_size) { if (writecnt - 4 > data->emu_max_byteprogram_size) {
msg_perr("Max BYTE PROGRAM size exceeded!\n"); msg_perr("Max BYTE PROGRAM size exceeded!\n");
return 1; return 1;
} }
memcpy(flashchip_contents + offs, writearr + 4, writecnt - 4); memcpy(flashchip_contents + offs, writearr + 4, writecnt - 4);
emu_modified = 1; data->emu_modified = 1;
break; break;
case JEDEC_BYTE_PROGRAM_4BA: case JEDEC_BYTE_PROGRAM_4BA:
offs = writearr[1] << 24 | writearr[2] << 16 | writearr[3] << 8 | writearr[4]; offs = writearr[1] << 24 | writearr[2] << 16 | writearr[3] << 8 | writearr[4];
/* Truncate to emu_chip_size. */ /* Truncate to emu_chip_size. */
offs %= emu_chip_size; offs %= data->emu_chip_size;
if (writecnt < 6) { if (writecnt < 6) {
msg_perr("BYTE PROGRAM size too short!\n"); msg_perr("BYTE PROGRAM size too short!\n");
return 1; return 1;
} }
if (writecnt - 5 > emu_max_byteprogram_size) { if (writecnt - 5 > data->emu_max_byteprogram_size) {
msg_perr("Max BYTE PROGRAM size exceeded!\n"); msg_perr("Max BYTE PROGRAM size exceeded!\n");
return 1; return 1;
} }
memcpy(flashchip_contents + offs, writearr + 5, writecnt - 5); memcpy(flashchip_contents + offs, writearr + 5, writecnt - 5);
emu_modified = 1; data->emu_modified = 1;
break; break;
case JEDEC_AAI_WORD_PROGRAM: case JEDEC_AAI_WORD_PROGRAM:
if (!emu_max_aai_size) if (!data->emu_max_aai_size)
break; break;
if (!(emu_status & SPI_SR_AAI)) { if (!(data->emu_status & SPI_SR_AAI)) {
if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) { if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
msg_perr("Initial AAI WORD PROGRAM size too " msg_perr("Initial AAI WORD PROGRAM size too "
"short!\n"); "short!\n");
@ -737,11 +753,11 @@ static int emulate_spi_chip_response(unsigned int writecnt,
"long!\n"); "long!\n");
return 1; return 1;
} }
emu_status |= SPI_SR_AAI; data->emu_status |= SPI_SR_AAI;
aai_offs = writearr[1] << 16 | writearr[2] << 8 | aai_offs = writearr[1] << 16 | writearr[2] << 8 |
writearr[3]; writearr[3];
/* Truncate to emu_chip_size. */ /* Truncate to emu_chip_size. */
aai_offs %= emu_chip_size; aai_offs %= data->emu_chip_size;
memcpy(flashchip_contents + aai_offs, writearr + 4, 2); memcpy(flashchip_contents + aai_offs, writearr + 4, 2);
aai_offs += 2; aai_offs += 2;
} else { } else {
@ -758,14 +774,14 @@ static int emulate_spi_chip_response(unsigned int writecnt,
memcpy(flashchip_contents + aai_offs, writearr + 1, 2); memcpy(flashchip_contents + aai_offs, writearr + 1, 2);
aai_offs += 2; aai_offs += 2;
} }
emu_modified = 1; data->emu_modified = 1;
break; break;
case JEDEC_WRDI: case JEDEC_WRDI:
if (emu_max_aai_size) if (data->emu_max_aai_size)
emu_status &= ~SPI_SR_AAI; data->emu_status &= ~SPI_SR_AAI;
break; break;
case JEDEC_SE: case JEDEC_SE:
if (!emu_jedec_se_size) if (!data->emu_jedec_se_size)
break; break;
if (writecnt != JEDEC_SE_OUTSIZE) { if (writecnt != JEDEC_SE_OUTSIZE) {
msg_perr("SECTOR ERASE 0x20 outsize invalid!\n"); msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
@ -776,14 +792,14 @@ static int emulate_spi_chip_response(unsigned int writecnt,
return 1; return 1;
} }
offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
if (offs & (emu_jedec_se_size - 1)) if (offs & (data->emu_jedec_se_size - 1))
msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs); msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
offs &= ~(emu_jedec_se_size - 1); offs &= ~(data->emu_jedec_se_size - 1);
memset(flashchip_contents + offs, 0xff, emu_jedec_se_size); memset(flashchip_contents + offs, 0xff, data->emu_jedec_se_size);
emu_modified = 1; data->emu_modified = 1;
break; break;
case JEDEC_BE_52: case JEDEC_BE_52:
if (!emu_jedec_be_52_size) if (!data->emu_jedec_be_52_size)
break; break;
if (writecnt != JEDEC_BE_52_OUTSIZE) { if (writecnt != JEDEC_BE_52_OUTSIZE) {
msg_perr("BLOCK ERASE 0x52 outsize invalid!\n"); msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
@ -794,14 +810,14 @@ static int emulate_spi_chip_response(unsigned int writecnt,
return 1; return 1;
} }
offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
if (offs & (emu_jedec_be_52_size - 1)) if (offs & (data->emu_jedec_be_52_size - 1))
msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs); msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
offs &= ~(emu_jedec_be_52_size - 1); offs &= ~(data->emu_jedec_be_52_size - 1);
memset(flashchip_contents + offs, 0xff, emu_jedec_be_52_size); memset(flashchip_contents + offs, 0xff, data->emu_jedec_be_52_size);
emu_modified = 1; data->emu_modified = 1;
break; break;
case JEDEC_BE_D8: case JEDEC_BE_D8:
if (!emu_jedec_be_d8_size) if (!data->emu_jedec_be_d8_size)
break; break;
if (writecnt != JEDEC_BE_D8_OUTSIZE) { if (writecnt != JEDEC_BE_D8_OUTSIZE) {
msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n"); msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
@ -812,14 +828,14 @@ static int emulate_spi_chip_response(unsigned int writecnt,
return 1; return 1;
} }
offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
if (offs & (emu_jedec_be_d8_size - 1)) if (offs & (data->emu_jedec_be_d8_size - 1))
msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs); msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
offs &= ~(emu_jedec_be_d8_size - 1); offs &= ~(data->emu_jedec_be_d8_size - 1);
memset(flashchip_contents + offs, 0xff, emu_jedec_be_d8_size); memset(flashchip_contents + offs, 0xff, data->emu_jedec_be_d8_size);
emu_modified = 1; data->emu_modified = 1;
break; break;
case JEDEC_CE_60: case JEDEC_CE_60:
if (!emu_jedec_ce_60_size) if (!data->emu_jedec_ce_60_size)
break; break;
if (writecnt != JEDEC_CE_60_OUTSIZE) { if (writecnt != JEDEC_CE_60_OUTSIZE) {
msg_perr("CHIP ERASE 0x60 outsize invalid!\n"); msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
@ -831,11 +847,11 @@ static int emulate_spi_chip_response(unsigned int writecnt,
} }
/* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */ /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
/* emu_jedec_ce_60_size is emu_chip_size. */ /* emu_jedec_ce_60_size is emu_chip_size. */
memset(flashchip_contents, 0xff, emu_jedec_ce_60_size); memset(flashchip_contents, 0xff, data->emu_jedec_ce_60_size);
emu_modified = 1; data->emu_modified = 1;
break; break;
case JEDEC_CE_C7: case JEDEC_CE_C7:
if (!emu_jedec_ce_c7_size) if (!data->emu_jedec_ce_c7_size)
break; break;
if (writecnt != JEDEC_CE_C7_OUTSIZE) { if (writecnt != JEDEC_CE_C7_OUTSIZE) {
msg_perr("CHIP ERASE 0xc7 outsize invalid!\n"); msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
@ -847,11 +863,11 @@ static int emulate_spi_chip_response(unsigned int writecnt,
} }
/* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */ /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
/* emu_jedec_ce_c7_size is emu_chip_size. */ /* emu_jedec_ce_c7_size is emu_chip_size. */
memset(flashchip_contents, 0xff, emu_jedec_ce_c7_size); memset(flashchip_contents, 0xff, data->emu_jedec_ce_c7_size);
emu_modified = 1; data->emu_modified = 1;
break; break;
case JEDEC_SFDP: case JEDEC_SFDP:
if (emu_chip != EMULATE_MACRONIX_MX25L6436) if (data->emu_chip != EMULATE_MACRONIX_MX25L6436)
break; break;
if (writecnt < 4) if (writecnt < 4)
break; break;
@ -890,17 +906,32 @@ static int emulate_spi_chip_response(unsigned int writecnt,
break; break;
} }
if (writearr[0] != JEDEC_WREN && writearr[0] != JEDEC_EWSR) if (writearr[0] != JEDEC_WREN && writearr[0] != JEDEC_EWSR)
emu_status &= ~SPI_SR_WEL; data->emu_status &= ~SPI_SR_WEL;
return 0; return 0;
} }
#endif #endif
static struct emu_data* get_data_from_context(const struct flashctx *flash)
{
if (dummy_buses_supported & (BUS_PARALLEL | BUS_LPC | BUS_FWH))
return (struct emu_data *)flash->mst->par.data;
else if (dummy_buses_supported & BUS_SPI)
return (struct emu_data *)flash->mst->spi.data;
return NULL; /* buses was set to BUS_NONE. */
}
static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt, static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
unsigned int readcnt, unsigned int readcnt,
const unsigned char *writearr, const unsigned char *writearr,
unsigned char *readarr) unsigned char *readarr)
{ {
unsigned int i; unsigned int i;
struct emu_data *emu_data = get_data_from_context(flash);
if (!emu_data) {
msg_perr("No data in flash context!\n");
return 1;
}
msg_pspew("%s:", __func__); msg_pspew("%s:", __func__);
@ -911,7 +942,7 @@ static int dummy_spi_send_command(const struct flashctx *flash, unsigned int wri
/* Response for unknown commands and missing chip is 0xff. */ /* Response for unknown commands and missing chip is 0xff. */
memset(readarr, 0xff, readcnt); memset(readarr, 0xff, readcnt);
#if EMULATE_SPI_CHIP #if EMULATE_SPI_CHIP
switch (emu_chip) { switch (emu_data->emu_chip) {
case EMULATE_ST_M25P10_RES: case EMULATE_ST_M25P10_RES:
case EMULATE_SST_SST25VF040_REMS: case EMULATE_SST_SST25VF040_REMS:
case EMULATE_SST_SST25VF032B: case EMULATE_SST_SST25VF032B:
@ -919,7 +950,7 @@ static int dummy_spi_send_command(const struct flashctx *flash, unsigned int wri
case EMULATE_WINBOND_W25Q128FV: case EMULATE_WINBOND_W25Q128FV:
case EMULATE_VARIABLE_SIZE: case EMULATE_VARIABLE_SIZE:
if (emulate_spi_chip_response(writecnt, readcnt, writearr, if (emulate_spi_chip_response(writecnt, readcnt, writearr,
readarr)) { readarr, emu_data)) {
msg_pdbg("Invalid command sent to flash chip!\n"); msg_pdbg("Invalid command sent to flash chip!\n");
return 1; return 1;
} }
@ -945,9 +976,14 @@ static int dummy_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsig
int probe_variable_size(struct flashctx *flash) int probe_variable_size(struct flashctx *flash)
{ {
unsigned int i; unsigned int i;
const struct emu_data *emu_data = get_data_from_context(flash);
if (!emu_data) {
msg_perr("No data in flash context!\n");
return 0;
}
/* Skip the probing if we don't emulate this chip. */ /* 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; return 0;
/* /*
@ -961,7 +997,7 @@ int probe_variable_size(struct flashctx *flash)
* *
* Search "total_size * 1024" in code. * 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__, msg_cdbg("%s: set flash->total_size to %dK bytes.\n", __func__,
flash->chip->total_size); flash->chip->total_size);
@ -972,7 +1008,7 @@ int probe_variable_size(struct flashctx *flash)
break; break;
eraser->eraseblocks[0].count = 1; 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", msg_cdbg("%s: eraser.size=%d, .count=%d\n",
__func__, eraser->eraseblocks[0].size, __func__, eraser->eraseblocks[0].size,
eraser->eraseblocks[0].count); eraser->eraseblocks[0].count);