blob: 374290b80996d4a36a6d7b87713dd3d1cd556552 [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>
Nico Huberab696292021-06-09 18:10:07 +020021#include <sys/types.h>
22#include <sys/stat.h>
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000023#include "flash.h"
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +000024#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000025#include "programmer.h"
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000026
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000027#include "spi.h"
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000028
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000029enum emu_chip {
30 EMULATE_NONE,
31 EMULATE_ST_M25P10_RES,
32 EMULATE_SST_SST25VF040_REMS,
33 EMULATE_SST_SST25VF032B,
Stefan Tauner0b9df972012-05-07 22:12:16 +000034 EMULATE_MACRONIX_MX25L6436,
Nico Huberf9632d82019-01-20 11:23:49 +010035 EMULATE_WINBOND_W25Q128FV,
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000036};
Stefan Tauner0b9df972012-05-07 22:12:16 +000037
Lachlan Bishopc753c402020-09-10 14:57:05 +100038struct emu_data {
39 enum emu_chip emu_chip;
40 char *emu_persistent_image;
41 unsigned int emu_chip_size;
42 int emu_modified; /* is the image modified since reading it? */
43 uint8_t emu_status;
44 unsigned int emu_max_byteprogram_size;
45 unsigned int emu_max_aai_size;
46 unsigned int emu_jedec_se_size;
47 unsigned int emu_jedec_be_52_size;
48 unsigned int emu_jedec_be_d8_size;
49 unsigned int emu_jedec_ce_60_size;
50 unsigned int emu_jedec_ce_c7_size;
51 unsigned char spi_blacklist[256];
52 unsigned char spi_ignorelist[256];
53 unsigned int spi_blacklist_size;
54 unsigned int spi_ignorelist_size;
Edward O'Callaghan94250222021-05-20 20:34:02 +100055
56 uint8_t *flashchip_contents;
Lachlan Bishopc753c402020-09-10 14:57:05 +100057};
58
Stefan Tauner0b9df972012-05-07 22:12:16 +000059/* A legit complete SFDP table based on the MX25L6436E (rev. 1.8) datasheet. */
Stefan Tauner67d163d2013-01-15 17:37:48 +000060static const uint8_t sfdp_table[] = {
Stefan Tauner0b9df972012-05-07 22:12:16 +000061 0x53, 0x46, 0x44, 0x50, // @0x00: SFDP signature
62 0x00, 0x01, 0x01, 0xFF, // @0x04: revision 1.0, 2 headers
63 0x00, 0x00, 0x01, 0x09, // @0x08: JEDEC SFDP header rev. 1.0, 9 DW long
64 0x1C, 0x00, 0x00, 0xFF, // @0x0C: PTP0 = 0x1C (instead of 0x30)
65 0xC2, 0x00, 0x01, 0x04, // @0x10: Macronix header rev. 1.0, 4 DW long
66 0x48, 0x00, 0x00, 0xFF, // @0x14: PTP1 = 0x48 (instead of 0x60)
67 0xFF, 0xFF, 0xFF, 0xFF, // @0x18: hole.
68 0xE5, 0x20, 0xC9, 0xFF, // @0x1C: SFDP parameter table start
69 0xFF, 0xFF, 0xFF, 0x03, // @0x20
70 0x00, 0xFF, 0x08, 0x6B, // @0x24
71 0x08, 0x3B, 0x00, 0xFF, // @0x28
72 0xEE, 0xFF, 0xFF, 0xFF, // @0x2C
73 0xFF, 0xFF, 0x00, 0x00, // @0x30
74 0xFF, 0xFF, 0x00, 0xFF, // @0x34
75 0x0C, 0x20, 0x0F, 0x52, // @0x38
76 0x10, 0xD8, 0x00, 0xFF, // @0x3C: SFDP parameter table end
77 0xFF, 0xFF, 0xFF, 0xFF, // @0x40: hole.
78 0xFF, 0xFF, 0xFF, 0xFF, // @0x44: hole.
79 0x00, 0x36, 0x00, 0x27, // @0x48: Macronix parameter table start
80 0xF4, 0x4F, 0xFF, 0xFF, // @0x4C
81 0xD9, 0xC8, 0xFF, 0xFF, // @0x50
82 0xFF, 0xFF, 0xFF, 0xFF, // @0x54: Macronix parameter table end
83};
84
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000085
Stefan Taunerc69c9c82011-11-23 09:13:48 +000086static unsigned int spi_write_256_chunksize = 256;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000087
Edward O'Callaghan5eca4272020-04-12 17:27:53 +100088static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
Mark Marshallf20b7be2014-05-09 21:16:21 +000089 const unsigned char *writearr, unsigned char *readarr);
90static int dummy_spi_write_256(struct flashctx *flash, const uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +000091 unsigned int start, unsigned int len);
Mark Marshallf20b7be2014-05-09 21:16:21 +000092static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr);
93static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr);
94static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr);
95static void dummy_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len);
96static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr);
97static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr);
98static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr);
99static void dummy_chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len);
Michael Karcherb9dbe482011-05-11 17:07:07 +0000100
Lachlan Bishopc753c402020-09-10 14:57:05 +1000101static struct spi_master spi_master_dummyflasher = {
Nico Huber1cf407b2017-11-10 20:18:23 +0100102 .features = SPI_MASTER_4BA,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000103 .max_data_read = MAX_DATA_READ_UNLIMITED,
104 .max_data_write = MAX_DATA_UNSPECIFIED,
105 .command = dummy_spi_send_command,
106 .multicommand = default_spi_send_multicommand,
107 .read = default_spi_read,
108 .write_256 = dummy_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +0000109 .write_aai = default_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000110};
David Hendricks8bb20212011-06-14 01:35:36 +0000111
Lachlan Bishopc753c402020-09-10 14:57:05 +1000112static struct par_master par_master_dummy = {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000113 .chip_readb = dummy_chip_readb,
114 .chip_readw = dummy_chip_readw,
115 .chip_readl = dummy_chip_readl,
116 .chip_readn = dummy_chip_readn,
117 .chip_writeb = dummy_chip_writeb,
118 .chip_writew = dummy_chip_writew,
119 .chip_writel = dummy_chip_writel,
120 .chip_writen = dummy_chip_writen,
121};
122
David Hendricks8bb20212011-06-14 01:35:36 +0000123static int dummy_shutdown(void *data)
124{
125 msg_pspew("%s\n", __func__);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000126 struct emu_data *emu_data = (struct emu_data *)data;
127 if (emu_data->emu_chip != EMULATE_NONE) {
128 if (emu_data->emu_persistent_image && emu_data->emu_modified) {
129 msg_pdbg("Writing %s\n", emu_data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000130 write_buf_to_file(emu_data->flashchip_contents,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000131 emu_data->emu_chip_size,
132 emu_data->emu_persistent_image);
David Hendricks8bb20212011-06-14 01:35:36 +0000133 }
Angel Pons02b9ae22021-05-25 12:46:43 +0200134 free(emu_data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000135 free(emu_data->flashchip_contents);
David Hendricks8bb20212011-06-14 01:35:36 +0000136 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000137 free(data);
David Hendricks8bb20212011-06-14 01:35:36 +0000138 return 0;
139}
140
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000141static int init_data(struct emu_data *data, enum chipbustype *dummy_buses_supported)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000142{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000143 char *bustext = NULL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000144 char *tmp = NULL;
Nico Huber519be662018-12-23 20:03:35 +0100145 unsigned int i;
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000146 char *endptr;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000147 char *status = NULL;
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000148
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000149 bustext = extract_programmer_param("bus");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000150 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
151 if (!bustext)
152 bustext = strdup("parallel+lpc+fwh+spi");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000153 /* Convert the parameters to lowercase. */
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000154 tolower_string(bustext);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000155
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000156 *dummy_buses_supported = BUS_NONE;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000157 if (strstr(bustext, "parallel")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000158 *dummy_buses_supported |= BUS_PARALLEL;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000159 msg_pdbg("Enabling support for %s flash.\n", "parallel");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000160 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000161 if (strstr(bustext, "lpc")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000162 *dummy_buses_supported |= BUS_LPC;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000163 msg_pdbg("Enabling support for %s flash.\n", "LPC");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000164 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000165 if (strstr(bustext, "fwh")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000166 *dummy_buses_supported |= BUS_FWH;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000167 msg_pdbg("Enabling support for %s flash.\n", "FWH");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000168 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000169 if (strstr(bustext, "spi")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000170 *dummy_buses_supported |= BUS_SPI;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000171 msg_pdbg("Enabling support for %s flash.\n", "SPI");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000172 }
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000173 if (*dummy_buses_supported == BUS_NONE)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000174 msg_pdbg("Support for all flash bus types disabled.\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000175 free(bustext);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000176
177 tmp = extract_programmer_param("spi_write_256_chunksize");
178 if (tmp) {
Edward O'Callaghanc785f882021-05-23 22:14:36 +1000179 spi_write_256_chunksize = strtoul(tmp, &endptr, 0);
180 if (*endptr != '\0' || spi_write_256_chunksize < 1) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000181 msg_perr("invalid spi_write_256_chunksize\n");
Edward O'Callaghanc785f882021-05-23 22:14:36 +1000182 free(tmp);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000183 return 1;
184 }
Edward O'Callaghanc785f882021-05-23 22:14:36 +1000185 free(tmp);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000186 }
187
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000188 tmp = extract_programmer_param("spi_blacklist");
189 if (tmp) {
190 i = strlen(tmp);
191 if (!strncmp(tmp, "0x", 2)) {
192 i -= 2;
193 memmove(tmp, tmp + 2, i + 1);
194 }
195 if ((i > 512) || (i % 2)) {
196 msg_perr("Invalid SPI command blacklist length\n");
197 free(tmp);
198 return 1;
199 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000200 data->spi_blacklist_size = i / 2;
201 for (i = 0; i < data->spi_blacklist_size * 2; i++) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000202 if (!isxdigit((unsigned char)tmp[i])) {
203 msg_perr("Invalid char \"%c\" in SPI command "
204 "blacklist\n", tmp[i]);
205 free(tmp);
206 return 1;
207 }
208 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000209 for (i = 0; i < data->spi_blacklist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000210 unsigned int tmp2;
211 /* SCNx8 is apparently not supported by MSVC (and thus
212 * MinGW), so work around it with an extra variable
213 */
214 sscanf(tmp + i * 2, "%2x", &tmp2);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000215 data->spi_blacklist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000216 }
217 msg_pdbg("SPI blacklist is ");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000218 for (i = 0; i < data->spi_blacklist_size; i++)
219 msg_pdbg("%02x ", data->spi_blacklist[i]);
220 msg_pdbg(", size %u\n", data->spi_blacklist_size);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000221 }
222 free(tmp);
223
224 tmp = extract_programmer_param("spi_ignorelist");
225 if (tmp) {
226 i = strlen(tmp);
227 if (!strncmp(tmp, "0x", 2)) {
228 i -= 2;
229 memmove(tmp, tmp + 2, i + 1);
230 }
231 if ((i > 512) || (i % 2)) {
232 msg_perr("Invalid SPI command ignorelist length\n");
233 free(tmp);
234 return 1;
235 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000236 data->spi_ignorelist_size = i / 2;
237 for (i = 0; i < data->spi_ignorelist_size * 2; i++) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000238 if (!isxdigit((unsigned char)tmp[i])) {
239 msg_perr("Invalid char \"%c\" in SPI command "
240 "ignorelist\n", tmp[i]);
241 free(tmp);
242 return 1;
243 }
244 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000245 for (i = 0; i < data->spi_ignorelist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000246 unsigned int tmp2;
247 /* SCNx8 is apparently not supported by MSVC (and thus
248 * MinGW), so work around it with an extra variable
249 */
250 sscanf(tmp + i * 2, "%2x", &tmp2);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000251 data->spi_ignorelist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000252 }
253 msg_pdbg("SPI ignorelist is ");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000254 for (i = 0; i < data->spi_ignorelist_size; i++)
255 msg_pdbg("%02x ", data->spi_ignorelist[i]);
256 msg_pdbg(", size %u\n", data->spi_ignorelist_size);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000257 }
258 free(tmp);
259
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000260 tmp = extract_programmer_param("emulate");
261 if (!tmp) {
262 msg_pdbg("Not emulating any flash chip.\n");
263 /* Nothing else to do. */
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000264 return 0;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000265 }
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000266
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000267 if (!strcmp(tmp, "M25P10.RES")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000268 data->emu_chip = EMULATE_ST_M25P10_RES;
269 data->emu_chip_size = 128 * 1024;
270 data->emu_max_byteprogram_size = 128;
271 data->emu_max_aai_size = 0;
272 data->emu_jedec_se_size = 0;
273 data->emu_jedec_be_52_size = 0;
274 data->emu_jedec_be_d8_size = 32 * 1024;
275 data->emu_jedec_ce_60_size = 0;
276 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000277 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
278 "write)\n");
279 }
280 if (!strcmp(tmp, "SST25VF040.REMS")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000281 data->emu_chip = EMULATE_SST_SST25VF040_REMS;
282 data->emu_chip_size = 512 * 1024;
283 data->emu_max_byteprogram_size = 1;
284 data->emu_max_aai_size = 0;
285 data->emu_jedec_se_size = 4 * 1024;
286 data->emu_jedec_be_52_size = 32 * 1024;
287 data->emu_jedec_be_d8_size = 0;
288 data->emu_jedec_ce_60_size = data->emu_chip_size;
289 data->emu_jedec_ce_c7_size = 0;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000290 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
291 "byte write)\n");
292 }
293 if (!strcmp(tmp, "SST25VF032B")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000294 data->emu_chip = EMULATE_SST_SST25VF032B;
295 data->emu_chip_size = 4 * 1024 * 1024;
296 data->emu_max_byteprogram_size = 1;
297 data->emu_max_aai_size = 2;
298 data->emu_jedec_se_size = 4 * 1024;
299 data->emu_jedec_be_52_size = 32 * 1024;
300 data->emu_jedec_be_d8_size = 64 * 1024;
301 data->emu_jedec_ce_60_size = data->emu_chip_size;
302 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000303 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
304 "write)\n");
305 }
Stefan Tauner0b9df972012-05-07 22:12:16 +0000306 if (!strcmp(tmp, "MX25L6436")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000307 data->emu_chip = EMULATE_MACRONIX_MX25L6436;
308 data->emu_chip_size = 8 * 1024 * 1024;
309 data->emu_max_byteprogram_size = 256;
310 data->emu_max_aai_size = 0;
311 data->emu_jedec_se_size = 4 * 1024;
312 data->emu_jedec_be_52_size = 32 * 1024;
313 data->emu_jedec_be_d8_size = 64 * 1024;
314 data->emu_jedec_ce_60_size = data->emu_chip_size;
315 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000316 msg_pdbg("Emulating Macronix MX25L6436 SPI flash chip (RDID, "
317 "SFDP)\n");
318 }
Nico Huberf9632d82019-01-20 11:23:49 +0100319 if (!strcmp(tmp, "W25Q128FV")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000320 data->emu_chip = EMULATE_WINBOND_W25Q128FV;
321 data->emu_chip_size = 16 * 1024 * 1024;
322 data->emu_max_byteprogram_size = 256;
323 data->emu_max_aai_size = 0;
324 data->emu_jedec_se_size = 4 * 1024;
325 data->emu_jedec_be_52_size = 32 * 1024;
326 data->emu_jedec_be_d8_size = 64 * 1024;
327 data->emu_jedec_ce_60_size = data->emu_chip_size;
328 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Nico Huberf9632d82019-01-20 11:23:49 +0100329 msg_pdbg("Emulating Winbond W25Q128FV SPI flash chip (RDID)\n");
330 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000331 if (data->emu_chip == EMULATE_NONE) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000332 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
333 free(tmp);
334 return 1;
335 }
336 free(tmp);
David Hendricks8bb20212011-06-14 01:35:36 +0000337
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000338 status = extract_programmer_param("spi_status");
339 if (status) {
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000340 errno = 0;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000341 data->emu_status = strtoul(status, &endptr, 0);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000342 if (errno != 0 || status == endptr) {
Angel Ponsc2484642021-05-25 13:03:24 +0200343 free(status);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000344 msg_perr("Error: initial status register specified, "
345 "but the value could not be converted.\n");
346 return 1;
347 }
Angel Ponsc2484642021-05-25 13:03:24 +0200348 free(status);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000349 msg_pdbg("Initial status register is set to 0x%02x.\n",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000350 data->emu_status);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000351 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000352
Angel Pons328898a2021-05-25 12:56:18 +0200353 data->flashchip_contents = malloc(data->emu_chip_size);
354 if (!data->flashchip_contents) {
355 msg_perr("Out of memory!\n");
356 return 1;
357 }
358
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000359
360 return 0;
361}
362
Thomas Heijligencc853d82021-05-04 15:32:17 +0200363static int dummy_init(void)
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000364{
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000365 struct stat image_stat;
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000366
367 struct emu_data *data = calloc(1, sizeof(struct emu_data));
368 if (!data) {
369 msg_perr("Out of memory!\n");
370 return 1;
371 }
372 data->emu_chip = EMULATE_NONE;
373 spi_master_dummyflasher.data = data;
374 par_master_dummy.data = data;
375
376 msg_pspew("%s\n", __func__);
377
378 enum chipbustype dummy_buses_supported;
379 if (init_data(data, &dummy_buses_supported)) {
380 free(data);
381 return 1;
382 }
383
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000384 if (data->emu_chip == EMULATE_NONE) {
385 msg_pdbg("Not emulating any flash chip.\n");
386 /* Nothing else to do. */
387 goto dummy_init_out;
388 }
389
Lachlan Bishopc753c402020-09-10 14:57:05 +1000390 msg_pdbg("Filling fake flash chip with 0xff, size %i\n", data->emu_chip_size);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000391 memset(data->flashchip_contents, 0xff, data->emu_chip_size);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000392
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000393 /* Will be freed by shutdown function if necessary. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000394 data->emu_persistent_image = extract_programmer_param("image");
395 if (!data->emu_persistent_image) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000396 /* Nothing else to do. */
David Hendricks8bb20212011-06-14 01:35:36 +0000397 goto dummy_init_out;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000398 }
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000399 /* We will silently (in default verbosity) ignore the file if it does not exist (yet) or the size does
400 * not match the emulated chip. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000401 if (!stat(data->emu_persistent_image, &image_stat)) {
Stefan Tauner23e10b82016-01-23 16:16:49 +0000402 msg_pdbg("Found persistent image %s, %jd B ",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000403 data->emu_persistent_image, (intmax_t)image_stat.st_size);
404 if ((uintmax_t)image_stat.st_size == data->emu_chip_size) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000405 msg_pdbg("matches.\n");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000406 msg_pdbg("Reading %s\n", data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000407 if (read_buf_from_file(data->flashchip_contents, data->emu_chip_size,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000408 data->emu_persistent_image)) {
409 msg_perr("Unable to read %s\n", data->emu_persistent_image);
Angel Pons02b9ae22021-05-25 12:46:43 +0200410 free(data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000411 free(data->flashchip_contents);
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000412 free(data);
Jacob Garberca598da2019-08-12 10:44:17 -0600413 return 1;
414 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000415 } else {
416 msg_pdbg("doesn't match.\n");
417 }
418 }
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000419
David Hendricks8bb20212011-06-14 01:35:36 +0000420dummy_init_out:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000421 if (register_shutdown(dummy_shutdown, data)) {
Angel Pons02b9ae22021-05-25 12:46:43 +0200422 free(data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000423 free(data->flashchip_contents);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000424 free(data);
David Hendricks8bb20212011-06-14 01:35:36 +0000425 return 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000426 }
Edward O'Callaghan3fa321d2021-05-17 20:01:27 +1000427 if (dummy_buses_supported & BUS_NONSPI)
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000428 register_par_master(&par_master_dummy,
Edward O'Callaghan3fa321d2021-05-17 20:01:27 +1000429 dummy_buses_supported & BUS_NONSPI);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000430 if (dummy_buses_supported & BUS_SPI)
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000431 register_spi_master(&spi_master_dummyflasher);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000432
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000433 return 0;
434}
435
Thomas Heijligencc853d82021-05-04 15:32:17 +0200436static void *dummy_map(const char *descr, uintptr_t phys_addr, size_t len)
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000437{
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000438 msg_pspew("%s: Mapping %s, 0x%zx bytes at 0x%0*" PRIxPTR "\n",
Stefan Tauner0554ca52013-07-25 22:54:25 +0000439 __func__, descr, len, PRIxPTR_WIDTH, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000440 return (void *)phys_addr;
441}
442
Thomas Heijligencc853d82021-05-04 15:32:17 +0200443static void dummy_unmap(void *virt_addr, size_t len)
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000444{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000445 msg_pspew("%s: Unmapping 0x%zx bytes at %p\n", __func__, len, virt_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000446}
447
Mark Marshallf20b7be2014-05-09 21:16:21 +0000448static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000449{
Stefan Taunerc2333752013-07-13 23:31:37 +0000450 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%02x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000451}
452
Mark Marshallf20b7be2014-05-09 21:16:21 +0000453static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000454{
Stefan Taunerc2333752013-07-13 23:31:37 +0000455 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%04x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000456}
457
Mark Marshallf20b7be2014-05-09 21:16:21 +0000458static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000459{
Stefan Taunerc2333752013-07-13 23:31:37 +0000460 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%08x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000461}
462
Mark Marshallf20b7be2014-05-09 21:16:21 +0000463static 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 +0000464{
465 size_t i;
Stefan Tauner0554ca52013-07-25 22:54:25 +0000466 msg_pspew("%s: addr=0x%" PRIxPTR ", len=0x%zx, writing data (hex):", __func__, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000467 for (i = 0; i < len; i++) {
468 if ((i % 16) == 0)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000469 msg_pspew("\n");
470 msg_pspew("%02x ", buf[i]);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000471 }
472}
473
Mark Marshallf20b7be2014-05-09 21:16:21 +0000474static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000475{
Stefan Taunerc2333752013-07-13 23:31:37 +0000476 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000477 return 0xff;
478}
479
Mark Marshallf20b7be2014-05-09 21:16:21 +0000480static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000481{
Stefan Taunerc2333752013-07-13 23:31:37 +0000482 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000483 return 0xffff;
484}
485
Mark Marshallf20b7be2014-05-09 21:16:21 +0000486static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000487{
Stefan Taunerc2333752013-07-13 23:31:37 +0000488 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffffffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000489 return 0xffffffff;
490}
491
Mark Marshallf20b7be2014-05-09 21:16:21 +0000492static 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 +0000493{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000494 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 +0000495 memset(buf, 0xff, len);
496 return;
497}
498
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000499static int emulate_spi_chip_response(unsigned int writecnt,
500 unsigned int readcnt,
501 const unsigned char *writearr,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000502 unsigned char *readarr,
503 struct emu_data *data)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000504{
Stefan Tauner0b9df972012-05-07 22:12:16 +0000505 unsigned int offs, i, toread;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000506 static int unsigned aai_offs;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000507 const unsigned char sst25vf040_rems_response[2] = {0xbf, 0x44};
508 const unsigned char sst25vf032b_rems_response[2] = {0xbf, 0x4a};
509 const unsigned char mx25l6436_rems_response[2] = {0xc2, 0x16};
Nico Huberf9632d82019-01-20 11:23:49 +0100510 const unsigned char w25q128fv_rems_response[2] = {0xef, 0x17};
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000511
512 if (writecnt == 0) {
513 msg_perr("No command sent to the chip!\n");
514 return 1;
515 }
Paul Menzelac427b22012-02-16 21:07:07 +0000516 /* spi_blacklist has precedence over spi_ignorelist. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000517 for (i = 0; i < data->spi_blacklist_size; i++) {
518 if (writearr[0] == data->spi_blacklist[i]) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000519 msg_pdbg("Refusing blacklisted SPI command 0x%02x\n",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000520 data->spi_blacklist[i]);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000521 return SPI_INVALID_OPCODE;
522 }
523 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000524 for (i = 0; i < data->spi_ignorelist_size; i++) {
525 if (writearr[0] == data->spi_ignorelist[i]) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000526 msg_cdbg("Ignoring ignorelisted SPI command 0x%02x\n",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000527 data->spi_ignorelist[i]);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000528 /* Return success because the command does not fail,
529 * it is simply ignored.
530 */
531 return 0;
532 }
533 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000534
Lachlan Bishopc753c402020-09-10 14:57:05 +1000535 if (data->emu_max_aai_size && (data->emu_status & SPI_SR_AAI)) {
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000536 if (writearr[0] != JEDEC_AAI_WORD_PROGRAM &&
537 writearr[0] != JEDEC_WRDI &&
538 writearr[0] != JEDEC_RDSR) {
539 msg_perr("Forbidden opcode (0x%02x) attempted during "
540 "AAI sequence!\n", writearr[0]);
541 return 0;
542 }
543 }
544
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000545 switch (writearr[0]) {
546 case JEDEC_RES:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000547 if (writecnt < JEDEC_RES_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000548 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000549 /* offs calculation is only needed for SST chips which treat RES like REMS. */
550 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
551 offs += writecnt - JEDEC_REMS_OUTSIZE;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000552 switch (data->emu_chip) {
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000553 case EMULATE_ST_M25P10_RES:
554 if (readcnt > 0)
555 memset(readarr, 0x10, readcnt);
556 break;
557 case EMULATE_SST_SST25VF040_REMS:
558 for (i = 0; i < readcnt; i++)
559 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
560 break;
561 case EMULATE_SST_SST25VF032B:
562 for (i = 0; i < readcnt; i++)
563 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
564 break;
565 case EMULATE_MACRONIX_MX25L6436:
566 if (readcnt > 0)
567 memset(readarr, 0x16, readcnt);
568 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100569 case EMULATE_WINBOND_W25Q128FV:
570 if (readcnt > 0)
571 memset(readarr, 0x17, readcnt);
572 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000573 default: /* ignore */
574 break;
575 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000576 break;
577 case JEDEC_REMS:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000578 /* REMS response has wraparound and uses an address parameter. */
579 if (writecnt < JEDEC_REMS_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000580 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000581 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
582 offs += writecnt - JEDEC_REMS_OUTSIZE;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000583 switch (data->emu_chip) {
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000584 case EMULATE_SST_SST25VF040_REMS:
585 for (i = 0; i < readcnt; i++)
586 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
587 break;
588 case EMULATE_SST_SST25VF032B:
589 for (i = 0; i < readcnt; i++)
590 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
591 break;
592 case EMULATE_MACRONIX_MX25L6436:
593 for (i = 0; i < readcnt; i++)
594 readarr[i] = mx25l6436_rems_response[(offs + i) % 2];
595 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100596 case EMULATE_WINBOND_W25Q128FV:
597 for (i = 0; i < readcnt; i++)
598 readarr[i] = w25q128fv_rems_response[(offs + i) % 2];
599 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000600 default: /* ignore */
601 break;
602 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000603 break;
604 case JEDEC_RDID:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000605 switch (data->emu_chip) {
Stefan Tauner0b9df972012-05-07 22:12:16 +0000606 case EMULATE_SST_SST25VF032B:
607 if (readcnt > 0)
608 readarr[0] = 0xbf;
609 if (readcnt > 1)
610 readarr[1] = 0x25;
611 if (readcnt > 2)
612 readarr[2] = 0x4a;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000613 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000614 case EMULATE_MACRONIX_MX25L6436:
615 if (readcnt > 0)
616 readarr[0] = 0xc2;
617 if (readcnt > 1)
618 readarr[1] = 0x20;
619 if (readcnt > 2)
620 readarr[2] = 0x17;
621 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100622 case EMULATE_WINBOND_W25Q128FV:
623 if (readcnt > 0)
624 readarr[0] = 0xef;
625 if (readcnt > 1)
626 readarr[1] = 0x40;
627 if (readcnt > 2)
628 readarr[2] = 0x18;
629 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000630 default: /* ignore */
631 break;
632 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000633 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000634 case JEDEC_RDSR:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000635 memset(readarr, data->emu_status, readcnt);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000636 break;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000637 /* FIXME: this should be chip-specific. */
638 case JEDEC_EWSR:
639 case JEDEC_WREN:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000640 data->emu_status |= SPI_SR_WEL;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000641 break;
642 case JEDEC_WRSR:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000643 if (!(data->emu_status & SPI_SR_WEL)) {
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000644 msg_perr("WRSR attempted, but WEL is 0!\n");
645 break;
646 }
647 /* FIXME: add some reasonable simulation of the busy flag */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000648 data->emu_status = writearr[1] & ~SPI_SR_WIP;
649 msg_pdbg2("WRSR wrote 0x%02x.\n", data->emu_status);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000650 break;
651 case JEDEC_READ:
652 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
653 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000654 offs %= data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000655 if (readcnt > 0)
Edward O'Callaghan94250222021-05-20 20:34:02 +1000656 memcpy(readarr, data->flashchip_contents + offs, readcnt);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000657 break;
658 case JEDEC_BYTE_PROGRAM:
659 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
660 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000661 offs %= data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000662 if (writecnt < 5) {
663 msg_perr("BYTE PROGRAM size too short!\n");
664 return 1;
665 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000666 if (writecnt - 4 > data->emu_max_byteprogram_size) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000667 msg_perr("Max BYTE PROGRAM size exceeded!\n");
668 return 1;
669 }
Edward O'Callaghan94250222021-05-20 20:34:02 +1000670 memcpy(data->flashchip_contents + offs, writearr + 4, writecnt - 4);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000671 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000672 break;
673 case JEDEC_AAI_WORD_PROGRAM:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000674 if (!data->emu_max_aai_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000675 break;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000676 if (!(data->emu_status & SPI_SR_AAI)) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000677 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
678 msg_perr("Initial AAI WORD PROGRAM size too "
679 "short!\n");
680 return 1;
681 }
682 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
683 msg_perr("Initial AAI WORD PROGRAM size too "
684 "long!\n");
685 return 1;
686 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000687 data->emu_status |= SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000688 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
689 writearr[3];
690 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000691 aai_offs %= data->emu_chip_size;
Edward O'Callaghan94250222021-05-20 20:34:02 +1000692 memcpy(data->flashchip_contents + aai_offs, writearr + 4, 2);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000693 aai_offs += 2;
694 } else {
695 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
696 msg_perr("Continuation AAI WORD PROGRAM size "
697 "too short!\n");
698 return 1;
699 }
700 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
701 msg_perr("Continuation AAI WORD PROGRAM size "
702 "too long!\n");
703 return 1;
704 }
Edward O'Callaghan94250222021-05-20 20:34:02 +1000705 memcpy(data->flashchip_contents + aai_offs, writearr + 1, 2);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000706 aai_offs += 2;
707 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000708 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000709 break;
710 case JEDEC_WRDI:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000711 if (data->emu_max_aai_size)
712 data->emu_status &= ~SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000713 break;
714 case JEDEC_SE:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000715 if (!data->emu_jedec_se_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000716 break;
717 if (writecnt != JEDEC_SE_OUTSIZE) {
718 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
719 return 1;
720 }
721 if (readcnt != JEDEC_SE_INSIZE) {
722 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
723 return 1;
724 }
725 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +1000726 if (offs & (data->emu_jedec_se_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000727 msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000728 offs &= ~(data->emu_jedec_se_size - 1);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000729 memset(data->flashchip_contents + offs, 0xff, data->emu_jedec_se_size);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000730 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000731 break;
732 case JEDEC_BE_52:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000733 if (!data->emu_jedec_be_52_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000734 break;
735 if (writecnt != JEDEC_BE_52_OUTSIZE) {
736 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
737 return 1;
738 }
739 if (readcnt != JEDEC_BE_52_INSIZE) {
740 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
741 return 1;
742 }
743 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +1000744 if (offs & (data->emu_jedec_be_52_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000745 msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000746 offs &= ~(data->emu_jedec_be_52_size - 1);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000747 memset(data->flashchip_contents + offs, 0xff, data->emu_jedec_be_52_size);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000748 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000749 break;
750 case JEDEC_BE_D8:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000751 if (!data->emu_jedec_be_d8_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000752 break;
753 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
754 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
755 return 1;
756 }
757 if (readcnt != JEDEC_BE_D8_INSIZE) {
758 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
759 return 1;
760 }
761 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +1000762 if (offs & (data->emu_jedec_be_d8_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000763 msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000764 offs &= ~(data->emu_jedec_be_d8_size - 1);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000765 memset(data->flashchip_contents + offs, 0xff, data->emu_jedec_be_d8_size);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000766 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000767 break;
768 case JEDEC_CE_60:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000769 if (!data->emu_jedec_ce_60_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000770 break;
771 if (writecnt != JEDEC_CE_60_OUTSIZE) {
772 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
773 return 1;
774 }
775 if (readcnt != JEDEC_CE_60_INSIZE) {
776 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
777 return 1;
778 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000779 /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000780 /* emu_jedec_ce_60_size is emu_chip_size. */
Edward O'Callaghan94250222021-05-20 20:34:02 +1000781 memset(data->flashchip_contents, 0xff, data->emu_jedec_ce_60_size);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000782 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000783 break;
784 case JEDEC_CE_C7:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000785 if (!data->emu_jedec_ce_c7_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000786 break;
787 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
788 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
789 return 1;
790 }
791 if (readcnt != JEDEC_CE_C7_INSIZE) {
792 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
793 return 1;
794 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000795 /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000796 /* emu_jedec_ce_c7_size is emu_chip_size. */
Edward O'Callaghan94250222021-05-20 20:34:02 +1000797 memset(data->flashchip_contents, 0xff, data->emu_jedec_ce_c7_size);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000798 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000799 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000800 case JEDEC_SFDP:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000801 if (data->emu_chip != EMULATE_MACRONIX_MX25L6436)
Stefan Tauner0b9df972012-05-07 22:12:16 +0000802 break;
803 if (writecnt < 4)
804 break;
805 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
806
807 /* SFDP expects one dummy byte after the address. */
808 if (writecnt == 4) {
809 /* The dummy byte was not written, make sure it is read instead.
810 * Shifting and shortening the read array does achieve this goal.
811 */
812 readarr++;
813 readcnt--;
814 } else {
815 /* The response is shifted if more than 5 bytes are written, because SFDP data is
816 * already shifted out by the chip while those superfluous bytes are written. */
817 offs += writecnt - 5;
818 }
819
820 /* The SFDP spec implies that the start address of an SFDP read may be truncated to fit in the
821 * SFDP table address space, i.e. the start address may be wrapped around at SFDP table size.
822 * This is a reasonable implementation choice in hardware because it saves a few gates. */
823 if (offs >= sizeof(sfdp_table)) {
824 msg_pdbg("Wrapping the start address around the SFDP table boundary (using 0x%x "
825 "instead of 0x%x).\n", (unsigned int)(offs % sizeof(sfdp_table)), offs);
826 offs %= sizeof(sfdp_table);
827 }
828 toread = min(sizeof(sfdp_table) - offs, readcnt);
829 memcpy(readarr, sfdp_table + offs, toread);
830 if (toread < readcnt)
831 msg_pdbg("Crossing the SFDP table boundary in a single "
832 "continuous chunk produces undefined results "
833 "after that point.\n");
834 break;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000835 default:
836 /* No special response. */
837 break;
838 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000839 if (writearr[0] != JEDEC_WREN && writearr[0] != JEDEC_EWSR)
Lachlan Bishopc753c402020-09-10 14:57:05 +1000840 data->emu_status &= ~SPI_SR_WEL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000841 return 0;
842}
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000843
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000844static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000845 unsigned int readcnt,
846 const unsigned char *writearr,
847 unsigned char *readarr)
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000848{
Nico Huber519be662018-12-23 20:03:35 +0100849 unsigned int i;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000850 struct emu_data *emu_data = flash->mst->spi.data;
851 if (!emu_data) {
852 msg_perr("No data in flash context!\n");
853 return 1;
854 }
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000855
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000856 msg_pspew("%s:", __func__);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000857
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000858 msg_pspew(" writing %u bytes:", writecnt);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000859 for (i = 0; i < writecnt; i++)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000860 msg_pspew(" 0x%02x", writearr[i]);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000861
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000862 /* Response for unknown commands and missing chip is 0xff. */
863 memset(readarr, 0xff, readcnt);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000864 switch (emu_data->emu_chip) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000865 case EMULATE_ST_M25P10_RES:
866 case EMULATE_SST_SST25VF040_REMS:
867 case EMULATE_SST_SST25VF032B:
Stefan Tauner0b9df972012-05-07 22:12:16 +0000868 case EMULATE_MACRONIX_MX25L6436:
Nico Huberf9632d82019-01-20 11:23:49 +0100869 case EMULATE_WINBOND_W25Q128FV:
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000870 if (emulate_spi_chip_response(writecnt, readcnt, writearr,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000871 readarr, emu_data)) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000872 msg_pdbg("Invalid command sent to flash chip!\n");
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000873 return 1;
874 }
875 break;
876 default:
877 break;
878 }
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000879 msg_pspew(" reading %u bytes:", readcnt);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000880 for (i = 0; i < readcnt; i++)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000881 msg_pspew(" 0x%02x", readarr[i]);
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000882 msg_pspew("\n");
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000883 return 0;
884}
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +0000885
Mark Marshallf20b7be2014-05-09 21:16:21 +0000886static 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 +0000887{
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000888 return spi_write_chunked(flash, buf, start, len,
889 spi_write_256_chunksize);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000890}
Thomas Heijligencc853d82021-05-04 15:32:17 +0200891
892const struct programmer_entry programmer_dummy = {
893 .name = "dummy",
894 .type = OTHER,
895 /* FIXME */
896 .devs.note = "Dummy device, does nothing and logs all accesses\n",
897 .init = dummy_init,
898 .map_flash_region = dummy_map,
899 .unmap_flash_region = dummy_unmap,
900 .delay = internal_delay,
901};