blob: cb4fecafd0b6c6447a70cfcbc58d83b11de415f5 [file] [log] [blame]
Marti Bolivarbf909a12017-11-13 19:43:46 -05001# CMakeLists.txt for building mcuboot as a Zephyr project
2#
3# Copyright (c) 2017 Open Source Foundries Limited
4#
5# SPDX-License-Identifier: Apache-2.0
6
7cmake_minimum_required(VERSION 3.8.2)
8
Sigvart Hovlandebd05032019-03-21 10:47:32 +01009# Board-specific CONF_FILES should get merged into the build as well.
Sebastian Bøeb94bda02019-01-22 12:33:18 +010010# Default to qemu_x86 if no board has been specified.
11set(BOARD qemu_x86)
Marti Bolivarbf909a12017-11-13 19:43:46 -050012
Marti Bolivar58b321a2018-03-20 15:52:47 -040013# Add a common dts overlay necessary to ensure mcuboot is linked into,
14# and fits inside, the boot partition. (If the user specified a
15# DTC_OVERLAY_FILE on the CMake command line, we need to append onto
16# the list).
17if(DTC_OVERLAY_FILE)
18 set(DTC_OVERLAY_FILE
Sebastian Bøeea49cd02019-04-03 15:14:37 +020019 "${DTC_OVERLAY_FILE} ${CMAKE_CURRENT_LIST_DIR}/dts.overlay"
20 CACHE STRING "" FORCE
21 )
Marti Bolivar58b321a2018-03-20 15:52:47 -040022else()
23 set(DTC_OVERLAY_FILE ${CMAKE_CURRENT_LIST_DIR}/dts.overlay)
24endif()
Marti Bolivarbf909a12017-11-13 19:43:46 -050025
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +053026if (EXISTS ${CMAKE_CURRENT_LIST_DIR}/boards/${BOARD}.overlay)
27 set(DTC_OVERLAY_FILE
Sebastian Bøeea49cd02019-04-03 15:14:37 +020028 "${DTC_OVERLAY_FILE} ${CMAKE_CURRENT_LIST_DIR}/boards/${BOARD}.overlay"
29 CACHE STRING "" FORCE
30 )
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +053031endif()
32
Marti Bolivaraefbd462017-12-15 03:43:46 -050033# Enable Zephyr runner options which request mass erase if so
34# configured.
35#
36# Note that this also disables the default "leave" option when
37# targeting STM32 DfuSe devices with dfu-util, making the chip stay in
38# the bootloader after flashing.
39#
40# That's the right thing, because mcuboot has nothing to do since the
41# chip was just erased. The next thing the user is going to want to do
42# is flash the application. (Developers can reset DfuSE devices
43# manually to test mcuboot behavior on an otherwise erased flash
44# device.)
45macro(app_set_runner_args)
Marti Bolivar53e2c262018-04-12 14:13:28 -040046 if(CONFIG_ZEPHYR_TRY_MASS_ERASE)
Marti Bolivaraefbd462017-12-15 03:43:46 -050047 board_runner_args(dfu-util "--dfuse-modifiers=force:mass-erase")
Maureen Helm4df602a2019-02-18 17:26:39 -060048 board_runner_args(pyocd "--flash-opt=-e=chip")
Marti Bolivar23e38532018-03-26 13:14:22 -040049 board_runner_args(nrfjprog "--erase")
Marti Bolivaraefbd462017-12-15 03:43:46 -050050 endif()
51endmacro()
52
Marti Bolivarbf909a12017-11-13 19:43:46 -050053# Standard Zephyr application boilerplate:
54# http://docs.zephyrproject.org/application/application.html
55include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
56project(NONE)
57
58# Path to "boot" subdirectory of repository root.
59get_filename_component(BOOT_DIR ${APPLICATION_SOURCE_DIR} DIRECTORY)
60# Path to top-level repository root directory.
61get_filename_component(MCUBOOT_DIR ${BOOT_DIR} DIRECTORY)
62# Path to tinycrypt library source subdirectory of MCUBOOT_DIR.
63set(TINYCRYPT_DIR "${MCUBOOT_DIR}/ext/tinycrypt/lib")
Sigvart Hovlandebd05032019-03-21 10:47:32 +010064assert_exists(TINYCRYPT_DIR)
Fabio Utzig28ee5b02017-12-12 08:10:40 -020065# Path to mbed-tls' asn1 parser library.
66set(MBEDTLS_ASN1_DIR "${MCUBOOT_DIR}/ext/mbedtls")
Sigvart Hovlandebd05032019-03-21 10:47:32 +010067assert_exists(MBEDTLS_ASN1_DIR)
68set(NRF_DIR "${MCUBOOT_DIR}/ext/nrf")
69
70if(CONFIG_BOOT_USE_NRF_CC310_BL)
71set(NRFXLIB_DIR ${MCUBOOT_DIR}/../nrfxlib)
72assert_exists(NRFXLIB_DIR)
73# Don't include this if we are using west
74 add_subdirectory(${NRFXLIB_DIR} ${PROJECT_BINARY_DIR}/nrfxlib)
75endif()
Marti Bolivarbf909a12017-11-13 19:43:46 -050076
Sebastian Bøebe972172019-01-22 14:05:14 +010077zephyr_library_include_directories(
78 include
79 targets
80 )
81if(EXISTS targets/${BOARD}.h)
82 zephyr_library_compile_definitions(MCUBOOT_TARGET_CONFIG="${BOARD}.h")
Marti Bolivarbf909a12017-11-13 19:43:46 -050083endif()
84
85# Zephyr port-specific sources.
Sebastian Bøebe972172019-01-22 14:05:14 +010086zephyr_library_sources(
87 main.c
88 flash_map_extended.c
89 os.c
90 keys.c
91 )
92
Marti Bolivarbf909a12017-11-13 19:43:46 -050093if(NOT DEFINED CONFIG_FLASH_PAGE_LAYOUT)
Sebastian Bøebe972172019-01-22 14:05:14 +010094 zephyr_library_sources(
95 flash_map_legacy.c
96 )
Marti Bolivarbf909a12017-11-13 19:43:46 -050097endif()
98
99# Generic bootutil sources and includes.
Sebastian Bøebe972172019-01-22 14:05:14 +0100100zephyr_library_include_directories(${BOOT_DIR}/bootutil/include)
101zephyr_library_sources(
102 ${BOOT_DIR}/bootutil/src/loader.c
103 ${BOOT_DIR}/bootutil/src/bootutil_misc.c
104 ${BOOT_DIR}/bootutil/src/image_validate.c
105 ${BOOT_DIR}/bootutil/src/encrypted.c
106 ${BOOT_DIR}/bootutil/src/image_rsa.c
107 ${BOOT_DIR}/bootutil/src/image_ec256.c
108 ${BOOT_DIR}/bootutil/src/caps.c
109 )
Marti Bolivarbf909a12017-11-13 19:43:46 -0500110
Marti Bolivara4818a52018-04-12 13:02:38 -0400111if(CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256)
Sigvart Hovlandebd05032019-03-21 10:47:32 +0100112 zephyr_library_include_directories(
113 ${MBEDTLS_ASN1_DIR}/include
114 )
115 zephyr_library_sources(
116 # Additionally pull in just the ASN.1 parser from mbedTLS.
117 ${MBEDTLS_ASN1_DIR}/src/asn1parse.c
118 ${MBEDTLS_ASN1_DIR}/src/platform_util.c
119 )
120 if(CONFIG_BOOT_USE_TINYCRYPT)
Marti Bolivara4818a52018-04-12 13:02:38 -0400121 # When using ECDSA signatures, pull in our copy of the tinycrypt library.
Sebastian Bøebe972172019-01-22 14:05:14 +0100122 zephyr_library_include_directories(
123 ${BOOT_DIR}/zephyr/include
124 ${TINYCRYPT_DIR}/include
Sebastian Bøebe972172019-01-22 14:05:14 +0100125 )
Marti Bolivarbf909a12017-11-13 19:43:46 -0500126
Sebastian Bøebe972172019-01-22 14:05:14 +0100127 zephyr_library_sources(
128 ${TINYCRYPT_DIR}/source/ecc.c
129 ${TINYCRYPT_DIR}/source/ecc_dsa.c
130 ${TINYCRYPT_DIR}/source/sha256.c
131 ${TINYCRYPT_DIR}/source/utils.c
Sigvart Hovlandebd05032019-03-21 10:47:32 +0100132 )
133 elseif(CONFIG_BOOT_USE_NRF_CC310_BL)
134 zephyr_library_sources(${NRF_DIR}/cc310_glue.c)
135 zephyr_library_include_directories(${NRF_DIR})
136 zephyr_link_libraries(nrfxlib_crypto)
137 endif()
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200138
Ding Taof97cb712018-06-08 14:37:13 +0000139 # Since here we are not using Zephyr's mbedTLS but rather our own, we need
Carles Cufi69c61d02018-06-05 15:56:08 +0200140 # to set MBEDTLS_CONFIG_FILE ourselves. When using Zephyr's copy, this
141 # variable is set by its Kconfig in the Zephyr codebase.
Sebastian Bøebe972172019-01-22 14:05:14 +0100142 zephyr_library_compile_definitions(
143 MBEDTLS_CONFIG_FILE="${CMAKE_CURRENT_LIST_DIR}/include/mcuboot-mbedtls-cfg.h"
144 )
Marti Bolivara4818a52018-04-12 13:02:38 -0400145elseif(CONFIG_BOOT_SIGNATURE_TYPE_RSA)
146 # Use mbedTLS provided by Zephyr for RSA signatures. (Its config file
147 # is set using Kconfig.)
148 zephyr_include_directories(include)
Marti Bolivarbf909a12017-11-13 19:43:46 -0500149endif()
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200150
Sebastian Bøebe972172019-01-22 14:05:14 +0100151if(CONFIG_MCUBOOT_SERIAL)
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200152 zephyr_sources(${BOOT_DIR}/zephyr/serial_adapter.c)
153 zephyr_sources(${BOOT_DIR}/boot_serial/src/boot_serial.c)
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200154
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200155 zephyr_include_directories(${BOOT_DIR}/bootutil/include)
156 zephyr_include_directories(${BOOT_DIR}/boot_serial/include)
157 zephyr_include_directories(include)
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200158
Sebastian Bøebe972172019-01-22 14:05:14 +0100159 zephyr_link_libraries_ifdef(
160 CONFIG_TINYCBOR
161 TINYCBOR
162 )
163
164 zephyr_include_directories_ifdef(
165 CONFIG_BOOT_ERASE_PROGRESSIVELY
166 ${BOOT_DIR}/bootutil/src
167 )
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200168endif()
Fabio Utzigb1e0dc52018-04-26 10:53:19 -0300169
170if(NOT CONFIG_BOOT_SIGNATURE_KEY_FILE STREQUAL "")
171 if(IS_ABSOLUTE ${CONFIG_BOOT_SIGNATURE_KEY_FILE})
172 set(KEY_FILE ${CONFIG_BOOT_SIGNATURE_KEY_FILE})
173 else()
174 set(KEY_FILE ${MCUBOOT_DIR}/${CONFIG_BOOT_SIGNATURE_KEY_FILE})
175 endif()
176 set(GENERATED_PUBKEY ${ZEPHYR_BINARY_DIR}/autogen-pubkey.c)
177 add_custom_command(
178 OUTPUT ${GENERATED_PUBKEY}
179 COMMAND
180 ${PYTHON_EXECUTABLE}
181 ${MCUBOOT_DIR}/scripts/imgtool.py
182 getpub
183 -k
184 ${KEY_FILE}
185 > ${GENERATED_PUBKEY}
186 DEPENDS ${KEY_FILE}
187 )
Sebastian Bøebe972172019-01-22 14:05:14 +0100188 zephyr_library_sources(${GENERATED_PUBKEY})
Fabio Utzigb1e0dc52018-04-26 10:53:19 -0300189endif()
Sigvart Hovlandebd05032019-03-21 10:47:32 +0100190