blob: bef546c0f9b39b034e455692995d6b0c4f8baf86 [file] [log] [blame]
Gyorgy Szingdb9783c2019-04-17 21:08:48 +02001##############################
2Trusted Firmware M secure boot
3##############################
4For secure devices it is security critical to enforce firmware authenticity to
5protect against execution of malicious software. This is implemented by building
6a trust chain where each step in the execution chain authenticates the next
7step before execution. The chain of trust in based on a "Root of Trust" which
8is implemented using asymmetric cryptography. The Root of Trust is a combination
9of an immutable bootloader and a public key (ROTPK).
10
11*******************************
12Second stage bootloader in TF-M
13*******************************
14To implement secure boot functionality an external project MCUBoot has been
15integrated 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
18information about MCUBoot modifications and how MCUBoot has been integrated to
19TF-M.
20
21Bootloader is started when CPU is released from reset. It runs in secure mode.
22It authenticates the firmware image by hash (SHA-256) and digital signature
23(RSA-2048) validation. Public key, that the checks happens against, is built
24into the bootloader image. Metadata of the image is delivered together with the
25image itself in a header and trailer section. In case of successful
26authentication, bootloader passes execution to the secure image. Execution never
27returns to bootloader until next reset.
28
29A default RSA key pair is stored in the repository, public key is in ``keys.c``
30and 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
35Private key must be stored in a safe place outside of the repository.
36``Imgtool.py`` can be used to generate new key pairs.
37
38The bootloader handles the secure and non-secure images as a single blob which
39is contiguous in the device memory. At compile time these images are
40concatenated and signed with RSA-2048 digital signature. Preparation of payload
41is done by Python scripts: ``bl2/ext/mcuboot/scripts/``. At the end of a
42successful build signed TF-M payload can be found in::
43
44 <build_dir>/install/outputs/fvp/tfm_sign.bin
45
46*********************
47Integration with TF-M
48*********************
49MCUBoot assumes a predefined memory layout which is described below (applicable
50for AN521). It is mandatory to define slot 0, slot 1 and scratch partitions, but
51their 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**************************
69Firmware upgrade operation
70**************************
71MCUBoot handles only the firmware authenticity check after start-up and the
72firmware switch part of the firmware update process. Downloading the new version
73of the firmware is out-of-scope for MCUBoot. MCUBoot supports two different ways
74to switch to the new firmware and it is assumed that firmware images are
75executed-in-place (XIP). The default behaviour is the image swapping. In this
76case active firmware is always executed from slot 0 and slot 1 is a staging area
77for new images. Before executing the new firmware image, the content of the two
78memory slots must be physically swapped. The other option is the non-swapping
79version, which eliminates the complexity of image swapping and its
80administration. Active image can be executed from either memory slot, but new
81firmware must be linked to the address space of the proper (currently inactive)
82memory slot.
83
84Swapping operation
85==================
86Active image is stored in slot 0, and this image is started always by the
87bootloader. Therefore images must be linked to slot 0. If the bootloader finds a
88valid image in slot 1, which is marked for upgrade, then contents of slot 0 and
89slot 1 will be swapped, before starting the new image from slot 0. Scratch area
90is used as a temporary storage place during image swapping. Update mark from
91slot 1 is removed when the swapping is successful. The boot loader can revert
92the swapping as a fall-back mechanism to recover the previous working firmware
93version after a faulty update. The swap operation is fail-safe and resistant to
94power-cut failures. For more details please refer to the MCUBoot
95`documentation <https://www.mcuboot.com/mcuboot/design.html>`__.
96
97Non-swapping operation
98======================
99This operation can be turned on with ``MCUBOOT_NO_SWAP`` compile time switch
100(see `Build time configuration`_). When enabling non-swapping operation then the
101active image flag is moved between slots during firmware upgrade. If firmware is
102executed-in-place (XIP), then two firmware images must be generated.
103One of them is linked to be executed from slot 0 memory region and the other
104from slot 1. The firmware upgrade client, which downloads the new image, must be
105aware, which slot hosts the active firmware and which acts as a staging area and
106it is responsible for downloading the proper firmware image. At boot time
107MCUBoot inspects the version number in the image header and passes execution to
108the newer firmware version. New image must be marked for upgrade which is
109automatically done by Python scripts at compile time. Image verification is done
110the same way in both operational modes. If new image fails during authentication
111then MCUBoot erases the memory slot and starts the other image, after successful
112authentication.
113
114At 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
121RAM Loading firmware upgrade
122============================
123Musca A1 supports an image upgrade mode that is separate to both the swapping
124and non-swapping modes. This is the ``RAM loading`` mode (please refer to the
125table below). Like the non-swapping mode, this selects the newest image by
126reading the image version numbers in the image headers, but instead of
127executing it in place, the newest image is copied to RAM for execution. The
128load address, the location in RAM where the image is copied to, is stored
129in the image header.
130
131Summary of different modes for image upgrade
132============================================
133Different implementations of the image upgrade operation (whether through
134swapping, non-swapping, or loading into RAM and executing from there) are
135supported by the platforms. The table below shows which of these
136modes 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************************
171Build time configuration
172************************
173MCUBoot related compile time switches can be set in the high level build
174configuration files::
175
176 ConfigDefault.cmake
177 ConfigCoreTest.cmake
178 ConfigRegression.cmake
179
180Compile 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
197Image versioning
198================
199An image version number is written to its header by one of the python scripts,
200and this number is used by the bootloader when the non-swapping mode is
201enabled.
202
203The version number of the image can manually be passed in through the command
204line in the cmake configuration step::
205
206 cmake -G"Unix Makefiles" -DTARGET_PLATFORM=AN521 -DCOMPILER=ARMCLANG -DIMAGE_VERSION=1.2.3+4 ../
207
208Alternatively, the version number can be less specific (e.g 1, 1.2, or 1.2.3),
209where the missing numbers are automatically set to zero. The image version
210number argument is optional, and if it is left out, then the version numbers of
211the image(s) being built in the same directory will automatically change. In
212this case, the last component (the build number) automatically increments from
213the 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
215and then an image version number is provided for the first time, the new number
216will take precedence and be used instead. All subsequent image versions are
217then set to the last number that has been specified, and the build number would
218stop incrementing. Any new version numbers that are provided will overwrite
219the previous one: 0.0.0+1 -> 0.0.0+2. Note: To re-apply automatic image
220versioning, please start a clean build without specifying the image version
221number at all.
222
223************************
224Testing firmware upgrade
225************************
226As downloading the new firmware image is out of scope for MCUBoot, the update
227process is started from a state where the original and the new image are already
228programmed to the appropriate memory slots. To generate the original and a new
229firmware package, TF-M is built twice with different build configurations.
230
231Swapping firmware upgrade
232=========================
233Run TF-M build twice with two different build configuration: default and
234regression. Save the artefacts between builds, because second run can overwrite
235original binaries. Download default build to slot 0 and regression build to
236slot 1.
237
238Executing 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
258Executing 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
273The 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
286Execute test suites for the secure storage service
287--------------------------------------------------
288Running Test Suite SST secure interface tests (TFM_SST_TEST_2XXX)....
289
290Non-swapping firmware upgrade
291=============================
292Follow the same instructions as in case of swapping build including these
293changes:
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
298Executing 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
319Executing 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
334Executing firmware upgrade on Musca-B1 board
335--------------------------------------------
336After two images have been built, they can be concatenated to create the
337combined image using ``srec_cat``:
338
339- Linux::
340 srec_cat bl2/ext/mcuboot/mcuboot.bin -Binary -offset 0xA000000 tfm_sign_0.bin -Binary -offset 0xA020000 tfm_sign_1.bin -Binary -offset 0xA0A0000 -o tfm.hex -Intel
341
342- Windows::
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 0xA0A0000 -o tfm.hex -Intel
344
345The following message will be shown in case of successful firmware upgrade,
346notice 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
358Execute test suites for the Secure area
359---------------------------------------
360Running Test Suite SST secure interface tests (TFM_SST_TEST_2XXX)...
361
362RAM loading firmware upgrade
363============================
364To enable RAM loading, please set ``MCUBOOT_RAM_LOADING`` to True (either in the
365configuration file or through the command line), and then specify a destination
366load 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,
368for example with Musca A1, its ``flash_layout.h`` file in the ``platform``
369folder should include ``#define IMAGE_LOAD_ADDRESS #0x10020000``
370
371Executing firmware upgrade on Musca-A1 board
372--------------------------------------------
373After two images have been built, they can be concatenated to create the
374combined 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
382The following message will be shown in case of successful firmware upgrade when,
383RAM 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.*