blob: 4ed53010842d3c2fbb9ed4803cab573830e744cd [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'Callaghan94250222021-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'Callaghan94250222021-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 Pons02b9ae22021-05-25 12:46:43 +0200148 free(emu_data->emu_persistent_image);
Edward O'Callaghan94250222021-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
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000156int dummy_init(void)
157{
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;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000161#if EMULATE_SPI_CHIP
162 char *status = NULL;
163#endif
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000164#if EMULATE_CHIP
165 struct stat image_stat;
166#endif
Edward O'Callaghanc785f882021-05-23 22:14:36 +1000167 char *endptr;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000168
Lachlan Bishopc753c402020-09-10 14:57:05 +1000169 struct emu_data *data = calloc(1, sizeof(struct emu_data));
170 if (!data) {
171 msg_perr("Out of memory!\n");
172 return 1;
173 }
174 data->emu_chip = EMULATE_NONE;
175 spi_master_dummyflasher.data = data;
176 par_master_dummy.data = data;
177
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000178 msg_pspew("%s\n", __func__);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000179
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000180 bustext = extract_programmer_param("bus");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000181 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
182 if (!bustext)
183 bustext = strdup("parallel+lpc+fwh+spi");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000184 /* Convert the parameters to lowercase. */
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000185 tolower_string(bustext);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000186
Lachlan Bishopc753c402020-09-10 14:57:05 +1000187 enum chipbustype dummy_buses_supported = BUS_NONE;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000188 if (strstr(bustext, "parallel")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000189 dummy_buses_supported |= BUS_PARALLEL;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000190 msg_pdbg("Enabling support for %s flash.\n", "parallel");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000191 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000192 if (strstr(bustext, "lpc")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000193 dummy_buses_supported |= BUS_LPC;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000194 msg_pdbg("Enabling support for %s flash.\n", "LPC");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000195 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000196 if (strstr(bustext, "fwh")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000197 dummy_buses_supported |= BUS_FWH;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000198 msg_pdbg("Enabling support for %s flash.\n", "FWH");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000199 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000200 if (strstr(bustext, "spi")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000201 dummy_buses_supported |= BUS_SPI;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000202 msg_pdbg("Enabling support for %s flash.\n", "SPI");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000203 }
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000204 if (dummy_buses_supported == BUS_NONE)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000205 msg_pdbg("Support for all flash bus types disabled.\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000206 free(bustext);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000207
208 tmp = extract_programmer_param("spi_write_256_chunksize");
209 if (tmp) {
Edward O'Callaghanc785f882021-05-23 22:14:36 +1000210 spi_write_256_chunksize = strtoul(tmp, &endptr, 0);
211 if (*endptr != '\0' || spi_write_256_chunksize < 1) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000212 msg_perr("invalid spi_write_256_chunksize\n");
Edward O'Callaghanc785f882021-05-23 22:14:36 +1000213 free(tmp);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000214 return 1;
215 }
Edward O'Callaghanc785f882021-05-23 22:14:36 +1000216 free(tmp);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000217 }
218
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000219 tmp = extract_programmer_param("spi_blacklist");
220 if (tmp) {
221 i = strlen(tmp);
222 if (!strncmp(tmp, "0x", 2)) {
223 i -= 2;
224 memmove(tmp, tmp + 2, i + 1);
225 }
226 if ((i > 512) || (i % 2)) {
227 msg_perr("Invalid SPI command blacklist length\n");
228 free(tmp);
229 return 1;
230 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000231 data->spi_blacklist_size = i / 2;
232 for (i = 0; i < data->spi_blacklist_size * 2; i++) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000233 if (!isxdigit((unsigned char)tmp[i])) {
234 msg_perr("Invalid char \"%c\" in SPI command "
235 "blacklist\n", tmp[i]);
236 free(tmp);
237 return 1;
238 }
239 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000240 for (i = 0; i < data->spi_blacklist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000241 unsigned int tmp2;
242 /* SCNx8 is apparently not supported by MSVC (and thus
243 * MinGW), so work around it with an extra variable
244 */
245 sscanf(tmp + i * 2, "%2x", &tmp2);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000246 data->spi_blacklist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000247 }
248 msg_pdbg("SPI blacklist is ");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000249 for (i = 0; i < data->spi_blacklist_size; i++)
250 msg_pdbg("%02x ", data->spi_blacklist[i]);
251 msg_pdbg(", size %u\n", data->spi_blacklist_size);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000252 }
253 free(tmp);
254
255 tmp = extract_programmer_param("spi_ignorelist");
256 if (tmp) {
257 i = strlen(tmp);
258 if (!strncmp(tmp, "0x", 2)) {
259 i -= 2;
260 memmove(tmp, tmp + 2, i + 1);
261 }
262 if ((i > 512) || (i % 2)) {
263 msg_perr("Invalid SPI command ignorelist length\n");
264 free(tmp);
265 return 1;
266 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000267 data->spi_ignorelist_size = i / 2;
268 for (i = 0; i < data->spi_ignorelist_size * 2; i++) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000269 if (!isxdigit((unsigned char)tmp[i])) {
270 msg_perr("Invalid char \"%c\" in SPI command "
271 "ignorelist\n", tmp[i]);
272 free(tmp);
273 return 1;
274 }
275 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000276 for (i = 0; i < data->spi_ignorelist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000277 unsigned int tmp2;
278 /* SCNx8 is apparently not supported by MSVC (and thus
279 * MinGW), so work around it with an extra variable
280 */
281 sscanf(tmp + i * 2, "%2x", &tmp2);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000282 data->spi_ignorelist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000283 }
284 msg_pdbg("SPI ignorelist is ");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000285 for (i = 0; i < data->spi_ignorelist_size; i++)
286 msg_pdbg("%02x ", data->spi_ignorelist[i]);
287 msg_pdbg(", size %u\n", data->spi_ignorelist_size);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000288 }
289 free(tmp);
290
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000291#if EMULATE_CHIP
292 tmp = extract_programmer_param("emulate");
293 if (!tmp) {
294 msg_pdbg("Not emulating any flash chip.\n");
295 /* Nothing else to do. */
David Hendricks8bb20212011-06-14 01:35:36 +0000296 goto dummy_init_out;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000297 }
298#if EMULATE_SPI_CHIP
299 if (!strcmp(tmp, "M25P10.RES")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000300 data->emu_chip = EMULATE_ST_M25P10_RES;
301 data->emu_chip_size = 128 * 1024;
302 data->emu_max_byteprogram_size = 128;
303 data->emu_max_aai_size = 0;
304 data->emu_jedec_se_size = 0;
305 data->emu_jedec_be_52_size = 0;
306 data->emu_jedec_be_d8_size = 32 * 1024;
307 data->emu_jedec_ce_60_size = 0;
308 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000309 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
310 "write)\n");
311 }
312 if (!strcmp(tmp, "SST25VF040.REMS")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000313 data->emu_chip = EMULATE_SST_SST25VF040_REMS;
314 data->emu_chip_size = 512 * 1024;
315 data->emu_max_byteprogram_size = 1;
316 data->emu_max_aai_size = 0;
317 data->emu_jedec_se_size = 4 * 1024;
318 data->emu_jedec_be_52_size = 32 * 1024;
319 data->emu_jedec_be_d8_size = 0;
320 data->emu_jedec_ce_60_size = data->emu_chip_size;
321 data->emu_jedec_ce_c7_size = 0;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000322 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
323 "byte write)\n");
324 }
325 if (!strcmp(tmp, "SST25VF032B")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000326 data->emu_chip = EMULATE_SST_SST25VF032B;
327 data->emu_chip_size = 4 * 1024 * 1024;
328 data->emu_max_byteprogram_size = 1;
329 data->emu_max_aai_size = 2;
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;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000335 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
336 "write)\n");
337 }
Stefan Tauner0b9df972012-05-07 22:12:16 +0000338 if (!strcmp(tmp, "MX25L6436")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000339 data->emu_chip = EMULATE_MACRONIX_MX25L6436;
340 data->emu_chip_size = 8 * 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;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000348 msg_pdbg("Emulating Macronix MX25L6436 SPI flash chip (RDID, "
349 "SFDP)\n");
350 }
Nico Huberf9632d82019-01-20 11:23:49 +0100351 if (!strcmp(tmp, "W25Q128FV")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000352 data->emu_chip = EMULATE_WINBOND_W25Q128FV;
353 data->emu_chip_size = 16 * 1024 * 1024;
354 data->emu_max_byteprogram_size = 256;
355 data->emu_max_aai_size = 0;
356 data->emu_jedec_se_size = 4 * 1024;
357 data->emu_jedec_be_52_size = 32 * 1024;
358 data->emu_jedec_be_d8_size = 64 * 1024;
359 data->emu_jedec_ce_60_size = data->emu_chip_size;
360 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Nico Huberf9632d82019-01-20 11:23:49 +0100361 msg_pdbg("Emulating Winbond W25Q128FV SPI flash chip (RDID)\n");
362 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000363#endif
Lachlan Bishopc753c402020-09-10 14:57:05 +1000364 if (data->emu_chip == EMULATE_NONE) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000365 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
366 free(tmp);
367 return 1;
368 }
369 free(tmp);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000370 data->flashchip_contents = malloc(data->emu_chip_size);
371 if (!data->flashchip_contents) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000372 msg_perr("Out of memory!\n");
373 return 1;
374 }
David Hendricks8bb20212011-06-14 01:35:36 +0000375
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000376#ifdef EMULATE_SPI_CHIP
377 status = extract_programmer_param("spi_status");
378 if (status) {
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000379 errno = 0;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000380 data->emu_status = strtoul(status, &endptr, 0);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000381 free(status);
382 if (errno != 0 || status == endptr) {
383 msg_perr("Error: initial status register specified, "
384 "but the value could not be converted.\n");
385 return 1;
386 }
387 msg_pdbg("Initial status register is set to 0x%02x.\n",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000388 data->emu_status);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000389 }
390#endif
391
Lachlan Bishopc753c402020-09-10 14:57:05 +1000392 msg_pdbg("Filling fake flash chip with 0xff, size %i\n", data->emu_chip_size);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000393 memset(data->flashchip_contents, 0xff, data->emu_chip_size);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000394
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000395 /* Will be freed by shutdown function if necessary. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000396 data->emu_persistent_image = extract_programmer_param("image");
397 if (!data->emu_persistent_image) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000398 /* Nothing else to do. */
David Hendricks8bb20212011-06-14 01:35:36 +0000399 goto dummy_init_out;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000400 }
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000401 /* We will silently (in default verbosity) ignore the file if it does not exist (yet) or the size does
402 * not match the emulated chip. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000403 if (!stat(data->emu_persistent_image, &image_stat)) {
Stefan Tauner23e10b82016-01-23 16:16:49 +0000404 msg_pdbg("Found persistent image %s, %jd B ",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000405 data->emu_persistent_image, (intmax_t)image_stat.st_size);
406 if ((uintmax_t)image_stat.st_size == data->emu_chip_size) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000407 msg_pdbg("matches.\n");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000408 msg_pdbg("Reading %s\n", data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000409 if (read_buf_from_file(data->flashchip_contents, data->emu_chip_size,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000410 data->emu_persistent_image)) {
411 msg_perr("Unable to read %s\n", data->emu_persistent_image);
Angel Pons02b9ae22021-05-25 12:46:43 +0200412 free(data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000413 free(data->flashchip_contents);
Jacob Garberca598da2019-08-12 10:44:17 -0600414 return 1;
415 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000416 } else {
417 msg_pdbg("doesn't match.\n");
418 }
419 }
420#endif
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000421
David Hendricks8bb20212011-06-14 01:35:36 +0000422dummy_init_out:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000423 if (register_shutdown(dummy_shutdown, data)) {
Angel Pons02b9ae22021-05-25 12:46:43 +0200424 free(data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000425 free(data->flashchip_contents);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000426 free(data);
David Hendricks8bb20212011-06-14 01:35:36 +0000427 return 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000428 }
Edward O'Callaghan3fa321d2021-05-17 20:01:27 +1000429 if (dummy_buses_supported & BUS_NONSPI)
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000430 register_par_master(&par_master_dummy,
Edward O'Callaghan3fa321d2021-05-17 20:01:27 +1000431 dummy_buses_supported & BUS_NONSPI);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000432 if (dummy_buses_supported & BUS_SPI)
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000433 register_spi_master(&spi_master_dummyflasher);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000434
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000435 return 0;
436}
437
Stefan Tauner305e0b92013-07-17 23:46:44 +0000438void *dummy_map(const char *descr, uintptr_t phys_addr, size_t len)
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000439{
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000440 msg_pspew("%s: Mapping %s, 0x%zx bytes at 0x%0*" PRIxPTR "\n",
Stefan Tauner0554ca52013-07-25 22:54:25 +0000441 __func__, descr, len, PRIxPTR_WIDTH, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000442 return (void *)phys_addr;
443}
444
445void dummy_unmap(void *virt_addr, size_t len)
446{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000447 msg_pspew("%s: Unmapping 0x%zx bytes at %p\n", __func__, len, virt_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000448}
449
Mark Marshallf20b7be2014-05-09 21:16:21 +0000450static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000451{
Stefan Taunerc2333752013-07-13 23:31:37 +0000452 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%02x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000453}
454
Mark Marshallf20b7be2014-05-09 21:16:21 +0000455static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000456{
Stefan Taunerc2333752013-07-13 23:31:37 +0000457 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%04x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000458}
459
Mark Marshallf20b7be2014-05-09 21:16:21 +0000460static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000461{
Stefan Taunerc2333752013-07-13 23:31:37 +0000462 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%08x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000463}
464
Mark Marshallf20b7be2014-05-09 21:16:21 +0000465static 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 +0000466{
467 size_t i;
Stefan Tauner0554ca52013-07-25 22:54:25 +0000468 msg_pspew("%s: addr=0x%" PRIxPTR ", len=0x%zx, writing data (hex):", __func__, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000469 for (i = 0; i < len; i++) {
470 if ((i % 16) == 0)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000471 msg_pspew("\n");
472 msg_pspew("%02x ", buf[i]);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000473 }
474}
475
Mark Marshallf20b7be2014-05-09 21:16:21 +0000476static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000477{
Stefan Taunerc2333752013-07-13 23:31:37 +0000478 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000479 return 0xff;
480}
481
Mark Marshallf20b7be2014-05-09 21:16:21 +0000482static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000483{
Stefan Taunerc2333752013-07-13 23:31:37 +0000484 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000485 return 0xffff;
486}
487
Mark Marshallf20b7be2014-05-09 21:16:21 +0000488static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000489{
Stefan Taunerc2333752013-07-13 23:31:37 +0000490 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffffffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000491 return 0xffffffff;
492}
493
Mark Marshallf20b7be2014-05-09 21:16:21 +0000494static 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 +0000495{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000496 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 +0000497 memset(buf, 0xff, len);
498 return;
499}
500
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000501#if EMULATE_SPI_CHIP
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000502static int emulate_spi_chip_response(unsigned int writecnt,
503 unsigned int readcnt,
504 const unsigned char *writearr,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000505 unsigned char *readarr,
506 struct emu_data *data)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000507{
Stefan Tauner0b9df972012-05-07 22:12:16 +0000508 unsigned int offs, i, toread;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000509 static int unsigned aai_offs;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000510 const unsigned char sst25vf040_rems_response[2] = {0xbf, 0x44};
511 const unsigned char sst25vf032b_rems_response[2] = {0xbf, 0x4a};
512 const unsigned char mx25l6436_rems_response[2] = {0xc2, 0x16};
Nico Huberf9632d82019-01-20 11:23:49 +0100513 const unsigned char w25q128fv_rems_response[2] = {0xef, 0x17};
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000514
515 if (writecnt == 0) {
516 msg_perr("No command sent to the chip!\n");
517 return 1;
518 }
Paul Menzelac427b22012-02-16 21:07:07 +0000519 /* spi_blacklist has precedence over spi_ignorelist. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000520 for (i = 0; i < data->spi_blacklist_size; i++) {
521 if (writearr[0] == data->spi_blacklist[i]) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000522 msg_pdbg("Refusing blacklisted SPI command 0x%02x\n",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000523 data->spi_blacklist[i]);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000524 return SPI_INVALID_OPCODE;
525 }
526 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000527 for (i = 0; i < data->spi_ignorelist_size; i++) {
528 if (writearr[0] == data->spi_ignorelist[i]) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000529 msg_cdbg("Ignoring ignorelisted SPI command 0x%02x\n",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000530 data->spi_ignorelist[i]);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000531 /* Return success because the command does not fail,
532 * it is simply ignored.
533 */
534 return 0;
535 }
536 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000537
Lachlan Bishopc753c402020-09-10 14:57:05 +1000538 if (data->emu_max_aai_size && (data->emu_status & SPI_SR_AAI)) {
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000539 if (writearr[0] != JEDEC_AAI_WORD_PROGRAM &&
540 writearr[0] != JEDEC_WRDI &&
541 writearr[0] != JEDEC_RDSR) {
542 msg_perr("Forbidden opcode (0x%02x) attempted during "
543 "AAI sequence!\n", writearr[0]);
544 return 0;
545 }
546 }
547
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000548 switch (writearr[0]) {
549 case JEDEC_RES:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000550 if (writecnt < JEDEC_RES_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000551 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000552 /* offs calculation is only needed for SST chips which treat RES like REMS. */
553 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
554 offs += writecnt - JEDEC_REMS_OUTSIZE;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000555 switch (data->emu_chip) {
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000556 case EMULATE_ST_M25P10_RES:
557 if (readcnt > 0)
558 memset(readarr, 0x10, readcnt);
559 break;
560 case EMULATE_SST_SST25VF040_REMS:
561 for (i = 0; i < readcnt; i++)
562 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
563 break;
564 case EMULATE_SST_SST25VF032B:
565 for (i = 0; i < readcnt; i++)
566 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
567 break;
568 case EMULATE_MACRONIX_MX25L6436:
569 if (readcnt > 0)
570 memset(readarr, 0x16, readcnt);
571 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100572 case EMULATE_WINBOND_W25Q128FV:
573 if (readcnt > 0)
574 memset(readarr, 0x17, readcnt);
575 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000576 default: /* ignore */
577 break;
578 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000579 break;
580 case JEDEC_REMS:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000581 /* REMS response has wraparound and uses an address parameter. */
582 if (writecnt < JEDEC_REMS_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000583 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000584 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
585 offs += writecnt - JEDEC_REMS_OUTSIZE;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000586 switch (data->emu_chip) {
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000587 case EMULATE_SST_SST25VF040_REMS:
588 for (i = 0; i < readcnt; i++)
589 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
590 break;
591 case EMULATE_SST_SST25VF032B:
592 for (i = 0; i < readcnt; i++)
593 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
594 break;
595 case EMULATE_MACRONIX_MX25L6436:
596 for (i = 0; i < readcnt; i++)
597 readarr[i] = mx25l6436_rems_response[(offs + i) % 2];
598 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100599 case EMULATE_WINBOND_W25Q128FV:
600 for (i = 0; i < readcnt; i++)
601 readarr[i] = w25q128fv_rems_response[(offs + i) % 2];
602 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000603 default: /* ignore */
604 break;
605 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000606 break;
607 case JEDEC_RDID:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000608 switch (data->emu_chip) {
Stefan Tauner0b9df972012-05-07 22:12:16 +0000609 case EMULATE_SST_SST25VF032B:
610 if (readcnt > 0)
611 readarr[0] = 0xbf;
612 if (readcnt > 1)
613 readarr[1] = 0x25;
614 if (readcnt > 2)
615 readarr[2] = 0x4a;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000616 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000617 case EMULATE_MACRONIX_MX25L6436:
618 if (readcnt > 0)
619 readarr[0] = 0xc2;
620 if (readcnt > 1)
621 readarr[1] = 0x20;
622 if (readcnt > 2)
623 readarr[2] = 0x17;
624 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100625 case EMULATE_WINBOND_W25Q128FV:
626 if (readcnt > 0)
627 readarr[0] = 0xef;
628 if (readcnt > 1)
629 readarr[1] = 0x40;
630 if (readcnt > 2)
631 readarr[2] = 0x18;
632 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000633 default: /* ignore */
634 break;
635 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000636 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000637 case JEDEC_RDSR:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000638 memset(readarr, data->emu_status, readcnt);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000639 break;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000640 /* FIXME: this should be chip-specific. */
641 case JEDEC_EWSR:
642 case JEDEC_WREN:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000643 data->emu_status |= SPI_SR_WEL;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000644 break;
645 case JEDEC_WRSR:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000646 if (!(data->emu_status & SPI_SR_WEL)) {
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000647 msg_perr("WRSR attempted, but WEL is 0!\n");
648 break;
649 }
650 /* FIXME: add some reasonable simulation of the busy flag */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000651 data->emu_status = writearr[1] & ~SPI_SR_WIP;
652 msg_pdbg2("WRSR wrote 0x%02x.\n", data->emu_status);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000653 break;
654 case JEDEC_READ:
655 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
656 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000657 offs %= data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000658 if (readcnt > 0)
Edward O'Callaghan94250222021-05-20 20:34:02 +1000659 memcpy(readarr, data->flashchip_contents + offs, readcnt);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000660 break;
661 case JEDEC_BYTE_PROGRAM:
662 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
663 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000664 offs %= data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000665 if (writecnt < 5) {
666 msg_perr("BYTE PROGRAM size too short!\n");
667 return 1;
668 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000669 if (writecnt - 4 > data->emu_max_byteprogram_size) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000670 msg_perr("Max BYTE PROGRAM size exceeded!\n");
671 return 1;
672 }
Edward O'Callaghan94250222021-05-20 20:34:02 +1000673 memcpy(data->flashchip_contents + offs, writearr + 4, writecnt - 4);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000674 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000675 break;
676 case JEDEC_AAI_WORD_PROGRAM:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000677 if (!data->emu_max_aai_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000678 break;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000679 if (!(data->emu_status & SPI_SR_AAI)) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000680 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
681 msg_perr("Initial AAI WORD PROGRAM size too "
682 "short!\n");
683 return 1;
684 }
685 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
686 msg_perr("Initial AAI WORD PROGRAM size too "
687 "long!\n");
688 return 1;
689 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000690 data->emu_status |= SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000691 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
692 writearr[3];
693 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000694 aai_offs %= data->emu_chip_size;
Edward O'Callaghan94250222021-05-20 20:34:02 +1000695 memcpy(data->flashchip_contents + aai_offs, writearr + 4, 2);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000696 aai_offs += 2;
697 } else {
698 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
699 msg_perr("Continuation AAI WORD PROGRAM size "
700 "too short!\n");
701 return 1;
702 }
703 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
704 msg_perr("Continuation AAI WORD PROGRAM size "
705 "too long!\n");
706 return 1;
707 }
Edward O'Callaghan94250222021-05-20 20:34:02 +1000708 memcpy(data->flashchip_contents + aai_offs, writearr + 1, 2);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000709 aai_offs += 2;
710 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000711 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000712 break;
713 case JEDEC_WRDI:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000714 if (data->emu_max_aai_size)
715 data->emu_status &= ~SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000716 break;
717 case JEDEC_SE:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000718 if (!data->emu_jedec_se_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000719 break;
720 if (writecnt != JEDEC_SE_OUTSIZE) {
721 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
722 return 1;
723 }
724 if (readcnt != JEDEC_SE_INSIZE) {
725 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
726 return 1;
727 }
728 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +1000729 if (offs & (data->emu_jedec_se_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000730 msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000731 offs &= ~(data->emu_jedec_se_size - 1);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000732 memset(data->flashchip_contents + offs, 0xff, data->emu_jedec_se_size);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000733 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000734 break;
735 case JEDEC_BE_52:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000736 if (!data->emu_jedec_be_52_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000737 break;
738 if (writecnt != JEDEC_BE_52_OUTSIZE) {
739 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
740 return 1;
741 }
742 if (readcnt != JEDEC_BE_52_INSIZE) {
743 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
744 return 1;
745 }
746 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +1000747 if (offs & (data->emu_jedec_be_52_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000748 msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000749 offs &= ~(data->emu_jedec_be_52_size - 1);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000750 memset(data->flashchip_contents + offs, 0xff, data->emu_jedec_be_52_size);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000751 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000752 break;
753 case JEDEC_BE_D8:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000754 if (!data->emu_jedec_be_d8_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000755 break;
756 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
757 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
758 return 1;
759 }
760 if (readcnt != JEDEC_BE_D8_INSIZE) {
761 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
762 return 1;
763 }
764 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +1000765 if (offs & (data->emu_jedec_be_d8_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000766 msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000767 offs &= ~(data->emu_jedec_be_d8_size - 1);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000768 memset(data->flashchip_contents + offs, 0xff, data->emu_jedec_be_d8_size);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000769 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000770 break;
771 case JEDEC_CE_60:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000772 if (!data->emu_jedec_ce_60_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000773 break;
774 if (writecnt != JEDEC_CE_60_OUTSIZE) {
775 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
776 return 1;
777 }
778 if (readcnt != JEDEC_CE_60_INSIZE) {
779 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
780 return 1;
781 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000782 /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000783 /* emu_jedec_ce_60_size is emu_chip_size. */
Edward O'Callaghan94250222021-05-20 20:34:02 +1000784 memset(data->flashchip_contents, 0xff, data->emu_jedec_ce_60_size);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000785 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000786 break;
787 case JEDEC_CE_C7:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000788 if (!data->emu_jedec_ce_c7_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000789 break;
790 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
791 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
792 return 1;
793 }
794 if (readcnt != JEDEC_CE_C7_INSIZE) {
795 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
796 return 1;
797 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000798 /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000799 /* emu_jedec_ce_c7_size is emu_chip_size. */
Edward O'Callaghan94250222021-05-20 20:34:02 +1000800 memset(data->flashchip_contents, 0xff, data->emu_jedec_ce_c7_size);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000801 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000802 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000803 case JEDEC_SFDP:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000804 if (data->emu_chip != EMULATE_MACRONIX_MX25L6436)
Stefan Tauner0b9df972012-05-07 22:12:16 +0000805 break;
806 if (writecnt < 4)
807 break;
808 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
809
810 /* SFDP expects one dummy byte after the address. */
811 if (writecnt == 4) {
812 /* The dummy byte was not written, make sure it is read instead.
813 * Shifting and shortening the read array does achieve this goal.
814 */
815 readarr++;
816 readcnt--;
817 } else {
818 /* The response is shifted if more than 5 bytes are written, because SFDP data is
819 * already shifted out by the chip while those superfluous bytes are written. */
820 offs += writecnt - 5;
821 }
822
823 /* The SFDP spec implies that the start address of an SFDP read may be truncated to fit in the
824 * SFDP table address space, i.e. the start address may be wrapped around at SFDP table size.
825 * This is a reasonable implementation choice in hardware because it saves a few gates. */
826 if (offs >= sizeof(sfdp_table)) {
827 msg_pdbg("Wrapping the start address around the SFDP table boundary (using 0x%x "
828 "instead of 0x%x).\n", (unsigned int)(offs % sizeof(sfdp_table)), offs);
829 offs %= sizeof(sfdp_table);
830 }
831 toread = min(sizeof(sfdp_table) - offs, readcnt);
832 memcpy(readarr, sfdp_table + offs, toread);
833 if (toread < readcnt)
834 msg_pdbg("Crossing the SFDP table boundary in a single "
835 "continuous chunk produces undefined results "
836 "after that point.\n");
837 break;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000838 default:
839 /* No special response. */
840 break;
841 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000842 if (writearr[0] != JEDEC_WREN && writearr[0] != JEDEC_EWSR)
Lachlan Bishopc753c402020-09-10 14:57:05 +1000843 data->emu_status &= ~SPI_SR_WEL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000844 return 0;
845}
846#endif
847
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000848static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000849 unsigned int readcnt,
850 const unsigned char *writearr,
851 unsigned char *readarr)
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000852{
Nico Huber519be662018-12-23 20:03:35 +0100853 unsigned int i;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000854 struct emu_data *emu_data = flash->mst->spi.data;
855 if (!emu_data) {
856 msg_perr("No data in flash context!\n");
857 return 1;
858 }
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000859
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000860 msg_pspew("%s:", __func__);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000861
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000862 msg_pspew(" writing %u bytes:", writecnt);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000863 for (i = 0; i < writecnt; i++)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000864 msg_pspew(" 0x%02x", writearr[i]);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000865
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000866 /* Response for unknown commands and missing chip is 0xff. */
867 memset(readarr, 0xff, readcnt);
868#if EMULATE_SPI_CHIP
Lachlan Bishopc753c402020-09-10 14:57:05 +1000869 switch (emu_data->emu_chip) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000870 case EMULATE_ST_M25P10_RES:
871 case EMULATE_SST_SST25VF040_REMS:
872 case EMULATE_SST_SST25VF032B:
Stefan Tauner0b9df972012-05-07 22:12:16 +0000873 case EMULATE_MACRONIX_MX25L6436:
Nico Huberf9632d82019-01-20 11:23:49 +0100874 case EMULATE_WINBOND_W25Q128FV:
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000875 if (emulate_spi_chip_response(writecnt, readcnt, writearr,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000876 readarr, emu_data)) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000877 msg_pdbg("Invalid command sent to flash chip!\n");
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000878 return 1;
879 }
880 break;
881 default:
882 break;
883 }
884#endif
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000885 msg_pspew(" reading %u bytes:", readcnt);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000886 for (i = 0; i < readcnt; i++)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000887 msg_pspew(" 0x%02x", readarr[i]);
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000888 msg_pspew("\n");
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000889 return 0;
890}
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +0000891
Mark Marshallf20b7be2014-05-09 21:16:21 +0000892static 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 +0000893{
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000894 return spi_write_chunked(flash, buf, start, len,
895 spi_write_256_chunksize);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000896}