blob: a5ff6e518dcbcdb82bf34ab8a12bd4b6a56981b0 [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/* Remove the #define below if you don't want SPI flash chip emulation. */
26#define EMULATE_SPI_CHIP 1
27
28#if EMULATE_SPI_CHIP
29#define EMULATE_CHIP 1
30#include "spi.h"
31#endif
32
33#if EMULATE_CHIP
34#include <sys/types.h>
35#include <sys/stat.h>
36#endif
37
38#if EMULATE_CHIP
39static uint8_t *flashchip_contents = NULL;
40enum emu_chip {
41 EMULATE_NONE,
42 EMULATE_ST_M25P10_RES,
43 EMULATE_SST_SST25VF040_REMS,
44 EMULATE_SST_SST25VF032B,
Stefan Tauner0b9df972012-05-07 22:12:16 +000045 EMULATE_MACRONIX_MX25L6436,
Nico Huberf9632d82019-01-20 11:23:49 +010046 EMULATE_WINBOND_W25Q128FV,
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000047};
Stefan Tauner0b9df972012-05-07 22:12:16 +000048
Lachlan Bishopc753c402020-09-10 14:57:05 +100049struct emu_data {
50 enum emu_chip emu_chip;
51 char *emu_persistent_image;
52 unsigned int emu_chip_size;
53 int emu_modified; /* is the image modified since reading it? */
54 uint8_t emu_status;
55 unsigned int emu_max_byteprogram_size;
56 unsigned int emu_max_aai_size;
57 unsigned int emu_jedec_se_size;
58 unsigned int emu_jedec_be_52_size;
59 unsigned int emu_jedec_be_d8_size;
60 unsigned int emu_jedec_ce_60_size;
61 unsigned int emu_jedec_ce_c7_size;
62 unsigned char spi_blacklist[256];
63 unsigned char spi_ignorelist[256];
64 unsigned int spi_blacklist_size;
65 unsigned int spi_ignorelist_size;
66};
67
68#if EMULATE_SPI_CHIP
Stefan Tauner0b9df972012-05-07 22:12:16 +000069/* A legit complete SFDP table based on the MX25L6436E (rev. 1.8) datasheet. */
Stefan Tauner67d163d2013-01-15 17:37:48 +000070static const uint8_t sfdp_table[] = {
Stefan Tauner0b9df972012-05-07 22:12:16 +000071 0x53, 0x46, 0x44, 0x50, // @0x00: SFDP signature
72 0x00, 0x01, 0x01, 0xFF, // @0x04: revision 1.0, 2 headers
73 0x00, 0x00, 0x01, 0x09, // @0x08: JEDEC SFDP header rev. 1.0, 9 DW long
74 0x1C, 0x00, 0x00, 0xFF, // @0x0C: PTP0 = 0x1C (instead of 0x30)
75 0xC2, 0x00, 0x01, 0x04, // @0x10: Macronix header rev. 1.0, 4 DW long
76 0x48, 0x00, 0x00, 0xFF, // @0x14: PTP1 = 0x48 (instead of 0x60)
77 0xFF, 0xFF, 0xFF, 0xFF, // @0x18: hole.
78 0xE5, 0x20, 0xC9, 0xFF, // @0x1C: SFDP parameter table start
79 0xFF, 0xFF, 0xFF, 0x03, // @0x20
80 0x00, 0xFF, 0x08, 0x6B, // @0x24
81 0x08, 0x3B, 0x00, 0xFF, // @0x28
82 0xEE, 0xFF, 0xFF, 0xFF, // @0x2C
83 0xFF, 0xFF, 0x00, 0x00, // @0x30
84 0xFF, 0xFF, 0x00, 0xFF, // @0x34
85 0x0C, 0x20, 0x0F, 0x52, // @0x38
86 0x10, 0xD8, 0x00, 0xFF, // @0x3C: SFDP parameter table end
87 0xFF, 0xFF, 0xFF, 0xFF, // @0x40: hole.
88 0xFF, 0xFF, 0xFF, 0xFF, // @0x44: hole.
89 0x00, 0x36, 0x00, 0x27, // @0x48: Macronix parameter table start
90 0xF4, 0x4F, 0xFF, 0xFF, // @0x4C
91 0xD9, 0xC8, 0xFF, 0xFF, // @0x50
92 0xFF, 0xFF, 0xFF, 0xFF, // @0x54: Macronix parameter table end
93};
94
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000095#endif
96#endif
97
Stefan Taunerc69c9c82011-11-23 09:13:48 +000098static unsigned int spi_write_256_chunksize = 256;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000099
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000100static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
Mark Marshallf20b7be2014-05-09 21:16:21 +0000101 const unsigned char *writearr, unsigned char *readarr);
102static int dummy_spi_write_256(struct flashctx *flash, const uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000103 unsigned int start, unsigned int len);
Mark Marshallf20b7be2014-05-09 21:16:21 +0000104static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr);
105static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr);
106static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr);
107static void dummy_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len);
108static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr);
109static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr);
110static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr);
111static void dummy_chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len);
Michael Karcherb9dbe482011-05-11 17:07:07 +0000112
Lachlan Bishopc753c402020-09-10 14:57:05 +1000113static struct spi_master spi_master_dummyflasher = {
Nico Huber1cf407b2017-11-10 20:18:23 +0100114 .features = SPI_MASTER_4BA,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000115 .max_data_read = MAX_DATA_READ_UNLIMITED,
116 .max_data_write = MAX_DATA_UNSPECIFIED,
117 .command = dummy_spi_send_command,
118 .multicommand = default_spi_send_multicommand,
119 .read = default_spi_read,
120 .write_256 = dummy_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +0000121 .write_aai = default_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000122};
David Hendricks8bb20212011-06-14 01:35:36 +0000123
Lachlan Bishopc753c402020-09-10 14:57:05 +1000124static struct par_master par_master_dummy = {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000125 .chip_readb = dummy_chip_readb,
126 .chip_readw = dummy_chip_readw,
127 .chip_readl = dummy_chip_readl,
128 .chip_readn = dummy_chip_readn,
129 .chip_writeb = dummy_chip_writeb,
130 .chip_writew = dummy_chip_writew,
131 .chip_writel = dummy_chip_writel,
132 .chip_writen = dummy_chip_writen,
133};
134
David Hendricks8bb20212011-06-14 01:35:36 +0000135static int dummy_shutdown(void *data)
136{
137 msg_pspew("%s\n", __func__);
138#if EMULATE_CHIP
Lachlan Bishopc753c402020-09-10 14:57:05 +1000139 struct emu_data *emu_data = (struct emu_data *)data;
140 if (emu_data->emu_chip != EMULATE_NONE) {
141 if (emu_data->emu_persistent_image && emu_data->emu_modified) {
142 msg_pdbg("Writing %s\n", emu_data->emu_persistent_image);
143 write_buf_to_file(flashchip_contents,
144 emu_data->emu_chip_size,
145 emu_data->emu_persistent_image);
146 free(emu_data->emu_persistent_image);
147 emu_data->emu_persistent_image = NULL;
David Hendricks8bb20212011-06-14 01:35:36 +0000148 }
149 free(flashchip_contents);
150 }
151#endif
Lachlan Bishopc753c402020-09-10 14:57:05 +1000152 free(data);
David Hendricks8bb20212011-06-14 01:35:36 +0000153 return 0;
154}
155
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000156int dummy_init(void)
157{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000158 char *bustext = NULL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000159 char *tmp = NULL;
Nico Huber519be662018-12-23 20:03:35 +0100160 unsigned int i;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000161#if EMULATE_SPI_CHIP
162 char *status = NULL;
163#endif
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000164#if EMULATE_CHIP
165 struct stat image_stat;
166#endif
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000167
Lachlan Bishopc753c402020-09-10 14:57:05 +1000168 struct emu_data *data = calloc(1, sizeof(struct emu_data));
169 if (!data) {
170 msg_perr("Out of memory!\n");
171 return 1;
172 }
173 data->emu_chip = EMULATE_NONE;
174 spi_master_dummyflasher.data = data;
175 par_master_dummy.data = data;
176
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000177 msg_pspew("%s\n", __func__);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000178
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000179 bustext = extract_programmer_param("bus");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000180 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
181 if (!bustext)
182 bustext = strdup("parallel+lpc+fwh+spi");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000183 /* Convert the parameters to lowercase. */
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000184 tolower_string(bustext);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000185
Lachlan Bishopc753c402020-09-10 14:57:05 +1000186 enum chipbustype dummy_buses_supported = BUS_NONE;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000187 if (strstr(bustext, "parallel")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000188 dummy_buses_supported |= BUS_PARALLEL;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000189 msg_pdbg("Enabling support for %s flash.\n", "parallel");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000190 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000191 if (strstr(bustext, "lpc")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000192 dummy_buses_supported |= BUS_LPC;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000193 msg_pdbg("Enabling support for %s flash.\n", "LPC");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000194 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000195 if (strstr(bustext, "fwh")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000196 dummy_buses_supported |= BUS_FWH;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000197 msg_pdbg("Enabling support for %s flash.\n", "FWH");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000198 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000199 if (strstr(bustext, "spi")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000200 dummy_buses_supported |= BUS_SPI;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000201 msg_pdbg("Enabling support for %s flash.\n", "SPI");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000202 }
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000203 if (dummy_buses_supported == BUS_NONE)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000204 msg_pdbg("Support for all flash bus types disabled.\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000205 free(bustext);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000206
207 tmp = extract_programmer_param("spi_write_256_chunksize");
208 if (tmp) {
209 spi_write_256_chunksize = atoi(tmp);
210 free(tmp);
211 if (spi_write_256_chunksize < 1) {
212 msg_perr("invalid spi_write_256_chunksize\n");
213 return 1;
214 }
215 }
216
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000217 tmp = extract_programmer_param("spi_blacklist");
218 if (tmp) {
219 i = strlen(tmp);
220 if (!strncmp(tmp, "0x", 2)) {
221 i -= 2;
222 memmove(tmp, tmp + 2, i + 1);
223 }
224 if ((i > 512) || (i % 2)) {
225 msg_perr("Invalid SPI command blacklist length\n");
226 free(tmp);
227 return 1;
228 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000229 data->spi_blacklist_size = i / 2;
230 for (i = 0; i < data->spi_blacklist_size * 2; i++) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000231 if (!isxdigit((unsigned char)tmp[i])) {
232 msg_perr("Invalid char \"%c\" in SPI command "
233 "blacklist\n", tmp[i]);
234 free(tmp);
235 return 1;
236 }
237 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000238 for (i = 0; i < data->spi_blacklist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000239 unsigned int tmp2;
240 /* SCNx8 is apparently not supported by MSVC (and thus
241 * MinGW), so work around it with an extra variable
242 */
243 sscanf(tmp + i * 2, "%2x", &tmp2);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000244 data->spi_blacklist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000245 }
246 msg_pdbg("SPI blacklist is ");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000247 for (i = 0; i < data->spi_blacklist_size; i++)
248 msg_pdbg("%02x ", data->spi_blacklist[i]);
249 msg_pdbg(", size %u\n", data->spi_blacklist_size);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000250 }
251 free(tmp);
252
253 tmp = extract_programmer_param("spi_ignorelist");
254 if (tmp) {
255 i = strlen(tmp);
256 if (!strncmp(tmp, "0x", 2)) {
257 i -= 2;
258 memmove(tmp, tmp + 2, i + 1);
259 }
260 if ((i > 512) || (i % 2)) {
261 msg_perr("Invalid SPI command ignorelist length\n");
262 free(tmp);
263 return 1;
264 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000265 data->spi_ignorelist_size = i / 2;
266 for (i = 0; i < data->spi_ignorelist_size * 2; i++) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000267 if (!isxdigit((unsigned char)tmp[i])) {
268 msg_perr("Invalid char \"%c\" in SPI command "
269 "ignorelist\n", tmp[i]);
270 free(tmp);
271 return 1;
272 }
273 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000274 for (i = 0; i < data->spi_ignorelist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000275 unsigned int tmp2;
276 /* SCNx8 is apparently not supported by MSVC (and thus
277 * MinGW), so work around it with an extra variable
278 */
279 sscanf(tmp + i * 2, "%2x", &tmp2);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000280 data->spi_ignorelist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000281 }
282 msg_pdbg("SPI ignorelist is ");
Lachlan Bishopc753c402020-09-10 14:57:05 +1000283 for (i = 0; i < data->spi_ignorelist_size; i++)
284 msg_pdbg("%02x ", data->spi_ignorelist[i]);
285 msg_pdbg(", size %u\n", data->spi_ignorelist_size);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000286 }
287 free(tmp);
288
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000289#if EMULATE_CHIP
290 tmp = extract_programmer_param("emulate");
291 if (!tmp) {
292 msg_pdbg("Not emulating any flash chip.\n");
293 /* Nothing else to do. */
David Hendricks8bb20212011-06-14 01:35:36 +0000294 goto dummy_init_out;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000295 }
296#if EMULATE_SPI_CHIP
297 if (!strcmp(tmp, "M25P10.RES")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000298 data->emu_chip = EMULATE_ST_M25P10_RES;
299 data->emu_chip_size = 128 * 1024;
300 data->emu_max_byteprogram_size = 128;
301 data->emu_max_aai_size = 0;
302 data->emu_jedec_se_size = 0;
303 data->emu_jedec_be_52_size = 0;
304 data->emu_jedec_be_d8_size = 32 * 1024;
305 data->emu_jedec_ce_60_size = 0;
306 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000307 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
308 "write)\n");
309 }
310 if (!strcmp(tmp, "SST25VF040.REMS")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000311 data->emu_chip = EMULATE_SST_SST25VF040_REMS;
312 data->emu_chip_size = 512 * 1024;
313 data->emu_max_byteprogram_size = 1;
314 data->emu_max_aai_size = 0;
315 data->emu_jedec_se_size = 4 * 1024;
316 data->emu_jedec_be_52_size = 32 * 1024;
317 data->emu_jedec_be_d8_size = 0;
318 data->emu_jedec_ce_60_size = data->emu_chip_size;
319 data->emu_jedec_ce_c7_size = 0;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000320 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
321 "byte write)\n");
322 }
323 if (!strcmp(tmp, "SST25VF032B")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000324 data->emu_chip = EMULATE_SST_SST25VF032B;
325 data->emu_chip_size = 4 * 1024 * 1024;
326 data->emu_max_byteprogram_size = 1;
327 data->emu_max_aai_size = 2;
328 data->emu_jedec_se_size = 4 * 1024;
329 data->emu_jedec_be_52_size = 32 * 1024;
330 data->emu_jedec_be_d8_size = 64 * 1024;
331 data->emu_jedec_ce_60_size = data->emu_chip_size;
332 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000333 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
334 "write)\n");
335 }
Stefan Tauner0b9df972012-05-07 22:12:16 +0000336 if (!strcmp(tmp, "MX25L6436")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000337 data->emu_chip = EMULATE_MACRONIX_MX25L6436;
338 data->emu_chip_size = 8 * 1024 * 1024;
339 data->emu_max_byteprogram_size = 256;
340 data->emu_max_aai_size = 0;
341 data->emu_jedec_se_size = 4 * 1024;
342 data->emu_jedec_be_52_size = 32 * 1024;
343 data->emu_jedec_be_d8_size = 64 * 1024;
344 data->emu_jedec_ce_60_size = data->emu_chip_size;
345 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000346 msg_pdbg("Emulating Macronix MX25L6436 SPI flash chip (RDID, "
347 "SFDP)\n");
348 }
Nico Huberf9632d82019-01-20 11:23:49 +0100349 if (!strcmp(tmp, "W25Q128FV")) {
Lachlan Bishopc753c402020-09-10 14:57:05 +1000350 data->emu_chip = EMULATE_WINBOND_W25Q128FV;
351 data->emu_chip_size = 16 * 1024 * 1024;
352 data->emu_max_byteprogram_size = 256;
353 data->emu_max_aai_size = 0;
354 data->emu_jedec_se_size = 4 * 1024;
355 data->emu_jedec_be_52_size = 32 * 1024;
356 data->emu_jedec_be_d8_size = 64 * 1024;
357 data->emu_jedec_ce_60_size = data->emu_chip_size;
358 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Nico Huberf9632d82019-01-20 11:23:49 +0100359 msg_pdbg("Emulating Winbond W25Q128FV SPI flash chip (RDID)\n");
360 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000361#endif
Lachlan Bishopc753c402020-09-10 14:57:05 +1000362 if (data->emu_chip == EMULATE_NONE) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000363 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
364 free(tmp);
365 return 1;
366 }
367 free(tmp);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000368 flashchip_contents = malloc(data->emu_chip_size);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000369 if (!flashchip_contents) {
370 msg_perr("Out of memory!\n");
371 return 1;
372 }
David Hendricks8bb20212011-06-14 01:35:36 +0000373
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000374#ifdef EMULATE_SPI_CHIP
375 status = extract_programmer_param("spi_status");
376 if (status) {
377 char *endptr;
378 errno = 0;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000379 data->emu_status = strtoul(status, &endptr, 0);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000380 free(status);
381 if (errno != 0 || status == endptr) {
382 msg_perr("Error: initial status register specified, "
383 "but the value could not be converted.\n");
384 return 1;
385 }
386 msg_pdbg("Initial status register is set to 0x%02x.\n",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000387 data->emu_status);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000388 }
389#endif
390
Lachlan Bishopc753c402020-09-10 14:57:05 +1000391 msg_pdbg("Filling fake flash chip with 0xff, size %i\n", data->emu_chip_size);
392 memset(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);
408 if (read_buf_from_file(flashchip_contents, data->emu_chip_size,
409 data->emu_persistent_image)) {
410 msg_perr("Unable to read %s\n", data->emu_persistent_image);
Jacob Garberca598da2019-08-12 10:44:17 -0600411 free(flashchip_contents);
412 return 1;
413 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000414 } else {
415 msg_pdbg("doesn't match.\n");
416 }
417 }
418#endif
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000419
David Hendricks8bb20212011-06-14 01:35:36 +0000420dummy_init_out:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000421 if (register_shutdown(dummy_shutdown, data)) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000422 free(flashchip_contents);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000423 free(data);
David Hendricks8bb20212011-06-14 01:35:36 +0000424 return 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000425 }
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000426 if (dummy_buses_supported & (BUS_PARALLEL | BUS_LPC | BUS_FWH))
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000427 register_par_master(&par_master_dummy,
428 dummy_buses_supported & (BUS_PARALLEL | BUS_LPC | BUS_FWH));
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000429 if (dummy_buses_supported & BUS_SPI)
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000430 register_spi_master(&spi_master_dummyflasher);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000431
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000432 return 0;
433}
434
Stefan Tauner305e0b92013-07-17 23:46:44 +0000435void *dummy_map(const char *descr, uintptr_t phys_addr, size_t len)
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000436{
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000437 msg_pspew("%s: Mapping %s, 0x%zx bytes at 0x%0*" PRIxPTR "\n",
Stefan Tauner0554ca52013-07-25 22:54:25 +0000438 __func__, descr, len, PRIxPTR_WIDTH, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000439 return (void *)phys_addr;
440}
441
442void dummy_unmap(void *virt_addr, size_t len)
443{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000444 msg_pspew("%s: Unmapping 0x%zx bytes at %p\n", __func__, len, virt_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000445}
446
Mark Marshallf20b7be2014-05-09 21:16:21 +0000447static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000448{
Stefan Taunerc2333752013-07-13 23:31:37 +0000449 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%02x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000450}
451
Mark Marshallf20b7be2014-05-09 21:16:21 +0000452static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000453{
Stefan Taunerc2333752013-07-13 23:31:37 +0000454 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%04x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000455}
456
Mark Marshallf20b7be2014-05-09 21:16:21 +0000457static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000458{
Stefan Taunerc2333752013-07-13 23:31:37 +0000459 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%08x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000460}
461
Mark Marshallf20b7be2014-05-09 21:16:21 +0000462static 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 +0000463{
464 size_t i;
Stefan Tauner0554ca52013-07-25 22:54:25 +0000465 msg_pspew("%s: addr=0x%" PRIxPTR ", len=0x%zx, writing data (hex):", __func__, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000466 for (i = 0; i < len; i++) {
467 if ((i % 16) == 0)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000468 msg_pspew("\n");
469 msg_pspew("%02x ", buf[i]);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000470 }
471}
472
Mark Marshallf20b7be2014-05-09 21:16:21 +0000473static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000474{
Stefan Taunerc2333752013-07-13 23:31:37 +0000475 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000476 return 0xff;
477}
478
Mark Marshallf20b7be2014-05-09 21:16:21 +0000479static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000480{
Stefan Taunerc2333752013-07-13 23:31:37 +0000481 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000482 return 0xffff;
483}
484
Mark Marshallf20b7be2014-05-09 21:16:21 +0000485static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000486{
Stefan Taunerc2333752013-07-13 23:31:37 +0000487 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffffffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000488 return 0xffffffff;
489}
490
Mark Marshallf20b7be2014-05-09 21:16:21 +0000491static 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 +0000492{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000493 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 +0000494 memset(buf, 0xff, len);
495 return;
496}
497
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000498#if EMULATE_SPI_CHIP
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000499static int emulate_spi_chip_response(unsigned int writecnt,
500 unsigned int readcnt,
501 const unsigned char *writearr,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000502 unsigned char *readarr,
503 struct emu_data *data)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000504{
Stefan Tauner0b9df972012-05-07 22:12:16 +0000505 unsigned int offs, i, toread;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000506 static int unsigned aai_offs;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000507 const unsigned char sst25vf040_rems_response[2] = {0xbf, 0x44};
508 const unsigned char sst25vf032b_rems_response[2] = {0xbf, 0x4a};
509 const unsigned char mx25l6436_rems_response[2] = {0xc2, 0x16};
Nico Huberf9632d82019-01-20 11:23:49 +0100510 const unsigned char w25q128fv_rems_response[2] = {0xef, 0x17};
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000511
512 if (writecnt == 0) {
513 msg_perr("No command sent to the chip!\n");
514 return 1;
515 }
Paul Menzelac427b22012-02-16 21:07:07 +0000516 /* spi_blacklist has precedence over spi_ignorelist. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000517 for (i = 0; i < data->spi_blacklist_size; i++) {
518 if (writearr[0] == data->spi_blacklist[i]) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000519 msg_pdbg("Refusing blacklisted SPI command 0x%02x\n",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000520 data->spi_blacklist[i]);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000521 return SPI_INVALID_OPCODE;
522 }
523 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000524 for (i = 0; i < data->spi_ignorelist_size; i++) {
525 if (writearr[0] == data->spi_ignorelist[i]) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000526 msg_cdbg("Ignoring ignorelisted SPI command 0x%02x\n",
Lachlan Bishopc753c402020-09-10 14:57:05 +1000527 data->spi_ignorelist[i]);
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000528 /* Return success because the command does not fail,
529 * it is simply ignored.
530 */
531 return 0;
532 }
533 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000534
Lachlan Bishopc753c402020-09-10 14:57:05 +1000535 if (data->emu_max_aai_size && (data->emu_status & SPI_SR_AAI)) {
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000536 if (writearr[0] != JEDEC_AAI_WORD_PROGRAM &&
537 writearr[0] != JEDEC_WRDI &&
538 writearr[0] != JEDEC_RDSR) {
539 msg_perr("Forbidden opcode (0x%02x) attempted during "
540 "AAI sequence!\n", writearr[0]);
541 return 0;
542 }
543 }
544
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000545 switch (writearr[0]) {
546 case JEDEC_RES:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000547 if (writecnt < JEDEC_RES_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000548 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000549 /* offs calculation is only needed for SST chips which treat RES like REMS. */
550 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
551 offs += writecnt - JEDEC_REMS_OUTSIZE;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000552 switch (data->emu_chip) {
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000553 case EMULATE_ST_M25P10_RES:
554 if (readcnt > 0)
555 memset(readarr, 0x10, readcnt);
556 break;
557 case EMULATE_SST_SST25VF040_REMS:
558 for (i = 0; i < readcnt; i++)
559 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
560 break;
561 case EMULATE_SST_SST25VF032B:
562 for (i = 0; i < readcnt; i++)
563 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
564 break;
565 case EMULATE_MACRONIX_MX25L6436:
566 if (readcnt > 0)
567 memset(readarr, 0x16, readcnt);
568 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100569 case EMULATE_WINBOND_W25Q128FV:
570 if (readcnt > 0)
571 memset(readarr, 0x17, readcnt);
572 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000573 default: /* ignore */
574 break;
575 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000576 break;
577 case JEDEC_REMS:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000578 /* REMS response has wraparound and uses an address parameter. */
579 if (writecnt < JEDEC_REMS_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000580 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000581 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
582 offs += writecnt - JEDEC_REMS_OUTSIZE;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000583 switch (data->emu_chip) {
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000584 case EMULATE_SST_SST25VF040_REMS:
585 for (i = 0; i < readcnt; i++)
586 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
587 break;
588 case EMULATE_SST_SST25VF032B:
589 for (i = 0; i < readcnt; i++)
590 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
591 break;
592 case EMULATE_MACRONIX_MX25L6436:
593 for (i = 0; i < readcnt; i++)
594 readarr[i] = mx25l6436_rems_response[(offs + i) % 2];
595 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100596 case EMULATE_WINBOND_W25Q128FV:
597 for (i = 0; i < readcnt; i++)
598 readarr[i] = w25q128fv_rems_response[(offs + i) % 2];
599 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000600 default: /* ignore */
601 break;
602 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000603 break;
604 case JEDEC_RDID:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000605 switch (data->emu_chip) {
Stefan Tauner0b9df972012-05-07 22:12:16 +0000606 case EMULATE_SST_SST25VF032B:
607 if (readcnt > 0)
608 readarr[0] = 0xbf;
609 if (readcnt > 1)
610 readarr[1] = 0x25;
611 if (readcnt > 2)
612 readarr[2] = 0x4a;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000613 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000614 case EMULATE_MACRONIX_MX25L6436:
615 if (readcnt > 0)
616 readarr[0] = 0xc2;
617 if (readcnt > 1)
618 readarr[1] = 0x20;
619 if (readcnt > 2)
620 readarr[2] = 0x17;
621 break;
Nico Huberf9632d82019-01-20 11:23:49 +0100622 case EMULATE_WINBOND_W25Q128FV:
623 if (readcnt > 0)
624 readarr[0] = 0xef;
625 if (readcnt > 1)
626 readarr[1] = 0x40;
627 if (readcnt > 2)
628 readarr[2] = 0x18;
629 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000630 default: /* ignore */
631 break;
632 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000633 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000634 case JEDEC_RDSR:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000635 memset(readarr, data->emu_status, readcnt);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000636 break;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000637 /* FIXME: this should be chip-specific. */
638 case JEDEC_EWSR:
639 case JEDEC_WREN:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000640 data->emu_status |= SPI_SR_WEL;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000641 break;
642 case JEDEC_WRSR:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000643 if (!(data->emu_status & SPI_SR_WEL)) {
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000644 msg_perr("WRSR attempted, but WEL is 0!\n");
645 break;
646 }
647 /* FIXME: add some reasonable simulation of the busy flag */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000648 data->emu_status = writearr[1] & ~SPI_SR_WIP;
649 msg_pdbg2("WRSR wrote 0x%02x.\n", data->emu_status);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000650 break;
651 case JEDEC_READ:
652 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
653 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000654 offs %= data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000655 if (readcnt > 0)
656 memcpy(readarr, flashchip_contents + offs, readcnt);
657 break;
658 case JEDEC_BYTE_PROGRAM:
659 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
660 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000661 offs %= data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000662 if (writecnt < 5) {
663 msg_perr("BYTE PROGRAM size too short!\n");
664 return 1;
665 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000666 if (writecnt - 4 > data->emu_max_byteprogram_size) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000667 msg_perr("Max BYTE PROGRAM size exceeded!\n");
668 return 1;
669 }
670 memcpy(flashchip_contents + offs, writearr + 4, writecnt - 4);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000671 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000672 break;
673 case JEDEC_AAI_WORD_PROGRAM:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000674 if (!data->emu_max_aai_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000675 break;
Lachlan Bishopc753c402020-09-10 14:57:05 +1000676 if (!(data->emu_status & SPI_SR_AAI)) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000677 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
678 msg_perr("Initial AAI WORD PROGRAM size too "
679 "short!\n");
680 return 1;
681 }
682 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
683 msg_perr("Initial AAI WORD PROGRAM size too "
684 "long!\n");
685 return 1;
686 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000687 data->emu_status |= SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000688 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
689 writearr[3];
690 /* Truncate to emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000691 aai_offs %= data->emu_chip_size;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000692 memcpy(flashchip_contents + aai_offs, writearr + 4, 2);
693 aai_offs += 2;
694 } else {
695 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
696 msg_perr("Continuation AAI WORD PROGRAM size "
697 "too short!\n");
698 return 1;
699 }
700 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
701 msg_perr("Continuation AAI WORD PROGRAM size "
702 "too long!\n");
703 return 1;
704 }
705 memcpy(flashchip_contents + aai_offs, writearr + 1, 2);
706 aai_offs += 2;
707 }
Lachlan Bishopc753c402020-09-10 14:57:05 +1000708 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000709 break;
710 case JEDEC_WRDI:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000711 if (data->emu_max_aai_size)
712 data->emu_status &= ~SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000713 break;
714 case JEDEC_SE:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000715 if (!data->emu_jedec_se_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000716 break;
717 if (writecnt != JEDEC_SE_OUTSIZE) {
718 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
719 return 1;
720 }
721 if (readcnt != JEDEC_SE_INSIZE) {
722 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
723 return 1;
724 }
725 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +1000726 if (offs & (data->emu_jedec_se_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000727 msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000728 offs &= ~(data->emu_jedec_se_size - 1);
729 memset(flashchip_contents + offs, 0xff, data->emu_jedec_se_size);
730 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000731 break;
732 case JEDEC_BE_52:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000733 if (!data->emu_jedec_be_52_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000734 break;
735 if (writecnt != JEDEC_BE_52_OUTSIZE) {
736 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
737 return 1;
738 }
739 if (readcnt != JEDEC_BE_52_INSIZE) {
740 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
741 return 1;
742 }
743 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +1000744 if (offs & (data->emu_jedec_be_52_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000745 msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000746 offs &= ~(data->emu_jedec_be_52_size - 1);
747 memset(flashchip_contents + offs, 0xff, data->emu_jedec_be_52_size);
748 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000749 break;
750 case JEDEC_BE_D8:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000751 if (!data->emu_jedec_be_d8_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000752 break;
753 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
754 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
755 return 1;
756 }
757 if (readcnt != JEDEC_BE_D8_INSIZE) {
758 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
759 return 1;
760 }
761 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopc753c402020-09-10 14:57:05 +1000762 if (offs & (data->emu_jedec_be_d8_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000763 msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
Lachlan Bishopc753c402020-09-10 14:57:05 +1000764 offs &= ~(data->emu_jedec_be_d8_size - 1);
765 memset(flashchip_contents + offs, 0xff, data->emu_jedec_be_d8_size);
766 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000767 break;
768 case JEDEC_CE_60:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000769 if (!data->emu_jedec_ce_60_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000770 break;
771 if (writecnt != JEDEC_CE_60_OUTSIZE) {
772 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
773 return 1;
774 }
775 if (readcnt != JEDEC_CE_60_INSIZE) {
776 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
777 return 1;
778 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000779 /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000780 /* emu_jedec_ce_60_size is emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000781 memset(flashchip_contents, 0xff, data->emu_jedec_ce_60_size);
782 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000783 break;
784 case JEDEC_CE_C7:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000785 if (!data->emu_jedec_ce_c7_size)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000786 break;
787 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
788 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
789 return 1;
790 }
791 if (readcnt != JEDEC_CE_C7_INSIZE) {
792 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
793 return 1;
794 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000795 /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000796 /* emu_jedec_ce_c7_size is emu_chip_size. */
Lachlan Bishopc753c402020-09-10 14:57:05 +1000797 memset(flashchip_contents, 0xff, data->emu_jedec_ce_c7_size);
798 data->emu_modified = 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000799 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000800 case JEDEC_SFDP:
Lachlan Bishopc753c402020-09-10 14:57:05 +1000801 if (data->emu_chip != EMULATE_MACRONIX_MX25L6436)
Stefan Tauner0b9df972012-05-07 22:12:16 +0000802 break;
803 if (writecnt < 4)
804 break;
805 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
806
807 /* SFDP expects one dummy byte after the address. */
808 if (writecnt == 4) {
809 /* The dummy byte was not written, make sure it is read instead.
810 * Shifting and shortening the read array does achieve this goal.
811 */
812 readarr++;
813 readcnt--;
814 } else {
815 /* The response is shifted if more than 5 bytes are written, because SFDP data is
816 * already shifted out by the chip while those superfluous bytes are written. */
817 offs += writecnt - 5;
818 }
819
820 /* The SFDP spec implies that the start address of an SFDP read may be truncated to fit in the
821 * SFDP table address space, i.e. the start address may be wrapped around at SFDP table size.
822 * This is a reasonable implementation choice in hardware because it saves a few gates. */
823 if (offs >= sizeof(sfdp_table)) {
824 msg_pdbg("Wrapping the start address around the SFDP table boundary (using 0x%x "
825 "instead of 0x%x).\n", (unsigned int)(offs % sizeof(sfdp_table)), offs);
826 offs %= sizeof(sfdp_table);
827 }
828 toread = min(sizeof(sfdp_table) - offs, readcnt);
829 memcpy(readarr, sfdp_table + offs, toread);
830 if (toread < readcnt)
831 msg_pdbg("Crossing the SFDP table boundary in a single "
832 "continuous chunk produces undefined results "
833 "after that point.\n");
834 break;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000835 default:
836 /* No special response. */
837 break;
838 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000839 if (writearr[0] != JEDEC_WREN && writearr[0] != JEDEC_EWSR)
Lachlan Bishopc753c402020-09-10 14:57:05 +1000840 data->emu_status &= ~SPI_SR_WEL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000841 return 0;
842}
843#endif
844
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);
865#if EMULATE_SPI_CHIP
Lachlan Bishopc753c402020-09-10 14:57:05 +1000866 switch (emu_data->emu_chip) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000867 case EMULATE_ST_M25P10_RES:
868 case EMULATE_SST_SST25VF040_REMS:
869 case EMULATE_SST_SST25VF032B:
Stefan Tauner0b9df972012-05-07 22:12:16 +0000870 case EMULATE_MACRONIX_MX25L6436:
Nico Huberf9632d82019-01-20 11:23:49 +0100871 case EMULATE_WINBOND_W25Q128FV:
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000872 if (emulate_spi_chip_response(writecnt, readcnt, writearr,
Lachlan Bishopc753c402020-09-10 14:57:05 +1000873 readarr, emu_data)) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000874 msg_pdbg("Invalid command sent to flash chip!\n");
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000875 return 1;
876 }
877 break;
878 default:
879 break;
880 }
881#endif
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000882 msg_pspew(" reading %u bytes:", readcnt);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000883 for (i = 0; i < readcnt; i++)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000884 msg_pspew(" 0x%02x", readarr[i]);
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000885 msg_pspew("\n");
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000886 return 0;
887}
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +0000888
Mark Marshallf20b7be2014-05-09 21:16:21 +0000889static 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 +0000890{
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000891 return spi_write_chunked(flash, buf, start, len,
892 spi_write_256_chunksize);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000893}