blob: 948dc0bc88ba8666c317f900f9fb69214d76698d [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,
34 EMULATE_ST_M25P10_RES,
35 EMULATE_SST_SST25VF040_REMS,
36 EMULATE_SST_SST25VF032B,
Stefan Tauner0b9df972012-05-07 22:12:16 +000037 EMULATE_MACRONIX_MX25L6436,
Nico Huberf9632d82019-01-20 11:23:49 +010038 EMULATE_WINBOND_W25Q128FV,
Nico Huber4203a472022-05-28 17:28:05 +020039 EMULATE_SPANSION_S25FL128L,
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000040};
Stefan Tauner0b9df972012-05-07 22:12:16 +000041
Lachlan Bishopc753c402020-09-10 14:57:05 +100042struct emu_data {
43 enum emu_chip emu_chip;
44 char *emu_persistent_image;
45 unsigned int emu_chip_size;
Sergii Dmytruk59151a42021-11-08 00:05:12 +020046 /* Note: W25Q128FV doesn't change value of SR2 if it's not provided, but
47 * even its previous generations do, so don't forget to update
Nico Huberbbccdb22022-05-28 16:48:26 +020048 * WRSR code on enabling WRSR_EXT2 for more chips. */
49 bool emu_wrsr_ext2;
50 bool emu_wrsr_ext3;
Felix Singer2fb53b12022-08-19 03:29:32 +020051 bool emu_modified; /* is the image modified since reading it? */
Sergii Dmytruk59151a42021-11-08 00:05:12 +020052 uint8_t emu_status[3];
53 uint8_t emu_status_len; /* number of emulated status registers */
Lachlan Bishopc753c402020-09-10 14:57:05 +100054 unsigned int emu_max_byteprogram_size;
55 unsigned int emu_max_aai_size;
56 unsigned int emu_jedec_se_size;
57 unsigned int emu_jedec_be_52_size;
58 unsigned int emu_jedec_be_d8_size;
59 unsigned int emu_jedec_ce_60_size;
60 unsigned int emu_jedec_ce_c7_size;
61 unsigned char spi_blacklist[256];
62 unsigned char spi_ignorelist[256];
63 unsigned int spi_blacklist_size;
64 unsigned int spi_ignorelist_size;
Edward O'Callaghan94250222021-05-20 20:34:02 +100065
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +020066 bool hwwp; /* state of hardware write protection */
67 /* wp_start == wp_end when write-protection is disabled */
68 uint32_t wp_start;
69 uint32_t wp_end;
70
Edward O'Callaghanb1313422021-05-20 20:27:59 +100071 unsigned int spi_write_256_chunksize;
Edward O'Callaghan94250222021-05-20 20:34:02 +100072 uint8_t *flashchip_contents;
Lachlan Bishopc753c402020-09-10 14:57:05 +100073};
74
Stefan Tauner0b9df972012-05-07 22:12:16 +000075/* A legit complete SFDP table based on the MX25L6436E (rev. 1.8) datasheet. */
Stefan Tauner67d163d2013-01-15 17:37:48 +000076static const uint8_t sfdp_table[] = {
Stefan Tauner0b9df972012-05-07 22:12:16 +000077 0x53, 0x46, 0x44, 0x50, // @0x00: SFDP signature
78 0x00, 0x01, 0x01, 0xFF, // @0x04: revision 1.0, 2 headers
79 0x00, 0x00, 0x01, 0x09, // @0x08: JEDEC SFDP header rev. 1.0, 9 DW long
80 0x1C, 0x00, 0x00, 0xFF, // @0x0C: PTP0 = 0x1C (instead of 0x30)
81 0xC2, 0x00, 0x01, 0x04, // @0x10: Macronix header rev. 1.0, 4 DW long
82 0x48, 0x00, 0x00, 0xFF, // @0x14: PTP1 = 0x48 (instead of 0x60)
83 0xFF, 0xFF, 0xFF, 0xFF, // @0x18: hole.
84 0xE5, 0x20, 0xC9, 0xFF, // @0x1C: SFDP parameter table start
85 0xFF, 0xFF, 0xFF, 0x03, // @0x20
86 0x00, 0xFF, 0x08, 0x6B, // @0x24
87 0x08, 0x3B, 0x00, 0xFF, // @0x28
88 0xEE, 0xFF, 0xFF, 0xFF, // @0x2C
89 0xFF, 0xFF, 0x00, 0x00, // @0x30
90 0xFF, 0xFF, 0x00, 0xFF, // @0x34
91 0x0C, 0x20, 0x0F, 0x52, // @0x38
92 0x10, 0xD8, 0x00, 0xFF, // @0x3C: SFDP parameter table end
93 0xFF, 0xFF, 0xFF, 0xFF, // @0x40: hole.
94 0xFF, 0xFF, 0xFF, 0xFF, // @0x44: hole.
95 0x00, 0x36, 0x00, 0x27, // @0x48: Macronix parameter table start
96 0xF4, 0x4F, 0xFF, 0xFF, // @0x4C
97 0xD9, 0xC8, 0xFF, 0xFF, // @0x50
98 0xFF, 0xFF, 0xFF, 0xFF, // @0x54: Macronix parameter table end
99};
100
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000101
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000102
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000103static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
Mark Marshallf20b7be2014-05-09 21:16:21 +0000104 const unsigned char *writearr, unsigned char *readarr);
105static int dummy_spi_write_256(struct flashctx *flash, const uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000106 unsigned int start, unsigned int len);
Mark Marshallf20b7be2014-05-09 21:16:21 +0000107static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr);
108static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr);
109static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr);
110static void dummy_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len);
111static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr);
112static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr);
113static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr);
114static void dummy_chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len);
Nikolai Artemieve7a41e32022-11-28 17:40:56 +1100115static bool dummy_spi_probe_opcode(const struct flashctx *flash, uint8_t opcode);
Nico Huber0e76d992023-01-12 20:22:55 +0100116static void *dummy_map(const char *descr, uintptr_t phys_addr, size_t len);
117static void dummy_unmap(void *virt_addr, size_t len);
Michael Karcherb9dbe482011-05-11 17:07:07 +0000118
Nico Huber03f3a6d2021-05-11 17:53:34 +0200119static const struct spi_master spi_master_dummyflasher = {
Nico Huber1cf407b2017-11-10 20:18:23 +0100120 .features = SPI_MASTER_4BA,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000121 .max_data_read = MAX_DATA_READ_UNLIMITED,
122 .max_data_write = MAX_DATA_UNSPECIFIED,
123 .command = dummy_spi_send_command,
124 .multicommand = default_spi_send_multicommand,
125 .read = default_spi_read,
126 .write_256 = dummy_spi_write_256,
Aarya Chaumal0cea7532022-07-04 18:21:50 +0530127 .probe_opcode = dummy_spi_probe_opcode,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000128};
David Hendricks8bb20212011-06-14 01:35:36 +0000129
Edward O'Callaghanf7504a92021-05-20 20:21:13 +1000130static const struct par_master par_master_dummyflasher = {
Thomas Heijligen43040f22022-06-23 14:38:35 +0200131 .chip_readb = dummy_chip_readb,
132 .chip_readw = dummy_chip_readw,
133 .chip_readl = dummy_chip_readl,
134 .chip_readn = dummy_chip_readn,
135 .chip_writeb = dummy_chip_writeb,
136 .chip_writew = dummy_chip_writew,
137 .chip_writel = dummy_chip_writel,
138 .chip_writen = dummy_chip_writen,
Nico Huber0e76d992023-01-12 20:22:55 +0100139 .map_flash = dummy_map,
140 .unmap_flash = dummy_unmap,
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000141};
142
David Hendricks8bb20212011-06-14 01:35:36 +0000143static int dummy_shutdown(void *data)
144{
145 msg_pspew("%s\n", __func__);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000146 struct emu_data *emu_data = (struct emu_data *)data;
147 if (emu_data->emu_chip != EMULATE_NONE) {
148 if (emu_data->emu_persistent_image && emu_data->emu_modified) {
149 msg_pdbg("Writing %s\n", emu_data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000150 write_buf_to_file(emu_data->flashchip_contents,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000151 emu_data->emu_chip_size,
152 emu_data->emu_persistent_image);
David Hendricks8bb20212011-06-14 01:35:36 +0000153 }
Angel Pons02b9ae22021-05-25 12:46:43 +0200154 free(emu_data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000155 free(emu_data->flashchip_contents);
David Hendricks8bb20212011-06-14 01:35:36 +0000156 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000157 free(data);
David Hendricks8bb20212011-06-14 01:35:36 +0000158 return 0;
159}
160
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000161static int init_data(struct emu_data *data, enum chipbustype *dummy_buses_supported)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000162{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000163 char *bustext = NULL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000164 char *tmp = NULL;
Nico Huber519be662018-12-23 20:03:35 +0100165 unsigned int i;
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000166 char *endptr;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000167 char *status = NULL;
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000168
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000169 bustext = extract_programmer_param("bus");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000170 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
171 if (!bustext)
172 bustext = strdup("parallel+lpc+fwh+spi");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000173 /* Convert the parameters to lowercase. */
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000174 tolower_string(bustext);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000175
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000176 *dummy_buses_supported = BUS_NONE;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000177 if (strstr(bustext, "parallel")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000178 *dummy_buses_supported |= BUS_PARALLEL;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000179 msg_pdbg("Enabling support for %s flash.\n", "parallel");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000180 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000181 if (strstr(bustext, "lpc")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000182 *dummy_buses_supported |= BUS_LPC;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000183 msg_pdbg("Enabling support for %s flash.\n", "LPC");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000184 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000185 if (strstr(bustext, "fwh")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000186 *dummy_buses_supported |= BUS_FWH;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000187 msg_pdbg("Enabling support for %s flash.\n", "FWH");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000188 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000189 if (strstr(bustext, "spi")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000190 *dummy_buses_supported |= BUS_SPI;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000191 msg_pdbg("Enabling support for %s flash.\n", "SPI");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000192 }
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000193 if (*dummy_buses_supported == BUS_NONE)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000194 msg_pdbg("Support for all flash bus types disabled.\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000195 free(bustext);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000196
197 tmp = extract_programmer_param("spi_write_256_chunksize");
198 if (tmp) {
Edward O'Callaghanb1313422021-05-20 20:27:59 +1000199 data->spi_write_256_chunksize = strtoul(tmp, &endptr, 0);
200 if (*endptr != '\0' || data->spi_write_256_chunksize < 1) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000201 msg_perr("invalid spi_write_256_chunksize\n");
Edward O'Callaghanc785f882021-05-23 22:14:36 +1000202 free(tmp);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000203 return 1;
204 }
Edward O'Callaghanc785f882021-05-23 22:14:36 +1000205 free(tmp);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000206 }
207
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000208 tmp = extract_programmer_param("spi_blacklist");
209 if (tmp) {
210 i = strlen(tmp);
211 if (!strncmp(tmp, "0x", 2)) {
212 i -= 2;
213 memmove(tmp, tmp + 2, i + 1);
214 }
215 if ((i > 512) || (i % 2)) {
216 msg_perr("Invalid SPI command blacklist length\n");
217 free(tmp);
218 return 1;
219 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000220 data->spi_blacklist_size = i / 2;
221 for (i = 0; i < data->spi_blacklist_size * 2; i++) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000222 if (!isxdigit((unsigned char)tmp[i])) {
223 msg_perr("Invalid char \"%c\" in SPI command "
224 "blacklist\n", tmp[i]);
225 free(tmp);
226 return 1;
227 }
228 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000229 for (i = 0; i < data->spi_blacklist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000230 unsigned int tmp2;
231 /* SCNx8 is apparently not supported by MSVC (and thus
232 * MinGW), so work around it with an extra variable
233 */
234 sscanf(tmp + i * 2, "%2x", &tmp2);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000235 data->spi_blacklist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000236 }
237 msg_pdbg("SPI blacklist is ");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000238 for (i = 0; i < data->spi_blacklist_size; i++)
239 msg_pdbg("%02x ", data->spi_blacklist[i]);
240 msg_pdbg(", size %u\n", data->spi_blacklist_size);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000241 }
242 free(tmp);
243
244 tmp = extract_programmer_param("spi_ignorelist");
245 if (tmp) {
246 i = strlen(tmp);
247 if (!strncmp(tmp, "0x", 2)) {
248 i -= 2;
249 memmove(tmp, tmp + 2, i + 1);
250 }
251 if ((i > 512) || (i % 2)) {
252 msg_perr("Invalid SPI command ignorelist length\n");
253 free(tmp);
254 return 1;
255 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000256 data->spi_ignorelist_size = i / 2;
257 for (i = 0; i < data->spi_ignorelist_size * 2; i++) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000258 if (!isxdigit((unsigned char)tmp[i])) {
259 msg_perr("Invalid char \"%c\" in SPI command "
260 "ignorelist\n", tmp[i]);
261 free(tmp);
262 return 1;
263 }
264 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000265 for (i = 0; i < data->spi_ignorelist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000266 unsigned int tmp2;
267 /* SCNx8 is apparently not supported by MSVC (and thus
268 * MinGW), so work around it with an extra variable
269 */
270 sscanf(tmp + i * 2, "%2x", &tmp2);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000271 data->spi_ignorelist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000272 }
273 msg_pdbg("SPI ignorelist is ");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000274 for (i = 0; i < data->spi_ignorelist_size; i++)
275 msg_pdbg("%02x ", data->spi_ignorelist[i]);
276 msg_pdbg(", size %u\n", data->spi_ignorelist_size);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000277 }
278 free(tmp);
279
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200280 tmp = extract_programmer_param("hwwp");
281 if (tmp) {
282 if (!strcmp(tmp, "yes")) {
283 msg_pdbg("Emulated chip will have hardware WP enabled\n");
284 data->hwwp = true;
285 } else if (!strcmp(tmp, "no")) {
286 msg_pdbg("Emulated chip will have hardware WP disabled\n");
287 } else {
288 msg_perr("hwwp can be \"yes\" or \"no\"\n");
289 free(tmp);
290 return 1;
291 }
292 free(tmp);
293 }
294
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000295 tmp = extract_programmer_param("emulate");
296 if (!tmp) {
297 msg_pdbg("Not emulating any flash chip.\n");
298 /* Nothing else to do. */
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000299 return 0;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000300 }
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000301
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000302 if (!strcmp(tmp, "M25P10.RES")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000303 data->emu_chip = EMULATE_ST_M25P10_RES;
304 data->emu_chip_size = 128 * 1024;
305 data->emu_max_byteprogram_size = 128;
306 data->emu_max_aai_size = 0;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200307 data->emu_status_len = 1;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000308 data->emu_jedec_se_size = 0;
309 data->emu_jedec_be_52_size = 0;
310 data->emu_jedec_be_d8_size = 32 * 1024;
311 data->emu_jedec_ce_60_size = 0;
312 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000313 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
314 "write)\n");
315 }
316 if (!strcmp(tmp, "SST25VF040.REMS")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000317 data->emu_chip = EMULATE_SST_SST25VF040_REMS;
318 data->emu_chip_size = 512 * 1024;
319 data->emu_max_byteprogram_size = 1;
320 data->emu_max_aai_size = 0;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200321 data->emu_status_len = 1;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000322 data->emu_jedec_se_size = 4 * 1024;
323 data->emu_jedec_be_52_size = 32 * 1024;
324 data->emu_jedec_be_d8_size = 0;
325 data->emu_jedec_ce_60_size = data->emu_chip_size;
326 data->emu_jedec_ce_c7_size = 0;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000327 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
328 "byte write)\n");
329 }
330 if (!strcmp(tmp, "SST25VF032B")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000331 data->emu_chip = EMULATE_SST_SST25VF032B;
332 data->emu_chip_size = 4 * 1024 * 1024;
333 data->emu_max_byteprogram_size = 1;
334 data->emu_max_aai_size = 2;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200335 data->emu_status_len = 1;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000336 data->emu_jedec_se_size = 4 * 1024;
337 data->emu_jedec_be_52_size = 32 * 1024;
338 data->emu_jedec_be_d8_size = 64 * 1024;
339 data->emu_jedec_ce_60_size = data->emu_chip_size;
340 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000341 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
342 "write)\n");
343 }
Stefan Tauner0b9df972012-05-07 22:12:16 +0000344 if (!strcmp(tmp, "MX25L6436")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000345 data->emu_chip = EMULATE_MACRONIX_MX25L6436;
346 data->emu_chip_size = 8 * 1024 * 1024;
347 data->emu_max_byteprogram_size = 256;
348 data->emu_max_aai_size = 0;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200349 data->emu_status_len = 1;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000350 data->emu_jedec_se_size = 4 * 1024;
351 data->emu_jedec_be_52_size = 32 * 1024;
352 data->emu_jedec_be_d8_size = 64 * 1024;
353 data->emu_jedec_ce_60_size = data->emu_chip_size;
354 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000355 msg_pdbg("Emulating Macronix MX25L6436 SPI flash chip (RDID, "
356 "SFDP)\n");
357 }
Nico Huberf9632d82019-01-20 11:23:49 +0100358 if (!strcmp(tmp, "W25Q128FV")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000359 data->emu_chip = EMULATE_WINBOND_W25Q128FV;
Nico Huberbbccdb22022-05-28 16:48:26 +0200360 data->emu_wrsr_ext2 = true;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000361 data->emu_chip_size = 16 * 1024 * 1024;
362 data->emu_max_byteprogram_size = 256;
363 data->emu_max_aai_size = 0;
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200364 data->emu_status_len = 3;
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;
Nico Huberf9632d82019-01-20 11:23:49 +0100370 msg_pdbg("Emulating Winbond W25Q128FV SPI flash chip (RDID)\n");
371 }
Nico Huber4203a472022-05-28 17:28:05 +0200372 if (!strcmp(tmp, "S25FL128L")) {
373 data->emu_chip = EMULATE_SPANSION_S25FL128L;
374 data->emu_wrsr_ext2 = true;
375 data->emu_wrsr_ext3 = true;
376 data->emu_chip_size = 16 * 1024 * 1024;
377 data->emu_max_byteprogram_size = 256;
378 data->emu_max_aai_size = 0;
379 data->emu_status_len = 3;
380 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;
385 msg_pdbg("Emulating Spansion S25FL128L SPI flash chip (RES, RDID, WP)\n");
386 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000387 if (data->emu_chip == EMULATE_NONE) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000388 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
389 free(tmp);
390 return 1;
391 }
392 free(tmp);
David Hendricks8bb20212011-06-14 01:35:36 +0000393
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000394 status = extract_programmer_param("spi_status");
395 if (status) {
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200396 unsigned int emu_status;
397
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000398 errno = 0;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200399 emu_status = strtoul(status, &endptr, 0);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000400 if (errno != 0 || status == endptr) {
Angel Ponsc2484642021-05-25 13:03:24 +0200401 free(status);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000402 msg_perr("Error: initial status register specified, "
403 "but the value could not be converted.\n");
404 return 1;
405 }
Angel Ponsc2484642021-05-25 13:03:24 +0200406 free(status);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200407
408 data->emu_status[0] = emu_status;
409 data->emu_status[1] = emu_status >> 8;
410 data->emu_status[2] = emu_status >> 16;
411
412 if (data->emu_status_len == 3) {
413 msg_pdbg("Initial status registers:\n"
414 "\tSR1 is set to 0x%02x\n"
415 "\tSR2 is set to 0x%02x\n"
416 "\tSR3 is set to 0x%02x\n",
417 data->emu_status[0], data->emu_status[1], data->emu_status[2]);
418 } else if (data->emu_status_len == 2) {
419 msg_pdbg("Initial status registers:\n"
420 "\tSR1 is set to 0x%02x\n"
421 "\tSR2 is set to 0x%02x\n",
422 data->emu_status[0], data->emu_status[1]);
423 } else {
424 msg_pdbg("Initial status register is set to 0x%02x.\n",
425 data->emu_status[0]);
426 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000427 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000428
Angel Pons328898a2021-05-25 12:56:18 +0200429 data->flashchip_contents = malloc(data->emu_chip_size);
430 if (!data->flashchip_contents) {
431 msg_perr("Out of memory!\n");
432 return 1;
433 }
434
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000435
436 return 0;
437}
438
Nico Hubere3a26882023-01-11 21:45:51 +0100439static int dummy_init(struct flashprog_programmer *const prog)
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000440{
Alexander Goncharov0d929fe2023-01-24 14:43:12 +0400441 int ret = 0;
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000442 struct stat image_stat;
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000443
Nico Huber4e9e99c2021-06-09 18:08:48 +0200444 struct emu_data *data = calloc(1, sizeof(*data));
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000445 if (!data) {
446 msg_perr("Out of memory!\n");
447 return 1;
448 }
449 data->emu_chip = EMULATE_NONE;
Edward O'Callaghanb1313422021-05-20 20:27:59 +1000450 data->spi_write_256_chunksize = 256;
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000451
452 msg_pspew("%s\n", __func__);
453
454 enum chipbustype dummy_buses_supported;
455 if (init_data(data, &dummy_buses_supported)) {
456 free(data);
457 return 1;
458 }
459
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000460 if (data->emu_chip == EMULATE_NONE) {
461 msg_pdbg("Not emulating any flash chip.\n");
462 /* Nothing else to do. */
463 goto dummy_init_out;
464 }
465
Lachlan Bishopc753c402020-09-10 14:57:05 +1000466 msg_pdbg("Filling fake flash chip with 0xff, size %i\n", data->emu_chip_size);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000467 memset(data->flashchip_contents, 0xff, data->emu_chip_size);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000468
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000469 /* Will be freed by shutdown function if necessary. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000470 data->emu_persistent_image = extract_programmer_param("image");
471 if (!data->emu_persistent_image) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000472 /* Nothing else to do. */
David Hendricks8bb20212011-06-14 01:35:36 +0000473 goto dummy_init_out;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000474 }
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000475 /* We will silently (in default verbosity) ignore the file if it does not exist (yet) or the size does
476 * not match the emulated chip. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000477 if (!stat(data->emu_persistent_image, &image_stat)) {
Stefan Tauner23e10b82016-01-23 16:16:49 +0000478 msg_pdbg("Found persistent image %s, %jd B ",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000479 data->emu_persistent_image, (intmax_t)image_stat.st_size);
480 if ((uintmax_t)image_stat.st_size == data->emu_chip_size) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000481 msg_pdbg("matches.\n");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000482 msg_pdbg("Reading %s\n", data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000483 if (read_buf_from_file(data->flashchip_contents, data->emu_chip_size,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000484 data->emu_persistent_image)) {
485 msg_perr("Unable to read %s\n", data->emu_persistent_image);
Angel Pons02b9ae22021-05-25 12:46:43 +0200486 free(data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000487 free(data->flashchip_contents);
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000488 free(data);
Jacob Garberca598da2019-08-12 10:44:17 -0600489 return 1;
490 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000491 } else {
492 msg_pdbg("doesn't match.\n");
493 }
494 }
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000495
David Hendricks8bb20212011-06-14 01:35:36 +0000496dummy_init_out:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000497 if (register_shutdown(dummy_shutdown, data)) {
Angel Pons02b9ae22021-05-25 12:46:43 +0200498 free(data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000499 free(data->flashchip_contents);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000500 free(data);
David Hendricks8bb20212011-06-14 01:35:36 +0000501 return 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000502 }
Edward O'Callaghan3fa321d2021-05-17 20:01:27 +1000503 if (dummy_buses_supported & BUS_NONSPI)
Alexander Goncharov0d929fe2023-01-24 14:43:12 +0400504 ret |= register_par_master(&par_master_dummyflasher,
505 dummy_buses_supported & BUS_NONSPI,
Nico Huber89569d62023-01-12 23:31:40 +0100506 0, data);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000507 if (dummy_buses_supported & BUS_SPI)
Nico Huber89569d62023-01-12 23:31:40 +0100508 ret |= register_spi_master(&spi_master_dummyflasher, 0, data);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000509
Alexander Goncharov0d929fe2023-01-24 14:43:12 +0400510 return ret;
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000511}
512
Thomas Heijligencc853d82021-05-04 15:32:17 +0200513static void *dummy_map(const char *descr, uintptr_t phys_addr, size_t len)
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000514{
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000515 msg_pspew("%s: Mapping %s, 0x%zx bytes at 0x%0*" PRIxPTR "\n",
Stefan Tauner0554ca52013-07-25 22:54:25 +0000516 __func__, descr, len, PRIxPTR_WIDTH, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000517 return (void *)phys_addr;
518}
519
Thomas Heijligencc853d82021-05-04 15:32:17 +0200520static void dummy_unmap(void *virt_addr, size_t len)
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000521{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000522 msg_pspew("%s: Unmapping 0x%zx bytes at %p\n", __func__, len, virt_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000523}
524
Mark Marshallf20b7be2014-05-09 21:16:21 +0000525static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000526{
Stefan Taunerc2333752013-07-13 23:31:37 +0000527 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%02x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000528}
529
Mark Marshallf20b7be2014-05-09 21:16:21 +0000530static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000531{
Stefan Taunerc2333752013-07-13 23:31:37 +0000532 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%04x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000533}
534
Mark Marshallf20b7be2014-05-09 21:16:21 +0000535static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000536{
Stefan Taunerc2333752013-07-13 23:31:37 +0000537 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%08x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000538}
539
Mark Marshallf20b7be2014-05-09 21:16:21 +0000540static 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 +0000541{
542 size_t i;
Stefan Tauner0554ca52013-07-25 22:54:25 +0000543 msg_pspew("%s: addr=0x%" PRIxPTR ", len=0x%zx, writing data (hex):", __func__, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000544 for (i = 0; i < len; i++) {
545 if ((i % 16) == 0)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000546 msg_pspew("\n");
547 msg_pspew("%02x ", buf[i]);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000548 }
549}
550
Mark Marshallf20b7be2014-05-09 21:16:21 +0000551static uint8_t dummy_chip_readb(const struct flashctx *flash, const 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 ", returning 0xff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000554 return 0xff;
555}
556
Mark Marshallf20b7be2014-05-09 21:16:21 +0000557static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000558{
Stefan Taunerc2333752013-07-13 23:31:37 +0000559 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000560 return 0xffff;
561}
562
Mark Marshallf20b7be2014-05-09 21:16:21 +0000563static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000564{
Stefan Taunerc2333752013-07-13 23:31:37 +0000565 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffffffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000566 return 0xffffffff;
567}
568
Mark Marshallf20b7be2014-05-09 21:16:21 +0000569static 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 +0000570{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000571 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 +0000572 memset(buf, 0xff, len);
573 return;
574}
575
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200576static uint8_t get_reg_ro_bit_mask(const struct emu_data *data, enum flash_reg reg)
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200577{
578 /* Whoever adds a new register must not forget to update this function
579 or at least shouldn't use it incorrectly. */
580 assert(reg == STATUS1 || reg == STATUS2 || reg == STATUS3);
581
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200582 uint8_t ro_bits = reg == STATUS1 ? SPI_SR_WIP : 0;
583
584 if (data->emu_chip == EMULATE_WINBOND_W25Q128FV) {
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200585 const bool srp0 = (data->emu_status[0] >> 7);
586 const bool srp1 = (data->emu_status[1] & 1);
587
588 const bool wp_active = (srp1 || (srp0 && data->hwwp));
589
590 if (wp_active) {
591 ro_bits = 0xff;
592 } else if (reg == STATUS2) {
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200593 /* SUS (bit_7) and (R) (bit_2). */
594 ro_bits = 0x84;
595 /* Once any of the lock bits (LB[1..3]) are set, they
596 can't be unset. */
597 ro_bits |= data->emu_status[1] & (1 << 3);
598 ro_bits |= data->emu_status[1] & (1 << 4);
599 ro_bits |= data->emu_status[1] & (1 << 5);
600 } else if (reg == STATUS3) {
601 /* Four reserved bits. */
602 ro_bits = 0x1b;
603 }
604 }
605
Nico Huber4203a472022-05-28 17:28:05 +0200606 if (data->emu_chip == EMULATE_SPANSION_S25FL128L) {
607 const bool srp0 = (data->emu_status[0] >> 7);
608 const bool srp1 = (data->emu_status[1] & 1);
609
610 const bool wp_active = (srp1 || (srp0 && data->hwwp));
611
612 if (wp_active) {
613 ro_bits = 0xff;
614 } else if (reg == STATUS2) {
615 /* SUS (bit_7) */
616 ro_bits = 0x80;
617 /* Once any of the lock bits (LB[0..3]) are set, they
618 can't be unset. */
619 ro_bits |= data->emu_status[1] & (1 << 2);
620 ro_bits |= data->emu_status[1] & (1 << 3);
621 ro_bits |= data->emu_status[1] & (1 << 4);
622 ro_bits |= data->emu_status[1] & (1 << 5);
623 } else if (reg == STATUS3) {
624 /* Two reserved bits. */
625 ro_bits = 0x11;
626 }
627 }
628
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200629 return ro_bits;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200630}
631
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200632static void update_write_protection(struct emu_data *data)
633{
Nico Huber4203a472022-05-28 17:28:05 +0200634 if (data->emu_chip != EMULATE_WINBOND_W25Q128FV &&
635 data->emu_chip != EMULATE_SPANSION_S25FL128L)
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200636 return;
637
638 const struct wp_bits bits = {
639 .srp = data->emu_status[0] >> 7,
640 .srl = data->emu_status[1] & 1,
641
642 .bp_bit_count = 3,
643 .bp =
644 {
645 (data->emu_status[0] >> 2) & 1,
646 (data->emu_status[0] >> 3) & 1,
647 (data->emu_status[0] >> 4) & 1
648 },
649
650 .tb_bit_present = true,
651 .tb = (data->emu_status[0] >> 5) & 1,
652
653 .sec_bit_present = true,
654 .sec = (data->emu_status[0] >> 6) & 1,
655
656 .cmp_bit_present = true,
657 .cmp = (data->emu_status[1] >> 6) & 1,
658 };
659
660 size_t start;
661 size_t len;
662 decode_range_spi25(&start, &len, &bits, data->emu_chip_size);
663
664 data->wp_start = start;
665 data->wp_end = start + len;
666}
667
668/* Checks whether range intersects a write-protected area of the flash if one is
669 * defined. */
670static bool is_write_protected(const struct emu_data *data, uint32_t start, uint32_t len)
671{
672 if (len == 0)
673 return false;
674
675 const uint32_t last = start + len - 1;
676 return (start < data->wp_end && last >= data->wp_start);
677}
678
679/* Returns non-zero on error. */
680static int write_flash_data(struct emu_data *data, uint32_t start, uint32_t len, const uint8_t *buf)
681{
682 if (is_write_protected(data, start, len)) {
683 msg_perr("At least part of the write range is write protected!\n");
684 return 1;
685 }
686
687 memcpy(data->flashchip_contents + start, buf, len);
Felix Singer2fb53b12022-08-19 03:29:32 +0200688 data->emu_modified = true;
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200689 return 0;
690}
691
692/* Returns non-zero on error. */
693static int erase_flash_data(struct emu_data *data, uint32_t start, uint32_t len)
694{
695 if (is_write_protected(data, start, len)) {
696 msg_perr("At least part of the erase range is write protected!\n");
697 return 1;
698 }
699
700 memset(data->flashchip_contents + start, 0xff, len);
Felix Singer2fb53b12022-08-19 03:29:32 +0200701 data->emu_modified = true;
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200702 return 0;
703}
704
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000705static int emulate_spi_chip_response(unsigned int writecnt,
706 unsigned int readcnt,
707 const unsigned char *writearr,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000708 unsigned char *readarr,
709 struct emu_data *data)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000710{
Stefan Tauner0b9df972012-05-07 22:12:16 +0000711 unsigned int offs, i, toread;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200712 uint8_t ro_bits;
Nico Huberbbccdb22022-05-28 16:48:26 +0200713 bool wrsr_ext2, wrsr_ext3;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000714 static int unsigned aai_offs;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000715 const unsigned char sst25vf040_rems_response[2] = {0xbf, 0x44};
716 const unsigned char sst25vf032b_rems_response[2] = {0xbf, 0x4a};
717 const unsigned char mx25l6436_rems_response[2] = {0xc2, 0x16};
Nico Huberf9632d82019-01-20 11:23:49 +0100718 const unsigned char w25q128fv_rems_response[2] = {0xef, 0x17};
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000719
720 if (writecnt == 0) {
721 msg_perr("No command sent to the chip!\n");
722 return 1;
723 }
Paul Menzelac427b22012-02-16 21:07:07 +0000724 /* spi_blacklist has precedence over spi_ignorelist. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000725 for (i = 0; i < data->spi_blacklist_size; i++) {
726 if (writearr[0] == data->spi_blacklist[i]) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000727 msg_pdbg("Refusing blacklisted SPI command 0x%02x\n",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000728 data->spi_blacklist[i]);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000729 return SPI_INVALID_OPCODE;
730 }
731 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000732 for (i = 0; i < data->spi_ignorelist_size; i++) {
733 if (writearr[0] == data->spi_ignorelist[i]) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000734 msg_cdbg("Ignoring ignorelisted SPI command 0x%02x\n",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000735 data->spi_ignorelist[i]);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000736 /* Return success because the command does not fail,
737 * it is simply ignored.
738 */
739 return 0;
740 }
741 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000742
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200743 if (data->emu_max_aai_size && (data->emu_status[0] & SPI_SR_AAI)) {
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000744 if (writearr[0] != JEDEC_AAI_WORD_PROGRAM &&
745 writearr[0] != JEDEC_WRDI &&
746 writearr[0] != JEDEC_RDSR) {
747 msg_perr("Forbidden opcode (0x%02x) attempted during "
748 "AAI sequence!\n", writearr[0]);
749 return 0;
750 }
751 }
752
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000753 switch (writearr[0]) {
754 case JEDEC_RES:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000755 if (writecnt < JEDEC_RES_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000756 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000757 /* offs calculation is only needed for SST chips which treat RES like REMS. */
758 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
759 offs += writecnt - JEDEC_REMS_OUTSIZE;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000760 switch (data->emu_chip) {
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000761 case EMULATE_ST_M25P10_RES:
762 if (readcnt > 0)
763 memset(readarr, 0x10, readcnt);
764 break;
765 case EMULATE_SST_SST25VF040_REMS:
766 for (i = 0; i < readcnt; i++)
767 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
768 break;
769 case EMULATE_SST_SST25VF032B:
770 for (i = 0; i < readcnt; i++)
771 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
772 break;
773 case EMULATE_MACRONIX_MX25L6436:
774 if (readcnt > 0)
775 memset(readarr, 0x16, readcnt);
776 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100777 case EMULATE_WINBOND_W25Q128FV:
778 if (readcnt > 0)
779 memset(readarr, 0x17, readcnt);
780 break;
Nico Huber4203a472022-05-28 17:28:05 +0200781 case EMULATE_SPANSION_S25FL128L:
782 if (readcnt > 0)
783 readarr[0] = 0x60;
784 if (readcnt > 1)
785 readarr[1] = 0x18;
786 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000787 default: /* ignore */
788 break;
789 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000790 break;
791 case JEDEC_REMS:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000792 /* REMS response has wraparound and uses an address parameter. */
793 if (writecnt < JEDEC_REMS_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000794 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000795 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
796 offs += writecnt - JEDEC_REMS_OUTSIZE;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000797 switch (data->emu_chip) {
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000798 case EMULATE_SST_SST25VF040_REMS:
799 for (i = 0; i < readcnt; i++)
800 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
801 break;
802 case EMULATE_SST_SST25VF032B:
803 for (i = 0; i < readcnt; i++)
804 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
805 break;
806 case EMULATE_MACRONIX_MX25L6436:
807 for (i = 0; i < readcnt; i++)
808 readarr[i] = mx25l6436_rems_response[(offs + i) % 2];
809 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100810 case EMULATE_WINBOND_W25Q128FV:
811 for (i = 0; i < readcnt; i++)
812 readarr[i] = w25q128fv_rems_response[(offs + i) % 2];
813 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000814 default: /* ignore */
815 break;
816 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000817 break;
818 case JEDEC_RDID:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000819 switch (data->emu_chip) {
Stefan Tauner0b9df972012-05-07 22:12:16 +0000820 case EMULATE_SST_SST25VF032B:
821 if (readcnt > 0)
822 readarr[0] = 0xbf;
823 if (readcnt > 1)
824 readarr[1] = 0x25;
825 if (readcnt > 2)
826 readarr[2] = 0x4a;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000827 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000828 case EMULATE_MACRONIX_MX25L6436:
829 if (readcnt > 0)
830 readarr[0] = 0xc2;
831 if (readcnt > 1)
832 readarr[1] = 0x20;
833 if (readcnt > 2)
834 readarr[2] = 0x17;
835 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100836 case EMULATE_WINBOND_W25Q128FV:
837 if (readcnt > 0)
838 readarr[0] = 0xef;
839 if (readcnt > 1)
840 readarr[1] = 0x40;
841 if (readcnt > 2)
842 readarr[2] = 0x18;
843 break;
Nico Huber4203a472022-05-28 17:28:05 +0200844 case EMULATE_SPANSION_S25FL128L:
845 if (readcnt > 0)
846 readarr[0] = 0x01;
847 if (readcnt > 1)
848 readarr[1] = 0x60;
849 if (readcnt > 2)
850 readarr[2] = 0x18;
851 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000852 default: /* ignore */
853 break;
854 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000855 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000856 case JEDEC_RDSR:
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200857 memset(readarr, data->emu_status[0], readcnt);
858 break;
859 case JEDEC_RDSR2:
860 if (data->emu_status_len >= 2)
861 memset(readarr, data->emu_status[1], readcnt);
862 break;
863 case JEDEC_RDSR3:
864 if (data->emu_status_len >= 3)
865 memset(readarr, data->emu_status[2], readcnt);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000866 break;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000867 /* FIXME: this should be chip-specific. */
868 case JEDEC_EWSR:
869 case JEDEC_WREN:
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200870 data->emu_status[0] |= SPI_SR_WEL;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000871 break;
872 case JEDEC_WRSR:
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200873 if (!(data->emu_status[0] & SPI_SR_WEL)) {
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000874 msg_perr("WRSR attempted, but WEL is 0!\n");
875 break;
876 }
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200877
Nico Huberbbccdb22022-05-28 16:48:26 +0200878 wrsr_ext2 = (writecnt == 3 && data->emu_wrsr_ext2);
879 wrsr_ext3 = (writecnt == 4 && data->emu_wrsr_ext3);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200880
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000881 /* FIXME: add some reasonable simulation of the busy flag */
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200882
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200883 ro_bits = get_reg_ro_bit_mask(data, STATUS1);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200884 data->emu_status[0] &= ro_bits;
885 data->emu_status[0] |= writearr[1] & ~ro_bits;
Nico Huberbbccdb22022-05-28 16:48:26 +0200886 if (wrsr_ext2 || wrsr_ext3) {
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200887 ro_bits = get_reg_ro_bit_mask(data, STATUS2);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200888 data->emu_status[1] &= ro_bits;
889 data->emu_status[1] |= writearr[2] & ~ro_bits;
890 }
Nico Huberbbccdb22022-05-28 16:48:26 +0200891 if (wrsr_ext3) {
892 ro_bits = get_reg_ro_bit_mask(data, STATUS3);
893 data->emu_status[2] &= ro_bits;
894 data->emu_status[2] |= writearr[3] & ~ro_bits;
895 }
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200896
Nico Huberbbccdb22022-05-28 16:48:26 +0200897 if (wrsr_ext3)
898 msg_pdbg2("WRSR wrote 0x%02x%02x%02x.\n", data->emu_status[2], data->emu_status[1], data->emu_status[0]);
899 else if (wrsr_ext2)
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200900 msg_pdbg2("WRSR wrote 0x%02x%02x.\n", data->emu_status[1], data->emu_status[0]);
901 else
902 msg_pdbg2("WRSR wrote 0x%02x.\n", data->emu_status[0]);
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200903
904 update_write_protection(data);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200905 break;
906 case JEDEC_WRSR2:
907 if (data->emu_status_len < 2)
908 break;
909 if (!(data->emu_status[0] & SPI_SR_WEL)) {
910 msg_perr("WRSR2 attempted, but WEL is 0!\n");
911 break;
912 }
913
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200914 ro_bits = get_reg_ro_bit_mask(data, STATUS2);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200915 data->emu_status[1] &= ro_bits;
916 data->emu_status[1] |= (writearr[1] & ~ro_bits);
917
918 msg_pdbg2("WRSR2 wrote 0x%02x.\n", data->emu_status[1]);
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200919
920 update_write_protection(data);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200921 break;
922 case JEDEC_WRSR3:
923 if (data->emu_status_len < 3)
924 break;
925 if (!(data->emu_status[0] & SPI_SR_WEL)) {
926 msg_perr("WRSR3 attempted, but WEL is 0!\n");
927 break;
928 }
929
Sergii Dmytruk27835ea2021-11-08 00:06:33 +0200930 ro_bits = get_reg_ro_bit_mask(data, STATUS3);
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200931 data->emu_status[2] &= ro_bits;
932 data->emu_status[2] |= (writearr[1] & ~ro_bits);
933
934 msg_pdbg2("WRSR3 wrote 0x%02x.\n", data->emu_status[2]);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000935 break;
936 case JEDEC_READ:
937 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
938 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000939 offs %= data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000940 if (readcnt > 0)
Edward O'Callaghan94250222021-05-20 20:34:02 +1000941 memcpy(readarr, data->flashchip_contents + offs, readcnt);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000942 break;
943 case JEDEC_BYTE_PROGRAM:
944 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
945 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000946 offs %= data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000947 if (writecnt < 5) {
948 msg_perr("BYTE PROGRAM size too short!\n");
949 return 1;
950 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000951 if (writecnt - 4 > data->emu_max_byteprogram_size) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000952 msg_perr("Max BYTE PROGRAM size exceeded!\n");
953 return 1;
954 }
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200955 if (write_flash_data(data, offs, writecnt - 4, writearr + 4)) {
956 msg_perr("Failed to program flash!\n");
957 return 1;
958 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000959 break;
960 case JEDEC_AAI_WORD_PROGRAM:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000961 if (!data->emu_max_aai_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000962 break;
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200963 if (!(data->emu_status[0] & SPI_SR_AAI)) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000964 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
965 msg_perr("Initial AAI WORD PROGRAM size too "
966 "short!\n");
967 return 1;
968 }
969 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
970 msg_perr("Initial AAI WORD PROGRAM size too "
971 "long!\n");
972 return 1;
973 }
Sergii Dmytruk59151a42021-11-08 00:05:12 +0200974 data->emu_status[0] |= SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000975 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
976 writearr[3];
977 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000978 aai_offs %= data->emu_chip_size;
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200979 if (write_flash_data(data, aai_offs, 2, writearr + 4)) {
980 msg_perr("Failed to program flash!\n");
981 return 1;
982 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000983 aai_offs += 2;
984 } else {
985 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
986 msg_perr("Continuation AAI WORD PROGRAM size "
987 "too short!\n");
988 return 1;
989 }
990 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
991 msg_perr("Continuation AAI WORD PROGRAM size "
992 "too long!\n");
993 return 1;
994 }
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +0200995 if (write_flash_data(data, aai_offs, 2, writearr + 1)) {
996 msg_perr("Failed to program flash!\n");
997 return 1;
998 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000999 aai_offs += 2;
1000 }
1001 break;
1002 case JEDEC_WRDI:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001003 if (data->emu_max_aai_size)
Sergii Dmytruk59151a42021-11-08 00:05:12 +02001004 data->emu_status[0] &= ~SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001005 break;
1006 case JEDEC_SE:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001007 if (!data->emu_jedec_se_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001008 break;
1009 if (writecnt != JEDEC_SE_OUTSIZE) {
1010 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
1011 return 1;
1012 }
1013 if (readcnt != JEDEC_SE_INSIZE) {
1014 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
1015 return 1;
1016 }
1017 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +10001018 if (offs & (data->emu_jedec_se_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +00001019 msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +10001020 offs &= ~(data->emu_jedec_se_size - 1);
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +02001021 if (erase_flash_data(data, offs, data->emu_jedec_se_size)) {
1022 msg_perr("Failed to erase flash!\n");
1023 return 1;
1024 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001025 break;
1026 case JEDEC_BE_52:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001027 if (!data->emu_jedec_be_52_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001028 break;
1029 if (writecnt != JEDEC_BE_52_OUTSIZE) {
1030 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
1031 return 1;
1032 }
1033 if (readcnt != JEDEC_BE_52_INSIZE) {
1034 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
1035 return 1;
1036 }
1037 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +10001038 if (offs & (data->emu_jedec_be_52_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +00001039 msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +10001040 offs &= ~(data->emu_jedec_be_52_size - 1);
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +02001041 if (erase_flash_data(data, offs, data->emu_jedec_be_52_size)) {
1042 msg_perr("Failed to erase flash!\n");
1043 return 1;
1044 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001045 break;
1046 case JEDEC_BE_D8:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001047 if (!data->emu_jedec_be_d8_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001048 break;
1049 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
1050 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
1051 return 1;
1052 }
1053 if (readcnt != JEDEC_BE_D8_INSIZE) {
1054 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
1055 return 1;
1056 }
1057 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +10001058 if (offs & (data->emu_jedec_be_d8_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +00001059 msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +10001060 offs &= ~(data->emu_jedec_be_d8_size - 1);
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +02001061 if (erase_flash_data(data, offs, data->emu_jedec_be_d8_size)) {
1062 msg_perr("Failed to erase flash!\n");
1063 return 1;
1064 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001065 break;
1066 case JEDEC_CE_60:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001067 if (!data->emu_jedec_ce_60_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001068 break;
1069 if (writecnt != JEDEC_CE_60_OUTSIZE) {
1070 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
1071 return 1;
1072 }
1073 if (readcnt != JEDEC_CE_60_INSIZE) {
1074 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
1075 return 1;
1076 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +00001077 /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001078 /* emu_jedec_ce_60_size is emu_chip_size. */
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +02001079 if (erase_flash_data(data, 0, data->emu_jedec_ce_60_size)) {
1080 msg_perr("Failed to erase flash!\n");
1081 return 1;
1082 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001083 break;
1084 case JEDEC_CE_C7:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001085 if (!data->emu_jedec_ce_c7_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001086 break;
1087 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
1088 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
1089 return 1;
1090 }
1091 if (readcnt != JEDEC_CE_C7_INSIZE) {
1092 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
1093 return 1;
1094 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +00001095 /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001096 /* emu_jedec_ce_c7_size is emu_chip_size. */
Sergii Dmytruk2fc70dc2021-11-08 01:38:52 +02001097 if (erase_flash_data(data, 0, data->emu_jedec_ce_c7_size)) {
1098 msg_perr("Failed to erase flash!\n");
1099 return 1;
1100 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001101 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +00001102 case JEDEC_SFDP:
Lachlan Bishopc753c402020-09-10 14:57:05 +10001103 if (data->emu_chip != EMULATE_MACRONIX_MX25L6436)
Stefan Tauner0b9df972012-05-07 22:12:16 +00001104 break;
1105 if (writecnt < 4)
1106 break;
1107 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
1108
1109 /* SFDP expects one dummy byte after the address. */
1110 if (writecnt == 4) {
1111 /* The dummy byte was not written, make sure it is read instead.
1112 * Shifting and shortening the read array does achieve this goal.
1113 */
1114 readarr++;
1115 readcnt--;
1116 } else {
1117 /* The response is shifted if more than 5 bytes are written, because SFDP data is
1118 * already shifted out by the chip while those superfluous bytes are written. */
1119 offs += writecnt - 5;
1120 }
1121
1122 /* The SFDP spec implies that the start address of an SFDP read may be truncated to fit in the
1123 * SFDP table address space, i.e. the start address may be wrapped around at SFDP table size.
1124 * This is a reasonable implementation choice in hardware because it saves a few gates. */
1125 if (offs >= sizeof(sfdp_table)) {
1126 msg_pdbg("Wrapping the start address around the SFDP table boundary (using 0x%x "
1127 "instead of 0x%x).\n", (unsigned int)(offs % sizeof(sfdp_table)), offs);
1128 offs %= sizeof(sfdp_table);
1129 }
1130 toread = min(sizeof(sfdp_table) - offs, readcnt);
1131 memcpy(readarr, sfdp_table + offs, toread);
1132 if (toread < readcnt)
1133 msg_pdbg("Crossing the SFDP table boundary in a single "
1134 "continuous chunk produces undefined results "
1135 "after that point.\n");
1136 break;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001137 default:
1138 /* No special response. */
1139 break;
1140 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +00001141 if (writearr[0] != JEDEC_WREN && writearr[0] != JEDEC_EWSR)
Sergii Dmytruk59151a42021-11-08 00:05:12 +02001142 data->emu_status[0] &= ~SPI_SR_WEL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001143 return 0;
1144}
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001145
Edward O'Callaghan5eca4272020-04-12 17:27:53 +10001146static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001147 unsigned int readcnt,
1148 const unsigned char *writearr,
1149 unsigned char *readarr)
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001150{
Nico Huber519be662018-12-23 20:03:35 +01001151 unsigned int i;
Nico Huber9a11cbf2023-01-13 01:19:07 +01001152 struct emu_data *emu_data = flash->mst.spi->data;
Lachlan Bishopc753c402020-09-10 14:57:05 +10001153 if (!emu_data) {
1154 msg_perr("No data in flash context!\n");
1155 return 1;
1156 }
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001157
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +00001158 msg_pspew("%s:", __func__);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001159
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +00001160 msg_pspew(" writing %u bytes:", writecnt);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001161 for (i = 0; i < writecnt; i++)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +00001162 msg_pspew(" 0x%02x", writearr[i]);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001163
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001164 /* Response for unknown commands and missing chip is 0xff. */
1165 memset(readarr, 0xff, readcnt);
Lachlan Bishopc753c402020-09-10 14:57:05 +10001166 switch (emu_data->emu_chip) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001167 case EMULATE_ST_M25P10_RES:
1168 case EMULATE_SST_SST25VF040_REMS:
1169 case EMULATE_SST_SST25VF032B:
Stefan Tauner0b9df972012-05-07 22:12:16 +00001170 case EMULATE_MACRONIX_MX25L6436:
Nico Huberf9632d82019-01-20 11:23:49 +01001171 case EMULATE_WINBOND_W25Q128FV:
Nico Huber4203a472022-05-28 17:28:05 +02001172 case EMULATE_SPANSION_S25FL128L:
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001173 if (emulate_spi_chip_response(writecnt, readcnt, writearr,
Lachlan Bishopc753c402020-09-10 14:57:05 +10001174 readarr, emu_data)) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +00001175 msg_pdbg("Invalid command sent to flash chip!\n");
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001176 return 1;
1177 }
1178 break;
1179 default:
1180 break;
1181 }
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +00001182 msg_pspew(" reading %u bytes:", readcnt);
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001183 for (i = 0; i < readcnt; i++)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00001184 msg_pspew(" 0x%02x", readarr[i]);
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +00001185 msg_pspew("\n");
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +00001186 return 0;
1187}
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +00001188
Mark Marshallf20b7be2014-05-09 21:16:21 +00001189static 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 +00001190{
Nico Huber9a11cbf2023-01-13 01:19:07 +01001191 const struct emu_data *const data = flash->mst.spi->data;
Edward O'Callaghanb1313422021-05-20 20:27:59 +10001192 return spi_write_chunked(flash, buf, start, len, data->spi_write_256_chunksize);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001193}
Thomas Heijligencc853d82021-05-04 15:32:17 +02001194
Nikolai Artemieve7a41e32022-11-28 17:40:56 +11001195static bool dummy_spi_probe_opcode(const struct flashctx *flash, uint8_t opcode)
Aarya Chaumal0cea7532022-07-04 18:21:50 +05301196{
1197 size_t i;
Nico Huber9a11cbf2023-01-13 01:19:07 +01001198 const struct emu_data *emu_data = flash->mst.spi->data;
Aarya Chaumal0cea7532022-07-04 18:21:50 +05301199 for (i = 0; i < emu_data->spi_blacklist_size; i++) {
1200 if (emu_data->spi_blacklist[i] == opcode)
1201 return false;
1202 }
1203 return true;
1204}
1205
Thomas Heijligencc853d82021-05-04 15:32:17 +02001206const struct programmer_entry programmer_dummy = {
1207 .name = "dummy",
1208 .type = OTHER,
1209 /* FIXME */
1210 .devs.note = "Dummy device, does nothing and logs all accesses\n",
1211 .init = dummy_init,
Thomas Heijligencc853d82021-05-04 15:32:17 +02001212};