From 0fa1f04f8b43bcce1fe84e314c194f2ff6d28219 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20B=C3=B8e?= Date: Wed, 12 Dec 2018 08:59:47 +0100 Subject: [PATCH] [nrf noup] cmake: multi-image + partition manager MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Upstream PR#430, plus various fixes from the v1.0.0 development cycle. This added support for being built as a sub image from the downstream Nordic patch set for a zephyr multi image build system. - Downstream partition manager support, plus bug fixes from the v1.0.0 dev cycle. Partition Manager is a component which uses yaml files to resolve flash placement with a wholistic view of the device. Over time, these formerly separate downstream patch series grew increasingly intertwined after the initial rejection of the upstream multi-image patch set. This represents the results which roughly appeared in NCS v1.0. Signed-off-by: Håkon Øye Amundsen Signed-off-by: Øyvind Rønningstad Signed-off-by: Sebastian Bøe Signed-off-by: Sigvart Hovland Signed-off-by: Marti Bolivar (cherry picked from commit 0e68ab46461cfe62a6db342cbd69a08aac01d82d) (cherry picked from commit 6168414cb103f97b016160396ccc88d9ed45d457) (cherry picked from commit d62e18977b01b074dacd4fc0bfa234286c553cb2) (cherry picked from commit a4db98dff803e17b06908419341577b14ce16107) (cherry picked from commit b6d368756888f1d169d5a02b56c70138f26ce578) (cherry picked from commit 4b919890cc848316304b81d9e160fe5a10ce3764) (cherry picked from commit edc8dc635209e5ef596614f6338157e2e406103f) (cherry picked from commit 289f108b6ce992dc3a0a4038ba2e10fb6ae5336a) (cherry picked from commit 447495faa326082198c8e12493a2097f2ae20a99) (cherry picked from commit 8cb0e49ea071f06f276e1d69566e7c1a3ba36516) (cherry picked from commit 4a61a17c9fc9559512b7b790f841a92cc16e12e1) (cherry picked from commit 8b0e50cd5898e66e8bf92b5378c5b29fb0838471) (cherry picked from commit 545e2c010de334b5b24552b805972d974e8428d5) Signed-off-by: Martí Bolívar --- boot/zephyr/CMakeLists.txt | 17 ++++- boot/zephyr/Kconfig | 39 ++++++++--- boot/zephyr/include/sysflash/sysflash.h | 11 +++ boot/zephyr/pm.yml | 38 +++++++++++ zephyr/CMakeLists.txt | 89 +++++++++++++++++++++++++ zephyr/Kconfig | 56 ++++++++++++++++ 6 files changed, 239 insertions(+), 11 deletions(-) create mode 100644 boot/zephyr/pm.yml create mode 100644 zephyr/CMakeLists.txt create mode 100644 zephyr/Kconfig diff --git a/boot/zephyr/CMakeLists.txt b/boot/zephyr/CMakeLists.txt index 647ba13a1..eff634922 100644 --- a/boot/zephyr/CMakeLists.txt +++ b/boot/zephyr/CMakeLists.txt @@ -8,7 +8,11 @@ cmake_minimum_required(VERSION 3.8.2) # Board-specific CONF_FILES should get merged into the build as well. # Default to qemu_x86 if no board has been specified. -set(BOARD qemu_x86) +# If BOARD is already defined, mcuboot is being built in a multi image +# context, and should default to its parent image BOARD. +if (NOT BOARD) + set(BOARD qemu_x86) +endif() # Add a common dts overlay necessary to ensure mcuboot is linked into, # and fits inside, the boot partition. (If the user specified a @@ -177,7 +181,7 @@ if(CONFIG_MCUBOOT_SERIAL) zephyr_link_libraries_ifdef( CONFIG_TINYCBOR - TINYCBOR + ${IMAGE}TINYCBOR ) zephyr_include_directories_ifdef( @@ -192,6 +196,14 @@ if(NOT CONFIG_BOOT_SIGNATURE_KEY_FILE STREQUAL "") else() set(KEY_FILE ${MCUBOOT_DIR}/${CONFIG_BOOT_SIGNATURE_KEY_FILE}) endif() + + set_property( + GLOBAL + PROPERTY + KEY_FILE + ${KEY_FILE} + ) + set(GENERATED_PUBKEY ${ZEPHYR_BINARY_DIR}/autogen-pubkey.c) add_custom_command( OUTPUT ${GENERATED_PUBKEY} @@ -206,4 +218,3 @@ if(NOT CONFIG_BOOT_SIGNATURE_KEY_FILE STREQUAL "") ) zephyr_library_sources(${GENERATED_PUBKEY}) endif() - diff --git a/boot/zephyr/Kconfig b/boot/zephyr/Kconfig index 52072df52..3fcef6f9f 100644 --- a/boot/zephyr/Kconfig +++ b/boot/zephyr/Kconfig @@ -14,6 +14,37 @@ config MCUBOOT select MPU_ALLOW_FLASH_WRITE if ARM_MPU select USE_CODE_PARTITION if HAS_FLASH_LOAD_OFFSET +# Define used by partition_manager.py to deduce size of partition +config PM_PARTITION_SIZE_MCUBOOT + hex "Flash space reserved for bootloader." + default 0xc000 + help + Flash space set aside for the MCUBoot. Note, the name + of this configuration needs to match the requirements set by the + script 'partition_manager.py'. See pm.yaml. + + +# Define used by partition_manager.py to deduce size of partition +config PM_PARTITION_SIZE_MCUBOOT_SCRATCH + hex "Flash space reserved for scratch." + default 0x1e000 + help + Flash space set aside for the scratch area. + +# Define used by partition_manager.py to deduce size of partition +config PM_PARTITION_SIZE_MCUBOOT_STORAGE + hex "Flash space reserved for storage." + default 0x4000 + help + Flash space set aside for the storage area. + +# Define used by partition_manager.py to deduce size of partition +config PM_PARTITION_SIZE_MCUBOOT_PAD + hex "Flash space reserved for padding area." + default 0x200 + help + Flash space set aside for the padding area. + config BOOT_USE_MBEDTLS bool # Hidden option @@ -111,14 +142,6 @@ endif #BOOT_SIGNATURE_TYPE_ECDSA_P256 endchoice -config BOOT_SIGNATURE_KEY_FILE - string "PEM key file" - default "" - help - The key file will be parsed by imgtool's getpub command and a .c source - with the public key information will be written in a format expected by - MCUboot. - config MBEDTLS_CFG_FILE default "mcuboot-mbedtls-cfg.h" diff --git a/boot/zephyr/include/sysflash/sysflash.h b/boot/zephyr/include/sysflash/sysflash.h index c422efd64..905e6a65f 100644 --- a/boot/zephyr/include/sysflash/sysflash.h +++ b/boot/zephyr/include/sysflash/sysflash.h @@ -3,6 +3,15 @@ #ifndef __SYSFLASH_H__ #define __SYSFLASH_H__ +#if USE_PARTITION_MANAGER +#include + +#define FLASH_AREA_IMAGE_PRIMARY PM_MCUBOOT_PRIMARY_ID +#define FLASH_AREA_IMAGE_SECONDARY PM_MCUBOOT_SECONDARY_ID +#define FLASH_AREA_IMAGE_SCRATCH PM_MCUBOOT_SCRATCH_ID + +#else + #include #include @@ -38,5 +47,7 @@ #endif #define FLASH_AREA_IMAGE_SCRATCH DT_FLASH_AREA_IMAGE_SCRATCH_ID +#endif /* USE_PARTITION_MANAGER */ + #endif /* __SYSFLASH_H__ */ diff --git a/boot/zephyr/pm.yml b/boot/zephyr/pm.yml new file mode 100644 index 000000000..8788652bf --- /dev/null +++ b/boot/zephyr/pm.yml @@ -0,0 +1,38 @@ +#include + +mcuboot: + size: CONFIG_PM_PARTITION_SIZE_MCUBOOT + placement: + before: [mcuboot_primary] + +mcuboot_primary_app: + # All images to be placed in MCUboot's slot 0 should be placed in this + # partition + span: [app] + +mcuboot_primary: + span: [mcuboot_pad, mcuboot_primary_app] + +mcuboot_secondary: + share_size: [mcuboot_primary] + placement: + after: + [mcuboot_primary] + +mcuboot_scratch: + size: CONFIG_PM_PARTITION_SIZE_MCUBOOT_SCRATCH + placement: + after: [app] + +mcuboot_storage: + size: CONFIG_PM_PARTITION_SIZE_MCUBOOT_STORAGE + placement: + after: [mcuboot_scratch] + +# Padding placed before image to boot +mcuboot_pad: + # MCUboot pad must be placed before the 'spm' partition if that is present. + # If 'spm' partition is not present, it must be placed before the 'app'. + size: CONFIG_PM_PARTITION_SIZE_MCUBOOT_PAD + placement: + before: [mcuboot_primary_app] diff --git a/zephyr/CMakeLists.txt b/zephyr/CMakeLists.txt new file mode 100644 index 000000000..8958665bf --- /dev/null +++ b/zephyr/CMakeLists.txt @@ -0,0 +1,89 @@ +if(CONFIG_BOOTLOADER_MCUBOOT) + # Build a second bootloader image + + set(MCUBOOT_BASE ${CMAKE_CURRENT_LIST_DIR}/..) + + zephyr_add_executable(mcuboot require_build) + + if (${require_build}) + add_subdirectory(${MCUBOOT_BASE}/boot/zephyr ${CMAKE_BINARY_DIR}/mcuboot) + endif() # ${require_build} + + set(to_sign_hex ${KERNEL_HEX_NAME}) + + # TODO: Assert that the bootloader and image use the same key. + + set(signed_image_hex ${PROJECT_BINARY_DIR}/signed.hex) + set(signed_image_bin ${PROJECT_BINARY_DIR}/signed.bin) + set(to_sign_bin ${PROJECT_BINARY_DIR}/to_sign.bin) + set(update_hex ${PROJECT_BINARY_DIR}/update.hex) + set(update_bin ${PROJECT_BINARY_DIR}/update.bin) + + get_property(app_binary_dir GLOBAL PROPERTY PROJECT_BINARY_DIR) + set(merged_hex_file + ${app_binary_dir}/mcuboot_primary_app.hex) + set(merged_hex_file_depends + mcuboot_primary_app_hex$${PROJECT_BINARY_DIR}/mcuboot_primary_app.hex) + set(sign_merged + $) + set(to_sign_hex + $) + set(sign_depends + $) + set(sign_cmd + ${PYTHON_EXECUTABLE} + ${MCUBOOT_BASE}/scripts/imgtool.py + sign + --key ${MCUBOOT_BASE}/${CONFIG_BOOT_SIGNATURE_KEY_FILE} + --header-size $ + --align ${DT_FLASH_WRITE_BLOCK_SIZE} + --version ${CONFIG_MCUBOOT_IMAGE_VERSION} + --slot-size $ + --pad-header + ) + + add_custom_command( + OUTPUT + ${signed_image_hex} + ${update_hex} + ${update_bin} + COMMAND + ${sign_cmd} + ${to_sign_hex} + ${signed_image_hex} + COMMAND + ${CMAKE_OBJCOPY} + --input-target=ihex + --output-target=binary + ${to_sign_hex} + ${to_sign_bin} + COMMAND + ${sign_cmd} + ${to_sign_bin} + ${update_bin} + COMMAND + ${sign_cmd} + --pad # This argument is needed for MCUboot to apply the test swap. + ${to_sign_hex} + ${update_hex} + COMMAND + ${CMAKE_OBJCOPY} + --input-target=ihex + --output-target=ihex + --change-address $ + ${update_hex} + ${PROJECT_BINARY_DIR}/moved_update.hex + DEPENDS + ${sign_depends} + ) + add_custom_target(mcuboot_sign_target DEPENDS ${signed_image_hex}) + + set_property(GLOBAL PROPERTY + mcuboot_primary_app_PM_HEX_FILE + ${signed_image_hex} + ) + set_property(GLOBAL PROPERTY + mcuboot_primary_app_PM_TARGET + mcuboot_sign_target + ) +endif() diff --git a/zephyr/Kconfig b/zephyr/Kconfig new file mode 100644 index 000000000..c395b2e5b --- /dev/null +++ b/zephyr/Kconfig @@ -0,0 +1,56 @@ +menu "MCUboot" + +if BOOTLOADER_MCUBOOT + +config MCUBOOT_CMAKELISTS_DIR + string "Path to the directory of the MCUBoot CMakeLists.txt file" + default "$MCUBOOT_BASE/boot/zephyr/" + + +choice + prompt "MCUBoot build strategy" + default MCUBOOT_BUILD_STRATEGY_FROM_SOURCE + +config MCUBOOT_BUILD_STRATEGY_USE_HEX_FILE + # Mandatory option when being built through 'zephyr_add_executable' + bool "Use hex file instead of building MCUBoot" + +if MCUBOOT_BUILD_STRATEGY_USE_HEX_FILE + +config MCUBOOT_HEX_FILE + # Mandatory option when being built through 'zephyr_add_executable' + string "MCUBoot hex file" + +endif # MCUBOOT_USE_HEX_FILE + +config MCUBOOT_BUILD_STRATEGY_SKIP_BUILD + # Mandatory option when being built through 'zephyr_add_executable' + bool "Skip building MCUBoot" + +config MCUBOOT_BUILD_STRATEGY_FROM_SOURCE + # Mandatory option when being built through 'zephyr_add_executable' + bool "Build from source" + +endchoice + +config MCUBOOT_IMAGE_VERSION + string "Image version" + default "0.0.0+0" + help + Value to be passed as 'version' argument to 'imgtool.py' when + creating signed image. Note that no semantics are connected to + this variable. It does not provide downgrade prevention, and is only + valuable for debugging purposes. Format: maj.min.rev+build with + latter parts optional. + +endif # BOOTLOADER_MCUBOOT + +config BOOT_SIGNATURE_KEY_FILE + string "PEM key file" + default "root-rsa-2048.pem" + help + The key file will be parsed by imgtool's getpub command and a .c source + with the public key information will be written in a format expected by + MCUboot. + +endmenu