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