blob: 2cf3183c0f5de0a61636b0c20796cbd8d5168cd4 [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;
Simon Glasscf253a72013-07-10 21:10:11 -070059static int emu_modified; /* is the image modified since reading it? */
hailfinger6ead7222010-11-01 22:07:04 +000060#if EMULATE_SPI_CHIP
stefanctc5eb8a92011-11-23 09:13:48 +000061static unsigned int emu_max_byteprogram_size = 0;
62static unsigned int emu_max_aai_size = 0;
63static unsigned int emu_jedec_se_size = 0;
64static unsigned int emu_jedec_be_52_size = 0;
65static unsigned int emu_jedec_be_d8_size = 0;
66static unsigned int emu_jedec_ce_60_size = 0;
67static unsigned int emu_jedec_ce_c7_size = 0;
hailfinger6ead7222010-11-01 22:07:04 +000068#endif
69#endif
70
stefanctc5eb8a92011-11-23 09:13:48 +000071static unsigned int spi_write_256_chunksize = 256;
hailfinger6ead7222010-11-01 22:07:04 +000072
mkarcherd264e9e2011-05-11 17:07:07 +000073static int dummy_spi_send_command(unsigned int writecnt, unsigned int readcnt,
74 const unsigned char *writearr, unsigned char *readarr);
uwe8d342eb2011-07-28 08:13:25 +000075static int dummy_spi_write_256(struct flashchip *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +000076 unsigned int start, unsigned int len);
mkarcherd264e9e2011-05-11 17:07:07 +000077
78static const struct spi_programmer spi_programmer_dummyflasher = {
uwe8d342eb2011-07-28 08:13:25 +000079 .type = SPI_CONTROLLER_DUMMY,
80 .max_data_read = MAX_DATA_READ_UNLIMITED,
81 .max_data_write = MAX_DATA_UNSPECIFIED,
82 .command = dummy_spi_send_command,
83 .multicommand = default_spi_send_multicommand,
84 .read = default_spi_read,
85 .write_256 = dummy_spi_write_256,
mkarcherd264e9e2011-05-11 17:07:07 +000086};
dhendrix0ffc2eb2011-06-14 01:35:36 +000087
hailfinger76bb7e92011-11-09 23:40:00 +000088static const struct par_programmer par_programmer_dummy = {
89 .chip_readb = dummy_chip_readb,
90 .chip_readw = dummy_chip_readw,
91 .chip_readl = dummy_chip_readl,
92 .chip_readn = dummy_chip_readn,
93 .chip_writeb = dummy_chip_writeb,
94 .chip_writew = dummy_chip_writew,
95 .chip_writel = dummy_chip_writel,
96 .chip_writen = dummy_chip_writen,
97};
98
99enum chipbustype dummy_buses_supported = BUS_NONE;
100
dhendrix0ffc2eb2011-06-14 01:35:36 +0000101static int dummy_shutdown(void *data)
102{
103 msg_pspew("%s\n", __func__);
104#if EMULATE_CHIP
105 if (emu_chip != EMULATE_NONE) {
Simon Glasscf253a72013-07-10 21:10:11 -0700106 if (emu_persistent_image && emu_modified) {
dhendrix0ffc2eb2011-06-14 01:35:36 +0000107 msg_pdbg("Writing %s\n", emu_persistent_image);
108 write_buf_to_file(flashchip_contents, emu_chip_size,
109 emu_persistent_image);
110 }
111 free(flashchip_contents);
112 }
113#endif
114 return 0;
115}
116
Simon Glassd2c64a22013-07-03 22:05:21 +0900117/* Values for the 'size' parameter */
118enum {
119 SIZE_UNKNOWN = -1,
120 SIZE_AUTO = -2,
121};
122
hailfingera9df33c2009-05-09 00:54:55 +0000123int dummy_init(void)
124{
hailfinger1ef766d2010-07-06 09:55:48 +0000125 char *bustext = NULL;
hailfinger6ead7222010-11-01 22:07:04 +0000126 char *tmp = NULL;
127#if EMULATE_CHIP
128 struct stat image_stat;
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800129#if EMULATE_SPI_CHIP
Simon Glassd2c64a22013-07-03 22:05:21 +0900130 int size = SIZE_UNKNOWN; /* size for generic chip */
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800131#endif
hailfinger6ead7222010-11-01 22:07:04 +0000132#endif
Simon Glassd2c64a22013-07-03 22:05:21 +0900133 int image_size = SIZE_UNKNOWN;
hailfinger1ef766d2010-07-06 09:55:48 +0000134
hailfinger50c335f2010-01-09 04:32:23 +0000135 msg_pspew("%s\n", __func__);
hailfinger668f3502009-06-01 00:02:11 +0000136
hailfingerddeb4ac2010-07-08 10:13:37 +0000137 bustext = extract_programmer_param("bus");
hailfinger1ef766d2010-07-06 09:55:48 +0000138 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
139 if (!bustext)
140 bustext = strdup("parallel+lpc+fwh+spi");
hailfinger668f3502009-06-01 00:02:11 +0000141 /* Convert the parameters to lowercase. */
hailfinger1ef766d2010-07-06 09:55:48 +0000142 tolower_string(bustext);
hailfinger668f3502009-06-01 00:02:11 +0000143
hailfinger76bb7e92011-11-09 23:40:00 +0000144 dummy_buses_supported = BUS_NONE;
hailfinger1ef766d2010-07-06 09:55:48 +0000145 if (strstr(bustext, "parallel")) {
hailfinger76bb7e92011-11-09 23:40:00 +0000146 dummy_buses_supported |= BUS_PARALLEL;
hailfinger50c335f2010-01-09 04:32:23 +0000147 msg_pdbg("Enabling support for %s flash.\n", "parallel");
hailfinger668f3502009-06-01 00:02:11 +0000148 }
hailfinger1ef766d2010-07-06 09:55:48 +0000149 if (strstr(bustext, "lpc")) {
hailfinger76bb7e92011-11-09 23:40:00 +0000150 dummy_buses_supported |= BUS_LPC;
hailfinger50c335f2010-01-09 04:32:23 +0000151 msg_pdbg("Enabling support for %s flash.\n", "LPC");
hailfinger668f3502009-06-01 00:02:11 +0000152 }
hailfinger1ef766d2010-07-06 09:55:48 +0000153 if (strstr(bustext, "fwh")) {
hailfinger76bb7e92011-11-09 23:40:00 +0000154 dummy_buses_supported |= BUS_FWH;
hailfinger50c335f2010-01-09 04:32:23 +0000155 msg_pdbg("Enabling support for %s flash.\n", "FWH");
hailfinger668f3502009-06-01 00:02:11 +0000156 }
hailfinger1ef766d2010-07-06 09:55:48 +0000157 if (strstr(bustext, "spi")) {
hailfinger76bb7e92011-11-09 23:40:00 +0000158 dummy_buses_supported |= BUS_SPI;
hailfinger50c335f2010-01-09 04:32:23 +0000159 msg_pdbg("Enabling support for %s flash.\n", "SPI");
hailfinger668f3502009-06-01 00:02:11 +0000160 }
hailfinger76bb7e92011-11-09 23:40:00 +0000161 if (dummy_buses_supported == BUS_NONE)
hailfinger50c335f2010-01-09 04:32:23 +0000162 msg_pdbg("Support for all flash bus types disabled.\n");
hailfinger1ef766d2010-07-06 09:55:48 +0000163 free(bustext);
hailfinger6ead7222010-11-01 22:07:04 +0000164
165 tmp = extract_programmer_param("spi_write_256_chunksize");
166 if (tmp) {
167 spi_write_256_chunksize = atoi(tmp);
168 free(tmp);
169 if (spi_write_256_chunksize < 1) {
170 msg_perr("invalid spi_write_256_chunksize\n");
171 return 1;
172 }
173 }
174
175#if EMULATE_CHIP
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800176#if EMULATE_SPI_CHIP
177 tmp = extract_programmer_param("size");
178 if (tmp) {
179 int multiplier = 1;
Simon Glassd2c64a22013-07-03 22:05:21 +0900180 if (!strcmp(tmp, "auto"))
181 size = SIZE_AUTO;
182 else if (strlen(tmp)) {
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800183 int remove_last_char = 1;
184 switch (tmp[strlen(tmp) - 1]) {
185 case 'k': case 'K':
186 multiplier = 1024;
187 break;
188 case 'm': case 'M':
189 multiplier = 1024 * 1024;
190 break;
191 default:
192 remove_last_char = 0;
193 break;
194 }
195 if (remove_last_char) tmp[strlen(tmp) - 1] = '\0';
Simon Glassd2c64a22013-07-03 22:05:21 +0900196 size = atoi(tmp) * multiplier;
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800197 }
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800198 }
199#endif
200
hailfinger6ead7222010-11-01 22:07:04 +0000201 tmp = extract_programmer_param("emulate");
202 if (!tmp) {
203 msg_pdbg("Not emulating any flash chip.\n");
204 /* Nothing else to do. */
dhendrix0ffc2eb2011-06-14 01:35:36 +0000205 goto dummy_init_out;
hailfinger6ead7222010-11-01 22:07:04 +0000206 }
207#if EMULATE_SPI_CHIP
208 if (!strcmp(tmp, "M25P10.RES")) {
209 emu_chip = EMULATE_ST_M25P10_RES;
210 emu_chip_size = 128 * 1024;
211 emu_max_byteprogram_size = 128;
212 emu_max_aai_size = 0;
213 emu_jedec_se_size = 0;
214 emu_jedec_be_52_size = 0;
215 emu_jedec_be_d8_size = 32 * 1024;
216 emu_jedec_ce_60_size = 0;
217 emu_jedec_ce_c7_size = emu_chip_size;
218 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
219 "write)\n");
220 }
221 if (!strcmp(tmp, "SST25VF040.REMS")) {
222 emu_chip = EMULATE_SST_SST25VF040_REMS;
223 emu_chip_size = 512 * 1024;
224 emu_max_byteprogram_size = 1;
225 emu_max_aai_size = 0;
226 emu_jedec_se_size = 4 * 1024;
227 emu_jedec_be_52_size = 32 * 1024;
228 emu_jedec_be_d8_size = 0;
229 emu_jedec_ce_60_size = emu_chip_size;
230 emu_jedec_ce_c7_size = 0;
231 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
232 "byte write)\n");
233 }
234 if (!strcmp(tmp, "SST25VF032B")) {
235 emu_chip = EMULATE_SST_SST25VF032B;
236 emu_chip_size = 4 * 1024 * 1024;
237 emu_max_byteprogram_size = 1;
238 emu_max_aai_size = 2;
239 emu_jedec_se_size = 4 * 1024;
240 emu_jedec_be_52_size = 32 * 1024;
241 emu_jedec_be_d8_size = 64 * 1024;
242 emu_jedec_ce_60_size = emu_chip_size;
243 emu_jedec_ce_c7_size = emu_chip_size;
244 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
245 "write)\n");
246 }
Simon Glassd2c64a22013-07-03 22:05:21 +0900247 emu_persistent_image = extract_programmer_param("image");
248 if (!stat(emu_persistent_image, &image_stat))
249 image_size = image_stat.st_size;
250
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800251 if (!strncmp(tmp, VARIABLE_SIZE_CHIP_NAME,
252 strlen(VARIABLE_SIZE_CHIP_NAME))) {
Simon Glassd2c64a22013-07-03 22:05:21 +0900253 if (size == SIZE_UNKNOWN) {
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800254 msg_perr("%s: the size parameter is not given.\n",
255 __func__);
256 free(tmp);
257 return 1;
Simon Glassd2c64a22013-07-03 22:05:21 +0900258 } else if (size == SIZE_AUTO) {
259 if (image_size == SIZE_UNKNOWN) {
260 msg_perr("%s: no image so cannot use automatic size.\n",
261 __func__);
262 free(tmp);
263 return 1;
264 }
265 size = image_size;
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800266 }
267 emu_chip = EMULATE_VARIABLE_SIZE;
268 emu_chip_size = size;
269 emu_max_byteprogram_size = 256;
270 emu_max_aai_size = 0;
271 emu_jedec_se_size = 4 * 1024;
272 emu_jedec_be_52_size = 32 * 1024;
273 emu_jedec_be_d8_size = 64 * 1024;
274 emu_jedec_ce_60_size = emu_chip_size;
275 emu_jedec_ce_c7_size = emu_chip_size;
276 msg_pdbg("Emulating generic SPI flash chip (size=%d bytes)\n",
277 emu_chip_size);
278 }
hailfinger6ead7222010-11-01 22:07:04 +0000279#endif
280 if (emu_chip == EMULATE_NONE) {
281 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
282 free(tmp);
283 return 1;
284 }
285 free(tmp);
286 flashchip_contents = malloc(emu_chip_size);
287 if (!flashchip_contents) {
288 msg_perr("Out of memory!\n");
289 return 1;
290 }
dhendrix0ffc2eb2011-06-14 01:35:36 +0000291
hailfinger6ead7222010-11-01 22:07:04 +0000292 msg_pdbg("Filling fake flash chip with 0xff, size %i\n", emu_chip_size);
293 memset(flashchip_contents, 0xff, emu_chip_size);
294
hailfinger6ead7222010-11-01 22:07:04 +0000295 if (!emu_persistent_image) {
296 /* Nothing else to do. */
dhendrix0ffc2eb2011-06-14 01:35:36 +0000297 goto dummy_init_out;
hailfinger6ead7222010-11-01 22:07:04 +0000298 }
299 if (!stat(emu_persistent_image, &image_stat)) {
300 msg_pdbg("Found persistent image %s, size %li ",
301 emu_persistent_image, (long)image_stat.st_size);
302 if (image_stat.st_size == emu_chip_size) {
303 msg_pdbg("matches.\n");
304 msg_pdbg("Reading %s\n", emu_persistent_image);
305 read_buf_from_file(flashchip_contents, emu_chip_size,
306 emu_persistent_image);
307 } else {
308 msg_pdbg("doesn't match.\n");
309 }
310 }
311#endif
hailfingera9df33c2009-05-09 00:54:55 +0000312
dhendrix0ffc2eb2011-06-14 01:35:36 +0000313dummy_init_out:
314 if (register_shutdown(dummy_shutdown, NULL)) {
hailfinger6ead7222010-11-01 22:07:04 +0000315 free(flashchip_contents);
dhendrix0ffc2eb2011-06-14 01:35:36 +0000316 return 1;
hailfinger6ead7222010-11-01 22:07:04 +0000317 }
hailfinger76bb7e92011-11-09 23:40:00 +0000318 if (dummy_buses_supported & (BUS_PARALLEL | BUS_LPC | BUS_FWH))
319 register_par_programmer(&par_programmer_dummy,
320 dummy_buses_supported &
321 (BUS_PARALLEL | BUS_LPC |
322 BUS_FWH));
323 if (dummy_buses_supported & BUS_SPI)
324 register_spi_programmer(&spi_programmer_dummyflasher);
325
hailfingera9df33c2009-05-09 00:54:55 +0000326 return 0;
327}
328
hailfinger11ae3c42009-05-11 14:13:25 +0000329void *dummy_map(const char *descr, unsigned long phys_addr, size_t len)
330{
hailfinger50c335f2010-01-09 04:32:23 +0000331 msg_pspew("%s: Mapping %s, 0x%lx bytes at 0x%08lx\n",
332 __func__, descr, (unsigned long)len, phys_addr);
hailfinger11ae3c42009-05-11 14:13:25 +0000333 return (void *)phys_addr;
334}
335
336void dummy_unmap(void *virt_addr, size_t len)
337{
hailfinger50c335f2010-01-09 04:32:23 +0000338 msg_pspew("%s: Unmapping 0x%lx bytes at %p\n",
339 __func__, (unsigned long)len, virt_addr);
hailfinger11ae3c42009-05-11 14:13:25 +0000340}
341
hailfinger82719632009-05-16 21:22:56 +0000342void dummy_chip_writeb(uint8_t val, chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000343{
hailfinger50c335f2010-01-09 04:32:23 +0000344 msg_pspew("%s: addr=0x%lx, val=0x%02x\n", __func__, addr, val);
hailfingera9df33c2009-05-09 00:54:55 +0000345}
346
hailfinger82719632009-05-16 21:22:56 +0000347void dummy_chip_writew(uint16_t val, chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000348{
hailfinger50c335f2010-01-09 04:32:23 +0000349 msg_pspew("%s: addr=0x%lx, val=0x%04x\n", __func__, addr, val);
hailfingera9df33c2009-05-09 00:54:55 +0000350}
351
hailfinger82719632009-05-16 21:22:56 +0000352void dummy_chip_writel(uint32_t val, chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000353{
hailfinger50c335f2010-01-09 04:32:23 +0000354 msg_pspew("%s: addr=0x%lx, val=0x%08x\n", __func__, addr, val);
hailfingera9df33c2009-05-09 00:54:55 +0000355}
356
hailfinger9d987ef2009-06-05 18:32:07 +0000357void dummy_chip_writen(uint8_t *buf, chipaddr addr, size_t len)
358{
359 size_t i;
hailfinger50c335f2010-01-09 04:32:23 +0000360 msg_pspew("%s: addr=0x%lx, len=0x%08lx, writing data (hex):",
361 __func__, addr, (unsigned long)len);
hailfinger9d987ef2009-06-05 18:32:07 +0000362 for (i = 0; i < len; i++) {
363 if ((i % 16) == 0)
hailfinger50c335f2010-01-09 04:32:23 +0000364 msg_pspew("\n");
365 msg_pspew("%02x ", buf[i]);
hailfinger9d987ef2009-06-05 18:32:07 +0000366 }
367}
368
hailfinger82719632009-05-16 21:22:56 +0000369uint8_t dummy_chip_readb(const chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000370{
hailfinger50c335f2010-01-09 04:32:23 +0000371 msg_pspew("%s: addr=0x%lx, returning 0xff\n", __func__, addr);
hailfingera9df33c2009-05-09 00:54:55 +0000372 return 0xff;
373}
374
hailfinger82719632009-05-16 21:22:56 +0000375uint16_t dummy_chip_readw(const chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000376{
hailfinger50c335f2010-01-09 04:32:23 +0000377 msg_pspew("%s: addr=0x%lx, returning 0xffff\n", __func__, addr);
hailfingera9df33c2009-05-09 00:54:55 +0000378 return 0xffff;
379}
380
hailfinger82719632009-05-16 21:22:56 +0000381uint32_t dummy_chip_readl(const chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000382{
hailfinger50c335f2010-01-09 04:32:23 +0000383 msg_pspew("%s: addr=0x%lx, returning 0xffffffff\n", __func__, addr);
hailfingera9df33c2009-05-09 00:54:55 +0000384 return 0xffffffff;
385}
386
hailfinger9d987ef2009-06-05 18:32:07 +0000387void dummy_chip_readn(uint8_t *buf, const chipaddr addr, size_t len)
388{
hailfinger50c335f2010-01-09 04:32:23 +0000389 msg_pspew("%s: addr=0x%lx, len=0x%lx, returning array of 0xff\n",
390 __func__, addr, (unsigned long)len);
hailfinger9d987ef2009-06-05 18:32:07 +0000391 memset(buf, 0xff, len);
392 return;
393}
394
hailfinger6ead7222010-11-01 22:07:04 +0000395#if EMULATE_SPI_CHIP
396static int emulate_spi_chip_response(unsigned int writecnt, unsigned int readcnt,
397 const unsigned char *writearr, unsigned char *readarr)
398{
stefanctc5eb8a92011-11-23 09:13:48 +0000399 unsigned int offs;
400 static int unsigned aai_offs;
hailfinger6ead7222010-11-01 22:07:04 +0000401 static int aai_active = 0;
402
403 if (writecnt == 0) {
404 msg_perr("No command sent to the chip!\n");
405 return 1;
406 }
407 /* TODO: Implement command blacklists here. */
408 switch (writearr[0]) {
409 case JEDEC_RES:
410 if (emu_chip != EMULATE_ST_M25P10_RES)
411 break;
412 /* Respond with ST_M25P10_RES. */
413 if (readcnt > 0)
414 readarr[0] = 0x10;
415 break;
416 case JEDEC_REMS:
417 if (emu_chip != EMULATE_SST_SST25VF040_REMS)
418 break;
419 /* Respond with SST_SST25VF040_REMS. */
420 if (readcnt > 0)
421 readarr[0] = 0xbf;
422 if (readcnt > 1)
423 readarr[1] = 0x44;
424 break;
425 case JEDEC_RDID:
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800426 if (emu_chip == EMULATE_SST_SST25VF032B) {
427 /* Respond with SST_SST25VF032B. */
428 if (readcnt > 0)
429 readarr[0] = 0xbf;
430 if (readcnt > 1)
431 readarr[1] = 0x25;
432 if (readcnt > 2)
433 readarr[2] = 0x4a;
434 } else if (emu_chip == EMULATE_VARIABLE_SIZE) {
435 const uint16_t man_id = VARIABLE_SIZE_MANUF_ID;
436 const uint16_t dev_id = VARIABLE_SIZE_DEVICE_ID;
437 if (readcnt > 0) readarr[0] = man_id >> 8;
438 if (readcnt > 1) readarr[1] = man_id & 0xff;
439 if (readcnt > 2) readarr[2] = dev_id >> 8;
440 if (readcnt > 3) readarr[3] = dev_id & 0xff;
441 }
hailfinger6ead7222010-11-01 22:07:04 +0000442 break;
443 case JEDEC_RDSR:
444 memset(readarr, 0, readcnt);
445 if (aai_active)
446 memset(readarr, 1 << 6, readcnt);
447 break;
448 case JEDEC_READ:
449 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
450 /* Truncate to emu_chip_size. */
451 offs %= emu_chip_size;
452 if (readcnt > 0)
453 memcpy(readarr, flashchip_contents + offs, readcnt);
454 break;
455 case JEDEC_BYTE_PROGRAM:
456 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
457 /* Truncate to emu_chip_size. */
458 offs %= emu_chip_size;
459 if (writecnt < 5) {
460 msg_perr("BYTE PROGRAM size too short!\n");
461 return 1;
462 }
463 if (writecnt - 4 > emu_max_byteprogram_size) {
464 msg_perr("Max BYTE PROGRAM size exceeded!\n");
465 return 1;
466 }
467 memcpy(flashchip_contents + offs, writearr + 4, writecnt - 4);
Simon Glasscf253a72013-07-10 21:10:11 -0700468 emu_modified = 1;
hailfinger6ead7222010-11-01 22:07:04 +0000469 break;
470 case JEDEC_AAI_WORD_PROGRAM:
471 if (!emu_max_aai_size)
472 break;
473 if (!aai_active) {
474 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
475 msg_perr("Initial AAI WORD PROGRAM size too "
476 "short!\n");
477 return 1;
478 }
479 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
480 msg_perr("Initial AAI WORD PROGRAM size too "
481 "long!\n");
482 return 1;
483 }
484 aai_active = 1;
485 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
486 writearr[3];
487 /* Truncate to emu_chip_size. */
488 aai_offs %= emu_chip_size;
489 memcpy(flashchip_contents + aai_offs, writearr + 4, 2);
490 aai_offs += 2;
491 } else {
492 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
493 msg_perr("Continuation AAI WORD PROGRAM size "
494 "too short!\n");
495 return 1;
496 }
497 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
498 msg_perr("Continuation AAI WORD PROGRAM size "
499 "too long!\n");
500 return 1;
501 }
502 memcpy(flashchip_contents + aai_offs, writearr + 1, 2);
503 aai_offs += 2;
504 }
Simon Glasscf253a72013-07-10 21:10:11 -0700505 emu_modified = 1;
hailfinger6ead7222010-11-01 22:07:04 +0000506 break;
507 case JEDEC_WRDI:
508 if (!emu_max_aai_size)
509 break;
510 aai_active = 0;
511 break;
512 case JEDEC_SE:
513 if (!emu_jedec_se_size)
514 break;
515 if (writecnt != JEDEC_SE_OUTSIZE) {
516 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
517 return 1;
518 }
519 if (readcnt != JEDEC_SE_INSIZE) {
520 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
521 return 1;
522 }
523 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
524 if (offs & (emu_jedec_se_size - 1))
hailfingere53f5e42011-02-04 22:52:04 +0000525 msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
hailfinger6ead7222010-11-01 22:07:04 +0000526 offs &= ~(emu_jedec_se_size - 1);
527 memset(flashchip_contents + offs, 0xff, emu_jedec_se_size);
Simon Glasscf253a72013-07-10 21:10:11 -0700528 emu_modified = 1;
hailfinger6ead7222010-11-01 22:07:04 +0000529 break;
530 case JEDEC_BE_52:
531 if (!emu_jedec_be_52_size)
532 break;
533 if (writecnt != JEDEC_BE_52_OUTSIZE) {
534 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
535 return 1;
536 }
537 if (readcnt != JEDEC_BE_52_INSIZE) {
538 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
539 return 1;
540 }
541 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
542 if (offs & (emu_jedec_be_52_size - 1))
hailfingere53f5e42011-02-04 22:52:04 +0000543 msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
hailfinger6ead7222010-11-01 22:07:04 +0000544 offs &= ~(emu_jedec_be_52_size - 1);
545 memset(flashchip_contents + offs, 0xff, emu_jedec_be_52_size);
Simon Glasscf253a72013-07-10 21:10:11 -0700546 emu_modified = 1;
hailfinger6ead7222010-11-01 22:07:04 +0000547 break;
548 case JEDEC_BE_D8:
549 if (!emu_jedec_be_d8_size)
550 break;
551 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
552 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
553 return 1;
554 }
555 if (readcnt != JEDEC_BE_D8_INSIZE) {
556 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
557 return 1;
558 }
559 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
560 if (offs & (emu_jedec_be_d8_size - 1))
hailfingere53f5e42011-02-04 22:52:04 +0000561 msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
hailfinger6ead7222010-11-01 22:07:04 +0000562 offs &= ~(emu_jedec_be_d8_size - 1);
563 memset(flashchip_contents + offs, 0xff, emu_jedec_be_d8_size);
564 break;
565 case JEDEC_CE_60:
566 if (!emu_jedec_ce_60_size)
567 break;
568 if (writecnt != JEDEC_CE_60_OUTSIZE) {
569 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
570 return 1;
571 }
572 if (readcnt != JEDEC_CE_60_INSIZE) {
573 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
574 return 1;
575 }
hailfingere53f5e42011-02-04 22:52:04 +0000576 /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
hailfinger6ead7222010-11-01 22:07:04 +0000577 /* emu_jedec_ce_60_size is emu_chip_size. */
hailfingere53f5e42011-02-04 22:52:04 +0000578 memset(flashchip_contents, 0xff, emu_jedec_ce_60_size);
Simon Glasscf253a72013-07-10 21:10:11 -0700579 emu_modified = 1;
hailfinger6ead7222010-11-01 22:07:04 +0000580 break;
581 case JEDEC_CE_C7:
582 if (!emu_jedec_ce_c7_size)
583 break;
584 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
585 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
586 return 1;
587 }
588 if (readcnt != JEDEC_CE_C7_INSIZE) {
589 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
590 return 1;
591 }
hailfingere53f5e42011-02-04 22:52:04 +0000592 /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
hailfinger6ead7222010-11-01 22:07:04 +0000593 /* emu_jedec_ce_c7_size is emu_chip_size. */
594 memset(flashchip_contents, 0xff, emu_jedec_ce_c7_size);
Simon Glasscf253a72013-07-10 21:10:11 -0700595 emu_modified = 1;
hailfinger6ead7222010-11-01 22:07:04 +0000596 break;
597 default:
598 /* No special response. */
599 break;
600 }
601 return 0;
602}
603#endif
604
mkarcherd264e9e2011-05-11 17:07:07 +0000605static int dummy_spi_send_command(unsigned int writecnt, unsigned int readcnt,
hailfingerf91e3b52009-05-14 12:59:36 +0000606 const unsigned char *writearr, unsigned char *readarr)
607{
608 int i;
609
hailfinger50c335f2010-01-09 04:32:23 +0000610 msg_pspew("%s:", __func__);
hailfingerf91e3b52009-05-14 12:59:36 +0000611
hailfinger50c335f2010-01-09 04:32:23 +0000612 msg_pspew(" writing %u bytes:", writecnt);
hailfingerf91e3b52009-05-14 12:59:36 +0000613 for (i = 0; i < writecnt; i++)
hailfinger50c335f2010-01-09 04:32:23 +0000614 msg_pspew(" 0x%02x", writearr[i]);
hailfingerf91e3b52009-05-14 12:59:36 +0000615
hailfinger6ead7222010-11-01 22:07:04 +0000616 /* Response for unknown commands and missing chip is 0xff. */
617 memset(readarr, 0xff, readcnt);
618#if EMULATE_SPI_CHIP
619 switch (emu_chip) {
620 case EMULATE_ST_M25P10_RES:
621 case EMULATE_SST_SST25VF040_REMS:
622 case EMULATE_SST_SST25VF032B:
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800623 case EMULATE_VARIABLE_SIZE:
hailfinger6ead7222010-11-01 22:07:04 +0000624 if (emulate_spi_chip_response(writecnt, readcnt, writearr,
625 readarr)) {
626 msg_perr("Invalid command sent to flash chip!\n");
627 return 1;
628 }
629 break;
630 default:
631 break;
632 }
633#endif
hailfinger50c335f2010-01-09 04:32:23 +0000634 msg_pspew(" reading %u bytes:", readcnt);
uwe8d342eb2011-07-28 08:13:25 +0000635 for (i = 0; i < readcnt; i++)
hailfinger6ead7222010-11-01 22:07:04 +0000636 msg_pspew(" 0x%02x", readarr[i]);
hailfinger50c335f2010-01-09 04:32:23 +0000637 msg_pspew("\n");
hailfingerf91e3b52009-05-14 12:59:36 +0000638 return 0;
639}
hailfingera8727712010-06-20 10:58:32 +0000640
uwe8d342eb2011-07-28 08:13:25 +0000641static int dummy_spi_write_256(struct flashchip *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +0000642 unsigned int start, unsigned int len)
hailfingerc7d06c62010-07-14 16:19:05 +0000643{
hailfinger6ead7222010-11-01 22:07:04 +0000644 return spi_write_chunked(flash, buf, start, len,
645 spi_write_256_chunksize);
hailfingerc7d06c62010-07-14 16:19:05 +0000646}
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800647
648#if EMULATE_CHIP && EMULATE_SPI_CHIP
649int probe_variable_size(struct flashchip *flash)
650{
651 int i;
652
653 /* Skip the probing if we don't emulate this chip. */
654 if (emu_chip != EMULATE_VARIABLE_SIZE)
655 return 0;
656
657 /*
658 * This will break if one day flashchip becomes read-only.
659 * Once that happens, we need to have special hacks in functions:
660 *
661 * erase_and_write_flash() in flashrom.c
662 * read_flash_to_file()
663 * handle_romentries()
664 * ...
665 *
666 * Search "total_size * 1024" in code.
667 */
668 if (emu_chip_size % 1024)
669 msg_perr("%s: emu_chip_size is not multipler of 1024.\n",
670 __func__);
671 flash->total_size = emu_chip_size / 1024;
672 msg_cdbg("%s: set flash->total_size to %dK bytes.\n", __func__,
673 flash->total_size);
674
675 /* Update eraser count */
676 for (i = 0; i < NUM_ERASEFUNCTIONS; i++) {
677 struct block_eraser *eraser = &flash->block_erasers[i];
678 if (eraser->block_erase == NULL)
679 break;
680
681 eraser->eraseblocks[0].count = emu_chip_size /
682 eraser->eraseblocks[0].size;
683 msg_cdbg("%s: eraser.size=%d, .count=%d\n",
684 __func__, eraser->eraseblocks[0].size,
685 eraser->eraseblocks[0].count);
686 }
687
688 return 1;
689}
690#endif