From 09950797cbedad3032aa1711a20c3065721f4e44 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Wed, 22 Jan 2020 17:40:10 +0100 Subject: [PATCH 1/2] esptool_py: pass flash mode/frequency/size to elf2image Otherwise the image gets generated with wrong parameters, and the binary does not boot unless it has been "fixed" by esptool during the upload. --- components/esptool_py/CMakeLists.txt | 10 +--------- components/esptool_py/project_include.cmake | 15 +++++++++++---- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/components/esptool_py/CMakeLists.txt b/components/esptool_py/CMakeLists.txt index bf779ce24d..174de18acc 100644 --- a/components/esptool_py/CMakeLists.txt +++ b/components/esptool_py/CMakeLists.txt @@ -22,15 +22,7 @@ if(NOT BOOTLOADER_BUILD) # Generate flasher_args.json for tools that need it. The variables below are used # in configuring the template flasher_args.json.in. - set(ESPFLASHMODE ${CONFIG_ESPTOOLPY_FLASHMODE}) - set(ESPFLASHFREQ ${CONFIG_ESPTOOLPY_FLASHFREQ}) - set(ESPFLASHSIZE ${CONFIG_ESPTOOLPY_FLASHSIZE}) - - set(ESPTOOLPY_FLASH_OPTIONS - --flash_mode ${ESPFLASHMODE} - --flash_freq ${ESPFLASHFREQ} - --flash_size ${ESPFLASHSIZE} - ) + # Some of the variables (flash mode, size, frequency) are already set in project_include.cmake. set(ESPTOOLPY_BEFORE "${CONFIG_ESPTOOLPY_BEFORE}") set(ESPTOOLPY_AFTER "${CONFIG_ESPTOOLPY_AFTER}") diff --git a/components/esptool_py/project_include.cmake b/components/esptool_py/project_include.cmake index 4d06269945..67ffd464d0 100644 --- a/components/esptool_py/project_include.cmake +++ b/components/esptool_py/project_include.cmake @@ -8,6 +8,16 @@ set(ESPTOOLPY ${python} "${CMAKE_CURRENT_LIST_DIR}/esptool/esptool.py" --chip ${ set(ESPSECUREPY ${python} "${CMAKE_CURRENT_LIST_DIR}/esptool/espsecure.py") set(ESPEFUSEPY ${python} "${CMAKE_CURRENT_LIST_DIR}/esptool/espefuse.py") +set(ESPFLASHMODE ${CONFIG_ESPTOOLPY_FLASHMODE}) +set(ESPFLASHFREQ ${CONFIG_ESPTOOLPY_FLASHFREQ}) +set(ESPFLASHSIZE ${CONFIG_ESPTOOLPY_FLASHSIZE}) + +set(ESPTOOLPY_FLASH_OPTIONS + --flash_mode ${ESPFLASHMODE} + --flash_freq ${ESPFLASHFREQ} + --flash_size ${ESPFLASHSIZE} + ) + if(NOT BOOTLOADER_BUILD) set(esptool_elf2image_args --elf-sha256-offset 0xb0) endif() @@ -128,12 +138,9 @@ else() list(APPEND esptool_flash_main_args "--after=${CONFIG_ESPTOOLPY_AFTER}") endif() -set(esptool_flash_sub_args "--flash_mode=${CONFIG_ESPTOOLPY_FLASHMODE}" - "--flash_freq=${CONFIG_ESPTOOLPY_FLASHFREQ}" - "--flash_size=${CONFIG_ESPTOOLPY_FLASHSIZE}") idf_component_set_property(esptool_py FLASH_ARGS "${esptool_flash_main_args}") -idf_component_set_property(esptool_py FLASH_SUB_ARGS "${esptool_flash_sub_args}") +idf_component_set_property(esptool_py FLASH_SUB_ARGS "${ESPTOOLPY_FLASH_OPTIONS}") function(esptool_py_flash_target_image target_name image_name offset image) idf_build_get_property(build_dir BUILD_DIR) From cbb6beccd971fc1951fea68c1a80e192849e1efa Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Thu, 23 Jan 2020 12:20:22 +0100 Subject: [PATCH 2/2] ci: add build system tests for bootloader flash mode/freq/size --- tools/ci/test_build_system.sh | 31 ++++++++++++++++++++++++ tools/ci/test_build_system_cmake.sh | 37 +++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/tools/ci/test_build_system.sh b/tools/ci/test_build_system.sh index ebe459dd54..0239a1029a 100755 --- a/tools/ci/test_build_system.sh +++ b/tools/ci/test_build_system.sh @@ -288,6 +288,25 @@ function run_tests() ( make 2>&1 | grep "does not fit in configured flash size 1MB" ) || failure "Build didn't fail with expected flash size failure message" mv sdkconfig.bak sdkconfig + print_status "Flash size is correctly set in the bootloader image header" + # Build with the default 2MB setting + rm sdkconfig + make defconfig && make bootloader || failure "Failed to build bootloader" + bin_header_match build/bootloader/bootloader.bin "0210" + # Change to 4MB + echo "CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y" > sdkconfig + make defconfig && make bootloader || failure "Failed to build bootloader" + bin_header_match build/bootloader/bootloader.bin "0220" + # Change to QIO, bootloader should still be DIO (will change to QIO in 2nd stage bootloader) + echo "CONFIG_FLASHMODE_QIO=y" > sdkconfig + make defconfig && make bootloader || failure "Failed to build bootloader" + bin_header_match build/bootloader/bootloader.bin "0210" + # Change to 80 MHz + echo "CONFIG_ESPTOOLPY_FLASHFREQ_80M=y" > sdkconfig + make defconfig && make bootloader || failure "Failed to build bootloader" + bin_header_match build/bootloader/bootloader.bin "021f" + rm sdkconfig + print_status "sdkconfig should have contents of all files: sdkconfig, sdkconfig.defaults, sdkconfig.defaults.IDF_TARGET" make clean > /dev/null; rm -f sdkconfig.defaults; @@ -491,5 +510,17 @@ function clean_build_dir() rm -rf --preserve-root ${BUILD}/* } +# check the bytes 3-4 of the binary image header. e.g.: +# bin_header_match app.bin 0210 +function bin_header_match() +{ + expected=$2 + filename=$1 + actual=$(xxd -s 2 -l 2 -ps $1) + if [ ! "$expected" = "$actual" ]; then + failure "Incorrect binary image header, expected $expected got $actual" + fi +} + cd ${TESTDIR} run_tests diff --git a/tools/ci/test_build_system_cmake.sh b/tools/ci/test_build_system_cmake.sh index 339aa008e8..00b15274b0 100755 --- a/tools/ci/test_build_system_cmake.sh +++ b/tools/ci/test_build_system_cmake.sh @@ -595,6 +595,31 @@ endmenu\n" >> ${IDF_PATH}/Kconfig rm idf_project.yml git checkout main/main.c + print_status "Build fails if partitions don't fit in flash" + sed -i.bak "s/CONFIG_ESPTOOLPY_FLASHSIZE.\+//" sdkconfig # remove all flashsize config + echo "CONFIG_ESPTOOLPY_FLASHSIZE_1MB=y" >> sdkconfig # introduce undersize flash + ( idf.py build 2>&1 | grep "does not fit in configured flash size 1MB" ) || failure "Build didn't fail with expected flash size failure message" + mv sdkconfig.bak sdkconfig + + print_status "Flash size is correctly set in the bootloader image header" + # Build with the default 2MB setting + rm sdkconfig + idf.py bootloader || failure "Failed to build bootloader" + bin_header_match build/bootloader/bootloader.bin "0210" + # Change to 4MB + echo "CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y" > sdkconfig + idf.py bootloader || failure "Failed to build bootloader" + bin_header_match build/bootloader/bootloader.bin "0220" + # Change to QIO, bootloader should still be DIO (will change to QIO in 2nd stage bootloader) + echo "CONFIG_FLASHMODE_QIO=y" > sdkconfig + idf.py bootloader || failure "Failed to build bootloader" + bin_header_match build/bootloader/bootloader.bin "0210" + # Change to 80 MHz + echo "CONFIG_ESPTOOLPY_FLASHFREQ_80M=y" > sdkconfig + idf.py bootloader || failure "Failed to build bootloader" + bin_header_match build/bootloader/bootloader.bin "021f" + rm sdkconfig + print_status "All tests completed" if [ -n "${FAILURES}" ]; then echo "Some failures were detected:" @@ -722,5 +747,17 @@ function clean_build_dir() rm -rf $PRESERVE_ROOT_ARG ${BUILD}/* ${BUILD}/.* } +# check the bytes 3-4 of the binary image header. e.g.: +# bin_header_match app.bin 0210 +function bin_header_match() +{ + expected=$2 + filename=$1 + actual=$(xxd -s 2 -l 2 -ps $1) + if [ ! "$expected" = "$actual" ]; then + failure "Incorrect binary image header, expected $expected got $actual" + fi +} + cd ${TESTDIR} run_tests