blob: 3f3983be92850ce14fc6923703a6a1937524fdd4 [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>
Felix Singer2fb53b12022-08-19 03:29:32 +020018#include <stdbool.h>
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000019#include <stdlib.h>
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +000020#include <stdio.h>
21#include <ctype.h>
Stefan Tauner5e695ab2012-05-06 17:03:40 +000022#include <errno.h>
Nico Huberab696292021-06-09 18:10:07 +020023#include <sys/types.h>
24#include <sys/stat.h>
Nico Huberfbc41d22026-02-22 23:04:01 +010025
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000026#include "flash.h"
Nico Huberfbc41d22026-02-22 23:04:01 +010027#include "chipdrivers/spi.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000028#include "programmer.h"
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000029
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000030#include "spi.h"
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +020031#include "writeprotect.h"
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000032
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000033enum emu_chip {
34 EMULATE_NONE,
Nico Huberafb5dd02026-02-15 13:26:10 +010035 EMULATE_ST_M25P10_A,
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000036 EMULATE_ST_M25P10_RES,
37 EMULATE_SST_SST25VF040_REMS,
38 EMULATE_SST_SST25VF032B,
Stefan Tauner0b9df972012-05-07 22:12:16 +000039 EMULATE_MACRONIX_MX25L6436,
Nico Huberf9632d82019-01-20 11:23:49 +010040 EMULATE_WINBOND_W25Q128FV,
Nico Huber4203a472022-05-28 17:28:05 +020041 EMULATE_SPANSION_S25FL128L,
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000042};
Stefan Tauner0b9df972012-05-07 22:12:16 +000043
Lachlan Bishopc753c402020-09-10 14:57:05 +100044struct emu_data {
45 enum emu_chip emu_chip;
46 char *emu_persistent_image;
47 unsigned int emu_chip_size;
Sergii Dmytruk59151a42021-11-08 00:05:12 +020048 /* Note: W25Q128FV doesn't change value of SR2 if it's not provided, but
49 * even its previous generations do, so don't forget to update
Nico Huberbbccdb22022-05-28 16:48:26 +020050 * WRSR code on enabling WRSR_EXT2 for more chips. */
51 bool emu_wrsr_ext2;
52 bool emu_wrsr_ext3;
Felix Singer2fb53b12022-08-19 03:29:32 +020053 bool emu_modified; /* is the image modified since reading it? */
Sergii Dmytruk59151a42021-11-08 00:05:12 +020054 uint8_t emu_status[3];
55 uint8_t emu_status_len; /* number of emulated status registers */
Lachlan Bishopc753c402020-09-10 14:57:05 +100056 unsigned int emu_max_byteprogram_size;
57 unsigned int emu_max_aai_size;
58 unsigned int emu_jedec_se_size;
59 unsigned int emu_jedec_be_52_size;
60 unsigned int emu_jedec_be_d8_size;
61 unsigned int emu_jedec_ce_60_size;
62 unsigned int emu_jedec_ce_c7_size;
63 unsigned char spi_blacklist[256];
64 unsigned char spi_ignorelist[256];
65 unsigned int spi_blacklist_size;
66 unsigned int spi_ignorelist_size;
Edward O'Callaghan94250222021-05-20 20:34:02 +100067
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +020068 bool hwwp; /* state of hardware write protection */
69 /* wp_start == wp_end when write-protection is disabled */
70 uint32_t wp_start;
71 uint32_t wp_end;
72
Edward O'Callaghanb1313422021-05-20 20:27:59 +100073 unsigned int spi_write_256_chunksize;
Edward O'Callaghan94250222021-05-20 20:34:02 +100074 uint8_t *flashchip_contents;
Lachlan Bishopc753c402020-09-10 14:57:05 +100075};
76
Stefan Tauner0b9df972012-05-07 22:12:16 +000077/* A legit complete SFDP table based on the MX25L6436E (rev. 1.8) datasheet. */
Stefan Tauner67d163d2013-01-15 17:37:48 +000078static const uint8_t sfdp_table[] = {
Stefan Tauner0b9df972012-05-07 22:12:16 +000079 0x53, 0x46, 0x44, 0x50, // @0x00: SFDP signature
80 0x00, 0x01, 0x01, 0xFF, // @0x04: revision 1.0, 2 headers
81 0x00, 0x00, 0x01, 0x09, // @0x08: JEDEC SFDP header rev. 1.0, 9 DW long
82 0x1C, 0x00, 0x00, 0xFF, // @0x0C: PTP0 = 0x1C (instead of 0x30)
83 0xC2, 0x00, 0x01, 0x04, // @0x10: Macronix header rev. 1.0, 4 DW long
84 0x48, 0x00, 0x00, 0xFF, // @0x14: PTP1 = 0x48 (instead of 0x60)
85 0xFF, 0xFF, 0xFF, 0xFF, // @0x18: hole.
86 0xE5, 0x20, 0xC9, 0xFF, // @0x1C: SFDP parameter table start
87 0xFF, 0xFF, 0xFF, 0x03, // @0x20
88 0x00, 0xFF, 0x08, 0x6B, // @0x24
89 0x08, 0x3B, 0x00, 0xFF, // @0x28
90 0xEE, 0xFF, 0xFF, 0xFF, // @0x2C
91 0xFF, 0xFF, 0x00, 0x00, // @0x30
92 0xFF, 0xFF, 0x00, 0xFF, // @0x34
93 0x0C, 0x20, 0x0F, 0x52, // @0x38
94 0x10, 0xD8, 0x00, 0xFF, // @0x3C: SFDP parameter table end
95 0xFF, 0xFF, 0xFF, 0xFF, // @0x40: hole.
96 0xFF, 0xFF, 0xFF, 0xFF, // @0x44: hole.
97 0x00, 0x36, 0x00, 0x27, // @0x48: Macronix parameter table start
98 0xF4, 0x4F, 0xFF, 0xFF, // @0x4C
99 0xD9, 0xC8, 0xFF, 0xFF, // @0x50
100 0xFF, 0xFF, 0xFF, 0xFF, // @0x54: Macronix parameter table end
101};
102
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000103
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000104
Nico Huber610c1aa2023-02-15 02:56:05 +0100105static int dummy_spi_send_command(const struct spi_master *, unsigned int writecnt, unsigned int readcnt,
Mark Marshallf20b7be2014-05-09 21:16:21 +0000106 const unsigned char *writearr, unsigned char *readarr);
107static int dummy_spi_write_256(struct flashctx *flash, const uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000108 unsigned int start, unsigned int len);
Mark Marshallf20b7be2014-05-09 21:16:21 +0000109static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr);
110static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr);
111static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr);
112static void dummy_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len);
113static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr);
114static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr);
115static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr);
116static void dummy_chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len);
Nikolai Artemieve7a41e32022-11-28 17:40:56 +1100117static bool dummy_spi_probe_opcode(const struct flashctx *flash, uint8_t opcode);
Nico Huber0e76d992023-01-12 20:22:55 +0100118static void *dummy_map(const char *descr, uintptr_t phys_addr, size_t len);
119static void dummy_unmap(void *virt_addr, size_t len);
Michael Karcherb9dbe482011-05-11 17:07:07 +0000120
Nico Huber03f3a6d2021-05-11 17:53:34 +0200121static const struct spi_master spi_master_dummyflasher = {
Nico Huber1cf407b2017-11-10 20:18:23 +0100122 .features = SPI_MASTER_4BA,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000123 .max_data_read = MAX_DATA_READ_UNLIMITED,
124 .max_data_write = MAX_DATA_UNSPECIFIED,
125 .command = dummy_spi_send_command,
126 .multicommand = default_spi_send_multicommand,
127 .read = default_spi_read,
128 .write_256 = dummy_spi_write_256,
Aarya Chaumal0cea7532022-07-04 18:21:50 +0530129 .probe_opcode = dummy_spi_probe_opcode,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000130};
David Hendricks8bb20212011-06-14 01:35:36 +0000131
Edward O'Callaghanf7504a92021-05-20 20:21:13 +1000132static const struct par_master par_master_dummyflasher = {
Thomas Heijligen43040f22022-06-23 14:38:35 +0200133 .chip_readb = dummy_chip_readb,
134 .chip_readw = dummy_chip_readw,
135 .chip_readl = dummy_chip_readl,
136 .chip_readn = dummy_chip_readn,
137 .chip_writeb = dummy_chip_writeb,
138 .chip_writew = dummy_chip_writew,
139 .chip_writel = dummy_chip_writel,
140 .chip_writen = dummy_chip_writen,
Nico Huber0e76d992023-01-12 20:22:55 +0100141 .map_flash = dummy_map,
142 .unmap_flash = dummy_unmap,
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000143};
144
David Hendricks8bb20212011-06-14 01:35:36 +0000145static int dummy_shutdown(void *data)
146{
147 msg_pspew("%s\n", __func__);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000148 struct emu_data *emu_data = (struct emu_data *)data;
149 if (emu_data->emu_chip != EMULATE_NONE) {
150 if (emu_data->emu_persistent_image && emu_data->emu_modified) {
151 msg_pdbg("Writing %s\n", emu_data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000152 write_buf_to_file(emu_data->flashchip_contents,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000153 emu_data->emu_chip_size,
154 emu_data->emu_persistent_image);
David Hendricks8bb20212011-06-14 01:35:36 +0000155 }
Angel Pons02b9ae22021-05-25 12:46:43 +0200156 free(emu_data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000157 free(emu_data->flashchip_contents);
David Hendricks8bb20212011-06-14 01:35:36 +0000158 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000159 free(data);
David Hendricks8bb20212011-06-14 01:35:36 +0000160 return 0;
161}
162
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000163static int init_data(struct emu_data *data, enum chipbustype *dummy_buses_supported)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000164{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000165 char *bustext = NULL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000166 char *tmp = NULL;
Nico Huber519be662018-12-23 20:03:35 +0100167 unsigned int i;
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000168 char *endptr;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000169 char *status = NULL;
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000170
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000171 bustext = extract_programmer_param("bus");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000172 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
173 if (!bustext)
174 bustext = strdup("parallel+lpc+fwh+spi");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000175 /* Convert the parameters to lowercase. */
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000176 tolower_string(bustext);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000177
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000178 *dummy_buses_supported = BUS_NONE;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000179 if (strstr(bustext, "parallel")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000180 *dummy_buses_supported |= BUS_PARALLEL;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000181 msg_pdbg("Enabling support for %s flash.\n", "parallel");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000182 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000183 if (strstr(bustext, "lpc")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000184 *dummy_buses_supported |= BUS_LPC;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000185 msg_pdbg("Enabling support for %s flash.\n", "LPC");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000186 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000187 if (strstr(bustext, "fwh")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000188 *dummy_buses_supported |= BUS_FWH;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000189 msg_pdbg("Enabling support for %s flash.\n", "FWH");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000190 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000191 if (strstr(bustext, "spi")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000192 *dummy_buses_supported |= BUS_SPI;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000193 msg_pdbg("Enabling support for %s flash.\n", "SPI");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000194 }
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000195 if (*dummy_buses_supported == BUS_NONE)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000196 msg_pdbg("Support for all flash bus types disabled.\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000197 free(bustext);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000198
199 tmp = extract_programmer_param("spi_write_256_chunksize");
200 if (tmp) {
Edward O'Callaghanb1313422021-05-20 20:27:59 +1000201 data->spi_write_256_chunksize = strtoul(tmp, &endptr, 0);
202 if (*endptr != '\0' || data->spi_write_256_chunksize < 1) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000203 msg_perr("invalid spi_write_256_chunksize\n");
Edward O'Callaghanc785f882021-05-23 22:14:36 +1000204 free(tmp);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000205 return 1;
206 }
Edward O'Callaghanc785f882021-05-23 22:14:36 +1000207 free(tmp);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000208 }
209
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000210 tmp = extract_programmer_param("spi_blacklist");
211 if (tmp) {
212 i = strlen(tmp);
213 if (!strncmp(tmp, "0x", 2)) {
214 i -= 2;
215 memmove(tmp, tmp + 2, i + 1);
216 }
217 if ((i > 512) || (i % 2)) {
218 msg_perr("Invalid SPI command blacklist length\n");
219 free(tmp);
220 return 1;
221 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000222 data->spi_blacklist_size = i / 2;
223 for (i = 0; i < data->spi_blacklist_size * 2; i++) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000224 if (!isxdigit((unsigned char)tmp[i])) {
225 msg_perr("Invalid char \"%c\" in SPI command "
226 "blacklist\n", tmp[i]);
227 free(tmp);
228 return 1;
229 }
230 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000231 for (i = 0; i < data->spi_blacklist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000232 unsigned int tmp2;
233 /* SCNx8 is apparently not supported by MSVC (and thus
234 * MinGW), so work around it with an extra variable
235 */
236 sscanf(tmp + i * 2, "%2x", &tmp2);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000237 data->spi_blacklist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000238 }
239 msg_pdbg("SPI blacklist is ");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000240 for (i = 0; i < data->spi_blacklist_size; i++)
241 msg_pdbg("%02x ", data->spi_blacklist[i]);
242 msg_pdbg(", size %u\n", data->spi_blacklist_size);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000243 }
244 free(tmp);
245
246 tmp = extract_programmer_param("spi_ignorelist");
247 if (tmp) {
248 i = strlen(tmp);
249 if (!strncmp(tmp, "0x", 2)) {
250 i -= 2;
251 memmove(tmp, tmp + 2, i + 1);
252 }
253 if ((i > 512) || (i % 2)) {
254 msg_perr("Invalid SPI command ignorelist length\n");
255 free(tmp);
256 return 1;
257 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000258 data->spi_ignorelist_size = i / 2;
259 for (i = 0; i < data->spi_ignorelist_size * 2; i++) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000260 if (!isxdigit((unsigned char)tmp[i])) {
261 msg_perr("Invalid char \"%c\" in SPI command "
262 "ignorelist\n", tmp[i]);
263 free(tmp);
264 return 1;
265 }
266 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000267 for (i = 0; i < data->spi_ignorelist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000268 unsigned int tmp2;
269 /* SCNx8 is apparently not supported by MSVC (and thus
270 * MinGW), so work around it with an extra variable
271 */
272 sscanf(tmp + i * 2, "%2x", &tmp2);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000273 data->spi_ignorelist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000274 }
275 msg_pdbg("SPI ignorelist is ");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000276 for (i = 0; i < data->spi_ignorelist_size; i++)
277 msg_pdbg("%02x ", data->spi_ignorelist[i]);
278 msg_pdbg(", size %u\n", data->spi_ignorelist_size);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000279 }
280 free(tmp);
281
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200282 tmp = extract_programmer_param("hwwp");
283 if (tmp) {
284 if (!strcmp(tmp, "yes")) {
285 msg_pdbg("Emulated chip will have hardware WP enabled\n");
286 data->hwwp = true;
287 } else if (!strcmp(tmp, "no")) {
288 msg_pdbg("Emulated chip will have hardware WP disabled\n");
289 } else {
290 msg_perr("hwwp can be \"yes\" or \"no\"\n");
291 free(tmp);
292 return 1;
293 }
294 free(tmp);
295 }
296
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000297 tmp = extract_programmer_param("emulate");
298 if (!tmp) {
299 msg_pdbg("Not emulating any flash chip.\n");
300 /* Nothing else to do. */
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000301 return 0;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000302 }
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000303
Nico Huberafb5dd02026-02-15 13:26:10 +0100304 if (!strcmp(tmp, "M25P10-A")) {
305 data->emu_chip = EMULATE_ST_M25P10_A;
306 data->emu_chip_size = 128 * 1024;
307 data->emu_max_byteprogram_size = 256;
308 data->emu_max_aai_size = 0;
309 data->emu_status_len = 1;
310 data->emu_jedec_se_size = 0;
311 data->emu_jedec_be_52_size = 0;
312 data->emu_jedec_be_d8_size = 32 * 1024;
313 data->emu_jedec_ce_60_size = 0;
314 data->emu_jedec_ce_c7_size = data->emu_chip_size;
315 msg_pdbg("Emulating ST M25P10-A SPI flash chip (RDID, page "
316 "write)\n");
317 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000318 if (!strcmp(tmp, "M25P10.RES")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000319 data->emu_chip = EMULATE_ST_M25P10_RES;
320 data->emu_chip_size = 128 * 1024;
321 data->emu_max_byteprogram_size = 128;
322 data->emu_max_aai_size = 0;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200323 data->emu_status_len = 1;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000324 data->emu_jedec_se_size = 0;
325 data->emu_jedec_be_52_size = 0;
326 data->emu_jedec_be_d8_size = 32 * 1024;
327 data->emu_jedec_ce_60_size = 0;
328 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000329 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
330 "write)\n");
331 }
332 if (!strcmp(tmp, "SST25VF040.REMS")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000333 data->emu_chip = EMULATE_SST_SST25VF040_REMS;
334 data->emu_chip_size = 512 * 1024;
335 data->emu_max_byteprogram_size = 1;
336 data->emu_max_aai_size = 0;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200337 data->emu_status_len = 1;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000338 data->emu_jedec_se_size = 4 * 1024;
339 data->emu_jedec_be_52_size = 32 * 1024;
340 data->emu_jedec_be_d8_size = 0;
341 data->emu_jedec_ce_60_size = data->emu_chip_size;
342 data->emu_jedec_ce_c7_size = 0;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000343 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
344 "byte write)\n");
345 }
346 if (!strcmp(tmp, "SST25VF032B")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000347 data->emu_chip = EMULATE_SST_SST25VF032B;
348 data->emu_chip_size = 4 * 1024 * 1024;
349 data->emu_max_byteprogram_size = 1;
350 data->emu_max_aai_size = 2;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200351 data->emu_status_len = 1;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000352 data->emu_jedec_se_size = 4 * 1024;
353 data->emu_jedec_be_52_size = 32 * 1024;
354 data->emu_jedec_be_d8_size = 64 * 1024;
355 data->emu_jedec_ce_60_size = data->emu_chip_size;
356 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000357 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
358 "write)\n");
359 }
Stefan Tauner0b9df972012-05-07 22:12:16 +0000360 if (!strcmp(tmp, "MX25L6436")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000361 data->emu_chip = EMULATE_MACRONIX_MX25L6436;
362 data->emu_chip_size = 8 * 1024 * 1024;
363 data->emu_max_byteprogram_size = 256;
364 data->emu_max_aai_size = 0;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200365 data->emu_status_len = 1;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000366 data->emu_jedec_se_size = 4 * 1024;
367 data->emu_jedec_be_52_size = 32 * 1024;
368 data->emu_jedec_be_d8_size = 64 * 1024;
369 data->emu_jedec_ce_60_size = data->emu_chip_size;
370 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000371 msg_pdbg("Emulating Macronix MX25L6436 SPI flash chip (RDID, "
372 "SFDP)\n");
373 }
Nico Huberf9632d82019-01-20 11:23:49 +0100374 if (!strcmp(tmp, "W25Q128FV")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000375 data->emu_chip = EMULATE_WINBOND_W25Q128FV;
Nico Huberbbccdb22022-05-28 16:48:26 +0200376 data->emu_wrsr_ext2 = true;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000377 data->emu_chip_size = 16 * 1024 * 1024;
378 data->emu_max_byteprogram_size = 256;
379 data->emu_max_aai_size = 0;
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200380 data->emu_status_len = 3;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000381 data->emu_jedec_se_size = 4 * 1024;
382 data->emu_jedec_be_52_size = 32 * 1024;
383 data->emu_jedec_be_d8_size = 64 * 1024;
384 data->emu_jedec_ce_60_size = data->emu_chip_size;
385 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Nico Huberf9632d82019-01-20 11:23:49 +0100386 msg_pdbg("Emulating Winbond W25Q128FV SPI flash chip (RDID)\n");
387 }
Nico Huber4203a472022-05-28 17:28:05 +0200388 if (!strcmp(tmp, "S25FL128L")) {
389 data->emu_chip = EMULATE_SPANSION_S25FL128L;
390 data->emu_wrsr_ext2 = true;
391 data->emu_wrsr_ext3 = true;
392 data->emu_chip_size = 16 * 1024 * 1024;
393 data->emu_max_byteprogram_size = 256;
394 data->emu_max_aai_size = 0;
395 data->emu_status_len = 3;
396 data->emu_jedec_se_size = 4 * 1024;
397 data->emu_jedec_be_52_size = 32 * 1024;
398 data->emu_jedec_be_d8_size = 64 * 1024;
399 data->emu_jedec_ce_60_size = data->emu_chip_size;
400 data->emu_jedec_ce_c7_size = data->emu_chip_size;
401 msg_pdbg("Emulating Spansion S25FL128L SPI flash chip (RES, RDID, WP)\n");
402 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000403 if (data->emu_chip == EMULATE_NONE) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000404 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
405 free(tmp);
406 return 1;
407 }
408 free(tmp);
David Hendricks8bb20212011-06-14 01:35:36 +0000409
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000410 status = extract_programmer_param("spi_status");
411 if (status) {
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200412 unsigned int emu_status;
413
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000414 errno = 0;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200415 emu_status = strtoul(status, &endptr, 0);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000416 if (errno != 0 || status == endptr) {
Angel Ponsc2484642021-05-25 13:03:24 +0200417 free(status);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000418 msg_perr("Error: initial status register specified, "
419 "but the value could not be converted.\n");
420 return 1;
421 }
Angel Ponsc2484642021-05-25 13:03:24 +0200422 free(status);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200423
424 data->emu_status[0] = emu_status;
425 data->emu_status[1] = emu_status >> 8;
426 data->emu_status[2] = emu_status >> 16;
427
428 if (data->emu_status_len == 3) {
429 msg_pdbg("Initial status registers:\n"
430 "\tSR1 is set to 0x%02x\n"
431 "\tSR2 is set to 0x%02x\n"
432 "\tSR3 is set to 0x%02x\n",
433 data->emu_status[0], data->emu_status[1], data->emu_status[2]);
434 } else if (data->emu_status_len == 2) {
435 msg_pdbg("Initial status registers:\n"
436 "\tSR1 is set to 0x%02x\n"
437 "\tSR2 is set to 0x%02x\n",
438 data->emu_status[0], data->emu_status[1]);
439 } else {
440 msg_pdbg("Initial status register is set to 0x%02x.\n",
441 data->emu_status[0]);
442 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000443 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000444
Angel Pons328898a2021-05-25 12:56:18 +0200445 data->flashchip_contents = malloc(data->emu_chip_size);
446 if (!data->flashchip_contents) {
447 msg_perr("Out of memory!\n");
448 return 1;
449 }
450
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000451
452 return 0;
453}
454
Nico Hubere3a26882023-01-11 21:45:51 +0100455static int dummy_init(struct flashprog_programmer *const prog)
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000456{
Alexander Goncharov0d929fe2023-01-24 14:43:12 +0400457 int ret = 0;
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000458 struct stat image_stat;
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000459
Nico Huber4e9e99c2021-06-09 18:08:48 +0200460 struct emu_data *data = calloc(1, sizeof(*data));
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000461 if (!data) {
462 msg_perr("Out of memory!\n");
463 return 1;
464 }
465 data->emu_chip = EMULATE_NONE;
Edward O'Callaghanb1313422021-05-20 20:27:59 +1000466 data->spi_write_256_chunksize = 256;
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000467
468 msg_pspew("%s\n", __func__);
469
470 enum chipbustype dummy_buses_supported;
471 if (init_data(data, &dummy_buses_supported)) {
472 free(data);
473 return 1;
474 }
475
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000476 if (data->emu_chip == EMULATE_NONE) {
477 msg_pdbg("Not emulating any flash chip.\n");
478 /* Nothing else to do. */
479 goto dummy_init_out;
480 }
481
Lachlan Bishopc753c402020-09-10 14:57:05 +1000482 msg_pdbg("Filling fake flash chip with 0xff, size %i\n", data->emu_chip_size);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000483 memset(data->flashchip_contents, 0xff, data->emu_chip_size);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000484
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000485 /* Will be freed by shutdown function if necessary. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000486 data->emu_persistent_image = extract_programmer_param("image");
487 if (!data->emu_persistent_image) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000488 /* Nothing else to do. */
David Hendricks8bb20212011-06-14 01:35:36 +0000489 goto dummy_init_out;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000490 }
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000491 /* We will silently (in default verbosity) ignore the file if it does not exist (yet) or the size does
492 * not match the emulated chip. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000493 if (!stat(data->emu_persistent_image, &image_stat)) {
Stefan Tauner23e10b82016-01-23 16:16:49 +0000494 msg_pdbg("Found persistent image %s, %jd B ",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000495 data->emu_persistent_image, (intmax_t)image_stat.st_size);
496 if ((uintmax_t)image_stat.st_size == data->emu_chip_size) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000497 msg_pdbg("matches.\n");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000498 msg_pdbg("Reading %s\n", data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000499 if (read_buf_from_file(data->flashchip_contents, data->emu_chip_size,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000500 data->emu_persistent_image)) {
501 msg_perr("Unable to read %s\n", data->emu_persistent_image);
Angel Pons02b9ae22021-05-25 12:46:43 +0200502 free(data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000503 free(data->flashchip_contents);
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000504 free(data);
Jacob Garberca598da2019-08-12 10:44:17 -0600505 return 1;
506 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000507 } else {
508 msg_pdbg("doesn't match.\n");
509 }
510 }
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000511
David Hendricks8bb20212011-06-14 01:35:36 +0000512dummy_init_out:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000513 if (register_shutdown(dummy_shutdown, data)) {
Angel Pons02b9ae22021-05-25 12:46:43 +0200514 free(data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000515 free(data->flashchip_contents);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000516 free(data);
David Hendricks8bb20212011-06-14 01:35:36 +0000517 return 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000518 }
Edward O'Callaghan3fa321d2021-05-17 20:01:27 +1000519 if (dummy_buses_supported & BUS_NONSPI)
Alexander Goncharov0d929fe2023-01-24 14:43:12 +0400520 ret |= register_par_master(&par_master_dummyflasher,
521 dummy_buses_supported & BUS_NONSPI,
Nico Huber89569d62023-01-12 23:31:40 +0100522 0, data);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000523 if (dummy_buses_supported & BUS_SPI)
Nico Huber89569d62023-01-12 23:31:40 +0100524 ret |= register_spi_master(&spi_master_dummyflasher, 0, data);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000525
Alexander Goncharov0d929fe2023-01-24 14:43:12 +0400526 return ret;
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000527}
528
Thomas Heijligencc853d82021-05-04 15:32:17 +0200529static void *dummy_map(const char *descr, uintptr_t phys_addr, size_t len)
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000530{
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000531 msg_pspew("%s: Mapping %s, 0x%zx bytes at 0x%0*" PRIxPTR "\n",
Stefan Tauner0554ca52013-07-25 22:54:25 +0000532 __func__, descr, len, PRIxPTR_WIDTH, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000533 return (void *)phys_addr;
534}
535
Thomas Heijligencc853d82021-05-04 15:32:17 +0200536static void dummy_unmap(void *virt_addr, size_t len)
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000537{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000538 msg_pspew("%s: Unmapping 0x%zx bytes at %p\n", __func__, len, virt_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000539}
540
Mark Marshallf20b7be2014-05-09 21:16:21 +0000541static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000542{
Stefan Taunerc2333752013-07-13 23:31:37 +0000543 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%02x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000544}
545
Mark Marshallf20b7be2014-05-09 21:16:21 +0000546static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000547{
Stefan Taunerc2333752013-07-13 23:31:37 +0000548 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%04x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000549}
550
Mark Marshallf20b7be2014-05-09 21:16:21 +0000551static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000552{
Stefan Taunerc2333752013-07-13 23:31:37 +0000553 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%08x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000554}
555
Mark Marshallf20b7be2014-05-09 21:16:21 +0000556static 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 +0000557{
558 size_t i;
Stefan Tauner0554ca52013-07-25 22:54:25 +0000559 msg_pspew("%s: addr=0x%" PRIxPTR ", len=0x%zx, writing data (hex):", __func__, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000560 for (i = 0; i < len; i++) {
561 if ((i % 16) == 0)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000562 msg_pspew("\n");
563 msg_pspew("%02x ", buf[i]);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000564 }
565}
566
Mark Marshallf20b7be2014-05-09 21:16:21 +0000567static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000568{
Stefan Taunerc2333752013-07-13 23:31:37 +0000569 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000570 return 0xff;
571}
572
Mark Marshallf20b7be2014-05-09 21:16:21 +0000573static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000574{
Stefan Taunerc2333752013-07-13 23:31:37 +0000575 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000576 return 0xffff;
577}
578
Mark Marshallf20b7be2014-05-09 21:16:21 +0000579static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000580{
Stefan Taunerc2333752013-07-13 23:31:37 +0000581 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffffffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000582 return 0xffffffff;
583}
584
Mark Marshallf20b7be2014-05-09 21:16:21 +0000585static 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 +0000586{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000587 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 +0000588 memset(buf, 0xff, len);
589 return;
590}
591
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200592static uint8_t get_reg_ro_bit_mask(const struct emu_data *data, enum flash_reg reg)
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200593{
594 /* Whoever adds a new register must not forget to update this function
595 or at least shouldn't use it incorrectly. */
596 assert(reg == STATUS1 || reg == STATUS2 || reg == STATUS3);
597
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200598 uint8_t ro_bits = reg == STATUS1 ? SPI_SR_WIP : 0;
599
600 if (data->emu_chip == EMULATE_WINBOND_W25Q128FV) {
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200601 const bool srp0 = (data->emu_status[0] >> 7);
602 const bool srp1 = (data->emu_status[1] & 1);
603
604 const bool wp_active = (srp1 || (srp0 && data->hwwp));
605
606 if (wp_active) {
607 ro_bits = 0xff;
608 } else if (reg == STATUS2) {
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200609 /* SUS (bit_7) and (R) (bit_2). */
610 ro_bits = 0x84;
611 /* Once any of the lock bits (LB[1..3]) are set, they
612 can't be unset. */
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 /* Four reserved bits. */
618 ro_bits = 0x1b;
619 }
620 }
621
Nico Huber4203a472022-05-28 17:28:05 +0200622 if (data->emu_chip == EMULATE_SPANSION_S25FL128L) {
623 const bool srp0 = (data->emu_status[0] >> 7);
624 const bool srp1 = (data->emu_status[1] & 1);
625
626 const bool wp_active = (srp1 || (srp0 && data->hwwp));
627
628 if (wp_active) {
629 ro_bits = 0xff;
630 } else if (reg == STATUS2) {
631 /* SUS (bit_7) */
632 ro_bits = 0x80;
633 /* Once any of the lock bits (LB[0..3]) are set, they
634 can't be unset. */
635 ro_bits |= data->emu_status[1] & (1 << 2);
636 ro_bits |= data->emu_status[1] & (1 << 3);
637 ro_bits |= data->emu_status[1] & (1 << 4);
638 ro_bits |= data->emu_status[1] & (1 << 5);
639 } else if (reg == STATUS3) {
640 /* Two reserved bits. */
641 ro_bits = 0x11;
642 }
643 }
644
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200645 return ro_bits;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200646}
647
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200648static void update_write_protection(struct emu_data *data)
649{
Nico Huber4203a472022-05-28 17:28:05 +0200650 if (data->emu_chip != EMULATE_WINBOND_W25Q128FV &&
651 data->emu_chip != EMULATE_SPANSION_S25FL128L)
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200652 return;
653
654 const struct wp_bits bits = {
655 .srp = data->emu_status[0] >> 7,
656 .srl = data->emu_status[1] & 1,
657
658 .bp_bit_count = 3,
659 .bp =
660 {
661 (data->emu_status[0] >> 2) & 1,
662 (data->emu_status[0] >> 3) & 1,
663 (data->emu_status[0] >> 4) & 1
664 },
665
666 .tb_bit_present = true,
667 .tb = (data->emu_status[0] >> 5) & 1,
668
669 .sec_bit_present = true,
670 .sec = (data->emu_status[0] >> 6) & 1,
671
672 .cmp_bit_present = true,
673 .cmp = (data->emu_status[1] >> 6) & 1,
674 };
675
676 size_t start;
677 size_t len;
678 decode_range_spi25(&start, &len, &bits, data->emu_chip_size);
679
680 data->wp_start = start;
681 data->wp_end = start + len;
682}
683
684/* Checks whether range intersects a write-protected area of the flash if one is
685 * defined. */
686static bool is_write_protected(const struct emu_data *data, uint32_t start, uint32_t len)
687{
688 if (len == 0)
689 return false;
690
691 const uint32_t last = start + len - 1;
692 return (start < data->wp_end && last >= data->wp_start);
693}
694
695/* Returns non-zero on error. */
696static int write_flash_data(struct emu_data *data, uint32_t start, uint32_t len, const uint8_t *buf)
697{
698 if (is_write_protected(data, start, len)) {
699 msg_perr("At least part of the write range is write protected!\n");
700 return 1;
701 }
702
703 memcpy(data->flashchip_contents + start, buf, len);
Felix Singer2fb53b12022-08-19 03:29:32 +0200704 data->emu_modified = true;
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200705 return 0;
706}
707
708/* Returns non-zero on error. */
709static int erase_flash_data(struct emu_data *data, uint32_t start, uint32_t len)
710{
711 if (is_write_protected(data, start, len)) {
712 msg_perr("At least part of the erase range is write protected!\n");
713 return 1;
714 }
715
716 memset(data->flashchip_contents + start, 0xff, len);
Felix Singer2fb53b12022-08-19 03:29:32 +0200717 data->emu_modified = true;
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200718 return 0;
719}
720
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000721static int emulate_spi_chip_response(unsigned int writecnt,
722 unsigned int readcnt,
723 const unsigned char *writearr,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000724 unsigned char *readarr,
725 struct emu_data *data)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000726{
Stefan Tauner0b9df972012-05-07 22:12:16 +0000727 unsigned int offs, i, toread;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200728 uint8_t ro_bits;
Nico Huberbbccdb22022-05-28 16:48:26 +0200729 bool wrsr_ext2, wrsr_ext3;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000730 static int unsigned aai_offs;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000731 const unsigned char sst25vf040_rems_response[2] = {0xbf, 0x44};
732 const unsigned char sst25vf032b_rems_response[2] = {0xbf, 0x4a};
733 const unsigned char mx25l6436_rems_response[2] = {0xc2, 0x16};
Nico Huberf9632d82019-01-20 11:23:49 +0100734 const unsigned char w25q128fv_rems_response[2] = {0xef, 0x17};
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000735
736 if (writecnt == 0) {
737 msg_perr("No command sent to the chip!\n");
738 return 1;
739 }
Paul Menzelac427b22012-02-16 21:07:07 +0000740 /* spi_blacklist has precedence over spi_ignorelist. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000741 for (i = 0; i < data->spi_blacklist_size; i++) {
742 if (writearr[0] == data->spi_blacklist[i]) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000743 msg_pdbg("Refusing blacklisted SPI command 0x%02x\n",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000744 data->spi_blacklist[i]);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000745 return SPI_INVALID_OPCODE;
746 }
747 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000748 for (i = 0; i < data->spi_ignorelist_size; i++) {
749 if (writearr[0] == data->spi_ignorelist[i]) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000750 msg_cdbg("Ignoring ignorelisted SPI command 0x%02x\n",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000751 data->spi_ignorelist[i]);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000752 /* Return success because the command does not fail,
753 * it is simply ignored.
754 */
755 return 0;
756 }
757 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000758
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200759 if (data->emu_max_aai_size && (data->emu_status[0] & SPI_SR_AAI)) {
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000760 if (writearr[0] != JEDEC_AAI_WORD_PROGRAM &&
761 writearr[0] != JEDEC_WRDI &&
762 writearr[0] != JEDEC_RDSR) {
763 msg_perr("Forbidden opcode (0x%02x) attempted during "
764 "AAI sequence!\n", writearr[0]);
765 return 0;
766 }
767 }
768
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000769 switch (writearr[0]) {
770 case JEDEC_RES:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000771 if (writecnt < JEDEC_RES_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000772 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000773 /* offs calculation is only needed for SST chips which treat RES like REMS. */
774 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
775 offs += writecnt - JEDEC_REMS_OUTSIZE;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000776 switch (data->emu_chip) {
Nico Huberafb5dd02026-02-15 13:26:10 +0100777 case EMULATE_ST_M25P10_A:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000778 case EMULATE_ST_M25P10_RES:
779 if (readcnt > 0)
780 memset(readarr, 0x10, readcnt);
781 break;
782 case EMULATE_SST_SST25VF040_REMS:
783 for (i = 0; i < readcnt; i++)
784 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
785 break;
786 case EMULATE_SST_SST25VF032B:
787 for (i = 0; i < readcnt; i++)
788 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
789 break;
790 case EMULATE_MACRONIX_MX25L6436:
791 if (readcnt > 0)
792 memset(readarr, 0x16, readcnt);
793 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100794 case EMULATE_WINBOND_W25Q128FV:
795 if (readcnt > 0)
796 memset(readarr, 0x17, readcnt);
797 break;
Nico Huber4203a472022-05-28 17:28:05 +0200798 case EMULATE_SPANSION_S25FL128L:
799 if (readcnt > 0)
800 readarr[0] = 0x60;
801 if (readcnt > 1)
802 readarr[1] = 0x18;
803 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000804 default: /* ignore */
805 break;
806 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000807 break;
808 case JEDEC_REMS:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000809 /* REMS response has wraparound and uses an address parameter. */
810 if (writecnt < JEDEC_REMS_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000811 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000812 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
813 offs += writecnt - JEDEC_REMS_OUTSIZE;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000814 switch (data->emu_chip) {
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000815 case EMULATE_SST_SST25VF040_REMS:
816 for (i = 0; i < readcnt; i++)
817 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
818 break;
819 case EMULATE_SST_SST25VF032B:
820 for (i = 0; i < readcnt; i++)
821 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
822 break;
823 case EMULATE_MACRONIX_MX25L6436:
824 for (i = 0; i < readcnt; i++)
825 readarr[i] = mx25l6436_rems_response[(offs + i) % 2];
826 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100827 case EMULATE_WINBOND_W25Q128FV:
828 for (i = 0; i < readcnt; i++)
829 readarr[i] = w25q128fv_rems_response[(offs + i) % 2];
830 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000831 default: /* ignore */
832 break;
833 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000834 break;
835 case JEDEC_RDID:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000836 switch (data->emu_chip) {
Nico Huberafb5dd02026-02-15 13:26:10 +0100837 case EMULATE_ST_M25P10_A:
838 if (readcnt > 0)
839 readarr[0] = 0x20;
840 if (readcnt > 1)
841 readarr[1] = 0x20;
842 if (readcnt > 2)
843 readarr[2] = 0x11;
844 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000845 case EMULATE_SST_SST25VF032B:
846 if (readcnt > 0)
847 readarr[0] = 0xbf;
848 if (readcnt > 1)
849 readarr[1] = 0x25;
850 if (readcnt > 2)
851 readarr[2] = 0x4a;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000852 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000853 case EMULATE_MACRONIX_MX25L6436:
854 if (readcnt > 0)
855 readarr[0] = 0xc2;
856 if (readcnt > 1)
857 readarr[1] = 0x20;
858 if (readcnt > 2)
859 readarr[2] = 0x17;
860 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100861 case EMULATE_WINBOND_W25Q128FV:
862 if (readcnt > 0)
863 readarr[0] = 0xef;
864 if (readcnt > 1)
865 readarr[1] = 0x40;
866 if (readcnt > 2)
867 readarr[2] = 0x18;
868 break;
Nico Huber4203a472022-05-28 17:28:05 +0200869 case EMULATE_SPANSION_S25FL128L:
870 if (readcnt > 0)
871 readarr[0] = 0x01;
872 if (readcnt > 1)
873 readarr[1] = 0x60;
874 if (readcnt > 2)
875 readarr[2] = 0x18;
876 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000877 default: /* ignore */
878 break;
879 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000880 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000881 case JEDEC_RDSR:
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200882 memset(readarr, data->emu_status[0], readcnt);
883 break;
884 case JEDEC_RDSR2:
885 if (data->emu_status_len >= 2)
886 memset(readarr, data->emu_status[1], readcnt);
887 break;
888 case JEDEC_RDSR3:
889 if (data->emu_status_len >= 3)
890 memset(readarr, data->emu_status[2], readcnt);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000891 break;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000892 /* FIXME: this should be chip-specific. */
893 case JEDEC_EWSR:
894 case JEDEC_WREN:
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200895 data->emu_status[0] |= SPI_SR_WEL;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000896 break;
897 case JEDEC_WRSR:
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200898 if (!(data->emu_status[0] & SPI_SR_WEL)) {
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000899 msg_perr("WRSR attempted, but WEL is 0!\n");
900 break;
901 }
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200902
Nico Huberbbccdb22022-05-28 16:48:26 +0200903 wrsr_ext2 = (writecnt == 3 && data->emu_wrsr_ext2);
904 wrsr_ext3 = (writecnt == 4 && data->emu_wrsr_ext3);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200905
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000906 /* FIXME: add some reasonable simulation of the busy flag */
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200907
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200908 ro_bits = get_reg_ro_bit_mask(data, STATUS1);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200909 data->emu_status[0] &= ro_bits;
910 data->emu_status[0] |= writearr[1] & ~ro_bits;
Nico Huberbbccdb22022-05-28 16:48:26 +0200911 if (wrsr_ext2 || wrsr_ext3) {
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200912 ro_bits = get_reg_ro_bit_mask(data, STATUS2);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200913 data->emu_status[1] &= ro_bits;
914 data->emu_status[1] |= writearr[2] & ~ro_bits;
915 }
Nico Huberbbccdb22022-05-28 16:48:26 +0200916 if (wrsr_ext3) {
917 ro_bits = get_reg_ro_bit_mask(data, STATUS3);
918 data->emu_status[2] &= ro_bits;
919 data->emu_status[2] |= writearr[3] & ~ro_bits;
920 }
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200921
Nico Huberbbccdb22022-05-28 16:48:26 +0200922 if (wrsr_ext3)
923 msg_pdbg2("WRSR wrote 0x%02x%02x%02x.\n", data->emu_status[2], data->emu_status[1], data->emu_status[0]);
924 else if (wrsr_ext2)
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200925 msg_pdbg2("WRSR wrote 0x%02x%02x.\n", data->emu_status[1], data->emu_status[0]);
926 else
927 msg_pdbg2("WRSR wrote 0x%02x.\n", data->emu_status[0]);
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200928
929 update_write_protection(data);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200930 break;
931 case JEDEC_WRSR2:
932 if (data->emu_status_len < 2)
933 break;
934 if (!(data->emu_status[0] & SPI_SR_WEL)) {
935 msg_perr("WRSR2 attempted, but WEL is 0!\n");
936 break;
937 }
938
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200939 ro_bits = get_reg_ro_bit_mask(data, STATUS2);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200940 data->emu_status[1] &= ro_bits;
941 data->emu_status[1] |= (writearr[1] & ~ro_bits);
942
943 msg_pdbg2("WRSR2 wrote 0x%02x.\n", data->emu_status[1]);
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200944
945 update_write_protection(data);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200946 break;
947 case JEDEC_WRSR3:
948 if (data->emu_status_len < 3)
949 break;
950 if (!(data->emu_status[0] & SPI_SR_WEL)) {
951 msg_perr("WRSR3 attempted, but WEL is 0!\n");
952 break;
953 }
954
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200955 ro_bits = get_reg_ro_bit_mask(data, STATUS3);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200956 data->emu_status[2] &= ro_bits;
957 data->emu_status[2] |= (writearr[1] & ~ro_bits);
958
959 msg_pdbg2("WRSR3 wrote 0x%02x.\n", data->emu_status[2]);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000960 break;
961 case JEDEC_READ:
962 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
963 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000964 offs %= data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000965 if (readcnt > 0)
Edward O'Callaghan94250222021-05-20 20:34:02 +1000966 memcpy(readarr, data->flashchip_contents + offs, readcnt);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000967 break;
968 case JEDEC_BYTE_PROGRAM:
969 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
970 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000971 offs %= data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000972 if (writecnt < 5) {
973 msg_perr("BYTE PROGRAM size too short!\n");
974 return 1;
975 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000976 if (writecnt - 4 > data->emu_max_byteprogram_size) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000977 msg_perr("Max BYTE PROGRAM size exceeded!\n");
978 return 1;
979 }
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200980 if (write_flash_data(data, offs, writecnt - 4, writearr + 4)) {
981 msg_perr("Failed to program flash!\n");
982 return 1;
983 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000984 break;
985 case JEDEC_AAI_WORD_PROGRAM:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000986 if (!data->emu_max_aai_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000987 break;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200988 if (!(data->emu_status[0] & SPI_SR_AAI)) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000989 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
990 msg_perr("Initial AAI WORD PROGRAM size too "
991 "short!\n");
992 return 1;
993 }
994 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
995 msg_perr("Initial AAI WORD PROGRAM size too "
996 "long!\n");
997 return 1;
998 }
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200999 data->emu_status[0] |= SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001000 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
1001 writearr[3];
1002 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +10001003 aai_offs %= data->emu_chip_size;
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +02001004 if (write_flash_data(data, aai_offs, 2, writearr + 4)) {
1005 msg_perr("Failed to program flash!\n");
1006 return 1;
1007 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001008 aai_offs += 2;
1009 } else {
1010 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
1011 msg_perr("Continuation AAI WORD PROGRAM size "
1012 "too short!\n");
1013 return 1;
1014 }
1015 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
1016 msg_perr("Continuation AAI WORD PROGRAM size "
1017 "too long!\n");
1018 return 1;
1019 }
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +02001020 if (write_flash_data(data, aai_offs, 2, writearr + 1)) {
1021 msg_perr("Failed to program flash!\n");
1022 return 1;
1023 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001024 aai_offs += 2;
1025 }
1026 break;
1027 case JEDEC_WRDI:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001028 if (data->emu_max_aai_size)
Sergii Dmytruk59151a42021-11-08 00:05:12 +02001029 data->emu_status[0] &= ~SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001030 break;
1031 case JEDEC_SE:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001032 if (!data->emu_jedec_se_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001033 break;
1034 if (writecnt != JEDEC_SE_OUTSIZE) {
1035 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
1036 return 1;
1037 }
1038 if (readcnt != JEDEC_SE_INSIZE) {
1039 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
1040 return 1;
1041 }
1042 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +10001043 if (offs & (data->emu_jedec_se_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +00001044 msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +10001045 offs &= ~(data->emu_jedec_se_size - 1);
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +02001046 if (erase_flash_data(data, offs, data->emu_jedec_se_size)) {
1047 msg_perr("Failed to erase flash!\n");
1048 return 1;
1049 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001050 break;
1051 case JEDEC_BE_52:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001052 if (!data->emu_jedec_be_52_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001053 break;
1054 if (writecnt != JEDEC_BE_52_OUTSIZE) {
1055 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
1056 return 1;
1057 }
1058 if (readcnt != JEDEC_BE_52_INSIZE) {
1059 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
1060 return 1;
1061 }
1062 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +10001063 if (offs & (data->emu_jedec_be_52_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +00001064 msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +10001065 offs &= ~(data->emu_jedec_be_52_size - 1);
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +02001066 if (erase_flash_data(data, offs, data->emu_jedec_be_52_size)) {
1067 msg_perr("Failed to erase flash!\n");
1068 return 1;
1069 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001070 break;
1071 case JEDEC_BE_D8:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001072 if (!data->emu_jedec_be_d8_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001073 break;
1074 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
1075 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
1076 return 1;
1077 }
1078 if (readcnt != JEDEC_BE_D8_INSIZE) {
1079 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
1080 return 1;
1081 }
1082 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +10001083 if (offs & (data->emu_jedec_be_d8_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +00001084 msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +10001085 offs &= ~(data->emu_jedec_be_d8_size - 1);
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +02001086 if (erase_flash_data(data, offs, data->emu_jedec_be_d8_size)) {
1087 msg_perr("Failed to erase flash!\n");
1088 return 1;
1089 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001090 break;
1091 case JEDEC_CE_60:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001092 if (!data->emu_jedec_ce_60_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001093 break;
1094 if (writecnt != JEDEC_CE_60_OUTSIZE) {
1095 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
1096 return 1;
1097 }
1098 if (readcnt != JEDEC_CE_60_INSIZE) {
1099 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
1100 return 1;
1101 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +00001102 /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001103 /* emu_jedec_ce_60_size is emu_chip_size. */
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +02001104 if (erase_flash_data(data, 0, data->emu_jedec_ce_60_size)) {
1105 msg_perr("Failed to erase flash!\n");
1106 return 1;
1107 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001108 break;
1109 case JEDEC_CE_C7:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001110 if (!data->emu_jedec_ce_c7_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001111 break;
1112 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
1113 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
1114 return 1;
1115 }
1116 if (readcnt != JEDEC_CE_C7_INSIZE) {
1117 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
1118 return 1;
1119 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +00001120 /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001121 /* emu_jedec_ce_c7_size is emu_chip_size. */
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +02001122 if (erase_flash_data(data, 0, data->emu_jedec_ce_c7_size)) {
1123 msg_perr("Failed to erase flash!\n");
1124 return 1;
1125 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001126 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +00001127 case JEDEC_SFDP:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001128 if (data->emu_chip != EMULATE_MACRONIX_MX25L6436)
Stefan Tauner0b9df972012-05-07 22:12:16 +00001129 break;
1130 if (writecnt < 4)
1131 break;
1132 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
1133
1134 /* SFDP expects one dummy byte after the address. */
1135 if (writecnt == 4) {
1136 /* The dummy byte was not written, make sure it is read instead.
1137 * Shifting and shortening the read array does achieve this goal.
1138 */
1139 readarr++;
1140 readcnt--;
1141 } else {
1142 /* The response is shifted if more than 5 bytes are written, because SFDP data is
1143 * already shifted out by the chip while those superfluous bytes are written. */
1144 offs += writecnt - 5;
1145 }
1146
1147 /* The SFDP spec implies that the start address of an SFDP read may be truncated to fit in the
1148 * SFDP table address space, i.e. the start address may be wrapped around at SFDP table size.
1149 * This is a reasonable implementation choice in hardware because it saves a few gates. */
1150 if (offs >= sizeof(sfdp_table)) {
1151 msg_pdbg("Wrapping the start address around the SFDP table boundary (using 0x%x "
1152 "instead of 0x%x).\n", (unsigned int)(offs % sizeof(sfdp_table)), offs);
1153 offs %= sizeof(sfdp_table);
1154 }
1155 toread = min(sizeof(sfdp_table) - offs, readcnt);
1156 memcpy(readarr, sfdp_table + offs, toread);
1157 if (toread < readcnt)
1158 msg_pdbg("Crossing the SFDP table boundary in a single "
1159 "continuous chunk produces undefined results "
1160 "after that point.\n");
1161 break;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001162 default:
1163 /* No special response. */
1164 break;
1165 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +00001166 if (writearr[0] != JEDEC_WREN && writearr[0] != JEDEC_EWSR)
Sergii Dmytruk59151a42021-11-08 00:05:12 +02001167 data->emu_status[0] &= ~SPI_SR_WEL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001168 return 0;
1169}
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001170
Nico Huber610c1aa2023-02-15 02:56:05 +01001171static int dummy_spi_send_command(const struct spi_master *mst,
1172 unsigned int writecnt, unsigned int readcnt,
1173 const unsigned char *writearr, unsigned char *readarr)
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001174{
Nico Huber519be662018-12-23 20:03:35 +01001175 unsigned int i;
Nico Huber610c1aa2023-02-15 02:56:05 +01001176 struct emu_data *emu_data = mst->data;
Lachlan Bishopc753c402020-09-10 14:57:05 +10001177 if (!emu_data) {
1178 msg_perr("No data in flash context!\n");
1179 return 1;
1180 }
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001181
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +00001182 msg_pspew("%s:", __func__);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001183
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +00001184 msg_pspew(" writing %u bytes:", writecnt);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001185 for (i = 0; i < writecnt; i++)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +00001186 msg_pspew(" 0x%02x", writearr[i]);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001187
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001188 /* Response for unknown commands and missing chip is 0xff. */
1189 memset(readarr, 0xff, readcnt);
Lachlan Bishopc753c402020-09-10 14:57:05 +10001190 switch (emu_data->emu_chip) {
Nico Huberafb5dd02026-02-15 13:26:10 +01001191 case EMULATE_ST_M25P10_A:
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001192 case EMULATE_ST_M25P10_RES:
1193 case EMULATE_SST_SST25VF040_REMS:
1194 case EMULATE_SST_SST25VF032B:
Stefan Tauner0b9df972012-05-07 22:12:16 +00001195 case EMULATE_MACRONIX_MX25L6436:
Nico Huberf9632d82019-01-20 11:23:49 +01001196 case EMULATE_WINBOND_W25Q128FV:
Nico Huber4203a472022-05-28 17:28:05 +02001197 case EMULATE_SPANSION_S25FL128L:
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001198 if (emulate_spi_chip_response(writecnt, readcnt, writearr,
Lachlan Bishopc753c402020-09-10 14:57:05 +10001199 readarr, emu_data)) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +00001200 msg_pdbg("Invalid command sent to flash chip!\n");
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001201 return 1;
1202 }
1203 break;
1204 default:
1205 break;
1206 }
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +00001207 msg_pspew(" reading %u bytes:", readcnt);
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001208 for (i = 0; i < readcnt; i++)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001209 msg_pspew(" 0x%02x", readarr[i]);
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +00001210 msg_pspew("\n");
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001211 return 0;
1212}
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +00001213
Mark Marshallf20b7be2014-05-09 21:16:21 +00001214static 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 +00001215{
Nico Huber9a11cbf2023-01-13 01:19:07 +01001216 const struct emu_data *const data = flash->mst.spi->data;
Edward O'Callaghanb1313422021-05-20 20:27:59 +10001217 return spi_write_chunked(flash, buf, start, len, data->spi_write_256_chunksize);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001218}
Thomas Heijligencc853d82021-05-04 15:32:17 +02001219
Nikolai Artemieve7a41e32022-11-28 17:40:56 +11001220static bool dummy_spi_probe_opcode(const struct flashctx *flash, uint8_t opcode)
Aarya Chaumal0cea7532022-07-04 18:21:50 +05301221{
1222 size_t i;
Nico Huber9a11cbf2023-01-13 01:19:07 +01001223 const struct emu_data *emu_data = flash->mst.spi->data;
Aarya Chaumal0cea7532022-07-04 18:21:50 +05301224 for (i = 0; i < emu_data->spi_blacklist_size; i++) {
1225 if (emu_data->spi_blacklist[i] == opcode)
1226 return false;
1227 }
1228 return true;
1229}
1230
Thomas Heijligencc853d82021-05-04 15:32:17 +02001231const struct programmer_entry programmer_dummy = {
1232 .name = "dummy",
1233 .type = OTHER,
1234 /* FIXME */
1235 .devs.note = "Dummy device, does nothing and logs all accesses\n",
1236 .init = dummy_init,
Thomas Heijligencc853d82021-05-04 15:32:17 +02001237};