/**************************************************************************** * * Copyright(c) 2019 by Aerospace C.Power-tech Technologies. ALL RIGHTS RESERVED. * * This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT * be copied by any method or incorporated into another program without * the express written consent of Aerospace C.Power. This Information or any portion * thereof remains the property of Aerospace C.Power. The Information contained herein * is believed to be accurate and Aerospace C.Power assumes no responsibility or * liability for its use in any way and conveys no license or title under * any patent or copyright and makes no representation or warranty that this * Information is free from patent or copyright infringement. * * ****************************************************************************/ #ifndef IOT_IMG_HDR_H #define IOT_IMG_HDR_H #ifdef __cplusplus extern "C" { #endif /* pack for the structures in the whole file */ #pragma pack(push) /* save the pack status */ #pragma pack(1) /* 1 byte align */ typedef enum { devKunlun = 0xDC01, /* KL1/KL2 */ devKunlun2 = 0xDC02, /* KL2 */ devKunlun3 = 0xDC03, /* KL3 */ devMAX = 0xFFFF }dvcType; typedef enum { imgROM = 0x0000, /* Maybe,ROM doesn't need a imgType */ imgSP = 0xFC00, /* sp */ imgSBL = 0xFC01, /* sbl */ imgFRW = 0xFC02, /* kl1: 1MB fw, uncompressed for sta, run in flash; compressed for cco, run in psram */ imgCFG = 0xFC03, /* The first pib file. */ imgEFUSE = 0xFC04, /* efuse */ imgOEM = 0xFC05, /* oem */ imgFRW2 = 0xFC06, /* The second fw, maybe the backup one. not used. */ imgCFG2 = 0xFC07, /* The second pib file. */ imgCFRW1 = 0xFC08, /* kl1: 2MB fw, compressed for sta or cco, run in flash; kl2: 4M flash fw */ imgCFRW2 = 0xFC09, /* The second fw, maybe the backup one. not used. */ imgCFRW = 0xFC0A, /* kl1: 2MB fw, compressed for sta or cco, run in psram; kl2: 4M psram fw */ imgREF = 0xFC0B, /* ref */ imgCUS = 0xFC0C, /* cus */ imgCFRW4F = 0xFC0D, /* kl1: 4MB fw, compressed for sta or cco, run in flash */ imgCFRW4P = 0xFC0E, /* kl1: 4MB fw, compressed for sta or cco, run in psram */ imgMAX = 0xFFFF, /* invalid */ }imgType; typedef enum { devV1Kunlun = 0x01, /* KL1/KL2 */ devV1Kunlun2= 0x02, /* KL2 */ devV1Kunlun3= 0x03, /* KL3 */ devV1MAX = 0xFF }dvcTypeV1; typedef enum { imgV1ROM = 0x00, /* Maybe,ROM doesn't need a imgType */ imgV1SBL = 0x01, /* sbl */ imgV1FRWPLC = 0x02, /* FW-PLC */ imgV1CFG = 0x03, /* The first pib file. */ imgV1OEM = 0x05, /* oem */ imgV1FRWCUS = 0x06, /* FW-CUS */ imgV1MAX = 0xFF /* invalid */ }imgTypeV1; typedef enum { encType_none = 0x00, encType_aa = 0xAA, encType_99 = 0x99, }encType; typedef enum { zipType_none = 0x00, /* Uncompressed file. */ zipType_defult = 0x01, /* Compressed with lzma, level-0. */ zipType_max = 0xFF }zipType; typedef enum { sizeType_0MB = 0x00, sizeType_1MB = 0x01, sizeType_2MB = 0x02, sizeType_4MB = 0x04, sizeType_8MB = 0x08, sizeType_max = 0xFF }sizeType; typedef enum { hdrVer_00 = 0x00, /* V0.0, first edition */ hdrVer_01 = 0x01, /* V0.1, be from v0.0, enable 4m flash module, kl1 kl2 used it */ hdrVer_02 = 0x02, /* V0.2, be from V0.1, offset->layout index + resv[3], kl1 kl2 */ hdrVer_10 = 0x10, /* V1.0, enable kl3, kl3 used it */ hdrVer_max = 0xFF }hdrVersion; #define IOT_MAGIC_NO 0x5566 #define IOT_XOR_VALUE 0xAA #define IOT_XOR_VALUE_FLX 0x99 #define IOT_XOR_VALUE_QJ 0x66 #define IOT_XOR_VALUE_SPE 0xCC #define IOT_XOR_VALUE_GX 0x33 #define IOT_XOR_VALUE_DT 0xDD #define IOT_XOR_VALUE_YP 0xA9 #define IOT_FILE_MODE_PLAIN 0x00 #define IOT_FILE_MODE_ADD 0x01 #define IOT_FILE_MODE_SUB 0x02 #define IOT_FILE_MODE_XOR 0x03 #define IOT_FILE_MODE_UNKNOWN 0xFF #define IOT_IMG_FLAG_MASK 0xf #define IOT_IMG_FLAG_OFFSET 0x0 #define IOT_IMG_SIZE_MASK 0xf #define IOT_IMG_SIZE_OFFSET 0x4 /* image global flag: bit0-3 */ #define IOT_IMG_FLAG_LZMA_EN 0x4 #define IOT_IMG_FLAG_FW2_EXT 0x8 /* image size: bit4-7 */ #define IOT_IMG_FLAG_SIZE_1M 0x1 #define IOT_IMG_FLAG_SIZE_2M 0x2 #define IOT_PKG_HDR_LEN 96 typedef struct iot_pkg_hdr { /* 0 - plain, 1 - add enc_patern, 2 - dec enc_patern */ uint8_t enctype; /* encrypt pattern */ uint8_t pattern; /* 0 for CCo, 1 for STA. other value is reserved. */ uint16_t dev_type; /* CRC of the whole upgrade file */ uint32_t file_crc; /* length of the upgrade file */ uint32_t file_len; /* magic number */ uint32_t magic_no; /* version info */ uint32_t version; /* start index of sp section */ uint32_t sp_start; /* length of sp section */ uint32_t sp_len; /* start index of sbl section */ uint32_t sbl_start; /* length of sbl section */ uint32_t sbl_len; /* start of oem section */ uint32_t oem_start; /* length of oem section */ uint32_t oem_len; /* start index of pib section */ uint32_t pib_start; /* length of pib section */ uint32_t pib_len; /* start index of fw section */ uint32_t fw_start; /* length of fw section */ uint32_t fw_len; /* start index of efuse section */ uint32_t efuse_start; /* length of efuse section */ uint32_t efuse_len; /* start index of fw2 section */ uint32_t fw2_start; /* length of fw2 section */ uint32_t fw2_len; /* start index of reference section */ uint32_t ref_start; /* length of reference section */ uint32_t ref_len; /* start index of custom section */ uint32_t cus_start; /* length of custom section */ uint32_t cus_len; /* vendor id*/ uint16_t vendor_id; /* image type */ uint8_t img_type; /* image flags */ uint8_t img_flag; }iot_pkg_hdr_t; /* Format of image: 0 31 ____________________________ |__________________________| --> image header size 64Bytes | | | | | PAYLOAD | | | | _________________| | |XXXXXXXXXXXXXXXXX| --> X is FIXED pad for 32Byte-aligning, suggested keeping '0'. ---------------------------- 0 31 Description for image header V0 : 0 64 ______________________________________________________________________________________________________________ | _dvcType | _imgType | _imgSize | _imgVer | _bltTimeS | _bltTimeB | _imgCRC | _guard | rev[8] | sha256[32]| -------------------------------------------------------------------------------------------------------------- | 2Byte | 2Byte | 4Byte | 2Byte | 2Byte | 4Byte | 4Byte | 4Byte | 8Byte| 32Byte | -------------------------------------------------------------------------------------------------------------- When written into file, PLS use big-endian . eg : _________________________________________________________ image-header: | _dvcType=0xDEAD | _imgType=0xBEEF | ------------------------------------------------------ ....... bytes-stream: | byte0=0xDE | byte1=0xAD | byte2=0xBE | byte3=0xEF | --------------------------------------------------------- */ #define HEADER_REV_SIZE 0 #define SHA256_SIZE 32 /* 64Bytes-len */ typedef struct { /* Refrence to enum dvcType. */ uint16_t devType; /* Refrence to enum imgType. _dvcType + _imgType -> MagicNum. */ uint16_t imgType; /* Size of Payload only.NOT include imgHdr or data for aligning */ uint32_t imgSize; /* Version. 0xA1C8 stands for V 10.1.200 (0x F.F.FF).V 15.15.255 is maximum. */ uint16_t imgVer; /* Psram size, 0, 1, 2, 4, unit is MB. */ uint8_t psramSize; /* Header version. reference for hdrVersion. */ uint8_t hdrVer; /* firmware size, non-compressed */ uint32_t fwSize; /* CRC32 for 'payload + pad for aligning' */ uint32_t imgCRC; /* fixed, 0xa4e49a17 */ uint32_t guard; /* flash layout index */ uint8_t layoutIdx; /* reserved */ uint8_t resv[3]; /* 这个可能会用来存放压缩前img的crc */ uint32_t fwCRC; /* sha256 for payload */ uint8_t sha256[SHA256_SIZE]; } imgHdrV0; /* 64 bytes */ typedef struct { /* Refrence to enum dvcType. */ uint8_t devType; /* Refrence to enum imgType. _dvcType + _imgType -> MagicNum. */ uint8_t imgType; /* Refrence to enum encType. */ uint8_t encType; /* * bit0 : Only for ROM while loading SBL. * 1b-Quad read enable if supported, 0b-Single byte read mode. */ uint8_t cfg; /* Size of Payload only.NOT include imgHdr or data for aligning */ uint32_t imgSize; /* Compress type, reference for zipType. */ uint8_t zipType; /* Flash size, reference for sizeType. */ uint8_t flashSize; /* Psram/DDR size, reference for sizeType. */ uint8_t psramSize; /* Header version. reference for hdrVersion. */ uint8_t hdrVer; // imgheader版本,V1和V0位置相同 uint32_t imgVer; /* CRC32 for 'payload + pad for aligning' */ uint32_t imgCRC; /* fixed, 0xa4e49a17 */ uint32_t guard; // 通过这个值来确定是不是imgheader,V1和V0位置相同 /* flash layout index */ uint8_t layoutIdx; /* reserved */ uint8_t resv[3]; /* Run address. */ uint32_t runAddr; /* sha256 for payload */ uint8_t sha256[SHA256_SIZE]; } imgHdrV1; #define HEADER_TOLTAL_SIZE 64 #define IMAGE_ALIGNED_SIZE 32 // 如果没有32字节对齐 把大小扩展至32字节对齐 #define IMAGE_LEN_FOR_CRC(l) ((((l)&(IMAGE_ALIGNED_SIZE-1)) ?\ (IMAGE_ALIGNED_SIZE-((l)&(IMAGE_ALIGNED_SIZE-1))) :0) + (l)) #define IMAGE_GUARD 0xa4e49a17 #define UINT8_GET(buf, n) (buf[n]&0xFF) #define UINT16_GET(buf, n) (((buf[n]<<8)&0xFF00) | (buf[(n)+1]&0x00FF)) #define UINT32_GET(buf, n) (((buf[n]<<24)&0xFF000000) | ((buf[n+1]<<16)&0x00FF0000)\ |((buf[n+2]<<8)&0x0000FF00) | (buf[n+3]&0x000000FF)) #define IMGHDR_GET_DEVTYPE(p) UINT16_GET(p, 0) #define IMGHDR_GET_IMGTYPE(p) UINT16_GET(p, 2) #define IMGHDR_GET_IMGSIZE(p) UINT32_GET(p, 4) #define IMGHDR_GET_IMGVER(p) UINT16_GET(p, 8) #define IMGHDR_GET_PSRAMSIZE(p) UINT8_GET(p, 10) #define IMGHDR_GET_HDRVER(p) UINT8_GET(p, 11) #define IMGHDR_GET_FWSIZE(p) UINT32_GET(p, 12) #define IMGHDR_GET_CRC(p) UINT32_GET(p, 16) #define IMGHDR_GET_GUARD(p) UINT32_GET(p, 20) #define IMGHDR_GET_LAYOUTIDX(p) UINT8_GET(p, 24) #define IMGHDR_GET_FWCRC(p) UINT32_GET(p, 28) #define IMGHDR_V1_GET_DEVTYPE(p) UINT8_GET(p, 0) #define IMGHDR_V1_GET_IMGTYPE(p) UINT8_GET(p, 1) #define IMGHDR_V1_GET_ENCVAL(p) UINT8_GET(p, 2) #define IMGHDR_V1_GET_CONFIG(p) UINT8_GET(p, 3) #define IMGHDR_V1_GET_IMGSIZE(p) UINT32_GET(p, 4) #define IMGHDR_V1_GET_ZIPTYPE(p) UINT8_GET(p, 8) #define IMGHDR_V1_GET_FLASHSIZE(p) UINT8_GET(p, 9) #define IMGHDR_V1_GET_PSRAMSIZE(p) UINT8_GET(p, 10) #define IMGHDR_V1_GET_HDRVER(p) UINT8_GET(p, 11) #define IMGHDR_V1_GET_IMGVER(p) UINT32_GET(p, 12) #define IMGHDR_V1_GET_IMGCRC(p) UINT32_GET(p, 16) #define IMGHDR_V1_GET_GUARD(p) UINT32_GET(p, 20) #define IMGHDR_V1_GET_LAYOUTIDX(p) UINT8_GET(p, 24) #define IMGHDR_V1_GET_RUNADDR(p) UINT32_GET(p, 28) #pragma pack(pop) /* restore the pack status */ #ifndef BUILD_BUG_ON #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - (2 * (!(condition)))])) #endif typedef union { imgHdrV0 v0; imgHdrV1 v1; }imgHdr; void static inline img_header_construct(imgHdr *pHr, char *pStr) { uint8_t hdrVersion = IMGHDR_GET_HDRVER(pStr); uint32_t ui32 = IMGHDR_GET_GUARD(pStr); BUILD_BUG_ON (sizeof(imgHdrV0) == sizeof(imgHdrV1)); if (ui32 != IMAGE_GUARD) { pHr->v0.hdrVer = hdrVer_max; pHr->v0.guard = 0; return; } if (hdrVersion == hdrVer_10) { pHr->v1.devType = IMGHDR_V1_GET_DEVTYPE(pStr); pHr->v1.imgType = IMGHDR_V1_GET_IMGTYPE(pStr); pHr->v1.encType = IMGHDR_V1_GET_ENCVAL(pStr); pHr->v1.cfg = IMGHDR_V1_GET_CONFIG(pStr); pHr->v1.imgSize = IMGHDR_V1_GET_IMGSIZE(pStr); pHr->v1.zipType = IMGHDR_V1_GET_ZIPTYPE(pStr); pHr->v1.flashSize = IMGHDR_V1_GET_FLASHSIZE(pStr); pHr->v1.psramSize = IMGHDR_V1_GET_PSRAMSIZE(pStr); pHr->v1.hdrVer = hdrVersion; pHr->v1.imgVer = IMGHDR_V1_GET_IMGVER(pStr); pHr->v1.imgCRC = IMGHDR_V1_GET_IMGCRC(pStr); pHr->v1.guard = IMAGE_GUARD; pHr->v1.layoutIdx = IMGHDR_V1_GET_LAYOUTIDX(pStr); pHr->v1.runAddr = IMGHDR_V1_GET_RUNADDR(pStr); for(ui32 = 0; ui32 < SHA256_SIZE; ui32++) { pHr->v1.sha256[ui32] = (uint8_t)pStr[32 + ui32]; } } else { pHr->v0.devType = IMGHDR_GET_DEVTYPE(pStr); pHr->v0.imgType = IMGHDR_GET_IMGTYPE(pStr); pHr->v0.imgSize = IMGHDR_GET_IMGSIZE(pStr); pHr->v0.hdrVer = hdrVersion; pHr->v0.psramSize = IMGHDR_GET_PSRAMSIZE(pStr); pHr->v0.imgVer = IMGHDR_GET_IMGVER(pStr); pHr->v0.fwSize = IMGHDR_GET_FWSIZE(pStr); pHr->v0.imgCRC = IMGHDR_GET_CRC(pStr); pHr->v0.guard = IMAGE_GUARD; pHr->v0.layoutIdx = IMGHDR_GET_LAYOUTIDX(pStr); pHr->v0.fwCRC = IMGHDR_GET_FWCRC(pStr); for(ui32 = 0; ui32 < SHA256_SIZE; ui32++) { pHr->v0.sha256[ui32] = (uint8_t)pStr[32 + ui32]; } } return; } #define IMGHDR_GET_COMMON_ITERM(pHdr, item) \ (((pHdr)->v0.guard != IMAGE_GUARD) ? \ (uint32_t)(0) : ((pHdr)->v1.hdrVer == hdrVer_10 ? \ (uint32_t)(pHdr)->v1.item : (uint32_t)(pHdr)->v0.item)) #define iot_imghdr_get_hdrVer(pHdr) IMGHDR_GET_COMMON_ITERM(pHdr, hdrVer) #define iot_imghdr_get_devType(pHdr) IMGHDR_GET_COMMON_ITERM(pHdr, devType) #define iot_imghdr_get_imgType(pHdr) IMGHDR_GET_COMMON_ITERM(pHdr, imgType) #define iot_imghdr_get_imgSize(pHdr) IMGHDR_GET_COMMON_ITERM(pHdr, imgSize) #define iot_imghdr_get_psramSize(pHdr) IMGHDR_GET_COMMON_ITERM(pHdr, psramSize) #define iot_imghdr_get_imgVer(pHdr) IMGHDR_GET_COMMON_ITERM(pHdr, imgVer) #define iot_imghdr_get_imgCRC(pHdr) IMGHDR_GET_COMMON_ITERM(pHdr, imgCRC) #define iot_imghdr_get_guard(pHdr) IMGHDR_GET_COMMON_ITERM(pHdr, guard) #define iot_imghdr_get_layout_idx(pHdr) IMGHDR_GET_COMMON_ITERM(pHdr, layoutIdx) #define iot_imghdr_get_sha256(pHdr) IMGHDR_GET_COMMON_ITERM(pHdr, sha256) #define iot_imghdr_get_fwSize(pHdr) \ (((pHdr)->v0.guard != IMAGE_GUARD) || (pHdr)->v1.hdrVer == hdrVer_10 ? \ (uint32_t)(0) : (uint32_t)(pHdr)->v0.fwSize) #define iot_imghdr_get_fwCRC(pHdr) \ (((pHdr)->v0.guard != IMAGE_GUARD) || (pHdr)->v1.hdrVer == hdrVer_10 ? \ (uint32_t)(0) : (uint32_t)(pHdr)->v0.fwCRC) #define iot_imghdr_get_encType(pHdr) \ (((pHdr)->v1.guard != IMAGE_GUARD) || (pHdr)->v1.hdrVer != hdrVer_10 ? \ (uint32_t)(0) : (uint32_t)(pHdr)->v1.encType) #define iot_imghdr_get_zipType(pHdr) \ (((pHdr)->v1.guard != IMAGE_GUARD) || (pHdr)->v1.hdrVer != hdrVer_10 ? \ (uint32_t)(0) : (uint32_t)(pHdr)->v1.zipType) #define iot_imghdr_get_flashSize(pHdr) \ (((pHdr)->v1.guard != IMAGE_GUARD) || (pHdr)->v1.hdrVer != hdrVer_10 ? \ (uint32_t)(0) : (uint32_t)(pHdr)->v1.flashSize) #define iot_imghdr_get_runAddr(pHdr) \ (((pHdr)->v1.guard != IMAGE_GUARD) || (pHdr)->v1.hdrVer != hdrVer_10 ? \ (uint32_t)(0) : (uint32_t)(pHdr)->v1.runAddr) #define iot_imghdr_get_quad_rd(pHdr) \ (((pHdr)->v1.guard != IMAGE_GUARD) || (pHdr)->v1.hdrVer != hdrVer_10 ? \ (uint32_t)(0) : (uint32_t)(((pHdr)->v1.cfg) & 0x1)) #ifdef __cplusplus } #endif #endif //IOT_IMG_HDR_H