/*
 * Common firmware image format
 */

#ifndef FWIMG_H
#define FWIMG_H

#include <inttypes.h>

/*
 * Firmware chunk header.
 */
#define FW_MAGIC_V1		0x7a07fbd6
#define FW_MAGIC_V2		0xa924ed0b

#define FW_HDR_LEN_V1		16
#define FW_HDR_LEN_V2		32

struct fw_header {
    /* All versions */
    uint32_t magic;		/* Magic number */
    uint16_t type;		/* Content type */
    uint16_t flags;		/* Content flags */
    uint32_t len;		/* Content length (excluding header) */
    uint32_t addr;		/* Address or similar */

    /* v2 only */
    uint32_t vmatch;		/* Flags to match */
    uint32_t vmask;		/* Mask of flags to match */
    uint16_t vmin;		/* Minimum version match */
    uint16_t vmax;		/* Maximum version match */
    uint32_t resv;		/* For future use */
};

enum fw_data_type {
    FDT_END,			/* End of stream */
    FDT_DATA,			/* FPGA firmware ata to be flashed */
    FDT_TARGET,			/* Subsystem string (must match) */
    FDT_NOTE,			/* Version: XXXXX or similar */
    FDT_ESP_OTA,		/* ESP32 OTA image */
    FDT_FPGA_INIT,		/* FPGA bitstream for update */
    FDT_ESP_PART,		/* ESP32 partition table */
    FDT_ESP_SYS,		/* ESP32 boot loader, OTA control, etc */
    FDT_ESP_TOOL,		/* esptool.py options for serial flashing */
    FDT_BOARDINFO		/* Board information flash address */
};
enum fw_data_flags {
    FDF_OPTIONAL     = 0x0001,	/* Ignore if chunk data type unknown */
    FDF_PRETARGET    = 0x0002	/* Matching FDT_TARGET not required (yet) */
};

/*
 * Additional error codes beyond those defined in zlib
 */
#define FWUPDATE_ERR_IN_PROGRESS	(-10)
#define FWUPDATE_ERR_BAD_CHUNK		(-11)
#define FWUPDATE_ERR_ERASE_FAILED	(-12)
#define FWUPDATE_ERR_PROGRAM_FAILED	(-13)
#define FWUPDATE_ERR_WRITE_PROTECT	(-14)
#define FWUPDATE_ERR_NOT_READY		(-15)
#define FWUPDATE_ERR_FPGA_JTAG          (-16)
#define FWUPDATE_ERR_FPGA_MISMATCH	(-17)
#define FWUPDATE_ERR_FPGA_FAILED	(-18)
#define FWUPDATE_ERR_UNKNOWN            (-19)
#define FWUPDATE_ERR_ESP_NO_PARTITION	(-20)
#define FWUPDATE_ERR_ESP_BAD_OTA	(-21)
#define FWUPDATE_ERR_ESP_FLASH_FAILED	(-22)
#define FWUPDATE_ERR_ESP_BAD_DATA	(-23)
#define FWUPDATE_ERR_CONFIG_READ	(-24)
#define FWUPDATE_ERR_CONFIG_SAVE	(-25)
#define FWUPDATE_ERR_NOT_MINE		(-26)

#endif /* FW_H */
