Files
kunlun/rom/riscv2/inc/imgHdr.h
2024-09-28 14:24:04 +08:00

127 lines
4.4 KiB
C

#ifndef _IMGHDR_H_
#define _IMGHDR_H_
#include <stdint.h>
typedef enum
{
devKunlun = 0xDC01,
devMAX = 0xFFFF
}dvcType;
typedef enum
{
imgROM = 0x0000, /* Maybe,ROM doesn't need a imgType */
imgSP = 0xFC00,
imgSBL = 0xFC01,
imgFRW = 0xFC02,
imgCFG = 0xFC03,
imgEFUSE = 0xFC04,
imgMAX = 0xFFFF
}imgType;
/*
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 :
0 64
___________________________________________________________________________________________________
| _dvcType | _imgType | _imgSize | _imgVer | _bltTimeS | _bltTimeB | _imgCRC | rev[12] | sha256[32]|
----------------------------------------------------------------------------------------------------
| 2Byte | 2Byte | 4Byte | 2Byte | 2Byte | 4Byte | 4Byte | 12Byte| 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 8
#define SHA256_SIZE 32
#pragma pack (1)
typedef struct
{
unsigned short _dvcType; /* Refrence to enum dvcType. */
unsigned short _imgType; /* Refrence to enum imgType. _dvcType + _imgType -> MagicNum. */
unsigned int _imgSize; /* Size of Payload only.NOT include imgHdr or data for aligning */
unsigned short _imgVer; /* Version. 0xA1C8 stands for V 10.1.200 (0x F.F.FF).V 15.15.255 is maximum. */
unsigned short _bltTimeS; /* Built small time. */
unsigned int _bltTimeB; /* Built big time.If _bltTimeB=0x01234567 and _bltTimeS=0x89AB,
we got a big num 0x012_3_45_67_89_AB. Y_M_D_H_M_S <- 0xFFF_F_FF_FF_FF_FF */
unsigned int _imgCRC; /* CRC32 for 'payload + pad for aligning' */
unsigned int _guard; /* fixed, 0xa4e49a17 */
unsigned char rev[HEADER_REV_SIZE]; /* Futrue use. */
unsigned char sha256[SHA256_SIZE]; /* sha256 for 'payload + pad for aligning' */
} imgHdr; /* 64Bytes-len */
#pragma pack ()
#define HEADER_TOLTAL_SIZE 64
#define IMAGE_ALIGNED_SIZE 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 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_BTIMS(p) UINT16_GET(p, 10)
#define IMGHDR_GET_BTIMB(p) UINT32_GET(p, 12)
#define IMGHDR_GET_CRC(p) UINT32_GET(p, 16)
#define IMGHDR_GET_GUARD(p) UINT32_GET(p, 20)
void static inline img_header_construct(imgHdr *pHr, char *pStr)
{
int cnt;
pHr->_dvcType = IMGHDR_GET_DEVTYPE(pStr);
pHr->_imgType = IMGHDR_GET_IMGTYPE(pStr);
pHr->_imgSize = IMGHDR_GET_IMGSIZE(pStr);
pHr->_imgVer = IMGHDR_GET_IMGVER(pStr);
pHr->_bltTimeS = IMGHDR_GET_BTIMS(pStr);
pHr->_bltTimeB = IMGHDR_GET_BTIMB(pStr);
pHr->_imgCRC = IMGHDR_GET_CRC(pStr);
pHr->_guard = IMGHDR_GET_GUARD(pStr);
for(cnt=0; cnt<HEADER_REV_SIZE; cnt++)
{
pHr->rev[cnt] = (unsigned char)pStr[20+cnt];
}
for(cnt=0; cnt<SHA256_SIZE; cnt++)
{
pHr->sha256[cnt] = (unsigned char)pStr[32+cnt];
}
return ;
}
#endif