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