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