Skip to content

Commit

Permalink
[nrf noup] cmake: multi-image + partition manager
Browse files Browse the repository at this point in the history
- 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 <[email protected]>
Signed-off-by: Øyvind Rønningstad <[email protected]>
Signed-off-by: Sebastian Bøe <[email protected]>
Signed-off-by: Sigvart Hovland <[email protected]>
Signed-off-by: Marti Bolivar <[email protected]>
(cherry picked from commit 0e68ab4)
(cherry picked from commit 6168414)
(cherry picked from commit d62e189)
(cherry picked from commit a4db98d)
(cherry picked from commit b6d3687)
(cherry picked from commit 4b91989)
(cherry picked from commit edc8dc6)
(cherry picked from commit 289f108)
(cherry picked from commit 447495f)
(cherry picked from commit 8cb0e49)
(cherry picked from commit 4a61a17)
(cherry picked from commit 8b0e50c)
(cherry picked from commit 545e2c0)
Signed-off-by: Martí Bolívar <[email protected]>
  • Loading branch information
SebastianBoe authored and mbolivar-nordic committed Oct 24, 2019
1 parent 4bcdaad commit 0fa1f04
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 11 deletions.
17 changes: 14 additions & 3 deletions boot/zephyr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -177,7 +181,7 @@ if(CONFIG_MCUBOOT_SERIAL)

zephyr_link_libraries_ifdef(
CONFIG_TINYCBOR
TINYCBOR
${IMAGE}TINYCBOR
)

zephyr_include_directories_ifdef(
Expand All @@ -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}
Expand All @@ -206,4 +218,3 @@ if(NOT CONFIG_BOOT_SIGNATURE_KEY_FILE STREQUAL "")
)
zephyr_library_sources(${GENERATED_PUBKEY})
endif()

39 changes: 31 additions & 8 deletions boot/zephyr/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"

Expand Down
11 changes: 11 additions & 0 deletions boot/zephyr/include/sysflash/sysflash.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
#ifndef __SYSFLASH_H__
#define __SYSFLASH_H__

#if USE_PARTITION_MANAGER
#include <pm_config.h>

#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 <generated_dts_board.h>
#include <mcuboot_config/mcuboot_config.h>

Expand Down Expand Up @@ -38,5 +47,7 @@
#endif

#define FLASH_AREA_IMAGE_SCRATCH DT_FLASH_AREA_IMAGE_SCRATCH_ID
#endif /* USE_PARTITION_MANAGER */


#endif /* __SYSFLASH_H__ */
38 changes: 38 additions & 0 deletions boot/zephyr/pm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <autoconf.h>

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]
89 changes: 89 additions & 0 deletions zephyr/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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$<SEMICOLON>${PROJECT_BINARY_DIR}/mcuboot_primary_app.hex)
set(sign_merged
$<TARGET_EXISTS:partition_manager>)
set(to_sign_hex
$<IF:${sign_merged},${merged_hex_file},${PROJECT_BINARY_DIR}/${KERNEL_HEX_NAME}>)
set(sign_depends
$<IF:${sign_merged},${merged_hex_file_depends},zephyr_final>)
set(sign_cmd
${PYTHON_EXECUTABLE}
${MCUBOOT_BASE}/scripts/imgtool.py
sign
--key ${MCUBOOT_BASE}/${CONFIG_BOOT_SIGNATURE_KEY_FILE}
--header-size $<TARGET_PROPERTY:partition_manager,PM_MCUBOOT_PAD_SIZE>
--align ${DT_FLASH_WRITE_BLOCK_SIZE}
--version ${CONFIG_MCUBOOT_IMAGE_VERSION}
--slot-size $<TARGET_PROPERTY:partition_manager,PM_MCUBOOT_PRIMARY_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 $<TARGET_PROPERTY:partition_manager,PM_MCUBOOT_PRIMARY_SIZE>
${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()
56 changes: 56 additions & 0 deletions zephyr/Kconfig
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0fa1f04

Please sign in to comment.