Improvements and fixes for CMake

pull/261/head
Cameron McQuinn 2022-12-14 18:06:51 -07:00
parent 95d5ed6c01
commit 8ace007cd8
5 changed files with 136 additions and 100 deletions

View File

@ -32,33 +32,37 @@
cmake_minimum_required(VERSION 3.16) cmake_minimum_required(VERSION 3.16)
if("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_BINARY_DIR}") if("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_BINARY_DIR}")
message(FATAL_ERROR "In-source builds are not allowed.\ message(
FATAL_ERROR
"In-source builds are not allowed.\
Run cmake from a separate directory from where CMakeLists.txt lives.\ Run cmake from a separate directory from where CMakeLists.txt lives.\
NOTE: cmake will now create CMakeCache.txt and CMakeFiles/*.\ NOTE: cmake will now create CMakeCache.txt and CMakeFiles/*.\
You must delete them, or cmake will refuse to work.") You must delete them, or cmake will refuse to work.")
endif() endif()
project(wolfBoot) project(wolfBoot)
include(cmake/utils.cmake) include(cmake/utils.cmake)
include(cmake/functions.cmake) include(cmake/functions.cmake)
if(NOT DEFINED WOLFBOOT_TARGET) if(NOT DEFINED WOLFBOOT_TARGET)
message(FATAL_ERROR "WOLFBOOT_TARGET must be defined") message(FATAL_ERROR "WOLFBOOT_TARGET must be defined")
else() else()
message(STATUS "Building for ${WOLFBOOT_TARGET}") message(STATUS "Building for ${WOLFBOOT_TARGET}")
endif() endif()
if(NOT DEFINED WOLFBOOT_PARTITION_SIZE)
message(FATAL_ERROR "WOLFBOOT_PARTITION_SIZE must be defined")
endif()
if(NOT DEFINED WOLFBOOT_SECTOR_SIZE) if(NOT DEFINED WOLFBOOT_SECTOR_SIZE)
message(FATAL_ERROR "WOLFBOOT_SECTOR_SIZE must be defined") message(FATAL_ERROR "WOLFBOOT_SECTOR_SIZE must be defined")
endif() endif()
# If PULL_LINKER_DEFINES is set, partition addresses and size info will come from the linker script,
# so no need to pass them on the command line. These variables do need to be set for building the
# test apps.
if(NOT DEFINED PULL_LINKER_DEFINES AND NOT DEFINED BUILD_TEST_APPS)
if(NOT DEFINED WOLFBOOT_PARTITION_SIZE)
message(FATAL_ERROR "WOLFBOOT_PARTITION_SIZE must be defined")
endif()
if(NOT DEFINED WOLFBOOT_PARTITION_BOOT_ADDRESS) if(NOT DEFINED WOLFBOOT_PARTITION_BOOT_ADDRESS)
message(FATAL_ERROR "WOLFBOOT_PARTITION_BOOT_ADDRESS must be defined") message(FATAL_ERROR "WOLFBOOT_PARTITION_BOOT_ADDRESS must be defined")
endif() endif()
@ -70,7 +74,7 @@ endif()
if(NOT DEFINED WOLFBOOT_PARTITION_SWAP_ADDRESS) if(NOT DEFINED WOLFBOOT_PARTITION_SWAP_ADDRESS)
message(FATAL_ERROR "WOLFBOOT_PARTITION_SWAP_ADDRESS must be defined") message(FATAL_ERROR "WOLFBOOT_PARTITION_SWAP_ADDRESS must be defined")
endif() endif()
endif()
if(NOT DEFINED ARM_TARGETS) if(NOT DEFINED ARM_TARGETS)
list(APPEND ARM_TARGETS stm32h7 stm32l0 stm32f4 stm32u5) list(APPEND ARM_TARGETS stm32h7 stm32l0 stm32f4 stm32u5)
@ -140,7 +144,13 @@ add_option(
"no" "no"
"yes;no") "yes;no")
add_option("BUILD_TEST_APPS" "Build the wolfBoot test apps (default: disabled)" "no" "yes;no") add_option("BUILD_TEST_APPS" "Build the wolfBoot test apps (default: disabled)" "no" "yes;no")
add_option("PYTHON_KEYTOOLS" "Use wolfCrypt-py for key generation and signing (default: disabled)" "no" "yes;no") add_option("PYTHON_KEYTOOLS" "Use wolfCrypt-py for key generation and signing (default: disabled)"
"no" "yes;no")
add_option(
"PULL_LINKER_DEFINES"
"Pull partition addresses from the linker script instead of defining fixed addresses in target.h (default: disabled)"
"no"
"yes;no")
# unset cache variables Variables that need to be accessed by the gen_wolfboot_platform_target cmake # unset cache variables Variables that need to be accessed by the gen_wolfboot_platform_target cmake
# function called from the parent cmake project are added to the cache so that they can be accessed # function called from the parent cmake project are added to the cache so that they can be accessed
@ -240,7 +250,6 @@ if(ARCH STREQUAL "AARCH64")
endif() endif()
list(APPEND WOLFBOOT_DEFS ARCH_FLASH_OFFSET=${ARCH_FLASH_OFFSET}) list(APPEND WOLFBOOT_DEFS ARCH_FLASH_OFFSET=${ARCH_FLASH_OFFSET})
if(${WOLFBOOT_TARGET} STREQUAL "x86_64_efi") if(${WOLFBOOT_TARGET} STREQUAL "x86_64_efi")
@ -432,6 +441,10 @@ list(APPEND WOLFBOOT_DEFS ${SIGN_OPTIONS})
list(APPEND WOLFBOOT_COMPILE_OPTIONS -Wstack-usage=${STACK_USAGE} -Wno-unused) list(APPEND WOLFBOOT_COMPILE_OPTIONS -Wstack-usage=${STACK_USAGE} -Wno-unused)
if(PULL_LINKER_DEFINES)
list(APPEND WOLFBOOT_DEFS PULL_LINKER_DEFINES)
endif()
if(RAM_CODE) if(RAM_CODE)
list(APPEND WOLFBOOT_DEFS RAM_CODE) list(APPEND WOLFBOOT_DEFS RAM_CODE)
endif() endif()
@ -490,6 +503,7 @@ endif()
if(NOT DEFINED WOLFBOOT_VERSION) if(NOT DEFINED WOLFBOOT_VERSION)
set(WOLFBOOT_VERSION 1) set(WOLFBOOT_VERSION 1)
endif() endif()
list(APPEND WOLFBOOT_DEFS WOLFBOOT_VERSION=${WOLFBOOT_VERSION}) list(APPEND WOLFBOOT_DEFS WOLFBOOT_VERSION=${WOLFBOOT_VERSION})
if(DELTA_UPDATES) if(DELTA_UPDATES)
@ -536,19 +550,16 @@ set(SPI_TARGET ${WOLFBOOT_TARGET})
# Default UART driver name # Default UART driver name
set(UART_TARGET ${WOLFBOOT_TARGET}) set(UART_TARGET ${WOLFBOOT_TARGET})
if(${WOLFBOOT_TARGET} STREQUAL "stm32l0") set(SPI_DRV_STM32_TARGETS
set(SPI_TARGET stm32) "stm32l0"
endif() "stm32f4"
"stm32l4"
"stm32f7"
"stm32h7"
"stm32wb"
"stm32u5")
if(${WOLFBOOT_TARGET} STREQUAL "stm32h7") if(${WOLFBOOT_TARGET} IN_LIST SPI_DRV_STM32_TARGETS)
set(SPI_TARGET stm32)
endif()
if(${WOLFBOOT_TARGET} STREQUAL "stm32u5")
set(SPI_TARGET stm32)
endif()
if(${WOLFBOOT_TARGET} STREQUAL "stm32f4")
set(SPI_TARGET stm32) set(SPI_TARGET stm32)
endif() endif()
@ -657,16 +668,17 @@ set(WOLFBOOT_VERSION
configure_file(include/target.h.in ${CMAKE_CURRENT_BINARY_DIR}/target.h @ONLY) configure_file(include/target.h.in ${CMAKE_CURRENT_BINARY_DIR}/target.h @ONLY)
add_library(target INTERFACE) add_library(target INTERFACE)
target_compile_definitions(target INTERFACE ${WOLFBOOT_DEFS})
target_include_directories(target BEFORE INTERFACE ${CMAKE_CURRENT_BINARY_DIR}) target_include_directories(target BEFORE INTERFACE ${CMAKE_CURRENT_BINARY_DIR})
set(KEYSTORE ${CMAKE_CURRENT_BINARY_DIR}/keystore.c) set(KEYSTORE ${CMAKE_CURRENT_BINARY_DIR}/keystore.c)
if(NOT SIGN STREQUAL "NONE") if(NOT SIGN STREQUAL "NONE")
if(NOT EXISTS ${KEYSTORE})
if(NOT PYTHON_KEYTOOLS) if(NOT PYTHON_KEYTOOLS)
list(APPEND KEYTOOL_SOURCES src/delta.c list(
APPEND
KEYTOOL_SOURCES
src/delta.c
lib/wolfssl/wolfcrypt/src/asn.c lib/wolfssl/wolfcrypt/src/asn.c
lib/wolfssl/wolfcrypt/src/aes.c lib/wolfssl/wolfcrypt/src/aes.c
lib/wolfssl/wolfcrypt/src/ecc.c lib/wolfssl/wolfcrypt/src/ecc.c
@ -693,34 +705,58 @@ if(NOT SIGN STREQUAL "NONE")
lib/wolfssl/wolfcrypt/src/wc_port.c lib/wolfssl/wolfcrypt/src/wc_port.c
lib/wolfssl/wolfcrypt/src/wolfmath.c) lib/wolfssl/wolfcrypt/src/wolfmath.c)
list(APPEND KEYTOOL_FLAGS -Wall -Wextra -Werror -Itools/keytools -DWOLFSSL_USER_SETTINGS list(
-Ilib/wolfssl/ -Iinclude -I${CMAKE_CURRENT_BINARY_DIR} -DWOLFBOOT_KEYTOOLS -O2 -DIMAGE_HEADER_SIZE=256 -DDELTA_UPDATES) APPEND
KEYTOOL_FLAGS
-Wall
-Wextra
-Werror
-Itools/keytools
-DWOLFSSL_USER_SETTINGS
-Ilib/wolfssl/
-Iinclude
-I${CMAKE_CURRENT_BINARY_DIR}
-DWOLFBOOT_KEYTOOLS
-O2
-DIMAGE_HEADER_SIZE=${IMAGE_HEADER_SIZE}
-DDELTA_UPDATES)
add_custom_command( add_custom_command(
OUTPUT ${SIGN_TOOL} OUTPUT ${SIGN_TOOL}
COMMAND gcc -o ${CMAKE_CURRENT_BINARY_DIR}/sign tools/keytools/sign.c ${KEYTOOL_SOURCES} ${KEYTOOL_FLAGS} COMMAND gcc -o ${CMAKE_CURRENT_BINARY_DIR}/sign tools/keytools/sign.c ${KEYTOOL_SOURCES}
${KEYTOOL_FLAGS}
WORKING_DIRECTORY ${WOLFBOOT_ROOT} WORKING_DIRECTORY ${WOLFBOOT_ROOT}
COMMENT "Building signing tool") COMMENT "Building signing tool")
add_custom_command( add_custom_command(
OUTPUT ${KEYGEN_TOOL} OUTPUT ${KEYGEN_TOOL}
COMMAND gcc -o ${CMAKE_CURRENT_BINARY_DIR}/keygen tools/keytools/keygen.c ${KEYTOOL_SOURCES} ${KEYTOOL_FLAGS} COMMAND gcc -o ${CMAKE_CURRENT_BINARY_DIR}/keygen tools/keytools/keygen.c
${KEYTOOL_SOURCES} ${KEYTOOL_FLAGS}
WORKING_DIRECTORY ${WOLFBOOT_ROOT} WORKING_DIRECTORY ${WOLFBOOT_ROOT}
COMMENT "Building keygen tool") COMMENT "Building keygen tool")
endif() endif()
add_custom_command(
OUTPUT ${KEYSTORE}
COMMAND ${KEYGEN_TOOL} ${KEYTOOL_OPTIONS} -g ${WOLFBOOT_SIGNING_PRIVATE_KEY} -keystoreDir ${CMAKE_CURRENT_BINARY_DIR}
WORKING_DIRECTORY ${WOLFBOOT_ROOT}
COMMENT "Generating keystore.c and signing private key")
if(PYTHON_KEYTOOLS) if(PYTHON_KEYTOOLS)
add_custom_target(keystore DEPENDS ${KEYSTORE}) add_custom_target(keystore DEPENDS ${KEYSTORE})
else() else()
add_custom_target(keystore ALL DEPENDS ${SIGN_TOOL} ${KEYGEN_TOOL} ${KEYSTORE}) add_custom_target(keystore DEPENDS ${SIGN_TOOL} ${KEYGEN_TOOL} ${KEYSTORE})
endif() endif()
# generate keystore if it does not already exist
if(NOT EXISTS ${KEYSTORE})
add_custom_command(
OUTPUT ${KEYSTORE} ${WOLFBOOT_SIGNING_PRIVATE_KEY}
COMMAND ${KEYGEN_TOOL} ${KEYTOOL_OPTIONS} -g ${WOLFBOOT_SIGNING_PRIVATE_KEY}
-keystoreDir ${CMAKE_CURRENT_BINARY_DIR}
WORKING_DIRECTORY ${WOLFBOOT_ROOT}
COMMENT "Generating keystore.c and signing private key")
if(NOT PYTHON_KEYTOOLS)
add_custom_command(
OUTPUT ${KEYSTORE} ${WOLFBOOT_SIGNING_PRIVATE_KEY}
DEPENDS ${KEYGEN_TOOL}
APPEND)
endif()
endif() endif()
add_library(public_key) add_library(public_key)
@ -740,4 +776,3 @@ target_link_libraries(wolfboot wolfboothal target wolfcrypt)
# dont warn on unused code # dont warn on unused code
target_compile_options(wolfboot PRIVATE -Wno-unused ${SIM_COMPILE_OPTIONS}) target_compile_options(wolfboot PRIVATE -Wno-unused ${SIM_COMPILE_OPTIONS})

View File

@ -75,15 +75,15 @@ function(gen_wolfboot_signed_image TARGET)
# generate signed image # generate signed image
add_custom_command( add_custom_command(
OUTPUT ${TARGET}_signed.bin OUTPUT ${TARGET}_v${VERSION}_signed.bin
DEPENDS ${INPUT_IMAGE} ${WOLFBOOT_SIGNING_PRIVATE_KEY} DEPENDS ${INPUT_IMAGE} ${WOLFBOOT_SIGNING_PRIVATE_KEY} keystore
COMMAND ${SIGN_TOOL} ${KEYTOOL_OPTIONS} ${INPUT_IMAGE} ${WOLFBOOT_SIGNING_PRIVATE_KEY} ${VERSION} COMMAND ${SIGN_TOOL} ${KEYTOOL_OPTIONS} ${INPUT_IMAGE} ${WOLFBOOT_SIGNING_PRIVATE_KEY} ${VERSION}
COMMENT "Signing ${TARGET}" COMMENT "Signing ${TARGET}"
) )
add_custom_target(${TARGET}_signed ALL DEPENDS ${TARGET}_signed.bin) add_custom_target(${TARGET}_signed ALL DEPENDS ${TARGET}_v${VERSION}_signed.bin)
multiconfigfileinstall(${TARGET}_signed.bin bin) multiconfigfileinstall(${TARGET}_v${VERSION}_signed.bin bin)
endfunction() endfunction()
function(gen_wolfboot_factory_image PLATFORM_NAME TARGET) function(gen_wolfboot_factory_image PLATFORM_NAME TARGET)
@ -108,7 +108,7 @@ function(gen_wolfboot_factory_image PLATFORM_NAME TARGET)
# merge images # merge images
add_custom_command( add_custom_command(
OUTPUT ${FILENAME}_factory.bin OUTPUT ${FILENAME}_factory.bin
DEPENDS $<TARGET_FILE:wolfboot_${PLATFORM_NAME}>.bin ${FILENAME}_signed.bin DEPENDS $<TARGET_FILE:wolfboot_${PLATFORM_NAME}>.bin ${FILENAME}_v${VERSION}_signed.bin
${WOLFBOOT_SIGNING_PRIVATE_KEY} binAssemble ${WOLFBOOT_SIGNING_PRIVATE_KEY} binAssemble
COMMAND ${BINASSEMBLE} ${FILENAME}_factory.bin ${ARCH_FLASH_OFFSET} COMMAND ${BINASSEMBLE} ${FILENAME}_factory.bin ${ARCH_FLASH_OFFSET}
$<TARGET_FILE:wolfboot_${PLATFORM_NAME}>.bin ${BOOT_ADDRESS} ${TARGET}_v${VERSION}_signed.bin $<TARGET_FILE:wolfboot_${PLATFORM_NAME}>.bin ${BOOT_ADDRESS} ${TARGET}_v${VERSION}_signed.bin

View File

@ -721,6 +721,7 @@ st-flash write test-app/image_v1_signed.bin 0x08020000
To sign the same application image as new version (2), use the sign tools To sign the same application image as new version (2), use the sign tools
Python: `tools/keytools/sign.py --ecc256 --sha256 test-app/image.bin wolfboot_signing_private_key.der 2` Python: `tools/keytools/sign.py --ecc256 --sha256 test-app/image.bin wolfboot_signing_private_key.der 2`
C Tool: `tools/keytools/sign --ecc256 --sha256 test-app/image.bin wolfboot_signing_private_key.der 2` C Tool: `tools/keytools/sign --ecc256 --sha256 test-app/image.bin wolfboot_signing_private_key.der 2`
Flash the updated version 2 image: `st-flash write test-app/image_v2_signed.bin 0x08120000` Flash the updated version 2 image: `st-flash write test-app/image_v2_signed.bin 0x08120000`

View File

@ -47,9 +47,9 @@ SECTIONS
. = ALIGN(4); . = ALIGN(4);
} }
_wolfboot_partition_boot_address = 0x8020000; _wolfboot_partition_boot_address = @WOLFBOOT_PARTITION_BOOT_ADDRESS@;
_wolfboot_partition_size = 0xD0000; _wolfboot_partition_size = @WOLFBOOT_PARTITION_SIZE@;
_wolfboot_partition_update_address = 0x80F0000; _wolfboot_partition_update_address = @WOLFBOOT_PARTITION_UPDATE_ADDRESS@;
_wolfboot_partition_swap_address = 0x81C0000; _wolfboot_partition_swap_address = @WOLFBOOT_PARTITION_SWAP_ADDRESS@;
END_STACK = ORIGIN(RAM) + LENGTH(RAM); END_STACK = ORIGIN(RAM) + LENGTH(RAM);

View File

@ -41,10 +41,10 @@ SECTIONS
} > RAM } > RAM
} }
_wolfboot_partition_boot_address = 0x8020000; _wolfboot_partition_boot_address = @WOLFBOOT_PARTITION_BOOT_ADDRESS@;
_wolfboot_partition_size = 0xD0000; _wolfboot_partition_size = @WOLFBOOT_PARTITION_SIZE@;
_wolfboot_partition_update_address = 0x80F0000; _wolfboot_partition_update_address = @WOLFBOOT_PARTITION_UPDATE_ADDRESS@;
_wolfboot_partition_swap_address = 0x81C0000; _wolfboot_partition_swap_address = @WOLFBOOT_PARTITION_SWAP_ADDRESS@;
PROVIDE(_start_heap = _end); PROVIDE(_start_heap = _end);
PROVIDE(_end_stack = ORIGIN(RAM) + LENGTH(RAM)); PROVIDE(_end_stack = ORIGIN(RAM) + LENGTH(RAM));