dummyflasher.c: Factor out global state

Moves global state into spi_master data.

flashrom-stable: Squashed many fixups

Change-Id: I972b085875f1277d9ff33326669d2676a3bcd3aa
Signed-off-by: Lachlan Bishop <lxb@google.com>
Original-Reviewed-on: https://review.coreboot.org/c/flashrom/+/45230
Original-Reviewed-by: Edward O'Callaghan <quasisec@chromium.org>
Reviewed-on: https://review.coreboot.org/c/flashrom-stable/+/71323
Reviewed-by: Nico Huber <nico.h@gmx.de>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Angel Pons <th3fanbus@gmail.com>
diff --git a/dummyflasher.c b/dummyflasher.c
index 86211dc..a5ff6e5 100644
--- a/dummyflasher.c
+++ b/dummyflasher.c
@@ -45,24 +45,27 @@
 	EMULATE_MACRONIX_MX25L6436,
 	EMULATE_WINBOND_W25Q128FV,
 };
-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
@@ -107,7 +110,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,
@@ -118,7 +121,7 @@
 	.write_aai	= default_spi_write_aai,
 };
 
-static const struct par_master par_master_dummy = {
+static struct par_master par_master_dummy = {
 		.chip_readb		= dummy_chip_readb,
 		.chip_readw		= dummy_chip_readw,
 		.chip_readl		= dummy_chip_readl,
@@ -129,22 +132,24 @@
 		.chip_writen		= dummy_chip_writen,
 };
 
-static enum chipbustype dummy_buses_supported = BUS_NONE;
-
 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);
 	}
 #endif
+	free(data);
 	return 0;
 }
 
@@ -160,6 +165,15 @@
 	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;
+	par_master_dummy.data = data;
+
 	msg_pspew("%s\n", __func__);
 
 	bustext = extract_programmer_param("bus");
@@ -169,7 +183,7 @@
 	/* Convert the parameters to lowercase. */
 	tolower_string(bustext);
 
-	dummy_buses_supported = BUS_NONE;
+	enum chipbustype dummy_buses_supported = BUS_NONE;
 	if (strstr(bustext, "parallel")) {
 		dummy_buses_supported |= BUS_PARALLEL;
 		msg_pdbg("Enabling support for %s flash.\n", "parallel");
@@ -212,8 +226,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]);
@@ -221,18 +235,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);
 
@@ -248,8 +262,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]);
@@ -257,18 +271,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);
 
@@ -281,77 +295,77 @@
 	}
 #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");
 	}
 #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;
@@ -362,7 +376,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, "
@@ -370,30 +384,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;
 			}
@@ -404,8 +418,9 @@
 #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))
@@ -484,7 +499,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 *data)
 {
 	unsigned int offs, i, toread;
 	static int unsigned aai_offs;
@@ -498,17 +514,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 < 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.
 			 */
@@ -516,7 +532,7 @@
 		}
 	}
 
-	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) {
@@ -533,7 +549,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 (data->emu_chip) {
 		case EMULATE_ST_M25P10_RES:
 			if (readcnt > 0)
 				memset(readarr, 0x10, readcnt);
@@ -564,7 +580,7 @@
 			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];
@@ -586,7 +602,7 @@
 		}
 		break;
 	case JEDEC_RDID:
-		switch (emu_chip) {
+		switch (data->emu_chip) {
 		case EMULATE_SST_SST25VF032B:
 			if (readcnt > 0)
 				readarr[0] = 0xbf;
@@ -616,48 +632,48 @@
 		}
 		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_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_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");
@@ -668,11 +684,11 @@
 					 "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 {
@@ -689,14 +705,14 @@
 			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");
@@ -707,14 +723,14 @@
 			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");
@@ -725,14 +741,14 @@
 			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");
@@ -743,14 +759,14 @@
 			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");
@@ -762,11 +778,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, 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");
@@ -778,11 +794,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, 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;
@@ -821,7 +837,7 @@
 		break;
 	}
 	if (writearr[0] != JEDEC_WREN && writearr[0] != JEDEC_EWSR)
-		emu_status &= ~SPI_SR_WEL;
+		data->emu_status &= ~SPI_SR_WEL;
 	return 0;
 }
 #endif
@@ -832,6 +848,11 @@
 				  unsigned char *readarr)
 {
 	unsigned int i;
+	struct emu_data *emu_data = flash->mst->spi.data;
+	if (!emu_data) {
+		msg_perr("No data in flash context!\n");
+		return 1;
+	}
 
 	msg_pspew("%s:", __func__);
 
@@ -842,14 +863,14 @@
 	/* 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:
 	case EMULATE_MACRONIX_MX25L6436:
 	case EMULATE_WINBOND_W25Q128FV:
 		if (emulate_spi_chip_response(writecnt, readcnt, writearr,
-					      readarr)) {
+					      readarr, emu_data)) {
 			msg_pdbg("Invalid command sent to flash chip!\n");
 			return 1;
 		}