blob: 55ba173f807ff62adaa88608cf3b6f2227f404f1 [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 Utzig1171df92019-05-10 19:26:38 -030065# Path to crypto-fiat
66set(FIAT_DIR "${MCUBOOT_DIR}/ext/fiat")
67assert_exists(FIAT_DIR)
Fabio Utzig28ee5b02017-12-12 08:10:40 -020068# Path to mbed-tls' asn1 parser library.
69set(MBEDTLS_ASN1_DIR "${MCUBOOT_DIR}/ext/mbedtls")
Sigvart Hovlandebd05032019-03-21 10:47:32 +010070assert_exists(MBEDTLS_ASN1_DIR)
71set(NRF_DIR "${MCUBOOT_DIR}/ext/nrf")
72
73if(CONFIG_BOOT_USE_NRF_CC310_BL)
74set(NRFXLIB_DIR ${MCUBOOT_DIR}/../nrfxlib)
75assert_exists(NRFXLIB_DIR)
76# Don't include this if we are using west
77 add_subdirectory(${NRFXLIB_DIR} ${PROJECT_BINARY_DIR}/nrfxlib)
78endif()
Marti Bolivarbf909a12017-11-13 19:43:46 -050079
Sebastian Bøebe972172019-01-22 14:05:14 +010080zephyr_library_include_directories(
81 include
82 targets
83 )
84if(EXISTS targets/${BOARD}.h)
85 zephyr_library_compile_definitions(MCUBOOT_TARGET_CONFIG="${BOARD}.h")
Marti Bolivarbf909a12017-11-13 19:43:46 -050086endif()
87
88# Zephyr port-specific sources.
Sebastian Bøebe972172019-01-22 14:05:14 +010089zephyr_library_sources(
90 main.c
91 flash_map_extended.c
92 os.c
93 keys.c
94 )
95
Marti Bolivarbf909a12017-11-13 19:43:46 -050096if(NOT DEFINED CONFIG_FLASH_PAGE_LAYOUT)
Sebastian Bøebe972172019-01-22 14:05:14 +010097 zephyr_library_sources(
98 flash_map_legacy.c
99 )
Marti Bolivarbf909a12017-11-13 19:43:46 -0500100endif()
101
102# Generic bootutil sources and includes.
Sebastian Bøebe972172019-01-22 14:05:14 +0100103zephyr_library_include_directories(${BOOT_DIR}/bootutil/include)
104zephyr_library_sources(
105 ${BOOT_DIR}/bootutil/src/loader.c
106 ${BOOT_DIR}/bootutil/src/bootutil_misc.c
107 ${BOOT_DIR}/bootutil/src/image_validate.c
108 ${BOOT_DIR}/bootutil/src/encrypted.c
109 ${BOOT_DIR}/bootutil/src/image_rsa.c
110 ${BOOT_DIR}/bootutil/src/image_ec256.c
Fabio Utzig1171df92019-05-10 19:26:38 -0300111 ${BOOT_DIR}/bootutil/src/image_ed25519.c
Sebastian Bøebe972172019-01-22 14:05:14 +0100112 ${BOOT_DIR}/bootutil/src/caps.c
113 )
Marti Bolivarbf909a12017-11-13 19:43:46 -0500114
Marti Bolivara4818a52018-04-12 13:02:38 -0400115if(CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256)
Sigvart Hovlandebd05032019-03-21 10:47:32 +0100116 zephyr_library_include_directories(
117 ${MBEDTLS_ASN1_DIR}/include
118 )
119 zephyr_library_sources(
120 # Additionally pull in just the ASN.1 parser from mbedTLS.
121 ${MBEDTLS_ASN1_DIR}/src/asn1parse.c
122 ${MBEDTLS_ASN1_DIR}/src/platform_util.c
123 )
124 if(CONFIG_BOOT_USE_TINYCRYPT)
Marti Bolivara4818a52018-04-12 13:02:38 -0400125 # When using ECDSA signatures, pull in our copy of the tinycrypt library.
Sebastian Bøebe972172019-01-22 14:05:14 +0100126 zephyr_library_include_directories(
127 ${BOOT_DIR}/zephyr/include
128 ${TINYCRYPT_DIR}/include
Sebastian Bøebe972172019-01-22 14:05:14 +0100129 )
Marti Bolivarbf909a12017-11-13 19:43:46 -0500130
Sebastian Bøebe972172019-01-22 14:05:14 +0100131 zephyr_library_sources(
132 ${TINYCRYPT_DIR}/source/ecc.c
133 ${TINYCRYPT_DIR}/source/ecc_dsa.c
134 ${TINYCRYPT_DIR}/source/sha256.c
135 ${TINYCRYPT_DIR}/source/utils.c
Sigvart Hovlandebd05032019-03-21 10:47:32 +0100136 )
137 elseif(CONFIG_BOOT_USE_NRF_CC310_BL)
138 zephyr_library_sources(${NRF_DIR}/cc310_glue.c)
139 zephyr_library_include_directories(${NRF_DIR})
140 zephyr_link_libraries(nrfxlib_crypto)
141 endif()
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200142
Ding Taof97cb712018-06-08 14:37:13 +0000143 # Since here we are not using Zephyr's mbedTLS but rather our own, we need
Carles Cufi69c61d02018-06-05 15:56:08 +0200144 # to set MBEDTLS_CONFIG_FILE ourselves. When using Zephyr's copy, this
145 # variable is set by its Kconfig in the Zephyr codebase.
Sebastian Bøebe972172019-01-22 14:05:14 +0100146 zephyr_library_compile_definitions(
147 MBEDTLS_CONFIG_FILE="${CMAKE_CURRENT_LIST_DIR}/include/mcuboot-mbedtls-cfg.h"
148 )
Marti Bolivara4818a52018-04-12 13:02:38 -0400149elseif(CONFIG_BOOT_SIGNATURE_TYPE_RSA)
150 # Use mbedTLS provided by Zephyr for RSA signatures. (Its config file
151 # is set using Kconfig.)
152 zephyr_include_directories(include)
Fabio Utzig1171df92019-05-10 19:26:38 -0300153elseif(CONFIG_BOOT_SIGNATURE_TYPE_ED25519)
154 # For ed25519, mbedTLS is used for ASN1 parsing and SHA512
155 zephyr_include_directories(include)
156
157 zephyr_library_include_directories(
158 ${BOOT_DIR}/zephyr/include
159 ${FIAT_DIR}/include/
160 )
161
162 zephyr_library_sources(
163 ${FIAT_DIR}/src/curve25519.c
164 )
Marti Bolivarbf909a12017-11-13 19:43:46 -0500165endif()
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200166
Sebastian Bøebe972172019-01-22 14:05:14 +0100167if(CONFIG_MCUBOOT_SERIAL)
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200168 zephyr_sources(${BOOT_DIR}/zephyr/serial_adapter.c)
169 zephyr_sources(${BOOT_DIR}/boot_serial/src/boot_serial.c)
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200170
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200171 zephyr_include_directories(${BOOT_DIR}/bootutil/include)
172 zephyr_include_directories(${BOOT_DIR}/boot_serial/include)
173 zephyr_include_directories(include)
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200174
Sebastian Bøebe972172019-01-22 14:05:14 +0100175 zephyr_link_libraries_ifdef(
176 CONFIG_TINYCBOR
177 TINYCBOR
178 )
179
180 zephyr_include_directories_ifdef(
181 CONFIG_BOOT_ERASE_PROGRESSIVELY
182 ${BOOT_DIR}/bootutil/src
183 )
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200184endif()
Fabio Utzigb1e0dc52018-04-26 10:53:19 -0300185
186if(NOT CONFIG_BOOT_SIGNATURE_KEY_FILE STREQUAL "")
187 if(IS_ABSOLUTE ${CONFIG_BOOT_SIGNATURE_KEY_FILE})
188 set(KEY_FILE ${CONFIG_BOOT_SIGNATURE_KEY_FILE})
189 else()
190 set(KEY_FILE ${MCUBOOT_DIR}/${CONFIG_BOOT_SIGNATURE_KEY_FILE})
191 endif()
192 set(GENERATED_PUBKEY ${ZEPHYR_BINARY_DIR}/autogen-pubkey.c)
193 add_custom_command(
194 OUTPUT ${GENERATED_PUBKEY}
195 COMMAND
196 ${PYTHON_EXECUTABLE}
197 ${MCUBOOT_DIR}/scripts/imgtool.py
198 getpub
199 -k
200 ${KEY_FILE}
201 > ${GENERATED_PUBKEY}
202 DEPENDS ${KEY_FILE}
203 )
Sebastian Bøebe972172019-01-22 14:05:14 +0100204 zephyr_library_sources(${GENERATED_PUBKEY})
Fabio Utzigb1e0dc52018-04-26 10:53:19 -0300205endif()
Sigvart Hovlandebd05032019-03-21 10:47:32 +0100206