Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cpu/esp32: build the SDK bootloader from source #17457

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .murdock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh

# uncomment and change this to limit builds, e.g.,
#export BOARDS="samr21-xpro native"
export BOARDS="esp32-ethernet-kit-v1_0 esp32-mh-et-live-minikit esp32-wemos-lolin-d32-pro esp32-ethernet-kit-v1_1 esp32-olimex-evb esp32-wroom-32 esp32-ethernet-kit-v1_2 esp32-ttgo-t-beam esp32-wrover-kit esp32-heltec-lora32-v2"
# and / or
#export APPS="examples/hello-world tests/unittests"

Expand Down
6 changes: 6 additions & 0 deletions cpu/esp32/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ menu "ESP32 specific configurations"
bool "Enable JTAG debugging interface"
depends on HAS_ESP_JTAG

config MODULE_ESP_BOOTLOADER
bool
default y
help
Compile Bootloader from ESP-IDF

endmenu

rsource "periph/Kconfig"
Expand Down
4 changes: 4 additions & 0 deletions cpu/esp32/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ DIRS += $(RIOTCPU)/esp_common
DIRS += periph
DIRS += vendor

ifneq (, $(filter esp_bootloader, $(USEMODULE)))
DIRS += bootloader
endif

ifneq (, $(filter esp_eth, $(USEMODULE)))
DIRS += esp-eth
endif
Expand Down
1 change: 1 addition & 0 deletions cpu/esp32/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ include $(RIOTCPU)/esp_common/Makefile.dep
USEPKG += esp32_sdk
USEPKG += esp32_sdk_libs

USEMODULE += esp_bootloader
USEMODULE += esp_idf_driver
USEMODULE += esp_idf_esp32
USEMODULE += esp_idf_soc
Expand Down
10 changes: 9 additions & 1 deletion cpu/esp32/Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ include $(RIOTCPU)/esp_common/Makefile.include
TARGET_ARCH_ESP32 ?= xtensa-esp32-elf
TARGET_ARCH ?= $(TARGET_ARCH_ESP32)

PSEUDOMODULES += esp_bootloader
PSEUDOMODULES += esp_gdbstub
PSEUDOMODULES += esp_hw_counter
PSEUDOMODULES += esp_i2c_hw
Expand Down Expand Up @@ -58,11 +59,16 @@ ifneq (,$(filter cpp,$(USEMODULE)))
ARCHIVES += -lstdc++
endif

ifneq (,$(filter esp_bootloader,$(USEMODULE)))
# Bootloader file used by esptool.inc.mk
BOOTLOADER_BIN ?= $(BINDIR)/esp_bootloader/bootloader.bin
endif

ifneq (,$(filter esp_jtag,$(USEMODULE)))
PROGRAMMERS_SUPPORTED += openocd
PARTITION_POS = 0x8000
OPENOCD_PRE_FLASH_CMDS = -c 'echo "Installing Bootloader at $(BOOTLOADER_POS)"' \
-c 'flash write_image erase "$(RIOTCPU)/$(CPU)/bin/bootloader.bin" $(BOOTLOADER_POS) bin' \
-c 'flash write_image erase "$(BOOTLOADER_BIN)" $(BOOTLOADER_POS) bin' \
-c 'echo "Installing partition table at $(PARTITION_POS)"' \
-c 'flash write_image erase "$(BINDIR)/partitions.bin" $(PARTITION_POS) bin'
IMAGE_OFFSET = 0x10000
Expand All @@ -71,3 +77,5 @@ ifneq (,$(filter esp_jtag,$(USEMODULE)))
# Without resets debug target fails with 'Target not examined yet'
OPENOCD_DBG_EXTRA_CMD += -c 'reset halt'
endif

$(BOOTLOADER_BIN):
Binary file removed cpu/esp32/bin/bootloader.bin
Binary file not shown.
Binary file removed cpu/esp32/bin/bootloader_colors.bin
Binary file not shown.
Binary file removed cpu/esp32/bin/bootloader_colors_info.bin
Binary file not shown.
Binary file removed cpu/esp32/bin/bootloader_info.bin
Binary file not shown.
140 changes: 140 additions & 0 deletions cpu/esp32/bootloader/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
# Bootloader binary blob
MODULE := esp_bootloader

# We are compiling the bootloader from the ESP32 SDK sources, so we
# can't use the automatic module SRC discovery rules. Also, the compiled code
# would not be linked into the application, instead it is linked into its own
# binary file.
NO_AUTO_SRC = 1

include $(RIOTBASE)/Makefile.base

# List of bootloader sources.
include $(CURDIR)/bootloader.inc.mk

# Bootloader baudrate, set to the application defined one if any or the default
# in serial.inc.mk
BOOTLOADER_BAUD ?= $(BAUD)

# Bootloader sdkconfig.h defined in CURDIR directory.
INCLUDES = \
-I$(dir $(RIOTBUILD_CONFIG_HEADER_C)) \
-I$(CURDIR) \
-I$(ESP32_SDK_DIR)/components \
-I$(ESP32_SDK_DIR)/components/esp32/include \
-I$(ESP32_SDK_DIR)/components/soc/esp32/include \
-I$(ESP32_SDK_DIR)/components/soc/include \
-I$(ESP32_SDK_DIR)/components/bootloader_support/include \
-I$(ESP32_SDK_DIR)/components/bootloader_support/include_priv \
-I$(ESP32_SDK_DIR)/components/log/include \
-I$(ESP32_SDK_DIR)/components/spi_flash/include \
#

# BOOTLOADER_BUILD=1 signals to the SDK that's a bootloader build.
CFLAGS = \
-DBOOTLOADER_BUILD=1 \
-DESP_PLATFORM \
-DRIOT_BOOTLOADER_BAUD=$(BOOTLOADER_BAUD) \
-DRIOT_FLASH_SIZE=$(FLASH_SIZE) \
-DWITH_POSIX \
-DHAVE_CONFIG_H \
-std=gnu99 \
-Og \
-ggdb \
-ffunction-sections \
-fdata-sections \
-fstrict-volatile-bitfields \
-mlongcalls \
-nostdlib \
-Wall \
-Werror=all \
-Wno-error=unused-function \
-Wno-error=unused-but-set-variable \
-Wno-error=unused-variable \
-Wno-error=deprecated-declarations \
-Wextra \
-Wno-unused-parameter \
-Wno-sign-compare \
-Wno-old-style-declaration \
#

# Bootloader link flags. We use the SDK source and linking files instead of the
# RIOT-OS ones to link the bootloader. Note that we also use the unmodified
# SDK libraries.
LINKFLAGS = \
-nostdlib \
-u call_user_start_cpu0 \
-Wl,--gc-sections \
-Wl,-static \
-Wl,--start-group \
$(ESP_SDK_BOOTLOADER_OBJS) \
-lgcc \
-lstdc++ \
-lgcov \
-T$(ESP32_SDK_DIR)/components/bootloader/subproject/main/esp32.bootloader.ld \
-T$(ESP32_SDK_DIR)/components/bootloader/subproject/main/esp32.bootloader.rom.ld \
-T$(ESP32_SDK_DIR)/components/esp32/ld/esp32.rom.ld \
-T$(ESP32_SDK_DIR)/components/esp32/ld/esp32.rom.spiram_incompatible_fns.ld \
-T$(ESP32_SDK_DIR)/components/esp32/ld/esp32.peripherals.ld \
-Wl,--end-group \
-Wl,-EL \
#

# Build the bootloader on the application directory as it depends on the current
# app settings from riotbuild.h.
ESP_SDK_BOOTLOADER_DIR = $(BINDIR)/$(MODULE)
ESP_SDK_BOOTLOADER_BIN = $(ESP_SDK_BOOTLOADER_DIR)/bootloader.bin
ESP_SDK_BOOTLOADER_ELF = $(ESP_SDK_BOOTLOADER_DIR)/bootloader.elf

# Objects that need to be filtered out because they are not part of the SDK
# but are used as submodules and required by the Secure Boot which is not yet
# supported due to a conflict of the packages `tinycrypt` and `micro-ecc`
ESP_SDK_BOOTLOADER_OBJ_FILTER = \
components/bootloader_support/src/secure_boot.o \
components/bootloader_support/src/secure_boot_signatures.o \
components/micro-ecc/micro-ecc/uECC.o

ESP_SDK_BOOTLOADER_OBJS = \
$(addprefix $(ESP_SDK_BOOTLOADER_DIR)/, \
$(filter-out $(ESP_SDK_BOOTLOADER_OBJ_FILTER), \
$(ESP_SDK_BOOTLOADER_SRCS:%.c=%.o)))

DEPS := $(ESP_SDK_BOOTLOADER_OBJS:%.o=%.d)
-include $(DEPS)

# ESP32 SDK submodules required for compilation of bootloader
ESP32_SDK_SUBMODULES = $(ESP32_SDK_DIR)/components/micro-ecc/micro-ecc
ESP32_SDK_SUBMODULES_GIT = $(addprefix $(ESP32_SDK_SUBMODULES),/.git)

# Main module dependency. We only build the bootloader.bin from this module.
$(MODULE).module: $(ESP_SDK_BOOTLOADER_BIN)

OBJ_DEPS += $(CURDIR)/sdkconfig.h

$(ESP_SDK_BOOTLOADER_DIR)/%.o: $(ESP32_SDK_DIR)/%.c $(OBJ_DEPS)
$(Q)mkdir -p $(dir $@)
$(Q)$(CCACHE) $(CC) \
$(CFLAGS) $(INCLUDES) -MMD -MP -c $(abspath $<) -o $@

$(ESP_SDK_BOOTLOADER_DIR):
mkdir -p $@

$(ESP_SDK_BOOTLOADER_ELF): $(ESP_SDK_BOOTLOADER_OBJS) $(ESP_SDK_BOOTLOADER_SUPPORT_AR) \
| $(ESP_SDK_BOOTLOADER_DIR)
$(Q)$(CC) -o $@ $(LINKFLAGS) -Wl,-Map=$(@:%.elf=%.map)

FLASH_CHIP = esp32
ESPTOOL ?= $(RIOTTOOLS)/esptool/esptool.py
# TODO: These should be exported/configurable from the app side. That would
# require to export these values.
FLASH_MODE ?= dout
FLASH_FREQ ?= 40m
FLASH_SIZE ?= 4

# We use esptool to extract a version 1 app from the bootloader.elf. This is
# like the regular objdump binary file but it contains a 16 byte header which
# specifies the flash size, mode and speed that the ROM bootloader uses to load
# this second-stage bootloader image.
$(ESP_SDK_BOOTLOADER_BIN): $(ESP_SDK_BOOTLOADER_ELF)
$(Q)$(ESPTOOL) --chip $(FLASH_CHIP) elf2image --flash_mode $(FLASH_MODE) \
--flash_size $(FLASH_SIZE)MB --flash_freq $(FLASH_FREQ) -o $@ $<
32 changes: 32 additions & 0 deletions cpu/esp32/bootloader/bootloader.inc.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by ./update_mk.sh, don't modify directly.

ESP_SDK_BOOTLOADER_SRCS = \
components/bootloader/subproject/main/bootloader_start.c \
components/bootloader_support/src/bootloader_clock.c \
components/bootloader_support/src/bootloader_common.c \
components/bootloader_support/src/bootloader_flash.c \
components/bootloader_support/src/bootloader_init.c \
components/bootloader_support/src/bootloader_random.c \
components/bootloader_support/src/bootloader_sha.c \
components/bootloader_support/src/bootloader_utility.c \
components/bootloader_support/src/efuse.c \
components/bootloader_support/src/esp_image_format.c \
components/bootloader_support/src/flash_encrypt.c \
components/bootloader_support/src/flash_partitions.c \
components/bootloader_support/src/flash_qio_mode.c \
components/bootloader_support/src/secure_boot.c \
components/bootloader_support/src/secure_boot_signatures.c \
components/log/log.c \
components/micro-ecc/micro-ecc/uECC.c \
components/soc/esp32/cpu_util.c \
components/soc/esp32/gpio_periph.c \
components/soc/esp32/rtc_clk.c \
components/soc/esp32/rtc_init.c \
components/soc/esp32/rtc_periph.c \
components/soc/esp32/rtc_pm.c \
components/soc/esp32/rtc_sleep.c \
components/soc/esp32/rtc_time.c \
components/soc/esp32/soc_memory_layout.c \
components/soc/esp32/spi_periph.c \
components/spi_flash/spi_flash_rom_patch.c \
#
94 changes: 94 additions & 0 deletions cpu/esp32/bootloader/sdkconfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright (C) 2021 iosabi
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @ingroup pkg_esp32_sdk
* @{
*
* @file
* @brief RIOT-OS modification of the bootloader SDK configuration
*
* The bootloader build of the ESP32 SDK needs some settings from the SDK
* configuration. These are normally generated by the menuconfig in the vendor
* SDK.
*
* Some of these parameters are configurable by the application. For example,
* the UART baudrate used by the console and the verbose level of the
* bootloader.
*
* @author iosabi <iosabi@protonmail.com>
*/

#ifndef SDKCONFIG_H
#define SDKCONFIG_H

#ifndef DOXYGEN

#include "riotbuild.h"

#include "esp32_idf_version.h"
#include "sdkconfig_default.h"

#ifdef __cplusplus
extern "C" {
#endif

#if MODULE_ESP_LOG_COLORED
#define CONFIG_LOG_COLORS 1
#endif

#ifndef CONFIG_LOG_BOOTLOADER_LEVEL
/*
* SDK Log levels:
*
* 0 = NONE
* 1 = ERROR
* 2 = WARN
* 3 = INFO
* 4 = DEBUG
* 5 = VERBOSE
*/
#if MODULE_ESP_LOG_STARTUP
#define CONFIG_LOG_BOOTLOADER_LEVEL 3 /* INFO */
#else
#define CONFIG_LOG_BOOTLOADER_LEVEL 0 /* NONE */
#endif
#endif

#if FLASH_MODE_QIO
#define CONFIG_FLASHMODE_QIO 1
#elif FLASH_MODE_QOUT
#define CONFIG_FLASHMODE_QOUT 1
#elif FLASH_MODE_DIO
#define CONFIG_FLASHMODE_DIO 1
#elif FLASH_MODE_DOUT
#define CONFIG_FLASHMODE_DOUT 1
#else
#error "Unknown flash mode selected."
#endif

/*
* Bootloader output baudrate, defined by the app settings as BAUD or
* BOOTLOADER_BAUD.
*/
#define CONFIG_CONSOLE_UART_BAUDRATE (RIOT_BOOTLOADER_BAUD)

/*
* The Makefile FLASH_SIZE value is set in MB, but set as bytes to
* CONFIG_SPI_FLASH_SIZE.
*/
#define CONFIG_SPI_FLASH_SIZE ((RIOT_FLASH_SIZE) * 1024 * 1024)

#ifdef __cplusplus
}
#endif

#endif /* DOXYGEN */
/** @} */

#endif /* SDKCONFIG_H */
29 changes: 29 additions & 0 deletions cpu/esp32/bootloader/sdkconfig_default.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Generated by ./update_mk.sh, don't modify directly.
* Default CONFIG_ parameters from the SDK package.
*/

#ifndef SDKCONFIG_DEFAULT_H
#define SDKCONFIG_DEFAULT_H

#ifndef DOXYGEN

#ifdef __cplusplus
extern "C" {
#endif

#define CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V 1
#define CONFIG_CONSOLE_UART_NUM 0
#define CONFIG_ESP32_DEBUG_OCDAWARE 1
#define CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ 160
#define CONFIG_ESP32_XTAL_FREQ 40
#define CONFIG_LOG_DEFAULT_LEVEL 3
#define CONFIG_PARTITION_TABLE_OFFSET 0x8000
#define CONFIG_SPI_FLASH_ROM_DRIVER_PATCH 1

#ifdef __cplusplus
}
#endif

#endif /* DOXYGEN */
#endif /* SDKCONFIG_DEFAULT_H */
Loading