blob: de7754abf852b4feec448d381764c78126790e60 [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.
David Brownb748f6f2019-10-11 10:07:31 -060069set(MBEDTLS_ASN1_DIR "${MCUBOOT_DIR}/ext/mbedtls-asn1")
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(
Fabio Utzigccc02802019-11-05 07:55:14 -030098 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
Fabio Utzig61fd8882019-09-14 20:00:20 -0300113 ${BOOT_DIR}/bootutil/src/tlv.c
Sebastian Bøebe972172019-01-22 14:05:14 +0100114 )
Marti Bolivarbf909a12017-11-13 19:43:46 -0500115
Fabio Utzig42cc29a2019-11-05 07:54:41 -0300116if(CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256 OR CONFIG_BOOT_ENCRYPT_EC256)
Sigvart Hovlandebd05032019-03-21 10:47:32 +0100117 zephyr_library_include_directories(
Fabio Utzigccc02802019-11-05 07:55:14 -0300118 ${MBEDTLS_ASN1_DIR}/include
119 )
Sigvart Hovlandebd05032019-03-21 10:47:32 +0100120 zephyr_library_sources(
Fabio Utzigccc02802019-11-05 07:55:14 -0300121 # Additionally pull in just the ASN.1 parser from mbedTLS.
122 ${MBEDTLS_ASN1_DIR}/src/asn1parse.c
123 ${MBEDTLS_ASN1_DIR}/src/platform_util.c
124 )
Sigvart Hovlandebd05032019-03-21 10:47:32 +0100125 if(CONFIG_BOOT_USE_TINYCRYPT)
Marti Bolivara4818a52018-04-12 13:02:38 -0400126 # When using ECDSA signatures, pull in our copy of the tinycrypt library.
Sebastian Bøebe972172019-01-22 14:05:14 +0100127 zephyr_library_include_directories(
Fabio Utzigccc02802019-11-05 07:55:14 -0300128 ${BOOT_DIR}/zephyr/include
129 ${TINYCRYPT_DIR}/include
130 )
Marti Bolivarbf909a12017-11-13 19:43:46 -0500131
Sebastian Bøebe972172019-01-22 14:05:14 +0100132 zephyr_library_sources(
Fabio Utzigccc02802019-11-05 07:55:14 -0300133 ${TINYCRYPT_DIR}/source/ecc.c
134 ${TINYCRYPT_DIR}/source/ecc_dsa.c
135 ${TINYCRYPT_DIR}/source/sha256.c
136 ${TINYCRYPT_DIR}/source/utils.c
137 )
Sigvart Hovlandebd05032019-03-21 10:47:32 +0100138 elseif(CONFIG_BOOT_USE_NRF_CC310_BL)
139 zephyr_library_sources(${NRF_DIR}/cc310_glue.c)
140 zephyr_library_include_directories(${NRF_DIR})
141 zephyr_link_libraries(nrfxlib_crypto)
142 endif()
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200143
Ding Taof97cb712018-06-08 14:37:13 +0000144 # Since here we are not using Zephyr's mbedTLS but rather our own, we need
Carles Cufi69c61d02018-06-05 15:56:08 +0200145 # to set MBEDTLS_CONFIG_FILE ourselves. When using Zephyr's copy, this
146 # variable is set by its Kconfig in the Zephyr codebase.
Sebastian Bøebe972172019-01-22 14:05:14 +0100147 zephyr_library_compile_definitions(
Fabio Utzigccc02802019-11-05 07:55:14 -0300148 MBEDTLS_CONFIG_FILE="${CMAKE_CURRENT_LIST_DIR}/include/mcuboot-mbedtls-cfg.h"
149 )
Marti Bolivara4818a52018-04-12 13:02:38 -0400150elseif(CONFIG_BOOT_SIGNATURE_TYPE_RSA)
151 # Use mbedTLS provided by Zephyr for RSA signatures. (Its config file
152 # is set using Kconfig.)
153 zephyr_include_directories(include)
Fabio Utzig1171df92019-05-10 19:26:38 -0300154elseif(CONFIG_BOOT_SIGNATURE_TYPE_ED25519)
155 # For ed25519, mbedTLS is used for ASN1 parsing and SHA512
156 zephyr_include_directories(include)
157
158 zephyr_library_include_directories(
159 ${BOOT_DIR}/zephyr/include
160 ${FIAT_DIR}/include/
161 )
162
163 zephyr_library_sources(
164 ${FIAT_DIR}/src/curve25519.c
165 )
Marti Bolivarbf909a12017-11-13 19:43:46 -0500166endif()
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200167
Fabio Utzig42cc29a2019-11-05 07:54:41 -0300168if(CONFIG_BOOT_ENCRYPT_EC256)
169 zephyr_library_sources(
170 ${TINYCRYPT_DIR}/source/aes_encrypt.c
171 ${TINYCRYPT_DIR}/source/aes_decrypt.c
172 ${TINYCRYPT_DIR}/source/ctr_mode.c
173 ${TINYCRYPT_DIR}/source/hmac.c
174 ${TINYCRYPT_DIR}/source/ecc_dh.c
175 )
176endif()
177
Sebastian Bøebe972172019-01-22 14:05:14 +0100178if(CONFIG_MCUBOOT_SERIAL)
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200179 zephyr_sources(${BOOT_DIR}/zephyr/serial_adapter.c)
180 zephyr_sources(${BOOT_DIR}/boot_serial/src/boot_serial.c)
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200181
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200182 zephyr_include_directories(${BOOT_DIR}/bootutil/include)
183 zephyr_include_directories(${BOOT_DIR}/boot_serial/include)
184 zephyr_include_directories(include)
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200185
Sebastian Bøebe972172019-01-22 14:05:14 +0100186 zephyr_link_libraries_ifdef(
Fabio Utzigccc02802019-11-05 07:55:14 -0300187 CONFIG_TINYCBOR
188 TINYCBOR
189 )
Sebastian Bøebe972172019-01-22 14:05:14 +0100190
191 zephyr_include_directories_ifdef(
Fabio Utzigccc02802019-11-05 07:55:14 -0300192 CONFIG_BOOT_ERASE_PROGRESSIVELY
193 ${BOOT_DIR}/bootutil/src
194 )
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200195endif()
Fabio Utzigb1e0dc52018-04-26 10:53:19 -0300196
197if(NOT CONFIG_BOOT_SIGNATURE_KEY_FILE STREQUAL "")
198 if(IS_ABSOLUTE ${CONFIG_BOOT_SIGNATURE_KEY_FILE})
199 set(KEY_FILE ${CONFIG_BOOT_SIGNATURE_KEY_FILE})
200 else()
201 set(KEY_FILE ${MCUBOOT_DIR}/${CONFIG_BOOT_SIGNATURE_KEY_FILE})
202 endif()
203 set(GENERATED_PUBKEY ${ZEPHYR_BINARY_DIR}/autogen-pubkey.c)
204 add_custom_command(
205 OUTPUT ${GENERATED_PUBKEY}
206 COMMAND
207 ${PYTHON_EXECUTABLE}
208 ${MCUBOOT_DIR}/scripts/imgtool.py
209 getpub
210 -k
211 ${KEY_FILE}
212 > ${GENERATED_PUBKEY}
213 DEPENDS ${KEY_FILE}
214 )
Sebastian Bøebe972172019-01-22 14:05:14 +0100215 zephyr_library_sources(${GENERATED_PUBKEY})
Fabio Utzigb1e0dc52018-04-26 10:53:19 -0300216endif()
Sigvart Hovlandebd05032019-03-21 10:47:32 +0100217