Gyorgy Szing | db9783c | 2019-04-17 21:08:48 +0200 | [diff] [blame] | 1 | ############################## |
| 2 | Trusted Firmware M secure boot |
| 3 | ############################## |
| 4 | For secure devices it is security critical to enforce firmware authenticity to |
| 5 | protect against execution of malicious software. This is implemented by building |
| 6 | a trust chain where each step in the execution chain authenticates the next |
| 7 | step before execution. The chain of trust in based on a "Root of Trust" which |
| 8 | is implemented using asymmetric cryptography. The Root of Trust is a combination |
| 9 | of an immutable bootloader and a public key (ROTPK). |
| 10 | |
| 11 | ******************************* |
| 12 | Second stage bootloader in TF-M |
| 13 | ******************************* |
| 14 | To implement secure boot functionality an external project MCUBoot has been |
| 15 | integrated to TF-M. For further information please refer to the |
| 16 | `MCUBoot homepage <https://www.mcuboot.com/>`__. Original source-code is available at |
| 17 | `GitHub <https://github.com/runtimeco/mcuboot>`__. This document contains |
| 18 | information about MCUBoot modifications and how MCUBoot has been integrated to |
| 19 | TF-M. |
| 20 | |
| 21 | Bootloader is started when CPU is released from reset. It runs in secure mode. |
| 22 | It authenticates the firmware image by hash (SHA-256) and digital signature |
| 23 | (RSA-2048) validation. Public key, that the checks happens against, is built |
| 24 | into the bootloader image. Metadata of the image is delivered together with the |
| 25 | image itself in a header and trailer section. In case of successful |
| 26 | authentication, bootloader passes execution to the secure image. Execution never |
| 27 | returns to bootloader until next reset. |
| 28 | |
| 29 | A default RSA key pair is stored in the repository, public key is in ``keys.c`` |
| 30 | and private key is in ``root-rsa-2048.pem``. |
| 31 | .. Warning:: |
| 32 | |
| 33 | ``DO NOT use them in production code, they are exclusively for testing!`` |
| 34 | |
| 35 | Private key must be stored in a safe place outside of the repository. |
| 36 | ``Imgtool.py`` can be used to generate new key pairs. |
| 37 | |
| 38 | The bootloader handles the secure and non-secure images as a single blob which |
| 39 | is contiguous in the device memory. At compile time these images are |
| 40 | concatenated and signed with RSA-2048 digital signature. Preparation of payload |
| 41 | is done by Python scripts: ``bl2/ext/mcuboot/scripts/``. At the end of a |
| 42 | successful build signed TF-M payload can be found in:: |
| 43 | |
| 44 | <build_dir>/install/outputs/fvp/tfm_sign.bin |
| 45 | |
| 46 | ********************* |
| 47 | Integration with TF-M |
| 48 | ********************* |
| 49 | MCUBoot assumes a predefined memory layout which is described below (applicable |
| 50 | for AN521). It is mandatory to define slot 0, slot 1 and scratch partitions, but |
| 51 | their size can be changed:: |
| 52 | |
| 53 | - 0x0000_0000 - 0x0007_FFFF: BL2 bootloader - MCUBoot |
| 54 | - 0x0008_0000 - 0x000F_FFFF: Slot 0 : Single binary blob: Secure + Non-Secure |
| 55 | image; Primary memory partition |
| 56 | - 0x0008_0000 - 0x0008_03FF: Common image header |
| 57 | - 0x0008_0400 - 0x0008_xxxx: Secure image |
| 58 | - 0x0008_xxxx - 0x0010_03FF: Padding (with 0xFF) |
| 59 | - 0x0010_0400 - 0x0010_xxxx: Non-secure image |
| 60 | - 0x0010_xxxx - 0x0010_xxxx: Hash value(SHA256) and RSA signature |
| 61 | of combined image |
| 62 | |
| 63 | - 0x0018_0000 - 0x0027_FFFF: Slot 1 : Secure + Non-Secure image; Secondary |
| 64 | memory partition, structured identically to slot |
| 65 | 0 |
| 66 | - 0x0028_0000 - 0x0037_FFFF: Scratch area, used during image swapping |
| 67 | |
| 68 | ************************** |
| 69 | Firmware upgrade operation |
| 70 | ************************** |
| 71 | MCUBoot handles only the firmware authenticity check after start-up and the |
| 72 | firmware switch part of the firmware update process. Downloading the new version |
| 73 | of the firmware is out-of-scope for MCUBoot. MCUBoot supports two different ways |
| 74 | to switch to the new firmware and it is assumed that firmware images are |
| 75 | executed-in-place (XIP). The default behaviour is the image swapping. In this |
| 76 | case active firmware is always executed from slot 0 and slot 1 is a staging area |
| 77 | for new images. Before executing the new firmware image, the content of the two |
| 78 | memory slots must be physically swapped. The other option is the non-swapping |
| 79 | version, which eliminates the complexity of image swapping and its |
| 80 | administration. Active image can be executed from either memory slot, but new |
| 81 | firmware must be linked to the address space of the proper (currently inactive) |
| 82 | memory slot. |
| 83 | |
| 84 | Swapping operation |
| 85 | ================== |
| 86 | Active image is stored in slot 0, and this image is started always by the |
| 87 | bootloader. Therefore images must be linked to slot 0. If the bootloader finds a |
| 88 | valid image in slot 1, which is marked for upgrade, then contents of slot 0 and |
| 89 | slot 1 will be swapped, before starting the new image from slot 0. Scratch area |
| 90 | is used as a temporary storage place during image swapping. Update mark from |
| 91 | slot 1 is removed when the swapping is successful. The boot loader can revert |
| 92 | the swapping as a fall-back mechanism to recover the previous working firmware |
| 93 | version after a faulty update. The swap operation is fail-safe and resistant to |
| 94 | power-cut failures. For more details please refer to the MCUBoot |
| 95 | `documentation <https://www.mcuboot.com/mcuboot/design.html>`__. |
| 96 | |
| 97 | Non-swapping operation |
| 98 | ====================== |
| 99 | This operation can be turned on with ``MCUBOOT_NO_SWAP`` compile time switch |
| 100 | (see `Build time configuration`_). When enabling non-swapping operation then the |
| 101 | active image flag is moved between slots during firmware upgrade. If firmware is |
| 102 | executed-in-place (XIP), then two firmware images must be generated. |
| 103 | One of them is linked to be executed from slot 0 memory region and the other |
| 104 | from slot 1. The firmware upgrade client, which downloads the new image, must be |
| 105 | aware, which slot hosts the active firmware and which acts as a staging area and |
| 106 | it is responsible for downloading the proper firmware image. At boot time |
| 107 | MCUBoot inspects the version number in the image header and passes execution to |
| 108 | the newer firmware version. New image must be marked for upgrade which is |
| 109 | automatically done by Python scripts at compile time. Image verification is done |
| 110 | the same way in both operational modes. If new image fails during authentication |
| 111 | then MCUBoot erases the memory slot and starts the other image, after successful |
| 112 | authentication. |
| 113 | |
| 114 | At build time automatically two binaries are generated:: |
| 115 | |
| 116 | <build_dir>/install/outputs/fvp/tfm_s_ns_signed_0.bin : Image linked for slot 0 |
| 117 | memory partition |
| 118 | <build_dir>/install/outputs/fvp/tfm_s_ns_signed_1.bin : Image linked for slot 1 |
| 119 | memory partition |
| 120 | |
| 121 | RAM Loading firmware upgrade |
| 122 | ============================ |
| 123 | Musca A1 supports an image upgrade mode that is separate to both the swapping |
| 124 | and non-swapping modes. This is the ``RAM loading`` mode (please refer to the |
| 125 | table below). Like the non-swapping mode, this selects the newest image by |
| 126 | reading the image version numbers in the image headers, but instead of |
| 127 | executing it in place, the newest image is copied to RAM for execution. The |
| 128 | load address, the location in RAM where the image is copied to, is stored |
| 129 | in the image header. |
| 130 | |
| 131 | Summary of different modes for image upgrade |
| 132 | ============================================ |
| 133 | Different implementations of the image upgrade operation (whether through |
| 134 | swapping, non-swapping, or loading into RAM and executing from there) are |
| 135 | supported by the platforms. The table below shows which of these |
| 136 | modes are supported by which platforms: |
| 137 | |
| 138 | +------------+-----------------+--------------+--------------+-----------------+ |
| 139 | | | Without BL2 [1]_| With BL2 [2]_| With BL2 [2]_| With BL2 [2]_ | |
| 140 | +============+=================+==============+==============+=================+ |
| 141 | | | XIP | XIP | XIP | Not XIP | |
| 142 | +------------+-----------------+--------------+--------------+-----------------+ |
| 143 | | | | Swap [3]_ | No-swap [4]_ | RAM loading [5]_| |
| 144 | +------------+-----------------+--------------+--------------+-----------------+ |
| 145 | | AN521 | Yes | Yes | Yes | No | |
| 146 | +------------+-----------------+--------------+--------------+-----------------+ |
| 147 | | AN519 | Yes | Yes | Yes | No | |
| 148 | +------------+-----------------+--------------+--------------+-----------------+ |
| 149 | | Musca-A1 | No | No | No | Yes | |
| 150 | +------------+-----------------+--------------+--------------+-----------------+ |
| 151 | | Musca-B1 | Yes | No | Yes | No | |
| 152 | +------------+-----------------+--------------+--------------+-----------------+ |
| 153 | |
| 154 | .. [1] To disable BL2, please turn off the ``BL2`` compiler switch in the |
| 155 | top-level configuration files or in the command line |
| 156 | |
| 157 | .. [2] BL2 is enabled by default |
| 158 | |
| 159 | .. [3] The image executes in-place (XIP) and is in swapping mode for image |
| 160 | update by default |
| 161 | |
| 162 | .. [4] To enable XIP No-swap, set the configuration variable ``MCUBOOT_NO_SWAP`` |
| 163 | to ``True`` in the top-level configuration files, or include the |
| 164 | ``MCUBOOT_NO_SWAP`` macro in the command line |
| 165 | |
| 166 | .. [5] To enable RAM loading, set the configuration variable |
| 167 | ``MCUBOOT_RAM_LOADING`` to ``True`` in the top-level configuration files, or |
| 168 | include the ``MCUBOOT_RAM_LOADING`` macro in the command line |
| 169 | |
| 170 | ************************ |
| 171 | Build time configuration |
| 172 | ************************ |
| 173 | MCUBoot related compile time switches can be set in the high level build |
| 174 | configuration files:: |
| 175 | |
| 176 | ConfigDefault.cmake |
| 177 | ConfigCoreTest.cmake |
| 178 | ConfigRegression.cmake |
| 179 | |
| 180 | Compile time switches: |
| 181 | |
| 182 | - BL2 (default: True): |
| 183 | - **True:** TF-M built together with bootloader. MCUBoot is executed after |
| 184 | reset and it authenticates TF-M and starts secure code. |
| 185 | - **False:** TF-M built without bootloader. Secure image linked to the |
| 186 | beginning of the device memory and executed after reset. If it is false |
| 187 | then using any of the further compile time switches are invalid. |
| 188 | - MCUBOOT_NO_SWAP (default: False): |
| 189 | - **True:** Activate non-swapping firmware upgrade operation. |
| 190 | - **False:** Original firmware upgrade operation with image swapping. |
| 191 | - MCUBOOT_RAM_LOADING (default: False): |
| 192 | - **True:** Activate RAM loading firmware upgrade operation, where latest |
| 193 | image is copied to RAM and runs from there instead of being executed |
| 194 | in-place. |
| 195 | - **False:** Original firmware upgrade operation with image swapping. |
| 196 | |
| 197 | Image versioning |
| 198 | ================ |
| 199 | An image version number is written to its header by one of the python scripts, |
| 200 | and this number is used by the bootloader when the non-swapping mode is |
| 201 | enabled. |
| 202 | |
| 203 | The version number of the image can manually be passed in through the command |
| 204 | line in the cmake configuration step:: |
| 205 | |
| 206 | cmake -G"Unix Makefiles" -DTARGET_PLATFORM=AN521 -DCOMPILER=ARMCLANG -DIMAGE_VERSION=1.2.3+4 ../ |
| 207 | |
| 208 | Alternatively, the version number can be less specific (e.g 1, 1.2, or 1.2.3), |
| 209 | where the missing numbers are automatically set to zero. The image version |
| 210 | number argument is optional, and if it is left out, then the version numbers of |
| 211 | the image(s) being built in the same directory will automatically change. In |
| 212 | this case, the last component (the build number) automatically increments from |
| 213 | the previous one: 0.0.0+1 -> 0.0.0+2, for as many times as the build is re-ran, |
| 214 | **until a number is explicitly provided**. If automatic versioning is in place |
| 215 | and then an image version number is provided for the first time, the new number |
| 216 | will take precedence and be used instead. All subsequent image versions are |
| 217 | then set to the last number that has been specified, and the build number would |
| 218 | stop incrementing. Any new version numbers that are provided will overwrite |
| 219 | the previous one: 0.0.0+1 -> 0.0.0+2. Note: To re-apply automatic image |
| 220 | versioning, please start a clean build without specifying the image version |
| 221 | number at all. |
| 222 | |
| 223 | ************************ |
| 224 | Testing firmware upgrade |
| 225 | ************************ |
| 226 | As downloading the new firmware image is out of scope for MCUBoot, the update |
| 227 | process is started from a state where the original and the new image are already |
| 228 | programmed to the appropriate memory slots. To generate the original and a new |
| 229 | firmware package, TF-M is built twice with different build configurations. |
| 230 | |
| 231 | Swapping firmware upgrade |
| 232 | ========================= |
| 233 | Run TF-M build twice with two different build configuration: default and |
| 234 | regression. Save the artefacts between builds, because second run can overwrite |
| 235 | original binaries. Download default build to slot 0 and regression build to |
| 236 | slot 1. |
| 237 | |
| 238 | Executing firmware upgrade on FVP_MPS2_AEMv8M |
| 239 | --------------------------------------------- |
| 240 | .. code-block:: bash |
| 241 | |
| 242 | <DS5_PATH>/sw/models/bin/FVP_MPS2_AEMv8M \ |
| 243 | --parameter fvp_mps2.platform_type=2 \ |
| 244 | --parameter cpu0.baseline=0 \ |
| 245 | --parameter cpu0.INITVTOR_S=0x10000000 \ |
| 246 | --parameter cpu0.semihosting-enable=0 \ |
| 247 | --parameter fvp_mps2.DISABLE_GATING=0 \ |
| 248 | --parameter fvp_mps2.telnetterminal0.start_telnet=1 \ |
| 249 | --parameter fvp_mps2.telnetterminal1.start_telnet=0 \ |
| 250 | --parameter fvp_mps2.telnetterminal2.start_telnet=0 \ |
| 251 | --parameter fvp_mps2.telnetterminal0.quiet=0 \ |
| 252 | --parameter fvp_mps2.telnetterminal1.quiet=1 \ |
| 253 | --parameter fvp_mps2.telnetterminal2.quiet=1 \ |
| 254 | --application cpu0=<build_dir>/bl2/ext/mcuboot/mcuboot.axf \ |
| 255 | --data cpu0=<default_build_dir>/install/outputs/fvp/tfm_s_ns_signed.bin@0x10080000 \ |
| 256 | --data cpu0=<regresssion_build_dir>/install/outputs/fvp/tfm_s_ns_signed.bin@0x10180000 |
| 257 | |
| 258 | Executing firmware upgrade on SSE 200 FPGA on MPS2 board |
| 259 | -------------------------------------------------------- |
| 260 | |
| 261 | :: |
| 262 | |
| 263 | TITLE: Versatile Express Images Configuration File |
| 264 | [IMAGES] |
| 265 | TOTALIMAGES: 3 ;Number of Images (Max: 32) |
| 266 | IMAGE0ADDRESS: 0x00000000 |
| 267 | IMAGE0FILE: \Software\mcuboot.axf ; BL2 bootloader |
| 268 | IMAGE1ADDRESS: 0x10080000 |
| 269 | IMAGE1FILE: \Software\tfm_sig1.bin ; TF-M example application binary blob |
| 270 | IMAGE2ADDRESS: 0x10180000 |
| 271 | IMAGE2FILE: \Software\tfm_sig2.bin ; TF-M regression test binary blob |
| 272 | |
| 273 | The following message will be shown in case of successful firmware upgrade, |
| 274 | ``Swap type: test`` indicates that images were swapped: |
| 275 | |
| 276 | :: |
| 277 | |
| 278 | [INF] Image 0: magic=good, copy_done=0xff, image_ok=0xff |
| 279 | [INF] Scratch: magic=bad, copy_done=0x5, image_ok=0xcf |
| 280 | [INF] Boot source: slot 0 |
| 281 | [INF] Swap type: test |
| 282 | [INF] Bootloader chainload address offset: 0x80000 |
| 283 | [INF] Jumping to the first image slot |
| 284 | [Sec Thread] Secure image initializing! |
| 285 | |
| 286 | Execute test suites for the secure storage service |
| 287 | -------------------------------------------------- |
| 288 | Running Test Suite SST secure interface tests (TFM_SST_TEST_2XXX).... |
| 289 | |
| 290 | Non-swapping firmware upgrade |
| 291 | ============================= |
| 292 | Follow the same instructions as in case of swapping build including these |
| 293 | changes: |
| 294 | |
| 295 | - Set MCUBOOT_NO_SWAP compile time switch to true before build. |
| 296 | - Increase the image version number between the two build run. |
| 297 | |
| 298 | Executing firmware upgrade on FVP_MPS2_AEMv8M |
| 299 | --------------------------------------------- |
| 300 | |
| 301 | .. code-block:: bash |
| 302 | |
| 303 | <DS5_PATH>/sw/models/bin/FVP_MPS2_AEMv8M \ |
| 304 | --parameter fvp_mps2.platform_type=2 \ |
| 305 | --parameter cpu0.baseline=0 \ |
| 306 | --parameter cpu0.INITVTOR_S=0x10000000 \ |
| 307 | --parameter cpu0.semihosting-enable=0 \ |
| 308 | --parameter fvp_mps2.DISABLE_GATING=0 \ |
| 309 | --parameter fvp_mps2.telnetterminal0.start_telnet=1 \ |
| 310 | --parameter fvp_mps2.telnetterminal1.start_telnet=0 \ |
| 311 | --parameter fvp_mps2.telnetterminal2.start_telnet=0 \ |
| 312 | --parameter fvp_mps2.telnetterminal0.quiet=0 \ |
| 313 | --parameter fvp_mps2.telnetterminal1.quiet=1 \ |
| 314 | --parameter fvp_mps2.telnetterminal2.quiet=1 \ |
| 315 | --application cpu0=<build_dir>/bl2/ext/mcuboot/mcuboot.axf \ |
| 316 | --data cpu0=<default_build_dir>/install/outputs/fvp/tfm_s_ns_signed_0.bin@0x10080000 \ |
| 317 | --data cpu0=<regresssion_build_dir>/install/outputs/fvp/tfm_s_ns_signed_1.bin@0x10180000 |
| 318 | |
| 319 | Executing firmware upgrade on SSE 200 FPGA on MPS2 board |
| 320 | -------------------------------------------------------- |
| 321 | |
| 322 | :: |
| 323 | |
| 324 | TITLE: Versatile Express Images Configuration File |
| 325 | [IMAGES] |
| 326 | TOTALIMAGES: 3 ;Number of Images (Max: 32) |
| 327 | IMAGE0ADDRESS: 0x00000000 |
| 328 | IMAGE0FILE: \Software\mcuboot.axf ; BL2 bootloader |
| 329 | IMAGE1ADDRESS: 0x10080000 |
| 330 | IMAGE1FILE: \Software\tfm_sig0.bin ; TF-M example application binary blob |
| 331 | IMAGE2ADDRESS: 0x10180000 |
| 332 | IMAGE2FILE: \Software\tfm_sig1.bin ; TF-M regression test binary blob |
| 333 | |
| 334 | Executing firmware upgrade on Musca-B1 board |
| 335 | -------------------------------------------- |
| 336 | After two images have been built, they can be concatenated to create the |
| 337 | combined image using ``srec_cat``: |
| 338 | |
| 339 | - Linux:: |
Kevin Peng | 1960b55 | 2019-05-06 16:30:49 +0800 | [diff] [blame] | 340 | srec_cat bl2/ext/mcuboot/mcuboot.bin -Binary -offset 0xA000000 tfm_sign_0.bin -Binary -offset 0xA020000 tfm_sign_1.bin -Binary -offset 0xA0E0000 -o tfm.hex -Intel |
Gyorgy Szing | db9783c | 2019-04-17 21:08:48 +0200 | [diff] [blame] | 341 | |
| 342 | - Windows:: |
Kevin Peng | 1960b55 | 2019-05-06 16:30:49 +0800 | [diff] [blame] | 343 | srec_cat.exe bl2\ext\mcuboot\mcuboot.bin -Binary -offset 0xA000000 tfm_sign_0.bin -Binary -offset 0xA020000 tfm_sign_1.bin -Binary -offset 0xA0E0000 -o tfm.hex -Intel |
Gyorgy Szing | db9783c | 2019-04-17 21:08:48 +0200 | [diff] [blame] | 344 | |
| 345 | The following message will be shown in case of successful firmware upgrade, |
| 346 | notice that image with higher version number (``version=1.2.3.5``) is executed: |
| 347 | |
| 348 | :: |
| 349 | |
| 350 | [INF] Starting bootloader |
| 351 | [INF] Image 0: version=1.2.3.4, magic= good, image_ok=0xff |
| 352 | [INF] Image 1: version=1.2.3.5, magic= good, image_ok=0xff |
| 353 | [INF] Booting image from slot 1 |
| 354 | [INF] Bootloader chainload address offset: 0x180000 |
| 355 | [INF] Jumping to the first image slot |
| 356 | [Sec Thread] Secure image initializing! |
| 357 | |
| 358 | Execute test suites for the Secure area |
| 359 | --------------------------------------- |
| 360 | Running Test Suite SST secure interface tests (TFM_SST_TEST_2XXX)... |
| 361 | |
| 362 | RAM loading firmware upgrade |
| 363 | ============================ |
| 364 | To enable RAM loading, please set ``MCUBOOT_RAM_LOADING`` to True (either in the |
| 365 | configuration file or through the command line), and then specify a destination |
| 366 | load address in RAM where the image can be copied to and executed from. The |
| 367 | ``IMAGE_LOAD_ADDRESS`` macro must be specified in the target dependent files, |
| 368 | for example with Musca A1, its ``flash_layout.h`` file in the ``platform`` |
| 369 | folder should include ``#define IMAGE_LOAD_ADDRESS #0x10020000`` |
| 370 | |
| 371 | Executing firmware upgrade on Musca-A1 board |
| 372 | -------------------------------------------- |
| 373 | After two images have been built, they can be concatenated to create the |
| 374 | combined image using ``srec_cat``: |
| 375 | |
| 376 | - Linux: |
| 377 | srec_cat bl2/ext/mcuboot/mcuboot.bin -Binary -offset 0x200000 tfm_sign_old.bin -Binary -offset 0x220000 tfm_sign_new.bin -Binary -offset 0x320000 -o tfm.hex -Intel |
| 378 | |
| 379 | - Windows:: |
| 380 | srec_cat.exe bl2\ext\mcuboot\mcuboot.bin -Binary -offset 0x200000 tfm_sign_old.bin -Binary -offset 0x220000 tfm_sign_new.bin -Binary -offset 0x320000 -o tfm.hex -Intel |
| 381 | |
| 382 | The following message will be shown in case of successful firmware upgrade when, |
| 383 | RAM loading is enabled, notice that image with higher version number |
| 384 | (``version=0.0.0.2``) is executed: |
| 385 | |
| 386 | :: |
| 387 | |
| 388 | [INF] Image 0: version=0.0.0.1, magic= good, image_ok=0xff |
| 389 | [INF] Image 1: version=0.0.0.2, magic= good, image_ok=0xff |
| 390 | [INF] Image has been copied from slot 1 in flash to SRAM address 0x10020000 |
| 391 | [INF] Booting image from SRAM at address 0x10020000 |
| 392 | [INF] Bootloader chainload address offset: 0x20000 |
| 393 | [INF] Jumping to the first image slot |
| 394 | [Sec Thread] Secure image initializing! |
| 395 | |
| 396 | -------------- |
| 397 | |
| 398 | *Copyright (c) 2018-2019, Arm Limited. All rights reserved.* |