blob: 4a86dc47388427c0f903ce517f50630b114f80b4 [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>
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000025#include "flash.h"
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +000026#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000027#include "programmer.h"
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000028
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000029#include "spi.h"
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +020030#include "writeprotect.h"
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000031
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000032enum emu_chip {
33 EMULATE_NONE,
Nico Huberafb5dd02026-02-15 13:26:10 +010034 EMULATE_ST_M25P10_A,
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000035 EMULATE_ST_M25P10_RES,
36 EMULATE_SST_SST25VF040_REMS,
37 EMULATE_SST_SST25VF032B,
Stefan Tauner0b9df972012-05-07 22:12:16 +000038 EMULATE_MACRONIX_MX25L6436,
Nico Huberf9632d82019-01-20 11:23:49 +010039 EMULATE_WINBOND_W25Q128FV,
Nico Huber4203a472022-05-28 17:28:05 +020040 EMULATE_SPANSION_S25FL128L,
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000041};
Stefan Tauner0b9df972012-05-07 22:12:16 +000042
Lachlan Bishopc753c402020-09-10 14:57:05 +100043struct emu_data {
44 enum emu_chip emu_chip;
45 char *emu_persistent_image;
46 unsigned int emu_chip_size;
Sergii Dmytruk59151a42021-11-08 00:05:12 +020047 /* Note: W25Q128FV doesn't change value of SR2 if it's not provided, but
48 * even its previous generations do, so don't forget to update
Nico Huberbbccdb22022-05-28 16:48:26 +020049 * WRSR code on enabling WRSR_EXT2 for more chips. */
50 bool emu_wrsr_ext2;
51 bool emu_wrsr_ext3;
Felix Singer2fb53b12022-08-19 03:29:32 +020052 bool emu_modified; /* is the image modified since reading it? */
Sergii Dmytruk59151a42021-11-08 00:05:12 +020053 uint8_t emu_status[3];
54 uint8_t emu_status_len; /* number of emulated status registers */
Lachlan Bishopc753c402020-09-10 14:57:05 +100055 unsigned int emu_max_byteprogram_size;
56 unsigned int emu_max_aai_size;
57 unsigned int emu_jedec_se_size;
58 unsigned int emu_jedec_be_52_size;
59 unsigned int emu_jedec_be_d8_size;
60 unsigned int emu_jedec_ce_60_size;
61 unsigned int emu_jedec_ce_c7_size;
62 unsigned char spi_blacklist[256];
63 unsigned char spi_ignorelist[256];
64 unsigned int spi_blacklist_size;
65 unsigned int spi_ignorelist_size;
Edward O'Callaghan94250222021-05-20 20:34:02 +100066
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +020067 bool hwwp; /* state of hardware write protection */
68 /* wp_start == wp_end when write-protection is disabled */
69 uint32_t wp_start;
70 uint32_t wp_end;
71
Edward O'Callaghanb1313422021-05-20 20:27:59 +100072 unsigned int spi_write_256_chunksize;
Edward O'Callaghan94250222021-05-20 20:34:02 +100073 uint8_t *flashchip_contents;
Lachlan Bishopc753c402020-09-10 14:57:05 +100074};
75
Stefan Tauner0b9df972012-05-07 22:12:16 +000076/* A legit complete SFDP table based on the MX25L6436E (rev. 1.8) datasheet. */
Stefan Tauner67d163d2013-01-15 17:37:48 +000077static const uint8_t sfdp_table[] = {
Stefan Tauner0b9df972012-05-07 22:12:16 +000078 0x53, 0x46, 0x44, 0x50, // @0x00: SFDP signature
79 0x00, 0x01, 0x01, 0xFF, // @0x04: revision 1.0, 2 headers
80 0x00, 0x00, 0x01, 0x09, // @0x08: JEDEC SFDP header rev. 1.0, 9 DW long
81 0x1C, 0x00, 0x00, 0xFF, // @0x0C: PTP0 = 0x1C (instead of 0x30)
82 0xC2, 0x00, 0x01, 0x04, // @0x10: Macronix header rev. 1.0, 4 DW long
83 0x48, 0x00, 0x00, 0xFF, // @0x14: PTP1 = 0x48 (instead of 0x60)
84 0xFF, 0xFF, 0xFF, 0xFF, // @0x18: hole.
85 0xE5, 0x20, 0xC9, 0xFF, // @0x1C: SFDP parameter table start
86 0xFF, 0xFF, 0xFF, 0x03, // @0x20
87 0x00, 0xFF, 0x08, 0x6B, // @0x24
88 0x08, 0x3B, 0x00, 0xFF, // @0x28
89 0xEE, 0xFF, 0xFF, 0xFF, // @0x2C
90 0xFF, 0xFF, 0x00, 0x00, // @0x30
91 0xFF, 0xFF, 0x00, 0xFF, // @0x34
92 0x0C, 0x20, 0x0F, 0x52, // @0x38
93 0x10, 0xD8, 0x00, 0xFF, // @0x3C: SFDP parameter table end
94 0xFF, 0xFF, 0xFF, 0xFF, // @0x40: hole.
95 0xFF, 0xFF, 0xFF, 0xFF, // @0x44: hole.
96 0x00, 0x36, 0x00, 0x27, // @0x48: Macronix parameter table start
97 0xF4, 0x4F, 0xFF, 0xFF, // @0x4C
98 0xD9, 0xC8, 0xFF, 0xFF, // @0x50
99 0xFF, 0xFF, 0xFF, 0xFF, // @0x54: Macronix parameter table end
100};
101
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000102
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000103
Nico Huber610c1aa2023-02-15 02:56:05 +0100104static int dummy_spi_send_command(const struct spi_master *, unsigned int writecnt, unsigned int readcnt,
Mark Marshallf20b7be2014-05-09 21:16:21 +0000105 const unsigned char *writearr, unsigned char *readarr);
106static int dummy_spi_write_256(struct flashctx *flash, const uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000107 unsigned int start, unsigned int len);
Mark Marshallf20b7be2014-05-09 21:16:21 +0000108static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr);
109static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr);
110static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr);
111static void dummy_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len);
112static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr);
113static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr);
114static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr);
115static void dummy_chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len);
Nikolai Artemieve7a41e32022-11-28 17:40:56 +1100116static bool dummy_spi_probe_opcode(const struct flashctx *flash, uint8_t opcode);
Nico Huber0e76d992023-01-12 20:22:55 +0100117static void *dummy_map(const char *descr, uintptr_t phys_addr, size_t len);
118static void dummy_unmap(void *virt_addr, size_t len);
Michael Karcherb9dbe482011-05-11 17:07:07 +0000119
Nico Huber03f3a6d2021-05-11 17:53:34 +0200120static const struct spi_master spi_master_dummyflasher = {
Nico Huber1cf407b2017-11-10 20:18:23 +0100121 .features = SPI_MASTER_4BA,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000122 .max_data_read = MAX_DATA_READ_UNLIMITED,
123 .max_data_write = MAX_DATA_UNSPECIFIED,
124 .command = dummy_spi_send_command,
125 .multicommand = default_spi_send_multicommand,
126 .read = default_spi_read,
127 .write_256 = dummy_spi_write_256,
Aarya Chaumal0cea7532022-07-04 18:21:50 +0530128 .probe_opcode = dummy_spi_probe_opcode,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000129};
David Hendricks8bb20212011-06-14 01:35:36 +0000130
Edward O'Callaghanf7504a92021-05-20 20:21:13 +1000131static const struct par_master par_master_dummyflasher = {
Thomas Heijligen43040f22022-06-23 14:38:35 +0200132 .chip_readb = dummy_chip_readb,
133 .chip_readw = dummy_chip_readw,
134 .chip_readl = dummy_chip_readl,
135 .chip_readn = dummy_chip_readn,
136 .chip_writeb = dummy_chip_writeb,
137 .chip_writew = dummy_chip_writew,
138 .chip_writel = dummy_chip_writel,
139 .chip_writen = dummy_chip_writen,
Nico Huber0e76d992023-01-12 20:22:55 +0100140 .map_flash = dummy_map,
141 .unmap_flash = dummy_unmap,
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000142};
143
David Hendricks8bb20212011-06-14 01:35:36 +0000144static int dummy_shutdown(void *data)
145{
146 msg_pspew("%s\n", __func__);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000147 struct emu_data *emu_data = (struct emu_data *)data;
148 if (emu_data->emu_chip != EMULATE_NONE) {
149 if (emu_data->emu_persistent_image && emu_data->emu_modified) {
150 msg_pdbg("Writing %s\n", emu_data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000151 write_buf_to_file(emu_data->flashchip_contents,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000152 emu_data->emu_chip_size,
153 emu_data->emu_persistent_image);
David Hendricks8bb20212011-06-14 01:35:36 +0000154 }
Angel Pons02b9ae22021-05-25 12:46:43 +0200155 free(emu_data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000156 free(emu_data->flashchip_contents);
David Hendricks8bb20212011-06-14 01:35:36 +0000157 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000158 free(data);
David Hendricks8bb20212011-06-14 01:35:36 +0000159 return 0;
160}
161
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000162static int init_data(struct emu_data *data, enum chipbustype *dummy_buses_supported)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000163{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000164 char *bustext = NULL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000165 char *tmp = NULL;
Nico Huber519be662018-12-23 20:03:35 +0100166 unsigned int i;
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000167 char *endptr;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000168 char *status = NULL;
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000169
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000170 bustext = extract_programmer_param("bus");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000171 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
172 if (!bustext)
173 bustext = strdup("parallel+lpc+fwh+spi");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000174 /* Convert the parameters to lowercase. */
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000175 tolower_string(bustext);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000176
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000177 *dummy_buses_supported = BUS_NONE;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000178 if (strstr(bustext, "parallel")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000179 *dummy_buses_supported |= BUS_PARALLEL;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000180 msg_pdbg("Enabling support for %s flash.\n", "parallel");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000181 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000182 if (strstr(bustext, "lpc")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000183 *dummy_buses_supported |= BUS_LPC;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000184 msg_pdbg("Enabling support for %s flash.\n", "LPC");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000185 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000186 if (strstr(bustext, "fwh")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000187 *dummy_buses_supported |= BUS_FWH;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000188 msg_pdbg("Enabling support for %s flash.\n", "FWH");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000189 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000190 if (strstr(bustext, "spi")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000191 *dummy_buses_supported |= BUS_SPI;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000192 msg_pdbg("Enabling support for %s flash.\n", "SPI");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000193 }
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000194 if (*dummy_buses_supported == BUS_NONE)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000195 msg_pdbg("Support for all flash bus types disabled.\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000196 free(bustext);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000197
198 tmp = extract_programmer_param("spi_write_256_chunksize");
199 if (tmp) {
Edward O'Callaghanb1313422021-05-20 20:27:59 +1000200 data->spi_write_256_chunksize = strtoul(tmp, &endptr, 0);
201 if (*endptr != '\0' || data->spi_write_256_chunksize < 1) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000202 msg_perr("invalid spi_write_256_chunksize\n");
Edward O'Callaghanc785f882021-05-23 22:14:36 +1000203 free(tmp);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000204 return 1;
205 }
Edward O'Callaghanc785f882021-05-23 22:14:36 +1000206 free(tmp);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000207 }
208
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000209 tmp = extract_programmer_param("spi_blacklist");
210 if (tmp) {
211 i = strlen(tmp);
212 if (!strncmp(tmp, "0x", 2)) {
213 i -= 2;
214 memmove(tmp, tmp + 2, i + 1);
215 }
216 if ((i > 512) || (i % 2)) {
217 msg_perr("Invalid SPI command blacklist length\n");
218 free(tmp);
219 return 1;
220 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000221 data->spi_blacklist_size = i / 2;
222 for (i = 0; i < data->spi_blacklist_size * 2; i++) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000223 if (!isxdigit((unsigned char)tmp[i])) {
224 msg_perr("Invalid char \"%c\" in SPI command "
225 "blacklist\n", tmp[i]);
226 free(tmp);
227 return 1;
228 }
229 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000230 for (i = 0; i < data->spi_blacklist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000231 unsigned int tmp2;
232 /* SCNx8 is apparently not supported by MSVC (and thus
233 * MinGW), so work around it with an extra variable
234 */
235 sscanf(tmp + i * 2, "%2x", &tmp2);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000236 data->spi_blacklist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000237 }
238 msg_pdbg("SPI blacklist is ");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000239 for (i = 0; i < data->spi_blacklist_size; i++)
240 msg_pdbg("%02x ", data->spi_blacklist[i]);
241 msg_pdbg(", size %u\n", data->spi_blacklist_size);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000242 }
243 free(tmp);
244
245 tmp = extract_programmer_param("spi_ignorelist");
246 if (tmp) {
247 i = strlen(tmp);
248 if (!strncmp(tmp, "0x", 2)) {
249 i -= 2;
250 memmove(tmp, tmp + 2, i + 1);
251 }
252 if ((i > 512) || (i % 2)) {
253 msg_perr("Invalid SPI command ignorelist length\n");
254 free(tmp);
255 return 1;
256 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000257 data->spi_ignorelist_size = i / 2;
258 for (i = 0; i < data->spi_ignorelist_size * 2; i++) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000259 if (!isxdigit((unsigned char)tmp[i])) {
260 msg_perr("Invalid char \"%c\" in SPI command "
261 "ignorelist\n", tmp[i]);
262 free(tmp);
263 return 1;
264 }
265 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000266 for (i = 0; i < data->spi_ignorelist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000267 unsigned int tmp2;
268 /* SCNx8 is apparently not supported by MSVC (and thus
269 * MinGW), so work around it with an extra variable
270 */
271 sscanf(tmp + i * 2, "%2x", &tmp2);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000272 data->spi_ignorelist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000273 }
274 msg_pdbg("SPI ignorelist is ");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000275 for (i = 0; i < data->spi_ignorelist_size; i++)
276 msg_pdbg("%02x ", data->spi_ignorelist[i]);
277 msg_pdbg(", size %u\n", data->spi_ignorelist_size);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000278 }
279 free(tmp);
280
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200281 tmp = extract_programmer_param("hwwp");
282 if (tmp) {
283 if (!strcmp(tmp, "yes")) {
284 msg_pdbg("Emulated chip will have hardware WP enabled\n");
285 data->hwwp = true;
286 } else if (!strcmp(tmp, "no")) {
287 msg_pdbg("Emulated chip will have hardware WP disabled\n");
288 } else {
289 msg_perr("hwwp can be \"yes\" or \"no\"\n");
290 free(tmp);
291 return 1;
292 }
293 free(tmp);
294 }
295
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000296 tmp = extract_programmer_param("emulate");
297 if (!tmp) {
298 msg_pdbg("Not emulating any flash chip.\n");
299 /* Nothing else to do. */
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000300 return 0;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000301 }
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000302
Nico Huberafb5dd02026-02-15 13:26:10 +0100303 if (!strcmp(tmp, "M25P10-A")) {
304 data->emu_chip = EMULATE_ST_M25P10_A;
305 data->emu_chip_size = 128 * 1024;
306 data->emu_max_byteprogram_size = 256;
307 data->emu_max_aai_size = 0;
308 data->emu_status_len = 1;
309 data->emu_jedec_se_size = 0;
310 data->emu_jedec_be_52_size = 0;
311 data->emu_jedec_be_d8_size = 32 * 1024;
312 data->emu_jedec_ce_60_size = 0;
313 data->emu_jedec_ce_c7_size = data->emu_chip_size;
314 msg_pdbg("Emulating ST M25P10-A SPI flash chip (RDID, page "
315 "write)\n");
316 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000317 if (!strcmp(tmp, "M25P10.RES")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000318 data->emu_chip = EMULATE_ST_M25P10_RES;
319 data->emu_chip_size = 128 * 1024;
320 data->emu_max_byteprogram_size = 128;
321 data->emu_max_aai_size = 0;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200322 data->emu_status_len = 1;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000323 data->emu_jedec_se_size = 0;
324 data->emu_jedec_be_52_size = 0;
325 data->emu_jedec_be_d8_size = 32 * 1024;
326 data->emu_jedec_ce_60_size = 0;
327 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000328 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
329 "write)\n");
330 }
331 if (!strcmp(tmp, "SST25VF040.REMS")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000332 data->emu_chip = EMULATE_SST_SST25VF040_REMS;
333 data->emu_chip_size = 512 * 1024;
334 data->emu_max_byteprogram_size = 1;
335 data->emu_max_aai_size = 0;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200336 data->emu_status_len = 1;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000337 data->emu_jedec_se_size = 4 * 1024;
338 data->emu_jedec_be_52_size = 32 * 1024;
339 data->emu_jedec_be_d8_size = 0;
340 data->emu_jedec_ce_60_size = data->emu_chip_size;
341 data->emu_jedec_ce_c7_size = 0;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000342 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
343 "byte write)\n");
344 }
345 if (!strcmp(tmp, "SST25VF032B")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000346 data->emu_chip = EMULATE_SST_SST25VF032B;
347 data->emu_chip_size = 4 * 1024 * 1024;
348 data->emu_max_byteprogram_size = 1;
349 data->emu_max_aai_size = 2;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200350 data->emu_status_len = 1;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000351 data->emu_jedec_se_size = 4 * 1024;
352 data->emu_jedec_be_52_size = 32 * 1024;
353 data->emu_jedec_be_d8_size = 64 * 1024;
354 data->emu_jedec_ce_60_size = data->emu_chip_size;
355 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000356 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
357 "write)\n");
358 }
Stefan Tauner0b9df972012-05-07 22:12:16 +0000359 if (!strcmp(tmp, "MX25L6436")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000360 data->emu_chip = EMULATE_MACRONIX_MX25L6436;
361 data->emu_chip_size = 8 * 1024 * 1024;
362 data->emu_max_byteprogram_size = 256;
363 data->emu_max_aai_size = 0;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200364 data->emu_status_len = 1;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000365 data->emu_jedec_se_size = 4 * 1024;
366 data->emu_jedec_be_52_size = 32 * 1024;
367 data->emu_jedec_be_d8_size = 64 * 1024;
368 data->emu_jedec_ce_60_size = data->emu_chip_size;
369 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000370 msg_pdbg("Emulating Macronix MX25L6436 SPI flash chip (RDID, "
371 "SFDP)\n");
372 }
Nico Huberf9632d82019-01-20 11:23:49 +0100373 if (!strcmp(tmp, "W25Q128FV")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000374 data->emu_chip = EMULATE_WINBOND_W25Q128FV;
Nico Huberbbccdb22022-05-28 16:48:26 +0200375 data->emu_wrsr_ext2 = true;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000376 data->emu_chip_size = 16 * 1024 * 1024;
377 data->emu_max_byteprogram_size = 256;
378 data->emu_max_aai_size = 0;
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200379 data->emu_status_len = 3;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000380 data->emu_jedec_se_size = 4 * 1024;
381 data->emu_jedec_be_52_size = 32 * 1024;
382 data->emu_jedec_be_d8_size = 64 * 1024;
383 data->emu_jedec_ce_60_size = data->emu_chip_size;
384 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Nico Huberf9632d82019-01-20 11:23:49 +0100385 msg_pdbg("Emulating Winbond W25Q128FV SPI flash chip (RDID)\n");
386 }
Nico Huber4203a472022-05-28 17:28:05 +0200387 if (!strcmp(tmp, "S25FL128L")) {
388 data->emu_chip = EMULATE_SPANSION_S25FL128L;
389 data->emu_wrsr_ext2 = true;
390 data->emu_wrsr_ext3 = true;
391 data->emu_chip_size = 16 * 1024 * 1024;
392 data->emu_max_byteprogram_size = 256;
393 data->emu_max_aai_size = 0;
394 data->emu_status_len = 3;
395 data->emu_jedec_se_size = 4 * 1024;
396 data->emu_jedec_be_52_size = 32 * 1024;
397 data->emu_jedec_be_d8_size = 64 * 1024;
398 data->emu_jedec_ce_60_size = data->emu_chip_size;
399 data->emu_jedec_ce_c7_size = data->emu_chip_size;
400 msg_pdbg("Emulating Spansion S25FL128L SPI flash chip (RES, RDID, WP)\n");
401 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000402 if (data->emu_chip == EMULATE_NONE) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000403 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
404 free(tmp);
405 return 1;
406 }
407 free(tmp);
David Hendricks8bb20212011-06-14 01:35:36 +0000408
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000409 status = extract_programmer_param("spi_status");
410 if (status) {
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200411 unsigned int emu_status;
412
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000413 errno = 0;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200414 emu_status = strtoul(status, &endptr, 0);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000415 if (errno != 0 || status == endptr) {
Angel Ponsc2484642021-05-25 13:03:24 +0200416 free(status);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000417 msg_perr("Error: initial status register specified, "
418 "but the value could not be converted.\n");
419 return 1;
420 }
Angel Ponsc2484642021-05-25 13:03:24 +0200421 free(status);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200422
423 data->emu_status[0] = emu_status;
424 data->emu_status[1] = emu_status >> 8;
425 data->emu_status[2] = emu_status >> 16;
426
427 if (data->emu_status_len == 3) {
428 msg_pdbg("Initial status registers:\n"
429 "\tSR1 is set to 0x%02x\n"
430 "\tSR2 is set to 0x%02x\n"
431 "\tSR3 is set to 0x%02x\n",
432 data->emu_status[0], data->emu_status[1], data->emu_status[2]);
433 } else if (data->emu_status_len == 2) {
434 msg_pdbg("Initial status registers:\n"
435 "\tSR1 is set to 0x%02x\n"
436 "\tSR2 is set to 0x%02x\n",
437 data->emu_status[0], data->emu_status[1]);
438 } else {
439 msg_pdbg("Initial status register is set to 0x%02x.\n",
440 data->emu_status[0]);
441 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000442 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000443
Angel Pons328898a2021-05-25 12:56:18 +0200444 data->flashchip_contents = malloc(data->emu_chip_size);
445 if (!data->flashchip_contents) {
446 msg_perr("Out of memory!\n");
447 return 1;
448 }
449
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000450
451 return 0;
452}
453
Nico Hubere3a26882023-01-11 21:45:51 +0100454static int dummy_init(struct flashprog_programmer *const prog)
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000455{
Alexander Goncharov0d929fe2023-01-24 14:43:12 +0400456 int ret = 0;
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000457 struct stat image_stat;
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000458
Nico Huber4e9e99c2021-06-09 18:08:48 +0200459 struct emu_data *data = calloc(1, sizeof(*data));
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000460 if (!data) {
461 msg_perr("Out of memory!\n");
462 return 1;
463 }
464 data->emu_chip = EMULATE_NONE;
Edward O'Callaghanb1313422021-05-20 20:27:59 +1000465 data->spi_write_256_chunksize = 256;
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000466
467 msg_pspew("%s\n", __func__);
468
469 enum chipbustype dummy_buses_supported;
470 if (init_data(data, &dummy_buses_supported)) {
471 free(data);
472 return 1;
473 }
474
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000475 if (data->emu_chip == EMULATE_NONE) {
476 msg_pdbg("Not emulating any flash chip.\n");
477 /* Nothing else to do. */
478 goto dummy_init_out;
479 }
480
Lachlan Bishopc753c402020-09-10 14:57:05 +1000481 msg_pdbg("Filling fake flash chip with 0xff, size %i\n", data->emu_chip_size);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000482 memset(data->flashchip_contents, 0xff, data->emu_chip_size);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000483
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000484 /* Will be freed by shutdown function if necessary. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000485 data->emu_persistent_image = extract_programmer_param("image");
486 if (!data->emu_persistent_image) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000487 /* Nothing else to do. */
David Hendricks8bb20212011-06-14 01:35:36 +0000488 goto dummy_init_out;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000489 }
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000490 /* We will silently (in default verbosity) ignore the file if it does not exist (yet) or the size does
491 * not match the emulated chip. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000492 if (!stat(data->emu_persistent_image, &image_stat)) {
Stefan Tauner23e10b82016-01-23 16:16:49 +0000493 msg_pdbg("Found persistent image %s, %jd B ",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000494 data->emu_persistent_image, (intmax_t)image_stat.st_size);
495 if ((uintmax_t)image_stat.st_size == data->emu_chip_size) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000496 msg_pdbg("matches.\n");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000497 msg_pdbg("Reading %s\n", data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000498 if (read_buf_from_file(data->flashchip_contents, data->emu_chip_size,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000499 data->emu_persistent_image)) {
500 msg_perr("Unable to read %s\n", data->emu_persistent_image);
Angel Pons02b9ae22021-05-25 12:46:43 +0200501 free(data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000502 free(data->flashchip_contents);
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000503 free(data);
Jacob Garberca598da2019-08-12 10:44:17 -0600504 return 1;
505 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000506 } else {
507 msg_pdbg("doesn't match.\n");
508 }
509 }
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000510
David Hendricks8bb20212011-06-14 01:35:36 +0000511dummy_init_out:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000512 if (register_shutdown(dummy_shutdown, data)) {
Angel Pons02b9ae22021-05-25 12:46:43 +0200513 free(data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000514 free(data->flashchip_contents);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000515 free(data);
David Hendricks8bb20212011-06-14 01:35:36 +0000516 return 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000517 }
Edward O'Callaghan3fa321d2021-05-17 20:01:27 +1000518 if (dummy_buses_supported & BUS_NONSPI)
Alexander Goncharov0d929fe2023-01-24 14:43:12 +0400519 ret |= register_par_master(&par_master_dummyflasher,
520 dummy_buses_supported & BUS_NONSPI,
Nico Huber89569d62023-01-12 23:31:40 +0100521 0, data);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000522 if (dummy_buses_supported & BUS_SPI)
Nico Huber89569d62023-01-12 23:31:40 +0100523 ret |= register_spi_master(&spi_master_dummyflasher, 0, data);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000524
Alexander Goncharov0d929fe2023-01-24 14:43:12 +0400525 return ret;
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000526}
527
Thomas Heijligencc853d82021-05-04 15:32:17 +0200528static void *dummy_map(const char *descr, uintptr_t phys_addr, size_t len)
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000529{
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000530 msg_pspew("%s: Mapping %s, 0x%zx bytes at 0x%0*" PRIxPTR "\n",
Stefan Tauner0554ca52013-07-25 22:54:25 +0000531 __func__, descr, len, PRIxPTR_WIDTH, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000532 return (void *)phys_addr;
533}
534
Thomas Heijligencc853d82021-05-04 15:32:17 +0200535static void dummy_unmap(void *virt_addr, size_t len)
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000536{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000537 msg_pspew("%s: Unmapping 0x%zx bytes at %p\n", __func__, len, virt_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000538}
539
Mark Marshallf20b7be2014-05-09 21:16:21 +0000540static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000541{
Stefan Taunerc2333752013-07-13 23:31:37 +0000542 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%02x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000543}
544
Mark Marshallf20b7be2014-05-09 21:16:21 +0000545static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000546{
Stefan Taunerc2333752013-07-13 23:31:37 +0000547 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%04x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000548}
549
Mark Marshallf20b7be2014-05-09 21:16:21 +0000550static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, 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 ", val=0x%08x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000553}
554
Mark Marshallf20b7be2014-05-09 21:16:21 +0000555static 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 +0000556{
557 size_t i;
Stefan Tauner0554ca52013-07-25 22:54:25 +0000558 msg_pspew("%s: addr=0x%" PRIxPTR ", len=0x%zx, writing data (hex):", __func__, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000559 for (i = 0; i < len; i++) {
560 if ((i % 16) == 0)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000561 msg_pspew("\n");
562 msg_pspew("%02x ", buf[i]);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000563 }
564}
565
Mark Marshallf20b7be2014-05-09 21:16:21 +0000566static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000567{
Stefan Taunerc2333752013-07-13 23:31:37 +0000568 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000569 return 0xff;
570}
571
Mark Marshallf20b7be2014-05-09 21:16:21 +0000572static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000573{
Stefan Taunerc2333752013-07-13 23:31:37 +0000574 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000575 return 0xffff;
576}
577
Mark Marshallf20b7be2014-05-09 21:16:21 +0000578static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000579{
Stefan Taunerc2333752013-07-13 23:31:37 +0000580 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffffffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000581 return 0xffffffff;
582}
583
Mark Marshallf20b7be2014-05-09 21:16:21 +0000584static 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 +0000585{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000586 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 +0000587 memset(buf, 0xff, len);
588 return;
589}
590
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200591static uint8_t get_reg_ro_bit_mask(const struct emu_data *data, enum flash_reg reg)
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200592{
593 /* Whoever adds a new register must not forget to update this function
594 or at least shouldn't use it incorrectly. */
595 assert(reg == STATUS1 || reg == STATUS2 || reg == STATUS3);
596
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200597 uint8_t ro_bits = reg == STATUS1 ? SPI_SR_WIP : 0;
598
599 if (data->emu_chip == EMULATE_WINBOND_W25Q128FV) {
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200600 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) {
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200608 /* SUS (bit_7) and (R) (bit_2). */
609 ro_bits = 0x84;
610 /* Once any of the lock bits (LB[1..3]) are set, they
611 can't be unset. */
612 ro_bits |= data->emu_status[1] & (1 << 3);
613 ro_bits |= data->emu_status[1] & (1 << 4);
614 ro_bits |= data->emu_status[1] & (1 << 5);
615 } else if (reg == STATUS3) {
616 /* Four reserved bits. */
617 ro_bits = 0x1b;
618 }
619 }
620
Nico Huber4203a472022-05-28 17:28:05 +0200621 if (data->emu_chip == EMULATE_SPANSION_S25FL128L) {
622 const bool srp0 = (data->emu_status[0] >> 7);
623 const bool srp1 = (data->emu_status[1] & 1);
624
625 const bool wp_active = (srp1 || (srp0 && data->hwwp));
626
627 if (wp_active) {
628 ro_bits = 0xff;
629 } else if (reg == STATUS2) {
630 /* SUS (bit_7) */
631 ro_bits = 0x80;
632 /* Once any of the lock bits (LB[0..3]) are set, they
633 can't be unset. */
634 ro_bits |= data->emu_status[1] & (1 << 2);
635 ro_bits |= data->emu_status[1] & (1 << 3);
636 ro_bits |= data->emu_status[1] & (1 << 4);
637 ro_bits |= data->emu_status[1] & (1 << 5);
638 } else if (reg == STATUS3) {
639 /* Two reserved bits. */
640 ro_bits = 0x11;
641 }
642 }
643
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200644 return ro_bits;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200645}
646
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200647static void update_write_protection(struct emu_data *data)
648{
Nico Huber4203a472022-05-28 17:28:05 +0200649 if (data->emu_chip != EMULATE_WINBOND_W25Q128FV &&
650 data->emu_chip != EMULATE_SPANSION_S25FL128L)
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200651 return;
652
653 const struct wp_bits bits = {
654 .srp = data->emu_status[0] >> 7,
655 .srl = data->emu_status[1] & 1,
656
657 .bp_bit_count = 3,
658 .bp =
659 {
660 (data->emu_status[0] >> 2) & 1,
661 (data->emu_status[0] >> 3) & 1,
662 (data->emu_status[0] >> 4) & 1
663 },
664
665 .tb_bit_present = true,
666 .tb = (data->emu_status[0] >> 5) & 1,
667
668 .sec_bit_present = true,
669 .sec = (data->emu_status[0] >> 6) & 1,
670
671 .cmp_bit_present = true,
672 .cmp = (data->emu_status[1] >> 6) & 1,
673 };
674
675 size_t start;
676 size_t len;
677 decode_range_spi25(&start, &len, &bits, data->emu_chip_size);
678
679 data->wp_start = start;
680 data->wp_end = start + len;
681}
682
683/* Checks whether range intersects a write-protected area of the flash if one is
684 * defined. */
685static bool is_write_protected(const struct emu_data *data, uint32_t start, uint32_t len)
686{
687 if (len == 0)
688 return false;
689
690 const uint32_t last = start + len - 1;
691 return (start < data->wp_end && last >= data->wp_start);
692}
693
694/* Returns non-zero on error. */
695static int write_flash_data(struct emu_data *data, uint32_t start, uint32_t len, const uint8_t *buf)
696{
697 if (is_write_protected(data, start, len)) {
698 msg_perr("At least part of the write range is write protected!\n");
699 return 1;
700 }
701
702 memcpy(data->flashchip_contents + start, buf, len);
Felix Singer2fb53b12022-08-19 03:29:32 +0200703 data->emu_modified = true;
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200704 return 0;
705}
706
707/* Returns non-zero on error. */
708static int erase_flash_data(struct emu_data *data, uint32_t start, uint32_t len)
709{
710 if (is_write_protected(data, start, len)) {
711 msg_perr("At least part of the erase range is write protected!\n");
712 return 1;
713 }
714
715 memset(data->flashchip_contents + start, 0xff, len);
Felix Singer2fb53b12022-08-19 03:29:32 +0200716 data->emu_modified = true;
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200717 return 0;
718}
719
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000720static int emulate_spi_chip_response(unsigned int writecnt,
721 unsigned int readcnt,
722 const unsigned char *writearr,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000723 unsigned char *readarr,
724 struct emu_data *data)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000725{
Stefan Tauner0b9df972012-05-07 22:12:16 +0000726 unsigned int offs, i, toread;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200727 uint8_t ro_bits;
Nico Huberbbccdb22022-05-28 16:48:26 +0200728 bool wrsr_ext2, wrsr_ext3;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000729 static int unsigned aai_offs;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000730 const unsigned char sst25vf040_rems_response[2] = {0xbf, 0x44};
731 const unsigned char sst25vf032b_rems_response[2] = {0xbf, 0x4a};
732 const unsigned char mx25l6436_rems_response[2] = {0xc2, 0x16};
Nico Huberf9632d82019-01-20 11:23:49 +0100733 const unsigned char w25q128fv_rems_response[2] = {0xef, 0x17};
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000734
735 if (writecnt == 0) {
736 msg_perr("No command sent to the chip!\n");
737 return 1;
738 }
Paul Menzelac427b22012-02-16 21:07:07 +0000739 /* spi_blacklist has precedence over spi_ignorelist. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000740 for (i = 0; i < data->spi_blacklist_size; i++) {
741 if (writearr[0] == data->spi_blacklist[i]) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000742 msg_pdbg("Refusing blacklisted SPI command 0x%02x\n",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000743 data->spi_blacklist[i]);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000744 return SPI_INVALID_OPCODE;
745 }
746 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000747 for (i = 0; i < data->spi_ignorelist_size; i++) {
748 if (writearr[0] == data->spi_ignorelist[i]) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000749 msg_cdbg("Ignoring ignorelisted SPI command 0x%02x\n",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000750 data->spi_ignorelist[i]);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000751 /* Return success because the command does not fail,
752 * it is simply ignored.
753 */
754 return 0;
755 }
756 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000757
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200758 if (data->emu_max_aai_size && (data->emu_status[0] & SPI_SR_AAI)) {
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000759 if (writearr[0] != JEDEC_AAI_WORD_PROGRAM &&
760 writearr[0] != JEDEC_WRDI &&
761 writearr[0] != JEDEC_RDSR) {
762 msg_perr("Forbidden opcode (0x%02x) attempted during "
763 "AAI sequence!\n", writearr[0]);
764 return 0;
765 }
766 }
767
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000768 switch (writearr[0]) {
769 case JEDEC_RES:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000770 if (writecnt < JEDEC_RES_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000771 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000772 /* offs calculation is only needed for SST chips which treat RES like REMS. */
773 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
774 offs += writecnt - JEDEC_REMS_OUTSIZE;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000775 switch (data->emu_chip) {
Nico Huberafb5dd02026-02-15 13:26:10 +0100776 case EMULATE_ST_M25P10_A:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000777 case EMULATE_ST_M25P10_RES:
778 if (readcnt > 0)
779 memset(readarr, 0x10, readcnt);
780 break;
781 case EMULATE_SST_SST25VF040_REMS:
782 for (i = 0; i < readcnt; i++)
783 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
784 break;
785 case EMULATE_SST_SST25VF032B:
786 for (i = 0; i < readcnt; i++)
787 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
788 break;
789 case EMULATE_MACRONIX_MX25L6436:
790 if (readcnt > 0)
791 memset(readarr, 0x16, readcnt);
792 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100793 case EMULATE_WINBOND_W25Q128FV:
794 if (readcnt > 0)
795 memset(readarr, 0x17, readcnt);
796 break;
Nico Huber4203a472022-05-28 17:28:05 +0200797 case EMULATE_SPANSION_S25FL128L:
798 if (readcnt > 0)
799 readarr[0] = 0x60;
800 if (readcnt > 1)
801 readarr[1] = 0x18;
802 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000803 default: /* ignore */
804 break;
805 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000806 break;
807 case JEDEC_REMS:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000808 /* REMS response has wraparound and uses an address parameter. */
809 if (writecnt < JEDEC_REMS_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000810 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000811 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
812 offs += writecnt - JEDEC_REMS_OUTSIZE;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000813 switch (data->emu_chip) {
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000814 case EMULATE_SST_SST25VF040_REMS:
815 for (i = 0; i < readcnt; i++)
816 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
817 break;
818 case EMULATE_SST_SST25VF032B:
819 for (i = 0; i < readcnt; i++)
820 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
821 break;
822 case EMULATE_MACRONIX_MX25L6436:
823 for (i = 0; i < readcnt; i++)
824 readarr[i] = mx25l6436_rems_response[(offs + i) % 2];
825 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100826 case EMULATE_WINBOND_W25Q128FV:
827 for (i = 0; i < readcnt; i++)
828 readarr[i] = w25q128fv_rems_response[(offs + i) % 2];
829 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000830 default: /* ignore */
831 break;
832 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000833 break;
834 case JEDEC_RDID:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000835 switch (data->emu_chip) {
Nico Huberafb5dd02026-02-15 13:26:10 +0100836 case EMULATE_ST_M25P10_A:
837 if (readcnt > 0)
838 readarr[0] = 0x20;
839 if (readcnt > 1)
840 readarr[1] = 0x20;
841 if (readcnt > 2)
842 readarr[2] = 0x11;
843 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000844 case EMULATE_SST_SST25VF032B:
845 if (readcnt > 0)
846 readarr[0] = 0xbf;
847 if (readcnt > 1)
848 readarr[1] = 0x25;
849 if (readcnt > 2)
850 readarr[2] = 0x4a;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000851 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000852 case EMULATE_MACRONIX_MX25L6436:
853 if (readcnt > 0)
854 readarr[0] = 0xc2;
855 if (readcnt > 1)
856 readarr[1] = 0x20;
857 if (readcnt > 2)
858 readarr[2] = 0x17;
859 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100860 case EMULATE_WINBOND_W25Q128FV:
861 if (readcnt > 0)
862 readarr[0] = 0xef;
863 if (readcnt > 1)
864 readarr[1] = 0x40;
865 if (readcnt > 2)
866 readarr[2] = 0x18;
867 break;
Nico Huber4203a472022-05-28 17:28:05 +0200868 case EMULATE_SPANSION_S25FL128L:
869 if (readcnt > 0)
870 readarr[0] = 0x01;
871 if (readcnt > 1)
872 readarr[1] = 0x60;
873 if (readcnt > 2)
874 readarr[2] = 0x18;
875 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000876 default: /* ignore */
877 break;
878 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000879 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000880 case JEDEC_RDSR:
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200881 memset(readarr, data->emu_status[0], readcnt);
882 break;
883 case JEDEC_RDSR2:
884 if (data->emu_status_len >= 2)
885 memset(readarr, data->emu_status[1], readcnt);
886 break;
887 case JEDEC_RDSR3:
888 if (data->emu_status_len >= 3)
889 memset(readarr, data->emu_status[2], readcnt);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000890 break;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000891 /* FIXME: this should be chip-specific. */
892 case JEDEC_EWSR:
893 case JEDEC_WREN:
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200894 data->emu_status[0] |= SPI_SR_WEL;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000895 break;
896 case JEDEC_WRSR:
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200897 if (!(data->emu_status[0] & SPI_SR_WEL)) {
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000898 msg_perr("WRSR attempted, but WEL is 0!\n");
899 break;
900 }
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200901
Nico Huberbbccdb22022-05-28 16:48:26 +0200902 wrsr_ext2 = (writecnt == 3 && data->emu_wrsr_ext2);
903 wrsr_ext3 = (writecnt == 4 && data->emu_wrsr_ext3);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200904
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000905 /* FIXME: add some reasonable simulation of the busy flag */
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200906
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200907 ro_bits = get_reg_ro_bit_mask(data, STATUS1);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200908 data->emu_status[0] &= ro_bits;
909 data->emu_status[0] |= writearr[1] & ~ro_bits;
Nico Huberbbccdb22022-05-28 16:48:26 +0200910 if (wrsr_ext2 || wrsr_ext3) {
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200911 ro_bits = get_reg_ro_bit_mask(data, STATUS2);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200912 data->emu_status[1] &= ro_bits;
913 data->emu_status[1] |= writearr[2] & ~ro_bits;
914 }
Nico Huberbbccdb22022-05-28 16:48:26 +0200915 if (wrsr_ext3) {
916 ro_bits = get_reg_ro_bit_mask(data, STATUS3);
917 data->emu_status[2] &= ro_bits;
918 data->emu_status[2] |= writearr[3] & ~ro_bits;
919 }
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200920
Nico Huberbbccdb22022-05-28 16:48:26 +0200921 if (wrsr_ext3)
922 msg_pdbg2("WRSR wrote 0x%02x%02x%02x.\n", data->emu_status[2], data->emu_status[1], data->emu_status[0]);
923 else if (wrsr_ext2)
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200924 msg_pdbg2("WRSR wrote 0x%02x%02x.\n", data->emu_status[1], data->emu_status[0]);
925 else
926 msg_pdbg2("WRSR wrote 0x%02x.\n", data->emu_status[0]);
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200927
928 update_write_protection(data);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200929 break;
930 case JEDEC_WRSR2:
931 if (data->emu_status_len < 2)
932 break;
933 if (!(data->emu_status[0] & SPI_SR_WEL)) {
934 msg_perr("WRSR2 attempted, but WEL is 0!\n");
935 break;
936 }
937
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200938 ro_bits = get_reg_ro_bit_mask(data, STATUS2);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200939 data->emu_status[1] &= ro_bits;
940 data->emu_status[1] |= (writearr[1] & ~ro_bits);
941
942 msg_pdbg2("WRSR2 wrote 0x%02x.\n", data->emu_status[1]);
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200943
944 update_write_protection(data);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200945 break;
946 case JEDEC_WRSR3:
947 if (data->emu_status_len < 3)
948 break;
949 if (!(data->emu_status[0] & SPI_SR_WEL)) {
950 msg_perr("WRSR3 attempted, but WEL is 0!\n");
951 break;
952 }
953
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200954 ro_bits = get_reg_ro_bit_mask(data, STATUS3);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200955 data->emu_status[2] &= ro_bits;
956 data->emu_status[2] |= (writearr[1] & ~ro_bits);
957
958 msg_pdbg2("WRSR3 wrote 0x%02x.\n", data->emu_status[2]);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000959 break;
960 case JEDEC_READ:
961 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
962 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000963 offs %= data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000964 if (readcnt > 0)
Edward O'Callaghan94250222021-05-20 20:34:02 +1000965 memcpy(readarr, data->flashchip_contents + offs, readcnt);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000966 break;
967 case JEDEC_BYTE_PROGRAM:
968 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
969 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000970 offs %= data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000971 if (writecnt < 5) {
972 msg_perr("BYTE PROGRAM size too short!\n");
973 return 1;
974 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000975 if (writecnt - 4 > data->emu_max_byteprogram_size) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000976 msg_perr("Max BYTE PROGRAM size exceeded!\n");
977 return 1;
978 }
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200979 if (write_flash_data(data, offs, writecnt - 4, writearr + 4)) {
980 msg_perr("Failed to program flash!\n");
981 return 1;
982 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000983 break;
984 case JEDEC_AAI_WORD_PROGRAM:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000985 if (!data->emu_max_aai_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000986 break;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200987 if (!(data->emu_status[0] & SPI_SR_AAI)) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000988 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
989 msg_perr("Initial AAI WORD PROGRAM size too "
990 "short!\n");
991 return 1;
992 }
993 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
994 msg_perr("Initial AAI WORD PROGRAM size too "
995 "long!\n");
996 return 1;
997 }
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200998 data->emu_status[0] |= SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000999 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
1000 writearr[3];
1001 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +10001002 aai_offs %= data->emu_chip_size;
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +02001003 if (write_flash_data(data, aai_offs, 2, writearr + 4)) {
1004 msg_perr("Failed to program flash!\n");
1005 return 1;
1006 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001007 aai_offs += 2;
1008 } else {
1009 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
1010 msg_perr("Continuation AAI WORD PROGRAM size "
1011 "too short!\n");
1012 return 1;
1013 }
1014 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
1015 msg_perr("Continuation AAI WORD PROGRAM size "
1016 "too long!\n");
1017 return 1;
1018 }
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +02001019 if (write_flash_data(data, aai_offs, 2, writearr + 1)) {
1020 msg_perr("Failed to program flash!\n");
1021 return 1;
1022 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001023 aai_offs += 2;
1024 }
1025 break;
1026 case JEDEC_WRDI:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001027 if (data->emu_max_aai_size)
Sergii Dmytruk59151a42021-11-08 00:05:12 +02001028 data->emu_status[0] &= ~SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001029 break;
1030 case JEDEC_SE:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001031 if (!data->emu_jedec_se_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001032 break;
1033 if (writecnt != JEDEC_SE_OUTSIZE) {
1034 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
1035 return 1;
1036 }
1037 if (readcnt != JEDEC_SE_INSIZE) {
1038 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
1039 return 1;
1040 }
1041 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +10001042 if (offs & (data->emu_jedec_se_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +00001043 msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +10001044 offs &= ~(data->emu_jedec_se_size - 1);
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +02001045 if (erase_flash_data(data, offs, data->emu_jedec_se_size)) {
1046 msg_perr("Failed to erase flash!\n");
1047 return 1;
1048 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001049 break;
1050 case JEDEC_BE_52:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001051 if (!data->emu_jedec_be_52_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001052 break;
1053 if (writecnt != JEDEC_BE_52_OUTSIZE) {
1054 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
1055 return 1;
1056 }
1057 if (readcnt != JEDEC_BE_52_INSIZE) {
1058 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
1059 return 1;
1060 }
1061 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +10001062 if (offs & (data->emu_jedec_be_52_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +00001063 msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +10001064 offs &= ~(data->emu_jedec_be_52_size - 1);
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +02001065 if (erase_flash_data(data, offs, data->emu_jedec_be_52_size)) {
1066 msg_perr("Failed to erase flash!\n");
1067 return 1;
1068 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001069 break;
1070 case JEDEC_BE_D8:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001071 if (!data->emu_jedec_be_d8_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001072 break;
1073 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
1074 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
1075 return 1;
1076 }
1077 if (readcnt != JEDEC_BE_D8_INSIZE) {
1078 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
1079 return 1;
1080 }
1081 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +10001082 if (offs & (data->emu_jedec_be_d8_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +00001083 msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +10001084 offs &= ~(data->emu_jedec_be_d8_size - 1);
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +02001085 if (erase_flash_data(data, offs, data->emu_jedec_be_d8_size)) {
1086 msg_perr("Failed to erase flash!\n");
1087 return 1;
1088 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001089 break;
1090 case JEDEC_CE_60:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001091 if (!data->emu_jedec_ce_60_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001092 break;
1093 if (writecnt != JEDEC_CE_60_OUTSIZE) {
1094 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
1095 return 1;
1096 }
1097 if (readcnt != JEDEC_CE_60_INSIZE) {
1098 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
1099 return 1;
1100 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +00001101 /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001102 /* emu_jedec_ce_60_size is emu_chip_size. */
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +02001103 if (erase_flash_data(data, 0, data->emu_jedec_ce_60_size)) {
1104 msg_perr("Failed to erase flash!\n");
1105 return 1;
1106 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001107 break;
1108 case JEDEC_CE_C7:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001109 if (!data->emu_jedec_ce_c7_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001110 break;
1111 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
1112 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
1113 return 1;
1114 }
1115 if (readcnt != JEDEC_CE_C7_INSIZE) {
1116 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
1117 return 1;
1118 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +00001119 /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001120 /* emu_jedec_ce_c7_size is emu_chip_size. */
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +02001121 if (erase_flash_data(data, 0, data->emu_jedec_ce_c7_size)) {
1122 msg_perr("Failed to erase flash!\n");
1123 return 1;
1124 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001125 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +00001126 case JEDEC_SFDP:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001127 if (data->emu_chip != EMULATE_MACRONIX_MX25L6436)
Stefan Tauner0b9df972012-05-07 22:12:16 +00001128 break;
1129 if (writecnt < 4)
1130 break;
1131 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
1132
1133 /* SFDP expects one dummy byte after the address. */
1134 if (writecnt == 4) {
1135 /* The dummy byte was not written, make sure it is read instead.
1136 * Shifting and shortening the read array does achieve this goal.
1137 */
1138 readarr++;
1139 readcnt--;
1140 } else {
1141 /* The response is shifted if more than 5 bytes are written, because SFDP data is
1142 * already shifted out by the chip while those superfluous bytes are written. */
1143 offs += writecnt - 5;
1144 }
1145
1146 /* The SFDP spec implies that the start address of an SFDP read may be truncated to fit in the
1147 * SFDP table address space, i.e. the start address may be wrapped around at SFDP table size.
1148 * This is a reasonable implementation choice in hardware because it saves a few gates. */
1149 if (offs >= sizeof(sfdp_table)) {
1150 msg_pdbg("Wrapping the start address around the SFDP table boundary (using 0x%x "
1151 "instead of 0x%x).\n", (unsigned int)(offs % sizeof(sfdp_table)), offs);
1152 offs %= sizeof(sfdp_table);
1153 }
1154 toread = min(sizeof(sfdp_table) - offs, readcnt);
1155 memcpy(readarr, sfdp_table + offs, toread);
1156 if (toread < readcnt)
1157 msg_pdbg("Crossing the SFDP table boundary in a single "
1158 "continuous chunk produces undefined results "
1159 "after that point.\n");
1160 break;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001161 default:
1162 /* No special response. */
1163 break;
1164 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +00001165 if (writearr[0] != JEDEC_WREN && writearr[0] != JEDEC_EWSR)
Sergii Dmytruk59151a42021-11-08 00:05:12 +02001166 data->emu_status[0] &= ~SPI_SR_WEL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001167 return 0;
1168}
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001169
Nico Huber610c1aa2023-02-15 02:56:05 +01001170static int dummy_spi_send_command(const struct spi_master *mst,
1171 unsigned int writecnt, unsigned int readcnt,
1172 const unsigned char *writearr, unsigned char *readarr)
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001173{
Nico Huber519be662018-12-23 20:03:35 +01001174 unsigned int i;
Nico Huber610c1aa2023-02-15 02:56:05 +01001175 struct emu_data *emu_data = mst->data;
Lachlan Bishopc753c402020-09-10 14:57:05 +10001176 if (!emu_data) {
1177 msg_perr("No data in flash context!\n");
1178 return 1;
1179 }
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001180
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +00001181 msg_pspew("%s:", __func__);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001182
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +00001183 msg_pspew(" writing %u bytes:", writecnt);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001184 for (i = 0; i < writecnt; i++)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +00001185 msg_pspew(" 0x%02x", writearr[i]);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001186
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001187 /* Response for unknown commands and missing chip is 0xff. */
1188 memset(readarr, 0xff, readcnt);
Lachlan Bishopc753c402020-09-10 14:57:05 +10001189 switch (emu_data->emu_chip) {
Nico Huberafb5dd02026-02-15 13:26:10 +01001190 case EMULATE_ST_M25P10_A:
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001191 case EMULATE_ST_M25P10_RES:
1192 case EMULATE_SST_SST25VF040_REMS:
1193 case EMULATE_SST_SST25VF032B:
Stefan Tauner0b9df972012-05-07 22:12:16 +00001194 case EMULATE_MACRONIX_MX25L6436:
Nico Huberf9632d82019-01-20 11:23:49 +01001195 case EMULATE_WINBOND_W25Q128FV:
Nico Huber4203a472022-05-28 17:28:05 +02001196 case EMULATE_SPANSION_S25FL128L:
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001197 if (emulate_spi_chip_response(writecnt, readcnt, writearr,
Lachlan Bishopc753c402020-09-10 14:57:05 +10001198 readarr, emu_data)) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +00001199 msg_pdbg("Invalid command sent to flash chip!\n");
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001200 return 1;
1201 }
1202 break;
1203 default:
1204 break;
1205 }
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +00001206 msg_pspew(" reading %u bytes:", readcnt);
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001207 for (i = 0; i < readcnt; i++)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001208 msg_pspew(" 0x%02x", readarr[i]);
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +00001209 msg_pspew("\n");
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001210 return 0;
1211}
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +00001212
Mark Marshallf20b7be2014-05-09 21:16:21 +00001213static 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 +00001214{
Nico Huber9a11cbf2023-01-13 01:19:07 +01001215 const struct emu_data *const data = flash->mst.spi->data;
Edward O'Callaghanb1313422021-05-20 20:27:59 +10001216 return spi_write_chunked(flash, buf, start, len, data->spi_write_256_chunksize);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001217}
Thomas Heijligencc853d82021-05-04 15:32:17 +02001218
Nikolai Artemieve7a41e32022-11-28 17:40:56 +11001219static bool dummy_spi_probe_opcode(const struct flashctx *flash, uint8_t opcode)
Aarya Chaumal0cea7532022-07-04 18:21:50 +05301220{
1221 size_t i;
Nico Huber9a11cbf2023-01-13 01:19:07 +01001222 const struct emu_data *emu_data = flash->mst.spi->data;
Aarya Chaumal0cea7532022-07-04 18:21:50 +05301223 for (i = 0; i < emu_data->spi_blacklist_size; i++) {
1224 if (emu_data->spi_blacklist[i] == opcode)
1225 return false;
1226 }
1227 return true;
1228}
1229
Thomas Heijligencc853d82021-05-04 15:32:17 +02001230const struct programmer_entry programmer_dummy = {
1231 .name = "dummy",
1232 .type = OTHER,
1233 /* FIXME */
1234 .devs.note = "Dummy device, does nothing and logs all accesses\n",
1235 .init = dummy_init,
Thomas Heijligencc853d82021-05-04 15:32:17 +02001236};