blob: 27b212efc2fe84226887de574571314819fed025 [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
Nico Huberbbccdb22022-05-28 16:48:26 +020046 * WRSR code on enabling WRSR_EXT2 for more chips. */
47 bool emu_wrsr_ext2;
48 bool emu_wrsr_ext3;
Lachlan Bishopc753c402020-09-10 14:57:05 +100049 int emu_modified; /* is the image modified since reading it? */
Sergii Dmytruk59151a42021-11-08 00:05:12 +020050 uint8_t emu_status[3];
51 uint8_t emu_status_len; /* number of emulated status registers */
Lachlan Bishopc753c402020-09-10 14:57:05 +100052 unsigned int emu_max_byteprogram_size;
53 unsigned int emu_max_aai_size;
54 unsigned int emu_jedec_se_size;
55 unsigned int emu_jedec_be_52_size;
56 unsigned int emu_jedec_be_d8_size;
57 unsigned int emu_jedec_ce_60_size;
58 unsigned int emu_jedec_ce_c7_size;
59 unsigned char spi_blacklist[256];
60 unsigned char spi_ignorelist[256];
61 unsigned int spi_blacklist_size;
62 unsigned int spi_ignorelist_size;
Edward O'Callaghan94250222021-05-20 20:34:02 +100063
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +020064 bool hwwp; /* state of hardware write protection */
65 /* wp_start == wp_end when write-protection is disabled */
66 uint32_t wp_start;
67 uint32_t wp_end;
68
Edward O'Callaghan94250222021-05-20 20:34:02 +100069 uint8_t *flashchip_contents;
Lachlan Bishopc753c402020-09-10 14:57:05 +100070};
71
Stefan Tauner0b9df972012-05-07 22:12:16 +000072/* A legit complete SFDP table based on the MX25L6436E (rev. 1.8) datasheet. */
Stefan Tauner67d163d2013-01-15 17:37:48 +000073static const uint8_t sfdp_table[] = {
Stefan Tauner0b9df972012-05-07 22:12:16 +000074 0x53, 0x46, 0x44, 0x50, // @0x00: SFDP signature
75 0x00, 0x01, 0x01, 0xFF, // @0x04: revision 1.0, 2 headers
76 0x00, 0x00, 0x01, 0x09, // @0x08: JEDEC SFDP header rev. 1.0, 9 DW long
77 0x1C, 0x00, 0x00, 0xFF, // @0x0C: PTP0 = 0x1C (instead of 0x30)
78 0xC2, 0x00, 0x01, 0x04, // @0x10: Macronix header rev. 1.0, 4 DW long
79 0x48, 0x00, 0x00, 0xFF, // @0x14: PTP1 = 0x48 (instead of 0x60)
80 0xFF, 0xFF, 0xFF, 0xFF, // @0x18: hole.
81 0xE5, 0x20, 0xC9, 0xFF, // @0x1C: SFDP parameter table start
82 0xFF, 0xFF, 0xFF, 0x03, // @0x20
83 0x00, 0xFF, 0x08, 0x6B, // @0x24
84 0x08, 0x3B, 0x00, 0xFF, // @0x28
85 0xEE, 0xFF, 0xFF, 0xFF, // @0x2C
86 0xFF, 0xFF, 0x00, 0x00, // @0x30
87 0xFF, 0xFF, 0x00, 0xFF, // @0x34
88 0x0C, 0x20, 0x0F, 0x52, // @0x38
89 0x10, 0xD8, 0x00, 0xFF, // @0x3C: SFDP parameter table end
90 0xFF, 0xFF, 0xFF, 0xFF, // @0x40: hole.
91 0xFF, 0xFF, 0xFF, 0xFF, // @0x44: hole.
92 0x00, 0x36, 0x00, 0x27, // @0x48: Macronix parameter table start
93 0xF4, 0x4F, 0xFF, 0xFF, // @0x4C
94 0xD9, 0xC8, 0xFF, 0xFF, // @0x50
95 0xFF, 0xFF, 0xFF, 0xFF, // @0x54: Macronix parameter table end
96};
97
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000098
Stefan Taunerc69c9c82011-11-23 09:13:48 +000099static unsigned int spi_write_256_chunksize = 256;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000100
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000101static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
Mark Marshallf20b7be2014-05-09 21:16:21 +0000102 const unsigned char *writearr, unsigned char *readarr);
103static int dummy_spi_write_256(struct flashctx *flash, const uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000104 unsigned int start, unsigned int len);
Mark Marshallf20b7be2014-05-09 21:16:21 +0000105static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr);
106static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr);
107static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr);
108static void dummy_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len);
109static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr);
110static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr);
111static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr);
112static void dummy_chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len);
Michael Karcherb9dbe482011-05-11 17:07:07 +0000113
Lachlan Bishopc753c402020-09-10 14:57:05 +1000114static struct spi_master spi_master_dummyflasher = {
Nico Huber1cf407b2017-11-10 20:18:23 +0100115 .features = SPI_MASTER_4BA,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000116 .max_data_read = MAX_DATA_READ_UNLIMITED,
117 .max_data_write = MAX_DATA_UNSPECIFIED,
118 .command = dummy_spi_send_command,
119 .multicommand = default_spi_send_multicommand,
120 .read = default_spi_read,
121 .write_256 = dummy_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +0000122 .write_aai = default_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000123};
David Hendricks8bb20212011-06-14 01:35:36 +0000124
Lachlan Bishopc753c402020-09-10 14:57:05 +1000125static struct par_master par_master_dummy = {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000126 .chip_readb = dummy_chip_readb,
127 .chip_readw = dummy_chip_readw,
128 .chip_readl = dummy_chip_readl,
129 .chip_readn = dummy_chip_readn,
130 .chip_writeb = dummy_chip_writeb,
131 .chip_writew = dummy_chip_writew,
132 .chip_writel = dummy_chip_writel,
133 .chip_writen = dummy_chip_writen,
134};
135
David Hendricks8bb20212011-06-14 01:35:36 +0000136static int dummy_shutdown(void *data)
137{
138 msg_pspew("%s\n", __func__);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000139 struct emu_data *emu_data = (struct emu_data *)data;
140 if (emu_data->emu_chip != EMULATE_NONE) {
141 if (emu_data->emu_persistent_image && emu_data->emu_modified) {
142 msg_pdbg("Writing %s\n", emu_data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000143 write_buf_to_file(emu_data->flashchip_contents,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000144 emu_data->emu_chip_size,
145 emu_data->emu_persistent_image);
David Hendricks8bb20212011-06-14 01:35:36 +0000146 }
Angel Pons02b9ae22021-05-25 12:46:43 +0200147 free(emu_data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000148 free(emu_data->flashchip_contents);
David Hendricks8bb20212011-06-14 01:35:36 +0000149 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000150 free(data);
David Hendricks8bb20212011-06-14 01:35:36 +0000151 return 0;
152}
153
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000154static int init_data(struct emu_data *data, enum chipbustype *dummy_buses_supported)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000155{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000156 char *bustext = NULL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000157 char *tmp = NULL;
Nico Huber519be662018-12-23 20:03:35 +0100158 unsigned int i;
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000159 char *endptr;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000160 char *status = NULL;
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000161
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000162 bustext = extract_programmer_param("bus");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000163 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
164 if (!bustext)
165 bustext = strdup("parallel+lpc+fwh+spi");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000166 /* Convert the parameters to lowercase. */
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000167 tolower_string(bustext);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000168
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000169 *dummy_buses_supported = BUS_NONE;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000170 if (strstr(bustext, "parallel")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000171 *dummy_buses_supported |= BUS_PARALLEL;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000172 msg_pdbg("Enabling support for %s flash.\n", "parallel");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000173 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000174 if (strstr(bustext, "lpc")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000175 *dummy_buses_supported |= BUS_LPC;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000176 msg_pdbg("Enabling support for %s flash.\n", "LPC");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000177 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000178 if (strstr(bustext, "fwh")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000179 *dummy_buses_supported |= BUS_FWH;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000180 msg_pdbg("Enabling support for %s flash.\n", "FWH");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000181 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000182 if (strstr(bustext, "spi")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000183 *dummy_buses_supported |= BUS_SPI;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000184 msg_pdbg("Enabling support for %s flash.\n", "SPI");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000185 }
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000186 if (*dummy_buses_supported == BUS_NONE)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000187 msg_pdbg("Support for all flash bus types disabled.\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000188 free(bustext);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000189
190 tmp = extract_programmer_param("spi_write_256_chunksize");
191 if (tmp) {
Edward O'Callaghanc785f882021-05-23 22:14:36 +1000192 spi_write_256_chunksize = strtoul(tmp, &endptr, 0);
193 if (*endptr != '\0' || spi_write_256_chunksize < 1) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000194 msg_perr("invalid spi_write_256_chunksize\n");
Edward O'Callaghanc785f882021-05-23 22:14:36 +1000195 free(tmp);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000196 return 1;
197 }
Edward O'Callaghanc785f882021-05-23 22:14:36 +1000198 free(tmp);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000199 }
200
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000201 tmp = extract_programmer_param("spi_blacklist");
202 if (tmp) {
203 i = strlen(tmp);
204 if (!strncmp(tmp, "0x", 2)) {
205 i -= 2;
206 memmove(tmp, tmp + 2, i + 1);
207 }
208 if ((i > 512) || (i % 2)) {
209 msg_perr("Invalid SPI command blacklist length\n");
210 free(tmp);
211 return 1;
212 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000213 data->spi_blacklist_size = i / 2;
214 for (i = 0; i < data->spi_blacklist_size * 2; i++) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000215 if (!isxdigit((unsigned char)tmp[i])) {
216 msg_perr("Invalid char \"%c\" in SPI command "
217 "blacklist\n", tmp[i]);
218 free(tmp);
219 return 1;
220 }
221 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000222 for (i = 0; i < data->spi_blacklist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000223 unsigned int tmp2;
224 /* SCNx8 is apparently not supported by MSVC (and thus
225 * MinGW), so work around it with an extra variable
226 */
227 sscanf(tmp + i * 2, "%2x", &tmp2);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000228 data->spi_blacklist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000229 }
230 msg_pdbg("SPI blacklist is ");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000231 for (i = 0; i < data->spi_blacklist_size; i++)
232 msg_pdbg("%02x ", data->spi_blacklist[i]);
233 msg_pdbg(", size %u\n", data->spi_blacklist_size);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000234 }
235 free(tmp);
236
237 tmp = extract_programmer_param("spi_ignorelist");
238 if (tmp) {
239 i = strlen(tmp);
240 if (!strncmp(tmp, "0x", 2)) {
241 i -= 2;
242 memmove(tmp, tmp + 2, i + 1);
243 }
244 if ((i > 512) || (i % 2)) {
245 msg_perr("Invalid SPI command ignorelist length\n");
246 free(tmp);
247 return 1;
248 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000249 data->spi_ignorelist_size = i / 2;
250 for (i = 0; i < data->spi_ignorelist_size * 2; i++) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000251 if (!isxdigit((unsigned char)tmp[i])) {
252 msg_perr("Invalid char \"%c\" in SPI command "
253 "ignorelist\n", tmp[i]);
254 free(tmp);
255 return 1;
256 }
257 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000258 for (i = 0; i < data->spi_ignorelist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000259 unsigned int tmp2;
260 /* SCNx8 is apparently not supported by MSVC (and thus
261 * MinGW), so work around it with an extra variable
262 */
263 sscanf(tmp + i * 2, "%2x", &tmp2);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000264 data->spi_ignorelist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000265 }
266 msg_pdbg("SPI ignorelist is ");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000267 for (i = 0; i < data->spi_ignorelist_size; i++)
268 msg_pdbg("%02x ", data->spi_ignorelist[i]);
269 msg_pdbg(", size %u\n", data->spi_ignorelist_size);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000270 }
271 free(tmp);
272
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200273 tmp = extract_programmer_param("hwwp");
274 if (tmp) {
275 if (!strcmp(tmp, "yes")) {
276 msg_pdbg("Emulated chip will have hardware WP enabled\n");
277 data->hwwp = true;
278 } else if (!strcmp(tmp, "no")) {
279 msg_pdbg("Emulated chip will have hardware WP disabled\n");
280 } else {
281 msg_perr("hwwp can be \"yes\" or \"no\"\n");
282 free(tmp);
283 return 1;
284 }
285 free(tmp);
286 }
287
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000288 tmp = extract_programmer_param("emulate");
289 if (!tmp) {
290 msg_pdbg("Not emulating any flash chip.\n");
291 /* Nothing else to do. */
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000292 return 0;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000293 }
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000294
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000295 if (!strcmp(tmp, "M25P10.RES")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000296 data->emu_chip = EMULATE_ST_M25P10_RES;
297 data->emu_chip_size = 128 * 1024;
298 data->emu_max_byteprogram_size = 128;
299 data->emu_max_aai_size = 0;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200300 data->emu_status_len = 1;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000301 data->emu_jedec_se_size = 0;
302 data->emu_jedec_be_52_size = 0;
303 data->emu_jedec_be_d8_size = 32 * 1024;
304 data->emu_jedec_ce_60_size = 0;
305 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000306 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
307 "write)\n");
308 }
309 if (!strcmp(tmp, "SST25VF040.REMS")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000310 data->emu_chip = EMULATE_SST_SST25VF040_REMS;
311 data->emu_chip_size = 512 * 1024;
312 data->emu_max_byteprogram_size = 1;
313 data->emu_max_aai_size = 0;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200314 data->emu_status_len = 1;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000315 data->emu_jedec_se_size = 4 * 1024;
316 data->emu_jedec_be_52_size = 32 * 1024;
317 data->emu_jedec_be_d8_size = 0;
318 data->emu_jedec_ce_60_size = data->emu_chip_size;
319 data->emu_jedec_ce_c7_size = 0;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000320 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
321 "byte write)\n");
322 }
323 if (!strcmp(tmp, "SST25VF032B")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000324 data->emu_chip = EMULATE_SST_SST25VF032B;
325 data->emu_chip_size = 4 * 1024 * 1024;
326 data->emu_max_byteprogram_size = 1;
327 data->emu_max_aai_size = 2;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200328 data->emu_status_len = 1;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000329 data->emu_jedec_se_size = 4 * 1024;
330 data->emu_jedec_be_52_size = 32 * 1024;
331 data->emu_jedec_be_d8_size = 64 * 1024;
332 data->emu_jedec_ce_60_size = data->emu_chip_size;
333 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000334 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
335 "write)\n");
336 }
Stefan Tauner0b9df972012-05-07 22:12:16 +0000337 if (!strcmp(tmp, "MX25L6436")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000338 data->emu_chip = EMULATE_MACRONIX_MX25L6436;
339 data->emu_chip_size = 8 * 1024 * 1024;
340 data->emu_max_byteprogram_size = 256;
341 data->emu_max_aai_size = 0;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200342 data->emu_status_len = 1;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000343 data->emu_jedec_se_size = 4 * 1024;
344 data->emu_jedec_be_52_size = 32 * 1024;
345 data->emu_jedec_be_d8_size = 64 * 1024;
346 data->emu_jedec_ce_60_size = data->emu_chip_size;
347 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000348 msg_pdbg("Emulating Macronix MX25L6436 SPI flash chip (RDID, "
349 "SFDP)\n");
350 }
Nico Huberf9632d82019-01-20 11:23:49 +0100351 if (!strcmp(tmp, "W25Q128FV")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000352 data->emu_chip = EMULATE_WINBOND_W25Q128FV;
Nico Huberbbccdb22022-05-28 16:48:26 +0200353 data->emu_wrsr_ext2 = true;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000354 data->emu_chip_size = 16 * 1024 * 1024;
355 data->emu_max_byteprogram_size = 256;
356 data->emu_max_aai_size = 0;
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200357 data->emu_status_len = 3;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000358 data->emu_jedec_se_size = 4 * 1024;
359 data->emu_jedec_be_52_size = 32 * 1024;
360 data->emu_jedec_be_d8_size = 64 * 1024;
361 data->emu_jedec_ce_60_size = data->emu_chip_size;
362 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Nico Huberf9632d82019-01-20 11:23:49 +0100363 msg_pdbg("Emulating Winbond W25Q128FV SPI flash chip (RDID)\n");
364 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000365 if (data->emu_chip == EMULATE_NONE) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000366 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
367 free(tmp);
368 return 1;
369 }
370 free(tmp);
David Hendricks8bb20212011-06-14 01:35:36 +0000371
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000372 status = extract_programmer_param("spi_status");
373 if (status) {
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200374 unsigned int emu_status;
375
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000376 errno = 0;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200377 emu_status = strtoul(status, &endptr, 0);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000378 if (errno != 0 || status == endptr) {
Angel Ponsc2484642021-05-25 13:03:24 +0200379 free(status);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000380 msg_perr("Error: initial status register specified, "
381 "but the value could not be converted.\n");
382 return 1;
383 }
Angel Ponsc2484642021-05-25 13:03:24 +0200384 free(status);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200385
386 data->emu_status[0] = emu_status;
387 data->emu_status[1] = emu_status >> 8;
388 data->emu_status[2] = emu_status >> 16;
389
390 if (data->emu_status_len == 3) {
391 msg_pdbg("Initial status registers:\n"
392 "\tSR1 is set to 0x%02x\n"
393 "\tSR2 is set to 0x%02x\n"
394 "\tSR3 is set to 0x%02x\n",
395 data->emu_status[0], data->emu_status[1], data->emu_status[2]);
396 } else if (data->emu_status_len == 2) {
397 msg_pdbg("Initial status registers:\n"
398 "\tSR1 is set to 0x%02x\n"
399 "\tSR2 is set to 0x%02x\n",
400 data->emu_status[0], data->emu_status[1]);
401 } else {
402 msg_pdbg("Initial status register is set to 0x%02x.\n",
403 data->emu_status[0]);
404 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000405 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000406
Angel Pons328898a2021-05-25 12:56:18 +0200407 data->flashchip_contents = malloc(data->emu_chip_size);
408 if (!data->flashchip_contents) {
409 msg_perr("Out of memory!\n");
410 return 1;
411 }
412
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000413
414 return 0;
415}
416
Thomas Heijligencc853d82021-05-04 15:32:17 +0200417static int dummy_init(void)
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000418{
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000419 struct stat image_stat;
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000420
421 struct emu_data *data = calloc(1, sizeof(struct emu_data));
422 if (!data) {
423 msg_perr("Out of memory!\n");
424 return 1;
425 }
426 data->emu_chip = EMULATE_NONE;
427 spi_master_dummyflasher.data = data;
428 par_master_dummy.data = data;
429
430 msg_pspew("%s\n", __func__);
431
432 enum chipbustype dummy_buses_supported;
433 if (init_data(data, &dummy_buses_supported)) {
434 free(data);
435 return 1;
436 }
437
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000438 if (data->emu_chip == EMULATE_NONE) {
439 msg_pdbg("Not emulating any flash chip.\n");
440 /* Nothing else to do. */
441 goto dummy_init_out;
442 }
443
Lachlan Bishopc753c402020-09-10 14:57:05 +1000444 msg_pdbg("Filling fake flash chip with 0xff, size %i\n", data->emu_chip_size);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000445 memset(data->flashchip_contents, 0xff, data->emu_chip_size);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000446
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000447 /* Will be freed by shutdown function if necessary. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000448 data->emu_persistent_image = extract_programmer_param("image");
449 if (!data->emu_persistent_image) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000450 /* Nothing else to do. */
David Hendricks8bb20212011-06-14 01:35:36 +0000451 goto dummy_init_out;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000452 }
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000453 /* We will silently (in default verbosity) ignore the file if it does not exist (yet) or the size does
454 * not match the emulated chip. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000455 if (!stat(data->emu_persistent_image, &image_stat)) {
Stefan Tauner23e10b82016-01-23 16:16:49 +0000456 msg_pdbg("Found persistent image %s, %jd B ",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000457 data->emu_persistent_image, (intmax_t)image_stat.st_size);
458 if ((uintmax_t)image_stat.st_size == data->emu_chip_size) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000459 msg_pdbg("matches.\n");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000460 msg_pdbg("Reading %s\n", data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000461 if (read_buf_from_file(data->flashchip_contents, data->emu_chip_size,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000462 data->emu_persistent_image)) {
463 msg_perr("Unable to read %s\n", data->emu_persistent_image);
Angel Pons02b9ae22021-05-25 12:46:43 +0200464 free(data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000465 free(data->flashchip_contents);
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000466 free(data);
Jacob Garberca598da2019-08-12 10:44:17 -0600467 return 1;
468 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000469 } else {
470 msg_pdbg("doesn't match.\n");
471 }
472 }
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000473
David Hendricks8bb20212011-06-14 01:35:36 +0000474dummy_init_out:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000475 if (register_shutdown(dummy_shutdown, data)) {
Angel Pons02b9ae22021-05-25 12:46:43 +0200476 free(data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000477 free(data->flashchip_contents);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000478 free(data);
David Hendricks8bb20212011-06-14 01:35:36 +0000479 return 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000480 }
Edward O'Callaghan3fa321d2021-05-17 20:01:27 +1000481 if (dummy_buses_supported & BUS_NONSPI)
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000482 register_par_master(&par_master_dummy,
Edward O'Callaghan3fa321d2021-05-17 20:01:27 +1000483 dummy_buses_supported & BUS_NONSPI);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000484 if (dummy_buses_supported & BUS_SPI)
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000485 register_spi_master(&spi_master_dummyflasher);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000486
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000487 return 0;
488}
489
Thomas Heijligencc853d82021-05-04 15:32:17 +0200490static void *dummy_map(const char *descr, uintptr_t phys_addr, size_t len)
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000491{
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000492 msg_pspew("%s: Mapping %s, 0x%zx bytes at 0x%0*" PRIxPTR "\n",
Stefan Tauner0554ca52013-07-25 22:54:25 +0000493 __func__, descr, len, PRIxPTR_WIDTH, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000494 return (void *)phys_addr;
495}
496
Thomas Heijligencc853d82021-05-04 15:32:17 +0200497static void dummy_unmap(void *virt_addr, size_t len)
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000498{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000499 msg_pspew("%s: Unmapping 0x%zx bytes at %p\n", __func__, len, virt_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000500}
501
Mark Marshallf20b7be2014-05-09 21:16:21 +0000502static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000503{
Stefan Taunerc2333752013-07-13 23:31:37 +0000504 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%02x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000505}
506
Mark Marshallf20b7be2014-05-09 21:16:21 +0000507static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000508{
Stefan Taunerc2333752013-07-13 23:31:37 +0000509 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%04x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000510}
511
Mark Marshallf20b7be2014-05-09 21:16:21 +0000512static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000513{
Stefan Taunerc2333752013-07-13 23:31:37 +0000514 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%08x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000515}
516
Mark Marshallf20b7be2014-05-09 21:16:21 +0000517static 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 +0000518{
519 size_t i;
Stefan Tauner0554ca52013-07-25 22:54:25 +0000520 msg_pspew("%s: addr=0x%" PRIxPTR ", len=0x%zx, writing data (hex):", __func__, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000521 for (i = 0; i < len; i++) {
522 if ((i % 16) == 0)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000523 msg_pspew("\n");
524 msg_pspew("%02x ", buf[i]);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000525 }
526}
527
Mark Marshallf20b7be2014-05-09 21:16:21 +0000528static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000529{
Stefan Taunerc2333752013-07-13 23:31:37 +0000530 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000531 return 0xff;
532}
533
Mark Marshallf20b7be2014-05-09 21:16:21 +0000534static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000535{
Stefan Taunerc2333752013-07-13 23:31:37 +0000536 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000537 return 0xffff;
538}
539
Mark Marshallf20b7be2014-05-09 21:16:21 +0000540static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000541{
Stefan Taunerc2333752013-07-13 23:31:37 +0000542 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffffffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000543 return 0xffffffff;
544}
545
Mark Marshallf20b7be2014-05-09 21:16:21 +0000546static 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 +0000547{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000548 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 +0000549 memset(buf, 0xff, len);
550 return;
551}
552
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200553static uint8_t get_reg_ro_bit_mask(const struct emu_data *data, enum flash_reg reg)
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200554{
555 /* Whoever adds a new register must not forget to update this function
556 or at least shouldn't use it incorrectly. */
557 assert(reg == STATUS1 || reg == STATUS2 || reg == STATUS3);
558
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200559 uint8_t ro_bits = reg == STATUS1 ? SPI_SR_WIP : 0;
560
561 if (data->emu_chip == EMULATE_WINBOND_W25Q128FV) {
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200562 const bool srp0 = (data->emu_status[0] >> 7);
563 const bool srp1 = (data->emu_status[1] & 1);
564
565 const bool wp_active = (srp1 || (srp0 && data->hwwp));
566
567 if (wp_active) {
568 ro_bits = 0xff;
569 } else if (reg == STATUS2) {
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200570 /* SUS (bit_7) and (R) (bit_2). */
571 ro_bits = 0x84;
572 /* Once any of the lock bits (LB[1..3]) are set, they
573 can't be unset. */
574 ro_bits |= data->emu_status[1] & (1 << 3);
575 ro_bits |= data->emu_status[1] & (1 << 4);
576 ro_bits |= data->emu_status[1] & (1 << 5);
577 } else if (reg == STATUS3) {
578 /* Four reserved bits. */
579 ro_bits = 0x1b;
580 }
581 }
582
583 return ro_bits;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200584}
585
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200586static void update_write_protection(struct emu_data *data)
587{
588 if (data->emu_chip != EMULATE_WINBOND_W25Q128FV)
589 return;
590
591 const struct wp_bits bits = {
592 .srp = data->emu_status[0] >> 7,
593 .srl = data->emu_status[1] & 1,
594
595 .bp_bit_count = 3,
596 .bp =
597 {
598 (data->emu_status[0] >> 2) & 1,
599 (data->emu_status[0] >> 3) & 1,
600 (data->emu_status[0] >> 4) & 1
601 },
602
603 .tb_bit_present = true,
604 .tb = (data->emu_status[0] >> 5) & 1,
605
606 .sec_bit_present = true,
607 .sec = (data->emu_status[0] >> 6) & 1,
608
609 .cmp_bit_present = true,
610 .cmp = (data->emu_status[1] >> 6) & 1,
611 };
612
613 size_t start;
614 size_t len;
615 decode_range_spi25(&start, &len, &bits, data->emu_chip_size);
616
617 data->wp_start = start;
618 data->wp_end = start + len;
619}
620
621/* Checks whether range intersects a write-protected area of the flash if one is
622 * defined. */
623static bool is_write_protected(const struct emu_data *data, uint32_t start, uint32_t len)
624{
625 if (len == 0)
626 return false;
627
628 const uint32_t last = start + len - 1;
629 return (start < data->wp_end && last >= data->wp_start);
630}
631
632/* Returns non-zero on error. */
633static int write_flash_data(struct emu_data *data, uint32_t start, uint32_t len, const uint8_t *buf)
634{
635 if (is_write_protected(data, start, len)) {
636 msg_perr("At least part of the write range is write protected!\n");
637 return 1;
638 }
639
640 memcpy(data->flashchip_contents + start, buf, len);
641 data->emu_modified = 1;
642 return 0;
643}
644
645/* Returns non-zero on error. */
646static int erase_flash_data(struct emu_data *data, uint32_t start, uint32_t len)
647{
648 if (is_write_protected(data, start, len)) {
649 msg_perr("At least part of the erase range is write protected!\n");
650 return 1;
651 }
652
653 memset(data->flashchip_contents + start, 0xff, len);
654 data->emu_modified = 1;
655 return 0;
656}
657
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000658static int emulate_spi_chip_response(unsigned int writecnt,
659 unsigned int readcnt,
660 const unsigned char *writearr,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000661 unsigned char *readarr,
662 struct emu_data *data)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000663{
Stefan Tauner0b9df972012-05-07 22:12:16 +0000664 unsigned int offs, i, toread;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200665 uint8_t ro_bits;
Nico Huberbbccdb22022-05-28 16:48:26 +0200666 bool wrsr_ext2, wrsr_ext3;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000667 static int unsigned aai_offs;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000668 const unsigned char sst25vf040_rems_response[2] = {0xbf, 0x44};
669 const unsigned char sst25vf032b_rems_response[2] = {0xbf, 0x4a};
670 const unsigned char mx25l6436_rems_response[2] = {0xc2, 0x16};
Nico Huberf9632d82019-01-20 11:23:49 +0100671 const unsigned char w25q128fv_rems_response[2] = {0xef, 0x17};
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000672
673 if (writecnt == 0) {
674 msg_perr("No command sent to the chip!\n");
675 return 1;
676 }
Paul Menzelac427b22012-02-16 21:07:07 +0000677 /* spi_blacklist has precedence over spi_ignorelist. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000678 for (i = 0; i < data->spi_blacklist_size; i++) {
679 if (writearr[0] == data->spi_blacklist[i]) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000680 msg_pdbg("Refusing blacklisted SPI command 0x%02x\n",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000681 data->spi_blacklist[i]);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000682 return SPI_INVALID_OPCODE;
683 }
684 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000685 for (i = 0; i < data->spi_ignorelist_size; i++) {
686 if (writearr[0] == data->spi_ignorelist[i]) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000687 msg_cdbg("Ignoring ignorelisted SPI command 0x%02x\n",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000688 data->spi_ignorelist[i]);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000689 /* Return success because the command does not fail,
690 * it is simply ignored.
691 */
692 return 0;
693 }
694 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000695
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200696 if (data->emu_max_aai_size && (data->emu_status[0] & SPI_SR_AAI)) {
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000697 if (writearr[0] != JEDEC_AAI_WORD_PROGRAM &&
698 writearr[0] != JEDEC_WRDI &&
699 writearr[0] != JEDEC_RDSR) {
700 msg_perr("Forbidden opcode (0x%02x) attempted during "
701 "AAI sequence!\n", writearr[0]);
702 return 0;
703 }
704 }
705
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000706 switch (writearr[0]) {
707 case JEDEC_RES:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000708 if (writecnt < JEDEC_RES_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000709 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000710 /* offs calculation is only needed for SST chips which treat RES like REMS. */
711 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
712 offs += writecnt - JEDEC_REMS_OUTSIZE;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000713 switch (data->emu_chip) {
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000714 case EMULATE_ST_M25P10_RES:
715 if (readcnt > 0)
716 memset(readarr, 0x10, readcnt);
717 break;
718 case EMULATE_SST_SST25VF040_REMS:
719 for (i = 0; i < readcnt; i++)
720 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
721 break;
722 case EMULATE_SST_SST25VF032B:
723 for (i = 0; i < readcnt; i++)
724 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
725 break;
726 case EMULATE_MACRONIX_MX25L6436:
727 if (readcnt > 0)
728 memset(readarr, 0x16, readcnt);
729 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100730 case EMULATE_WINBOND_W25Q128FV:
731 if (readcnt > 0)
732 memset(readarr, 0x17, readcnt);
733 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000734 default: /* ignore */
735 break;
736 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000737 break;
738 case JEDEC_REMS:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000739 /* REMS response has wraparound and uses an address parameter. */
740 if (writecnt < JEDEC_REMS_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000741 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000742 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
743 offs += writecnt - JEDEC_REMS_OUTSIZE;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000744 switch (data->emu_chip) {
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000745 case EMULATE_SST_SST25VF040_REMS:
746 for (i = 0; i < readcnt; i++)
747 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
748 break;
749 case EMULATE_SST_SST25VF032B:
750 for (i = 0; i < readcnt; i++)
751 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
752 break;
753 case EMULATE_MACRONIX_MX25L6436:
754 for (i = 0; i < readcnt; i++)
755 readarr[i] = mx25l6436_rems_response[(offs + i) % 2];
756 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100757 case EMULATE_WINBOND_W25Q128FV:
758 for (i = 0; i < readcnt; i++)
759 readarr[i] = w25q128fv_rems_response[(offs + i) % 2];
760 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000761 default: /* ignore */
762 break;
763 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000764 break;
765 case JEDEC_RDID:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000766 switch (data->emu_chip) {
Stefan Tauner0b9df972012-05-07 22:12:16 +0000767 case EMULATE_SST_SST25VF032B:
768 if (readcnt > 0)
769 readarr[0] = 0xbf;
770 if (readcnt > 1)
771 readarr[1] = 0x25;
772 if (readcnt > 2)
773 readarr[2] = 0x4a;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000774 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000775 case EMULATE_MACRONIX_MX25L6436:
776 if (readcnt > 0)
777 readarr[0] = 0xc2;
778 if (readcnt > 1)
779 readarr[1] = 0x20;
780 if (readcnt > 2)
781 readarr[2] = 0x17;
782 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100783 case EMULATE_WINBOND_W25Q128FV:
784 if (readcnt > 0)
785 readarr[0] = 0xef;
786 if (readcnt > 1)
787 readarr[1] = 0x40;
788 if (readcnt > 2)
789 readarr[2] = 0x18;
790 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000791 default: /* ignore */
792 break;
793 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000794 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000795 case JEDEC_RDSR:
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200796 memset(readarr, data->emu_status[0], readcnt);
797 break;
798 case JEDEC_RDSR2:
799 if (data->emu_status_len >= 2)
800 memset(readarr, data->emu_status[1], readcnt);
801 break;
802 case JEDEC_RDSR3:
803 if (data->emu_status_len >= 3)
804 memset(readarr, data->emu_status[2], readcnt);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000805 break;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000806 /* FIXME: this should be chip-specific. */
807 case JEDEC_EWSR:
808 case JEDEC_WREN:
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200809 data->emu_status[0] |= SPI_SR_WEL;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000810 break;
811 case JEDEC_WRSR:
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200812 if (!(data->emu_status[0] & SPI_SR_WEL)) {
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000813 msg_perr("WRSR attempted, but WEL is 0!\n");
814 break;
815 }
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200816
Nico Huberbbccdb22022-05-28 16:48:26 +0200817 wrsr_ext2 = (writecnt == 3 && data->emu_wrsr_ext2);
818 wrsr_ext3 = (writecnt == 4 && data->emu_wrsr_ext3);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200819
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000820 /* FIXME: add some reasonable simulation of the busy flag */
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200821
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200822 ro_bits = get_reg_ro_bit_mask(data, STATUS1);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200823 data->emu_status[0] &= ro_bits;
824 data->emu_status[0] |= writearr[1] & ~ro_bits;
Nico Huberbbccdb22022-05-28 16:48:26 +0200825 if (wrsr_ext2 || wrsr_ext3) {
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200826 ro_bits = get_reg_ro_bit_mask(data, STATUS2);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200827 data->emu_status[1] &= ro_bits;
828 data->emu_status[1] |= writearr[2] & ~ro_bits;
829 }
Nico Huberbbccdb22022-05-28 16:48:26 +0200830 if (wrsr_ext3) {
831 ro_bits = get_reg_ro_bit_mask(data, STATUS3);
832 data->emu_status[2] &= ro_bits;
833 data->emu_status[2] |= writearr[3] & ~ro_bits;
834 }
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200835
Nico Huberbbccdb22022-05-28 16:48:26 +0200836 if (wrsr_ext3)
837 msg_pdbg2("WRSR wrote 0x%02x%02x%02x.\n", data->emu_status[2], data->emu_status[1], data->emu_status[0]);
838 else if (wrsr_ext2)
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200839 msg_pdbg2("WRSR wrote 0x%02x%02x.\n", data->emu_status[1], data->emu_status[0]);
840 else
841 msg_pdbg2("WRSR wrote 0x%02x.\n", data->emu_status[0]);
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200842
843 update_write_protection(data);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200844 break;
845 case JEDEC_WRSR2:
846 if (data->emu_status_len < 2)
847 break;
848 if (!(data->emu_status[0] & SPI_SR_WEL)) {
849 msg_perr("WRSR2 attempted, but WEL is 0!\n");
850 break;
851 }
852
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200853 ro_bits = get_reg_ro_bit_mask(data, STATUS2);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200854 data->emu_status[1] &= ro_bits;
855 data->emu_status[1] |= (writearr[1] & ~ro_bits);
856
857 msg_pdbg2("WRSR2 wrote 0x%02x.\n", data->emu_status[1]);
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200858
859 update_write_protection(data);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200860 break;
861 case JEDEC_WRSR3:
862 if (data->emu_status_len < 3)
863 break;
864 if (!(data->emu_status[0] & SPI_SR_WEL)) {
865 msg_perr("WRSR3 attempted, but WEL is 0!\n");
866 break;
867 }
868
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200869 ro_bits = get_reg_ro_bit_mask(data, STATUS3);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200870 data->emu_status[2] &= ro_bits;
871 data->emu_status[2] |= (writearr[1] & ~ro_bits);
872
873 msg_pdbg2("WRSR3 wrote 0x%02x.\n", data->emu_status[2]);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000874 break;
875 case JEDEC_READ:
876 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
877 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000878 offs %= data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000879 if (readcnt > 0)
Edward O'Callaghan94250222021-05-20 20:34:02 +1000880 memcpy(readarr, data->flashchip_contents + offs, readcnt);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000881 break;
882 case JEDEC_BYTE_PROGRAM:
883 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
884 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000885 offs %= data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000886 if (writecnt < 5) {
887 msg_perr("BYTE PROGRAM size too short!\n");
888 return 1;
889 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000890 if (writecnt - 4 > data->emu_max_byteprogram_size) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000891 msg_perr("Max BYTE PROGRAM size exceeded!\n");
892 return 1;
893 }
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200894 if (write_flash_data(data, offs, writecnt - 4, writearr + 4)) {
895 msg_perr("Failed to program flash!\n");
896 return 1;
897 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000898 break;
899 case JEDEC_AAI_WORD_PROGRAM:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000900 if (!data->emu_max_aai_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000901 break;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200902 if (!(data->emu_status[0] & SPI_SR_AAI)) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000903 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
904 msg_perr("Initial AAI WORD PROGRAM size too "
905 "short!\n");
906 return 1;
907 }
908 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
909 msg_perr("Initial AAI WORD PROGRAM size too "
910 "long!\n");
911 return 1;
912 }
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200913 data->emu_status[0] |= SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000914 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
915 writearr[3];
916 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000917 aai_offs %= data->emu_chip_size;
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200918 if (write_flash_data(data, aai_offs, 2, writearr + 4)) {
919 msg_perr("Failed to program flash!\n");
920 return 1;
921 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000922 aai_offs += 2;
923 } else {
924 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
925 msg_perr("Continuation AAI WORD PROGRAM size "
926 "too short!\n");
927 return 1;
928 }
929 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
930 msg_perr("Continuation AAI WORD PROGRAM size "
931 "too long!\n");
932 return 1;
933 }
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200934 if (write_flash_data(data, aai_offs, 2, writearr + 1)) {
935 msg_perr("Failed to program flash!\n");
936 return 1;
937 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000938 aai_offs += 2;
939 }
940 break;
941 case JEDEC_WRDI:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000942 if (data->emu_max_aai_size)
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200943 data->emu_status[0] &= ~SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000944 break;
945 case JEDEC_SE:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000946 if (!data->emu_jedec_se_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000947 break;
948 if (writecnt != JEDEC_SE_OUTSIZE) {
949 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
950 return 1;
951 }
952 if (readcnt != JEDEC_SE_INSIZE) {
953 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
954 return 1;
955 }
956 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +1000957 if (offs & (data->emu_jedec_se_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000958 msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000959 offs &= ~(data->emu_jedec_se_size - 1);
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200960 if (erase_flash_data(data, offs, data->emu_jedec_se_size)) {
961 msg_perr("Failed to erase flash!\n");
962 return 1;
963 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000964 break;
965 case JEDEC_BE_52:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000966 if (!data->emu_jedec_be_52_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000967 break;
968 if (writecnt != JEDEC_BE_52_OUTSIZE) {
969 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
970 return 1;
971 }
972 if (readcnt != JEDEC_BE_52_INSIZE) {
973 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
974 return 1;
975 }
976 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +1000977 if (offs & (data->emu_jedec_be_52_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000978 msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000979 offs &= ~(data->emu_jedec_be_52_size - 1);
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200980 if (erase_flash_data(data, offs, data->emu_jedec_be_52_size)) {
981 msg_perr("Failed to erase flash!\n");
982 return 1;
983 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000984 break;
985 case JEDEC_BE_D8:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000986 if (!data->emu_jedec_be_d8_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000987 break;
988 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
989 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
990 return 1;
991 }
992 if (readcnt != JEDEC_BE_D8_INSIZE) {
993 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
994 return 1;
995 }
996 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +1000997 if (offs & (data->emu_jedec_be_d8_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000998 msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000999 offs &= ~(data->emu_jedec_be_d8_size - 1);
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +02001000 if (erase_flash_data(data, offs, data->emu_jedec_be_d8_size)) {
1001 msg_perr("Failed to erase flash!\n");
1002 return 1;
1003 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001004 break;
1005 case JEDEC_CE_60:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001006 if (!data->emu_jedec_ce_60_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001007 break;
1008 if (writecnt != JEDEC_CE_60_OUTSIZE) {
1009 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
1010 return 1;
1011 }
1012 if (readcnt != JEDEC_CE_60_INSIZE) {
1013 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
1014 return 1;
1015 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +00001016 /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001017 /* emu_jedec_ce_60_size is emu_chip_size. */
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +02001018 if (erase_flash_data(data, 0, data->emu_jedec_ce_60_size)) {
1019 msg_perr("Failed to erase flash!\n");
1020 return 1;
1021 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001022 break;
1023 case JEDEC_CE_C7:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001024 if (!data->emu_jedec_ce_c7_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001025 break;
1026 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
1027 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
1028 return 1;
1029 }
1030 if (readcnt != JEDEC_CE_C7_INSIZE) {
1031 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
1032 return 1;
1033 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +00001034 /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001035 /* emu_jedec_ce_c7_size is emu_chip_size. */
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +02001036 if (erase_flash_data(data, 0, data->emu_jedec_ce_c7_size)) {
1037 msg_perr("Failed to erase flash!\n");
1038 return 1;
1039 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001040 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +00001041 case JEDEC_SFDP:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001042 if (data->emu_chip != EMULATE_MACRONIX_MX25L6436)
Stefan Tauner0b9df972012-05-07 22:12:16 +00001043 break;
1044 if (writecnt < 4)
1045 break;
1046 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
1047
1048 /* SFDP expects one dummy byte after the address. */
1049 if (writecnt == 4) {
1050 /* The dummy byte was not written, make sure it is read instead.
1051 * Shifting and shortening the read array does achieve this goal.
1052 */
1053 readarr++;
1054 readcnt--;
1055 } else {
1056 /* The response is shifted if more than 5 bytes are written, because SFDP data is
1057 * already shifted out by the chip while those superfluous bytes are written. */
1058 offs += writecnt - 5;
1059 }
1060
1061 /* The SFDP spec implies that the start address of an SFDP read may be truncated to fit in the
1062 * SFDP table address space, i.e. the start address may be wrapped around at SFDP table size.
1063 * This is a reasonable implementation choice in hardware because it saves a few gates. */
1064 if (offs >= sizeof(sfdp_table)) {
1065 msg_pdbg("Wrapping the start address around the SFDP table boundary (using 0x%x "
1066 "instead of 0x%x).\n", (unsigned int)(offs % sizeof(sfdp_table)), offs);
1067 offs %= sizeof(sfdp_table);
1068 }
1069 toread = min(sizeof(sfdp_table) - offs, readcnt);
1070 memcpy(readarr, sfdp_table + offs, toread);
1071 if (toread < readcnt)
1072 msg_pdbg("Crossing the SFDP table boundary in a single "
1073 "continuous chunk produces undefined results "
1074 "after that point.\n");
1075 break;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001076 default:
1077 /* No special response. */
1078 break;
1079 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +00001080 if (writearr[0] != JEDEC_WREN && writearr[0] != JEDEC_EWSR)
Sergii Dmytruk59151a42021-11-08 00:05:12 +02001081 data->emu_status[0] &= ~SPI_SR_WEL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001082 return 0;
1083}
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001084
Edward O'Callaghan5eca4272020-04-12 17:27:53 +10001085static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001086 unsigned int readcnt,
1087 const unsigned char *writearr,
1088 unsigned char *readarr)
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001089{
Nico Huber519be662018-12-23 20:03:35 +01001090 unsigned int i;
Lachlan Bishopc753c402020-09-10 14:57:05 +10001091 struct emu_data *emu_data = flash->mst->spi.data;
1092 if (!emu_data) {
1093 msg_perr("No data in flash context!\n");
1094 return 1;
1095 }
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001096
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +00001097 msg_pspew("%s:", __func__);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001098
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +00001099 msg_pspew(" writing %u bytes:", writecnt);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001100 for (i = 0; i < writecnt; i++)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +00001101 msg_pspew(" 0x%02x", writearr[i]);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001102
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001103 /* Response for unknown commands and missing chip is 0xff. */
1104 memset(readarr, 0xff, readcnt);
Lachlan Bishopc753c402020-09-10 14:57:05 +10001105 switch (emu_data->emu_chip) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001106 case EMULATE_ST_M25P10_RES:
1107 case EMULATE_SST_SST25VF040_REMS:
1108 case EMULATE_SST_SST25VF032B:
Stefan Tauner0b9df972012-05-07 22:12:16 +00001109 case EMULATE_MACRONIX_MX25L6436:
Nico Huberf9632d82019-01-20 11:23:49 +01001110 case EMULATE_WINBOND_W25Q128FV:
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001111 if (emulate_spi_chip_response(writecnt, readcnt, writearr,
Lachlan Bishopc753c402020-09-10 14:57:05 +10001112 readarr, emu_data)) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +00001113 msg_pdbg("Invalid command sent to flash chip!\n");
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001114 return 1;
1115 }
1116 break;
1117 default:
1118 break;
1119 }
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +00001120 msg_pspew(" reading %u bytes:", readcnt);
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001121 for (i = 0; i < readcnt; i++)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001122 msg_pspew(" 0x%02x", readarr[i]);
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +00001123 msg_pspew("\n");
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001124 return 0;
1125}
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +00001126
Mark Marshallf20b7be2014-05-09 21:16:21 +00001127static 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 +00001128{
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001129 return spi_write_chunked(flash, buf, start, len,
1130 spi_write_256_chunksize);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001131}
Thomas Heijligencc853d82021-05-04 15:32:17 +02001132
1133const struct programmer_entry programmer_dummy = {
1134 .name = "dummy",
1135 .type = OTHER,
1136 /* FIXME */
1137 .devs.note = "Dummy device, does nothing and logs all accesses\n",
1138 .init = dummy_init,
1139 .map_flash_region = dummy_map,
1140 .unmap_flash_region = dummy_unmap,
1141 .delay = internal_delay,
1142};