blob: e1a1d8013973e1a55254b2d52ac605aab0ba1c4b [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;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000051#if EMULATE_SPI_CHIP
Stefan Taunerc69c9c82011-11-23 09:13:48 +000052static unsigned int emu_max_byteprogram_size = 0;
53static unsigned int emu_max_aai_size = 0;
54static unsigned int emu_jedec_se_size = 0;
55static unsigned int emu_jedec_be_52_size = 0;
56static unsigned int emu_jedec_be_d8_size = 0;
57static unsigned int emu_jedec_ce_60_size = 0;
58static unsigned int emu_jedec_ce_c7_size = 0;
Jacob Garberafc3ad62019-06-24 16:05:28 -060059static unsigned char spi_blacklist[256];
60static unsigned char spi_ignorelist[256];
Nico Huber519be662018-12-23 20:03:35 +010061static unsigned int spi_blacklist_size = 0;
62static unsigned int spi_ignorelist_size = 0;
Stefan Tauner5e695ab2012-05-06 17:03:40 +000063static uint8_t emu_status = 0;
Stefan Tauner0b9df972012-05-07 22:12:16 +000064
65/* A legit complete SFDP table based on the MX25L6436E (rev. 1.8) datasheet. */
Stefan Tauner67d163d2013-01-15 17:37:48 +000066static const uint8_t sfdp_table[] = {
Stefan Tauner0b9df972012-05-07 22:12:16 +000067 0x53, 0x46, 0x44, 0x50, // @0x00: SFDP signature
68 0x00, 0x01, 0x01, 0xFF, // @0x04: revision 1.0, 2 headers
69 0x00, 0x00, 0x01, 0x09, // @0x08: JEDEC SFDP header rev. 1.0, 9 DW long
70 0x1C, 0x00, 0x00, 0xFF, // @0x0C: PTP0 = 0x1C (instead of 0x30)
71 0xC2, 0x00, 0x01, 0x04, // @0x10: Macronix header rev. 1.0, 4 DW long
72 0x48, 0x00, 0x00, 0xFF, // @0x14: PTP1 = 0x48 (instead of 0x60)
73 0xFF, 0xFF, 0xFF, 0xFF, // @0x18: hole.
74 0xE5, 0x20, 0xC9, 0xFF, // @0x1C: SFDP parameter table start
75 0xFF, 0xFF, 0xFF, 0x03, // @0x20
76 0x00, 0xFF, 0x08, 0x6B, // @0x24
77 0x08, 0x3B, 0x00, 0xFF, // @0x28
78 0xEE, 0xFF, 0xFF, 0xFF, // @0x2C
79 0xFF, 0xFF, 0x00, 0x00, // @0x30
80 0xFF, 0xFF, 0x00, 0xFF, // @0x34
81 0x0C, 0x20, 0x0F, 0x52, // @0x38
82 0x10, 0xD8, 0x00, 0xFF, // @0x3C: SFDP parameter table end
83 0xFF, 0xFF, 0xFF, 0xFF, // @0x40: hole.
84 0xFF, 0xFF, 0xFF, 0xFF, // @0x44: hole.
85 0x00, 0x36, 0x00, 0x27, // @0x48: Macronix parameter table start
86 0xF4, 0x4F, 0xFF, 0xFF, // @0x4C
87 0xD9, 0xC8, 0xFF, 0xFF, // @0x50
88 0xFF, 0xFF, 0xFF, 0xFF, // @0x54: Macronix parameter table end
89};
90
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000091#endif
92#endif
93
Stefan Taunerc69c9c82011-11-23 09:13:48 +000094static unsigned int spi_write_256_chunksize = 256;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000095
Edward O'Callaghan5eca4272020-04-12 17:27:53 +100096static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
Mark Marshallf20b7be2014-05-09 21:16:21 +000097 const unsigned char *writearr, unsigned char *readarr);
98static int dummy_spi_write_256(struct flashctx *flash, const uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +000099 unsigned int start, unsigned int len);
Mark Marshallf20b7be2014-05-09 21:16:21 +0000100static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr);
101static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr);
102static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr);
103static void dummy_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len);
104static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr);
105static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr);
106static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr);
107static void dummy_chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len);
Michael Karcherb9dbe482011-05-11 17:07:07 +0000108
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000109static const struct spi_master spi_master_dummyflasher = {
Nico Huber1cf407b2017-11-10 20:18:23 +0100110 .features = SPI_MASTER_4BA,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000111 .max_data_read = MAX_DATA_READ_UNLIMITED,
112 .max_data_write = MAX_DATA_UNSPECIFIED,
113 .command = dummy_spi_send_command,
114 .multicommand = default_spi_send_multicommand,
115 .read = default_spi_read,
116 .write_256 = dummy_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +0000117 .write_aai = default_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000118};
David Hendricks8bb20212011-06-14 01:35:36 +0000119
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000120static const struct par_master par_master_dummy = {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000121 .chip_readb = dummy_chip_readb,
122 .chip_readw = dummy_chip_readw,
123 .chip_readl = dummy_chip_readl,
124 .chip_readn = dummy_chip_readn,
125 .chip_writeb = dummy_chip_writeb,
126 .chip_writew = dummy_chip_writew,
127 .chip_writel = dummy_chip_writel,
128 .chip_writen = dummy_chip_writen,
129};
130
Jacob Garberafc3ad62019-06-24 16:05:28 -0600131static enum chipbustype dummy_buses_supported = BUS_NONE;
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000132
David Hendricks8bb20212011-06-14 01:35:36 +0000133static int dummy_shutdown(void *data)
134{
135 msg_pspew("%s\n", __func__);
136#if EMULATE_CHIP
137 if (emu_chip != EMULATE_NONE) {
138 if (emu_persistent_image) {
139 msg_pdbg("Writing %s\n", emu_persistent_image);
Stefan Taunere0ff1652012-09-22 22:56:09 +0000140 write_buf_to_file(flashchip_contents, emu_chip_size, emu_persistent_image);
141 free(emu_persistent_image);
142 emu_persistent_image = NULL;
David Hendricks8bb20212011-06-14 01:35:36 +0000143 }
144 free(flashchip_contents);
145 }
146#endif
147 return 0;
148}
149
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000150int dummy_init(void)
151{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000152 char *bustext = NULL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000153 char *tmp = NULL;
Nico Huber519be662018-12-23 20:03:35 +0100154 unsigned int i;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000155#if EMULATE_SPI_CHIP
156 char *status = NULL;
157#endif
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000158#if EMULATE_CHIP
159 struct stat image_stat;
160#endif
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000161
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000162 msg_pspew("%s\n", __func__);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000163
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000164 bustext = extract_programmer_param("bus");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000165 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
166 if (!bustext)
167 bustext = strdup("parallel+lpc+fwh+spi");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000168 /* Convert the parameters to lowercase. */
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000169 tolower_string(bustext);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000170
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000171 dummy_buses_supported = BUS_NONE;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000172 if (strstr(bustext, "parallel")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000173 dummy_buses_supported |= BUS_PARALLEL;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000174 msg_pdbg("Enabling support for %s flash.\n", "parallel");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000175 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000176 if (strstr(bustext, "lpc")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000177 dummy_buses_supported |= BUS_LPC;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000178 msg_pdbg("Enabling support for %s flash.\n", "LPC");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000179 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000180 if (strstr(bustext, "fwh")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000181 dummy_buses_supported |= BUS_FWH;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000182 msg_pdbg("Enabling support for %s flash.\n", "FWH");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000183 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000184 if (strstr(bustext, "spi")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000185 dummy_buses_supported |= BUS_SPI;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000186 msg_pdbg("Enabling support for %s flash.\n", "SPI");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000187 }
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000188 if (dummy_buses_supported == BUS_NONE)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000189 msg_pdbg("Support for all flash bus types disabled.\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000190 free(bustext);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000191
192 tmp = extract_programmer_param("spi_write_256_chunksize");
193 if (tmp) {
194 spi_write_256_chunksize = atoi(tmp);
195 free(tmp);
196 if (spi_write_256_chunksize < 1) {
197 msg_perr("invalid spi_write_256_chunksize\n");
198 return 1;
199 }
200 }
201
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000202 tmp = extract_programmer_param("spi_blacklist");
203 if (tmp) {
204 i = strlen(tmp);
205 if (!strncmp(tmp, "0x", 2)) {
206 i -= 2;
207 memmove(tmp, tmp + 2, i + 1);
208 }
209 if ((i > 512) || (i % 2)) {
210 msg_perr("Invalid SPI command blacklist length\n");
211 free(tmp);
212 return 1;
213 }
214 spi_blacklist_size = i / 2;
215 for (i = 0; i < spi_blacklist_size * 2; i++) {
216 if (!isxdigit((unsigned char)tmp[i])) {
217 msg_perr("Invalid char \"%c\" in SPI command "
218 "blacklist\n", tmp[i]);
219 free(tmp);
220 return 1;
221 }
222 }
223 for (i = 0; i < spi_blacklist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000224 unsigned int tmp2;
225 /* SCNx8 is apparently not supported by MSVC (and thus
226 * MinGW), so work around it with an extra variable
227 */
228 sscanf(tmp + i * 2, "%2x", &tmp2);
229 spi_blacklist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000230 }
231 msg_pdbg("SPI blacklist is ");
232 for (i = 0; i < spi_blacklist_size; i++)
233 msg_pdbg("%02x ", spi_blacklist[i]);
Nico Huber519be662018-12-23 20:03:35 +0100234 msg_pdbg(", size %u\n", spi_blacklist_size);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000235 }
236 free(tmp);
237
238 tmp = extract_programmer_param("spi_ignorelist");
239 if (tmp) {
240 i = strlen(tmp);
241 if (!strncmp(tmp, "0x", 2)) {
242 i -= 2;
243 memmove(tmp, tmp + 2, i + 1);
244 }
245 if ((i > 512) || (i % 2)) {
246 msg_perr("Invalid SPI command ignorelist length\n");
247 free(tmp);
248 return 1;
249 }
250 spi_ignorelist_size = i / 2;
251 for (i = 0; i < spi_ignorelist_size * 2; i++) {
252 if (!isxdigit((unsigned char)tmp[i])) {
253 msg_perr("Invalid char \"%c\" in SPI command "
254 "ignorelist\n", tmp[i]);
255 free(tmp);
256 return 1;
257 }
258 }
259 for (i = 0; i < spi_ignorelist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000260 unsigned int tmp2;
261 /* SCNx8 is apparently not supported by MSVC (and thus
262 * MinGW), so work around it with an extra variable
263 */
264 sscanf(tmp + i * 2, "%2x", &tmp2);
265 spi_ignorelist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000266 }
267 msg_pdbg("SPI ignorelist is ");
268 for (i = 0; i < spi_ignorelist_size; i++)
269 msg_pdbg("%02x ", spi_ignorelist[i]);
Nico Huber519be662018-12-23 20:03:35 +0100270 msg_pdbg(", size %u\n", spi_ignorelist_size);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000271 }
272 free(tmp);
273
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000274#if EMULATE_CHIP
275 tmp = extract_programmer_param("emulate");
276 if (!tmp) {
277 msg_pdbg("Not emulating any flash chip.\n");
278 /* Nothing else to do. */
David Hendricks8bb20212011-06-14 01:35:36 +0000279 goto dummy_init_out;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000280 }
281#if EMULATE_SPI_CHIP
282 if (!strcmp(tmp, "M25P10.RES")) {
283 emu_chip = EMULATE_ST_M25P10_RES;
284 emu_chip_size = 128 * 1024;
285 emu_max_byteprogram_size = 128;
286 emu_max_aai_size = 0;
287 emu_jedec_se_size = 0;
288 emu_jedec_be_52_size = 0;
289 emu_jedec_be_d8_size = 32 * 1024;
290 emu_jedec_ce_60_size = 0;
291 emu_jedec_ce_c7_size = emu_chip_size;
292 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
293 "write)\n");
294 }
295 if (!strcmp(tmp, "SST25VF040.REMS")) {
296 emu_chip = EMULATE_SST_SST25VF040_REMS;
297 emu_chip_size = 512 * 1024;
298 emu_max_byteprogram_size = 1;
299 emu_max_aai_size = 0;
300 emu_jedec_se_size = 4 * 1024;
301 emu_jedec_be_52_size = 32 * 1024;
302 emu_jedec_be_d8_size = 0;
303 emu_jedec_ce_60_size = emu_chip_size;
304 emu_jedec_ce_c7_size = 0;
305 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
306 "byte write)\n");
307 }
308 if (!strcmp(tmp, "SST25VF032B")) {
309 emu_chip = EMULATE_SST_SST25VF032B;
310 emu_chip_size = 4 * 1024 * 1024;
311 emu_max_byteprogram_size = 1;
312 emu_max_aai_size = 2;
313 emu_jedec_se_size = 4 * 1024;
314 emu_jedec_be_52_size = 32 * 1024;
315 emu_jedec_be_d8_size = 64 * 1024;
316 emu_jedec_ce_60_size = emu_chip_size;
317 emu_jedec_ce_c7_size = emu_chip_size;
318 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
319 "write)\n");
320 }
Stefan Tauner0b9df972012-05-07 22:12:16 +0000321 if (!strcmp(tmp, "MX25L6436")) {
322 emu_chip = EMULATE_MACRONIX_MX25L6436;
323 emu_chip_size = 8 * 1024 * 1024;
324 emu_max_byteprogram_size = 256;
325 emu_max_aai_size = 0;
326 emu_jedec_se_size = 4 * 1024;
327 emu_jedec_be_52_size = 32 * 1024;
328 emu_jedec_be_d8_size = 64 * 1024;
329 emu_jedec_ce_60_size = emu_chip_size;
330 emu_jedec_ce_c7_size = emu_chip_size;
331 msg_pdbg("Emulating Macronix MX25L6436 SPI flash chip (RDID, "
332 "SFDP)\n");
333 }
Nico Huberf9632d82019-01-20 11:23:49 +0100334 if (!strcmp(tmp, "W25Q128FV")) {
335 emu_chip = EMULATE_WINBOND_W25Q128FV;
336 emu_chip_size = 16 * 1024 * 1024;
337 emu_max_byteprogram_size = 256;
338 emu_max_aai_size = 0;
339 emu_jedec_se_size = 4 * 1024;
340 emu_jedec_be_52_size = 32 * 1024;
341 emu_jedec_be_d8_size = 64 * 1024;
342 emu_jedec_ce_60_size = emu_chip_size;
343 emu_jedec_ce_c7_size = emu_chip_size;
344 msg_pdbg("Emulating Winbond W25Q128FV SPI flash chip (RDID)\n");
345 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000346#endif
347 if (emu_chip == EMULATE_NONE) {
348 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
349 free(tmp);
350 return 1;
351 }
352 free(tmp);
353 flashchip_contents = malloc(emu_chip_size);
354 if (!flashchip_contents) {
355 msg_perr("Out of memory!\n");
356 return 1;
357 }
David Hendricks8bb20212011-06-14 01:35:36 +0000358
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000359#ifdef EMULATE_SPI_CHIP
360 status = extract_programmer_param("spi_status");
361 if (status) {
362 char *endptr;
363 errno = 0;
364 emu_status = strtoul(status, &endptr, 0);
365 free(status);
366 if (errno != 0 || status == endptr) {
367 msg_perr("Error: initial status register specified, "
368 "but the value could not be converted.\n");
369 return 1;
370 }
371 msg_pdbg("Initial status register is set to 0x%02x.\n",
372 emu_status);
373 }
374#endif
375
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000376 msg_pdbg("Filling fake flash chip with 0xff, size %i\n", emu_chip_size);
377 memset(flashchip_contents, 0xff, emu_chip_size);
378
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000379 /* Will be freed by shutdown function if necessary. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000380 emu_persistent_image = extract_programmer_param("image");
381 if (!emu_persistent_image) {
382 /* Nothing else to do. */
David Hendricks8bb20212011-06-14 01:35:36 +0000383 goto dummy_init_out;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000384 }
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000385 /* We will silently (in default verbosity) ignore the file if it does not exist (yet) or the size does
386 * not match the emulated chip. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000387 if (!stat(emu_persistent_image, &image_stat)) {
Stefan Tauner23e10b82016-01-23 16:16:49 +0000388 msg_pdbg("Found persistent image %s, %jd B ",
389 emu_persistent_image, (intmax_t)image_stat.st_size);
Nico Huber961f4a12019-10-04 17:34:22 +0200390 if ((uintmax_t)image_stat.st_size == emu_chip_size) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000391 msg_pdbg("matches.\n");
392 msg_pdbg("Reading %s\n", emu_persistent_image);
Jacob Garberca598da2019-08-12 10:44:17 -0600393 if (read_buf_from_file(flashchip_contents, emu_chip_size,
394 emu_persistent_image)) {
395 msg_perr("Unable to read %s\n", emu_persistent_image);
396 free(flashchip_contents);
397 return 1;
398 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000399 } else {
400 msg_pdbg("doesn't match.\n");
401 }
402 }
403#endif
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000404
David Hendricks8bb20212011-06-14 01:35:36 +0000405dummy_init_out:
406 if (register_shutdown(dummy_shutdown, NULL)) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000407 free(flashchip_contents);
David Hendricks8bb20212011-06-14 01:35:36 +0000408 return 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000409 }
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000410 if (dummy_buses_supported & (BUS_PARALLEL | BUS_LPC | BUS_FWH))
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000411 register_par_master(&par_master_dummy,
412 dummy_buses_supported & (BUS_PARALLEL | BUS_LPC | BUS_FWH));
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000413 if (dummy_buses_supported & BUS_SPI)
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000414 register_spi_master(&spi_master_dummyflasher);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000415
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000416 return 0;
417}
418
Stefan Tauner305e0b92013-07-17 23:46:44 +0000419void *dummy_map(const char *descr, uintptr_t phys_addr, size_t len)
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000420{
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000421 msg_pspew("%s: Mapping %s, 0x%zx bytes at 0x%0*" PRIxPTR "\n",
Stefan Tauner0554ca52013-07-25 22:54:25 +0000422 __func__, descr, len, PRIxPTR_WIDTH, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000423 return (void *)phys_addr;
424}
425
426void dummy_unmap(void *virt_addr, size_t len)
427{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000428 msg_pspew("%s: Unmapping 0x%zx bytes at %p\n", __func__, len, virt_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000429}
430
Mark Marshallf20b7be2014-05-09 21:16:21 +0000431static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000432{
Stefan Taunerc2333752013-07-13 23:31:37 +0000433 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%02x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000434}
435
Mark Marshallf20b7be2014-05-09 21:16:21 +0000436static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000437{
Stefan Taunerc2333752013-07-13 23:31:37 +0000438 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%04x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000439}
440
Mark Marshallf20b7be2014-05-09 21:16:21 +0000441static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000442{
Stefan Taunerc2333752013-07-13 23:31:37 +0000443 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%08x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000444}
445
Mark Marshallf20b7be2014-05-09 21:16:21 +0000446static 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 +0000447{
448 size_t i;
Stefan Tauner0554ca52013-07-25 22:54:25 +0000449 msg_pspew("%s: addr=0x%" PRIxPTR ", len=0x%zx, writing data (hex):", __func__, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000450 for (i = 0; i < len; i++) {
451 if ((i % 16) == 0)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000452 msg_pspew("\n");
453 msg_pspew("%02x ", buf[i]);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000454 }
455}
456
Mark Marshallf20b7be2014-05-09 21:16:21 +0000457static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000458{
Stefan Taunerc2333752013-07-13 23:31:37 +0000459 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000460 return 0xff;
461}
462
Mark Marshallf20b7be2014-05-09 21:16:21 +0000463static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000464{
Stefan Taunerc2333752013-07-13 23:31:37 +0000465 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000466 return 0xffff;
467}
468
Mark Marshallf20b7be2014-05-09 21:16:21 +0000469static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000470{
Stefan Taunerc2333752013-07-13 23:31:37 +0000471 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffffffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000472 return 0xffffffff;
473}
474
Mark Marshallf20b7be2014-05-09 21:16:21 +0000475static 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 +0000476{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000477 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 +0000478 memset(buf, 0xff, len);
479 return;
480}
481
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000482#if EMULATE_SPI_CHIP
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000483static int emulate_spi_chip_response(unsigned int writecnt,
484 unsigned int readcnt,
485 const unsigned char *writearr,
486 unsigned char *readarr)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000487{
Stefan Tauner0b9df972012-05-07 22:12:16 +0000488 unsigned int offs, i, toread;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000489 static int unsigned aai_offs;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000490 const unsigned char sst25vf040_rems_response[2] = {0xbf, 0x44};
491 const unsigned char sst25vf032b_rems_response[2] = {0xbf, 0x4a};
492 const unsigned char mx25l6436_rems_response[2] = {0xc2, 0x16};
Nico Huberf9632d82019-01-20 11:23:49 +0100493 const unsigned char w25q128fv_rems_response[2] = {0xef, 0x17};
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000494
495 if (writecnt == 0) {
496 msg_perr("No command sent to the chip!\n");
497 return 1;
498 }
Paul Menzelac427b22012-02-16 21:07:07 +0000499 /* spi_blacklist has precedence over spi_ignorelist. */
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000500 for (i = 0; i < spi_blacklist_size; i++) {
501 if (writearr[0] == spi_blacklist[i]) {
502 msg_pdbg("Refusing blacklisted SPI command 0x%02x\n",
503 spi_blacklist[i]);
504 return SPI_INVALID_OPCODE;
505 }
506 }
507 for (i = 0; i < spi_ignorelist_size; i++) {
508 if (writearr[0] == spi_ignorelist[i]) {
509 msg_cdbg("Ignoring ignorelisted SPI command 0x%02x\n",
510 spi_ignorelist[i]);
511 /* Return success because the command does not fail,
512 * it is simply ignored.
513 */
514 return 0;
515 }
516 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000517
518 if (emu_max_aai_size && (emu_status & SPI_SR_AAI)) {
519 if (writearr[0] != JEDEC_AAI_WORD_PROGRAM &&
520 writearr[0] != JEDEC_WRDI &&
521 writearr[0] != JEDEC_RDSR) {
522 msg_perr("Forbidden opcode (0x%02x) attempted during "
523 "AAI sequence!\n", writearr[0]);
524 return 0;
525 }
526 }
527
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000528 switch (writearr[0]) {
529 case JEDEC_RES:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000530 if (writecnt < JEDEC_RES_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000531 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000532 /* offs calculation is only needed for SST chips which treat RES like REMS. */
533 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
534 offs += writecnt - JEDEC_REMS_OUTSIZE;
535 switch (emu_chip) {
536 case EMULATE_ST_M25P10_RES:
537 if (readcnt > 0)
538 memset(readarr, 0x10, readcnt);
539 break;
540 case EMULATE_SST_SST25VF040_REMS:
541 for (i = 0; i < readcnt; i++)
542 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
543 break;
544 case EMULATE_SST_SST25VF032B:
545 for (i = 0; i < readcnt; i++)
546 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
547 break;
548 case EMULATE_MACRONIX_MX25L6436:
549 if (readcnt > 0)
550 memset(readarr, 0x16, readcnt);
551 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100552 case EMULATE_WINBOND_W25Q128FV:
553 if (readcnt > 0)
554 memset(readarr, 0x17, readcnt);
555 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000556 default: /* ignore */
557 break;
558 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000559 break;
560 case JEDEC_REMS:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000561 /* REMS response has wraparound and uses an address parameter. */
562 if (writecnt < JEDEC_REMS_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000563 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000564 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
565 offs += writecnt - JEDEC_REMS_OUTSIZE;
566 switch (emu_chip) {
567 case EMULATE_SST_SST25VF040_REMS:
568 for (i = 0; i < readcnt; i++)
569 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
570 break;
571 case EMULATE_SST_SST25VF032B:
572 for (i = 0; i < readcnt; i++)
573 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
574 break;
575 case EMULATE_MACRONIX_MX25L6436:
576 for (i = 0; i < readcnt; i++)
577 readarr[i] = mx25l6436_rems_response[(offs + i) % 2];
578 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100579 case EMULATE_WINBOND_W25Q128FV:
580 for (i = 0; i < readcnt; i++)
581 readarr[i] = w25q128fv_rems_response[(offs + i) % 2];
582 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000583 default: /* ignore */
584 break;
585 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000586 break;
587 case JEDEC_RDID:
Stefan Tauner0b9df972012-05-07 22:12:16 +0000588 switch (emu_chip) {
589 case EMULATE_SST_SST25VF032B:
590 if (readcnt > 0)
591 readarr[0] = 0xbf;
592 if (readcnt > 1)
593 readarr[1] = 0x25;
594 if (readcnt > 2)
595 readarr[2] = 0x4a;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000596 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000597 case EMULATE_MACRONIX_MX25L6436:
598 if (readcnt > 0)
599 readarr[0] = 0xc2;
600 if (readcnt > 1)
601 readarr[1] = 0x20;
602 if (readcnt > 2)
603 readarr[2] = 0x17;
604 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100605 case EMULATE_WINBOND_W25Q128FV:
606 if (readcnt > 0)
607 readarr[0] = 0xef;
608 if (readcnt > 1)
609 readarr[1] = 0x40;
610 if (readcnt > 2)
611 readarr[2] = 0x18;
612 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000613 default: /* ignore */
614 break;
615 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000616 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000617 case JEDEC_RDSR:
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000618 memset(readarr, emu_status, readcnt);
619 break;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000620 /* FIXME: this should be chip-specific. */
621 case JEDEC_EWSR:
622 case JEDEC_WREN:
623 emu_status |= SPI_SR_WEL;
624 break;
625 case JEDEC_WRSR:
626 if (!(emu_status & SPI_SR_WEL)) {
627 msg_perr("WRSR attempted, but WEL is 0!\n");
628 break;
629 }
630 /* FIXME: add some reasonable simulation of the busy flag */
631 emu_status = writearr[1] & ~SPI_SR_WIP;
632 msg_pdbg2("WRSR wrote 0x%02x.\n", emu_status);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000633 break;
634 case JEDEC_READ:
635 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
636 /* Truncate to emu_chip_size. */
637 offs %= emu_chip_size;
638 if (readcnt > 0)
639 memcpy(readarr, flashchip_contents + offs, readcnt);
640 break;
641 case JEDEC_BYTE_PROGRAM:
642 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
643 /* Truncate to emu_chip_size. */
644 offs %= emu_chip_size;
645 if (writecnt < 5) {
646 msg_perr("BYTE PROGRAM size too short!\n");
647 return 1;
648 }
649 if (writecnt - 4 > emu_max_byteprogram_size) {
650 msg_perr("Max BYTE PROGRAM size exceeded!\n");
651 return 1;
652 }
653 memcpy(flashchip_contents + offs, writearr + 4, writecnt - 4);
654 break;
655 case JEDEC_AAI_WORD_PROGRAM:
656 if (!emu_max_aai_size)
657 break;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000658 if (!(emu_status & SPI_SR_AAI)) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000659 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
660 msg_perr("Initial AAI WORD PROGRAM size too "
661 "short!\n");
662 return 1;
663 }
664 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
665 msg_perr("Initial AAI WORD PROGRAM size too "
666 "long!\n");
667 return 1;
668 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000669 emu_status |= SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000670 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
671 writearr[3];
672 /* Truncate to emu_chip_size. */
673 aai_offs %= emu_chip_size;
674 memcpy(flashchip_contents + aai_offs, writearr + 4, 2);
675 aai_offs += 2;
676 } else {
677 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
678 msg_perr("Continuation AAI WORD PROGRAM size "
679 "too short!\n");
680 return 1;
681 }
682 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
683 msg_perr("Continuation AAI WORD PROGRAM size "
684 "too long!\n");
685 return 1;
686 }
687 memcpy(flashchip_contents + aai_offs, writearr + 1, 2);
688 aai_offs += 2;
689 }
690 break;
691 case JEDEC_WRDI:
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000692 if (emu_max_aai_size)
693 emu_status &= ~SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000694 break;
695 case JEDEC_SE:
696 if (!emu_jedec_se_size)
697 break;
698 if (writecnt != JEDEC_SE_OUTSIZE) {
699 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
700 return 1;
701 }
702 if (readcnt != JEDEC_SE_INSIZE) {
703 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
704 return 1;
705 }
706 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
707 if (offs & (emu_jedec_se_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000708 msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000709 offs &= ~(emu_jedec_se_size - 1);
710 memset(flashchip_contents + offs, 0xff, emu_jedec_se_size);
711 break;
712 case JEDEC_BE_52:
713 if (!emu_jedec_be_52_size)
714 break;
715 if (writecnt != JEDEC_BE_52_OUTSIZE) {
716 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
717 return 1;
718 }
719 if (readcnt != JEDEC_BE_52_INSIZE) {
720 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
721 return 1;
722 }
723 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
724 if (offs & (emu_jedec_be_52_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000725 msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000726 offs &= ~(emu_jedec_be_52_size - 1);
727 memset(flashchip_contents + offs, 0xff, emu_jedec_be_52_size);
728 break;
729 case JEDEC_BE_D8:
730 if (!emu_jedec_be_d8_size)
731 break;
732 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
733 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
734 return 1;
735 }
736 if (readcnt != JEDEC_BE_D8_INSIZE) {
737 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
738 return 1;
739 }
740 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
741 if (offs & (emu_jedec_be_d8_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000742 msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000743 offs &= ~(emu_jedec_be_d8_size - 1);
744 memset(flashchip_contents + offs, 0xff, emu_jedec_be_d8_size);
745 break;
746 case JEDEC_CE_60:
747 if (!emu_jedec_ce_60_size)
748 break;
749 if (writecnt != JEDEC_CE_60_OUTSIZE) {
750 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
751 return 1;
752 }
753 if (readcnt != JEDEC_CE_60_INSIZE) {
754 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
755 return 1;
756 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000757 /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000758 /* emu_jedec_ce_60_size is emu_chip_size. */
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000759 memset(flashchip_contents, 0xff, emu_jedec_ce_60_size);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000760 break;
761 case JEDEC_CE_C7:
762 if (!emu_jedec_ce_c7_size)
763 break;
764 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
765 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
766 return 1;
767 }
768 if (readcnt != JEDEC_CE_C7_INSIZE) {
769 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
770 return 1;
771 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000772 /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000773 /* emu_jedec_ce_c7_size is emu_chip_size. */
774 memset(flashchip_contents, 0xff, emu_jedec_ce_c7_size);
775 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000776 case JEDEC_SFDP:
777 if (emu_chip != EMULATE_MACRONIX_MX25L6436)
778 break;
779 if (writecnt < 4)
780 break;
781 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
782
783 /* SFDP expects one dummy byte after the address. */
784 if (writecnt == 4) {
785 /* The dummy byte was not written, make sure it is read instead.
786 * Shifting and shortening the read array does achieve this goal.
787 */
788 readarr++;
789 readcnt--;
790 } else {
791 /* The response is shifted if more than 5 bytes are written, because SFDP data is
792 * already shifted out by the chip while those superfluous bytes are written. */
793 offs += writecnt - 5;
794 }
795
796 /* The SFDP spec implies that the start address of an SFDP read may be truncated to fit in the
797 * SFDP table address space, i.e. the start address may be wrapped around at SFDP table size.
798 * This is a reasonable implementation choice in hardware because it saves a few gates. */
799 if (offs >= sizeof(sfdp_table)) {
800 msg_pdbg("Wrapping the start address around the SFDP table boundary (using 0x%x "
801 "instead of 0x%x).\n", (unsigned int)(offs % sizeof(sfdp_table)), offs);
802 offs %= sizeof(sfdp_table);
803 }
804 toread = min(sizeof(sfdp_table) - offs, readcnt);
805 memcpy(readarr, sfdp_table + offs, toread);
806 if (toread < readcnt)
807 msg_pdbg("Crossing the SFDP table boundary in a single "
808 "continuous chunk produces undefined results "
809 "after that point.\n");
810 break;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000811 default:
812 /* No special response. */
813 break;
814 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000815 if (writearr[0] != JEDEC_WREN && writearr[0] != JEDEC_EWSR)
816 emu_status &= ~SPI_SR_WEL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000817 return 0;
818}
819#endif
820
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000821static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000822 unsigned int readcnt,
823 const unsigned char *writearr,
824 unsigned char *readarr)
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000825{
Nico Huber519be662018-12-23 20:03:35 +0100826 unsigned int i;
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000827
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000828 msg_pspew("%s:", __func__);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000829
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000830 msg_pspew(" writing %u bytes:", writecnt);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000831 for (i = 0; i < writecnt; i++)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000832 msg_pspew(" 0x%02x", writearr[i]);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000833
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000834 /* Response for unknown commands and missing chip is 0xff. */
835 memset(readarr, 0xff, readcnt);
836#if EMULATE_SPI_CHIP
837 switch (emu_chip) {
838 case EMULATE_ST_M25P10_RES:
839 case EMULATE_SST_SST25VF040_REMS:
840 case EMULATE_SST_SST25VF032B:
Stefan Tauner0b9df972012-05-07 22:12:16 +0000841 case EMULATE_MACRONIX_MX25L6436:
Nico Huberf9632d82019-01-20 11:23:49 +0100842 case EMULATE_WINBOND_W25Q128FV:
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000843 if (emulate_spi_chip_response(writecnt, readcnt, writearr,
844 readarr)) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000845 msg_pdbg("Invalid command sent to flash chip!\n");
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000846 return 1;
847 }
848 break;
849 default:
850 break;
851 }
852#endif
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000853 msg_pspew(" reading %u bytes:", readcnt);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000854 for (i = 0; i < readcnt; i++)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000855 msg_pspew(" 0x%02x", readarr[i]);
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000856 msg_pspew("\n");
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000857 return 0;
858}
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +0000859
Mark Marshallf20b7be2014-05-09 21:16:21 +0000860static 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 +0000861{
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000862 return spi_write_chunked(flash, buf, start, len,
863 spi_write_256_chunksize);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000864}