fwupdate.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. #define MODULE "fwupdate"
  2. #define DEBUG 1
  3. #include "common.h"
  4. #include "jtag.h"
  5. #include "spiflash.h"
  6. #include "fpga.h"
  7. #include "ota.h"
  8. #include "spz.h"
  9. #include "httpd.h"
  10. #include "fw.h"
  11. #include "boardinfo_esp.h"
  12. #include <unzipLIB.h>
  13. #include <zlib.h>
  14. /* Needed for struct inflate_state, due to unziplib hacks */
  15. #include <zutil.h>
  16. #include <inftrees.h>
  17. #include <inflate.h>
  18. #ifndef local
  19. # define local static
  20. #endif
  21. #define BUFFER_SIZE SPIFLASH_SECTOR_SIZE
  22. #define FWUPDATE_STACK 8192
  23. #define FWUPDATE_PRIORITY 3
  24. static void heap_info(void)
  25. {
  26. #if DEBUG > 1
  27. MSG("Heap: sram ");
  28. MSG("%u/", heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL));
  29. MSG("%u, spiram ", heap_caps_get_free_size(MALLOC_CAP_INTERNAL));
  30. MSG("%u/", heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM));
  31. MSG("%u\n", heap_caps_get_free_size(MALLOC_CAP_SPIRAM));
  32. #endif
  33. }
  34. static void *spz_calloc(void *opaque, unsigned int items, unsigned int size)
  35. {
  36. spz_stream *spz = opaque;
  37. heap_info();
  38. MSG("spz_calloc(%u,%u) = %u = ", items, size, items*size);
  39. void *p = calloc(items, size);
  40. CMSG("%p\n", p);
  41. heap_info();
  42. if (!p)
  43. spz->err = Z_MEM_ERROR;
  44. return p;
  45. }
  46. static void *spz_malloc(void *opaque, unsigned int size)
  47. {
  48. spz_stream *spz = opaque;
  49. heap_info();
  50. MSG("spz_malloc(%u) = ", size);
  51. void *p = malloc(size);
  52. CMSG("%p\n", p);
  53. heap_info();
  54. if (!p)
  55. spz->err = Z_MEM_ERROR;
  56. return p;
  57. }
  58. static void spz_free(void *opaque, void *ptr)
  59. {
  60. heap_info();
  61. MSG("spz_free(%p)\n", ptr);
  62. (void)opaque;
  63. free(ptr);
  64. heap_info();
  65. }
  66. int spz_read_data(spz_stream *spz, void *buf, size_t len)
  67. {
  68. uint8_t *p = buf;
  69. while (len) {
  70. unsigned int avail = spz->zs.next_out - spz->optr;
  71. if (spz->err)
  72. break;
  73. if (avail) {
  74. if (avail > len)
  75. avail = len;
  76. memcpy(p, spz->optr, avail);
  77. p += avail;
  78. spz->optr += avail;
  79. len -= avail;
  80. } else {
  81. spz->optr = spz->zs.next_out = spz->obuf;
  82. spz->zs.avail_out = BUFFER_SIZE;
  83. while (spz->zs.avail_out) {
  84. if (!spz->zs.avail_in && !spz->eoi) {
  85. int rlen;
  86. spz->zs.next_in = spz->ibuf;
  87. rlen = spz->read_data(spz->token, spz->ibuf, BUFFER_SIZE);
  88. if (rlen < 0) {
  89. if (!spz->err)
  90. spz->err = rlen;
  91. rlen = 0;
  92. }
  93. spz->eoi = !rlen;
  94. spz->zs.avail_in = rlen;
  95. }
  96. int rv = inflate(&spz->zs, Z_SYNC_FLUSH);
  97. if (rv == Z_OK || (rv == Z_BUF_ERROR && !spz->eoi))
  98. continue;
  99. spz->eoi = true;
  100. if (rv != Z_STREAM_END && !spz->err)
  101. spz->err = rv;
  102. break;
  103. }
  104. }
  105. }
  106. return p - (uint8_t *)buf;
  107. }
  108. /*
  109. * spz needs to be initialized to zero except the read_data and cookie
  110. * fields.
  111. */
  112. static int fwupdate_data_init(spz_stream *spz)
  113. {
  114. spz->zs.zalloc = spz_calloc;
  115. spz->zs.zfree = spz_free;
  116. spz->zs.opaque = spz; /* For error reporting */
  117. spz->err = Z_OK;
  118. /* This is necessary due to unziplib damage */
  119. spz->zs.state = spz_calloc(spz, 1, sizeof(struct inflate_state));
  120. if (!spz->zs.state)
  121. goto err;
  122. for (int i = 0; i < SPZ_NBUF; i++) {
  123. spz->bufs[i] = spz_malloc(spz, BUFFER_SIZE);
  124. if (!spz->bufs[i])
  125. goto err;
  126. }
  127. /* gzip, max window size */
  128. int rv = inflateInit2(&spz->zs, 16 + 15);
  129. printf("[FWUP] fwupdate_data_init: inflateInit2 returned %d\n", rv);
  130. if (rv != Z_OK && rv != Z_STREAM_END) {
  131. spz->err = rv;
  132. goto err;
  133. }
  134. spz->cleanup = true;
  135. err:
  136. return spz->err;
  137. }
  138. static int fwupdate_data_cleanup(spz_stream *spz)
  139. {
  140. int err = 0;
  141. if (!spz)
  142. return 0;
  143. err = spz->err;
  144. if (spz->cleanup)
  145. inflateEnd(&spz->zs);
  146. /* Don't reload the FPGA on error; it wedges the JTAG bus */
  147. if (spz->fpga_updated && !err)
  148. fpga_reset();
  149. for (int i = 0; i < SPZ_NBUF; i++) {
  150. if (spz->bufs[i])
  151. free(spz->bufs[i]);
  152. }
  153. if (spz->zs.state)
  154. free(spz->zs.state);
  155. return err;
  156. }
  157. /*
  158. * Blash a full chunk of data as a JTAG SHIFT_DR transaction
  159. */
  160. int jtag_shift_spz(spz_stream *spz, enum jtag_io_flags flags)
  161. {
  162. unsigned int data_left = spz->header.len;
  163. int err = 0;
  164. if (!data_left)
  165. return 0;
  166. while (data_left) {
  167. unsigned int bytes = data_left;
  168. int rv;
  169. if (bytes > BUFFER_SIZE)
  170. bytes = BUFFER_SIZE;
  171. rv = spz_read_data(spz, spz->dbuf, bytes);
  172. if (rv < 1) {
  173. err = Z_DATA_ERROR;
  174. break;
  175. }
  176. data_left -= rv;
  177. jtag_io(rv << 3, data_left ? 0 : flags, spz->dbuf, NULL);
  178. }
  179. return err;
  180. }
  181. static void *fwupdate_read_chunk_str(spz_stream *spz)
  182. {
  183. int rv;
  184. if (spz->header.len >= BUFFER_SIZE) {
  185. spz->err = Z_DATA_ERROR;
  186. return NULL;
  187. }
  188. rv = spz_read_data(spz, spz->dbuf, spz->header.len);
  189. if (spz->err) {
  190. return NULL;
  191. }
  192. if (rv != (int)spz->header.len) {
  193. spz->err = Z_DATA_ERROR;
  194. return NULL;
  195. }
  196. spz->dbuf[spz->header.len] = '\0';
  197. return spz->dbuf;
  198. }
  199. /* Skip a data chunk */
  200. static int fwupdate_skip_chunk(spz_stream *spz)
  201. {
  202. unsigned int skip = spz->header.len;
  203. while (skip) {
  204. unsigned int block = skip;
  205. if (block > BUFFER_SIZE)
  206. block = BUFFER_SIZE;
  207. int rv = spz_read_data(spz, spz->dbuf, block);
  208. if (spz->err)
  209. return spz->err;
  210. if (rv != (int)block) {
  211. return spz->err = Z_DATA_ERROR;
  212. }
  213. skip -= block;
  214. }
  215. return 0;
  216. }
  217. static int fwupdate_boardinfo(spz_stream *spz)
  218. {
  219. uint8_t *board_info_data = spz_malloc(spz, BOARDINFO_SIZE);
  220. int rv = Z_OK;
  221. MSG("updating FPGA board_info\n");
  222. if (!board_info_data)
  223. return spz->err;
  224. if (board_info.len >= 16 &&
  225. board_info.len <= sizeof board_info &&
  226. board_info.len <= BOARDINFO_SIZE) {
  227. memcpy(board_info_data, &board_info, board_info.len);
  228. memset(board_info_data + board_info.len, 0xff,
  229. BOARDINFO_SIZE - board_info.len);
  230. rv = spiflash_write_spz(spz, board_info_data, BOARDINFO_SIZE);
  231. }
  232. free(board_info_data);
  233. fwupdate_skip_chunk(spz);
  234. if (rv)
  235. spz->err = rv;
  236. return spz->err;
  237. }
  238. /* Process a data chunk; return a nonzero value if done */
  239. static int fwupdate_process_chunk(spz_stream *spz)
  240. {
  241. int rv;
  242. char *str;
  243. rv = spz_read_data(spz, &spz->header, sizeof spz->header);
  244. if (spz->err)
  245. return spz->err;
  246. else if (!rv)
  247. return Z_STREAM_END;
  248. else if (rv != sizeof spz->header)
  249. return spz->err = Z_STREAM_ERROR;
  250. if (spz->header.magic != FW_MAGIC) {
  251. MSG("bad chunk header magic 0x%08x\n", spz->header.magic);
  252. return spz->err = Z_DATA_ERROR;
  253. }
  254. switch (spz->header.type) {
  255. case FDT_END:
  256. return Z_STREAM_END; /* End of data - not an error */
  257. case FDT_DATA:
  258. MSG("updating FPGA flash\n");
  259. return spiflash_write_spz(spz, NULL, 0);
  260. case FDT_BOARDINFO:
  261. return fwupdate_boardinfo(spz);
  262. case FDT_TARGET:
  263. str = fwupdate_read_chunk_str(spz);
  264. #if 0
  265. if (!str || strcmp(str, spz->flash->target)) {
  266. MSG("this firmware file targets \"%s\", need \"%s\"\n",
  267. str, spz->flash->target);
  268. return spz->err = Z_DATA_ERROR;
  269. }
  270. #else
  271. MSG("firmware target: \"%s\"\n", str);
  272. #endif
  273. return Z_OK;
  274. case FDT_NOTE:
  275. str = fwupdate_read_chunk_str(spz);
  276. MSG("%s\n", str);
  277. return Z_OK;
  278. case FDT_ESP_OTA:
  279. MSG("updating ESP32... ");
  280. spz->esp_updated = true;
  281. rv = esp_update((read_func_t)spz_read_data, (token_t)spz,
  282. spz->header.len);
  283. CMSG("done.\n");
  284. return rv;
  285. case FDT_FPGA_INIT:
  286. MSG("initializing FPGA for flash programming... ");
  287. spz->fpga_updated = true;
  288. rv = fpga_program_spz(spz);
  289. CMSG("done\n");
  290. return rv;
  291. case FDT_ESP_PART:
  292. case FDT_ESP_SYS:
  293. case FDT_ESP_TOOL:
  294. /* Not applicable to this update method */
  295. return fwupdate_skip_chunk(spz);
  296. default:
  297. if (spz->header.flags & FDF_OPTIONAL) {
  298. return fwupdate_skip_chunk(spz);
  299. } else {
  300. MSG("unknown chunk type: %u\n", spz->header.type);
  301. return spz->err = Z_DATA_ERROR;
  302. }
  303. }
  304. }
  305. const char *firmware_errstr(int err)
  306. {
  307. static char unknown_err[32];
  308. static const char * const errstr[] = {
  309. [-Z_STREAM_ERROR] = "Decompression error",
  310. [-Z_DATA_ERROR] = "Invalid data stream",
  311. [-Z_MEM_ERROR] = "Out of memory",
  312. [-Z_BUF_ERROR] = "Decompression error",
  313. [-FWUPDATE_ERR_IN_PROGRESS] =
  314. "Firmware update already in progress",
  315. [-FWUPDATE_ERR_BAD_CHUNK] = "Invalid firmware chunk header",
  316. [-FWUPDATE_ERR_ERASE_FAILED] = "FPGA flash erase failed",
  317. [-FWUPDATE_ERR_PROGRAM_FAILED] = "FGPA flash program failed",
  318. [-FWUPDATE_ERR_WRITE_PROTECT] = "FPGA flash write protected",
  319. [-FWUPDATE_ERR_NOT_READY] = "FPGA flash stuck at not ready",
  320. [-FWUPDATE_ERR_FPGA_JTAG] =
  321. "FPGA JTAG bus stuck, check for JTAG adapter or power cycle board",
  322. [-FWUPDATE_ERR_FPGA_MISMATCH] =
  323. "Bad FPGA IDCODE, check for JTAG adapter or power cycle board",
  324. [-FWUPDATE_ERR_FPGA_FAILED] = "FPGA reboot failed",
  325. [-FWUPDATE_ERR_UNKNOWN] = "Unidentified error",
  326. [-FWUPDATE_ERR_ESP_NO_PARTITION] = "No available ESP partition",
  327. [-FWUPDATE_ERR_ESP_BAD_OTA] = "ESP OTA information corrupt",
  328. [-FWUPDATE_ERR_ESP_FLASH_FAILED] = "ESP flash program failed",
  329. [-FWUPDATE_ERR_ESP_BAD_DATA] = "ESP firmware image corrupt",
  330. [-FWUPDATE_ERR_CONFIG_READ] = "Configuration upload failure",
  331. [-FWUPDATE_ERR_CONFIG_SAVE] = "Error saving configuration"
  332. };
  333. switch (err) {
  334. case Z_OK:
  335. return errstr[-FWUPDATE_ERR_UNKNOWN];
  336. case Z_ERRNO:
  337. return strerror(errno);
  338. case -ARRAY_SIZE(errstr)+1 ... Z_STREAM_ERROR:
  339. if (errstr[-err])
  340. return errstr[-err];
  341. /* fall through */
  342. default:
  343. snprintf(unknown_err, sizeof unknown_err, "error %d", -err);
  344. return unknown_err;
  345. }
  346. }
  347. static TaskHandle_t fwupdate_task;
  348. static spz_stream *fwupdate_spz;
  349. static SemaphoreHandle_t fwupdate_done;
  350. static int fwupdate_err;
  351. static bool do_reboot;
  352. static void firmware_update_task(void *pvt)
  353. {
  354. spz_stream *spz = pvt;
  355. fpga_service_enable(false);
  356. printf("[FWUP] fwupdate_data_init()\n");
  357. spz->err = fwupdate_data_init(spz);
  358. if (spz->err)
  359. goto fail;
  360. printf("[FWUP] fwupdate_process_chunk loop\n");
  361. int err;
  362. while (!(err = fwupdate_process_chunk(spz))) {
  363. /* Process data chunks until end */
  364. }
  365. if (!spz->err && err != Z_STREAM_END)
  366. spz->err = err;
  367. printf("[FWUP] fwupdate_data_cleanup\n");
  368. err = fwupdate_data_cleanup(spz);
  369. if (err)
  370. spz->err = err;
  371. fail:
  372. if (spz->err)
  373. MSG("failed (err %d)\n", spz->err);
  374. xSemaphoreGive(fwupdate_done);
  375. if (do_reboot) {
  376. printf("[FWUP] rebooting in %d seconds\n", reboot_delayed());
  377. while (1)
  378. vTaskSuspend(NULL);
  379. } else {
  380. exit_task();
  381. }
  382. }
  383. static int firmware_update_cleanup(void)
  384. {
  385. int err = Z_OK;
  386. fwupdate_task = NULL;
  387. if (fwupdate_done) {
  388. SemaphoreHandle_t done = fwupdate_done;
  389. fwupdate_done = NULL;
  390. vSemaphoreDelete(done);
  391. } else {
  392. err = Z_MEM_ERROR;
  393. }
  394. if (fwupdate_spz) {
  395. struct spz_stream *spz = fwupdate_spz;
  396. if (spz->err)
  397. err = spz->err;
  398. fwupdate_spz = NULL;
  399. free(spz);
  400. } else {
  401. err = Z_MEM_ERROR;
  402. }
  403. return err;
  404. }
  405. int firmware_update_start(read_func_t read_data, token_t token, bool autoreboot)
  406. {
  407. int err;
  408. SemaphoreHandle_t done = NULL;
  409. do_reboot = autoreboot;
  410. if (fwupdate_spz)
  411. return FWUPDATE_ERR_IN_PROGRESS;
  412. fwupdate_spz = calloc(1, sizeof *fwupdate_spz);
  413. if (!fwupdate_spz)
  414. goto err;
  415. fwupdate_spz->read_data = read_data;
  416. fwupdate_spz->token = token;
  417. fwupdate_done = xSemaphoreCreateBinary();
  418. if (!fwupdate_done)
  419. goto err;
  420. if (xTaskCreate(firmware_update_task, "fwupdate",
  421. FWUPDATE_STACK, fwupdate_spz,
  422. FWUPDATE_PRIORITY, &fwupdate_task) != pdPASS) {
  423. xSemaphoreGive(fwupdate_done);
  424. }
  425. return Z_OK;
  426. err:
  427. return firmware_update_cleanup();
  428. }
  429. int firmware_update_wait(TickType_t delay)
  430. {
  431. if (!fwupdate_done)
  432. return Z_MEM_ERROR;
  433. if (!xSemaphoreTake(fwupdate_done, delay))
  434. return FWUPDATE_ERR_IN_PROGRESS;
  435. return firmware_update_cleanup();
  436. }