Skip to content

Commit

Permalink
Merge branch 'feature/speed_up_system_startup' into 'master'
Browse files Browse the repository at this point in the history
feat(startup): add fast boot and fast restart function

See merge request sdk/ESP8266_RTOS_SDK!1302
  • Loading branch information
donghengqaz committed Aug 5, 2020
2 parents 5a916cd + b967cae commit 1366f6d
Show file tree
Hide file tree
Showing 19 changed files with 427 additions and 42 deletions.
7 changes: 7 additions & 0 deletions components/bootloader/Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ config BOOTLOADER_DISABLE_JTAG_IO

If users use JTAG to help develop, please disable this option.

config BOOTLOADER_FAST_BOOT
bool "Bootloader fast boot"
default n
help
Enable this option, after initializing hardware, bootloader will try to load boot image
information from RTC memory directly and then run image without verifying it.

choice LOG_BOOTLOADER_LEVEL
bool "Bootloader log verbosity"
default LOG_BOOTLOADER_LEVEL_INFO
Expand Down
8 changes: 8 additions & 0 deletions components/bootloader/subproject/main/bootloader_start.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,19 @@ static int selected_boot_partition(const bootloader_state_t *bs);

void call_start_cpu(void)
{
#ifdef CONFIG_BOOTLOADER_FAST_BOOT
REG_SET_BIT(DPORT_CTL_REG, DPORT_CTL_DOUBLE_CLK);
#endif

// 1. Hardware initialization
if(bootloader_init() != ESP_OK){
return;
}

#ifdef CONFIG_BOOTLOADER_FAST_BOOT
bootloader_utility_fast_boot_image();
#endif

// 2. Select image to boot
esp_image_metadata_t image_data;
if(select_image(&image_data) != ESP_OK){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,8 @@ bool bootloader_utility_load_boot_image(const bootloader_state_t *bs, int start_
* @param[in] data Structure to hold on-flash image metadata.
*/
void bootloader_utility_load_image(const esp_image_metadata_t* image_data);

/**
* @brief Loading boot image information from RTC memory and then running image without verifying.
*/
void bootloader_utility_fast_boot_image(void);
28 changes: 22 additions & 6 deletions components/bootloader_support/src/bootloader_utility.c
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ static void set_cache_and_start_app(
#include "esp_log.h"

#include "esp_flash_partitions.h"
#include "esp_fast_boot.h"
#include "internal/esp_system_internal.h"

static const char* TAG = "boot";
Expand Down Expand Up @@ -743,6 +744,16 @@ static bool try_load_partition(const esp_partition_pos_t *partition, esp_image_m

#define TRY_LOG_FORMAT "Trying partition index %d offs 0x%x size 0x%x"

static void bootloader_utility_start_image(uint32_t image_start, uint32_t image_size, uint32_t entry_addr)
{
void (*user_start)(size_t start_addr);

bootloader_mmap(image_start, image_size);

user_start = (void *)entry_addr;
user_start(image_start);
}

bool bootloader_utility_load_boot_image(const bootloader_state_t *bs, int start_index, esp_image_metadata_t *result)
{
int index = start_index;
Expand Down Expand Up @@ -793,8 +804,6 @@ bool bootloader_utility_load_boot_image(const bootloader_state_t *bs, int start_

void bootloader_utility_load_image(const esp_image_metadata_t* image_data)
{
void (*user_start)(size_t start_addr);

#if defined(CONFIG_SECURE_BOOT_ENABLED) || defined(CONFIG_FLASH_ENCRYPTION_ENABLED)
esp_err_t err;
#endif
Expand Down Expand Up @@ -838,11 +847,18 @@ void bootloader_utility_load_image(const esp_image_metadata_t* image_data)
copy loaded segments to RAM, set up caches for mapped segments, and start application
unpack_load_app(image_data);
#else
bootloader_mmap(image_data->start_addr, image_data->image_len);

user_start = (void *)image_data->image.entry_addr;
user_start(image_data->start_addr);
bootloader_utility_start_image(image_data->start_addr, image_data->image_len, image_data->image.entry_addr);
#endif /* BOOTLOADER_UNPACK_APP */
}

void bootloader_utility_fast_boot_image(void)
{
uint32_t image_start, image_size, image_entry;

if (!esp_fast_boot_get_info(&image_start, &image_size, &image_entry)) {
bootloader_utility_start_image(image_start, image_size, image_entry);
}

ESP_LOGD(TAG, "fast boot image fail");
}
#endif
5 changes: 3 additions & 2 deletions components/esp8266/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ if(BOOTLOADER_BUILD)
# For bootloader, all we need from esp8266 is headers
set(COMPONENT_ADD_INCLUDEDIRS include)
# set(COMPONENT_REQUIRES ${COMPONENTS})
set(COMPONENT_SRCS source/ets_printf.c source/crc.c)
set(COMPONENT_SRCS source/ets_printf.c source/crc.c source/esp_fast_boot.c)
register_component(esp8266)

# as cmake won't attach linker args to a header-only library, attach
Expand Down Expand Up @@ -37,6 +37,7 @@ else()
"source/task_wdt.c"
"source/rom.c"
"source/hw_random.c"
"source/esp_fast_boot.c"
"driver/adc.c"
"driver/gpio.c"
"driver/hw_timer.c"
Expand All @@ -53,7 +54,7 @@ else()
set(include_dirs "include" "include/driver")

set(requires "esp_common" "esp_event")
set(priv_requires "wpa_supplicant" "log" "spi_flash" "tcpip_adapter" "esp_ringbuf" "bootloader_support" "nvs_flash")
set(priv_requires "wpa_supplicant" "log" "spi_flash" "tcpip_adapter" "esp_ringbuf" "bootloader_support" "nvs_flash" "app_update")
set(fragments linker.lf ld/esp8266_fragments.lf ld/esp8266_bss_fragments.lf)

idf_component_register(SRCS "${srcs}"
Expand Down
2 changes: 1 addition & 1 deletion components/esp8266/component.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Component Makefile
#
ifdef IS_BOOTLOADER_BUILD
COMPONENT_OBJS := source/ets_printf.o source/crc.o
COMPONENT_OBJS := source/ets_printf.o source/crc.o source/esp_fast_boot.o
COMPONENT_SRCDIRS := source
else
COMPONENT_ADD_INCLUDEDIRS += include
Expand Down
3 changes: 3 additions & 0 deletions components/esp8266/include/esp8266/eagle_soc.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@
#define CACHE_READ_EN_BIT BIT8
//}}

#define ESP_CACHE1_ADDR_MAX (0x100000)
#define ESP_CACHE2_ADDR_MAX (0x200000)

#define DRAM_BASE (0x3FFE8000)
#define DRAM_SIZE (96 * 1024)

Expand Down
7 changes: 7 additions & 0 deletions components/esp8266/include/esp8266/rom_functions.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
#ifndef _ROM_FUNCTIONS_H
#define _ROM_FUNCTIONS_H

#include "sdkconfig.h"
#include <stdint.h>
#include <stdarg.h>

#ifdef CONFIG_SOC_FULL_ICACHE
#define SOC_CACHE_SIZE 1 // 32KB
#else
#define SOC_CACHE_SIZE 0 // 16KB
#endif

#define ROM_FLASH_BUF_DECLARE(__name, __size) uint8_t __name[__size] __attribute__((aligned(4)))

typedef struct {
Expand Down
87 changes: 87 additions & 0 deletions components/esp8266/include/esp_fast_boot.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2020-2021 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.

#pragma once

#ifndef BOOTLOADER_BUILD
#include "esp_partition.h"
#endif

#ifdef __cplusplus
extern "C" {
#endif

#ifndef BOOTLOADER_BUILD
/**
* @brief Setting target partition as fast boot partition and enable fast boot function.
*
* @param partition partition which has image to be booted
*
* @return
* - 0 on success
* - -EINVAL parameter error
* - -EIO read flash error
*/
int esp_fast_boot_enable_partition(const esp_partition_t *partition);

/**
* @brief Setting current running partition as fast boot partition and enable fast boot function.
*
* @return
* - 0 on success
* - -EINVAL current running partition information error
* - -EIO read flash error
*/
int esp_fast_boot_enable(void);

/**
* @brief Directly running the image which is to be booted after restart.
*
* @note It is just like jumping directly from one APP to another one without running ROM bootloader and level 2 bootloader.
* Using this API, system starting up is fastest.
*
* @return
* - 0 on success
* - -EINVAL booted partition information error
* - -EIO read flash error
*/
int esp_fast_boot_restart(void);
#endif

/**
* @brief Disabling fast boot function and bootloader will not boot fast.
*/
void esp_fast_boot_disable(void);

/**
* @brief Getting fast boot information.
*
* @param image_start image startting address in the SPI Flash
* @param image_size image max size in the SPI Flash
* @param image_entry image entry address in the SPI Flash
*
* @return
* - 0 on success
* - -EINVAL fast boot information error
*/
int esp_fast_boot_get_info(uint32_t *image_start, uint32_t *image_size, uint32_t *image_entry);

/**
* @brief Printing fast boot information.
*/
void esp_fast_boot_print_info(void);

#ifdef __cplusplus
}
#endif
8 changes: 8 additions & 0 deletions components/esp8266/include/esp_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ typedef enum {
ESP_RST_DEEPSLEEP, //!< Reset after exiting deep sleep mode
ESP_RST_BROWNOUT, //!< Brownout reset (software or hardware)
ESP_RST_SDIO, //!< Reset over SDIO
ESP_RST_FAST_SW, //!< Fast reboot
} esp_reset_reason_t;

/**
Expand Down Expand Up @@ -187,6 +188,13 @@ uint32_t esp_random(void);
*/
void esp_fill_random(void *buf, size_t len);

/**
* @brief Initialize MAC address
*
* @return 0 if sucess or others failed
*/
esp_err_t esp_mac_init(void);

typedef enum {
FLASH_SIZE_4M_MAP_256_256 = 0, /**< Flash size : 4Mbits. Map : 256KBytes + 256KBytes */
FLASH_SIZE_2M, /**< Flash size : 2Mbits. Map : 256KBytes */
Expand Down
8 changes: 8 additions & 0 deletions components/esp8266/include/internal/esp_system_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ extern "C" {
#endif

#define RTC_SYS_RAM_SIZE 256
#define ESP_SYSTEM_FAST_BOOT_IMAGE 0x5aa5a55a

/**
* @brief Station's AP base information of old SDK
Expand Down Expand Up @@ -50,6 +51,13 @@ struct _rtc_sys_info {
uint32_t hint; // software reset reason
uint32_t old_sysconf_addr; /*<! old SDK system configuration parameters base address,
if your bootloader is older than v3.2, please don't use this */
struct {
uint32_t magic;
uint32_t image_start;
uint32_t image_size;
uint32_t image_entry;
uint32_t crc32;
} fast_boot;
};

/**
Expand Down
5 changes: 5 additions & 0 deletions components/esp8266/include/rom/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ static inline STATUS UartRxString(uint8_t *pString, uint8_t MaxStrlen)
return OK;
}

/**
* @brief Disable UART0 I/O swap and don't care about if TX FIFO is empty
*/
void uart_disable_swap_io(void);

/**
* @}
*/
Expand Down
Loading

0 comments on commit 1366f6d

Please sign in to comment.