blob: 99f81f5f74052e4e8759da2ba139a8c4af4870f4 [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 Hailfingerc3129202009-05-09 00:54:55 +000022#include "flash.h"
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +000023#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000024#include "programmer.h"
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000025
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000026/* Remove the #define below if you don't want SPI flash chip emulation. */
27#define EMULATE_SPI_CHIP 1
28
29#if EMULATE_SPI_CHIP
30#define EMULATE_CHIP 1
31#include "spi.h"
32#endif
33
34#if EMULATE_CHIP
35#include <sys/types.h>
36#include <sys/stat.h>
37#endif
38
39#if EMULATE_CHIP
40static uint8_t *flashchip_contents = NULL;
41enum emu_chip {
42 EMULATE_NONE,
43 EMULATE_ST_M25P10_RES,
44 EMULATE_SST_SST25VF040_REMS,
45 EMULATE_SST_SST25VF032B,
46};
47static enum emu_chip emu_chip = EMULATE_NONE;
48static char *emu_persistent_image = NULL;
Stefan Taunerc69c9c82011-11-23 09:13:48 +000049static unsigned int emu_chip_size = 0;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000050#if EMULATE_SPI_CHIP
Stefan Taunerc69c9c82011-11-23 09:13:48 +000051static unsigned int emu_max_byteprogram_size = 0;
52static unsigned int emu_max_aai_size = 0;
53static unsigned int emu_jedec_se_size = 0;
54static unsigned int emu_jedec_be_52_size = 0;
55static unsigned int emu_jedec_be_d8_size = 0;
56static unsigned int emu_jedec_ce_60_size = 0;
57static unsigned int emu_jedec_ce_c7_size = 0;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000058#endif
59#endif
60
Stefan Taunerc69c9c82011-11-23 09:13:48 +000061static unsigned int spi_write_256_chunksize = 256;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000062
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000063static int dummy_spi_send_command(struct flashctx *flash, unsigned int writecnt,
64 unsigned int readcnt,
65 const unsigned char *writearr,
66 unsigned char *readarr);
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000067static int dummy_spi_write_256(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +000068 unsigned int start, unsigned int len);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000069static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val,
70 chipaddr addr);
71static void dummy_chip_writew(const struct flashctx *flash, uint16_t val,
72 chipaddr addr);
73static void dummy_chip_writel(const struct flashctx *flash, uint32_t val,
74 chipaddr addr);
75static void dummy_chip_writen(const struct flashctx *flash, uint8_t *buf,
76 chipaddr addr, size_t len);
77static uint8_t dummy_chip_readb(const struct flashctx *flash,
78 const chipaddr addr);
79static uint16_t dummy_chip_readw(const struct flashctx *flash,
80 const chipaddr addr);
81static uint32_t dummy_chip_readl(const struct flashctx *flash,
82 const chipaddr addr);
83static void dummy_chip_readn(const struct flashctx *flash, uint8_t *buf,
84 const chipaddr addr, size_t len);
Michael Karcherb9dbe482011-05-11 17:07:07 +000085
86static const struct spi_programmer spi_programmer_dummyflasher = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +000087 .type = SPI_CONTROLLER_DUMMY,
88 .max_data_read = MAX_DATA_READ_UNLIMITED,
89 .max_data_write = MAX_DATA_UNSPECIFIED,
90 .command = dummy_spi_send_command,
91 .multicommand = default_spi_send_multicommand,
92 .read = default_spi_read,
93 .write_256 = dummy_spi_write_256,
Michael Karcherb9dbe482011-05-11 17:07:07 +000094};
David Hendricks8bb20212011-06-14 01:35:36 +000095
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +000096static const struct par_programmer par_programmer_dummy = {
97 .chip_readb = dummy_chip_readb,
98 .chip_readw = dummy_chip_readw,
99 .chip_readl = dummy_chip_readl,
100 .chip_readn = dummy_chip_readn,
101 .chip_writeb = dummy_chip_writeb,
102 .chip_writew = dummy_chip_writew,
103 .chip_writel = dummy_chip_writel,
104 .chip_writen = dummy_chip_writen,
105};
106
107enum chipbustype dummy_buses_supported = BUS_NONE;
108
David Hendricks8bb20212011-06-14 01:35:36 +0000109static int dummy_shutdown(void *data)
110{
111 msg_pspew("%s\n", __func__);
112#if EMULATE_CHIP
113 if (emu_chip != EMULATE_NONE) {
114 if (emu_persistent_image) {
115 msg_pdbg("Writing %s\n", emu_persistent_image);
116 write_buf_to_file(flashchip_contents, emu_chip_size,
117 emu_persistent_image);
118 }
119 free(flashchip_contents);
120 }
121#endif
122 return 0;
123}
124
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000125int dummy_init(void)
126{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000127 char *bustext = NULL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000128 char *tmp = NULL;
129#if EMULATE_CHIP
130 struct stat image_stat;
131#endif
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000132
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000133 msg_pspew("%s\n", __func__);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000134
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000135 bustext = extract_programmer_param("bus");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000136 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
137 if (!bustext)
138 bustext = strdup("parallel+lpc+fwh+spi");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000139 /* Convert the parameters to lowercase. */
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000140 tolower_string(bustext);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000141
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000142 dummy_buses_supported = BUS_NONE;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000143 if (strstr(bustext, "parallel")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000144 dummy_buses_supported |= BUS_PARALLEL;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000145 msg_pdbg("Enabling support for %s flash.\n", "parallel");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000146 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000147 if (strstr(bustext, "lpc")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000148 dummy_buses_supported |= BUS_LPC;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000149 msg_pdbg("Enabling support for %s flash.\n", "LPC");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000150 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000151 if (strstr(bustext, "fwh")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000152 dummy_buses_supported |= BUS_FWH;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000153 msg_pdbg("Enabling support for %s flash.\n", "FWH");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000154 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000155 if (strstr(bustext, "spi")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000156 dummy_buses_supported |= BUS_SPI;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000157 msg_pdbg("Enabling support for %s flash.\n", "SPI");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000158 }
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000159 if (dummy_buses_supported == BUS_NONE)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000160 msg_pdbg("Support for all flash bus types disabled.\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000161 free(bustext);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000162
163 tmp = extract_programmer_param("spi_write_256_chunksize");
164 if (tmp) {
165 spi_write_256_chunksize = atoi(tmp);
166 free(tmp);
167 if (spi_write_256_chunksize < 1) {
168 msg_perr("invalid spi_write_256_chunksize\n");
169 return 1;
170 }
171 }
172
173#if EMULATE_CHIP
174 tmp = extract_programmer_param("emulate");
175 if (!tmp) {
176 msg_pdbg("Not emulating any flash chip.\n");
177 /* Nothing else to do. */
David Hendricks8bb20212011-06-14 01:35:36 +0000178 goto dummy_init_out;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000179 }
180#if EMULATE_SPI_CHIP
181 if (!strcmp(tmp, "M25P10.RES")) {
182 emu_chip = EMULATE_ST_M25P10_RES;
183 emu_chip_size = 128 * 1024;
184 emu_max_byteprogram_size = 128;
185 emu_max_aai_size = 0;
186 emu_jedec_se_size = 0;
187 emu_jedec_be_52_size = 0;
188 emu_jedec_be_d8_size = 32 * 1024;
189 emu_jedec_ce_60_size = 0;
190 emu_jedec_ce_c7_size = emu_chip_size;
191 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
192 "write)\n");
193 }
194 if (!strcmp(tmp, "SST25VF040.REMS")) {
195 emu_chip = EMULATE_SST_SST25VF040_REMS;
196 emu_chip_size = 512 * 1024;
197 emu_max_byteprogram_size = 1;
198 emu_max_aai_size = 0;
199 emu_jedec_se_size = 4 * 1024;
200 emu_jedec_be_52_size = 32 * 1024;
201 emu_jedec_be_d8_size = 0;
202 emu_jedec_ce_60_size = emu_chip_size;
203 emu_jedec_ce_c7_size = 0;
204 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
205 "byte write)\n");
206 }
207 if (!strcmp(tmp, "SST25VF032B")) {
208 emu_chip = EMULATE_SST_SST25VF032B;
209 emu_chip_size = 4 * 1024 * 1024;
210 emu_max_byteprogram_size = 1;
211 emu_max_aai_size = 2;
212 emu_jedec_se_size = 4 * 1024;
213 emu_jedec_be_52_size = 32 * 1024;
214 emu_jedec_be_d8_size = 64 * 1024;
215 emu_jedec_ce_60_size = emu_chip_size;
216 emu_jedec_ce_c7_size = emu_chip_size;
217 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
218 "write)\n");
219 }
220#endif
221 if (emu_chip == EMULATE_NONE) {
222 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
223 free(tmp);
224 return 1;
225 }
226 free(tmp);
227 flashchip_contents = malloc(emu_chip_size);
228 if (!flashchip_contents) {
229 msg_perr("Out of memory!\n");
230 return 1;
231 }
David Hendricks8bb20212011-06-14 01:35:36 +0000232
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000233 msg_pdbg("Filling fake flash chip with 0xff, size %i\n", emu_chip_size);
234 memset(flashchip_contents, 0xff, emu_chip_size);
235
236 emu_persistent_image = extract_programmer_param("image");
237 if (!emu_persistent_image) {
238 /* Nothing else to do. */
David Hendricks8bb20212011-06-14 01:35:36 +0000239 goto dummy_init_out;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000240 }
241 if (!stat(emu_persistent_image, &image_stat)) {
242 msg_pdbg("Found persistent image %s, size %li ",
243 emu_persistent_image, (long)image_stat.st_size);
244 if (image_stat.st_size == emu_chip_size) {
245 msg_pdbg("matches.\n");
246 msg_pdbg("Reading %s\n", emu_persistent_image);
247 read_buf_from_file(flashchip_contents, emu_chip_size,
248 emu_persistent_image);
249 } else {
250 msg_pdbg("doesn't match.\n");
251 }
252 }
253#endif
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000254
David Hendricks8bb20212011-06-14 01:35:36 +0000255dummy_init_out:
256 if (register_shutdown(dummy_shutdown, NULL)) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000257 free(flashchip_contents);
David Hendricks8bb20212011-06-14 01:35:36 +0000258 return 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000259 }
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000260 if (dummy_buses_supported & (BUS_PARALLEL | BUS_LPC | BUS_FWH))
261 register_par_programmer(&par_programmer_dummy,
262 dummy_buses_supported &
263 (BUS_PARALLEL | BUS_LPC |
264 BUS_FWH));
265 if (dummy_buses_supported & BUS_SPI)
266 register_spi_programmer(&spi_programmer_dummyflasher);
267
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000268 return 0;
269}
270
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000271void *dummy_map(const char *descr, unsigned long phys_addr, size_t len)
272{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000273 msg_pspew("%s: Mapping %s, 0x%lx bytes at 0x%08lx\n",
274 __func__, descr, (unsigned long)len, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000275 return (void *)phys_addr;
276}
277
278void dummy_unmap(void *virt_addr, size_t len)
279{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000280 msg_pspew("%s: Unmapping 0x%lx bytes at %p\n",
281 __func__, (unsigned long)len, virt_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000282}
283
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000284static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val,
285 chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000286{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000287 msg_pspew("%s: addr=0x%lx, val=0x%02x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000288}
289
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000290static void dummy_chip_writew(const struct flashctx *flash, uint16_t val,
291 chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000292{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000293 msg_pspew("%s: addr=0x%lx, val=0x%04x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000294}
295
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000296static void dummy_chip_writel(const struct flashctx *flash, uint32_t val,
297 chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000298{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000299 msg_pspew("%s: addr=0x%lx, val=0x%08x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000300}
301
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000302static void dummy_chip_writen(const struct flashctx *flash, uint8_t *buf,
303 chipaddr addr, size_t len)
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000304{
305 size_t i;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000306 msg_pspew("%s: addr=0x%lx, len=0x%08lx, writing data (hex):",
307 __func__, addr, (unsigned long)len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000308 for (i = 0; i < len; i++) {
309 if ((i % 16) == 0)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000310 msg_pspew("\n");
311 msg_pspew("%02x ", buf[i]);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000312 }
313}
314
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000315static uint8_t dummy_chip_readb(const struct flashctx *flash,
316 const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000317{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000318 msg_pspew("%s: addr=0x%lx, returning 0xff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000319 return 0xff;
320}
321
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000322static uint16_t dummy_chip_readw(const struct flashctx *flash,
323 const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000324{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000325 msg_pspew("%s: addr=0x%lx, returning 0xffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000326 return 0xffff;
327}
328
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000329static uint32_t dummy_chip_readl(const struct flashctx *flash,
330 const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000331{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000332 msg_pspew("%s: addr=0x%lx, returning 0xffffffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000333 return 0xffffffff;
334}
335
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000336static void dummy_chip_readn(const struct flashctx *flash, uint8_t *buf,
337 const chipaddr addr, size_t len)
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000338{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000339 msg_pspew("%s: addr=0x%lx, len=0x%lx, returning array of 0xff\n",
340 __func__, addr, (unsigned long)len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000341 memset(buf, 0xff, len);
342 return;
343}
344
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000345#if EMULATE_SPI_CHIP
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000346static int emulate_spi_chip_response(unsigned int writecnt,
347 unsigned int readcnt,
348 const unsigned char *writearr,
349 unsigned char *readarr)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000350{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000351 unsigned int offs;
352 static int unsigned aai_offs;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000353 static int aai_active = 0;
354
355 if (writecnt == 0) {
356 msg_perr("No command sent to the chip!\n");
357 return 1;
358 }
359 /* TODO: Implement command blacklists here. */
360 switch (writearr[0]) {
361 case JEDEC_RES:
362 if (emu_chip != EMULATE_ST_M25P10_RES)
363 break;
364 /* Respond with ST_M25P10_RES. */
365 if (readcnt > 0)
366 readarr[0] = 0x10;
367 break;
368 case JEDEC_REMS:
369 if (emu_chip != EMULATE_SST_SST25VF040_REMS)
370 break;
371 /* Respond with SST_SST25VF040_REMS. */
372 if (readcnt > 0)
373 readarr[0] = 0xbf;
374 if (readcnt > 1)
375 readarr[1] = 0x44;
376 break;
377 case JEDEC_RDID:
378 if (emu_chip != EMULATE_SST_SST25VF032B)
379 break;
380 /* Respond with SST_SST25VF032B. */
381 if (readcnt > 0)
382 readarr[0] = 0xbf;
383 if (readcnt > 1)
384 readarr[1] = 0x25;
385 if (readcnt > 2)
386 readarr[2] = 0x4a;
387 break;
388 case JEDEC_RDSR:
389 memset(readarr, 0, readcnt);
390 if (aai_active)
391 memset(readarr, 1 << 6, readcnt);
392 break;
393 case JEDEC_READ:
394 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
395 /* Truncate to emu_chip_size. */
396 offs %= emu_chip_size;
397 if (readcnt > 0)
398 memcpy(readarr, flashchip_contents + offs, readcnt);
399 break;
400 case JEDEC_BYTE_PROGRAM:
401 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
402 /* Truncate to emu_chip_size. */
403 offs %= emu_chip_size;
404 if (writecnt < 5) {
405 msg_perr("BYTE PROGRAM size too short!\n");
406 return 1;
407 }
408 if (writecnt - 4 > emu_max_byteprogram_size) {
409 msg_perr("Max BYTE PROGRAM size exceeded!\n");
410 return 1;
411 }
412 memcpy(flashchip_contents + offs, writearr + 4, writecnt - 4);
413 break;
414 case JEDEC_AAI_WORD_PROGRAM:
415 if (!emu_max_aai_size)
416 break;
417 if (!aai_active) {
418 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
419 msg_perr("Initial AAI WORD PROGRAM size too "
420 "short!\n");
421 return 1;
422 }
423 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
424 msg_perr("Initial AAI WORD PROGRAM size too "
425 "long!\n");
426 return 1;
427 }
428 aai_active = 1;
429 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
430 writearr[3];
431 /* Truncate to emu_chip_size. */
432 aai_offs %= emu_chip_size;
433 memcpy(flashchip_contents + aai_offs, writearr + 4, 2);
434 aai_offs += 2;
435 } else {
436 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
437 msg_perr("Continuation AAI WORD PROGRAM size "
438 "too short!\n");
439 return 1;
440 }
441 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
442 msg_perr("Continuation AAI WORD PROGRAM size "
443 "too long!\n");
444 return 1;
445 }
446 memcpy(flashchip_contents + aai_offs, writearr + 1, 2);
447 aai_offs += 2;
448 }
449 break;
450 case JEDEC_WRDI:
451 if (!emu_max_aai_size)
452 break;
453 aai_active = 0;
454 break;
455 case JEDEC_SE:
456 if (!emu_jedec_se_size)
457 break;
458 if (writecnt != JEDEC_SE_OUTSIZE) {
459 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
460 return 1;
461 }
462 if (readcnt != JEDEC_SE_INSIZE) {
463 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
464 return 1;
465 }
466 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
467 if (offs & (emu_jedec_se_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000468 msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000469 offs &= ~(emu_jedec_se_size - 1);
470 memset(flashchip_contents + offs, 0xff, emu_jedec_se_size);
471 break;
472 case JEDEC_BE_52:
473 if (!emu_jedec_be_52_size)
474 break;
475 if (writecnt != JEDEC_BE_52_OUTSIZE) {
476 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
477 return 1;
478 }
479 if (readcnt != JEDEC_BE_52_INSIZE) {
480 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
481 return 1;
482 }
483 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
484 if (offs & (emu_jedec_be_52_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000485 msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000486 offs &= ~(emu_jedec_be_52_size - 1);
487 memset(flashchip_contents + offs, 0xff, emu_jedec_be_52_size);
488 break;
489 case JEDEC_BE_D8:
490 if (!emu_jedec_be_d8_size)
491 break;
492 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
493 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
494 return 1;
495 }
496 if (readcnt != JEDEC_BE_D8_INSIZE) {
497 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
498 return 1;
499 }
500 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
501 if (offs & (emu_jedec_be_d8_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000502 msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000503 offs &= ~(emu_jedec_be_d8_size - 1);
504 memset(flashchip_contents + offs, 0xff, emu_jedec_be_d8_size);
505 break;
506 case JEDEC_CE_60:
507 if (!emu_jedec_ce_60_size)
508 break;
509 if (writecnt != JEDEC_CE_60_OUTSIZE) {
510 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
511 return 1;
512 }
513 if (readcnt != JEDEC_CE_60_INSIZE) {
514 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
515 return 1;
516 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000517 /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000518 /* emu_jedec_ce_60_size is emu_chip_size. */
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000519 memset(flashchip_contents, 0xff, emu_jedec_ce_60_size);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000520 break;
521 case JEDEC_CE_C7:
522 if (!emu_jedec_ce_c7_size)
523 break;
524 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
525 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
526 return 1;
527 }
528 if (readcnt != JEDEC_CE_C7_INSIZE) {
529 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
530 return 1;
531 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000532 /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000533 /* emu_jedec_ce_c7_size is emu_chip_size. */
534 memset(flashchip_contents, 0xff, emu_jedec_ce_c7_size);
535 break;
536 default:
537 /* No special response. */
538 break;
539 }
540 return 0;
541}
542#endif
543
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000544static int dummy_spi_send_command(struct flashctx *flash, unsigned int writecnt,
545 unsigned int readcnt,
546 const unsigned char *writearr,
547 unsigned char *readarr)
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000548{
549 int i;
550
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000551 msg_pspew("%s:", __func__);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000552
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000553 msg_pspew(" writing %u bytes:", writecnt);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000554 for (i = 0; i < writecnt; i++)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000555 msg_pspew(" 0x%02x", writearr[i]);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000556
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000557 /* Response for unknown commands and missing chip is 0xff. */
558 memset(readarr, 0xff, readcnt);
559#if EMULATE_SPI_CHIP
560 switch (emu_chip) {
561 case EMULATE_ST_M25P10_RES:
562 case EMULATE_SST_SST25VF040_REMS:
563 case EMULATE_SST_SST25VF032B:
564 if (emulate_spi_chip_response(writecnt, readcnt, writearr,
565 readarr)) {
566 msg_perr("Invalid command sent to flash chip!\n");
567 return 1;
568 }
569 break;
570 default:
571 break;
572 }
573#endif
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000574 msg_pspew(" reading %u bytes:", readcnt);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000575 for (i = 0; i < readcnt; i++)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000576 msg_pspew(" 0x%02x", readarr[i]);
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000577 msg_pspew("\n");
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000578 return 0;
579}
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +0000580
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000581static int dummy_spi_write_256(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000582 unsigned int start, unsigned int len)
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000583{
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000584 return spi_write_chunked(flash, buf, start, len,
585 spi_write_256_chunksize);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000586}