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