Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 4 | * Copyright (C) 2009,2010 Carl-Daniel Hailfinger |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 8 | * the Free Software Foundation; version 2 of the License. |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 9 | * |
| 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. |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 16 | #include <string.h> |
| 17 | #include <stdlib.h> |
Carl-Daniel Hailfinger | 1b83be5 | 2012-02-08 23:28:54 +0000 | [diff] [blame] | 18 | #include <stdio.h> |
| 19 | #include <ctype.h> |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 20 | #include <errno.h> |
Nico Huber | ab69629 | 2021-06-09 18:10:07 +0200 | [diff] [blame] | 21 | #include <sys/types.h> |
| 22 | #include <sys/stat.h> |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 23 | #include "flash.h" |
Carl-Daniel Hailfinger | 1b0ba89 | 2010-06-20 10:58:32 +0000 | [diff] [blame] | 24 | #include "chipdrivers.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 25 | #include "programmer.h" |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 26 | |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 27 | #include "spi.h" |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 28 | |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 29 | enum emu_chip { |
| 30 | EMULATE_NONE, |
| 31 | EMULATE_ST_M25P10_RES, |
| 32 | EMULATE_SST_SST25VF040_REMS, |
| 33 | EMULATE_SST_SST25VF032B, |
Stefan Tauner | 0b9df97 | 2012-05-07 22:12:16 +0000 | [diff] [blame] | 34 | EMULATE_MACRONIX_MX25L6436, |
Nico Huber | f9632d8 | 2019-01-20 11:23:49 +0100 | [diff] [blame] | 35 | EMULATE_WINBOND_W25Q128FV, |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 36 | }; |
Stefan Tauner | 0b9df97 | 2012-05-07 22:12:16 +0000 | [diff] [blame] | 37 | |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 38 | struct emu_data { |
| 39 | enum emu_chip emu_chip; |
| 40 | char *emu_persistent_image; |
| 41 | unsigned int emu_chip_size; |
| 42 | int emu_modified; /* is the image modified since reading it? */ |
| 43 | uint8_t emu_status; |
| 44 | unsigned int emu_max_byteprogram_size; |
| 45 | unsigned int emu_max_aai_size; |
| 46 | unsigned int emu_jedec_se_size; |
| 47 | unsigned int emu_jedec_be_52_size; |
| 48 | unsigned int emu_jedec_be_d8_size; |
| 49 | unsigned int emu_jedec_ce_60_size; |
| 50 | unsigned int emu_jedec_ce_c7_size; |
| 51 | unsigned char spi_blacklist[256]; |
| 52 | unsigned char spi_ignorelist[256]; |
| 53 | unsigned int spi_blacklist_size; |
| 54 | unsigned int spi_ignorelist_size; |
Edward O'Callaghan | 9425022 | 2021-05-20 20:34:02 +1000 | [diff] [blame] | 55 | |
| 56 | uint8_t *flashchip_contents; |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 57 | }; |
| 58 | |
Stefan Tauner | 0b9df97 | 2012-05-07 22:12:16 +0000 | [diff] [blame] | 59 | /* A legit complete SFDP table based on the MX25L6436E (rev. 1.8) datasheet. */ |
Stefan Tauner | 67d163d | 2013-01-15 17:37:48 +0000 | [diff] [blame] | 60 | static const uint8_t sfdp_table[] = { |
Stefan Tauner | 0b9df97 | 2012-05-07 22:12:16 +0000 | [diff] [blame] | 61 | 0x53, 0x46, 0x44, 0x50, // @0x00: SFDP signature |
| 62 | 0x00, 0x01, 0x01, 0xFF, // @0x04: revision 1.0, 2 headers |
| 63 | 0x00, 0x00, 0x01, 0x09, // @0x08: JEDEC SFDP header rev. 1.0, 9 DW long |
| 64 | 0x1C, 0x00, 0x00, 0xFF, // @0x0C: PTP0 = 0x1C (instead of 0x30) |
| 65 | 0xC2, 0x00, 0x01, 0x04, // @0x10: Macronix header rev. 1.0, 4 DW long |
| 66 | 0x48, 0x00, 0x00, 0xFF, // @0x14: PTP1 = 0x48 (instead of 0x60) |
| 67 | 0xFF, 0xFF, 0xFF, 0xFF, // @0x18: hole. |
| 68 | 0xE5, 0x20, 0xC9, 0xFF, // @0x1C: SFDP parameter table start |
| 69 | 0xFF, 0xFF, 0xFF, 0x03, // @0x20 |
| 70 | 0x00, 0xFF, 0x08, 0x6B, // @0x24 |
| 71 | 0x08, 0x3B, 0x00, 0xFF, // @0x28 |
| 72 | 0xEE, 0xFF, 0xFF, 0xFF, // @0x2C |
| 73 | 0xFF, 0xFF, 0x00, 0x00, // @0x30 |
| 74 | 0xFF, 0xFF, 0x00, 0xFF, // @0x34 |
| 75 | 0x0C, 0x20, 0x0F, 0x52, // @0x38 |
| 76 | 0x10, 0xD8, 0x00, 0xFF, // @0x3C: SFDP parameter table end |
| 77 | 0xFF, 0xFF, 0xFF, 0xFF, // @0x40: hole. |
| 78 | 0xFF, 0xFF, 0xFF, 0xFF, // @0x44: hole. |
| 79 | 0x00, 0x36, 0x00, 0x27, // @0x48: Macronix parameter table start |
| 80 | 0xF4, 0x4F, 0xFF, 0xFF, // @0x4C |
| 81 | 0xD9, 0xC8, 0xFF, 0xFF, // @0x50 |
| 82 | 0xFF, 0xFF, 0xFF, 0xFF, // @0x54: Macronix parameter table end |
| 83 | }; |
| 84 | |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 85 | |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 86 | static unsigned int spi_write_256_chunksize = 256; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 87 | |
Edward O'Callaghan | 5eca427 | 2020-04-12 17:27:53 +1000 | [diff] [blame] | 88 | static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 89 | const unsigned char *writearr, unsigned char *readarr); |
| 90 | static int dummy_spi_write_256(struct flashctx *flash, const uint8_t *buf, |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 91 | unsigned int start, unsigned int len); |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 92 | static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr); |
| 93 | static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr); |
| 94 | static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr); |
| 95 | static void dummy_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len); |
| 96 | static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr); |
| 97 | static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr); |
| 98 | static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr); |
| 99 | static void dummy_chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len); |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 100 | |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 101 | static struct spi_master spi_master_dummyflasher = { |
Nico Huber | 1cf407b | 2017-11-10 20:18:23 +0100 | [diff] [blame] | 102 | .features = SPI_MASTER_4BA, |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 103 | .max_data_read = MAX_DATA_READ_UNLIMITED, |
| 104 | .max_data_write = MAX_DATA_UNSPECIFIED, |
| 105 | .command = dummy_spi_send_command, |
| 106 | .multicommand = default_spi_send_multicommand, |
| 107 | .read = default_spi_read, |
| 108 | .write_256 = dummy_spi_write_256, |
Nico Huber | 7bca126 | 2012-06-15 22:28:12 +0000 | [diff] [blame] | 109 | .write_aai = default_spi_write_aai, |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 110 | }; |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 111 | |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 112 | static struct par_master par_master_dummy = { |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 113 | .chip_readb = dummy_chip_readb, |
| 114 | .chip_readw = dummy_chip_readw, |
| 115 | .chip_readl = dummy_chip_readl, |
| 116 | .chip_readn = dummy_chip_readn, |
| 117 | .chip_writeb = dummy_chip_writeb, |
| 118 | .chip_writew = dummy_chip_writew, |
| 119 | .chip_writel = dummy_chip_writel, |
| 120 | .chip_writen = dummy_chip_writen, |
| 121 | }; |
| 122 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 123 | static int dummy_shutdown(void *data) |
| 124 | { |
| 125 | msg_pspew("%s\n", __func__); |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 126 | struct emu_data *emu_data = (struct emu_data *)data; |
| 127 | if (emu_data->emu_chip != EMULATE_NONE) { |
| 128 | if (emu_data->emu_persistent_image && emu_data->emu_modified) { |
| 129 | msg_pdbg("Writing %s\n", emu_data->emu_persistent_image); |
Edward O'Callaghan | 9425022 | 2021-05-20 20:34:02 +1000 | [diff] [blame] | 130 | write_buf_to_file(emu_data->flashchip_contents, |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 131 | emu_data->emu_chip_size, |
| 132 | emu_data->emu_persistent_image); |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 133 | } |
Angel Pons | 02b9ae2 | 2021-05-25 12:46:43 +0200 | [diff] [blame] | 134 | free(emu_data->emu_persistent_image); |
Edward O'Callaghan | 9425022 | 2021-05-20 20:34:02 +1000 | [diff] [blame] | 135 | free(emu_data->flashchip_contents); |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 136 | } |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 137 | free(data); |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 138 | return 0; |
| 139 | } |
| 140 | |
Anastasia Klimchuk | 3c55c79 | 2021-05-31 09:42:36 +1000 | [diff] [blame] | 141 | static int init_data(struct emu_data *data, enum chipbustype *dummy_buses_supported) |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 142 | { |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 143 | char *bustext = NULL; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 144 | char *tmp = NULL; |
Nico Huber | 519be66 | 2018-12-23 20:03:35 +0100 | [diff] [blame] | 145 | unsigned int i; |
Anastasia Klimchuk | 3c55c79 | 2021-05-31 09:42:36 +1000 | [diff] [blame] | 146 | char *endptr; |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 147 | char *status = NULL; |
Carl-Daniel Hailfinger | 3504b53 | 2009-06-01 00:02:11 +0000 | [diff] [blame] | 148 | |
Carl-Daniel Hailfinger | 2b6dcb3 | 2010-07-08 10:13:37 +0000 | [diff] [blame] | 149 | bustext = extract_programmer_param("bus"); |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 150 | msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default"); |
| 151 | if (!bustext) |
| 152 | bustext = strdup("parallel+lpc+fwh+spi"); |
Carl-Daniel Hailfinger | 3504b53 | 2009-06-01 00:02:11 +0000 | [diff] [blame] | 153 | /* Convert the parameters to lowercase. */ |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 154 | tolower_string(bustext); |
Carl-Daniel Hailfinger | 3504b53 | 2009-06-01 00:02:11 +0000 | [diff] [blame] | 155 | |
Anastasia Klimchuk | 3c55c79 | 2021-05-31 09:42:36 +1000 | [diff] [blame] | 156 | *dummy_buses_supported = BUS_NONE; |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 157 | if (strstr(bustext, "parallel")) { |
Anastasia Klimchuk | 3c55c79 | 2021-05-31 09:42:36 +1000 | [diff] [blame] | 158 | *dummy_buses_supported |= BUS_PARALLEL; |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 159 | msg_pdbg("Enabling support for %s flash.\n", "parallel"); |
Carl-Daniel Hailfinger | 3504b53 | 2009-06-01 00:02:11 +0000 | [diff] [blame] | 160 | } |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 161 | if (strstr(bustext, "lpc")) { |
Anastasia Klimchuk | 3c55c79 | 2021-05-31 09:42:36 +1000 | [diff] [blame] | 162 | *dummy_buses_supported |= BUS_LPC; |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 163 | msg_pdbg("Enabling support for %s flash.\n", "LPC"); |
Carl-Daniel Hailfinger | 3504b53 | 2009-06-01 00:02:11 +0000 | [diff] [blame] | 164 | } |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 165 | if (strstr(bustext, "fwh")) { |
Anastasia Klimchuk | 3c55c79 | 2021-05-31 09:42:36 +1000 | [diff] [blame] | 166 | *dummy_buses_supported |= BUS_FWH; |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 167 | msg_pdbg("Enabling support for %s flash.\n", "FWH"); |
Carl-Daniel Hailfinger | 3504b53 | 2009-06-01 00:02:11 +0000 | [diff] [blame] | 168 | } |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 169 | if (strstr(bustext, "spi")) { |
Anastasia Klimchuk | 3c55c79 | 2021-05-31 09:42:36 +1000 | [diff] [blame] | 170 | *dummy_buses_supported |= BUS_SPI; |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 171 | msg_pdbg("Enabling support for %s flash.\n", "SPI"); |
Carl-Daniel Hailfinger | 3504b53 | 2009-06-01 00:02:11 +0000 | [diff] [blame] | 172 | } |
Anastasia Klimchuk | 3c55c79 | 2021-05-31 09:42:36 +1000 | [diff] [blame] | 173 | if (*dummy_buses_supported == BUS_NONE) |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 174 | msg_pdbg("Support for all flash bus types disabled.\n"); |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 175 | free(bustext); |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 176 | |
| 177 | tmp = extract_programmer_param("spi_write_256_chunksize"); |
| 178 | if (tmp) { |
Edward O'Callaghan | c785f88 | 2021-05-23 22:14:36 +1000 | [diff] [blame] | 179 | spi_write_256_chunksize = strtoul(tmp, &endptr, 0); |
| 180 | if (*endptr != '\0' || spi_write_256_chunksize < 1) { |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 181 | msg_perr("invalid spi_write_256_chunksize\n"); |
Edward O'Callaghan | c785f88 | 2021-05-23 22:14:36 +1000 | [diff] [blame] | 182 | free(tmp); |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 183 | return 1; |
| 184 | } |
Edward O'Callaghan | c785f88 | 2021-05-23 22:14:36 +1000 | [diff] [blame] | 185 | free(tmp); |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Carl-Daniel Hailfinger | 1b83be5 | 2012-02-08 23:28:54 +0000 | [diff] [blame] | 188 | tmp = extract_programmer_param("spi_blacklist"); |
| 189 | if (tmp) { |
| 190 | i = strlen(tmp); |
| 191 | if (!strncmp(tmp, "0x", 2)) { |
| 192 | i -= 2; |
| 193 | memmove(tmp, tmp + 2, i + 1); |
| 194 | } |
| 195 | if ((i > 512) || (i % 2)) { |
| 196 | msg_perr("Invalid SPI command blacklist length\n"); |
| 197 | free(tmp); |
| 198 | return 1; |
| 199 | } |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 200 | data->spi_blacklist_size = i / 2; |
| 201 | for (i = 0; i < data->spi_blacklist_size * 2; i++) { |
Carl-Daniel Hailfinger | 1b83be5 | 2012-02-08 23:28:54 +0000 | [diff] [blame] | 202 | if (!isxdigit((unsigned char)tmp[i])) { |
| 203 | msg_perr("Invalid char \"%c\" in SPI command " |
| 204 | "blacklist\n", tmp[i]); |
| 205 | free(tmp); |
| 206 | return 1; |
| 207 | } |
| 208 | } |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 209 | for (i = 0; i < data->spi_blacklist_size; i++) { |
Carl-Daniel Hailfinger | 5b55471 | 2012-02-16 01:43:06 +0000 | [diff] [blame] | 210 | unsigned int tmp2; |
| 211 | /* SCNx8 is apparently not supported by MSVC (and thus |
| 212 | * MinGW), so work around it with an extra variable |
| 213 | */ |
| 214 | sscanf(tmp + i * 2, "%2x", &tmp2); |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 215 | data->spi_blacklist[i] = (uint8_t)tmp2; |
Carl-Daniel Hailfinger | 1b83be5 | 2012-02-08 23:28:54 +0000 | [diff] [blame] | 216 | } |
| 217 | msg_pdbg("SPI blacklist is "); |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 218 | for (i = 0; i < data->spi_blacklist_size; i++) |
| 219 | msg_pdbg("%02x ", data->spi_blacklist[i]); |
| 220 | msg_pdbg(", size %u\n", data->spi_blacklist_size); |
Carl-Daniel Hailfinger | 1b83be5 | 2012-02-08 23:28:54 +0000 | [diff] [blame] | 221 | } |
| 222 | free(tmp); |
| 223 | |
| 224 | tmp = extract_programmer_param("spi_ignorelist"); |
| 225 | if (tmp) { |
| 226 | i = strlen(tmp); |
| 227 | if (!strncmp(tmp, "0x", 2)) { |
| 228 | i -= 2; |
| 229 | memmove(tmp, tmp + 2, i + 1); |
| 230 | } |
| 231 | if ((i > 512) || (i % 2)) { |
| 232 | msg_perr("Invalid SPI command ignorelist length\n"); |
| 233 | free(tmp); |
| 234 | return 1; |
| 235 | } |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 236 | data->spi_ignorelist_size = i / 2; |
| 237 | for (i = 0; i < data->spi_ignorelist_size * 2; i++) { |
Carl-Daniel Hailfinger | 1b83be5 | 2012-02-08 23:28:54 +0000 | [diff] [blame] | 238 | if (!isxdigit((unsigned char)tmp[i])) { |
| 239 | msg_perr("Invalid char \"%c\" in SPI command " |
| 240 | "ignorelist\n", tmp[i]); |
| 241 | free(tmp); |
| 242 | return 1; |
| 243 | } |
| 244 | } |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 245 | for (i = 0; i < data->spi_ignorelist_size; i++) { |
Carl-Daniel Hailfinger | 5b55471 | 2012-02-16 01:43:06 +0000 | [diff] [blame] | 246 | unsigned int tmp2; |
| 247 | /* SCNx8 is apparently not supported by MSVC (and thus |
| 248 | * MinGW), so work around it with an extra variable |
| 249 | */ |
| 250 | sscanf(tmp + i * 2, "%2x", &tmp2); |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 251 | data->spi_ignorelist[i] = (uint8_t)tmp2; |
Carl-Daniel Hailfinger | 1b83be5 | 2012-02-08 23:28:54 +0000 | [diff] [blame] | 252 | } |
| 253 | msg_pdbg("SPI ignorelist is "); |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 254 | for (i = 0; i < data->spi_ignorelist_size; i++) |
| 255 | msg_pdbg("%02x ", data->spi_ignorelist[i]); |
| 256 | msg_pdbg(", size %u\n", data->spi_ignorelist_size); |
Carl-Daniel Hailfinger | 1b83be5 | 2012-02-08 23:28:54 +0000 | [diff] [blame] | 257 | } |
| 258 | free(tmp); |
| 259 | |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 260 | tmp = extract_programmer_param("emulate"); |
| 261 | if (!tmp) { |
| 262 | msg_pdbg("Not emulating any flash chip.\n"); |
| 263 | /* Nothing else to do. */ |
Anastasia Klimchuk | 3c55c79 | 2021-05-31 09:42:36 +1000 | [diff] [blame] | 264 | return 0; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 265 | } |
Anastasia Klimchuk | 3c55c79 | 2021-05-31 09:42:36 +1000 | [diff] [blame] | 266 | |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 267 | if (!strcmp(tmp, "M25P10.RES")) { |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 268 | data->emu_chip = EMULATE_ST_M25P10_RES; |
| 269 | data->emu_chip_size = 128 * 1024; |
| 270 | data->emu_max_byteprogram_size = 128; |
| 271 | data->emu_max_aai_size = 0; |
| 272 | data->emu_jedec_se_size = 0; |
| 273 | data->emu_jedec_be_52_size = 0; |
| 274 | data->emu_jedec_be_d8_size = 32 * 1024; |
| 275 | data->emu_jedec_ce_60_size = 0; |
| 276 | data->emu_jedec_ce_c7_size = data->emu_chip_size; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 277 | msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page " |
| 278 | "write)\n"); |
| 279 | } |
| 280 | if (!strcmp(tmp, "SST25VF040.REMS")) { |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 281 | data->emu_chip = EMULATE_SST_SST25VF040_REMS; |
| 282 | data->emu_chip_size = 512 * 1024; |
| 283 | data->emu_max_byteprogram_size = 1; |
| 284 | data->emu_max_aai_size = 0; |
| 285 | data->emu_jedec_se_size = 4 * 1024; |
| 286 | data->emu_jedec_be_52_size = 32 * 1024; |
| 287 | data->emu_jedec_be_d8_size = 0; |
| 288 | data->emu_jedec_ce_60_size = data->emu_chip_size; |
| 289 | data->emu_jedec_ce_c7_size = 0; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 290 | msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, " |
| 291 | "byte write)\n"); |
| 292 | } |
| 293 | if (!strcmp(tmp, "SST25VF032B")) { |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 294 | data->emu_chip = EMULATE_SST_SST25VF032B; |
| 295 | data->emu_chip_size = 4 * 1024 * 1024; |
| 296 | data->emu_max_byteprogram_size = 1; |
| 297 | data->emu_max_aai_size = 2; |
| 298 | data->emu_jedec_se_size = 4 * 1024; |
| 299 | data->emu_jedec_be_52_size = 32 * 1024; |
| 300 | data->emu_jedec_be_d8_size = 64 * 1024; |
| 301 | data->emu_jedec_ce_60_size = data->emu_chip_size; |
| 302 | data->emu_jedec_ce_c7_size = data->emu_chip_size; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 303 | msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI " |
| 304 | "write)\n"); |
| 305 | } |
Stefan Tauner | 0b9df97 | 2012-05-07 22:12:16 +0000 | [diff] [blame] | 306 | if (!strcmp(tmp, "MX25L6436")) { |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 307 | data->emu_chip = EMULATE_MACRONIX_MX25L6436; |
| 308 | data->emu_chip_size = 8 * 1024 * 1024; |
| 309 | data->emu_max_byteprogram_size = 256; |
| 310 | data->emu_max_aai_size = 0; |
| 311 | data->emu_jedec_se_size = 4 * 1024; |
| 312 | data->emu_jedec_be_52_size = 32 * 1024; |
| 313 | data->emu_jedec_be_d8_size = 64 * 1024; |
| 314 | data->emu_jedec_ce_60_size = data->emu_chip_size; |
| 315 | data->emu_jedec_ce_c7_size = data->emu_chip_size; |
Stefan Tauner | 0b9df97 | 2012-05-07 22:12:16 +0000 | [diff] [blame] | 316 | msg_pdbg("Emulating Macronix MX25L6436 SPI flash chip (RDID, " |
| 317 | "SFDP)\n"); |
| 318 | } |
Nico Huber | f9632d8 | 2019-01-20 11:23:49 +0100 | [diff] [blame] | 319 | if (!strcmp(tmp, "W25Q128FV")) { |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 320 | data->emu_chip = EMULATE_WINBOND_W25Q128FV; |
| 321 | data->emu_chip_size = 16 * 1024 * 1024; |
| 322 | data->emu_max_byteprogram_size = 256; |
| 323 | data->emu_max_aai_size = 0; |
| 324 | data->emu_jedec_se_size = 4 * 1024; |
| 325 | data->emu_jedec_be_52_size = 32 * 1024; |
| 326 | data->emu_jedec_be_d8_size = 64 * 1024; |
| 327 | data->emu_jedec_ce_60_size = data->emu_chip_size; |
| 328 | data->emu_jedec_ce_c7_size = data->emu_chip_size; |
Nico Huber | f9632d8 | 2019-01-20 11:23:49 +0100 | [diff] [blame] | 329 | msg_pdbg("Emulating Winbond W25Q128FV SPI flash chip (RDID)\n"); |
| 330 | } |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 331 | if (data->emu_chip == EMULATE_NONE) { |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 332 | msg_perr("Invalid chip specified for emulation: %s\n", tmp); |
| 333 | free(tmp); |
| 334 | return 1; |
| 335 | } |
| 336 | free(tmp); |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 337 | |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 338 | status = extract_programmer_param("spi_status"); |
| 339 | if (status) { |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 340 | errno = 0; |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 341 | data->emu_status = strtoul(status, &endptr, 0); |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 342 | if (errno != 0 || status == endptr) { |
Angel Pons | c248464 | 2021-05-25 13:03:24 +0200 | [diff] [blame] | 343 | free(status); |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 344 | msg_perr("Error: initial status register specified, " |
| 345 | "but the value could not be converted.\n"); |
| 346 | return 1; |
| 347 | } |
Angel Pons | c248464 | 2021-05-25 13:03:24 +0200 | [diff] [blame] | 348 | free(status); |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 349 | msg_pdbg("Initial status register is set to 0x%02x.\n", |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 350 | data->emu_status); |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 351 | } |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 352 | |
Angel Pons | 328898a | 2021-05-25 12:56:18 +0200 | [diff] [blame] | 353 | data->flashchip_contents = malloc(data->emu_chip_size); |
| 354 | if (!data->flashchip_contents) { |
| 355 | msg_perr("Out of memory!\n"); |
| 356 | return 1; |
| 357 | } |
| 358 | |
Anastasia Klimchuk | 3c55c79 | 2021-05-31 09:42:36 +1000 | [diff] [blame] | 359 | |
| 360 | return 0; |
| 361 | } |
| 362 | |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 363 | static int dummy_init(void) |
Anastasia Klimchuk | 3c55c79 | 2021-05-31 09:42:36 +1000 | [diff] [blame] | 364 | { |
Anastasia Klimchuk | 3c55c79 | 2021-05-31 09:42:36 +1000 | [diff] [blame] | 365 | struct stat image_stat; |
Anastasia Klimchuk | 3c55c79 | 2021-05-31 09:42:36 +1000 | [diff] [blame] | 366 | |
| 367 | struct emu_data *data = calloc(1, sizeof(struct emu_data)); |
| 368 | if (!data) { |
| 369 | msg_perr("Out of memory!\n"); |
| 370 | return 1; |
| 371 | } |
| 372 | data->emu_chip = EMULATE_NONE; |
| 373 | spi_master_dummyflasher.data = data; |
| 374 | par_master_dummy.data = data; |
| 375 | |
| 376 | msg_pspew("%s\n", __func__); |
| 377 | |
| 378 | enum chipbustype dummy_buses_supported; |
| 379 | if (init_data(data, &dummy_buses_supported)) { |
| 380 | free(data); |
| 381 | return 1; |
| 382 | } |
| 383 | |
Anastasia Klimchuk | 3c55c79 | 2021-05-31 09:42:36 +1000 | [diff] [blame] | 384 | if (data->emu_chip == EMULATE_NONE) { |
| 385 | msg_pdbg("Not emulating any flash chip.\n"); |
| 386 | /* Nothing else to do. */ |
| 387 | goto dummy_init_out; |
| 388 | } |
| 389 | |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 390 | msg_pdbg("Filling fake flash chip with 0xff, size %i\n", data->emu_chip_size); |
Edward O'Callaghan | 9425022 | 2021-05-20 20:34:02 +1000 | [diff] [blame] | 391 | memset(data->flashchip_contents, 0xff, data->emu_chip_size); |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 392 | |
Stefan Tauner | c2eec2c | 2014-05-03 21:33:01 +0000 | [diff] [blame] | 393 | /* Will be freed by shutdown function if necessary. */ |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 394 | data->emu_persistent_image = extract_programmer_param("image"); |
| 395 | if (!data->emu_persistent_image) { |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 396 | /* Nothing else to do. */ |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 397 | goto dummy_init_out; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 398 | } |
Stefan Tauner | c2eec2c | 2014-05-03 21:33:01 +0000 | [diff] [blame] | 399 | /* We will silently (in default verbosity) ignore the file if it does not exist (yet) or the size does |
| 400 | * not match the emulated chip. */ |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 401 | if (!stat(data->emu_persistent_image, &image_stat)) { |
Stefan Tauner | 23e10b8 | 2016-01-23 16:16:49 +0000 | [diff] [blame] | 402 | msg_pdbg("Found persistent image %s, %jd B ", |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 403 | data->emu_persistent_image, (intmax_t)image_stat.st_size); |
| 404 | if ((uintmax_t)image_stat.st_size == data->emu_chip_size) { |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 405 | msg_pdbg("matches.\n"); |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 406 | msg_pdbg("Reading %s\n", data->emu_persistent_image); |
Edward O'Callaghan | 9425022 | 2021-05-20 20:34:02 +1000 | [diff] [blame] | 407 | if (read_buf_from_file(data->flashchip_contents, data->emu_chip_size, |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 408 | data->emu_persistent_image)) { |
| 409 | msg_perr("Unable to read %s\n", data->emu_persistent_image); |
Angel Pons | 02b9ae2 | 2021-05-25 12:46:43 +0200 | [diff] [blame] | 410 | free(data->emu_persistent_image); |
Edward O'Callaghan | 9425022 | 2021-05-20 20:34:02 +1000 | [diff] [blame] | 411 | free(data->flashchip_contents); |
Anastasia Klimchuk | 3c55c79 | 2021-05-31 09:42:36 +1000 | [diff] [blame] | 412 | free(data); |
Jacob Garber | ca598da | 2019-08-12 10:44:17 -0600 | [diff] [blame] | 413 | return 1; |
| 414 | } |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 415 | } else { |
| 416 | msg_pdbg("doesn't match.\n"); |
| 417 | } |
| 418 | } |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 419 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 420 | dummy_init_out: |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 421 | if (register_shutdown(dummy_shutdown, data)) { |
Angel Pons | 02b9ae2 | 2021-05-25 12:46:43 +0200 | [diff] [blame] | 422 | free(data->emu_persistent_image); |
Edward O'Callaghan | 9425022 | 2021-05-20 20:34:02 +1000 | [diff] [blame] | 423 | free(data->flashchip_contents); |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 424 | free(data); |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 425 | return 1; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 426 | } |
Edward O'Callaghan | 3fa321d | 2021-05-17 20:01:27 +1000 | [diff] [blame] | 427 | if (dummy_buses_supported & BUS_NONSPI) |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 428 | register_par_master(&par_master_dummy, |
Edward O'Callaghan | 3fa321d | 2021-05-17 20:01:27 +1000 | [diff] [blame] | 429 | dummy_buses_supported & BUS_NONSPI); |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 430 | if (dummy_buses_supported & BUS_SPI) |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 431 | register_spi_master(&spi_master_dummyflasher); |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 432 | |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 433 | return 0; |
| 434 | } |
| 435 | |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 436 | static void *dummy_map(const char *descr, uintptr_t phys_addr, size_t len) |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 437 | { |
Stefan Tauner | c2eec2c | 2014-05-03 21:33:01 +0000 | [diff] [blame] | 438 | msg_pspew("%s: Mapping %s, 0x%zx bytes at 0x%0*" PRIxPTR "\n", |
Stefan Tauner | 0554ca5 | 2013-07-25 22:54:25 +0000 | [diff] [blame] | 439 | __func__, descr, len, PRIxPTR_WIDTH, phys_addr); |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 440 | return (void *)phys_addr; |
| 441 | } |
| 442 | |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 443 | static void dummy_unmap(void *virt_addr, size_t len) |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 444 | { |
Stefan Tauner | 0554ca5 | 2013-07-25 22:54:25 +0000 | [diff] [blame] | 445 | msg_pspew("%s: Unmapping 0x%zx bytes at %p\n", __func__, len, virt_addr); |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 448 | static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr) |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 449 | { |
Stefan Tauner | c233375 | 2013-07-13 23:31:37 +0000 | [diff] [blame] | 450 | msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%02x\n", __func__, addr, val); |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 453 | static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr) |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 454 | { |
Stefan Tauner | c233375 | 2013-07-13 23:31:37 +0000 | [diff] [blame] | 455 | msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%04x\n", __func__, addr, val); |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 458 | static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr) |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 459 | { |
Stefan Tauner | c233375 | 2013-07-13 23:31:37 +0000 | [diff] [blame] | 460 | msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%08x\n", __func__, addr, val); |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 463 | static void dummy_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len) |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 464 | { |
| 465 | size_t i; |
Stefan Tauner | 0554ca5 | 2013-07-25 22:54:25 +0000 | [diff] [blame] | 466 | msg_pspew("%s: addr=0x%" PRIxPTR ", len=0x%zx, writing data (hex):", __func__, addr, len); |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 467 | for (i = 0; i < len; i++) { |
| 468 | if ((i % 16) == 0) |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 469 | msg_pspew("\n"); |
| 470 | msg_pspew("%02x ", buf[i]); |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 474 | static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr) |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 475 | { |
Stefan Tauner | c233375 | 2013-07-13 23:31:37 +0000 | [diff] [blame] | 476 | msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xff\n", __func__, addr); |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 477 | return 0xff; |
| 478 | } |
| 479 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 480 | static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr) |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 481 | { |
Stefan Tauner | c233375 | 2013-07-13 23:31:37 +0000 | [diff] [blame] | 482 | msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffff\n", __func__, addr); |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 483 | return 0xffff; |
| 484 | } |
| 485 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 486 | static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr) |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 487 | { |
Stefan Tauner | c233375 | 2013-07-13 23:31:37 +0000 | [diff] [blame] | 488 | msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffffffff\n", __func__, addr); |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 489 | return 0xffffffff; |
| 490 | } |
| 491 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 492 | static void dummy_chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len) |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 493 | { |
Stefan Tauner | 0554ca5 | 2013-07-25 22:54:25 +0000 | [diff] [blame] | 494 | msg_pspew("%s: addr=0x%" PRIxPTR ", len=0x%zx, returning array of 0xff\n", __func__, addr, len); |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 495 | memset(buf, 0xff, len); |
| 496 | return; |
| 497 | } |
| 498 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 499 | static int emulate_spi_chip_response(unsigned int writecnt, |
| 500 | unsigned int readcnt, |
| 501 | const unsigned char *writearr, |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 502 | unsigned char *readarr, |
| 503 | struct emu_data *data) |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 504 | { |
Stefan Tauner | 0b9df97 | 2012-05-07 22:12:16 +0000 | [diff] [blame] | 505 | unsigned int offs, i, toread; |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 506 | static int unsigned aai_offs; |
Carl-Daniel Hailfinger | af2cac0 | 2012-08-30 21:41:00 +0000 | [diff] [blame] | 507 | const unsigned char sst25vf040_rems_response[2] = {0xbf, 0x44}; |
| 508 | const unsigned char sst25vf032b_rems_response[2] = {0xbf, 0x4a}; |
| 509 | const unsigned char mx25l6436_rems_response[2] = {0xc2, 0x16}; |
Nico Huber | f9632d8 | 2019-01-20 11:23:49 +0100 | [diff] [blame] | 510 | const unsigned char w25q128fv_rems_response[2] = {0xef, 0x17}; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 511 | |
| 512 | if (writecnt == 0) { |
| 513 | msg_perr("No command sent to the chip!\n"); |
| 514 | return 1; |
| 515 | } |
Paul Menzel | ac427b2 | 2012-02-16 21:07:07 +0000 | [diff] [blame] | 516 | /* spi_blacklist has precedence over spi_ignorelist. */ |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 517 | for (i = 0; i < data->spi_blacklist_size; i++) { |
| 518 | if (writearr[0] == data->spi_blacklist[i]) { |
Carl-Daniel Hailfinger | 1b83be5 | 2012-02-08 23:28:54 +0000 | [diff] [blame] | 519 | msg_pdbg("Refusing blacklisted SPI command 0x%02x\n", |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 520 | data->spi_blacklist[i]); |
Carl-Daniel Hailfinger | 1b83be5 | 2012-02-08 23:28:54 +0000 | [diff] [blame] | 521 | return SPI_INVALID_OPCODE; |
| 522 | } |
| 523 | } |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 524 | for (i = 0; i < data->spi_ignorelist_size; i++) { |
| 525 | if (writearr[0] == data->spi_ignorelist[i]) { |
Carl-Daniel Hailfinger | 1b83be5 | 2012-02-08 23:28:54 +0000 | [diff] [blame] | 526 | msg_cdbg("Ignoring ignorelisted SPI command 0x%02x\n", |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 527 | data->spi_ignorelist[i]); |
Carl-Daniel Hailfinger | 1b83be5 | 2012-02-08 23:28:54 +0000 | [diff] [blame] | 528 | /* Return success because the command does not fail, |
| 529 | * it is simply ignored. |
| 530 | */ |
| 531 | return 0; |
| 532 | } |
| 533 | } |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 534 | |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 535 | if (data->emu_max_aai_size && (data->emu_status & SPI_SR_AAI)) { |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 536 | if (writearr[0] != JEDEC_AAI_WORD_PROGRAM && |
| 537 | writearr[0] != JEDEC_WRDI && |
| 538 | writearr[0] != JEDEC_RDSR) { |
| 539 | msg_perr("Forbidden opcode (0x%02x) attempted during " |
| 540 | "AAI sequence!\n", writearr[0]); |
| 541 | return 0; |
| 542 | } |
| 543 | } |
| 544 | |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 545 | switch (writearr[0]) { |
| 546 | case JEDEC_RES: |
Carl-Daniel Hailfinger | af2cac0 | 2012-08-30 21:41:00 +0000 | [diff] [blame] | 547 | if (writecnt < JEDEC_RES_OUTSIZE) |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 548 | break; |
Carl-Daniel Hailfinger | af2cac0 | 2012-08-30 21:41:00 +0000 | [diff] [blame] | 549 | /* offs calculation is only needed for SST chips which treat RES like REMS. */ |
| 550 | offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; |
| 551 | offs += writecnt - JEDEC_REMS_OUTSIZE; |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 552 | switch (data->emu_chip) { |
Carl-Daniel Hailfinger | af2cac0 | 2012-08-30 21:41:00 +0000 | [diff] [blame] | 553 | case EMULATE_ST_M25P10_RES: |
| 554 | if (readcnt > 0) |
| 555 | memset(readarr, 0x10, readcnt); |
| 556 | break; |
| 557 | case EMULATE_SST_SST25VF040_REMS: |
| 558 | for (i = 0; i < readcnt; i++) |
| 559 | readarr[i] = sst25vf040_rems_response[(offs + i) % 2]; |
| 560 | break; |
| 561 | case EMULATE_SST_SST25VF032B: |
| 562 | for (i = 0; i < readcnt; i++) |
| 563 | readarr[i] = sst25vf032b_rems_response[(offs + i) % 2]; |
| 564 | break; |
| 565 | case EMULATE_MACRONIX_MX25L6436: |
| 566 | if (readcnt > 0) |
| 567 | memset(readarr, 0x16, readcnt); |
| 568 | break; |
Nico Huber | f9632d8 | 2019-01-20 11:23:49 +0100 | [diff] [blame] | 569 | case EMULATE_WINBOND_W25Q128FV: |
| 570 | if (readcnt > 0) |
| 571 | memset(readarr, 0x17, readcnt); |
| 572 | break; |
Carl-Daniel Hailfinger | af2cac0 | 2012-08-30 21:41:00 +0000 | [diff] [blame] | 573 | default: /* ignore */ |
| 574 | break; |
| 575 | } |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 576 | break; |
| 577 | case JEDEC_REMS: |
Carl-Daniel Hailfinger | af2cac0 | 2012-08-30 21:41:00 +0000 | [diff] [blame] | 578 | /* REMS response has wraparound and uses an address parameter. */ |
| 579 | if (writecnt < JEDEC_REMS_OUTSIZE) |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 580 | break; |
Carl-Daniel Hailfinger | af2cac0 | 2012-08-30 21:41:00 +0000 | [diff] [blame] | 581 | offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; |
| 582 | offs += writecnt - JEDEC_REMS_OUTSIZE; |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 583 | switch (data->emu_chip) { |
Carl-Daniel Hailfinger | af2cac0 | 2012-08-30 21:41:00 +0000 | [diff] [blame] | 584 | case EMULATE_SST_SST25VF040_REMS: |
| 585 | for (i = 0; i < readcnt; i++) |
| 586 | readarr[i] = sst25vf040_rems_response[(offs + i) % 2]; |
| 587 | break; |
| 588 | case EMULATE_SST_SST25VF032B: |
| 589 | for (i = 0; i < readcnt; i++) |
| 590 | readarr[i] = sst25vf032b_rems_response[(offs + i) % 2]; |
| 591 | break; |
| 592 | case EMULATE_MACRONIX_MX25L6436: |
| 593 | for (i = 0; i < readcnt; i++) |
| 594 | readarr[i] = mx25l6436_rems_response[(offs + i) % 2]; |
| 595 | break; |
Nico Huber | f9632d8 | 2019-01-20 11:23:49 +0100 | [diff] [blame] | 596 | case EMULATE_WINBOND_W25Q128FV: |
| 597 | for (i = 0; i < readcnt; i++) |
| 598 | readarr[i] = w25q128fv_rems_response[(offs + i) % 2]; |
| 599 | break; |
Carl-Daniel Hailfinger | af2cac0 | 2012-08-30 21:41:00 +0000 | [diff] [blame] | 600 | default: /* ignore */ |
| 601 | break; |
| 602 | } |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 603 | break; |
| 604 | case JEDEC_RDID: |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 605 | switch (data->emu_chip) { |
Stefan Tauner | 0b9df97 | 2012-05-07 22:12:16 +0000 | [diff] [blame] | 606 | case EMULATE_SST_SST25VF032B: |
| 607 | if (readcnt > 0) |
| 608 | readarr[0] = 0xbf; |
| 609 | if (readcnt > 1) |
| 610 | readarr[1] = 0x25; |
| 611 | if (readcnt > 2) |
| 612 | readarr[2] = 0x4a; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 613 | break; |
Stefan Tauner | 0b9df97 | 2012-05-07 22:12:16 +0000 | [diff] [blame] | 614 | case EMULATE_MACRONIX_MX25L6436: |
| 615 | if (readcnt > 0) |
| 616 | readarr[0] = 0xc2; |
| 617 | if (readcnt > 1) |
| 618 | readarr[1] = 0x20; |
| 619 | if (readcnt > 2) |
| 620 | readarr[2] = 0x17; |
| 621 | break; |
Nico Huber | f9632d8 | 2019-01-20 11:23:49 +0100 | [diff] [blame] | 622 | case EMULATE_WINBOND_W25Q128FV: |
| 623 | if (readcnt > 0) |
| 624 | readarr[0] = 0xef; |
| 625 | if (readcnt > 1) |
| 626 | readarr[1] = 0x40; |
| 627 | if (readcnt > 2) |
| 628 | readarr[2] = 0x18; |
| 629 | break; |
Stefan Tauner | 0b9df97 | 2012-05-07 22:12:16 +0000 | [diff] [blame] | 630 | default: /* ignore */ |
| 631 | break; |
| 632 | } |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 633 | break; |
Stefan Tauner | 0b9df97 | 2012-05-07 22:12:16 +0000 | [diff] [blame] | 634 | case JEDEC_RDSR: |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 635 | memset(readarr, data->emu_status, readcnt); |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 636 | break; |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 637 | /* FIXME: this should be chip-specific. */ |
| 638 | case JEDEC_EWSR: |
| 639 | case JEDEC_WREN: |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 640 | data->emu_status |= SPI_SR_WEL; |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 641 | break; |
| 642 | case JEDEC_WRSR: |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 643 | if (!(data->emu_status & SPI_SR_WEL)) { |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 644 | msg_perr("WRSR attempted, but WEL is 0!\n"); |
| 645 | break; |
| 646 | } |
| 647 | /* FIXME: add some reasonable simulation of the busy flag */ |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 648 | data->emu_status = writearr[1] & ~SPI_SR_WIP; |
| 649 | msg_pdbg2("WRSR wrote 0x%02x.\n", data->emu_status); |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 650 | break; |
| 651 | case JEDEC_READ: |
| 652 | offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; |
| 653 | /* Truncate to emu_chip_size. */ |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 654 | offs %= data->emu_chip_size; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 655 | if (readcnt > 0) |
Edward O'Callaghan | 9425022 | 2021-05-20 20:34:02 +1000 | [diff] [blame] | 656 | memcpy(readarr, data->flashchip_contents + offs, readcnt); |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 657 | break; |
| 658 | case JEDEC_BYTE_PROGRAM: |
| 659 | offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; |
| 660 | /* Truncate to emu_chip_size. */ |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 661 | offs %= data->emu_chip_size; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 662 | if (writecnt < 5) { |
| 663 | msg_perr("BYTE PROGRAM size too short!\n"); |
| 664 | return 1; |
| 665 | } |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 666 | if (writecnt - 4 > data->emu_max_byteprogram_size) { |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 667 | msg_perr("Max BYTE PROGRAM size exceeded!\n"); |
| 668 | return 1; |
| 669 | } |
Edward O'Callaghan | 9425022 | 2021-05-20 20:34:02 +1000 | [diff] [blame] | 670 | memcpy(data->flashchip_contents + offs, writearr + 4, writecnt - 4); |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 671 | data->emu_modified = 1; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 672 | break; |
| 673 | case JEDEC_AAI_WORD_PROGRAM: |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 674 | if (!data->emu_max_aai_size) |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 675 | break; |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 676 | if (!(data->emu_status & SPI_SR_AAI)) { |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 677 | if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) { |
| 678 | msg_perr("Initial AAI WORD PROGRAM size too " |
| 679 | "short!\n"); |
| 680 | return 1; |
| 681 | } |
| 682 | if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) { |
| 683 | msg_perr("Initial AAI WORD PROGRAM size too " |
| 684 | "long!\n"); |
| 685 | return 1; |
| 686 | } |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 687 | data->emu_status |= SPI_SR_AAI; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 688 | aai_offs = writearr[1] << 16 | writearr[2] << 8 | |
| 689 | writearr[3]; |
| 690 | /* Truncate to emu_chip_size. */ |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 691 | aai_offs %= data->emu_chip_size; |
Edward O'Callaghan | 9425022 | 2021-05-20 20:34:02 +1000 | [diff] [blame] | 692 | memcpy(data->flashchip_contents + aai_offs, writearr + 4, 2); |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 693 | aai_offs += 2; |
| 694 | } else { |
| 695 | if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) { |
| 696 | msg_perr("Continuation AAI WORD PROGRAM size " |
| 697 | "too short!\n"); |
| 698 | return 1; |
| 699 | } |
| 700 | if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) { |
| 701 | msg_perr("Continuation AAI WORD PROGRAM size " |
| 702 | "too long!\n"); |
| 703 | return 1; |
| 704 | } |
Edward O'Callaghan | 9425022 | 2021-05-20 20:34:02 +1000 | [diff] [blame] | 705 | memcpy(data->flashchip_contents + aai_offs, writearr + 1, 2); |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 706 | aai_offs += 2; |
| 707 | } |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 708 | data->emu_modified = 1; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 709 | break; |
| 710 | case JEDEC_WRDI: |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 711 | if (data->emu_max_aai_size) |
| 712 | data->emu_status &= ~SPI_SR_AAI; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 713 | break; |
| 714 | case JEDEC_SE: |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 715 | if (!data->emu_jedec_se_size) |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 716 | break; |
| 717 | if (writecnt != JEDEC_SE_OUTSIZE) { |
| 718 | msg_perr("SECTOR ERASE 0x20 outsize invalid!\n"); |
| 719 | return 1; |
| 720 | } |
| 721 | if (readcnt != JEDEC_SE_INSIZE) { |
| 722 | msg_perr("SECTOR ERASE 0x20 insize invalid!\n"); |
| 723 | return 1; |
| 724 | } |
| 725 | offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 726 | if (offs & (data->emu_jedec_se_size - 1)) |
Carl-Daniel Hailfinger | 146b77d | 2011-02-04 22:52:04 +0000 | [diff] [blame] | 727 | msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs); |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 728 | offs &= ~(data->emu_jedec_se_size - 1); |
Edward O'Callaghan | 9425022 | 2021-05-20 20:34:02 +1000 | [diff] [blame] | 729 | memset(data->flashchip_contents + offs, 0xff, data->emu_jedec_se_size); |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 730 | data->emu_modified = 1; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 731 | break; |
| 732 | case JEDEC_BE_52: |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 733 | if (!data->emu_jedec_be_52_size) |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 734 | break; |
| 735 | if (writecnt != JEDEC_BE_52_OUTSIZE) { |
| 736 | msg_perr("BLOCK ERASE 0x52 outsize invalid!\n"); |
| 737 | return 1; |
| 738 | } |
| 739 | if (readcnt != JEDEC_BE_52_INSIZE) { |
| 740 | msg_perr("BLOCK ERASE 0x52 insize invalid!\n"); |
| 741 | return 1; |
| 742 | } |
| 743 | offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 744 | if (offs & (data->emu_jedec_be_52_size - 1)) |
Carl-Daniel Hailfinger | 146b77d | 2011-02-04 22:52:04 +0000 | [diff] [blame] | 745 | msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs); |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 746 | offs &= ~(data->emu_jedec_be_52_size - 1); |
Edward O'Callaghan | 9425022 | 2021-05-20 20:34:02 +1000 | [diff] [blame] | 747 | memset(data->flashchip_contents + offs, 0xff, data->emu_jedec_be_52_size); |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 748 | data->emu_modified = 1; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 749 | break; |
| 750 | case JEDEC_BE_D8: |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 751 | if (!data->emu_jedec_be_d8_size) |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 752 | break; |
| 753 | if (writecnt != JEDEC_BE_D8_OUTSIZE) { |
| 754 | msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n"); |
| 755 | return 1; |
| 756 | } |
| 757 | if (readcnt != JEDEC_BE_D8_INSIZE) { |
| 758 | msg_perr("BLOCK ERASE 0xd8 insize invalid!\n"); |
| 759 | return 1; |
| 760 | } |
| 761 | offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 762 | if (offs & (data->emu_jedec_be_d8_size - 1)) |
Carl-Daniel Hailfinger | 146b77d | 2011-02-04 22:52:04 +0000 | [diff] [blame] | 763 | msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs); |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 764 | offs &= ~(data->emu_jedec_be_d8_size - 1); |
Edward O'Callaghan | 9425022 | 2021-05-20 20:34:02 +1000 | [diff] [blame] | 765 | memset(data->flashchip_contents + offs, 0xff, data->emu_jedec_be_d8_size); |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 766 | data->emu_modified = 1; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 767 | break; |
| 768 | case JEDEC_CE_60: |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 769 | if (!data->emu_jedec_ce_60_size) |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 770 | break; |
| 771 | if (writecnt != JEDEC_CE_60_OUTSIZE) { |
| 772 | msg_perr("CHIP ERASE 0x60 outsize invalid!\n"); |
| 773 | return 1; |
| 774 | } |
| 775 | if (readcnt != JEDEC_CE_60_INSIZE) { |
| 776 | msg_perr("CHIP ERASE 0x60 insize invalid!\n"); |
| 777 | return 1; |
| 778 | } |
Carl-Daniel Hailfinger | 146b77d | 2011-02-04 22:52:04 +0000 | [diff] [blame] | 779 | /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */ |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 780 | /* emu_jedec_ce_60_size is emu_chip_size. */ |
Edward O'Callaghan | 9425022 | 2021-05-20 20:34:02 +1000 | [diff] [blame] | 781 | memset(data->flashchip_contents, 0xff, data->emu_jedec_ce_60_size); |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 782 | data->emu_modified = 1; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 783 | break; |
| 784 | case JEDEC_CE_C7: |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 785 | if (!data->emu_jedec_ce_c7_size) |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 786 | break; |
| 787 | if (writecnt != JEDEC_CE_C7_OUTSIZE) { |
| 788 | msg_perr("CHIP ERASE 0xc7 outsize invalid!\n"); |
| 789 | return 1; |
| 790 | } |
| 791 | if (readcnt != JEDEC_CE_C7_INSIZE) { |
| 792 | msg_perr("CHIP ERASE 0xc7 insize invalid!\n"); |
| 793 | return 1; |
| 794 | } |
Carl-Daniel Hailfinger | 146b77d | 2011-02-04 22:52:04 +0000 | [diff] [blame] | 795 | /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */ |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 796 | /* emu_jedec_ce_c7_size is emu_chip_size. */ |
Edward O'Callaghan | 9425022 | 2021-05-20 20:34:02 +1000 | [diff] [blame] | 797 | memset(data->flashchip_contents, 0xff, data->emu_jedec_ce_c7_size); |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 798 | data->emu_modified = 1; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 799 | break; |
Stefan Tauner | 0b9df97 | 2012-05-07 22:12:16 +0000 | [diff] [blame] | 800 | case JEDEC_SFDP: |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 801 | if (data->emu_chip != EMULATE_MACRONIX_MX25L6436) |
Stefan Tauner | 0b9df97 | 2012-05-07 22:12:16 +0000 | [diff] [blame] | 802 | break; |
| 803 | if (writecnt < 4) |
| 804 | break; |
| 805 | offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; |
| 806 | |
| 807 | /* SFDP expects one dummy byte after the address. */ |
| 808 | if (writecnt == 4) { |
| 809 | /* The dummy byte was not written, make sure it is read instead. |
| 810 | * Shifting and shortening the read array does achieve this goal. |
| 811 | */ |
| 812 | readarr++; |
| 813 | readcnt--; |
| 814 | } else { |
| 815 | /* The response is shifted if more than 5 bytes are written, because SFDP data is |
| 816 | * already shifted out by the chip while those superfluous bytes are written. */ |
| 817 | offs += writecnt - 5; |
| 818 | } |
| 819 | |
| 820 | /* The SFDP spec implies that the start address of an SFDP read may be truncated to fit in the |
| 821 | * SFDP table address space, i.e. the start address may be wrapped around at SFDP table size. |
| 822 | * This is a reasonable implementation choice in hardware because it saves a few gates. */ |
| 823 | if (offs >= sizeof(sfdp_table)) { |
| 824 | msg_pdbg("Wrapping the start address around the SFDP table boundary (using 0x%x " |
| 825 | "instead of 0x%x).\n", (unsigned int)(offs % sizeof(sfdp_table)), offs); |
| 826 | offs %= sizeof(sfdp_table); |
| 827 | } |
| 828 | toread = min(sizeof(sfdp_table) - offs, readcnt); |
| 829 | memcpy(readarr, sfdp_table + offs, toread); |
| 830 | if (toread < readcnt) |
| 831 | msg_pdbg("Crossing the SFDP table boundary in a single " |
| 832 | "continuous chunk produces undefined results " |
| 833 | "after that point.\n"); |
| 834 | break; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 835 | default: |
| 836 | /* No special response. */ |
| 837 | break; |
| 838 | } |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 839 | if (writearr[0] != JEDEC_WREN && writearr[0] != JEDEC_EWSR) |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 840 | data->emu_status &= ~SPI_SR_WEL; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 841 | return 0; |
| 842 | } |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 843 | |
Edward O'Callaghan | 5eca427 | 2020-04-12 17:27:53 +1000 | [diff] [blame] | 844 | static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt, |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 845 | unsigned int readcnt, |
| 846 | const unsigned char *writearr, |
| 847 | unsigned char *readarr) |
Carl-Daniel Hailfinger | bfe2e0c | 2009-05-14 12:59:36 +0000 | [diff] [blame] | 848 | { |
Nico Huber | 519be66 | 2018-12-23 20:03:35 +0100 | [diff] [blame] | 849 | unsigned int i; |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 850 | struct emu_data *emu_data = flash->mst->spi.data; |
| 851 | if (!emu_data) { |
| 852 | msg_perr("No data in flash context!\n"); |
| 853 | return 1; |
| 854 | } |
Carl-Daniel Hailfinger | bfe2e0c | 2009-05-14 12:59:36 +0000 | [diff] [blame] | 855 | |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 856 | msg_pspew("%s:", __func__); |
Carl-Daniel Hailfinger | bfe2e0c | 2009-05-14 12:59:36 +0000 | [diff] [blame] | 857 | |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 858 | msg_pspew(" writing %u bytes:", writecnt); |
Carl-Daniel Hailfinger | bfe2e0c | 2009-05-14 12:59:36 +0000 | [diff] [blame] | 859 | for (i = 0; i < writecnt; i++) |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 860 | msg_pspew(" 0x%02x", writearr[i]); |
Carl-Daniel Hailfinger | bfe2e0c | 2009-05-14 12:59:36 +0000 | [diff] [blame] | 861 | |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 862 | /* Response for unknown commands and missing chip is 0xff. */ |
| 863 | memset(readarr, 0xff, readcnt); |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 864 | switch (emu_data->emu_chip) { |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 865 | case EMULATE_ST_M25P10_RES: |
| 866 | case EMULATE_SST_SST25VF040_REMS: |
| 867 | case EMULATE_SST_SST25VF032B: |
Stefan Tauner | 0b9df97 | 2012-05-07 22:12:16 +0000 | [diff] [blame] | 868 | case EMULATE_MACRONIX_MX25L6436: |
Nico Huber | f9632d8 | 2019-01-20 11:23:49 +0100 | [diff] [blame] | 869 | case EMULATE_WINBOND_W25Q128FV: |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 870 | if (emulate_spi_chip_response(writecnt, readcnt, writearr, |
Lachlan Bishop | c753c40 | 2020-09-10 14:57:05 +1000 | [diff] [blame] | 871 | readarr, emu_data)) { |
Carl-Daniel Hailfinger | 1b83be5 | 2012-02-08 23:28:54 +0000 | [diff] [blame] | 872 | msg_pdbg("Invalid command sent to flash chip!\n"); |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 873 | return 1; |
| 874 | } |
| 875 | break; |
| 876 | default: |
| 877 | break; |
| 878 | } |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 879 | msg_pspew(" reading %u bytes:", readcnt); |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 880 | for (i = 0; i < readcnt; i++) |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 881 | msg_pspew(" 0x%02x", readarr[i]); |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 882 | msg_pspew("\n"); |
Carl-Daniel Hailfinger | bfe2e0c | 2009-05-14 12:59:36 +0000 | [diff] [blame] | 883 | return 0; |
| 884 | } |
Carl-Daniel Hailfinger | 1b0ba89 | 2010-06-20 10:58:32 +0000 | [diff] [blame] | 885 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 886 | static int dummy_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 887 | { |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 888 | return spi_write_chunked(flash, buf, start, len, |
| 889 | spi_write_256_chunksize); |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 890 | } |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 891 | |
| 892 | const struct programmer_entry programmer_dummy = { |
| 893 | .name = "dummy", |
| 894 | .type = OTHER, |
| 895 | /* FIXME */ |
| 896 | .devs.note = "Dummy device, does nothing and logs all accesses\n", |
| 897 | .init = dummy_init, |
| 898 | .map_flash_region = dummy_map, |
| 899 | .unmap_flash_region = dummy_unmap, |
| 900 | .delay = internal_delay, |
| 901 | }; |