blob: 2361e28ace78b627cf59eab85d6bc539db225c8f [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
136# This is necessary to ensure mcuboot is linked into, and fits inside,
137# the boot partition.
Sebastian Bøe8680b902018-01-29 10:34:43 +0100138set(DTC_OVERLAY_FILE ${CMAKE_CURRENT_LIST_DIR}/dts.overlay)
Marti Bolivarbf909a12017-11-13 19:43:46 -0500139
Marti Bolivaraefbd462017-12-15 03:43:46 -0500140# Enable Zephyr runner options which request mass erase if so
141# configured.
142#
143# Note that this also disables the default "leave" option when
144# targeting STM32 DfuSe devices with dfu-util, making the chip stay in
145# the bootloader after flashing.
146#
147# That's the right thing, because mcuboot has nothing to do since the
148# chip was just erased. The next thing the user is going to want to do
149# is flash the application. (Developers can reset DfuSE devices
150# manually to test mcuboot behavior on an otherwise erased flash
151# device.)
152macro(app_set_runner_args)
153 if(CONF_ZEPHYR_TRY_MASS_ERASE)
154 board_runner_args(dfu-util "--dfuse-modifiers=force:mass-erase")
155 board_runner_args(pyocd "--flashtool-opt=-ce")
156 endif()
157endmacro()
158
Marti Bolivarbf909a12017-11-13 19:43:46 -0500159# Standard Zephyr application boilerplate:
160# http://docs.zephyrproject.org/application/application.html
161include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
162project(NONE)
163
164# Path to "boot" subdirectory of repository root.
165get_filename_component(BOOT_DIR ${APPLICATION_SOURCE_DIR} DIRECTORY)
166# Path to top-level repository root directory.
167get_filename_component(MCUBOOT_DIR ${BOOT_DIR} DIRECTORY)
168# Path to tinycrypt library source subdirectory of MCUBOOT_DIR.
169set(TINYCRYPT_DIR "${MCUBOOT_DIR}/ext/tinycrypt/lib")
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200170# Path to mbed-tls' asn1 parser library.
171set(MBEDTLS_ASN1_DIR "${MCUBOOT_DIR}/ext/mbedtls")
Marti Bolivarbf909a12017-11-13 19:43:46 -0500172
173# Zephyr application include directories.
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200174if (NOT NEED_TINYCRYPT)
175 # Zephyr's mbedTLS needs this.
176 zephyr_include_directories(include)
177
178 # Use full mbedtls provided by OS for RSA
179 target_include_directories(app PRIVATE $ENV{ZEPHYR_BASE}/ext/lib/crypto/mbedtls/include)
180endif()
181
Marti Bolivarbf909a12017-11-13 19:43:46 -0500182target_include_directories(app PRIVATE include)
183target_include_directories(app PRIVATE targets)
184if(EXISTS "${APPLICATION_SOURCE_DIR}/targets/${BOARD}.h")
Marti Bolivar38845482018-01-25 17:51:40 -0500185 target_compile_definitions(app PRIVATE "-DMCUBOOT_TARGET_CONFIG=\"${BOARD}.h\"")
Marti Bolivarbf909a12017-11-13 19:43:46 -0500186endif()
187
188# Zephyr port-specific sources.
189target_sources(app PRIVATE main.c)
190target_sources(app PRIVATE flash_map.c)
191target_sources(app PRIVATE hal_flash.c)
192target_sources(app PRIVATE os.c)
193target_sources(app PRIVATE keys.c)
194if(NOT DEFINED CONFIG_FLASH_PAGE_LAYOUT)
195 target_sources(app PRIVATE flash_map_legacy.c)
196endif()
197
198# Generic bootutil sources and includes.
199target_include_directories(app PRIVATE "${BOOT_DIR}/bootutil/include")
200target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/loader.c")
201target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/bootutil_misc.c")
202target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/image_validate.c")
203target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/image_rsa.c")
204target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/image_ec256.c")
205target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/caps.c")
206
207# Tinycrypt sources and includes, if needed.
208if (NEED_TINYCRYPT)
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200209 target_include_directories(app PRIVATE "${BOOT_DIR}/zephyr/include")
Marti Bolivarbf909a12017-11-13 19:43:46 -0500210 target_include_directories(app PRIVATE "${TINYCRYPT_DIR}/include")
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200211 target_include_directories(app PRIVATE "${MBEDTLS_ASN1_DIR}/include")
Marti Bolivarbf909a12017-11-13 19:43:46 -0500212
213 target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/ecc.c")
214 target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/ecc_dsa.c")
215 target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/sha256.c")
216 target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/utils.c")
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200217
218 target_sources(app PRIVATE "${MBEDTLS_ASN1_DIR}/src/asn1parse.c")
Marti Bolivarbf909a12017-11-13 19:43:46 -0500219endif()
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200220
221if (CONFIG_MCUBOOT_SERIAL)
222zephyr_sources(${BOOT_DIR}/zephyr/serial_adapter.c)
223
224add_subdirectory(${BOOT_DIR}/boot_serial ./boot/boot_serial)
225add_subdirectory(${BOOT_DIR}/zephyr/tinycbor)
226add_subdirectory(${BOOT_DIR}/zephyr/cborattr)
227
228endif()