blob: d9f88839ca3a5c6e33ec5a531b79945ad8be6797 [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.
Sebastian Bøe8680b902018-01-29 10:34:43 +0100121set(DTC_OVERLAY_FILE ${CMAKE_CURRENT_LIST_DIR}/dts.overlay)
Marti Bolivarbf909a12017-11-13 19:43:46 -0500122
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")
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200134# Path to mbed-tls' asn1 parser library.
135set(MBEDTLS_ASN1_DIR "${MCUBOOT_DIR}/ext/mbedtls")
Marti Bolivarbf909a12017-11-13 19:43:46 -0500136
137# Zephyr application include directories.
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200138if (NOT NEED_TINYCRYPT)
139 # Zephyr's mbedTLS needs this.
140 zephyr_include_directories(include)
141
142 # Use full mbedtls provided by OS for RSA
143 target_include_directories(app PRIVATE $ENV{ZEPHYR_BASE}/ext/lib/crypto/mbedtls/include)
144endif()
145
Marti Bolivarbf909a12017-11-13 19:43:46 -0500146target_include_directories(app PRIVATE include)
147target_include_directories(app PRIVATE targets)
148if(EXISTS "${APPLICATION_SOURCE_DIR}/targets/${BOARD}.h")
149 target_compile_definitions(app PRIVATE "-DMCUBOOT_TARGET_CONFIG='\"${BOARD}.h\"'")
150endif()
151
152# Zephyr port-specific sources.
153target_sources(app PRIVATE main.c)
154target_sources(app PRIVATE flash_map.c)
155target_sources(app PRIVATE hal_flash.c)
156target_sources(app PRIVATE os.c)
157target_sources(app PRIVATE keys.c)
158if(NOT DEFINED CONFIG_FLASH_PAGE_LAYOUT)
159 target_sources(app PRIVATE flash_map_legacy.c)
160endif()
161
162# Generic bootutil sources and includes.
163target_include_directories(app PRIVATE "${BOOT_DIR}/bootutil/include")
164target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/loader.c")
165target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/bootutil_misc.c")
166target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/image_validate.c")
167target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/image_rsa.c")
168target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/image_ec256.c")
169target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/caps.c")
170
171# Tinycrypt sources and includes, if needed.
172if (NEED_TINYCRYPT)
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200173 target_include_directories(app PRIVATE "${BOOT_DIR}/zephyr/include")
Marti Bolivarbf909a12017-11-13 19:43:46 -0500174 target_include_directories(app PRIVATE "${TINYCRYPT_DIR}/include")
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200175 target_include_directories(app PRIVATE "${MBEDTLS_ASN1_DIR}/include")
Marti Bolivarbf909a12017-11-13 19:43:46 -0500176
177 target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/ecc.c")
178 target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/ecc_dsa.c")
179 target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/sha256.c")
180 target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/utils.c")
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200181
182 target_sources(app PRIVATE "${MBEDTLS_ASN1_DIR}/src/asn1parse.c")
Marti Bolivarbf909a12017-11-13 19:43:46 -0500183endif()
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200184
185if (CONFIG_MCUBOOT_SERIAL)
186zephyr_sources(${BOOT_DIR}/zephyr/serial_adapter.c)
187
188add_subdirectory(${BOOT_DIR}/boot_serial ./boot/boot_serial)
189add_subdirectory(${BOOT_DIR}/zephyr/tinycbor)
190add_subdirectory(${BOOT_DIR}/zephyr/cborattr)
191
192endif()