blob: f0095d1a610a8be01329e8ff92e7a88e0e45896d [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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000020#include <string.h>
21#include <stdlib.h>
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +000022#include <stdio.h>
23#include <ctype.h>
Stefan Tauner5e695ab2012-05-06 17:03:40 +000024#include <errno.h>
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000025#include "flash.h"
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +000026#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000027#include "programmer.h"
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000028
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000029/* Remove the #define below if you don't want SPI flash chip emulation. */
30#define EMULATE_SPI_CHIP 1
31
32#if EMULATE_SPI_CHIP
33#define EMULATE_CHIP 1
34#include "spi.h"
35#endif
36
37#if EMULATE_CHIP
38#include <sys/types.h>
39#include <sys/stat.h>
40#endif
41
42#if EMULATE_CHIP
43static uint8_t *flashchip_contents = NULL;
44enum emu_chip {
45 EMULATE_NONE,
46 EMULATE_ST_M25P10_RES,
47 EMULATE_SST_SST25VF040_REMS,
48 EMULATE_SST_SST25VF032B,
Stefan Tauner0b9df972012-05-07 22:12:16 +000049 EMULATE_MACRONIX_MX25L6436,
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000050};
51static enum emu_chip emu_chip = EMULATE_NONE;
52static char *emu_persistent_image = NULL;
Stefan Taunerc69c9c82011-11-23 09:13:48 +000053static unsigned int emu_chip_size = 0;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000054#if EMULATE_SPI_CHIP
Stefan Taunerc69c9c82011-11-23 09:13:48 +000055static unsigned int emu_max_byteprogram_size = 0;
56static unsigned int emu_max_aai_size = 0;
57static unsigned int emu_jedec_se_size = 0;
58static unsigned int emu_jedec_be_52_size = 0;
59static unsigned int emu_jedec_be_d8_size = 0;
60static unsigned int emu_jedec_ce_60_size = 0;
61static unsigned int emu_jedec_ce_c7_size = 0;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +000062unsigned char spi_blacklist[256];
63unsigned char spi_ignorelist[256];
64int spi_blacklist_size = 0;
65int spi_ignorelist_size = 0;
Stefan Tauner5e695ab2012-05-06 17:03:40 +000066static uint8_t emu_status = 0;
Stefan Tauner0b9df972012-05-07 22:12:16 +000067
68/* A legit complete SFDP table based on the MX25L6436E (rev. 1.8) datasheet. */
Stefan Tauner67d163d2013-01-15 17:37:48 +000069static const uint8_t sfdp_table[] = {
Stefan Tauner0b9df972012-05-07 22:12:16 +000070 0x53, 0x46, 0x44, 0x50, // @0x00: SFDP signature
71 0x00, 0x01, 0x01, 0xFF, // @0x04: revision 1.0, 2 headers
72 0x00, 0x00, 0x01, 0x09, // @0x08: JEDEC SFDP header rev. 1.0, 9 DW long
73 0x1C, 0x00, 0x00, 0xFF, // @0x0C: PTP0 = 0x1C (instead of 0x30)
74 0xC2, 0x00, 0x01, 0x04, // @0x10: Macronix header rev. 1.0, 4 DW long
75 0x48, 0x00, 0x00, 0xFF, // @0x14: PTP1 = 0x48 (instead of 0x60)
76 0xFF, 0xFF, 0xFF, 0xFF, // @0x18: hole.
77 0xE5, 0x20, 0xC9, 0xFF, // @0x1C: SFDP parameter table start
78 0xFF, 0xFF, 0xFF, 0x03, // @0x20
79 0x00, 0xFF, 0x08, 0x6B, // @0x24
80 0x08, 0x3B, 0x00, 0xFF, // @0x28
81 0xEE, 0xFF, 0xFF, 0xFF, // @0x2C
82 0xFF, 0xFF, 0x00, 0x00, // @0x30
83 0xFF, 0xFF, 0x00, 0xFF, // @0x34
84 0x0C, 0x20, 0x0F, 0x52, // @0x38
85 0x10, 0xD8, 0x00, 0xFF, // @0x3C: SFDP parameter table end
86 0xFF, 0xFF, 0xFF, 0xFF, // @0x40: hole.
87 0xFF, 0xFF, 0xFF, 0xFF, // @0x44: hole.
88 0x00, 0x36, 0x00, 0x27, // @0x48: Macronix parameter table start
89 0xF4, 0x4F, 0xFF, 0xFF, // @0x4C
90 0xD9, 0xC8, 0xFF, 0xFF, // @0x50
91 0xFF, 0xFF, 0xFF, 0xFF, // @0x54: Macronix parameter table end
92};
93
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000094#endif
95#endif
96
Stefan Taunerc69c9c82011-11-23 09:13:48 +000097static unsigned int spi_write_256_chunksize = 256;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000098
Mark Marshallf20b7be2014-05-09 21:16:21 +000099static int dummy_spi_send_command(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
100 const unsigned char *writearr, unsigned char *readarr);
101static int dummy_spi_write_256(struct flashctx *flash, const uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000102 unsigned int start, unsigned int len);
Mark Marshallf20b7be2014-05-09 21:16:21 +0000103static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr);
104static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr);
105static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr);
106static void dummy_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len);
107static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr);
108static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr);
109static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr);
110static void dummy_chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len);
Michael Karcherb9dbe482011-05-11 17:07:07 +0000111
112static const struct spi_programmer spi_programmer_dummyflasher = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000113 .type = SPI_CONTROLLER_DUMMY,
114 .max_data_read = MAX_DATA_READ_UNLIMITED,
115 .max_data_write = MAX_DATA_UNSPECIFIED,
116 .command = dummy_spi_send_command,
117 .multicommand = default_spi_send_multicommand,
118 .read = default_spi_read,
119 .write_256 = dummy_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +0000120 .write_aai = default_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000121};
David Hendricks8bb20212011-06-14 01:35:36 +0000122
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000123static const struct par_programmer par_programmer_dummy = {
124 .chip_readb = dummy_chip_readb,
125 .chip_readw = dummy_chip_readw,
126 .chip_readl = dummy_chip_readl,
127 .chip_readn = dummy_chip_readn,
128 .chip_writeb = dummy_chip_writeb,
129 .chip_writew = dummy_chip_writew,
130 .chip_writel = dummy_chip_writel,
131 .chip_writen = dummy_chip_writen,
132};
133
134enum chipbustype dummy_buses_supported = BUS_NONE;
135
David Hendricks8bb20212011-06-14 01:35:36 +0000136static int dummy_shutdown(void *data)
137{
138 msg_pspew("%s\n", __func__);
139#if EMULATE_CHIP
140 if (emu_chip != EMULATE_NONE) {
141 if (emu_persistent_image) {
142 msg_pdbg("Writing %s\n", emu_persistent_image);
Stefan Taunere0ff1652012-09-22 22:56:09 +0000143 write_buf_to_file(flashchip_contents, emu_chip_size, emu_persistent_image);
144 free(emu_persistent_image);
145 emu_persistent_image = NULL;
David Hendricks8bb20212011-06-14 01:35:36 +0000146 }
147 free(flashchip_contents);
148 }
149#endif
150 return 0;
151}
152
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000153int dummy_init(void)
154{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000155 char *bustext = NULL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000156 char *tmp = NULL;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000157 int i;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000158#if EMULATE_SPI_CHIP
159 char *status = NULL;
160#endif
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000161#if EMULATE_CHIP
162 struct stat image_stat;
163#endif
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000164
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000165 msg_pspew("%s\n", __func__);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000166
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000167 bustext = extract_programmer_param("bus");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000168 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
169 if (!bustext)
170 bustext = strdup("parallel+lpc+fwh+spi");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000171 /* Convert the parameters to lowercase. */
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000172 tolower_string(bustext);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000173
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000174 dummy_buses_supported = BUS_NONE;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000175 if (strstr(bustext, "parallel")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000176 dummy_buses_supported |= BUS_PARALLEL;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000177 msg_pdbg("Enabling support for %s flash.\n", "parallel");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000178 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000179 if (strstr(bustext, "lpc")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000180 dummy_buses_supported |= BUS_LPC;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000181 msg_pdbg("Enabling support for %s flash.\n", "LPC");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000182 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000183 if (strstr(bustext, "fwh")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000184 dummy_buses_supported |= BUS_FWH;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000185 msg_pdbg("Enabling support for %s flash.\n", "FWH");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000186 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000187 if (strstr(bustext, "spi")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000188 dummy_buses_supported |= BUS_SPI;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000189 msg_pdbg("Enabling support for %s flash.\n", "SPI");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000190 }
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000191 if (dummy_buses_supported == BUS_NONE)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000192 msg_pdbg("Support for all flash bus types disabled.\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000193 free(bustext);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000194
195 tmp = extract_programmer_param("spi_write_256_chunksize");
196 if (tmp) {
197 spi_write_256_chunksize = atoi(tmp);
198 free(tmp);
199 if (spi_write_256_chunksize < 1) {
200 msg_perr("invalid spi_write_256_chunksize\n");
201 return 1;
202 }
203 }
204
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000205 tmp = extract_programmer_param("spi_blacklist");
206 if (tmp) {
207 i = strlen(tmp);
208 if (!strncmp(tmp, "0x", 2)) {
209 i -= 2;
210 memmove(tmp, tmp + 2, i + 1);
211 }
212 if ((i > 512) || (i % 2)) {
213 msg_perr("Invalid SPI command blacklist length\n");
214 free(tmp);
215 return 1;
216 }
217 spi_blacklist_size = i / 2;
218 for (i = 0; i < spi_blacklist_size * 2; i++) {
219 if (!isxdigit((unsigned char)tmp[i])) {
220 msg_perr("Invalid char \"%c\" in SPI command "
221 "blacklist\n", tmp[i]);
222 free(tmp);
223 return 1;
224 }
225 }
226 for (i = 0; i < spi_blacklist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000227 unsigned int tmp2;
228 /* SCNx8 is apparently not supported by MSVC (and thus
229 * MinGW), so work around it with an extra variable
230 */
231 sscanf(tmp + i * 2, "%2x", &tmp2);
232 spi_blacklist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000233 }
234 msg_pdbg("SPI blacklist is ");
235 for (i = 0; i < spi_blacklist_size; i++)
236 msg_pdbg("%02x ", spi_blacklist[i]);
237 msg_pdbg(", size %i\n", spi_blacklist_size);
238 }
239 free(tmp);
240
241 tmp = extract_programmer_param("spi_ignorelist");
242 if (tmp) {
243 i = strlen(tmp);
244 if (!strncmp(tmp, "0x", 2)) {
245 i -= 2;
246 memmove(tmp, tmp + 2, i + 1);
247 }
248 if ((i > 512) || (i % 2)) {
249 msg_perr("Invalid SPI command ignorelist length\n");
250 free(tmp);
251 return 1;
252 }
253 spi_ignorelist_size = i / 2;
254 for (i = 0; i < spi_ignorelist_size * 2; i++) {
255 if (!isxdigit((unsigned char)tmp[i])) {
256 msg_perr("Invalid char \"%c\" in SPI command "
257 "ignorelist\n", tmp[i]);
258 free(tmp);
259 return 1;
260 }
261 }
262 for (i = 0; i < spi_ignorelist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000263 unsigned int tmp2;
264 /* SCNx8 is apparently not supported by MSVC (and thus
265 * MinGW), so work around it with an extra variable
266 */
267 sscanf(tmp + i * 2, "%2x", &tmp2);
268 spi_ignorelist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000269 }
270 msg_pdbg("SPI ignorelist is ");
271 for (i = 0; i < spi_ignorelist_size; i++)
272 msg_pdbg("%02x ", spi_ignorelist[i]);
273 msg_pdbg(", size %i\n", spi_ignorelist_size);
274 }
275 free(tmp);
276
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000277#if EMULATE_CHIP
278 tmp = extract_programmer_param("emulate");
279 if (!tmp) {
280 msg_pdbg("Not emulating any flash chip.\n");
281 /* Nothing else to do. */
David Hendricks8bb20212011-06-14 01:35:36 +0000282 goto dummy_init_out;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000283 }
284#if EMULATE_SPI_CHIP
285 if (!strcmp(tmp, "M25P10.RES")) {
286 emu_chip = EMULATE_ST_M25P10_RES;
287 emu_chip_size = 128 * 1024;
288 emu_max_byteprogram_size = 128;
289 emu_max_aai_size = 0;
290 emu_jedec_se_size = 0;
291 emu_jedec_be_52_size = 0;
292 emu_jedec_be_d8_size = 32 * 1024;
293 emu_jedec_ce_60_size = 0;
294 emu_jedec_ce_c7_size = emu_chip_size;
295 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
296 "write)\n");
297 }
298 if (!strcmp(tmp, "SST25VF040.REMS")) {
299 emu_chip = EMULATE_SST_SST25VF040_REMS;
300 emu_chip_size = 512 * 1024;
301 emu_max_byteprogram_size = 1;
302 emu_max_aai_size = 0;
303 emu_jedec_se_size = 4 * 1024;
304 emu_jedec_be_52_size = 32 * 1024;
305 emu_jedec_be_d8_size = 0;
306 emu_jedec_ce_60_size = emu_chip_size;
307 emu_jedec_ce_c7_size = 0;
308 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
309 "byte write)\n");
310 }
311 if (!strcmp(tmp, "SST25VF032B")) {
312 emu_chip = EMULATE_SST_SST25VF032B;
313 emu_chip_size = 4 * 1024 * 1024;
314 emu_max_byteprogram_size = 1;
315 emu_max_aai_size = 2;
316 emu_jedec_se_size = 4 * 1024;
317 emu_jedec_be_52_size = 32 * 1024;
318 emu_jedec_be_d8_size = 64 * 1024;
319 emu_jedec_ce_60_size = emu_chip_size;
320 emu_jedec_ce_c7_size = emu_chip_size;
321 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
322 "write)\n");
323 }
Stefan Tauner0b9df972012-05-07 22:12:16 +0000324 if (!strcmp(tmp, "MX25L6436")) {
325 emu_chip = EMULATE_MACRONIX_MX25L6436;
326 emu_chip_size = 8 * 1024 * 1024;
327 emu_max_byteprogram_size = 256;
328 emu_max_aai_size = 0;
329 emu_jedec_se_size = 4 * 1024;
330 emu_jedec_be_52_size = 32 * 1024;
331 emu_jedec_be_d8_size = 64 * 1024;
332 emu_jedec_ce_60_size = emu_chip_size;
333 emu_jedec_ce_c7_size = emu_chip_size;
334 msg_pdbg("Emulating Macronix MX25L6436 SPI flash chip (RDID, "
335 "SFDP)\n");
336 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000337#endif
338 if (emu_chip == EMULATE_NONE) {
339 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
340 free(tmp);
341 return 1;
342 }
343 free(tmp);
344 flashchip_contents = malloc(emu_chip_size);
345 if (!flashchip_contents) {
346 msg_perr("Out of memory!\n");
347 return 1;
348 }
David Hendricks8bb20212011-06-14 01:35:36 +0000349
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000350#ifdef EMULATE_SPI_CHIP
351 status = extract_programmer_param("spi_status");
352 if (status) {
353 char *endptr;
354 errno = 0;
355 emu_status = strtoul(status, &endptr, 0);
356 free(status);
357 if (errno != 0 || status == endptr) {
358 msg_perr("Error: initial status register specified, "
359 "but the value could not be converted.\n");
360 return 1;
361 }
362 msg_pdbg("Initial status register is set to 0x%02x.\n",
363 emu_status);
364 }
365#endif
366
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000367 msg_pdbg("Filling fake flash chip with 0xff, size %i\n", emu_chip_size);
368 memset(flashchip_contents, 0xff, emu_chip_size);
369
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000370 /* Will be freed by shutdown function if necessary. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000371 emu_persistent_image = extract_programmer_param("image");
372 if (!emu_persistent_image) {
373 /* Nothing else to do. */
David Hendricks8bb20212011-06-14 01:35:36 +0000374 goto dummy_init_out;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000375 }
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000376 /* We will silently (in default verbosity) ignore the file if it does not exist (yet) or the size does
377 * not match the emulated chip. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000378 if (!stat(emu_persistent_image, &image_stat)) {
379 msg_pdbg("Found persistent image %s, size %li ",
380 emu_persistent_image, (long)image_stat.st_size);
381 if (image_stat.st_size == emu_chip_size) {
382 msg_pdbg("matches.\n");
383 msg_pdbg("Reading %s\n", emu_persistent_image);
384 read_buf_from_file(flashchip_contents, emu_chip_size,
385 emu_persistent_image);
386 } else {
387 msg_pdbg("doesn't match.\n");
388 }
389 }
390#endif
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000391
David Hendricks8bb20212011-06-14 01:35:36 +0000392dummy_init_out:
393 if (register_shutdown(dummy_shutdown, NULL)) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000394 free(flashchip_contents);
David Hendricks8bb20212011-06-14 01:35:36 +0000395 return 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000396 }
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000397 if (dummy_buses_supported & (BUS_PARALLEL | BUS_LPC | BUS_FWH))
398 register_par_programmer(&par_programmer_dummy,
399 dummy_buses_supported &
400 (BUS_PARALLEL | BUS_LPC |
401 BUS_FWH));
402 if (dummy_buses_supported & BUS_SPI)
403 register_spi_programmer(&spi_programmer_dummyflasher);
404
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000405 return 0;
406}
407
Stefan Tauner305e0b92013-07-17 23:46:44 +0000408void *dummy_map(const char *descr, uintptr_t phys_addr, size_t len)
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000409{
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000410 msg_pspew("%s: Mapping %s, 0x%zx bytes at 0x%0*" PRIxPTR "\n",
Stefan Tauner0554ca52013-07-25 22:54:25 +0000411 __func__, descr, len, PRIxPTR_WIDTH, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000412 return (void *)phys_addr;
413}
414
415void dummy_unmap(void *virt_addr, size_t len)
416{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000417 msg_pspew("%s: Unmapping 0x%zx bytes at %p\n", __func__, len, virt_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000418}
419
Mark Marshallf20b7be2014-05-09 21:16:21 +0000420static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000421{
Stefan Taunerc2333752013-07-13 23:31:37 +0000422 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%02x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000423}
424
Mark Marshallf20b7be2014-05-09 21:16:21 +0000425static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000426{
Stefan Taunerc2333752013-07-13 23:31:37 +0000427 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%04x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000428}
429
Mark Marshallf20b7be2014-05-09 21:16:21 +0000430static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000431{
Stefan Taunerc2333752013-07-13 23:31:37 +0000432 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%08x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000433}
434
Mark Marshallf20b7be2014-05-09 21:16:21 +0000435static 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 +0000436{
437 size_t i;
Stefan Tauner0554ca52013-07-25 22:54:25 +0000438 msg_pspew("%s: addr=0x%" PRIxPTR ", len=0x%zx, writing data (hex):", __func__, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000439 for (i = 0; i < len; i++) {
440 if ((i % 16) == 0)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000441 msg_pspew("\n");
442 msg_pspew("%02x ", buf[i]);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000443 }
444}
445
Mark Marshallf20b7be2014-05-09 21:16:21 +0000446static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000447{
Stefan Taunerc2333752013-07-13 23:31:37 +0000448 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000449 return 0xff;
450}
451
Mark Marshallf20b7be2014-05-09 21:16:21 +0000452static uint16_t dummy_chip_readw(const struct flashctx *flash, const 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 ", returning 0xffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000455 return 0xffff;
456}
457
Mark Marshallf20b7be2014-05-09 21:16:21 +0000458static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000459{
Stefan Taunerc2333752013-07-13 23:31:37 +0000460 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffffffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000461 return 0xffffffff;
462}
463
Mark Marshallf20b7be2014-05-09 21:16:21 +0000464static 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 +0000465{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000466 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 +0000467 memset(buf, 0xff, len);
468 return;
469}
470
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000471#if EMULATE_SPI_CHIP
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000472static int emulate_spi_chip_response(unsigned int writecnt,
473 unsigned int readcnt,
474 const unsigned char *writearr,
475 unsigned char *readarr)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000476{
Stefan Tauner0b9df972012-05-07 22:12:16 +0000477 unsigned int offs, i, toread;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000478 static int unsigned aai_offs;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000479 const unsigned char sst25vf040_rems_response[2] = {0xbf, 0x44};
480 const unsigned char sst25vf032b_rems_response[2] = {0xbf, 0x4a};
481 const unsigned char mx25l6436_rems_response[2] = {0xc2, 0x16};
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000482
483 if (writecnt == 0) {
484 msg_perr("No command sent to the chip!\n");
485 return 1;
486 }
Paul Menzelac427b22012-02-16 21:07:07 +0000487 /* spi_blacklist has precedence over spi_ignorelist. */
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000488 for (i = 0; i < spi_blacklist_size; i++) {
489 if (writearr[0] == spi_blacklist[i]) {
490 msg_pdbg("Refusing blacklisted SPI command 0x%02x\n",
491 spi_blacklist[i]);
492 return SPI_INVALID_OPCODE;
493 }
494 }
495 for (i = 0; i < spi_ignorelist_size; i++) {
496 if (writearr[0] == spi_ignorelist[i]) {
497 msg_cdbg("Ignoring ignorelisted SPI command 0x%02x\n",
498 spi_ignorelist[i]);
499 /* Return success because the command does not fail,
500 * it is simply ignored.
501 */
502 return 0;
503 }
504 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000505
506 if (emu_max_aai_size && (emu_status & SPI_SR_AAI)) {
507 if (writearr[0] != JEDEC_AAI_WORD_PROGRAM &&
508 writearr[0] != JEDEC_WRDI &&
509 writearr[0] != JEDEC_RDSR) {
510 msg_perr("Forbidden opcode (0x%02x) attempted during "
511 "AAI sequence!\n", writearr[0]);
512 return 0;
513 }
514 }
515
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000516 switch (writearr[0]) {
517 case JEDEC_RES:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000518 if (writecnt < JEDEC_RES_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000519 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000520 /* offs calculation is only needed for SST chips which treat RES like REMS. */
521 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
522 offs += writecnt - JEDEC_REMS_OUTSIZE;
523 switch (emu_chip) {
524 case EMULATE_ST_M25P10_RES:
525 if (readcnt > 0)
526 memset(readarr, 0x10, readcnt);
527 break;
528 case EMULATE_SST_SST25VF040_REMS:
529 for (i = 0; i < readcnt; i++)
530 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
531 break;
532 case EMULATE_SST_SST25VF032B:
533 for (i = 0; i < readcnt; i++)
534 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
535 break;
536 case EMULATE_MACRONIX_MX25L6436:
537 if (readcnt > 0)
538 memset(readarr, 0x16, readcnt);
539 break;
540 default: /* ignore */
541 break;
542 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000543 break;
544 case JEDEC_REMS:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000545 /* REMS response has wraparound and uses an address parameter. */
546 if (writecnt < JEDEC_REMS_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000547 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000548 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
549 offs += writecnt - JEDEC_REMS_OUTSIZE;
550 switch (emu_chip) {
551 case EMULATE_SST_SST25VF040_REMS:
552 for (i = 0; i < readcnt; i++)
553 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
554 break;
555 case EMULATE_SST_SST25VF032B:
556 for (i = 0; i < readcnt; i++)
557 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
558 break;
559 case EMULATE_MACRONIX_MX25L6436:
560 for (i = 0; i < readcnt; i++)
561 readarr[i] = mx25l6436_rems_response[(offs + i) % 2];
562 break;
563 default: /* ignore */
564 break;
565 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000566 break;
567 case JEDEC_RDID:
Stefan Tauner0b9df972012-05-07 22:12:16 +0000568 switch (emu_chip) {
569 case EMULATE_SST_SST25VF032B:
570 if (readcnt > 0)
571 readarr[0] = 0xbf;
572 if (readcnt > 1)
573 readarr[1] = 0x25;
574 if (readcnt > 2)
575 readarr[2] = 0x4a;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000576 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000577 case EMULATE_MACRONIX_MX25L6436:
578 if (readcnt > 0)
579 readarr[0] = 0xc2;
580 if (readcnt > 1)
581 readarr[1] = 0x20;
582 if (readcnt > 2)
583 readarr[2] = 0x17;
584 break;
585 default: /* ignore */
586 break;
587 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000588 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000589 case JEDEC_RDSR:
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000590 memset(readarr, emu_status, readcnt);
591 break;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000592 /* FIXME: this should be chip-specific. */
593 case JEDEC_EWSR:
594 case JEDEC_WREN:
595 emu_status |= SPI_SR_WEL;
596 break;
597 case JEDEC_WRSR:
598 if (!(emu_status & SPI_SR_WEL)) {
599 msg_perr("WRSR attempted, but WEL is 0!\n");
600 break;
601 }
602 /* FIXME: add some reasonable simulation of the busy flag */
603 emu_status = writearr[1] & ~SPI_SR_WIP;
604 msg_pdbg2("WRSR wrote 0x%02x.\n", emu_status);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000605 break;
606 case JEDEC_READ:
607 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
608 /* Truncate to emu_chip_size. */
609 offs %= emu_chip_size;
610 if (readcnt > 0)
611 memcpy(readarr, flashchip_contents + offs, readcnt);
612 break;
613 case JEDEC_BYTE_PROGRAM:
614 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
615 /* Truncate to emu_chip_size. */
616 offs %= emu_chip_size;
617 if (writecnt < 5) {
618 msg_perr("BYTE PROGRAM size too short!\n");
619 return 1;
620 }
621 if (writecnt - 4 > emu_max_byteprogram_size) {
622 msg_perr("Max BYTE PROGRAM size exceeded!\n");
623 return 1;
624 }
625 memcpy(flashchip_contents + offs, writearr + 4, writecnt - 4);
626 break;
627 case JEDEC_AAI_WORD_PROGRAM:
628 if (!emu_max_aai_size)
629 break;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000630 if (!(emu_status & SPI_SR_AAI)) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000631 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
632 msg_perr("Initial AAI WORD PROGRAM size too "
633 "short!\n");
634 return 1;
635 }
636 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
637 msg_perr("Initial AAI WORD PROGRAM size too "
638 "long!\n");
639 return 1;
640 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000641 emu_status |= SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000642 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
643 writearr[3];
644 /* Truncate to emu_chip_size. */
645 aai_offs %= emu_chip_size;
646 memcpy(flashchip_contents + aai_offs, writearr + 4, 2);
647 aai_offs += 2;
648 } else {
649 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
650 msg_perr("Continuation AAI WORD PROGRAM size "
651 "too short!\n");
652 return 1;
653 }
654 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
655 msg_perr("Continuation AAI WORD PROGRAM size "
656 "too long!\n");
657 return 1;
658 }
659 memcpy(flashchip_contents + aai_offs, writearr + 1, 2);
660 aai_offs += 2;
661 }
662 break;
663 case JEDEC_WRDI:
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000664 if (emu_max_aai_size)
665 emu_status &= ~SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000666 break;
667 case JEDEC_SE:
668 if (!emu_jedec_se_size)
669 break;
670 if (writecnt != JEDEC_SE_OUTSIZE) {
671 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
672 return 1;
673 }
674 if (readcnt != JEDEC_SE_INSIZE) {
675 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
676 return 1;
677 }
678 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
679 if (offs & (emu_jedec_se_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000680 msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000681 offs &= ~(emu_jedec_se_size - 1);
682 memset(flashchip_contents + offs, 0xff, emu_jedec_se_size);
683 break;
684 case JEDEC_BE_52:
685 if (!emu_jedec_be_52_size)
686 break;
687 if (writecnt != JEDEC_BE_52_OUTSIZE) {
688 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
689 return 1;
690 }
691 if (readcnt != JEDEC_BE_52_INSIZE) {
692 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
693 return 1;
694 }
695 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
696 if (offs & (emu_jedec_be_52_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000697 msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000698 offs &= ~(emu_jedec_be_52_size - 1);
699 memset(flashchip_contents + offs, 0xff, emu_jedec_be_52_size);
700 break;
701 case JEDEC_BE_D8:
702 if (!emu_jedec_be_d8_size)
703 break;
704 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
705 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
706 return 1;
707 }
708 if (readcnt != JEDEC_BE_D8_INSIZE) {
709 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
710 return 1;
711 }
712 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
713 if (offs & (emu_jedec_be_d8_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000714 msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000715 offs &= ~(emu_jedec_be_d8_size - 1);
716 memset(flashchip_contents + offs, 0xff, emu_jedec_be_d8_size);
717 break;
718 case JEDEC_CE_60:
719 if (!emu_jedec_ce_60_size)
720 break;
721 if (writecnt != JEDEC_CE_60_OUTSIZE) {
722 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
723 return 1;
724 }
725 if (readcnt != JEDEC_CE_60_INSIZE) {
726 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
727 return 1;
728 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000729 /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000730 /* emu_jedec_ce_60_size is emu_chip_size. */
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000731 memset(flashchip_contents, 0xff, emu_jedec_ce_60_size);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000732 break;
733 case JEDEC_CE_C7:
734 if (!emu_jedec_ce_c7_size)
735 break;
736 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
737 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
738 return 1;
739 }
740 if (readcnt != JEDEC_CE_C7_INSIZE) {
741 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
742 return 1;
743 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000744 /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000745 /* emu_jedec_ce_c7_size is emu_chip_size. */
746 memset(flashchip_contents, 0xff, emu_jedec_ce_c7_size);
747 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000748 case JEDEC_SFDP:
749 if (emu_chip != EMULATE_MACRONIX_MX25L6436)
750 break;
751 if (writecnt < 4)
752 break;
753 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
754
755 /* SFDP expects one dummy byte after the address. */
756 if (writecnt == 4) {
757 /* The dummy byte was not written, make sure it is read instead.
758 * Shifting and shortening the read array does achieve this goal.
759 */
760 readarr++;
761 readcnt--;
762 } else {
763 /* The response is shifted if more than 5 bytes are written, because SFDP data is
764 * already shifted out by the chip while those superfluous bytes are written. */
765 offs += writecnt - 5;
766 }
767
768 /* The SFDP spec implies that the start address of an SFDP read may be truncated to fit in the
769 * SFDP table address space, i.e. the start address may be wrapped around at SFDP table size.
770 * This is a reasonable implementation choice in hardware because it saves a few gates. */
771 if (offs >= sizeof(sfdp_table)) {
772 msg_pdbg("Wrapping the start address around the SFDP table boundary (using 0x%x "
773 "instead of 0x%x).\n", (unsigned int)(offs % sizeof(sfdp_table)), offs);
774 offs %= sizeof(sfdp_table);
775 }
776 toread = min(sizeof(sfdp_table) - offs, readcnt);
777 memcpy(readarr, sfdp_table + offs, toread);
778 if (toread < readcnt)
779 msg_pdbg("Crossing the SFDP table boundary in a single "
780 "continuous chunk produces undefined results "
781 "after that point.\n");
782 break;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000783 default:
784 /* No special response. */
785 break;
786 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000787 if (writearr[0] != JEDEC_WREN && writearr[0] != JEDEC_EWSR)
788 emu_status &= ~SPI_SR_WEL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000789 return 0;
790}
791#endif
792
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000793static int dummy_spi_send_command(struct flashctx *flash, unsigned int writecnt,
794 unsigned int readcnt,
795 const unsigned char *writearr,
796 unsigned char *readarr)
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000797{
798 int i;
799
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000800 msg_pspew("%s:", __func__);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000801
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000802 msg_pspew(" writing %u bytes:", writecnt);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000803 for (i = 0; i < writecnt; i++)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000804 msg_pspew(" 0x%02x", writearr[i]);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000805
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000806 /* Response for unknown commands and missing chip is 0xff. */
807 memset(readarr, 0xff, readcnt);
808#if EMULATE_SPI_CHIP
809 switch (emu_chip) {
810 case EMULATE_ST_M25P10_RES:
811 case EMULATE_SST_SST25VF040_REMS:
812 case EMULATE_SST_SST25VF032B:
Stefan Tauner0b9df972012-05-07 22:12:16 +0000813 case EMULATE_MACRONIX_MX25L6436:
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000814 if (emulate_spi_chip_response(writecnt, readcnt, writearr,
815 readarr)) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000816 msg_pdbg("Invalid command sent to flash chip!\n");
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000817 return 1;
818 }
819 break;
820 default:
821 break;
822 }
823#endif
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000824 msg_pspew(" reading %u bytes:", readcnt);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000825 for (i = 0; i < readcnt; i++)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000826 msg_pspew(" 0x%02x", readarr[i]);
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000827 msg_pspew("\n");
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000828 return 0;
829}
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +0000830
Mark Marshallf20b7be2014-05-09 21:16:21 +0000831static 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 +0000832{
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000833 return spi_write_chunked(flash, buf, start, len,
834 spi_write_256_chunksize);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000835}