blob: 86211dc50103eb091d9f93a6a8d40b11c96de48d [file] [log] [blame]
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +00001/*
2 * This file is part of the flashrom project.
3 *
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00004 * Copyright (C) 2009,2010 Carl-Daniel Hailfinger
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00008 * the Free Software Foundation; version 2 of the License.
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +00009 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000014 */
15
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000016#include <string.h>
17#include <stdlib.h>
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +000018#include <stdio.h>
19#include <ctype.h>
Stefan Tauner5e695ab2012-05-06 17:03:40 +000020#include <errno.h>
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000021#include "flash.h"
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +000022#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000023#include "programmer.h"
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000024
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000025/* Remove the #define below if you don't want SPI flash chip emulation. */
26#define EMULATE_SPI_CHIP 1
27
28#if EMULATE_SPI_CHIP
29#define EMULATE_CHIP 1
30#include "spi.h"
31#endif
32
33#if EMULATE_CHIP
34#include <sys/types.h>
35#include <sys/stat.h>
36#endif
37
38#if EMULATE_CHIP
39static uint8_t *flashchip_contents = NULL;
40enum emu_chip {
41 EMULATE_NONE,
42 EMULATE_ST_M25P10_RES,
43 EMULATE_SST_SST25VF040_REMS,
44 EMULATE_SST_SST25VF032B,
Stefan Tauner0b9df972012-05-07 22:12:16 +000045 EMULATE_MACRONIX_MX25L6436,
Nico Huberf9632d82019-01-20 11:23:49 +010046 EMULATE_WINBOND_W25Q128FV,
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000047};
48static enum emu_chip emu_chip = EMULATE_NONE;
49static char *emu_persistent_image = NULL;
Stefan Taunerc69c9c82011-11-23 09:13:48 +000050static unsigned int emu_chip_size = 0;
Namyoon Woo35311232020-08-28 07:56:00 -070051static int emu_modified = 0; /* is the image modified since reading it? */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000052#if EMULATE_SPI_CHIP
Stefan Taunerc69c9c82011-11-23 09:13:48 +000053static unsigned int emu_max_byteprogram_size = 0;
54static unsigned int emu_max_aai_size = 0;
55static unsigned int emu_jedec_se_size = 0;
56static unsigned int emu_jedec_be_52_size = 0;
57static unsigned int emu_jedec_be_d8_size = 0;
58static unsigned int emu_jedec_ce_60_size = 0;
59static unsigned int emu_jedec_ce_c7_size = 0;
Jacob Garberafc3ad62019-06-24 16:05:28 -060060static unsigned char spi_blacklist[256];
61static unsigned char spi_ignorelist[256];
Nico Huber519be662018-12-23 20:03:35 +010062static unsigned int spi_blacklist_size = 0;
63static unsigned int spi_ignorelist_size = 0;
Stefan Tauner5e695ab2012-05-06 17:03:40 +000064static uint8_t emu_status = 0;
Stefan Tauner0b9df972012-05-07 22:12:16 +000065
66/* A legit complete SFDP table based on the MX25L6436E (rev. 1.8) datasheet. */
Stefan Tauner67d163d2013-01-15 17:37:48 +000067static const uint8_t sfdp_table[] = {
Stefan Tauner0b9df972012-05-07 22:12:16 +000068 0x53, 0x46, 0x44, 0x50, // @0x00: SFDP signature
69 0x00, 0x01, 0x01, 0xFF, // @0x04: revision 1.0, 2 headers
70 0x00, 0x00, 0x01, 0x09, // @0x08: JEDEC SFDP header rev. 1.0, 9 DW long
71 0x1C, 0x00, 0x00, 0xFF, // @0x0C: PTP0 = 0x1C (instead of 0x30)
72 0xC2, 0x00, 0x01, 0x04, // @0x10: Macronix header rev. 1.0, 4 DW long
73 0x48, 0x00, 0x00, 0xFF, // @0x14: PTP1 = 0x48 (instead of 0x60)
74 0xFF, 0xFF, 0xFF, 0xFF, // @0x18: hole.
75 0xE5, 0x20, 0xC9, 0xFF, // @0x1C: SFDP parameter table start
76 0xFF, 0xFF, 0xFF, 0x03, // @0x20
77 0x00, 0xFF, 0x08, 0x6B, // @0x24
78 0x08, 0x3B, 0x00, 0xFF, // @0x28
79 0xEE, 0xFF, 0xFF, 0xFF, // @0x2C
80 0xFF, 0xFF, 0x00, 0x00, // @0x30
81 0xFF, 0xFF, 0x00, 0xFF, // @0x34
82 0x0C, 0x20, 0x0F, 0x52, // @0x38
83 0x10, 0xD8, 0x00, 0xFF, // @0x3C: SFDP parameter table end
84 0xFF, 0xFF, 0xFF, 0xFF, // @0x40: hole.
85 0xFF, 0xFF, 0xFF, 0xFF, // @0x44: hole.
86 0x00, 0x36, 0x00, 0x27, // @0x48: Macronix parameter table start
87 0xF4, 0x4F, 0xFF, 0xFF, // @0x4C
88 0xD9, 0xC8, 0xFF, 0xFF, // @0x50
89 0xFF, 0xFF, 0xFF, 0xFF, // @0x54: Macronix parameter table end
90};
91
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000092#endif
93#endif
94
Stefan Taunerc69c9c82011-11-23 09:13:48 +000095static unsigned int spi_write_256_chunksize = 256;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000096
Edward O'Callaghan5eca4272020-04-12 17:27:53 +100097static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
Mark Marshallf20b7be2014-05-09 21:16:21 +000098 const unsigned char *writearr, unsigned char *readarr);
99static int dummy_spi_write_256(struct flashctx *flash, const uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000100 unsigned int start, unsigned int len);
Mark Marshallf20b7be2014-05-09 21:16:21 +0000101static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr);
102static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr);
103static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr);
104static void dummy_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len);
105static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr);
106static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr);
107static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr);
108static void dummy_chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len);
Michael Karcherb9dbe482011-05-11 17:07:07 +0000109
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000110static const struct spi_master spi_master_dummyflasher = {
Nico Huber1cf407b2017-11-10 20:18:23 +0100111 .features = SPI_MASTER_4BA,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000112 .max_data_read = MAX_DATA_READ_UNLIMITED,
113 .max_data_write = MAX_DATA_UNSPECIFIED,
114 .command = dummy_spi_send_command,
115 .multicommand = default_spi_send_multicommand,
116 .read = default_spi_read,
117 .write_256 = dummy_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +0000118 .write_aai = default_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000119};
David Hendricks8bb20212011-06-14 01:35:36 +0000120
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000121static const struct par_master par_master_dummy = {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000122 .chip_readb = dummy_chip_readb,
123 .chip_readw = dummy_chip_readw,
124 .chip_readl = dummy_chip_readl,
125 .chip_readn = dummy_chip_readn,
126 .chip_writeb = dummy_chip_writeb,
127 .chip_writew = dummy_chip_writew,
128 .chip_writel = dummy_chip_writel,
129 .chip_writen = dummy_chip_writen,
130};
131
Jacob Garberafc3ad62019-06-24 16:05:28 -0600132static enum chipbustype dummy_buses_supported = BUS_NONE;
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000133
David Hendricks8bb20212011-06-14 01:35:36 +0000134static int dummy_shutdown(void *data)
135{
136 msg_pspew("%s\n", __func__);
137#if EMULATE_CHIP
138 if (emu_chip != EMULATE_NONE) {
Namyoon Woo35311232020-08-28 07:56:00 -0700139 if (emu_persistent_image && emu_modified) {
David Hendricks8bb20212011-06-14 01:35:36 +0000140 msg_pdbg("Writing %s\n", emu_persistent_image);
Stefan Taunere0ff1652012-09-22 22:56:09 +0000141 write_buf_to_file(flashchip_contents, emu_chip_size, emu_persistent_image);
142 free(emu_persistent_image);
143 emu_persistent_image = NULL;
David Hendricks8bb20212011-06-14 01:35:36 +0000144 }
145 free(flashchip_contents);
146 }
147#endif
148 return 0;
149}
150
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000151int dummy_init(void)
152{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000153 char *bustext = NULL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000154 char *tmp = NULL;
Nico Huber519be662018-12-23 20:03:35 +0100155 unsigned int i;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000156#if EMULATE_SPI_CHIP
157 char *status = NULL;
158#endif
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000159#if EMULATE_CHIP
160 struct stat image_stat;
161#endif
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000162
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000163 msg_pspew("%s\n", __func__);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000164
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000165 bustext = extract_programmer_param("bus");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000166 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
167 if (!bustext)
168 bustext = strdup("parallel+lpc+fwh+spi");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000169 /* Convert the parameters to lowercase. */
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000170 tolower_string(bustext);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000171
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000172 dummy_buses_supported = BUS_NONE;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000173 if (strstr(bustext, "parallel")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000174 dummy_buses_supported |= BUS_PARALLEL;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000175 msg_pdbg("Enabling support for %s flash.\n", "parallel");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000176 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000177 if (strstr(bustext, "lpc")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000178 dummy_buses_supported |= BUS_LPC;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000179 msg_pdbg("Enabling support for %s flash.\n", "LPC");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000180 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000181 if (strstr(bustext, "fwh")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000182 dummy_buses_supported |= BUS_FWH;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000183 msg_pdbg("Enabling support for %s flash.\n", "FWH");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000184 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000185 if (strstr(bustext, "spi")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000186 dummy_buses_supported |= BUS_SPI;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000187 msg_pdbg("Enabling support for %s flash.\n", "SPI");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000188 }
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000189 if (dummy_buses_supported == BUS_NONE)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000190 msg_pdbg("Support for all flash bus types disabled.\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000191 free(bustext);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000192
193 tmp = extract_programmer_param("spi_write_256_chunksize");
194 if (tmp) {
195 spi_write_256_chunksize = atoi(tmp);
196 free(tmp);
197 if (spi_write_256_chunksize < 1) {
198 msg_perr("invalid spi_write_256_chunksize\n");
199 return 1;
200 }
201 }
202
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000203 tmp = extract_programmer_param("spi_blacklist");
204 if (tmp) {
205 i = strlen(tmp);
206 if (!strncmp(tmp, "0x", 2)) {
207 i -= 2;
208 memmove(tmp, tmp + 2, i + 1);
209 }
210 if ((i > 512) || (i % 2)) {
211 msg_perr("Invalid SPI command blacklist length\n");
212 free(tmp);
213 return 1;
214 }
215 spi_blacklist_size = i / 2;
216 for (i = 0; i < spi_blacklist_size * 2; i++) {
217 if (!isxdigit((unsigned char)tmp[i])) {
218 msg_perr("Invalid char \"%c\" in SPI command "
219 "blacklist\n", tmp[i]);
220 free(tmp);
221 return 1;
222 }
223 }
224 for (i = 0; i < spi_blacklist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000225 unsigned int tmp2;
226 /* SCNx8 is apparently not supported by MSVC (and thus
227 * MinGW), so work around it with an extra variable
228 */
229 sscanf(tmp + i * 2, "%2x", &tmp2);
230 spi_blacklist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000231 }
232 msg_pdbg("SPI blacklist is ");
233 for (i = 0; i < spi_blacklist_size; i++)
234 msg_pdbg("%02x ", spi_blacklist[i]);
Nico Huber519be662018-12-23 20:03:35 +0100235 msg_pdbg(", size %u\n", spi_blacklist_size);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000236 }
237 free(tmp);
238
239 tmp = extract_programmer_param("spi_ignorelist");
240 if (tmp) {
241 i = strlen(tmp);
242 if (!strncmp(tmp, "0x", 2)) {
243 i -= 2;
244 memmove(tmp, tmp + 2, i + 1);
245 }
246 if ((i > 512) || (i % 2)) {
247 msg_perr("Invalid SPI command ignorelist length\n");
248 free(tmp);
249 return 1;
250 }
251 spi_ignorelist_size = i / 2;
252 for (i = 0; i < spi_ignorelist_size * 2; i++) {
253 if (!isxdigit((unsigned char)tmp[i])) {
254 msg_perr("Invalid char \"%c\" in SPI command "
255 "ignorelist\n", tmp[i]);
256 free(tmp);
257 return 1;
258 }
259 }
260 for (i = 0; i < spi_ignorelist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000261 unsigned int tmp2;
262 /* SCNx8 is apparently not supported by MSVC (and thus
263 * MinGW), so work around it with an extra variable
264 */
265 sscanf(tmp + i * 2, "%2x", &tmp2);
266 spi_ignorelist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000267 }
268 msg_pdbg("SPI ignorelist is ");
269 for (i = 0; i < spi_ignorelist_size; i++)
270 msg_pdbg("%02x ", spi_ignorelist[i]);
Nico Huber519be662018-12-23 20:03:35 +0100271 msg_pdbg(", size %u\n", spi_ignorelist_size);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000272 }
273 free(tmp);
274
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000275#if EMULATE_CHIP
276 tmp = extract_programmer_param("emulate");
277 if (!tmp) {
278 msg_pdbg("Not emulating any flash chip.\n");
279 /* Nothing else to do. */
David Hendricks8bb20212011-06-14 01:35:36 +0000280 goto dummy_init_out;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000281 }
282#if EMULATE_SPI_CHIP
283 if (!strcmp(tmp, "M25P10.RES")) {
284 emu_chip = EMULATE_ST_M25P10_RES;
285 emu_chip_size = 128 * 1024;
286 emu_max_byteprogram_size = 128;
287 emu_max_aai_size = 0;
288 emu_jedec_se_size = 0;
289 emu_jedec_be_52_size = 0;
290 emu_jedec_be_d8_size = 32 * 1024;
291 emu_jedec_ce_60_size = 0;
292 emu_jedec_ce_c7_size = emu_chip_size;
293 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
294 "write)\n");
295 }
296 if (!strcmp(tmp, "SST25VF040.REMS")) {
297 emu_chip = EMULATE_SST_SST25VF040_REMS;
298 emu_chip_size = 512 * 1024;
299 emu_max_byteprogram_size = 1;
300 emu_max_aai_size = 0;
301 emu_jedec_se_size = 4 * 1024;
302 emu_jedec_be_52_size = 32 * 1024;
303 emu_jedec_be_d8_size = 0;
304 emu_jedec_ce_60_size = emu_chip_size;
305 emu_jedec_ce_c7_size = 0;
306 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
307 "byte write)\n");
308 }
309 if (!strcmp(tmp, "SST25VF032B")) {
310 emu_chip = EMULATE_SST_SST25VF032B;
311 emu_chip_size = 4 * 1024 * 1024;
312 emu_max_byteprogram_size = 1;
313 emu_max_aai_size = 2;
314 emu_jedec_se_size = 4 * 1024;
315 emu_jedec_be_52_size = 32 * 1024;
316 emu_jedec_be_d8_size = 64 * 1024;
317 emu_jedec_ce_60_size = emu_chip_size;
318 emu_jedec_ce_c7_size = emu_chip_size;
319 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
320 "write)\n");
321 }
Stefan Tauner0b9df972012-05-07 22:12:16 +0000322 if (!strcmp(tmp, "MX25L6436")) {
323 emu_chip = EMULATE_MACRONIX_MX25L6436;
324 emu_chip_size = 8 * 1024 * 1024;
325 emu_max_byteprogram_size = 256;
326 emu_max_aai_size = 0;
327 emu_jedec_se_size = 4 * 1024;
328 emu_jedec_be_52_size = 32 * 1024;
329 emu_jedec_be_d8_size = 64 * 1024;
330 emu_jedec_ce_60_size = emu_chip_size;
331 emu_jedec_ce_c7_size = emu_chip_size;
332 msg_pdbg("Emulating Macronix MX25L6436 SPI flash chip (RDID, "
333 "SFDP)\n");
334 }
Nico Huberf9632d82019-01-20 11:23:49 +0100335 if (!strcmp(tmp, "W25Q128FV")) {
336 emu_chip = EMULATE_WINBOND_W25Q128FV;
337 emu_chip_size = 16 * 1024 * 1024;
338 emu_max_byteprogram_size = 256;
339 emu_max_aai_size = 0;
340 emu_jedec_se_size = 4 * 1024;
341 emu_jedec_be_52_size = 32 * 1024;
342 emu_jedec_be_d8_size = 64 * 1024;
343 emu_jedec_ce_60_size = emu_chip_size;
344 emu_jedec_ce_c7_size = emu_chip_size;
345 msg_pdbg("Emulating Winbond W25Q128FV SPI flash chip (RDID)\n");
346 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000347#endif
348 if (emu_chip == EMULATE_NONE) {
349 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
350 free(tmp);
351 return 1;
352 }
353 free(tmp);
354 flashchip_contents = malloc(emu_chip_size);
355 if (!flashchip_contents) {
356 msg_perr("Out of memory!\n");
357 return 1;
358 }
David Hendricks8bb20212011-06-14 01:35:36 +0000359
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000360#ifdef EMULATE_SPI_CHIP
361 status = extract_programmer_param("spi_status");
362 if (status) {
363 char *endptr;
364 errno = 0;
365 emu_status = strtoul(status, &endptr, 0);
366 free(status);
367 if (errno != 0 || status == endptr) {
368 msg_perr("Error: initial status register specified, "
369 "but the value could not be converted.\n");
370 return 1;
371 }
372 msg_pdbg("Initial status register is set to 0x%02x.\n",
373 emu_status);
374 }
375#endif
376
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000377 msg_pdbg("Filling fake flash chip with 0xff, size %i\n", emu_chip_size);
378 memset(flashchip_contents, 0xff, emu_chip_size);
379
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000380 /* Will be freed by shutdown function if necessary. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000381 emu_persistent_image = extract_programmer_param("image");
382 if (!emu_persistent_image) {
383 /* Nothing else to do. */
David Hendricks8bb20212011-06-14 01:35:36 +0000384 goto dummy_init_out;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000385 }
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000386 /* We will silently (in default verbosity) ignore the file if it does not exist (yet) or the size does
387 * not match the emulated chip. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000388 if (!stat(emu_persistent_image, &image_stat)) {
Stefan Tauner23e10b82016-01-23 16:16:49 +0000389 msg_pdbg("Found persistent image %s, %jd B ",
390 emu_persistent_image, (intmax_t)image_stat.st_size);
Nico Huber961f4a12019-10-04 17:34:22 +0200391 if ((uintmax_t)image_stat.st_size == emu_chip_size) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000392 msg_pdbg("matches.\n");
393 msg_pdbg("Reading %s\n", emu_persistent_image);
Jacob Garberca598da2019-08-12 10:44:17 -0600394 if (read_buf_from_file(flashchip_contents, emu_chip_size,
395 emu_persistent_image)) {
396 msg_perr("Unable to read %s\n", emu_persistent_image);
397 free(flashchip_contents);
398 return 1;
399 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000400 } else {
401 msg_pdbg("doesn't match.\n");
402 }
403 }
404#endif
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000405
David Hendricks8bb20212011-06-14 01:35:36 +0000406dummy_init_out:
407 if (register_shutdown(dummy_shutdown, NULL)) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000408 free(flashchip_contents);
David Hendricks8bb20212011-06-14 01:35:36 +0000409 return 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000410 }
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000411 if (dummy_buses_supported & (BUS_PARALLEL | BUS_LPC | BUS_FWH))
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000412 register_par_master(&par_master_dummy,
413 dummy_buses_supported & (BUS_PARALLEL | BUS_LPC | BUS_FWH));
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000414 if (dummy_buses_supported & BUS_SPI)
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000415 register_spi_master(&spi_master_dummyflasher);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000416
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000417 return 0;
418}
419
Stefan Tauner305e0b92013-07-17 23:46:44 +0000420void *dummy_map(const char *descr, uintptr_t phys_addr, size_t len)
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000421{
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000422 msg_pspew("%s: Mapping %s, 0x%zx bytes at 0x%0*" PRIxPTR "\n",
Stefan Tauner0554ca52013-07-25 22:54:25 +0000423 __func__, descr, len, PRIxPTR_WIDTH, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000424 return (void *)phys_addr;
425}
426
427void dummy_unmap(void *virt_addr, size_t len)
428{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000429 msg_pspew("%s: Unmapping 0x%zx bytes at %p\n", __func__, len, virt_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000430}
431
Mark Marshallf20b7be2014-05-09 21:16:21 +0000432static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000433{
Stefan Taunerc2333752013-07-13 23:31:37 +0000434 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%02x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000435}
436
Mark Marshallf20b7be2014-05-09 21:16:21 +0000437static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000438{
Stefan Taunerc2333752013-07-13 23:31:37 +0000439 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%04x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000440}
441
Mark Marshallf20b7be2014-05-09 21:16:21 +0000442static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000443{
Stefan Taunerc2333752013-07-13 23:31:37 +0000444 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%08x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000445}
446
Mark Marshallf20b7be2014-05-09 21:16:21 +0000447static void dummy_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len)
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000448{
449 size_t i;
Stefan Tauner0554ca52013-07-25 22:54:25 +0000450 msg_pspew("%s: addr=0x%" PRIxPTR ", len=0x%zx, writing data (hex):", __func__, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000451 for (i = 0; i < len; i++) {
452 if ((i % 16) == 0)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000453 msg_pspew("\n");
454 msg_pspew("%02x ", buf[i]);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000455 }
456}
457
Mark Marshallf20b7be2014-05-09 21:16:21 +0000458static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000459{
Stefan Taunerc2333752013-07-13 23:31:37 +0000460 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000461 return 0xff;
462}
463
Mark Marshallf20b7be2014-05-09 21:16:21 +0000464static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000465{
Stefan Taunerc2333752013-07-13 23:31:37 +0000466 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000467 return 0xffff;
468}
469
Mark Marshallf20b7be2014-05-09 21:16:21 +0000470static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000471{
Stefan Taunerc2333752013-07-13 23:31:37 +0000472 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffffffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000473 return 0xffffffff;
474}
475
Mark Marshallf20b7be2014-05-09 21:16:21 +0000476static void dummy_chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len)
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000477{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000478 msg_pspew("%s: addr=0x%" PRIxPTR ", len=0x%zx, returning array of 0xff\n", __func__, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000479 memset(buf, 0xff, len);
480 return;
481}
482
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000483#if EMULATE_SPI_CHIP
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000484static int emulate_spi_chip_response(unsigned int writecnt,
485 unsigned int readcnt,
486 const unsigned char *writearr,
487 unsigned char *readarr)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000488{
Stefan Tauner0b9df972012-05-07 22:12:16 +0000489 unsigned int offs, i, toread;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000490 static int unsigned aai_offs;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000491 const unsigned char sst25vf040_rems_response[2] = {0xbf, 0x44};
492 const unsigned char sst25vf032b_rems_response[2] = {0xbf, 0x4a};
493 const unsigned char mx25l6436_rems_response[2] = {0xc2, 0x16};
Nico Huberf9632d82019-01-20 11:23:49 +0100494 const unsigned char w25q128fv_rems_response[2] = {0xef, 0x17};
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000495
496 if (writecnt == 0) {
497 msg_perr("No command sent to the chip!\n");
498 return 1;
499 }
Paul Menzelac427b22012-02-16 21:07:07 +0000500 /* spi_blacklist has precedence over spi_ignorelist. */
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000501 for (i = 0; i < spi_blacklist_size; i++) {
502 if (writearr[0] == spi_blacklist[i]) {
503 msg_pdbg("Refusing blacklisted SPI command 0x%02x\n",
504 spi_blacklist[i]);
505 return SPI_INVALID_OPCODE;
506 }
507 }
508 for (i = 0; i < spi_ignorelist_size; i++) {
509 if (writearr[0] == spi_ignorelist[i]) {
510 msg_cdbg("Ignoring ignorelisted SPI command 0x%02x\n",
511 spi_ignorelist[i]);
512 /* Return success because the command does not fail,
513 * it is simply ignored.
514 */
515 return 0;
516 }
517 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000518
519 if (emu_max_aai_size && (emu_status & SPI_SR_AAI)) {
520 if (writearr[0] != JEDEC_AAI_WORD_PROGRAM &&
521 writearr[0] != JEDEC_WRDI &&
522 writearr[0] != JEDEC_RDSR) {
523 msg_perr("Forbidden opcode (0x%02x) attempted during "
524 "AAI sequence!\n", writearr[0]);
525 return 0;
526 }
527 }
528
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000529 switch (writearr[0]) {
530 case JEDEC_RES:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000531 if (writecnt < JEDEC_RES_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000532 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000533 /* offs calculation is only needed for SST chips which treat RES like REMS. */
534 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
535 offs += writecnt - JEDEC_REMS_OUTSIZE;
536 switch (emu_chip) {
537 case EMULATE_ST_M25P10_RES:
538 if (readcnt > 0)
539 memset(readarr, 0x10, readcnt);
540 break;
541 case EMULATE_SST_SST25VF040_REMS:
542 for (i = 0; i < readcnt; i++)
543 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
544 break;
545 case EMULATE_SST_SST25VF032B:
546 for (i = 0; i < readcnt; i++)
547 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
548 break;
549 case EMULATE_MACRONIX_MX25L6436:
550 if (readcnt > 0)
551 memset(readarr, 0x16, readcnt);
552 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100553 case EMULATE_WINBOND_W25Q128FV:
554 if (readcnt > 0)
555 memset(readarr, 0x17, readcnt);
556 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000557 default: /* ignore */
558 break;
559 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000560 break;
561 case JEDEC_REMS:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000562 /* REMS response has wraparound and uses an address parameter. */
563 if (writecnt < JEDEC_REMS_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000564 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000565 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
566 offs += writecnt - JEDEC_REMS_OUTSIZE;
567 switch (emu_chip) {
568 case EMULATE_SST_SST25VF040_REMS:
569 for (i = 0; i < readcnt; i++)
570 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
571 break;
572 case EMULATE_SST_SST25VF032B:
573 for (i = 0; i < readcnt; i++)
574 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
575 break;
576 case EMULATE_MACRONIX_MX25L6436:
577 for (i = 0; i < readcnt; i++)
578 readarr[i] = mx25l6436_rems_response[(offs + i) % 2];
579 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100580 case EMULATE_WINBOND_W25Q128FV:
581 for (i = 0; i < readcnt; i++)
582 readarr[i] = w25q128fv_rems_response[(offs + i) % 2];
583 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000584 default: /* ignore */
585 break;
586 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000587 break;
588 case JEDEC_RDID:
Stefan Tauner0b9df972012-05-07 22:12:16 +0000589 switch (emu_chip) {
590 case EMULATE_SST_SST25VF032B:
591 if (readcnt > 0)
592 readarr[0] = 0xbf;
593 if (readcnt > 1)
594 readarr[1] = 0x25;
595 if (readcnt > 2)
596 readarr[2] = 0x4a;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000597 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000598 case EMULATE_MACRONIX_MX25L6436:
599 if (readcnt > 0)
600 readarr[0] = 0xc2;
601 if (readcnt > 1)
602 readarr[1] = 0x20;
603 if (readcnt > 2)
604 readarr[2] = 0x17;
605 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100606 case EMULATE_WINBOND_W25Q128FV:
607 if (readcnt > 0)
608 readarr[0] = 0xef;
609 if (readcnt > 1)
610 readarr[1] = 0x40;
611 if (readcnt > 2)
612 readarr[2] = 0x18;
613 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000614 default: /* ignore */
615 break;
616 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000617 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000618 case JEDEC_RDSR:
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000619 memset(readarr, emu_status, readcnt);
620 break;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000621 /* FIXME: this should be chip-specific. */
622 case JEDEC_EWSR:
623 case JEDEC_WREN:
624 emu_status |= SPI_SR_WEL;
625 break;
626 case JEDEC_WRSR:
627 if (!(emu_status & SPI_SR_WEL)) {
628 msg_perr("WRSR attempted, but WEL is 0!\n");
629 break;
630 }
631 /* FIXME: add some reasonable simulation of the busy flag */
632 emu_status = writearr[1] & ~SPI_SR_WIP;
633 msg_pdbg2("WRSR wrote 0x%02x.\n", emu_status);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000634 break;
635 case JEDEC_READ:
636 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
637 /* Truncate to emu_chip_size. */
638 offs %= emu_chip_size;
639 if (readcnt > 0)
640 memcpy(readarr, flashchip_contents + offs, readcnt);
641 break;
642 case JEDEC_BYTE_PROGRAM:
643 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
644 /* Truncate to emu_chip_size. */
645 offs %= emu_chip_size;
646 if (writecnt < 5) {
647 msg_perr("BYTE PROGRAM size too short!\n");
648 return 1;
649 }
650 if (writecnt - 4 > emu_max_byteprogram_size) {
651 msg_perr("Max BYTE PROGRAM size exceeded!\n");
652 return 1;
653 }
654 memcpy(flashchip_contents + offs, writearr + 4, writecnt - 4);
Namyoon Woo35311232020-08-28 07:56:00 -0700655 emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000656 break;
657 case JEDEC_AAI_WORD_PROGRAM:
658 if (!emu_max_aai_size)
659 break;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000660 if (!(emu_status & SPI_SR_AAI)) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000661 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
662 msg_perr("Initial AAI WORD PROGRAM size too "
663 "short!\n");
664 return 1;
665 }
666 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
667 msg_perr("Initial AAI WORD PROGRAM size too "
668 "long!\n");
669 return 1;
670 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000671 emu_status |= SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000672 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
673 writearr[3];
674 /* Truncate to emu_chip_size. */
675 aai_offs %= emu_chip_size;
676 memcpy(flashchip_contents + aai_offs, writearr + 4, 2);
677 aai_offs += 2;
678 } else {
679 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
680 msg_perr("Continuation AAI WORD PROGRAM size "
681 "too short!\n");
682 return 1;
683 }
684 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
685 msg_perr("Continuation AAI WORD PROGRAM size "
686 "too long!\n");
687 return 1;
688 }
689 memcpy(flashchip_contents + aai_offs, writearr + 1, 2);
690 aai_offs += 2;
691 }
Namyoon Woo35311232020-08-28 07:56:00 -0700692 emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000693 break;
694 case JEDEC_WRDI:
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000695 if (emu_max_aai_size)
696 emu_status &= ~SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000697 break;
698 case JEDEC_SE:
699 if (!emu_jedec_se_size)
700 break;
701 if (writecnt != JEDEC_SE_OUTSIZE) {
702 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
703 return 1;
704 }
705 if (readcnt != JEDEC_SE_INSIZE) {
706 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
707 return 1;
708 }
709 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
710 if (offs & (emu_jedec_se_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000711 msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000712 offs &= ~(emu_jedec_se_size - 1);
713 memset(flashchip_contents + offs, 0xff, emu_jedec_se_size);
Namyoon Woo35311232020-08-28 07:56:00 -0700714 emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000715 break;
716 case JEDEC_BE_52:
717 if (!emu_jedec_be_52_size)
718 break;
719 if (writecnt != JEDEC_BE_52_OUTSIZE) {
720 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
721 return 1;
722 }
723 if (readcnt != JEDEC_BE_52_INSIZE) {
724 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
725 return 1;
726 }
727 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
728 if (offs & (emu_jedec_be_52_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000729 msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000730 offs &= ~(emu_jedec_be_52_size - 1);
731 memset(flashchip_contents + offs, 0xff, emu_jedec_be_52_size);
Namyoon Woo35311232020-08-28 07:56:00 -0700732 emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000733 break;
734 case JEDEC_BE_D8:
735 if (!emu_jedec_be_d8_size)
736 break;
737 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
738 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
739 return 1;
740 }
741 if (readcnt != JEDEC_BE_D8_INSIZE) {
742 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
743 return 1;
744 }
745 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
746 if (offs & (emu_jedec_be_d8_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000747 msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000748 offs &= ~(emu_jedec_be_d8_size - 1);
749 memset(flashchip_contents + offs, 0xff, emu_jedec_be_d8_size);
Namyoon Woo35311232020-08-28 07:56:00 -0700750 emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000751 break;
752 case JEDEC_CE_60:
753 if (!emu_jedec_ce_60_size)
754 break;
755 if (writecnt != JEDEC_CE_60_OUTSIZE) {
756 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
757 return 1;
758 }
759 if (readcnt != JEDEC_CE_60_INSIZE) {
760 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
761 return 1;
762 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000763 /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000764 /* emu_jedec_ce_60_size is emu_chip_size. */
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000765 memset(flashchip_contents, 0xff, emu_jedec_ce_60_size);
Namyoon Woo35311232020-08-28 07:56:00 -0700766 emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000767 break;
768 case JEDEC_CE_C7:
769 if (!emu_jedec_ce_c7_size)
770 break;
771 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
772 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
773 return 1;
774 }
775 if (readcnt != JEDEC_CE_C7_INSIZE) {
776 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
777 return 1;
778 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000779 /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000780 /* emu_jedec_ce_c7_size is emu_chip_size. */
781 memset(flashchip_contents, 0xff, emu_jedec_ce_c7_size);
Namyoon Woo35311232020-08-28 07:56:00 -0700782 emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000783 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000784 case JEDEC_SFDP:
785 if (emu_chip != EMULATE_MACRONIX_MX25L6436)
786 break;
787 if (writecnt < 4)
788 break;
789 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
790
791 /* SFDP expects one dummy byte after the address. */
792 if (writecnt == 4) {
793 /* The dummy byte was not written, make sure it is read instead.
794 * Shifting and shortening the read array does achieve this goal.
795 */
796 readarr++;
797 readcnt--;
798 } else {
799 /* The response is shifted if more than 5 bytes are written, because SFDP data is
800 * already shifted out by the chip while those superfluous bytes are written. */
801 offs += writecnt - 5;
802 }
803
804 /* The SFDP spec implies that the start address of an SFDP read may be truncated to fit in the
805 * SFDP table address space, i.e. the start address may be wrapped around at SFDP table size.
806 * This is a reasonable implementation choice in hardware because it saves a few gates. */
807 if (offs >= sizeof(sfdp_table)) {
808 msg_pdbg("Wrapping the start address around the SFDP table boundary (using 0x%x "
809 "instead of 0x%x).\n", (unsigned int)(offs % sizeof(sfdp_table)), offs);
810 offs %= sizeof(sfdp_table);
811 }
812 toread = min(sizeof(sfdp_table) - offs, readcnt);
813 memcpy(readarr, sfdp_table + offs, toread);
814 if (toread < readcnt)
815 msg_pdbg("Crossing the SFDP table boundary in a single "
816 "continuous chunk produces undefined results "
817 "after that point.\n");
818 break;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000819 default:
820 /* No special response. */
821 break;
822 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000823 if (writearr[0] != JEDEC_WREN && writearr[0] != JEDEC_EWSR)
824 emu_status &= ~SPI_SR_WEL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000825 return 0;
826}
827#endif
828
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000829static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000830 unsigned int readcnt,
831 const unsigned char *writearr,
832 unsigned char *readarr)
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000833{
Nico Huber519be662018-12-23 20:03:35 +0100834 unsigned int i;
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000835
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000836 msg_pspew("%s:", __func__);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000837
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000838 msg_pspew(" writing %u bytes:", writecnt);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000839 for (i = 0; i < writecnt; i++)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000840 msg_pspew(" 0x%02x", writearr[i]);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000841
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000842 /* Response for unknown commands and missing chip is 0xff. */
843 memset(readarr, 0xff, readcnt);
844#if EMULATE_SPI_CHIP
845 switch (emu_chip) {
846 case EMULATE_ST_M25P10_RES:
847 case EMULATE_SST_SST25VF040_REMS:
848 case EMULATE_SST_SST25VF032B:
Stefan Tauner0b9df972012-05-07 22:12:16 +0000849 case EMULATE_MACRONIX_MX25L6436:
Nico Huberf9632d82019-01-20 11:23:49 +0100850 case EMULATE_WINBOND_W25Q128FV:
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000851 if (emulate_spi_chip_response(writecnt, readcnt, writearr,
852 readarr)) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000853 msg_pdbg("Invalid command sent to flash chip!\n");
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000854 return 1;
855 }
856 break;
857 default:
858 break;
859 }
860#endif
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000861 msg_pspew(" reading %u bytes:", readcnt);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000862 for (i = 0; i < readcnt; i++)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000863 msg_pspew(" 0x%02x", readarr[i]);
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000864 msg_pspew("\n");
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000865 return 0;
866}
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +0000867
Mark Marshallf20b7be2014-05-09 21:16:21 +0000868static int dummy_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000869{
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000870 return spi_write_chunked(flash, buf, start, len,
871 spi_write_256_chunksize);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000872}