Skip to content

Commit 9fee7d9

Browse files
committed
M2354: Use TF-M exported region_defs.h to replace target.mbed_rom_start and friends
1 parent 7db4414 commit 9fee7d9

File tree

5 files changed

+222
-33
lines changed

5 files changed

+222
-33
lines changed

targets/TARGET_NUVOTON/TARGET_M2354/TARGET_TFM/TARGET_NU_M2354/COMPONENT_TFM_S_FW/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,15 @@ mbed_post_build_nuvoton_tfm_sign_image_tgt(
1313
${CMAKE_CURRENT_SOURCE_DIR}
1414
nuvoton_m2354-root-rsa-3072.pem
1515
)
16+
17+
set(bl2 true)
18+
set(mcuboot_image_number 1)
19+
set(region_defs_h_path "${CMAKE_CURRENT_SOURCE_DIR}/partition/region_defs.h")
20+
21+
target_compile_definitions(mbed-m2354-tfm
22+
INTERFACE
23+
NU_TFM_S_BL2=$<IF:$<BOOL:${bl2}>,1,0>
24+
NU_TFM_S_MCUBOOT_IMAGE_NUMBER=${mcuboot_image_number}
25+
# TODO: Fix escape sequence in NU_TFM_S_REGION_DEFS_H_PATH
26+
#NU_TFM_S_REGION_DEFS_H_PATH=\"${region_defs_h_path}\"
27+
)
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/*
2+
* @copyright SPDX-License-Identifier: Apache-2.0
3+
* Copyright (c) 2017-2020 Arm Limited. All rights reserved.
4+
* Copyright (c) 2020 Nuvoton Technology Corp. All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#ifndef __REGION_DEFS_H__
20+
#define __REGION_DEFS_H__
21+
22+
#include "flash_layout.h"
23+
24+
#define BL2_HEAP_SIZE (0x0001000)
25+
#define BL2_MSP_STACK_SIZE (0x0001800)
26+
27+
#define S_HEAP_SIZE (0x0001000)
28+
#define S_MSP_STACK_SIZE_INIT (0x0000400)
29+
#define S_MSP_STACK_SIZE (0x0000800)
30+
#define S_PSP_STACK_SIZE (0x0000800)
31+
32+
#define NS_HEAP_SIZE (0x0001000)
33+
#define NS_MSP_STACK_SIZE (0x0000800)
34+
#define NS_PSP_STACK_SIZE (0x0000800)
35+
36+
/* This size of buffer is big enough to store an attestation
37+
* token produced by initial attestation service
38+
*/
39+
#define PSA_INITIAL_ATTEST_TOKEN_MAX_SIZE (0x250)
40+
41+
/* MPC granularity is 128 KB on AN519 MPS2 FPGA image. Alignment
42+
* of partitions is defined in accordance with this constraint.
43+
*/
44+
45+
#ifdef BL2
46+
#ifndef LINK_TO_SECONDARY_PARTITION
47+
#define S_IMAGE_PRIMARY_PARTITION_OFFSET (FLASH_AREA_0_OFFSET)
48+
#define S_IMAGE_SECONDARY_PARTITION_OFFSET (FLASH_AREA_0_OFFSET)
49+
#else
50+
#error "No secondary partition supported!"
51+
#endif /* !LINK_TO_SECONDARY_PARTITION */
52+
#else
53+
#define S_IMAGE_PRIMARY_PARTITION_OFFSET (0x0)
54+
#endif /* BL2 */
55+
56+
#ifndef LINK_TO_SECONDARY_PARTITION
57+
#define NS_IMAGE_PRIMARY_PARTITION_OFFSET (FLASH_AREA_0_OFFSET + FLASH_S_PARTITION_SIZE)
58+
#else
59+
#error "No secondary partition supported!"
60+
#endif /* !LINK_TO_SECONDARY_PARTITION */
61+
62+
/* Boot partition structure if MCUBoot is used:
63+
* 0x0_0000 Bootloader header
64+
* 0x0_0400 Image area
65+
* 0x7_0000 Trailer
66+
*/
67+
/* IMAGE_CODE_SIZE is the space available for the software binary image.
68+
* It is less than the FLASH_S_PARTITION_SIZE + FLASH_NS_PARTITION_SIZE
69+
* because we reserve space for the image header and trailer introduced
70+
* by the bootloader.
71+
*/
72+
#ifdef BL2
73+
#define BL2_HEADER_SIZE (0x400) /* 1 KB */
74+
#define BL2_TRAILER_SIZE (0x800) /* 2 KB */
75+
#else
76+
/* No header if no bootloader, but keep IMAGE_CODE_SIZE the same */
77+
#define BL2_HEADER_SIZE (0x0)
78+
#define BL2_TRAILER_SIZE (0x800)
79+
#endif /* BL2 */
80+
81+
#define IMAGE_S_CODE_SIZE (FLASH_S_PARTITION_SIZE - BL2_HEADER_SIZE - BL2_TRAILER_SIZE)
82+
#define IMAGE_NS_CODE_SIZE (FLASH_NS_PARTITION_SIZE - BL2_HEADER_SIZE - BL2_TRAILER_SIZE)
83+
84+
#define CMSE_VENEER_REGION_SIZE (0x340)
85+
86+
/* Alias definitions for secure and non-secure areas*/
87+
#define S_ROM_ALIAS(x) (S_ROM_ALIAS_BASE + (x))
88+
#define NS_ROM_ALIAS(x) (NS_ROM_ALIAS_BASE + (x))
89+
90+
#define S_RAM_ALIAS(x) (S_RAM_ALIAS_BASE + (x))
91+
#define NS_RAM_ALIAS(x) (NS_RAM_ALIAS_BASE + (x))
92+
93+
/* Secure regions */
94+
#define S_IMAGE_PRIMARY_AREA_OFFSET \
95+
(S_IMAGE_PRIMARY_PARTITION_OFFSET + BL2_HEADER_SIZE) /* 0x8400 */
96+
#define S_CODE_START (S_ROM_ALIAS(S_IMAGE_PRIMARY_AREA_OFFSET)) /* 0x8400 */
97+
#define S_CODE_SIZE (IMAGE_S_CODE_SIZE - CMSE_VENEER_REGION_SIZE)
98+
#define S_CODE_LIMIT (S_CODE_START + S_CODE_SIZE - 1)
99+
100+
#define S_DATA_START (S_RAM_ALIAS(0x0))
101+
#define S_DATA_SIZE (80 * 1024)
102+
#define S_DATA_LIMIT (S_DATA_START + S_DATA_SIZE - 1)
103+
104+
/* CMSE Veneers region */
105+
#define CMSE_VENEER_REGION_START (S_CODE_LIMIT + 1)
106+
107+
/* Non-secure regions */
108+
#define NS_IMAGE_PRIMARY_AREA_OFFSET \
109+
(NS_IMAGE_PRIMARY_PARTITION_OFFSET + BL2_HEADER_SIZE)
110+
#define NS_CODE_START (NS_ROM_ALIAS(NS_IMAGE_PRIMARY_AREA_OFFSET))
111+
#define NS_CODE_SIZE (IMAGE_NS_CODE_SIZE)
112+
#define NS_CODE_LIMIT (NS_CODE_START + NS_CODE_SIZE - 1)
113+
114+
#define NS_DATA_START (NS_RAM_ALIAS(S_DATA_SIZE))
115+
#define NS_DATA_SIZE (TOTAL_RAM_SIZE - S_DATA_SIZE)
116+
#define NS_DATA_LIMIT (NS_DATA_START + NS_DATA_SIZE - 1)
117+
118+
/* NS partition information is used for MPC and SAU configuration */
119+
#define NS_PARTITION_START \
120+
(NS_ROM_ALIAS(NS_IMAGE_PRIMARY_PARTITION_OFFSET))
121+
#define NS_PARTITION_SIZE (FLASH_NS_PARTITION_SIZE)
122+
123+
/* Secondary partition for new images in case of firmware upgrade */
124+
#define SECONDARY_PARTITION_START \
125+
(NS_ROM_ALIAS(S_IMAGE_SECONDARY_PARTITION_OFFSET))
126+
#define SECONDARY_PARTITION_SIZE (FLASH_S_PARTITION_SIZE + \
127+
FLASH_NS_PARTITION_SIZE)
128+
129+
#ifdef BL2
130+
/* Bootloader regions */
131+
#define BL2_CODE_START (S_ROM_ALIAS(FLASH_AREA_BL2_OFFSET))
132+
#define BL2_CODE_SIZE (FLASH_AREA_BL2_SIZE)
133+
#define BL2_CODE_LIMIT (BL2_CODE_START + BL2_CODE_SIZE - 1)
134+
135+
#define BL2_DATA_START (S_RAM_ALIAS(0x0))
136+
#define BL2_DATA_SIZE (TOTAL_RAM_SIZE)
137+
#define BL2_DATA_LIMIT (BL2_DATA_START + BL2_DATA_SIZE - 1)
138+
#endif /* BL2 */
139+
140+
/* Shared data area between bootloader and runtime firmware.
141+
* Shared data area is allocated at the beginning of the RAM, it is overlapping
142+
* with TF-M Secure code's MSP stack
143+
*/
144+
#define BOOT_TFM_SHARED_DATA_BASE S_RAM_ALIAS_BASE
145+
#define BOOT_TFM_SHARED_DATA_SIZE (0x400)
146+
#define BOOT_TFM_SHARED_DATA_LIMIT (BOOT_TFM_SHARED_DATA_BASE + BOOT_TFM_SHARED_DATA_SIZE - 1)
147+
148+
#endif /* __REGION_DEFS_H__ */

targets/TARGET_NUVOTON/TARGET_M2354/device/partition_M2354_mem.h

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,55 @@
3333
* (already there or via copy).
3434
*/
3535

36-
/* Resolve non-secure ROM start */
37-
#ifndef MBED_ROM_START
38-
#error("MBED_ROM_START must be present!!!")
36+
/* Update NU_TFM_S_BL2 and friends on redoing TF-M import
37+
*
38+
* We expect NU_TFM_S_BL2 and friends are passed along from build tool, esp. Mbed CLI2,
39+
* If not, NU_TFM_S_BL2 and friends must update manually. */
40+
41+
#ifndef NU_TFM_S_BL2
42+
#define NU_TFM_S_BL2 1
3943
#endif
4044

41-
/* Resolve non-secure ROM size */
42-
#ifndef MBED_ROM_SIZE
43-
#error("MBED_ROM_SIZE must be present!!!")
45+
#ifndef NU_TFM_S_MCUBOOT_IMAGE_NUMBER
46+
#define NU_TFM_S_MCUBOOT_IMAGE_NUMBER 1
4447
#endif
4548

46-
/* Resolve non-secure RAM start */
47-
#ifndef MBED_RAM_START
48-
#error("MBED_RAM_START must be present!!!")
49+
#ifndef NU_TFM_S_REGION_DEFS_H_PATH
50+
#define NU_TFM_S_REGION_DEFS_H_PATH "../TARGET_TFM/TARGET_NU_M2354/COMPONENT_TFM_S_FW/partition/region_defs.h"
4951
#endif
5052

51-
/* Resolve non-secure RAM size */
52-
#ifndef MBED_RAM_SIZE
53-
#error("MBED_RAM_SIZE must be present!!!")
53+
/* TF-M exported region_defs.h depends on BL2 and MCUBOOT_IMAGE_NUMBER, so the
54+
* following order is significant. */
55+
#if NU_TFM_S_BL2
56+
#define BL2
5457
#endif
58+
#define MCUBOOT_IMAGE_NUMBER NU_TFM_S_MCUBOOT_IMAGE_NUMBER
59+
#include NU_TFM_S_REGION_DEFS_H_PATH
60+
61+
/* Avoid polluting name space, esp. BL2 */
62+
#undef BL2
63+
#undef MCUBOOT_IMAGE_NUMBER
64+
65+
/* Resolve MBED_ROM_START and friends
66+
*
67+
* TF-M exported region_defs.h essentially resolves MBED_ROM_START and friends.
68+
* target.mbed_rom_start and friends get unnecessary.
69+
*/
70+
/* Resolve non-secure ROM start */
71+
#undef MBED_ROM_START
72+
#define MBED_ROM_START NS_CODE_START
73+
74+
/* Resolve non-secure ROM size */
75+
#undef MBED_ROM_SIZE
76+
#define MBED_ROM_SIZE NS_CODE_SIZE
77+
78+
/* Resolve non-secure RAM start */
79+
#undef MBED_RAM_START
80+
#define MBED_RAM_START NS_DATA_START
81+
82+
/* Resolve non-secure RAM size */
83+
#undef MBED_RAM_SIZE
84+
#define MBED_RAM_SIZE NS_DATA_SIZE
5585

5686
/* Mbed build tool passes just APPLICATION_xxx macros to C/C++ files and just
5787
* MBED_APP_xxx macros to linker files even though they mean the same thing.

targets/TARGET_NUVOTON/TARGET_M2354/device/partition_M2354_mem.icf.h

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,28 @@
1818

1919
/* See partition_M2354_mem.h for documentation */
2020

21+
/* Update MBED_ROM_START and friends on redoing TF-M import (for IAR)
22+
*
23+
* IAR doesn't support CPP, so we cannot resolve MBED_ROM_START and friends by
24+
* TF-M exported region_defs.h.
25+
*
26+
* Without target.mbed_rom_start and friends (replaced with TF-M exported region_defs.h),
27+
* MBED_ROM_START and friends passed along are incorrect. Because IAR doesn't allow symbol
28+
* redefinition, we change to MBED_ROM_START_ORIDE and friends instead for a temporary
29+
* solution.
30+
*/
31+
2132
/* Resolve non-secure ROM start */
22-
if (! isdefinedsymbol(MBED_ROM_START)) {
23-
error "MBED_ROM_START must be present!!!";
24-
}
33+
define symbol MBED_ROM_START_ORIDE = 0x10070400;
2534

2635
/* Resolve non-secure ROM size */
27-
if (! isdefinedsymbol(MBED_ROM_SIZE)) {
28-
error "MBED_ROM_SIZE must be present!!!";
29-
}
36+
define symbol MBED_ROM_SIZE_ORIDE = 0x8F400;
3037

3138
/* Resolve non-secure RAM start */
32-
if (! isdefinedsymbol(MBED_RAM_START)) {
33-
error "MBED_RAM_START must be present!!!";
34-
}
39+
define symbol MBED_RAM_START_ORIDE = 0x30014000;
3540

3641
/* Resolve non-secure RAM size */
37-
if (! isdefinedsymbol(MBED_RAM_SIZE)) {
38-
error "MBED_RAM_SIZE must be present!!!";
39-
}
42+
define symbol MBED_RAM_SIZE_ORIDE = 0x2C000;
4043

4144
/* Mbed build tool passes just APPLICATION_xxx macros to C/C++ files and just
4245
* MBED_APP_xxx macros to linker files even though they mean the same thing.
@@ -48,31 +51,31 @@ if (!isdefinedsymbol(APPLICATION_ADDR)) {
4851
if (isdefinedsymbol(MBED_APP_START)) {
4952
define symbol APPLICATION_ADDR = MBED_APP_START;
5053
} else {
51-
define symbol APPLICATION_ADDR = MBED_ROM_START;
54+
define symbol APPLICATION_ADDR = MBED_ROM_START_ORIDE;
5255
}
5356
}
5457

5558
if (!isdefinedsymbol(APPLICATION_SIZE)) {
5659
if (isdefinedsymbol(MBED_APP_SIZE)) {
5760
define symbol APPLICATION_SIZE = MBED_APP_SIZE;
5861
} else {
59-
define symbol APPLICATION_SIZE = MBED_ROM_SIZE;
62+
define symbol APPLICATION_SIZE = MBED_ROM_SIZE_ORIDE;
6063
}
6164
}
6265

6366
if (!isdefinedsymbol(APPLICATION_RAM_ADDR)) {
6467
if (isdefinedsymbol(MBED_RAM_APP_START)) {
6568
define symbol APPLICATION_RAM_ADDR = MBED_RAM_APP_START;
6669
} else {
67-
define symbol APPLICATION_RAM_ADDR = MBED_RAM_START;
70+
define symbol APPLICATION_RAM_ADDR = MBED_RAM_START_ORIDE;
6871
}
6972
}
7073

7174
if (!isdefinedsymbol(APPLICATION_RAM_SIZE)) {
7275
if (isdefinedsymbol(MBED_RAM_APP_SIZE)) {
7376
define symbol APPLICATION_RAM_SIZE = MBED_RAM_APP_SIZE;
7477
} else {
75-
define symbol APPLICATION_RAM_SIZE = MBED_RAM_SIZE;
78+
define symbol APPLICATION_RAM_SIZE = MBED_RAM_SIZE_ORIDE;
7679
}
7780
}
7881

targets/targets.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6968,11 +6968,7 @@
69686968
"ARMCLANG",
69696969
"GNUARM"
69706970
],
6971-
"tfm_delivery_dir": "TARGET_NUVOTON/TARGET_M2354/TARGET_TFM/TARGET_NU_M2354/COMPONENT_TFM_S_FW",
6972-
"mbed_rom_start": "0x10070400",
6973-
"mbed_rom_size": "0x8F400",
6974-
"mbed_ram_start": "0x30014000",
6975-
"mbed_ram_size": "0x2C000"
6971+
"tfm_delivery_dir": "TARGET_NUVOTON/TARGET_M2354/TARGET_TFM/TARGET_NU_M2354/COMPONENT_TFM_S_FW"
69766972
},
69776973
"MCU_M251": {
69786974
"core": "Cortex-M23",

0 commit comments

Comments
 (0)