spiflash.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. #include "common.h"
  2. #include "zlib.h"
  3. #include "spiflash.h"
  4. #include "esp.h"
  5. #if 1
  6. # include "console.h"
  7. # define MSG(...) con_printf(__VA_ARGS__)
  8. #else
  9. # define MSG(...) ((void)0)
  10. #endif
  11. struct spz_stream;
  12. typedef struct spz_stream spz_stream;
  13. #define NBUF 4
  14. struct spz_stream {
  15. z_stream zs;
  16. const struct spiflash *flash;
  17. uint8_t *optr; /* Output data pointer into obuf */
  18. /* Note: available output data ends at zs->next_out */
  19. union {
  20. uint8_t *bufs[NBUF];
  21. struct {
  22. uint8_t *ibuf; /* Input buffer if compressed */
  23. uint8_t *obuf; /* Output buffer */
  24. uint8_t *dbuf; /* Block data buffer */
  25. uint8_t *vbuf; /* Readback/verify buffer */
  26. };
  27. };
  28. struct fw_header header; /* Header of currently processed chunk */
  29. int err; /* Error code to return */
  30. bool eoi; /* Reached end of input */
  31. bool cleanup; /* Call inflateEnd() */
  32. };
  33. static void *spz_malloc(spz_stream *spz, size_t bytes)
  34. {
  35. void *p = malloc(bytes);
  36. if (!p && !spz->err) {
  37. spz->err = Z_MEM_ERROR;
  38. }
  39. return p;
  40. }
  41. static int spiflash_read_data(spz_stream *spz, void *buf, unsigned int len)
  42. {
  43. uint8_t *p = buf;
  44. while (len) {
  45. unsigned int avail = spz->zs.next_out - spz->optr;
  46. if (spz->err)
  47. break;
  48. if (avail) {
  49. if (avail > len)
  50. avail = len;
  51. memcpy(p, spz->optr, avail);
  52. p += avail;
  53. spz->optr += avail;
  54. len -= avail;
  55. } else {
  56. spz->optr = spz->zs.next_out = spz->obuf;
  57. spz->zs.avail_out = SPIFLASH_BLOCK_SIZE;
  58. while (spz->zs.avail_out) {
  59. if (!spz->zs.avail_in && !spz->eoi) {
  60. int (*read_data)(void *, void *, unsigned int);
  61. read_data = spz->flash->read_data;
  62. spz->zs.next_in = spz->ibuf;
  63. int rlen = read_data(spz->flash->cookie,
  64. spz->ibuf, SPIFLASH_BLOCK_SIZE);
  65. spz->eoi = rlen < SPIFLASH_BLOCK_SIZE;
  66. if (rlen < 0) {
  67. if (!spz->err)
  68. spz->err = rlen;
  69. rlen = 0;
  70. }
  71. spz->zs.avail_in = rlen;
  72. }
  73. int rv = inflate(&spz->zs, Z_SYNC_FLUSH);
  74. if (rv == Z_OK || (rv == Z_BUF_ERROR && !spz->eoi))
  75. continue;
  76. spz->eoi = true;
  77. if (rv != Z_STREAM_END && !spz->err)
  78. spz->err = rv;
  79. break;
  80. }
  81. }
  82. }
  83. return p - (uint8_t *)buf;
  84. }
  85. /*
  86. * spz needs to be initialized to zero except the flash, zs.next_in,
  87. * and zs.avail_in fields.
  88. */
  89. static int spiflash_data_init(spz_stream *spz)
  90. {
  91. int rv = Z_OK;
  92. uint8_t *rdbuf = NULL;
  93. int rlen;
  94. uint32_t header_crc;
  95. for (int i = 0; i < NBUF; i++) {
  96. spz->bufs[i] = spz_malloc(spz, SPIFLASH_BLOCK_SIZE);
  97. if (!spz->bufs[i])
  98. goto err;
  99. }
  100. spz->eoi = !spz->flash->read_data;
  101. /* gzip, max window size */
  102. rv = inflateInit2(&spz->zs, 16 + 15);
  103. if (rv != Z_OK && rv != Z_STREAM_END) {
  104. spz->err = rv;
  105. goto err;
  106. }
  107. spz->cleanup = true;
  108. err:
  109. return spz->err;
  110. }
  111. static int spiflash_data_cleanup(spz_stream *spz)
  112. {
  113. int err = 0;
  114. if (!spz)
  115. return 0;
  116. err = spz->err;
  117. if (spz->cleanup)
  118. inflateEnd(&spz->zs);
  119. for (int i = 0; i < NBUF; i++) {
  120. if (spz->bufs[i])
  121. free(spz->bufs[i]);
  122. }
  123. return err;
  124. }
  125. /*
  126. * Set up a command header with an address according to the SPI
  127. * addressing mode. Returns a pointer to the first byte past the
  128. * address.
  129. */
  130. static void *spiflash_setup_addrcmd(const struct spiflash *flash,
  131. uint32_t addr,
  132. uint8_t cmd24, uint8_t cmd32,
  133. void *cmdbuf)
  134. {
  135. enum spiflash_addr_mode mode = flash->param->addr;
  136. uint8_t *cmd = cmdbuf;
  137. if (!mode)
  138. mode = addr < (1 << 24) ? SPIFLASH_ADDR_24BIT : SPIFLASH_ADDR_32BIT;
  139. if (mode == SPIFLASH_ADDR_24BIT) {
  140. *cmd++ = cmd24;
  141. } else {
  142. *cmd++ = cmd32;
  143. *cmd++ = addr >> 24;
  144. }
  145. *cmd++ = addr >> 16;
  146. *cmd++ = addr >> 8;
  147. *cmd++ = addr;
  148. return cmd;
  149. }
  150. static int spiflash_get_status(const struct spiflash *flash,
  151. uint8_t cmd, uint8_t *sr)
  152. {
  153. return flash->ops->spi_read(flash->cookie, &cmd, 1, sr, 1,
  154. flash->param->tshsl);
  155. }
  156. /* This needs a timeout function */
  157. static int spiflash_wait_status(const struct spiflash *flash,
  158. int delay, uint8_t mask, uint8_t val)
  159. {
  160. uint8_t sr1;
  161. int rv;
  162. do {
  163. if (flash->ops->yield)
  164. flash->ops->yield(flash->cookie, delay);
  165. rv = spiflash_get_status(flash, ROM_READ_SR1, &sr1);
  166. if (rv)
  167. return rv;
  168. } while ((sr1 & mask) != val); /* Waiting... */
  169. return 0;
  170. }
  171. int spiflash_read(const struct spiflash *flash,
  172. uint32_t addr, void *buffer, size_t len)
  173. {
  174. uint8_t cmdbuf[6];
  175. uint8_t *cmd;
  176. cmd = spiflash_setup_addrcmd(flash, addr,
  177. ROM_FAST_READ, ROM_FAST_READ_32BIT,
  178. cmdbuf);
  179. *cmd++ = 0; /* Dummy cycles */
  180. return flash->ops->spi_read(flash->cookie, cmdbuf, cmd - cmdbuf,
  181. buffer, len, flash->param->tshsl1);
  182. }
  183. static int spiflash_simple_command(const struct spiflash *flash, uint8_t cmd)
  184. {
  185. return flash->ops->spi_write(flash->cookie, &cmd, 1, NULL, 0,
  186. flash->param->tshsl);
  187. }
  188. static int spiflash_write_enable(const struct spiflash *flash)
  189. {
  190. uint8_t sr1;
  191. int rv;
  192. rv = spiflash_wait_status(flash, 0, 1, 0);
  193. if (rv)
  194. return rv;
  195. rv = spiflash_simple_command(flash, ROM_WRITE_ENABLE);
  196. if (rv)
  197. return rv;
  198. return spiflash_wait_status(flash, 0, 3, 2);
  199. }
  200. static int spiflash_program(const struct spiflash *flash,
  201. uint32_t addr, const void *buffer, size_t len)
  202. {
  203. uint8_t cmdbuf[5];
  204. uint8_t *cmd;
  205. int rv;
  206. rv = spiflash_write_enable(flash);
  207. if (rv)
  208. return rv;
  209. cmd = spiflash_setup_addrcmd(flash, addr,
  210. ROM_PAGE_PROGRAM, ROM_PAGE_PROGRAM_32BIT,
  211. cmdbuf);
  212. rv = flash->ops->spi_write(flash->cookie, cmdbuf, cmd - cmdbuf,
  213. buffer, len, flash->param->tshsl2);
  214. if (rv)
  215. return rv;
  216. return spiflash_wait_status(flash, flash->param->tpp, 3, 0);
  217. }
  218. /*
  219. * Erase up to (long bits) sectors, using block erase if possible.
  220. */
  221. static int spiflash_erase(const struct spiflash *flash,
  222. uint32_t addr, unsigned long sector_mask)
  223. {
  224. uint8_t cmdbuf[5];
  225. uint8_t *cmd;
  226. uint8_t cmd24, cmd32;
  227. uint32_t erasesize;
  228. int rv;
  229. int delay;
  230. const uint32_t block_mask = SPIFLASH_BLOCK_SIZE - 1;
  231. const unsigned long block_sector_mask
  232. = block_mask >> SPIFLASH_SECTOR_SHIFT;
  233. if (!sector_mask) {
  234. MSG("update: nothing to erase\n");
  235. return 0;
  236. }
  237. while (sector_mask) {
  238. if (!(addr & block_mask) &&
  239. ((sector_mask & block_sector_mask) == block_sector_mask)) {
  240. cmd24 = ROM_ERASE_64K;
  241. cmd32 = ROM_ERASE_64K_32BIT;
  242. delay = flash->param->tbe2;
  243. erasesize = SPIFLASH_BLOCK_SIZE;
  244. } else {
  245. cmd24 = ROM_ERASE_4K;
  246. cmd32 = ROM_ERASE_4K_32BIT;
  247. delay = flash->param->tse;
  248. erasesize = SPIFLASH_SECTOR_SIZE;
  249. }
  250. if (sector_mask & 1) {
  251. MSG("\rupdate: erasing %2uK at 0x%06x... ", erasesize >> 10, addr);
  252. rv = spiflash_write_enable(flash);
  253. if (rv)
  254. return rv;
  255. cmd = spiflash_setup_addrcmd(flash, addr, cmd24, cmd32, cmdbuf);
  256. rv = flash->ops->spi_write(flash->cookie, cmdbuf, cmd - cmdbuf,
  257. NULL, 0, flash->param->tshsl2);
  258. if (rv)
  259. return rv;
  260. rv = spiflash_wait_status(flash, delay, 3, 0);
  261. if (rv)
  262. return rv;
  263. }
  264. addr += erasesize;
  265. sector_mask >>= (erasesize >> SPIFLASH_SECTOR_SHIFT);
  266. }
  267. MSG("ok\n");
  268. return 0;
  269. }
  270. /*
  271. * from: current flash contents
  272. * to: desired flash contents
  273. *
  274. * These are assumed to be aligned full block buffers
  275. */
  276. enum flashmem_status {
  277. FMS_DONE, /* All done, no programming needed */
  278. FMS_PROGRAM, /* Can be programmed */
  279. FMS_ERASE, /* Needs erase before programming */
  280. FMS_NOTCHECKED /* Not checked yet */
  281. };
  282. static enum flashmem_status
  283. spiflash_memcmp(const void *from, const void *to, size_t len)
  284. {
  285. const uint32_t *pf = from;
  286. const uint32_t *pt = to;
  287. const uint32_t *pfend = (const uint32_t *)((const char *)from + len);
  288. uint32_t doprog = 0;
  289. uint32_t doerase = 0;
  290. while (pf < pfend) {
  291. uint32_t f = *pf++;
  292. uint32_t t = *pt++;
  293. doprog |= f ^ t; /* Need programming if any data mismatch */
  294. doerase |= ~f & t; /* Need erasing if any 0 -> 1 */
  295. }
  296. return doerase ? FMS_ERASE : doprog ? FMS_PROGRAM : FMS_DONE;
  297. }
  298. /*
  299. * Check a block for sectors which need erasing and pages which need
  300. * programming; the prog_mask is 256 bits long and so span multiple words.
  301. *
  302. * The desired input is spz->dbuf and the existing flash content should be
  303. * already read into spz->vbuf.
  304. *
  305. */
  306. static void spiflash_check_block(spz_stream *spz, uint32_t addr,
  307. uint32_t *erase_mask, uint32_t *prog_mask)
  308. {
  309. const uint8_t *p, *q;
  310. unsigned int page;
  311. *erase_mask = 0;
  312. memset(prog_mask, 0, SPIFLASH_BLOCK_SIZE/SPIFLASH_PAGE_SIZE/8);
  313. p = spz->vbuf;
  314. q = spz->dbuf;
  315. for (page = 0; page < SPIFLASH_BLOCK_SIZE/SPIFLASH_PAGE_SIZE; page++) {
  316. enum flashmem_status status;
  317. switch (spiflash_memcmp(p, q, SPIFLASH_PAGE_SIZE)) {
  318. case FMS_ERASE:
  319. *erase_mask |= UINT32_C(1) <<
  320. (page >> (SPIFLASH_SECTOR_SHIFT-SPIFLASH_PAGE_SHIFT));
  321. break;
  322. case FMS_PROGRAM:
  323. prog_mask[page >> 5] |= UINT32_C(1) << (page & 31);
  324. break;
  325. default:
  326. /* Nothing to do! */
  327. break;
  328. }
  329. p += SPIFLASH_PAGE_SIZE;
  330. q += SPIFLASH_PAGE_SIZE;
  331. }
  332. }
  333. static int spiflash_flash_chunk(spz_stream *spz)
  334. {
  335. unsigned int data_left = spz->header.len;
  336. unsigned int addr = spz->header.addr;
  337. int rv;
  338. while (data_left && !spz->err) {
  339. unsigned int pre_padding = addr & (SPIFLASH_BLOCK_SIZE-1);
  340. unsigned int post_padding;
  341. unsigned int bytes;
  342. bytes = SPIFLASH_BLOCK_SIZE - pre_padding;
  343. post_padding = 0;
  344. if (bytes > data_left) {
  345. post_padding = bytes - data_left;
  346. bytes = data_left;
  347. }
  348. /* Read the current content of this block into vbuf */
  349. rv = spiflash_read(spz->flash, addr, spz->vbuf, SPIFLASH_BLOCK_SIZE);
  350. if (rv)
  351. goto err;
  352. /* Copy any invariant chunk */
  353. if (pre_padding)
  354. memcpy(spz->dbuf, spz->vbuf, pre_padding);
  355. if (post_padding)
  356. memcpy(spz->dbuf+SPIFLASH_BLOCK_SIZE-post_padding,
  357. spz->vbuf+SPIFLASH_BLOCK_SIZE-post_padding,
  358. post_padding);
  359. rv = spiflash_read_data(spz, spz->dbuf+pre_padding, bytes);
  360. if (rv != (int)bytes) {
  361. MSG("needed %u bytes got %d\n", rv);
  362. rv = Z_DATA_ERROR;
  363. goto err;
  364. }
  365. MSG("update: flash block at 0x%06x (%5u bytes):\n", addr, bytes);
  366. addr -= pre_padding;
  367. uint32_t erase_mask;
  368. uint32_t prog_mask[SPIFLASH_BLOCK_SIZE >> (SPIFLASH_PAGE_SHIFT+5)];
  369. spiflash_check_block(spz, addr, &erase_mask, prog_mask);
  370. if (erase_mask) {
  371. rv = spiflash_erase(spz->flash, addr, erase_mask);
  372. if (rv)
  373. goto err;
  374. /* Verify that the sector did erase */
  375. rv = spiflash_read(spz->flash, addr, spz->vbuf, SPIFLASH_BLOCK_SIZE);
  376. if (rv)
  377. goto err;
  378. spiflash_check_block(spz, addr, &erase_mask, prog_mask);
  379. if (erase_mask) {
  380. MSG("[erase mask = %04x] ", erase_mask);
  381. spz->err = SPIFLASH_ERR_ERASE_FAILED;
  382. goto err;
  383. }
  384. }
  385. unsigned int page;
  386. bool programmed = false;
  387. for (page = 0; page < (SPIFLASH_BLOCK_SIZE >> SPIFLASH_PAGE_SHIFT);
  388. page++) {
  389. uint32_t page_offs = page << SPIFLASH_PAGE_SHIFT;
  390. if (!(prog_mask[page >> 5] & (UINT32_C(1) << (page & 31))))
  391. continue; /* No need to program */
  392. programmed = true;
  393. udelay(100);
  394. MSG("\rupdate: writing at 0x%06x... ", addr + page_offs);
  395. rv = spiflash_program(spz->flash, addr + page_offs,
  396. spz->dbuf + page_offs,
  397. SPIFLASH_PAGE_SIZE);
  398. if (rv)
  399. goto err;
  400. /* Verify that the page did write */
  401. rv = spiflash_read(spz->flash, addr + page_offs,
  402. spz->vbuf + page_offs,
  403. SPIFLASH_PAGE_SIZE);
  404. if (rv) {
  405. MSG("readback ");
  406. goto err;
  407. }
  408. if (memcmp(spz->dbuf + page_offs, spz->vbuf + page_offs,
  409. SPIFLASH_PAGE_SIZE)) {
  410. MSG("verify @ 0x%06x ", addr + page_offs);
  411. spz->err = SPIFLASH_ERR_PROGRAM_FAILED;
  412. goto err;
  413. }
  414. }
  415. if (programmed)
  416. MSG("ok\n");
  417. else
  418. MSG("update: nothing to write\n");
  419. addr += pre_padding + bytes;
  420. data_left -= bytes;
  421. }
  422. return spz->err;
  423. err:
  424. if (!spz->err)
  425. spz->err = rv;
  426. return spz->err;
  427. }
  428. /* Serial Flash Discoverable Parameter Table, see JESD216 */
  429. static int spiflash_get_sfdp(const struct spiflash *flash, void *sfdp)
  430. {
  431. static const uint8_t cmd_read_sfdp[] = { ROM_READ_SFDP, 0, 0, 0, 0 };
  432. return flash->ops->spi_read(flash->cookie, cmd_read_sfdp,
  433. sizeof cmd_read_sfdp, sfdp, SPIFLASH_SFDP_SIZE,
  434. flash->param->tshsl);
  435. }
  436. static void *spiflash_read_chunk_str(spz_stream *spz)
  437. {
  438. int rv;
  439. if (spz->header.len >= SPIFLASH_BLOCK_SIZE) {
  440. spz->err = Z_DATA_ERROR;
  441. return NULL;
  442. }
  443. rv = spiflash_read_data(spz, spz->dbuf, spz->header.len);
  444. if (spz->err) {
  445. return NULL;
  446. }
  447. if (rv != (int)spz->header.len) {
  448. spz->err = Z_DATA_ERROR;
  449. return NULL;
  450. }
  451. spz->dbuf[spz->header.len] = '\0';
  452. return spz->dbuf;
  453. }
  454. /* Skip a data chunk */
  455. static int spiflash_skip_chunk(spz_stream *spz)
  456. {
  457. unsigned int skip = spz->header.len;
  458. while (skip) {
  459. unsigned int block = min(skip, SPIFLASH_BLOCK_SIZE);
  460. int rv = spiflash_read_data(spz, spz->dbuf, block);
  461. if (spz->err)
  462. return spz->err;
  463. if (rv != (int)block) {
  464. return spz->err = Z_DATA_ERROR;
  465. }
  466. skip -= block;
  467. }
  468. return 0;
  469. }
  470. /* Read a chunk into malloc()'d storage */
  471. static int spiflash_load_chunk(spz_stream *spz, void **dptr)
  472. {
  473. void *data;
  474. int len = spz->header.len;
  475. *dptr = NULL;
  476. data = spz_malloc(spz, len);
  477. if (!data) {
  478. spiflash_skip_chunk(spz);
  479. return spz->err;
  480. } else {
  481. int rv = spiflash_read_data(spz, data, len);
  482. if (!spz->err && rv != len)
  483. spz->err = Z_DATA_ERROR;
  484. if (spz->err) {
  485. free(data);
  486. return spz->err;
  487. }
  488. *dptr = data;
  489. return rv;
  490. }
  491. }
  492. static int esp_ota_chunk(spz_stream *spz)
  493. {
  494. void *data;
  495. int len = spiflash_load_chunk(spz, &data);
  496. if (data) {
  497. esp_ota(data, len);
  498. free(data);
  499. }
  500. return spz->err;
  501. }
  502. /* Process a data chunk; return a nonzero value if done */
  503. static int spiflash_process_chunk(spz_stream *spz)
  504. {
  505. int rv;
  506. char *str;
  507. rv = spiflash_read_data(spz, &spz->header, sizeof spz->header);
  508. if (spz->err)
  509. return spz->err;
  510. else if (!rv)
  511. return Z_STREAM_END;
  512. else if (rv != sizeof spz->header)
  513. return spz->err = Z_STREAM_ERROR;
  514. if (spz->header.magic != FW_MAGIC) {
  515. MSG("update: bad chunk header magic 0x%08x\n", spz->header.magic);
  516. return spz->err = Z_DATA_ERROR;
  517. }
  518. con_printf("update: chunk type %u size %u addr 0x%08x\n",
  519. spz->header.type, spz->header.len, spz->header.addr);
  520. switch (spz->header.type) {
  521. case FDT_END:
  522. return Z_STREAM_END; /* End of data - not an error */
  523. case FDT_DATA:
  524. if (!spz->flash->ops)
  525. goto skip;
  526. return spiflash_flash_chunk(spz);
  527. case FDT_TARGET:
  528. str = spiflash_read_chunk_str(spz);
  529. if (!str || strcmp(str, spz->flash->target)) {
  530. MSG("update: this firmware file targets \"%s\", need \"%s\"\n",
  531. str, spz->flash->target);
  532. return spz->err = Z_DATA_ERROR;
  533. }
  534. break;
  535. case FDT_NOTE:
  536. str = spiflash_read_chunk_str(spz);
  537. MSG("update: %s\n", str);
  538. break;
  539. case FDT_FPGA_INIT: /* Used only when flashing from ESP32 */
  540. return spiflash_skip_chunk(spz);
  541. case FDT_ESP_OTA:
  542. if (!spz->flash->ops)
  543. goto skip;
  544. return esp_ota_chunk(spz);
  545. default:
  546. if (spz->header.flags & FDF_OPTIONAL)
  547. goto skip;
  548. MSG("update: unknown chunk type: %u\n", spz->header.type);
  549. return spz->err = Z_DATA_ERROR;
  550. }
  551. return spz->err;
  552. skip:
  553. return spiflash_skip_chunk(spz);
  554. }
  555. int spiflash_flash_file(const struct spiflash *flash, void *buf, size_t buflen)
  556. {
  557. spz_stream _spz;
  558. spz_stream * const spz = &_spz; /* For consistency in notation */
  559. int err = 0;
  560. memset(spz, 0, sizeof *spz);
  561. spz->zs.avail_in = buflen;
  562. spz->zs.next_in = buf;
  563. spz->flash = flash;
  564. err = spiflash_data_init(spz);
  565. if (err)
  566. return err;
  567. while (!spiflash_process_chunk(spz)) {
  568. /* Process data chunks until end */
  569. }
  570. err = spiflash_data_cleanup(spz);
  571. if (err)
  572. MSG("failed (err %d)\n", err);
  573. return err;
  574. }
  575. /*
  576. * Read unique serial number from flash. Note: returns id in
  577. * bigendian ("network") byte order.
  578. */
  579. int spiflash_read_id(const struct spiflash *flash, void *id)
  580. {
  581. static const uint8_t read_unique_id[] = { ROM_READ_UNIQUE_ID, 0, 0, 0, 0 };
  582. return flash->ops->spi_read(flash->cookie, read_unique_id,
  583. sizeof read_unique_id,
  584. id, SPIFLASH_ID_LEN, flash->param->tshsl);
  585. }
  586. /*
  587. * Read vendor and device ID from flash.
  588. */
  589. int spiflash_read_vdid(const struct spiflash *flash, void *vdid)
  590. {
  591. static const uint8_t read_vdid[] = { ROM_MANUFACTURER_DEVICE_ID, 0, 0, 0 };
  592. return flash->ops->spi_read(flash->cookie, read_vdid,
  593. sizeof read_vdid,
  594. vdid, SPIFLASH_VDID_LEN, flash->param->tshsl);
  595. }