blob: 6050e0d52f7822c2efd6edf49ff3fd34437cadd5 [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")
Marti Bolivar23e38532018-03-26 13:14:22 -040056 board_runner_args(nrfjprog "--erase")
Marti Bolivaraefbd462017-12-15 03:43:46 -050057 endif()
58endmacro()
59
Marti Bolivarbf909a12017-11-13 19:43:46 -050060# Standard Zephyr application boilerplate:
61# http://docs.zephyrproject.org/application/application.html
62include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
63project(NONE)
64
65# Path to "boot" subdirectory of repository root.
66get_filename_component(BOOT_DIR ${APPLICATION_SOURCE_DIR} DIRECTORY)
67# Path to top-level repository root directory.
68get_filename_component(MCUBOOT_DIR ${BOOT_DIR} DIRECTORY)
69# Path to tinycrypt library source subdirectory of MCUBOOT_DIR.
70set(TINYCRYPT_DIR "${MCUBOOT_DIR}/ext/tinycrypt/lib")
Fabio Utzig28ee5b02017-12-12 08:10:40 -020071# Path to mbed-tls' asn1 parser library.
72set(MBEDTLS_ASN1_DIR "${MCUBOOT_DIR}/ext/mbedtls")
Marti Bolivarbf909a12017-11-13 19:43:46 -050073
Sebastian Bøebe972172019-01-22 14:05:14 +010074zephyr_library_include_directories(
75 include
76 targets
77 )
78if(EXISTS targets/${BOARD}.h)
79 zephyr_library_compile_definitions(MCUBOOT_TARGET_CONFIG="${BOARD}.h")
Marti Bolivarbf909a12017-11-13 19:43:46 -050080endif()
81
82# Zephyr port-specific sources.
Sebastian Bøebe972172019-01-22 14:05:14 +010083zephyr_library_sources(
84 main.c
85 flash_map_extended.c
86 os.c
87 keys.c
88 )
89
Marti Bolivarbf909a12017-11-13 19:43:46 -050090if(NOT DEFINED CONFIG_FLASH_PAGE_LAYOUT)
Sebastian Bøebe972172019-01-22 14:05:14 +010091 zephyr_library_sources(
92 flash_map_legacy.c
93 )
Marti Bolivarbf909a12017-11-13 19:43:46 -050094endif()
95
96# Generic bootutil sources and includes.
Sebastian Bøebe972172019-01-22 14:05:14 +010097zephyr_library_include_directories(${BOOT_DIR}/bootutil/include)
98zephyr_library_sources(
99 ${BOOT_DIR}/bootutil/src/loader.c
100 ${BOOT_DIR}/bootutil/src/bootutil_misc.c
101 ${BOOT_DIR}/bootutil/src/image_validate.c
102 ${BOOT_DIR}/bootutil/src/encrypted.c
103 ${BOOT_DIR}/bootutil/src/image_rsa.c
104 ${BOOT_DIR}/bootutil/src/image_ec256.c
105 ${BOOT_DIR}/bootutil/src/caps.c
106 )
Marti Bolivarbf909a12017-11-13 19:43:46 -0500107
Marti Bolivara4818a52018-04-12 13:02:38 -0400108if(CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256)
109 # When using ECDSA signatures, pull in our copy of the tinycrypt library.
Sebastian Bøebe972172019-01-22 14:05:14 +0100110 zephyr_library_include_directories(
111 ${BOOT_DIR}/zephyr/include
112 ${TINYCRYPT_DIR}/include
113 ${MBEDTLS_ASN1_DIR}/include
114 )
Marti Bolivarbf909a12017-11-13 19:43:46 -0500115
Sebastian Bøebe972172019-01-22 14:05:14 +0100116 zephyr_library_sources(
117 ${TINYCRYPT_DIR}/source/ecc.c
118 ${TINYCRYPT_DIR}/source/ecc_dsa.c
119 ${TINYCRYPT_DIR}/source/sha256.c
120 ${TINYCRYPT_DIR}/source/utils.c
121
122 # Additionally pull in just the ASN.1 parser from mbedTLS.
123 ${MBEDTLS_ASN1_DIR}/src/asn1parse.c
124 ${MBEDTLS_ASN1_DIR}/src/platform_util.c
125 )
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200126
Ding Taof97cb712018-06-08 14:37:13 +0000127 # Since here we are not using Zephyr's mbedTLS but rather our own, we need
Carles Cufi69c61d02018-06-05 15:56:08 +0200128 # to set MBEDTLS_CONFIG_FILE ourselves. When using Zephyr's copy, this
129 # variable is set by its Kconfig in the Zephyr codebase.
Sebastian Bøebe972172019-01-22 14:05:14 +0100130 zephyr_library_compile_definitions(
131 MBEDTLS_CONFIG_FILE="${CMAKE_CURRENT_LIST_DIR}/include/mcuboot-mbedtls-cfg.h"
132 )
Marti Bolivara4818a52018-04-12 13:02:38 -0400133elseif(CONFIG_BOOT_SIGNATURE_TYPE_RSA)
134 # Use mbedTLS provided by Zephyr for RSA signatures. (Its config file
135 # is set using Kconfig.)
136 zephyr_include_directories(include)
Marti Bolivarbf909a12017-11-13 19:43:46 -0500137endif()
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200138
Sebastian Bøebe972172019-01-22 14:05:14 +0100139if(CONFIG_MCUBOOT_SERIAL)
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200140 zephyr_sources(${BOOT_DIR}/zephyr/serial_adapter.c)
141 zephyr_sources(${BOOT_DIR}/boot_serial/src/boot_serial.c)
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200142
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200143 zephyr_include_directories(${BOOT_DIR}/bootutil/include)
144 zephyr_include_directories(${BOOT_DIR}/boot_serial/include)
145 zephyr_include_directories(include)
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200146
Sebastian Bøebe972172019-01-22 14:05:14 +0100147 zephyr_link_libraries_ifdef(
148 CONFIG_TINYCBOR
149 TINYCBOR
150 )
151
152 zephyr_include_directories_ifdef(
153 CONFIG_BOOT_ERASE_PROGRESSIVELY
154 ${BOOT_DIR}/bootutil/src
155 )
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200156endif()
Fabio Utzigb1e0dc52018-04-26 10:53:19 -0300157
158if(NOT CONFIG_BOOT_SIGNATURE_KEY_FILE STREQUAL "")
159 if(IS_ABSOLUTE ${CONFIG_BOOT_SIGNATURE_KEY_FILE})
160 set(KEY_FILE ${CONFIG_BOOT_SIGNATURE_KEY_FILE})
161 else()
162 set(KEY_FILE ${MCUBOOT_DIR}/${CONFIG_BOOT_SIGNATURE_KEY_FILE})
163 endif()
164 set(GENERATED_PUBKEY ${ZEPHYR_BINARY_DIR}/autogen-pubkey.c)
165 add_custom_command(
166 OUTPUT ${GENERATED_PUBKEY}
167 COMMAND
168 ${PYTHON_EXECUTABLE}
169 ${MCUBOOT_DIR}/scripts/imgtool.py
170 getpub
171 -k
172 ${KEY_FILE}
173 > ${GENERATED_PUBKEY}
174 DEPENDS ${KEY_FILE}
175 )
Sebastian Bøebe972172019-01-22 14:05:14 +0100176 zephyr_library_sources(${GENERATED_PUBKEY})
Fabio Utzigb1e0dc52018-04-26 10:53:19 -0300177endif()