blob: 9a692796507f463e06d107cc222bc9136919faf7 [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# Board-specific CONF_FILES should get merged into the build as well.
12#
13# Do this by defining the set_conf_file macro:
14# http://docs.zephyrproject.org/application/application.html#application-configuration
15macro(set_conf_file)
16 if (EXISTS ${APPLICATION_SOURCE_DIR}/boards/${BOARD}.conf)
Marti Bolivara4818a52018-04-12 13:02:38 -040017 set(CONF_FILE "prj.conf ${APPLICATION_SOURCE_DIR}/boards/${BOARD}.conf")
Marti Bolivarbf909a12017-11-13 19:43:46 -050018 else()
Marti Bolivara4818a52018-04-12 13:02:38 -040019 set(CONF_FILE prj.conf)
Marti Bolivarbf909a12017-11-13 19:43:46 -050020 endif()
21endmacro()
22
Marti Bolivarbf909a12017-11-13 19:43:46 -050023# The board should be set to a supported target.
24if (NOT DEFINED BOARD)
25 set(BOARD qemu_x86)
26endif()
27
Marti Bolivar58b321a2018-03-20 15:52:47 -040028# Add a common dts overlay necessary to ensure mcuboot is linked into,
29# and fits inside, the boot partition. (If the user specified a
30# DTC_OVERLAY_FILE on the CMake command line, we need to append onto
31# the list).
32if(DTC_OVERLAY_FILE)
33 set(DTC_OVERLAY_FILE
34 "${DTC_OVERLAY_FILE} ${CMAKE_CURRENT_LIST_DIR}/dts.overlay")
35else()
36 set(DTC_OVERLAY_FILE ${CMAKE_CURRENT_LIST_DIR}/dts.overlay)
37endif()
Marti Bolivarbf909a12017-11-13 19:43:46 -050038
Marti Bolivaraefbd462017-12-15 03:43:46 -050039# Enable Zephyr runner options which request mass erase if so
40# configured.
41#
42# Note that this also disables the default "leave" option when
43# targeting STM32 DfuSe devices with dfu-util, making the chip stay in
44# the bootloader after flashing.
45#
46# That's the right thing, because mcuboot has nothing to do since the
47# chip was just erased. The next thing the user is going to want to do
48# is flash the application. (Developers can reset DfuSE devices
49# manually to test mcuboot behavior on an otherwise erased flash
50# device.)
51macro(app_set_runner_args)
Marti Bolivar53e2c262018-04-12 14:13:28 -040052 if(CONFIG_ZEPHYR_TRY_MASS_ERASE)
Marti Bolivaraefbd462017-12-15 03:43:46 -050053 board_runner_args(dfu-util "--dfuse-modifiers=force:mass-erase")
54 board_runner_args(pyocd "--flashtool-opt=-ce")
55 endif()
56endmacro()
57
Marti Bolivarbf909a12017-11-13 19:43:46 -050058# Standard Zephyr application boilerplate:
59# http://docs.zephyrproject.org/application/application.html
60include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
61project(NONE)
62
63# Path to "boot" subdirectory of repository root.
64get_filename_component(BOOT_DIR ${APPLICATION_SOURCE_DIR} DIRECTORY)
65# Path to top-level repository root directory.
66get_filename_component(MCUBOOT_DIR ${BOOT_DIR} DIRECTORY)
67# Path to tinycrypt library source subdirectory of MCUBOOT_DIR.
68set(TINYCRYPT_DIR "${MCUBOOT_DIR}/ext/tinycrypt/lib")
Fabio Utzig28ee5b02017-12-12 08:10:40 -020069# Path to mbed-tls' asn1 parser library.
70set(MBEDTLS_ASN1_DIR "${MCUBOOT_DIR}/ext/mbedtls")
Marti Bolivarbf909a12017-11-13 19:43:46 -050071
Marti Bolivarbf909a12017-11-13 19:43:46 -050072target_include_directories(app PRIVATE include)
73target_include_directories(app PRIVATE targets)
74if(EXISTS "${APPLICATION_SOURCE_DIR}/targets/${BOARD}.h")
Marti Bolivar38845482018-01-25 17:51:40 -050075 target_compile_definitions(app PRIVATE "-DMCUBOOT_TARGET_CONFIG=\"${BOARD}.h\"")
Marti Bolivarbf909a12017-11-13 19:43:46 -050076endif()
77
78# Zephyr port-specific sources.
79target_sources(app PRIVATE main.c)
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020080target_sources(app PRIVATE flash_map_extended.c)
Marti Bolivarbf909a12017-11-13 19:43:46 -050081target_sources(app PRIVATE os.c)
82target_sources(app PRIVATE keys.c)
83if(NOT DEFINED CONFIG_FLASH_PAGE_LAYOUT)
84 target_sources(app PRIVATE flash_map_legacy.c)
85endif()
86
87# Generic bootutil sources and includes.
88target_include_directories(app PRIVATE "${BOOT_DIR}/bootutil/include")
89target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/loader.c")
90target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/bootutil_misc.c")
91target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/image_validate.c")
92target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/image_rsa.c")
93target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/image_ec256.c")
94target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/caps.c")
95
Marti Bolivara4818a52018-04-12 13:02:38 -040096if(CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256)
97 # When using ECDSA signatures, pull in our copy of the tinycrypt library.
Fabio Utzig28ee5b02017-12-12 08:10:40 -020098 target_include_directories(app PRIVATE "${BOOT_DIR}/zephyr/include")
Marti Bolivarbf909a12017-11-13 19:43:46 -050099 target_include_directories(app PRIVATE "${TINYCRYPT_DIR}/include")
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200100 target_include_directories(app PRIVATE "${MBEDTLS_ASN1_DIR}/include")
Marti Bolivarbf909a12017-11-13 19:43:46 -0500101
102 target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/ecc.c")
103 target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/ecc_dsa.c")
104 target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/sha256.c")
105 target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/utils.c")
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200106
Carles Cufi69c61d02018-06-05 15:56:08 +0200107 # When building for ECDSA, we use our own copy of mbedTLS, so the Zephyr
108 # one must not be enabled or the MBEDTLS_CONFIG_FILE macros will collide.
109 if(DEFINED CONFIG_MBEDTLS)
110 message(FATAL_ERROR "\
111
112CONFIG_MBEDTLS should not be enabled. \
113Try adding CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256=y to your prj.conf, \
114delete the build folder and build from scratch.")
115 endif()
Ding Taof97cb712018-06-08 14:37:13 +0000116 # Since here we are not using Zephyr's mbedTLS but rather our own, we need
Carles Cufi69c61d02018-06-05 15:56:08 +0200117 # to set MBEDTLS_CONFIG_FILE ourselves. When using Zephyr's copy, this
118 # variable is set by its Kconfig in the Zephyr codebase.
119 target_compile_definitions(app PRIVATE MBEDTLS_CONFIG_FILE="${CMAKE_CURRENT_LIST_DIR}/include/mcuboot-mbedtls-cfg.h")
Marti Bolivara4818a52018-04-12 13:02:38 -0400120 # Additionally pull in just the ASN.1 parser from mbedTLS.
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200121 target_sources(app PRIVATE "${MBEDTLS_ASN1_DIR}/src/asn1parse.c")
Marti Bolivara4818a52018-04-12 13:02:38 -0400122elseif(CONFIG_BOOT_SIGNATURE_TYPE_RSA)
123 # Use mbedTLS provided by Zephyr for RSA signatures. (Its config file
124 # is set using Kconfig.)
125 zephyr_include_directories(include)
126 target_include_directories(app PRIVATE $ENV{ZEPHYR_BASE}/ext/lib/crypto/mbedtls/include)
Marti Bolivarbf909a12017-11-13 19:43:46 -0500127endif()
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200128
129if (CONFIG_MCUBOOT_SERIAL)
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200130 zephyr_sources(${BOOT_DIR}/zephyr/serial_adapter.c)
131 zephyr_sources(${BOOT_DIR}/boot_serial/src/boot_serial.c)
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200132
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200133 zephyr_include_directories(${BOOT_DIR}/bootutil/include)
134 zephyr_include_directories(${BOOT_DIR}/boot_serial/include)
135 zephyr_include_directories(include)
136 zephyr_link_libraries_ifdef(CONFIG_TINYCBOR TINYCBOR)
137
138 if (CONFIG_BOOT_ERASE_PROGRESSIVELY)
139 zephyr_include_directories(${BOOT_DIR}/bootutil/src)
140 endif()
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200141endif()
Fabio Utzigb1e0dc52018-04-26 10:53:19 -0300142
143if(NOT CONFIG_BOOT_SIGNATURE_KEY_FILE STREQUAL "")
144 if(IS_ABSOLUTE ${CONFIG_BOOT_SIGNATURE_KEY_FILE})
145 set(KEY_FILE ${CONFIG_BOOT_SIGNATURE_KEY_FILE})
146 else()
147 set(KEY_FILE ${MCUBOOT_DIR}/${CONFIG_BOOT_SIGNATURE_KEY_FILE})
148 endif()
149 set(GENERATED_PUBKEY ${ZEPHYR_BINARY_DIR}/autogen-pubkey.c)
150 add_custom_command(
151 OUTPUT ${GENERATED_PUBKEY}
152 COMMAND
153 ${PYTHON_EXECUTABLE}
154 ${MCUBOOT_DIR}/scripts/imgtool.py
155 getpub
156 -k
157 ${KEY_FILE}
158 > ${GENERATED_PUBKEY}
159 DEPENDS ${KEY_FILE}
160 )
161 target_sources(app PRIVATE "${GENERATED_PUBKEY}")
162endif()