blob: ae39c48d0be17787d77f51cc2b0e6d54b2dc8eef [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
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000016#include <string.h>
17#include <stdlib.h>
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +000018#include <stdio.h>
19#include <ctype.h>
Stefan Tauner5e695ab2012-05-06 17:03:40 +000020#include <errno.h>
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000021#include "flash.h"
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +000022#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000023#include "programmer.h"
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000024
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000025#include "spi.h"
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000026
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000027#include <sys/types.h>
28#include <sys/stat.h>
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000029
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000030enum emu_chip {
31 EMULATE_NONE,
32 EMULATE_ST_M25P10_RES,
33 EMULATE_SST_SST25VF040_REMS,
34 EMULATE_SST_SST25VF032B,
Stefan Tauner0b9df972012-05-07 22:12:16 +000035 EMULATE_MACRONIX_MX25L6436,
Nico Huberf9632d82019-01-20 11:23:49 +010036 EMULATE_WINBOND_W25Q128FV,
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000037};
Stefan Tauner0b9df972012-05-07 22:12:16 +000038
Lachlan Bishopc753c402020-09-10 14:57:05 +100039struct emu_data {
40 enum emu_chip emu_chip;
41 char *emu_persistent_image;
42 unsigned int emu_chip_size;
43 int emu_modified; /* is the image modified since reading it? */
44 uint8_t emu_status;
45 unsigned int emu_max_byteprogram_size;
46 unsigned int emu_max_aai_size;
47 unsigned int emu_jedec_se_size;
48 unsigned int emu_jedec_be_52_size;
49 unsigned int emu_jedec_be_d8_size;
50 unsigned int emu_jedec_ce_60_size;
51 unsigned int emu_jedec_ce_c7_size;
52 unsigned char spi_blacklist[256];
53 unsigned char spi_ignorelist[256];
54 unsigned int spi_blacklist_size;
55 unsigned int spi_ignorelist_size;
Edward O'Callaghan94250222021-05-20 20:34:02 +100056
57 uint8_t *flashchip_contents;
Lachlan Bishopc753c402020-09-10 14:57:05 +100058};
59
Stefan Tauner0b9df972012-05-07 22:12:16 +000060/* A legit complete SFDP table based on the MX25L6436E (rev. 1.8) datasheet. */
Stefan Tauner67d163d2013-01-15 17:37:48 +000061static const uint8_t sfdp_table[] = {
Stefan Tauner0b9df972012-05-07 22:12:16 +000062 0x53, 0x46, 0x44, 0x50, // @0x00: SFDP signature
63 0x00, 0x01, 0x01, 0xFF, // @0x04: revision 1.0, 2 headers
64 0x00, 0x00, 0x01, 0x09, // @0x08: JEDEC SFDP header rev. 1.0, 9 DW long
65 0x1C, 0x00, 0x00, 0xFF, // @0x0C: PTP0 = 0x1C (instead of 0x30)
66 0xC2, 0x00, 0x01, 0x04, // @0x10: Macronix header rev. 1.0, 4 DW long
67 0x48, 0x00, 0x00, 0xFF, // @0x14: PTP1 = 0x48 (instead of 0x60)
68 0xFF, 0xFF, 0xFF, 0xFF, // @0x18: hole.
69 0xE5, 0x20, 0xC9, 0xFF, // @0x1C: SFDP parameter table start
70 0xFF, 0xFF, 0xFF, 0x03, // @0x20
71 0x00, 0xFF, 0x08, 0x6B, // @0x24
72 0x08, 0x3B, 0x00, 0xFF, // @0x28
73 0xEE, 0xFF, 0xFF, 0xFF, // @0x2C
74 0xFF, 0xFF, 0x00, 0x00, // @0x30
75 0xFF, 0xFF, 0x00, 0xFF, // @0x34
76 0x0C, 0x20, 0x0F, 0x52, // @0x38
77 0x10, 0xD8, 0x00, 0xFF, // @0x3C: SFDP parameter table end
78 0xFF, 0xFF, 0xFF, 0xFF, // @0x40: hole.
79 0xFF, 0xFF, 0xFF, 0xFF, // @0x44: hole.
80 0x00, 0x36, 0x00, 0x27, // @0x48: Macronix parameter table start
81 0xF4, 0x4F, 0xFF, 0xFF, // @0x4C
82 0xD9, 0xC8, 0xFF, 0xFF, // @0x50
83 0xFF, 0xFF, 0xFF, 0xFF, // @0x54: Macronix parameter table end
84};
85
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000086
Stefan Taunerc69c9c82011-11-23 09:13:48 +000087static unsigned int spi_write_256_chunksize = 256;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000088
Edward O'Callaghan5eca4272020-04-12 17:27:53 +100089static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
Mark Marshallf20b7be2014-05-09 21:16:21 +000090 const unsigned char *writearr, unsigned char *readarr);
91static int dummy_spi_write_256(struct flashctx *flash, const uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +000092 unsigned int start, unsigned int len);
Mark Marshallf20b7be2014-05-09 21:16:21 +000093static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr);
94static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr);
95static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr);
96static void dummy_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len);
97static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr);
98static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr);
99static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr);
100static void dummy_chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len);
Michael Karcherb9dbe482011-05-11 17:07:07 +0000101
Lachlan Bishopc753c402020-09-10 14:57:05 +1000102static struct spi_master spi_master_dummyflasher = {
Nico Huber1cf407b2017-11-10 20:18:23 +0100103 .features = SPI_MASTER_4BA,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000104 .max_data_read = MAX_DATA_READ_UNLIMITED,
105 .max_data_write = MAX_DATA_UNSPECIFIED,
106 .command = dummy_spi_send_command,
107 .multicommand = default_spi_send_multicommand,
108 .read = default_spi_read,
109 .write_256 = dummy_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +0000110 .write_aai = default_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000111};
David Hendricks8bb20212011-06-14 01:35:36 +0000112
Lachlan Bishopc753c402020-09-10 14:57:05 +1000113static struct par_master par_master_dummy = {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000114 .chip_readb = dummy_chip_readb,
115 .chip_readw = dummy_chip_readw,
116 .chip_readl = dummy_chip_readl,
117 .chip_readn = dummy_chip_readn,
118 .chip_writeb = dummy_chip_writeb,
119 .chip_writew = dummy_chip_writew,
120 .chip_writel = dummy_chip_writel,
121 .chip_writen = dummy_chip_writen,
122};
123
David Hendricks8bb20212011-06-14 01:35:36 +0000124static int dummy_shutdown(void *data)
125{
126 msg_pspew("%s\n", __func__);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000127 struct emu_data *emu_data = (struct emu_data *)data;
128 if (emu_data->emu_chip != EMULATE_NONE) {
129 if (emu_data->emu_persistent_image && emu_data->emu_modified) {
130 msg_pdbg("Writing %s\n", emu_data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000131 write_buf_to_file(emu_data->flashchip_contents,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000132 emu_data->emu_chip_size,
133 emu_data->emu_persistent_image);
David Hendricks8bb20212011-06-14 01:35:36 +0000134 }
Angel Pons02b9ae22021-05-25 12:46:43 +0200135 free(emu_data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000136 free(emu_data->flashchip_contents);
David Hendricks8bb20212011-06-14 01:35:36 +0000137 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000138 free(data);
David Hendricks8bb20212011-06-14 01:35:36 +0000139 return 0;
140}
141
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000142static int init_data(struct emu_data *data, enum chipbustype *dummy_buses_supported)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000143{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000144 char *bustext = NULL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000145 char *tmp = NULL;
Nico Huber519be662018-12-23 20:03:35 +0100146 unsigned int i;
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000147 char *endptr;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000148 char *status = NULL;
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000149
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000150 bustext = extract_programmer_param("bus");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000151 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
152 if (!bustext)
153 bustext = strdup("parallel+lpc+fwh+spi");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000154 /* Convert the parameters to lowercase. */
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000155 tolower_string(bustext);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000156
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000157 *dummy_buses_supported = BUS_NONE;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000158 if (strstr(bustext, "parallel")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000159 *dummy_buses_supported |= BUS_PARALLEL;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000160 msg_pdbg("Enabling support for %s flash.\n", "parallel");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000161 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000162 if (strstr(bustext, "lpc")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000163 *dummy_buses_supported |= BUS_LPC;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000164 msg_pdbg("Enabling support for %s flash.\n", "LPC");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000165 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000166 if (strstr(bustext, "fwh")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000167 *dummy_buses_supported |= BUS_FWH;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000168 msg_pdbg("Enabling support for %s flash.\n", "FWH");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000169 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000170 if (strstr(bustext, "spi")) {
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000171 *dummy_buses_supported |= BUS_SPI;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000172 msg_pdbg("Enabling support for %s flash.\n", "SPI");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000173 }
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000174 if (*dummy_buses_supported == BUS_NONE)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000175 msg_pdbg("Support for all flash bus types disabled.\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000176 free(bustext);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000177
178 tmp = extract_programmer_param("spi_write_256_chunksize");
179 if (tmp) {
Edward O'Callaghanc785f882021-05-23 22:14:36 +1000180 spi_write_256_chunksize = strtoul(tmp, &endptr, 0);
181 if (*endptr != '\0' || spi_write_256_chunksize < 1) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000182 msg_perr("invalid spi_write_256_chunksize\n");
Edward O'Callaghanc785f882021-05-23 22:14:36 +1000183 free(tmp);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000184 return 1;
185 }
Edward O'Callaghanc785f882021-05-23 22:14:36 +1000186 free(tmp);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000187 }
188
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000189 tmp = extract_programmer_param("spi_blacklist");
190 if (tmp) {
191 i = strlen(tmp);
192 if (!strncmp(tmp, "0x", 2)) {
193 i -= 2;
194 memmove(tmp, tmp + 2, i + 1);
195 }
196 if ((i > 512) || (i % 2)) {
197 msg_perr("Invalid SPI command blacklist length\n");
198 free(tmp);
199 return 1;
200 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000201 data->spi_blacklist_size = i / 2;
202 for (i = 0; i < data->spi_blacklist_size * 2; i++) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000203 if (!isxdigit((unsigned char)tmp[i])) {
204 msg_perr("Invalid char \"%c\" in SPI command "
205 "blacklist\n", tmp[i]);
206 free(tmp);
207 return 1;
208 }
209 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000210 for (i = 0; i < data->spi_blacklist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000211 unsigned int tmp2;
212 /* SCNx8 is apparently not supported by MSVC (and thus
213 * MinGW), so work around it with an extra variable
214 */
215 sscanf(tmp + i * 2, "%2x", &tmp2);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000216 data->spi_blacklist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000217 }
218 msg_pdbg("SPI blacklist is ");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000219 for (i = 0; i < data->spi_blacklist_size; i++)
220 msg_pdbg("%02x ", data->spi_blacklist[i]);
221 msg_pdbg(", size %u\n", data->spi_blacklist_size);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000222 }
223 free(tmp);
224
225 tmp = extract_programmer_param("spi_ignorelist");
226 if (tmp) {
227 i = strlen(tmp);
228 if (!strncmp(tmp, "0x", 2)) {
229 i -= 2;
230 memmove(tmp, tmp + 2, i + 1);
231 }
232 if ((i > 512) || (i % 2)) {
233 msg_perr("Invalid SPI command ignorelist length\n");
234 free(tmp);
235 return 1;
236 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000237 data->spi_ignorelist_size = i / 2;
238 for (i = 0; i < data->spi_ignorelist_size * 2; i++) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000239 if (!isxdigit((unsigned char)tmp[i])) {
240 msg_perr("Invalid char \"%c\" in SPI command "
241 "ignorelist\n", tmp[i]);
242 free(tmp);
243 return 1;
244 }
245 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000246 for (i = 0; i < data->spi_ignorelist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000247 unsigned int tmp2;
248 /* SCNx8 is apparently not supported by MSVC (and thus
249 * MinGW), so work around it with an extra variable
250 */
251 sscanf(tmp + i * 2, "%2x", &tmp2);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000252 data->spi_ignorelist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000253 }
254 msg_pdbg("SPI ignorelist is ");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000255 for (i = 0; i < data->spi_ignorelist_size; i++)
256 msg_pdbg("%02x ", data->spi_ignorelist[i]);
257 msg_pdbg(", size %u\n", data->spi_ignorelist_size);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000258 }
259 free(tmp);
260
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000261 tmp = extract_programmer_param("emulate");
262 if (!tmp) {
263 msg_pdbg("Not emulating any flash chip.\n");
264 /* Nothing else to do. */
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000265 return 0;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000266 }
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000267
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000268 if (!strcmp(tmp, "M25P10.RES")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000269 data->emu_chip = EMULATE_ST_M25P10_RES;
270 data->emu_chip_size = 128 * 1024;
271 data->emu_max_byteprogram_size = 128;
272 data->emu_max_aai_size = 0;
273 data->emu_jedec_se_size = 0;
274 data->emu_jedec_be_52_size = 0;
275 data->emu_jedec_be_d8_size = 32 * 1024;
276 data->emu_jedec_ce_60_size = 0;
277 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000278 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
279 "write)\n");
280 }
281 if (!strcmp(tmp, "SST25VF040.REMS")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000282 data->emu_chip = EMULATE_SST_SST25VF040_REMS;
283 data->emu_chip_size = 512 * 1024;
284 data->emu_max_byteprogram_size = 1;
285 data->emu_max_aai_size = 0;
286 data->emu_jedec_se_size = 4 * 1024;
287 data->emu_jedec_be_52_size = 32 * 1024;
288 data->emu_jedec_be_d8_size = 0;
289 data->emu_jedec_ce_60_size = data->emu_chip_size;
290 data->emu_jedec_ce_c7_size = 0;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000291 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
292 "byte write)\n");
293 }
294 if (!strcmp(tmp, "SST25VF032B")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000295 data->emu_chip = EMULATE_SST_SST25VF032B;
296 data->emu_chip_size = 4 * 1024 * 1024;
297 data->emu_max_byteprogram_size = 1;
298 data->emu_max_aai_size = 2;
299 data->emu_jedec_se_size = 4 * 1024;
300 data->emu_jedec_be_52_size = 32 * 1024;
301 data->emu_jedec_be_d8_size = 64 * 1024;
302 data->emu_jedec_ce_60_size = data->emu_chip_size;
303 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000304 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
305 "write)\n");
306 }
Stefan Tauner0b9df972012-05-07 22:12:16 +0000307 if (!strcmp(tmp, "MX25L6436")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000308 data->emu_chip = EMULATE_MACRONIX_MX25L6436;
309 data->emu_chip_size = 8 * 1024 * 1024;
310 data->emu_max_byteprogram_size = 256;
311 data->emu_max_aai_size = 0;
312 data->emu_jedec_se_size = 4 * 1024;
313 data->emu_jedec_be_52_size = 32 * 1024;
314 data->emu_jedec_be_d8_size = 64 * 1024;
315 data->emu_jedec_ce_60_size = data->emu_chip_size;
316 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000317 msg_pdbg("Emulating Macronix MX25L6436 SPI flash chip (RDID, "
318 "SFDP)\n");
319 }
Nico Huberf9632d82019-01-20 11:23:49 +0100320 if (!strcmp(tmp, "W25Q128FV")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000321 data->emu_chip = EMULATE_WINBOND_W25Q128FV;
322 data->emu_chip_size = 16 * 1024 * 1024;
323 data->emu_max_byteprogram_size = 256;
324 data->emu_max_aai_size = 0;
325 data->emu_jedec_se_size = 4 * 1024;
326 data->emu_jedec_be_52_size = 32 * 1024;
327 data->emu_jedec_be_d8_size = 64 * 1024;
328 data->emu_jedec_ce_60_size = data->emu_chip_size;
329 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Nico Huberf9632d82019-01-20 11:23:49 +0100330 msg_pdbg("Emulating Winbond W25Q128FV SPI flash chip (RDID)\n");
331 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000332 if (data->emu_chip == EMULATE_NONE) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000333 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
334 free(tmp);
335 return 1;
336 }
337 free(tmp);
David Hendricks8bb20212011-06-14 01:35:36 +0000338
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000339 status = extract_programmer_param("spi_status");
340 if (status) {
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000341 errno = 0;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000342 data->emu_status = strtoul(status, &endptr, 0);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000343 if (errno != 0 || status == endptr) {
Angel Ponsc2484642021-05-25 13:03:24 +0200344 free(status);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000345 msg_perr("Error: initial status register specified, "
346 "but the value could not be converted.\n");
347 return 1;
348 }
Angel Ponsc2484642021-05-25 13:03:24 +0200349 free(status);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000350 msg_pdbg("Initial status register is set to 0x%02x.\n",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000351 data->emu_status);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000352 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000353
Angel Pons328898a2021-05-25 12:56:18 +0200354 data->flashchip_contents = malloc(data->emu_chip_size);
355 if (!data->flashchip_contents) {
356 msg_perr("Out of memory!\n");
357 return 1;
358 }
359
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000360
361 return 0;
362}
363
Thomas Heijligencc853d82021-05-04 15:32:17 +0200364static int dummy_init(void)
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000365{
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000366 struct stat image_stat;
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000367
368 struct emu_data *data = calloc(1, sizeof(struct emu_data));
369 if (!data) {
370 msg_perr("Out of memory!\n");
371 return 1;
372 }
373 data->emu_chip = EMULATE_NONE;
374 spi_master_dummyflasher.data = data;
375 par_master_dummy.data = data;
376
377 msg_pspew("%s\n", __func__);
378
379 enum chipbustype dummy_buses_supported;
380 if (init_data(data, &dummy_buses_supported)) {
381 free(data);
382 return 1;
383 }
384
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000385 if (data->emu_chip == EMULATE_NONE) {
386 msg_pdbg("Not emulating any flash chip.\n");
387 /* Nothing else to do. */
388 goto dummy_init_out;
389 }
390
Lachlan Bishopc753c402020-09-10 14:57:05 +1000391 msg_pdbg("Filling fake flash chip with 0xff, size %i\n", data->emu_chip_size);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000392 memset(data->flashchip_contents, 0xff, data->emu_chip_size);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000393
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000394 /* Will be freed by shutdown function if necessary. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000395 data->emu_persistent_image = extract_programmer_param("image");
396 if (!data->emu_persistent_image) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000397 /* Nothing else to do. */
David Hendricks8bb20212011-06-14 01:35:36 +0000398 goto dummy_init_out;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000399 }
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000400 /* We will silently (in default verbosity) ignore the file if it does not exist (yet) or the size does
401 * not match the emulated chip. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000402 if (!stat(data->emu_persistent_image, &image_stat)) {
Stefan Tauner23e10b82016-01-23 16:16:49 +0000403 msg_pdbg("Found persistent image %s, %jd B ",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000404 data->emu_persistent_image, (intmax_t)image_stat.st_size);
405 if ((uintmax_t)image_stat.st_size == data->emu_chip_size) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000406 msg_pdbg("matches.\n");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000407 msg_pdbg("Reading %s\n", data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000408 if (read_buf_from_file(data->flashchip_contents, data->emu_chip_size,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000409 data->emu_persistent_image)) {
410 msg_perr("Unable to read %s\n", data->emu_persistent_image);
Angel Pons02b9ae22021-05-25 12:46:43 +0200411 free(data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000412 free(data->flashchip_contents);
Anastasia Klimchuk3c55c792021-05-31 09:42:36 +1000413 free(data);
Jacob Garberca598da2019-08-12 10:44:17 -0600414 return 1;
415 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000416 } else {
417 msg_pdbg("doesn't match.\n");
418 }
419 }
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000420
David Hendricks8bb20212011-06-14 01:35:36 +0000421dummy_init_out:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000422 if (register_shutdown(dummy_shutdown, data)) {
Angel Pons02b9ae22021-05-25 12:46:43 +0200423 free(data->emu_persistent_image);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000424 free(data->flashchip_contents);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000425 free(data);
David Hendricks8bb20212011-06-14 01:35:36 +0000426 return 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000427 }
Edward O'Callaghan3fa321d2021-05-17 20:01:27 +1000428 if (dummy_buses_supported & BUS_NONSPI)
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000429 register_par_master(&par_master_dummy,
Edward O'Callaghan3fa321d2021-05-17 20:01:27 +1000430 dummy_buses_supported & BUS_NONSPI);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000431 if (dummy_buses_supported & BUS_SPI)
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000432 register_spi_master(&spi_master_dummyflasher);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000433
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000434 return 0;
435}
436
Thomas Heijligencc853d82021-05-04 15:32:17 +0200437static void *dummy_map(const char *descr, uintptr_t phys_addr, size_t len)
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000438{
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000439 msg_pspew("%s: Mapping %s, 0x%zx bytes at 0x%0*" PRIxPTR "\n",
Stefan Tauner0554ca52013-07-25 22:54:25 +0000440 __func__, descr, len, PRIxPTR_WIDTH, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000441 return (void *)phys_addr;
442}
443
Thomas Heijligencc853d82021-05-04 15:32:17 +0200444static void dummy_unmap(void *virt_addr, size_t len)
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000445{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000446 msg_pspew("%s: Unmapping 0x%zx bytes at %p\n", __func__, len, virt_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000447}
448
Mark Marshallf20b7be2014-05-09 21:16:21 +0000449static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000450{
Stefan Taunerc2333752013-07-13 23:31:37 +0000451 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%02x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000452}
453
Mark Marshallf20b7be2014-05-09 21:16:21 +0000454static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000455{
Stefan Taunerc2333752013-07-13 23:31:37 +0000456 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%04x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000457}
458
Mark Marshallf20b7be2014-05-09 21:16:21 +0000459static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000460{
Stefan Taunerc2333752013-07-13 23:31:37 +0000461 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%08x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000462}
463
Mark Marshallf20b7be2014-05-09 21:16:21 +0000464static 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 +0000465{
466 size_t i;
Stefan Tauner0554ca52013-07-25 22:54:25 +0000467 msg_pspew("%s: addr=0x%" PRIxPTR ", len=0x%zx, writing data (hex):", __func__, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000468 for (i = 0; i < len; i++) {
469 if ((i % 16) == 0)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000470 msg_pspew("\n");
471 msg_pspew("%02x ", buf[i]);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000472 }
473}
474
Mark Marshallf20b7be2014-05-09 21:16:21 +0000475static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000476{
Stefan Taunerc2333752013-07-13 23:31:37 +0000477 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000478 return 0xff;
479}
480
Mark Marshallf20b7be2014-05-09 21:16:21 +0000481static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000482{
Stefan Taunerc2333752013-07-13 23:31:37 +0000483 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000484 return 0xffff;
485}
486
Mark Marshallf20b7be2014-05-09 21:16:21 +0000487static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000488{
Stefan Taunerc2333752013-07-13 23:31:37 +0000489 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffffffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000490 return 0xffffffff;
491}
492
Mark Marshallf20b7be2014-05-09 21:16:21 +0000493static 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 +0000494{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000495 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 +0000496 memset(buf, 0xff, len);
497 return;
498}
499
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000500static int emulate_spi_chip_response(unsigned int writecnt,
501 unsigned int readcnt,
502 const unsigned char *writearr,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000503 unsigned char *readarr,
504 struct emu_data *data)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000505{
Stefan Tauner0b9df972012-05-07 22:12:16 +0000506 unsigned int offs, i, toread;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000507 static int unsigned aai_offs;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000508 const unsigned char sst25vf040_rems_response[2] = {0xbf, 0x44};
509 const unsigned char sst25vf032b_rems_response[2] = {0xbf, 0x4a};
510 const unsigned char mx25l6436_rems_response[2] = {0xc2, 0x16};
Nico Huberf9632d82019-01-20 11:23:49 +0100511 const unsigned char w25q128fv_rems_response[2] = {0xef, 0x17};
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000512
513 if (writecnt == 0) {
514 msg_perr("No command sent to the chip!\n");
515 return 1;
516 }
Paul Menzelac427b22012-02-16 21:07:07 +0000517 /* spi_blacklist has precedence over spi_ignorelist. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000518 for (i = 0; i < data->spi_blacklist_size; i++) {
519 if (writearr[0] == data->spi_blacklist[i]) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000520 msg_pdbg("Refusing blacklisted SPI command 0x%02x\n",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000521 data->spi_blacklist[i]);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000522 return SPI_INVALID_OPCODE;
523 }
524 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000525 for (i = 0; i < data->spi_ignorelist_size; i++) {
526 if (writearr[0] == data->spi_ignorelist[i]) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000527 msg_cdbg("Ignoring ignorelisted SPI command 0x%02x\n",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000528 data->spi_ignorelist[i]);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000529 /* Return success because the command does not fail,
530 * it is simply ignored.
531 */
532 return 0;
533 }
534 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000535
Lachlan Bishopc753c402020-09-10 14:57:05 +1000536 if (data->emu_max_aai_size && (data->emu_status & SPI_SR_AAI)) {
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000537 if (writearr[0] != JEDEC_AAI_WORD_PROGRAM &&
538 writearr[0] != JEDEC_WRDI &&
539 writearr[0] != JEDEC_RDSR) {
540 msg_perr("Forbidden opcode (0x%02x) attempted during "
541 "AAI sequence!\n", writearr[0]);
542 return 0;
543 }
544 }
545
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000546 switch (writearr[0]) {
547 case JEDEC_RES:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000548 if (writecnt < JEDEC_RES_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000549 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000550 /* offs calculation is only needed for SST chips which treat RES like REMS. */
551 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
552 offs += writecnt - JEDEC_REMS_OUTSIZE;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000553 switch (data->emu_chip) {
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000554 case EMULATE_ST_M25P10_RES:
555 if (readcnt > 0)
556 memset(readarr, 0x10, readcnt);
557 break;
558 case EMULATE_SST_SST25VF040_REMS:
559 for (i = 0; i < readcnt; i++)
560 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
561 break;
562 case EMULATE_SST_SST25VF032B:
563 for (i = 0; i < readcnt; i++)
564 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
565 break;
566 case EMULATE_MACRONIX_MX25L6436:
567 if (readcnt > 0)
568 memset(readarr, 0x16, readcnt);
569 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100570 case EMULATE_WINBOND_W25Q128FV:
571 if (readcnt > 0)
572 memset(readarr, 0x17, readcnt);
573 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000574 default: /* ignore */
575 break;
576 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000577 break;
578 case JEDEC_REMS:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000579 /* REMS response has wraparound and uses an address parameter. */
580 if (writecnt < JEDEC_REMS_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000581 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000582 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
583 offs += writecnt - JEDEC_REMS_OUTSIZE;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000584 switch (data->emu_chip) {
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000585 case EMULATE_SST_SST25VF040_REMS:
586 for (i = 0; i < readcnt; i++)
587 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
588 break;
589 case EMULATE_SST_SST25VF032B:
590 for (i = 0; i < readcnt; i++)
591 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
592 break;
593 case EMULATE_MACRONIX_MX25L6436:
594 for (i = 0; i < readcnt; i++)
595 readarr[i] = mx25l6436_rems_response[(offs + i) % 2];
596 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100597 case EMULATE_WINBOND_W25Q128FV:
598 for (i = 0; i < readcnt; i++)
599 readarr[i] = w25q128fv_rems_response[(offs + i) % 2];
600 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000601 default: /* ignore */
602 break;
603 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000604 break;
605 case JEDEC_RDID:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000606 switch (data->emu_chip) {
Stefan Tauner0b9df972012-05-07 22:12:16 +0000607 case EMULATE_SST_SST25VF032B:
608 if (readcnt > 0)
609 readarr[0] = 0xbf;
610 if (readcnt > 1)
611 readarr[1] = 0x25;
612 if (readcnt > 2)
613 readarr[2] = 0x4a;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000614 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000615 case EMULATE_MACRONIX_MX25L6436:
616 if (readcnt > 0)
617 readarr[0] = 0xc2;
618 if (readcnt > 1)
619 readarr[1] = 0x20;
620 if (readcnt > 2)
621 readarr[2] = 0x17;
622 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100623 case EMULATE_WINBOND_W25Q128FV:
624 if (readcnt > 0)
625 readarr[0] = 0xef;
626 if (readcnt > 1)
627 readarr[1] = 0x40;
628 if (readcnt > 2)
629 readarr[2] = 0x18;
630 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000631 default: /* ignore */
632 break;
633 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000634 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000635 case JEDEC_RDSR:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000636 memset(readarr, data->emu_status, readcnt);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000637 break;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000638 /* FIXME: this should be chip-specific. */
639 case JEDEC_EWSR:
640 case JEDEC_WREN:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000641 data->emu_status |= SPI_SR_WEL;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000642 break;
643 case JEDEC_WRSR:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000644 if (!(data->emu_status & SPI_SR_WEL)) {
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000645 msg_perr("WRSR attempted, but WEL is 0!\n");
646 break;
647 }
648 /* FIXME: add some reasonable simulation of the busy flag */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000649 data->emu_status = writearr[1] & ~SPI_SR_WIP;
650 msg_pdbg2("WRSR wrote 0x%02x.\n", data->emu_status);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000651 break;
652 case JEDEC_READ:
653 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
654 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000655 offs %= data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000656 if (readcnt > 0)
Edward O'Callaghan94250222021-05-20 20:34:02 +1000657 memcpy(readarr, data->flashchip_contents + offs, readcnt);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000658 break;
659 case JEDEC_BYTE_PROGRAM:
660 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
661 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000662 offs %= data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000663 if (writecnt < 5) {
664 msg_perr("BYTE PROGRAM size too short!\n");
665 return 1;
666 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000667 if (writecnt - 4 > data->emu_max_byteprogram_size) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000668 msg_perr("Max BYTE PROGRAM size exceeded!\n");
669 return 1;
670 }
Edward O'Callaghan94250222021-05-20 20:34:02 +1000671 memcpy(data->flashchip_contents + offs, writearr + 4, writecnt - 4);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000672 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000673 break;
674 case JEDEC_AAI_WORD_PROGRAM:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000675 if (!data->emu_max_aai_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000676 break;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000677 if (!(data->emu_status & SPI_SR_AAI)) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000678 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
679 msg_perr("Initial AAI WORD PROGRAM size too "
680 "short!\n");
681 return 1;
682 }
683 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
684 msg_perr("Initial AAI WORD PROGRAM size too "
685 "long!\n");
686 return 1;
687 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000688 data->emu_status |= SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000689 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
690 writearr[3];
691 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000692 aai_offs %= data->emu_chip_size;
Edward O'Callaghan94250222021-05-20 20:34:02 +1000693 memcpy(data->flashchip_contents + aai_offs, writearr + 4, 2);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000694 aai_offs += 2;
695 } else {
696 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
697 msg_perr("Continuation AAI WORD PROGRAM size "
698 "too short!\n");
699 return 1;
700 }
701 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
702 msg_perr("Continuation AAI WORD PROGRAM size "
703 "too long!\n");
704 return 1;
705 }
Edward O'Callaghan94250222021-05-20 20:34:02 +1000706 memcpy(data->flashchip_contents + aai_offs, writearr + 1, 2);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000707 aai_offs += 2;
708 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000709 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000710 break;
711 case JEDEC_WRDI:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000712 if (data->emu_max_aai_size)
713 data->emu_status &= ~SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000714 break;
715 case JEDEC_SE:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000716 if (!data->emu_jedec_se_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000717 break;
718 if (writecnt != JEDEC_SE_OUTSIZE) {
719 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
720 return 1;
721 }
722 if (readcnt != JEDEC_SE_INSIZE) {
723 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
724 return 1;
725 }
726 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +1000727 if (offs & (data->emu_jedec_se_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000728 msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000729 offs &= ~(data->emu_jedec_se_size - 1);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000730 memset(data->flashchip_contents + offs, 0xff, data->emu_jedec_se_size);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000731 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000732 break;
733 case JEDEC_BE_52:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000734 if (!data->emu_jedec_be_52_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000735 break;
736 if (writecnt != JEDEC_BE_52_OUTSIZE) {
737 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
738 return 1;
739 }
740 if (readcnt != JEDEC_BE_52_INSIZE) {
741 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
742 return 1;
743 }
744 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +1000745 if (offs & (data->emu_jedec_be_52_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000746 msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000747 offs &= ~(data->emu_jedec_be_52_size - 1);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000748 memset(data->flashchip_contents + offs, 0xff, data->emu_jedec_be_52_size);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000749 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000750 break;
751 case JEDEC_BE_D8:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000752 if (!data->emu_jedec_be_d8_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000753 break;
754 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
755 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
756 return 1;
757 }
758 if (readcnt != JEDEC_BE_D8_INSIZE) {
759 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
760 return 1;
761 }
762 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +1000763 if (offs & (data->emu_jedec_be_d8_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000764 msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000765 offs &= ~(data->emu_jedec_be_d8_size - 1);
Edward O'Callaghan94250222021-05-20 20:34:02 +1000766 memset(data->flashchip_contents + offs, 0xff, data->emu_jedec_be_d8_size);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000767 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000768 break;
769 case JEDEC_CE_60:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000770 if (!data->emu_jedec_ce_60_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000771 break;
772 if (writecnt != JEDEC_CE_60_OUTSIZE) {
773 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
774 return 1;
775 }
776 if (readcnt != JEDEC_CE_60_INSIZE) {
777 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
778 return 1;
779 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000780 /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000781 /* emu_jedec_ce_60_size is emu_chip_size. */
Edward O'Callaghan94250222021-05-20 20:34:02 +1000782 memset(data->flashchip_contents, 0xff, data->emu_jedec_ce_60_size);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000783 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000784 break;
785 case JEDEC_CE_C7:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000786 if (!data->emu_jedec_ce_c7_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000787 break;
788 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
789 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
790 return 1;
791 }
792 if (readcnt != JEDEC_CE_C7_INSIZE) {
793 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
794 return 1;
795 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000796 /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000797 /* emu_jedec_ce_c7_size is emu_chip_size. */
Edward O'Callaghan94250222021-05-20 20:34:02 +1000798 memset(data->flashchip_contents, 0xff, data->emu_jedec_ce_c7_size);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000799 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000800 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000801 case JEDEC_SFDP:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000802 if (data->emu_chip != EMULATE_MACRONIX_MX25L6436)
Stefan Tauner0b9df972012-05-07 22:12:16 +0000803 break;
804 if (writecnt < 4)
805 break;
806 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
807
808 /* SFDP expects one dummy byte after the address. */
809 if (writecnt == 4) {
810 /* The dummy byte was not written, make sure it is read instead.
811 * Shifting and shortening the read array does achieve this goal.
812 */
813 readarr++;
814 readcnt--;
815 } else {
816 /* The response is shifted if more than 5 bytes are written, because SFDP data is
817 * already shifted out by the chip while those superfluous bytes are written. */
818 offs += writecnt - 5;
819 }
820
821 /* The SFDP spec implies that the start address of an SFDP read may be truncated to fit in the
822 * SFDP table address space, i.e. the start address may be wrapped around at SFDP table size.
823 * This is a reasonable implementation choice in hardware because it saves a few gates. */
824 if (offs >= sizeof(sfdp_table)) {
825 msg_pdbg("Wrapping the start address around the SFDP table boundary (using 0x%x "
826 "instead of 0x%x).\n", (unsigned int)(offs % sizeof(sfdp_table)), offs);
827 offs %= sizeof(sfdp_table);
828 }
829 toread = min(sizeof(sfdp_table) - offs, readcnt);
830 memcpy(readarr, sfdp_table + offs, toread);
831 if (toread < readcnt)
832 msg_pdbg("Crossing the SFDP table boundary in a single "
833 "continuous chunk produces undefined results "
834 "after that point.\n");
835 break;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000836 default:
837 /* No special response. */
838 break;
839 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000840 if (writearr[0] != JEDEC_WREN && writearr[0] != JEDEC_EWSR)
Lachlan Bishopc753c402020-09-10 14:57:05 +1000841 data->emu_status &= ~SPI_SR_WEL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000842 return 0;
843}
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000844
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000845static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000846 unsigned int readcnt,
847 const unsigned char *writearr,
848 unsigned char *readarr)
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000849{
Nico Huber519be662018-12-23 20:03:35 +0100850 unsigned int i;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000851 struct emu_data *emu_data = flash->mst->spi.data;
852 if (!emu_data) {
853 msg_perr("No data in flash context!\n");
854 return 1;
855 }
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000856
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000857 msg_pspew("%s:", __func__);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000858
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000859 msg_pspew(" writing %u bytes:", writecnt);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000860 for (i = 0; i < writecnt; i++)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000861 msg_pspew(" 0x%02x", writearr[i]);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000862
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000863 /* Response for unknown commands and missing chip is 0xff. */
864 memset(readarr, 0xff, readcnt);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000865 switch (emu_data->emu_chip) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000866 case EMULATE_ST_M25P10_RES:
867 case EMULATE_SST_SST25VF040_REMS:
868 case EMULATE_SST_SST25VF032B:
Stefan Tauner0b9df972012-05-07 22:12:16 +0000869 case EMULATE_MACRONIX_MX25L6436:
Nico Huberf9632d82019-01-20 11:23:49 +0100870 case EMULATE_WINBOND_W25Q128FV:
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000871 if (emulate_spi_chip_response(writecnt, readcnt, writearr,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000872 readarr, emu_data)) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000873 msg_pdbg("Invalid command sent to flash chip!\n");
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000874 return 1;
875 }
876 break;
877 default:
878 break;
879 }
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000880 msg_pspew(" reading %u bytes:", readcnt);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000881 for (i = 0; i < readcnt; i++)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000882 msg_pspew(" 0x%02x", readarr[i]);
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000883 msg_pspew("\n");
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000884 return 0;
885}
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +0000886
Mark Marshallf20b7be2014-05-09 21:16:21 +0000887static 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 +0000888{
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000889 return spi_write_chunked(flash, buf, start, len,
890 spi_write_256_chunksize);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000891}
Thomas Heijligencc853d82021-05-04 15:32:17 +0200892
893const struct programmer_entry programmer_dummy = {
894 .name = "dummy",
895 .type = OTHER,
896 /* FIXME */
897 .devs.note = "Dummy device, does nothing and logs all accesses\n",
898 .init = dummy_init,
899 .map_flash_region = dummy_map,
900 .unmap_flash_region = dummy_unmap,
901 .delay = internal_delay,
902};