[feat] add new examples

This commit is contained in:
jzlv 2021-06-04 17:56:34 +08:00
parent bc120c8861
commit 0860ef4d6e
169 changed files with 192122 additions and 149 deletions

View file

@ -0,0 +1,16 @@
list(APPEND ADD_INCLUDE
"${CMAKE_CURRENT_SOURCE_DIR}"
)
set(TARGET_REQUIRED_LIBS xz)
list(APPEND TARGET_REQUIRED_SRCS blsp_common.c blsp_media_boot.c )
list(APPEND TARGET_REQUIRED_SRCS blsp_boot_parser.c blsp_boot_decompress.c blsp_port.c )
list(APPEND TARGET_REQUIRED_SRCS bflb_eflash_loader_uart.c ) #bflb_eflash_loader_gpio.c
list(APPEND TARGET_REQUIRED_SRCS bflb_eflash_loader_cmds.c )
SET(LINKER_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/blsp_boot2_iap_flash.ld)
set(mains blsp_boot2_iap.c)
generate_bin()

View file

@ -0,0 +1,159 @@
/**
******************************************************************************
* @file blsp_eflash_loader.h
* @version V1.2
* @date
* @brief This file is the peripheral case header file
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT(c) 2018 Bouffalo Lab</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of Bouffalo Lab nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
#ifndef __BFLB_EFLASH_LOADER_H__
#define __BFLB_EFLASH_LOADER_H__
/*error code definition*/
typedef enum tag_eflash_loader_error_code_t
{
BFLB_EFLASH_LOADER_SUCCESS=0x00,
/*flash*/
BFLB_EFLASH_LOADER_FLASH_INIT_ERROR=0x0001,
BFLB_EFLASH_LOADER_FLASH_ERASE_PARA_ERROR=0x0002,
BFLB_EFLASH_LOADER_FLASH_ERASE_ERROR=0x0003,
BFLB_EFLASH_LOADER_FLASH_WRITE_PARA_ERROR=0x0004,
BFLB_EFLASH_LOADER_FLASH_WRITE_ADDR_ERROR=0x0005,
BFLB_EFLASH_LOADER_FLASH_WRITE_ERROR=0x0006,
BFLB_EFLASH_LOADER_FLASH_BOOT_PARA_ERROR=0x0007,
BFLB_EFLASH_LOADER_FLASH_SET_PARA_ERROR=0x0008,
BFLB_EFLASH_LOADER_FLASH_READ_STATUS_REG_ERROR=0x0009,
BFLB_EFLASH_LOADER_FLASH_WRITE_STATUS_REG_ERROR=0x000A,
BFLB_EFLASH_LOADER_FLASH_DECOMPRESS_WRITE_ERROR=0x000B,
BFLB_EFLASH_LOADER_FLASH_WRITE_XZ_ERROR=0x000C,
/*cmd*/
BFLB_EFLASH_LOADER_CMD_ID_ERROR =0x0101,
BFLB_EFLASH_LOADER_CMD_LEN_ERROR=0x0102,
BFLB_EFLASH_LOADER_CMD_CRC_ERROR=0x0103,
BFLB_EFLASH_LOADER_CMD_SEQ_ERROR=0x0104,
/*image*/
BFLB_EFLASH_LOADER_IMG_BOOTHEADER_LEN_ERROR=0x0201,
BFLB_EFLASH_LOADER_IMG_BOOTHEADER_NOT_LOAD_ERROR=0x0202,
BFLB_EFLASH_LOADER_IMG_BOOTHEADER_MAGIC_ERROR=0x0203,
BFLB_EFLASH_LOADER_IMG_BOOTHEADER_CRC_ERROR=0x0204,
BFLB_EFLASH_LOADER_IMG_BOOTHEADER_ENCRYPT_NOTFIT=0x0205,
BFLB_EFLASH_LOADER_IMG_BOOTHEADER_SIGN_NOTFIT=0x0206,
BFLB_EFLASH_LOADER_IMG_SEGMENT_CNT_ERROR=0x0207,
BFLB_EFLASH_LOADER_IMG_AES_IV_LEN_ERROR=0x0208,
BFLB_EFLASH_LOADER_IMG_AES_IV_CRC_ERROR=0x0209,
BFLB_EFLASH_LOADER_IMG_PK_LEN_ERROR=0x020a,
BFLB_EFLASH_LOADER_IMG_PK_CRC_ERROR=0x020b,
BFLB_EFLASH_LOADER_IMG_PK_HASH_ERROR=0x020c,
BFLB_EFLASH_LOADER_IMG_SIGNATURE_LEN_ERROR=0x020d,
BFLB_EFLASH_LOADER_IMG_SIGNATURE_CRC_ERROR=0x020e,
BFLB_EFLASH_LOADER_IMG_SECTIONHEADER_LEN_ERROR=0x020f,
BFLB_EFLASH_LOADER_IMG_SECTIONHEADER_CRC_ERROR=0x0210,
BFLB_EFLASH_LOADER_IMG_SECTIONHEADER_DST_ERROR=0x0211,
BFLB_EFLASH_LOADER_IMG_SECTIONDATA_LEN_ERROR=0x0212,
BFLB_EFLASH_LOADER_IMG_SECTIONDATA_DEC_ERROR=0x0213,
BFLB_EFLASH_LOADER_IMG_SECTIONDATA_TLEN_ERROR=0x0214,
BFLB_EFLASH_LOADER_IMG_SECTIONDATA_CRC_ERROR=0x0215,
BFLB_EFLASH_LOADER_IMG_HALFBAKED_ERROR=0x0216,
BFLB_EFLASH_LOADER_IMG_HASH_ERROR=0x0217,
BFLB_EFLASH_LOADER_IMG_SIGN_PARSE_ERROR=0x0218,
BFLB_EFLASH_LOADER_IMG_SIGN_ERROR=0x0219,
BFLB_EFLASH_LOADER_IMG_DEC_ERROR=0x021a,
BFLB_EFLASH_LOADER_IMG_ALL_INVALID_ERROR=0x021b,
/*IF*/
BFLB_EFLASH_LOADER_IF_RATE_LEN_ERROR=0x0301,
BFLB_EFLASH_LOADER_IF_RATE_PARA_ERROR=0x0302,
BFLB_EFLASH_LOADER_IF_PASSWORDERROR=0x0303,
BFLB_EFLASH_LOADER_IF_PASSWORDCLOSE=0x0304,
/*efuse*/
BFLB_EFLASH_LOADER_EFUSE_WRITE_PARA_ERROR=0x0401,
BFLB_EFLASH_LOADER_EFUSE_WRITE_ADDR_ERROR=0x0402,
BFLB_EFLASH_LOADER_EFUSE_WRITE_ERROR=0x0403,
BFLB_EFLASH_LOADER_EFUSE_READ_PARA_ERROR=0x0404,
BFLB_EFLASH_LOADER_EFUSE_READ_ADDR_ERROR=0x0405,
BFLB_EFLASH_LOADER_EFUSE_READ_ERROR=0x0406,
BFLB_EFLASH_LOADER_EFUSE_READ_MAC_ERROR=0x0407,
/*MISC*/
BFLB_EFLASH_LOADER_PLL_ERROR=0xfffc,
BFLB_EFLASH_LOADER_INVASION_ERROR=0xfffd,
BFLB_EFLASH_LOADER_POLLING=0xfffe,
BFLB_EFLASH_LOADER_FAIL=0xffff,
}eflash_loader_error_code_t;
/** private definition**/
#define EFLASH_LOADER_CMD_DISABLE 0x000
#define EFLASH_LOADER_CMD_ENABLE 0x001
#define BFLB_EFLASH_LOADER_CMD_ACK 0x00004B4F
#define BFLB_EFLASH_LOADER_CMD_NACK 0x00004C46
#define BFLB_EFLASH_LOADER_EFUSE0_SIZE 128
/*max data payload in command*/
#define BFLB_EFLASH_LOADER_READBUF_SIZE (1024*16+8)
#define BFLB_EFLASH_LOADER_WRITEBUF_SIZE BFLB_EFLASH_LOADER_READBUF_SIZE
#define BFLB_EFLASH_LOADER_CMD_DATA_MAX_LEN (BFLB_EFLASH_LOADER_READBUF_SIZE-0x04) //cmd+rsvd+len(2bytes)
#define MAXOF(a,b) ((a)>(b)?(a):(b))
#define OFFSET(TYPE, MEMBER) ((uint32_t)(&(((TYPE *)0)->MEMBER)))
#ifndef EFLASH_LOADER_FAST
#define bflb_eflash_loader_printf(...) //bflb_platform_printf(__VA_ARGS__)
#define bflb_eflash_loader_printe(...) //bflb_platform_printf(__VA_ARGS__)
#define bflb_eflash_loader_printd(...) //bflb_platform_printf(__VA_ARGS__)
#else
#define bflb_eflash_loader_printf(...) //bflb_platform_printf(__VA_ARGS__)
#define bflb_eflash_loader_printe(...) //bflb_platform_printf(__VA_ARGS__)
#define bflb_eflash_loader_printd(...) //bflb_platform_printf(__VA_ARGS__)
#endif
#define bflb_eflash_loader_printw(...) //bflb_platform_printf(__VA_ARGS__)
#define bflb_eflash_loader_printx(...) //bflb_platform_printx(__VA_ARGS__)
/*read data buffer from flash or boot interface*/
extern volatile uint32_t g_rx_buf_index;
extern volatile uint32_t g_rx_buf_len;
extern uint32_t g_eflash_loader_readbuf[2][(BFLB_EFLASH_LOADER_READBUF_SIZE+3)/4];
extern uint32_t g_eflash_loader_cmd_ack_buf[16];
uint8_t bootrom_read_boot_mode(void);
void bflb_eflash_loader_init_uart_gpio(uint8_t eflash_loader_uart_pin_select);
void bflb_eflash_loader_deinit_uart_gpio_do(uint8_t eflash_loader_uart_pin_select);
void bflb_eflash_loader_deinit_uart_gpio(uint8_t eflash_loader_uart_pin_select);
void bflb_eflash_loader_init_flash_gpio(uint8_t flash_cfg);
#endif

View file

@ -0,0 +1,343 @@
/**
******************************************************************************
* @file blsp_eflash_loader_cmds.c
* @version V1.2
* @date
* @brief This file is the peripheral case header file
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT(c) 2018 Bouffalo Lab</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of Bouffalo Lab nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
#include "bflb_eflash_loader_cmds.h"
#include "bflb_eflash_loader.h"
#include "bflb_eflash_loader_uart.h"
#include "stdio.h"
#include "stdint.h"
#include "string.h"
#include "softcrc.h"
#include "bflb_platform.h"
#include "partition.h"
#include "bl702_sec_eng.h"
#include "hal_flash.h"
#include "bl702_hbn.h"
#include "bl702_glb.h"
/*for mass read comamnd(flash read and efuse read) is only valid for UART
this is due to ack buffer is g_eflash_loader_readbuf */
#define eflash_loader_cmd_mass_ack_buf g_eflash_loader_readbuf[1]
#define BFLB_EFLASH_LOADER_CHECK_LEN 2048
#define BFLB_EFLASH_MAX_SIZE 2*1024*1024
/*add for verify using SHA-256*/
uint32_t g_sha_tmp_buf[16]={0};
uint32_t g_padding[16]={0};
uint32_t g_sha_in_buf[(BFLB_EFLASH_LOADER_READBUF_SIZE+3)/4]={0};
static uint32_t g_eflash_loader_error=0;
extern struct device * download_uart;
static int32_t bflb_eflash_loader_cmd_read_jedec_id( uint16_t cmd, uint8_t *data, uint16_t len);
static int32_t bflb_eflash_loader_cmd_reset( uint16_t cmd, uint8_t *data, uint16_t len);
static int32_t bflb_eflash_loader_cmd_erase_flash( uint16_t cmd, uint8_t *data, uint16_t len);
static int32_t bflb_eflash_loader_cmd_write_flash( uint16_t cmd, uint8_t *data, uint16_t len);
static int32_t bflb_eflash_loader_cmd_read_flash( uint16_t cmd, uint8_t *data, uint16_t len);
static int32_t bflb_eflash_loader_cmd_readSha_flash( uint16_t cmd, uint8_t *data, uint16_t len);
static int32_t bflb_eflash_loader_cmd_xip_readSha_flash( uint16_t cmd, uint8_t *data, uint16_t len);
static int32_t bflb_eflash_loader_cmd_write_flash_check( uint16_t cmd, uint8_t *data, uint16_t len);
static int32_t bflb_eflash_loader_cmd_set_flash_para( uint16_t cmd, uint8_t *data, uint16_t len);
static int32_t bflb_eflash_loader_cmd_xip_read_flash_start( uint16_t cmd, uint8_t *data, uint16_t len);
static int32_t bflb_eflash_loader_cmd_xip_read_flash_finish( uint16_t cmd, uint8_t *data, uint16_t len);
static const struct eflash_loader_cmd_cfg_t eflash_loader_cmds[]=
{
{BFLB_EFLASH_LOADER_CMD_RESET, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_reset},
{BFLB_EFLASH_LOADER_CMD_FLASH_ERASE, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_erase_flash},
{BFLB_EFLASH_LOADER_CMD_FLASH_WRITE, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_write_flash},
{BFLB_EFLASH_LOADER_CMD_FLASH_READ, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_read_flash},
{BFLB_EFLASH_LOADER_CMD_FLASH_WRITE_CHECK, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_write_flash_check},
{BFLB_EFLASH_LOADER_CMD_FLASH_SET_PARA, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_set_flash_para},
{BFLB_EFLASH_LOADER_CMD_FLASH_READSHA, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_readSha_flash},
{BFLB_EFLASH_LOADER_CMD_FLASH_XIP_READSHA, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_xip_readSha_flash},
{BFLB_EFLASH_LOADER_CMD_XIP_READ_START, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_xip_read_flash_start},
{BFLB_EFLASH_LOADER_CMD_XIP_READ_FINISH, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_xip_read_flash_finish},
{BFLB_EFLASH_LOADER_CMD_FLASH_READ_JEDECID, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_read_jedec_id},
};
/* int boot command control */
void bflb_eflash_loader_cmd_init()
{
g_eflash_loader_error=BFLB_EFLASH_LOADER_SUCCESS;
//download_uart = device_find("download_uart");
}
/* ack host with command process result */
static void bflb_eflash_loader_cmd_ack(uint32_t result)
{
if(result==0){
/*OK*/
g_eflash_loader_cmd_ack_buf[0]=BFLB_EFLASH_LOADER_CMD_ACK;
bflb_eflash_loader_uart_send((uint32_t *)g_eflash_loader_cmd_ack_buf,2);
return;
}else{
/* FL+Error code(2bytes) */
g_eflash_loader_cmd_ack_buf[0]=BFLB_EFLASH_LOADER_CMD_NACK|((result<<16)&0xffff0000);
bflb_eflash_loader_uart_send(g_eflash_loader_cmd_ack_buf,4);
}
}
static int32_t bflb_eflash_loader_cmd_read_jedec_id( uint16_t cmd, uint8_t *data, uint16_t len)
{
uint8_t *ackdata=(uint8_t *)eflash_loader_cmd_mass_ack_buf;
bflb_eflash_loader_printf("JID\n");
eflash_loader_cmd_mass_ack_buf[0]=BFLB_EFLASH_LOADER_CMD_ACK;
/*ack read jedec ID */
ackdata[2]=4;
ackdata[3]=0;
flash_read_jedec_id((uint8_t *)&eflash_loader_cmd_mass_ack_buf[1]);
bflb_eflash_loader_uart_send((uint32_t *)ackdata,4+4);
return BFLB_EFLASH_LOADER_SUCCESS;
}
static int32_t bflb_eflash_loader_cmd_reset( uint16_t cmd, uint8_t *data, uint16_t len)
{
int32_t ret=BFLB_EFLASH_LOADER_SUCCESS;
pt_table_set_iap_para(&p_iap_param);
pt_table_dump();
bflb_eflash_loader_printf("RST\n");
bflb_eflash_loader_cmd_ack(ret);
bflb_eflash_loader_usart_wait_tx_idle(BFLB_EFLASH_LOADER_IF_TX_IDLE_TIMEOUT);
/* add for bl702, will impact on boot pin read */
HBN_Set_Status_Flag(0x594c440B);
/* FPGA POR RST NOT work,so add system reset */
bflb_platform_delay_us(10);
GLB_SW_System_Reset();
return ret;
}
static int32_t bflb_eflash_loader_cmd_erase_flash( uint16_t cmd, uint8_t *data, uint16_t len)
{
int32_t ret=BFLB_EFLASH_LOADER_SUCCESS;
uint32_t startaddr,endaddr;
bflb_eflash_loader_printf("E\n");
if(len!=8){
ret=BFLB_EFLASH_LOADER_FLASH_ERASE_PARA_ERROR;
}else{
/*clean write error, since write usually behand erase*/
g_eflash_loader_error=BFLB_EFLASH_LOADER_SUCCESS;
memcpy(&startaddr,data,4);
memcpy(&endaddr,data+4,4);
p_iap_param.iap_img_len=endaddr - startaddr + 1;
bflb_eflash_loader_printd("from%08xto%08x\n",p_iap_param.iap_start_addr,p_iap_param.iap_start_addr + p_iap_param.iap_img_len - 1);
if(SUCCESS!=flash_erase_xip(p_iap_param.iap_start_addr,p_iap_param.iap_start_addr + p_iap_param.iap_img_len-1))
{
bflb_eflash_loader_printe("fail\n");
ret=BFLB_EFLASH_LOADER_FLASH_ERASE_ERROR;
}
}
bflb_eflash_loader_cmd_ack(ret);
return ret;
}
static int32_t ATTR_TCM_SECTION bflb_eflash_loader_cmd_write_flash( uint16_t cmd, uint8_t *data, uint16_t len)
{
int32_t ret=BFLB_EFLASH_LOADER_SUCCESS;
uint32_t write_len;
//bflb_eflash_loader_printf("W\n");
if(len<=4){
ret=BFLB_EFLASH_LOADER_FLASH_WRITE_PARA_ERROR;
}else{
//memcpy(&startaddr,data,4);
write_len=len-4;
//MSG("to%08x,%d\n",p_iap_param.iap_write_addr,write_len);
bflb_platform_clear_time();
if(p_iap_param.iap_write_addr<0xffffffff){
bflb_eflash_loader_cmd_ack(ret);
if(SUCCESS!=flash_write_xip(p_iap_param.iap_write_addr,data+4,write_len))
{
/*error , response again with error */
bflb_eflash_loader_printe("fail\r\n");
ret=BFLB_EFLASH_LOADER_FLASH_WRITE_ERROR;
g_eflash_loader_error=ret;
}else{
p_iap_param.iap_write_addr += write_len;
//bflb_eflash_loader_printe("Write suss\r\n");
return BFLB_EFLASH_LOADER_SUCCESS;
}
}else{
ret=BFLB_EFLASH_LOADER_FLASH_WRITE_ADDR_ERROR;
}
bflb_eflash_loader_printd("%d\n",bflb_platform_get_time_us());
}
bflb_eflash_loader_cmd_ack(ret);
return ret;
}
static int32_t bflb_eflash_loader_cmd_read_flash( uint16_t cmd, uint8_t *data, uint16_t len)
{
return BFLB_EFLASH_LOADER_SUCCESS;
}
static int32_t bflb_eflash_loader_cmd_xip_read_flash_start( uint16_t cmd, uint8_t *data, uint16_t len)
{
int32_t ret=BFLB_EFLASH_LOADER_SUCCESS;
bflb_eflash_loader_cmd_ack(ret);
return ret;
}
static int32_t bflb_eflash_loader_cmd_xip_read_flash_finish( uint16_t cmd, uint8_t *data, uint16_t len)
{
int32_t ret=BFLB_EFLASH_LOADER_SUCCESS;
//bflb_eflash_loader_printf("exit\n");
bflb_eflash_loader_cmd_ack(ret);
return ret;
}
static int32_t bflb_eflash_loader_cmd_readSha_flash( uint16_t cmd, uint8_t *data, uint16_t len)
{
return BFLB_EFLASH_LOADER_SUCCESS;
}
static int32_t bflb_eflash_loader_cmd_xip_readSha_flash( uint16_t cmd, uint8_t *data, uint16_t len)
{
int32_t ret=BFLB_EFLASH_LOADER_SUCCESS;
uint32_t startaddr,read_len;
SEC_Eng_SHA256_Ctx sha_ctx;
SEC_ENG_SHA_ID_Type shaId=SEC_ENG_SHA_ID0;
uint16_t sha_len=32;
uint8_t *ackdata=(uint8_t *)eflash_loader_cmd_mass_ack_buf;
bflb_eflash_loader_printf("XRSha\n");
if(len!=8){
ret=BFLB_EFLASH_LOADER_FLASH_WRITE_PARA_ERROR;
bflb_eflash_loader_cmd_ack(ret);
}else{
startaddr = p_iap_param.iap_start_addr;
read_len = p_iap_param.iap_img_len;
bflb_eflash_loader_printd("from%08x,%d\n",startaddr,read_len);
//MSG("!!!Be careful that SHA input data should locate at OCRAM \n");
/* Init SHA and input SHA temp buffer for scattered data and g_padding buffer */
Sec_Eng_SHA256_Init(&sha_ctx,shaId,SEC_ENG_SHA256,g_sha_tmp_buf,g_padding );
Sec_Eng_SHA_Start(shaId);
while(read_len>0){
if(read_len>BFLB_EFLASH_LOADER_READBUF_SIZE){
flash_read_xip(startaddr,(uint8_t *)g_sha_in_buf,BFLB_EFLASH_LOADER_READBUF_SIZE);
/*cal sha here*/
Sec_Eng_SHA256_Update(&sha_ctx,shaId,(uint8_t *)g_sha_in_buf,BFLB_EFLASH_LOADER_READBUF_SIZE );
read_len-=BFLB_EFLASH_LOADER_READBUF_SIZE;
startaddr+=BFLB_EFLASH_LOADER_READBUF_SIZE;
}else{
flash_read_xip(startaddr,(uint8_t *)g_sha_in_buf,read_len);
/*cal sha here*/
Sec_Eng_SHA256_Update(&sha_ctx,shaId,(uint8_t *)g_sha_in_buf,read_len );
read_len-=read_len;
startaddr+=read_len;
}
}
Sec_Eng_SHA256_Finish(&sha_ctx,shaId,&ackdata[4]);
for(sha_len=0;sha_len<32;sha_len++){
bflb_eflash_loader_printf("\r\n");
bflb_eflash_loader_printx(ackdata[4+sha_len]);
}
sha_len=32;
/*ack read data */
ackdata[0]=BFLB_EFLASH_LOADER_CMD_ACK&0xff;
ackdata[1]=(BFLB_EFLASH_LOADER_CMD_ACK>>8)&0xff;
ackdata[2]=sha_len&0xff;
ackdata[3]=(sha_len>>8)&0xff;
bflb_eflash_loader_uart_send((uint32_t*)ackdata,sha_len+4);
}
return ret;
}
static int32_t bflb_eflash_loader_cmd_write_flash_check( uint16_t cmd, uint8_t *data, uint16_t len)
{
bflb_eflash_loader_printf("WC\n");
bflb_eflash_loader_cmd_ack(g_eflash_loader_error);
return BFLB_EFLASH_LOADER_SUCCESS;
}
static int32_t bflb_eflash_loader_cmd_set_flash_para( uint16_t cmd, uint8_t *data, uint16_t len)
{
bflb_eflash_loader_cmd_ack(BFLB_EFLASH_LOADER_SUCCESS);
return BFLB_EFLASH_LOADER_SUCCESS;
}
int32_t bflb_eflash_loader_cmd_process(uint8_t cmdid,uint8_t *data,uint16_t len)
{
int i=0;
int32_t ret=BFLB_EFLASH_LOADER_SUCCESS;
for(i=0;i<sizeof(eflash_loader_cmds)/sizeof(eflash_loader_cmds[0]);i++)
{
if(eflash_loader_cmds[i].cmd==cmdid){
if(EFLASH_LOADER_CMD_ENABLE==eflash_loader_cmds[i].enabled&&NULL!=eflash_loader_cmds[i].cmd_process){
ret=eflash_loader_cmds[i].cmd_process(cmdid,data,len);
}else{
return BFLB_EFLASH_LOADER_CMD_ID_ERROR;
}
break;
}
}
return ret;
}

View file

@ -0,0 +1,112 @@
/**
******************************************************************************
* @file blsp_eflash_loader_cmds.h
* @version V1.2
* @date
* @brief This file is the peripheral case header file
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT(c) 2018 Bouffalo Lab</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of Bouffalo Lab nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
#ifndef __BFLB_EFLASH_LOADER_CMDS_H__
#define __BFLB_EFLASH_LOADER_CMDS_H__
#include "stdint.h"
#define BFLB_EFLASH_LOADER_CMD_GET_BOOTINFO 0x0010
#define BFLB_EFLASH_LOADER_CMD_LOAD_BOOTHEADER 0x0011
#define BFLB_EFLASH_LOADER_CMD_LOAD_PUBLICKEY 0x0012
#define BFLB_EFLASH_LOADER_CMD_LOAD_PUBLICKEY2 0x0013
#define BFLB_EFLASH_LOADER_CMD_LOAD_SIGNATURE 0x0014
#define BFLB_EFLASH_LOADER_CMD_LOAD_SIGNATURE2 0x0015
#define BFLB_EFLASH_LOADER_CMD_LOAD_AESIV 0x0016
#define BFLB_EFLASH_LOADER_CMD_LOAD_SEGHEADER 0x0017
#define BFLB_EFLASH_LOADER_CMD_LOAD_SEGDATA 0x0018
#define BFLB_EFLASH_LOADER_CMD_CHECK_IMG 0x0019
#define BFLB_EFLASH_LOADER_CMD_RUN 0x001A
#define BFLB_EFLASH_LOADER_CMD_CHANGE_RATE 0x0020
#define BFLB_EFLASH_LOADER_CMD_RESET 0x0021
#define BFLB_EFLASH_LOADER_CMD_FLASH_ERASE 0x0030
#define BFLB_EFLASH_LOADER_CMD_FLASH_WRITE 0x0031
#define BFLB_EFLASH_LOADER_CMD_FLASH_READ 0x0032
#define BFLB_EFLASH_LOADER_CMD_FLASH_BOOT 0x0033
#define BFLB_EFLASH_LOADER_CMD_FLASH_WRITE_CHECK 0x003A
#define BFLB_EFLASH_LOADER_CMD_FLASH_SET_PARA 0x003B
#define BFLB_EFLASH_LOADER_CMD_FLASH_CHIPERASE 0x003C
#define BFLB_EFLASH_LOADER_CMD_FLASH_READSHA 0x003D
#define BFLB_EFLASH_LOADER_CMD_FLASH_XIP_READSHA 0x003E
#define BFLB_EFLASH_LOADER_CMD_FLASH_XIP_READ 0x0034
#define BFLB_EFLASH_LOADER_CMD_FLASH_SBUS_XIP_READ 0x0035
#define BFLB_EFLASH_LOADER_CMD_FLASH_READ_JEDECID 0x0036
#define BFLB_EFLASH_LOADER_CMD_FLASH_READ_STATUS_REG 0x0037
#define BFLB_EFLASH_LOADER_CMD_FLASH_WRITE_STATUS_REG 0x0038
#define BFLB_EFLASH_LOADER_CMD_EFUSE_WRITE 0x0040
#define BFLB_EFLASH_LOADER_CMD_EFUSE_READ 0x0041
#define BFLB_EFLASH_LOADER_CMD_EFUSE_READ_MAC_ADDR 0x0042
#define BFLB_EFLASH_LOADER_CMD_MEM_WRITE 0x0050
#define BFLB_EFLASH_LOADER_CMD_MEM_READ 0x0051
#define BFLB_EFLASH_LOADER_CMD_LOG_READ 0x0071
#define BFLB_EFLASH_LOADER_CMD_XIP_READ_START 0x0060
#define BFLB_EFLASH_LOADER_CMD_XIP_READ_FINISH 0x0061
#define BFLB_EFLASH_LOADER_CMD_GET_ECDH_PK 0x0090
#define BFLB_EFLASH_LOADER_CMD_ECDH_CHANLLENGE 0x0091
/* public key hash type and length */
#define BFLB_EFLASH_LOADER_PK_HASH_TYPE BFLB_HASH_TYPE_SHA256
#define BFLB_EFLASH_LOADER_PK_HASH_SIZE 256/8
#define BFLB_EFLASH_LOADER_HASH_TYPE BFLB_HASH_TYPE_SHA256
#define BFLB_EFLASH_LOADER_AES_TYPE BFLB_CRYPT_TYPE_AES_CBC
#define BFLB_EFLASH_LOADER_HASH_SIZE 256/8
void bflb_eflash_loader_cmd_init(void);
void bflb_eflash_loader_cmd_disable(uint8_t cmdid);
void bflb_eflash_loader_cmd_enable(uint8_t cmdid);
int32_t bflb_eflash_loader_cmd_process(uint8_t cmdid, uint8_t *data, uint16_t len);
typedef int32_t (*pfun_cmd_process)( uint16_t cmd, uint8_t *data, uint16_t len);
struct eflash_loader_cmd_cfg_t
{
uint8_t cmd;
uint8_t enabled;
pfun_cmd_process cmd_process;
};
#endif

View file

@ -0,0 +1,340 @@
/**
******************************************************************************
* @file blsp_eflash_loader_uart.c
* @version V1.2
* @date
* @brief This file is the peripheral case header file
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT(c) 2018 Bouffalo Lab</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of Bouffalo Lab nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
#include "bflb_eflash_loader_uart.h"
#include "bflb_eflash_loader_cmds.h"
#include "bflb_eflash_loader.h"
#include "bflb_platform.h"
#include "partition.h"
#include "hal_uart.h"
#include "drv_device.h"
#define LOCAL_FILE_NUM (0x0003)
#define UART_CLOCK (40000000)
static uint32_t g_detected_baudrate;
/* data buffer for read data and decrypt */
volatile uint32_t g_rx_buf_index=0;
volatile uint32_t g_rx_buf_len=0;
uint32_t g_eflash_loader_readbuf[2][(BFLB_EFLASH_LOADER_READBUF_SIZE+3)/4];
uint32_t g_eflash_loader_cmd_ack_buf[16];
static void bflb_eflash_loader_usart_if_deinit();
struct device * download_uart = NULL;
enum uart_index_type board_get_debug_uart_index(void)
{
return 1;
}
static void ATTR_TCM_SECTION uart0_irq_callback(struct device *dev, void *args, uint32_t size, uint32_t state)
{
uint8_t *buf=(uint8_t *)g_eflash_loader_readbuf[g_rx_buf_index];
if (state == UART_EVENT_RX_FIFO)
{
//g_rx_buf_len += device_read(download_uart,0,buf,BFLB_EFLASH_LOADER_READBUF_SIZE-g_rx_buf_len);
BL702_MemCpy(buf+g_rx_buf_len,args,size);
g_rx_buf_len += size;
}
else if (state == UART_EVENT_RTO)
{
//g_rx_buf_len += device_read(download_uart,0,buf,BFLB_EFLASH_LOADER_READBUF_SIZE-g_rx_buf_len);
BL702_MemCpy(buf+g_rx_buf_len,args,size);
g_rx_buf_len += size;
}
}
static void bflb_eflash_loader_usart_if_init(uint32_t bdrate)
{
uart_register(0, "download_uart", DEVICE_OFLAG_RDWR);
download_uart = device_find("download_uart");
if (download_uart)
{
UART_DEV(download_uart)->fifo_threshold = 16;
device_open(download_uart, DEVICE_OFLAG_STREAM_TX);
}
}
void bflb_eflash_loader_usart_if_enable_int(void)
{
struct device *uart = device_find("download_uart");
if(uart){
device_close(uart);
device_open(download_uart, DEVICE_OFLAG_STREAM_TX | DEVICE_OFLAG_INT_RX);
device_set_callback(uart, uart0_irq_callback);
device_control(uart, DEVICE_CTRL_SET_INT, (void *)(UART_RX_FIFO_IT|UART_RTO_IT));
}
}
void bflb_eflash_loader_usart_if_send(uint8_t *data,uint32_t len)
{
device_write(download_uart, 0, data, len);
}
int32_t bflb_eflash_loader_usart_if_wait_tx_idle(uint32_t timeout)
{
/*UART now can't judge tx idle now*/
bflb_platform_delay_ms(timeout);
return 0;
}
static uint32_t *bflb_eflash_loader_usart_if_receive(uint32_t *recv_len,uint16_t maxlen,uint16_t timeout)
{
uint8_t *buf=(uint8_t *)g_eflash_loader_readbuf[g_rx_buf_index];
uint16_t datalen=0;
bflb_platform_clear_time();
do
{
if(g_rx_buf_len>=4){
/* receive cmd id and data len*/
datalen=buf[2]+(buf[3]<<8);
if(g_rx_buf_len==datalen+4){
/*receive all the payload,return */
/* move on to next buffer */
g_rx_buf_index=(g_rx_buf_index+1)%2;
g_rx_buf_len=0;
if(datalen<=BFLB_EFLASH_LOADER_CMD_DATA_MAX_LEN){
*recv_len=datalen+4;
return (uint32_t *)buf;
} else {
*recv_len=0;
return NULL;
}
}
}
}while(bflb_platform_get_time_ms()<timeout);
*recv_len=0;
return NULL;
}
static void bflb_eflash_loader_usart_if_deinit()
{
struct device *uart = device_find("download_uart");
if (uart)
{
device_close(uart);
}
}
int32_t bflb_eflash_loader_uart_init()
{
//bflb_eflash_loader_usart_if_deinit();
bflb_eflash_loader_usart_if_init(0);
return BFLB_EFLASH_LOADER_SUCCESS;
}
int32_t bflb_eflash_loader_uart_handshake_poll()
{
uint8_t buf[128];
uint32_t i;
uint32_t handshake_count = 0;
uint32_t rcv_buf_len=0;
//rcv_buf_len = UART_ReceiveData(g_uart_if_id,buf,128);
struct device *download_uart = device_find("download_uart");
if (download_uart)
{
rcv_buf_len = device_read(download_uart, 0,buf,128);
}
//while(1)
{
if(rcv_buf_len>=16)
{
for(i = 0; i < 16; i++)
{
if(buf[i] == BFLB_EFLASH_LOADER_HAND_SHAKE_BYTE){
handshake_count++;
if(handshake_count > BFLB_EFLASH_LAODER_HAND_SHAKE_SUSS_COUNT)
break;
}
}
}
}
if(handshake_count >= BFLB_EFLASH_LAODER_HAND_SHAKE_SUSS_COUNT){
//reinit uart
bflb_eflash_loader_printf("handshake %d 0x55 rcv\r\n",handshake_count);
}
else{
//bflb_eflash_loader_printf("handshake err\r\n");
return BFLB_EFLASH_LOADER_HANDSHAKE_FAIL;
}
/*receive shake hanad signal*/
bflb_eflash_loader_usart_if_send((uint8_t*)"OK",2);
bflb_platform_delay_ms(400);
/* consume the remaining bytes when shake hand(0x55) if needed */
rcv_buf_len = device_read(download_uart, 0,buf,128);//UART_ReceiveData(g_uart_if_id,buf,128);
bflb_eflash_loader_printf("bflb_eflash_loader_usart_if_receive len %d\r\n",rcv_buf_len);
/*init rx info */
g_rx_buf_index=0;
g_rx_buf_len=0;
BL_WR_WORD(g_eflash_loader_readbuf[g_rx_buf_index],0);
bflb_eflash_loader_usart_if_enable_int();
return BFLB_EFLASH_LOADER_HANDSHAKE_SUSS;
}
uint32_t *bflb_eflash_loader_uart_recv(uint32_t *recv_len,uint32_t maxlen,uint32_t timeout)
{
return bflb_eflash_loader_usart_if_receive(recv_len,maxlen,timeout);
}
int32_t bflb_eflash_loader_uart_send(uint32_t *data,uint32_t len)
{
bflb_eflash_loader_usart_if_send((uint8_t*)data,len);
return BFLB_EFLASH_LOADER_SUCCESS;
}
int32_t bflb_eflash_loader_usart_wait_tx_idle(uint32_t timeout)
{
return bflb_eflash_loader_usart_if_wait_tx_idle(timeout);
}
int32_t bflb_eflash_loader_uart_change_rate(uint32_t oldval, uint32_t newval)
{
uint32_t b=(uint32_t)((g_detected_baudrate*1.0*newval)/oldval);
bflb_eflash_loader_printf("BDR:");
bflb_eflash_loader_printx(oldval);
bflb_eflash_loader_printx(newval);
bflb_eflash_loader_printx(g_detected_baudrate);
bflb_eflash_loader_printx(b);
bflb_eflash_loader_usart_if_wait_tx_idle(BFLB_EFLASH_LOADER_IF_TX_IDLE_TIMEOUT);
bflb_eflash_loader_usart_if_init(b);
bflb_eflash_loader_usart_if_send((uint8_t *)"OK",2);
return BFLB_EFLASH_LOADER_SUCCESS;
}
int32_t bflb_eflash_loader_uart_deinit()
{
/* delete uart deinit, when uart tx(gpio16) set input function, uart send 0xFF to uart tx fifo
bflb_eflash_loader_deinit_uart_gpio(g_abr_gpio_sel);
bflb_eflash_loader_usart_if_deinit();
*/
return BFLB_EFLASH_LOADER_SUCCESS;
}
void bflb_eflash_loader_main()
{
int32_t ret;
uint32_t total_len;
uint32_t i,tmp,cmd_len;
uint8_t *recv_buf=NULL;
uint8_t err_cnt=0;
uint8_t to_cnt=0;
bflb_eflash_loader_printf("bflb_eflash_loader_main\r\n");
pt_table_dump();
pt_table_get_iap_para(&p_iap_param);
bflb_eflash_loader_cmd_init();
while(1){
to_cnt=0;
total_len=0;
do{
total_len=0;
recv_buf=(uint8_t *)bflb_eflash_loader_uart_recv(&total_len,BFLB_EFLASH_LOADER_READBUF_SIZE,BFLB_EFLASH_LOADER_IF_UART_RX_TIMEOUT);
if(total_len<=0){
to_cnt++;
}
}while(to_cnt<2&&total_len<=0);
if(to_cnt>=2||total_len<=0){
bflb_eflash_loader_printf("rcv err break\r\n");
break;
}
//bflb_eflash_loader_printf("Recv\r\n");
//eflash_loader_dump_data(recv_buf,total_len);
cmd_len=recv_buf[2]+(recv_buf[3]<<8);
bflb_eflash_loader_printf("cmd_len %d\r\n",cmd_len);
/* Check checksum*/
if(recv_buf[1]!=0){
tmp=0;
for(i=2;i<cmd_len+4;i++){
tmp+=recv_buf[i];
}
if((tmp&0xff)!=recv_buf[1]){
/* FL+Error code(2bytes) */
bflb_eflash_loader_printf("Checksum error %02x\r\n",tmp&0xff);
g_eflash_loader_cmd_ack_buf[0]=BFLB_EFLASH_LOADER_CMD_NACK|((BFLB_EFLASH_LOADER_CMD_CRC_ERROR<<16)&0xffff0000);
bflb_eflash_loader_uart_send(g_eflash_loader_cmd_ack_buf,4);
continue;
}
}
ret=bflb_eflash_loader_cmd_process(recv_buf[0],recv_buf+4,cmd_len);
if(ret!=BFLB_EFLASH_LOADER_SUCCESS){
bflb_eflash_loader_printf(" CMD Pro Ret %d\r\n",ret);
err_cnt++;
if(err_cnt>2){
break;
}
}
}
/* read data finished,deinit and go on*/
bflb_eflash_loader_uart_deinit();
}

View file

@ -0,0 +1,72 @@
/**
******************************************************************************
* @file blsp_eflash_loader_uart.h
* @version V1.2
* @date
* @brief This file is the peripheral case header file
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT(c) 2018 Bouffalo Lab</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of Bouffalo Lab nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
#ifndef __BFLB_EFLASH_LOADER_UART_H__
#define __BFLB_EFLASH_LOADER_UART_H__
#include "stdint.h"
int32_t bflb_eflash_loader_uart_init();
int32_t bflb_eflash_loader_uart_handshake_poll();
uint32_t *bflb_eflash_loader_uart_recv(uint32_t *recv_len,uint32_t maxlen,uint32_t timeout);
int32_t bflb_eflash_loader_uart_send(uint32_t *data,uint32_t len);
int32_t bflb_eflash_loader_usart_wait_tx_idle(uint32_t timeout);
int32_t bflb_eflash_loader_uart_change_rate(uint32_t oldval,uint32_t newval);
int32_t bflb_eflash_loader_uart_deinit(void);
void bflb_eflash_loader_main();
typedef enum{
BFLB_EFLASH_LOADER_HANDSHAKE_SUSS = 0,
BFLB_EFLASH_LOADER_HANDSHAKE_FAIL,
}eflash_handshake_ret;
#define AUTO_BAUDRATE_CHECK_TIME_MAX_MS 2
#define BFLB_EFLASH_LOADER_IF_TX_IDLE_TIMEOUT 4 /*ms*/
#define BFLB_EFLASH_LOADER_IF_UART_RX_TIMEOUT 8000 /*ms*/
#define BFLB_EFLASH_LOADER_HAND_SHAKE_BYTE 0x55
#define BFLB_EFLASH_LAODER_HAND_SHAKE_SUSS_COUNT 5
#endif

View file

@ -0,0 +1,495 @@
/**
******************************************************************************
* @file blsp_boot2.c
* @version V1.2
* @date
* @brief This file is the peripheral case c file
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT(c) 2020 Bouffalo Lab</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of Bouffalo Lab nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
#include "bflb_platform.h"
#include "blsp_port.h"
#include "blsp_bootinfo.h"
#include "blsp_media_boot.h"
#include "partition.h"
#include "blsp_boot_decompress.h"
#include "blsp_common.h"
#include "softcrc.h"
#include "bflb_eflash_loader_uart.h"
#include "hal_uart.h"
#include "hal_flash.h"
#include "blsp_version.h"
/** @addtogroup BL606_BLSP_Boot2
* @{
*/
/** @addtogroup BLSP_BOOT2
* @{
*/
/** @defgroup BLSP_BOOT2_Private_Macros
* @{
*/
/*@} end of group BLSP_BOOT2_Private_Macros */
/** @defgroup BLSP_BOOT2_Private_Types
* @{
*/
/*@} end of group BLSP_BOOT2_Private_Types */
/** @defgroup BLSP_BOOT2_Private_Variables
* @{
*/
/*@} end of group BLSP_BOOT2_Private_Variables */
/** @defgroup BLSP_BOOT2_Global_Variables
* @{
*/
uint8_t g_boot2_read_buf[BFLB_BOOT2_READBUF_SIZE] __attribute__((section(".noinit_data")));
boot_image_config g_boot_img_cfg[2];
boot_cpu_config g_boot_cpu_cfg[2]=
{
/*CPU0 boot cfg*/
{
.msp_store_addr=0,
.pc_store_addr=0,
.default_xip_addr=0x23000000,
},
/*CPU1 boot cfg*/
{
.msp_store_addr=BFLB_BOOT2_CPU1_APP_MSP_ADDR,
.pc_store_addr=BFLB_BOOT2_CPU1_APP_PC_ADDR,
.default_xip_addr=0x23000000,
}
};
boot_efuse_hw_config g_efuse_cfg;
uint8_t g_ps_mode=BFLB_PSM_ACTIVE;
uint8_t g_cpu_count;
uint32_t g_user_hash_ignored=0;
int32_t blsp_boot2_get_clk_cfg(boot_clk_config *cfg);
void blsp_boot2_get_efuse_cfg(boot_efuse_hw_config *g_efuse_cfg);
/*@} end of group BLSP_BOOT2_Global_Variables */
/** @defgroup BLSP_BOOT2_Private_Fun_Declaration
* @{
*/
/*@} end of group BLSP_BOOT2_Private_Fun_Declaration */
/** @defgroup BLSP_BOOT2_Private_Functions_User_Define
* @{
*/
/*@} end of group BLSP_BOOT2_Private_Functions_User_Define */
/** @defgroup BLSP_BOOT2_Private_Functions
* @{
*/
/****************************************************************************//**
* @brief Boot2 runs error call back function
*
* @param log: Log to print
*
* @return None
*
*******************************************************************************/
static void blsp_boot2_on_error(void *log)
{
while(1){
if(BFLB_EFLASH_LOADER_HANDSHAKE_SUSS == bflb_eflash_loader_uart_handshake_poll()){
bflb_eflash_loader_main();
}
MSG_ERR("%s\r\n",(char*)log);
ARCH_Delay_MS(500);
}
}
/****************************************************************************//**
* @brief Boot2 Dump partition entry
*
* @param ptEntry: Partition entry pointer to dump
*
* @return None
*
*******************************************************************************/
static void blsp_dump_pt_entry(pt_table_entry_config *pt_entry)
{
MSG("Name=%s\r\n",pt_entry->name);
MSG("Age=%d\r\n",(unsigned int)pt_entry->age);
MSG("active Index=%d\r\n",(unsigned int)pt_entry->active_index);
MSG("active start_address=%08x\r\n",(unsigned int)pt_entry->start_address[pt_entry->active_index]);
}
/****************************************************************************//**
* @brief Boot2 check XZ FW and do decompression
*
* @param activeID: Active partition table ID
* @param ptStuff: Pointer of partition table stuff
* @param ptEntry: Pointer of active entry
*
* @return 1 for find XZ FW and decompress success, 0 for other cases
*
*******************************************************************************/
#if 0
static int BLSP_Boot2_Check_XZ_FW(pt_table_id_type activeID,pt_table_stuff_config *ptStuff,pt_table_entry_config *ptEntry)
{
uint8_t buf[6];
if(BFLB_BOOT2_SUCCESS!=blsp_mediaboot_read(ptEntry->start_address[ptEntry->active_index],buf,sizeof(buf))){
MSG_ERR("Read fw fail\r\n");
return 0;
}
if(blsp_boot2_dump_critical_flag()){
blsp_dump_data(buf,sizeof(buf));
}
if(blsp_boot2_verify_xz_header(buf)==1){
MSG_DBG("XZ image\r\n");
if(BFLB_BOOT2_SUCCESS==blsp_boot2_update_fw(activeID,ptStuff,ptEntry)){
return 1;
}else{
MSG_ERR("Img decompress fail\r\n");
/* Set flag to make it not boot */
ptEntry->active_index=0;
ptEntry->start_address[0]=0;
return 0;
}
}
return 0;
}
#endif
/****************************************************************************//**
* @brief Boot2 copy firmware from OTA region to normal region
*
* @param activeID: Active partition table ID
* @param ptStuff: Pointer of partition table stuff
* @param ptEntry: Pointer of active entry
*
* @return BL_Err_Type
*
*******************************************************************************/
static int blsp_boot2_do_fw_copy(pt_table_id_type active_id,pt_table_stuff_config *pt_stuff,pt_table_entry_config *pt_entry)
{
uint8_t active_index=pt_entry->active_index;
uint32_t src_address=pt_entry->start_address[active_index&0x01];
uint32_t dest_address=pt_entry->start_address[!(active_index&0x01)];
uint32_t dest_max_size= pt_entry->max_len[!(active_index&0x01)];
uint32_t total_len=pt_entry->len;
uint32_t deal_len=0;
uint32_t cur_len=0;
if(SUCCESS!=flash_erase_xip(dest_address,dest_address+dest_max_size-1)){
MSG_ERR("Erase flash fail");
return BFLB_BOOT2_FLASH_ERASE_ERROR;
}
while(deal_len<total_len){
cur_len=total_len-deal_len;
if(cur_len>sizeof(g_boot2_read_buf)){
cur_len=sizeof(g_boot2_read_buf);
}
if(BFLB_BOOT2_SUCCESS!=blsp_mediaboot_read(src_address,g_boot2_read_buf,cur_len)){
MSG_ERR("Read FW fail when copy\r\n");
return BFLB_BOOT2_FLASH_READ_ERROR;
}
if(SUCCESS!=flash_write_xip(dest_address,g_boot2_read_buf,cur_len)){
MSG_ERR("Write flash fail");
return BFLB_BOOT2_FLASH_WRITE_ERROR;
}
src_address+=cur_len;
dest_address+=cur_len;
deal_len+=cur_len;
}
return BFLB_BOOT2_SUCCESS;
}
/****************************************************************************//**
* @brief Boot2 deal with one firmware
*
* @param activeID: Active partition table ID
* @param ptStuff: Pointer of partition table stuff
* @param ptEntry: Pointer of active entry
* @param fwName: Firmware name pointer
* @param type: Firmware name ID
*
* @return 0 for partition table changed,need re-parse,1 for partition table or entry parsed successfully
*
*******************************************************************************/
static int blsp_boot2_deal_one_fw(pt_table_id_type active_id,pt_table_stuff_config *pt_stuff,
pt_table_entry_config *pt_entry,uint8_t *fw_name,
pt_table_entry_type type)
{
uint32_t ret;
if(fw_name!=NULL){
MSG_DBG("Get FW:%s\r\n",fw_name);
ret=pt_table_get_active_entries_by_name(pt_stuff,fw_name,pt_entry);
}else{
MSG_DBG("Get FW ID:%d\r\n",type);
ret=pt_table_get_active_entries_by_id(pt_stuff,type,pt_entry);
}
if(PT_ERROR_SUCCESS!=ret){
MSG_ERR("Entry not found\r\n");
}else{
blsp_dump_pt_entry(pt_entry);
MSG_DBG("Check Img\r\n");
//if(BLSP_Boot2_Check_XZ_FW(activeID,ptStuff,ptEntry)==1){
//return 0;
//}
/* Check if this partition need copy */
if(pt_entry->active_index>=2){
if(BFLB_BOOT2_SUCCESS==blsp_boot2_do_fw_copy(active_id,pt_stuff,pt_entry)){
return 0;
}
}
}
return 1;
}
/****************************************************************************//**
* @brief Boot2 Roll back pt entry
*
* @param activeID: Active partition table ID
* @param ptStuff: Pointer of partition table stuff
* @param ptEntry: Pointer of active entry
*
* @return boot_error_code
*
*******************************************************************************/
#ifdef BLSP_BOOT2_ROLLBACK
static int32_t blsp_boot2_rollback_ptentry(pt_table_id_type active_id,pt_table_stuff_config *pt_stuff,pt_table_entry_config *pt_entry)
{
int32_t ret;
pt_entry->active_index=!(pt_entry->active_index&0x01);
pt_entry->age++;
ret=pt_table_update_entry((pt_table_id_type)(!active_id),pt_stuff,pt_entry);
if(ret!=PT_ERROR_SUCCESS){
MSG_ERR("Update PT entry fail\r\n");
return BFLB_BOOT2_FAIL;
}
return BFLB_BOOT2_SUCCESS;
}
#endif
/*@} end of group BLSP_BOOT2_Private_Functions */
/** @defgroup BLSP_BOOT2_Public_Functions
* @{
*/
/****************************************************************************//**
* @brief Boot2 main function
*
* @param None
*
* @return Return value
*
*******************************************************************************/
int main(void)
{
uint32_t ret=0,i=0;
pt_table_stuff_config pt_table_stuff[2];
pt_table_id_type active_id;
/* Init to zero incase only one cpu boot up*/
pt_table_entry_config pt_entry[BFLB_BOOT2_CPU_MAX]={0};
uint32_t boot_header_addr[BFLB_BOOT2_CPU_MAX]={0};
uint8_t boot_rollback[BFLB_BOOT2_CPU_MAX]={0};
uint8_t pt_parsed=1;
#ifdef BLSP_BOOT2_ROLLBACK
uint8_t roll_backed=0;
#endif
uint8_t temp_mode=0;
//boot_clk_config clk_cfg;
uint8_t flash_cfg_buf[4+sizeof(SPI_Flash_Cfg_Type)+4]={0};
bflb_eflash_loader_uart_init();
bflb_platform_init(0);
flash_init();
bflb_platform_print_set(blsp_boot2_get_log_disable_flag());
bflb_platform_deinit_time();
if(blsp_boot2_get_feature_flag()==BLSP_BOOT2_CP_FLAG){
MSG_DBG("BLSP_Boot2_CP:%s,%s\r\n",__DATE__,__TIME__);
}else if(blsp_boot2_get_feature_flag()==BLSP_BOOT2_MP_FLAG){
MSG_DBG("BLSP_Boot2_MC:%s,%s\r\n",__DATE__,__TIME__);
}else{
MSG_DBG("BLSP_Boot2_SP:%s,%s\r\n",__DATE__,__TIME__);
}
#ifdef BL_SDK_VER
MSG_DBG("SDK:%s\r\n",BL_SDK_VER);
#else
MSG_DBG("MCU SDK:%s\r\n",MCU_SDK_VERSION);
MSG_DBG("BSP Driver:%s\r\n",BSP_DRIVER_VERSION);
MSG_DBG("BSP Common:%s\r\n",BSP_COMMON_VERSION);
#endif
if(blsp_boot2_dump_critical_flag()){
//blsp_dump_data(&clk_cfg,16);
}
MSG_DBG("Get efuse config\r\n");
blsp_boot2_get_efuse_cfg(&g_efuse_cfg);
/* Reset Sec_Eng for using */
blsp_boot2_reset_sec_eng();
if(blsp_boot2_get_feature_flag()!=BLSP_BOOT2_SP_FLAG){
/* Get cpu count info */
g_cpu_count=blsp_boot2_get_cpu_count();
}else{
g_cpu_count=1;
}
/* Get power save mode */
g_ps_mode=blsp_read_power_save_mode();
/* Get User specified FW */
//ARCH_MemCpy_Fast(userFwName,blsp_get_user_specified_fw(),4);
if(blsp_boot2_8m_support_flag()){
/* Set flash operation function, read via sbus */
pt_table_set_flash_operation(flash_erase_xip,flash_write_xip,flash_read_xip);
}else{
/* Set flash operation function, read via xip */
pt_table_set_flash_operation(flash_erase_xip,flash_write_xip,flash_read_xip);
}
while(1){
temp_mode=0;
do{
active_id=pt_table_get_active_partition_need_lock(pt_table_stuff);
if(PT_TABLE_ID_INVALID==active_id){
blsp_boot2_on_error("No valid PT\r\n");
}
MSG_DBG("Active PT:%d,%d\r\n",active_id,pt_table_stuff[active_id].pt_table.age);
pt_parsed=blsp_boot2_deal_one_fw(active_id,&pt_table_stuff[active_id],&pt_entry[0],NULL,PT_ENTRY_FW_CPU0);
if(pt_parsed==0){
continue;
}
pt_parsed=1;
}while(pt_parsed==0);
/* Pass data to App*/
blsp_boot2_pass_parameter(NULL,0);
/* Pass active partition table ID */
blsp_boot2_pass_parameter(&active_id,4);
/* Pass active partition table content: table header+ entries +crc32 */
blsp_boot2_pass_parameter(&pt_table_stuff[active_id],sizeof(pt_table_config)+4+
pt_table_stuff[active_id].pt_table.entryCnt*sizeof(pt_table_entry_config));
/* Pass flash config */
if(pt_entry[0].start_address[pt_entry[0].active_index]!=0){
XIP_SFlash_Read_Via_Cache_Need_Lock(BLSP_BOOT2_XIP_BASE+pt_entry[0].start_address[pt_entry[0].active_index]+8,flash_cfg_buf,sizeof(flash_cfg_buf));
/* Include magic and CRC32 */
blsp_boot2_pass_parameter(flash_cfg_buf,sizeof(flash_cfg_buf));
}
MSG_DBG("Boot start\r\n");
for(i=0;i<g_cpu_count;i++){
boot_header_addr[i]=pt_entry[i].start_address[pt_entry[i].active_index];
}
#ifdef BLSP_BOOT2_ROLLBACK
/* Test mode is not need roll back */
if(roll_backed==0 && temp_mode==0){
ret=blsp_mediaboot_main(boot_header_addr,boot_rollback,1);
}else{
ret=blsp_mediaboot_main(boot_header_addr,boot_rollback,0);
}
#else
ret=blsp_mediaboot_main(boot_header_addr,boot_rollback,0);
#endif
/* Fail in temp mode,continue to boot normal image */
if(temp_mode==1){
continue;
}
#ifdef BLSP_BOOT2_ROLLBACK
/* If rollback is done, we still fail, break */
if(roll_backed){
break;
}
MSG_DBG("Boot return %d\r\n",ret);
MSG_DBG("Check Rollback\r\n");
for(i=0;i<g_cpu_count;i++){
if(boot_rollback[i]!=0){
MSG_DBG("Rollback %d\r\n",i);
if(BFLB_BOOT2_SUCCESS==blsp_boot2_rollback_ptentry(active_id,&pt_table_stuff[active_id],&pt_entry[i])){
roll_backed=1;
}
}
}
/* If need no rollback, boot fail due to other reseaon instead of imgae issue,break */
if(roll_backed==0){
break;
}
#else
break;
#endif
}
/* We should never get here unless boot fail */
MSG_ERR("Media boot return %d\r\n",ret);
while(1){
if(BFLB_EFLASH_LOADER_HANDSHAKE_SUSS == bflb_eflash_loader_uart_handshake_poll()){
bflb_eflash_loader_main();
}
MSG_ERR("BLSP boot2 fail\r\n");
ARCH_Delay_MS(500);
}
}
void bfl_main()
{
main();
}
/*@} end of group BLSP_BOOT2_Public_Functions */
/*@} end of group BLSP_BOOT2 */
/*@} end of group BL606_BLSP_Boot2 */

View file

@ -0,0 +1,203 @@
/****************************************************************************************
* @file map.txt
*
* @brief This file is the map file (gnuarm or armgcc).
*
* Copyright (C) BouffaloLab 2018
*
****************************************************************************************
*/
/* configure the CPU type */
OUTPUT_ARCH( "riscv" )
/* link with the standard c library */
INPUT(-lc)
/* link with the standard GCC library */
INPUT(-lgcc)
/* configure the entry point */
ENTRY(_enter)
StackSize = 0x1000; /* 4KB */
HeapSize = 0x1000; /* 4KB */
MEMORY
{
xip_memory (rx) : ORIGIN = 0x23000000, LENGTH = 64K
itcm_memory (rx) : ORIGIN = 0x22014000, LENGTH = 16K
dtcm_memory (rx) : ORIGIN = 0x42018000, LENGTH = 32K
ram_memory (!rx) : ORIGIN = 0x42020000, LENGTH = 60K
}
SECTIONS
{
PROVIDE(__metal_chicken_bit = 0);
.text :
{
. = ALIGN(4);
__text_code_start__ = .;
KEEP (*(.text.metal.init.enter))
KEEP (*(SORT_NONE(.init)))
/* section information for finsh shell */
. = ALIGN(4);
_shell_command_start = .;
KEEP(*(shellCommand))
_shell_command_end = .;
*(.text)
*(.text.*)
*(.rodata)
*(.rodata.*)
*(.srodata)
*(.srodata.*)
. = ALIGN(4);
__text_code_end__ = .;
} > xip_memory
. = ALIGN(4);
__itcm_load_addr = .;
.itcm_region : AT (__itcm_load_addr)
{
. = ALIGN(4);
__tcm_code_start__ = .;
*(.tcm_code)
*(.tcm_const)
*(.sclock_rlt_code)
*(.sclock_rlt_const)
*bl602_romapi.o(.text)
*bl602_romapi.o(.text.*)
*bl602_romapi.o(.rodata)
*bl602_romapi.o(.rodata.*)
*bl602_romapi.o(.srodata)
*bl602_romapi.o(.srodata.*)
. = ALIGN(4);
__tcm_code_end__ = .;
} > itcm_memory
__dtcm_load_addr = __itcm_load_addr + SIZEOF(.itcm_region);
.dtcm_region : AT (__dtcm_load_addr)
{
. = ALIGN(4);
__tcm_data_start__ = .;
*(.tcm_data)
/* *finger_print.o(.data*) */
. = ALIGN(4);
__tcm_data_end__ = .;
} > dtcm_memory
/* .heap_dummy section doesn't contains any symbols. It is only
* used for linker to calculate size of heap sections, and assign
* values to heap symbols later */
.heap_dummy (NOLOAD):
{
. = ALIGN(0x4);
. = . + HeapSize;
. = ALIGN(0x4);
} > dtcm_memory
_HeapBase = ORIGIN(dtcm_memory) + LENGTH(dtcm_memory) - StackSize - HeapSize;
_HeapSize = HeapSize;
/* Check if data + heap + stack exceeds RAM limit */
ASSERT(_HeapBase >= __tcm_data_end__, "region RAM overflowed with stack")
/*************************************************************************/
/* .stack_dummy section doesn't contains any symbols. It is only
* used for linker to calculate size of stack sections, and assign
* values to stack symbols later */
.stack_dummy (NOLOAD):
{
. = ALIGN(0x4);
. = . + StackSize;
. = ALIGN(0x4);
} > dtcm_memory
/* Set stack top to end of RAM, and stack limit move down by
* size of stack_dummy section */
__StackTop = ORIGIN(dtcm_memory) + LENGTH(dtcm_memory);
__StackLimit = __StackTop - SIZEOF(.stack_dummy);
/* Check if data + heap + stack exceeds RAM limit */
ASSERT(__StackLimit >= __tcm_data_end__, "region RAM overflowed with stack")
/*************************************************************************/
__system_ram_load_addr = __dtcm_load_addr + SIZEOF(.dtcm_region);
.system_ram_data_region : AT (__system_ram_load_addr)
{
. = ALIGN(4);
__system_ram_data_start__ = .;
*(.system_ram)
. = ALIGN(4);
__system_ram_data_end__ = .;
} > ram_memory
__ram_load_addr = __system_ram_load_addr + SIZEOF(.system_ram_data_region);
/* Data section */
RAM_DATA : AT (__ram_load_addr)
{
. = ALIGN(4);
__ram_data_start__ = .;
PROVIDE( __global_pointer$ = . + 0x800 );
*(.data)
*(.data.*)
*(.sdata)
*(.sdata.*)
*(.sdata2)
*(.sdata2.*)
. = ALIGN(4);
__ram_data_end__ = .;
} > ram_memory
.bss (NOLOAD) :
{
. = ALIGN(4);
__bss_start__ = .;
*(.bss*)
*(.sbss*)
*(COMMON)
. = ALIGN(4);
__bss_end__ = .;
} > ram_memory
.noinit_data (NOLOAD) :
{
. = ALIGN(4);
__noinit_data_start__ = .;
*(.noinit_data*)
. = ALIGN(4);
__noinit_data_end__ = .;
} > ram_memory
.heap (NOLOAD):
{
. = ALIGN(4);
__HeapBase = .;
/*__end__ = .;*/
/*end = __end__;*/
KEEP(*(.heap*))
. = ALIGN(4);
__HeapLimit = .;
} > ram_memory
}

View file

@ -0,0 +1,299 @@
/**
******************************************************************************
* @file blsp_boot_decompress.c
* @version V1.2
* @date
* @brief This file is the peripheral case c file
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT(c) 2018 Bouffalo Lab</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of Bouffalo Lab nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
#include "xz.h"
#include "blsp_bootinfo.h"
#include "blsp_common.h"
#include "blsp_media_boot.h"
#include "bflb_platform.h"
#include "softcrc.h"
#include "partition.h"
#include "hal_flash.h"
/** @addtogroup BL606_BLSP_Boot2
* @{
*/
/** @addtogroup BLSP_BOOT_DECOMPRESS
* @{
*/
/** @defgroup BLSP_BOOT_DECOMPRESS_Private_Macros
* @{
*/
/*@} end of group BLSP_BOOT_DECOMPRESS_Private_Macros */
/** @defgroup BLSP_BOOT_DECOMPRESS_Private_Types
* @{
*/
/*@} end of group BLSP_BOOT_DECOMPRESS_Private_Types */
/** @defgroup BLSP_BOOT_DECOMPRESS_Private_Variables
* @{
*/
static uint8_t g_xz_output[BFLB_BOOT2_READBUF_SIZE] __attribute__((section(".noinit_data")));
static uint8_t g_malloc_buf[64*1024] __attribute__((section(".noinit_data")));
/*@} end of group BLSP_BOOT_DECOMPRESS_Private_Variables */
/** @defgroup BLSP_BOOT_DECOMPRESS_Global_Variables
* @{
*/
/*@} end of group BLSP_BOOT_DECOMPRESS_Global_Variables */
/** @defgroup BLSP_BOOT_DECOMPRESS_Private_Fun_Declaration
* @{
*/
/*@} end of group BLSP_BOOT_DECOMPRESS_Private_Fun_Declaration */
/** @defgroup BLSP_BOOT_DECOMPRESS_Private_Functions_User_Define
* @{
*/
/*@} end of group BLSP_BOOT_DECOMPRESS_Private_Functions_User_Define */
/** @defgroup BLSP_BOOT_DECOMPRESS_Private_Functions
* @{
*/
/****************************************************************************//**
* @brief Decompress XZ Firmware
*
* @param srcAddress: Source address on flash
* @param destAddress: Destination address on flash
* @param destMaxSize: Destination flash region size for erase
* @param pDestSize: Pointer for output destination firmware size
*
* @return Decompress result status
*
*******************************************************************************/
static int32_t blsp_boot2_fw_decompress(uint32_t src_address,uint32_t dest_address,uint32_t dest_max_size,uint32_t *p_dest_size)
{
struct xz_buf b;
struct xz_dec *s;
enum xz_ret ret;
*p_dest_size=0;
if(dest_max_size>0){
flash_erase_xip(dest_address,dest_address+dest_max_size-1);
}
xz_crc32_init();
simple_malloc_init(g_malloc_buf,sizeof(g_malloc_buf));
/*
* Support up to 32k dictionary. The actually needed memory
* is allocated once the headers have been parsed.
*/
s = xz_dec_init(XZ_PREALLOC, 1 << 15);
if (s == NULL) {
MSG_ERR("Memory allocation failed\n");
return BFLB_BOOT2_MEM_ERROR;
}
b.in = g_boot2_read_buf;
b.in_pos = 0;
b.in_size = 0;
b.out = g_xz_output;
b.out_pos = 0;
b.out_size = sizeof(g_xz_output);
while (1) {
if (b.in_pos == b.in_size) {
MSG_DBG("XZ Feeding\r\n");
if(BFLB_BOOT2_SUCCESS!=blsp_mediaboot_read(src_address,g_boot2_read_buf,sizeof(g_boot2_read_buf))){
MSG_ERR("Read XZFW fail\r\n");
return BFLB_BOOT2_FLASH_READ_ERROR;
}
b.in_size = sizeof(g_boot2_read_buf);
b.in_pos = 0;
src_address+=sizeof(g_boot2_read_buf);
}
ret = xz_dec_run(s, &b);
if (b.out_pos == sizeof(g_xz_output)) {
//if (fwrite(out, 1, b.out_pos, stdout) != b.out_pos) {
// msg = "Write error\n";
// goto error;
//}
MSG_DBG("XZ outputing\r\n");
if(dest_max_size>0){
flash_write_xip(dest_address,g_xz_output,sizeof(g_xz_output));
}
dest_address+=sizeof(g_xz_output);
*p_dest_size+=sizeof(g_xz_output);
b.out_pos = 0;
}
if (ret == XZ_OK)
continue;
//if (fwrite(out, 1, b.out_pos, stdout) != b.out_pos
// || fclose(stdout)) {
// msg = "Write error\n";
// goto error;
//}
if(b.out_pos>0){
if(dest_max_size>0){
flash_write_xip(dest_address,g_xz_output,b.out_pos);
}
dest_address+=b.out_pos;
*p_dest_size+=b.out_pos;
}
switch (ret) {
case XZ_STREAM_END:
xz_dec_end(s);
return 0;
case XZ_MEM_ERROR:
MSG_ERR("Memory allocation failed\n");
goto error;
case XZ_MEMLIMIT_ERROR:
MSG_ERR("Memory usage limit reached\n");
goto error;
case XZ_FORMAT_ERROR:
MSG_ERR("Not a .xz file\n");
goto error;
case XZ_OPTIONS_ERROR:
MSG_ERR("Unsupported options in the .xz headers\n");
goto error;
case XZ_DATA_ERROR:
case XZ_BUF_ERROR:
MSG_ERR("File is corrupt\n");
goto error;
default:
MSG_ERR("XZ Bug!\n");
goto error;
}
}
error:
xz_dec_end(s);
return BFLB_BOOT2_FAIL;
}
/*@} end of group BLSP_BOOT_DECOMPRESS_Private_Functions */
/** @defgroup BLSP_BOOT_DECOMPRESS_Public_Functions
* @{
*/
/****************************************************************************//**
* @brief Update decompressed firmware to flash according to XZ firmware
*
* @param activeID: Active partition table ID
* @param ptStuff: Pointer of partition table stuff
* @param ptEntry: Pointer of active entry
*
* @return XZ firmware update result status
*
*******************************************************************************/
int32_t blsp_boot2_update_fw(pt_table_id_type active_id,pt_table_stuff_config *pt_stuff,pt_table_entry_config *pt_entry)
{
uint8_t active_index=pt_entry->active_index;
uint32_t new_fw_len;
int32_t ret;
#ifdef BLSP_BOOT2_ROLLBACK
/* Try to check Image integrity: try to decompress */
if(BFLB_BOOT2_SUCCESS!=blsp_boot2_fw_decompress(pt_entry->start_address[active_index],
pt_entry->start_address[!(active_index&0x01)],
0,&new_fw_len)){
/* Decompress fail, try to rollback to old one */
pt_entry->active_index=!(active_index&0x01);
pt_entry->age++;
ret=pt_table_update_entry((pt_table_id_type)(!active_id),pt_stuff,pt_entry);
if(ret!=PT_ERROR_SUCCESS){
MSG_ERR("Rollback Update Partition table entry fail\r\n");
return BFLB_BOOT2_FAIL;
}
return BFLB_BOOT2_SUCCESS;
}
#endif
/* Do decompress */
if(BFLB_BOOT2_SUCCESS==blsp_boot2_fw_decompress(pt_entry->start_address[active_index],
pt_entry->start_address[!(active_index&0x01)],
pt_entry->max_len[!(active_index&0x01)],&new_fw_len)){
pt_entry->active_index=!(active_index&0x01);
pt_entry->len=new_fw_len;
pt_entry->age++;
ret=pt_table_update_entry((pt_table_id_type)(!active_id),pt_stuff,pt_entry);
if(ret!=PT_ERROR_SUCCESS){
MSG_ERR("Do Decompress Update Partition table entry fail\r\n");
return BFLB_BOOT2_FAIL;
}
}else{
MSG_ERR("XZ Decompress fail\r\n");
return BFLB_BOOT2_FAIL;
}
return BFLB_BOOT2_SUCCESS;
}
/****************************************************************************//**
* @brief Check if buffer is XZ header
*
* @param buffer: Buffer of firmware
*
* @return 1 for XZ firmware and 0 for not
*
*******************************************************************************/
int blsp_boot2_verify_xz_header(uint8_t *buffer)
{
const uint8_t xz_header[6] = { 0xFD, '7', 'z', 'X', 'Z', 0x00 };
//const uint8_t xz_header[6] = { 0x42, 0x46, 0x4e, 0x50, 0x01, 0x00 };
if (buffer) {
if (!memcmp(buffer, xz_header, 6)){
return 1;
}
}
return 0;
}
/*@} end of group BLSP_BOOT_DECOMPRESS_Public_Functions */
/*@} end of group BLSP_BOOT_DECOMPRESS */
/*@} end of group BL606_BLSP_Boot2 */

View file

@ -0,0 +1,79 @@
/**
******************************************************************************
* @file blsp_boot_decompress.h
* @version V1.2
* @date
* @brief This file is the peripheral case header file
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT(c) 2018 Bouffalo Lab</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of Bouffalo Lab nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
#ifndef __BLSP_BOOT_DECOMPRESS_H__
#define __BLSP_BOOT_DECOMPRESS_H__
#include "stdint.h"
/** @addtogroup BL606_BLSP_Boot2
* @{
*/
/** @addtogroup BLSP_BOOT_DECOMPRESS
* @{
*/
/** @defgroup BLSP_BOOT_DECOMPRESS_Public_Types
* @{
*/
/*@} end of group BLSP_BOOT_DECOMPRESS_Public_Types */
/** @defgroup BLSP_BOOT_DECOMPRESS_Public_Constants
* @{
*/
/*@} end of group BLSP_BOOT_DECOMPRESS_Public_Constants */
/** @defgroup BLSP_BOOT_DECOMPRESS_Public_Macros
* @{
*/
/*@} end of group BLSP_BOOT_DECOMPRESS_Public_Macros */
/** @defgroup BLSP_BOOT_DECOMPRESS_Public_Functions
* @{
*/
int32_t blsp_boot2_update_fw(pt_table_id_type activeID,pt_table_stuff_config *ptStuff,pt_table_entry_config *ptEntry);
int blsp_boot2_verify_xz_header(uint8_t *buffer);
/*@} end of group BLSP_BOOT_DECOMPRESS_Public_Functions */
/*@} end of group BLSP_BOOT_DECOMPRESS */
/*@} end of group BL606_BLSP_Boot2 */
#endif /* __BLSP_BOOT_DECOMPRESS_H__ */

View file

@ -0,0 +1,430 @@
/**
******************************************************************************
* @file blsp_boot_parser.c
* @version V1.2
* @date
* @brief This file is the peripheral case c file
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT(c) 2018 Bouffalo Lab</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of Bouffalo Lab nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
#include "blsp_common.h"
#include "bflb_platform.h"
#include "blsp_bootinfo.h"
#include "blsp_boot_parser.h"
#include "softcrc.h"
#include "blsp_port.h"
/** @addtogroup BL606_BLSP_Boot2
* @{
*/
/** @addtogroup BLSP_BOOT_PARSER
* @{
*/
/** @defgroup BLSP_BOOT_PARSER_Private_Macros
* @{
*/
/*@} end of group BLSP_BOOT_PARSER_Private_Macros */
/** @defgroup BLSP_BOOT_PARSER_Private_Types
* @{
*/
/*@} end of group BLSP_BOOT_PARSER_Private_Types */
/** @defgroup BLSP_BOOT_PARSER_Private_Variables
* @{
*/
static uint32_t g_sha_tmp_buf[16]={0};
static uint32_t g_padding[16]={0};
SEC_Eng_SHA256_Ctx g_sha_ctx;
extern uint32_t g_user_hash_ignored;
/*@} end of group BLSP_BOOT_PARSER_Private_Variables */
/** @defgroup BLSP_BOOT_PARSER_Global_Variables
* @{
*/
/*@} end of group BLSP_BOOT_PARSER_Global_Variables */
/** @defgroup BLSP_BOOT_PARSER_Private_Fun_Declaration
* @{
*/
/*@} end of group BLSP_BOOT_PARSER_Private_Fun_Declaration */
/** @defgroup BLSP_BOOT_PARSER_Private_Functions_User_Define
* @{
*/
/*@} end of group BLSP_BOOT_PARSER_Private_Functions_User_Define */
/** @defgroup BLSP_BOOT_PARSER_Private_Functions
* @{
*/
/****************************************************************************//**
* @brief Check if the input public key is the same as burned in the efuse
*
* @param cpu_type: CPU Type
* @param pkhash: Public key hash pointer
*
* @return 1 for valid and 0 for invalid
*
*******************************************************************************/
static uint32_t blsp_boot_parse_is_pkhash_valid(uint8_t cpu_type,uint8_t *pkhash)
{
if(cpu_type==BFLB_BOOT2_CPU_0){
if(0==memcmp(g_efuse_cfg.pk_hash_cpu0,pkhash,BFLB_BOOT2_PK_HASH_SIZE)){
return 1;
}
}else if(cpu_type==BFLB_BOOT2_CPU_1){
if(0==memcmp(g_efuse_cfg.pk_hash_cpu1,pkhash,BFLB_BOOT2_PK_HASH_SIZE)){
return 1;
}
}
return 0;
}
/*@} end of group BLSP_BOOT_PARSER_Private_Functions */
/** @defgroup BLSP_BOOT_PARSER_Public_Functions
* @{
*/
/****************************************************************************//**
* @brief Check if the input public key is the same as burned in the efuse
*
* @param g_boot_img_cfg: Boot image config pointer
* @param data: Image data pointer
*
* @return boot_error_code type
*
*******************************************************************************/
int32_t blsp_boot_parse_bootheader(boot_image_config *g_boot_img_cfg,uint8_t *data)
{
boot_header_config *header=(boot_header_config *)data;
uint32_t crc;
uint32_t crc_pass=0;
uint32_t i=0;
uint32_t *phash=(uint32_t *)header->hash;
if(header->bootCfg.bval.crcIgnore==1&&header->crc32==BFLB_BOOT2_DEADBEEF_VAL){
MSG_DBG("Crc ignored\r\n");
crc_pass=1;
}else{
crc=BFLB_Soft_CRC32((uint8_t *)header,sizeof(boot_header_config)-sizeof(header->crc32));
if(header->crc32==crc){
crc_pass=1;
}
}
if(crc_pass){
if(header->bootCfg.bval.notLoadInBoot){
return BFLB_BOOT2_IMG_BOOTHEADER_NOT_LOAD_ERROR;
}
/* Get which CPU's img it is*/
for(i=0;i<BFLB_BOOT2_CPU_MAX;i++){
if(0==memcmp((void *)&header->magicCode,BFLB_BOOT2_CPU0_MAGIC,
sizeof(header->magicCode))){
break;
}else if(0==memcmp((void *)&header->magicCode,BFLB_BOOT2_CPU1_MAGIC,
sizeof(header->magicCode))){
break;
}
}
if(i==BFLB_BOOT2_CPU_MAX){
/* No cpu img magic match */
MSG_ERR("Magic code error\r\n");
return BFLB_BOOT2_IMG_BOOTHEADER_MAGIC_ERROR;
}
g_boot_img_cfg->cpu_type=i;
g_boot_img_cfg->entry_point=0;
/* Set image valid 0 as default */
g_boot_img_cfg->img_valid=0;
/* Deal with pll config */
/* Encrypt and sign */
g_boot_img_cfg->encrypt_type=header->bootCfg.bval.encrypt_type;
g_boot_img_cfg->sign_type=header->bootCfg.bval.sign;
g_boot_img_cfg->key_sel=header->bootCfg.bval.key_sel;
/* Xip relative */
g_boot_img_cfg->no_segment=header->bootCfg.bval.no_segment;
g_boot_img_cfg->cache_enable=header->bootCfg.bval.cache_enable;
g_boot_img_cfg->aes_region_lock=header->bootCfg.bval.aes_region_lock;
g_boot_img_cfg->halt_cpu1=header->bootCfg.bval.halt_cpu1;
g_boot_img_cfg->cache_way_disable=header->bootCfg.bval.cache_way_disable;
g_boot_img_cfg->hash_ignore=header->bootCfg.bval.hash_ignore;
/* Firmware len*/
g_boot_img_cfg->img_segment_info.img_len=header->img_segment_info.img_len;
/* Boot entry and flash offset */
g_boot_img_cfg->entry_point=header->bootEntry;
g_boot_img_cfg->img_start.flash_offset=header->img_start.flash_offset;
MSG_DBG("sign %d,encrypt:%d\r\n",g_boot_img_cfg->sign_type,
g_boot_img_cfg->encrypt_type);
/* Check encrypt and sign match*/
if(g_efuse_cfg.encrypted[i]!=0){
if(g_boot_img_cfg->encrypt_type==0){
MSG_ERR("Encrypt not fit\r\n");
return BFLB_BOOT2_IMG_BOOTHEADER_ENCRYPT_NOTFIT;
}
}
if(g_efuse_cfg.sign[i]^g_boot_img_cfg->sign_type){
MSG_ERR("sign not fit\r\n");
g_boot_img_cfg->sign_type=g_efuse_cfg.sign[i];
return BFLB_BOOT2_IMG_BOOTHEADER_SIGN_NOTFIT;
}
if(g_ps_mode==BFLB_PSM_HBN && (!g_efuse_cfg.hbn_check_sign)){
/* In HBN Mode, if user select to ignore hash and sign*/
g_boot_img_cfg->hash_ignore=1;
}else if((g_boot_img_cfg->hash_ignore==1&& *phash!=BFLB_BOOT2_DEADBEEF_VAL)||
g_efuse_cfg.sign[i]!=0 ){
/* If signed or user not really want to ignore, hash can't be ignored*/
g_boot_img_cfg->hash_ignore=0;
}
if(g_user_hash_ignored){
g_boot_img_cfg->hash_ignore=1;
}
ARCH_MemCpy_Fast(g_boot_img_cfg->img_hash,header->hash,sizeof(header->hash));
if(g_boot_img_cfg->img_segment_info.img_len==0){
return BFLB_BOOT2_IMG_SEGMENT_CNT_ERROR;
}
/* Start hash here*/
Sec_Eng_SHA256_Init(&g_sha_ctx,SEC_ENG_SHA_ID0,SEC_ENG_SHA256, g_sha_tmp_buf,g_padding);
Sec_Eng_SHA_Start(SEC_ENG_SHA_ID0);
}else{
MSG_ERR("bootheader crc error\r\n");
blsp_dump_data((uint8_t *)&crc,4);
return BFLB_BOOT2_IMG_BOOTHEADER_CRC_ERROR;
}
return BFLB_BOOT2_SUCCESS;
}
/****************************************************************************//**
* @brief Parse public key
*
* @param g_boot_img_cfg: Boot image config pointer
* @param data: Public key data pointer
* @param own: 1 for current CPU, 0 for other(s)
*
* @return boot_error_code type
*
*******************************************************************************/
int32_t blsp_boot_parse_pkey(boot_image_config *g_boot_img_cfg,uint8_t *data,uint8_t own)
{
boot_pk_config *cfg=(boot_pk_config*)data;
uint32_t pk_hash[BFLB_BOOT2_PK_HASH_SIZE/4];
if(cfg->crc32==BFLB_Soft_CRC32((uint8_t*)cfg,sizeof(boot_pk_config)-4)){
/* Check public key with data info in OTP*/
Sec_Eng_SHA256_Update(&g_sha_ctx,SEC_ENG_SHA_ID0,data,BFLB_BOOT2_ECC_KEYXSIZE+BFLB_BOOT2_ECC_KEYYSIZE);
Sec_Eng_SHA256_Finish(&g_sha_ctx,SEC_ENG_SHA_ID0,(uint8_t *)pk_hash);
Sec_Eng_SHA256_Init(&g_sha_ctx,SEC_ENG_SHA_ID0,SEC_ENG_SHA256, g_sha_tmp_buf,g_padding);
Sec_Eng_SHA_Start(SEC_ENG_SHA_ID0);
/* Check pk is valid */
if(own==1){
if(1!=blsp_boot_parse_is_pkhash_valid(g_boot_img_cfg->cpu_type,
(uint8_t *)pk_hash)){
MSG_ERR("PK sha error\r\n");
return BFLB_BOOT2_IMG_PK_HASH_ERROR;
}
}
if(own==1){
ARCH_MemCpy_Fast(g_boot_img_cfg->eckye_x,cfg->eckye_x,sizeof(cfg->eckye_x));
ARCH_MemCpy_Fast(g_boot_img_cfg->eckey_y,cfg->eckey_y,sizeof(cfg->eckey_y));
}
}else{
MSG_ERR("PK crc error\r\n");
return BFLB_BOOT2_IMG_PK_CRC_ERROR;
}
return BFLB_BOOT2_SUCCESS;
}
/****************************************************************************//**
* @brief Parse signature
*
* @param g_boot_img_cfg: Boot image config pointer
* @param data: Signature data pointer
* @param own: 1 for current CPU, 0 for other(s)
*
* @return boot_error_code type
*
*******************************************************************************/
int32_t blsp_boot_parse_signature(boot_image_config *g_boot_img_cfg,uint8_t *data,uint8_t own)
{
boot_sign_config *cfg=(boot_sign_config*)data;
uint32_t crc;
if(cfg->sig_len>sizeof(g_boot_img_cfg->signature)){
return BFLB_BOOT2_IMG_SIGNATURE_LEN_ERROR;
}
/* CRC include sig_len*/
crc=BFLB_Soft_CRC32((uint8_t *)&cfg->sig_len,cfg->sig_len+sizeof(cfg->sig_len));
if(memcmp(&crc,&cfg->signature[cfg->sig_len],4)==0){
if(own==1){
ARCH_MemCpy_Fast(g_boot_img_cfg->signature,cfg->signature,cfg->sig_len);
g_boot_img_cfg->sig_len=cfg->sig_len;
}else{
ARCH_MemCpy_Fast(g_boot_img_cfg->signature2,cfg->signature,cfg->sig_len);
g_boot_img_cfg->sig_len2=cfg->sig_len;
}
}else{
MSG_ERR("SIG crc error\r\n");
return BFLB_BOOT2_IMG_SIGNATURE_CRC_ERROR;
}
return BFLB_BOOT2_SUCCESS;
}
/****************************************************************************//**
* @brief Parse ASE IV
*
* @param g_boot_img_cfg: Boot image config pointer
* @param data: AES IV data pointer
*
* @return boot_error_code type
*
*******************************************************************************/
int32_t blsp_boot_parse_aesiv(boot_image_config *g_boot_img_cfg,uint8_t *data)
{
boot_aes_config *cfg=(boot_aes_config *)data;
if(cfg->crc32==BFLB_Soft_CRC32(cfg->aes_iv,sizeof(cfg->aes_iv))){
memcpy(g_boot_img_cfg->aes_iv,cfg->aes_iv,sizeof(boot_aes_config));
/* Update image hash */
if(!g_boot_img_cfg->hash_ignore){
Sec_Eng_SHA256_Update(&g_sha_ctx,SEC_ENG_SHA_ID0,data,sizeof(boot_aes_config));
}
}else{
MSG_ERR("AES IV crc error\r\n");
return BFLB_BOOT2_IMG_AES_IV_CRC_ERROR;
}
return BFLB_BOOT2_SUCCESS;
}
#if 0
/****************************************************************************//**
* @brief Check signature is valid
*
* @param g_boot_img_cfg: Boot image config pointer
*
* @return boot_error_code type
*
*******************************************************************************/
int32_t blsp_boot_parser_check_signature(boot_image_config *g_boot_img_cfg)
{
int32_t ret=0;
uint64_t startTime=0;
BFLB_ECDSA_Handle_t ecdsaHandle;
MSG_DBG("%d,%d\r\n",g_ps_mode,g_efuse_cfg.hbn_check_sign);
if(g_ps_mode==BFLB_PSM_HBN&&(!g_efuse_cfg.hbn_check_sign)){
return BFLB_BOOT2_SUCCESS;
}
if(g_boot_img_cfg->sign_type){
MSG_DBG("Check sig1\r\n");
startTime=bflb_platform_get_time_ms();
bflb_ecdsa_init(&ecdsaHandle,ECP_SECP256R1);
ecdsaHandle.publicKeyx=(uint32_t *)g_boot_img_cfg->eckye_x;
ecdsaHandle.publicKeyy=(uint32_t *)g_boot_img_cfg->eckey_y;
bflb_ecdsa_verify(&ecdsaHandle,(uint32_t *)g_boot_img_cfg->img_hash,sizeof(g_boot_img_cfg->img_hash)/4,
(uint32_t *)g_boot_img_cfg->signature,(uint32_t *)&g_boot_img_cfg->signature[32]);
//ret=bflb_ecdsa_verify(0,(uint32_t *)g_boot_img_cfg->img_hash,sizeof(g_boot_img_cfg->img_hash),
// (uint32_t *)g_boot_img_cfg->eckye_x,(uint32_t *)g_boot_img_cfg->eckey_y,
// (uint32_t *)g_boot_img_cfg->signature,(uint32_t *)&g_boot_img_cfg->signature[32]);
if (ret!=0) {
MSG_ERR("verify failed\n");
return BFLB_BOOT2_IMG_SIGN_ERROR;
}
MSG_DBG("Time=%d ms\r\n",(unsigned int)(bflb_platform_get_time_ms()-startTime));
}
MSG_DBG("Success\r\n");
return BFLB_BOOT2_SUCCESS;
}
#endif
/****************************************************************************//**
* @brief Check hash is valid
*
* @param g_boot_img_cfg: Boot image config pointer
*
* @return boot_error_code type
*
*******************************************************************************/
int32_t blsp_boot_parser_check_hash(boot_image_config *g_boot_img_cfg)
{
uint32_t img_hash_cal[BFLB_BOOT2_HASH_SIZE/4];
if(!g_boot_img_cfg->hash_ignore){
Sec_Eng_SHA256_Finish(&g_sha_ctx,SEC_ENG_SHA_ID0,(uint8_t *)img_hash_cal);
if(memcmp(img_hash_cal,g_boot_img_cfg->img_hash ,
sizeof(g_boot_img_cfg->img_hash))!=0){
MSG_ERR("Hash error\r\n");
blsp_dump_data(img_hash_cal,BFLB_BOOT2_HASH_SIZE);
blsp_dump_data(g_boot_img_cfg->img_hash,BFLB_BOOT2_HASH_SIZE);
return BFLB_BOOT2_IMG_HASH_ERROR;
}else{
MSG_DBG("Success\r\n");
}
}
return BFLB_BOOT2_SUCCESS;
}
/*@} end of group BLSP_BOOT_PARSER_Public_Functions */
/*@} end of group BLSP_BOOT_PARSER */
/*@} end of group BL606_BLSP_Boot2 */

View file

@ -0,0 +1,83 @@
/**
******************************************************************************
* @file blsp_boot_parser.h
* @version V1.2
* @date
* @brief This file is the peripheral case header file
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT(c) 2018 Bouffalo Lab</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of Bouffalo Lab nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
#ifndef __BLSP_BOOT_PARSER_H__
#define __BLSP_BOOT_PARSER_H__
#include "stdint.h"
/** @addtogroup BL606_BLSP_Boot2
* @{
*/
/** @addtogroup BLSP_BOOT_PARSER
* @{
*/
/** @defgroup BLSP_BOOT_PARSER_Public_Types
* @{
*/
/*@} end of group BLSP_BOOT_PARSER_Public_Types */
/** @defgroup BLSP_BOOT_PARSER_Public_Constants
* @{
*/
/*@} end of group BLSP_BOOT_PARSER_Public_Constants */
/** @defgroup BLSP_BOOT_PARSER_Public_Macros
* @{
*/
/*@} end of group BLSP_BOOT_PARSER_Public_Macros */
/** @defgroup BLSP_BOOT_PARSER_Public_Functions
* @{
*/
int32_t blsp_boot_parse_bootheader(boot_image_config *g_boot_img_cfg,uint8_t *data);
int32_t blsp_boot_parse_pkey(boot_image_config *g_boot_img_cfg,uint8_t *data,uint8_t own);
int32_t blsp_boot_parse_signature(boot_image_config *g_boot_img_cfg,uint8_t *data,uint8_t own);
int32_t blsp_boot_parse_aesiv(boot_image_config *g_boot_img_cfg,uint8_t *data);
//int32_t blsp_boot_parser_check_signature(boot_image_config *g_boot_img_cfg);
int32_t blsp_boot_parser_check_hash(boot_image_config *g_boot_img_cfg);
/*@} end of group BLSP_BOOT_PARSER_Public_Functions */
/*@} end of group BLSP_BOOT_PARSER */
/*@} end of group BL606_BLSP_Boot2 */
#endif /* __BLSP_BOOT_PARSER_H__ */

View file

@ -0,0 +1,308 @@
/**
******************************************************************************
* @file blsp_bootinfo.h
* @version V1.2
* @date
* @brief This file is the peripheral case header file
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT(c) 2018 Bouffalo Lab</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of Bouffalo Lab nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
#ifndef __BLSP_BOOTINFO_H__
#define __BLSP_BOOTINFO_H__
#include "stdint.h"
#include "blsp_port.h"
/** @addtogroup BL606_BLSP_Boot2
* @{
*/
/** @addtogroup BLSP_BOOTINFO
* @{
*/
/** @defgroup BLSP_BOOTINFO_Public_Macros
* @{
*/
#define BFLB_BOOT2_CPU0_MAGIC "BFNP"
#define BFLB_BOOT2_CPU1_MAGIC "BFAP"
#define BFLB_BOOT2_FLASH_CFG_MAGIC "FCFG"
#define BFLB_BOOT2_PLL_CFG_MAGICCODE "PCFG"
#define BFLB_BOOT2_FLASH_TZC_MAGIC "TCFG"
#define BFLB_BOOT2_DEADBEEF_VAL 0xdeadbeef
#define BFLB_BOOT2_READBUF_SIZE 4*1024
#define BFLB_FW_IMG_OFFSET_AFTER_HEADER 4*1024
/* Image owner type */
#define BFLB_BOOT2_CPU_0 0
#define BFLB_BOOT2_CPU_1 1
#define BFLB_BOOT2_CPU_MAX 2
/* Public key hash size */
#define BFLB_BOOT2_PK_HASH_SIZE 256/8
#define BFLB_BOOT2_HASH_SIZE 256/8
/* Public key type */
#define BFLB_BOOT2_ECC_KEYXSIZE 256/8
#define BFLB_BOOT2_ECC_KEYYSIZE 256/8
#define BFLB_BOOT2_SIGN_MAXSIZE 2048/8
/* Power save define */
#define BFLB_PSM_ACTIVE 0
#define BFLB_PSM_HBN 1
/*@} end of group BLSP_BOOTINFO_Public_Macros */
/** @defgroup BLSP_BOOTINFO_Public_Types
* @{
*/
typedef enum {
BFLB_BOOT2_SUCCESS=0x00,
/* Flash*/
BFLB_BOOT2_FLASH_INIT_ERROR=0x0001,
BFLB_BOOT2_FLASH_ERASE_PARA_ERROR=0x0002,
BFLB_BOOT2_FLASH_ERASE_ERROR=0x0003,
BFLB_BOOT2_FLASH_WRITE_PARA_ERROR=0x0004,
BFLB_BOOT2_FLASH_WRITE_ADDR_ERROR=0x0005,
BFLB_BOOT2_FLASH_WRITE_ERROR=0x0006,
BFLB_BOOT2_FLASH_BOOT_PARA=0x0007,
BFLB_BOOT2_FLASH_READ_ERROR=0x0008,
/* Image*/
BFLB_BOOT2_IMG_BOOTHEADER_LEN_ERROR=0x0201,
BFLB_BOOT2_IMG_BOOTHEADER_NOT_LOAD_ERROR=0x0202,
BFLB_BOOT2_IMG_BOOTHEADER_MAGIC_ERROR=0x0203,
BFLB_BOOT2_IMG_BOOTHEADER_CRC_ERROR=0x0204,
BFLB_BOOT2_IMG_BOOTHEADER_ENCRYPT_NOTFIT=0x0205,
BFLB_BOOT2_IMG_BOOTHEADER_SIGN_NOTFIT=0x0206,
BFLB_BOOT2_IMG_SEGMENT_CNT_ERROR=0x0207,
BFLB_BOOT2_IMG_AES_IV_LEN_ERROR=0x0208,
BFLB_BOOT2_IMG_AES_IV_CRC_ERROR=0x0209,
BFLB_BOOT2_IMG_PK_LEN_ERROR=0x020a,
BFLB_BOOT2_IMG_PK_CRC_ERROR=0x020b,
BFLB_BOOT2_IMG_PK_HASH_ERROR=0x020c,
BFLB_BOOT2_IMG_SIGNATURE_LEN_ERROR=0x020d,
BFLB_BOOT2_IMG_SIGNATURE_CRC_ERROR=0x020e,
BFLB_BOOT2_IMG_SECTIONHEADER_LEN_ERROR=0x020f,
BFLB_BOOT2_IMG_SECTIONHEADER_CRC_ERROR=0x0210,
BFLB_BOOT2_IMG_SECTIONHEADER_DST_ERROR=0x0211,
BFLB_BOOT2_IMG_SECTIONDATA_LEN_ERROR=0x0212,
BFLB_BOOT2_IMG_SECTIONDATA_DEC_ERROR=0x0213,
BFLB_BOOT2_IMG_SECTIONDATA_TLEN_ERROR=0x0214,
BFLB_BOOT2_IMG_SECTIONDATA_CRC_ERROR=0x0215,
BFLB_BOOT2_IMG_HALFBAKED_ERROR=0x0216,
BFLB_BOOT2_IMG_HASH_ERROR=0x0217,
BFLB_BOOT2_IMG_SIGN_PARSE_ERROR=0x0218,
BFLB_BOOT2_IMG_SIGN_ERROR=0x0219,
BFLB_BOOT2_IMG_DEC_ERROR=0x021a,
BFLB_BOOT2_IMG_ALL_INVALID_ERROR=0x021b,
BFLB_BOOT2_IMG_Roll_Back=0x021c,
/* MISC*/
BFLB_BOOT2_MEM_ERROR=0xfffb,
BFLB_BOOT2_PLL_ERROR=0xfffc,
BFLB_BOOT2_INVASION_ERROR=0xfffd,
BFLB_BOOT2_POLLING=0xfffe,
BFLB_BOOT2_FAIL=0xffff,
}boot_error_code;
typedef struct {
uint8_t encrypted[BFLB_BOOT2_CPU_MAX];
uint8_t sign[BFLB_BOOT2_CPU_MAX];
uint8_t hbn_check_sign;
uint8_t rsvd[3];
uint8_t chip_id[8];
uint8_t pk_hash_cpu0[BFLB_BOOT2_PK_HASH_SIZE];
uint8_t pk_hash_cpu1[BFLB_BOOT2_PK_HASH_SIZE];
}boot_efuse_hw_config;
typedef struct {
uint32_t magicCode; /*'FCFG'*/
SPI_Flash_Cfg_Type cfg;
uint32_t crc32;
}boot_flash_config;
typedef struct {
uint8_t xtal_type;
uint8_t pll_clk;
uint8_t hclk_div;
uint8_t bclk_div;
uint8_t flash_clk_type;
uint8_t flash_clk_div;
uint8_t rsvd[2];
}boot_sys_clk_config;
typedef struct {
uint32_t magicCode; /*'PCFG'*/
boot_sys_clk_config cfg;
uint32_t crc32;
}boot_clk_config;
typedef struct {
uint32_t magicCode; /*'BFXP'*/
uint32_t rivison;
boot_flash_config flash_cfg;
boot_clk_config clk_cfg;
__PACKED_UNION {
__PACKED_STRUCT {
uint32_t sign : 2; /* [1: 0] for sign*/
uint32_t encrypt_type : 2; /* [3: 2] for encrypt */
uint32_t key_sel : 2; /* [5: 4] for key sel in boot interface*/
uint32_t rsvd6_7 : 2; /* [7: 6] for encrypt*/
uint32_t no_segment : 1; /* [8] no segment info */
uint32_t cache_enable : 1; /* [9] for cache */
uint32_t notLoadInBoot : 1; /* [10] not load this img in bootrom */
uint32_t aes_region_lock : 1; /* [11] aes region lock */
uint32_t cache_way_disable : 4; /* [15: 12] cache way disable info*/
uint32_t crcIgnore : 1; /* [16] ignore crc */
uint32_t hash_ignore : 1; /* [17] hash crc */
uint32_t halt_cpu1 : 1; /* [18] halt ap */
uint32_t rsvd19_31 : 13; /* [31:19] rsvd */
} bval;
uint32_t wval;
}bootCfg ;
__PACKED_UNION {
uint32_t segment_cnt;
uint32_t img_len;
}img_segment_info;
uint32_t bootEntry; /* entry point of the image*/
__PACKED_UNION {
uint32_t ram_addr;
uint32_t flash_offset;
}img_start;
uint8_t hash[BFLB_BOOT2_HASH_SIZE]; /*hash of the image*/
uint32_t rsv1;
uint32_t rsv2;
uint32_t crc32;
}boot_header_config;
typedef struct {
uint8_t aes_iv[16];
uint32_t crc32;
}boot_aes_config;
typedef struct {
uint8_t eckye_x[BFLB_BOOT2_ECC_KEYXSIZE]; //ec key in boot header
uint8_t eckey_y[BFLB_BOOT2_ECC_KEYYSIZE]; //ec key in boot header
uint32_t crc32;
}boot_pk_config;
typedef struct {
uint32_t sig_len;
uint8_t signature[BFLB_BOOT2_SIGN_MAXSIZE];
uint32_t crc32;
}boot_sign_config;
typedef struct {
uint8_t encrypt_type;
uint8_t sign_type;
uint8_t key_sel;
uint8_t img_valid;
uint8_t no_segment;
uint8_t cache_enable;
uint8_t cache_way_disable;
uint8_t hash_ignore;
uint8_t aes_region_lock;
uint8_t halt_cpu1;
uint8_t cpu_type;
uint8_t r[1];
__PACKED_UNION {
uint32_t segment_cnt;
uint32_t img_len;
}img_segment_info;
uint32_t msp_val;
uint32_t entry_point;
__PACKED_UNION {
uint32_t ram_addr;
uint32_t flash_offset;
}img_start;
uint32_t sig_len;
uint32_t sig_len2;
uint32_t deal_len;
uint32_t max_input_len;
uint8_t img_hash[BFLB_BOOT2_HASH_SIZE]; //hash of the whole (all)images
uint8_t aes_iv[16+4]; //iv in boot header
uint8_t eckye_x[BFLB_BOOT2_ECC_KEYXSIZE]; //ec key in boot header
uint8_t eckey_y[BFLB_BOOT2_ECC_KEYYSIZE]; //ec key in boot header
uint8_t eckey_x2[BFLB_BOOT2_ECC_KEYXSIZE]; //ec key in boot header
uint8_t eckey_y2[BFLB_BOOT2_ECC_KEYYSIZE]; //ec key in boot header
uint8_t signature[BFLB_BOOT2_SIGN_MAXSIZE]; //signature in boot header
uint8_t signature2[BFLB_BOOT2_SIGN_MAXSIZE]; //signature in boot header
}boot_image_config;
typedef struct {
uint32_t msp_store_addr;
uint32_t pc_store_addr;
uint32_t default_xip_addr;
}boot_cpu_config;
typedef void (*pentry_t)(void );
extern boot_cpu_config g_boot_cpu_cfg[2];
extern boot_image_config g_boot_img_cfg[2];
extern boot_efuse_hw_config g_efuse_cfg;
extern uint8_t g_ps_mode;
extern uint8_t g_cpu_count;
extern uint8_t g_boot2_read_buf[BFLB_BOOT2_READBUF_SIZE];
/*@} end of group BLSP_BOOTINFO_Public_Types */
/** @defgroup BLSP_BOOTINFO_Public_Constants
* @{
*/
/*@} end of group BLSP_BOOTINFO_Public_Constants */
/** @defgroup BLSP_BOOTINFO_Public_Functions
* @{
*/
/*@} end of group BLSP_BOOTINFO_Public_Functions */
/*@} end of group BLSP_BOOTINFO */
/*@} end of group BL606_BLSP_Boot2 */
#endif /* __BLSP_BOOTINFO_H__ */

View file

@ -0,0 +1,243 @@
/**
******************************************************************************
* @file blsp_common.c
* @version V1.2
* @date
* @brief This file is the peripheral case c file
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT(c) 2018 Bouffalo Lab</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of Bouffalo Lab nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
#include "stdio.h"
#include "stdint.h"
#include "string.h"
#include "blsp_port.h"
#include "bflb_platform.h"
#include "blsp_bootinfo.h"
#include "blsp_common.h"
#include "blsp_media_boot.h"
#include "hal_flash.h"
#include "bflb_eflash_loader.h"
/** @addtogroup BL606_BLSP_Boot2
* @{
*/
/** @addtogroup BLSP_COMMON
* @{
*/
/** @defgroup BLSP_COMMON_Private_Macros
* @{
*/
/*@} end of group BLSP_COMMON_Private_Macros */
/** @defgroup BLSP_COMMON_Private_Types
* @{
*/
/*@} end of group BLSP_COMMON_Private_Types */
/** @defgroup BLSP_COMMON_Private_Variables
* @{
*/
/*@} end of group BLSP_COMMON_Private_Variables */
/** @defgroup BLSP_COMMON_Global_Variables
* @{
*/
int32_t blsp_boot2_set_encrypt(uint8_t index,boot_image_config *g_boot_img_cfg);
int32_t blsp_boot2_set_cache(uint8_t cont_read,SPI_Flash_Cfg_Type *flash_cfg,boot_image_config *g_boot_img_cfg);
/*@} end of group BLSP_COMMON_Global_Variables */
/** @defgroup BLSP_COMMON_Private_Fun_Declaration
* @{
*/
/*@} end of group BLSP_COMMON_Private_Fun_Declaration */
/** @defgroup BLSP_COMMON_Private_Functions_User_Define
* @{
*/
/*@} end of group BLSP_COMMON_Private_Functions_User_Define */
/** @defgroup BLSP_COMMON_Private_Functions
* @{
*/
/****************************************************************************//**
* @brief Dump data
*
* @param datain: Data pointer to dump
* @param len: Data length to dump
*
* @return None
*
*******************************************************************************/
void blsp_dump_data(void *datain,int len)
{
int i=0;
uint8_t *data=(uint8_t *)datain;
data = data;
for(i=0;i<len;i++){
if(i%16==0&&i!=0){
bflb_eflash_loader_printf("\r\n");
}
bflb_eflash_loader_printf("%02x ",data[i]);
}
bflb_eflash_loader_printf("\r\n");
}
/****************************************************************************//**
* @brief Media boot pre-jump
*
* @param None
*
* @return BL_Err_Type
*
*******************************************************************************/
int32_t blsp_mediaboot_pre_jump(void)
{
/* Sec eng deinit*/
blsp_boot2_reset_sec_eng();
/* Platform deinit */
bflb_platform_deinit();
/* Jump to entry */
blsp_boot2_jump_entry();
return BFLB_BOOT2_SUCCESS;
}
/****************************************************************************//**
* @brief Boot2 exit with error and will stay forever
*
* @param None
*
* @return None
*
*******************************************************************************/
void blsp_boot2_exit(void)
{
uint32_t i=0;
blsp_sboot_finish();
/* Prepare release Other CPUs anyway */
/* Deal Other CPU's entry point */
for(i=1;i<g_cpu_count;i++){
BL_WR_WORD(g_boot_cpu_cfg[i].msp_store_addr,0);
BL_WR_WORD(g_boot_cpu_cfg[i].pc_store_addr,0);
}
/* Release other CPUs*/
if(g_cpu_count!=1&&!g_boot_img_cfg[0].halt_cpu1){
blsp_boot2_releae_other_cpu();
}
/* Stay here */
while(1){
/* Use soft delay only */
ARCH_Delay_MS(100);
}
}
/****************************************************************************//**
* @brief Boot2 jump to entry_point
*
* @param None
*
* @return None
*
*******************************************************************************/
void ATTR_TCM_SECTION blsp_boot2_jump_entry(void)
{
pentry_t pentry;
uint32_t i=0;
int32_t ret;
blsp_sboot_finish();
/*Note:enable cache with flash offset, after this, should be no flash directl read,
If need read, should take flash offset into consideration */
if(0!=g_efuse_cfg.encrypted[0]){
/*for encrypted img, use none-continuos read*/
ret=flash_set_cache(0,g_boot_img_cfg[0].cache_enable,g_boot_img_cfg[0].cache_way_disable,g_boot_img_cfg[0].img_start.flash_offset);
}else{
/*for unencrypted img, use continuos read*/
ret=flash_set_cache(1,g_boot_img_cfg[0].cache_enable,g_boot_img_cfg[0].cache_way_disable,g_boot_img_cfg[0].img_start.flash_offset);
}
if(ret!=BFLB_BOOT2_SUCCESS){
return;
}
/* Deal Other CPUs' entry point */
/* Prepare release CPU1 anyway */
for(i=1;i<g_cpu_count;i++){
if(g_boot_img_cfg[i].img_valid){
BL_WR_WORD(g_boot_cpu_cfg[i].msp_store_addr,g_boot_img_cfg[i].msp_val);
BL_WR_WORD(g_boot_cpu_cfg[i].pc_store_addr,g_boot_img_cfg[i].entry_point);
}else{
BL_WR_WORD(g_boot_cpu_cfg[i].msp_store_addr,0);
BL_WR_WORD(g_boot_cpu_cfg[i].pc_store_addr,0);
}
}
/* Deal CPU0's entry point */
if(g_boot_img_cfg[0].img_valid){
pentry=(pentry_t)g_boot_img_cfg[0].entry_point;
if(g_boot_img_cfg[0].msp_val!=0){
__set_MSP(g_boot_img_cfg[0].msp_val);
}
/* Release other CPUs unless user halt it */
if(g_cpu_count!=1&&!g_boot_img_cfg[0].halt_cpu1){
blsp_boot2_releae_other_cpu();
}
if(pentry!=NULL){
pentry();
}
}
/* Release other CPUs unless user halt it */
if(g_cpu_count!=1&&!g_boot_img_cfg[0].halt_cpu1){
blsp_boot2_releae_other_cpu();
}
/* If cann't jump stay here */
while(1){
/*use soft delay only */
ARCH_Delay_MS(100);
}
}
/*@} end of group BLSP_COMMON_Public_Functions */
/*@} end of group BLSP_COMMON */
/*@} end of group BL606_BLSP_Boot2 */

View file

@ -0,0 +1,89 @@
/**
******************************************************************************
* @file blsp_common.h
* @version V1.2
* @date
* @brief This file is the peripheral case header file
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT(c) 2018 Bouffalo Lab</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of Bouffalo Lab nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
#ifndef __BLSP_COMMON_H__
#define __BLSP_COMMON_H__
#include "stdint.h"
/** @addtogroup BL606_BLSP_Boot2
* @{
*/
/** @addtogroup BLSP_COMMON
* @{
*/
/** @defgroup BLSP_COMMON_Public_Types
* @{
*/
/*@} end of group BLSP_COMMON_Public_Types */
/** @defgroup BLSP_COMMON_Public_Constants
* @{
*/
/*@} end of group BLSP_COMMON_Public_Constants */
/** @defgroup BLSP_COMMON_Public_Macros
* @{
*/
#define BLSP_BOOT2_CP_FLAG 0x02
#define BLSP_BOOT2_MP_FLAG 0x01
#define BLSP_BOOT2_SP_FLAG 0x00
/*@} end of group BLSP_COMMON_Public_Macros */
/** @defgroup BLSP_COMMON_Public_Functions
* @{
*/
void blsp_dump_data(void *datain,int len);
void blsp_fix_invalid_msp_pc(void);
void blsp_boot2_jump_entry(void);
int32_t blsp_mediaboot_pre_jump(void);
uint8_t blsp_boot2_get_feature_flag(void);
uint8_t blsp_boot2_get_log_disable_flag(void);
uint8_t blsp_boot2_8m_support_flag(void);
uint8_t blsp_boot2_dump_critical_flag(void);
uint32_t blsp_boot2_get_baudrate(void);
uint8_t blsp_boot2_get_tx_gpio(void);
/*@} end of group BLSP_COMMON_Public_Functions */
/*@} end of group BLSP_COMMON */
/*@} end of group BL606_BLSP_Boot2 */
#endif /* __BLSP_COMMON_H__ */

View file

@ -0,0 +1,365 @@
/**
******************************************************************************
* @file blsp_media_boot.c
* @version V1.2
* @date
* @brief This file is the peripheral case c file
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT(c) 2018 Bouffalo Lab</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of Bouffalo Lab nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
#include "stdint.h"
#include "string.h"
#include "blsp_port.h"
#include "bflb_platform.h"
#include "blsp_bootinfo.h"
#include "blsp_common.h"
#include "blsp_boot_parser.h"
#include "blsp_media_boot.h"
#include "softcrc.h"
#include "bflb_eflash_loader_uart.h"
/** @addtogroup BL606_BLSP_Boot2
* @{
*/
/** @addtogroup BLSP_MEDIA_BOOT
* @{
*/
/** @defgroup BLSP_MEDIA_BOOT_Private_Macros
* @{
*/
/*@} end of group BLSP_MEDIA_BOOT_Private_Macros */
/** @defgroup BLSP_MEDIA_BOOT_Private_Types
* @{
*/
/*@} end of group BLSP_MEDIA_BOOT_Private_Types */
/** @defgroup BLSP_MEDIA_BOOT_Private_Variables
* @{
*/
/*@} end of group BLSP_MEDIA_BOOT_Private_Variables */
/** @defgroup BLSP_MEDIA_BOOT_Global_Variables
* @{
*/
extern SEC_Eng_SHA256_Ctx g_sha_ctx;
/*@} end of group BLSP_MEDIA_BOOT_Global_Variables */
/** @defgroup BLSP_MEDIA_BOOT_Private_Fun_Declaration
* @{
*/
/*@} end of group BLSP_MEDIA_BOOT_Private_Fun_Declaration */
/** @defgroup BLSP_MEDIA_BOOT_Private_Functions_User_Define
* @{
*/
/*@} end of group BLSP_MEDIA_BOOT_Private_Functions_User_Define */
/** @defgroup BLSP_MEDIA_BOOT_Private_Functions
* @{
*/
/****************************************************************************//**
* @brief Media boot calculate hash
*
* @param startAddr: Start address to calculate
* @param totalLen: Data length to calculate
*
* @return BL_Err_Type
*
*******************************************************************************/
static int32_t blsp_mediaboot_cal_hash(uint32_t start_addr,uint32_t total_len)
{
int32_t deal_len=0;
int32_t read_len=0;
uint32_t addr=start_addr;
int32_t ret;
uint32_t dump_cnt=0;
while(deal_len<total_len){
read_len=total_len-deal_len;
if(read_len>BFLB_BOOT2_READBUF_SIZE){
read_len=BFLB_BOOT2_READBUF_SIZE;
}
ret=blsp_mediaboot_read(addr,g_boot2_read_buf,read_len);
if(ret!=BFLB_BOOT2_SUCCESS){
return ret;
}
if(dump_cnt==0){
if(blsp_boot2_dump_critical_flag()){
blsp_dump_data(g_boot2_read_buf,read_len);
}
dump_cnt=1;
}
/* Update hash*/
Sec_Eng_SHA256_Update(&g_sha_ctx,SEC_ENG_SHA_ID0,(uint8_t *)g_boot2_read_buf,read_len);
//blsp_dump_data((uint8_t *)g_boot2_read_buf,readLen);
addr+=read_len;
deal_len+=read_len;
}
return BFLB_BOOT2_SUCCESS;
}
/****************************************************************************//**
* @brief Media boot read signature
*
* @param addr: Start address to read signature
* @param len: Signature length pointer
*
* @return BL_Err_Type
*
*******************************************************************************/
#if 0
static int32_t BLSP_MediaBoot_Read_Signaure(uint32_t addr,uint32_t *len)
{
int32_t ret=BFLB_BOOT2_SUCCESS;
uint32_t sig_len=0;
uint8_t *ptmp;
/* Read signature*/
ret=blsp_mediaboot_read(addr,(uint8_t*)&sig_len,sizeof(sig_len));
if(ret!=BFLB_BOOT2_SUCCESS){
return ret;
}
addr+=sizeof(sig_len);
if(sig_len>BFLB_BOOT2_SIGN_MAXSIZE){
return BFLB_BOOT2_IMG_SIGNATURE_LEN_ERROR;
}
/* Tail 4 bytes for crc */
ptmp=((uint8_t *)g_boot2_read_buf);
ret=blsp_mediaboot_read(addr,(uint8_t *)(ptmp+sizeof(sig_len)),sig_len+4);
if(ret!=BFLB_BOOT2_SUCCESS){
return ret;
}
ARCH_MemCpy_Fast(g_boot2_read_buf,&sig_len,sizeof(sig_len));
addr+=(sig_len+4);
*len=sig_len;
return ret;
}
#endif
/****************************************************************************//**
* @brief Media boot parse one firmware
*
* @param g_boot_img_cfg: Boot image config pointer to hold parsed information
* @param bootheaderAddr: Bootheader start address
* @param imgAddr: Image start address
*
* @return BL_Err_Type
*
*******************************************************************************/
static int32_t blsp_mediaboot_parse_one_fw(boot_image_config *boot_img_cfg,uint32_t boot_header_addr,uint32_t img_addr)
{
uint32_t addr=boot_header_addr;
int32_t ret;
// uint32_t sig_len=0;
/* Read boot header*/
MSG_DBG("R header from %08x\r\n",addr);
ret=blsp_mediaboot_read(addr,g_boot2_read_buf,sizeof(boot_header_config));
if(ret!=BFLB_BOOT2_SUCCESS){
return ret;
}
if(blsp_boot2_dump_critical_flag()){
blsp_dump_data(g_boot2_read_buf,sizeof(boot_header_config));
}
addr+=sizeof(boot_header_config);
ret=blsp_boot_parse_bootheader(boot_img_cfg,(uint8_t *)g_boot2_read_buf);
if(ret!=BFLB_BOOT2_SUCCESS){
return ret;
}
/* Due to OTA, the flash_offset is changed, so copy from partition info */
boot_img_cfg->img_start.flash_offset=img_addr;
if(boot_img_cfg->no_segment){
/* Flash image */
if(!boot_img_cfg->hash_ignore){
MSG_DBG("Cal hash\r\n");
ret= blsp_mediaboot_cal_hash(img_addr,
boot_img_cfg->img_segment_info.img_len);
if(ret!=BFLB_BOOT2_SUCCESS){
return ret;
}
ret= blsp_boot_parser_check_hash(boot_img_cfg);
if(ret!=BFLB_BOOT2_SUCCESS){
return ret;
}
}
// ret=blsp_boot_parser_check_signature(g_boot_img_cfg);
// if(ret!=BFLB_BOOT2_SUCCESS){
// return ret;
// }
boot_img_cfg->img_valid=1;
}else{
boot_img_cfg->img_valid=0;
}
return ret;
}
/*@} end of group BLSP_MEDIA_BOOT_Private_Functions */
/** @defgroup BLSP_MEDIA_BOOT_Public_Functions
* @{
*/
/****************************************************************************//**
* @brief Media boot read data
*
* @param addr: Start address to read
* @param data: Data buffer to hold read data
* @param len: Data length to read
*
* @return BL_Err_Type
*
*******************************************************************************/
int32_t ATTR_TCM_SECTION blsp_mediaboot_read(uint32_t addr,uint8_t *data, uint32_t len)
{
XIP_SFlash_Read_Via_Cache_Need_Lock(BLSP_BOOT2_XIP_BASE+addr,data,len);
return BFLB_BOOT2_SUCCESS;
}
/****************************************************************************//**
* @brief Media boot main process
*
* @param cpuBootheaderAddr[BFLB_BOOT2_CPU_MAX]: CPU bootheader address list
* @param cpuRollBack[BFLB_BOOT2_CPU_MAX]: CPU need roll back flag hold list
* @param rollBack: 1 for rollback when imge error occurs, 0 for not rollback when imge error occurs
*
* @return BL_Err_Type
*
*******************************************************************************/
int32_t blsp_mediaboot_main(uint32_t cpu_boot_header_addr[BFLB_BOOT2_CPU_MAX],uint8_t cpu_roll_back[BFLB_BOOT2_CPU_MAX],uint8_t roll_back)
{
int32_t ret;
uint32_t i=0;
uint32_t valid_img_found=0;
uint32_t boot_header_addr[BFLB_BOOT2_CPU_MAX];
MSG_DBG("Media boot main\r\n");
/* Reset some parameters*/
for(i=0;i<BFLB_BOOT2_CPU_MAX;i++){
memset(&g_boot_img_cfg[i],0,sizeof(g_boot_img_cfg[i]));
boot_header_addr[i]=cpu_boot_header_addr[i];
cpu_roll_back[i]=0;
}
g_boot_img_cfg[0].halt_cpu1=0;
/* Try to boot from flash */
for(i=0;i<g_cpu_count;i++){
if(boot_header_addr[i]==0){
MSG_ERR("CPU %d not boot\r\n",i);
continue;
}
ret=blsp_mediaboot_parse_one_fw(&g_boot_img_cfg[i],boot_header_addr[i],
boot_header_addr[i]+BFLB_FW_IMG_OFFSET_AFTER_HEADER);
if(ret!=BFLB_BOOT2_SUCCESS){
MSG_ERR("CPU %d boot fail\r\n",i);
cpu_roll_back[i]=1;
}else{
valid_img_found++;
}
}
if(valid_img_found!=g_cpu_count && 1==roll_back){
/* For CP and DP, found CPU0 image is taken as correct when the other not found, others as wrong and try to rollback */
if(boot_header_addr[1]==0 && valid_img_found==1){
MSG_DBG("Found One img Only\r\n");
}else{
MSG_ERR("Image roll back\r\n");
return BFLB_BOOT2_IMG_Roll_Back;
}
}
if(valid_img_found==0){
MSG_ERR("no valid img found\r\n");
return BFLB_BOOT2_IMG_ALL_INVALID_ERROR;
}
/* Get msp and pc value */
for(i=0;i<g_cpu_count;i++){
if(g_boot_img_cfg[i].img_valid){
if(g_boot_img_cfg[i].entry_point==0){
#ifdef ARCH_ARM
blsp_mediaboot_read(g_boot_img_cfg[i].img_start.flash_offset,
(uint8_t *)&g_boot_img_cfg[i].msp_val,4);
blsp_mediaboot_read(g_boot_img_cfg[i].img_start.flash_offset+4,
(uint8_t *)&g_boot_img_cfg[i].entry_point,4);
#endif
#ifdef ARCH_RISCV
g_boot_img_cfg[i].entry_point=g_boot_cpu_cfg[i].default_xip_addr;
#endif
}
}
}
if(blsp_boot2_get_feature_flag()==BLSP_BOOT2_CP_FLAG){
/*co-processor*/
g_boot_img_cfg[1].img_start.flash_offset=g_boot_img_cfg[0].img_start.flash_offset;
g_boot_img_cfg[1].msp_val=g_boot_img_cfg[0].msp_val;
g_boot_img_cfg[1].entry_point=g_boot_img_cfg[0].entry_point;
g_boot_img_cfg[1].cache_enable=g_boot_img_cfg[0].cache_enable;
g_boot_img_cfg[1].img_valid=1;
g_boot_img_cfg[1].cache_way_disable=0xf;
}
MSG_DBG("%08x,%08x\r\n",g_boot_img_cfg[0].msp_val,g_boot_img_cfg[0].entry_point);
MSG_DBG("%08x,%08x\r\n",g_boot_img_cfg[1].msp_val,g_boot_img_cfg[1].entry_point);
MSG_DBG("%08x,%08x\r\n",g_boot_img_cfg[0].img_start.flash_offset,g_boot_img_cfg[0].cache_way_disable);
MSG_DBG("%08x,%08x\r\n",g_boot_img_cfg[1].img_start.flash_offset,g_boot_img_cfg[1].cache_way_disable);
MSG_DBG("CPU Count %d,%d\r\n",g_cpu_count,g_boot_img_cfg[0].halt_cpu1);
blsp_boot2_show_timer();
/* Fix invalid pc and msp */
blsp_fix_invalid_msp_pc();
if(BFLB_EFLASH_LOADER_HANDSHAKE_SUSS == bflb_eflash_loader_uart_handshake_poll()){
bflb_eflash_loader_main();
}
/* Prepare jump to entry*/
blsp_mediaboot_pre_jump();
/* We should never get here unless something is wrong */
return BFLB_BOOT2_FAIL;
}
/*@} end of group BLSP_MEDIA_BOOT_Public_Functions */
/*@} end of group BLSP_MEDIA_BOOT */
/*@} end of group BL606_BLSP_Boot2 */

View file

@ -0,0 +1,85 @@
/**
******************************************************************************
* @file blsp_media_boot.h
* @version V1.2
* @date
* @brief This file is the peripheral case header file
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT(c) 2018 Bouffalo Lab</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of Bouffalo Lab nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
#ifndef __BLSP_MEDIA_BOOT_H__
#define __BLSP_MEDIA_BOOT_H__
#include "stdint.h"
#include "stdio.h"
#include "string.h"
/** @addtogroup BL606_BLSP_Boot2
* @{
*/
/** @addtogroup BLSP_MEDIA_BOOT
* @{
*/
/** @defgroup BLSP_MEDIA_BOOT_Public_Types
* @{
*/
/*@} end of group BLSP_MEDIA_BOOT_Public_Types */
/** @defgroup BLSP_MEDIA_BOOT_Public_Constants
* @{
*/
/*@} end of group BLSP_MEDIA_BOOT_Public_Constants */
/** @defgroup BLSP_MEDIA_BOOT_Public_Macros
* @{
*/
/*@} end of group BLSP_MEDIA_BOOT_Public_Macros */
/** @defgroup BLSP_MEDIA_BOOT_Public_Functions
* @{
*/
int32_t blsp_mediaboot_read(uint32_t addr,uint8_t *data, uint32_t len);
//uint32_t BLSP_MediaBoot_Get_Flash_Cfg(uint32_t bootheaderAddr);
//int32_t BLSP_MediaBoot_Cache_Enable(uint8_t contRead);
int32_t blsp_mediaboot_main(uint32_t cpu_boot_header_addr[BFLB_BOOT2_CPU_MAX],uint8_t cpu_roll_back[BFLB_BOOT2_CPU_MAX],uint8_t roll_back);
void blsp_boot2_show_timer(void);
//int32_t ATTR_TCM_SECTION BLSP_MediaBoot_Set_Encrypt(uint8_t index,boot_image_config *g_boot_img_cfg);
/*@} end of group BLSP_MEDIA_BOOT_Public_Functions */
/*@} end of group BLSP_MEDIA_BOOT */
/*@} end of group BL606_BLSP_Boot2 */
#endif /* __BLSP_MEDIA_BOOT_H__ */

View file

@ -0,0 +1,666 @@
/**
******************************************************************************
* @file blsp_port.c
* @version V1.2
* @date
* @brief This file is the peripheral case c file
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT(c) 2018 Bouffalo Lab</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of Bouffalo Lab nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
#include "stdio.h"
#include "stdint.h"
#include "string.h"
#include "blsp_port.h"
#include "bflb_platform.h"
#include "blsp_bootinfo.h"
#include "blsp_common.h"
#include "blsp_media_boot.h"
#include "tzc_sec_reg.h"
/** @addtogroup BL606_BLSP_Boot2
* @{
*/
/** @addtogroup BLSP_PORT
* @{
*/
/** @defgroup BLSP_PORT_Private_Macros
* @{
*/
/*@} end of group BLSP_PORT_Private_Macros */
/** @defgroup BLSP_PORT_Private_Types
* @{
*/
/*@} end of group BLSP_PORT_Private_Types */
/** @defgroup BLSP_PORT_Private_Variables
* @{
*/
/*@} end of group BLSP_PORT_Private_Variables */
/** @defgroup BLSP_PORT_Global_Variables
* @{
*/
/*@} end of group BLSP_PORT_Global_Variables */
/** @defgroup BLSP_PORT_Private_Fun_Declaration
* @{
*/
/*@} end of group BLSP_PORT_Private_Fun_Declaration */
/** @defgroup BLSP_PORT_Private_Functions_User_Define
* @{
*/
/*@} end of group BLSP_PORT_Private_Functions_User_Define */
/** @defgroup BLSP_PORT_Private_Functions
* @{
*/
/*@} end of group BLSP_PORT_Private_Functions */
/** @defgroup BLSP_PORT_Public_Functions
* @{
*/
/****************************************************************************//**
* @brief Boot2 init timer for cal boot time
*
* @param None
*
* @return None
*
*******************************************************************************/
void blsp_boot2_init_timer(void)
{
TIMER_CFG_Type timer_cfg = {
TIMER_CH0, /* timer channel 0 */
TIMER_CLKSRC_FCLK, /* timer clock source:bus clock */
TIMER_PRELOAD_TRIG_NONE, /* reaload on comaparator 2 */
TIMER_COUNT_FREERUN, /* preload when match occur */
159, /* clock divider */
0xfffffffe, /* match value 0 */
0xfffffffe, /* match value 1 */
0xfffffffe, /* match value 2 */
0, /* preload value */
};
/* Disable all interrupt */
TIMER_IntMask(timer_cfg.timerCh, TIMER_INT_ALL, MASK);
/* Disable timer before config */
TIMER_Disable(timer_cfg.timerCh);
/* Timer init with default configuration */
TIMER_Init(&timer_cfg);
/* Enable timer */
TIMER_Enable(timer_cfg.timerCh);
}
/****************************************************************************//**
* @brief Boot2 Disable other CPU cache
*
* @param None
*
* @return None
*
*******************************************************************************/
void blsp_boot2_disable_other_cache(void)
{
}
/****************************************************************************//**
* @brief Boot2 Flash Boot2 cache
*
* @param None
*
* @return None
*
*******************************************************************************/
void blsp_boot2_flush_xip_cache()
{
L1C_Cache_Flush(0xf);
}
/****************************************************************************//**
* @brief Boot2 Get fw clock config
*
* @param cfg: Clock config pointer
*
* @return boot_error_code
*
*******************************************************************************/
int32_t ATTR_TCM_SECTION blsp_boot2_get_clk_cfg(boot_clk_config *cfg)
{
XIP_SFlash_Read_Via_Cache_Need_Lock(8+sizeof(boot_flash_config)+BLSP_BOOT2_XIP_BASE,
(uint8_t *)cfg,sizeof(boot_clk_config));
return BFLB_BOOT2_SUCCESS;
}
/****************************************************************************//**
* @brief Boot2 show timer for cal boot time
*
* @param None
*
* @return None
*
*******************************************************************************/
void blsp_boot2_show_timer(void)
{
MSG("Counter value=%d\n",(unsigned int)bflb_platform_get_time_ms());
}
/****************************************************************************//**
* @brief Boot2 get efuse config security
*
* @param None
*
* @return None
*
*******************************************************************************/
void ATTR_TCM_SECTION blsp_boot2_get_efuse_cfg(boot_efuse_hw_config *g_efuse_cfg)
{
uint32_t tmp;
HBN_Set_ROOT_CLK_Sel(HBN_ROOT_CLK_XTAL);
/* Get sign and aes type*/
EF_Ctrl_Read_Secure_Boot((EF_Ctrl_Sign_Type*)g_efuse_cfg->sign, (EF_Ctrl_SF_AES_Type*)g_efuse_cfg->encrypted);
/* Get hash:aes key slot 0 and slot1*/
EF_Ctrl_Read_AES_Key(0,(uint32_t *)g_efuse_cfg->pk_hash_cpu0,8);
EF_Ctrl_Read_Chip_ID(g_efuse_cfg->chip_id);
/* Get HBN check sign config */
EF_Ctrl_Read_Sw_Usage(0,&tmp);
g_efuse_cfg->hbn_check_sign=(tmp>>22)&0x01;
GLB_Set_System_CLK_Div(0,1);
HBN_Set_ROOT_CLK_Sel(HBN_ROOT_CLK_DLL);
}
/****************************************************************************//**
* @brief Boot2 reset sec_eng module
*
* @param None
*
* @return None
*
*******************************************************************************/
void blsp_boot2_reset_sec_eng(void)
{
GLB_AHB_Slave1_Reset(BL_AHB_SLAVE1_SEC);
}
/****************************************************************************//**
* @brief Boot2 init sec_eng PKA module
*
* @param None
*
* @return None
*
*******************************************************************************/
void BLSP_Boot2_Init_Sec_Eng_PKA(void)
{
Sec_Eng_PKA_Reset();
Sec_Eng_PKA_BigEndian_Enable();
}
/****************************************************************************//**
* @brief Boot2 get cpu count info
*
* @param None
*
* @return None
*
*******************************************************************************/
uint32_t blsp_boot2_get_cpu_count(void)
{
return 1;
}
/****************************************************************************//**
* @brief Read power save mode
*
* @param None
*
* @return BFLB_PSM_ACTIVE or BFLB_PSM_HBN
*
*******************************************************************************/
uint8_t blsp_read_power_save_mode(void)
{
if(HBN_Get_Status_Flag()==HBN_STATUS_WAKEUP_FLAG){
return BFLB_PSM_HBN;
}else{
return BFLB_PSM_ACTIVE;
}
}
/****************************************************************************//**
* @brief Boot2 Pass parameter to FW
*
* @param data: Data pointer to pass
* @param len: Data length
*
* @return None
*
*******************************************************************************/
void blsp_boot2_pass_parameter(void *data,uint32_t len)
{
static uint8_t *p_parameter=NULL;
if(len==0){
GLB_Set_EM_Sel(0);
p_parameter=(uint8_t *)(0x42020000+60*1024);
return;
}
ARCH_MemCpy_Fast(p_parameter,data,len);
p_parameter+=len;
}
/****************************************************************************//**
* @brief Boot2 Get XIP offset
*
* @param None
*
* @return XIP offset
*
*******************************************************************************/
uint32_t blsp_boot2_get_xip_offset(void)
{
return SF_Ctrl_Get_Flash_Image_Offset();
}
/****************************************************************************//**
* @brief Get User specified firmware to boot up
*
* @param None
*
* @return User specified firmware name
*
*******************************************************************************/
uint8_t* blsp_get_user_specified_fw(void)
{
return (uint8_t*)(HBN_BASE+HBN_RSV0_OFFSET);
}
/****************************************************************************//**
* @brief Clear User specified firmware to boot up
*
* @param None
*
* @return None
*
*******************************************************************************/
void blsp_clr_user_specified_fw(void)
{
uint32_t *p=(uint32_t*)(HBN_BASE+HBN_RSV0_OFFSET);
*p=0;
}
/****************************************************************************//**
* @brief Release other CPU
*
* @param None
*
* @return None
*
*******************************************************************************/
void ATTR_TCM_SECTION blsp_boot2_releae_other_cpu(void)
{
}
/****************************************************************************//**
* @brief Set encryption config
*
* @param index: Region index
* @param g_boot_img_cfg: Boot image config pointer to hold parsed information
*
* @return BL_Err_Type
*
*******************************************************************************/
int32_t ATTR_TCM_SECTION blsp_boot2_set_encrypt(uint8_t index,boot_image_config *g_boot_img_cfg)
{
uint32_t aes_enabled=0;
uint32_t len=0;
/* FIXME:,1:lock, should be 1??*/
if(g_boot_img_cfg->encrypt_type!=0){
len=g_boot_img_cfg->img_segment_info.img_len;
if(len!=0){
SF_Ctrl_AES_Set_Key_BE(index,NULL,(SF_Ctrl_AES_Key_Type)(g_boot_img_cfg->encrypt_type-1));
SF_Ctrl_AES_Set_IV_BE(index,g_boot_img_cfg->aes_iv,g_boot_img_cfg->img_start.flash_offset);
SF_Ctrl_AES_Set_Region(index,1/*enable this region*/,1/*hardware key*/,
g_boot_img_cfg->img_start.flash_offset,
g_boot_img_cfg->img_start.flash_offset+len-1,
g_boot_img_cfg->aes_region_lock/*lock*/);
aes_enabled=1;
}
}
if(aes_enabled){
SF_Ctrl_AES_Enable_BE();
SF_Ctrl_AES_Enable();
}
return BFLB_BOOT2_SUCCESS;
}
/****************************************************************************//**
* @brief Media boot set cache according to image config
*
* @param None
*
* @return BL_Err_Type
*
*******************************************************************************/
int32_t ATTR_TCM_SECTION blsp_boot2_set_cache(uint8_t cont_read,SPI_Flash_Cfg_Type *flash_cfg,boot_image_config *g_boot_img_cfg)
{
uint32_t tmp[1];
uint32_t stat;
/* To make it simple, exit cont read anyway */
SF_Ctrl_Set_Owner(SF_CTRL_OWNER_SAHB);
SFlash_Reset_Continue_Read(flash_cfg);
if(flash_cfg->cReadSupport==0){
cont_read=0;
}
if(cont_read==1){
stat=SFlash_Read(flash_cfg, flash_cfg->ioMode&0xf, 1, 0x00000000, (uint8_t *)tmp, sizeof(tmp));
if(SUCCESS!=stat){
return BFLB_BOOT2_FAIL;
}
}
/* Set default value */
L1C_Cache_Enable_Set(0xf);
if(g_boot_img_cfg[0].cache_enable){
if((g_boot_img_cfg[0].entry_point&0xFF000000)==BLSP_BOOT2_XIP_BASE){
SF_Ctrl_Set_Flash_Image_Offset(g_boot_img_cfg[0].img_start.flash_offset);
SFlash_Cache_Read_Enable(flash_cfg, flash_cfg->ioMode&0xf, cont_read, g_boot_img_cfg[0].cache_way_disable);
}else{
}
}
return BFLB_BOOT2_SUCCESS;
}
/****************************************************************************//**
* @brief Check if MSP is valid
*
* @param msp_val: MSP value
*
* @return 1 for valid and 0 for invalid
*
*******************************************************************************/
int32_t blsp_is_msp_valid(uint32_t msp_val)
{
if(msp_val>BL702_WRAM_BASE&&msp_val<=BL702_WRAM_END){
return 1;
}else if(msp_val>BL702_WRAM_REMAP0_BASE&&msp_val<=BL702_WRAM_REMAP0_END){
return 1;
}else if(msp_val>BL702_WRAM_REMAP1_BASE&&msp_val<=BL702_WRAM_REMAP1_END){
return 1;
}else if(msp_val>BL702_WRAM_REMAP2_BASE&&msp_val<=BL702_WRAM_REMAP2_END){
return 1;
}else if(msp_val>BL702_TCM_BASE&&msp_val<=BL702_TCM_END){
return 1;
}else if(msp_val>BL702_TCM_REMAP0_BASE&&msp_val<=BL702_TCM_REMAP0_END){
return 1;
}else if(msp_val>BL702_TCM_REMAP1_BASE&&msp_val<=BL702_TCM_REMAP1_END){
return 1;
}else if(msp_val>BL702_TCM_REMAP2_BASE&&msp_val<=BL702_TCM_REMAP2_END){
return 1;
}
return 0;
}
/****************************************************************************//**
* @brief Check if PC is valid
*
* @param pcVal: PC value
*
* @return 1 for valid and 0 for invalid
*
*******************************************************************************/
int32_t blsp_is_pc_valid(uint32_t pc_val)
{
/* Check XIP memory */
if(pc_val>=BL702_FLASH_XIP_BASE&&pc_val<BL702_FLASH_XIP_END){
return 1;
}else if(pc_val>=BL702_FLASH_XIP_REMAP0_BASE&&pc_val<BL702_FLASH_XIP_REMAP0_END){
return 1;
}else if(pc_val>=BL702_FLASH_XIP_REMAP1_BASE&&pc_val<BL702_FLASH_XIP_REMAP1_END){
return 1;
}else if(pc_val>=BL702_FLASH_XIP_REMAP2_BASE&&pc_val<BL702_FLASH_XIP_REMAP2_END){
return 1;
}
/* Check RAM memory */
if(pc_val>=BL702_WRAM_BASE&&pc_val<BL702_WRAM_END){
return 1;
}else if(pc_val>=BL702_WRAM_REMAP0_BASE&&pc_val<BL702_WRAM_REMAP0_END){
return 1;
}else if(pc_val>=BL702_WRAM_REMAP1_BASE&&pc_val<BL702_WRAM_REMAP1_END){
return 1;
}else if(pc_val>=BL702_WRAM_REMAP2_BASE&&pc_val<BL702_WRAM_REMAP2_END){
return 1;
}else if(pc_val>=BL702_TCM_BASE&&pc_val<BL702_TCM_END){
return 1;
}else if(pc_val>=BL702_TCM_REMAP0_BASE&&pc_val<BL702_TCM_REMAP0_END){
return 1;
}else if(pc_val>=BL702_TCM_REMAP1_BASE&&pc_val<BL702_TCM_REMAP1_END){
return 1;
}else if(pc_val>=BL702_TCM_REMAP2_BASE&&pc_val<BL702_TCM_REMAP2_END){
return 1;
}
return 0;
}
/****************************************************************************//**
* @brief Security boot finished
*
* @param None
*
* @return None
*
*******************************************************************************/
void ATTR_TCM_SECTION blsp_sboot_finish(void)
{
uint32_t tmp_val;
tmp_val=BL_RD_REG(TZC_SEC_BASE,TZC_SEC_TZC_ROM_CTRL);
tmp_val=BL_SET_REG_BITS_VAL(tmp_val,TZC_SEC_TZC_SBOOT_DONE,0xf);
BL_WR_REG(TZC_SEC_BASE,TZC_SEC_TZC_ROM_CTRL,tmp_val);
}
/****************************************************************************//**
* @brief Fix MSP and PC,if they are invalid
*
* @param None
*
* @return None
*
*******************************************************************************/
void blsp_fix_invalid_msp_pc(void)
{
uint32_t i=0;
for(i=0;i<BFLB_BOOT2_CPU_MAX;i++){
if(!blsp_is_msp_valid(g_boot_img_cfg[i].msp_val)){
g_boot_img_cfg[i].msp_val=0;
}
if(!blsp_is_pc_valid(g_boot_img_cfg[i].entry_point)){
g_boot_img_cfg[i].entry_point=0;
}
}
}
typedef void( *pFunc )( void );
extern pFunc __Vectors[];
/****************************************************************************//**
* @brief Boot2 Get log tx GPIO
*
* @param None
*
* @return None
*
*******************************************************************************/
uint8_t ATTR_TCM_SECTION blsp_boot2_get_tx_gpio(void)
{
uint8_t *p=((uint8_t *)&__Vectors[9]+1);
return *p;
}
/****************************************************************************//**
* @brief Boot2 Get UART Port
*
* @param None
*
* @return 0(UART0)/1(UART1)
*
*******************************************************************************/
uint8_t ATTR_TCM_SECTION blsp_boot2_get_uart_port(void)
{
uint8_t *p=((uint8_t *)&__Vectors[9]+2);
return *p;
}
/****************************************************************************//**
* @brief Boot2 Get Feature Flag
*
* @param None
*
* @return None
*
*******************************************************************************/
uint8_t ATTR_TCM_SECTION blsp_boot2_get_feature_flag(void)
{
static uint8_t boot2_flag=0xff;
uint8_t *p=((uint8_t *)&__Vectors[10]+0);
if(boot2_flag==0xff){
boot2_flag=*p;
}
return boot2_flag;
}
/****************************************************************************//**
* @brief Boot2 Get log disable Flag
*
* @param None
*
* @return None
*
*******************************************************************************/
uint8_t ATTR_TCM_SECTION blsp_boot2_get_log_disable_flag(void)
{
uint8_t *p=((uint8_t *)&__Vectors[10]+1);
return *p;
}
/****************************************************************************//**
* @brief Boot2 Get 8M Flash support Flag
*
* @param None
*
* @return None
*
*******************************************************************************/
uint8_t ATTR_TCM_SECTION blsp_boot2_8m_support_flag(void)
{
uint8_t *p=((uint8_t *)&__Vectors[10]+2);
return *p;
}
/****************************************************************************//**
* @brief Boot2 Get dump critical data flag
*
* @param None
*
* @return None
*
*******************************************************************************/
uint8_t ATTR_TCM_SECTION blsp_boot2_dump_critical_flag(void)
{
uint8_t *p=((uint8_t *)&__Vectors[10]+3);
return *p;
}
/****************************************************************************//**
* @brief Boot2 Get Baudrate
*
* @param None
*
* @return None
*
*******************************************************************************/
uint32_t ATTR_TCM_SECTION blsp_boot2_get_baudrate(void)
{
uint32_t *p=((uint32_t *)&__Vectors[13]);
return *p;
}
/*@} end of group BLSP_PORT_Public_Functions */
/*@} end of group BLSP_PORT */
/*@} end of group BL606_BLSP_Boot2 */

View file

@ -0,0 +1,123 @@
/**
******************************************************************************
* @file blsp_port.h
* @version V1.2
* @date
* @brief This file is the peripheral case header file
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT(c) 2018 Bouffalo Lab</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of Bouffalo Lab nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
#ifndef __BLSP_PORT_H__
#define __BLSP_PORT_H__
#include "stdint.h"
#include "bl702_glb.h"
#include "bl702_aon.h"
#include "bl702_hbn.h"
#include "bl702_ef_ctrl.h"
#include "bl702_sflash.h"
#include "bl702_xip_sflash.h"
#include "bl702_xip_sflash_ext.h"
#include "bl702_sf_cfg.h"
#include "bl702_timer.h"
#include "bl702_sec_eng.h"
//#include "bflb_ecdsa.h"
/** @addtogroup BL606_BLSP_Boot2
* @{
*/
/** @addtogroup BLSP_PORT
* @{
*/
/** @defgroup BLSP_PORT_Public_Types
* @{
*/
/*@} end of group BLSP_PORT_Public_Types */
/** @defgroup BLSP_PORT_Public_Constants
* @{
*/
/*CPU1's MSP and PC from NP bootrom*/
//#define BFLB_BOOT2_CPU1_APP_PC_ADDR (BL60X_SRAM_END-4)
//#define BFLB_BOOT2_CPU1_APP_MSP_ADDR (BL60X_SRAM_END-8)
//#define BFLB_BOOT2_CPU1_DBG_INFO_ADDR (BL60X_SRAM_END-12)
#define BFLB_BOOT2_CPU1_APP_PC_ADDR (0)
#define BFLB_BOOT2_CPU1_APP_MSP_ADDR (0)
#define BFLB_BOOT2_CPU1_DBG_INFO_ADDR (0)
#define MFG_START_REQUEST_OFFSET ((4+184)*1024)
#define BLSP_BOOT2_XIP_BASE BL702_FLASH_XIP_BASE
#define ARCH_Delay_MS BL702_Delay_MS
/*@} end of group BLSP_PORT_Public_Constants */
/** @defgroup BLSP_PORT_Public_Macros
* @{
*/
/*@} end of group BLSP_PORT_Public_Macros */
/** @defgroup BLSP_PORT_Public_Functions
* @{
*/
void blsp_boot2_init_timer(void);
void blsp_boot2_disable_other_cache(void);
void blsp_boot2_flush_xip_cache(void);
void blsp_boot2_show_timer(void);
void blsp_boot2_reset_sec_eng(void);
void blsp_boot2_init_sec_eng_pka(void);
uint32_t blsp_boot2_get_cpu_count(void);
uint8_t blsp_read_power_save_mode(void);
void blsp_boot2_pass_parameter(void *data,uint32_t len);
uint32_t blsp_boot2_get_xip_offset();
uint8_t* blsp_get_user_specified_fw(void);
void blsp_clr_user_specified_fw(void);
void blsp_boot2_releae_other_cpu(void);
int32_t blsp_is_msp_valid(uint32_t msp_val);
int32_t blsp_is_pc_valid(uint32_t pc_val);
void ATTR_TCM_SECTION blsp_sboot_finish(void);;
void blsp_fix_invalid_msp_pc(void);
uint8_t blsp_boot2_get_tx_gpio(void);
uint8_t blsp_boot2_get_uart_port(void);
uint8_t blsp_boot2_get_feature_flag(void);
uint8_t blsp_boot2_get_log_disable_flag(void);
uint8_t blsp_boot2_8m_support_flag(void);
uint8_t blsp_boot2_dump_critical_flag(void);
uint32_t blsp_boot2_get_baudrate(void);
void blsp_boot2_init_gpio_for_bootrom();
/*@} end of group BLSP_PORT_Public_Functions */
/*@} end of group BLSP_PORT */
/*@} end of group BL606_BLSP_Boot2 */
#endif /* __BLSP_PORT_H__ */

View file

@ -0,0 +1,41 @@
/**
******************************************************************************
* @file blsp_version.h
* @version V1.2
* @date
* @brief This file is the peripheral case header file
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT(c) 2018 Bouffalo Lab</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of Bouffalo Lab nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
#ifndef __MCU_SDK_VERSION_H__
#define __MCU_SDK_VERSION_H__
#define BL_SDK_VER "1.00"
#endif

View file

@ -0,0 +1 @@
make build SUPPORT_XZ=y APP=boot2 BOARD=bl706_avb

View file

@ -0,0 +1,29 @@
[pt_table]
#partition table is 4K in size
address0 = 0xE000
address1 = 0xF000
[[pt_entry]]
type = 0
name = "FW"
device = 0
address0 = 0x010000
size0 = 0x60000
address1 = 0x70000
size1 = 0x60000
# compressed image must set len,normal image can left it to 0
len = 0
[[pt_entry]]
type = 7
name = "factory"
device = 0
address0 = 0x0FB000
size0 = 0x4000
address1 = 0
size1 = 0
# compressed image must set len,normal image can left it to 0
len = 0