blob: a8bf8f09ea5c200f2eecb9ba38532bd5ab0eca7c [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
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000039enum emu_chip {
40 EMULATE_NONE,
41 EMULATE_ST_M25P10_RES,
42 EMULATE_SST_SST25VF040_REMS,
43 EMULATE_SST_SST25VF032B,
Stefan Tauner0b9df972012-05-07 22:12:16 +000044 EMULATE_MACRONIX_MX25L6436,
Nico Huberf9632d82019-01-20 11:23:49 +010045 EMULATE_WINBOND_W25Q128FV,
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000046};
Stefan Tauner0b9df972012-05-07 22:12:16 +000047
Lachlan Bishopc753c402020-09-10 14:57:05 +100048struct emu_data {
49 enum emu_chip emu_chip;
50 char *emu_persistent_image;
51 unsigned int emu_chip_size;
52 int emu_modified; /* is the image modified since reading it? */
53 uint8_t emu_status;
54 unsigned int emu_max_byteprogram_size;
55 unsigned int emu_max_aai_size;
56 unsigned int emu_jedec_se_size;
57 unsigned int emu_jedec_be_52_size;
58 unsigned int emu_jedec_be_d8_size;
59 unsigned int emu_jedec_ce_60_size;
60 unsigned int emu_jedec_ce_c7_size;
61 unsigned char spi_blacklist[256];
62 unsigned char spi_ignorelist[256];
63 unsigned int spi_blacklist_size;
64 unsigned int spi_ignorelist_size;
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +100065
66 uint8_t *flashchip_contents;
Lachlan Bishopc753c402020-09-10 14:57:05 +100067};
68
69#if EMULATE_SPI_CHIP
Stefan Tauner0b9df972012-05-07 22:12:16 +000070/* A legit complete SFDP table based on the MX25L6436E (rev. 1.8) datasheet. */
Stefan Tauner67d163d2013-01-15 17:37:48 +000071static const uint8_t sfdp_table[] = {
Stefan Tauner0b9df972012-05-07 22:12:16 +000072 0x53, 0x46, 0x44, 0x50, // @0x00: SFDP signature
73 0x00, 0x01, 0x01, 0xFF, // @0x04: revision 1.0, 2 headers
74 0x00, 0x00, 0x01, 0x09, // @0x08: JEDEC SFDP header rev. 1.0, 9 DW long
75 0x1C, 0x00, 0x00, 0xFF, // @0x0C: PTP0 = 0x1C (instead of 0x30)
76 0xC2, 0x00, 0x01, 0x04, // @0x10: Macronix header rev. 1.0, 4 DW long
77 0x48, 0x00, 0x00, 0xFF, // @0x14: PTP1 = 0x48 (instead of 0x60)
78 0xFF, 0xFF, 0xFF, 0xFF, // @0x18: hole.
79 0xE5, 0x20, 0xC9, 0xFF, // @0x1C: SFDP parameter table start
80 0xFF, 0xFF, 0xFF, 0x03, // @0x20
81 0x00, 0xFF, 0x08, 0x6B, // @0x24
82 0x08, 0x3B, 0x00, 0xFF, // @0x28
83 0xEE, 0xFF, 0xFF, 0xFF, // @0x2C
84 0xFF, 0xFF, 0x00, 0x00, // @0x30
85 0xFF, 0xFF, 0x00, 0xFF, // @0x34
86 0x0C, 0x20, 0x0F, 0x52, // @0x38
87 0x10, 0xD8, 0x00, 0xFF, // @0x3C: SFDP parameter table end
88 0xFF, 0xFF, 0xFF, 0xFF, // @0x40: hole.
89 0xFF, 0xFF, 0xFF, 0xFF, // @0x44: hole.
90 0x00, 0x36, 0x00, 0x27, // @0x48: Macronix parameter table start
91 0xF4, 0x4F, 0xFF, 0xFF, // @0x4C
92 0xD9, 0xC8, 0xFF, 0xFF, // @0x50
93 0xFF, 0xFF, 0xFF, 0xFF, // @0x54: Macronix parameter table end
94};
95
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000096#endif
97#endif
98
Stefan Taunerc69c9c82011-11-23 09:13:48 +000099static unsigned int spi_write_256_chunksize = 256;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000100
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000101static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
Mark Marshallf20b7be2014-05-09 21:16:21 +0000102 const unsigned char *writearr, unsigned char *readarr);
103static int dummy_spi_write_256(struct flashctx *flash, const uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000104 unsigned int start, unsigned int len);
Mark Marshallf20b7be2014-05-09 21:16:21 +0000105static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr);
106static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr);
107static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr);
108static void dummy_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len);
109static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr);
110static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr);
111static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr);
112static void dummy_chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len);
Michael Karcherb9dbe482011-05-11 17:07:07 +0000113
Lachlan Bishopc753c402020-09-10 14:57:05 +1000114static struct spi_master spi_master_dummyflasher = {
Nico Huber1cf407b2017-11-10 20:18:23 +0100115 .features = SPI_MASTER_4BA,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000116 .max_data_read = MAX_DATA_READ_UNLIMITED,
117 .max_data_write = MAX_DATA_UNSPECIFIED,
118 .command = dummy_spi_send_command,
119 .multicommand = default_spi_send_multicommand,
120 .read = default_spi_read,
121 .write_256 = dummy_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +0000122 .write_aai = default_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000123};
David Hendricks8bb20212011-06-14 01:35:36 +0000124
Lachlan Bishopc753c402020-09-10 14:57:05 +1000125static struct par_master par_master_dummy = {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000126 .chip_readb = dummy_chip_readb,
127 .chip_readw = dummy_chip_readw,
128 .chip_readl = dummy_chip_readl,
129 .chip_readn = dummy_chip_readn,
130 .chip_writeb = dummy_chip_writeb,
131 .chip_writew = dummy_chip_writew,
132 .chip_writel = dummy_chip_writel,
133 .chip_writen = dummy_chip_writen,
134};
135
David Hendricks8bb20212011-06-14 01:35:36 +0000136static int dummy_shutdown(void *data)
137{
138 msg_pspew("%s\n", __func__);
139#if EMULATE_CHIP
Lachlan Bishopc753c402020-09-10 14:57:05 +1000140 struct emu_data *emu_data = (struct emu_data *)data;
141 if (emu_data->emu_chip != EMULATE_NONE) {
142 if (emu_data->emu_persistent_image && emu_data->emu_modified) {
143 msg_pdbg("Writing %s\n", emu_data->emu_persistent_image);
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +1000144 write_buf_to_file(emu_data->flashchip_contents,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000145 emu_data->emu_chip_size,
146 emu_data->emu_persistent_image);
David Hendricks8bb20212011-06-14 01:35:36 +0000147 }
Angel Pons86975952021-05-25 12:46:43 +0200148 free(emu_data->emu_persistent_image);
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +1000149 free(emu_data->flashchip_contents);
David Hendricks8bb20212011-06-14 01:35:36 +0000150 }
151#endif
Lachlan Bishopc753c402020-09-10 14:57:05 +1000152 free(data);
David Hendricks8bb20212011-06-14 01:35:36 +0000153 return 0;
154}
155
Anastasia Klimchuka9f2dca2021-05-31 09:42:36 +1000156static int init_data(struct emu_data *data, enum chipbustype *dummy_buses_supported)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000157{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000158 char *bustext = NULL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000159 char *tmp = NULL;
Nico Huber519be662018-12-23 20:03:35 +0100160 unsigned int i;
Anastasia Klimchuka9f2dca2021-05-31 09:42:36 +1000161 char *endptr;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000162#if EMULATE_SPI_CHIP
163 char *status = NULL;
164#endif
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000165
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000166 bustext = extract_programmer_param("bus");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000167 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
168 if (!bustext)
169 bustext = strdup("parallel+lpc+fwh+spi");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000170 /* Convert the parameters to lowercase. */
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000171 tolower_string(bustext);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000172
Anastasia Klimchuka9f2dca2021-05-31 09:42:36 +1000173 *dummy_buses_supported = BUS_NONE;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000174 if (strstr(bustext, "parallel")) {
Anastasia Klimchuka9f2dca2021-05-31 09:42:36 +1000175 *dummy_buses_supported |= BUS_PARALLEL;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000176 msg_pdbg("Enabling support for %s flash.\n", "parallel");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000177 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000178 if (strstr(bustext, "lpc")) {
Anastasia Klimchuka9f2dca2021-05-31 09:42:36 +1000179 *dummy_buses_supported |= BUS_LPC;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000180 msg_pdbg("Enabling support for %s flash.\n", "LPC");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000181 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000182 if (strstr(bustext, "fwh")) {
Anastasia Klimchuka9f2dca2021-05-31 09:42:36 +1000183 *dummy_buses_supported |= BUS_FWH;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000184 msg_pdbg("Enabling support for %s flash.\n", "FWH");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000185 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000186 if (strstr(bustext, "spi")) {
Anastasia Klimchuka9f2dca2021-05-31 09:42:36 +1000187 *dummy_buses_supported |= BUS_SPI;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000188 msg_pdbg("Enabling support for %s flash.\n", "SPI");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000189 }
Anastasia Klimchuka9f2dca2021-05-31 09:42:36 +1000190 if (*dummy_buses_supported == BUS_NONE)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000191 msg_pdbg("Support for all flash bus types disabled.\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000192 free(bustext);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000193
194 tmp = extract_programmer_param("spi_write_256_chunksize");
195 if (tmp) {
Edward O'Callaghan28868fd2021-05-23 22:14:36 +1000196 spi_write_256_chunksize = strtoul(tmp, &endptr, 0);
197 if (*endptr != '\0' || spi_write_256_chunksize < 1) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000198 msg_perr("invalid spi_write_256_chunksize\n");
Edward O'Callaghan28868fd2021-05-23 22:14:36 +1000199 free(tmp);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000200 return 1;
201 }
Edward O'Callaghan28868fd2021-05-23 22:14:36 +1000202 free(tmp);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000203 }
204
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000205 tmp = extract_programmer_param("spi_blacklist");
206 if (tmp) {
207 i = strlen(tmp);
208 if (!strncmp(tmp, "0x", 2)) {
209 i -= 2;
210 memmove(tmp, tmp + 2, i + 1);
211 }
212 if ((i > 512) || (i % 2)) {
213 msg_perr("Invalid SPI command blacklist length\n");
214 free(tmp);
215 return 1;
216 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000217 data->spi_blacklist_size = i / 2;
218 for (i = 0; i < data->spi_blacklist_size * 2; i++) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000219 if (!isxdigit((unsigned char)tmp[i])) {
220 msg_perr("Invalid char \"%c\" in SPI command "
221 "blacklist\n", tmp[i]);
222 free(tmp);
223 return 1;
224 }
225 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000226 for (i = 0; i < data->spi_blacklist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000227 unsigned int tmp2;
228 /* SCNx8 is apparently not supported by MSVC (and thus
229 * MinGW), so work around it with an extra variable
230 */
231 sscanf(tmp + i * 2, "%2x", &tmp2);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000232 data->spi_blacklist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000233 }
234 msg_pdbg("SPI blacklist is ");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000235 for (i = 0; i < data->spi_blacklist_size; i++)
236 msg_pdbg("%02x ", data->spi_blacklist[i]);
237 msg_pdbg(", size %u\n", data->spi_blacklist_size);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000238 }
239 free(tmp);
240
241 tmp = extract_programmer_param("spi_ignorelist");
242 if (tmp) {
243 i = strlen(tmp);
244 if (!strncmp(tmp, "0x", 2)) {
245 i -= 2;
246 memmove(tmp, tmp + 2, i + 1);
247 }
248 if ((i > 512) || (i % 2)) {
249 msg_perr("Invalid SPI command ignorelist length\n");
250 free(tmp);
251 return 1;
252 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000253 data->spi_ignorelist_size = i / 2;
254 for (i = 0; i < data->spi_ignorelist_size * 2; i++) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000255 if (!isxdigit((unsigned char)tmp[i])) {
256 msg_perr("Invalid char \"%c\" in SPI command "
257 "ignorelist\n", tmp[i]);
258 free(tmp);
259 return 1;
260 }
261 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000262 for (i = 0; i < data->spi_ignorelist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000263 unsigned int tmp2;
264 /* SCNx8 is apparently not supported by MSVC (and thus
265 * MinGW), so work around it with an extra variable
266 */
267 sscanf(tmp + i * 2, "%2x", &tmp2);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000268 data->spi_ignorelist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000269 }
270 msg_pdbg("SPI ignorelist is ");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000271 for (i = 0; i < data->spi_ignorelist_size; i++)
272 msg_pdbg("%02x ", data->spi_ignorelist[i]);
273 msg_pdbg(", size %u\n", data->spi_ignorelist_size);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000274 }
275 free(tmp);
276
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000277#if EMULATE_CHIP
278 tmp = extract_programmer_param("emulate");
279 if (!tmp) {
280 msg_pdbg("Not emulating any flash chip.\n");
281 /* Nothing else to do. */
Anastasia Klimchuka9f2dca2021-05-31 09:42:36 +1000282 return 0;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000283 }
Anastasia Klimchuka9f2dca2021-05-31 09:42:36 +1000284
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000285#if EMULATE_SPI_CHIP
286 if (!strcmp(tmp, "M25P10.RES")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000287 data->emu_chip = EMULATE_ST_M25P10_RES;
288 data->emu_chip_size = 128 * 1024;
289 data->emu_max_byteprogram_size = 128;
290 data->emu_max_aai_size = 0;
291 data->emu_jedec_se_size = 0;
292 data->emu_jedec_be_52_size = 0;
293 data->emu_jedec_be_d8_size = 32 * 1024;
294 data->emu_jedec_ce_60_size = 0;
295 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000296 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
297 "write)\n");
298 }
299 if (!strcmp(tmp, "SST25VF040.REMS")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000300 data->emu_chip = EMULATE_SST_SST25VF040_REMS;
301 data->emu_chip_size = 512 * 1024;
302 data->emu_max_byteprogram_size = 1;
303 data->emu_max_aai_size = 0;
304 data->emu_jedec_se_size = 4 * 1024;
305 data->emu_jedec_be_52_size = 32 * 1024;
306 data->emu_jedec_be_d8_size = 0;
307 data->emu_jedec_ce_60_size = data->emu_chip_size;
308 data->emu_jedec_ce_c7_size = 0;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000309 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
310 "byte write)\n");
311 }
312 if (!strcmp(tmp, "SST25VF032B")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000313 data->emu_chip = EMULATE_SST_SST25VF032B;
314 data->emu_chip_size = 4 * 1024 * 1024;
315 data->emu_max_byteprogram_size = 1;
316 data->emu_max_aai_size = 2;
317 data->emu_jedec_se_size = 4 * 1024;
318 data->emu_jedec_be_52_size = 32 * 1024;
319 data->emu_jedec_be_d8_size = 64 * 1024;
320 data->emu_jedec_ce_60_size = data->emu_chip_size;
321 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000322 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
323 "write)\n");
324 }
Stefan Tauner0b9df972012-05-07 22:12:16 +0000325 if (!strcmp(tmp, "MX25L6436")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000326 data->emu_chip = EMULATE_MACRONIX_MX25L6436;
327 data->emu_chip_size = 8 * 1024 * 1024;
328 data->emu_max_byteprogram_size = 256;
329 data->emu_max_aai_size = 0;
330 data->emu_jedec_se_size = 4 * 1024;
331 data->emu_jedec_be_52_size = 32 * 1024;
332 data->emu_jedec_be_d8_size = 64 * 1024;
333 data->emu_jedec_ce_60_size = data->emu_chip_size;
334 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000335 msg_pdbg("Emulating Macronix MX25L6436 SPI flash chip (RDID, "
336 "SFDP)\n");
337 }
Nico Huberf9632d82019-01-20 11:23:49 +0100338 if (!strcmp(tmp, "W25Q128FV")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000339 data->emu_chip = EMULATE_WINBOND_W25Q128FV;
340 data->emu_chip_size = 16 * 1024 * 1024;
341 data->emu_max_byteprogram_size = 256;
342 data->emu_max_aai_size = 0;
343 data->emu_jedec_se_size = 4 * 1024;
344 data->emu_jedec_be_52_size = 32 * 1024;
345 data->emu_jedec_be_d8_size = 64 * 1024;
346 data->emu_jedec_ce_60_size = data->emu_chip_size;
347 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Nico Huberf9632d82019-01-20 11:23:49 +0100348 msg_pdbg("Emulating Winbond W25Q128FV SPI flash chip (RDID)\n");
349 }
Anastasia Klimchuka9f2dca2021-05-31 09:42:36 +1000350#endif /* EMULATE_SPI_CHIP */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000351 if (data->emu_chip == EMULATE_NONE) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000352 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
353 free(tmp);
354 return 1;
355 }
356 free(tmp);
David Hendricks8bb20212011-06-14 01:35:36 +0000357
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000358#ifdef EMULATE_SPI_CHIP
359 status = extract_programmer_param("spi_status");
360 if (status) {
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000361 errno = 0;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000362 data->emu_status = strtoul(status, &endptr, 0);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000363 if (errno != 0 || status == endptr) {
Angel Ponsd377fe52021-05-25 13:03:24 +0200364 free(status);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000365 msg_perr("Error: initial status register specified, "
366 "but the value could not be converted.\n");
367 return 1;
368 }
Angel Ponsd377fe52021-05-25 13:03:24 +0200369 free(status);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000370 msg_pdbg("Initial status register is set to 0x%02x.\n",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000371 data->emu_status);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000372 }
Anastasia Klimchuka9f2dca2021-05-31 09:42:36 +1000373#endif /* EMULATE_SPI_CHIP */
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000374
Angel Ponsbb3e4e22021-05-25 12:56:18 +0200375 data->flashchip_contents = malloc(data->emu_chip_size);
376 if (!data->flashchip_contents) {
377 msg_perr("Out of memory!\n");
378 return 1;
379 }
380
Anastasia Klimchuka9f2dca2021-05-31 09:42:36 +1000381#endif /* EMULATE_CHIP */
382
383 return 0;
384}
385
386int dummy_init(void)
387{
388#if EMULATE_CHIP
389 struct stat image_stat;
390#endif
391
392 struct emu_data *data = calloc(1, sizeof(struct emu_data));
393 if (!data) {
394 msg_perr("Out of memory!\n");
395 return 1;
396 }
397 data->emu_chip = EMULATE_NONE;
398 spi_master_dummyflasher.data = data;
399 par_master_dummy.data = data;
400
401 msg_pspew("%s\n", __func__);
402
403 enum chipbustype dummy_buses_supported;
404 if (init_data(data, &dummy_buses_supported)) {
405 free(data);
406 return 1;
407 }
408
409#if EMULATE_CHIP
410 if (data->emu_chip == EMULATE_NONE) {
411 msg_pdbg("Not emulating any flash chip.\n");
412 /* Nothing else to do. */
413 goto dummy_init_out;
414 }
415
Lachlan Bishopc753c402020-09-10 14:57:05 +1000416 msg_pdbg("Filling fake flash chip with 0xff, size %i\n", data->emu_chip_size);
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +1000417 memset(data->flashchip_contents, 0xff, data->emu_chip_size);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000418
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000419 /* Will be freed by shutdown function if necessary. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000420 data->emu_persistent_image = extract_programmer_param("image");
421 if (!data->emu_persistent_image) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000422 /* Nothing else to do. */
David Hendricks8bb20212011-06-14 01:35:36 +0000423 goto dummy_init_out;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000424 }
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000425 /* We will silently (in default verbosity) ignore the file if it does not exist (yet) or the size does
426 * not match the emulated chip. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000427 if (!stat(data->emu_persistent_image, &image_stat)) {
Stefan Tauner23e10b82016-01-23 16:16:49 +0000428 msg_pdbg("Found persistent image %s, %jd B ",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000429 data->emu_persistent_image, (intmax_t)image_stat.st_size);
430 if ((uintmax_t)image_stat.st_size == data->emu_chip_size) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000431 msg_pdbg("matches.\n");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000432 msg_pdbg("Reading %s\n", data->emu_persistent_image);
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +1000433 if (read_buf_from_file(data->flashchip_contents, data->emu_chip_size,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000434 data->emu_persistent_image)) {
435 msg_perr("Unable to read %s\n", data->emu_persistent_image);
Angel Pons86975952021-05-25 12:46:43 +0200436 free(data->emu_persistent_image);
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +1000437 free(data->flashchip_contents);
Anastasia Klimchuka9f2dca2021-05-31 09:42:36 +1000438 free(data);
Jacob Garberca598da2019-08-12 10:44:17 -0600439 return 1;
440 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000441 } else {
442 msg_pdbg("doesn't match.\n");
443 }
444 }
Anastasia Klimchuka9f2dca2021-05-31 09:42:36 +1000445#endif /* EMULATE_CHIP */
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000446
David Hendricks8bb20212011-06-14 01:35:36 +0000447dummy_init_out:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000448 if (register_shutdown(dummy_shutdown, data)) {
Angel Pons86975952021-05-25 12:46:43 +0200449 free(data->emu_persistent_image);
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +1000450 free(data->flashchip_contents);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000451 free(data);
David Hendricks8bb20212011-06-14 01:35:36 +0000452 return 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000453 }
Edward O'Callaghancd9d6462021-05-17 20:01:27 +1000454 if (dummy_buses_supported & BUS_NONSPI)
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000455 register_par_master(&par_master_dummy,
Edward O'Callaghancd9d6462021-05-17 20:01:27 +1000456 dummy_buses_supported & BUS_NONSPI);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000457 if (dummy_buses_supported & BUS_SPI)
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000458 register_spi_master(&spi_master_dummyflasher);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000459
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000460 return 0;
461}
462
Stefan Tauner305e0b92013-07-17 23:46:44 +0000463void *dummy_map(const char *descr, uintptr_t phys_addr, size_t len)
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000464{
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000465 msg_pspew("%s: Mapping %s, 0x%zx bytes at 0x%0*" PRIxPTR "\n",
Stefan Tauner0554ca52013-07-25 22:54:25 +0000466 __func__, descr, len, PRIxPTR_WIDTH, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000467 return (void *)phys_addr;
468}
469
470void dummy_unmap(void *virt_addr, size_t len)
471{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000472 msg_pspew("%s: Unmapping 0x%zx bytes at %p\n", __func__, len, virt_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000473}
474
Mark Marshallf20b7be2014-05-09 21:16:21 +0000475static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000476{
Stefan Taunerc2333752013-07-13 23:31:37 +0000477 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%02x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000478}
479
Mark Marshallf20b7be2014-05-09 21:16:21 +0000480static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000481{
Stefan Taunerc2333752013-07-13 23:31:37 +0000482 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%04x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000483}
484
Mark Marshallf20b7be2014-05-09 21:16:21 +0000485static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000486{
Stefan Taunerc2333752013-07-13 23:31:37 +0000487 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%08x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000488}
489
Mark Marshallf20b7be2014-05-09 21:16:21 +0000490static 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 +0000491{
492 size_t i;
Stefan Tauner0554ca52013-07-25 22:54:25 +0000493 msg_pspew("%s: addr=0x%" PRIxPTR ", len=0x%zx, writing data (hex):", __func__, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000494 for (i = 0; i < len; i++) {
495 if ((i % 16) == 0)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000496 msg_pspew("\n");
497 msg_pspew("%02x ", buf[i]);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000498 }
499}
500
Mark Marshallf20b7be2014-05-09 21:16:21 +0000501static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000502{
Stefan Taunerc2333752013-07-13 23:31:37 +0000503 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000504 return 0xff;
505}
506
Mark Marshallf20b7be2014-05-09 21:16:21 +0000507static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000508{
Stefan Taunerc2333752013-07-13 23:31:37 +0000509 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000510 return 0xffff;
511}
512
Mark Marshallf20b7be2014-05-09 21:16:21 +0000513static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000514{
Stefan Taunerc2333752013-07-13 23:31:37 +0000515 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffffffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000516 return 0xffffffff;
517}
518
Mark Marshallf20b7be2014-05-09 21:16:21 +0000519static 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 +0000520{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000521 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 +0000522 memset(buf, 0xff, len);
523 return;
524}
525
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000526#if EMULATE_SPI_CHIP
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000527static int emulate_spi_chip_response(unsigned int writecnt,
528 unsigned int readcnt,
529 const unsigned char *writearr,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000530 unsigned char *readarr,
531 struct emu_data *data)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000532{
Stefan Tauner0b9df972012-05-07 22:12:16 +0000533 unsigned int offs, i, toread;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000534 static int unsigned aai_offs;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000535 const unsigned char sst25vf040_rems_response[2] = {0xbf, 0x44};
536 const unsigned char sst25vf032b_rems_response[2] = {0xbf, 0x4a};
537 const unsigned char mx25l6436_rems_response[2] = {0xc2, 0x16};
Nico Huberf9632d82019-01-20 11:23:49 +0100538 const unsigned char w25q128fv_rems_response[2] = {0xef, 0x17};
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000539
540 if (writecnt == 0) {
541 msg_perr("No command sent to the chip!\n");
542 return 1;
543 }
Paul Menzelac427b22012-02-16 21:07:07 +0000544 /* spi_blacklist has precedence over spi_ignorelist. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000545 for (i = 0; i < data->spi_blacklist_size; i++) {
546 if (writearr[0] == data->spi_blacklist[i]) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000547 msg_pdbg("Refusing blacklisted SPI command 0x%02x\n",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000548 data->spi_blacklist[i]);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000549 return SPI_INVALID_OPCODE;
550 }
551 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000552 for (i = 0; i < data->spi_ignorelist_size; i++) {
553 if (writearr[0] == data->spi_ignorelist[i]) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000554 msg_cdbg("Ignoring ignorelisted SPI command 0x%02x\n",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000555 data->spi_ignorelist[i]);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000556 /* Return success because the command does not fail,
557 * it is simply ignored.
558 */
559 return 0;
560 }
561 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000562
Lachlan Bishopc753c402020-09-10 14:57:05 +1000563 if (data->emu_max_aai_size && (data->emu_status & SPI_SR_AAI)) {
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000564 if (writearr[0] != JEDEC_AAI_WORD_PROGRAM &&
565 writearr[0] != JEDEC_WRDI &&
566 writearr[0] != JEDEC_RDSR) {
567 msg_perr("Forbidden opcode (0x%02x) attempted during "
568 "AAI sequence!\n", writearr[0]);
569 return 0;
570 }
571 }
572
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000573 switch (writearr[0]) {
574 case JEDEC_RES:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000575 if (writecnt < JEDEC_RES_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000576 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000577 /* offs calculation is only needed for SST chips which treat RES like REMS. */
578 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
579 offs += writecnt - JEDEC_REMS_OUTSIZE;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000580 switch (data->emu_chip) {
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000581 case EMULATE_ST_M25P10_RES:
582 if (readcnt > 0)
583 memset(readarr, 0x10, readcnt);
584 break;
585 case EMULATE_SST_SST25VF040_REMS:
586 for (i = 0; i < readcnt; i++)
587 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
588 break;
589 case EMULATE_SST_SST25VF032B:
590 for (i = 0; i < readcnt; i++)
591 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
592 break;
593 case EMULATE_MACRONIX_MX25L6436:
594 if (readcnt > 0)
595 memset(readarr, 0x16, readcnt);
596 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100597 case EMULATE_WINBOND_W25Q128FV:
598 if (readcnt > 0)
599 memset(readarr, 0x17, readcnt);
600 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000601 default: /* ignore */
602 break;
603 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000604 break;
605 case JEDEC_REMS:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000606 /* REMS response has wraparound and uses an address parameter. */
607 if (writecnt < JEDEC_REMS_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000608 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000609 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
610 offs += writecnt - JEDEC_REMS_OUTSIZE;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000611 switch (data->emu_chip) {
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000612 case EMULATE_SST_SST25VF040_REMS:
613 for (i = 0; i < readcnt; i++)
614 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
615 break;
616 case EMULATE_SST_SST25VF032B:
617 for (i = 0; i < readcnt; i++)
618 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
619 break;
620 case EMULATE_MACRONIX_MX25L6436:
621 for (i = 0; i < readcnt; i++)
622 readarr[i] = mx25l6436_rems_response[(offs + i) % 2];
623 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100624 case EMULATE_WINBOND_W25Q128FV:
625 for (i = 0; i < readcnt; i++)
626 readarr[i] = w25q128fv_rems_response[(offs + i) % 2];
627 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000628 default: /* ignore */
629 break;
630 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000631 break;
632 case JEDEC_RDID:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000633 switch (data->emu_chip) {
Stefan Tauner0b9df972012-05-07 22:12:16 +0000634 case EMULATE_SST_SST25VF032B:
635 if (readcnt > 0)
636 readarr[0] = 0xbf;
637 if (readcnt > 1)
638 readarr[1] = 0x25;
639 if (readcnt > 2)
640 readarr[2] = 0x4a;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000641 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000642 case EMULATE_MACRONIX_MX25L6436:
643 if (readcnt > 0)
644 readarr[0] = 0xc2;
645 if (readcnt > 1)
646 readarr[1] = 0x20;
647 if (readcnt > 2)
648 readarr[2] = 0x17;
649 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100650 case EMULATE_WINBOND_W25Q128FV:
651 if (readcnt > 0)
652 readarr[0] = 0xef;
653 if (readcnt > 1)
654 readarr[1] = 0x40;
655 if (readcnt > 2)
656 readarr[2] = 0x18;
657 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000658 default: /* ignore */
659 break;
660 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000661 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000662 case JEDEC_RDSR:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000663 memset(readarr, data->emu_status, readcnt);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000664 break;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000665 /* FIXME: this should be chip-specific. */
666 case JEDEC_EWSR:
667 case JEDEC_WREN:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000668 data->emu_status |= SPI_SR_WEL;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000669 break;
670 case JEDEC_WRSR:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000671 if (!(data->emu_status & SPI_SR_WEL)) {
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000672 msg_perr("WRSR attempted, but WEL is 0!\n");
673 break;
674 }
675 /* FIXME: add some reasonable simulation of the busy flag */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000676 data->emu_status = writearr[1] & ~SPI_SR_WIP;
677 msg_pdbg2("WRSR wrote 0x%02x.\n", data->emu_status);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000678 break;
679 case JEDEC_READ:
680 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
681 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000682 offs %= data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000683 if (readcnt > 0)
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +1000684 memcpy(readarr, data->flashchip_contents + offs, readcnt);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000685 break;
686 case JEDEC_BYTE_PROGRAM:
687 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
688 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000689 offs %= data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000690 if (writecnt < 5) {
691 msg_perr("BYTE PROGRAM size too short!\n");
692 return 1;
693 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000694 if (writecnt - 4 > data->emu_max_byteprogram_size) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000695 msg_perr("Max BYTE PROGRAM size exceeded!\n");
696 return 1;
697 }
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +1000698 memcpy(data->flashchip_contents + offs, writearr + 4, writecnt - 4);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000699 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000700 break;
701 case JEDEC_AAI_WORD_PROGRAM:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000702 if (!data->emu_max_aai_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000703 break;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000704 if (!(data->emu_status & SPI_SR_AAI)) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000705 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
706 msg_perr("Initial AAI WORD PROGRAM size too "
707 "short!\n");
708 return 1;
709 }
710 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
711 msg_perr("Initial AAI WORD PROGRAM size too "
712 "long!\n");
713 return 1;
714 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000715 data->emu_status |= SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000716 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
717 writearr[3];
718 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000719 aai_offs %= data->emu_chip_size;
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +1000720 memcpy(data->flashchip_contents + aai_offs, writearr + 4, 2);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000721 aai_offs += 2;
722 } else {
723 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
724 msg_perr("Continuation AAI WORD PROGRAM size "
725 "too short!\n");
726 return 1;
727 }
728 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
729 msg_perr("Continuation AAI WORD PROGRAM size "
730 "too long!\n");
731 return 1;
732 }
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +1000733 memcpy(data->flashchip_contents + aai_offs, writearr + 1, 2);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000734 aai_offs += 2;
735 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000736 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000737 break;
738 case JEDEC_WRDI:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000739 if (data->emu_max_aai_size)
740 data->emu_status &= ~SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000741 break;
742 case JEDEC_SE:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000743 if (!data->emu_jedec_se_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000744 break;
745 if (writecnt != JEDEC_SE_OUTSIZE) {
746 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
747 return 1;
748 }
749 if (readcnt != JEDEC_SE_INSIZE) {
750 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
751 return 1;
752 }
753 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +1000754 if (offs & (data->emu_jedec_se_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000755 msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000756 offs &= ~(data->emu_jedec_se_size - 1);
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +1000757 memset(data->flashchip_contents + offs, 0xff, data->emu_jedec_se_size);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000758 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000759 break;
760 case JEDEC_BE_52:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000761 if (!data->emu_jedec_be_52_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000762 break;
763 if (writecnt != JEDEC_BE_52_OUTSIZE) {
764 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
765 return 1;
766 }
767 if (readcnt != JEDEC_BE_52_INSIZE) {
768 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
769 return 1;
770 }
771 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +1000772 if (offs & (data->emu_jedec_be_52_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000773 msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000774 offs &= ~(data->emu_jedec_be_52_size - 1);
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +1000775 memset(data->flashchip_contents + offs, 0xff, data->emu_jedec_be_52_size);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000776 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000777 break;
778 case JEDEC_BE_D8:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000779 if (!data->emu_jedec_be_d8_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000780 break;
781 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
782 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
783 return 1;
784 }
785 if (readcnt != JEDEC_BE_D8_INSIZE) {
786 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
787 return 1;
788 }
789 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +1000790 if (offs & (data->emu_jedec_be_d8_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000791 msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000792 offs &= ~(data->emu_jedec_be_d8_size - 1);
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +1000793 memset(data->flashchip_contents + offs, 0xff, data->emu_jedec_be_d8_size);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000794 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000795 break;
796 case JEDEC_CE_60:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000797 if (!data->emu_jedec_ce_60_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000798 break;
799 if (writecnt != JEDEC_CE_60_OUTSIZE) {
800 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
801 return 1;
802 }
803 if (readcnt != JEDEC_CE_60_INSIZE) {
804 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
805 return 1;
806 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000807 /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000808 /* emu_jedec_ce_60_size is emu_chip_size. */
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +1000809 memset(data->flashchip_contents, 0xff, data->emu_jedec_ce_60_size);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000810 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000811 break;
812 case JEDEC_CE_C7:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000813 if (!data->emu_jedec_ce_c7_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000814 break;
815 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
816 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
817 return 1;
818 }
819 if (readcnt != JEDEC_CE_C7_INSIZE) {
820 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
821 return 1;
822 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000823 /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000824 /* emu_jedec_ce_c7_size is emu_chip_size. */
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +1000825 memset(data->flashchip_contents, 0xff, data->emu_jedec_ce_c7_size);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000826 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000827 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000828 case JEDEC_SFDP:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000829 if (data->emu_chip != EMULATE_MACRONIX_MX25L6436)
Stefan Tauner0b9df972012-05-07 22:12:16 +0000830 break;
831 if (writecnt < 4)
832 break;
833 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
834
835 /* SFDP expects one dummy byte after the address. */
836 if (writecnt == 4) {
837 /* The dummy byte was not written, make sure it is read instead.
838 * Shifting and shortening the read array does achieve this goal.
839 */
840 readarr++;
841 readcnt--;
842 } else {
843 /* The response is shifted if more than 5 bytes are written, because SFDP data is
844 * already shifted out by the chip while those superfluous bytes are written. */
845 offs += writecnt - 5;
846 }
847
848 /* The SFDP spec implies that the start address of an SFDP read may be truncated to fit in the
849 * SFDP table address space, i.e. the start address may be wrapped around at SFDP table size.
850 * This is a reasonable implementation choice in hardware because it saves a few gates. */
851 if (offs >= sizeof(sfdp_table)) {
852 msg_pdbg("Wrapping the start address around the SFDP table boundary (using 0x%x "
853 "instead of 0x%x).\n", (unsigned int)(offs % sizeof(sfdp_table)), offs);
854 offs %= sizeof(sfdp_table);
855 }
856 toread = min(sizeof(sfdp_table) - offs, readcnt);
857 memcpy(readarr, sfdp_table + offs, toread);
858 if (toread < readcnt)
859 msg_pdbg("Crossing the SFDP table boundary in a single "
860 "continuous chunk produces undefined results "
861 "after that point.\n");
862 break;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000863 default:
864 /* No special response. */
865 break;
866 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000867 if (writearr[0] != JEDEC_WREN && writearr[0] != JEDEC_EWSR)
Lachlan Bishopc753c402020-09-10 14:57:05 +1000868 data->emu_status &= ~SPI_SR_WEL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000869 return 0;
870}
871#endif
872
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000873static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000874 unsigned int readcnt,
875 const unsigned char *writearr,
876 unsigned char *readarr)
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000877{
Nico Huber519be662018-12-23 20:03:35 +0100878 unsigned int i;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000879 struct emu_data *emu_data = flash->mst->spi.data;
880 if (!emu_data) {
881 msg_perr("No data in flash context!\n");
882 return 1;
883 }
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000884
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000885 msg_pspew("%s:", __func__);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000886
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000887 msg_pspew(" writing %u bytes:", writecnt);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000888 for (i = 0; i < writecnt; i++)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000889 msg_pspew(" 0x%02x", writearr[i]);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000890
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000891 /* Response for unknown commands and missing chip is 0xff. */
892 memset(readarr, 0xff, readcnt);
893#if EMULATE_SPI_CHIP
Lachlan Bishopc753c402020-09-10 14:57:05 +1000894 switch (emu_data->emu_chip) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000895 case EMULATE_ST_M25P10_RES:
896 case EMULATE_SST_SST25VF040_REMS:
897 case EMULATE_SST_SST25VF032B:
Stefan Tauner0b9df972012-05-07 22:12:16 +0000898 case EMULATE_MACRONIX_MX25L6436:
Nico Huberf9632d82019-01-20 11:23:49 +0100899 case EMULATE_WINBOND_W25Q128FV:
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000900 if (emulate_spi_chip_response(writecnt, readcnt, writearr,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000901 readarr, emu_data)) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000902 msg_pdbg("Invalid command sent to flash chip!\n");
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000903 return 1;
904 }
905 break;
906 default:
907 break;
908 }
909#endif
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000910 msg_pspew(" reading %u bytes:", readcnt);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000911 for (i = 0; i < readcnt; i++)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000912 msg_pspew(" 0x%02x", readarr[i]);
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000913 msg_pspew("\n");
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000914 return 0;
915}
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +0000916
Mark Marshallf20b7be2014-05-09 21:16:21 +0000917static 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 +0000918{
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000919 return spi_write_chunked(flash, buf, start, len,
920 spi_write_256_chunksize);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000921}