diff --git a/arch/risc-v/src/esp32c3-legacy/Bootloader.mk b/arch/risc-v/src/esp32c3-legacy/Bootloader.mk index ac636e610d..bedd5e4604 100644 --- a/arch/risc-v/src/esp32c3-legacy/Bootloader.mk +++ b/arch/risc-v/src/esp32c3-legacy/Bootloader.mk @@ -125,7 +125,7 @@ $(MCUBOOT_SRCDIR): $(BOOTLOADER_BIN): chip/$(ESP_HAL_3RDPARTY_REPO) $(MCUBOOT_SRCDIR) $(BOOTLOADER_CONFIG) $(Q) echo "Building Bootloader" - $(Q) $(TOOLSDIR)/build_mcuboot.sh \ + $(Q) $(TOOLSDIR)/build_mcuboot_esp32c3_legacy.sh \ -c esp32c3 \ -f $(BOOTLOADER_CONFIG) \ -p $(BOOTLOADER_DIR) \ diff --git a/tools/espressif/build_mcuboot_esp32c3_legacy.sh b/tools/espressif/build_mcuboot_esp32c3_legacy.sh new file mode 100755 index 0000000000..6362ea50d4 --- /dev/null +++ b/tools/espressif/build_mcuboot_esp32c3_legacy.sh @@ -0,0 +1,136 @@ +#!/usr/bin/env bash + +SCRIPT_NAME=$(basename "${BASH_SOURCE[0]}") + +usage() { + echo "" + echo "USAGE: ${SCRIPT_NAME} [-h] -c -f -p -e " + echo "" + echo "Where:" + echo " -c Target chip" + echo " -f Path to file containing configuration options" + echo " -p Path to execute the script" + echo " -e Path to HAL directory" + echo " -h Show usage and terminate" + echo "" +} + +build_mcuboot() { + local target=${1} + local config=${2} + local mcuboot_dir="${exec_path}/mcuboot" + local build_dir=".build-${target}" + local source_dir="boot/espressif" + local output_dir="${exec_path}/out" + local toolchain_file="tools/toolchain-${target}.cmake" + local mcuboot_config + local mcuboot_flashsize + local mcuboot_flashmode + local mcuboot_flashfreq + local make_generator + + mcuboot_config=$(realpath "${config:-${exec_path}/mcuboot.conf}") + + # Try parsing Flash parameters from the mcuboot config file. + # If not found, let's assume some commonplace values. + + mcuboot_flashsize=$(sed -n 's/^CONFIG_ESPTOOLPY_FLASHSIZE_\(.*\)MB=1/\1MB/p' "${mcuboot_config}") + if [ -z "${mcuboot_flashsize}" ]; then + mcuboot_flashsize="4MB" + fi + + mcuboot_flashmode=$(sed -n 's/^CONFIG_ESPTOOLPY_FLASHMODE_\(.*\)=1/\L\1/p' "${mcuboot_config}") + if [ -z "${mcuboot_flashmode}" ]; then + mcuboot_flashmode="dio" + fi + + mcuboot_flashfreq=$(sed -n 's/^CONFIG_ESPTOOLPY_FLASHFREQ_\(.*\)M=1/\1m/p' "${mcuboot_config}") + if [ -z "${mcuboot_flashfreq}" ]; then + mcuboot_flashfreq="40m" + fi + + pushd "${exec_path}" &>/dev/null + mkdir -p "${output_dir}" &>/dev/null + + # Build with Ninja if installed + + if command -v ninja &>/dev/null; then + make_generator="-GNinja" + fi + + # Build bootloader for selected target + + cd "${mcuboot_dir}" &>/dev/null + cmake -DCMAKE_TOOLCHAIN_FILE="${toolchain_file}" \ + -DMCUBOOT_TARGET="${target}" \ + -DMCUBOOT_CONFIG_FILE="${mcuboot_config}" \ + -DESP_HAL_PATH="${esp_hal}" \ + -DCONFIG_ESP_FLASH_SIZE="${mcuboot_flashsize}" \ + -DESP_FLASH_MODE="${mcuboot_flashmode}" \ + -DESP_FLASH_FREQ="${mcuboot_flashfreq}" \ + -B "${build_dir}" \ + "${make_generator}" \ + "${source_dir}" + cmake --build "${build_dir}"/ + + # Copy bootloader binary file to output directory + + cp "${build_dir}"/mcuboot_"${target}".bin "${output_dir}"/mcuboot-"${target}".bin &>/dev/null + + # Remove build directory + + rm -rf "${build_dir}" &>/dev/null + + popd &>/dev/null +} + +while getopts ":hc:f:p:e:" arg; do + case "${arg}" in + c) + chip=${OPTARG} + ;; + f) + config=${OPTARG} + ;; + p) + exec_path=${OPTARG} + ;; + e) + esp_hal=${OPTARG} + ;; + h) + usage + exit 0 + ;; + *) + usage + exit 1 + ;; + esac +done + +if [ -z "${chip}" ]; then + printf "ERROR: Missing target chip.\n" + usage + exit 1 +fi + +if ! [ -d "${esp_hal}" ]; then + printf "ERROR: Invalid HAL path.\n" + usage + exit 1 +fi + +if [ ! -d "${exec_path}" ]; then + printf "ERROR: Invalid exec path.\n" + usage + exit 1 +fi + +if [ -n "${config}" ] && [ ! -f "${config}" ]; then + printf "ERROR: Configuration file %s not found.\n" "${config}" + usage + exit 1 +fi + +build_mcuboot "${chip}" "${config}"