blob: c3526016032d8c5883c68a216a7b6213d589bcd5 [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,
Nico Huber4203a472022-05-28 17:28:05 +020038 EMULATE_SPANSION_S25FL128L,
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000039};
Stefan Tauner0b9df972012-05-07 22:12:16 +000040
Lachlan Bishopc753c402020-09-10 14:57:05 +100041struct emu_data {
42 enum emu_chip emu_chip;
43 char *emu_persistent_image;
44 unsigned int emu_chip_size;
Sergii Dmytruk59151a42021-11-08 00:05:12 +020045 /* Note: W25Q128FV doesn't change value of SR2 if it's not provided, but
46 * even its previous generations do, so don't forget to update
Nico Huberbbccdb22022-05-28 16:48:26 +020047 * WRSR code on enabling WRSR_EXT2 for more chips. */
48 bool emu_wrsr_ext2;
49 bool emu_wrsr_ext3;
Lachlan Bishopc753c402020-09-10 14:57:05 +100050 int emu_modified; /* is the image modified since reading it? */
Sergii Dmytruk59151a42021-11-08 00:05:12 +020051 uint8_t emu_status[3];
52 uint8_t emu_status_len; /* number of emulated status registers */
Lachlan Bishopc753c402020-09-10 14:57:05 +100053 unsigned int emu_max_byteprogram_size;
54 unsigned int emu_max_aai_size;
55 unsigned int emu_jedec_se_size;
56 unsigned int emu_jedec_be_52_size;
57 unsigned int emu_jedec_be_d8_size;
58 unsigned int emu_jedec_ce_60_size;
59 unsigned int emu_jedec_ce_c7_size;
60 unsigned char spi_blacklist[256];
61 unsigned char spi_ignorelist[256];
62 unsigned int spi_blacklist_size;
63 unsigned int spi_ignorelist_size;
Edward O'Callaghan94250222021-05-20 20:34:02 +100064
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +020065 bool hwwp; /* state of hardware write protection */
66 /* wp_start == wp_end when write-protection is disabled */
67 uint32_t wp_start;
68 uint32_t wp_end;
69
Edward O'Callaghan94250222021-05-20 20:34:02 +100070 uint8_t *flashchip_contents;
Lachlan Bishopc753c402020-09-10 14:57:05 +100071};
72
Stefan Tauner0b9df972012-05-07 22:12:16 +000073/* A legit complete SFDP table based on the MX25L6436E (rev. 1.8) datasheet. */
Stefan Tauner67d163d2013-01-15 17:37:48 +000074static const uint8_t sfdp_table[] = {
Stefan Tauner0b9df972012-05-07 22:12:16 +000075 0x53, 0x46, 0x44, 0x50, // @0x00: SFDP signature
76 0x00, 0x01, 0x01, 0xFF, // @0x04: revision 1.0, 2 headers
77 0x00, 0x00, 0x01, 0x09, // @0x08: JEDEC SFDP header rev. 1.0, 9 DW long
78 0x1C, 0x00, 0x00, 0xFF, // @0x0C: PTP0 = 0x1C (instead of 0x30)
79 0xC2, 0x00, 0x01, 0x04, // @0x10: Macronix header rev. 1.0, 4 DW long
80 0x48, 0x00, 0x00, 0xFF, // @0x14: PTP1 = 0x48 (instead of 0x60)
81 0xFF, 0xFF, 0xFF, 0xFF, // @0x18: hole.
82 0xE5, 0x20, 0xC9, 0xFF, // @0x1C: SFDP parameter table start
83 0xFF, 0xFF, 0xFF, 0x03, // @0x20
84 0x00, 0xFF, 0x08, 0x6B, // @0x24
85 0x08, 0x3B, 0x00, 0xFF, // @0x28
86 0xEE, 0xFF, 0xFF, 0xFF, // @0x2C
87 0xFF, 0xFF, 0x00, 0x00, // @0x30
88 0xFF, 0xFF, 0x00, 0xFF, // @0x34
89 0x0C, 0x20, 0x0F, 0x52, // @0x38
90 0x10, 0xD8, 0x00, 0xFF, // @0x3C: SFDP parameter table end
91 0xFF, 0xFF, 0xFF, 0xFF, // @0x40: hole.
92 0xFF, 0xFF, 0xFF, 0xFF, // @0x44: hole.
93 0x00, 0x36, 0x00, 0x27, // @0x48: Macronix parameter table start
94 0xF4, 0x4F, 0xFF, 0xFF, // @0x4C
95 0xD9, 0xC8, 0xFF, 0xFF, // @0x50
96 0xFF, 0xFF, 0xFF, 0xFF, // @0x54: Macronix parameter table end
97};
98
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000099
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000100static unsigned int spi_write_256_chunksize = 256;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000101
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000102static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
Mark Marshallf20b7be2014-05-09 21:16:21 +0000103 const unsigned char *writearr, unsigned char *readarr);
104static int dummy_spi_write_256(struct flashctx *flash, const uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000105 unsigned int start, unsigned int len);
Mark Marshallf20b7be2014-05-09 21:16:21 +0000106static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr);
107static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr);
108static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr);
109static void dummy_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len);
110static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr);
111static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr);
112static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr);
113static void dummy_chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len);
Michael Karcherb9dbe482011-05-11 17:07:07 +0000114
Lachlan Bishopc753c402020-09-10 14:57:05 +1000115static struct spi_master spi_master_dummyflasher = {
Nico Huber1cf407b2017-11-10 20:18:23 +0100116 .features = SPI_MASTER_4BA,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000117 .max_data_read = MAX_DATA_READ_UNLIMITED,
118 .max_data_write = MAX_DATA_UNSPECIFIED,
119 .command = dummy_spi_send_command,
120 .multicommand = default_spi_send_multicommand,
121 .read = default_spi_read,
122 .write_256 = dummy_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +0000123 .write_aai = default_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000124};
David Hendricks8bb20212011-06-14 01:35:36 +0000125
Lachlan Bishopc753c402020-09-10 14:57:05 +1000126static struct par_master par_master_dummy = {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000127 .chip_readb = dummy_chip_readb,
128 .chip_readw = dummy_chip_readw,
129 .chip_readl = dummy_chip_readl,
130 .chip_readn = dummy_chip_readn,
131 .chip_writeb = dummy_chip_writeb,
132 .chip_writew = dummy_chip_writew,
133 .chip_writel = dummy_chip_writel,
134 .chip_writen = dummy_chip_writen,
135};
136
David Hendricks8bb20212011-06-14 01:35:36 +0000137static int dummy_shutdown(void *data)
138{
139 msg_pspew("%s\n", __func__);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000140 struct emu_data *emu_data = (struct emu_data *)data;
141 if (emu_data->emu_chip != EMULATE_NONE) {
142 if (emu_data->emu_persistent_image && emu_data->emu_modified) {
143 msg_pdbg("Writing %s\n", emu_data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000144 write_buf_to_file(emu_data->flashchip_contents,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000145 emu_data->emu_chip_size,
146 emu_data->emu_persistent_image);
David Hendricks8bb20212011-06-14 01:35:36 +0000147 }
Angel Pons02b9ae22021-05-25 12:46:43 +0200148 free(emu_data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000149 free(emu_data->flashchip_contents);
David Hendricks8bb20212011-06-14 01:35:36 +0000150 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000151 free(data);
David Hendricks8bb20212011-06-14 01:35:36 +0000152 return 0;
153}
154
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000155static int init_data(struct emu_data *data, enum chipbustype *dummy_buses_supported)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000156{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000157 char *bustext = NULL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000158 char *tmp = NULL;
Nico Huber519be662018-12-23 20:03:35 +0100159 unsigned int i;
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000160 char *endptr;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000161 char *status = NULL;
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000162
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000163 bustext = extract_programmer_param("bus");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000164 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
165 if (!bustext)
166 bustext = strdup("parallel+lpc+fwh+spi");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000167 /* Convert the parameters to lowercase. */
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000168 tolower_string(bustext);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000169
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000170 *dummy_buses_supported = BUS_NONE;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000171 if (strstr(bustext, "parallel")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000172 *dummy_buses_supported |= BUS_PARALLEL;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000173 msg_pdbg("Enabling support for %s flash.\n", "parallel");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000174 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000175 if (strstr(bustext, "lpc")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000176 *dummy_buses_supported |= BUS_LPC;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000177 msg_pdbg("Enabling support for %s flash.\n", "LPC");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000178 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000179 if (strstr(bustext, "fwh")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000180 *dummy_buses_supported |= BUS_FWH;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000181 msg_pdbg("Enabling support for %s flash.\n", "FWH");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000182 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000183 if (strstr(bustext, "spi")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000184 *dummy_buses_supported |= BUS_SPI;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000185 msg_pdbg("Enabling support for %s flash.\n", "SPI");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000186 }
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000187 if (*dummy_buses_supported == BUS_NONE)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000188 msg_pdbg("Support for all flash bus types disabled.\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000189 free(bustext);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000190
191 tmp = extract_programmer_param("spi_write_256_chunksize");
192 if (tmp) {
Edward O'Callaghanc785f882021-05-23 22:14:36 +1000193 spi_write_256_chunksize = strtoul(tmp, &endptr, 0);
194 if (*endptr != '\0' || spi_write_256_chunksize < 1) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000195 msg_perr("invalid spi_write_256_chunksize\n");
Edward O'Callaghanc785f882021-05-23 22:14:36 +1000196 free(tmp);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000197 return 1;
198 }
Edward O'Callaghanc785f882021-05-23 22:14:36 +1000199 free(tmp);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000200 }
201
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000202 tmp = extract_programmer_param("spi_blacklist");
203 if (tmp) {
204 i = strlen(tmp);
205 if (!strncmp(tmp, "0x", 2)) {
206 i -= 2;
207 memmove(tmp, tmp + 2, i + 1);
208 }
209 if ((i > 512) || (i % 2)) {
210 msg_perr("Invalid SPI command blacklist length\n");
211 free(tmp);
212 return 1;
213 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000214 data->spi_blacklist_size = i / 2;
215 for (i = 0; i < data->spi_blacklist_size * 2; i++) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000216 if (!isxdigit((unsigned char)tmp[i])) {
217 msg_perr("Invalid char \"%c\" in SPI command "
218 "blacklist\n", tmp[i]);
219 free(tmp);
220 return 1;
221 }
222 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000223 for (i = 0; i < data->spi_blacklist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000224 unsigned int tmp2;
225 /* SCNx8 is apparently not supported by MSVC (and thus
226 * MinGW), so work around it with an extra variable
227 */
228 sscanf(tmp + i * 2, "%2x", &tmp2);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000229 data->spi_blacklist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000230 }
231 msg_pdbg("SPI blacklist is ");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000232 for (i = 0; i < data->spi_blacklist_size; i++)
233 msg_pdbg("%02x ", data->spi_blacklist[i]);
234 msg_pdbg(", size %u\n", data->spi_blacklist_size);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000235 }
236 free(tmp);
237
238 tmp = extract_programmer_param("spi_ignorelist");
239 if (tmp) {
240 i = strlen(tmp);
241 if (!strncmp(tmp, "0x", 2)) {
242 i -= 2;
243 memmove(tmp, tmp + 2, i + 1);
244 }
245 if ((i > 512) || (i % 2)) {
246 msg_perr("Invalid SPI command ignorelist length\n");
247 free(tmp);
248 return 1;
249 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000250 data->spi_ignorelist_size = i / 2;
251 for (i = 0; i < data->spi_ignorelist_size * 2; i++) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000252 if (!isxdigit((unsigned char)tmp[i])) {
253 msg_perr("Invalid char \"%c\" in SPI command "
254 "ignorelist\n", tmp[i]);
255 free(tmp);
256 return 1;
257 }
258 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000259 for (i = 0; i < data->spi_ignorelist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000260 unsigned int tmp2;
261 /* SCNx8 is apparently not supported by MSVC (and thus
262 * MinGW), so work around it with an extra variable
263 */
264 sscanf(tmp + i * 2, "%2x", &tmp2);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000265 data->spi_ignorelist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000266 }
267 msg_pdbg("SPI ignorelist is ");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000268 for (i = 0; i < data->spi_ignorelist_size; i++)
269 msg_pdbg("%02x ", data->spi_ignorelist[i]);
270 msg_pdbg(", size %u\n", data->spi_ignorelist_size);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000271 }
272 free(tmp);
273
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200274 tmp = extract_programmer_param("hwwp");
275 if (tmp) {
276 if (!strcmp(tmp, "yes")) {
277 msg_pdbg("Emulated chip will have hardware WP enabled\n");
278 data->hwwp = true;
279 } else if (!strcmp(tmp, "no")) {
280 msg_pdbg("Emulated chip will have hardware WP disabled\n");
281 } else {
282 msg_perr("hwwp can be \"yes\" or \"no\"\n");
283 free(tmp);
284 return 1;
285 }
286 free(tmp);
287 }
288
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000289 tmp = extract_programmer_param("emulate");
290 if (!tmp) {
291 msg_pdbg("Not emulating any flash chip.\n");
292 /* Nothing else to do. */
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000293 return 0;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000294 }
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000295
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000296 if (!strcmp(tmp, "M25P10.RES")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000297 data->emu_chip = EMULATE_ST_M25P10_RES;
298 data->emu_chip_size = 128 * 1024;
299 data->emu_max_byteprogram_size = 128;
300 data->emu_max_aai_size = 0;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200301 data->emu_status_len = 1;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000302 data->emu_jedec_se_size = 0;
303 data->emu_jedec_be_52_size = 0;
304 data->emu_jedec_be_d8_size = 32 * 1024;
305 data->emu_jedec_ce_60_size = 0;
306 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000307 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
308 "write)\n");
309 }
310 if (!strcmp(tmp, "SST25VF040.REMS")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000311 data->emu_chip = EMULATE_SST_SST25VF040_REMS;
312 data->emu_chip_size = 512 * 1024;
313 data->emu_max_byteprogram_size = 1;
314 data->emu_max_aai_size = 0;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200315 data->emu_status_len = 1;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000316 data->emu_jedec_se_size = 4 * 1024;
317 data->emu_jedec_be_52_size = 32 * 1024;
318 data->emu_jedec_be_d8_size = 0;
319 data->emu_jedec_ce_60_size = data->emu_chip_size;
320 data->emu_jedec_ce_c7_size = 0;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000321 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
322 "byte write)\n");
323 }
324 if (!strcmp(tmp, "SST25VF032B")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000325 data->emu_chip = EMULATE_SST_SST25VF032B;
326 data->emu_chip_size = 4 * 1024 * 1024;
327 data->emu_max_byteprogram_size = 1;
328 data->emu_max_aai_size = 2;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200329 data->emu_status_len = 1;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000330 data->emu_jedec_se_size = 4 * 1024;
331 data->emu_jedec_be_52_size = 32 * 1024;
332 data->emu_jedec_be_d8_size = 64 * 1024;
333 data->emu_jedec_ce_60_size = data->emu_chip_size;
334 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000335 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
336 "write)\n");
337 }
Stefan Tauner0b9df972012-05-07 22:12:16 +0000338 if (!strcmp(tmp, "MX25L6436")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000339 data->emu_chip = EMULATE_MACRONIX_MX25L6436;
340 data->emu_chip_size = 8 * 1024 * 1024;
341 data->emu_max_byteprogram_size = 256;
342 data->emu_max_aai_size = 0;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200343 data->emu_status_len = 1;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000344 data->emu_jedec_se_size = 4 * 1024;
345 data->emu_jedec_be_52_size = 32 * 1024;
346 data->emu_jedec_be_d8_size = 64 * 1024;
347 data->emu_jedec_ce_60_size = data->emu_chip_size;
348 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000349 msg_pdbg("Emulating Macronix MX25L6436 SPI flash chip (RDID, "
350 "SFDP)\n");
351 }
Nico Huberf9632d82019-01-20 11:23:49 +0100352 if (!strcmp(tmp, "W25Q128FV")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000353 data->emu_chip = EMULATE_WINBOND_W25Q128FV;
Nico Huberbbccdb22022-05-28 16:48:26 +0200354 data->emu_wrsr_ext2 = true;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000355 data->emu_chip_size = 16 * 1024 * 1024;
356 data->emu_max_byteprogram_size = 256;
357 data->emu_max_aai_size = 0;
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200358 data->emu_status_len = 3;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000359 data->emu_jedec_se_size = 4 * 1024;
360 data->emu_jedec_be_52_size = 32 * 1024;
361 data->emu_jedec_be_d8_size = 64 * 1024;
362 data->emu_jedec_ce_60_size = data->emu_chip_size;
363 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Nico Huberf9632d82019-01-20 11:23:49 +0100364 msg_pdbg("Emulating Winbond W25Q128FV SPI flash chip (RDID)\n");
365 }
Nico Huber4203a472022-05-28 17:28:05 +0200366 if (!strcmp(tmp, "S25FL128L")) {
367 data->emu_chip = EMULATE_SPANSION_S25FL128L;
368 data->emu_wrsr_ext2 = true;
369 data->emu_wrsr_ext3 = true;
370 data->emu_chip_size = 16 * 1024 * 1024;
371 data->emu_max_byteprogram_size = 256;
372 data->emu_max_aai_size = 0;
373 data->emu_status_len = 3;
374 data->emu_jedec_se_size = 4 * 1024;
375 data->emu_jedec_be_52_size = 32 * 1024;
376 data->emu_jedec_be_d8_size = 64 * 1024;
377 data->emu_jedec_ce_60_size = data->emu_chip_size;
378 data->emu_jedec_ce_c7_size = data->emu_chip_size;
379 msg_pdbg("Emulating Spansion S25FL128L SPI flash chip (RES, RDID, WP)\n");
380 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000381 if (data->emu_chip == EMULATE_NONE) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000382 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
383 free(tmp);
384 return 1;
385 }
386 free(tmp);
David Hendricks8bb20212011-06-14 01:35:36 +0000387
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000388 status = extract_programmer_param("spi_status");
389 if (status) {
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200390 unsigned int emu_status;
391
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000392 errno = 0;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200393 emu_status = strtoul(status, &endptr, 0);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000394 if (errno != 0 || status == endptr) {
Angel Ponsc2484642021-05-25 13:03:24 +0200395 free(status);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000396 msg_perr("Error: initial status register specified, "
397 "but the value could not be converted.\n");
398 return 1;
399 }
Angel Ponsc2484642021-05-25 13:03:24 +0200400 free(status);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200401
402 data->emu_status[0] = emu_status;
403 data->emu_status[1] = emu_status >> 8;
404 data->emu_status[2] = emu_status >> 16;
405
406 if (data->emu_status_len == 3) {
407 msg_pdbg("Initial status registers:\n"
408 "\tSR1 is set to 0x%02x\n"
409 "\tSR2 is set to 0x%02x\n"
410 "\tSR3 is set to 0x%02x\n",
411 data->emu_status[0], data->emu_status[1], data->emu_status[2]);
412 } else if (data->emu_status_len == 2) {
413 msg_pdbg("Initial status registers:\n"
414 "\tSR1 is set to 0x%02x\n"
415 "\tSR2 is set to 0x%02x\n",
416 data->emu_status[0], data->emu_status[1]);
417 } else {
418 msg_pdbg("Initial status register is set to 0x%02x.\n",
419 data->emu_status[0]);
420 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000421 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000422
Angel Pons328898a2021-05-25 12:56:18 +0200423 data->flashchip_contents = malloc(data->emu_chip_size);
424 if (!data->flashchip_contents) {
425 msg_perr("Out of memory!\n");
426 return 1;
427 }
428
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000429
430 return 0;
431}
432
Thomas Heijligencc853d82021-05-04 15:32:17 +0200433static int dummy_init(void)
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000434{
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000435 struct stat image_stat;
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000436
437 struct emu_data *data = calloc(1, sizeof(struct emu_data));
438 if (!data) {
439 msg_perr("Out of memory!\n");
440 return 1;
441 }
442 data->emu_chip = EMULATE_NONE;
443 spi_master_dummyflasher.data = data;
444 par_master_dummy.data = data;
445
446 msg_pspew("%s\n", __func__);
447
448 enum chipbustype dummy_buses_supported;
449 if (init_data(data, &dummy_buses_supported)) {
450 free(data);
451 return 1;
452 }
453
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000454 if (data->emu_chip == EMULATE_NONE) {
455 msg_pdbg("Not emulating any flash chip.\n");
456 /* Nothing else to do. */
457 goto dummy_init_out;
458 }
459
Lachlan Bishopc753c402020-09-10 14:57:05 +1000460 msg_pdbg("Filling fake flash chip with 0xff, size %i\n", data->emu_chip_size);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000461 memset(data->flashchip_contents, 0xff, data->emu_chip_size);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000462
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000463 /* Will be freed by shutdown function if necessary. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000464 data->emu_persistent_image = extract_programmer_param("image");
465 if (!data->emu_persistent_image) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000466 /* Nothing else to do. */
David Hendricks8bb20212011-06-14 01:35:36 +0000467 goto dummy_init_out;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000468 }
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000469 /* We will silently (in default verbosity) ignore the file if it does not exist (yet) or the size does
470 * not match the emulated chip. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000471 if (!stat(data->emu_persistent_image, &image_stat)) {
Stefan Tauner23e10b82016-01-23 16:16:49 +0000472 msg_pdbg("Found persistent image %s, %jd B ",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000473 data->emu_persistent_image, (intmax_t)image_stat.st_size);
474 if ((uintmax_t)image_stat.st_size == data->emu_chip_size) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000475 msg_pdbg("matches.\n");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000476 msg_pdbg("Reading %s\n", data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000477 if (read_buf_from_file(data->flashchip_contents, data->emu_chip_size,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000478 data->emu_persistent_image)) {
479 msg_perr("Unable to read %s\n", data->emu_persistent_image);
Angel Pons02b9ae22021-05-25 12:46:43 +0200480 free(data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000481 free(data->flashchip_contents);
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000482 free(data);
Jacob Garberca598da2019-08-12 10:44:17 -0600483 return 1;
484 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000485 } else {
486 msg_pdbg("doesn't match.\n");
487 }
488 }
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000489
David Hendricks8bb20212011-06-14 01:35:36 +0000490dummy_init_out:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000491 if (register_shutdown(dummy_shutdown, data)) {
Angel Pons02b9ae22021-05-25 12:46:43 +0200492 free(data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000493 free(data->flashchip_contents);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000494 free(data);
David Hendricks8bb20212011-06-14 01:35:36 +0000495 return 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000496 }
Edward O'Callaghan3fa321d2021-05-17 20:01:27 +1000497 if (dummy_buses_supported & BUS_NONSPI)
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000498 register_par_master(&par_master_dummy,
Edward O'Callaghan3fa321d2021-05-17 20:01:27 +1000499 dummy_buses_supported & BUS_NONSPI);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000500 if (dummy_buses_supported & BUS_SPI)
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000501 register_spi_master(&spi_master_dummyflasher);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000502
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000503 return 0;
504}
505
Thomas Heijligencc853d82021-05-04 15:32:17 +0200506static void *dummy_map(const char *descr, uintptr_t phys_addr, size_t len)
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000507{
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000508 msg_pspew("%s: Mapping %s, 0x%zx bytes at 0x%0*" PRIxPTR "\n",
Stefan Tauner0554ca52013-07-25 22:54:25 +0000509 __func__, descr, len, PRIxPTR_WIDTH, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000510 return (void *)phys_addr;
511}
512
Thomas Heijligencc853d82021-05-04 15:32:17 +0200513static void dummy_unmap(void *virt_addr, size_t len)
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000514{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000515 msg_pspew("%s: Unmapping 0x%zx bytes at %p\n", __func__, len, virt_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000516}
517
Mark Marshallf20b7be2014-05-09 21:16:21 +0000518static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000519{
Stefan Taunerc2333752013-07-13 23:31:37 +0000520 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%02x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000521}
522
Mark Marshallf20b7be2014-05-09 21:16:21 +0000523static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000524{
Stefan Taunerc2333752013-07-13 23:31:37 +0000525 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%04x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000526}
527
Mark Marshallf20b7be2014-05-09 21:16:21 +0000528static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, 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 ", val=0x%08x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000531}
532
Mark Marshallf20b7be2014-05-09 21:16:21 +0000533static 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 +0000534{
535 size_t i;
Stefan Tauner0554ca52013-07-25 22:54:25 +0000536 msg_pspew("%s: addr=0x%" PRIxPTR ", len=0x%zx, writing data (hex):", __func__, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000537 for (i = 0; i < len; i++) {
538 if ((i % 16) == 0)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000539 msg_pspew("\n");
540 msg_pspew("%02x ", buf[i]);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000541 }
542}
543
Mark Marshallf20b7be2014-05-09 21:16:21 +0000544static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000545{
Stefan Taunerc2333752013-07-13 23:31:37 +0000546 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000547 return 0xff;
548}
549
Mark Marshallf20b7be2014-05-09 21:16:21 +0000550static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000551{
Stefan Taunerc2333752013-07-13 23:31:37 +0000552 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000553 return 0xffff;
554}
555
Mark Marshallf20b7be2014-05-09 21:16:21 +0000556static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000557{
Stefan Taunerc2333752013-07-13 23:31:37 +0000558 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffffffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000559 return 0xffffffff;
560}
561
Mark Marshallf20b7be2014-05-09 21:16:21 +0000562static 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 +0000563{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000564 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 +0000565 memset(buf, 0xff, len);
566 return;
567}
568
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200569static uint8_t get_reg_ro_bit_mask(const struct emu_data *data, enum flash_reg reg)
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200570{
571 /* Whoever adds a new register must not forget to update this function
572 or at least shouldn't use it incorrectly. */
573 assert(reg == STATUS1 || reg == STATUS2 || reg == STATUS3);
574
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200575 uint8_t ro_bits = reg == STATUS1 ? SPI_SR_WIP : 0;
576
577 if (data->emu_chip == EMULATE_WINBOND_W25Q128FV) {
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200578 const bool srp0 = (data->emu_status[0] >> 7);
579 const bool srp1 = (data->emu_status[1] & 1);
580
581 const bool wp_active = (srp1 || (srp0 && data->hwwp));
582
583 if (wp_active) {
584 ro_bits = 0xff;
585 } else if (reg == STATUS2) {
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200586 /* SUS (bit_7) and (R) (bit_2). */
587 ro_bits = 0x84;
588 /* Once any of the lock bits (LB[1..3]) are set, they
589 can't be unset. */
590 ro_bits |= data->emu_status[1] & (1 << 3);
591 ro_bits |= data->emu_status[1] & (1 << 4);
592 ro_bits |= data->emu_status[1] & (1 << 5);
593 } else if (reg == STATUS3) {
594 /* Four reserved bits. */
595 ro_bits = 0x1b;
596 }
597 }
598
Nico Huber4203a472022-05-28 17:28:05 +0200599 if (data->emu_chip == EMULATE_SPANSION_S25FL128L) {
600 const bool srp0 = (data->emu_status[0] >> 7);
601 const bool srp1 = (data->emu_status[1] & 1);
602
603 const bool wp_active = (srp1 || (srp0 && data->hwwp));
604
605 if (wp_active) {
606 ro_bits = 0xff;
607 } else if (reg == STATUS2) {
608 /* SUS (bit_7) */
609 ro_bits = 0x80;
610 /* Once any of the lock bits (LB[0..3]) are set, they
611 can't be unset. */
612 ro_bits |= data->emu_status[1] & (1 << 2);
613 ro_bits |= data->emu_status[1] & (1 << 3);
614 ro_bits |= data->emu_status[1] & (1 << 4);
615 ro_bits |= data->emu_status[1] & (1 << 5);
616 } else if (reg == STATUS3) {
617 /* Two reserved bits. */
618 ro_bits = 0x11;
619 }
620 }
621
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200622 return ro_bits;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200623}
624
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200625static void update_write_protection(struct emu_data *data)
626{
Nico Huber4203a472022-05-28 17:28:05 +0200627 if (data->emu_chip != EMULATE_WINBOND_W25Q128FV &&
628 data->emu_chip != EMULATE_SPANSION_S25FL128L)
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200629 return;
630
631 const struct wp_bits bits = {
632 .srp = data->emu_status[0] >> 7,
633 .srl = data->emu_status[1] & 1,
634
635 .bp_bit_count = 3,
636 .bp =
637 {
638 (data->emu_status[0] >> 2) & 1,
639 (data->emu_status[0] >> 3) & 1,
640 (data->emu_status[0] >> 4) & 1
641 },
642
643 .tb_bit_present = true,
644 .tb = (data->emu_status[0] >> 5) & 1,
645
646 .sec_bit_present = true,
647 .sec = (data->emu_status[0] >> 6) & 1,
648
649 .cmp_bit_present = true,
650 .cmp = (data->emu_status[1] >> 6) & 1,
651 };
652
653 size_t start;
654 size_t len;
655 decode_range_spi25(&start, &len, &bits, data->emu_chip_size);
656
657 data->wp_start = start;
658 data->wp_end = start + len;
659}
660
661/* Checks whether range intersects a write-protected area of the flash if one is
662 * defined. */
663static bool is_write_protected(const struct emu_data *data, uint32_t start, uint32_t len)
664{
665 if (len == 0)
666 return false;
667
668 const uint32_t last = start + len - 1;
669 return (start < data->wp_end && last >= data->wp_start);
670}
671
672/* Returns non-zero on error. */
673static int write_flash_data(struct emu_data *data, uint32_t start, uint32_t len, const uint8_t *buf)
674{
675 if (is_write_protected(data, start, len)) {
676 msg_perr("At least part of the write range is write protected!\n");
677 return 1;
678 }
679
680 memcpy(data->flashchip_contents + start, buf, len);
681 data->emu_modified = 1;
682 return 0;
683}
684
685/* Returns non-zero on error. */
686static int erase_flash_data(struct emu_data *data, uint32_t start, uint32_t len)
687{
688 if (is_write_protected(data, start, len)) {
689 msg_perr("At least part of the erase range is write protected!\n");
690 return 1;
691 }
692
693 memset(data->flashchip_contents + start, 0xff, len);
694 data->emu_modified = 1;
695 return 0;
696}
697
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000698static int emulate_spi_chip_response(unsigned int writecnt,
699 unsigned int readcnt,
700 const unsigned char *writearr,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000701 unsigned char *readarr,
702 struct emu_data *data)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000703{
Stefan Tauner0b9df972012-05-07 22:12:16 +0000704 unsigned int offs, i, toread;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200705 uint8_t ro_bits;
Nico Huberbbccdb22022-05-28 16:48:26 +0200706 bool wrsr_ext2, wrsr_ext3;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000707 static int unsigned aai_offs;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000708 const unsigned char sst25vf040_rems_response[2] = {0xbf, 0x44};
709 const unsigned char sst25vf032b_rems_response[2] = {0xbf, 0x4a};
710 const unsigned char mx25l6436_rems_response[2] = {0xc2, 0x16};
Nico Huberf9632d82019-01-20 11:23:49 +0100711 const unsigned char w25q128fv_rems_response[2] = {0xef, 0x17};
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000712
713 if (writecnt == 0) {
714 msg_perr("No command sent to the chip!\n");
715 return 1;
716 }
Paul Menzelac427b22012-02-16 21:07:07 +0000717 /* spi_blacklist has precedence over spi_ignorelist. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000718 for (i = 0; i < data->spi_blacklist_size; i++) {
719 if (writearr[0] == data->spi_blacklist[i]) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000720 msg_pdbg("Refusing blacklisted SPI command 0x%02x\n",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000721 data->spi_blacklist[i]);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000722 return SPI_INVALID_OPCODE;
723 }
724 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000725 for (i = 0; i < data->spi_ignorelist_size; i++) {
726 if (writearr[0] == data->spi_ignorelist[i]) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000727 msg_cdbg("Ignoring ignorelisted SPI command 0x%02x\n",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000728 data->spi_ignorelist[i]);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000729 /* Return success because the command does not fail,
730 * it is simply ignored.
731 */
732 return 0;
733 }
734 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000735
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200736 if (data->emu_max_aai_size && (data->emu_status[0] & SPI_SR_AAI)) {
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000737 if (writearr[0] != JEDEC_AAI_WORD_PROGRAM &&
738 writearr[0] != JEDEC_WRDI &&
739 writearr[0] != JEDEC_RDSR) {
740 msg_perr("Forbidden opcode (0x%02x) attempted during "
741 "AAI sequence!\n", writearr[0]);
742 return 0;
743 }
744 }
745
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000746 switch (writearr[0]) {
747 case JEDEC_RES:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000748 if (writecnt < JEDEC_RES_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000749 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000750 /* offs calculation is only needed for SST chips which treat RES like REMS. */
751 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
752 offs += writecnt - JEDEC_REMS_OUTSIZE;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000753 switch (data->emu_chip) {
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000754 case EMULATE_ST_M25P10_RES:
755 if (readcnt > 0)
756 memset(readarr, 0x10, readcnt);
757 break;
758 case EMULATE_SST_SST25VF040_REMS:
759 for (i = 0; i < readcnt; i++)
760 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
761 break;
762 case EMULATE_SST_SST25VF032B:
763 for (i = 0; i < readcnt; i++)
764 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
765 break;
766 case EMULATE_MACRONIX_MX25L6436:
767 if (readcnt > 0)
768 memset(readarr, 0x16, readcnt);
769 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100770 case EMULATE_WINBOND_W25Q128FV:
771 if (readcnt > 0)
772 memset(readarr, 0x17, readcnt);
773 break;
Nico Huber4203a472022-05-28 17:28:05 +0200774 case EMULATE_SPANSION_S25FL128L:
775 if (readcnt > 0)
776 readarr[0] = 0x60;
777 if (readcnt > 1)
778 readarr[1] = 0x18;
779 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000780 default: /* ignore */
781 break;
782 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000783 break;
784 case JEDEC_REMS:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000785 /* REMS response has wraparound and uses an address parameter. */
786 if (writecnt < JEDEC_REMS_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000787 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000788 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
789 offs += writecnt - JEDEC_REMS_OUTSIZE;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000790 switch (data->emu_chip) {
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000791 case EMULATE_SST_SST25VF040_REMS:
792 for (i = 0; i < readcnt; i++)
793 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
794 break;
795 case EMULATE_SST_SST25VF032B:
796 for (i = 0; i < readcnt; i++)
797 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
798 break;
799 case EMULATE_MACRONIX_MX25L6436:
800 for (i = 0; i < readcnt; i++)
801 readarr[i] = mx25l6436_rems_response[(offs + i) % 2];
802 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100803 case EMULATE_WINBOND_W25Q128FV:
804 for (i = 0; i < readcnt; i++)
805 readarr[i] = w25q128fv_rems_response[(offs + i) % 2];
806 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000807 default: /* ignore */
808 break;
809 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000810 break;
811 case JEDEC_RDID:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000812 switch (data->emu_chip) {
Stefan Tauner0b9df972012-05-07 22:12:16 +0000813 case EMULATE_SST_SST25VF032B:
814 if (readcnt > 0)
815 readarr[0] = 0xbf;
816 if (readcnt > 1)
817 readarr[1] = 0x25;
818 if (readcnt > 2)
819 readarr[2] = 0x4a;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000820 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000821 case EMULATE_MACRONIX_MX25L6436:
822 if (readcnt > 0)
823 readarr[0] = 0xc2;
824 if (readcnt > 1)
825 readarr[1] = 0x20;
826 if (readcnt > 2)
827 readarr[2] = 0x17;
828 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100829 case EMULATE_WINBOND_W25Q128FV:
830 if (readcnt > 0)
831 readarr[0] = 0xef;
832 if (readcnt > 1)
833 readarr[1] = 0x40;
834 if (readcnt > 2)
835 readarr[2] = 0x18;
836 break;
Nico Huber4203a472022-05-28 17:28:05 +0200837 case EMULATE_SPANSION_S25FL128L:
838 if (readcnt > 0)
839 readarr[0] = 0x01;
840 if (readcnt > 1)
841 readarr[1] = 0x60;
842 if (readcnt > 2)
843 readarr[2] = 0x18;
844 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000845 default: /* ignore */
846 break;
847 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000848 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000849 case JEDEC_RDSR:
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200850 memset(readarr, data->emu_status[0], readcnt);
851 break;
852 case JEDEC_RDSR2:
853 if (data->emu_status_len >= 2)
854 memset(readarr, data->emu_status[1], readcnt);
855 break;
856 case JEDEC_RDSR3:
857 if (data->emu_status_len >= 3)
858 memset(readarr, data->emu_status[2], readcnt);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000859 break;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000860 /* FIXME: this should be chip-specific. */
861 case JEDEC_EWSR:
862 case JEDEC_WREN:
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200863 data->emu_status[0] |= SPI_SR_WEL;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000864 break;
865 case JEDEC_WRSR:
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200866 if (!(data->emu_status[0] & SPI_SR_WEL)) {
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000867 msg_perr("WRSR attempted, but WEL is 0!\n");
868 break;
869 }
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200870
Nico Huberbbccdb22022-05-28 16:48:26 +0200871 wrsr_ext2 = (writecnt == 3 && data->emu_wrsr_ext2);
872 wrsr_ext3 = (writecnt == 4 && data->emu_wrsr_ext3);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200873
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000874 /* FIXME: add some reasonable simulation of the busy flag */
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200875
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200876 ro_bits = get_reg_ro_bit_mask(data, STATUS1);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200877 data->emu_status[0] &= ro_bits;
878 data->emu_status[0] |= writearr[1] & ~ro_bits;
Nico Huberbbccdb22022-05-28 16:48:26 +0200879 if (wrsr_ext2 || wrsr_ext3) {
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200880 ro_bits = get_reg_ro_bit_mask(data, STATUS2);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200881 data->emu_status[1] &= ro_bits;
882 data->emu_status[1] |= writearr[2] & ~ro_bits;
883 }
Nico Huberbbccdb22022-05-28 16:48:26 +0200884 if (wrsr_ext3) {
885 ro_bits = get_reg_ro_bit_mask(data, STATUS3);
886 data->emu_status[2] &= ro_bits;
887 data->emu_status[2] |= writearr[3] & ~ro_bits;
888 }
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200889
Nico Huberbbccdb22022-05-28 16:48:26 +0200890 if (wrsr_ext3)
891 msg_pdbg2("WRSR wrote 0x%02x%02x%02x.\n", data->emu_status[2], data->emu_status[1], data->emu_status[0]);
892 else if (wrsr_ext2)
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200893 msg_pdbg2("WRSR wrote 0x%02x%02x.\n", data->emu_status[1], data->emu_status[0]);
894 else
895 msg_pdbg2("WRSR wrote 0x%02x.\n", data->emu_status[0]);
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200896
897 update_write_protection(data);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200898 break;
899 case JEDEC_WRSR2:
900 if (data->emu_status_len < 2)
901 break;
902 if (!(data->emu_status[0] & SPI_SR_WEL)) {
903 msg_perr("WRSR2 attempted, but WEL is 0!\n");
904 break;
905 }
906
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200907 ro_bits = get_reg_ro_bit_mask(data, STATUS2);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200908 data->emu_status[1] &= ro_bits;
909 data->emu_status[1] |= (writearr[1] & ~ro_bits);
910
911 msg_pdbg2("WRSR2 wrote 0x%02x.\n", data->emu_status[1]);
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200912
913 update_write_protection(data);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200914 break;
915 case JEDEC_WRSR3:
916 if (data->emu_status_len < 3)
917 break;
918 if (!(data->emu_status[0] & SPI_SR_WEL)) {
919 msg_perr("WRSR3 attempted, but WEL is 0!\n");
920 break;
921 }
922
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200923 ro_bits = get_reg_ro_bit_mask(data, STATUS3);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200924 data->emu_status[2] &= ro_bits;
925 data->emu_status[2] |= (writearr[1] & ~ro_bits);
926
927 msg_pdbg2("WRSR3 wrote 0x%02x.\n", data->emu_status[2]);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000928 break;
929 case JEDEC_READ:
930 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
931 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000932 offs %= data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000933 if (readcnt > 0)
Edward O'Callaghan94250222021-05-20 20:34:02 +1000934 memcpy(readarr, data->flashchip_contents + offs, readcnt);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000935 break;
936 case JEDEC_BYTE_PROGRAM:
937 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
938 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000939 offs %= data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000940 if (writecnt < 5) {
941 msg_perr("BYTE PROGRAM size too short!\n");
942 return 1;
943 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000944 if (writecnt - 4 > data->emu_max_byteprogram_size) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000945 msg_perr("Max BYTE PROGRAM size exceeded!\n");
946 return 1;
947 }
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200948 if (write_flash_data(data, offs, writecnt - 4, writearr + 4)) {
949 msg_perr("Failed to program flash!\n");
950 return 1;
951 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000952 break;
953 case JEDEC_AAI_WORD_PROGRAM:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000954 if (!data->emu_max_aai_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000955 break;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200956 if (!(data->emu_status[0] & SPI_SR_AAI)) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000957 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
958 msg_perr("Initial AAI WORD PROGRAM size too "
959 "short!\n");
960 return 1;
961 }
962 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
963 msg_perr("Initial AAI WORD PROGRAM size too "
964 "long!\n");
965 return 1;
966 }
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200967 data->emu_status[0] |= SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000968 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
969 writearr[3];
970 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000971 aai_offs %= data->emu_chip_size;
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200972 if (write_flash_data(data, aai_offs, 2, writearr + 4)) {
973 msg_perr("Failed to program flash!\n");
974 return 1;
975 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000976 aai_offs += 2;
977 } else {
978 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
979 msg_perr("Continuation AAI WORD PROGRAM size "
980 "too short!\n");
981 return 1;
982 }
983 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
984 msg_perr("Continuation AAI WORD PROGRAM size "
985 "too long!\n");
986 return 1;
987 }
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200988 if (write_flash_data(data, aai_offs, 2, writearr + 1)) {
989 msg_perr("Failed to program flash!\n");
990 return 1;
991 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000992 aai_offs += 2;
993 }
994 break;
995 case JEDEC_WRDI:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000996 if (data->emu_max_aai_size)
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200997 data->emu_status[0] &= ~SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000998 break;
999 case JEDEC_SE:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001000 if (!data->emu_jedec_se_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001001 break;
1002 if (writecnt != JEDEC_SE_OUTSIZE) {
1003 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
1004 return 1;
1005 }
1006 if (readcnt != JEDEC_SE_INSIZE) {
1007 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
1008 return 1;
1009 }
1010 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +10001011 if (offs & (data->emu_jedec_se_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +00001012 msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +10001013 offs &= ~(data->emu_jedec_se_size - 1);
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +02001014 if (erase_flash_data(data, offs, data->emu_jedec_se_size)) {
1015 msg_perr("Failed to erase flash!\n");
1016 return 1;
1017 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001018 break;
1019 case JEDEC_BE_52:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001020 if (!data->emu_jedec_be_52_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001021 break;
1022 if (writecnt != JEDEC_BE_52_OUTSIZE) {
1023 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
1024 return 1;
1025 }
1026 if (readcnt != JEDEC_BE_52_INSIZE) {
1027 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
1028 return 1;
1029 }
1030 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +10001031 if (offs & (data->emu_jedec_be_52_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +00001032 msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +10001033 offs &= ~(data->emu_jedec_be_52_size - 1);
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +02001034 if (erase_flash_data(data, offs, data->emu_jedec_be_52_size)) {
1035 msg_perr("Failed to erase flash!\n");
1036 return 1;
1037 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001038 break;
1039 case JEDEC_BE_D8:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001040 if (!data->emu_jedec_be_d8_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001041 break;
1042 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
1043 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
1044 return 1;
1045 }
1046 if (readcnt != JEDEC_BE_D8_INSIZE) {
1047 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
1048 return 1;
1049 }
1050 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +10001051 if (offs & (data->emu_jedec_be_d8_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +00001052 msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +10001053 offs &= ~(data->emu_jedec_be_d8_size - 1);
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +02001054 if (erase_flash_data(data, offs, data->emu_jedec_be_d8_size)) {
1055 msg_perr("Failed to erase flash!\n");
1056 return 1;
1057 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001058 break;
1059 case JEDEC_CE_60:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001060 if (!data->emu_jedec_ce_60_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001061 break;
1062 if (writecnt != JEDEC_CE_60_OUTSIZE) {
1063 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
1064 return 1;
1065 }
1066 if (readcnt != JEDEC_CE_60_INSIZE) {
1067 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
1068 return 1;
1069 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +00001070 /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001071 /* emu_jedec_ce_60_size is emu_chip_size. */
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +02001072 if (erase_flash_data(data, 0, data->emu_jedec_ce_60_size)) {
1073 msg_perr("Failed to erase flash!\n");
1074 return 1;
1075 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001076 break;
1077 case JEDEC_CE_C7:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001078 if (!data->emu_jedec_ce_c7_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001079 break;
1080 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
1081 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
1082 return 1;
1083 }
1084 if (readcnt != JEDEC_CE_C7_INSIZE) {
1085 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
1086 return 1;
1087 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +00001088 /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001089 /* emu_jedec_ce_c7_size is emu_chip_size. */
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +02001090 if (erase_flash_data(data, 0, data->emu_jedec_ce_c7_size)) {
1091 msg_perr("Failed to erase flash!\n");
1092 return 1;
1093 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001094 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +00001095 case JEDEC_SFDP:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001096 if (data->emu_chip != EMULATE_MACRONIX_MX25L6436)
Stefan Tauner0b9df972012-05-07 22:12:16 +00001097 break;
1098 if (writecnt < 4)
1099 break;
1100 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
1101
1102 /* SFDP expects one dummy byte after the address. */
1103 if (writecnt == 4) {
1104 /* The dummy byte was not written, make sure it is read instead.
1105 * Shifting and shortening the read array does achieve this goal.
1106 */
1107 readarr++;
1108 readcnt--;
1109 } else {
1110 /* The response is shifted if more than 5 bytes are written, because SFDP data is
1111 * already shifted out by the chip while those superfluous bytes are written. */
1112 offs += writecnt - 5;
1113 }
1114
1115 /* The SFDP spec implies that the start address of an SFDP read may be truncated to fit in the
1116 * SFDP table address space, i.e. the start address may be wrapped around at SFDP table size.
1117 * This is a reasonable implementation choice in hardware because it saves a few gates. */
1118 if (offs >= sizeof(sfdp_table)) {
1119 msg_pdbg("Wrapping the start address around the SFDP table boundary (using 0x%x "
1120 "instead of 0x%x).\n", (unsigned int)(offs % sizeof(sfdp_table)), offs);
1121 offs %= sizeof(sfdp_table);
1122 }
1123 toread = min(sizeof(sfdp_table) - offs, readcnt);
1124 memcpy(readarr, sfdp_table + offs, toread);
1125 if (toread < readcnt)
1126 msg_pdbg("Crossing the SFDP table boundary in a single "
1127 "continuous chunk produces undefined results "
1128 "after that point.\n");
1129 break;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001130 default:
1131 /* No special response. */
1132 break;
1133 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +00001134 if (writearr[0] != JEDEC_WREN && writearr[0] != JEDEC_EWSR)
Sergii Dmytruk59151a42021-11-08 00:05:12 +02001135 data->emu_status[0] &= ~SPI_SR_WEL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001136 return 0;
1137}
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001138
Edward O'Callaghan5eca4272020-04-12 17:27:53 +10001139static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001140 unsigned int readcnt,
1141 const unsigned char *writearr,
1142 unsigned char *readarr)
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001143{
Nico Huber519be662018-12-23 20:03:35 +01001144 unsigned int i;
Lachlan Bishopc753c402020-09-10 14:57:05 +10001145 struct emu_data *emu_data = flash->mst->spi.data;
1146 if (!emu_data) {
1147 msg_perr("No data in flash context!\n");
1148 return 1;
1149 }
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001150
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +00001151 msg_pspew("%s:", __func__);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001152
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +00001153 msg_pspew(" writing %u bytes:", writecnt);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001154 for (i = 0; i < writecnt; i++)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +00001155 msg_pspew(" 0x%02x", writearr[i]);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001156
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001157 /* Response for unknown commands and missing chip is 0xff. */
1158 memset(readarr, 0xff, readcnt);
Lachlan Bishopc753c402020-09-10 14:57:05 +10001159 switch (emu_data->emu_chip) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001160 case EMULATE_ST_M25P10_RES:
1161 case EMULATE_SST_SST25VF040_REMS:
1162 case EMULATE_SST_SST25VF032B:
Stefan Tauner0b9df972012-05-07 22:12:16 +00001163 case EMULATE_MACRONIX_MX25L6436:
Nico Huberf9632d82019-01-20 11:23:49 +01001164 case EMULATE_WINBOND_W25Q128FV:
Nico Huber4203a472022-05-28 17:28:05 +02001165 case EMULATE_SPANSION_S25FL128L:
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001166 if (emulate_spi_chip_response(writecnt, readcnt, writearr,
Lachlan Bishopc753c402020-09-10 14:57:05 +10001167 readarr, emu_data)) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +00001168 msg_pdbg("Invalid command sent to flash chip!\n");
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001169 return 1;
1170 }
1171 break;
1172 default:
1173 break;
1174 }
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +00001175 msg_pspew(" reading %u bytes:", readcnt);
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001176 for (i = 0; i < readcnt; i++)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001177 msg_pspew(" 0x%02x", readarr[i]);
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +00001178 msg_pspew("\n");
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001179 return 0;
1180}
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +00001181
Mark Marshallf20b7be2014-05-09 21:16:21 +00001182static 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 +00001183{
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001184 return spi_write_chunked(flash, buf, start, len,
1185 spi_write_256_chunksize);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001186}
Thomas Heijligencc853d82021-05-04 15:32:17 +02001187
1188const struct programmer_entry programmer_dummy = {
1189 .name = "dummy",
1190 .type = OTHER,
1191 /* FIXME */
1192 .devs.note = "Dummy device, does nothing and logs all accesses\n",
1193 .init = dummy_init,
1194 .map_flash_region = dummy_map,
1195 .unmap_flash_region = dummy_unmap,
1196 .delay = internal_delay,
1197};