blob: 5965f4ab1d796f484a735b111a8b24ea3a4fb307 [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 Bolivarbf909a12017-11-13 19:43:46 -05009# Board-specific CONF_FILES should get merged into the build as well.
10#
11# Do this by defining the set_conf_file macro:
12# http://docs.zephyrproject.org/application/application.html#application-configuration
13macro(set_conf_file)
14 if (EXISTS ${APPLICATION_SOURCE_DIR}/boards/${BOARD}.conf)
Marti Bolivara4818a52018-04-12 13:02:38 -040015 set(CONF_FILE "prj.conf ${APPLICATION_SOURCE_DIR}/boards/${BOARD}.conf")
Marti Bolivarbf909a12017-11-13 19:43:46 -050016 else()
Marti Bolivara4818a52018-04-12 13:02:38 -040017 set(CONF_FILE prj.conf)
Marti Bolivarbf909a12017-11-13 19:43:46 -050018 endif()
19endmacro()
20
Sebastian Bøeb94bda02019-01-22 12:33:18 +010021# Default to qemu_x86 if no board has been specified.
22set(BOARD qemu_x86)
Marti Bolivarbf909a12017-11-13 19:43:46 -050023
Marti Bolivar58b321a2018-03-20 15:52:47 -040024# Add a common dts overlay necessary to ensure mcuboot is linked into,
25# and fits inside, the boot partition. (If the user specified a
26# DTC_OVERLAY_FILE on the CMake command line, we need to append onto
27# the list).
28if(DTC_OVERLAY_FILE)
29 set(DTC_OVERLAY_FILE
30 "${DTC_OVERLAY_FILE} ${CMAKE_CURRENT_LIST_DIR}/dts.overlay")
31else()
32 set(DTC_OVERLAY_FILE ${CMAKE_CURRENT_LIST_DIR}/dts.overlay)
33endif()
Marti Bolivarbf909a12017-11-13 19:43:46 -050034
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +053035if (EXISTS ${CMAKE_CURRENT_LIST_DIR}/boards/${BOARD}.overlay)
36 set(DTC_OVERLAY_FILE
37 "${DTC_OVERLAY_FILE} ${CMAKE_CURRENT_LIST_DIR}/boards/${BOARD}.overlay")
38endif()
39
Marti Bolivaraefbd462017-12-15 03:43:46 -050040# Enable Zephyr runner options which request mass erase if so
41# configured.
42#
43# Note that this also disables the default "leave" option when
44# targeting STM32 DfuSe devices with dfu-util, making the chip stay in
45# the bootloader after flashing.
46#
47# That's the right thing, because mcuboot has nothing to do since the
48# chip was just erased. The next thing the user is going to want to do
49# is flash the application. (Developers can reset DfuSE devices
50# manually to test mcuboot behavior on an otherwise erased flash
51# device.)
52macro(app_set_runner_args)
Marti Bolivar53e2c262018-04-12 14:13:28 -040053 if(CONFIG_ZEPHYR_TRY_MASS_ERASE)
Marti Bolivaraefbd462017-12-15 03:43:46 -050054 board_runner_args(dfu-util "--dfuse-modifiers=force:mass-erase")
55 board_runner_args(pyocd "--flashtool-opt=-ce")
56 endif()
57endmacro()
58
Marti Bolivarbf909a12017-11-13 19:43:46 -050059# Standard Zephyr application boilerplate:
60# http://docs.zephyrproject.org/application/application.html
61include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
62project(NONE)
63
64# Path to "boot" subdirectory of repository root.
65get_filename_component(BOOT_DIR ${APPLICATION_SOURCE_DIR} DIRECTORY)
66# Path to top-level repository root directory.
67get_filename_component(MCUBOOT_DIR ${BOOT_DIR} DIRECTORY)
68# Path to tinycrypt library source subdirectory of MCUBOOT_DIR.
69set(TINYCRYPT_DIR "${MCUBOOT_DIR}/ext/tinycrypt/lib")
Fabio Utzig28ee5b02017-12-12 08:10:40 -020070# Path to mbed-tls' asn1 parser library.
71set(MBEDTLS_ASN1_DIR "${MCUBOOT_DIR}/ext/mbedtls")
Marti Bolivarbf909a12017-11-13 19:43:46 -050072
Sebastian Bøebe972172019-01-22 14:05:14 +010073zephyr_library_include_directories(
74 include
75 targets
76 )
77if(EXISTS targets/${BOARD}.h)
78 zephyr_library_compile_definitions(MCUBOOT_TARGET_CONFIG="${BOARD}.h")
Marti Bolivarbf909a12017-11-13 19:43:46 -050079endif()
80
81# Zephyr port-specific sources.
Sebastian Bøebe972172019-01-22 14:05:14 +010082zephyr_library_sources(
83 main.c
84 flash_map_extended.c
85 os.c
86 keys.c
87 )
88
Marti Bolivarbf909a12017-11-13 19:43:46 -050089if(NOT DEFINED CONFIG_FLASH_PAGE_LAYOUT)
Sebastian Bøebe972172019-01-22 14:05:14 +010090 zephyr_library_sources(
91 flash_map_legacy.c
92 )
Marti Bolivarbf909a12017-11-13 19:43:46 -050093endif()
94
95# Generic bootutil sources and includes.
Sebastian Bøebe972172019-01-22 14:05:14 +010096zephyr_library_include_directories(${BOOT_DIR}/bootutil/include)
97zephyr_library_sources(
98 ${BOOT_DIR}/bootutil/src/loader.c
99 ${BOOT_DIR}/bootutil/src/bootutil_misc.c
100 ${BOOT_DIR}/bootutil/src/image_validate.c
101 ${BOOT_DIR}/bootutil/src/encrypted.c
102 ${BOOT_DIR}/bootutil/src/image_rsa.c
103 ${BOOT_DIR}/bootutil/src/image_ec256.c
104 ${BOOT_DIR}/bootutil/src/caps.c
105 )
Marti Bolivarbf909a12017-11-13 19:43:46 -0500106
Marti Bolivara4818a52018-04-12 13:02:38 -0400107if(CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256)
108 # When using ECDSA signatures, pull in our copy of the tinycrypt library.
Sebastian Bøebe972172019-01-22 14:05:14 +0100109 zephyr_library_include_directories(
110 ${BOOT_DIR}/zephyr/include
111 ${TINYCRYPT_DIR}/include
112 ${MBEDTLS_ASN1_DIR}/include
113 )
Marti Bolivarbf909a12017-11-13 19:43:46 -0500114
Sebastian Bøebe972172019-01-22 14:05:14 +0100115 zephyr_library_sources(
116 ${TINYCRYPT_DIR}/source/ecc.c
117 ${TINYCRYPT_DIR}/source/ecc_dsa.c
118 ${TINYCRYPT_DIR}/source/sha256.c
119 ${TINYCRYPT_DIR}/source/utils.c
120
121 # Additionally pull in just the ASN.1 parser from mbedTLS.
122 ${MBEDTLS_ASN1_DIR}/src/asn1parse.c
123 ${MBEDTLS_ASN1_DIR}/src/platform_util.c
124 )
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200125
Ding Taof97cb712018-06-08 14:37:13 +0000126 # Since here we are not using Zephyr's mbedTLS but rather our own, we need
Carles Cufi69c61d02018-06-05 15:56:08 +0200127 # to set MBEDTLS_CONFIG_FILE ourselves. When using Zephyr's copy, this
128 # variable is set by its Kconfig in the Zephyr codebase.
Sebastian Bøebe972172019-01-22 14:05:14 +0100129 zephyr_library_compile_definitions(
130 MBEDTLS_CONFIG_FILE="${CMAKE_CURRENT_LIST_DIR}/include/mcuboot-mbedtls-cfg.h"
131 )
Marti Bolivara4818a52018-04-12 13:02:38 -0400132elseif(CONFIG_BOOT_SIGNATURE_TYPE_RSA)
133 # Use mbedTLS provided by Zephyr for RSA signatures. (Its config file
134 # is set using Kconfig.)
135 zephyr_include_directories(include)
Marti Bolivarbf909a12017-11-13 19:43:46 -0500136endif()
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200137
Sebastian Bøebe972172019-01-22 14:05:14 +0100138if(CONFIG_MCUBOOT_SERIAL)
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200139 zephyr_sources(${BOOT_DIR}/zephyr/serial_adapter.c)
140 zephyr_sources(${BOOT_DIR}/boot_serial/src/boot_serial.c)
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200141
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200142 zephyr_include_directories(${BOOT_DIR}/bootutil/include)
143 zephyr_include_directories(${BOOT_DIR}/boot_serial/include)
144 zephyr_include_directories(include)
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200145
Sebastian Bøebe972172019-01-22 14:05:14 +0100146 zephyr_link_libraries_ifdef(
147 CONFIG_TINYCBOR
148 TINYCBOR
149 )
150
151 zephyr_include_directories_ifdef(
152 CONFIG_BOOT_ERASE_PROGRESSIVELY
153 ${BOOT_DIR}/bootutil/src
154 )
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200155endif()
Fabio Utzigb1e0dc52018-04-26 10:53:19 -0300156
157if(NOT CONFIG_BOOT_SIGNATURE_KEY_FILE STREQUAL "")
158 if(IS_ABSOLUTE ${CONFIG_BOOT_SIGNATURE_KEY_FILE})
159 set(KEY_FILE ${CONFIG_BOOT_SIGNATURE_KEY_FILE})
160 else()
161 set(KEY_FILE ${MCUBOOT_DIR}/${CONFIG_BOOT_SIGNATURE_KEY_FILE})
162 endif()
163 set(GENERATED_PUBKEY ${ZEPHYR_BINARY_DIR}/autogen-pubkey.c)
164 add_custom_command(
165 OUTPUT ${GENERATED_PUBKEY}
166 COMMAND
167 ${PYTHON_EXECUTABLE}
168 ${MCUBOOT_DIR}/scripts/imgtool.py
169 getpub
170 -k
171 ${KEY_FILE}
172 > ${GENERATED_PUBKEY}
173 DEPENDS ${KEY_FILE}
174 )
Sebastian Bøebe972172019-01-22 14:05:14 +0100175 zephyr_library_sources(${GENERATED_PUBKEY})
Fabio Utzigb1e0dc52018-04-26 10:53:19 -0300176endif()