mirror of
https://github.com/espressif/esp-idf.git
synced 2025-11-15 15:00:02 +01:00
components/log: add implementation, update a few components to use it
This also removes logging implementation from bootloader and replaces it with the one provided by the log component. Some occurrences of printf and ets_printf have been changed to ESP_LOGx APIs.
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
#
|
||||
|
||||
PROJECT_NAME := bootloader
|
||||
COMPONENTS := esptool_py bootloader
|
||||
COMPONENTS := esptool_py bootloader log
|
||||
|
||||
# The bootloader pseudo-component is also included in this build, for its Kconfig.projbuild to be included.
|
||||
#
|
||||
@@ -12,6 +12,6 @@ COMPONENTS := esptool_py bootloader
|
||||
IS_BOOTLOADER_BUILD := 1
|
||||
|
||||
#We cannot include the esp32 component directly but we need its includes. This is fixed by
|
||||
#adding it in the main/Makefile directory.
|
||||
EXTRA_CFLAGS := -D BOOTLOADER_BUILD=1 -I $(IDF_PATH)/components/esp32/include
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
|
||||
@@ -1,114 +0,0 @@
|
||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#ifndef __BOOT_LOG_H__
|
||||
#define __BOOT_LOG_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#define BOOT_LOG_LEVEL_NONE (0)
|
||||
#define BOOT_LOG_LEVEL_ERROR (1)
|
||||
#define BOOT_LOG_LEVEL_WARN (2)
|
||||
#define BOOT_LOG_LEVEL_INFO (3)
|
||||
#define BOOT_LOG_LEVEL_NOTICE (4)
|
||||
#define BOOT_LOG_LEVEL_DEBUG (5)
|
||||
|
||||
#define Black "30"
|
||||
#define Red "31"
|
||||
#define Green "32"
|
||||
#define Brown "33"
|
||||
#define Blue "34"
|
||||
#define Purple "35"
|
||||
#define Cyan "36"
|
||||
|
||||
#if CONFIG_BOOTLOADER_LOG_COLORS
|
||||
#define LOG_COLOR(COLOR) "\033[0;"COLOR"m"
|
||||
#define LOG_BOLD(COLOR) "\033[1;"COLOR"m"
|
||||
#define LOG_RESET_COLOR "\033[0m"
|
||||
#else
|
||||
#define LOG_COLOR(...)
|
||||
#define LOG_BOLD(...)
|
||||
#define LOG_RESET_COLOR ""
|
||||
#endif
|
||||
|
||||
// BOOT_LOG_LEVEL defined by make menuconfig
|
||||
#if CONFIG_BOOTLOADER_LOG_LEVEL_NONE
|
||||
#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_NONE
|
||||
#elif CONFIG_BOOTLOADER_LOG_LEVEL_ERROR
|
||||
#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_ERROR
|
||||
#elif CONFIG_BOOTLOADER_LOG_LEVEL_WARN
|
||||
#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_WARN
|
||||
#elif CONFIG_BOOTLOADER_LOG_LEVEL_INFO
|
||||
#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
|
||||
#elif CONFIG_BOOTLOADER_LOG_LEVEL_NOTICE
|
||||
#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_NOTICE
|
||||
#elif CONFIG_BOOTLOADER_LOG_LEVEL_DEBUG
|
||||
#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_DEBUG
|
||||
#else
|
||||
#error "No bootloader log level set in menuconfig!"
|
||||
#endif
|
||||
|
||||
//printf("\033[0;36m[NOTICE][%s][%s][%d]\n" format "\r\n", __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__);
|
||||
#define log_notice(format, ...) \
|
||||
do{\
|
||||
if(BOOT_LOG_LEVEL >= BOOT_LOG_LEVEL_NOTICE){\
|
||||
ets_printf(LOG_COLOR(Cyan) format "\r\n", ##__VA_ARGS__); \
|
||||
ets_printf(LOG_RESET_COLOR); \
|
||||
}\
|
||||
}while(0)
|
||||
|
||||
#define log_info(format, ...) \
|
||||
do{\
|
||||
if(BOOT_LOG_LEVEL >= BOOT_LOG_LEVEL_INFO){\
|
||||
ets_printf(LOG_BOLD(Cyan) format "\r\n", ##__VA_ARGS__); \
|
||||
ets_printf(LOG_RESET_COLOR); \
|
||||
}\
|
||||
}while(0)
|
||||
|
||||
//printf("\033[0;31m[ERROR][%s][%s][%d]\n" format "\r\n", __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__);
|
||||
#define log_error(format, ...) \
|
||||
do{\
|
||||
if(BOOT_LOG_LEVEL >= BOOT_LOG_LEVEL_ERROR){\
|
||||
ets_printf(LOG_COLOR(Red) "[ERROR][%s][%s][%d]\n" format "\r\n", __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
|
||||
ets_printf(LOG_RESET_COLOR); \
|
||||
}\
|
||||
}while(0)
|
||||
|
||||
//printf("\033[1;33m[WARN][%s][%s][%d]\n" format "\r\n", __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__);
|
||||
#define log_warn(format, ...) \
|
||||
do{\
|
||||
if(BOOT_LOG_LEVEL >= BOOT_LOG_LEVEL_WARN){\
|
||||
ets_printf(LOG_BOLD(Brown) "[WARN][%s][%s][%d]\n" format "\r\n", __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
|
||||
ets_printf(LOG_RESET_COLOR); \
|
||||
}\
|
||||
}while(0)
|
||||
|
||||
//printf("\033[1;32m[DEBUG][%s][%s][%d]\n" format "\r\n", __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__);
|
||||
#define log_debug(format, ...) \
|
||||
do{\
|
||||
if(BOOT_LOG_LEVEL >= BOOT_LOG_LEVEL_DEBUG){\
|
||||
ets_printf(LOG_BOLD(Green) "[DEBUG][%s][%s][%d]\n" format "\r\n", __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
|
||||
ets_printf(LOG_RESET_COLOR); \
|
||||
}\
|
||||
}while(0)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __BOOT_LOGGING_H__ */
|
||||
@@ -16,6 +16,7 @@
|
||||
#include <limits.h>
|
||||
|
||||
#include "esp_attr.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#include "rom/cache.h"
|
||||
#include "rom/ets_sys.h"
|
||||
@@ -31,11 +32,12 @@
|
||||
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#include "bootloader_log.h"
|
||||
#include "bootloader_config.h"
|
||||
|
||||
extern int _bss_start;
|
||||
extern int _bss_end;
|
||||
|
||||
static const char* TAG = "boot";
|
||||
/*
|
||||
We arrive here after the bootloader finished loading the program from flash. The hardware is mostly uninitialized,
|
||||
flash cache is down and the app CPU is in reset. We do have a stack, so we can do the initialization in C.
|
||||
@@ -130,7 +132,7 @@ uint32_t get_bin_len(uint32_t pos)
|
||||
{
|
||||
uint32_t len = 8 + 16;
|
||||
uint8_t i;
|
||||
log_debug("pos %d %x\n",pos,*(uint8_t *)pos);
|
||||
ESP_LOGD(TAG, "pos %d %x",pos,*(uint8_t *)pos);
|
||||
if(0xE9 != *(uint8_t *)pos) {
|
||||
return 0;
|
||||
}
|
||||
@@ -142,7 +144,7 @@ uint32_t get_bin_len(uint32_t pos)
|
||||
} else {
|
||||
len += 16;
|
||||
}
|
||||
log_debug("bin length = %d\n", len);
|
||||
ESP_LOGD(TAG, "bin length = %d", len);
|
||||
return len;
|
||||
}
|
||||
|
||||
@@ -161,7 +163,7 @@ void boot_cache_redirect( uint32_t pos, size_t size )
|
||||
uint32_t count = (size + 0xffff) / 0x10000;
|
||||
Cache_Read_Disable( 0 );
|
||||
Cache_Flush( 0 );
|
||||
log_debug( "mmu set paddr=%08x count=%d", pos_aligned, count );
|
||||
ESP_LOGD(TAG, "mmu set paddr=%08x count=%d", pos_aligned, count );
|
||||
cache_flash_mmu_set( 0, 0, 0x3f400000, pos_aligned, 64, count );
|
||||
Cache_Read_Enable( 0 );
|
||||
}
|
||||
@@ -183,13 +185,13 @@ bool load_partition_table(bootloader_state_t* bs, uint32_t addr)
|
||||
int index = 0;
|
||||
char *partition_usage;
|
||||
|
||||
log_info("Partition Table:");
|
||||
log_info("## Label Usage Type ST Offset Length");
|
||||
ESP_LOGI(TAG, "Partition Table:");
|
||||
ESP_LOGI(TAG, "## Label Usage Type ST Offset Length");
|
||||
|
||||
while (addr < end) {
|
||||
log_debug("load partition table entry from %x(%08x)", addr, MEM_CACHE(addr));
|
||||
ESP_LOGD(TAG, "load partition table entry from %x(%08x)", addr, MEM_CACHE(addr));
|
||||
memcpy(&partition, MEM_CACHE(addr), sizeof(partition));
|
||||
log_debug("type=%x subtype=%x", partition.type, partition.subtype);
|
||||
ESP_LOGD(TAG, "type=%x subtype=%x", partition.type, partition.subtype);
|
||||
partition_usage = "unknown";
|
||||
|
||||
if (partition.magic == PARTITION_MAGIC) { /* valid partition definition */
|
||||
@@ -244,14 +246,14 @@ bool load_partition_table(bootloader_state_t* bs, uint32_t addr)
|
||||
}
|
||||
|
||||
/* print partition type info */
|
||||
log_info("%2d %-16s %-16s %02x %02x %08x %08x", index, partition.label, partition_usage,
|
||||
ESP_LOGI(TAG, "%2d %-16s %-16s %02x %02x %08x %08x", index, partition.label, partition_usage,
|
||||
partition.type, partition.subtype,
|
||||
partition.pos.offset, partition.pos.size);
|
||||
index++;
|
||||
addr += sizeof(partition);
|
||||
}
|
||||
|
||||
log_info("End of partition table");
|
||||
ESP_LOGI(TAG,"End of partition table");
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -274,14 +276,7 @@ static bool ota_select_valid(const ota_select *s)
|
||||
|
||||
void bootloader_main()
|
||||
{
|
||||
//Run start routine.
|
||||
/*ESP32 2ND bootload start here*/
|
||||
log_info( "\n" );
|
||||
log_info( "**************************************" );
|
||||
log_info( "* hello espressif ESP32! *" );
|
||||
log_info( "* 2nd boot is running! *" );
|
||||
log_info( "* version (%s) *", BOOT_VERSION);
|
||||
log_info( "**************************************");
|
||||
ESP_LOGI(TAG, "Espressif ESP32 2nd stage bootloader v. %s", BOOT_VERSION);
|
||||
|
||||
struct flash_hdr fhdr;
|
||||
bootloader_state_t bs;
|
||||
@@ -289,7 +284,7 @@ void bootloader_main()
|
||||
ota_select sa,sb;
|
||||
memset(&bs, 0, sizeof(bs));
|
||||
|
||||
log_notice( "compile time %s\n", __TIME__ );
|
||||
ESP_LOGI(TAG, "compile time " __TIME__ );
|
||||
/* close watch dog here */
|
||||
REG_CLR_BIT( RTC_WDTCONFIG0, RTC_CNTL_WDT_FLASHBOOT_MOD_EN );
|
||||
REG_CLR_BIT( WDTCONFIG0(0), TIMERS_WDT_FLASHBOOT_MOD_EN );
|
||||
@@ -302,14 +297,14 @@ void bootloader_main()
|
||||
print_flash_info(&fhdr);
|
||||
|
||||
if (!load_partition_table(&bs, PARTITION_ADD)) {
|
||||
log_error("load partition table error!");
|
||||
ESP_LOGE(TAG, "load partition table error!");
|
||||
return;
|
||||
}
|
||||
|
||||
partition_pos_t load_part_pos;
|
||||
|
||||
if (bs.ota_info.offset != 0) { // check if partition table has OTA info partition
|
||||
//log_error("OTA info sector handling is not implemented");
|
||||
//ESP_LOGE("OTA info sector handling is not implemented");
|
||||
boot_cache_redirect(bs.ota_info.offset, bs.ota_info.size );
|
||||
memcpy(&sa,MEM_CACHE(bs.ota_info.offset & 0x0000ffff),sizeof(sa));
|
||||
memcpy(&sb,MEM_CACHE((bs.ota_info.offset + 0x1000)&0x0000ffff) ,sizeof(sb));
|
||||
@@ -325,13 +320,13 @@ void bootloader_main()
|
||||
spiRet1 = SPIEraseSector(bs.ota_info.offset/0x1000);
|
||||
spiRet2 = SPIEraseSector(bs.ota_info.offset/0x1000+1);
|
||||
if (spiRet1 != SPI_FLASH_RESULT_OK || spiRet2 != SPI_FLASH_RESULT_OK ) {
|
||||
log_error(SPI_ERROR_LOG);
|
||||
ESP_LOGE(TAG, SPI_ERROR_LOG);
|
||||
return;
|
||||
}
|
||||
spiRet1 = SPIWrite(bs.ota_info.offset,(uint32_t *)&sa,sizeof(ota_select));
|
||||
spiRet2 = SPIWrite(bs.ota_info.offset + 0x1000,(uint32_t *)&sb,sizeof(ota_select));
|
||||
if (spiRet1 != SPI_FLASH_RESULT_OK || spiRet2 != SPI_FLASH_RESULT_OK ) {
|
||||
log_error(SPI_ERROR_LOG);
|
||||
ESP_LOGE(TAG, SPI_ERROR_LOG);
|
||||
return;
|
||||
}
|
||||
Cache_Read_Enable(0);
|
||||
@@ -344,7 +339,7 @@ void bootloader_main()
|
||||
}else if(ota_select_valid(&sb)) {
|
||||
load_part_pos = bs.ota[(sb.ota_seq - 1) % bs.app_count];
|
||||
}else {
|
||||
log_error("ota data partition info error");
|
||||
ESP_LOGE(TAG, "ota data partition info error");
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -353,15 +348,15 @@ void bootloader_main()
|
||||
} else if (bs.test.offset != 0) { // otherwise, look for test app parition
|
||||
load_part_pos = bs.test;
|
||||
} else { // nothing to load, bail out
|
||||
log_error("nothing to load");
|
||||
ESP_LOGE(TAG, "nothing to load");
|
||||
return;
|
||||
}
|
||||
|
||||
log_info("Loading app partition at offset %08x", load_part_pos);
|
||||
ESP_LOGI(TAG, "Loading app partition at offset %08x", load_part_pos);
|
||||
if(fhdr.secury_boot_flag == 0x01) {
|
||||
/* protect the 2nd_boot */
|
||||
if(false == secure_boot()){
|
||||
log_error("secure boot failed");
|
||||
ESP_LOGE(TAG, "secure boot failed");
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -369,7 +364,7 @@ void bootloader_main()
|
||||
if(fhdr.encrypt_flag == 0x01) {
|
||||
/* encrypt flash */
|
||||
if (false == flash_encrypt(&bs)) {
|
||||
log_error("flash encrypt failed");
|
||||
ESP_LOGE(TAG, "flash encrypt failed");
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -395,7 +390,7 @@ void unpack_load_app(const partition_pos_t* partition)
|
||||
uint32_t irom_load_addr = 0;
|
||||
uint32_t irom_size = 0;
|
||||
|
||||
log_debug("bin_header: %u %u %u %u %08x\n", image_header.magic,
|
||||
ESP_LOGD(TAG, "bin_header: %u %u %u %u %08x", image_header.magic,
|
||||
image_header.blocks,
|
||||
image_header.spi_mode,
|
||||
image_header.spi_size,
|
||||
@@ -420,7 +415,7 @@ void unpack_load_app(const partition_pos_t* partition)
|
||||
}
|
||||
|
||||
if (address >= DROM_LOW && address < DROM_HIGH) {
|
||||
log_debug("found drom section, map from %08x to %08x\n", pos,
|
||||
ESP_LOGD(TAG, "found drom section, map from %08x to %08x", pos,
|
||||
section_header.load_addr);
|
||||
drom_addr = partition->offset + pos - sizeof(section_header);
|
||||
drom_load_addr = section_header.load_addr;
|
||||
@@ -430,7 +425,7 @@ void unpack_load_app(const partition_pos_t* partition)
|
||||
}
|
||||
|
||||
if (address >= IROM_LOW && address < IROM_HIGH) {
|
||||
log_debug("found irom section, map from %08x to %08x\n", pos,
|
||||
ESP_LOGD(TAG, "found irom section, map from %08x to %08x", pos,
|
||||
section_header.load_addr);
|
||||
irom_addr = partition->offset + pos - sizeof(section_header);
|
||||
irom_load_addr = section_header.load_addr;
|
||||
@@ -439,7 +434,7 @@ void unpack_load_app(const partition_pos_t* partition)
|
||||
map = true;
|
||||
}
|
||||
|
||||
log_notice("section %d: paddr=0x%08x vaddr=0x%08x size=0x%05x (%6d) %s", section_index, pos, section_header.load_addr, section_header.data_len, section_header.data_len, (load)?"load":(map)?"map":"");
|
||||
ESP_LOGI(TAG, "section %d: paddr=0x%08x vaddr=0x%08x size=0x%05x (%6d) %s", section_index, pos, section_header.load_addr, section_header.data_len, section_header.data_len, (load)?"load":(map)?"map":"");
|
||||
|
||||
if (!load) {
|
||||
pos += section_header.data_len;
|
||||
@@ -468,29 +463,29 @@ void IRAM_ATTR set_cache_and_start_app(
|
||||
uint32_t irom_size,
|
||||
uint32_t entry_addr)
|
||||
{
|
||||
log_debug("configure drom and irom and start\n");
|
||||
ESP_LOGD(TAG, "configure drom and irom and start");
|
||||
Cache_Read_Disable( 0 );
|
||||
Cache_Read_Disable( 1 );
|
||||
Cache_Flush( 0 );
|
||||
Cache_Flush( 1 );
|
||||
uint32_t drom_page_count = (drom_size + 64*1024 - 1) / (64*1024); // round up to 64k
|
||||
log_debug( "d mmu set paddr=%08x vaddr=%08x size=%d n=%d \n", drom_addr & 0xffff0000, drom_load_addr & 0xffff0000, drom_size, drom_page_count );
|
||||
ESP_LOGV(TAG, "d mmu set paddr=%08x vaddr=%08x size=%d n=%d", drom_addr & 0xffff0000, drom_load_addr & 0xffff0000, drom_size, drom_page_count );
|
||||
int rc = cache_flash_mmu_set( 0, 0, drom_load_addr & 0xffff0000, drom_addr & 0xffff0000, 64, drom_page_count );
|
||||
log_debug( "rc=%d", rc );
|
||||
ESP_LOGV(TAG, "rc=%d", rc );
|
||||
rc = cache_flash_mmu_set( 1, 0, drom_load_addr & 0xffff0000, drom_addr & 0xffff0000, 64, drom_page_count );
|
||||
log_debug( "rc=%d", rc );
|
||||
ESP_LOGV(TAG, "rc=%d", rc );
|
||||
uint32_t irom_page_count = (irom_size + 64*1024 - 1) / (64*1024); // round up to 64k
|
||||
log_debug( "i mmu set paddr=%08x vaddr=%08x size=%d n=%d\n", irom_addr & 0xffff0000, irom_load_addr & 0xffff0000, irom_size, irom_page_count );
|
||||
ESP_LOGV(TAG, "i mmu set paddr=%08x vaddr=%08x size=%d n=%d", irom_addr & 0xffff0000, irom_load_addr & 0xffff0000, irom_size, irom_page_count );
|
||||
rc = cache_flash_mmu_set( 0, 0, irom_load_addr & 0xffff0000, irom_addr & 0xffff0000, 64, irom_page_count );
|
||||
log_debug( "rc=%d", rc );
|
||||
ESP_LOGV(TAG, "rc=%d", rc );
|
||||
rc = cache_flash_mmu_set( 1, 0, irom_load_addr & 0xffff0000, irom_addr & 0xffff0000, 64, irom_page_count );
|
||||
log_debug( "rc=%d", rc );
|
||||
ESP_LOGV(TAG, "rc=%d", rc );
|
||||
REG_CLR_BIT( PRO_CACHE_CTRL1_REG, (DPORT_PRO_CACHE_MASK_IRAM0) | (DPORT_PRO_CACHE_MASK_IRAM1 & 0) | (DPORT_PRO_CACHE_MASK_IROM0 & 0) | DPORT_PRO_CACHE_MASK_DROM0 | DPORT_PRO_CACHE_MASK_DRAM1 );
|
||||
REG_CLR_BIT( APP_CACHE_CTRL1_REG, (DPORT_APP_CACHE_MASK_IRAM0) | (DPORT_APP_CACHE_MASK_IRAM1 & 0) | (DPORT_APP_CACHE_MASK_IROM0 & 0) | DPORT_APP_CACHE_MASK_DROM0 | DPORT_APP_CACHE_MASK_DRAM1 );
|
||||
Cache_Read_Enable( 0 );
|
||||
Cache_Read_Enable( 1 );
|
||||
|
||||
log_notice("start: 0x%08x\n", entry_addr);
|
||||
ESP_LOGD(TAG, "start: 0x%08x", entry_addr);
|
||||
typedef void (*entry_t)(void);
|
||||
entry_t entry = ((entry_t) entry_addr);
|
||||
|
||||
@@ -506,11 +501,11 @@ void print_flash_info(struct flash_hdr* pfhdr)
|
||||
|
||||
struct flash_hdr fhdr = *pfhdr;
|
||||
|
||||
log_debug( "[D]: magic %02x\n", fhdr.magic );
|
||||
log_debug( "[D]: blocks %02x\n", fhdr.blocks );
|
||||
log_debug( "[D]: spi_mode %02x\n", fhdr.spi_mode );
|
||||
log_debug( "[D]: spi_speed %02x\n", fhdr.spi_speed );
|
||||
log_debug( "[D]: spi_size %02x\n", fhdr.spi_size );
|
||||
ESP_LOGD(TAG, "magic %02x", fhdr.magic );
|
||||
ESP_LOGD(TAG, "blocks %02x", fhdr.blocks );
|
||||
ESP_LOGD(TAG, "spi_mode %02x", fhdr.spi_mode );
|
||||
ESP_LOGD(TAG, "spi_speed %02x", fhdr.spi_speed );
|
||||
ESP_LOGD(TAG, "spi_size %02x", fhdr.spi_size );
|
||||
|
||||
const char* str;
|
||||
switch ( fhdr.spi_speed ) {
|
||||
@@ -534,7 +529,7 @@ void print_flash_info(struct flash_hdr* pfhdr)
|
||||
str = "20MHz";
|
||||
break;
|
||||
}
|
||||
log_notice( " SPI Speed : %s", str );
|
||||
ESP_LOGI(TAG, "SPI Speed : %s", str );
|
||||
|
||||
|
||||
|
||||
@@ -566,7 +561,7 @@ void print_flash_info(struct flash_hdr* pfhdr)
|
||||
str = "DIO";
|
||||
break;
|
||||
}
|
||||
log_notice( " SPI Mode : %s", str );
|
||||
ESP_LOGI(TAG, "SPI Mode : %s", str );
|
||||
|
||||
|
||||
|
||||
@@ -595,6 +590,6 @@ void print_flash_info(struct flash_hdr* pfhdr)
|
||||
str = "1MB";
|
||||
break;
|
||||
}
|
||||
log_notice( " SPI Flash Size : %s", str );
|
||||
ESP_LOGI(TAG, "SPI Flash Size : %s", str );
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -8,6 +8,5 @@
|
||||
#
|
||||
|
||||
COMPONENT_ADD_LDFLAGS := -L $(abspath .) -lmain -T esp32.bootloader.ld -T $(IDF_PATH)/components/esp32/ld/esp32.rom.ld
|
||||
COMPONENT_EXTRA_INCLUDES := $(IDF_PATH)/components/esp32/include
|
||||
|
||||
include $(IDF_PATH)/make/component_common.mk
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
#include "esp_types.h"
|
||||
#include "esp_attr.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#include "rom/cache.h"
|
||||
#include "rom/ets_sys.h"
|
||||
@@ -28,13 +29,14 @@
|
||||
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#include "bootloader_log.h"
|
||||
#include "bootloader_config.h"
|
||||
|
||||
static const char* TAG = "flash_encrypt";
|
||||
|
||||
/**
|
||||
* @function : bitcount
|
||||
* @description: caculate bit 1 in flash_crypt_cnt
|
||||
* if it's even number ,need encrypt flash data,and burn efuse
|
||||
* @description: calculate bit 1 in flash_crypt_cnt
|
||||
* if it's even number, need encrypt flash data, and burn efuse
|
||||
*
|
||||
* @inputs: n flash_crypt_cnt
|
||||
* @return: number of 1 in flash_crypt_cnt
|
||||
@@ -68,19 +70,19 @@ bool flash_encrypt_write(uint32_t pos, uint32_t len)
|
||||
spiRet = SPIRead(pos, buf, SPI_SEC_SIZE);
|
||||
if (spiRet != SPI_FLASH_RESULT_OK) {
|
||||
Cache_Read_Enable(0);
|
||||
log_error(SPI_ERROR_LOG);
|
||||
ESP_LOGE(TAG, SPI_ERROR_LOG);
|
||||
return false;
|
||||
}
|
||||
spiRet = SPIEraseSector(pos/SPI_SEC_SIZE);
|
||||
if (spiRet != SPI_FLASH_RESULT_OK) {
|
||||
Cache_Read_Enable(0);
|
||||
log_error(SPI_ERROR_LOG);
|
||||
ESP_LOGE(TAG, SPI_ERROR_LOG);
|
||||
return false;
|
||||
}
|
||||
spiRet = SPI_Encrypt_Write(pos, buf, SPI_SEC_SIZE);
|
||||
if (spiRet != SPI_FLASH_RESULT_OK) {
|
||||
Cache_Read_Enable(0);
|
||||
log_error(SPI_ERROR_LOG);
|
||||
ESP_LOGE(TAG, SPI_ERROR_LOG);
|
||||
return false;
|
||||
}
|
||||
pos += SPI_SEC_SIZE;
|
||||
@@ -104,53 +106,53 @@ bool flash_encrypt(bootloader_state_t *bs)
|
||||
uint32_t flash_crypt_cnt = REG_GET_FIELD(EFUSE_BLK0_RDATA0, EFUSE_FLASH_CRYPT_CNT);
|
||||
uint8_t count = bitcount(flash_crypt_cnt);
|
||||
int i = 0;
|
||||
log_debug("flash crypt cnt %x, count %d\n", flash_crypt_cnt, count);
|
||||
ESP_LOGD(TAG, "flash encrypt cnt %x, bitcount %d\n", flash_crypt_cnt, count);
|
||||
|
||||
if ((count % 2) == 0) {
|
||||
boot_cache_redirect( 0, 64*1024);
|
||||
/* encrypt iv and abstruct */
|
||||
if (false == flash_encrypt_write(0,SPI_SEC_SIZE)) {
|
||||
log_error("encrypt iv and abstruct error");
|
||||
if (false == flash_encrypt_write(0, SPI_SEC_SIZE)) {
|
||||
ESP_LOGE(TAG, "encrypt iv and abstract error");
|
||||
return false;
|
||||
}
|
||||
|
||||
/* encrypt write boot bin*/
|
||||
bin_len = get_bin_len((uint32_t)MEM_CACHE(0x1000));
|
||||
if(bin_len != 0) {
|
||||
if (false == flash_encrypt_write(0x1000,bin_len)) {
|
||||
log_error("encrypt 2nd boot error");
|
||||
if (false == flash_encrypt_write(0x1000, bin_len)) {
|
||||
ESP_LOGE(TAG, "encrypt 2nd boot error");
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
log_error("2nd boot len error");
|
||||
ESP_LOGE(TAG, "2nd boot len error");
|
||||
return false;
|
||||
}
|
||||
/* encrypt partition table */
|
||||
if (false == flash_encrypt_write(PARTITION_ADD,SPI_SEC_SIZE)) {
|
||||
log_error("encrypt partition table error");
|
||||
if (false == flash_encrypt_write(PARTITION_ADD, SPI_SEC_SIZE)) {
|
||||
ESP_LOGE(TAG, "encrypt partition table error");
|
||||
return false;
|
||||
}
|
||||
|
||||
/* encrypt write factory bin */
|
||||
if(bs->factory.offset != 0x00) {
|
||||
log_debug("have factory bin\n");
|
||||
boot_cache_redirect(bs->factory.offset,bs->factory.size);
|
||||
ESP_LOGD(TAG, "have factory bin\n");
|
||||
boot_cache_redirect(bs->factory.offset, bs->factory.size);
|
||||
bin_len = get_bin_len((uint32_t)MEM_CACHE(bs->factory.offset&0xffff));
|
||||
if(bin_len != 0) {
|
||||
if (false == flash_encrypt_write(bs->factory.offset,bin_len)) {
|
||||
log_error("encrypt factory bin error");
|
||||
if (false == flash_encrypt_write(bs->factory.offset, bin_len)) {
|
||||
ESP_LOGE(TAG, "encrypt factory bin error");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* encrypt write test bin */
|
||||
if(bs->test.offset != 0x00) {
|
||||
ets_printf("have test bin\n");
|
||||
boot_cache_redirect(bs->test.offset,bs->test.size);
|
||||
ESP_LOGD(TAG, "have test bin\n");
|
||||
boot_cache_redirect(bs->test.offset, bs->test.size);
|
||||
bin_len = get_bin_len((uint32_t)MEM_CACHE(bs->test.offset&0xffff));
|
||||
if(bin_len != 0) {
|
||||
if (false == flash_encrypt_write(bs->test.offset,bin_len)) {
|
||||
log_error("encrypt test bin error");
|
||||
if (false == flash_encrypt_write(bs->test.offset, bin_len)) {
|
||||
ESP_LOGE(TAG, "encrypt test bin error");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -158,33 +160,33 @@ bool flash_encrypt(bootloader_state_t *bs)
|
||||
/* encrypt write ota bin */
|
||||
for (i = 0;i<16;i++) {
|
||||
if(bs->ota[i].offset != 0x00) {
|
||||
log_debug("have ota[%d] bin\n",i);
|
||||
boot_cache_redirect(bs->ota[i].offset,bs->ota[i].size);
|
||||
ESP_LOGD(TAG, "have ota[%d] bin\n",i);
|
||||
boot_cache_redirect(bs->ota[i].offset, bs->ota[i].size);
|
||||
bin_len = get_bin_len((uint32_t)MEM_CACHE(bs->ota[i].offset&0xffff));
|
||||
if(bin_len != 0) {
|
||||
if (false == flash_encrypt_write(bs->ota[i].offset,bin_len)) {
|
||||
log_error("encrypt ota bin error");
|
||||
if (false == flash_encrypt_write(bs->ota[i].offset, bin_len)) {
|
||||
ESP_LOGE(TAG, "encrypt ota bin error");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/* encrypt write ota info bin */
|
||||
if (false == flash_encrypt_write(bs->ota_info.offset,2*SPI_SEC_SIZE)) {
|
||||
log_error("encrypt ota binfo error");
|
||||
if (false == flash_encrypt_write(bs->ota_info.offset, 2*SPI_SEC_SIZE)) {
|
||||
ESP_LOGE(TAG, "encrypt ota info error");
|
||||
return false;
|
||||
}
|
||||
REG_SET_FIELD(EFUSE_BLK0_WDATA0, EFUSE_FLASH_CRYPT_CNT, 0x04);
|
||||
REG_WRITE(EFUSE_CONF, 0x5A5A); /* efuse_pgm_op_ena, force no rd/wr disable */
|
||||
REG_WRITE(EFUSE_CMD, 0x02); /* efuse_pgm_cmd */
|
||||
while (REG_READ(EFUSE_CMD)); /* wait for efuse_pagm_cmd=0 */
|
||||
log_warn("burn flash_crypt_cnt\n");
|
||||
ESP_LOGW(TAG, "burn flash_crypt_cnt");
|
||||
REG_WRITE(EFUSE_CONF, 0x5AA5); /* efuse_read_op_ena, release force */
|
||||
REG_WRITE(EFUSE_CMD, 0x01); /* efuse_read_cmd */
|
||||
while (REG_READ(EFUSE_CMD)); /* wait for efuse_read_cmd=0 */
|
||||
return true;
|
||||
} else {
|
||||
log_info("flash already encrypted.\n");
|
||||
ESP_LOGI(TAG, "flash already encrypted.");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
#include "esp_attr.h"
|
||||
#include "esp_types.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#include "rom/cache.h"
|
||||
#include "rom/ets_sys.h"
|
||||
@@ -29,12 +30,13 @@
|
||||
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#include "bootloader_log.h"
|
||||
#include "bootloader_config.h"
|
||||
|
||||
static const char* TAG = "secure_boot";
|
||||
|
||||
/**
|
||||
* @function : secure_boot_generate
|
||||
* @description: generate boot abstruct & iv
|
||||
* @description: generate boot abstract & iv
|
||||
*
|
||||
* @inputs: bool
|
||||
*/
|
||||
@@ -53,17 +55,17 @@ bool secure_boot_generate(uint32_t bin_len){
|
||||
spiRet = SPIEraseSector(0);
|
||||
if (spiRet != SPI_FLASH_RESULT_OK)
|
||||
{
|
||||
log_error(SPI_ERROR_LOG);
|
||||
ESP_LOGE(TAG, SPI_ERROR_LOG);
|
||||
return false;
|
||||
}
|
||||
/* write iv to flash, 0x0000, 128 bytes (1024 bits) */
|
||||
spiRet = SPIWrite(0, buf, 128);
|
||||
if (spiRet != SPI_FLASH_RESULT_OK)
|
||||
{
|
||||
log_error(SPI_ERROR_LOG);
|
||||
ESP_LOGE(TAG, SPI_ERROR_LOG);
|
||||
return false;
|
||||
}
|
||||
log_debug("write iv to flash.\n");
|
||||
ESP_LOGD(TAG, "write iv to flash.");
|
||||
Cache_Read_Enable(0);
|
||||
/* read 4K code image from flash, for test */
|
||||
for (i = 0; i < bin_len; i+=128) {
|
||||
@@ -77,10 +79,10 @@ bool secure_boot_generate(uint32_t bin_len){
|
||||
/* write abstract to flash, 0x0080, 64 bytes (512 bits) */
|
||||
spiRet = SPIWrite(0x80, buf, 64);
|
||||
if (spiRet != SPI_FLASH_RESULT_OK) {
|
||||
log_error(SPI_ERROR_LOG);
|
||||
ESP_LOGE(TAG, SPI_ERROR_LOG);
|
||||
return false;
|
||||
}
|
||||
log_debug("write abstract to flash.\n");
|
||||
ESP_LOGD(TAG, "write abstract to flash.");
|
||||
Cache_Read_Enable(0);
|
||||
return true;
|
||||
}
|
||||
@@ -88,7 +90,7 @@ bool secure_boot_generate(uint32_t bin_len){
|
||||
|
||||
/**
|
||||
* @function : secure_boot
|
||||
* @description: protect boot code inflash
|
||||
* @description: protect boot code in flash
|
||||
*
|
||||
* @inputs: bool
|
||||
*/
|
||||
@@ -96,17 +98,17 @@ bool secure_boot(void){
|
||||
uint32_t bin_len = 0;
|
||||
if (REG_READ(EFUSE_BLK0_RDATA6) & EFUSE_RD_ABS_DONE_0)
|
||||
{
|
||||
log_info("already secure boot !\n");
|
||||
ESP_LOGD(TAG, "already secure boot !");
|
||||
return true;
|
||||
} else {
|
||||
boot_cache_redirect( 0, 64*1024);
|
||||
bin_len = get_bin_len((uint32_t)MEM_CACHE(0x1000));
|
||||
if (bin_len == 0) {
|
||||
log_error("boot len is error");
|
||||
ESP_LOGE(TAG, "boot len is error");
|
||||
return false;
|
||||
}
|
||||
if (false == secure_boot_generate(bin_len)){
|
||||
log_error("secure boot generate failed");
|
||||
ESP_LOGE(TAG, "secure boot generate failed");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -115,11 +117,11 @@ bool secure_boot(void){
|
||||
REG_WRITE(EFUSE_CONF, 0x5A5A); /* efuse_pgm_op_ena, force no rd/wr disable */
|
||||
REG_WRITE(EFUSE_CMD, 0x02); /* efuse_pgm_cmd */
|
||||
while (REG_READ(EFUSE_CMD)); /* wait for efuse_pagm_cmd=0 */
|
||||
log_warn("burn abstract_done_0\n");
|
||||
ESP_LOGI(TAG, "burn abstract_done_0");
|
||||
REG_WRITE(EFUSE_CONF, 0x5AA5); /* efuse_read_op_ena, release force */
|
||||
REG_WRITE(EFUSE_CMD, 0x01); /* efuse_read_cmd */
|
||||
while (REG_READ(EFUSE_CMD)); /* wait for efuse_read_cmd=0 */
|
||||
log_debug("read EFUSE_BLK0_RDATA6 %x\n", REG_READ(EFUSE_BLK0_RDATA6));
|
||||
ESP_LOGD(TAG, "read EFUSE_BLK0_RDATA6 %x\n", REG_READ(EFUSE_BLK0_RDATA6));
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user