Marti Bolivar | bf909a1 | 2017-11-13 19:43:46 -0500 | [diff] [blame] | 1 | # 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 | |
| 7 | cmake_minimum_required(VERSION 3.8.2) |
| 8 | |
Marti Bolivar | dde1b1c | 2018-01-29 14:41:58 -0500 | [diff] [blame] | 9 | set(KCONFIG_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/Kconfig) |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 10 | |
Marti Bolivar | bf909a1 | 2017-11-13 19:43:46 -0500 | [diff] [blame] | 11 | ######################## |
| 12 | # Configuration choices. |
| 13 | ######################## |
| 14 | |
Marti Bolivar | bf909a1 | 2017-11-13 19:43:46 -0500 | [diff] [blame] | 15 | # If CONF_VALIDATE_SLOT0 is set, the bootloader attempts to validate |
| 16 | # the signature of slot0 every boot. This adds the signature check |
| 17 | # time to every boot, but can mitigate against some changes that are |
| 18 | # able to modify the flash image itself. |
| 19 | # |
| 20 | # To enable validation (this is the default): |
| 21 | # |
| 22 | # cmake -DCONF_VALIDATE_SLOT0=YES [...] |
| 23 | # |
| 24 | # To disable validation: |
| 25 | # |
| 26 | # cmake -DCONF_VALIDATE_SLOT0=NO [...] |
| 27 | if (NOT DEFINED CONF_VALIDATE_SLOT0) |
| 28 | set(CONF_VALIDATE_SLOT0 YES) |
| 29 | endif() |
| 30 | |
| 31 | # If CONF_UPGRADE_ONLY is set, overwrite slot0 with the upgrade image |
| 32 | # instead of swapping them. This prevents the fallback recovery, but |
| 33 | # uses a much simpler code path. |
| 34 | # |
| 35 | # To enable "upgrade only" mode: |
| 36 | # |
| 37 | # cmake -DCONF_UPGRADE_ONLY=YES [...] |
| 38 | # |
| 39 | # To disable "upgrade only" mode (this is the default): |
| 40 | # |
| 41 | # cmake -DCONF_UPGRADE_ONLY=NO [...] |
| 42 | if (NOT DEFINED CONF_UPGRADE_ONLY) |
| 43 | set(CONF_UPGRADE_ONLY NO) |
| 44 | endif() |
| 45 | |
Marti Bolivar | aefbd46 | 2017-12-15 03:43:46 -0500 | [diff] [blame] | 46 | # If CONF_ZEPHYR_TRY_MASS_ERASE is set (it is set by default), the |
| 47 | # Zephyr build system configuration attempts to force a mass erase |
| 48 | # before flashing. This ensures the scratch and other partitions are |
| 49 | # in a consistent state. |
| 50 | # |
| 51 | # This is not available for all boards. |
| 52 | # |
| 53 | # To enable the mass erase attempt (this is the default): |
| 54 | # |
| 55 | # cmake -DCONF_ZEPHYR_TRY_MASS_ERASE=YES [...] |
| 56 | # |
| 57 | # To disable the mass erase attempt: |
| 58 | # |
| 59 | # cmake -DCONF_ZEPHYR_TRY_MASS_ERASE=NO [...] |
| 60 | if (NOT DEFINED CONF_ZEPHYR_TRY_MASS_ERASE) |
| 61 | set(CONF_ZEPHYR_TRY_MASS_ERASE YES) |
| 62 | endif() |
| 63 | |
Marti Bolivar | bf909a1 | 2017-11-13 19:43:46 -0500 | [diff] [blame] | 64 | ############################## |
| 65 | # End of configuration blocks. |
| 66 | ############################## |
| 67 | |
| 68 | set(MCUBOOT_EXTRA_CFLAGS) |
| 69 | |
Marti Bolivar | bf909a1 | 2017-11-13 19:43:46 -0500 | [diff] [blame] | 70 | # Board-specific CONF_FILES should get merged into the build as well. |
| 71 | # |
| 72 | # Do this by defining the set_conf_file macro: |
| 73 | # http://docs.zephyrproject.org/application/application.html#application-configuration |
| 74 | macro(set_conf_file) |
| 75 | if (EXISTS ${APPLICATION_SOURCE_DIR}/boards/${BOARD}.conf) |
Marti Bolivar | a4818a5 | 2018-04-12 13:02:38 -0400 | [diff] [blame^] | 76 | set(CONF_FILE "prj.conf ${APPLICATION_SOURCE_DIR}/boards/${BOARD}.conf") |
Marti Bolivar | bf909a1 | 2017-11-13 19:43:46 -0500 | [diff] [blame] | 77 | else() |
Marti Bolivar | a4818a5 | 2018-04-12 13:02:38 -0400 | [diff] [blame^] | 78 | set(CONF_FILE prj.conf) |
Marti Bolivar | bf909a1 | 2017-11-13 19:43:46 -0500 | [diff] [blame] | 79 | endif() |
| 80 | endmacro() |
| 81 | |
| 82 | # Check if we need to validate slot 0. |
| 83 | if(CONF_VALIDATE_SLOT0 STREQUAL YES) |
| 84 | list(APPEND MCUBOOT_EXTRA_CFLAGS "-DMCUBOOT_VALIDATE_SLOT0") |
| 85 | endif() |
| 86 | |
| 87 | # Enabling this option uses newer flash map APIs. This saves RAM and |
| 88 | # avoids deprecated API usage. |
| 89 | # |
| 90 | # (This can be deleted when flash_area_to_sectors() is removed instead |
| 91 | # of simply deprecated.) |
| 92 | list(APPEND MCUBOOT_EXTRA_CFLAGS "-DMCUBOOT_USE_FLASH_AREA_GET_SECTORS") |
| 93 | |
| 94 | # Check if we're operating in overwrite-only mode. |
| 95 | if(CONF_UPGRADE_ONLY STREQUAL YES) |
| 96 | list (APPEND MCUBOOT_EXTRA_CFLAGS "-DMCUBOOT_OVERWRITE_ONLY" "-DMCUBOOT_OVERWRITE_ONLY_FAST") |
| 97 | endif() |
| 98 | |
| 99 | # Add values in MCUBOOT_EXTRA_CFLAGS_STR to the Zephyr build's |
| 100 | # EXTRA_CFLAGS variable. |
| 101 | string(REPLACE ";" " " MCUBOOT_EXTRA_CFLAGS_STR "${MCUBOOT_EXTRA_CFLAGS}") |
| 102 | set(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${MCUBOOT_EXTRA_CFLAGS_STR}") |
| 103 | |
| 104 | # The board should be set to a supported target. |
| 105 | if (NOT DEFINED BOARD) |
| 106 | set(BOARD qemu_x86) |
| 107 | endif() |
| 108 | |
Marti Bolivar | 58b321a | 2018-03-20 15:52:47 -0400 | [diff] [blame] | 109 | # Add a common dts overlay necessary to ensure mcuboot is linked into, |
| 110 | # and fits inside, the boot partition. (If the user specified a |
| 111 | # DTC_OVERLAY_FILE on the CMake command line, we need to append onto |
| 112 | # the list). |
| 113 | if(DTC_OVERLAY_FILE) |
| 114 | set(DTC_OVERLAY_FILE |
| 115 | "${DTC_OVERLAY_FILE} ${CMAKE_CURRENT_LIST_DIR}/dts.overlay") |
| 116 | else() |
| 117 | set(DTC_OVERLAY_FILE ${CMAKE_CURRENT_LIST_DIR}/dts.overlay) |
| 118 | endif() |
Marti Bolivar | bf909a1 | 2017-11-13 19:43:46 -0500 | [diff] [blame] | 119 | |
Marti Bolivar | aefbd46 | 2017-12-15 03:43:46 -0500 | [diff] [blame] | 120 | # Enable Zephyr runner options which request mass erase if so |
| 121 | # configured. |
| 122 | # |
| 123 | # Note that this also disables the default "leave" option when |
| 124 | # targeting STM32 DfuSe devices with dfu-util, making the chip stay in |
| 125 | # the bootloader after flashing. |
| 126 | # |
| 127 | # That's the right thing, because mcuboot has nothing to do since the |
| 128 | # chip was just erased. The next thing the user is going to want to do |
| 129 | # is flash the application. (Developers can reset DfuSE devices |
| 130 | # manually to test mcuboot behavior on an otherwise erased flash |
| 131 | # device.) |
| 132 | macro(app_set_runner_args) |
| 133 | if(CONF_ZEPHYR_TRY_MASS_ERASE) |
| 134 | board_runner_args(dfu-util "--dfuse-modifiers=force:mass-erase") |
| 135 | board_runner_args(pyocd "--flashtool-opt=-ce") |
| 136 | endif() |
| 137 | endmacro() |
| 138 | |
Marti Bolivar | bf909a1 | 2017-11-13 19:43:46 -0500 | [diff] [blame] | 139 | # Standard Zephyr application boilerplate: |
| 140 | # http://docs.zephyrproject.org/application/application.html |
| 141 | include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE) |
| 142 | project(NONE) |
| 143 | |
| 144 | # Path to "boot" subdirectory of repository root. |
| 145 | get_filename_component(BOOT_DIR ${APPLICATION_SOURCE_DIR} DIRECTORY) |
| 146 | # Path to top-level repository root directory. |
| 147 | get_filename_component(MCUBOOT_DIR ${BOOT_DIR} DIRECTORY) |
| 148 | # Path to tinycrypt library source subdirectory of MCUBOOT_DIR. |
| 149 | set(TINYCRYPT_DIR "${MCUBOOT_DIR}/ext/tinycrypt/lib") |
Fabio Utzig | 28ee5b0 | 2017-12-12 08:10:40 -0200 | [diff] [blame] | 150 | # Path to mbed-tls' asn1 parser library. |
| 151 | set(MBEDTLS_ASN1_DIR "${MCUBOOT_DIR}/ext/mbedtls") |
Marti Bolivar | bf909a1 | 2017-11-13 19:43:46 -0500 | [diff] [blame] | 152 | |
Marti Bolivar | bf909a1 | 2017-11-13 19:43:46 -0500 | [diff] [blame] | 153 | target_include_directories(app PRIVATE include) |
| 154 | target_include_directories(app PRIVATE targets) |
| 155 | if(EXISTS "${APPLICATION_SOURCE_DIR}/targets/${BOARD}.h") |
Marti Bolivar | 3884548 | 2018-01-25 17:51:40 -0500 | [diff] [blame] | 156 | target_compile_definitions(app PRIVATE "-DMCUBOOT_TARGET_CONFIG=\"${BOARD}.h\"") |
Marti Bolivar | bf909a1 | 2017-11-13 19:43:46 -0500 | [diff] [blame] | 157 | endif() |
| 158 | |
| 159 | # Zephyr port-specific sources. |
| 160 | target_sources(app PRIVATE main.c) |
| 161 | target_sources(app PRIVATE flash_map.c) |
| 162 | target_sources(app PRIVATE hal_flash.c) |
| 163 | target_sources(app PRIVATE os.c) |
| 164 | target_sources(app PRIVATE keys.c) |
| 165 | if(NOT DEFINED CONFIG_FLASH_PAGE_LAYOUT) |
| 166 | target_sources(app PRIVATE flash_map_legacy.c) |
| 167 | endif() |
| 168 | |
| 169 | # Generic bootutil sources and includes. |
| 170 | target_include_directories(app PRIVATE "${BOOT_DIR}/bootutil/include") |
| 171 | target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/loader.c") |
| 172 | target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/bootutil_misc.c") |
| 173 | target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/image_validate.c") |
| 174 | target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/image_rsa.c") |
| 175 | target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/image_ec256.c") |
| 176 | target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/caps.c") |
| 177 | |
Marti Bolivar | a4818a5 | 2018-04-12 13:02:38 -0400 | [diff] [blame^] | 178 | if(CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256) |
| 179 | # When using ECDSA signatures, pull in our copy of the tinycrypt library. |
Fabio Utzig | 28ee5b0 | 2017-12-12 08:10:40 -0200 | [diff] [blame] | 180 | target_include_directories(app PRIVATE "${BOOT_DIR}/zephyr/include") |
Marti Bolivar | bf909a1 | 2017-11-13 19:43:46 -0500 | [diff] [blame] | 181 | target_include_directories(app PRIVATE "${TINYCRYPT_DIR}/include") |
Fabio Utzig | 28ee5b0 | 2017-12-12 08:10:40 -0200 | [diff] [blame] | 182 | target_include_directories(app PRIVATE "${MBEDTLS_ASN1_DIR}/include") |
Marti Bolivar | bf909a1 | 2017-11-13 19:43:46 -0500 | [diff] [blame] | 183 | |
| 184 | target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/ecc.c") |
| 185 | target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/ecc_dsa.c") |
| 186 | target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/sha256.c") |
| 187 | target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/utils.c") |
Fabio Utzig | 28ee5b0 | 2017-12-12 08:10:40 -0200 | [diff] [blame] | 188 | |
Marti Bolivar | a4818a5 | 2018-04-12 13:02:38 -0400 | [diff] [blame^] | 189 | # Additionally pull in just the ASN.1 parser from mbedTLS. |
| 190 | target_compile_definitions(app PRIVATE MBEDTLS_CFG_FILE=config-asn1.h) |
Fabio Utzig | 28ee5b0 | 2017-12-12 08:10:40 -0200 | [diff] [blame] | 191 | target_sources(app PRIVATE "${MBEDTLS_ASN1_DIR}/src/asn1parse.c") |
Marti Bolivar | a4818a5 | 2018-04-12 13:02:38 -0400 | [diff] [blame^] | 192 | elseif(CONFIG_BOOT_SIGNATURE_TYPE_RSA) |
| 193 | # Use mbedTLS provided by Zephyr for RSA signatures. (Its config file |
| 194 | # is set using Kconfig.) |
| 195 | zephyr_include_directories(include) |
| 196 | target_include_directories(app PRIVATE $ENV{ZEPHYR_BASE}/ext/lib/crypto/mbedtls/include) |
Marti Bolivar | bf909a1 | 2017-11-13 19:43:46 -0500 | [diff] [blame] | 197 | endif() |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 198 | |
| 199 | if (CONFIG_MCUBOOT_SERIAL) |
| 200 | zephyr_sources(${BOOT_DIR}/zephyr/serial_adapter.c) |
| 201 | |
| 202 | add_subdirectory(${BOOT_DIR}/boot_serial ./boot/boot_serial) |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 203 | endif() |