This repository has been archived on 2023-07-17. You can view files and clone it, but cannot push or open issues or pull requests.
bl_mcu_sdk/examples/boot2_iap/blsp_common.c

213 lines
6.9 KiB
C
Raw Normal View History

2021-06-04 17:56:34 +08:00
/**
******************************************************************************
* @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 "hal_boot2.h"
2021-06-04 17:56:34 +08:00
#include "bflb_eflash_loader.h"
int32_t blsp_boot2_set_encrypt(uint8_t index, boot2_image_config *g_boot_img_cfg);
2021-06-04 17:56:34 +08:00
2021-06-20 12:25:46 +08:00
/****************************************************************************/ /**
2021-06-04 17:56:34 +08:00
* @brief Dump data
*
* @param datain: Data pointer to dump
* @param len: Data length to dump
*
* @return None
*
*******************************************************************************/
2021-06-20 12:25:46 +08:00
void blsp_dump_data(void *datain, int len)
2021-06-04 17:56:34 +08:00
{
2021-06-20 12:25:46 +08:00
int i = 0;
uint8_t *data = (uint8_t *)datain;
2021-06-04 17:56:34 +08:00
data = data;
2021-06-20 12:25:46 +08:00
for (i = 0; i < len; i++) {
if (i % 16 == 0 && i != 0) {
MSG("\r\n");
2021-06-04 17:56:34 +08:00
}
2021-06-20 12:25:46 +08:00
MSG("%02x ", data[i]);
2021-06-04 17:56:34 +08:00
}
2021-06-20 12:25:46 +08:00
MSG("\r\n");
2021-06-04 17:56:34 +08:00
}
2021-06-20 12:25:46 +08:00
/****************************************************************************/ /**
2021-06-04 17:56:34 +08:00
* @brief Media boot pre-jump
*
* @param None
*
* @return BL_Err_Type
*
*******************************************************************************/
int32_t blsp_mediaboot_pre_jump(void)
{
/* Sec eng deinit*/
hal_boot2_reset_sec_eng();
2021-06-20 12:25:46 +08:00
2021-06-04 17:56:34 +08:00
/* Platform deinit */
2021-06-20 12:25:46 +08:00
bflb_platform_deinit();
2021-06-04 17:56:34 +08:00
/* Jump to entry */
blsp_boot2_jump_entry();
2021-06-20 12:25:46 +08:00
2021-06-04 17:56:34 +08:00
return BFLB_BOOT2_SUCCESS;
}
2021-06-20 12:25:46 +08:00
/****************************************************************************/ /**
2021-06-04 17:56:34 +08:00
* @brief Boot2 exit with error and will stay forever
*
* @param None
*
* @return None
*
*******************************************************************************/
void blsp_boot2_exit(void)
2021-06-20 12:25:46 +08:00
{
hal_boot2_sboot_finish();
2021-06-20 12:25:46 +08:00
2021-06-04 17:56:34 +08:00
/* Release other CPUs*/
2021-06-20 12:25:46 +08:00
if (g_cpu_count != 1 && !g_boot_img_cfg[0].halt_cpu1) {
2021-06-04 17:56:34 +08:00
blsp_boot2_releae_other_cpu();
2021-06-20 12:25:46 +08:00
}
2021-06-04 17:56:34 +08:00
/* Stay here */
2021-06-20 12:25:46 +08:00
while (1) {
2021-06-04 17:56:34 +08:00
/* Use soft delay only */
ARCH_Delay_MS(100);
}
}
2021-06-20 12:25:46 +08:00
/****************************************************************************/ /**
2021-06-04 17:56:34 +08:00
* @brief Boot2 jump to entry_point
*
* @param None
*
* @return None
*
*******************************************************************************/
void ATTR_TCM_SECTION blsp_boot2_jump_entry(void)
{
2021-06-20 12:25:46 +08:00
pentry_t pentry;
uint32_t i = 0;
2021-06-04 17:56:34 +08:00
int32_t ret;
2021-06-20 12:25:46 +08:00
hal_boot2_sboot_finish();
2021-06-20 12:25:46 +08:00
2021-06-04 17:56:34 +08:00
/*Note:enable cache with flash offset, after this, should be no flash directl read,
If need read, should take flash offset into consideration */
2021-06-20 12:25:46 +08:00
if (0 != g_efuse_cfg.encrypted[0]) {
2021-06-04 17:56:34 +08:00
/*for encrypted img, use none-continuos read*/
2021-06-20 12:25:46 +08:00
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 {
2021-06-04 17:56:34 +08:00
/*for unencrypted img, use continuos read*/
2021-06-20 12:25:46 +08:00
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);
2021-06-04 17:56:34 +08:00
}
2021-06-20 12:25:46 +08:00
if (ret != BFLB_BOOT2_SUCCESS) {
2021-06-04 17:56:34 +08:00
return;
}
/* Set decryption before read MSP and PC*/
if(0!=g_efuse_cfg.encrypted[0]){
blsp_boot2_set_encrypt(0,&g_boot_img_cfg[0]);
blsp_boot2_set_encrypt(1,&g_boot_img_cfg[1]);
/* Get msp and pc value */
for(i=0;i<g_cpu_count;i++){
if(g_boot_img_cfg[i].img_valid){
//if(bootImgCfg[i].entryPoint==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(bootImgCfg[i].imgStart.flashOffset+4,
(uint8_t *)&g_boot_img_cfg[i].entry_point,4);
#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;
2021-06-04 17:56:34 +08:00
}
}
2021-06-20 12:25:46 +08:00
2021-06-04 17:56:34 +08:00
/* Deal CPU0's entry point */
2021-06-20 12:25:46 +08:00
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) {
2021-06-04 17:56:34 +08:00
__set_MSP(g_boot_img_cfg[0].msp_val);
}
2021-06-20 12:25:46 +08:00
2021-06-04 17:56:34 +08:00
/* Release other CPUs unless user halt it */
2021-06-20 12:25:46 +08:00
if (g_cpu_count != 1 && !g_boot_img_cfg[0].halt_cpu1) {
2021-06-04 17:56:34 +08:00
blsp_boot2_releae_other_cpu();
}
2021-06-20 12:25:46 +08:00
if (pentry != NULL) {
2021-06-04 17:56:34 +08:00
pentry();
}
2021-06-20 12:25:46 +08:00
}
2021-06-04 17:56:34 +08:00
/* Release other CPUs unless user halt it */
2021-06-20 12:25:46 +08:00
if (g_cpu_count != 1 && !g_boot_img_cfg[0].halt_cpu1) {
2021-06-04 17:56:34 +08:00
blsp_boot2_releae_other_cpu();
}
2021-06-20 12:25:46 +08:00
2021-06-04 17:56:34 +08:00
/* If cann't jump stay here */
2021-06-20 12:25:46 +08:00
while (1) {
2021-06-04 17:56:34 +08:00
/*use soft delay only */
ARCH_Delay_MS(100);
}
}