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

198 lines
6.3 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"
2022-03-22 11:19:23 +08:00
ATTR_NOCACHE_NOINIT_RAM_SECTION uint8_t g_malloc_buf[BFLB_BOOT2_XZ_MALLOC_BUF_SIZE];
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)
{
2022-03-22 11:19:23 +08:00
extern void system_mtimer_clock_reinit(void);
/* reinit mtimer clock */
system_mtimer_clock_reinit();
/* deinit uart */
hal_boot2_debug_uart_gpio_deinit();
2021-06-04 17:56:34 +08:00
/* 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 */
2022-03-22 11:19:23 +08:00
__disable_irq();
2021-06-04 17:56:34 +08:00
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*/
2022-03-22 11:19:23 +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-04 17:56:34 +08:00
}
}
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;
2021-06-04 17:56:34 +08:00
int32_t ret;
2021-06-20 12:25:46 +08:00
2022-03-22 11:19:23 +08:00
hal_boot2_clean_cache();
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*/
2022-03-22 11:19:23 +08:00
ret = hal_boot2_set_cache(0, &g_boot_img_cfg[0]);
2021-06-20 12:25:46 +08:00
} else {
2021-06-04 17:56:34 +08:00
/*for unencrypted img, use continuos read*/
2022-03-22 11:19:23 +08:00
ret = hal_boot2_set_cache(1, &g_boot_img_cfg[0]);
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]);
2022-03-22 11:19:23 +08:00
#if BLSP_BOOT2_CPU_MAX>1
if (hal_boot2_get_feature_flag() == HAL_BOOT2_CP_FLAG) {
/*co-processor*/
2022-03-22 11:19:23 +08:00
g_boot_img_cfg[0].cpu_cfg[1].msp_val = g_boot_img_cfg[0].cpu_cfg[0].msp_val;
g_boot_img_cfg[0].cpu_cfg[1].boot_entry = g_boot_img_cfg[0].cpu_cfg[0].boot_entry;
g_boot_img_cfg[0].cpu_cfg[1].cache_enable = g_boot_img_cfg[0].cpu_cfg[0].cache_enable;
g_boot_img_cfg[0].img_valid = 1;
g_boot_img_cfg[0].cpu_cfg[1].cache_way_dis = 0xf;
2021-06-04 17:56:34 +08:00
}
2022-03-22 11:19:23 +08:00
#endif
2021-06-04 17:56:34 +08:00
}
2021-06-20 12:25:46 +08:00
2022-03-22 11:19:23 +08:00
blsp_boot2_releae_other_cpu();
2021-06-20 12:25:46 +08:00
2022-03-22 11:19:23 +08:00
/* Deal CPU0's entry point */
for(uint32_t group = 0; group < BLSP_BOOT2_CPU_GROUP_MAX; group++){
if(g_boot_img_cfg[group].img_valid&&g_boot_img_cfg[group].cpu_cfg[0].config_enable){
if(g_boot_img_cfg[group].cpu_cfg[0].halt_cpu == 0){
pentry = (pentry_t)g_boot_img_cfg[group].cpu_cfg[0].boot_entry;
if(pentry){
pentry();
}
}
2021-06-04 17:56:34 +08:00
}
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);
2021-06-04 17:56:34 +08:00
}
}