dummyflasher.c 25 KB

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