blob: 2cc2aea78a9d96f3f6495f179e7c56a88e6dd006 [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
Marti Bolivarbf909a12017-11-13 19:43:46 -050015# If CONF_VALIDATE_SLOT0 is set, the bootloader attempts to validate
16# the signature of slot0 every boot. This adds the signature check
17# time to every boot, but can mitigate against some changes that are
18# able to modify the flash image itself.
19#
20# To enable validation (this is the default):
21#
22# cmake -DCONF_VALIDATE_SLOT0=YES [...]
23#
24# To disable validation:
25#
26# cmake -DCONF_VALIDATE_SLOT0=NO [...]
27if (NOT DEFINED CONF_VALIDATE_SLOT0)
28 set(CONF_VALIDATE_SLOT0 YES)
29endif()
30
31# If CONF_UPGRADE_ONLY is set, overwrite slot0 with the upgrade image
32# instead of swapping them. This prevents the fallback recovery, but
33# uses a much simpler code path.
34#
35# To enable "upgrade only" mode:
36#
37# cmake -DCONF_UPGRADE_ONLY=YES [...]
38#
39# To disable "upgrade only" mode (this is the default):
40#
41# cmake -DCONF_UPGRADE_ONLY=NO [...]
42if (NOT DEFINED CONF_UPGRADE_ONLY)
43 set(CONF_UPGRADE_ONLY NO)
44endif()
45
Marti Bolivaraefbd462017-12-15 03:43:46 -050046# If CONF_ZEPHYR_TRY_MASS_ERASE is set (it is set by default), the
47# Zephyr build system configuration attempts to force a mass erase
48# before flashing. This ensures the scratch and other partitions are
49# in a consistent state.
50#
51# This is not available for all boards.
52#
53# To enable the mass erase attempt (this is the default):
54#
55# cmake -DCONF_ZEPHYR_TRY_MASS_ERASE=YES [...]
56#
57# To disable the mass erase attempt:
58#
59# cmake -DCONF_ZEPHYR_TRY_MASS_ERASE=NO [...]
60if (NOT DEFINED CONF_ZEPHYR_TRY_MASS_ERASE)
61 set(CONF_ZEPHYR_TRY_MASS_ERASE YES)
62endif()
63
Marti Bolivarbf909a12017-11-13 19:43:46 -050064##############################
65# End of configuration blocks.
66##############################
67
68set(MCUBOOT_EXTRA_CFLAGS)
69
Marti Bolivarbf909a12017-11-13 19:43:46 -050070# Board-specific CONF_FILES should get merged into the build as well.
71#
72# Do this by defining the set_conf_file macro:
73# http://docs.zephyrproject.org/application/application.html#application-configuration
74macro(set_conf_file)
75 if (EXISTS ${APPLICATION_SOURCE_DIR}/boards/${BOARD}.conf)
Marti Bolivara4818a52018-04-12 13:02:38 -040076 set(CONF_FILE "prj.conf ${APPLICATION_SOURCE_DIR}/boards/${BOARD}.conf")
Marti Bolivarbf909a12017-11-13 19:43:46 -050077 else()
Marti Bolivara4818a52018-04-12 13:02:38 -040078 set(CONF_FILE prj.conf)
Marti Bolivarbf909a12017-11-13 19:43:46 -050079 endif()
80endmacro()
81
82# Check if we need to validate slot 0.
83if(CONF_VALIDATE_SLOT0 STREQUAL YES)
84 list(APPEND MCUBOOT_EXTRA_CFLAGS "-DMCUBOOT_VALIDATE_SLOT0")
85endif()
86
87# Enabling this option uses newer flash map APIs. This saves RAM and
88# avoids deprecated API usage.
89#
90# (This can be deleted when flash_area_to_sectors() is removed instead
91# of simply deprecated.)
92list(APPEND MCUBOOT_EXTRA_CFLAGS "-DMCUBOOT_USE_FLASH_AREA_GET_SECTORS")
93
94# Check if we're operating in overwrite-only mode.
95if(CONF_UPGRADE_ONLY STREQUAL YES)
96 list (APPEND MCUBOOT_EXTRA_CFLAGS "-DMCUBOOT_OVERWRITE_ONLY" "-DMCUBOOT_OVERWRITE_ONLY_FAST")
97endif()
98
99# Add values in MCUBOOT_EXTRA_CFLAGS_STR to the Zephyr build's
100# EXTRA_CFLAGS variable.
101string(REPLACE ";" " " MCUBOOT_EXTRA_CFLAGS_STR "${MCUBOOT_EXTRA_CFLAGS}")
102set(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${MCUBOOT_EXTRA_CFLAGS_STR}")
103
104# The board should be set to a supported target.
105if (NOT DEFINED BOARD)
106 set(BOARD qemu_x86)
107endif()
108
Marti Bolivar58b321a2018-03-20 15:52:47 -0400109# Add a common dts overlay necessary to ensure mcuboot is linked into,
110# and fits inside, the boot partition. (If the user specified a
111# DTC_OVERLAY_FILE on the CMake command line, we need to append onto
112# the list).
113if(DTC_OVERLAY_FILE)
114 set(DTC_OVERLAY_FILE
115 "${DTC_OVERLAY_FILE} ${CMAKE_CURRENT_LIST_DIR}/dts.overlay")
116else()
117 set(DTC_OVERLAY_FILE ${CMAKE_CURRENT_LIST_DIR}/dts.overlay)
118endif()
Marti Bolivarbf909a12017-11-13 19:43:46 -0500119
Marti Bolivaraefbd462017-12-15 03:43:46 -0500120# Enable Zephyr runner options which request mass erase if so
121# configured.
122#
123# Note that this also disables the default "leave" option when
124# targeting STM32 DfuSe devices with dfu-util, making the chip stay in
125# the bootloader after flashing.
126#
127# That's the right thing, because mcuboot has nothing to do since the
128# chip was just erased. The next thing the user is going to want to do
129# is flash the application. (Developers can reset DfuSE devices
130# manually to test mcuboot behavior on an otherwise erased flash
131# device.)
132macro(app_set_runner_args)
133 if(CONF_ZEPHYR_TRY_MASS_ERASE)
134 board_runner_args(dfu-util "--dfuse-modifiers=force:mass-erase")
135 board_runner_args(pyocd "--flashtool-opt=-ce")
136 endif()
137endmacro()
138
Marti Bolivarbf909a12017-11-13 19:43:46 -0500139# Standard Zephyr application boilerplate:
140# http://docs.zephyrproject.org/application/application.html
141include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
142project(NONE)
143
144# Path to "boot" subdirectory of repository root.
145get_filename_component(BOOT_DIR ${APPLICATION_SOURCE_DIR} DIRECTORY)
146# Path to top-level repository root directory.
147get_filename_component(MCUBOOT_DIR ${BOOT_DIR} DIRECTORY)
148# Path to tinycrypt library source subdirectory of MCUBOOT_DIR.
149set(TINYCRYPT_DIR "${MCUBOOT_DIR}/ext/tinycrypt/lib")
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200150# Path to mbed-tls' asn1 parser library.
151set(MBEDTLS_ASN1_DIR "${MCUBOOT_DIR}/ext/mbedtls")
Marti Bolivarbf909a12017-11-13 19:43:46 -0500152
Marti Bolivarbf909a12017-11-13 19:43:46 -0500153target_include_directories(app PRIVATE include)
154target_include_directories(app PRIVATE targets)
155if(EXISTS "${APPLICATION_SOURCE_DIR}/targets/${BOARD}.h")
Marti Bolivar38845482018-01-25 17:51:40 -0500156 target_compile_definitions(app PRIVATE "-DMCUBOOT_TARGET_CONFIG=\"${BOARD}.h\"")
Marti Bolivarbf909a12017-11-13 19:43:46 -0500157endif()
158
159# Zephyr port-specific sources.
160target_sources(app PRIVATE main.c)
161target_sources(app PRIVATE flash_map.c)
162target_sources(app PRIVATE hal_flash.c)
163target_sources(app PRIVATE os.c)
164target_sources(app PRIVATE keys.c)
165if(NOT DEFINED CONFIG_FLASH_PAGE_LAYOUT)
166 target_sources(app PRIVATE flash_map_legacy.c)
167endif()
168
169# Generic bootutil sources and includes.
170target_include_directories(app PRIVATE "${BOOT_DIR}/bootutil/include")
171target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/loader.c")
172target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/bootutil_misc.c")
173target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/image_validate.c")
174target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/image_rsa.c")
175target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/image_ec256.c")
176target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/caps.c")
177
Marti Bolivara4818a52018-04-12 13:02:38 -0400178if(CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256)
179 # When using ECDSA signatures, pull in our copy of the tinycrypt library.
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200180 target_include_directories(app PRIVATE "${BOOT_DIR}/zephyr/include")
Marti Bolivarbf909a12017-11-13 19:43:46 -0500181 target_include_directories(app PRIVATE "${TINYCRYPT_DIR}/include")
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200182 target_include_directories(app PRIVATE "${MBEDTLS_ASN1_DIR}/include")
Marti Bolivarbf909a12017-11-13 19:43:46 -0500183
184 target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/ecc.c")
185 target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/ecc_dsa.c")
186 target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/sha256.c")
187 target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/utils.c")
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200188
Marti Bolivara4818a52018-04-12 13:02:38 -0400189 # Additionally pull in just the ASN.1 parser from mbedTLS.
190 target_compile_definitions(app PRIVATE MBEDTLS_CFG_FILE=config-asn1.h)
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200191 target_sources(app PRIVATE "${MBEDTLS_ASN1_DIR}/src/asn1parse.c")
Marti Bolivara4818a52018-04-12 13:02:38 -0400192elseif(CONFIG_BOOT_SIGNATURE_TYPE_RSA)
193 # Use mbedTLS provided by Zephyr for RSA signatures. (Its config file
194 # is set using Kconfig.)
195 zephyr_include_directories(include)
196 target_include_directories(app PRIVATE $ENV{ZEPHYR_BASE}/ext/lib/crypto/mbedtls/include)
Marti Bolivarbf909a12017-11-13 19:43:46 -0500197endif()
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200198
199if (CONFIG_MCUBOOT_SERIAL)
200zephyr_sources(${BOOT_DIR}/zephyr/serial_adapter.c)
201
202add_subdirectory(${BOOT_DIR}/boot_serial ./boot/boot_serial)
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200203endif()