Skip to content

Commit 6ec7e7b

Browse files
theotherjimmymbolivar-nordic
authored andcommitted
samples: psa-firmware: Push firmware over psa_fwu_write
Previously, the example was a scheleton. This patch pushes firmware images with psa_fwu_write, and completes the firmware update example. Signed-off-by: Jimmy Brisson <[email protected]>
1 parent eacae5e commit 6ec7e7b

File tree

8 files changed

+3995
-71
lines changed

8 files changed

+3995
-71
lines changed

samples/tfm_integration/psa_firmware/CMakeLists.txt

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,108 @@
22

33
cmake_minimum_required(VERSION 3.13.1)
44

5+
if (NOT TFM_IMAGE_VERSION_NS)
6+
set(TFM_IMAGE_VERSION_NS 0.0.1+0)
7+
endif()
8+
59
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
610

711
project(tfm_psa_firmware)
812

13+
14+
if (NOT CONFIG_APP_FIRMWARE_UPDATE_IMAGE)
15+
message(FATAL_ERROR "CONFIG_APP_FIRMWARE_UPDATE_IMAGE required")
16+
endif()
17+
18+
# NOTE: These must not include ${CMAKE_BINARY_DIR} otherwise you get an
19+
# absolute path as part of the C symbols in the generated object file.
20+
# This is difficult to use in a correct and portable way. Instead, we will
21+
# take advantage of running everything from within the build directory.
22+
set(UPDATE_SIGNED_HEX update-signed.hex)
23+
set(UPDATE_BIN update-image.bin)
24+
set(UPDATE_OBJ update-image.o)
25+
set(UPDATE_HEADER_BIN update-header.bin)
26+
set(UPDATE_HEADER_OBJ update-header.o)
27+
28+
# The following sequence of add_custom_command calls builds a dependency
29+
# graph of all the bits we need to sign# an image. The process looks
30+
# something like:
31+
#
32+
# [(1) sample.hex ]
33+
# |
34+
# v
35+
# [(2) sign with imgtool ]
36+
# |
37+
# v
38+
# [(3) split-header.py ]
39+
# | |
40+
# app | | header
41+
# v v
42+
# [(4,5) objdump bin to obj ]
43+
# | |
44+
# app obj | | header obj
45+
# v v
46+
# [(6) target_sources(..) ]
47+
#
48+
# Note that node (1) is an input.
49+
50+
# This is duplicated from the trusted-firmware-m CMakeLists.txt, as this
51+
# needs it and CMAKE does not allow us to import the varibales from that
52+
# directory.
53+
set(TFM_MCUBOOT_DIR "${ZEPHYR_TRUSTED_FIRMWARE_M_MODULE_DIR}/bl2/ext/mcuboot")
54+
# Node (2) in the above graphic
55+
add_custom_command(
56+
DEPENDS ${CONFIG_APP_FIRMWARE_UPDATE_IMAGE}
57+
OUTPUT ${UPDATE_SIGNED_HEX}
58+
COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH=${ZEPHYR_MCUBOOT_MODULE_DIR}/scripts
59+
${PYTHON_EXECUTABLE}
60+
${TFM_MCUBOOT_DIR}/scripts/wrapper/wrapper.py
61+
--layout "${CMAKE_BINARY_DIR}/tfm/bl2/ext/mcuboot/CMakeFiles/signing_layout_ns.dir/signing_layout_ns.o"
62+
-k ${CONFIG_TFM_KEY_FILE_NS}
63+
--public-key-format "full"
64+
--align 1
65+
-v ${CONFIG_APP_FIRMWARE_UPDATE_IMAGE_VERSION}
66+
--pad
67+
-s auto
68+
-H ${CONFIG_ROM_START_OFFSET}
69+
${CONFIG_APP_FIRMWARE_UPDATE_IMAGE}
70+
${UPDATE_SIGNED_HEX}
71+
)
72+
73+
# Node (3) in the above graphic
74+
add_custom_command(
75+
OUTPUT ${UPDATE_HEADER_BIN}
76+
OUTPUT ${UPDATE_BIN}
77+
DEPENDS ${UPDATE_SIGNED_HEX}
78+
COMMAND ${PYTHON_EXECUTABLE}
79+
${CMAKE_CURRENT_LIST_DIR}/split-header.py
80+
${UPDATE_SIGNED_HEX}
81+
${UPDATE_BIN}
82+
${UPDATE_HEADER_BIN}
83+
)
84+
85+
# Node (4) in the above graphic
86+
add_custom_command(
87+
OUTPUT ${UPDATE_HEADER_OBJ}
88+
DEPENDS ${UPDATE_HEADER_BIN}
89+
COMMAND ${CMAKE_OBJCOPY} -I binary -O elf32-littlearm -B arm
90+
${UPDATE_HEADER_BIN}
91+
${UPDATE_HEADER_OBJ}
92+
)
93+
94+
# Node (5) in the above graphic
95+
add_custom_command(
96+
OUTPUT ${UPDATE_OBJ}
97+
DEPENDS ${UPDATE_BIN}
98+
COMMAND ${CMAKE_OBJCOPY} -I binary -O elf32-littlearm -B arm
99+
${UPDATE_BIN}
100+
${UPDATE_OBJ}
101+
)
102+
9103
# Source files in this sample
10-
target_sources(app PRIVATE src/main.c)
104+
# Node (6) in the above graphic
105+
target_sources(app PRIVATE src/main.c ${UPDATE_OBJ} ${UPDATE_HEADER_OBJ})
106+
107+
target_include_directories(app PRIVATE
108+
$<TARGET_PROPERTY:tfm,TFM_BINARY_DIR>/install/interface/include
109+
)

samples/tfm_integration/psa_firmware/Kconfig

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@ source "subsys/logging/Kconfig.template.log_config"
1313

1414
endmenu
1515

16-
config PSA_SHELL
17-
bool "Enable the 'psa' shell command"
18-
depends on SHELL
16+
config APP_FIRMWARE_UPDATE_IMAGE
17+
string "Firmware update image to update to"
1918
help
20-
Enabling this option will make the 'psa' shell command available.
19+
This required option specifies the path to an image that this
20+
exapmle will update to.
21+
default "$(shell, dirname $(filename))/boards/hello-an547.hex" if BOARD_MPS3_AN547
22+
23+
config APP_FIRMWARE_UPDATE_IMAGE_VERSION
24+
string "Version of the new image to update to"
25+
default "0.0.2+0"
2126

2227
source "Kconfig.zephyr"

samples/tfm_integration/psa_firmware/README.rst

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,29 @@ multi-thread application.
2323
Building and Running
2424
********************
2525

26+
This project needs another firmware as the update payload. It must use another
27+
example's hex file, and should be specified on the command line
28+
as ZEPHYR_FIRMWARE_UPDATE_SAMPLE_BUILD. For example, to use the sample
29+
`tfm_integration/tfm_ipc` as the payload:
30+
31+
.. code-block:: bash
32+
33+
cd <ZEPHYR_ROOT>
34+
west build -p -b lpcxpresso5s69_ns samples/tfm_integration/psa_firmware \
35+
-d build/lpcxpresso55s69_ns/tfm_integration/psa_firmware
36+
-- -DCONFIG_FIRMWARE_UPDATE_IMAGE=`realpath build/lpcxpresso55s69_ns/tfm_integration/tfm_ipc/zephyr_ns_signed.hex`
37+
38+
2639
This project outputs startup status and info to the console. It can be built and
2740
executed on an ARM Cortex M33 target board or QEMU.
2841

2942
This sample will only build on a Linux or macOS development system
3043
(not Windows), and has been tested on the following setups:
3144

3245
- macOS Big Sur using QEMU 6.0.0 with gcc-arm-none-eabi-9-2020-q2-update
46+
- Linux (NixOS) using QEMU 6.2.50 with gcc from Zephyr SDK 0.13.2
3347

34-
On MPS2+ AN521:
48+
On MPS3 AN547:
3549
===============
3650

3751
1. Build Zephyr with a non-secure configuration
@@ -42,7 +56,8 @@ On MPS2+ AN521:
4256
.. code-block:: bash
4357
4458
cd <ZEPHYR_ROOT>
45-
west build -p -b mps2_an521_ns samples/tfm_integration/psa_firmware
59+
west build -p -b mps3_an547_ns samples/tfm_integration/psa_firmware
60+
4661
4762
Using ``cmake`` and ``ninja``
4863

@@ -61,13 +76,13 @@ On MPS2+ AN521:
6176
cd <ZEPHYR_ROOT>/samples/tfm_integration/psa_firmware/
6277
rm -rf build
6378
mkdir build && cd build
64-
cmake -DBOARD=mps2_an521_ns ..
79+
cmake -DBOARD=mps3_an547_ns ..
6580
make
6681
6782
2. Copy application binary files (mcuboot.bin and tfm_sign.bin) to
68-
``<MPS2 device name>/SOFTWARE/``.
83+
``<MPS3 device name>/SOFTWARE/``.
6984

70-
3. Edit (e.g., with vim) the ``<MPS2 device name>/MB/HBI0263C/AN521/images.txt``
85+
3. Edit (e.g., with vim) the ``<MPS3 device name>/MB/HBI0263C/AN547/images.txt``
7186
file, and update it as shown below:
7287

7388
.. code-block:: bash
@@ -83,20 +98,20 @@ On MPS2+ AN521:
8398
IMAGE1ADDRESS: 0x10080000
8499
IMAGE1FILE: \SOFTWARE\tfm_sign.bin ; TF-M with application binary blob
85100
86-
4. Save the file, exit the editor, and reset the MPS2+ board.
101+
4. Save the file, exit the editor, and reset the MPS3 board.
87102

88103
On QEMU:
89104
========
90105

91-
Build Zephyr with a non-secure configuration (``-DBOARD=mps2_an521_ns``)
106+
Build Zephyr with a non-secure configuration (``-DBOARD=mps3_an547_ns``)
92107
and run it in qemu via the ``run`` command.
93108

94109
Using ``west``
95110

96111
.. code-block:: bash
97112
98113
cd <ZEPHYR_ROOT>
99-
west build -p -b mps2_an521_ns samples/tfm_integration/psa_firmware -t run
114+
west build -p -b mps3_an547_ns samples/tfm_integration/psa_firmware -t run
100115
101116
Using ``cmake`` and ``ninja``
102117

@@ -105,7 +120,7 @@ and run it in qemu via the ``run`` command.
105120
cd <ZEPHYR_ROOT>/samples/tfm_integration/psa_firmware/
106121
rm -rf build
107122
mkdir build && cd build
108-
cmake -GNinja -DBOARD=mps2_an521_ns ..
123+
cmake -GNinja -DBOARD=mps3_an547_ns ..
109124
ninja run
110125
111126
Using ``cmake`` and ``make``
@@ -115,7 +130,7 @@ and run it in qemu via the ``run`` command.
115130
cd <ZEPHYR_ROOT>/samples/tfm_integration/psa_firmware/
116131
rm -rf build
117132
mkdir build && cd build
118-
cmake -DBOARD=mps2_an521_ns ..
133+
cmake -DBOARD=mps3_an547_ns ..
119134
make run
120135
121136
On LPCxpresso55S69:
@@ -145,42 +160,28 @@ it's in an unknown state and can't be flashed.
145160

146161
We need to reset the board manually after flashing the image to run this code.
147162

148-
On nRF5340 and nRF9160:
149-
=======================
150-
151-
Build Zephyr with a non-secure configuration
152-
(``-DBOARD=nrf5340dk_nrf5340_cpuappns`` or ``-DBOARD=nrf9160dk_nrf9160ns``).
153-
154-
Example, for nRF9160, using ``cmake`` and ``ninja``
155-
156-
.. code-block:: bash
157-
158-
cd <ZEPHYR_ROOT>/samples/tfm_integration/psa_firmware/
159-
rm -rf build
160-
mkdir build && cd build
161-
cmake -GNinja -DBOARD=nrf9160dk_nrf9160ns ..
162-
163-
If building with BL2 (MCUboot bootloader) enabled, manually flash
164-
the MCUboot bootloader image binary (``bl2.hex``).
165-
166-
Example, using ``nrfjprog`` on nRF9160:
167-
168-
.. code-block:: bash
169-
170-
nrfjprg -f NRF91 --program tfm/bin/bl2.hex --sectorerase
171-
172-
Finally, flash the concatenated TF-M + Zephyr binary.
173-
174-
Example, for nRF9160, using ``cmake`` and ``ninja``
175-
176-
.. code-block:: bash
177-
178-
ninja flash
179-
180-
181163
Sample Output
182164
=============
183165

184166
.. code-block:: console
185167
186-
TODO!
168+
[INF] Beginning TF-M provisioning
169+
[WRN] TFM_DUMMY_PROVISIONING is not suitable for production! This device is NOT SECURE
170+
[Sec Thread] Secure image initializing!
171+
TF-M FP mode: Software
172+
Booting TFM v1.5.0
173+
Creating an empty ITS flash layout.
174+
Creating an empty PS flash layout.
175+
*** Booting Zephyr OS build v3.0.0-rc1-321-gbe26b6a260d6 ***
176+
PSA Firmware API test
177+
Active NS image version: 0.0.0-0
178+
Starting FWU; Writing Firmware from 21000000 size 58466 bytes
179+
Wrote Firmware; Writing Header from 2100e462 size 432 bytes
180+
Wrote Header; Installing Image
181+
Installed New Firmware; Reboot Needed; Rebooting
182+
[WRN] This device was provisioned with dummy keys. This device is NOT SECURE
183+
[Sec Thread] Secure image initializing!
184+
TF-M FP mode: Software
185+
Booting TFM v1.5.0
186+
*** Booting Zephyr OS build v3.0.0-rc1-35-g03f2993ef07b ***
187+
Hello World from UserSpace! mps3_an547

0 commit comments

Comments
 (0)