Skip to content

Commit e61c514

Browse files
committed
[M2351] Fix binary-compatible across compilers in secure functions
1. Rename m2351_stddriver_sup.h/c to stddriver_secure.h/.c for naming consistency 2. Add hal_secure.h to include hal-exported secure functions 3. Change return/argument type in secure functions: (1) Change int to int32_t (2) Change PinName to int32_t (3) Change time_t to int64_t 4. Update secure lib/bin accordingly
1 parent 51276a6 commit e61c514

File tree

8 files changed

+1143
-819
lines changed

8 files changed

+1143
-819
lines changed

targets/TARGET_NUVOTON/TARGET_M2351/TARGET_NUMAKER_PFM_M2351/TARGET_M23_NS/NuMaker-mbed-TZ-secure-example.hex

Lines changed: 999 additions & 777 deletions
Large diffs are not rendered by default.

targets/TARGET_NUVOTON/TARGET_M2351/device/cmsis.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#define MBED_CMSIS_H
1919

2020
#include "M2351.h"
21-
#include "m2351_stddriver_sup.h"
21+
#include "stddriver_secure.h"
2222
#include "cmsis_nvic.h"
2323
#include "partition_M2351.h"
2424

targets/TARGET_NUVOTON/TARGET_M2351/device/m2351_stddriver_sup.c renamed to targets/TARGET_NUVOTON/TARGET_M2351/device/stddriver_secure.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include <stdbool.h>
1919
#include "cmsis.h"
2020
#include "partition_M2351.h"
21-
#include "m2351_stddriver_sup.h"
21+
#include "stddriver_secure.h"
2222
#include "mbed_error.h"
2323

2424
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)

targets/TARGET_NUVOTON/TARGET_M2351/device/m2351_stddriver_sup.h renamed to targets/TARGET_NUVOTON/TARGET_M2351/device/stddriver_secure.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
#ifndef M2351_STDDRIVER_SUP_H
18-
#define M2351_STDDRIVER_SUP_H
17+
#ifndef STDDRIVER_SECURE_H
18+
#define STDDRIVER_SECURE_H
1919

2020
#include "cmsis.h"
2121

@@ -28,41 +28,47 @@ extern "C" {
2828
* Guard access to secure module from non-secure domain before SYS_ResetModule.
2929
* Its synopsis is the same as SYS_ResetModule.
3030
*/
31+
__NONSECURE_ENTRY
3132
void SYS_ResetModule_S(uint32_t u32ModuleIndex);
3233

3334
/* Secure CLK_SetModuleClock
3435
*
3536
* Guard access to secure module from non-secure domain before CLK_SetModuleClock.
3637
* Its synopsis is the same as CLK_SetModuleClock.
3738
*/
39+
__NONSECURE_ENTRY
3840
void CLK_SetModuleClock_S(uint32_t u32ModuleIndex, uint32_t u32ClkSrc, uint32_t u32ClkDiv);
3941

4042
/* Secure CLK_EnableModuleClock
4143
*
4244
* Guard access to secure module from non-secure domain before CLK_EnableModuleClock.
4345
* Its synopsis is the same as CLK_EnableModuleClock.
4446
*/
47+
__NONSECURE_ENTRY
4548
void CLK_EnableModuleClock_S(uint32_t u32ModuleIndex);
4649

4750
/* Secure CLK_DisableModuleClock
4851
*
4952
* Guard access to secure module from non-secure domain before CLK_DisableModuleClock.
5053
* Its synopsis is the same as CLK_DisableModuleClock.
5154
*/
55+
__NONSECURE_ENTRY
5256
void CLK_DisableModuleClock_S(uint32_t u32ModuleIndex);
5357

5458
/* Secure SYS_LockReg
5559
*
5660
* Guard access to secure module from non-secure domain before SYS_LockReg.
5761
* Its synopsis is the same as SYS_LockReg.
5862
*/
63+
__NONSECURE_ENTRY
5964
void SYS_LockReg_S(void);
6065

6166
/* Secure SYS_UnlockReg
6267
*
6368
* Guard access to secure module from non-secure domain before SYS_UnlockReg.
6469
* Its synopsis is the same as SYS_UnlockReg.
6570
*/
71+
__NONSECURE_ENTRY
6672
void SYS_UnlockReg_S(void);
6773

6874
#ifdef __cplusplus
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017-2018 Nuvoton
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef HAL_SECURE_H
18+
#define HAL_SECURE_H
19+
20+
#include "cmsis.h"
21+
22+
#ifdef __cplusplus
23+
extern "C" {
24+
#endif
25+
26+
/* pin_function (secure version)
27+
*
28+
* Guard access to secure GPIO from non-secure domain.
29+
*
30+
* Its synopsis is the same as normal version except change of return/argument type for
31+
* binary-compatible across compilers.
32+
*/
33+
__NONSECURE_ENTRY
34+
void pin_function_s(int32_t pin, int32_t data);
35+
36+
/* NOTE: time_t
37+
*
38+
* time_t may be defined to int32_t/int64_t dependent on compilers. For binary-compatible
39+
* among compilers, we must replace time_t with int32_t/int64_t explicitly in rtc secure
40+
* functions. We determine to choose int64_t rather than int32_t to avoid 'Year 2038 problem'.
41+
*/
42+
43+
/* rtc_init (secure version) */
44+
__NONSECURE_ENTRY
45+
void rtc_init_s(void);
46+
47+
/* rtc_free (secure version) */
48+
__NONSECURE_ENTRY
49+
void rtc_free_s(void);
50+
51+
/* rtc_isenabled (secure version)
52+
*
53+
* Its synopsis is the same as normal version except change of return/argument type for
54+
* binary-compatible across compilers.
55+
*/
56+
__NONSECURE_ENTRY
57+
int32_t rtc_isenabled_s(void);
58+
59+
/* rtc_read (secure version)
60+
*
61+
* Its synopsis is the same as normal version except change of return/argument type for
62+
* binary-compatible across compilers.
63+
*/
64+
__NONSECURE_ENTRY
65+
int64_t rtc_read_s(void);
66+
67+
/* rtc_write (secure version)
68+
*
69+
* Its synopsis is the same as normal version except change of return/argument type for
70+
* binary-compatible across compilers.
71+
*/
72+
__NONSECURE_ENTRY
73+
void rtc_write_s(int64_t t);
74+
75+
#ifdef __cplusplus
76+
}
77+
#endif
78+
79+
#endif

targets/TARGET_NUVOTON/TARGET_M2351/pinmap.c

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,15 @@
2020
#include "PortNames.h"
2121
#include "mbed_error.h"
2222
#include "partition_M2351.h"
23+
#include "hal_secure.h"
2324

2425
/**
2526
* Configure pin multi-function
2627
*/
27-
28-
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
29-
__NONSECURE_ENTRY
3028
void pin_function(PinName pin, int data)
3129
{
32-
MBED_ASSERT(pin != (PinName)NC);
33-
uint32_t pin_index = NU_PINNAME_TO_PIN(pin);
34-
uint32_t port_index = NU_PINNAME_TO_PORT(pin);
35-
36-
/* Guard access to secure GPIO from non-secure domain */
37-
if (cmse_nonsecure_caller() &&
38-
(! (SCU_INIT_IONSSET_VAL & (1 << (port_index + 0))))) {
39-
error("Non-secure domain tries to control secure or undefined GPIO.");
40-
}
41-
42-
__IO uint32_t *GPx_MFPx = ((__IO uint32_t *) &SYS->GPA_MFPL) + port_index * 2 + (pin_index / 8);
43-
uint32_t MFP_Msk = NU_MFP_MSK(pin_index);
44-
45-
// E.g.: SYS->GPA_MFPL = (SYS->GPA_MFPL & (~SYS_GPA_MFPL_PA0MFP_Msk) ) | SYS_GPA_MFPL_PA0MFP_SC0_CD ;
46-
*GPx_MFPx = (*GPx_MFPx & (~MFP_Msk)) | data;
30+
pin_function_s(pin, data);
4731
}
48-
#endif
4932

5033
/**
5134
* Configure pin pull-up/pull-down
@@ -84,3 +67,25 @@ void pin_mode(PinName pin, PinMode mode)
8467

8568
GPIO_SetMode(gpio_base, 1 << pin_index, mode_intern);
8669
}
70+
71+
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
72+
__NONSECURE_ENTRY
73+
void pin_function_s(int32_t pin, int32_t data)
74+
{
75+
MBED_ASSERT(pin != (PinName)NC);
76+
uint32_t pin_index = NU_PINNAME_TO_PIN(pin);
77+
uint32_t port_index = NU_PINNAME_TO_PORT(pin);
78+
79+
/* Guard access to secure GPIO from non-secure domain */
80+
if (cmse_nonsecure_caller() &&
81+
(! (SCU_INIT_IONSSET_VAL & (1 << (port_index + 0))))) {
82+
error("Non-secure domain tries to control secure or undefined GPIO.");
83+
}
84+
85+
__IO uint32_t *GPx_MFPx = ((__IO uint32_t *) &SYS->GPA_MFPL) + port_index * 2 + (pin_index / 8);
86+
uint32_t MFP_Msk = NU_MFP_MSK(pin_index);
87+
88+
// E.g.: SYS->GPA_MFPL = (SYS->GPA_MFPL & (~SYS_GPA_MFPL_PA0MFP_Msk) ) | SYS_GPA_MFPL_PA0MFP_SC0_CD ;
89+
*GPx_MFPx = (*GPx_MFPx & (~MFP_Msk)) | data;
90+
}
91+
#endif

targets/TARGET_NUVOTON/TARGET_M2351/rtc_api.c

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "nu_miscutil.h"
2525
#include "mbed_mktime.h"
2626
#include "partition_M2351.h"
27+
#include "hal_secure.h"
2728

2829
/* NOTE: BSP RTC driver judges secure/non-secure RTC by PC. This implementation cannot support non-secure RTC
2930
* controlled by secure executable. A better way would be that secure/non-secure RTC base is passed
@@ -33,21 +34,32 @@
3334
#error("Limited by BSP/RTC, we can only support secure RTC.")
3435
#endif
3536

36-
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
37+
void rtc_init(void)
38+
{
39+
rtc_init_s();
40+
}
3741

38-
/* As of ARM Compiler 6.9, we meet some NSC functions are missing in secure gateway
39-
* import library (cmse_lib.o) in secure build. Per test, we could get around it by
40-
* adding declaration for NSC functions. */
41-
__NONSECURE_ENTRY
42-
void rtc_init(void);
43-
__NONSECURE_ENTRY
44-
void rtc_free(void);
45-
__NONSECURE_ENTRY
46-
int rtc_isenabled(void);
47-
__NONSECURE_ENTRY
48-
time_t rtc_read(void);
49-
__NONSECURE_ENTRY
50-
void rtc_write(time_t t);
42+
void rtc_free(void)
43+
{
44+
rtc_free_s();
45+
}
46+
47+
int rtc_isenabled(void)
48+
{
49+
return rtc_isenabled_s();
50+
}
51+
52+
time_t rtc_read(void)
53+
{
54+
return rtc_read_s();
55+
}
56+
57+
void rtc_write(time_t t)
58+
{
59+
rtc_write_s(t);
60+
}
61+
62+
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
5163

5264
/* Micro seconds per second */
5365
#define NU_US_PER_SEC 1000000
@@ -112,7 +124,7 @@ static void rtc_convert_datetime_hwrtc_to_tm(struct tm *datetime_tm, const S_RTC
112124
static const struct nu_modinit_s rtc_modinit = {RTC_0, RTC_MODULE, 0, 0, 0, RTC_IRQn, NULL};
113125

114126
__NONSECURE_ENTRY
115-
void rtc_init(void)
127+
void rtc_init_s(void)
116128
{
117129
if (rtc_isenabled()) {
118130
return;
@@ -125,13 +137,13 @@ void rtc_init(void)
125137
}
126138

127139
__NONSECURE_ENTRY
128-
void rtc_free(void)
140+
void rtc_free_s(void)
129141
{
130142
CLK_DisableModuleClock_S(rtc_modinit.clkidx);
131143
}
132144

133145
__NONSECURE_ENTRY
134-
int rtc_isenabled(void)
146+
int32_t rtc_isenabled_s(void)
135147
{
136148
// NOTE: To access (RTC) registers, clock must be enabled first.
137149
if (! (CLK->APBCLK0 & CLK_APBCLK0_RTCCKEN_Msk)) {
@@ -146,7 +158,7 @@ int rtc_isenabled(void)
146158
}
147159

148160
__NONSECURE_ENTRY
149-
time_t rtc_read(void)
161+
int64_t rtc_read_s(void)
150162
{
151163
/* NOTE: After boot, RTC time registers are not synced immediately, about 1 sec latency.
152164
* RTC time got (through RTC_GetDateAndTime()) in this sec would be last-synced and incorrect.
@@ -195,7 +207,7 @@ time_t rtc_read(void)
195207
}
196208

197209
__NONSECURE_ENTRY
198-
void rtc_write(time_t t)
210+
void rtc_write_s(int64_t t)
199211
{
200212
if (! rtc_isenabled()) {
201213
rtc_init();

0 commit comments

Comments
 (0)