blob: a4607524f0bcb91db1315f47481bc48ccbfa6250 [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
Marti Bolivardde1b1c2018-01-29 14:41:58 -05009set(KCONFIG_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/Kconfig)
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020010
Marti Bolivarbf909a12017-11-13 19:43:46 -050011########################
12# Configuration choices.
13########################
14
15# Set CONF_SIGNATURE_TYPE to determine the signature type used.
16# Currently, it should be set to either RSA or ECDSA_P256.
17#
18# To choose RSA (this is the default):
19#
20# cmake -DCONF_SIGNATURE_TYPE=RSA [...]
21#
22# To use ECDSA_P256:
23#
24# cmake -DCONF_SIGNATURE_TYPE=ECDSA_P256 [...]
25if (NOT DEFINED CONF_SIGNATURE_TYPE)
26 set(CONF_SIGNATURE_TYPE RSA)
27endif()
28
29# If CONF_VALIDATE_SLOT0 is set, the bootloader attempts to validate
30# the signature of slot0 every boot. This adds the signature check
31# time to every boot, but can mitigate against some changes that are
32# able to modify the flash image itself.
33#
34# To enable validation (this is the default):
35#
36# cmake -DCONF_VALIDATE_SLOT0=YES [...]
37#
38# To disable validation:
39#
40# cmake -DCONF_VALIDATE_SLOT0=NO [...]
41if (NOT DEFINED CONF_VALIDATE_SLOT0)
42 set(CONF_VALIDATE_SLOT0 YES)
43endif()
44
45# If CONF_UPGRADE_ONLY is set, overwrite slot0 with the upgrade image
46# instead of swapping them. This prevents the fallback recovery, but
47# uses a much simpler code path.
48#
49# To enable "upgrade only" mode:
50#
51# cmake -DCONF_UPGRADE_ONLY=YES [...]
52#
53# To disable "upgrade only" mode (this is the default):
54#
55# cmake -DCONF_UPGRADE_ONLY=NO [...]
56if (NOT DEFINED CONF_UPGRADE_ONLY)
57 set(CONF_UPGRADE_ONLY NO)
58endif()
59
Marti Bolivaraefbd462017-12-15 03:43:46 -050060# If CONF_ZEPHYR_TRY_MASS_ERASE is set (it is set by default), the
61# Zephyr build system configuration attempts to force a mass erase
62# before flashing. This ensures the scratch and other partitions are
63# in a consistent state.
64#
65# This is not available for all boards.
66#
67# To enable the mass erase attempt (this is the default):
68#
69# cmake -DCONF_ZEPHYR_TRY_MASS_ERASE=YES [...]
70#
71# To disable the mass erase attempt:
72#
73# cmake -DCONF_ZEPHYR_TRY_MASS_ERASE=NO [...]
74if (NOT DEFINED CONF_ZEPHYR_TRY_MASS_ERASE)
75 set(CONF_ZEPHYR_TRY_MASS_ERASE YES)
76endif()
77
Marti Bolivarbf909a12017-11-13 19:43:46 -050078##############################
79# End of configuration blocks.
80##############################
81
82set(MCUBOOT_EXTRA_CFLAGS)
83
84# Determine CFLAGS / MCUBOOT_CONF_FILE / NEED_TINYCRYPT from the signature type.
85if(CONF_SIGNATURE_TYPE STREQUAL RSA)
86 set(MCUBOOT_CONF_FILE prj.conf) # RSA
87 list(APPEND MCUBOOT_EXTRA_CFLAGS "-DMCUBOOT_SIGN_RSA" "-DMCUBOOT_USE_MBED_TLS")
88 set(NEED_TINYCRYPT NO)
89elseif(CONF_SIGNATURE_TYPE STREQUAL ECDSA_P256)
90 set(MCUBOOT_CONF_FILE prj-p256.conf) # ECDSA P-256
91 list(APPEND MCUBOOT_EXTRA_CFLAGS "-DMCUBOOT_SIGN_EC256" "-DMCUBOOT_USE_TINYCRYPT")
92 set(NEED_TINYCRYPT YES)
93else()
94 message(FATAL_ERROR "Invalid CONF_SIGNATURE_TYPE specified: '${CONF_SIGNATURE_TYPE}'")
95endif()
96
97# Board-specific CONF_FILES should get merged into the build as well.
98#
99# Do this by defining the set_conf_file macro:
100# http://docs.zephyrproject.org/application/application.html#application-configuration
101macro(set_conf_file)
102 if (EXISTS ${APPLICATION_SOURCE_DIR}/boards/${BOARD}.conf)
103 set(CONF_FILE "${MCUBOOT_CONF_FILE} ${APPLICATION_SOURCE_DIR}/boards/${BOARD}.conf")
104 else()
105 set(CONF_FILE "${MCUBOOT_CONF_FILE}")
106 endif()
107endmacro()
108
109# Check if we need to validate slot 0.
110if(CONF_VALIDATE_SLOT0 STREQUAL YES)
111 list(APPEND MCUBOOT_EXTRA_CFLAGS "-DMCUBOOT_VALIDATE_SLOT0")
112endif()
113
114# Enabling this option uses newer flash map APIs. This saves RAM and
115# avoids deprecated API usage.
116#
117# (This can be deleted when flash_area_to_sectors() is removed instead
118# of simply deprecated.)
119list(APPEND MCUBOOT_EXTRA_CFLAGS "-DMCUBOOT_USE_FLASH_AREA_GET_SECTORS")
120
121# Check if we're operating in overwrite-only mode.
122if(CONF_UPGRADE_ONLY STREQUAL YES)
123 list (APPEND MCUBOOT_EXTRA_CFLAGS "-DMCUBOOT_OVERWRITE_ONLY" "-DMCUBOOT_OVERWRITE_ONLY_FAST")
124endif()
125
126# Add values in MCUBOOT_EXTRA_CFLAGS_STR to the Zephyr build's
127# EXTRA_CFLAGS variable.
128string(REPLACE ";" " " MCUBOOT_EXTRA_CFLAGS_STR "${MCUBOOT_EXTRA_CFLAGS}")
129set(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${MCUBOOT_EXTRA_CFLAGS_STR}")
130
131# The board should be set to a supported target.
132if (NOT DEFINED BOARD)
133 set(BOARD qemu_x86)
134endif()
135
Marti Bolivar58b321a2018-03-20 15:52:47 -0400136# Add a common dts overlay necessary to ensure mcuboot is linked into,
137# and fits inside, the boot partition. (If the user specified a
138# DTC_OVERLAY_FILE on the CMake command line, we need to append onto
139# the list).
140if(DTC_OVERLAY_FILE)
141 set(DTC_OVERLAY_FILE
142 "${DTC_OVERLAY_FILE} ${CMAKE_CURRENT_LIST_DIR}/dts.overlay")
143else()
144 set(DTC_OVERLAY_FILE ${CMAKE_CURRENT_LIST_DIR}/dts.overlay)
145endif()
Marti Bolivarbf909a12017-11-13 19:43:46 -0500146
Marti Bolivaraefbd462017-12-15 03:43:46 -0500147# Enable Zephyr runner options which request mass erase if so
148# configured.
149#
150# Note that this also disables the default "leave" option when
151# targeting STM32 DfuSe devices with dfu-util, making the chip stay in
152# the bootloader after flashing.
153#
154# That's the right thing, because mcuboot has nothing to do since the
155# chip was just erased. The next thing the user is going to want to do
156# is flash the application. (Developers can reset DfuSE devices
157# manually to test mcuboot behavior on an otherwise erased flash
158# device.)
159macro(app_set_runner_args)
160 if(CONF_ZEPHYR_TRY_MASS_ERASE)
161 board_runner_args(dfu-util "--dfuse-modifiers=force:mass-erase")
162 board_runner_args(pyocd "--flashtool-opt=-ce")
163 endif()
164endmacro()
165
Marti Bolivarbf909a12017-11-13 19:43:46 -0500166# Standard Zephyr application boilerplate:
167# http://docs.zephyrproject.org/application/application.html
168include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
169project(NONE)
170
171# Path to "boot" subdirectory of repository root.
172get_filename_component(BOOT_DIR ${APPLICATION_SOURCE_DIR} DIRECTORY)
173# Path to top-level repository root directory.
174get_filename_component(MCUBOOT_DIR ${BOOT_DIR} DIRECTORY)
175# Path to tinycrypt library source subdirectory of MCUBOOT_DIR.
176set(TINYCRYPT_DIR "${MCUBOOT_DIR}/ext/tinycrypt/lib")
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200177# Path to mbed-tls' asn1 parser library.
178set(MBEDTLS_ASN1_DIR "${MCUBOOT_DIR}/ext/mbedtls")
Marti Bolivarbf909a12017-11-13 19:43:46 -0500179
180# Zephyr application include directories.
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200181if (NOT NEED_TINYCRYPT)
182 # Zephyr's mbedTLS needs this.
183 zephyr_include_directories(include)
184
185 # Use full mbedtls provided by OS for RSA
186 target_include_directories(app PRIVATE $ENV{ZEPHYR_BASE}/ext/lib/crypto/mbedtls/include)
187endif()
188
Marti Bolivarbf909a12017-11-13 19:43:46 -0500189target_include_directories(app PRIVATE include)
190target_include_directories(app PRIVATE targets)
191if(EXISTS "${APPLICATION_SOURCE_DIR}/targets/${BOARD}.h")
Marti Bolivar38845482018-01-25 17:51:40 -0500192 target_compile_definitions(app PRIVATE "-DMCUBOOT_TARGET_CONFIG=\"${BOARD}.h\"")
Marti Bolivarbf909a12017-11-13 19:43:46 -0500193endif()
194
195# Zephyr port-specific sources.
196target_sources(app PRIVATE main.c)
197target_sources(app PRIVATE flash_map.c)
198target_sources(app PRIVATE hal_flash.c)
199target_sources(app PRIVATE os.c)
200target_sources(app PRIVATE keys.c)
201if(NOT DEFINED CONFIG_FLASH_PAGE_LAYOUT)
202 target_sources(app PRIVATE flash_map_legacy.c)
203endif()
204
205# Generic bootutil sources and includes.
206target_include_directories(app PRIVATE "${BOOT_DIR}/bootutil/include")
207target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/loader.c")
208target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/bootutil_misc.c")
209target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/image_validate.c")
210target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/image_rsa.c")
211target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/image_ec256.c")
212target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/caps.c")
213
214# Tinycrypt sources and includes, if needed.
215if (NEED_TINYCRYPT)
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200216 target_include_directories(app PRIVATE "${BOOT_DIR}/zephyr/include")
Marti Bolivarbf909a12017-11-13 19:43:46 -0500217 target_include_directories(app PRIVATE "${TINYCRYPT_DIR}/include")
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200218 target_include_directories(app PRIVATE "${MBEDTLS_ASN1_DIR}/include")
Marti Bolivarbf909a12017-11-13 19:43:46 -0500219
220 target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/ecc.c")
221 target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/ecc_dsa.c")
222 target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/sha256.c")
223 target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/utils.c")
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200224
225 target_sources(app PRIVATE "${MBEDTLS_ASN1_DIR}/src/asn1parse.c")
Marti Bolivarbf909a12017-11-13 19:43:46 -0500226endif()
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200227
228if (CONFIG_MCUBOOT_SERIAL)
229zephyr_sources(${BOOT_DIR}/zephyr/serial_adapter.c)
230
231add_subdirectory(${BOOT_DIR}/boot_serial ./boot/boot_serial)
232add_subdirectory(${BOOT_DIR}/zephyr/tinycbor)
233add_subdirectory(${BOOT_DIR}/zephyr/cborattr)
234
235endif()