Skip to content

Commit 9ceb6f7

Browse files
committed
M2354: Refine TF-M/Mbed integration readme
Add adjusting Flash/SRAM memory spec
1 parent 30754bf commit 9ceb6f7

File tree

1 file changed

+134
-37
lines changed
  • targets/TARGET_NUVOTON/TARGET_M2354/TARGET_TFM/TARGET_NU_M2354/COMPONENT_TFM_S_FW

1 file changed

+134
-37
lines changed
Lines changed: 134 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,27 @@
1-
# TF-M exported stuff for Mbed integration
1+
# Rebuild TF-M and integrate with Mbed on M2354
22

3-
This directory contains TF-M exported stuff to integrate with Mbed.
3+
This document guides how to rebuild TF-M and integrate with Mbed on M2354.
44

5-
## TF-M exported stuff
5+
### Downloading TF-M source
66

7-
- bl2.bin: [MCUboot](https://github.com/mcu-tools/mcuboot) bootloader binary
7+
The M2354 port in TF-M must patch to enable TF-M integration with Mbed.
8+
For TF-M 1.2/Mbed integration on M2354, the [mainstream TF-M](https://git.trustedfirmware.org/TF-M/trusted-firmware-m.git) is patched as follows:
9+
- Apply Mbed-enabled patch to `nuvoton/m2354` TF-M target.
10+
- Backport to base on [ARMmbed TF-M mbed-tfm-1.2](https://github.com/ARMmbed/trusted-firmware-m/tree/mbed-tfm-1.2).
811

9-
- tfm_s.bin: TF-M secure binary
12+
Run the following command to fetch and switch to the intended version:
13+
```sh
14+
git clone https://github.com/OpenNuvoton/trusted-firmware-m -b nuvoton_mbed_m2354_tfm-1.2
15+
```
1016

11-
- s_veneers.o: TF-M secure gateway library
17+
## Customizing TF-M
1218

13-
- partition/: Flash layout for image signing and concatenating in post-build process
19+
TF-M is out of scope of Mbed.
20+
We only provide limited support for customizing M2354 port in TF-M.
1421

15-
**NOTE**: On import, `signing_layout_s_ns.o` is renamed to `signing_layout_preprocessed.h` for the following reasons:
16-
- Post-build script checks file name with `_s`/`_ns` to resolve `sw_type` as `SPE`/`NSPE` respectively.
17-
To recognize as `NSPE_SPE`, don't use `_s_ns`/`_ns_s` file name to avoid mis-recognized.
18-
- Use `.h` instead of `.c` as file extension name.
19-
This is to enable custom TF-M build where the locatioin of this directory can change elsewhere.
20-
In Greentea build process, `.c` file isn't but`.h` file is copied into `BUILD` directory, so that post-build script can still access the file.
21-
22-
- [signing_key/](signing_key/nuvoton_m2354-root-rsa-3072.md)
22+
### Default hardware partition
2323

24-
### Copy path summary
25-
26-
This section summarizes the copy paths from TF-M to Mbed.
27-
28-
- trusted-firmware-m/cmake_build/bin/bl2.bin → bl2.bin
29-
- trusted-firmware-m/cmake_build/install/export/tfm/lib/s_veneers.o → s_veneers.o
30-
- trusted-firmware-m/cmake_build/bin/tfm_s.bin → tfm_s.bin
31-
- trusted-firmware-m/platform/ext/target/nuvoton/m2354/partition/flash_layout.h → partition/flash_layout.h
32-
- trusted-firmware-m/cmake_build/bl2/ext/mcuboot/CMakeFiles/signing_layout_s.dir/signing_layout_s_ns.o → partition/signing_layout_preprocessed.h
33-
- trusted-firmware-m/bl2/ext/mcuboot/root-RSA-3072.pem → signing_key/nuvoton_m2354-root-rsa-3072.pem
34-
35-
## Partitioning the hardware
36-
37-
In TF-M, the hardware is partitioned as follows:
24+
In TF-M, by default, the M2354 hardware is partitioned as follows:
3825

3926
- Flash (1024KiB in total): 448KiB for secure and 576KiB for nonsecure.
4027
- SRAM (256KiB in total): 80KiB for secure and 176KiB for nonsecure.
@@ -51,16 +38,52 @@ In TF-M, the hardware is partitioned as follows:
5138
- **CRYPTO**: Configured to secure. Inaccessible to Mbed.
5239
- **TRNG**: Hardwired to secure. Accessible to Mbed through secure gateway.
5340

54-
## Building TF-M
41+
### Defining Flash for TF-M/Mbed
5542

56-
For Mbed/TF-M 1.2 integration, the mainstream TF-M is patched as follows:
57-
- Apply Mbed-enabled patch to `nuvoton/m2354` TF-M target.
58-
- Backport to base on [ARMmbed TF-M mbed-tfm-1.2](https://github.com/ARMmbed/trusted-firmware-m/tree/mbed-tfm-1.2).
43+
To define memory spec of Flash for TF-M/Mbed, search/change the line:
44+
45+
**trusted-firmware-m/platform/ext/target/nuvoton/m2354/partition/flash_layout.h**:
46+
```C
47+
/* Max Flash size for TF-M + bootloader information */
48+
#define FLASH_S_PARTITION_SIZE (0x50000)
49+
/* Max Flash size for Mbed + bootloader information */
50+
#define FLASH_NS_PARTITION_SIZE (0x90000)
51+
```
52+
53+
M2354 has 1024KiB Flash in total, among which 128KiB have been allocated for bootloader code and ITS/PS storage.
54+
896KiB are left for `FLASH_S_PARTITION_SIZE` and `FLASH_NS_PARTITION_SIZE`.
55+
56+
**NOTE**: `FLASH_S_PARTITION_SIZE` and `FLASH_NS_PARTITION_SIZE` must be sector size (2KiB)-aligned.
57+
58+
### Defining SRAM for TF-M/Mbed
59+
60+
To define memory spec of SRAM for TF-M/Mbed, search/change the line:
61+
62+
**trusted-firmware-m/platform/ext/target/nuvoton/m2354/partition/region_defs.h**:
63+
```C
64+
/* Max SRAM size for TF-M */
65+
#define S_DATA_SIZE (80 * 1024)
66+
/* Max SRAM size for Mbed = Total - Max SRAM size for TF-M */
67+
#define NS_DATA_SIZE (TOTAL_RAM_SIZE - S_DATA_SIZE)
68+
```
69+
70+
**NOTE**: `S_DATA_SIZE` and `NS_DATA_SIZE` must be 16KiB-aligned required by M2354 Security Configuration Unit (SCU).
71+
72+
## Rebuilding TF-M
73+
74+
### Environment
75+
76+
Navigate [TF-M](https://www.trustedfirmware.org/projects/tf-m/).
77+
Then go through **DOCS****Getting Started Guides****Software requirements** for TF-M build environment setup.
78+
79+
**NOTE**: For unknown reason, **GNU Arm Embedded Toolchain 10-2020-q4-major** built code **FAILS** to run. Avoid this toolchain version.
5980

60-
To re-build TF-M, run:
81+
### Compile
82+
83+
To compile TF-M on M2354, run:
6184

6285
```sh
63-
$ cmake -S . \
86+
cmake -S . \
6487
-B cmake_build \
6588
-DTFM_PLATFORM=nuvoton/m2354 \
6689
-DTFM_TOOLCHAIN_FILE=toolchain_GNUARM.cmake \
@@ -69,8 +92,82 @@ $ cmake -S . \
6992
-G"Unix Makefiles"
7093
```
7194

95+
Then:
96+
7297
```sh
73-
$ cmake --build cmake_build -- install
98+
cmake --build cmake_build -- install
99+
```
100+
101+
## Integrating with Mbed
102+
103+
### Updating TF-M exported stuff
104+
105+
The following TF-M exported stuffs must update into Mbed:
106+
107+
- bl2.bin: [MCUboot](https://github.com/mcu-tools/mcuboot) bootloader binary
108+
109+
- tfm_s.bin: TF-M secure binary
110+
111+
- s_veneers.o: TF-M secure gateway library
112+
113+
- partition/: Flash layout for image signing and concatenating in post-build process
114+
115+
**NOTE**: On import, `signing_layout_s_ns.o` is renamed to `signing_layout_preprocessed.h` for the following reasons:
116+
- Post-build script checks file name with `_s`/`_ns` to resolve `sw_type` as `SPE`/`NSPE` respectively.
117+
To recognize as `NSPE_SPE`, don't use `_s_ns`/`_ns_s` file name to avoid mis-recognized.
118+
- Use `.h` instead of `.c` as file extension name.
119+
This is to enable custom TF-M build where the locatioin of this directory can change elsewhere.
120+
In Greentea build process, `.c` file isn't but`.h` file is copied into `BUILD` directory, so that post-build script can still access the file.
121+
122+
- [signing_key/](signing_key/nuvoton_m2354-root-rsa-3072.md)
123+
124+
Below summarizes the copy paths from TF-M into Mbed:
125+
126+
- trusted-firmware-m/cmake_build/bin/bl2.bin → bl2.bin
127+
- trusted-firmware-m/cmake_build/install/export/tfm/lib/s_veneers.o → s_veneers.o
128+
- trusted-firmware-m/cmake_build/bin/tfm_s.bin → tfm_s.bin
129+
- trusted-firmware-m/platform/ext/target/nuvoton/m2354/partition/flash_layout.h → partition/flash_layout.h
130+
- trusted-firmware-m/cmake_build/bl2/ext/mcuboot/CMakeFiles/signing_layout_s.dir/signing_layout_s_ns.o → partition/signing_layout_preprocessed.h
131+
- trusted-firmware-m/bl2/ext/mcuboot/root-RSA-3072.pem → signing_key/nuvoton_m2354-root-rsa-3072.pem
132+
133+
### Updating TF-M defined Flash/SRAM
134+
135+
TF-M defines the memory spec of Flash/SRAM for TF-M/Mbed on M2354.
136+
Mbed needs to know its own memory spec of Flash/SRAM for its build.
137+
They can configure into Mbed via Mbed target configuration `mbed_rom_start` and friends:
138+
139+
**mbed-os/targets/targets.json**:
140+
```json
141+
"NU_M2354": {
142+
......
143+
"mbed_rom_start" : "<NS_CODE_START from TF-M>",
144+
"mbed_rom_size" : "<NS_CODE_SIZE from TF-M>",
145+
"mbed_ram_start" : "<NS_DATA_START from TF-M>",
146+
"mbed_ram_size" : "<NS_DATA_SIZE from TF-M>",
147+
......
148+
```
149+
150+
Where `NS_CODE_START` and friends are defined in the location:
151+
152+
**trusted-firmware-m/platform/ext/target/nuvoton/m2354/partition/region_defs.h**:
153+
```C
154+
#define NS_CODE_START (NS_ROM_ALIAS(NS_IMAGE_PRIMARY_AREA_OFFSET))
155+
#define NS_CODE_SIZE (IMAGE_NS_CODE_SIZE)
156+
#define NS_CODE_LIMIT (NS_CODE_START + NS_CODE_SIZE - 1)
157+
158+
#define NS_DATA_START (NS_RAM_ALIAS(S_DATA_SIZE))
159+
#define NS_DATA_SIZE (TOTAL_RAM_SIZE - S_DATA_SIZE)
160+
#define NS_DATA_LIMIT (NS_DATA_START + NS_DATA_SIZE - 1)
74161
```
75162

76-
**NOTE**: This patch has not been public yet.
163+
But due to conditional compile, these valuse are not clear until expanding.
164+
However, their expanded values can acquire in the location after TF-M build:
165+
166+
**trusted-firmware-m/cmake_build/bin/tfm_ns.map**:
167+
```
168+
Memory Configuration
169+
170+
Name Origin Length Attributes
171+
FLASH NS_CODE_START NS_CODE_SIZE xr
172+
RAM NS_DATA_START NS_DATA_SIZE xrw
173+
```

0 commit comments

Comments
 (0)