spiflash.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. #include "common.h"
  2. #include "zlib.h"
  3. #include "spiflash.h"
  4. #include "esp.h"
  5. #include "matchver.h"
  6. #if 1
  7. # include "console.h"
  8. # define MSG(...) con_printf(__VA_ARGS__)
  9. #else
  10. # define MSG(...) ((void)0)
  11. #endif
  12. struct spz_stream;
  13. typedef struct spz_stream spz_stream;
  14. #define NBUF 4
  15. struct spz_stream {
  16. z_stream zs;
  17. const struct spiflash *flash;
  18. uint8_t *optr; /* Output data pointer into obuf */
  19. /* Note: available output data ends at zs->next_out */
  20. union {
  21. uint8_t *bufs[NBUF];
  22. struct {
  23. uint8_t *ibuf; /* Input buffer if compressed */
  24. uint8_t *obuf; /* Output buffer */
  25. uint8_t *dbuf; /* Block data buffer */
  26. uint8_t *vbuf; /* Readback/verify buffer */
  27. };
  28. };
  29. struct fw_header header; /* Header of currently processed chunk */
  30. int err; /* Error code to return */
  31. bool eoi; /* Reached end of input */
  32. bool cleanup; /* Call inflateEnd() */
  33. };
  34. static void *spz_malloc(spz_stream *spz, size_t bytes)
  35. {
  36. void *p = malloc(bytes);
  37. if (!p && !spz->err) {
  38. spz->err = Z_MEM_ERROR;
  39. }
  40. return p;
  41. }
  42. static int spiflash_read_data(spz_stream *spz, void *buf, unsigned int len)
  43. {
  44. uint8_t *p = buf;
  45. while (len) {
  46. unsigned int avail = spz->zs.next_out - spz->optr;
  47. if (spz->err)
  48. break;
  49. if (avail) {
  50. if (avail > len)
  51. avail = len;
  52. memcpy(p, spz->optr, avail);
  53. p += avail;
  54. spz->optr += avail;
  55. len -= avail;
  56. } else {
  57. spz->optr = spz->zs.next_out = spz->obuf;
  58. spz->zs.avail_out = SPIFLASH_BLOCK_SIZE;
  59. while (spz->zs.avail_out) {
  60. if (!spz->zs.avail_in && !spz->eoi) {
  61. int (*read_data)(void *, void *, unsigned int);
  62. read_data = spz->flash->read_data;
  63. spz->zs.next_in = spz->ibuf;
  64. int rlen = read_data(spz->flash->cookie,
  65. spz->ibuf, SPIFLASH_BLOCK_SIZE);
  66. spz->eoi = rlen < SPIFLASH_BLOCK_SIZE;
  67. if (rlen < 0) {
  68. if (!spz->err)
  69. spz->err = rlen;
  70. rlen = 0;
  71. }
  72. spz->zs.avail_in = rlen;
  73. }
  74. int rv = inflate(&spz->zs, Z_SYNC_FLUSH);
  75. if (rv == Z_OK || (rv == Z_BUF_ERROR && !spz->eoi))
  76. continue;
  77. spz->eoi = true;
  78. if (rv != Z_STREAM_END && !spz->err)
  79. spz->err = rv;
  80. break;
  81. }
  82. }
  83. }
  84. return p - (uint8_t *)buf;
  85. }
  86. /*
  87. * spz needs to be initialized to zero except the flash, zs.next_in,
  88. * and zs.avail_in fields.
  89. */
  90. static int spiflash_data_init(spz_stream *spz)
  91. {
  92. int rv = Z_OK;
  93. uint8_t *rdbuf = NULL;
  94. int rlen;
  95. uint32_t header_crc;
  96. for (int i = 0; i < NBUF; i++) {
  97. spz->bufs[i] = spz_malloc(spz, SPIFLASH_BLOCK_SIZE);
  98. if (!spz->bufs[i])
  99. goto err;
  100. }
  101. spz->eoi = !spz->flash->read_data;
  102. /* gzip, max window size */
  103. rv = inflateInit2(&spz->zs, 16 + 15);
  104. if (rv != Z_OK && rv != Z_STREAM_END) {
  105. spz->err = rv;
  106. goto err;
  107. }
  108. spz->cleanup = true;
  109. err:
  110. return spz->err;
  111. }
  112. static int spiflash_data_cleanup(spz_stream *spz)
  113. {
  114. int err = 0;
  115. if (!spz)
  116. return 0;
  117. err = spz->err;
  118. if (spz->cleanup)
  119. inflateEnd(&spz->zs);
  120. for (int i = 0; i < NBUF; i++) {
  121. if (spz->bufs[i])
  122. free(spz->bufs[i]);
  123. }
  124. return err;
  125. }
  126. /*
  127. * Set up a command header with an address according to the SPI
  128. * addressing mode. Returns a pointer to the first byte past the
  129. * address.
  130. */
  131. static void *spiflash_setup_addrcmd(const struct spiflash *flash,
  132. uint32_t addr,
  133. uint8_t cmd24, uint8_t cmd32,
  134. void *cmdbuf)
  135. {
  136. enum spiflash_addr_mode mode = flash->param->addr;
  137. uint8_t *cmd = cmdbuf;
  138. if (!mode)
  139. mode = addr < (1 << 24) ? SPIFLASH_ADDR_24BIT : SPIFLASH_ADDR_32BIT;
  140. if (mode == SPIFLASH_ADDR_24BIT) {
  141. *cmd++ = cmd24;
  142. } else {
  143. *cmd++ = cmd32;
  144. *cmd++ = addr >> 24;
  145. }
  146. *cmd++ = addr >> 16;
  147. *cmd++ = addr >> 8;
  148. *cmd++ = addr;
  149. return cmd;
  150. }
  151. static int spiflash_get_status(const struct spiflash *flash,
  152. uint8_t cmd, uint8_t *sr)
  153. {
  154. return flash->ops->spi_read(flash->cookie, &cmd, 1, sr, 1,
  155. flash->param->tshsl);
  156. }
  157. /* This needs a timeout function */
  158. static int spiflash_wait_status(const struct spiflash *flash,
  159. int delay, uint8_t mask, uint8_t val)
  160. {
  161. uint8_t sr1;
  162. int rv;
  163. do {
  164. if (flash->ops->yield)
  165. flash->ops->yield(flash->cookie, delay);
  166. rv = spiflash_get_status(flash, ROM_READ_SR1, &sr1);
  167. if (rv)
  168. return rv;
  169. } while ((sr1 & mask) != val); /* Waiting... */
  170. return 0;
  171. }
  172. int spiflash_read(const struct spiflash *flash,
  173. uint32_t addr, void *buffer, size_t len)
  174. {
  175. uint8_t cmdbuf[6];
  176. uint8_t *cmd;
  177. cmd = spiflash_setup_addrcmd(flash, addr,
  178. ROM_FAST_READ, ROM_FAST_READ_32BIT,
  179. cmdbuf);
  180. *cmd++ = 0; /* Dummy cycles */
  181. return flash->ops->spi_read(flash->cookie, cmdbuf, cmd - cmdbuf,
  182. buffer, len, flash->param->tshsl1);
  183. }
  184. static int spiflash_simple_command(const struct spiflash *flash, uint8_t cmd)
  185. {
  186. return flash->ops->spi_write(flash->cookie, &cmd, 1, NULL, 0,
  187. flash->param->tshsl);
  188. }
  189. static int spiflash_write_enable(const struct spiflash *flash)
  190. {
  191. uint8_t sr1;
  192. int rv;
  193. rv = spiflash_wait_status(flash, 0, 1, 0);
  194. if (rv)
  195. return rv;
  196. rv = spiflash_simple_command(flash, ROM_WRITE_ENABLE);
  197. if (rv)
  198. return rv;
  199. return spiflash_wait_status(flash, 0, 3, 2);
  200. }
  201. static int spiflash_program(const struct spiflash *flash,
  202. uint32_t addr, const void *buffer, size_t len)
  203. {
  204. uint8_t cmdbuf[5];
  205. uint8_t *cmd;
  206. int rv;
  207. rv = spiflash_write_enable(flash);
  208. if (rv)
  209. return rv;
  210. cmd = spiflash_setup_addrcmd(flash, addr,
  211. ROM_PAGE_PROGRAM, ROM_PAGE_PROGRAM_32BIT,
  212. cmdbuf);
  213. rv = flash->ops->spi_write(flash->cookie, cmdbuf, cmd - cmdbuf,
  214. buffer, len, flash->param->tshsl2);
  215. if (rv)
  216. return rv;
  217. return spiflash_wait_status(flash, flash->param->tpp, 3, 0);
  218. }
  219. /*
  220. * Erase up to (long bits) sectors, using block erase if possible.
  221. */
  222. static int spiflash_erase(const struct spiflash *flash,
  223. uint32_t addr, unsigned long sector_mask)
  224. {
  225. uint8_t cmdbuf[5];
  226. uint8_t *cmd;
  227. uint8_t cmd24, cmd32;
  228. uint32_t erasesize;
  229. int rv;
  230. int delay;
  231. const uint32_t block_mask = SPIFLASH_BLOCK_SIZE - 1;
  232. const unsigned long block_sector_mask
  233. = block_mask >> SPIFLASH_SECTOR_SHIFT;
  234. if (!sector_mask) {
  235. MSG("update: nothing to erase\n");
  236. return 0;
  237. }
  238. while (sector_mask) {
  239. if (!(addr & block_mask) &&
  240. ((sector_mask & block_sector_mask) == block_sector_mask)) {
  241. cmd24 = ROM_ERASE_64K;
  242. cmd32 = ROM_ERASE_64K_32BIT;
  243. delay = flash->param->tbe2;
  244. erasesize = SPIFLASH_BLOCK_SIZE;
  245. } else {
  246. cmd24 = ROM_ERASE_4K;
  247. cmd32 = ROM_ERASE_4K_32BIT;
  248. delay = flash->param->tse;
  249. erasesize = SPIFLASH_SECTOR_SIZE;
  250. }
  251. if (sector_mask & 1) {
  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. addr -= pre_padding;
  349. /* Read the current content of this block into vbuf */
  350. rv = spiflash_read(spz->flash, addr, spz->vbuf, SPIFLASH_BLOCK_SIZE);
  351. if (rv)
  352. goto err;
  353. /* Copy any invariant chunk */
  354. if (pre_padding)
  355. memcpy(spz->dbuf, spz->vbuf, pre_padding);
  356. if (post_padding)
  357. memcpy(spz->dbuf+SPIFLASH_BLOCK_SIZE-post_padding,
  358. spz->vbuf+SPIFLASH_BLOCK_SIZE-post_padding,
  359. post_padding);
  360. rv = spiflash_read_data(spz, spz->dbuf+pre_padding, bytes);
  361. if (rv != (int)bytes) {
  362. MSG("needed %u bytes got %d, \n", bytes, rv);
  363. rv = Z_DATA_ERROR;
  364. goto err;
  365. }
  366. uint32_t erase_mask;
  367. uint32_t prog_mask[SPIFLASH_BLOCK_SIZE >> (SPIFLASH_PAGE_SHIFT+5)];
  368. spiflash_check_block(spz, addr, &erase_mask, prog_mask);
  369. if (erase_mask) {
  370. MSG("flash: erasing at 0x%06x mask %04x... ", addr, 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. MSG("readback ");
  378. goto err;
  379. }
  380. spiflash_check_block(spz, addr, &erase_mask, prog_mask);
  381. if (erase_mask) {
  382. spz->err = SPIFLASH_ERR_ERASE_FAILED;
  383. MSG("%04x left, ", erase_mask);
  384. goto err;
  385. }
  386. MSG("ok\n");
  387. }
  388. unsigned int page;
  389. bool programmed = false;
  390. for (page = 0; page < (SPIFLASH_BLOCK_SIZE >> SPIFLASH_PAGE_SHIFT);
  391. page++) {
  392. uint32_t page_offs = page << SPIFLASH_PAGE_SHIFT;
  393. if (!(prog_mask[page >> 5] & (UINT32_C(1) << (page & 31))))
  394. continue; /* No need to program */
  395. programmed = true;
  396. udelay(100);
  397. MSG("flash: writing at 0x%06x... ", addr + page_offs);
  398. rv = spiflash_program(spz->flash, addr + page_offs,
  399. spz->dbuf + page_offs,
  400. SPIFLASH_PAGE_SIZE);
  401. if (rv)
  402. goto err;
  403. /* Verify that the page did write */
  404. rv = spiflash_read(spz->flash, addr + page_offs,
  405. spz->vbuf + page_offs,
  406. SPIFLASH_PAGE_SIZE);
  407. if (rv) {
  408. MSG("readback ");
  409. goto err;
  410. }
  411. if (memcmp(spz->dbuf + page_offs, spz->vbuf + page_offs,
  412. SPIFLASH_PAGE_SIZE)) {
  413. MSG("verify ");
  414. spz->err = SPIFLASH_ERR_PROGRAM_FAILED;
  415. goto err;
  416. }
  417. MSG("ok\n");
  418. }
  419. if (programmed)
  420. MSG("ok\n");
  421. else
  422. MSG("unchanged\n");
  423. addr += pre_padding + bytes;
  424. data_left -= bytes;
  425. }
  426. return spz->err;
  427. err:
  428. MSG("failed\n");
  429. if (!spz->err)
  430. spz->err = rv;
  431. return spz->err;
  432. }
  433. /* Serial Flash Discoverable Parameter Table, see JESD216 */
  434. static int spiflash_get_sfdp(const struct spiflash *flash, void *sfdp)
  435. {
  436. static const uint8_t cmd_read_sfdp[] = { ROM_READ_SFDP, 0, 0, 0, 0 };
  437. return flash->ops->spi_read(flash->cookie, cmd_read_sfdp,
  438. sizeof cmd_read_sfdp, sfdp, SPIFLASH_SFDP_SIZE,
  439. flash->param->tshsl);
  440. }
  441. static void *spiflash_read_chunk_str(spz_stream *spz)
  442. {
  443. int rv;
  444. if (spz->header.len >= SPIFLASH_BLOCK_SIZE) {
  445. spz->err = Z_DATA_ERROR;
  446. return NULL;
  447. }
  448. rv = spiflash_read_data(spz, spz->dbuf, spz->header.len);
  449. if (spz->err) {
  450. return NULL;
  451. }
  452. if (rv != (int)spz->header.len) {
  453. spz->err = Z_DATA_ERROR;
  454. return NULL;
  455. }
  456. spz->dbuf[spz->header.len] = '\0';
  457. return spz->dbuf;
  458. }
  459. /* Skip a data chunk */
  460. static int spiflash_skip_chunk(spz_stream *spz)
  461. {
  462. unsigned int skip = spz->header.len;
  463. while (skip) {
  464. unsigned int block = min(skip, SPIFLASH_BLOCK_SIZE);
  465. int rv = spiflash_read_data(spz, spz->dbuf, block);
  466. if (spz->err)
  467. return spz->err;
  468. if (rv != (int)block) {
  469. return spz->err = Z_DATA_ERROR;
  470. }
  471. skip -= block;
  472. }
  473. return 0;
  474. }
  475. /* Read a chunk into malloc()'d storage */
  476. static int spiflash_load_chunk(spz_stream *spz, void **dptr)
  477. {
  478. void *data;
  479. int len = spz->header.len;
  480. *dptr = NULL;
  481. data = spz_malloc(spz, len);
  482. if (!data) {
  483. spiflash_skip_chunk(spz);
  484. return spz->err;
  485. } else {
  486. int rv = spiflash_read_data(spz, data, len);
  487. if (!spz->err && rv != len)
  488. spz->err = Z_DATA_ERROR;
  489. if (spz->err) {
  490. free(data);
  491. return spz->err;
  492. }
  493. *dptr = data;
  494. return rv;
  495. }
  496. }
  497. static int esp_ota_chunk(spz_stream *spz)
  498. {
  499. void *data;
  500. int len = spiflash_load_chunk(spz, &data);
  501. if (data) {
  502. esp_ota(data, len);
  503. free(data);
  504. }
  505. return spz->err;
  506. }
  507. /* Process a data chunk; return a nonzero value if done */
  508. static int spiflash_process_chunk(spz_stream *spz)
  509. {
  510. int rv;
  511. char *str;
  512. rv = spiflash_read_data(spz, &spz->header, sizeof spz->header);
  513. if (spz->err)
  514. return spz->err;
  515. else if (!rv)
  516. return Z_STREAM_END;
  517. else if (rv != sizeof spz->header)
  518. return spz->err = Z_STREAM_ERROR;
  519. if (spz->header.magic != FW_MAGIC) {
  520. MSG("update: bad chunk header magic 0x%08x\n", spz->header.magic);
  521. return spz->err = Z_DATA_ERROR;
  522. }
  523. con_printf("update: chunk type %u size %u addr 0x%08x\n",
  524. spz->header.type, spz->header.len, spz->header.addr);
  525. switch (spz->header.type) {
  526. case FDT_END:
  527. return Z_STREAM_END; /* End of data - not an error */
  528. case FDT_DATA:
  529. if (!spz->flash->ops)
  530. goto skip;
  531. return spiflash_flash_chunk(spz);
  532. case FDT_TARGET:
  533. str = spiflash_read_chunk_str(spz);
  534. /* XXX: replace with proper matching algorithm */
  535. if (!str || !match_version(spz->flash->target, str)) {
  536. MSG("update: this firmware file targets \"%s\", need \"%s\"\n",
  537. str, spz->flash->target);
  538. return spz->err = Z_DATA_ERROR;
  539. }
  540. break;
  541. case FDT_NOTE:
  542. str = spiflash_read_chunk_str(spz);
  543. MSG("update: %s\n", str);
  544. break;
  545. case FDT_ESP_OTA:
  546. if (!spz->flash->ops)
  547. goto skip;
  548. return esp_ota_chunk(spz);
  549. case FDT_FPGA_INIT:
  550. case FDT_ESP_PART:
  551. case FDT_ESP_SYS:
  552. case FDT_ESP_TOOL:
  553. case FDT_BOARDINFO:
  554. /* Used only when flashing from ESP32 */
  555. goto skip;
  556. default:
  557. if (spz->header.flags & FDF_OPTIONAL)
  558. goto skip;
  559. MSG("update: unknown chunk type: %u\n", spz->header.type);
  560. return spz->err = Z_DATA_ERROR;
  561. }
  562. return spz->err;
  563. skip:
  564. return spiflash_skip_chunk(spz);
  565. }
  566. int spiflash_flash_file(const struct spiflash *flash, void *buf, size_t buflen)
  567. {
  568. spz_stream _spz;
  569. spz_stream * const spz = &_spz; /* For consistency in notation */
  570. int err = 0;
  571. memset(spz, 0, sizeof *spz);
  572. spz->zs.avail_in = buflen;
  573. spz->zs.next_in = buf;
  574. spz->flash = flash;
  575. err = spiflash_data_init(spz);
  576. if (err)
  577. return err;
  578. while (!spiflash_process_chunk(spz)) {
  579. /* Process data chunks until end */
  580. }
  581. err = spiflash_data_cleanup(spz);
  582. if (err)
  583. MSG("failed (err %d)\n", err);
  584. return err;
  585. }
  586. /*
  587. * Read unique serial number from flash. Note: returns id in
  588. * bigendian ("network") byte order.
  589. */
  590. int spiflash_read_id(const struct spiflash *flash, void *id)
  591. {
  592. static const uint8_t read_unique_id[] = { ROM_READ_UNIQUE_ID, 0, 0, 0, 0 };
  593. return flash->ops->spi_read(flash->cookie, read_unique_id,
  594. sizeof read_unique_id,
  595. id, SPIFLASH_ID_LEN, flash->param->tshsl);
  596. }
  597. /*
  598. * Read vendor and device ID from flash.
  599. */
  600. int spiflash_read_vdid(const struct spiflash *flash, void *vdid)
  601. {
  602. static const uint8_t read_vdid[] = { ROM_MANUFACTURER_DEVICE_ID, 0, 0, 0 };
  603. return flash->ops->spi_read(flash->cookie, read_vdid,
  604. sizeof read_vdid,
  605. vdid, SPIFLASH_VDID_LEN, flash->param->tshsl);
  606. }
  607. /*
  608. * Write an absolute region to flash
  609. */
  610. int spiflash_flash_data(const struct spiflash *flash, uint32_t addr,
  611. const void *data, size_t len)
  612. {
  613. spz_stream _spz;
  614. spz_stream * const spz = &_spz; /* For consistency in notation */
  615. int err = 0;
  616. memset(spz, 0, sizeof *spz);
  617. /* No ibuf or obuf */
  618. for (int i = 2; i < NBUF; i++) {
  619. spz->bufs[i] = spz_malloc(spz, SPIFLASH_BLOCK_SIZE);
  620. if (!spz->bufs[i])
  621. goto err;
  622. }
  623. spz->flash = flash;
  624. spz->optr = (uint8_t *)data; /* OK to lose const here */
  625. spz->obuf = (uint8_t *)data; /* OK to lose const here */
  626. spz->zs.next_out = (uint8_t *)data + len;
  627. spz->eoi = true;
  628. spz->header.len = len;
  629. spz->header.addr = addr;
  630. spiflash_flash_chunk(spz);
  631. err:
  632. for (int i = 2; i < NBUF; i++)
  633. if (spz->bufs[i])
  634. free(spz->bufs[i]);
  635. return spz->err;
  636. }