blob: e8b40143567c13a39a7dabf5e2dc41d936633c9f [file] [log] [blame]
hailfingera9df33c2009-05-09 00:54:55 +00001/*
2 * This file is part of the flashrom project.
3 *
hailfinger6ead7222010-11-01 22:07:04 +00004 * Copyright (C) 2009,2010 Carl-Daniel Hailfinger
hailfingera9df33c2009-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
hailfinger6ead7222010-11-01 22:07:04 +00008 * the Free Software Foundation; version 2 of the License.
hailfingera9df33c2009-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
hailfingera9df33c2009-05-09 00:54:55 +000020#include <string.h>
21#include <stdlib.h>
hailfingera9df33c2009-05-09 00:54:55 +000022#include "flash.h"
hailfingera8727712010-06-20 10:58:32 +000023#include "chipdrivers.h"
hailfinger428f6852010-07-27 22:41:39 +000024#include "programmer.h"
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +080025#include "flashchips.h"
hailfingera9df33c2009-05-09 00:54:55 +000026
hailfinger6ead7222010-11-01 22:07:04 +000027/* Remove the #define below if you don't want SPI flash chip emulation. */
28#define EMULATE_SPI_CHIP 1
29
30#if EMULATE_SPI_CHIP
31#define EMULATE_CHIP 1
32#include "spi.h"
33#endif
34
35#if EMULATE_CHIP
36#include <sys/types.h>
37#include <sys/stat.h>
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +080038
39#if EMULATE_SPI_CHIP
40/* The name of variable-size virtual chip. A 4MB flash example:
41 * flashrom -p dummy:emulate=VARIABLE_SIZE,size=4194304
42 */
43#define VARIABLE_SIZE_CHIP_NAME "VARIABLE_SIZE"
44#endif
hailfinger6ead7222010-11-01 22:07:04 +000045#endif
46
47#if EMULATE_CHIP
48static uint8_t *flashchip_contents = NULL;
49enum emu_chip {
50 EMULATE_NONE,
51 EMULATE_ST_M25P10_RES,
52 EMULATE_SST_SST25VF040_REMS,
53 EMULATE_SST_SST25VF032B,
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +080054 EMULATE_VARIABLE_SIZE,
hailfinger6ead7222010-11-01 22:07:04 +000055};
56static enum emu_chip emu_chip = EMULATE_NONE;
57static char *emu_persistent_image = NULL;
stefanctc5eb8a92011-11-23 09:13:48 +000058static unsigned int emu_chip_size = 0;
hailfinger6ead7222010-11-01 22:07:04 +000059#if EMULATE_SPI_CHIP
stefanctc5eb8a92011-11-23 09:13:48 +000060static unsigned int emu_max_byteprogram_size = 0;
61static unsigned int emu_max_aai_size = 0;
62static unsigned int emu_jedec_se_size = 0;
63static unsigned int emu_jedec_be_52_size = 0;
64static unsigned int emu_jedec_be_d8_size = 0;
65static unsigned int emu_jedec_ce_60_size = 0;
66static unsigned int emu_jedec_ce_c7_size = 0;
hailfinger6ead7222010-11-01 22:07:04 +000067#endif
68#endif
69
stefanctc5eb8a92011-11-23 09:13:48 +000070static unsigned int spi_write_256_chunksize = 256;
hailfinger6ead7222010-11-01 22:07:04 +000071
mkarcherd264e9e2011-05-11 17:07:07 +000072static int dummy_spi_send_command(unsigned int writecnt, unsigned int readcnt,
73 const unsigned char *writearr, unsigned char *readarr);
uwe8d342eb2011-07-28 08:13:25 +000074static int dummy_spi_write_256(struct flashchip *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +000075 unsigned int start, unsigned int len);
mkarcherd264e9e2011-05-11 17:07:07 +000076
77static const struct spi_programmer spi_programmer_dummyflasher = {
uwe8d342eb2011-07-28 08:13:25 +000078 .type = SPI_CONTROLLER_DUMMY,
79 .max_data_read = MAX_DATA_READ_UNLIMITED,
80 .max_data_write = MAX_DATA_UNSPECIFIED,
81 .command = dummy_spi_send_command,
82 .multicommand = default_spi_send_multicommand,
83 .read = default_spi_read,
84 .write_256 = dummy_spi_write_256,
mkarcherd264e9e2011-05-11 17:07:07 +000085};
dhendrix0ffc2eb2011-06-14 01:35:36 +000086
hailfinger76bb7e92011-11-09 23:40:00 +000087static const struct par_programmer par_programmer_dummy = {
88 .chip_readb = dummy_chip_readb,
89 .chip_readw = dummy_chip_readw,
90 .chip_readl = dummy_chip_readl,
91 .chip_readn = dummy_chip_readn,
92 .chip_writeb = dummy_chip_writeb,
93 .chip_writew = dummy_chip_writew,
94 .chip_writel = dummy_chip_writel,
95 .chip_writen = dummy_chip_writen,
96};
97
98enum chipbustype dummy_buses_supported = BUS_NONE;
99
dhendrix0ffc2eb2011-06-14 01:35:36 +0000100static int dummy_shutdown(void *data)
101{
102 msg_pspew("%s\n", __func__);
103#if EMULATE_CHIP
104 if (emu_chip != EMULATE_NONE) {
105 if (emu_persistent_image) {
106 msg_pdbg("Writing %s\n", emu_persistent_image);
107 write_buf_to_file(flashchip_contents, emu_chip_size,
108 emu_persistent_image);
109 }
110 free(flashchip_contents);
111 }
112#endif
113 return 0;
114}
115
hailfingera9df33c2009-05-09 00:54:55 +0000116int dummy_init(void)
117{
hailfinger1ef766d2010-07-06 09:55:48 +0000118 char *bustext = NULL;
hailfinger6ead7222010-11-01 22:07:04 +0000119 char *tmp = NULL;
120#if EMULATE_CHIP
121 struct stat image_stat;
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800122#if EMULATE_SPI_CHIP
123 int size = -1; /* size for generic chip */
124#endif
hailfinger6ead7222010-11-01 22:07:04 +0000125#endif
hailfinger1ef766d2010-07-06 09:55:48 +0000126
hailfinger50c335f2010-01-09 04:32:23 +0000127 msg_pspew("%s\n", __func__);
hailfinger668f3502009-06-01 00:02:11 +0000128
hailfingerddeb4ac2010-07-08 10:13:37 +0000129 bustext = extract_programmer_param("bus");
hailfinger1ef766d2010-07-06 09:55:48 +0000130 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
131 if (!bustext)
132 bustext = strdup("parallel+lpc+fwh+spi");
hailfinger668f3502009-06-01 00:02:11 +0000133 /* Convert the parameters to lowercase. */
hailfinger1ef766d2010-07-06 09:55:48 +0000134 tolower_string(bustext);
hailfinger668f3502009-06-01 00:02:11 +0000135
hailfinger76bb7e92011-11-09 23:40:00 +0000136 dummy_buses_supported = BUS_NONE;
hailfinger1ef766d2010-07-06 09:55:48 +0000137 if (strstr(bustext, "parallel")) {
hailfinger76bb7e92011-11-09 23:40:00 +0000138 dummy_buses_supported |= BUS_PARALLEL;
hailfinger50c335f2010-01-09 04:32:23 +0000139 msg_pdbg("Enabling support for %s flash.\n", "parallel");
hailfinger668f3502009-06-01 00:02:11 +0000140 }
hailfinger1ef766d2010-07-06 09:55:48 +0000141 if (strstr(bustext, "lpc")) {
hailfinger76bb7e92011-11-09 23:40:00 +0000142 dummy_buses_supported |= BUS_LPC;
hailfinger50c335f2010-01-09 04:32:23 +0000143 msg_pdbg("Enabling support for %s flash.\n", "LPC");
hailfinger668f3502009-06-01 00:02:11 +0000144 }
hailfinger1ef766d2010-07-06 09:55:48 +0000145 if (strstr(bustext, "fwh")) {
hailfinger76bb7e92011-11-09 23:40:00 +0000146 dummy_buses_supported |= BUS_FWH;
hailfinger50c335f2010-01-09 04:32:23 +0000147 msg_pdbg("Enabling support for %s flash.\n", "FWH");
hailfinger668f3502009-06-01 00:02:11 +0000148 }
hailfinger1ef766d2010-07-06 09:55:48 +0000149 if (strstr(bustext, "spi")) {
hailfinger76bb7e92011-11-09 23:40:00 +0000150 dummy_buses_supported |= BUS_SPI;
hailfinger50c335f2010-01-09 04:32:23 +0000151 msg_pdbg("Enabling support for %s flash.\n", "SPI");
hailfinger668f3502009-06-01 00:02:11 +0000152 }
hailfinger76bb7e92011-11-09 23:40:00 +0000153 if (dummy_buses_supported == BUS_NONE)
hailfinger50c335f2010-01-09 04:32:23 +0000154 msg_pdbg("Support for all flash bus types disabled.\n");
hailfinger1ef766d2010-07-06 09:55:48 +0000155 free(bustext);
hailfinger6ead7222010-11-01 22:07:04 +0000156
157 tmp = extract_programmer_param("spi_write_256_chunksize");
158 if (tmp) {
159 spi_write_256_chunksize = atoi(tmp);
160 free(tmp);
161 if (spi_write_256_chunksize < 1) {
162 msg_perr("invalid spi_write_256_chunksize\n");
163 return 1;
164 }
165 }
166
167#if EMULATE_CHIP
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800168#if EMULATE_SPI_CHIP
169 tmp = extract_programmer_param("size");
170 if (tmp) {
171 int multiplier = 1;
172 if (strlen(tmp)) {
173 int remove_last_char = 1;
174 switch (tmp[strlen(tmp) - 1]) {
175 case 'k': case 'K':
176 multiplier = 1024;
177 break;
178 case 'm': case 'M':
179 multiplier = 1024 * 1024;
180 break;
181 default:
182 remove_last_char = 0;
183 break;
184 }
185 if (remove_last_char) tmp[strlen(tmp) - 1] = '\0';
186 }
187 size = atoi(tmp) * multiplier;
188 }
189#endif
190
hailfinger6ead7222010-11-01 22:07:04 +0000191 tmp = extract_programmer_param("emulate");
192 if (!tmp) {
193 msg_pdbg("Not emulating any flash chip.\n");
194 /* Nothing else to do. */
dhendrix0ffc2eb2011-06-14 01:35:36 +0000195 goto dummy_init_out;
hailfinger6ead7222010-11-01 22:07:04 +0000196 }
197#if EMULATE_SPI_CHIP
198 if (!strcmp(tmp, "M25P10.RES")) {
199 emu_chip = EMULATE_ST_M25P10_RES;
200 emu_chip_size = 128 * 1024;
201 emu_max_byteprogram_size = 128;
202 emu_max_aai_size = 0;
203 emu_jedec_se_size = 0;
204 emu_jedec_be_52_size = 0;
205 emu_jedec_be_d8_size = 32 * 1024;
206 emu_jedec_ce_60_size = 0;
207 emu_jedec_ce_c7_size = emu_chip_size;
208 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
209 "write)\n");
210 }
211 if (!strcmp(tmp, "SST25VF040.REMS")) {
212 emu_chip = EMULATE_SST_SST25VF040_REMS;
213 emu_chip_size = 512 * 1024;
214 emu_max_byteprogram_size = 1;
215 emu_max_aai_size = 0;
216 emu_jedec_se_size = 4 * 1024;
217 emu_jedec_be_52_size = 32 * 1024;
218 emu_jedec_be_d8_size = 0;
219 emu_jedec_ce_60_size = emu_chip_size;
220 emu_jedec_ce_c7_size = 0;
221 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
222 "byte write)\n");
223 }
224 if (!strcmp(tmp, "SST25VF032B")) {
225 emu_chip = EMULATE_SST_SST25VF032B;
226 emu_chip_size = 4 * 1024 * 1024;
227 emu_max_byteprogram_size = 1;
228 emu_max_aai_size = 2;
229 emu_jedec_se_size = 4 * 1024;
230 emu_jedec_be_52_size = 32 * 1024;
231 emu_jedec_be_d8_size = 64 * 1024;
232 emu_jedec_ce_60_size = emu_chip_size;
233 emu_jedec_ce_c7_size = emu_chip_size;
234 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
235 "write)\n");
236 }
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800237 if (!strncmp(tmp, VARIABLE_SIZE_CHIP_NAME,
238 strlen(VARIABLE_SIZE_CHIP_NAME))) {
239 if (size == -1) {
240 msg_perr("%s: the size parameter is not given.\n",
241 __func__);
242 free(tmp);
243 return 1;
244 }
245 emu_chip = EMULATE_VARIABLE_SIZE;
246 emu_chip_size = size;
247 emu_max_byteprogram_size = 256;
248 emu_max_aai_size = 0;
249 emu_jedec_se_size = 4 * 1024;
250 emu_jedec_be_52_size = 32 * 1024;
251 emu_jedec_be_d8_size = 64 * 1024;
252 emu_jedec_ce_60_size = emu_chip_size;
253 emu_jedec_ce_c7_size = emu_chip_size;
254 msg_pdbg("Emulating generic SPI flash chip (size=%d bytes)\n",
255 emu_chip_size);
256 }
hailfinger6ead7222010-11-01 22:07:04 +0000257#endif
258 if (emu_chip == EMULATE_NONE) {
259 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
260 free(tmp);
261 return 1;
262 }
263 free(tmp);
264 flashchip_contents = malloc(emu_chip_size);
265 if (!flashchip_contents) {
266 msg_perr("Out of memory!\n");
267 return 1;
268 }
dhendrix0ffc2eb2011-06-14 01:35:36 +0000269
hailfinger6ead7222010-11-01 22:07:04 +0000270 msg_pdbg("Filling fake flash chip with 0xff, size %i\n", emu_chip_size);
271 memset(flashchip_contents, 0xff, emu_chip_size);
272
273 emu_persistent_image = extract_programmer_param("image");
274 if (!emu_persistent_image) {
275 /* Nothing else to do. */
dhendrix0ffc2eb2011-06-14 01:35:36 +0000276 goto dummy_init_out;
hailfinger6ead7222010-11-01 22:07:04 +0000277 }
278 if (!stat(emu_persistent_image, &image_stat)) {
279 msg_pdbg("Found persistent image %s, size %li ",
280 emu_persistent_image, (long)image_stat.st_size);
281 if (image_stat.st_size == emu_chip_size) {
282 msg_pdbg("matches.\n");
283 msg_pdbg("Reading %s\n", emu_persistent_image);
284 read_buf_from_file(flashchip_contents, emu_chip_size,
285 emu_persistent_image);
286 } else {
287 msg_pdbg("doesn't match.\n");
288 }
289 }
290#endif
hailfingera9df33c2009-05-09 00:54:55 +0000291
dhendrix0ffc2eb2011-06-14 01:35:36 +0000292dummy_init_out:
293 if (register_shutdown(dummy_shutdown, NULL)) {
hailfinger6ead7222010-11-01 22:07:04 +0000294 free(flashchip_contents);
dhendrix0ffc2eb2011-06-14 01:35:36 +0000295 return 1;
hailfinger6ead7222010-11-01 22:07:04 +0000296 }
hailfinger76bb7e92011-11-09 23:40:00 +0000297 if (dummy_buses_supported & (BUS_PARALLEL | BUS_LPC | BUS_FWH))
298 register_par_programmer(&par_programmer_dummy,
299 dummy_buses_supported &
300 (BUS_PARALLEL | BUS_LPC |
301 BUS_FWH));
302 if (dummy_buses_supported & BUS_SPI)
303 register_spi_programmer(&spi_programmer_dummyflasher);
304
hailfingera9df33c2009-05-09 00:54:55 +0000305 return 0;
306}
307
hailfinger11ae3c42009-05-11 14:13:25 +0000308void *dummy_map(const char *descr, unsigned long phys_addr, size_t len)
309{
hailfinger50c335f2010-01-09 04:32:23 +0000310 msg_pspew("%s: Mapping %s, 0x%lx bytes at 0x%08lx\n",
311 __func__, descr, (unsigned long)len, phys_addr);
hailfinger11ae3c42009-05-11 14:13:25 +0000312 return (void *)phys_addr;
313}
314
315void dummy_unmap(void *virt_addr, size_t len)
316{
hailfinger50c335f2010-01-09 04:32:23 +0000317 msg_pspew("%s: Unmapping 0x%lx bytes at %p\n",
318 __func__, (unsigned long)len, virt_addr);
hailfinger11ae3c42009-05-11 14:13:25 +0000319}
320
hailfinger82719632009-05-16 21:22:56 +0000321void dummy_chip_writeb(uint8_t val, chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000322{
hailfinger50c335f2010-01-09 04:32:23 +0000323 msg_pspew("%s: addr=0x%lx, val=0x%02x\n", __func__, addr, val);
hailfingera9df33c2009-05-09 00:54:55 +0000324}
325
hailfinger82719632009-05-16 21:22:56 +0000326void dummy_chip_writew(uint16_t val, chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000327{
hailfinger50c335f2010-01-09 04:32:23 +0000328 msg_pspew("%s: addr=0x%lx, val=0x%04x\n", __func__, addr, val);
hailfingera9df33c2009-05-09 00:54:55 +0000329}
330
hailfinger82719632009-05-16 21:22:56 +0000331void dummy_chip_writel(uint32_t val, chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000332{
hailfinger50c335f2010-01-09 04:32:23 +0000333 msg_pspew("%s: addr=0x%lx, val=0x%08x\n", __func__, addr, val);
hailfingera9df33c2009-05-09 00:54:55 +0000334}
335
hailfinger9d987ef2009-06-05 18:32:07 +0000336void dummy_chip_writen(uint8_t *buf, chipaddr addr, size_t len)
337{
338 size_t i;
hailfinger50c335f2010-01-09 04:32:23 +0000339 msg_pspew("%s: addr=0x%lx, len=0x%08lx, writing data (hex):",
340 __func__, addr, (unsigned long)len);
hailfinger9d987ef2009-06-05 18:32:07 +0000341 for (i = 0; i < len; i++) {
342 if ((i % 16) == 0)
hailfinger50c335f2010-01-09 04:32:23 +0000343 msg_pspew("\n");
344 msg_pspew("%02x ", buf[i]);
hailfinger9d987ef2009-06-05 18:32:07 +0000345 }
346}
347
hailfinger82719632009-05-16 21:22:56 +0000348uint8_t dummy_chip_readb(const chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000349{
hailfinger50c335f2010-01-09 04:32:23 +0000350 msg_pspew("%s: addr=0x%lx, returning 0xff\n", __func__, addr);
hailfingera9df33c2009-05-09 00:54:55 +0000351 return 0xff;
352}
353
hailfinger82719632009-05-16 21:22:56 +0000354uint16_t dummy_chip_readw(const chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000355{
hailfinger50c335f2010-01-09 04:32:23 +0000356 msg_pspew("%s: addr=0x%lx, returning 0xffff\n", __func__, addr);
hailfingera9df33c2009-05-09 00:54:55 +0000357 return 0xffff;
358}
359
hailfinger82719632009-05-16 21:22:56 +0000360uint32_t dummy_chip_readl(const chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000361{
hailfinger50c335f2010-01-09 04:32:23 +0000362 msg_pspew("%s: addr=0x%lx, returning 0xffffffff\n", __func__, addr);
hailfingera9df33c2009-05-09 00:54:55 +0000363 return 0xffffffff;
364}
365
hailfinger9d987ef2009-06-05 18:32:07 +0000366void dummy_chip_readn(uint8_t *buf, const chipaddr addr, size_t len)
367{
hailfinger50c335f2010-01-09 04:32:23 +0000368 msg_pspew("%s: addr=0x%lx, len=0x%lx, returning array of 0xff\n",
369 __func__, addr, (unsigned long)len);
hailfinger9d987ef2009-06-05 18:32:07 +0000370 memset(buf, 0xff, len);
371 return;
372}
373
hailfinger6ead7222010-11-01 22:07:04 +0000374#if EMULATE_SPI_CHIP
375static int emulate_spi_chip_response(unsigned int writecnt, unsigned int readcnt,
376 const unsigned char *writearr, unsigned char *readarr)
377{
stefanctc5eb8a92011-11-23 09:13:48 +0000378 unsigned int offs;
379 static int unsigned aai_offs;
hailfinger6ead7222010-11-01 22:07:04 +0000380 static int aai_active = 0;
381
382 if (writecnt == 0) {
383 msg_perr("No command sent to the chip!\n");
384 return 1;
385 }
386 /* TODO: Implement command blacklists here. */
387 switch (writearr[0]) {
388 case JEDEC_RES:
389 if (emu_chip != EMULATE_ST_M25P10_RES)
390 break;
391 /* Respond with ST_M25P10_RES. */
392 if (readcnt > 0)
393 readarr[0] = 0x10;
394 break;
395 case JEDEC_REMS:
396 if (emu_chip != EMULATE_SST_SST25VF040_REMS)
397 break;
398 /* Respond with SST_SST25VF040_REMS. */
399 if (readcnt > 0)
400 readarr[0] = 0xbf;
401 if (readcnt > 1)
402 readarr[1] = 0x44;
403 break;
404 case JEDEC_RDID:
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800405 if (emu_chip == EMULATE_SST_SST25VF032B) {
406 /* Respond with SST_SST25VF032B. */
407 if (readcnt > 0)
408 readarr[0] = 0xbf;
409 if (readcnt > 1)
410 readarr[1] = 0x25;
411 if (readcnt > 2)
412 readarr[2] = 0x4a;
413 } else if (emu_chip == EMULATE_VARIABLE_SIZE) {
414 const uint16_t man_id = VARIABLE_SIZE_MANUF_ID;
415 const uint16_t dev_id = VARIABLE_SIZE_DEVICE_ID;
416 if (readcnt > 0) readarr[0] = man_id >> 8;
417 if (readcnt > 1) readarr[1] = man_id & 0xff;
418 if (readcnt > 2) readarr[2] = dev_id >> 8;
419 if (readcnt > 3) readarr[3] = dev_id & 0xff;
420 }
hailfinger6ead7222010-11-01 22:07:04 +0000421 break;
422 case JEDEC_RDSR:
423 memset(readarr, 0, readcnt);
424 if (aai_active)
425 memset(readarr, 1 << 6, readcnt);
426 break;
427 case JEDEC_READ:
428 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
429 /* Truncate to emu_chip_size. */
430 offs %= emu_chip_size;
431 if (readcnt > 0)
432 memcpy(readarr, flashchip_contents + offs, readcnt);
433 break;
434 case JEDEC_BYTE_PROGRAM:
435 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
436 /* Truncate to emu_chip_size. */
437 offs %= emu_chip_size;
438 if (writecnt < 5) {
439 msg_perr("BYTE PROGRAM size too short!\n");
440 return 1;
441 }
442 if (writecnt - 4 > emu_max_byteprogram_size) {
443 msg_perr("Max BYTE PROGRAM size exceeded!\n");
444 return 1;
445 }
446 memcpy(flashchip_contents + offs, writearr + 4, writecnt - 4);
447 break;
448 case JEDEC_AAI_WORD_PROGRAM:
449 if (!emu_max_aai_size)
450 break;
451 if (!aai_active) {
452 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
453 msg_perr("Initial AAI WORD PROGRAM size too "
454 "short!\n");
455 return 1;
456 }
457 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
458 msg_perr("Initial AAI WORD PROGRAM size too "
459 "long!\n");
460 return 1;
461 }
462 aai_active = 1;
463 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
464 writearr[3];
465 /* Truncate to emu_chip_size. */
466 aai_offs %= emu_chip_size;
467 memcpy(flashchip_contents + aai_offs, writearr + 4, 2);
468 aai_offs += 2;
469 } else {
470 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
471 msg_perr("Continuation AAI WORD PROGRAM size "
472 "too short!\n");
473 return 1;
474 }
475 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
476 msg_perr("Continuation AAI WORD PROGRAM size "
477 "too long!\n");
478 return 1;
479 }
480 memcpy(flashchip_contents + aai_offs, writearr + 1, 2);
481 aai_offs += 2;
482 }
483 break;
484 case JEDEC_WRDI:
485 if (!emu_max_aai_size)
486 break;
487 aai_active = 0;
488 break;
489 case JEDEC_SE:
490 if (!emu_jedec_se_size)
491 break;
492 if (writecnt != JEDEC_SE_OUTSIZE) {
493 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
494 return 1;
495 }
496 if (readcnt != JEDEC_SE_INSIZE) {
497 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
498 return 1;
499 }
500 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
501 if (offs & (emu_jedec_se_size - 1))
hailfingere53f5e42011-02-04 22:52:04 +0000502 msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
hailfinger6ead7222010-11-01 22:07:04 +0000503 offs &= ~(emu_jedec_se_size - 1);
504 memset(flashchip_contents + offs, 0xff, emu_jedec_se_size);
505 break;
506 case JEDEC_BE_52:
507 if (!emu_jedec_be_52_size)
508 break;
509 if (writecnt != JEDEC_BE_52_OUTSIZE) {
510 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
511 return 1;
512 }
513 if (readcnt != JEDEC_BE_52_INSIZE) {
514 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
515 return 1;
516 }
517 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
518 if (offs & (emu_jedec_be_52_size - 1))
hailfingere53f5e42011-02-04 22:52:04 +0000519 msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
hailfinger6ead7222010-11-01 22:07:04 +0000520 offs &= ~(emu_jedec_be_52_size - 1);
521 memset(flashchip_contents + offs, 0xff, emu_jedec_be_52_size);
522 break;
523 case JEDEC_BE_D8:
524 if (!emu_jedec_be_d8_size)
525 break;
526 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
527 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
528 return 1;
529 }
530 if (readcnt != JEDEC_BE_D8_INSIZE) {
531 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
532 return 1;
533 }
534 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
535 if (offs & (emu_jedec_be_d8_size - 1))
hailfingere53f5e42011-02-04 22:52:04 +0000536 msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
hailfinger6ead7222010-11-01 22:07:04 +0000537 offs &= ~(emu_jedec_be_d8_size - 1);
538 memset(flashchip_contents + offs, 0xff, emu_jedec_be_d8_size);
539 break;
540 case JEDEC_CE_60:
541 if (!emu_jedec_ce_60_size)
542 break;
543 if (writecnt != JEDEC_CE_60_OUTSIZE) {
544 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
545 return 1;
546 }
547 if (readcnt != JEDEC_CE_60_INSIZE) {
548 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
549 return 1;
550 }
hailfingere53f5e42011-02-04 22:52:04 +0000551 /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
hailfinger6ead7222010-11-01 22:07:04 +0000552 /* emu_jedec_ce_60_size is emu_chip_size. */
hailfingere53f5e42011-02-04 22:52:04 +0000553 memset(flashchip_contents, 0xff, emu_jedec_ce_60_size);
hailfinger6ead7222010-11-01 22:07:04 +0000554 break;
555 case JEDEC_CE_C7:
556 if (!emu_jedec_ce_c7_size)
557 break;
558 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
559 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
560 return 1;
561 }
562 if (readcnt != JEDEC_CE_C7_INSIZE) {
563 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
564 return 1;
565 }
hailfingere53f5e42011-02-04 22:52:04 +0000566 /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
hailfinger6ead7222010-11-01 22:07:04 +0000567 /* emu_jedec_ce_c7_size is emu_chip_size. */
568 memset(flashchip_contents, 0xff, emu_jedec_ce_c7_size);
569 break;
570 default:
571 /* No special response. */
572 break;
573 }
574 return 0;
575}
576#endif
577
mkarcherd264e9e2011-05-11 17:07:07 +0000578static int dummy_spi_send_command(unsigned int writecnt, unsigned int readcnt,
hailfingerf91e3b52009-05-14 12:59:36 +0000579 const unsigned char *writearr, unsigned char *readarr)
580{
581 int i;
582
hailfinger50c335f2010-01-09 04:32:23 +0000583 msg_pspew("%s:", __func__);
hailfingerf91e3b52009-05-14 12:59:36 +0000584
hailfinger50c335f2010-01-09 04:32:23 +0000585 msg_pspew(" writing %u bytes:", writecnt);
hailfingerf91e3b52009-05-14 12:59:36 +0000586 for (i = 0; i < writecnt; i++)
hailfinger50c335f2010-01-09 04:32:23 +0000587 msg_pspew(" 0x%02x", writearr[i]);
hailfingerf91e3b52009-05-14 12:59:36 +0000588
hailfinger6ead7222010-11-01 22:07:04 +0000589 /* Response for unknown commands and missing chip is 0xff. */
590 memset(readarr, 0xff, readcnt);
591#if EMULATE_SPI_CHIP
592 switch (emu_chip) {
593 case EMULATE_ST_M25P10_RES:
594 case EMULATE_SST_SST25VF040_REMS:
595 case EMULATE_SST_SST25VF032B:
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800596 case EMULATE_VARIABLE_SIZE:
hailfinger6ead7222010-11-01 22:07:04 +0000597 if (emulate_spi_chip_response(writecnt, readcnt, writearr,
598 readarr)) {
599 msg_perr("Invalid command sent to flash chip!\n");
600 return 1;
601 }
602 break;
603 default:
604 break;
605 }
606#endif
hailfinger50c335f2010-01-09 04:32:23 +0000607 msg_pspew(" reading %u bytes:", readcnt);
uwe8d342eb2011-07-28 08:13:25 +0000608 for (i = 0; i < readcnt; i++)
hailfinger6ead7222010-11-01 22:07:04 +0000609 msg_pspew(" 0x%02x", readarr[i]);
hailfinger50c335f2010-01-09 04:32:23 +0000610 msg_pspew("\n");
hailfingerf91e3b52009-05-14 12:59:36 +0000611 return 0;
612}
hailfingera8727712010-06-20 10:58:32 +0000613
uwe8d342eb2011-07-28 08:13:25 +0000614static int dummy_spi_write_256(struct flashchip *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +0000615 unsigned int start, unsigned int len)
hailfingerc7d06c62010-07-14 16:19:05 +0000616{
hailfinger6ead7222010-11-01 22:07:04 +0000617 return spi_write_chunked(flash, buf, start, len,
618 spi_write_256_chunksize);
hailfingerc7d06c62010-07-14 16:19:05 +0000619}
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800620
621#if EMULATE_CHIP && EMULATE_SPI_CHIP
622int probe_variable_size(struct flashchip *flash)
623{
624 int i;
625
626 /* Skip the probing if we don't emulate this chip. */
627 if (emu_chip != EMULATE_VARIABLE_SIZE)
628 return 0;
629
630 /*
631 * This will break if one day flashchip becomes read-only.
632 * Once that happens, we need to have special hacks in functions:
633 *
634 * erase_and_write_flash() in flashrom.c
635 * read_flash_to_file()
636 * handle_romentries()
637 * ...
638 *
639 * Search "total_size * 1024" in code.
640 */
641 if (emu_chip_size % 1024)
642 msg_perr("%s: emu_chip_size is not multipler of 1024.\n",
643 __func__);
644 flash->total_size = emu_chip_size / 1024;
645 msg_cdbg("%s: set flash->total_size to %dK bytes.\n", __func__,
646 flash->total_size);
647
648 /* Update eraser count */
649 for (i = 0; i < NUM_ERASEFUNCTIONS; i++) {
650 struct block_eraser *eraser = &flash->block_erasers[i];
651 if (eraser->block_erase == NULL)
652 break;
653
654 eraser->eraseblocks[0].count = emu_chip_size /
655 eraser->eraseblocks[0].size;
656 msg_cdbg("%s: eraser.size=%d, .count=%d\n",
657 __func__, eraser->eraseblocks[0].size,
658 eraser->eraseblocks[0].count);
659 }
660
661 return 1;
662}
663#endif