123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- #define MODULE "fwupdate"
- #define DEBUG 1
- #include "common.h"
- #include "jtag.h"
- #include "spiflash.h"
- #include "fpga.h"
- #include "ota.h"
- #include "spz.h"
- #include "httpd.h"
- #include "fw.h"
- #include <unzipLIB.h>
- #include <zlib.h>
- /* Needed for struct inflate_state, due to unziplib hacks */
- #include <zutil.h>
- #include <inftrees.h>
- #include <inflate.h>
- #ifndef local
- # define local static
- #endif
- #define BUFFER_SIZE SPIFLASH_SECTOR_SIZE
- /* Normally provided by zlib, but UnzipLIB breaks it */
- static void *z_calloc(void *opaque, unsigned int items, unsigned int size)
- {
- (void)opaque;
- return calloc(items, size);
- }
- static void z_free(void *opaque, void *ptr)
- {
- (void)opaque;
- free(ptr);
- }
- int spz_read_data(spz_stream *spz, void *buf, size_t len)
- {
- uint8_t *p = buf;
- while (len) {
- unsigned int avail = spz->zs.next_out - spz->optr;
- if (spz->err)
- break;
- if (avail) {
- if (avail > len)
- avail = len;
- memcpy(p, spz->optr, avail);
- p += avail;
- spz->optr += avail;
- len -= avail;
- } else {
- spz->optr = spz->zs.next_out = spz->obuf;
- spz->zs.avail_out = BUFFER_SIZE;
- while (spz->zs.avail_out) {
- if (!spz->zs.avail_in && !spz->eoi) {
- int rlen;
- spz->zs.next_in = spz->ibuf;
- rlen = spz->read_data(spz->token, spz->ibuf, BUFFER_SIZE);
- if (rlen < 0) {
- if (!spz->err)
- spz->err = rlen;
- rlen = 0;
- }
- spz->eoi = !rlen;
- spz->zs.avail_in = rlen;
- }
- int rv = inflate(&spz->zs, Z_SYNC_FLUSH);
- if (rv == Z_OK || (rv == Z_BUF_ERROR && !spz->eoi))
- continue;
- spz->eoi = true;
- if (rv != Z_STREAM_END && !spz->err)
- spz->err = rv;
- break;
- }
- }
- }
- return p - (uint8_t *)buf;
- }
- /*
- * spz needs to be initialized to zero except the read_data and cookie
- * fields.
- */
- static int fwupdate_data_init(spz_stream *spz)
- {
- spz->zs.zalloc = z_calloc;
- spz->zs.zfree = z_free;
- spz->zs.opaque = spz; /* Might be useful at some point */
- /* This is necessary due to unziplib damage */
- spz->zs.state = calloc(1, sizeof(struct inflate_state));
- for (int i = 0; i < SPZ_NBUF; i++) {
- spz->bufs[i] = malloc(BUFFER_SIZE);
- if (!spz->bufs[i])
- goto err;
- }
- /* gzip, max window size */
- int rv = inflateInit2(&spz->zs, 16 + 15);
- if (rv != Z_OK && rv != Z_STREAM_END) {
- spz->err = rv;
- goto err;
- }
- spz->cleanup = true;
- err:
- return spz->err;
- }
- static int fwupdate_data_cleanup(spz_stream *spz)
- {
- int err = 0;
- if (!spz)
- return 0;
- err = spz->err;
- if (spz->cleanup)
- inflateEnd(&spz->zs);
- /* Don't reload the FPGA on error; it wedges the JTAG bus */
- if (spz->fpga_updated && !err)
- fpga_reset();
- for (int i = 0; i < SPZ_NBUF; i++) {
- if (spz->bufs[i])
- free(spz->bufs[i]);
- }
- return err;
- }
- /*
- * Blash a full chunk of data as a JTAG SHIFT_DR transaction
- */
- int jtag_shift_spz(spz_stream *spz, enum jtag_io_flags flags)
- {
- unsigned int data_left = spz->header.len;
- int err = 0;
- if (!data_left)
- return 0;
- while (data_left) {
- unsigned int bytes = data_left;
- int rv;
- if (bytes > BUFFER_SIZE)
- bytes = BUFFER_SIZE;
- rv = spz_read_data(spz, spz->dbuf, bytes);
- if (rv < 1) {
- err = Z_DATA_ERROR;
- break;
- }
- data_left -= rv;
- jtag_io(rv << 3, data_left ? 0 : flags, spz->dbuf, NULL);
- }
- return err;
- }
- static void *fwupdate_read_chunk_str(spz_stream *spz)
- {
- int rv;
- if (spz->header.len >= BUFFER_SIZE) {
- spz->err = Z_DATA_ERROR;
- return NULL;
- }
- rv = spz_read_data(spz, spz->dbuf, spz->header.len);
- if (spz->err) {
- return NULL;
- }
- if (rv != (int)spz->header.len) {
- spz->err = Z_DATA_ERROR;
- return NULL;
- }
- spz->dbuf[spz->header.len] = '\0';
- return spz->dbuf;
- }
- /* Skip a data chunk */
- static int fwupdate_skip_chunk(spz_stream *spz)
- {
- unsigned int skip = spz->header.len;
- while (skip) {
- unsigned int block = skip;
- if (block > BUFFER_SIZE)
- block = BUFFER_SIZE;
- int rv = spz_read_data(spz, spz->dbuf, block);
- if (spz->err)
- return spz->err;
- if (rv != (int)block) {
- return spz->err = Z_DATA_ERROR;
- }
- skip -= block;
- }
- return 0;
- }
- /* Process a data chunk; return a nonzero value if done */
- static int fwupdate_process_chunk(spz_stream *spz)
- {
- int rv;
- char *str;
- rv = spz_read_data(spz, &spz->header, sizeof spz->header);
- if (spz->err)
- return spz->err;
- else if (!rv)
- return Z_STREAM_END;
- else if (rv != sizeof spz->header)
- return spz->err = Z_STREAM_ERROR;
- if (spz->header.magic != FW_MAGIC) {
- MSG("bad chunk header magic 0x%08x\n", spz->header.magic);
- return spz->err = Z_DATA_ERROR;
- }
- switch (spz->header.type) {
- case FDT_END:
- return Z_STREAM_END; /* End of data - not an error */
- case FDT_DATA:
- MSG("updating FPGA flash\n");
- return spiflash_write_spz(spz);
- case FDT_TARGET:
- str = fwupdate_read_chunk_str(spz);
- #if 0
- if (!str || strcmp(str, spz->flash->target)) {
- MSG("this firmware file targets \"%s\", need \"%s\"\n",
- str, spz->flash->target);
- return spz->err = Z_DATA_ERROR;
- }
- #else
- MSG("firmware target: \"%s\"\n", str);
- #endif
- return Z_OK;
- case FDT_NOTE:
- str = fwupdate_read_chunk_str(spz);
- MSG("%s\n", str);
- return Z_OK;
- case FDT_ESP_OTA:
- MSG("updating ESP32... ");
- spz->esp_updated = true;
- rv = esp_update((read_func_t)spz_read_data, (token_t)spz,
- spz->header.len);
- CMSG("done.\n");
- return rv;
- case FDT_FPGA_INIT:
- MSG("initializing FPGA for flash programming... ");
- spz->fpga_updated = true;
- rv = fpga_program_spz(spz);
- CMSG("done\n");
- return rv;
- default:
- if (spz->header.flags & FDF_OPTIONAL) {
- return fwupdate_skip_chunk(spz);
- } else {
- MSG("unknown chunk type: %u\n", spz->header.type);
- return spz->err = Z_DATA_ERROR;
- }
- }
- }
- const char *firmware_errstr(int err)
- {
- static char unknown_err[32];
- static const char * const errstr[] = {
- [-Z_STREAM_ERROR] = "Decompression error",
- [-Z_DATA_ERROR] = "Invalid data stream",
- [-Z_MEM_ERROR] = "Out of memory",
- [-Z_BUF_ERROR] = "Decompression error",
- [-FWUPDATE_ERR_IN_PROGRESS] =
- "Firmware update already in progress",
- [-FWUPDATE_ERR_BAD_CHUNK] = "Invalid firmware chunk header",
- [-FWUPDATE_ERR_ERASE_FAILED] = "FPGA flash erase failed",
- [-FWUPDATE_ERR_PROGRAM_FAILED] = "FGPA flash program failed",
- [-FWUPDATE_ERR_WRITE_PROTECT] = "FPGA flash write protected",
- [-FWUPDATE_ERR_NOT_READY] = "FPGA flash stuck at not ready",
- [-FWUPDATE_ERR_FPGA_JTAG] =
- "FPGA JTAG bus stuck, check for JTAG adapter or power cycle board",
- [-FWUPDATE_ERR_FPGA_MISMATCH] =
- "Bad FPGA IDCODE, check for JTAG adapter or power cycle board",
- [-FWUPDATE_ERR_FPGA_FAILED] = "FPGA reboot failed",
- [-FWUPDATE_ERR_UNKNOWN] = "Unidentified error",
- [-FWUPDATE_ERR_ESP_NO_PARTITION] = "No available ESP partition",
- [-FWUPDATE_ERR_ESP_BAD_OTA] = "ESP OTA information corrupt",
- [-FWUPDATE_ERR_ESP_FLASH_FAILED] = "ESP flash program failed",
- [-FWUPDATE_ERR_ESP_BAD_DATA] = "ESP firmware image corrupt",
- [-FWUPDATE_ERR_CONFIG_READ] = "Configuration upload failure",
- [-FWUPDATE_ERR_CONFIG_SAVE] = "Error saving configuration"
- };
- switch (err) {
- case Z_OK:
- return errstr[-FWUPDATE_ERR_UNKNOWN];
- case Z_ERRNO:
- return strerror(errno);
- case -ARRAY_SIZE(errstr)+1 ... Z_STREAM_ERROR:
- if (errstr[-err])
- return errstr[-err];
- /* fall through */
- default:
- snprintf(unknown_err, sizeof unknown_err, "error %d", -err);
- return unknown_err;
- }
- }
- int firmware_update(read_func_t read_data, token_t token)
- {
- struct spz_stream *spz = calloc(1, sizeof *spz);
- int err = 0;
- if (!spz) {
- return Z_MEM_ERROR;
- }
- fpga_service_enable(false);
- spz->read_data = read_data;
- spz->token = token;
- err = fwupdate_data_init(spz);
- if (err)
- goto fail;
- while (!(err = fwupdate_process_chunk(spz))) {
- /* Process data chunks until end */
- }
- if (!spz->err && err != Z_STREAM_END)
- spz->err = err;
- err = fwupdate_data_cleanup(spz);
- if (err)
- MSG("failed (err %d)\n", err);
- fail:
- free(spz);
- return err;
- }
|