mirror of
https://review.coreboot.org/flashrom.git
synced 2025-04-27 23:22:37 +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:
parent
95d822e342
commit
3b8fe0f8e9
372
dummyflasher.c
372
dummyflasher.c
@ -47,24 +47,27 @@ enum emu_chip {
|
||||
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? */
|
||||
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. */
|
||||
static const uint8_t sfdp_table[] = {
|
||||
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 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 +140,15 @@ static int dummy_shutdown(void *data)
|
||||
{
|
||||
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 +169,14 @@ int dummy_init(void)
|
||||
struct stat image_stat;
|
||||
#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__);
|
||||
|
||||
bustext = extract_programmer_param("bus");
|
||||
@ -215,8 +229,8 @@ int dummy_init(void)
|
||||
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 +238,18 @@ int dummy_init(void)
|
||||
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 +265,8 @@ int dummy_init(void)
|
||||
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 +274,18 @@ int dummy_init(void)
|
||||
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 +315,67 @@ int dummy_init(void)
|
||||
}
|
||||
#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 +383,26 @@ int dummy_init(void)
|
||||
* 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 +413,7 @@ int dummy_init(void)
|
||||
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 +421,30 @@ int dummy_init(void)
|
||||
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,8 +455,9 @@ int dummy_init(void)
|
||||
#endif
|
||||
|
||||
dummy_init_out:
|
||||
if (register_shutdown(dummy_shutdown, NULL)) {
|
||||
if (register_shutdown(dummy_shutdown, data)) {
|
||||
free(flashchip_contents);
|
||||
free(data);
|
||||
return 1;
|
||||
}
|
||||
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,
|
||||
unsigned int readcnt,
|
||||
const unsigned char *writearr,
|
||||
unsigned char *readarr)
|
||||
unsigned char *readarr,
|
||||
struct emu_data *data)
|
||||
{
|
||||
unsigned int offs, i, toread;
|
||||
static int unsigned aai_offs;
|
||||
@ -535,17 +551,17 @@ static int emulate_spi_chip_response(unsigned int writecnt,
|
||||
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 < data->spi_blacklist_size; i++) {
|
||||
if (writearr[0] == data->spi_blacklist[i]) {
|
||||
msg_pdbg("Refusing blacklisted SPI command 0x%02x\n",
|
||||
spi_blacklist[i]);
|
||||
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 < data->spi_ignorelist_size; i++) {
|
||||
if (writearr[0] == data->spi_ignorelist[i]) {
|
||||
msg_cdbg("Ignoring ignorelisted SPI command 0x%02x\n",
|
||||
spi_ignorelist[i]);
|
||||
data->spi_ignorelist[i]);
|
||||
/* Return success because the command does not fail,
|
||||
* 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 &&
|
||||
writearr[0] != JEDEC_WRDI &&
|
||||
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 = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
|
||||
offs += writecnt - JEDEC_REMS_OUTSIZE;
|
||||
switch (emu_chip) {
|
||||
switch (data->emu_chip) {
|
||||
case EMULATE_ST_M25P10_RES:
|
||||
if (readcnt > 0)
|
||||
memset(readarr, 0x10, readcnt);
|
||||
@ -601,7 +617,7 @@ static int emulate_spi_chip_response(unsigned int writecnt,
|
||||
break;
|
||||
offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
|
||||
offs += writecnt - JEDEC_REMS_OUTSIZE;
|
||||
switch (emu_chip) {
|
||||
switch (data->emu_chip) {
|
||||
case EMULATE_SST_SST25VF040_REMS:
|
||||
for (i = 0; i < readcnt; i++)
|
||||
readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
|
||||
@ -623,7 +639,7 @@ static int emulate_spi_chip_response(unsigned int writecnt,
|
||||
}
|
||||
break;
|
||||
case JEDEC_RDID:
|
||||
switch (emu_chip) {
|
||||
switch (data->emu_chip) {
|
||||
case EMULATE_SST_SST25VF032B:
|
||||
if (readcnt > 0)
|
||||
readarr[0] = 0xbf;
|
||||
@ -663,70 +679,70 @@ static int emulate_spi_chip_response(unsigned int writecnt,
|
||||
}
|
||||
break;
|
||||
case JEDEC_RDSR:
|
||||
memset(readarr, emu_status, readcnt);
|
||||
memset(readarr, data->emu_status, readcnt);
|
||||
break;
|
||||
/* FIXME: this should be chip-specific. */
|
||||
case JEDEC_EWSR:
|
||||
case JEDEC_WREN:
|
||||
emu_status |= SPI_SR_WEL;
|
||||
data->emu_status |= SPI_SR_WEL;
|
||||
break;
|
||||
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");
|
||||
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);
|
||||
data->emu_status = writearr[1] & ~SPI_SR_WIP;
|
||||
msg_pdbg2("WRSR wrote 0x%02x.\n", 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 %= 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 %= 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 %= 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 > 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;
|
||||
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 %= 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 > 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;
|
||||
data->emu_modified = 1;
|
||||
break;
|
||||
case JEDEC_AAI_WORD_PROGRAM:
|
||||
if (!emu_max_aai_size)
|
||||
if (!data->emu_max_aai_size)
|
||||
break;
|
||||
if (!(emu_status & SPI_SR_AAI)) {
|
||||
if (!(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 +753,11 @@ static int emulate_spi_chip_response(unsigned int writecnt,
|
||||
"long!\n");
|
||||
return 1;
|
||||
}
|
||||
emu_status |= SPI_SR_AAI;
|
||||
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 %= data->emu_chip_size;
|
||||
memcpy(flashchip_contents + aai_offs, writearr + 4, 2);
|
||||
aai_offs += 2;
|
||||
} else {
|
||||
@ -758,14 +774,14 @@ static int emulate_spi_chip_response(unsigned int writecnt,
|
||||
memcpy(flashchip_contents + aai_offs, writearr + 1, 2);
|
||||
aai_offs += 2;
|
||||
}
|
||||
emu_modified = 1;
|
||||
data->emu_modified = 1;
|
||||
break;
|
||||
case JEDEC_WRDI:
|
||||
if (emu_max_aai_size)
|
||||
emu_status &= ~SPI_SR_AAI;
|
||||
if (data->emu_max_aai_size)
|
||||
data->emu_status &= ~SPI_SR_AAI;
|
||||
break;
|
||||
case JEDEC_SE:
|
||||
if (!emu_jedec_se_size)
|
||||
if (!data->emu_jedec_se_size)
|
||||
break;
|
||||
if (writecnt != JEDEC_SE_OUTSIZE) {
|
||||
msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
|
||||
@ -776,14 +792,14 @@ static int emulate_spi_chip_response(unsigned int writecnt,
|
||||
return 1;
|
||||
}
|
||||
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);
|
||||
offs &= ~(emu_jedec_se_size - 1);
|
||||
memset(flashchip_contents + offs, 0xff, emu_jedec_se_size);
|
||||
emu_modified = 1;
|
||||
offs &= ~(data->emu_jedec_se_size - 1);
|
||||
memset(flashchip_contents + offs, 0xff, data->emu_jedec_se_size);
|
||||
data->emu_modified = 1;
|
||||
break;
|
||||
case JEDEC_BE_52:
|
||||
if (!emu_jedec_be_52_size)
|
||||
if (!data->emu_jedec_be_52_size)
|
||||
break;
|
||||
if (writecnt != JEDEC_BE_52_OUTSIZE) {
|
||||
msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
|
||||
@ -794,14 +810,14 @@ static int emulate_spi_chip_response(unsigned int writecnt,
|
||||
return 1;
|
||||
}
|
||||
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);
|
||||
offs &= ~(emu_jedec_be_52_size - 1);
|
||||
memset(flashchip_contents + offs, 0xff, emu_jedec_be_52_size);
|
||||
emu_modified = 1;
|
||||
offs &= ~(data->emu_jedec_be_52_size - 1);
|
||||
memset(flashchip_contents + offs, 0xff, data->emu_jedec_be_52_size);
|
||||
data->emu_modified = 1;
|
||||
break;
|
||||
case JEDEC_BE_D8:
|
||||
if (!emu_jedec_be_d8_size)
|
||||
if (!data->emu_jedec_be_d8_size)
|
||||
break;
|
||||
if (writecnt != JEDEC_BE_D8_OUTSIZE) {
|
||||
msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
|
||||
@ -812,14 +828,14 @@ static int emulate_spi_chip_response(unsigned int writecnt,
|
||||
return 1;
|
||||
}
|
||||
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);
|
||||
offs &= ~(emu_jedec_be_d8_size - 1);
|
||||
memset(flashchip_contents + offs, 0xff, emu_jedec_be_d8_size);
|
||||
emu_modified = 1;
|
||||
offs &= ~(data->emu_jedec_be_d8_size - 1);
|
||||
memset(flashchip_contents + offs, 0xff, data->emu_jedec_be_d8_size);
|
||||
data->emu_modified = 1;
|
||||
break;
|
||||
case JEDEC_CE_60:
|
||||
if (!emu_jedec_ce_60_size)
|
||||
if (!data->emu_jedec_ce_60_size)
|
||||
break;
|
||||
if (writecnt != JEDEC_CE_60_OUTSIZE) {
|
||||
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. */
|
||||
/* 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, data->emu_jedec_ce_60_size);
|
||||
data->emu_modified = 1;
|
||||
break;
|
||||
case JEDEC_CE_C7:
|
||||
if (!emu_jedec_ce_c7_size)
|
||||
if (!data->emu_jedec_ce_c7_size)
|
||||
break;
|
||||
if (writecnt != JEDEC_CE_C7_OUTSIZE) {
|
||||
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. */
|
||||
/* 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, data->emu_jedec_ce_c7_size);
|
||||
data->emu_modified = 1;
|
||||
break;
|
||||
case JEDEC_SFDP:
|
||||
if (emu_chip != EMULATE_MACRONIX_MX25L6436)
|
||||
if (data->emu_chip != EMULATE_MACRONIX_MX25L6436)
|
||||
break;
|
||||
if (writecnt < 4)
|
||||
break;
|
||||
@ -890,17 +906,32 @@ static int emulate_spi_chip_response(unsigned int writecnt,
|
||||
break;
|
||||
}
|
||||
if (writearr[0] != JEDEC_WREN && writearr[0] != JEDEC_EWSR)
|
||||
emu_status &= ~SPI_SR_WEL;
|
||||
data->emu_status &= ~SPI_SR_WEL;
|
||||
return 0;
|
||||
}
|
||||
#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,
|
||||
unsigned int readcnt,
|
||||
const unsigned char *writearr,
|
||||
unsigned char *readarr)
|
||||
{
|
||||
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__);
|
||||
|
||||
@ -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. */
|
||||
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 +950,7 @@ static int dummy_spi_send_command(const struct flashctx *flash, unsigned int wri
|
||||
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 +976,14 @@ static int dummy_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsig
|
||||
int probe_variable_size(struct flashctx *flash)
|
||||
{
|
||||
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. */
|
||||
if (emu_chip != EMULATE_VARIABLE_SIZE)
|
||||
if (emu_data->emu_chip != EMULATE_VARIABLE_SIZE)
|
||||
return 0;
|
||||
|
||||
/*
|
||||
@ -961,7 +997,7 @@ int probe_variable_size(struct flashctx *flash)
|
||||
*
|
||||
* 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 +1008,7 @@ int probe_variable_size(struct flashctx *flash)
|
||||
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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user