blob: 768ec8979dc2dc62933c11036a522b55d502239d [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 Dmytruk59151a42021-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 Huberab696292021-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"
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +020029#include "writeprotect.h"
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000030
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000031enum emu_chip {
32 EMULATE_NONE,
33 EMULATE_ST_M25P10_RES,
34 EMULATE_SST_SST25VF040_REMS,
35 EMULATE_SST_SST25VF032B,
Stefan Tauner0b9df972012-05-07 22:12:16 +000036 EMULATE_MACRONIX_MX25L6436,
Nico Huberf9632d82019-01-20 11:23:49 +010037 EMULATE_WINBOND_W25Q128FV,
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000038};
Stefan Tauner0b9df972012-05-07 22:12:16 +000039
Lachlan Bishopc753c402020-09-10 14:57:05 +100040struct emu_data {
41 enum emu_chip emu_chip;
42 char *emu_persistent_image;
43 unsigned int emu_chip_size;
Sergii Dmytruk59151a42021-11-08 00:05:12 +020044 /* Note: W25Q128FV doesn't change value of SR2 if it's not provided, but
45 * even its previous generations do, so don't forget to update
46 * WRSR code on enabling WRSR_EXT for more chips. */
47 bool emu_wrsr_ext;
Lachlan Bishopc753c402020-09-10 14:57:05 +100048 int emu_modified; /* is the image modified since reading it? */
Sergii Dmytruk59151a42021-11-08 00:05:12 +020049 uint8_t emu_status[3];
50 uint8_t emu_status_len; /* number of emulated status registers */
Lachlan Bishopc753c402020-09-10 14:57:05 +100051 unsigned int emu_max_byteprogram_size;
52 unsigned int emu_max_aai_size;
53 unsigned int emu_jedec_se_size;
54 unsigned int emu_jedec_be_52_size;
55 unsigned int emu_jedec_be_d8_size;
56 unsigned int emu_jedec_ce_60_size;
57 unsigned int emu_jedec_ce_c7_size;
58 unsigned char spi_blacklist[256];
59 unsigned char spi_ignorelist[256];
60 unsigned int spi_blacklist_size;
61 unsigned int spi_ignorelist_size;
Edward O'Callaghan94250222021-05-20 20:34:02 +100062
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +020063 bool hwwp; /* state of hardware write protection */
64 /* wp_start == wp_end when write-protection is disabled */
65 uint32_t wp_start;
66 uint32_t wp_end;
67
Edward O'Callaghan94250222021-05-20 20:34:02 +100068 uint8_t *flashchip_contents;
Lachlan Bishopc753c402020-09-10 14:57:05 +100069};
70
Stefan Tauner0b9df972012-05-07 22:12:16 +000071/* A legit complete SFDP table based on the MX25L6436E (rev. 1.8) datasheet. */
Stefan Tauner67d163d2013-01-15 17:37:48 +000072static const uint8_t sfdp_table[] = {
Stefan Tauner0b9df972012-05-07 22:12:16 +000073 0x53, 0x46, 0x44, 0x50, // @0x00: SFDP signature
74 0x00, 0x01, 0x01, 0xFF, // @0x04: revision 1.0, 2 headers
75 0x00, 0x00, 0x01, 0x09, // @0x08: JEDEC SFDP header rev. 1.0, 9 DW long
76 0x1C, 0x00, 0x00, 0xFF, // @0x0C: PTP0 = 0x1C (instead of 0x30)
77 0xC2, 0x00, 0x01, 0x04, // @0x10: Macronix header rev. 1.0, 4 DW long
78 0x48, 0x00, 0x00, 0xFF, // @0x14: PTP1 = 0x48 (instead of 0x60)
79 0xFF, 0xFF, 0xFF, 0xFF, // @0x18: hole.
80 0xE5, 0x20, 0xC9, 0xFF, // @0x1C: SFDP parameter table start
81 0xFF, 0xFF, 0xFF, 0x03, // @0x20
82 0x00, 0xFF, 0x08, 0x6B, // @0x24
83 0x08, 0x3B, 0x00, 0xFF, // @0x28
84 0xEE, 0xFF, 0xFF, 0xFF, // @0x2C
85 0xFF, 0xFF, 0x00, 0x00, // @0x30
86 0xFF, 0xFF, 0x00, 0xFF, // @0x34
87 0x0C, 0x20, 0x0F, 0x52, // @0x38
88 0x10, 0xD8, 0x00, 0xFF, // @0x3C: SFDP parameter table end
89 0xFF, 0xFF, 0xFF, 0xFF, // @0x40: hole.
90 0xFF, 0xFF, 0xFF, 0xFF, // @0x44: hole.
91 0x00, 0x36, 0x00, 0x27, // @0x48: Macronix parameter table start
92 0xF4, 0x4F, 0xFF, 0xFF, // @0x4C
93 0xD9, 0xC8, 0xFF, 0xFF, // @0x50
94 0xFF, 0xFF, 0xFF, 0xFF, // @0x54: Macronix parameter table end
95};
96
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000097
Stefan Taunerc69c9c82011-11-23 09:13:48 +000098static unsigned int spi_write_256_chunksize = 256;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000099
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000100static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
Mark Marshallf20b7be2014-05-09 21:16:21 +0000101 const unsigned char *writearr, unsigned char *readarr);
102static int dummy_spi_write_256(struct flashctx *flash, const uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000103 unsigned int start, unsigned int len);
Mark Marshallf20b7be2014-05-09 21:16:21 +0000104static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr);
105static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr);
106static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr);
107static void dummy_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len);
108static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr);
109static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr);
110static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr);
111static void dummy_chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len);
Michael Karcherb9dbe482011-05-11 17:07:07 +0000112
Lachlan Bishopc753c402020-09-10 14:57:05 +1000113static struct spi_master spi_master_dummyflasher = {
Nico Huber1cf407b2017-11-10 20:18:23 +0100114 .features = SPI_MASTER_4BA,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000115 .max_data_read = MAX_DATA_READ_UNLIMITED,
116 .max_data_write = MAX_DATA_UNSPECIFIED,
117 .command = dummy_spi_send_command,
118 .multicommand = default_spi_send_multicommand,
119 .read = default_spi_read,
120 .write_256 = dummy_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +0000121 .write_aai = default_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000122};
David Hendricks8bb20212011-06-14 01:35:36 +0000123
Lachlan Bishopc753c402020-09-10 14:57:05 +1000124static struct par_master par_master_dummy = {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000125 .chip_readb = dummy_chip_readb,
126 .chip_readw = dummy_chip_readw,
127 .chip_readl = dummy_chip_readl,
128 .chip_readn = dummy_chip_readn,
129 .chip_writeb = dummy_chip_writeb,
130 .chip_writew = dummy_chip_writew,
131 .chip_writel = dummy_chip_writel,
132 .chip_writen = dummy_chip_writen,
133};
134
David Hendricks8bb20212011-06-14 01:35:36 +0000135static int dummy_shutdown(void *data)
136{
137 msg_pspew("%s\n", __func__);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000138 struct emu_data *emu_data = (struct emu_data *)data;
139 if (emu_data->emu_chip != EMULATE_NONE) {
140 if (emu_data->emu_persistent_image && emu_data->emu_modified) {
141 msg_pdbg("Writing %s\n", emu_data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000142 write_buf_to_file(emu_data->flashchip_contents,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000143 emu_data->emu_chip_size,
144 emu_data->emu_persistent_image);
David Hendricks8bb20212011-06-14 01:35:36 +0000145 }
Angel Pons02b9ae22021-05-25 12:46:43 +0200146 free(emu_data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000147 free(emu_data->flashchip_contents);
David Hendricks8bb20212011-06-14 01:35:36 +0000148 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000149 free(data);
David Hendricks8bb20212011-06-14 01:35:36 +0000150 return 0;
151}
152
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000153static int init_data(struct emu_data *data, enum chipbustype *dummy_buses_supported)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000154{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000155 char *bustext = NULL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000156 char *tmp = NULL;
Nico Huber519be662018-12-23 20:03:35 +0100157 unsigned int i;
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000158 char *endptr;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000159 char *status = NULL;
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000160
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000161 bustext = extract_programmer_param("bus");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000162 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
163 if (!bustext)
164 bustext = strdup("parallel+lpc+fwh+spi");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000165 /* Convert the parameters to lowercase. */
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000166 tolower_string(bustext);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000167
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000168 *dummy_buses_supported = BUS_NONE;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000169 if (strstr(bustext, "parallel")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000170 *dummy_buses_supported |= BUS_PARALLEL;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000171 msg_pdbg("Enabling support for %s flash.\n", "parallel");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000172 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000173 if (strstr(bustext, "lpc")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000174 *dummy_buses_supported |= BUS_LPC;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000175 msg_pdbg("Enabling support for %s flash.\n", "LPC");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000176 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000177 if (strstr(bustext, "fwh")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000178 *dummy_buses_supported |= BUS_FWH;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000179 msg_pdbg("Enabling support for %s flash.\n", "FWH");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000180 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000181 if (strstr(bustext, "spi")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000182 *dummy_buses_supported |= BUS_SPI;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000183 msg_pdbg("Enabling support for %s flash.\n", "SPI");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000184 }
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000185 if (*dummy_buses_supported == BUS_NONE)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000186 msg_pdbg("Support for all flash bus types disabled.\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000187 free(bustext);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000188
189 tmp = extract_programmer_param("spi_write_256_chunksize");
190 if (tmp) {
Edward O'Callaghanc785f882021-05-23 22:14:36 +1000191 spi_write_256_chunksize = strtoul(tmp, &endptr, 0);
192 if (*endptr != '\0' || spi_write_256_chunksize < 1) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000193 msg_perr("invalid spi_write_256_chunksize\n");
Edward O'Callaghanc785f882021-05-23 22:14:36 +1000194 free(tmp);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000195 return 1;
196 }
Edward O'Callaghanc785f882021-05-23 22:14:36 +1000197 free(tmp);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000198 }
199
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000200 tmp = extract_programmer_param("spi_blacklist");
201 if (tmp) {
202 i = strlen(tmp);
203 if (!strncmp(tmp, "0x", 2)) {
204 i -= 2;
205 memmove(tmp, tmp + 2, i + 1);
206 }
207 if ((i > 512) || (i % 2)) {
208 msg_perr("Invalid SPI command blacklist length\n");
209 free(tmp);
210 return 1;
211 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000212 data->spi_blacklist_size = i / 2;
213 for (i = 0; i < data->spi_blacklist_size * 2; i++) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000214 if (!isxdigit((unsigned char)tmp[i])) {
215 msg_perr("Invalid char \"%c\" in SPI command "
216 "blacklist\n", tmp[i]);
217 free(tmp);
218 return 1;
219 }
220 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000221 for (i = 0; i < data->spi_blacklist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000222 unsigned int tmp2;
223 /* SCNx8 is apparently not supported by MSVC (and thus
224 * MinGW), so work around it with an extra variable
225 */
226 sscanf(tmp + i * 2, "%2x", &tmp2);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000227 data->spi_blacklist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000228 }
229 msg_pdbg("SPI blacklist is ");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000230 for (i = 0; i < data->spi_blacklist_size; i++)
231 msg_pdbg("%02x ", data->spi_blacklist[i]);
232 msg_pdbg(", size %u\n", data->spi_blacklist_size);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000233 }
234 free(tmp);
235
236 tmp = extract_programmer_param("spi_ignorelist");
237 if (tmp) {
238 i = strlen(tmp);
239 if (!strncmp(tmp, "0x", 2)) {
240 i -= 2;
241 memmove(tmp, tmp + 2, i + 1);
242 }
243 if ((i > 512) || (i % 2)) {
244 msg_perr("Invalid SPI command ignorelist length\n");
245 free(tmp);
246 return 1;
247 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000248 data->spi_ignorelist_size = i / 2;
249 for (i = 0; i < data->spi_ignorelist_size * 2; i++) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000250 if (!isxdigit((unsigned char)tmp[i])) {
251 msg_perr("Invalid char \"%c\" in SPI command "
252 "ignorelist\n", tmp[i]);
253 free(tmp);
254 return 1;
255 }
256 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000257 for (i = 0; i < data->spi_ignorelist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000258 unsigned int tmp2;
259 /* SCNx8 is apparently not supported by MSVC (and thus
260 * MinGW), so work around it with an extra variable
261 */
262 sscanf(tmp + i * 2, "%2x", &tmp2);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000263 data->spi_ignorelist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000264 }
265 msg_pdbg("SPI ignorelist is ");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000266 for (i = 0; i < data->spi_ignorelist_size; i++)
267 msg_pdbg("%02x ", data->spi_ignorelist[i]);
268 msg_pdbg(", size %u\n", data->spi_ignorelist_size);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000269 }
270 free(tmp);
271
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200272 tmp = extract_programmer_param("hwwp");
273 if (tmp) {
274 if (!strcmp(tmp, "yes")) {
275 msg_pdbg("Emulated chip will have hardware WP enabled\n");
276 data->hwwp = true;
277 } else if (!strcmp(tmp, "no")) {
278 msg_pdbg("Emulated chip will have hardware WP disabled\n");
279 } else {
280 msg_perr("hwwp can be \"yes\" or \"no\"\n");
281 free(tmp);
282 return 1;
283 }
284 free(tmp);
285 }
286
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000287 tmp = extract_programmer_param("emulate");
288 if (!tmp) {
289 msg_pdbg("Not emulating any flash chip.\n");
290 /* Nothing else to do. */
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000291 return 0;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000292 }
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000293
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000294 if (!strcmp(tmp, "M25P10.RES")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000295 data->emu_chip = EMULATE_ST_M25P10_RES;
296 data->emu_chip_size = 128 * 1024;
297 data->emu_max_byteprogram_size = 128;
298 data->emu_max_aai_size = 0;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200299 data->emu_status_len = 1;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000300 data->emu_jedec_se_size = 0;
301 data->emu_jedec_be_52_size = 0;
302 data->emu_jedec_be_d8_size = 32 * 1024;
303 data->emu_jedec_ce_60_size = 0;
304 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000305 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
306 "write)\n");
307 }
308 if (!strcmp(tmp, "SST25VF040.REMS")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000309 data->emu_chip = EMULATE_SST_SST25VF040_REMS;
310 data->emu_chip_size = 512 * 1024;
311 data->emu_max_byteprogram_size = 1;
312 data->emu_max_aai_size = 0;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200313 data->emu_status_len = 1;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000314 data->emu_jedec_se_size = 4 * 1024;
315 data->emu_jedec_be_52_size = 32 * 1024;
316 data->emu_jedec_be_d8_size = 0;
317 data->emu_jedec_ce_60_size = data->emu_chip_size;
318 data->emu_jedec_ce_c7_size = 0;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000319 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
320 "byte write)\n");
321 }
322 if (!strcmp(tmp, "SST25VF032B")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000323 data->emu_chip = EMULATE_SST_SST25VF032B;
324 data->emu_chip_size = 4 * 1024 * 1024;
325 data->emu_max_byteprogram_size = 1;
326 data->emu_max_aai_size = 2;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200327 data->emu_status_len = 1;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000328 data->emu_jedec_se_size = 4 * 1024;
329 data->emu_jedec_be_52_size = 32 * 1024;
330 data->emu_jedec_be_d8_size = 64 * 1024;
331 data->emu_jedec_ce_60_size = data->emu_chip_size;
332 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000333 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
334 "write)\n");
335 }
Stefan Tauner0b9df972012-05-07 22:12:16 +0000336 if (!strcmp(tmp, "MX25L6436")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000337 data->emu_chip = EMULATE_MACRONIX_MX25L6436;
338 data->emu_chip_size = 8 * 1024 * 1024;
339 data->emu_max_byteprogram_size = 256;
340 data->emu_max_aai_size = 0;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200341 data->emu_status_len = 1;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000342 data->emu_jedec_se_size = 4 * 1024;
343 data->emu_jedec_be_52_size = 32 * 1024;
344 data->emu_jedec_be_d8_size = 64 * 1024;
345 data->emu_jedec_ce_60_size = data->emu_chip_size;
346 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000347 msg_pdbg("Emulating Macronix MX25L6436 SPI flash chip (RDID, "
348 "SFDP)\n");
349 }
Nico Huberf9632d82019-01-20 11:23:49 +0100350 if (!strcmp(tmp, "W25Q128FV")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000351 data->emu_chip = EMULATE_WINBOND_W25Q128FV;
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200352 data->emu_wrsr_ext = true;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000353 data->emu_chip_size = 16 * 1024 * 1024;
354 data->emu_max_byteprogram_size = 256;
355 data->emu_max_aai_size = 0;
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200356 data->emu_status_len = 3;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000357 data->emu_jedec_se_size = 4 * 1024;
358 data->emu_jedec_be_52_size = 32 * 1024;
359 data->emu_jedec_be_d8_size = 64 * 1024;
360 data->emu_jedec_ce_60_size = data->emu_chip_size;
361 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Nico Huberf9632d82019-01-20 11:23:49 +0100362 msg_pdbg("Emulating Winbond W25Q128FV SPI flash chip (RDID)\n");
363 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000364 if (data->emu_chip == EMULATE_NONE) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000365 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
366 free(tmp);
367 return 1;
368 }
369 free(tmp);
David Hendricks8bb20212011-06-14 01:35:36 +0000370
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000371 status = extract_programmer_param("spi_status");
372 if (status) {
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200373 unsigned int emu_status;
374
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000375 errno = 0;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200376 emu_status = strtoul(status, &endptr, 0);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000377 if (errno != 0 || status == endptr) {
Angel Ponsc2484642021-05-25 13:03:24 +0200378 free(status);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000379 msg_perr("Error: initial status register specified, "
380 "but the value could not be converted.\n");
381 return 1;
382 }
Angel Ponsc2484642021-05-25 13:03:24 +0200383 free(status);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200384
385 data->emu_status[0] = emu_status;
386 data->emu_status[1] = emu_status >> 8;
387 data->emu_status[2] = emu_status >> 16;
388
389 if (data->emu_status_len == 3) {
390 msg_pdbg("Initial status registers:\n"
391 "\tSR1 is set to 0x%02x\n"
392 "\tSR2 is set to 0x%02x\n"
393 "\tSR3 is set to 0x%02x\n",
394 data->emu_status[0], data->emu_status[1], data->emu_status[2]);
395 } else if (data->emu_status_len == 2) {
396 msg_pdbg("Initial status registers:\n"
397 "\tSR1 is set to 0x%02x\n"
398 "\tSR2 is set to 0x%02x\n",
399 data->emu_status[0], data->emu_status[1]);
400 } else {
401 msg_pdbg("Initial status register is set to 0x%02x.\n",
402 data->emu_status[0]);
403 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000404 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000405
Angel Pons328898a2021-05-25 12:56:18 +0200406 data->flashchip_contents = malloc(data->emu_chip_size);
407 if (!data->flashchip_contents) {
408 msg_perr("Out of memory!\n");
409 return 1;
410 }
411
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000412
413 return 0;
414}
415
Thomas Heijligencc853d82021-05-04 15:32:17 +0200416static int dummy_init(void)
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000417{
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000418 struct stat image_stat;
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000419
420 struct emu_data *data = calloc(1, sizeof(struct emu_data));
421 if (!data) {
422 msg_perr("Out of memory!\n");
423 return 1;
424 }
425 data->emu_chip = EMULATE_NONE;
426 spi_master_dummyflasher.data = data;
427 par_master_dummy.data = data;
428
429 msg_pspew("%s\n", __func__);
430
431 enum chipbustype dummy_buses_supported;
432 if (init_data(data, &dummy_buses_supported)) {
433 free(data);
434 return 1;
435 }
436
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000437 if (data->emu_chip == EMULATE_NONE) {
438 msg_pdbg("Not emulating any flash chip.\n");
439 /* Nothing else to do. */
440 goto dummy_init_out;
441 }
442
Lachlan Bishopc753c402020-09-10 14:57:05 +1000443 msg_pdbg("Filling fake flash chip with 0xff, size %i\n", data->emu_chip_size);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000444 memset(data->flashchip_contents, 0xff, data->emu_chip_size);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000445
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000446 /* Will be freed by shutdown function if necessary. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000447 data->emu_persistent_image = extract_programmer_param("image");
448 if (!data->emu_persistent_image) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000449 /* Nothing else to do. */
David Hendricks8bb20212011-06-14 01:35:36 +0000450 goto dummy_init_out;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000451 }
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000452 /* We will silently (in default verbosity) ignore the file if it does not exist (yet) or the size does
453 * not match the emulated chip. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000454 if (!stat(data->emu_persistent_image, &image_stat)) {
Stefan Tauner23e10b82016-01-23 16:16:49 +0000455 msg_pdbg("Found persistent image %s, %jd B ",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000456 data->emu_persistent_image, (intmax_t)image_stat.st_size);
457 if ((uintmax_t)image_stat.st_size == data->emu_chip_size) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000458 msg_pdbg("matches.\n");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000459 msg_pdbg("Reading %s\n", data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000460 if (read_buf_from_file(data->flashchip_contents, data->emu_chip_size,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000461 data->emu_persistent_image)) {
462 msg_perr("Unable to read %s\n", data->emu_persistent_image);
Angel Pons02b9ae22021-05-25 12:46:43 +0200463 free(data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000464 free(data->flashchip_contents);
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000465 free(data);
Jacob Garberca598da2019-08-12 10:44:17 -0600466 return 1;
467 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000468 } else {
469 msg_pdbg("doesn't match.\n");
470 }
471 }
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000472
David Hendricks8bb20212011-06-14 01:35:36 +0000473dummy_init_out:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000474 if (register_shutdown(dummy_shutdown, data)) {
Angel Pons02b9ae22021-05-25 12:46:43 +0200475 free(data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000476 free(data->flashchip_contents);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000477 free(data);
David Hendricks8bb20212011-06-14 01:35:36 +0000478 return 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000479 }
Edward O'Callaghan3fa321d2021-05-17 20:01:27 +1000480 if (dummy_buses_supported & BUS_NONSPI)
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000481 register_par_master(&par_master_dummy,
Edward O'Callaghan3fa321d2021-05-17 20:01:27 +1000482 dummy_buses_supported & BUS_NONSPI);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000483 if (dummy_buses_supported & BUS_SPI)
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000484 register_spi_master(&spi_master_dummyflasher);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000485
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000486 return 0;
487}
488
Thomas Heijligencc853d82021-05-04 15:32:17 +0200489static void *dummy_map(const char *descr, uintptr_t phys_addr, size_t len)
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000490{
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000491 msg_pspew("%s: Mapping %s, 0x%zx bytes at 0x%0*" PRIxPTR "\n",
Stefan Tauner0554ca52013-07-25 22:54:25 +0000492 __func__, descr, len, PRIxPTR_WIDTH, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000493 return (void *)phys_addr;
494}
495
Thomas Heijligencc853d82021-05-04 15:32:17 +0200496static void dummy_unmap(void *virt_addr, size_t len)
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000497{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000498 msg_pspew("%s: Unmapping 0x%zx bytes at %p\n", __func__, len, virt_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000499}
500
Mark Marshallf20b7be2014-05-09 21:16:21 +0000501static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000502{
Stefan Taunerc2333752013-07-13 23:31:37 +0000503 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%02x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000504}
505
Mark Marshallf20b7be2014-05-09 21:16:21 +0000506static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000507{
Stefan Taunerc2333752013-07-13 23:31:37 +0000508 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%04x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000509}
510
Mark Marshallf20b7be2014-05-09 21:16:21 +0000511static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, 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 ", val=0x%08x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000514}
515
Mark Marshallf20b7be2014-05-09 21:16:21 +0000516static 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 +0000517{
518 size_t i;
Stefan Tauner0554ca52013-07-25 22:54:25 +0000519 msg_pspew("%s: addr=0x%" PRIxPTR ", len=0x%zx, writing data (hex):", __func__, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000520 for (i = 0; i < len; i++) {
521 if ((i % 16) == 0)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000522 msg_pspew("\n");
523 msg_pspew("%02x ", buf[i]);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000524 }
525}
526
Mark Marshallf20b7be2014-05-09 21:16:21 +0000527static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000528{
Stefan Taunerc2333752013-07-13 23:31:37 +0000529 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000530 return 0xff;
531}
532
Mark Marshallf20b7be2014-05-09 21:16:21 +0000533static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000534{
Stefan Taunerc2333752013-07-13 23:31:37 +0000535 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000536 return 0xffff;
537}
538
Mark Marshallf20b7be2014-05-09 21:16:21 +0000539static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000540{
Stefan Taunerc2333752013-07-13 23:31:37 +0000541 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffffffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000542 return 0xffffffff;
543}
544
Mark Marshallf20b7be2014-05-09 21:16:21 +0000545static 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 +0000546{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000547 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 +0000548 memset(buf, 0xff, len);
549 return;
550}
551
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200552static uint8_t get_reg_ro_bit_mask(const struct emu_data *data, enum flash_reg reg)
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200553{
554 /* Whoever adds a new register must not forget to update this function
555 or at least shouldn't use it incorrectly. */
556 assert(reg == STATUS1 || reg == STATUS2 || reg == STATUS3);
557
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200558 uint8_t ro_bits = reg == STATUS1 ? SPI_SR_WIP : 0;
559
560 if (data->emu_chip == EMULATE_WINBOND_W25Q128FV) {
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200561 const bool srp0 = (data->emu_status[0] >> 7);
562 const bool srp1 = (data->emu_status[1] & 1);
563
564 const bool wp_active = (srp1 || (srp0 && data->hwwp));
565
566 if (wp_active) {
567 ro_bits = 0xff;
568 } else if (reg == STATUS2) {
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200569 /* SUS (bit_7) and (R) (bit_2). */
570 ro_bits = 0x84;
571 /* Once any of the lock bits (LB[1..3]) are set, they
572 can't be unset. */
573 ro_bits |= data->emu_status[1] & (1 << 3);
574 ro_bits |= data->emu_status[1] & (1 << 4);
575 ro_bits |= data->emu_status[1] & (1 << 5);
576 } else if (reg == STATUS3) {
577 /* Four reserved bits. */
578 ro_bits = 0x1b;
579 }
580 }
581
582 return ro_bits;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200583}
584
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200585static void update_write_protection(struct emu_data *data)
586{
587 if (data->emu_chip != EMULATE_WINBOND_W25Q128FV)
588 return;
589
590 const struct wp_bits bits = {
591 .srp = data->emu_status[0] >> 7,
592 .srl = data->emu_status[1] & 1,
593
594 .bp_bit_count = 3,
595 .bp =
596 {
597 (data->emu_status[0] >> 2) & 1,
598 (data->emu_status[0] >> 3) & 1,
599 (data->emu_status[0] >> 4) & 1
600 },
601
602 .tb_bit_present = true,
603 .tb = (data->emu_status[0] >> 5) & 1,
604
605 .sec_bit_present = true,
606 .sec = (data->emu_status[0] >> 6) & 1,
607
608 .cmp_bit_present = true,
609 .cmp = (data->emu_status[1] >> 6) & 1,
610 };
611
612 size_t start;
613 size_t len;
614 decode_range_spi25(&start, &len, &bits, data->emu_chip_size);
615
616 data->wp_start = start;
617 data->wp_end = start + len;
618}
619
620/* Checks whether range intersects a write-protected area of the flash if one is
621 * defined. */
622static bool is_write_protected(const struct emu_data *data, uint32_t start, uint32_t len)
623{
624 if (len == 0)
625 return false;
626
627 const uint32_t last = start + len - 1;
628 return (start < data->wp_end && last >= data->wp_start);
629}
630
631/* Returns non-zero on error. */
632static int write_flash_data(struct emu_data *data, uint32_t start, uint32_t len, const uint8_t *buf)
633{
634 if (is_write_protected(data, start, len)) {
635 msg_perr("At least part of the write range is write protected!\n");
636 return 1;
637 }
638
639 memcpy(data->flashchip_contents + start, buf, len);
640 data->emu_modified = 1;
641 return 0;
642}
643
644/* Returns non-zero on error. */
645static int erase_flash_data(struct emu_data *data, uint32_t start, uint32_t len)
646{
647 if (is_write_protected(data, start, len)) {
648 msg_perr("At least part of the erase range is write protected!\n");
649 return 1;
650 }
651
652 memset(data->flashchip_contents + start, 0xff, len);
653 data->emu_modified = 1;
654 return 0;
655}
656
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000657static int emulate_spi_chip_response(unsigned int writecnt,
658 unsigned int readcnt,
659 const unsigned char *writearr,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000660 unsigned char *readarr,
661 struct emu_data *data)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000662{
Stefan Tauner0b9df972012-05-07 22:12:16 +0000663 unsigned int offs, i, toread;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200664 uint8_t ro_bits;
665 bool wrsr_ext;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000666 static int unsigned aai_offs;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000667 const unsigned char sst25vf040_rems_response[2] = {0xbf, 0x44};
668 const unsigned char sst25vf032b_rems_response[2] = {0xbf, 0x4a};
669 const unsigned char mx25l6436_rems_response[2] = {0xc2, 0x16};
Nico Huberf9632d82019-01-20 11:23:49 +0100670 const unsigned char w25q128fv_rems_response[2] = {0xef, 0x17};
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000671
672 if (writecnt == 0) {
673 msg_perr("No command sent to the chip!\n");
674 return 1;
675 }
Paul Menzelac427b22012-02-16 21:07:07 +0000676 /* spi_blacklist has precedence over spi_ignorelist. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000677 for (i = 0; i < data->spi_blacklist_size; i++) {
678 if (writearr[0] == data->spi_blacklist[i]) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000679 msg_pdbg("Refusing blacklisted SPI command 0x%02x\n",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000680 data->spi_blacklist[i]);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000681 return SPI_INVALID_OPCODE;
682 }
683 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000684 for (i = 0; i < data->spi_ignorelist_size; i++) {
685 if (writearr[0] == data->spi_ignorelist[i]) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000686 msg_cdbg("Ignoring ignorelisted SPI command 0x%02x\n",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000687 data->spi_ignorelist[i]);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000688 /* Return success because the command does not fail,
689 * it is simply ignored.
690 */
691 return 0;
692 }
693 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000694
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200695 if (data->emu_max_aai_size && (data->emu_status[0] & SPI_SR_AAI)) {
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000696 if (writearr[0] != JEDEC_AAI_WORD_PROGRAM &&
697 writearr[0] != JEDEC_WRDI &&
698 writearr[0] != JEDEC_RDSR) {
699 msg_perr("Forbidden opcode (0x%02x) attempted during "
700 "AAI sequence!\n", writearr[0]);
701 return 0;
702 }
703 }
704
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000705 switch (writearr[0]) {
706 case JEDEC_RES:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000707 if (writecnt < JEDEC_RES_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000708 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000709 /* offs calculation is only needed for SST chips which treat RES like REMS. */
710 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
711 offs += writecnt - JEDEC_REMS_OUTSIZE;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000712 switch (data->emu_chip) {
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000713 case EMULATE_ST_M25P10_RES:
714 if (readcnt > 0)
715 memset(readarr, 0x10, readcnt);
716 break;
717 case EMULATE_SST_SST25VF040_REMS:
718 for (i = 0; i < readcnt; i++)
719 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
720 break;
721 case EMULATE_SST_SST25VF032B:
722 for (i = 0; i < readcnt; i++)
723 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
724 break;
725 case EMULATE_MACRONIX_MX25L6436:
726 if (readcnt > 0)
727 memset(readarr, 0x16, readcnt);
728 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100729 case EMULATE_WINBOND_W25Q128FV:
730 if (readcnt > 0)
731 memset(readarr, 0x17, readcnt);
732 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000733 default: /* ignore */
734 break;
735 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000736 break;
737 case JEDEC_REMS:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000738 /* REMS response has wraparound and uses an address parameter. */
739 if (writecnt < JEDEC_REMS_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000740 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000741 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
742 offs += writecnt - JEDEC_REMS_OUTSIZE;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000743 switch (data->emu_chip) {
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000744 case EMULATE_SST_SST25VF040_REMS:
745 for (i = 0; i < readcnt; i++)
746 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
747 break;
748 case EMULATE_SST_SST25VF032B:
749 for (i = 0; i < readcnt; i++)
750 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
751 break;
752 case EMULATE_MACRONIX_MX25L6436:
753 for (i = 0; i < readcnt; i++)
754 readarr[i] = mx25l6436_rems_response[(offs + i) % 2];
755 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100756 case EMULATE_WINBOND_W25Q128FV:
757 for (i = 0; i < readcnt; i++)
758 readarr[i] = w25q128fv_rems_response[(offs + i) % 2];
759 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000760 default: /* ignore */
761 break;
762 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000763 break;
764 case JEDEC_RDID:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000765 switch (data->emu_chip) {
Stefan Tauner0b9df972012-05-07 22:12:16 +0000766 case EMULATE_SST_SST25VF032B:
767 if (readcnt > 0)
768 readarr[0] = 0xbf;
769 if (readcnt > 1)
770 readarr[1] = 0x25;
771 if (readcnt > 2)
772 readarr[2] = 0x4a;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000773 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000774 case EMULATE_MACRONIX_MX25L6436:
775 if (readcnt > 0)
776 readarr[0] = 0xc2;
777 if (readcnt > 1)
778 readarr[1] = 0x20;
779 if (readcnt > 2)
780 readarr[2] = 0x17;
781 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100782 case EMULATE_WINBOND_W25Q128FV:
783 if (readcnt > 0)
784 readarr[0] = 0xef;
785 if (readcnt > 1)
786 readarr[1] = 0x40;
787 if (readcnt > 2)
788 readarr[2] = 0x18;
789 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000790 default: /* ignore */
791 break;
792 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000793 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000794 case JEDEC_RDSR:
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200795 memset(readarr, data->emu_status[0], readcnt);
796 break;
797 case JEDEC_RDSR2:
798 if (data->emu_status_len >= 2)
799 memset(readarr, data->emu_status[1], readcnt);
800 break;
801 case JEDEC_RDSR3:
802 if (data->emu_status_len >= 3)
803 memset(readarr, data->emu_status[2], readcnt);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000804 break;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000805 /* FIXME: this should be chip-specific. */
806 case JEDEC_EWSR:
807 case JEDEC_WREN:
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200808 data->emu_status[0] |= SPI_SR_WEL;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000809 break;
810 case JEDEC_WRSR:
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200811 if (!(data->emu_status[0] & SPI_SR_WEL)) {
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000812 msg_perr("WRSR attempted, but WEL is 0!\n");
813 break;
814 }
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200815
816 wrsr_ext = (writecnt == 3 && data->emu_wrsr_ext);
817
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000818 /* FIXME: add some reasonable simulation of the busy flag */
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200819
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200820 ro_bits = get_reg_ro_bit_mask(data, STATUS1);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200821 data->emu_status[0] &= ro_bits;
822 data->emu_status[0] |= writearr[1] & ~ro_bits;
823 if (wrsr_ext) {
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200824 ro_bits = get_reg_ro_bit_mask(data, STATUS2);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200825 data->emu_status[1] &= ro_bits;
826 data->emu_status[1] |= writearr[2] & ~ro_bits;
827 }
828
829 if (wrsr_ext)
830 msg_pdbg2("WRSR wrote 0x%02x%02x.\n", data->emu_status[1], data->emu_status[0]);
831 else
832 msg_pdbg2("WRSR wrote 0x%02x.\n", data->emu_status[0]);
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200833
834 update_write_protection(data);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200835 break;
836 case JEDEC_WRSR2:
837 if (data->emu_status_len < 2)
838 break;
839 if (!(data->emu_status[0] & SPI_SR_WEL)) {
840 msg_perr("WRSR2 attempted, but WEL is 0!\n");
841 break;
842 }
843
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200844 ro_bits = get_reg_ro_bit_mask(data, STATUS2);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200845 data->emu_status[1] &= ro_bits;
846 data->emu_status[1] |= (writearr[1] & ~ro_bits);
847
848 msg_pdbg2("WRSR2 wrote 0x%02x.\n", data->emu_status[1]);
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200849
850 update_write_protection(data);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200851 break;
852 case JEDEC_WRSR3:
853 if (data->emu_status_len < 3)
854 break;
855 if (!(data->emu_status[0] & SPI_SR_WEL)) {
856 msg_perr("WRSR3 attempted, but WEL is 0!\n");
857 break;
858 }
859
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200860 ro_bits = get_reg_ro_bit_mask(data, STATUS3);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200861 data->emu_status[2] &= ro_bits;
862 data->emu_status[2] |= (writearr[1] & ~ro_bits);
863
864 msg_pdbg2("WRSR3 wrote 0x%02x.\n", data->emu_status[2]);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000865 break;
866 case JEDEC_READ:
867 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
868 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000869 offs %= data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000870 if (readcnt > 0)
Edward O'Callaghan94250222021-05-20 20:34:02 +1000871 memcpy(readarr, data->flashchip_contents + offs, readcnt);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000872 break;
873 case JEDEC_BYTE_PROGRAM:
874 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
875 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000876 offs %= data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000877 if (writecnt < 5) {
878 msg_perr("BYTE PROGRAM size too short!\n");
879 return 1;
880 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000881 if (writecnt - 4 > data->emu_max_byteprogram_size) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000882 msg_perr("Max BYTE PROGRAM size exceeded!\n");
883 return 1;
884 }
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200885 if (write_flash_data(data, offs, writecnt - 4, writearr + 4)) {
886 msg_perr("Failed to program flash!\n");
887 return 1;
888 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000889 break;
890 case JEDEC_AAI_WORD_PROGRAM:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000891 if (!data->emu_max_aai_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000892 break;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200893 if (!(data->emu_status[0] & SPI_SR_AAI)) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000894 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
895 msg_perr("Initial AAI WORD PROGRAM size too "
896 "short!\n");
897 return 1;
898 }
899 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
900 msg_perr("Initial AAI WORD PROGRAM size too "
901 "long!\n");
902 return 1;
903 }
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200904 data->emu_status[0] |= SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000905 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
906 writearr[3];
907 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000908 aai_offs %= data->emu_chip_size;
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200909 if (write_flash_data(data, aai_offs, 2, writearr + 4)) {
910 msg_perr("Failed to program flash!\n");
911 return 1;
912 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000913 aai_offs += 2;
914 } else {
915 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
916 msg_perr("Continuation AAI WORD PROGRAM size "
917 "too short!\n");
918 return 1;
919 }
920 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
921 msg_perr("Continuation AAI WORD PROGRAM size "
922 "too long!\n");
923 return 1;
924 }
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200925 if (write_flash_data(data, aai_offs, 2, writearr + 1)) {
926 msg_perr("Failed to program flash!\n");
927 return 1;
928 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000929 aai_offs += 2;
930 }
931 break;
932 case JEDEC_WRDI:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000933 if (data->emu_max_aai_size)
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200934 data->emu_status[0] &= ~SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000935 break;
936 case JEDEC_SE:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000937 if (!data->emu_jedec_se_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000938 break;
939 if (writecnt != JEDEC_SE_OUTSIZE) {
940 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
941 return 1;
942 }
943 if (readcnt != JEDEC_SE_INSIZE) {
944 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
945 return 1;
946 }
947 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +1000948 if (offs & (data->emu_jedec_se_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000949 msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000950 offs &= ~(data->emu_jedec_se_size - 1);
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200951 if (erase_flash_data(data, offs, data->emu_jedec_se_size)) {
952 msg_perr("Failed to erase flash!\n");
953 return 1;
954 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000955 break;
956 case JEDEC_BE_52:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000957 if (!data->emu_jedec_be_52_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000958 break;
959 if (writecnt != JEDEC_BE_52_OUTSIZE) {
960 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
961 return 1;
962 }
963 if (readcnt != JEDEC_BE_52_INSIZE) {
964 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
965 return 1;
966 }
967 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +1000968 if (offs & (data->emu_jedec_be_52_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000969 msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000970 offs &= ~(data->emu_jedec_be_52_size - 1);
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200971 if (erase_flash_data(data, offs, data->emu_jedec_be_52_size)) {
972 msg_perr("Failed to erase flash!\n");
973 return 1;
974 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000975 break;
976 case JEDEC_BE_D8:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000977 if (!data->emu_jedec_be_d8_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000978 break;
979 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
980 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
981 return 1;
982 }
983 if (readcnt != JEDEC_BE_D8_INSIZE) {
984 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
985 return 1;
986 }
987 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +1000988 if (offs & (data->emu_jedec_be_d8_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000989 msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000990 offs &= ~(data->emu_jedec_be_d8_size - 1);
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200991 if (erase_flash_data(data, offs, data->emu_jedec_be_d8_size)) {
992 msg_perr("Failed to erase flash!\n");
993 return 1;
994 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000995 break;
996 case JEDEC_CE_60:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000997 if (!data->emu_jedec_ce_60_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000998 break;
999 if (writecnt != JEDEC_CE_60_OUTSIZE) {
1000 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
1001 return 1;
1002 }
1003 if (readcnt != JEDEC_CE_60_INSIZE) {
1004 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
1005 return 1;
1006 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +00001007 /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001008 /* emu_jedec_ce_60_size is emu_chip_size. */
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +02001009 if (erase_flash_data(data, 0, data->emu_jedec_ce_60_size)) {
1010 msg_perr("Failed to erase flash!\n");
1011 return 1;
1012 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001013 break;
1014 case JEDEC_CE_C7:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001015 if (!data->emu_jedec_ce_c7_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001016 break;
1017 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
1018 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
1019 return 1;
1020 }
1021 if (readcnt != JEDEC_CE_C7_INSIZE) {
1022 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
1023 return 1;
1024 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +00001025 /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001026 /* emu_jedec_ce_c7_size is emu_chip_size. */
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +02001027 if (erase_flash_data(data, 0, data->emu_jedec_ce_c7_size)) {
1028 msg_perr("Failed to erase flash!\n");
1029 return 1;
1030 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001031 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +00001032 case JEDEC_SFDP:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001033 if (data->emu_chip != EMULATE_MACRONIX_MX25L6436)
Stefan Tauner0b9df972012-05-07 22:12:16 +00001034 break;
1035 if (writecnt < 4)
1036 break;
1037 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
1038
1039 /* SFDP expects one dummy byte after the address. */
1040 if (writecnt == 4) {
1041 /* The dummy byte was not written, make sure it is read instead.
1042 * Shifting and shortening the read array does achieve this goal.
1043 */
1044 readarr++;
1045 readcnt--;
1046 } else {
1047 /* The response is shifted if more than 5 bytes are written, because SFDP data is
1048 * already shifted out by the chip while those superfluous bytes are written. */
1049 offs += writecnt - 5;
1050 }
1051
1052 /* The SFDP spec implies that the start address of an SFDP read may be truncated to fit in the
1053 * SFDP table address space, i.e. the start address may be wrapped around at SFDP table size.
1054 * This is a reasonable implementation choice in hardware because it saves a few gates. */
1055 if (offs >= sizeof(sfdp_table)) {
1056 msg_pdbg("Wrapping the start address around the SFDP table boundary (using 0x%x "
1057 "instead of 0x%x).\n", (unsigned int)(offs % sizeof(sfdp_table)), offs);
1058 offs %= sizeof(sfdp_table);
1059 }
1060 toread = min(sizeof(sfdp_table) - offs, readcnt);
1061 memcpy(readarr, sfdp_table + offs, toread);
1062 if (toread < readcnt)
1063 msg_pdbg("Crossing the SFDP table boundary in a single "
1064 "continuous chunk produces undefined results "
1065 "after that point.\n");
1066 break;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001067 default:
1068 /* No special response. */
1069 break;
1070 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +00001071 if (writearr[0] != JEDEC_WREN && writearr[0] != JEDEC_EWSR)
Sergii Dmytruk59151a42021-11-08 00:05:12 +02001072 data->emu_status[0] &= ~SPI_SR_WEL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001073 return 0;
1074}
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001075
Edward O'Callaghan5eca4272020-04-12 17:27:53 +10001076static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001077 unsigned int readcnt,
1078 const unsigned char *writearr,
1079 unsigned char *readarr)
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001080{
Nico Huber519be662018-12-23 20:03:35 +01001081 unsigned int i;
Lachlan Bishopc753c402020-09-10 14:57:05 +10001082 struct emu_data *emu_data = flash->mst->spi.data;
1083 if (!emu_data) {
1084 msg_perr("No data in flash context!\n");
1085 return 1;
1086 }
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001087
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +00001088 msg_pspew("%s:", __func__);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001089
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +00001090 msg_pspew(" writing %u bytes:", writecnt);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001091 for (i = 0; i < writecnt; i++)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +00001092 msg_pspew(" 0x%02x", writearr[i]);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001093
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001094 /* Response for unknown commands and missing chip is 0xff. */
1095 memset(readarr, 0xff, readcnt);
Lachlan Bishopc753c402020-09-10 14:57:05 +10001096 switch (emu_data->emu_chip) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001097 case EMULATE_ST_M25P10_RES:
1098 case EMULATE_SST_SST25VF040_REMS:
1099 case EMULATE_SST_SST25VF032B:
Stefan Tauner0b9df972012-05-07 22:12:16 +00001100 case EMULATE_MACRONIX_MX25L6436:
Nico Huberf9632d82019-01-20 11:23:49 +01001101 case EMULATE_WINBOND_W25Q128FV:
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001102 if (emulate_spi_chip_response(writecnt, readcnt, writearr,
Lachlan Bishopc753c402020-09-10 14:57:05 +10001103 readarr, emu_data)) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +00001104 msg_pdbg("Invalid command sent to flash chip!\n");
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001105 return 1;
1106 }
1107 break;
1108 default:
1109 break;
1110 }
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +00001111 msg_pspew(" reading %u bytes:", readcnt);
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001112 for (i = 0; i < readcnt; i++)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001113 msg_pspew(" 0x%02x", readarr[i]);
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +00001114 msg_pspew("\n");
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001115 return 0;
1116}
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +00001117
Mark Marshallf20b7be2014-05-09 21:16:21 +00001118static 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 +00001119{
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001120 return spi_write_chunked(flash, buf, start, len,
1121 spi_write_256_chunksize);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001122}
Thomas Heijligencc853d82021-05-04 15:32:17 +02001123
1124const struct programmer_entry programmer_dummy = {
1125 .name = "dummy",
1126 .type = OTHER,
1127 /* FIXME */
1128 .devs.note = "Dummy device, does nothing and logs all accesses\n",
1129 .init = dummy_init,
1130 .map_flash_region = dummy_map,
1131 .unmap_flash_region = dummy_unmap,
1132 .delay = internal_delay,
1133};