From e2646f2d537796222440a1e80fae7c08d9623d4d Mon Sep 17 00:00:00 2001 From: Wang Jikai Date: Mon, 19 Jun 2023 15:14:11 +0800 Subject: [PATCH] =?UTF-8?q?bl616:=20=E5=A2=9E=E5=8A=A0=E4=BA=86bl616?= =?UTF-8?q?=E8=8A=AF=E7=89=87=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Wang Jikai --- BL616Image.bt | 304 ++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 3 +- 2 files changed, 306 insertions(+), 1 deletion(-) create mode 100644 BL616Image.bt diff --git a/BL616Image.bt b/BL616Image.bt new file mode 100644 index 0000000..48abbe9 --- /dev/null +++ b/BL616Image.bt @@ -0,0 +1,304 @@ +//------------------------------------------------ +//--- 010 Editor v12.0.1 Binary Template +// +// File: BL616Image +// Authors: Wang Jikai +// 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; +struct HAL_CPU_CFG; +struct HAL_PATCH_CFG; +struct FlashConfig; + +typedef struct { + local int64 start = FTell(); + uint32 magic ; + uint32 revision; + HAL_FLASH_CONFIG flashCfg; + HAL_PLL_CONFIG clkCfg; + HAL_BASIC_CONFIG basicCfg; + HAL_CPU_CFG cpuCfg; + uint32 boot2_pt_table_0 ; + uint32 boot2_pt_table_1 ; + uint32 flashCfgTableAddr ; + uint32 flashCfgTableLen ; + // patch config guessed from header file content + HAL_PATCH_CFG patch_on_read[3] ; + HAL_PATCH_CFG patch_on_jump[3] ; + uint32 reserved[1] ; + 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 mcu_clk; + ubyte mcu_clk_div; + ubyte mcu_bclk_div; + + ubyte mcu_pbclk_div; + ubyte emi_clk; + ubyte emi_clk_div; + ubyte flash_clk_type; + + ubyte flash_clk_div; + ubyte wifipll_pu; + ubyte aupll_pu; + ubyte rsvd0; +} HAL_SYS_CLK_CONFIG; + +typedef struct { + uint32 sign_type : 2 ; + uint32 encrypt_type : 2 ; + uint32 key_sel : 2 ; + uint32 xts_mode : 1 ; + uint32 aes_region_lock : 1 ; + uint32 no_segment : 1 ; + uint32 rsvd_0 : 1 ; + uint32 rsvd_1 : 1 ; + uint32 cpu_master_id : 4 ; + uint32 notload_in_bootrom : 1 ; + uint32 crc_ignore : 1 ; + uint32 hash_ignore : 1 ; + uint32 power_on_mm : 1 ; + uint32 em_sel : 3 ; + uint32 cmds_en : 1 ; + uint32 cmds_wrap_mode : 2 ; + uint32 cmds_wrap_len : 4 ; + uint32 icache_invalid : 1 ; + uint32 dcache_invalid : 1 ; + uint32 rsvd_3 : 1 ; + uint32 group_image_offset ; + uint32 aes_region_len ; + uint32 img_len_cnt ; + uint32 hash[8] ; +} HAL_BASIC_CONFIG; + +typedef struct { + ubyte config_enable ; + ubyte halt_cpu ; + ubyte cache_enable : 1 ; + ubyte cache_wa : 1 ; + ubyte cache_wb : 1 ; + ubyte cache_wt : 1 ; + ubyte cache_way_dis : 4 ; + ubyte rsvd; + + uint32 image_address_offset ; + uint32 rsvd1 ; // boot_entry in BL808 + uint32 msp_val ; +} HAL_CPU_CFG; + +typedef struct { + uint32 addr ; + uint32 value ; +} HAL_PATCH_CFG; + +typedef struct { + uint32 jedec_id ; + uint32 offset ; +} FlashConfig; + +/**** PARSING CODE ****/ +LittleEndian(); + +HAL_BOOTHEADER BootHeader ; + +if (BootHeader.magic != 0x504e4642) +{ + Warning("Invalid Magic.\n"); + return 1; +} + +if (BootHeader.flashCfgTableLen != 0 && BootHeader.flashCfgTableAddr < FileSize() && + BootHeader.flashCfgTableAddr + BootHeader.flashCfgTableLen <= FileSize()) { + FSeek(BootHeader.flashCfgTableAddr); + local quad num_configs = (BootHeader.flashCfgTableLen - 8) / 8; + typedef struct { + uint32 magic ; + FlashConfig config[num_configs]; + local uint32 checksum = Checksum(CHECKSUM_CRC32,startof(config),sizeof(config)); + uint32 crc ; + if(checksum != crc) { + Warning("invalid crc32 for FlashConfigTable!"); + } + } FlashConfigTable; + FlashConfigTable flashConfigTable ; + local quad off = FTell(); + local quad i, j; + local quad min = 0; + local quad minCurr = ~0; + for (i = 0; minCurr > min; i++) { + // each iteration find smallist + minCurr = ~0; + for (j = 0; j min && flashConfigTable.config[j].offset < minCurr) { + minCurr = flashConfigTable.config[j].offset; + } + } + FSeek(minCurr); + min = minCurr; + if (minCurr == ~0) { + break; + } else { + HAL_FLASH_CONFIG conf ; + minCurr = ~0; + } + } + FSeek(off); +} else { + // If flashCfgTableLen == 0, don't raise warning as it is allowed by this file format. + if (BootHeader.flashCfgTableLen != 0) { + Warning("flashCfgTable appears to either overlap with header, exist after the end of the file or overlap with the end of the file!"); + } +} + +if(BootHeader.basicCfg.img_len_cnt < FileSize() && + BootHeader.basicCfg.img_len_cnt + BootHeader.basicCfg.group_image_offset <= FileSize()) { + FSeek(BootHeader.basicCfg.group_image_offset); + ubyte image[BootHeader.basicCfg.img_len_cnt] ; + 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 f532826..3696fcc 100644 --- a/README.md +++ b/README.md @@ -6,12 +6,13 @@ | 芯片型号 | 链接 | |:----|:----| +| BL616 | [BL616Image.bt](./BL616Image.bt) | | BL808 | [BL808Image.bt](./BL808Image.bt) | | D1 | [D1Image.bt](./D1Image.bt) | ## 许可协议 -本项目采用木兰宽松许可证,第2版。 +本项目采用木兰宽松许可证,第2版。 ``` Copyright (c) 2023 RustSBI Team -- Gitee