blob: eaf8ccd4c2d68fe720f70e8695fab91e9a595141 [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
Sergii Dmytruk2ac444f2021-11-08 00:05:12 +020016#include <assert.h>
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000017#include <string.h>
18#include <stdlib.h>
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +000019#include <stdio.h>
20#include <ctype.h>
Stefan Tauner5e695ab2012-05-06 17:03:40 +000021#include <errno.h>
Nico Huber81d3f3e2021-06-09 18:10:07 +020022#include <sys/types.h>
23#include <sys/stat.h>
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000024#include "flash.h"
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +000025#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000026#include "programmer.h"
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000027
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000028#include "spi.h"
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000029
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000030enum emu_chip {
31 EMULATE_NONE,
32 EMULATE_ST_M25P10_RES,
33 EMULATE_SST_SST25VF040_REMS,
34 EMULATE_SST_SST25VF032B,
Stefan Tauner0b9df972012-05-07 22:12:16 +000035 EMULATE_MACRONIX_MX25L6436,
Nico Huberf9632d82019-01-20 11:23:49 +010036 EMULATE_WINBOND_W25Q128FV,
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000037};
Stefan Tauner0b9df972012-05-07 22:12:16 +000038
Lachlan Bishopc753c402020-09-10 14:57:05 +100039struct emu_data {
40 enum emu_chip emu_chip;
41 char *emu_persistent_image;
42 unsigned int emu_chip_size;
Sergii Dmytruk2ac444f2021-11-08 00:05:12 +020043 /* Note: W25Q128FV doesn't change value of SR2 if it's not provided, but
44 * even its previous generations do, so don't forget to update
45 * WRSR code on enabling WRSR_EXT for more chips. */
46 bool emu_wrsr_ext;
Lachlan Bishopc753c402020-09-10 14:57:05 +100047 int emu_modified; /* is the image modified since reading it? */
Sergii Dmytruk2ac444f2021-11-08 00:05:12 +020048 uint8_t emu_status[3];
49 uint8_t emu_status_len; /* number of emulated status registers */
Lachlan Bishopc753c402020-09-10 14:57:05 +100050 unsigned int emu_max_byteprogram_size;
51 unsigned int emu_max_aai_size;
52 unsigned int emu_jedec_se_size;
53 unsigned int emu_jedec_be_52_size;
54 unsigned int emu_jedec_be_d8_size;
55 unsigned int emu_jedec_ce_60_size;
56 unsigned int emu_jedec_ce_c7_size;
57 unsigned char spi_blacklist[256];
58 unsigned char spi_ignorelist[256];
59 unsigned int spi_blacklist_size;
60 unsigned int spi_ignorelist_size;
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +100061
62 uint8_t *flashchip_contents;
Lachlan Bishopc753c402020-09-10 14:57:05 +100063};
64
Stefan Tauner0b9df972012-05-07 22:12:16 +000065/* A legit complete SFDP table based on the MX25L6436E (rev. 1.8) datasheet. */
Stefan Tauner67d163d2013-01-15 17:37:48 +000066static const uint8_t sfdp_table[] = {
Stefan Tauner0b9df972012-05-07 22:12:16 +000067 0x53, 0x46, 0x44, 0x50, // @0x00: SFDP signature
68 0x00, 0x01, 0x01, 0xFF, // @0x04: revision 1.0, 2 headers
69 0x00, 0x00, 0x01, 0x09, // @0x08: JEDEC SFDP header rev. 1.0, 9 DW long
70 0x1C, 0x00, 0x00, 0xFF, // @0x0C: PTP0 = 0x1C (instead of 0x30)
71 0xC2, 0x00, 0x01, 0x04, // @0x10: Macronix header rev. 1.0, 4 DW long
72 0x48, 0x00, 0x00, 0xFF, // @0x14: PTP1 = 0x48 (instead of 0x60)
73 0xFF, 0xFF, 0xFF, 0xFF, // @0x18: hole.
74 0xE5, 0x20, 0xC9, 0xFF, // @0x1C: SFDP parameter table start
75 0xFF, 0xFF, 0xFF, 0x03, // @0x20
76 0x00, 0xFF, 0x08, 0x6B, // @0x24
77 0x08, 0x3B, 0x00, 0xFF, // @0x28
78 0xEE, 0xFF, 0xFF, 0xFF, // @0x2C
79 0xFF, 0xFF, 0x00, 0x00, // @0x30
80 0xFF, 0xFF, 0x00, 0xFF, // @0x34
81 0x0C, 0x20, 0x0F, 0x52, // @0x38
82 0x10, 0xD8, 0x00, 0xFF, // @0x3C: SFDP parameter table end
83 0xFF, 0xFF, 0xFF, 0xFF, // @0x40: hole.
84 0xFF, 0xFF, 0xFF, 0xFF, // @0x44: hole.
85 0x00, 0x36, 0x00, 0x27, // @0x48: Macronix parameter table start
86 0xF4, 0x4F, 0xFF, 0xFF, // @0x4C
87 0xD9, 0xC8, 0xFF, 0xFF, // @0x50
88 0xFF, 0xFF, 0xFF, 0xFF, // @0x54: Macronix parameter table end
89};
90
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000091
Stefan Taunerc69c9c82011-11-23 09:13:48 +000092static unsigned int spi_write_256_chunksize = 256;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000093
Edward O'Callaghan5eca4272020-04-12 17:27:53 +100094static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
Mark Marshallf20b7be2014-05-09 21:16:21 +000095 const unsigned char *writearr, unsigned char *readarr);
96static int dummy_spi_write_256(struct flashctx *flash, const uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +000097 unsigned int start, unsigned int len);
Mark Marshallf20b7be2014-05-09 21:16:21 +000098static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr);
99static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr);
100static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr);
101static void dummy_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len);
102static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr);
103static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr);
104static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr);
105static void dummy_chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len);
Michael Karcherb9dbe482011-05-11 17:07:07 +0000106
Lachlan Bishopc753c402020-09-10 14:57:05 +1000107static struct spi_master spi_master_dummyflasher = {
Nico Huber1cf407b2017-11-10 20:18:23 +0100108 .features = SPI_MASTER_4BA,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000109 .max_data_read = MAX_DATA_READ_UNLIMITED,
110 .max_data_write = MAX_DATA_UNSPECIFIED,
111 .command = dummy_spi_send_command,
112 .multicommand = default_spi_send_multicommand,
113 .read = default_spi_read,
114 .write_256 = dummy_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +0000115 .write_aai = default_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000116};
David Hendricks8bb20212011-06-14 01:35:36 +0000117
Lachlan Bishopc753c402020-09-10 14:57:05 +1000118static struct par_master par_master_dummy = {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000119 .chip_readb = dummy_chip_readb,
120 .chip_readw = dummy_chip_readw,
121 .chip_readl = dummy_chip_readl,
122 .chip_readn = dummy_chip_readn,
123 .chip_writeb = dummy_chip_writeb,
124 .chip_writew = dummy_chip_writew,
125 .chip_writel = dummy_chip_writel,
126 .chip_writen = dummy_chip_writen,
127};
128
David Hendricks8bb20212011-06-14 01:35:36 +0000129static int dummy_shutdown(void *data)
130{
131 msg_pspew("%s\n", __func__);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000132 struct emu_data *emu_data = (struct emu_data *)data;
133 if (emu_data->emu_chip != EMULATE_NONE) {
134 if (emu_data->emu_persistent_image && emu_data->emu_modified) {
135 msg_pdbg("Writing %s\n", emu_data->emu_persistent_image);
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +1000136 write_buf_to_file(emu_data->flashchip_contents,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000137 emu_data->emu_chip_size,
138 emu_data->emu_persistent_image);
David Hendricks8bb20212011-06-14 01:35:36 +0000139 }
Angel Pons86975952021-05-25 12:46:43 +0200140 free(emu_data->emu_persistent_image);
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +1000141 free(emu_data->flashchip_contents);
David Hendricks8bb20212011-06-14 01:35:36 +0000142 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000143 free(data);
David Hendricks8bb20212011-06-14 01:35:36 +0000144 return 0;
145}
146
Anastasia Klimchuka9f2dca2021-05-31 09:42:36 +1000147static int init_data(struct emu_data *data, enum chipbustype *dummy_buses_supported)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000148{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000149 char *bustext = NULL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000150 char *tmp = NULL;
Nico Huber519be662018-12-23 20:03:35 +0100151 unsigned int i;
Anastasia Klimchuka9f2dca2021-05-31 09:42:36 +1000152 char *endptr;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000153 char *status = NULL;
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000154
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000155 bustext = extract_programmer_param("bus");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000156 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
157 if (!bustext)
158 bustext = strdup("parallel+lpc+fwh+spi");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000159 /* Convert the parameters to lowercase. */
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000160 tolower_string(bustext);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000161
Anastasia Klimchuka9f2dca2021-05-31 09:42:36 +1000162 *dummy_buses_supported = BUS_NONE;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000163 if (strstr(bustext, "parallel")) {
Anastasia Klimchuka9f2dca2021-05-31 09:42:36 +1000164 *dummy_buses_supported |= BUS_PARALLEL;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000165 msg_pdbg("Enabling support for %s flash.\n", "parallel");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000166 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000167 if (strstr(bustext, "lpc")) {
Anastasia Klimchuka9f2dca2021-05-31 09:42:36 +1000168 *dummy_buses_supported |= BUS_LPC;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000169 msg_pdbg("Enabling support for %s flash.\n", "LPC");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000170 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000171 if (strstr(bustext, "fwh")) {
Anastasia Klimchuka9f2dca2021-05-31 09:42:36 +1000172 *dummy_buses_supported |= BUS_FWH;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000173 msg_pdbg("Enabling support for %s flash.\n", "FWH");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000174 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000175 if (strstr(bustext, "spi")) {
Anastasia Klimchuka9f2dca2021-05-31 09:42:36 +1000176 *dummy_buses_supported |= BUS_SPI;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000177 msg_pdbg("Enabling support for %s flash.\n", "SPI");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000178 }
Anastasia Klimchuka9f2dca2021-05-31 09:42:36 +1000179 if (*dummy_buses_supported == BUS_NONE)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000180 msg_pdbg("Support for all flash bus types disabled.\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000181 free(bustext);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000182
183 tmp = extract_programmer_param("spi_write_256_chunksize");
184 if (tmp) {
Edward O'Callaghan28868fd2021-05-23 22:14:36 +1000185 spi_write_256_chunksize = strtoul(tmp, &endptr, 0);
186 if (*endptr != '\0' || spi_write_256_chunksize < 1) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000187 msg_perr("invalid spi_write_256_chunksize\n");
Edward O'Callaghan28868fd2021-05-23 22:14:36 +1000188 free(tmp);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000189 return 1;
190 }
Edward O'Callaghan28868fd2021-05-23 22:14:36 +1000191 free(tmp);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000192 }
193
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000194 tmp = extract_programmer_param("spi_blacklist");
195 if (tmp) {
196 i = strlen(tmp);
197 if (!strncmp(tmp, "0x", 2)) {
198 i -= 2;
199 memmove(tmp, tmp + 2, i + 1);
200 }
201 if ((i > 512) || (i % 2)) {
202 msg_perr("Invalid SPI command blacklist length\n");
203 free(tmp);
204 return 1;
205 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000206 data->spi_blacklist_size = i / 2;
207 for (i = 0; i < data->spi_blacklist_size * 2; i++) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000208 if (!isxdigit((unsigned char)tmp[i])) {
209 msg_perr("Invalid char \"%c\" in SPI command "
210 "blacklist\n", tmp[i]);
211 free(tmp);
212 return 1;
213 }
214 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000215 for (i = 0; i < data->spi_blacklist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000216 unsigned int tmp2;
217 /* SCNx8 is apparently not supported by MSVC (and thus
218 * MinGW), so work around it with an extra variable
219 */
220 sscanf(tmp + i * 2, "%2x", &tmp2);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000221 data->spi_blacklist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000222 }
223 msg_pdbg("SPI blacklist is ");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000224 for (i = 0; i < data->spi_blacklist_size; i++)
225 msg_pdbg("%02x ", data->spi_blacklist[i]);
226 msg_pdbg(", size %u\n", data->spi_blacklist_size);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000227 }
228 free(tmp);
229
230 tmp = extract_programmer_param("spi_ignorelist");
231 if (tmp) {
232 i = strlen(tmp);
233 if (!strncmp(tmp, "0x", 2)) {
234 i -= 2;
235 memmove(tmp, tmp + 2, i + 1);
236 }
237 if ((i > 512) || (i % 2)) {
238 msg_perr("Invalid SPI command ignorelist length\n");
239 free(tmp);
240 return 1;
241 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000242 data->spi_ignorelist_size = i / 2;
243 for (i = 0; i < data->spi_ignorelist_size * 2; i++) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000244 if (!isxdigit((unsigned char)tmp[i])) {
245 msg_perr("Invalid char \"%c\" in SPI command "
246 "ignorelist\n", tmp[i]);
247 free(tmp);
248 return 1;
249 }
250 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000251 for (i = 0; i < data->spi_ignorelist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000252 unsigned int tmp2;
253 /* SCNx8 is apparently not supported by MSVC (and thus
254 * MinGW), so work around it with an extra variable
255 */
256 sscanf(tmp + i * 2, "%2x", &tmp2);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000257 data->spi_ignorelist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000258 }
259 msg_pdbg("SPI ignorelist is ");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000260 for (i = 0; i < data->spi_ignorelist_size; i++)
261 msg_pdbg("%02x ", data->spi_ignorelist[i]);
262 msg_pdbg(", size %u\n", data->spi_ignorelist_size);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000263 }
264 free(tmp);
265
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000266 tmp = extract_programmer_param("emulate");
267 if (!tmp) {
268 msg_pdbg("Not emulating any flash chip.\n");
269 /* Nothing else to do. */
Anastasia Klimchuka9f2dca2021-05-31 09:42:36 +1000270 return 0;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000271 }
Anastasia Klimchuka9f2dca2021-05-31 09:42:36 +1000272
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000273 if (!strcmp(tmp, "M25P10.RES")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000274 data->emu_chip = EMULATE_ST_M25P10_RES;
275 data->emu_chip_size = 128 * 1024;
276 data->emu_max_byteprogram_size = 128;
277 data->emu_max_aai_size = 0;
Sergii Dmytruk2ac444f2021-11-08 00:05:12 +0200278 data->emu_status_len = 1;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000279 data->emu_jedec_se_size = 0;
280 data->emu_jedec_be_52_size = 0;
281 data->emu_jedec_be_d8_size = 32 * 1024;
282 data->emu_jedec_ce_60_size = 0;
283 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000284 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
285 "write)\n");
286 }
287 if (!strcmp(tmp, "SST25VF040.REMS")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000288 data->emu_chip = EMULATE_SST_SST25VF040_REMS;
289 data->emu_chip_size = 512 * 1024;
290 data->emu_max_byteprogram_size = 1;
291 data->emu_max_aai_size = 0;
Sergii Dmytruk2ac444f2021-11-08 00:05:12 +0200292 data->emu_status_len = 1;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000293 data->emu_jedec_se_size = 4 * 1024;
294 data->emu_jedec_be_52_size = 32 * 1024;
295 data->emu_jedec_be_d8_size = 0;
296 data->emu_jedec_ce_60_size = data->emu_chip_size;
297 data->emu_jedec_ce_c7_size = 0;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000298 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
299 "byte write)\n");
300 }
301 if (!strcmp(tmp, "SST25VF032B")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000302 data->emu_chip = EMULATE_SST_SST25VF032B;
303 data->emu_chip_size = 4 * 1024 * 1024;
304 data->emu_max_byteprogram_size = 1;
305 data->emu_max_aai_size = 2;
Sergii Dmytruk2ac444f2021-11-08 00:05:12 +0200306 data->emu_status_len = 1;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000307 data->emu_jedec_se_size = 4 * 1024;
308 data->emu_jedec_be_52_size = 32 * 1024;
309 data->emu_jedec_be_d8_size = 64 * 1024;
310 data->emu_jedec_ce_60_size = data->emu_chip_size;
311 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000312 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
313 "write)\n");
314 }
Stefan Tauner0b9df972012-05-07 22:12:16 +0000315 if (!strcmp(tmp, "MX25L6436")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000316 data->emu_chip = EMULATE_MACRONIX_MX25L6436;
317 data->emu_chip_size = 8 * 1024 * 1024;
318 data->emu_max_byteprogram_size = 256;
319 data->emu_max_aai_size = 0;
Sergii Dmytruk2ac444f2021-11-08 00:05:12 +0200320 data->emu_status_len = 1;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000321 data->emu_jedec_se_size = 4 * 1024;
322 data->emu_jedec_be_52_size = 32 * 1024;
323 data->emu_jedec_be_d8_size = 64 * 1024;
324 data->emu_jedec_ce_60_size = data->emu_chip_size;
325 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000326 msg_pdbg("Emulating Macronix MX25L6436 SPI flash chip (RDID, "
327 "SFDP)\n");
328 }
Nico Huberf9632d82019-01-20 11:23:49 +0100329 if (!strcmp(tmp, "W25Q128FV")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000330 data->emu_chip = EMULATE_WINBOND_W25Q128FV;
331 data->emu_chip_size = 16 * 1024 * 1024;
332 data->emu_max_byteprogram_size = 256;
333 data->emu_max_aai_size = 0;
Sergii Dmytruk2ac444f2021-11-08 00:05:12 +0200334 data->emu_status_len = 1;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000335 data->emu_jedec_se_size = 4 * 1024;
336 data->emu_jedec_be_52_size = 32 * 1024;
337 data->emu_jedec_be_d8_size = 64 * 1024;
338 data->emu_jedec_ce_60_size = data->emu_chip_size;
339 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Nico Huberf9632d82019-01-20 11:23:49 +0100340 msg_pdbg("Emulating Winbond W25Q128FV SPI flash chip (RDID)\n");
341 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000342 if (data->emu_chip == EMULATE_NONE) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000343 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
344 free(tmp);
345 return 1;
346 }
347 free(tmp);
David Hendricks8bb20212011-06-14 01:35:36 +0000348
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000349 status = extract_programmer_param("spi_status");
350 if (status) {
Sergii Dmytruk2ac444f2021-11-08 00:05:12 +0200351 unsigned int emu_status;
352
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000353 errno = 0;
Sergii Dmytruk2ac444f2021-11-08 00:05:12 +0200354 emu_status = strtoul(status, &endptr, 0);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000355 if (errno != 0 || status == endptr) {
Angel Ponsd377fe52021-05-25 13:03:24 +0200356 free(status);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000357 msg_perr("Error: initial status register specified, "
358 "but the value could not be converted.\n");
359 return 1;
360 }
Angel Ponsd377fe52021-05-25 13:03:24 +0200361 free(status);
Sergii Dmytruk2ac444f2021-11-08 00:05:12 +0200362
363 data->emu_status[0] = emu_status;
364 data->emu_status[1] = emu_status >> 8;
365 data->emu_status[2] = emu_status >> 16;
366
367 if (data->emu_status_len == 3) {
368 msg_pdbg("Initial status registers:\n"
369 "\tSR1 is set to 0x%02x\n"
370 "\tSR2 is set to 0x%02x\n"
371 "\tSR3 is set to 0x%02x\n",
372 data->emu_status[0], data->emu_status[1], data->emu_status[2]);
373 } else if (data->emu_status_len == 2) {
374 msg_pdbg("Initial status registers:\n"
375 "\tSR1 is set to 0x%02x\n"
376 "\tSR2 is set to 0x%02x\n",
377 data->emu_status[0], data->emu_status[1]);
378 } else {
379 msg_pdbg("Initial status register is set to 0x%02x.\n",
380 data->emu_status[0]);
381 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000382 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000383
Angel Ponsbb3e4e22021-05-25 12:56:18 +0200384 data->flashchip_contents = malloc(data->emu_chip_size);
385 if (!data->flashchip_contents) {
386 msg_perr("Out of memory!\n");
387 return 1;
388 }
389
Anastasia Klimchuka9f2dca2021-05-31 09:42:36 +1000390
391 return 0;
392}
393
Thomas Heijligenddfaff02021-05-04 15:32:17 +0200394static int dummy_init(void)
Anastasia Klimchuka9f2dca2021-05-31 09:42:36 +1000395{
Anastasia Klimchuka9f2dca2021-05-31 09:42:36 +1000396 struct stat image_stat;
Anastasia Klimchuka9f2dca2021-05-31 09:42:36 +1000397
398 struct emu_data *data = calloc(1, sizeof(struct emu_data));
399 if (!data) {
400 msg_perr("Out of memory!\n");
401 return 1;
402 }
403 data->emu_chip = EMULATE_NONE;
404 spi_master_dummyflasher.data = data;
405 par_master_dummy.data = data;
406
407 msg_pspew("%s\n", __func__);
408
409 enum chipbustype dummy_buses_supported;
410 if (init_data(data, &dummy_buses_supported)) {
411 free(data);
412 return 1;
413 }
414
Anastasia Klimchuka9f2dca2021-05-31 09:42:36 +1000415 if (data->emu_chip == EMULATE_NONE) {
416 msg_pdbg("Not emulating any flash chip.\n");
417 /* Nothing else to do. */
418 goto dummy_init_out;
419 }
420
Lachlan Bishopc753c402020-09-10 14:57:05 +1000421 msg_pdbg("Filling fake flash chip with 0xff, size %i\n", data->emu_chip_size);
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +1000422 memset(data->flashchip_contents, 0xff, data->emu_chip_size);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000423
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000424 /* Will be freed by shutdown function if necessary. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000425 data->emu_persistent_image = extract_programmer_param("image");
426 if (!data->emu_persistent_image) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000427 /* Nothing else to do. */
David Hendricks8bb20212011-06-14 01:35:36 +0000428 goto dummy_init_out;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000429 }
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000430 /* We will silently (in default verbosity) ignore the file if it does not exist (yet) or the size does
431 * not match the emulated chip. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000432 if (!stat(data->emu_persistent_image, &image_stat)) {
Stefan Tauner23e10b82016-01-23 16:16:49 +0000433 msg_pdbg("Found persistent image %s, %jd B ",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000434 data->emu_persistent_image, (intmax_t)image_stat.st_size);
435 if ((uintmax_t)image_stat.st_size == data->emu_chip_size) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000436 msg_pdbg("matches.\n");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000437 msg_pdbg("Reading %s\n", data->emu_persistent_image);
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +1000438 if (read_buf_from_file(data->flashchip_contents, data->emu_chip_size,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000439 data->emu_persistent_image)) {
440 msg_perr("Unable to read %s\n", data->emu_persistent_image);
Angel Pons86975952021-05-25 12:46:43 +0200441 free(data->emu_persistent_image);
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +1000442 free(data->flashchip_contents);
Anastasia Klimchuka9f2dca2021-05-31 09:42:36 +1000443 free(data);
Jacob Garberca598da2019-08-12 10:44:17 -0600444 return 1;
445 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000446 } else {
447 msg_pdbg("doesn't match.\n");
448 }
449 }
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000450
David Hendricks8bb20212011-06-14 01:35:36 +0000451dummy_init_out:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000452 if (register_shutdown(dummy_shutdown, data)) {
Angel Pons86975952021-05-25 12:46:43 +0200453 free(data->emu_persistent_image);
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +1000454 free(data->flashchip_contents);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000455 free(data);
David Hendricks8bb20212011-06-14 01:35:36 +0000456 return 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000457 }
Edward O'Callaghancd9d6462021-05-17 20:01:27 +1000458 if (dummy_buses_supported & BUS_NONSPI)
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000459 register_par_master(&par_master_dummy,
Edward O'Callaghancd9d6462021-05-17 20:01:27 +1000460 dummy_buses_supported & BUS_NONSPI);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000461 if (dummy_buses_supported & BUS_SPI)
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000462 register_spi_master(&spi_master_dummyflasher);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000463
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000464 return 0;
465}
466
Thomas Heijligenddfaff02021-05-04 15:32:17 +0200467static void *dummy_map(const char *descr, uintptr_t phys_addr, size_t len)
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000468{
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000469 msg_pspew("%s: Mapping %s, 0x%zx bytes at 0x%0*" PRIxPTR "\n",
Stefan Tauner0554ca52013-07-25 22:54:25 +0000470 __func__, descr, len, PRIxPTR_WIDTH, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000471 return (void *)phys_addr;
472}
473
Thomas Heijligenddfaff02021-05-04 15:32:17 +0200474static void dummy_unmap(void *virt_addr, size_t len)
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000475{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000476 msg_pspew("%s: Unmapping 0x%zx bytes at %p\n", __func__, len, virt_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000477}
478
Mark Marshallf20b7be2014-05-09 21:16:21 +0000479static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000480{
Stefan Taunerc2333752013-07-13 23:31:37 +0000481 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%02x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000482}
483
Mark Marshallf20b7be2014-05-09 21:16:21 +0000484static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000485{
Stefan Taunerc2333752013-07-13 23:31:37 +0000486 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%04x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000487}
488
Mark Marshallf20b7be2014-05-09 21:16:21 +0000489static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000490{
Stefan Taunerc2333752013-07-13 23:31:37 +0000491 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%08x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000492}
493
Mark Marshallf20b7be2014-05-09 21:16:21 +0000494static 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 +0000495{
496 size_t i;
Stefan Tauner0554ca52013-07-25 22:54:25 +0000497 msg_pspew("%s: addr=0x%" PRIxPTR ", len=0x%zx, writing data (hex):", __func__, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000498 for (i = 0; i < len; i++) {
499 if ((i % 16) == 0)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000500 msg_pspew("\n");
501 msg_pspew("%02x ", buf[i]);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000502 }
503}
504
Mark Marshallf20b7be2014-05-09 21:16:21 +0000505static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000506{
Stefan Taunerc2333752013-07-13 23:31:37 +0000507 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000508 return 0xff;
509}
510
Mark Marshallf20b7be2014-05-09 21:16:21 +0000511static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000512{
Stefan Taunerc2333752013-07-13 23:31:37 +0000513 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000514 return 0xffff;
515}
516
Mark Marshallf20b7be2014-05-09 21:16:21 +0000517static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000518{
Stefan Taunerc2333752013-07-13 23:31:37 +0000519 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffffffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000520 return 0xffffffff;
521}
522
Mark Marshallf20b7be2014-05-09 21:16:21 +0000523static 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 +0000524{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000525 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 +0000526 memset(buf, 0xff, len);
527 return;
528}
529
Sergii Dmytruk2ac444f2021-11-08 00:05:12 +0200530static uint8_t get_reg_ro_bit_mask(enum flash_reg reg)
531{
532 /* Whoever adds a new register must not forget to update this function
533 or at least shouldn't use it incorrectly. */
534 assert(reg == STATUS1 || reg == STATUS2 || reg == STATUS3);
535
536 return reg == STATUS1 ? SPI_SR_WIP : 0;
537}
538
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000539static int emulate_spi_chip_response(unsigned int writecnt,
540 unsigned int readcnt,
541 const unsigned char *writearr,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000542 unsigned char *readarr,
543 struct emu_data *data)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000544{
Stefan Tauner0b9df972012-05-07 22:12:16 +0000545 unsigned int offs, i, toread;
Sergii Dmytruk2ac444f2021-11-08 00:05:12 +0200546 uint8_t ro_bits;
547 bool wrsr_ext;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000548 static int unsigned aai_offs;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000549 const unsigned char sst25vf040_rems_response[2] = {0xbf, 0x44};
550 const unsigned char sst25vf032b_rems_response[2] = {0xbf, 0x4a};
551 const unsigned char mx25l6436_rems_response[2] = {0xc2, 0x16};
Nico Huberf9632d82019-01-20 11:23:49 +0100552 const unsigned char w25q128fv_rems_response[2] = {0xef, 0x17};
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000553
554 if (writecnt == 0) {
555 msg_perr("No command sent to the chip!\n");
556 return 1;
557 }
Paul Menzelac427b22012-02-16 21:07:07 +0000558 /* spi_blacklist has precedence over spi_ignorelist. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000559 for (i = 0; i < data->spi_blacklist_size; i++) {
560 if (writearr[0] == data->spi_blacklist[i]) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000561 msg_pdbg("Refusing blacklisted SPI command 0x%02x\n",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000562 data->spi_blacklist[i]);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000563 return SPI_INVALID_OPCODE;
564 }
565 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000566 for (i = 0; i < data->spi_ignorelist_size; i++) {
567 if (writearr[0] == data->spi_ignorelist[i]) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000568 msg_cdbg("Ignoring ignorelisted SPI command 0x%02x\n",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000569 data->spi_ignorelist[i]);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000570 /* Return success because the command does not fail,
571 * it is simply ignored.
572 */
573 return 0;
574 }
575 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000576
Sergii Dmytruk2ac444f2021-11-08 00:05:12 +0200577 if (data->emu_max_aai_size && (data->emu_status[0] & SPI_SR_AAI)) {
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000578 if (writearr[0] != JEDEC_AAI_WORD_PROGRAM &&
579 writearr[0] != JEDEC_WRDI &&
580 writearr[0] != JEDEC_RDSR) {
581 msg_perr("Forbidden opcode (0x%02x) attempted during "
582 "AAI sequence!\n", writearr[0]);
583 return 0;
584 }
585 }
586
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000587 switch (writearr[0]) {
588 case JEDEC_RES:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000589 if (writecnt < JEDEC_RES_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000590 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000591 /* offs calculation is only needed for SST chips which treat RES like REMS. */
592 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
593 offs += writecnt - JEDEC_REMS_OUTSIZE;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000594 switch (data->emu_chip) {
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000595 case EMULATE_ST_M25P10_RES:
596 if (readcnt > 0)
597 memset(readarr, 0x10, readcnt);
598 break;
599 case EMULATE_SST_SST25VF040_REMS:
600 for (i = 0; i < readcnt; i++)
601 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
602 break;
603 case EMULATE_SST_SST25VF032B:
604 for (i = 0; i < readcnt; i++)
605 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
606 break;
607 case EMULATE_MACRONIX_MX25L6436:
608 if (readcnt > 0)
609 memset(readarr, 0x16, readcnt);
610 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100611 case EMULATE_WINBOND_W25Q128FV:
612 if (readcnt > 0)
613 memset(readarr, 0x17, readcnt);
614 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000615 default: /* ignore */
616 break;
617 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000618 break;
619 case JEDEC_REMS:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000620 /* REMS response has wraparound and uses an address parameter. */
621 if (writecnt < JEDEC_REMS_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000622 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000623 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
624 offs += writecnt - JEDEC_REMS_OUTSIZE;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000625 switch (data->emu_chip) {
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000626 case EMULATE_SST_SST25VF040_REMS:
627 for (i = 0; i < readcnt; i++)
628 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
629 break;
630 case EMULATE_SST_SST25VF032B:
631 for (i = 0; i < readcnt; i++)
632 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
633 break;
634 case EMULATE_MACRONIX_MX25L6436:
635 for (i = 0; i < readcnt; i++)
636 readarr[i] = mx25l6436_rems_response[(offs + i) % 2];
637 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100638 case EMULATE_WINBOND_W25Q128FV:
639 for (i = 0; i < readcnt; i++)
640 readarr[i] = w25q128fv_rems_response[(offs + i) % 2];
641 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000642 default: /* ignore */
643 break;
644 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000645 break;
646 case JEDEC_RDID:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000647 switch (data->emu_chip) {
Stefan Tauner0b9df972012-05-07 22:12:16 +0000648 case EMULATE_SST_SST25VF032B:
649 if (readcnt > 0)
650 readarr[0] = 0xbf;
651 if (readcnt > 1)
652 readarr[1] = 0x25;
653 if (readcnt > 2)
654 readarr[2] = 0x4a;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000655 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000656 case EMULATE_MACRONIX_MX25L6436:
657 if (readcnt > 0)
658 readarr[0] = 0xc2;
659 if (readcnt > 1)
660 readarr[1] = 0x20;
661 if (readcnt > 2)
662 readarr[2] = 0x17;
663 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100664 case EMULATE_WINBOND_W25Q128FV:
665 if (readcnt > 0)
666 readarr[0] = 0xef;
667 if (readcnt > 1)
668 readarr[1] = 0x40;
669 if (readcnt > 2)
670 readarr[2] = 0x18;
671 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000672 default: /* ignore */
673 break;
674 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000675 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000676 case JEDEC_RDSR:
Sergii Dmytruk2ac444f2021-11-08 00:05:12 +0200677 memset(readarr, data->emu_status[0], readcnt);
678 break;
679 case JEDEC_RDSR2:
680 if (data->emu_status_len >= 2)
681 memset(readarr, data->emu_status[1], readcnt);
682 break;
683 case JEDEC_RDSR3:
684 if (data->emu_status_len >= 3)
685 memset(readarr, data->emu_status[2], readcnt);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000686 break;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000687 /* FIXME: this should be chip-specific. */
688 case JEDEC_EWSR:
689 case JEDEC_WREN:
Sergii Dmytruk2ac444f2021-11-08 00:05:12 +0200690 data->emu_status[0] |= SPI_SR_WEL;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000691 break;
692 case JEDEC_WRSR:
Sergii Dmytruk2ac444f2021-11-08 00:05:12 +0200693 if (!(data->emu_status[0] & SPI_SR_WEL)) {
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000694 msg_perr("WRSR attempted, but WEL is 0!\n");
695 break;
696 }
Sergii Dmytruk2ac444f2021-11-08 00:05:12 +0200697
698 wrsr_ext = (writecnt == 3 && data->emu_wrsr_ext);
699
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000700 /* FIXME: add some reasonable simulation of the busy flag */
Sergii Dmytruk2ac444f2021-11-08 00:05:12 +0200701
702 ro_bits = get_reg_ro_bit_mask(STATUS1);
703 data->emu_status[0] &= ro_bits;
704 data->emu_status[0] |= writearr[1] & ~ro_bits;
705 if (wrsr_ext) {
706 ro_bits = get_reg_ro_bit_mask(STATUS2);
707 data->emu_status[1] &= ro_bits;
708 data->emu_status[1] |= writearr[2] & ~ro_bits;
709 }
710
711 if (wrsr_ext)
712 msg_pdbg2("WRSR wrote 0x%02x%02x.\n", data->emu_status[1], data->emu_status[0]);
713 else
714 msg_pdbg2("WRSR wrote 0x%02x.\n", data->emu_status[0]);
715 break;
716 case JEDEC_WRSR2:
717 if (data->emu_status_len < 2)
718 break;
719 if (!(data->emu_status[0] & SPI_SR_WEL)) {
720 msg_perr("WRSR2 attempted, but WEL is 0!\n");
721 break;
722 }
723
724 ro_bits = get_reg_ro_bit_mask(STATUS2);
725 data->emu_status[1] &= ro_bits;
726 data->emu_status[1] |= (writearr[1] & ~ro_bits);
727
728 msg_pdbg2("WRSR2 wrote 0x%02x.\n", data->emu_status[1]);
729 break;
730 case JEDEC_WRSR3:
731 if (data->emu_status_len < 3)
732 break;
733 if (!(data->emu_status[0] & SPI_SR_WEL)) {
734 msg_perr("WRSR3 attempted, but WEL is 0!\n");
735 break;
736 }
737
738 ro_bits = get_reg_ro_bit_mask(STATUS3);
739 data->emu_status[2] &= ro_bits;
740 data->emu_status[2] |= (writearr[1] & ~ro_bits);
741
742 msg_pdbg2("WRSR3 wrote 0x%02x.\n", data->emu_status[2]);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000743 break;
744 case JEDEC_READ:
745 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
746 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000747 offs %= data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000748 if (readcnt > 0)
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +1000749 memcpy(readarr, data->flashchip_contents + offs, readcnt);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000750 break;
751 case JEDEC_BYTE_PROGRAM:
752 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
753 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000754 offs %= data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000755 if (writecnt < 5) {
756 msg_perr("BYTE PROGRAM size too short!\n");
757 return 1;
758 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000759 if (writecnt - 4 > data->emu_max_byteprogram_size) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000760 msg_perr("Max BYTE PROGRAM size exceeded!\n");
761 return 1;
762 }
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +1000763 memcpy(data->flashchip_contents + offs, writearr + 4, writecnt - 4);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000764 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000765 break;
766 case JEDEC_AAI_WORD_PROGRAM:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000767 if (!data->emu_max_aai_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000768 break;
Sergii Dmytruk2ac444f2021-11-08 00:05:12 +0200769 if (!(data->emu_status[0] & SPI_SR_AAI)) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000770 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
771 msg_perr("Initial AAI WORD PROGRAM size too "
772 "short!\n");
773 return 1;
774 }
775 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
776 msg_perr("Initial AAI WORD PROGRAM size too "
777 "long!\n");
778 return 1;
779 }
Sergii Dmytruk2ac444f2021-11-08 00:05:12 +0200780 data->emu_status[0] |= SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000781 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
782 writearr[3];
783 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000784 aai_offs %= data->emu_chip_size;
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +1000785 memcpy(data->flashchip_contents + aai_offs, writearr + 4, 2);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000786 aai_offs += 2;
787 } else {
788 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
789 msg_perr("Continuation AAI WORD PROGRAM size "
790 "too short!\n");
791 return 1;
792 }
793 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
794 msg_perr("Continuation AAI WORD PROGRAM size "
795 "too long!\n");
796 return 1;
797 }
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +1000798 memcpy(data->flashchip_contents + aai_offs, writearr + 1, 2);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000799 aai_offs += 2;
800 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000801 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000802 break;
803 case JEDEC_WRDI:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000804 if (data->emu_max_aai_size)
Sergii Dmytruk2ac444f2021-11-08 00:05:12 +0200805 data->emu_status[0] &= ~SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000806 break;
807 case JEDEC_SE:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000808 if (!data->emu_jedec_se_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000809 break;
810 if (writecnt != JEDEC_SE_OUTSIZE) {
811 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
812 return 1;
813 }
814 if (readcnt != JEDEC_SE_INSIZE) {
815 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
816 return 1;
817 }
818 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +1000819 if (offs & (data->emu_jedec_se_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000820 msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000821 offs &= ~(data->emu_jedec_se_size - 1);
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +1000822 memset(data->flashchip_contents + offs, 0xff, data->emu_jedec_se_size);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000823 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000824 break;
825 case JEDEC_BE_52:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000826 if (!data->emu_jedec_be_52_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000827 break;
828 if (writecnt != JEDEC_BE_52_OUTSIZE) {
829 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
830 return 1;
831 }
832 if (readcnt != JEDEC_BE_52_INSIZE) {
833 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
834 return 1;
835 }
836 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +1000837 if (offs & (data->emu_jedec_be_52_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000838 msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000839 offs &= ~(data->emu_jedec_be_52_size - 1);
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +1000840 memset(data->flashchip_contents + offs, 0xff, data->emu_jedec_be_52_size);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000841 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000842 break;
843 case JEDEC_BE_D8:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000844 if (!data->emu_jedec_be_d8_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000845 break;
846 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
847 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
848 return 1;
849 }
850 if (readcnt != JEDEC_BE_D8_INSIZE) {
851 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
852 return 1;
853 }
854 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +1000855 if (offs & (data->emu_jedec_be_d8_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000856 msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000857 offs &= ~(data->emu_jedec_be_d8_size - 1);
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +1000858 memset(data->flashchip_contents + offs, 0xff, data->emu_jedec_be_d8_size);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000859 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000860 break;
861 case JEDEC_CE_60:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000862 if (!data->emu_jedec_ce_60_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000863 break;
864 if (writecnt != JEDEC_CE_60_OUTSIZE) {
865 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
866 return 1;
867 }
868 if (readcnt != JEDEC_CE_60_INSIZE) {
869 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
870 return 1;
871 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000872 /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000873 /* emu_jedec_ce_60_size is emu_chip_size. */
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +1000874 memset(data->flashchip_contents, 0xff, data->emu_jedec_ce_60_size);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000875 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000876 break;
877 case JEDEC_CE_C7:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000878 if (!data->emu_jedec_ce_c7_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000879 break;
880 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
881 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
882 return 1;
883 }
884 if (readcnt != JEDEC_CE_C7_INSIZE) {
885 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
886 return 1;
887 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000888 /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000889 /* emu_jedec_ce_c7_size is emu_chip_size. */
Edward O'Callaghanb1a51ab2021-05-20 20:34:02 +1000890 memset(data->flashchip_contents, 0xff, data->emu_jedec_ce_c7_size);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000891 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000892 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000893 case JEDEC_SFDP:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000894 if (data->emu_chip != EMULATE_MACRONIX_MX25L6436)
Stefan Tauner0b9df972012-05-07 22:12:16 +0000895 break;
896 if (writecnt < 4)
897 break;
898 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
899
900 /* SFDP expects one dummy byte after the address. */
901 if (writecnt == 4) {
902 /* The dummy byte was not written, make sure it is read instead.
903 * Shifting and shortening the read array does achieve this goal.
904 */
905 readarr++;
906 readcnt--;
907 } else {
908 /* The response is shifted if more than 5 bytes are written, because SFDP data is
909 * already shifted out by the chip while those superfluous bytes are written. */
910 offs += writecnt - 5;
911 }
912
913 /* The SFDP spec implies that the start address of an SFDP read may be truncated to fit in the
914 * SFDP table address space, i.e. the start address may be wrapped around at SFDP table size.
915 * This is a reasonable implementation choice in hardware because it saves a few gates. */
916 if (offs >= sizeof(sfdp_table)) {
917 msg_pdbg("Wrapping the start address around the SFDP table boundary (using 0x%x "
918 "instead of 0x%x).\n", (unsigned int)(offs % sizeof(sfdp_table)), offs);
919 offs %= sizeof(sfdp_table);
920 }
921 toread = min(sizeof(sfdp_table) - offs, readcnt);
922 memcpy(readarr, sfdp_table + offs, toread);
923 if (toread < readcnt)
924 msg_pdbg("Crossing the SFDP table boundary in a single "
925 "continuous chunk produces undefined results "
926 "after that point.\n");
927 break;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000928 default:
929 /* No special response. */
930 break;
931 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000932 if (writearr[0] != JEDEC_WREN && writearr[0] != JEDEC_EWSR)
Sergii Dmytruk2ac444f2021-11-08 00:05:12 +0200933 data->emu_status[0] &= ~SPI_SR_WEL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000934 return 0;
935}
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000936
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000937static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000938 unsigned int readcnt,
939 const unsigned char *writearr,
940 unsigned char *readarr)
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000941{
Nico Huber519be662018-12-23 20:03:35 +0100942 unsigned int i;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000943 struct emu_data *emu_data = flash->mst->spi.data;
944 if (!emu_data) {
945 msg_perr("No data in flash context!\n");
946 return 1;
947 }
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000948
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000949 msg_pspew("%s:", __func__);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000950
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000951 msg_pspew(" writing %u bytes:", writecnt);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000952 for (i = 0; i < writecnt; i++)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000953 msg_pspew(" 0x%02x", writearr[i]);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000954
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000955 /* Response for unknown commands and missing chip is 0xff. */
956 memset(readarr, 0xff, readcnt);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000957 switch (emu_data->emu_chip) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000958 case EMULATE_ST_M25P10_RES:
959 case EMULATE_SST_SST25VF040_REMS:
960 case EMULATE_SST_SST25VF032B:
Stefan Tauner0b9df972012-05-07 22:12:16 +0000961 case EMULATE_MACRONIX_MX25L6436:
Nico Huberf9632d82019-01-20 11:23:49 +0100962 case EMULATE_WINBOND_W25Q128FV:
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000963 if (emulate_spi_chip_response(writecnt, readcnt, writearr,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000964 readarr, emu_data)) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000965 msg_pdbg("Invalid command sent to flash chip!\n");
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000966 return 1;
967 }
968 break;
969 default:
970 break;
971 }
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000972 msg_pspew(" reading %u bytes:", readcnt);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000973 for (i = 0; i < readcnt; i++)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000974 msg_pspew(" 0x%02x", readarr[i]);
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000975 msg_pspew("\n");
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000976 return 0;
977}
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +0000978
Mark Marshallf20b7be2014-05-09 21:16:21 +0000979static 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 +0000980{
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000981 return spi_write_chunked(flash, buf, start, len,
982 spi_write_256_chunksize);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000983}
Thomas Heijligenddfaff02021-05-04 15:32:17 +0200984
985const struct programmer_entry programmer_dummy = {
986 .name = "dummy",
987 .type = OTHER,
988 /* FIXME */
989 .devs.note = "Dummy device, does nothing and logs all accesses\n",
990 .init = dummy_init,
991 .map_flash_region = dummy_map,
992 .unmap_flash_region = dummy_unmap,
993 .delay = internal_delay,
994};