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