blob: d9c40855d7d0330ae49c0fade3ff52d8e15af06a [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.
hailfingera9df33c2009-05-09 00:54:55 +000014 */
15
hailfingera9df33c2009-05-09 00:54:55 +000016#include <string.h>
17#include <stdlib.h>
Carl-Daniel Hailfingerc49783d2016-08-05 10:52:06 -070018#include <stdio.h>
19#include <ctype.h>
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +100020#include <errno.h>
hailfingera9df33c2009-05-09 00:54:55 +000021#include "flash.h"
hailfingera8727712010-06-20 10:58:32 +000022#include "chipdrivers.h"
hailfinger428f6852010-07-27 22:41:39 +000023#include "programmer.h"
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +080024#include "flashchips.h"
hailfingera9df33c2009-05-09 00:54:55 +000025
hailfinger6ead7222010-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,
Stuart Langley78cacf72020-04-23 11:48:01 +100046 EMULATE_MACRONIX_MX25L6436,
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +100047 EMULATE_WINBOND_W25Q128FV,
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +080048 EMULATE_VARIABLE_SIZE,
hailfinger6ead7222010-11-01 22:07:04 +000049};
Stuart Langley78cacf72020-04-23 11:48:01 +100050
Lachlan Bishopbf1476e2020-09-10 14:57:05 +100051struct emu_data {
52 enum emu_chip emu_chip;
53 char *emu_persistent_image;
54 unsigned int emu_chip_size;
55 int erase_to_zero;
56 int emu_modified; /* is the image modified since reading it? */
57 uint8_t emu_status;
Edward O'Callaghan0037f452020-09-17 13:32:00 +100058 /* If "freq" parameter is passed in from command line, commands will delay
59 * for this period before returning. */
60 unsigned long int delay_us;
Lachlan Bishopbf1476e2020-09-10 14:57:05 +100061 unsigned int emu_max_byteprogram_size;
62 unsigned int emu_max_aai_size;
63 unsigned int emu_jedec_se_size;
64 unsigned int emu_jedec_be_52_size;
65 unsigned int emu_jedec_be_d8_size;
66 unsigned int emu_jedec_ce_60_size;
67 unsigned int emu_jedec_ce_c7_size;
68 unsigned char spi_blacklist[256];
69 unsigned char spi_ignorelist[256];
70 unsigned int spi_blacklist_size;
71 unsigned int spi_ignorelist_size;
72};
73
74#if EMULATE_SPI_CHIP
Stuart Langley78cacf72020-04-23 11:48:01 +100075/* A legit complete SFDP table based on the MX25L6436E (rev. 1.8) datasheet. */
76static const uint8_t sfdp_table[] = {
77 0x53, 0x46, 0x44, 0x50, // @0x00: SFDP signature
78 0x00, 0x01, 0x01, 0xFF, // @0x04: revision 1.0, 2 headers
79 0x00, 0x00, 0x01, 0x09, // @0x08: JEDEC SFDP header rev. 1.0, 9 DW long
80 0x1C, 0x00, 0x00, 0xFF, // @0x0C: PTP0 = 0x1C (instead of 0x30)
81 0xC2, 0x00, 0x01, 0x04, // @0x10: Macronix header rev. 1.0, 4 DW long
82 0x48, 0x00, 0x00, 0xFF, // @0x14: PTP1 = 0x48 (instead of 0x60)
83 0xFF, 0xFF, 0xFF, 0xFF, // @0x18: hole.
84 0xE5, 0x20, 0xC9, 0xFF, // @0x1C: SFDP parameter table start
85 0xFF, 0xFF, 0xFF, 0x03, // @0x20
86 0x00, 0xFF, 0x08, 0x6B, // @0x24
87 0x08, 0x3B, 0x00, 0xFF, // @0x28
88 0xEE, 0xFF, 0xFF, 0xFF, // @0x2C
89 0xFF, 0xFF, 0x00, 0x00, // @0x30
90 0xFF, 0xFF, 0x00, 0xFF, // @0x34
91 0x0C, 0x20, 0x0F, 0x52, // @0x38
92 0x10, 0xD8, 0x00, 0xFF, // @0x3C: SFDP parameter table end
93 0xFF, 0xFF, 0xFF, 0xFF, // @0x40: hole.
94 0xFF, 0xFF, 0xFF, 0xFF, // @0x44: hole.
95 0x00, 0x36, 0x00, 0x27, // @0x48: Macronix parameter table start
96 0xF4, 0x4F, 0xFF, 0xFF, // @0x4C
97 0xD9, 0xC8, 0xFF, 0xFF, // @0x50
98 0xFF, 0xFF, 0xFF, 0xFF, // @0x54: Macronix parameter table end
99};
100
hailfinger6ead7222010-11-01 22:07:04 +0000101#endif
102#endif
103
stefanctc5eb8a92011-11-23 09:13:48 +0000104static unsigned int spi_write_256_chunksize = 256;
hailfinger6ead7222010-11-01 22:07:04 +0000105
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700106static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
Stuart langleyc98e43f2020-03-26 20:27:36 +1100107 const unsigned char *writearr, unsigned char *readarr);
Patrick Georgiab8353e2017-02-03 18:32:01 +0100108static int dummy_spi_write_256(struct flashctx *flash, const uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +0000109 unsigned int start, unsigned int len);
Stuart langleyc98e43f2020-03-26 20:27:36 +1100110static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr);
111static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr);
112static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr);
113static void dummy_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len);
114static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr);
115static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr);
116static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr);
117static void dummy_chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len);
mkarcherd264e9e2011-05-11 17:07:07 +0000118
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000119static struct spi_master spi_master_dummyflasher = {
Edward O'Callaghana6673bd2019-06-24 15:22:28 +1000120 .features = SPI_MASTER_4BA,
uwe8d342eb2011-07-28 08:13:25 +0000121 .max_data_read = MAX_DATA_READ_UNLIMITED,
122 .max_data_write = MAX_DATA_UNSPECIFIED,
123 .command = dummy_spi_send_command,
124 .multicommand = default_spi_send_multicommand,
125 .read = default_spi_read,
126 .write_256 = dummy_spi_write_256,
Edward O'Callaghaneeaac6b2020-10-12 19:51:56 +1100127 .write_aai = default_spi_write_aai,
mkarcherd264e9e2011-05-11 17:07:07 +0000128};
dhendrix0ffc2eb2011-06-14 01:35:36 +0000129
Namyoon Wooc429efb2020-10-17 20:25:15 -0700130static struct par_master par_master_dummy = {
hailfinger76bb7e92011-11-09 23:40:00 +0000131 .chip_readb = dummy_chip_readb,
132 .chip_readw = dummy_chip_readw,
133 .chip_readl = dummy_chip_readl,
134 .chip_readn = dummy_chip_readn,
135 .chip_writeb = dummy_chip_writeb,
136 .chip_writew = dummy_chip_writew,
137 .chip_writel = dummy_chip_writel,
138 .chip_writen = dummy_chip_writen,
139};
140
Edward O'Callaghanef4e28b2019-06-28 13:18:41 +1000141static enum chipbustype dummy_buses_supported = BUS_NONE;
hailfinger76bb7e92011-11-09 23:40:00 +0000142
David Hendricks93784b42016-08-09 17:00:38 -0700143static int dummy_shutdown(void *data)
dhendrix0ffc2eb2011-06-14 01:35:36 +0000144{
145 msg_pspew("%s\n", __func__);
146#if EMULATE_CHIP
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000147 struct emu_data *emu_data = (struct emu_data *)data;
148 if (emu_data->emu_chip != EMULATE_NONE) {
149 if (emu_data->emu_persistent_image && emu_data->emu_modified) {
150 msg_pdbg("Writing %s\n", emu_data->emu_persistent_image);
151 write_buf_to_file(flashchip_contents,
152 emu_data->emu_chip_size,
153 emu_data->emu_persistent_image);
154 free(emu_data->emu_persistent_image);
155 emu_data->emu_persistent_image = NULL;
dhendrix0ffc2eb2011-06-14 01:35:36 +0000156 }
157 free(flashchip_contents);
158 }
159#endif
160 return 0;
161}
162
David Hendricksac1d25c2016-08-09 17:00:58 -0700163int dummy_init(void)
hailfingera9df33c2009-05-09 00:54:55 +0000164{
hailfinger1ef766d2010-07-06 09:55:48 +0000165 char *bustext = NULL;
hailfinger6ead7222010-11-01 22:07:04 +0000166 char *tmp = NULL;
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +1000167 unsigned int i;
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800168#if EMULATE_SPI_CHIP
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +1000169 char *status = NULL;
Edward O'Callaghan602fbab2020-11-23 12:27:57 +1100170 int size = -1; /* size for VARIABLE_SIZE chip device */
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800171#endif
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +1000172#if EMULATE_CHIP
173 struct stat image_stat;
hailfinger6ead7222010-11-01 22:07:04 +0000174#endif
hailfinger1ef766d2010-07-06 09:55:48 +0000175
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000176 struct emu_data *data = calloc(1, sizeof(struct emu_data));
177 if (!data) {
178 msg_perr("Out of memory!\n");
179 return 1;
180 }
181 data->emu_chip = EMULATE_NONE;
Edward O'Callaghan0037f452020-09-17 13:32:00 +1000182 data->delay_us = 0;
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000183 spi_master_dummyflasher.data = data;
Namyoon Wooc429efb2020-10-17 20:25:15 -0700184 par_master_dummy.data = data;
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000185
hailfinger50c335f2010-01-09 04:32:23 +0000186 msg_pspew("%s\n", __func__);
hailfinger668f3502009-06-01 00:02:11 +0000187
hailfingerddeb4ac2010-07-08 10:13:37 +0000188 bustext = extract_programmer_param("bus");
hailfinger1ef766d2010-07-06 09:55:48 +0000189 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
190 if (!bustext)
191 bustext = strdup("parallel+lpc+fwh+spi");
hailfinger668f3502009-06-01 00:02:11 +0000192 /* Convert the parameters to lowercase. */
hailfinger1ef766d2010-07-06 09:55:48 +0000193 tolower_string(bustext);
hailfinger668f3502009-06-01 00:02:11 +0000194
hailfinger76bb7e92011-11-09 23:40:00 +0000195 dummy_buses_supported = BUS_NONE;
hailfinger1ef766d2010-07-06 09:55:48 +0000196 if (strstr(bustext, "parallel")) {
hailfinger76bb7e92011-11-09 23:40:00 +0000197 dummy_buses_supported |= BUS_PARALLEL;
hailfinger50c335f2010-01-09 04:32:23 +0000198 msg_pdbg("Enabling support for %s flash.\n", "parallel");
hailfinger668f3502009-06-01 00:02:11 +0000199 }
hailfinger1ef766d2010-07-06 09:55:48 +0000200 if (strstr(bustext, "lpc")) {
hailfinger76bb7e92011-11-09 23:40:00 +0000201 dummy_buses_supported |= BUS_LPC;
hailfinger50c335f2010-01-09 04:32:23 +0000202 msg_pdbg("Enabling support for %s flash.\n", "LPC");
hailfinger668f3502009-06-01 00:02:11 +0000203 }
hailfinger1ef766d2010-07-06 09:55:48 +0000204 if (strstr(bustext, "fwh")) {
hailfinger76bb7e92011-11-09 23:40:00 +0000205 dummy_buses_supported |= BUS_FWH;
hailfinger50c335f2010-01-09 04:32:23 +0000206 msg_pdbg("Enabling support for %s flash.\n", "FWH");
hailfinger668f3502009-06-01 00:02:11 +0000207 }
hailfinger1ef766d2010-07-06 09:55:48 +0000208 if (strstr(bustext, "spi")) {
hailfinger76bb7e92011-11-09 23:40:00 +0000209 dummy_buses_supported |= BUS_SPI;
hailfinger50c335f2010-01-09 04:32:23 +0000210 msg_pdbg("Enabling support for %s flash.\n", "SPI");
hailfinger668f3502009-06-01 00:02:11 +0000211 }
hailfinger76bb7e92011-11-09 23:40:00 +0000212 if (dummy_buses_supported == BUS_NONE)
hailfinger50c335f2010-01-09 04:32:23 +0000213 msg_pdbg("Support for all flash bus types disabled.\n");
hailfinger1ef766d2010-07-06 09:55:48 +0000214 free(bustext);
hailfinger6ead7222010-11-01 22:07:04 +0000215
216 tmp = extract_programmer_param("spi_write_256_chunksize");
217 if (tmp) {
218 spi_write_256_chunksize = atoi(tmp);
219 free(tmp);
220 if (spi_write_256_chunksize < 1) {
221 msg_perr("invalid spi_write_256_chunksize\n");
222 return 1;
223 }
224 }
225
Carl-Daniel Hailfingerc49783d2016-08-05 10:52:06 -0700226 tmp = extract_programmer_param("spi_blacklist");
227 if (tmp) {
228 i = strlen(tmp);
229 if (!strncmp(tmp, "0x", 2)) {
230 i -= 2;
231 memmove(tmp, tmp + 2, i + 1);
232 }
233 if ((i > 512) || (i % 2)) {
234 msg_perr("Invalid SPI command blacklist length\n");
235 free(tmp);
236 return 1;
237 }
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000238 data->spi_blacklist_size = i / 2;
239 for (i = 0; i < data->spi_blacklist_size * 2; i++) {
Carl-Daniel Hailfingerc49783d2016-08-05 10:52:06 -0700240 if (!isxdigit((unsigned char)tmp[i])) {
241 msg_perr("Invalid char \"%c\" in SPI command "
242 "blacklist\n", tmp[i]);
243 free(tmp);
244 return 1;
245 }
246 }
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000247 for (i = 0; i < data->spi_blacklist_size; i++) {
Carl-Daniel Hailfingerc49783d2016-08-05 10:52:06 -0700248 unsigned int tmp2;
249 /* SCNx8 is apparently not supported by MSVC (and thus
250 * MinGW), so work around it with an extra variable
251 */
252 sscanf(tmp + i * 2, "%2x", &tmp2);
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000253 data->spi_blacklist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfingerc49783d2016-08-05 10:52:06 -0700254 }
255 msg_pdbg("SPI blacklist is ");
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000256 for (i = 0; i < data->spi_blacklist_size; i++)
257 msg_pdbg("%02x ", data->spi_blacklist[i]);
258 msg_pdbg(", size %u\n", data->spi_blacklist_size);
Carl-Daniel Hailfingerc49783d2016-08-05 10:52:06 -0700259 }
260 free(tmp);
261
262 tmp = extract_programmer_param("spi_ignorelist");
263 if (tmp) {
264 i = strlen(tmp);
265 if (!strncmp(tmp, "0x", 2)) {
266 i -= 2;
267 memmove(tmp, tmp + 2, i + 1);
268 }
269 if ((i > 512) || (i % 2)) {
270 msg_perr("Invalid SPI command ignorelist length\n");
271 free(tmp);
272 return 1;
273 }
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000274 data->spi_ignorelist_size = i / 2;
275 for (i = 0; i < data->spi_ignorelist_size * 2; i++) {
Carl-Daniel Hailfingerc49783d2016-08-05 10:52:06 -0700276 if (!isxdigit((unsigned char)tmp[i])) {
277 msg_perr("Invalid char \"%c\" in SPI command "
278 "ignorelist\n", tmp[i]);
279 free(tmp);
280 return 1;
281 }
282 }
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000283 for (i = 0; i < data->spi_ignorelist_size; i++) {
Carl-Daniel Hailfingerc49783d2016-08-05 10:52:06 -0700284 unsigned int tmp2;
285 /* SCNx8 is apparently not supported by MSVC (and thus
286 * MinGW), so work around it with an extra variable
287 */
288 sscanf(tmp + i * 2, "%2x", &tmp2);
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000289 data->spi_ignorelist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfingerc49783d2016-08-05 10:52:06 -0700290 }
291 msg_pdbg("SPI ignorelist is ");
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000292 for (i = 0; i < data->spi_ignorelist_size; i++)
293 msg_pdbg("%02x ", data->spi_ignorelist[i]);
294 msg_pdbg(", size %u\n", data->spi_ignorelist_size);
Carl-Daniel Hailfingerc49783d2016-08-05 10:52:06 -0700295 }
296 free(tmp);
297
David Hendricks84377002014-09-09 16:09:31 -0700298 /* frequency to emulate in Hz (default), KHz, or MHz */
299 tmp = extract_programmer_param("freq");
300 if (tmp) {
301 unsigned long int freq;
302 char *units = tmp;
303 char *end = tmp + strlen(tmp);
304
305 errno = 0;
306 freq = strtoul(tmp, &units, 0);
307 if (errno) {
308 msg_perr("Invalid frequency \"%s\", %s\n",
309 tmp, strerror(errno));
Edward O'Callaghan0037f452020-09-17 13:32:00 +1000310 free(tmp);
311 return 1;
David Hendricks84377002014-09-09 16:09:31 -0700312 }
313
314 if ((units > tmp) && (units < end)) {
315 int units_valid = 0;
316
317 if (units < end - 3) {
318 ;
319 } else if (units == end - 2) {
320 if (!strcasecmp(units, "hz"))
321 units_valid = 1;
322 } else if (units == end - 3) {
323 if (!strcasecmp(units, "khz")) {
324 freq *= 1000;
325 units_valid = 1;
326 } else if (!strcasecmp(units, "mhz")) {
327 freq *= 1000000;
328 units_valid = 1;
329 }
330 }
331
332 if (!units_valid) {
333 msg_perr("Invalid units: %s\n", units);
Edward O'Callaghan0037f452020-09-17 13:32:00 +1000334 free(tmp);
David Hendricks84377002014-09-09 16:09:31 -0700335 return 1;
336 }
337 }
338
339 /* Assume we only work with bytes and transfer at 1 bit/Hz */
Edward O'Callaghan0037f452020-09-17 13:32:00 +1000340 data->delay_us = (1000000 * 8) / freq;
David Hendricks84377002014-09-09 16:09:31 -0700341 }
Edward O'Callaghan0037f452020-09-17 13:32:00 +1000342 free(tmp);
David Hendricks84377002014-09-09 16:09:31 -0700343
hailfinger6ead7222010-11-01 22:07:04 +0000344#if EMULATE_CHIP
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800345#if EMULATE_SPI_CHIP
346 tmp = extract_programmer_param("size");
347 if (tmp) {
Edward O'Callaghan602fbab2020-11-23 12:27:57 +1100348 size = strtol(tmp, NULL, 10);
349 if (size <= 0 || (size % 1024 != 0)) {
350 msg_perr("%s: Chip size is not a multipler of 1024: %s\n",
351 __func__, tmp);
352 free(tmp);
353 return 1;
Namyoon Wooc429efb2020-10-17 20:25:15 -0700354 }
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +1000355 free(tmp);
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800356 }
357#endif
358
hailfinger6ead7222010-11-01 22:07:04 +0000359 tmp = extract_programmer_param("emulate");
360 if (!tmp) {
361 msg_pdbg("Not emulating any flash chip.\n");
362 /* Nothing else to do. */
dhendrix0ffc2eb2011-06-14 01:35:36 +0000363 goto dummy_init_out;
hailfinger6ead7222010-11-01 22:07:04 +0000364 }
365#if EMULATE_SPI_CHIP
366 if (!strcmp(tmp, "M25P10.RES")) {
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000367 data->emu_chip = EMULATE_ST_M25P10_RES;
368 data->emu_chip_size = 128 * 1024;
369 data->emu_max_byteprogram_size = 128;
370 data->emu_max_aai_size = 0;
371 data->emu_jedec_se_size = 0;
372 data->emu_jedec_be_52_size = 0;
373 data->emu_jedec_be_d8_size = 32 * 1024;
374 data->emu_jedec_ce_60_size = 0;
375 data->emu_jedec_ce_c7_size = data->emu_chip_size;
hailfinger6ead7222010-11-01 22:07:04 +0000376 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
377 "write)\n");
378 }
379 if (!strcmp(tmp, "SST25VF040.REMS")) {
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000380 data->emu_chip = EMULATE_SST_SST25VF040_REMS;
381 data->emu_chip_size = 512 * 1024;
382 data->emu_max_byteprogram_size = 1;
383 data->emu_max_aai_size = 0;
384 data->emu_jedec_se_size = 4 * 1024;
385 data->emu_jedec_be_52_size = 32 * 1024;
386 data->emu_jedec_be_d8_size = 0;
387 data->emu_jedec_ce_60_size = data->emu_chip_size;
388 data->emu_jedec_ce_c7_size = 0;
hailfinger6ead7222010-11-01 22:07:04 +0000389 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
390 "byte write)\n");
391 }
392 if (!strcmp(tmp, "SST25VF032B")) {
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000393 data->emu_chip = EMULATE_SST_SST25VF032B;
394 data->emu_chip_size = 4 * 1024 * 1024;
395 data->emu_max_byteprogram_size = 1;
396 data->emu_max_aai_size = 2;
397 data->emu_jedec_se_size = 4 * 1024;
398 data->emu_jedec_be_52_size = 32 * 1024;
399 data->emu_jedec_be_d8_size = 64 * 1024;
400 data->emu_jedec_ce_60_size = data->emu_chip_size;
401 data->emu_jedec_ce_c7_size = data->emu_chip_size;
hailfinger6ead7222010-11-01 22:07:04 +0000402 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
403 "write)\n");
404 }
Stuart Langley78cacf72020-04-23 11:48:01 +1000405 if (!strcmp(tmp, "MX25L6436")) {
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000406 data->emu_chip = EMULATE_MACRONIX_MX25L6436;
407 data->emu_chip_size = 8 * 1024 * 1024;
408 data->emu_max_byteprogram_size = 256;
409 data->emu_max_aai_size = 0;
410 data->emu_jedec_se_size = 4 * 1024;
411 data->emu_jedec_be_52_size = 32 * 1024;
412 data->emu_jedec_be_d8_size = 64 * 1024;
413 data->emu_jedec_ce_60_size = data->emu_chip_size;
414 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Stuart Langley78cacf72020-04-23 11:48:01 +1000415 msg_pdbg("Emulating Macronix MX25L6436 SPI flash chip (RDID, "
416 "SFDP)\n");
417 }
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +1000418 if (!strcmp(tmp, "W25Q128FV")) {
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000419 data->emu_chip = EMULATE_WINBOND_W25Q128FV;
420 data->emu_chip_size = 16 * 1024 * 1024;
421 data->emu_max_byteprogram_size = 256;
422 data->emu_max_aai_size = 0;
423 data->emu_jedec_se_size = 4 * 1024;
424 data->emu_jedec_be_52_size = 32 * 1024;
425 data->emu_jedec_be_d8_size = 64 * 1024;
426 data->emu_jedec_ce_60_size = data->emu_chip_size;
427 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +1000428 msg_pdbg("Emulating Winbond W25Q128FV SPI flash chip (RDID)\n");
429 }
Simon Glassd2c64a22013-07-03 22:05:21 +0900430
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +1000431 /* The name of variable-size virtual chip. A 4 MiB flash example:
432 * flashrom -p dummy:emulate=VARIABLE_SIZE,size=4194304
433 */
434 if (!strcmp(tmp, "VARIABLE_SIZE")) {
Edward O'Callaghan602fbab2020-11-23 12:27:57 +1100435 if (size == -1) {
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000436 msg_perr("%s: the size parameter is not given.\n", __func__);
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800437 free(tmp);
438 return 1;
439 }
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000440 data->emu_chip = EMULATE_VARIABLE_SIZE;
441 data->emu_chip_size = size;
442 data->emu_max_byteprogram_size = 256;
443 data->emu_max_aai_size = 0;
444 data->emu_jedec_se_size = 4 * 1024;
445 data->emu_jedec_be_52_size = 32 * 1024;
446 data->emu_jedec_be_d8_size = 64 * 1024;
447 data->emu_jedec_ce_60_size = data->emu_chip_size;
448 data->emu_jedec_ce_c7_size = data->emu_chip_size;
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800449 msg_pdbg("Emulating generic SPI flash chip (size=%d bytes)\n",
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000450 data->emu_chip_size);
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800451 }
hailfinger6ead7222010-11-01 22:07:04 +0000452#endif
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000453 if (data->emu_chip == EMULATE_NONE) {
hailfinger6ead7222010-11-01 22:07:04 +0000454 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
455 free(tmp);
456 return 1;
457 }
David Hendricks0eda2a82014-09-12 16:32:05 -0700458
459 /* Should emulated flash erase to zero (yes/no)? */
460 tmp = extract_programmer_param("erase_to_zero");
461 if (tmp) {
462 if (!strcmp(tmp, "yes")) {
463 msg_pdbg("Emulated chip will erase to 0x00\n");
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000464 data->erase_to_zero = 1;
David Hendricks0eda2a82014-09-12 16:32:05 -0700465 } else if (!strcmp(tmp, "no")) {
466 msg_pdbg("Emulated chip will erase to 0xff\n");
467 } else {
468 msg_perr("erase_to_zero can be \"yes\" or \"no\"\n");
469 return 1;
470 }
471 }
472
hailfinger6ead7222010-11-01 22:07:04 +0000473 free(tmp);
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000474 flashchip_contents = malloc(data->emu_chip_size);
hailfinger6ead7222010-11-01 22:07:04 +0000475 if (!flashchip_contents) {
476 msg_perr("Out of memory!\n");
477 return 1;
478 }
dhendrix0ffc2eb2011-06-14 01:35:36 +0000479
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +1000480#ifdef EMULATE_SPI_CHIP
481 status = extract_programmer_param("spi_status");
482 if (status) {
483 char *endptr;
484 errno = 0;
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000485 data->emu_status = strtoul(status, &endptr, 0);
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +1000486 free(status);
487 if (errno != 0 || status == endptr) {
488 msg_perr("Error: initial status register specified, "
489 "but the value could not be converted.\n");
490 return 1;
491 }
492 msg_pdbg("Initial status register is set to 0x%02x.\n",
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000493 data->emu_status);
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +1000494 }
495#endif
496
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000497 msg_pdbg("Filling fake flash chip with 0x%02x, size %i\n",
498 data->erase_to_zero ? 0x00 : 0xff, data->emu_chip_size);
499 memset(flashchip_contents, data->erase_to_zero ? 0x00 : 0xff, data->emu_chip_size);
500
501 /* Will be freed by shutdown function if necessary. */
Edward O'Callaghan602fbab2020-11-23 12:27:57 +1100502 data->emu_persistent_image = extract_programmer_param("image");
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000503 if (!data->emu_persistent_image) {
hailfinger6ead7222010-11-01 22:07:04 +0000504 /* Nothing else to do. */
dhendrix0ffc2eb2011-06-14 01:35:36 +0000505 goto dummy_init_out;
hailfinger6ead7222010-11-01 22:07:04 +0000506 }
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +1000507 /* We will silently (in default verbosity) ignore the file if it does not exist (yet) or the size does
508 * not match the emulated chip. */
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000509 if (!stat(data->emu_persistent_image, &image_stat)) {
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +1000510 msg_pdbg("Found persistent image %s, %jd B ",
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000511 data->emu_persistent_image, (intmax_t)image_stat.st_size);
512 if ((uintmax_t)image_stat.st_size == data->emu_chip_size) {
hailfinger6ead7222010-11-01 22:07:04 +0000513 msg_pdbg("matches.\n");
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000514 msg_pdbg("Reading %s\n", data->emu_persistent_image);
515 if (read_buf_from_file(flashchip_contents, data->emu_chip_size,
516 data->emu_persistent_image)) {
517 msg_perr("Unable to read %s\n", data->emu_persistent_image);
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +1000518 free(flashchip_contents);
519 return 1;
520 }
hailfinger6ead7222010-11-01 22:07:04 +0000521 } else {
522 msg_pdbg("doesn't match.\n");
523 }
524 }
525#endif
hailfingera9df33c2009-05-09 00:54:55 +0000526
dhendrix0ffc2eb2011-06-14 01:35:36 +0000527dummy_init_out:
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000528 if (register_shutdown(dummy_shutdown, data)) {
hailfinger6ead7222010-11-01 22:07:04 +0000529 free(flashchip_contents);
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000530 free(data);
dhendrix0ffc2eb2011-06-14 01:35:36 +0000531 return 1;
hailfinger6ead7222010-11-01 22:07:04 +0000532 }
hailfinger76bb7e92011-11-09 23:40:00 +0000533 if (dummy_buses_supported & (BUS_PARALLEL | BUS_LPC | BUS_FWH))
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100534 register_par_master(&par_master_dummy,
Stuart langleyc98e43f2020-03-26 20:27:36 +1100535 dummy_buses_supported & (BUS_PARALLEL | BUS_LPC | BUS_FWH));
hailfinger76bb7e92011-11-09 23:40:00 +0000536 if (dummy_buses_supported & BUS_SPI)
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100537 register_spi_master(&spi_master_dummyflasher);
hailfinger76bb7e92011-11-09 23:40:00 +0000538
hailfingera9df33c2009-05-09 00:54:55 +0000539 return 0;
540}
541
Patrick Georgi4befc162017-02-03 18:32:01 +0100542void *dummy_map(const char *descr, uintptr_t phys_addr, size_t len)
hailfinger11ae3c42009-05-11 14:13:25 +0000543{
Stuart langleyc98e43f2020-03-26 20:27:36 +1100544 msg_pspew("%s: Mapping %s, 0x%zx bytes at 0x%0*" PRIxPTR "\n",
545 __func__, descr, len, PRIxPTR_WIDTH, phys_addr);
hailfinger11ae3c42009-05-11 14:13:25 +0000546 return (void *)phys_addr;
547}
548
549void dummy_unmap(void *virt_addr, size_t len)
550{
Stuart langleyc98e43f2020-03-26 20:27:36 +1100551 msg_pspew("%s: Unmapping 0x%zx bytes at %p\n", __func__, len, virt_addr);
hailfinger11ae3c42009-05-11 14:13:25 +0000552}
553
Stuart langleyc98e43f2020-03-26 20:27:36 +1100554static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000555{
Kangheui Won4974cc12019-10-18 12:59:01 +1100556 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%02x\n", __func__, addr, val);
hailfingera9df33c2009-05-09 00:54:55 +0000557}
558
Stuart langleyc98e43f2020-03-26 20:27:36 +1100559static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000560{
Kangheui Won4974cc12019-10-18 12:59:01 +1100561 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%04x\n", __func__, addr, val);
hailfingera9df33c2009-05-09 00:54:55 +0000562}
563
Stuart langleyc98e43f2020-03-26 20:27:36 +1100564static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000565{
Kangheui Won4974cc12019-10-18 12:59:01 +1100566 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%08x\n", __func__, addr, val);
hailfingera9df33c2009-05-09 00:54:55 +0000567}
568
Stuart langleyc98e43f2020-03-26 20:27:36 +1100569static void dummy_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len)
hailfinger9d987ef2009-06-05 18:32:07 +0000570{
571 size_t i;
Stuart langleyc98e43f2020-03-26 20:27:36 +1100572 msg_pspew("%s: addr=0x%" PRIxPTR ", len=0x%zx, writing data (hex):", __func__, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000573 for (i = 0; i < len; i++) {
574 if ((i % 16) == 0)
hailfinger50c335f2010-01-09 04:32:23 +0000575 msg_pspew("\n");
576 msg_pspew("%02x ", buf[i]);
hailfinger9d987ef2009-06-05 18:32:07 +0000577 }
578}
579
Stuart langleyc98e43f2020-03-26 20:27:36 +1100580static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000581{
Kangheui Won4974cc12019-10-18 12:59:01 +1100582 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xff\n", __func__, addr);
hailfingera9df33c2009-05-09 00:54:55 +0000583 return 0xff;
584}
585
Stuart langleyc98e43f2020-03-26 20:27:36 +1100586static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000587{
Kangheui Won4974cc12019-10-18 12:59:01 +1100588 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffff\n", __func__, addr);
hailfingera9df33c2009-05-09 00:54:55 +0000589 return 0xffff;
590}
591
Stuart langleyc98e43f2020-03-26 20:27:36 +1100592static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000593{
Kangheui Won4974cc12019-10-18 12:59:01 +1100594 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffffffff\n", __func__, addr);
hailfingera9df33c2009-05-09 00:54:55 +0000595 return 0xffffffff;
596}
597
Stuart langleyc98e43f2020-03-26 20:27:36 +1100598static void dummy_chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len)
hailfinger9d987ef2009-06-05 18:32:07 +0000599{
Stuart langleyc98e43f2020-03-26 20:27:36 +1100600 msg_pspew("%s: addr=0x%" PRIxPTR ", len=0x%zx, returning array of 0xff\n", __func__, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000601 memset(buf, 0xff, len);
602 return;
603}
604
hailfinger6ead7222010-11-01 22:07:04 +0000605#if EMULATE_SPI_CHIP
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +1000606static int emulate_spi_chip_response(unsigned int writecnt,
607 unsigned int readcnt,
608 const unsigned char *writearr,
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000609 unsigned char *readarr,
610 struct emu_data *data)
hailfinger6ead7222010-11-01 22:07:04 +0000611{
Stuart Langley78cacf72020-04-23 11:48:01 +1000612 unsigned int offs, i, toread;
stefanctc5eb8a92011-11-23 09:13:48 +0000613 static int unsigned aai_offs;
Carl-Daniel Hailfinger7a4c0472012-08-30 21:41:00 +0000614 const unsigned char sst25vf040_rems_response[2] = {0xbf, 0x44};
615 const unsigned char sst25vf032b_rems_response[2] = {0xbf, 0x4a};
616 const unsigned char mx25l6436_rems_response[2] = {0xc2, 0x16};
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +1000617 const unsigned char w25q128fv_rems_response[2] = {0xef, 0x17};
hailfinger6ead7222010-11-01 22:07:04 +0000618
619 if (writecnt == 0) {
620 msg_perr("No command sent to the chip!\n");
621 return 1;
622 }
Stefan Tauner718d1eb2016-08-18 18:00:53 -0700623 /* spi_blacklist has precedence over spi_ignorelist. */
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000624 for (i = 0; i < data->spi_blacklist_size; i++) {
625 if (writearr[0] == data->spi_blacklist[i]) {
Carl-Daniel Hailfingerc49783d2016-08-05 10:52:06 -0700626 msg_pdbg("Refusing blacklisted SPI command 0x%02x\n",
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000627 data->spi_blacklist[i]);
Carl-Daniel Hailfingerc49783d2016-08-05 10:52:06 -0700628 return SPI_INVALID_OPCODE;
629 }
630 }
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000631 for (i = 0; i < data->spi_ignorelist_size; i++) {
632 if (writearr[0] == data->spi_ignorelist[i]) {
Carl-Daniel Hailfingerc49783d2016-08-05 10:52:06 -0700633 msg_cdbg("Ignoring ignorelisted SPI command 0x%02x\n",
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000634 data->spi_ignorelist[i]);
Carl-Daniel Hailfingerc49783d2016-08-05 10:52:06 -0700635 /* Return success because the command does not fail,
636 * it is simply ignored.
637 */
638 return 0;
639 }
640 }
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +1000641
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000642 if (data->emu_max_aai_size && (data->emu_status & SPI_SR_AAI)) {
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +1000643 if (writearr[0] != JEDEC_AAI_WORD_PROGRAM &&
644 writearr[0] != JEDEC_WRDI &&
645 writearr[0] != JEDEC_RDSR) {
646 msg_perr("Forbidden opcode (0x%02x) attempted during "
647 "AAI sequence!\n", writearr[0]);
648 return 0;
649 }
650 }
651
hailfinger6ead7222010-11-01 22:07:04 +0000652 switch (writearr[0]) {
653 case JEDEC_RES:
Carl-Daniel Hailfinger7a4c0472012-08-30 21:41:00 +0000654 if (writecnt < JEDEC_RES_OUTSIZE)
hailfinger6ead7222010-11-01 22:07:04 +0000655 break;
Carl-Daniel Hailfinger7a4c0472012-08-30 21:41:00 +0000656 /* offs calculation is only needed for SST chips which treat RES like REMS. */
657 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
658 offs += writecnt - JEDEC_REMS_OUTSIZE;
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000659 switch (data->emu_chip) {
Carl-Daniel Hailfinger7a4c0472012-08-30 21:41:00 +0000660 case EMULATE_ST_M25P10_RES:
661 if (readcnt > 0)
662 memset(readarr, 0x10, readcnt);
663 break;
664 case EMULATE_SST_SST25VF040_REMS:
665 for (i = 0; i < readcnt; i++)
666 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
667 break;
668 case EMULATE_SST_SST25VF032B:
669 for (i = 0; i < readcnt; i++)
670 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
671 break;
672 case EMULATE_MACRONIX_MX25L6436:
673 if (readcnt > 0)
674 memset(readarr, 0x16, readcnt);
675 break;
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +1000676 case EMULATE_WINBOND_W25Q128FV:
677 if (readcnt > 0)
678 memset(readarr, 0x17, readcnt);
679 break;
Carl-Daniel Hailfinger7a4c0472012-08-30 21:41:00 +0000680 default: /* ignore */
681 break;
682 }
hailfinger6ead7222010-11-01 22:07:04 +0000683 break;
684 case JEDEC_REMS:
Carl-Daniel Hailfinger7a4c0472012-08-30 21:41:00 +0000685 /* REMS response has wraparound and uses an address parameter. */
686 if (writecnt < JEDEC_REMS_OUTSIZE)
hailfinger6ead7222010-11-01 22:07:04 +0000687 break;
Carl-Daniel Hailfinger7a4c0472012-08-30 21:41:00 +0000688 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
689 offs += writecnt - JEDEC_REMS_OUTSIZE;
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000690 switch (data->emu_chip) {
Carl-Daniel Hailfinger7a4c0472012-08-30 21:41:00 +0000691 case EMULATE_SST_SST25VF040_REMS:
692 for (i = 0; i < readcnt; i++)
693 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
694 break;
695 case EMULATE_SST_SST25VF032B:
696 for (i = 0; i < readcnt; i++)
697 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
698 break;
699 case EMULATE_MACRONIX_MX25L6436:
700 for (i = 0; i < readcnt; i++)
701 readarr[i] = mx25l6436_rems_response[(offs + i) % 2];
702 break;
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +1000703 case EMULATE_WINBOND_W25Q128FV:
704 for (i = 0; i < readcnt; i++)
705 readarr[i] = w25q128fv_rems_response[(offs + i) % 2];
706 break;
Carl-Daniel Hailfinger7a4c0472012-08-30 21:41:00 +0000707 default: /* ignore */
708 break;
709 }
hailfinger6ead7222010-11-01 22:07:04 +0000710 break;
711 case JEDEC_RDID:
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000712 switch (data->emu_chip) {
Stuart Langley78cacf72020-04-23 11:48:01 +1000713 case EMULATE_SST_SST25VF032B:
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800714 if (readcnt > 0)
715 readarr[0] = 0xbf;
716 if (readcnt > 1)
717 readarr[1] = 0x25;
718 if (readcnt > 2)
719 readarr[2] = 0x4a;
Stuart Langley78cacf72020-04-23 11:48:01 +1000720 break;
721 case EMULATE_MACRONIX_MX25L6436:
722 if (readcnt > 0)
723 readarr[0] = 0xc2;
724 if (readcnt > 1)
725 readarr[1] = 0x20;
726 if (readcnt > 2)
727 readarr[2] = 0x17;
728 break;
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +1000729 case EMULATE_WINBOND_W25Q128FV:
730 if (readcnt > 0)
731 readarr[0] = 0xef;
732 if (readcnt > 1)
733 readarr[1] = 0x40;
734 if (readcnt > 2)
735 readarr[2] = 0x18;
736 break;
Edward O'Callaghane0772312020-09-16 17:57:19 +1000737 case EMULATE_VARIABLE_SIZE:
738 if (readcnt > 0)
739 readarr[0] = (PROGMANUF_ID >> 8) & 0xff;
740 if (readcnt > 1)
741 readarr[1] = PROGMANUF_ID & 0xff;
742 if (readcnt > 2)
743 readarr[2] = (PROGDEV_ID >> 8) & 0xff;
744 if (readcnt > 3)
745 readarr[3] = PROGDEV_ID & 0xff;
Stuart Langley78cacf72020-04-23 11:48:01 +1000746 break;
Stuart Langley78cacf72020-04-23 11:48:01 +1000747 default: /* ignore */
748 break;
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800749 }
hailfinger6ead7222010-11-01 22:07:04 +0000750 break;
751 case JEDEC_RDSR:
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000752 memset(readarr, data->emu_status, readcnt);
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +1000753 break;
754 /* FIXME: this should be chip-specific. */
755 case JEDEC_EWSR:
756 case JEDEC_WREN:
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000757 data->emu_status |= SPI_SR_WEL;
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +1000758 break;
759 case JEDEC_WRSR:
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000760 if (!(data->emu_status & SPI_SR_WEL)) {
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +1000761 msg_perr("WRSR attempted, but WEL is 0!\n");
762 break;
763 }
764 /* FIXME: add some reasonable simulation of the busy flag */
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000765 data->emu_status = writearr[1] & ~SPI_SR_WIP;
766 msg_pdbg2("WRSR wrote 0x%02x.\n", data->emu_status);
hailfinger6ead7222010-11-01 22:07:04 +0000767 break;
768 case JEDEC_READ:
769 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
770 /* Truncate to emu_chip_size. */
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000771 offs %= data->emu_chip_size;
hailfinger6ead7222010-11-01 22:07:04 +0000772 if (readcnt > 0)
773 memcpy(readarr, flashchip_contents + offs, readcnt);
774 break;
Namyoon Woob71a2962020-08-27 16:27:49 -0700775 case JEDEC_READ_4BA:
776 offs = writearr[1] << 24 | writearr[2] << 16 | writearr[3] << 8 | writearr[4];
777 /* Truncate to emu_chip_size. */
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000778 offs %= data->emu_chip_size;
Namyoon Woob71a2962020-08-27 16:27:49 -0700779 if (readcnt > 0)
780 memcpy(readarr, flashchip_contents + offs, readcnt);
781 break;
hailfinger6ead7222010-11-01 22:07:04 +0000782 case JEDEC_BYTE_PROGRAM:
783 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
784 /* Truncate to emu_chip_size. */
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000785 offs %= data->emu_chip_size;
hailfinger6ead7222010-11-01 22:07:04 +0000786 if (writecnt < 5) {
787 msg_perr("BYTE PROGRAM size too short!\n");
788 return 1;
789 }
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000790 if (writecnt - 4 > data->emu_max_byteprogram_size) {
hailfinger6ead7222010-11-01 22:07:04 +0000791 msg_perr("Max BYTE PROGRAM size exceeded!\n");
792 return 1;
793 }
794 memcpy(flashchip_contents + offs, writearr + 4, writecnt - 4);
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000795 data->emu_modified = 1;
hailfinger6ead7222010-11-01 22:07:04 +0000796 break;
Namyoon Woob71a2962020-08-27 16:27:49 -0700797 case JEDEC_BYTE_PROGRAM_4BA:
798 offs = writearr[1] << 24 | writearr[2] << 16 | writearr[3] << 8 | writearr[4];
799 /* Truncate to emu_chip_size. */
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000800 offs %= data->emu_chip_size;
Namyoon Woob71a2962020-08-27 16:27:49 -0700801 if (writecnt < 6) {
802 msg_perr("BYTE PROGRAM size too short!\n");
803 return 1;
804 }
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000805 if (writecnt - 5 > data->emu_max_byteprogram_size) {
Namyoon Woob71a2962020-08-27 16:27:49 -0700806 msg_perr("Max BYTE PROGRAM size exceeded!\n");
807 return 1;
808 }
809 memcpy(flashchip_contents + offs, writearr + 5, writecnt - 5);
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000810 data->emu_modified = 1;
Namyoon Woob71a2962020-08-27 16:27:49 -0700811 break;
hailfinger6ead7222010-11-01 22:07:04 +0000812 case JEDEC_AAI_WORD_PROGRAM:
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000813 if (!data->emu_max_aai_size)
hailfinger6ead7222010-11-01 22:07:04 +0000814 break;
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000815 if (!(data->emu_status & SPI_SR_AAI)) {
hailfinger6ead7222010-11-01 22:07:04 +0000816 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
817 msg_perr("Initial AAI WORD PROGRAM size too "
818 "short!\n");
819 return 1;
820 }
821 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
822 msg_perr("Initial AAI WORD PROGRAM size too "
823 "long!\n");
824 return 1;
825 }
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000826 data->emu_status |= SPI_SR_AAI;
hailfinger6ead7222010-11-01 22:07:04 +0000827 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
828 writearr[3];
829 /* Truncate to emu_chip_size. */
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000830 aai_offs %= data->emu_chip_size;
hailfinger6ead7222010-11-01 22:07:04 +0000831 memcpy(flashchip_contents + aai_offs, writearr + 4, 2);
832 aai_offs += 2;
833 } else {
834 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
835 msg_perr("Continuation AAI WORD PROGRAM size "
836 "too short!\n");
837 return 1;
838 }
839 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
840 msg_perr("Continuation AAI WORD PROGRAM size "
841 "too long!\n");
842 return 1;
843 }
844 memcpy(flashchip_contents + aai_offs, writearr + 1, 2);
845 aai_offs += 2;
846 }
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000847 data->emu_modified = 1;
hailfinger6ead7222010-11-01 22:07:04 +0000848 break;
849 case JEDEC_WRDI:
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000850 if (data->emu_max_aai_size)
851 data->emu_status &= ~SPI_SR_AAI;
hailfinger6ead7222010-11-01 22:07:04 +0000852 break;
853 case JEDEC_SE:
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000854 if (!data->emu_jedec_se_size)
hailfinger6ead7222010-11-01 22:07:04 +0000855 break;
856 if (writecnt != JEDEC_SE_OUTSIZE) {
857 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
858 return 1;
859 }
860 if (readcnt != JEDEC_SE_INSIZE) {
861 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
862 return 1;
863 }
864 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000865 if (offs & (data->emu_jedec_se_size - 1))
hailfingere53f5e42011-02-04 22:52:04 +0000866 msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000867 offs &= ~(data->emu_jedec_se_size - 1);
868 memset(flashchip_contents + offs, 0xff, data->emu_jedec_se_size);
869 data->emu_modified = 1;
hailfinger6ead7222010-11-01 22:07:04 +0000870 break;
871 case JEDEC_BE_52:
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000872 if (!data->emu_jedec_be_52_size)
hailfinger6ead7222010-11-01 22:07:04 +0000873 break;
874 if (writecnt != JEDEC_BE_52_OUTSIZE) {
875 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
876 return 1;
877 }
878 if (readcnt != JEDEC_BE_52_INSIZE) {
879 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
880 return 1;
881 }
882 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000883 if (offs & (data->emu_jedec_be_52_size - 1))
hailfingere53f5e42011-02-04 22:52:04 +0000884 msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000885 offs &= ~(data->emu_jedec_be_52_size - 1);
886 memset(flashchip_contents + offs, 0xff, data->emu_jedec_be_52_size);
887 data->emu_modified = 1;
hailfinger6ead7222010-11-01 22:07:04 +0000888 break;
889 case JEDEC_BE_D8:
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000890 if (!data->emu_jedec_be_d8_size)
hailfinger6ead7222010-11-01 22:07:04 +0000891 break;
892 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
893 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
894 return 1;
895 }
896 if (readcnt != JEDEC_BE_D8_INSIZE) {
897 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
898 return 1;
899 }
900 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000901 if (offs & (data->emu_jedec_be_d8_size - 1))
hailfingere53f5e42011-02-04 22:52:04 +0000902 msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000903 offs &= ~(data->emu_jedec_be_d8_size - 1);
904 memset(flashchip_contents + offs, 0xff, data->emu_jedec_be_d8_size);
905 data->emu_modified = 1;
hailfinger6ead7222010-11-01 22:07:04 +0000906 break;
907 case JEDEC_CE_60:
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000908 if (!data->emu_jedec_ce_60_size)
hailfinger6ead7222010-11-01 22:07:04 +0000909 break;
910 if (writecnt != JEDEC_CE_60_OUTSIZE) {
911 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
912 return 1;
913 }
914 if (readcnt != JEDEC_CE_60_INSIZE) {
915 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
916 return 1;
917 }
hailfingere53f5e42011-02-04 22:52:04 +0000918 /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
hailfinger6ead7222010-11-01 22:07:04 +0000919 /* emu_jedec_ce_60_size is emu_chip_size. */
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000920 memset(flashchip_contents, 0xff, data->emu_jedec_ce_60_size);
921 data->emu_modified = 1;
hailfinger6ead7222010-11-01 22:07:04 +0000922 break;
923 case JEDEC_CE_C7:
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000924 if (!data->emu_jedec_ce_c7_size)
hailfinger6ead7222010-11-01 22:07:04 +0000925 break;
926 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
927 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
928 return 1;
929 }
930 if (readcnt != JEDEC_CE_C7_INSIZE) {
931 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
932 return 1;
933 }
hailfingere53f5e42011-02-04 22:52:04 +0000934 /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
hailfinger6ead7222010-11-01 22:07:04 +0000935 /* emu_jedec_ce_c7_size is emu_chip_size. */
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000936 memset(flashchip_contents, 0xff, data->emu_jedec_ce_c7_size);
937 data->emu_modified = 1;
hailfinger6ead7222010-11-01 22:07:04 +0000938 break;
Stuart Langley78cacf72020-04-23 11:48:01 +1000939 case JEDEC_SFDP:
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000940 if (data->emu_chip != EMULATE_MACRONIX_MX25L6436)
Stuart Langley78cacf72020-04-23 11:48:01 +1000941 break;
942 if (writecnt < 4)
943 break;
944 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
945
946 /* SFDP expects one dummy byte after the address. */
947 if (writecnt == 4) {
948 /* The dummy byte was not written, make sure it is read instead.
949 * Shifting and shortening the read array does achieve this goal.
950 */
951 readarr++;
952 readcnt--;
953 } else {
954 /* The response is shifted if more than 5 bytes are written, because SFDP data is
955 * already shifted out by the chip while those superfluous bytes are written. */
956 offs += writecnt - 5;
957 }
958
959 /* The SFDP spec implies that the start address of an SFDP read may be truncated to fit in the
960 * SFDP table address space, i.e. the start address may be wrapped around at SFDP table size.
961 * This is a reasonable implementation choice in hardware because it saves a few gates. */
962 if (offs >= sizeof(sfdp_table)) {
963 msg_pdbg("Wrapping the start address around the SFDP table boundary (using 0x%x "
964 "instead of 0x%x).\n", (unsigned int)(offs % sizeof(sfdp_table)), offs);
965 offs %= sizeof(sfdp_table);
966 }
967 toread = min(sizeof(sfdp_table) - offs, readcnt);
968 memcpy(readarr, sfdp_table + offs, toread);
969 if (toread < readcnt)
970 msg_pdbg("Crossing the SFDP table boundary in a single "
971 "continuous chunk produces undefined results "
972 "after that point.\n");
973 break;
hailfinger6ead7222010-11-01 22:07:04 +0000974 default:
975 /* No special response. */
976 break;
977 }
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +1000978 if (writearr[0] != JEDEC_WREN && writearr[0] != JEDEC_EWSR)
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000979 data->emu_status &= ~SPI_SR_WEL;
hailfinger6ead7222010-11-01 22:07:04 +0000980 return 0;
981}
982#endif
983
Lachlan Bishopbf1476e2020-09-10 14:57:05 +1000984static struct emu_data* get_data_from_context(const struct flashctx *flash)
985{
986 if (dummy_buses_supported & (BUS_PARALLEL | BUS_LPC | BUS_FWH))
987 return (struct emu_data *)flash->mst->par.data;
988 else if (dummy_buses_supported & BUS_SPI)
989 return (struct emu_data *)flash->mst->spi.data;
990
991 return NULL; /* buses was set to BUS_NONE. */
992}
993
Stuart langleyc98e43f2020-03-26 20:27:36 +1100994static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
995 unsigned int readcnt,
996 const unsigned char *writearr,
997 unsigned char *readarr)
hailfingerf91e3b52009-05-14 12:59:36 +0000998{
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +1000999 unsigned int i;
Lachlan Bishopbf1476e2020-09-10 14:57:05 +10001000 struct emu_data *emu_data = get_data_from_context(flash);
1001 if (!emu_data) {
1002 msg_perr("No data in flash context!\n");
1003 return 1;
1004 }
hailfingerf91e3b52009-05-14 12:59:36 +00001005
hailfinger50c335f2010-01-09 04:32:23 +00001006 msg_pspew("%s:", __func__);
hailfingerf91e3b52009-05-14 12:59:36 +00001007
hailfinger50c335f2010-01-09 04:32:23 +00001008 msg_pspew(" writing %u bytes:", writecnt);
hailfingerf91e3b52009-05-14 12:59:36 +00001009 for (i = 0; i < writecnt; i++)
hailfinger50c335f2010-01-09 04:32:23 +00001010 msg_pspew(" 0x%02x", writearr[i]);
hailfingerf91e3b52009-05-14 12:59:36 +00001011
hailfinger6ead7222010-11-01 22:07:04 +00001012 /* Response for unknown commands and missing chip is 0xff. */
1013 memset(readarr, 0xff, readcnt);
1014#if EMULATE_SPI_CHIP
Lachlan Bishopbf1476e2020-09-10 14:57:05 +10001015 switch (emu_data->emu_chip) {
hailfinger6ead7222010-11-01 22:07:04 +00001016 case EMULATE_ST_M25P10_RES:
1017 case EMULATE_SST_SST25VF040_REMS:
1018 case EMULATE_SST_SST25VF032B:
Stuart Langley78cacf72020-04-23 11:48:01 +10001019 case EMULATE_MACRONIX_MX25L6436:
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +10001020 case EMULATE_WINBOND_W25Q128FV:
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +08001021 case EMULATE_VARIABLE_SIZE:
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +10001022 if (emulate_spi_chip_response(writecnt, readcnt, writearr,
Lachlan Bishopbf1476e2020-09-10 14:57:05 +10001023 readarr, emu_data)) {
Carl-Daniel Hailfingerc49783d2016-08-05 10:52:06 -07001024 msg_pdbg("Invalid command sent to flash chip!\n");
hailfinger6ead7222010-11-01 22:07:04 +00001025 return 1;
1026 }
1027 break;
1028 default:
1029 break;
1030 }
1031#endif
hailfinger50c335f2010-01-09 04:32:23 +00001032 msg_pspew(" reading %u bytes:", readcnt);
uwe8d342eb2011-07-28 08:13:25 +00001033 for (i = 0; i < readcnt; i++)
hailfinger6ead7222010-11-01 22:07:04 +00001034 msg_pspew(" 0x%02x", readarr[i]);
hailfinger50c335f2010-01-09 04:32:23 +00001035 msg_pspew("\n");
David Hendricks84377002014-09-09 16:09:31 -07001036
Edward O'Callaghan0037f452020-09-17 13:32:00 +10001037 programmer_delay((writecnt + readcnt) * emu_data->delay_us);
hailfingerf91e3b52009-05-14 12:59:36 +00001038 return 0;
1039}
hailfingera8727712010-06-20 10:58:32 +00001040
Stuart langleyc98e43f2020-03-26 20:27:36 +11001041static int dummy_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
hailfingerc7d06c62010-07-14 16:19:05 +00001042{
hailfinger6ead7222010-11-01 22:07:04 +00001043 return spi_write_chunked(flash, buf, start, len,
1044 spi_write_256_chunksize);
hailfingerc7d06c62010-07-14 16:19:05 +00001045}
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +08001046
1047#if EMULATE_CHIP && EMULATE_SPI_CHIP
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001048int probe_variable_size(struct flashctx *flash)
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +08001049{
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +10001050 unsigned int i;
Lachlan Bishopbf1476e2020-09-10 14:57:05 +10001051 const struct emu_data *emu_data = get_data_from_context(flash);
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +08001052
1053 /* Skip the probing if we don't emulate this chip. */
Angel Pons7971a732020-10-17 15:20:27 +02001054 if (!emu_data || emu_data->emu_chip != EMULATE_VARIABLE_SIZE)
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +08001055 return 0;
1056
1057 /*
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001058 * This will break if one day flashctx becomes read-only.
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +08001059 * Once that happens, we need to have special hacks in functions:
1060 *
1061 * erase_and_write_flash() in flashrom.c
1062 * read_flash_to_file()
1063 * handle_romentries()
1064 * ...
1065 *
1066 * Search "total_size * 1024" in code.
1067 */
Lachlan Bishopbf1476e2020-09-10 14:57:05 +10001068 flash->chip->total_size = emu_data->emu_chip_size / 1024;
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +08001069 msg_cdbg("%s: set flash->total_size to %dK bytes.\n", __func__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001070 flash->chip->total_size);
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +08001071
Lachlan Bishopbf1476e2020-09-10 14:57:05 +10001072 if (emu_data->erase_to_zero)
Alan Greendbeec2b2019-09-16 14:36:52 +10001073 flash->chip->feature_bits |= FEATURE_ERASED_ZERO;
David Hendricks0eda2a82014-09-12 16:32:05 -07001074
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +10001075 /* Update the first count of each of the block_erasers. */
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +08001076 for (i = 0; i < NUM_ERASEFUNCTIONS; i++) {
Patrick Georgif3fa2992017-02-02 16:24:44 +01001077 struct block_eraser *eraser = &flash->chip->block_erasers[i];
Edward O'Callaghanec8b0d92020-09-17 17:27:45 +10001078 if (!eraser->block_erase)
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +08001079 break;
1080
Namyoon Woob71a2962020-08-27 16:27:49 -07001081 eraser->eraseblocks[0].count = 1;
Lachlan Bishopbf1476e2020-09-10 14:57:05 +10001082 eraser->eraseblocks[0].size = emu_data->emu_chip_size;
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +08001083 msg_cdbg("%s: eraser.size=%d, .count=%d\n",
1084 __func__, eraser->eraseblocks[0].size,
1085 eraser->eraseblocks[0].count);
1086 }
1087
1088 return 1;
1089}
1090#endif