blob: d5bcf075d808836d9abd996e5f1969e0d93887ce [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
David Hendricks84377002014-09-09 16:09:31 -070020#include <errno.h>
hailfingera9df33c2009-05-09 00:54:55 +000021#include <string.h>
22#include <stdlib.h>
Carl-Daniel Hailfingerc49783d2016-08-05 10:52:06 -070023#include <stdio.h>
24#include <ctype.h>
hailfingera9df33c2009-05-09 00:54:55 +000025#include "flash.h"
hailfingera8727712010-06-20 10:58:32 +000026#include "chipdrivers.h"
hailfinger428f6852010-07-27 22:41:39 +000027#include "programmer.h"
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +080028#include "flashchips.h"
hailfingera9df33c2009-05-09 00:54:55 +000029
hailfinger6ead7222010-11-01 22:07:04 +000030/* Remove the #define below if you don't want SPI flash chip emulation. */
31#define EMULATE_SPI_CHIP 1
32
33#if EMULATE_SPI_CHIP
34#define EMULATE_CHIP 1
35#include "spi.h"
36#endif
37
38#if EMULATE_CHIP
39#include <sys/types.h>
40#include <sys/stat.h>
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +080041
42#if EMULATE_SPI_CHIP
43/* The name of variable-size virtual chip. A 4MB flash example:
44 * flashrom -p dummy:emulate=VARIABLE_SIZE,size=4194304
45 */
46#define VARIABLE_SIZE_CHIP_NAME "VARIABLE_SIZE"
Carl-Daniel Hailfingerc49783d2016-08-05 10:52:06 -070047unsigned char spi_blacklist[256];
48unsigned char spi_ignorelist[256];
49int spi_blacklist_size = 0;
50int spi_ignorelist_size = 0;
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +080051#endif
hailfinger6ead7222010-11-01 22:07:04 +000052#endif
53
54#if EMULATE_CHIP
55static uint8_t *flashchip_contents = NULL;
56enum emu_chip {
57 EMULATE_NONE,
58 EMULATE_ST_M25P10_RES,
59 EMULATE_SST_SST25VF040_REMS,
60 EMULATE_SST_SST25VF032B,
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +080061 EMULATE_VARIABLE_SIZE,
hailfinger6ead7222010-11-01 22:07:04 +000062};
63static enum emu_chip emu_chip = EMULATE_NONE;
64static char *emu_persistent_image = NULL;
stefanctc5eb8a92011-11-23 09:13:48 +000065static unsigned int emu_chip_size = 0;
Simon Glasscf253a72013-07-10 21:10:11 -070066static int emu_modified; /* is the image modified since reading it? */
David Hendricks0eda2a82014-09-12 16:32:05 -070067static int erase_to_zero;
hailfinger6ead7222010-11-01 22:07:04 +000068#if EMULATE_SPI_CHIP
stefanctc5eb8a92011-11-23 09:13:48 +000069static unsigned int emu_max_byteprogram_size = 0;
70static unsigned int emu_max_aai_size = 0;
71static unsigned int emu_jedec_se_size = 0;
72static unsigned int emu_jedec_be_52_size = 0;
73static unsigned int emu_jedec_be_d8_size = 0;
74static unsigned int emu_jedec_ce_60_size = 0;
75static unsigned int emu_jedec_ce_c7_size = 0;
hailfinger6ead7222010-11-01 22:07:04 +000076#endif
77#endif
78
stefanctc5eb8a92011-11-23 09:13:48 +000079static unsigned int spi_write_256_chunksize = 256;
hailfinger6ead7222010-11-01 22:07:04 +000080
David Hendricks84377002014-09-09 16:09:31 -070081/* If "freq" parameter is passed in from command line, commands will delay
82 * for this period before returning. */
83static unsigned long int delay_us = 0;
84
Souvik Ghoshd75cd672016-06-17 14:21:39 -070085static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
mkarcherd264e9e2011-05-11 17:07:07 +000086 const unsigned char *writearr, unsigned char *readarr);
Souvik Ghoshd75cd672016-06-17 14:21:39 -070087static int dummy_spi_write_256(struct flashctx *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +000088 unsigned int start, unsigned int len);
Souvik Ghoshd75cd672016-06-17 14:21:39 -070089static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val,
90 chipaddr addr);
91static void dummy_chip_writew(const struct flashctx *flash, uint16_t val,
92 chipaddr addr);
93static void dummy_chip_writel(const struct flashctx *flash, uint32_t val,
94 chipaddr addr);
95static void dummy_chip_writen(const struct flashctx *flash, uint8_t *buf,
96 chipaddr addr, size_t len);
97static uint8_t dummy_chip_readb(const struct flashctx *flash,
98 const chipaddr addr);
99static uint16_t dummy_chip_readw(const struct flashctx *flash,
100 const chipaddr addr);
101static uint32_t dummy_chip_readl(const struct flashctx *flash,
102 const chipaddr addr);
103static void dummy_chip_readn(const struct flashctx *flash, uint8_t *buf,
104 const chipaddr addr, size_t len);
mkarcherd264e9e2011-05-11 17:07:07 +0000105
106static const struct spi_programmer spi_programmer_dummyflasher = {
uwe8d342eb2011-07-28 08:13:25 +0000107 .type = SPI_CONTROLLER_DUMMY,
108 .max_data_read = MAX_DATA_READ_UNLIMITED,
109 .max_data_write = MAX_DATA_UNSPECIFIED,
110 .command = dummy_spi_send_command,
111 .multicommand = default_spi_send_multicommand,
112 .read = default_spi_read,
113 .write_256 = dummy_spi_write_256,
mkarcherd264e9e2011-05-11 17:07:07 +0000114};
dhendrix0ffc2eb2011-06-14 01:35:36 +0000115
hailfinger76bb7e92011-11-09 23:40:00 +0000116static const struct par_programmer par_programmer_dummy = {
117 .chip_readb = dummy_chip_readb,
118 .chip_readw = dummy_chip_readw,
119 .chip_readl = dummy_chip_readl,
120 .chip_readn = dummy_chip_readn,
121 .chip_writeb = dummy_chip_writeb,
122 .chip_writew = dummy_chip_writew,
123 .chip_writel = dummy_chip_writel,
124 .chip_writen = dummy_chip_writen,
125};
126
127enum chipbustype dummy_buses_supported = BUS_NONE;
128
David Hendricks93784b42016-08-09 17:00:38 -0700129static int dummy_shutdown(void *data)
dhendrix0ffc2eb2011-06-14 01:35:36 +0000130{
131 msg_pspew("%s\n", __func__);
132#if EMULATE_CHIP
133 if (emu_chip != EMULATE_NONE) {
Simon Glasscf253a72013-07-10 21:10:11 -0700134 if (emu_persistent_image && emu_modified) {
dhendrix0ffc2eb2011-06-14 01:35:36 +0000135 msg_pdbg("Writing %s\n", emu_persistent_image);
136 write_buf_to_file(flashchip_contents, emu_chip_size,
137 emu_persistent_image);
138 }
139 free(flashchip_contents);
140 }
141#endif
142 return 0;
143}
144
Simon Glassd2c64a22013-07-03 22:05:21 +0900145/* Values for the 'size' parameter */
146enum {
147 SIZE_UNKNOWN = -1,
148 SIZE_AUTO = -2,
149};
150
David Hendricksac1d25c2016-08-09 17:00:58 -0700151int dummy_init(void)
hailfingera9df33c2009-05-09 00:54:55 +0000152{
hailfinger1ef766d2010-07-06 09:55:48 +0000153 char *bustext = NULL;
hailfinger6ead7222010-11-01 22:07:04 +0000154 char *tmp = NULL;
Carl-Daniel Hailfingerc49783d2016-08-05 10:52:06 -0700155 int i;
hailfinger6ead7222010-11-01 22:07:04 +0000156#if EMULATE_CHIP
157 struct stat image_stat;
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800158#if EMULATE_SPI_CHIP
Simon Glassd2c64a22013-07-03 22:05:21 +0900159 int size = SIZE_UNKNOWN; /* size for generic chip */
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800160#endif
hailfinger6ead7222010-11-01 22:07:04 +0000161#endif
Simon Glassd2c64a22013-07-03 22:05:21 +0900162 int image_size = SIZE_UNKNOWN;
hailfinger1ef766d2010-07-06 09:55:48 +0000163
hailfinger50c335f2010-01-09 04:32:23 +0000164 msg_pspew("%s\n", __func__);
hailfinger668f3502009-06-01 00:02:11 +0000165
hailfingerddeb4ac2010-07-08 10:13:37 +0000166 bustext = extract_programmer_param("bus");
hailfinger1ef766d2010-07-06 09:55:48 +0000167 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
168 if (!bustext)
169 bustext = strdup("parallel+lpc+fwh+spi");
hailfinger668f3502009-06-01 00:02:11 +0000170 /* Convert the parameters to lowercase. */
hailfinger1ef766d2010-07-06 09:55:48 +0000171 tolower_string(bustext);
hailfinger668f3502009-06-01 00:02:11 +0000172
hailfinger76bb7e92011-11-09 23:40:00 +0000173 dummy_buses_supported = BUS_NONE;
hailfinger1ef766d2010-07-06 09:55:48 +0000174 if (strstr(bustext, "parallel")) {
hailfinger76bb7e92011-11-09 23:40:00 +0000175 dummy_buses_supported |= BUS_PARALLEL;
hailfinger50c335f2010-01-09 04:32:23 +0000176 msg_pdbg("Enabling support for %s flash.\n", "parallel");
hailfinger668f3502009-06-01 00:02:11 +0000177 }
hailfinger1ef766d2010-07-06 09:55:48 +0000178 if (strstr(bustext, "lpc")) {
hailfinger76bb7e92011-11-09 23:40:00 +0000179 dummy_buses_supported |= BUS_LPC;
hailfinger50c335f2010-01-09 04:32:23 +0000180 msg_pdbg("Enabling support for %s flash.\n", "LPC");
hailfinger668f3502009-06-01 00:02:11 +0000181 }
hailfinger1ef766d2010-07-06 09:55:48 +0000182 if (strstr(bustext, "fwh")) {
hailfinger76bb7e92011-11-09 23:40:00 +0000183 dummy_buses_supported |= BUS_FWH;
hailfinger50c335f2010-01-09 04:32:23 +0000184 msg_pdbg("Enabling support for %s flash.\n", "FWH");
hailfinger668f3502009-06-01 00:02:11 +0000185 }
hailfinger1ef766d2010-07-06 09:55:48 +0000186 if (strstr(bustext, "spi")) {
hailfinger76bb7e92011-11-09 23:40:00 +0000187 dummy_buses_supported |= BUS_SPI;
hailfinger50c335f2010-01-09 04:32:23 +0000188 msg_pdbg("Enabling support for %s flash.\n", "SPI");
hailfinger668f3502009-06-01 00:02:11 +0000189 }
hailfinger76bb7e92011-11-09 23:40:00 +0000190 if (dummy_buses_supported == BUS_NONE)
hailfinger50c335f2010-01-09 04:32:23 +0000191 msg_pdbg("Support for all flash bus types disabled.\n");
hailfinger1ef766d2010-07-06 09:55:48 +0000192 free(bustext);
hailfinger6ead7222010-11-01 22:07:04 +0000193
194 tmp = extract_programmer_param("spi_write_256_chunksize");
195 if (tmp) {
196 spi_write_256_chunksize = atoi(tmp);
197 free(tmp);
198 if (spi_write_256_chunksize < 1) {
199 msg_perr("invalid spi_write_256_chunksize\n");
200 return 1;
201 }
202 }
203
Carl-Daniel Hailfingerc49783d2016-08-05 10:52:06 -0700204 tmp = extract_programmer_param("spi_blacklist");
205 if (tmp) {
206 i = strlen(tmp);
207 if (!strncmp(tmp, "0x", 2)) {
208 i -= 2;
209 memmove(tmp, tmp + 2, i + 1);
210 }
211 if ((i > 512) || (i % 2)) {
212 msg_perr("Invalid SPI command blacklist length\n");
213 free(tmp);
214 return 1;
215 }
216 spi_blacklist_size = i / 2;
217 for (i = 0; i < spi_blacklist_size * 2; i++) {
218 if (!isxdigit((unsigned char)tmp[i])) {
219 msg_perr("Invalid char \"%c\" in SPI command "
220 "blacklist\n", tmp[i]);
221 free(tmp);
222 return 1;
223 }
224 }
225 for (i = 0; i < spi_blacklist_size; i++) {
226 unsigned int tmp2;
227 /* SCNx8 is apparently not supported by MSVC (and thus
228 * MinGW), so work around it with an extra variable
229 */
230 sscanf(tmp + i * 2, "%2x", &tmp2);
231 spi_blacklist[i] = (uint8_t)tmp2;
232 }
233 msg_pdbg("SPI blacklist is ");
234 for (i = 0; i < spi_blacklist_size; i++)
235 msg_pdbg("%02x ", spi_blacklist[i]);
236 msg_pdbg(", size %i\n", spi_blacklist_size);
237 }
238 free(tmp);
239
240 tmp = extract_programmer_param("spi_ignorelist");
241 if (tmp) {
242 i = strlen(tmp);
243 if (!strncmp(tmp, "0x", 2)) {
244 i -= 2;
245 memmove(tmp, tmp + 2, i + 1);
246 }
247 if ((i > 512) || (i % 2)) {
248 msg_perr("Invalid SPI command ignorelist length\n");
249 free(tmp);
250 return 1;
251 }
252 spi_ignorelist_size = i / 2;
253 for (i = 0; i < spi_ignorelist_size * 2; i++) {
254 if (!isxdigit((unsigned char)tmp[i])) {
255 msg_perr("Invalid char \"%c\" in SPI command "
256 "ignorelist\n", tmp[i]);
257 free(tmp);
258 return 1;
259 }
260 }
261 for (i = 0; i < spi_ignorelist_size; i++) {
262 unsigned int tmp2;
263 /* SCNx8 is apparently not supported by MSVC (and thus
264 * MinGW), so work around it with an extra variable
265 */
266 sscanf(tmp + i * 2, "%2x", &tmp2);
267 spi_ignorelist[i] = (uint8_t)tmp2;
268 }
269 msg_pdbg("SPI ignorelist is ");
270 for (i = 0; i < spi_ignorelist_size; i++)
271 msg_pdbg("%02x ", spi_ignorelist[i]);
272 msg_pdbg(", size %i\n", spi_ignorelist_size);
273 }
274 free(tmp);
275
David Hendricks84377002014-09-09 16:09:31 -0700276 /* frequency to emulate in Hz (default), KHz, or MHz */
277 tmp = extract_programmer_param("freq");
278 if (tmp) {
279 unsigned long int freq;
280 char *units = tmp;
281 char *end = tmp + strlen(tmp);
282
283 errno = 0;
284 freq = strtoul(tmp, &units, 0);
285 if (errno) {
286 msg_perr("Invalid frequency \"%s\", %s\n",
287 tmp, strerror(errno));
288 goto dummy_init_out;
289 }
290
291 if ((units > tmp) && (units < end)) {
292 int units_valid = 0;
293
294 if (units < end - 3) {
295 ;
296 } else if (units == end - 2) {
297 if (!strcasecmp(units, "hz"))
298 units_valid = 1;
299 } else if (units == end - 3) {
300 if (!strcasecmp(units, "khz")) {
301 freq *= 1000;
302 units_valid = 1;
303 } else if (!strcasecmp(units, "mhz")) {
304 freq *= 1000000;
305 units_valid = 1;
306 }
307 }
308
309 if (!units_valid) {
310 msg_perr("Invalid units: %s\n", units);
311 return 1;
312 }
313 }
314
315 /* Assume we only work with bytes and transfer at 1 bit/Hz */
316 delay_us = (1000000 * 8) / freq;
317 }
318
hailfinger6ead7222010-11-01 22:07:04 +0000319#if EMULATE_CHIP
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800320#if EMULATE_SPI_CHIP
321 tmp = extract_programmer_param("size");
322 if (tmp) {
323 int multiplier = 1;
Simon Glassd2c64a22013-07-03 22:05:21 +0900324 if (!strcmp(tmp, "auto"))
325 size = SIZE_AUTO;
326 else if (strlen(tmp)) {
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800327 int remove_last_char = 1;
328 switch (tmp[strlen(tmp) - 1]) {
329 case 'k': case 'K':
330 multiplier = 1024;
331 break;
332 case 'm': case 'M':
333 multiplier = 1024 * 1024;
334 break;
335 default:
336 remove_last_char = 0;
337 break;
338 }
339 if (remove_last_char) tmp[strlen(tmp) - 1] = '\0';
Simon Glassd2c64a22013-07-03 22:05:21 +0900340 size = atoi(tmp) * multiplier;
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800341 }
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800342 }
343#endif
344
hailfinger6ead7222010-11-01 22:07:04 +0000345 tmp = extract_programmer_param("emulate");
346 if (!tmp) {
347 msg_pdbg("Not emulating any flash chip.\n");
348 /* Nothing else to do. */
dhendrix0ffc2eb2011-06-14 01:35:36 +0000349 goto dummy_init_out;
hailfinger6ead7222010-11-01 22:07:04 +0000350 }
David Hendricks84377002014-09-09 16:09:31 -0700351
hailfinger6ead7222010-11-01 22:07:04 +0000352#if EMULATE_SPI_CHIP
353 if (!strcmp(tmp, "M25P10.RES")) {
354 emu_chip = EMULATE_ST_M25P10_RES;
355 emu_chip_size = 128 * 1024;
356 emu_max_byteprogram_size = 128;
357 emu_max_aai_size = 0;
358 emu_jedec_se_size = 0;
359 emu_jedec_be_52_size = 0;
360 emu_jedec_be_d8_size = 32 * 1024;
361 emu_jedec_ce_60_size = 0;
362 emu_jedec_ce_c7_size = emu_chip_size;
363 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
364 "write)\n");
365 }
366 if (!strcmp(tmp, "SST25VF040.REMS")) {
367 emu_chip = EMULATE_SST_SST25VF040_REMS;
368 emu_chip_size = 512 * 1024;
369 emu_max_byteprogram_size = 1;
370 emu_max_aai_size = 0;
371 emu_jedec_se_size = 4 * 1024;
372 emu_jedec_be_52_size = 32 * 1024;
373 emu_jedec_be_d8_size = 0;
374 emu_jedec_ce_60_size = emu_chip_size;
375 emu_jedec_ce_c7_size = 0;
376 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
377 "byte write)\n");
378 }
379 if (!strcmp(tmp, "SST25VF032B")) {
380 emu_chip = EMULATE_SST_SST25VF032B;
381 emu_chip_size = 4 * 1024 * 1024;
382 emu_max_byteprogram_size = 1;
383 emu_max_aai_size = 2;
384 emu_jedec_se_size = 4 * 1024;
385 emu_jedec_be_52_size = 32 * 1024;
386 emu_jedec_be_d8_size = 64 * 1024;
387 emu_jedec_ce_60_size = emu_chip_size;
388 emu_jedec_ce_c7_size = emu_chip_size;
389 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
390 "write)\n");
391 }
Simon Glassd2c64a22013-07-03 22:05:21 +0900392 emu_persistent_image = extract_programmer_param("image");
393 if (!stat(emu_persistent_image, &image_stat))
394 image_size = image_stat.st_size;
395
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800396 if (!strncmp(tmp, VARIABLE_SIZE_CHIP_NAME,
397 strlen(VARIABLE_SIZE_CHIP_NAME))) {
Simon Glassd2c64a22013-07-03 22:05:21 +0900398 if (size == SIZE_UNKNOWN) {
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800399 msg_perr("%s: the size parameter is not given.\n",
400 __func__);
401 free(tmp);
402 return 1;
Simon Glassd2c64a22013-07-03 22:05:21 +0900403 } else if (size == SIZE_AUTO) {
404 if (image_size == SIZE_UNKNOWN) {
405 msg_perr("%s: no image so cannot use automatic size.\n",
406 __func__);
407 free(tmp);
408 return 1;
409 }
410 size = image_size;
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800411 }
412 emu_chip = EMULATE_VARIABLE_SIZE;
413 emu_chip_size = size;
414 emu_max_byteprogram_size = 256;
415 emu_max_aai_size = 0;
416 emu_jedec_se_size = 4 * 1024;
417 emu_jedec_be_52_size = 32 * 1024;
418 emu_jedec_be_d8_size = 64 * 1024;
419 emu_jedec_ce_60_size = emu_chip_size;
420 emu_jedec_ce_c7_size = emu_chip_size;
421 msg_pdbg("Emulating generic SPI flash chip (size=%d bytes)\n",
422 emu_chip_size);
423 }
hailfinger6ead7222010-11-01 22:07:04 +0000424#endif
425 if (emu_chip == EMULATE_NONE) {
426 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
427 free(tmp);
428 return 1;
429 }
David Hendricks0eda2a82014-09-12 16:32:05 -0700430
431 /* Should emulated flash erase to zero (yes/no)? */
432 tmp = extract_programmer_param("erase_to_zero");
433 if (tmp) {
434 if (!strcmp(tmp, "yes")) {
435 msg_pdbg("Emulated chip will erase to 0x00\n");
436 erase_to_zero = 1;
437 } else if (!strcmp(tmp, "no")) {
438 msg_pdbg("Emulated chip will erase to 0xff\n");
439 } else {
440 msg_perr("erase_to_zero can be \"yes\" or \"no\"\n");
441 return 1;
442 }
443 }
444
hailfinger6ead7222010-11-01 22:07:04 +0000445 free(tmp);
446 flashchip_contents = malloc(emu_chip_size);
447 if (!flashchip_contents) {
448 msg_perr("Out of memory!\n");
449 return 1;
450 }
dhendrix0ffc2eb2011-06-14 01:35:36 +0000451
David Hendricks0eda2a82014-09-12 16:32:05 -0700452 msg_pdbg("Filling fake flash chip with 0x%02x, size %i\n",
453 erase_to_zero ? 0x00 : 0xff, emu_chip_size);
454 memset(flashchip_contents, erase_to_zero ? 0x00 : 0xff, emu_chip_size);
hailfinger6ead7222010-11-01 22:07:04 +0000455
hailfinger6ead7222010-11-01 22:07:04 +0000456 if (!emu_persistent_image) {
457 /* Nothing else to do. */
dhendrix0ffc2eb2011-06-14 01:35:36 +0000458 goto dummy_init_out;
hailfinger6ead7222010-11-01 22:07:04 +0000459 }
460 if (!stat(emu_persistent_image, &image_stat)) {
461 msg_pdbg("Found persistent image %s, size %li ",
462 emu_persistent_image, (long)image_stat.st_size);
463 if (image_stat.st_size == emu_chip_size) {
464 msg_pdbg("matches.\n");
465 msg_pdbg("Reading %s\n", emu_persistent_image);
466 read_buf_from_file(flashchip_contents, emu_chip_size,
467 emu_persistent_image);
468 } else {
469 msg_pdbg("doesn't match.\n");
470 }
471 }
472#endif
hailfingera9df33c2009-05-09 00:54:55 +0000473
dhendrix0ffc2eb2011-06-14 01:35:36 +0000474dummy_init_out:
475 if (register_shutdown(dummy_shutdown, NULL)) {
hailfinger6ead7222010-11-01 22:07:04 +0000476 free(flashchip_contents);
dhendrix0ffc2eb2011-06-14 01:35:36 +0000477 return 1;
hailfinger6ead7222010-11-01 22:07:04 +0000478 }
hailfinger76bb7e92011-11-09 23:40:00 +0000479 if (dummy_buses_supported & (BUS_PARALLEL | BUS_LPC | BUS_FWH))
480 register_par_programmer(&par_programmer_dummy,
481 dummy_buses_supported &
482 (BUS_PARALLEL | BUS_LPC |
483 BUS_FWH));
484 if (dummy_buses_supported & BUS_SPI)
485 register_spi_programmer(&spi_programmer_dummyflasher);
486
hailfingera9df33c2009-05-09 00:54:55 +0000487 return 0;
488}
489
hailfinger11ae3c42009-05-11 14:13:25 +0000490void *dummy_map(const char *descr, unsigned long phys_addr, size_t len)
491{
hailfinger50c335f2010-01-09 04:32:23 +0000492 msg_pspew("%s: Mapping %s, 0x%lx bytes at 0x%08lx\n",
493 __func__, descr, (unsigned long)len, phys_addr);
hailfinger11ae3c42009-05-11 14:13:25 +0000494 return (void *)phys_addr;
495}
496
497void dummy_unmap(void *virt_addr, size_t len)
498{
hailfinger50c335f2010-01-09 04:32:23 +0000499 msg_pspew("%s: Unmapping 0x%lx bytes at %p\n",
500 __func__, (unsigned long)len, virt_addr);
hailfinger11ae3c42009-05-11 14:13:25 +0000501}
502
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700503void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000504{
hailfinger50c335f2010-01-09 04:32:23 +0000505 msg_pspew("%s: addr=0x%lx, val=0x%02x\n", __func__, addr, val);
hailfingera9df33c2009-05-09 00:54:55 +0000506}
507
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700508void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000509{
hailfinger50c335f2010-01-09 04:32:23 +0000510 msg_pspew("%s: addr=0x%lx, val=0x%04x\n", __func__, addr, val);
hailfingera9df33c2009-05-09 00:54:55 +0000511}
512
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700513void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000514{
hailfinger50c335f2010-01-09 04:32:23 +0000515 msg_pspew("%s: addr=0x%lx, val=0x%08x\n", __func__, addr, val);
hailfingera9df33c2009-05-09 00:54:55 +0000516}
517
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700518void dummy_chip_writen(const struct flashctx *flash, uint8_t *buf, chipaddr addr, size_t len)
hailfinger9d987ef2009-06-05 18:32:07 +0000519{
520 size_t i;
hailfinger50c335f2010-01-09 04:32:23 +0000521 msg_pspew("%s: addr=0x%lx, len=0x%08lx, writing data (hex):",
522 __func__, addr, (unsigned long)len);
hailfinger9d987ef2009-06-05 18:32:07 +0000523 for (i = 0; i < len; i++) {
524 if ((i % 16) == 0)
hailfinger50c335f2010-01-09 04:32:23 +0000525 msg_pspew("\n");
526 msg_pspew("%02x ", buf[i]);
hailfinger9d987ef2009-06-05 18:32:07 +0000527 }
528}
529
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700530uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000531{
hailfinger50c335f2010-01-09 04:32:23 +0000532 msg_pspew("%s: addr=0x%lx, returning 0xff\n", __func__, addr);
hailfingera9df33c2009-05-09 00:54:55 +0000533 return 0xff;
534}
535
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700536uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000537{
hailfinger50c335f2010-01-09 04:32:23 +0000538 msg_pspew("%s: addr=0x%lx, returning 0xffff\n", __func__, addr);
hailfingera9df33c2009-05-09 00:54:55 +0000539 return 0xffff;
540}
541
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700542uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000543{
hailfinger50c335f2010-01-09 04:32:23 +0000544 msg_pspew("%s: addr=0x%lx, returning 0xffffffff\n", __func__, addr);
hailfingera9df33c2009-05-09 00:54:55 +0000545 return 0xffffffff;
546}
547
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700548void dummy_chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len)
hailfinger9d987ef2009-06-05 18:32:07 +0000549{
hailfinger50c335f2010-01-09 04:32:23 +0000550 msg_pspew("%s: addr=0x%lx, len=0x%lx, returning array of 0xff\n",
551 __func__, addr, (unsigned long)len);
hailfinger9d987ef2009-06-05 18:32:07 +0000552 memset(buf, 0xff, len);
553 return;
554}
555
hailfinger6ead7222010-11-01 22:07:04 +0000556#if EMULATE_SPI_CHIP
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700557static int emulate_spi_chip_response(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
hailfinger6ead7222010-11-01 22:07:04 +0000558 const unsigned char *writearr, unsigned char *readarr)
559{
Carl-Daniel Hailfingerc49783d2016-08-05 10:52:06 -0700560 unsigned int offs, i;
stefanctc5eb8a92011-11-23 09:13:48 +0000561 static int unsigned aai_offs;
hailfinger6ead7222010-11-01 22:07:04 +0000562 static int aai_active = 0;
563
564 if (writecnt == 0) {
565 msg_perr("No command sent to the chip!\n");
566 return 1;
567 }
Carl-Daniel Hailfingerc49783d2016-08-05 10:52:06 -0700568 /* spi_blacklist has precedence before spi_ignorelist. */
569 for (i = 0; i < spi_blacklist_size; i++) {
570 if (writearr[0] == spi_blacklist[i]) {
571 msg_pdbg("Refusing blacklisted SPI command 0x%02x\n",
572 spi_blacklist[i]);
573 return SPI_INVALID_OPCODE;
574 }
575 }
576 for (i = 0; i < spi_ignorelist_size; i++) {
577 if (writearr[0] == spi_ignorelist[i]) {
578 msg_cdbg("Ignoring ignorelisted SPI command 0x%02x\n",
579 spi_ignorelist[i]);
580 /* Return success because the command does not fail,
581 * it is simply ignored.
582 */
583 return 0;
584 }
585 }
hailfinger6ead7222010-11-01 22:07:04 +0000586 switch (writearr[0]) {
587 case JEDEC_RES:
588 if (emu_chip != EMULATE_ST_M25P10_RES)
589 break;
590 /* Respond with ST_M25P10_RES. */
591 if (readcnt > 0)
592 readarr[0] = 0x10;
593 break;
594 case JEDEC_REMS:
595 if (emu_chip != EMULATE_SST_SST25VF040_REMS)
596 break;
597 /* Respond with SST_SST25VF040_REMS. */
598 if (readcnt > 0)
599 readarr[0] = 0xbf;
600 if (readcnt > 1)
601 readarr[1] = 0x44;
602 break;
603 case JEDEC_RDID:
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800604 if (emu_chip == EMULATE_SST_SST25VF032B) {
605 /* Respond with SST_SST25VF032B. */
606 if (readcnt > 0)
607 readarr[0] = 0xbf;
608 if (readcnt > 1)
609 readarr[1] = 0x25;
610 if (readcnt > 2)
611 readarr[2] = 0x4a;
612 } else if (emu_chip == EMULATE_VARIABLE_SIZE) {
613 const uint16_t man_id = VARIABLE_SIZE_MANUF_ID;
614 const uint16_t dev_id = VARIABLE_SIZE_DEVICE_ID;
615 if (readcnt > 0) readarr[0] = man_id >> 8;
616 if (readcnt > 1) readarr[1] = man_id & 0xff;
617 if (readcnt > 2) readarr[2] = dev_id >> 8;
618 if (readcnt > 3) readarr[3] = dev_id & 0xff;
619 }
hailfinger6ead7222010-11-01 22:07:04 +0000620 break;
621 case JEDEC_RDSR:
622 memset(readarr, 0, readcnt);
623 if (aai_active)
624 memset(readarr, 1 << 6, readcnt);
625 break;
626 case JEDEC_READ:
627 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
628 /* Truncate to emu_chip_size. */
629 offs %= emu_chip_size;
630 if (readcnt > 0)
631 memcpy(readarr, flashchip_contents + offs, readcnt);
632 break;
633 case JEDEC_BYTE_PROGRAM:
634 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
635 /* Truncate to emu_chip_size. */
636 offs %= emu_chip_size;
637 if (writecnt < 5) {
638 msg_perr("BYTE PROGRAM size too short!\n");
639 return 1;
640 }
641 if (writecnt - 4 > emu_max_byteprogram_size) {
642 msg_perr("Max BYTE PROGRAM size exceeded!\n");
643 return 1;
644 }
645 memcpy(flashchip_contents + offs, writearr + 4, writecnt - 4);
Simon Glasscf253a72013-07-10 21:10:11 -0700646 emu_modified = 1;
hailfinger6ead7222010-11-01 22:07:04 +0000647 break;
648 case JEDEC_AAI_WORD_PROGRAM:
649 if (!emu_max_aai_size)
650 break;
651 if (!aai_active) {
652 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
653 msg_perr("Initial AAI WORD PROGRAM size too "
654 "short!\n");
655 return 1;
656 }
657 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
658 msg_perr("Initial AAI WORD PROGRAM size too "
659 "long!\n");
660 return 1;
661 }
662 aai_active = 1;
663 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
664 writearr[3];
665 /* Truncate to emu_chip_size. */
666 aai_offs %= emu_chip_size;
667 memcpy(flashchip_contents + aai_offs, writearr + 4, 2);
668 aai_offs += 2;
669 } else {
670 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
671 msg_perr("Continuation AAI WORD PROGRAM size "
672 "too short!\n");
673 return 1;
674 }
675 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
676 msg_perr("Continuation AAI WORD PROGRAM size "
677 "too long!\n");
678 return 1;
679 }
680 memcpy(flashchip_contents + aai_offs, writearr + 1, 2);
681 aai_offs += 2;
682 }
Simon Glasscf253a72013-07-10 21:10:11 -0700683 emu_modified = 1;
hailfinger6ead7222010-11-01 22:07:04 +0000684 break;
685 case JEDEC_WRDI:
686 if (!emu_max_aai_size)
687 break;
688 aai_active = 0;
689 break;
690 case JEDEC_SE:
691 if (!emu_jedec_se_size)
692 break;
693 if (writecnt != JEDEC_SE_OUTSIZE) {
694 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
695 return 1;
696 }
697 if (readcnt != JEDEC_SE_INSIZE) {
698 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
699 return 1;
700 }
701 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
702 if (offs & (emu_jedec_se_size - 1))
hailfingere53f5e42011-02-04 22:52:04 +0000703 msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
hailfinger6ead7222010-11-01 22:07:04 +0000704 offs &= ~(emu_jedec_se_size - 1);
705 memset(flashchip_contents + offs, 0xff, emu_jedec_se_size);
Simon Glasscf253a72013-07-10 21:10:11 -0700706 emu_modified = 1;
hailfinger6ead7222010-11-01 22:07:04 +0000707 break;
708 case JEDEC_BE_52:
709 if (!emu_jedec_be_52_size)
710 break;
711 if (writecnt != JEDEC_BE_52_OUTSIZE) {
712 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
713 return 1;
714 }
715 if (readcnt != JEDEC_BE_52_INSIZE) {
716 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
717 return 1;
718 }
719 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
720 if (offs & (emu_jedec_be_52_size - 1))
hailfingere53f5e42011-02-04 22:52:04 +0000721 msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
hailfinger6ead7222010-11-01 22:07:04 +0000722 offs &= ~(emu_jedec_be_52_size - 1);
723 memset(flashchip_contents + offs, 0xff, emu_jedec_be_52_size);
Simon Glasscf253a72013-07-10 21:10:11 -0700724 emu_modified = 1;
hailfinger6ead7222010-11-01 22:07:04 +0000725 break;
726 case JEDEC_BE_D8:
727 if (!emu_jedec_be_d8_size)
728 break;
729 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
730 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
731 return 1;
732 }
733 if (readcnt != JEDEC_BE_D8_INSIZE) {
734 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
735 return 1;
736 }
737 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
738 if (offs & (emu_jedec_be_d8_size - 1))
hailfingere53f5e42011-02-04 22:52:04 +0000739 msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
hailfinger6ead7222010-11-01 22:07:04 +0000740 offs &= ~(emu_jedec_be_d8_size - 1);
741 memset(flashchip_contents + offs, 0xff, emu_jedec_be_d8_size);
742 break;
743 case JEDEC_CE_60:
744 if (!emu_jedec_ce_60_size)
745 break;
746 if (writecnt != JEDEC_CE_60_OUTSIZE) {
747 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
748 return 1;
749 }
750 if (readcnt != JEDEC_CE_60_INSIZE) {
751 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
752 return 1;
753 }
hailfingere53f5e42011-02-04 22:52:04 +0000754 /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
hailfinger6ead7222010-11-01 22:07:04 +0000755 /* emu_jedec_ce_60_size is emu_chip_size. */
hailfingere53f5e42011-02-04 22:52:04 +0000756 memset(flashchip_contents, 0xff, emu_jedec_ce_60_size);
Simon Glasscf253a72013-07-10 21:10:11 -0700757 emu_modified = 1;
hailfinger6ead7222010-11-01 22:07:04 +0000758 break;
759 case JEDEC_CE_C7:
760 if (!emu_jedec_ce_c7_size)
761 break;
762 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
763 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
764 return 1;
765 }
766 if (readcnt != JEDEC_CE_C7_INSIZE) {
767 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
768 return 1;
769 }
hailfingere53f5e42011-02-04 22:52:04 +0000770 /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
hailfinger6ead7222010-11-01 22:07:04 +0000771 /* emu_jedec_ce_c7_size is emu_chip_size. */
772 memset(flashchip_contents, 0xff, emu_jedec_ce_c7_size);
Simon Glasscf253a72013-07-10 21:10:11 -0700773 emu_modified = 1;
hailfinger6ead7222010-11-01 22:07:04 +0000774 break;
775 default:
776 /* No special response. */
777 break;
778 }
779 return 0;
780}
781#endif
782
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700783static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
hailfingerf91e3b52009-05-14 12:59:36 +0000784 const unsigned char *writearr, unsigned char *readarr)
785{
786 int i;
787
hailfinger50c335f2010-01-09 04:32:23 +0000788 msg_pspew("%s:", __func__);
hailfingerf91e3b52009-05-14 12:59:36 +0000789
hailfinger50c335f2010-01-09 04:32:23 +0000790 msg_pspew(" writing %u bytes:", writecnt);
hailfingerf91e3b52009-05-14 12:59:36 +0000791 for (i = 0; i < writecnt; i++)
hailfinger50c335f2010-01-09 04:32:23 +0000792 msg_pspew(" 0x%02x", writearr[i]);
hailfingerf91e3b52009-05-14 12:59:36 +0000793
hailfinger6ead7222010-11-01 22:07:04 +0000794 /* Response for unknown commands and missing chip is 0xff. */
795 memset(readarr, 0xff, readcnt);
796#if EMULATE_SPI_CHIP
797 switch (emu_chip) {
798 case EMULATE_ST_M25P10_RES:
799 case EMULATE_SST_SST25VF040_REMS:
800 case EMULATE_SST_SST25VF032B:
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800801 case EMULATE_VARIABLE_SIZE:
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700802 if (emulate_spi_chip_response(flash, writecnt, readcnt, writearr,
hailfinger6ead7222010-11-01 22:07:04 +0000803 readarr)) {
Carl-Daniel Hailfingerc49783d2016-08-05 10:52:06 -0700804 msg_pdbg("Invalid command sent to flash chip!\n");
hailfinger6ead7222010-11-01 22:07:04 +0000805 return 1;
806 }
807 break;
808 default:
809 break;
810 }
811#endif
hailfinger50c335f2010-01-09 04:32:23 +0000812 msg_pspew(" reading %u bytes:", readcnt);
uwe8d342eb2011-07-28 08:13:25 +0000813 for (i = 0; i < readcnt; i++)
hailfinger6ead7222010-11-01 22:07:04 +0000814 msg_pspew(" 0x%02x", readarr[i]);
hailfinger50c335f2010-01-09 04:32:23 +0000815 msg_pspew("\n");
David Hendricks84377002014-09-09 16:09:31 -0700816
817 programmer_delay((writecnt + readcnt) * delay_us);
hailfingerf91e3b52009-05-14 12:59:36 +0000818 return 0;
819}
hailfingera8727712010-06-20 10:58:32 +0000820
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700821static int dummy_spi_write_256(struct flashctx *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +0000822 unsigned int start, unsigned int len)
hailfingerc7d06c62010-07-14 16:19:05 +0000823{
hailfinger6ead7222010-11-01 22:07:04 +0000824 return spi_write_chunked(flash, buf, start, len,
825 spi_write_256_chunksize);
hailfingerc7d06c62010-07-14 16:19:05 +0000826}
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800827
828#if EMULATE_CHIP && EMULATE_SPI_CHIP
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700829int probe_variable_size(struct flashctx *flash)
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800830{
831 int i;
832
833 /* Skip the probing if we don't emulate this chip. */
834 if (emu_chip != EMULATE_VARIABLE_SIZE)
835 return 0;
836
837 /*
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700838 * This will break if one day flashctx becomes read-only.
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800839 * Once that happens, we need to have special hacks in functions:
840 *
841 * erase_and_write_flash() in flashrom.c
842 * read_flash_to_file()
843 * handle_romentries()
844 * ...
845 *
846 * Search "total_size * 1024" in code.
847 */
848 if (emu_chip_size % 1024)
849 msg_perr("%s: emu_chip_size is not multipler of 1024.\n",
850 __func__);
851 flash->total_size = emu_chip_size / 1024;
852 msg_cdbg("%s: set flash->total_size to %dK bytes.\n", __func__,
853 flash->total_size);
854
David Hendricks0eda2a82014-09-12 16:32:05 -0700855 if (erase_to_zero)
856 flash->feature_bits |= FEATURE_ERASE_TO_ZERO;
857
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800858 /* Update eraser count */
859 for (i = 0; i < NUM_ERASEFUNCTIONS; i++) {
860 struct block_eraser *eraser = &flash->block_erasers[i];
861 if (eraser->block_erase == NULL)
862 break;
863
864 eraser->eraseblocks[0].count = emu_chip_size /
865 eraser->eraseblocks[0].size;
866 msg_cdbg("%s: eraser.size=%d, .count=%d\n",
867 __func__, eraser->eraseblocks[0].size,
868 eraser->eraseblocks[0].count);
869 }
870
871 return 1;
872}
873#endif