From fafc9a8d2c035172d41702faeace578869587a36 Mon Sep 17 00:00:00 2001 From: RenXiao Date: Fri, 14 Jul 2023 15:55:27 +0800 Subject: [PATCH] =?UTF-8?q?bl602:=20=E6=B7=BB=E5=8A=A0bl602=E8=8A=AF?= =?UTF-8?q?=E7=89=87=E7=9A=84=E9=9D=99=E6=80=81=E5=88=86=E6=9E=90=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: RenXiao --- BL602Image.bt | 212 ++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 1 + 2 files changed, 213 insertions(+) create mode 100644 BL602Image.bt diff --git a/BL602Image.bt b/BL602Image.bt new file mode 100644 index 0000000..1a71542 --- /dev/null +++ b/BL602Image.bt @@ -0,0 +1,212 @@ +//------------------------------------------------ +//--- 010 Editor v12.0.1 Binary Template +// +// File: BL602Image +// Authors: Xiao Ren +// Version: 0.0.0 +// Purpose: +// Category: +// File Mask: +// ID Bytes: +// History: +//------------------------------------------------ +RequiresVersion( 12 ); + +/**** STRUCTURES ****/ +struct HAL_BOOTHEADER; +struct HAL_FLASH_CONFIG; +struct SPI_FLASH_CFG_TYPE; +struct HAL_PLL_CONFIG; +struct HAL_SYS_CLK_CONFIG; +struct HAL_BASIC_CONFIG; + +typedef struct { + local int64 start = FTell(); + uint32 magic ; + uint32 revision; + HAL_FLASH_CONFIG flashCfg; + HAL_PLL_CONFIG clkCfg; + HAL_BASIC_CONFIG basicCfg; + uint32 reserved[2] ; + local int64 end = FTell(); + local uint32 checksum = Checksum(CHECKSUM_CRC32,start,end - start); + uint32 crc32 ; + if(checksum != crc32) { + Warning("invalid crc32 for HAL_BOOTHEADER!"); + } +} HAL_BOOTHEADER; + +typedef struct { + uint32 magic ; + SPI_FLASH_CFG_TYPE cfg; + local uint32 checksum = Checksum(CHECKSUM_CRC32,startof(cfg),sizeof(cfg)); + uint32 crc32 ; + if(checksum != crc32) { + Warning("invalid crc32 for HAL_FLASH_CONFIG!"); + } +} HAL_FLASH_CONFIG; + +// source: bouffalo boot2 project +typedef struct { + ubyte ioMode ; + ubyte cReadSupport ; + ubyte clkDelay ; + ubyte clkInvert ; + ubyte resetEnCmd ; + ubyte resetCmd ; + ubyte resetCreadCmd ; + ubyte resetCreadCmdSize ; + ubyte jedecIdCmd ; + ubyte jedecIdCmdDmyClk ; + ubyte enter32BitsAddrCmd ; + ubyte exit32BitsAddrCmd ; + ubyte sectorSize ; + ubyte mid ; + uint16 pageSize ; + ubyte chipEraseCmd ; + ubyte sectorEraseCmd ; + ubyte blk32EraseCmd ; + ubyte blk64EraseCmd ; + ubyte writeEnableCmd ; + ubyte pageProgramCmd ; + ubyte qpageProgramCmd ; + ubyte qppAddrMode ; + ubyte fastReadCmd ; + ubyte frDmyClk ; + ubyte qpiFastReadCmd ; + ubyte qpiFrDmyClk ; + ubyte fastReadDoCmd ; + ubyte frDoDmyClk ; + ubyte fastReadDioCmd ; + ubyte frDioDmyClk ; + ubyte fastReadQoCmd ; + ubyte frQoDmyClk ; + ubyte fastReadQioCmd ; + ubyte frQioDmyClk ; + ubyte qpiFastReadQioCmd ; + ubyte qpiFrQioDmyClk ; + ubyte qpiPageProgramCmd ; + ubyte writeVregEnableCmd ; + ubyte wrEnableIndex ; + ubyte qeIndex ; + ubyte busyIndex ; + ubyte wrEnableBit ; + ubyte qeBit ; + ubyte busyBit ; + ubyte wrEnableWriteRegLen ; + ubyte wrEnableReadRegLen ; + ubyte qeWriteRegLen ; + ubyte qeReadRegLen ; + ubyte releasePowerDown ; + ubyte busyReadRegLen ; + ubyte readRegCmd[4] ; + ubyte writeRegCmd[4] ; + ubyte enterQpi ; + ubyte exitQpi ; + ubyte cReadMode ; + ubyte cRExit ; + ubyte burstWrapCmd ; + ubyte burstWrapCmdDmyClk ; + ubyte burstWrapDataMode ; + ubyte burstWrapData ; + ubyte deBurstWrapCmd ; + ubyte deBurstWrapCmdDmyClk ; + ubyte deBurstWrapDataMode ; + ubyte deBurstWrapData ; + uint16 timeEsector ; + uint16 timeE32k ; + uint16 timeE64k ; + uint16 timePagePgm ; + uint16 timeCe ; + ubyte pdDelay ; + ubyte qeData ; +} SPI_FLASH_CFG_TYPE; + +typedef struct { + uint32 magic ; + HAL_SYS_CLK_CONFIG cfg; + local uint32 checksum = Checksum(CHECKSUM_CRC32,startof(cfg),sizeof(cfg)); + uint32 crc32 ; + if(checksum != crc32) { + Warning("invalid crc32 for HAL_PLL_CONFIG"); + }; +} HAL_PLL_CONFIG; + +typedef struct { + ubyte xtal_type; + ubyte pll_clk; + ubyte hclk_div; + ubyte bclk_div; + + ubyte flash_clk_type; + ubyte flash_clk_div; + ubyte rsvd[2]; +} HAL_SYS_CLK_CONFIG; + +typedef struct { + uint32 sign : 2 ; + uint32 encrypt_type : 2 ; + uint32 key_sel : 2 ; + uint32 rsvd6_7 : 2 ; + uint32 no_segment : 1 ; + uint32 cache_select : 1 ; + uint32 notload_in_bootrom : 1 ; + uint32 aes_region_lock : 1 ; + uint32 cache_way_disable : 4 ; + uint32 crc_ignore : 1 ; + uint32 hash_ignore : 1 ; + uint32 halt_ap : 1 ; + uint32 rsvd19_31 : 13 ; + + uint32 img_len ; + uint32 rsvd0 ; + uint32 img_start ; + uint32 hash[8] ; +} HAL_BASIC_CONFIG; + +/**** PARSING CODE ****/ +LittleEndian(); + +HAL_BOOTHEADER BootHeader ; + +if (BootHeader.magic != 0x504e4642) +{ + Warning("Invalid Magic.\n"); + return 1; +} + +if(BootHeader.basicCfg.img_len < FileSize()) { + FSeek(BootHeader.basicCfg.img_start); + ubyte image[BootHeader.basicCfg.img_len] ; + local char s[64 + 1]; + local int ret = ChecksumAlgStr(CHECKSUM_SHA256, s, startof(image),sizeof(image)); + if(ret) { + local char ChecksumAlgStrResult[64 + 1]; + local quad k; + for (k = 0; k < 8; k++) { + ChecksumAlgStrResult[k * 8 + 1] = (char)(BootHeader.basicCfg.hash[k] & 0xF); + ChecksumAlgStrResult[k * 8 + 0] = (char)((BootHeader.basicCfg.hash[k] >> 4) & 0xF); + ChecksumAlgStrResult[k * 8 + 3] = (char)((BootHeader.basicCfg.hash[k] >> 8) & 0xF); + ChecksumAlgStrResult[k * 8 + 2] = (char)((BootHeader.basicCfg.hash[k] >> 12) & 0xF); + ChecksumAlgStrResult[k * 8 + 5] = (char)((BootHeader.basicCfg.hash[k] >> 16) & 0xF); + ChecksumAlgStrResult[k * 8 + 4] = (char)((BootHeader.basicCfg.hash[k] >> 20) & 0xF); + ChecksumAlgStrResult[k * 8 + 7] = (char)((BootHeader.basicCfg.hash[k] >> 24) & 0xF); + ChecksumAlgStrResult[k * 8 + 6] = (char)((BootHeader.basicCfg.hash[k] >> 28) & 0xF); + } + for (k = 0; k < 64; k++) { + if (ChecksumAlgStrResult[k] >= 10) { + ChecksumAlgStrResult[k] = ChecksumAlgStrResult[k] + 'A' - 10; + } else { + ChecksumAlgStrResult[k] += '0'; + } + } + ChecksumAlgStrResult[64] = '\0'; + if(Strcmp(s, ChecksumAlgStrResult)) { + Warning("invalid sha256 for the image!"); + } + } else { + Warning("hash check for the image failure"); + } +} else { + Warning("Segment data appears to either overlap with header, exist after the end of the file or overlap with the end of the file!"); +} diff --git a/README.md b/README.md index 6f6f1b4..be4c275 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ | 芯片型号 | 链接 | |:----|:----| +| BL602 | [BL602Image.bt](./BL602Image.bt) | | BL616 | [BL616Image.bt](./BL616Image.bt) | | BL702 | [BL702Image.bt](./BL702Image.bt) | | BL808 | [BL808Image.bt](./BL808Image.bt) | -- Gitee