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