|
| 1 | + |
| 2 | +/* |
| 3 | + Copyright (c) 2018-2024, IAR Systems AB. |
| 4 | + |
| 5 | + `secure` "Hello" - A simple CMSE example |
| 6 | + This minimalistic example is not production-ready within CMSE best-practices. |
| 7 | + However, it showcases some features within CMSE usage contexts. |
| 8 | +
|
| 9 | + The gateway interface towards the `non-secure` target consists of 2 functions |
| 10 | + - secure_hello(char const *str) |
| 11 | + Validates str and prints str to standard out |
| 12 | +
|
| 13 | + - register_secure_goodbye(secure_goodbye_t fptr) |
| 14 | + Validates fptr and stores it to be used when system terminates |
| 15 | +
|
| 16 | + Expects the `non-secure` target to have a |
| 17 | + `non_secure_init_t` structure @ 0x0020_0000. |
| 18 | +*/ |
| 19 | + |
| 20 | +#if (__ARM_FEATURE_CMSE & 1) == 0 |
| 21 | +#error "Need ARMv8-M security extensions" |
| 22 | +#elif (__ARM_FEATURE_CMSE & 2) == 0 |
| 23 | +#error "Compile with --cmse" |
| 24 | +#endif |
| 25 | + |
| 26 | +#include <arm_cmse.h> |
| 27 | +#include <stdio.h> |
| 28 | +#include <stdlib.h> |
| 29 | +#include <string.h> |
| 30 | +#include "secure-hello.h" |
| 31 | + |
| 32 | +#pragma language=extended |
| 33 | + |
| 34 | +/* Check address of string provided by `non-secure` */ |
| 35 | +static void check_string(char * s); |
| 36 | + |
| 37 | +/* Setup of access rights for memory regions */ |
| 38 | +static void SAU_setup(void); |
| 39 | + |
| 40 | +/* Callback, on exit from `secure` */ |
| 41 | +static secure_goodbye_t cb_goodbye = NULL; |
| 42 | + |
| 43 | +/* First entry: secure_hello */ |
| 44 | +__cmse_nonsecure_entry void secure_hello(char * s) |
| 45 | +{ |
| 46 | + /* Validate string if caller is non-secure */ |
| 47 | + if (cmse_nonsecure_caller()) |
| 48 | + check_string(s); |
| 49 | + |
| 50 | + printf("Hello %s!\n", s); |
| 51 | +} |
| 52 | + |
| 53 | +/* Second entry: register_secure_goodbye */ |
| 54 | +__cmse_nonsecure_entry void register_secure_goodbye(secure_goodbye_t goodbye) |
| 55 | +{ |
| 56 | + if (goodbye) |
| 57 | + { |
| 58 | + /* If goodbye is not NULL make sure the address |
| 59 | + * can be read from non-secure state |
| 60 | + */ |
| 61 | + cmse_address_info_t r = cmse_TTA_fptr(goodbye); |
| 62 | + if (!r.flags.nonsecure_read_ok) |
| 63 | + { |
| 64 | + printf("Unacceptable function pointer!\n"); |
| 65 | + abort(); |
| 66 | + } |
| 67 | + } |
| 68 | + cb_goodbye = goodbye; |
| 69 | +} |
| 70 | + |
| 71 | +/* "Handle" to the non-secure image */ |
| 72 | +#define unsecure_table (*((non_secure_init_t const *)NON_SECURE_ENTRY_TABLE)) |
| 73 | + |
| 74 | +int main(void) |
| 75 | +{ |
| 76 | + /* Setup non-secure and non-secure callable regions */ |
| 77 | + SAU_setup(); |
| 78 | + |
| 79 | + /* Let the world know non-secure code is up and running */ |
| 80 | + secure_hello("from secure World"); |
| 81 | + |
| 82 | + /* Set stack pointer for the `non-secure` target */ |
| 83 | + asm volatile("MSR SP_NS, %0" :: "r" (unsecure_table.stack)); |
| 84 | + |
| 85 | + /* Let non-secure code initialize its environment */ |
| 86 | + unsecure_table.init(); |
| 87 | + |
| 88 | + /* Call non-secure main */ |
| 89 | + unsecure_table.main(); |
| 90 | + |
| 91 | + /* Nothing more to do in this system... */ |
| 92 | + |
| 93 | + /* Goodbye... */ |
| 94 | + if (cb_goodbye != NULL) |
| 95 | + { |
| 96 | + char * s = cb_goodbye(); |
| 97 | + check_string(s); |
| 98 | + printf("%s\n", s); |
| 99 | + } |
| 100 | + return 0; |
| 101 | +} |
| 102 | + |
| 103 | +#define MAX_STRING_LENGTH 42 |
| 104 | +void check_string(char * s) |
| 105 | +{ |
| 106 | + /* Do not allow arbitrary length of string */ |
| 107 | + size_t n = strnlen(s, MAX_STRING_LENGTH); |
| 108 | + if (n == MAX_STRING_LENGTH && s[n] != '\0') |
| 109 | + { |
| 110 | + printf("Unacceptable string length!\n"); |
| 111 | + abort(); |
| 112 | + } |
| 113 | + if (cmse_check_address_range(s, n, CMSE_MPU_UNPRIV | CMSE_MPU_READ) == NULL) |
| 114 | + { |
| 115 | + printf("Unacceptable address range!\n"); |
| 116 | + abort(); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +/* When writing this we have no header file for the secure region SFRs, |
| 121 | + * so for now we define them here where they are needed. |
| 122 | + */ |
| 123 | +#define SAU_CTRL (*((volatile unsigned int *) 0xE000EDD0)) |
| 124 | +#define SAU_RNR (*((volatile unsigned int *) 0xE000EDD8)) |
| 125 | +#define SAU_RBAR (*((volatile unsigned int *) 0xE000EDDC)) |
| 126 | +#define SAU_RLAR (*((volatile unsigned int *) 0xE000EDE0)) |
| 127 | + |
| 128 | +/* |
| 129 | + * Note: MPU protection is not setup in this example |
| 130 | + */ |
| 131 | + |
| 132 | +static void SAU_setup(void) |
| 133 | +{ |
| 134 | + /* region #0: non-secure callable, 0x000000C0 - 0x000000DF */ |
| 135 | + SAU_RNR = 0; |
| 136 | + SAU_RBAR = 0x000000C0; |
| 137 | + SAU_RLAR = 0x000000C3; |
| 138 | + |
| 139 | + /* region #1: non-secure, 0x00200000 - 0x003fffff */ |
| 140 | + SAU_RNR = 1; |
| 141 | + SAU_RBAR = 0x00200000; |
| 142 | + SAU_RLAR = 0x003fffe1; |
| 143 | + |
| 144 | + /* region #2: non-secure, 0x20200000 - 0x203fffff */ |
| 145 | + SAU_RNR = 2; |
| 146 | + SAU_RBAR = 0x20200000; |
| 147 | + SAU_RLAR = 0x203fffe1; |
| 148 | + |
| 149 | + /* region #3: non-secure, 0x40000000 - 0x4004001f */ |
| 150 | + SAU_RNR = 3; |
| 151 | + SAU_RBAR = 0x40000000; |
| 152 | + SAU_RLAR = 0x40040001; |
| 153 | + |
| 154 | + /* Enable SAU */ |
| 155 | + SAU_CTRL = 1; |
| 156 | +} |
0 commit comments