blob: 075cd83d341ebd4fa0882dd1aeb78814a11d3393 [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########################
12# Configuration choices.
13########################
14
Marti Bolivaraefbd462017-12-15 03:43:46 -050015# If CONF_ZEPHYR_TRY_MASS_ERASE is set (it is set by default), the
16# Zephyr build system configuration attempts to force a mass erase
17# before flashing. This ensures the scratch and other partitions are
18# in a consistent state.
19#
20# This is not available for all boards.
21#
22# To enable the mass erase attempt (this is the default):
23#
24# cmake -DCONF_ZEPHYR_TRY_MASS_ERASE=YES [...]
25#
26# To disable the mass erase attempt:
27#
28# cmake -DCONF_ZEPHYR_TRY_MASS_ERASE=NO [...]
29if (NOT DEFINED CONF_ZEPHYR_TRY_MASS_ERASE)
30 set(CONF_ZEPHYR_TRY_MASS_ERASE YES)
31endif()
32
Marti Bolivarbf909a12017-11-13 19:43:46 -050033##############################
34# End of configuration blocks.
35##############################
36
37set(MCUBOOT_EXTRA_CFLAGS)
38
Marti Bolivarbf909a12017-11-13 19:43:46 -050039# Board-specific CONF_FILES should get merged into the build as well.
40#
41# Do this by defining the set_conf_file macro:
42# http://docs.zephyrproject.org/application/application.html#application-configuration
43macro(set_conf_file)
44 if (EXISTS ${APPLICATION_SOURCE_DIR}/boards/${BOARD}.conf)
Marti Bolivara4818a52018-04-12 13:02:38 -040045 set(CONF_FILE "prj.conf ${APPLICATION_SOURCE_DIR}/boards/${BOARD}.conf")
Marti Bolivarbf909a12017-11-13 19:43:46 -050046 else()
Marti Bolivara4818a52018-04-12 13:02:38 -040047 set(CONF_FILE prj.conf)
Marti Bolivarbf909a12017-11-13 19:43:46 -050048 endif()
49endmacro()
50
Marti Bolivarbf909a12017-11-13 19:43:46 -050051# Enabling this option uses newer flash map APIs. This saves RAM and
52# avoids deprecated API usage.
53#
54# (This can be deleted when flash_area_to_sectors() is removed instead
55# of simply deprecated.)
56list(APPEND MCUBOOT_EXTRA_CFLAGS "-DMCUBOOT_USE_FLASH_AREA_GET_SECTORS")
57
Marti Bolivarbf909a12017-11-13 19:43:46 -050058# Add values in MCUBOOT_EXTRA_CFLAGS_STR to the Zephyr build's
59# EXTRA_CFLAGS variable.
60string(REPLACE ";" " " MCUBOOT_EXTRA_CFLAGS_STR "${MCUBOOT_EXTRA_CFLAGS}")
61set(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${MCUBOOT_EXTRA_CFLAGS_STR}")
62
63# The board should be set to a supported target.
64if (NOT DEFINED BOARD)
65 set(BOARD qemu_x86)
66endif()
67
Marti Bolivar58b321a2018-03-20 15:52:47 -040068# Add a common dts overlay necessary to ensure mcuboot is linked into,
69# and fits inside, the boot partition. (If the user specified a
70# DTC_OVERLAY_FILE on the CMake command line, we need to append onto
71# the list).
72if(DTC_OVERLAY_FILE)
73 set(DTC_OVERLAY_FILE
74 "${DTC_OVERLAY_FILE} ${CMAKE_CURRENT_LIST_DIR}/dts.overlay")
75else()
76 set(DTC_OVERLAY_FILE ${CMAKE_CURRENT_LIST_DIR}/dts.overlay)
77endif()
Marti Bolivarbf909a12017-11-13 19:43:46 -050078
Marti Bolivaraefbd462017-12-15 03:43:46 -050079# Enable Zephyr runner options which request mass erase if so
80# configured.
81#
82# Note that this also disables the default "leave" option when
83# targeting STM32 DfuSe devices with dfu-util, making the chip stay in
84# the bootloader after flashing.
85#
86# That's the right thing, because mcuboot has nothing to do since the
87# chip was just erased. The next thing the user is going to want to do
88# is flash the application. (Developers can reset DfuSE devices
89# manually to test mcuboot behavior on an otherwise erased flash
90# device.)
91macro(app_set_runner_args)
92 if(CONF_ZEPHYR_TRY_MASS_ERASE)
93 board_runner_args(dfu-util "--dfuse-modifiers=force:mass-erase")
94 board_runner_args(pyocd "--flashtool-opt=-ce")
95 endif()
96endmacro()
97
Marti Bolivarbf909a12017-11-13 19:43:46 -050098# Standard Zephyr application boilerplate:
99# http://docs.zephyrproject.org/application/application.html
100include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
101project(NONE)
102
103# Path to "boot" subdirectory of repository root.
104get_filename_component(BOOT_DIR ${APPLICATION_SOURCE_DIR} DIRECTORY)
105# Path to top-level repository root directory.
106get_filename_component(MCUBOOT_DIR ${BOOT_DIR} DIRECTORY)
107# Path to tinycrypt library source subdirectory of MCUBOOT_DIR.
108set(TINYCRYPT_DIR "${MCUBOOT_DIR}/ext/tinycrypt/lib")
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200109# Path to mbed-tls' asn1 parser library.
110set(MBEDTLS_ASN1_DIR "${MCUBOOT_DIR}/ext/mbedtls")
Marti Bolivarbf909a12017-11-13 19:43:46 -0500111
Marti Bolivarbf909a12017-11-13 19:43:46 -0500112target_include_directories(app PRIVATE include)
113target_include_directories(app PRIVATE targets)
114if(EXISTS "${APPLICATION_SOURCE_DIR}/targets/${BOARD}.h")
Marti Bolivar38845482018-01-25 17:51:40 -0500115 target_compile_definitions(app PRIVATE "-DMCUBOOT_TARGET_CONFIG=\"${BOARD}.h\"")
Marti Bolivarbf909a12017-11-13 19:43:46 -0500116endif()
117
118# Zephyr port-specific sources.
119target_sources(app PRIVATE main.c)
120target_sources(app PRIVATE flash_map.c)
121target_sources(app PRIVATE hal_flash.c)
122target_sources(app PRIVATE os.c)
123target_sources(app PRIVATE keys.c)
124if(NOT DEFINED CONFIG_FLASH_PAGE_LAYOUT)
125 target_sources(app PRIVATE flash_map_legacy.c)
126endif()
127
128# Generic bootutil sources and includes.
129target_include_directories(app PRIVATE "${BOOT_DIR}/bootutil/include")
130target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/loader.c")
131target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/bootutil_misc.c")
132target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/image_validate.c")
133target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/image_rsa.c")
134target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/image_ec256.c")
135target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/caps.c")
136
Marti Bolivara4818a52018-04-12 13:02:38 -0400137if(CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256)
138 # When using ECDSA signatures, pull in our copy of the tinycrypt library.
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200139 target_include_directories(app PRIVATE "${BOOT_DIR}/zephyr/include")
Marti Bolivarbf909a12017-11-13 19:43:46 -0500140 target_include_directories(app PRIVATE "${TINYCRYPT_DIR}/include")
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200141 target_include_directories(app PRIVATE "${MBEDTLS_ASN1_DIR}/include")
Marti Bolivarbf909a12017-11-13 19:43:46 -0500142
143 target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/ecc.c")
144 target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/ecc_dsa.c")
145 target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/sha256.c")
146 target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/utils.c")
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200147
Marti Bolivara4818a52018-04-12 13:02:38 -0400148 # Additionally pull in just the ASN.1 parser from mbedTLS.
149 target_compile_definitions(app PRIVATE MBEDTLS_CFG_FILE=config-asn1.h)
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200150 target_sources(app PRIVATE "${MBEDTLS_ASN1_DIR}/src/asn1parse.c")
Marti Bolivara4818a52018-04-12 13:02:38 -0400151elseif(CONFIG_BOOT_SIGNATURE_TYPE_RSA)
152 # Use mbedTLS provided by Zephyr for RSA signatures. (Its config file
153 # is set using Kconfig.)
154 zephyr_include_directories(include)
155 target_include_directories(app PRIVATE $ENV{ZEPHYR_BASE}/ext/lib/crypto/mbedtls/include)
Marti Bolivarbf909a12017-11-13 19:43:46 -0500156endif()
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200157
158if (CONFIG_MCUBOOT_SERIAL)
159zephyr_sources(${BOOT_DIR}/zephyr/serial_adapter.c)
160
161add_subdirectory(${BOOT_DIR}/boot_serial ./boot/boot_serial)
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200162endif()