blob: e3034211740bd498509b353edbfbb0c1c759cf2d [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
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02009
10set(KCONFIG_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../Kconfig)
11
Marti Bolivarbf909a12017-11-13 19:43:46 -050012########################
13# Configuration choices.
14########################
15
16# Set CONF_SIGNATURE_TYPE to determine the signature type used.
17# Currently, it should be set to either RSA or ECDSA_P256.
18#
19# To choose RSA (this is the default):
20#
21# cmake -DCONF_SIGNATURE_TYPE=RSA [...]
22#
23# To use ECDSA_P256:
24#
25# cmake -DCONF_SIGNATURE_TYPE=ECDSA_P256 [...]
26if (NOT DEFINED CONF_SIGNATURE_TYPE)
27 set(CONF_SIGNATURE_TYPE RSA)
28endif()
29
30# If CONF_VALIDATE_SLOT0 is set, the bootloader attempts to validate
31# the signature of slot0 every boot. This adds the signature check
32# time to every boot, but can mitigate against some changes that are
33# able to modify the flash image itself.
34#
35# To enable validation (this is the default):
36#
37# cmake -DCONF_VALIDATE_SLOT0=YES [...]
38#
39# To disable validation:
40#
41# cmake -DCONF_VALIDATE_SLOT0=NO [...]
42if (NOT DEFINED CONF_VALIDATE_SLOT0)
43 set(CONF_VALIDATE_SLOT0 YES)
44endif()
45
46# If CONF_UPGRADE_ONLY is set, overwrite slot0 with the upgrade image
47# instead of swapping them. This prevents the fallback recovery, but
48# uses a much simpler code path.
49#
50# To enable "upgrade only" mode:
51#
52# cmake -DCONF_UPGRADE_ONLY=YES [...]
53#
54# To disable "upgrade only" mode (this is the default):
55#
56# cmake -DCONF_UPGRADE_ONLY=NO [...]
57if (NOT DEFINED CONF_UPGRADE_ONLY)
58 set(CONF_UPGRADE_ONLY NO)
59endif()
60
61##############################
62# End of configuration blocks.
63##############################
64
65set(MCUBOOT_EXTRA_CFLAGS)
66
67# Determine CFLAGS / MCUBOOT_CONF_FILE / NEED_TINYCRYPT from the signature type.
68if(CONF_SIGNATURE_TYPE STREQUAL RSA)
69 set(MCUBOOT_CONF_FILE prj.conf) # RSA
70 list(APPEND MCUBOOT_EXTRA_CFLAGS "-DMCUBOOT_SIGN_RSA" "-DMCUBOOT_USE_MBED_TLS")
71 set(NEED_TINYCRYPT NO)
72elseif(CONF_SIGNATURE_TYPE STREQUAL ECDSA_P256)
73 set(MCUBOOT_CONF_FILE prj-p256.conf) # ECDSA P-256
74 list(APPEND MCUBOOT_EXTRA_CFLAGS "-DMCUBOOT_SIGN_EC256" "-DMCUBOOT_USE_TINYCRYPT")
75 set(NEED_TINYCRYPT YES)
76else()
77 message(FATAL_ERROR "Invalid CONF_SIGNATURE_TYPE specified: '${CONF_SIGNATURE_TYPE}'")
78endif()
79
80# Board-specific CONF_FILES should get merged into the build as well.
81#
82# Do this by defining the set_conf_file macro:
83# http://docs.zephyrproject.org/application/application.html#application-configuration
84macro(set_conf_file)
85 if (EXISTS ${APPLICATION_SOURCE_DIR}/boards/${BOARD}.conf)
86 set(CONF_FILE "${MCUBOOT_CONF_FILE} ${APPLICATION_SOURCE_DIR}/boards/${BOARD}.conf")
87 else()
88 set(CONF_FILE "${MCUBOOT_CONF_FILE}")
89 endif()
90endmacro()
91
92# Check if we need to validate slot 0.
93if(CONF_VALIDATE_SLOT0 STREQUAL YES)
94 list(APPEND MCUBOOT_EXTRA_CFLAGS "-DMCUBOOT_VALIDATE_SLOT0")
95endif()
96
97# Enabling this option uses newer flash map APIs. This saves RAM and
98# avoids deprecated API usage.
99#
100# (This can be deleted when flash_area_to_sectors() is removed instead
101# of simply deprecated.)
102list(APPEND MCUBOOT_EXTRA_CFLAGS "-DMCUBOOT_USE_FLASH_AREA_GET_SECTORS")
103
104# Check if we're operating in overwrite-only mode.
105if(CONF_UPGRADE_ONLY STREQUAL YES)
106 list (APPEND MCUBOOT_EXTRA_CFLAGS "-DMCUBOOT_OVERWRITE_ONLY" "-DMCUBOOT_OVERWRITE_ONLY_FAST")
107endif()
108
109# Add values in MCUBOOT_EXTRA_CFLAGS_STR to the Zephyr build's
110# EXTRA_CFLAGS variable.
111string(REPLACE ";" " " MCUBOOT_EXTRA_CFLAGS_STR "${MCUBOOT_EXTRA_CFLAGS}")
112set(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${MCUBOOT_EXTRA_CFLAGS_STR}")
113
114# The board should be set to a supported target.
115if (NOT DEFINED BOARD)
116 set(BOARD qemu_x86)
117endif()
118
119# This is necessary to ensure mcuboot is linked into, and fits inside,
120# the boot partition.
121set(DTC_OVERLAY_FILE dts.overlay)
122
123# Standard Zephyr application boilerplate:
124# http://docs.zephyrproject.org/application/application.html
125include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
126project(NONE)
127
128# Path to "boot" subdirectory of repository root.
129get_filename_component(BOOT_DIR ${APPLICATION_SOURCE_DIR} DIRECTORY)
130# Path to top-level repository root directory.
131get_filename_component(MCUBOOT_DIR ${BOOT_DIR} DIRECTORY)
132# Path to tinycrypt library source subdirectory of MCUBOOT_DIR.
133set(TINYCRYPT_DIR "${MCUBOOT_DIR}/ext/tinycrypt/lib")
134
135# Zephyr's mbedTLS needs this.
136zephyr_include_directories(include)
137
138# Zephyr application include directories.
139target_include_directories(app PRIVATE $ENV{ZEPHYR_BASE}/ext/lib/crypto/mbedtls/include)
140target_include_directories(app PRIVATE include)
141target_include_directories(app PRIVATE targets)
142if(EXISTS "${APPLICATION_SOURCE_DIR}/targets/${BOARD}.h")
143 target_compile_definitions(app PRIVATE "-DMCUBOOT_TARGET_CONFIG='\"${BOARD}.h\"'")
144endif()
145
146# Zephyr port-specific sources.
147target_sources(app PRIVATE main.c)
148target_sources(app PRIVATE flash_map.c)
149target_sources(app PRIVATE hal_flash.c)
150target_sources(app PRIVATE os.c)
151target_sources(app PRIVATE keys.c)
152if(NOT DEFINED CONFIG_FLASH_PAGE_LAYOUT)
153 target_sources(app PRIVATE flash_map_legacy.c)
154endif()
155
156# Generic bootutil sources and includes.
157target_include_directories(app PRIVATE "${BOOT_DIR}/bootutil/include")
158target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/loader.c")
159target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/bootutil_misc.c")
160target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/image_validate.c")
161target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/image_rsa.c")
162target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/image_ec256.c")
163target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/caps.c")
164
165# Tinycrypt sources and includes, if needed.
166if (NEED_TINYCRYPT)
167 target_include_directories(app PRIVATE "${TINYCRYPT_DIR}/include")
168
169 target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/ecc.c")
170 target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/ecc_dsa.c")
171 target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/sha256.c")
172 target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/utils.c")
173endif()
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200174
175if (CONFIG_MCUBOOT_SERIAL)
176zephyr_sources(${BOOT_DIR}/zephyr/serial_adapter.c)
177
178add_subdirectory(${BOOT_DIR}/boot_serial ./boot/boot_serial)
179add_subdirectory(${BOOT_DIR}/zephyr/tinycbor)
180add_subdirectory(${BOOT_DIR}/zephyr/cborattr)
181
182endif()