Skip to content

Commit 8fec27d

Browse files
authored
Merge pull request #15 from AlessandroA/minor_fixes
Minor improvements to the app
2 parents 1903320 + 85d3c8b commit 8fec27d

File tree

9 files changed

+104
-178
lines changed

9 files changed

+104
-178
lines changed

.gitignore

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1+
# Misc hidden files
12
.DS_Store
2-
.build
3-
gdb.script
43
*.sw*
4+
5+
# mbed files and folders
6+
.build
7+
BUILD
8+
mbed-os
9+
mbed-os/*
510
mbed_settings.py*
6-
/mbed-os/
7-
/firmware.*
8-
/debug.elf
11+
.mbed
12+
13+
# Custom Makefile temp files
14+
gdb.script
15+
firmware.*
16+
debug.elf

README.md

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ This number can only be written by one box, but read by all boxes.
1212

1313
This demo contains three secure boxes:
1414

15-
1. The secure number vault. This box stores one number that can only be written to by client A, but read by everyone.
15+
1. The secure number vault. This box stores one number that can only be written to by Client A, but read by everyone.
1616
1. Client A, which attempts to write (and succeeds) and read the secure number.
1717
1. Client B, which attempts to write (but fails) and read the secure number.
1818

19-
The insecure box 0 also attempts to write (but fails) and read the secure number.
19+
As usual, all the code/data that is not protected by a secure box ends up in the public box (also known as box 0), which is visible by all other boxes and, hence, insecure. The public box also attempts to write (but fails) and read the secure number.
2020

2121
Supported devices:
2222

2323
| Target | Toolchain | Baud rate |
2424
|--------|-----------|-----------|
2525
| `K64F` | `GCC_ARM` | 9600 |
2626

27-
Latest release: [mbed-os-5.3.0](https://github.com/ARMmbed/mbed-os-example-uvisor/releases/tag/mbed-os-5.3.0). Tested with [mbed-cli v1.0.0](https://github.com/ARMmbed/mbed-cli/releases/tag/1.0.0).
27+
Latest release: [mbed-os-5.3.x](https://github.com/ARMmbed/mbed-os-example-uvisor/releases/tag/latest). Tested with [mbed-cli v1.0.0](https://github.com/ARMmbed/mbed-cli/releases/tag/1.0.0).
2828

2929
## Quickstart
3030

@@ -45,18 +45,16 @@ $ screen /dev/tty.usbmodem1422 9600
4545
You will see an output similar to the following one:
4646

4747
```
48-
**** uVisor secure number store example *****
49-
Trusted client a has box id 2
50-
2: Wrote '0xfffffed4'
51-
1: Read '0xfffffed4'
52-
1: Permission denied. This client cannot write the secure number '0xfffffe0c'
53-
0: Read '0xfffffed4'
54-
0: Permission denied. This client cannot write the secure number '0x00000019'
55-
2: Read '0xfffffed4'
56-
2: Wrote '0xfffffda8'
57-
2: Read '0xfffffda8'
58-
1: Permission denied. This client cannot write the secure number '0xfffffc18'
59-
2: Wrote '0xfffffc7c'
48+
***** uVisor secure number store example *****
49+
vault : Only client_a can write into the vault
50+
vault : All clients can read the vault
51+
client_b: Attempt to write 0xFFFFFED4 (denied)
52+
client_a: Attempt to read : 0x00000000 (granted)
53+
client_a: Attempt to write 0xFFFFFE0C (granted)
54+
public : Attempt to read : 0xFFFFFE0C (granted)
55+
public : Attempt to write 0x00000019 (denied)
56+
client_b: Attempt to read : 0xFFFFFE0C (granted)
57+
client_a: Attempt to read : 0xFFFFFE0C (granted)
6058
...
6159
```
6260

mbed_app.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"target_overrides": {
3-
"K64F": {
3+
"*": {
44
"target.features_add": ["UVISOR"],
55
"target.extra_labels_add": ["UVISOR_SUPPORTED"]
66
}

source/client_a.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,28 @@
2222

2323
struct box_context {
2424
uint32_t number;
25-
RawSerial * pc;
2625
};
2726

2827
static const UvisorBoxAclItem acl[] = {
2928
};
3029

3130
static void client_a_main(const void *);
3231

33-
/* Box configuration */
32+
/* Box configuration
33+
* This box has a smaller interrupt and main thread stack sizes as we do nothing
34+
* special in them. */
3435
UVISOR_BOX_NAMESPACE("client_a");
35-
UVISOR_BOX_HEAPSIZE(8192);
36-
UVISOR_BOX_MAIN(client_a_main, osPriorityNormal, UVISOR_BOX_STACK_SIZE);
37-
UVISOR_BOX_CONFIG(secure_number_client_a, acl, UVISOR_BOX_STACK_SIZE, box_context);
36+
UVISOR_BOX_HEAPSIZE(3072);
37+
UVISOR_BOX_MAIN(client_a_main, osPriorityNormal, 512);
38+
UVISOR_BOX_CONFIG(secure_number_client_a, acl, 512, box_context);
3839

3940
static uint32_t get_a_number()
4041
{
4142
/* Such random. Many secure. Much bits. Wow. */
4243
return (uvisor_ctx->number -= 500UL);
4344
}
4445

45-
static void box_async_runner(const void *)
46+
static void box_async_runner(void)
4647
{
4748
while (1) {
4849
uvisor_rpc_result_t result;
@@ -56,11 +57,8 @@ static void box_async_runner(const void *)
5657
while (1) {
5758
uint32_t ret;
5859
int status = rpc_fncall_wait(result, UVISOR_WAIT_FOREVER, &ret);
59-
uvisor_ctx->pc->printf("%c: %s '0x%08x'\r\n",
60-
(char) uvisor_box_id_self() + '0',
61-
(ret == 0) ? "Wrote" :
62-
"Permission denied. This client cannot write the secure number",
63-
(unsigned int) number);
60+
shared_pc.printf("client_a: Attempt to write 0x%08X (%s)\r\n",
61+
(unsigned int) number, (ret == 0) ? "granted" : "denied");
6462
/* FIXME: Add better error handling. */
6563
if (!status) {
6664
break;
@@ -71,27 +69,29 @@ static void box_async_runner(const void *)
7169
}
7270
}
7371

74-
static void box_sync_runner(const void *)
72+
static void box_sync_runner(void)
7573
{
7674
while (1) {
7775
/* Synchronous access to the number. */
7876
const uint32_t number = secure_number_get_number();
79-
uvisor_ctx->pc->printf("%c: Read '0x%08x'\r\n", (char) uvisor_box_id_self() + '0', (unsigned int) number);
77+
shared_pc.printf("client_a: Attempt to read : 0x%08X (granted)\r\n", (unsigned int) number);
8078

8179
Thread::wait(7000);
8280
}
8381
}
8482

8583
static void client_a_main(const void *)
8684
{
87-
/* Allocate serial port to ensure that code in this secure box won't touch
88-
* the handle in the default security context when printing. */
89-
uvisor_ctx->pc = new RawSerial(USBTX, USBRX);
90-
if (!uvisor_ctx->pc) {
91-
return;
92-
}
85+
/* Create new threads. */
86+
/* Note: The stack must be at least 1kB since threads will use printf. */
87+
Thread sync(osPriorityNormal, 1024, NULL);
88+
sync.start(box_sync_runner);
89+
Thread async(osPriorityNormal, 1024, NULL);
90+
async.start(box_async_runner);
9391

94-
srand(uvisor_box_id_self());
95-
new Thread(box_sync_runner, NULL);
96-
new Thread(box_async_runner, NULL);
92+
size_t count = 0;
93+
while (1) {
94+
/* Spin forever. */
95+
++count;
96+
}
9797
}

source/client_b.cpp

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
struct box_context {
2424
uint32_t number;
25-
RawSerial * pc;
2625
};
2726

2827
static const UvisorBoxAclItem acl[] = {
@@ -31,11 +30,13 @@ static const UvisorBoxAclItem acl[] = {
3130

3231
static void client_b_main(const void *);
3332

34-
/* Box configuration */
33+
/* Box configuration
34+
* This box has a smaller interrupt stack size as we do nothing special in it.
35+
* The main thread uses printf so it needs at least 1kB of stack. */
3536
UVISOR_BOX_NAMESPACE("client_b");
36-
UVISOR_BOX_HEAPSIZE(8192);
37-
UVISOR_BOX_MAIN(client_b_main, osPriorityNormal, UVISOR_BOX_STACK_SIZE);
38-
UVISOR_BOX_CONFIG(secure_number_client_b, acl, UVISOR_BOX_STACK_SIZE, box_context);
37+
UVISOR_BOX_HEAPSIZE(3072);
38+
UVISOR_BOX_MAIN(client_b_main, osPriorityNormal, 1024);
39+
UVISOR_BOX_CONFIG(secure_number_client_b, acl, 512, box_context);
3940

4041
static uint32_t get_a_number()
4142
{
@@ -45,13 +46,6 @@ static uint32_t get_a_number()
4546

4647
static void client_b_main(const void *)
4748
{
48-
/* Allocate serial port to ensure that code in this secure box won't touch
49-
* the handle in the default security context when printing. */
50-
uvisor_ctx->pc = new RawSerial(USBTX, USBRX);
51-
if (!uvisor_ctx->pc) {
52-
return;
53-
}
54-
5549
/* The entire box code runs in its main thread. */
5650
while (1) {
5751
uvisor_rpc_result_t result;
@@ -65,19 +59,16 @@ static void client_b_main(const void *)
6559
while (1) {
6660
uint32_t ret;
6761
int status = rpc_fncall_wait(result, UVISOR_WAIT_FOREVER, &ret);
68-
uvisor_ctx->pc->printf("%c: %s '0x%08x'\r\n",
69-
(char) uvisor_box_id_self() + '0',
70-
(ret == 0) ? "Wrote" :
71-
"Permission denied. This client cannot write the secure number",
72-
(unsigned int) number);
62+
shared_pc.printf("client_b: Attempt to write 0x%08X (%s)\r\n",
63+
(unsigned int) number, (ret == 0) ? "granted" : "denied");
7364
if (!status) {
7465
break;
7566
}
7667
}
7768

7869
/* Synchronous access to the number. */
7970
number = secure_number_get_number();
80-
uvisor_ctx->pc->printf("%c: Read '0x%08x'\r\n", (char) uvisor_box_id_self() + '0', (unsigned int) number);
71+
shared_pc.printf("client_b: Attempt to read : 0x%08X (granted)\r\n", (unsigned int) number);
8172

8273
Thread::wait(3000);
8374
}

source/main-hw.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,16 @@
1717
#ifndef __UVISOR_HELLOWORLD_MAIN_HW_H__
1818
#define __UVISOR_HELLOWORLD_MAIN_HW_H__
1919

20-
/* The vector containing the challenge is shared with the push-button ISR, so
21-
* that it can attempt to access it from an IRQ context. */
20+
extern DigitalOut led_red;
21+
extern DigitalOut led_green;
22+
extern DigitalOut led_blue;
2223

23-
#define LED_ON false
24-
#define LED_OFF true
24+
extern Serial shared_pc;
2525

26-
#define MAIN_LED LED_BLUE
27-
#define HALT_LED LED_RED
26+
#if defined(TARGET_K64F)
2827

29-
#define MAIN_BTN SW2
30-
#define MAIN_BTN_PUPD PullUp
28+
#define LED_ON false
29+
#define LED_OFF true
3130

3231
#define MAIN_ACL(acl_list_name) \
3332
static const UvisorBoxAclItem acl_list_name[] = { \
@@ -48,8 +47,10 @@
4847
{SPI0, sizeof(*SPI0), UVISOR_TACLDEF_PERIPH}, \
4948
}
5049

51-
extern DigitalOut led_red;
52-
extern DigitalOut led_green;
53-
extern DigitalOut led_blue;
50+
#else /* Target-specific settings */
51+
52+
#error "Unsupported target. Checkout the README.md file for the list of supported targets for this app."
53+
54+
#endif /* Target-specific settings */
5455

5556
#endif /* __UVISOR_HELLOWORLD_MAIN_HW_H__ */

source/main.cpp

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,23 @@
2424

2525
/* Create ACLs for main box. */
2626
MAIN_ACL(g_main_acl);
27-
28-
/* Register privleged system hooks. */
29-
UVISOR_EXTERN void SVC_Handler(void);
30-
UVISOR_EXTERN void PendSV_Handler(void);
31-
UVISOR_EXTERN void SysTick_Handler(void);
32-
extern "C" uint32_t rt_suspend(void);
33-
34-
UVISOR_SET_PRIV_SYS_HOOKS(SVC_Handler, PendSV_Handler, SysTick_Handler, rt_suspend, __uvisor_semaphore_post);
35-
3627
/* Enable uVisor. */
3728
UVISOR_SET_MODE_ACL(UVISOR_ENABLED, g_main_acl);
29+
UVISOR_SET_PAGE_HEAP(8 * 1024, 5);
3830

3931
DigitalOut led_red(LED1);
4032
DigitalOut led_green(LED2);
4133
DigitalOut led_blue(LED3);
4234

35+
Serial shared_pc(USBTX, USBRX);
36+
4337
static uint32_t get_a_number()
4438
{
4539
static uint32_t number = 425;
4640
return (number -= 400UL);
4741
}
4842

49-
static void main_async_runner(const void *)
43+
static void main_async_runner(void)
5044
{
5145
while (1) {
5246
uvisor_rpc_result_t result;
@@ -61,11 +55,8 @@ static void main_async_runner(const void *)
6155
/* TODO typesafe return codes */
6256
uint32_t ret;
6357
status = rpc_fncall_wait(result, UVISOR_WAIT_FOREVER, &ret);
64-
printf("%c: %s '0x%08x'\r\n",
65-
(char) uvisor_box_id_self() + '0',
66-
(ret == 0) ? "Wrote" :
67-
"Permission denied. This client cannot write the secure number",
68-
(unsigned int) number);
58+
shared_pc.printf("public : Attempt to write 0x%08X (%s)\r\n",
59+
(unsigned int) number, (ret == 0) ? "granted" : "denied");
6960
if (!status) {
7061
break;
7162
}
@@ -75,32 +66,33 @@ static void main_async_runner(const void *)
7566
}
7667
}
7768

78-
static void main_sync_runner(const void *)
69+
static void main_sync_runner(void)
7970
{
8071
while (1) {
8172
/* Synchronous access to the number. */
8273
const uint32_t number = secure_number_get_number();
83-
printf("%c: Read '0x%08x'\r\n", (char) uvisor_box_id_self() + '0', (unsigned int) number);
74+
shared_pc.printf("public : Attempt to read : 0x%08X (granted)\r\n", (unsigned int) number);
8475

8576
Thread::wait(11000);
8677
}
8778
}
8879

8980
int main(void)
9081
{
91-
printf("\r\n***** uVisor secure number store example *****\r\n");
82+
shared_pc.printf("\r\n***** uVisor secure number store example *****\r\n");
9283
led_red = LED_OFF;
9384
led_blue = LED_OFF;
9485
led_green = LED_OFF;
9586

9687
/* Startup a few RPC runners. */
97-
Thread sync(main_sync_runner, NULL);
98-
Thread async(main_async_runner, NULL);
88+
/* Note: The stack must be at least 1kB since threads will use printf. */
89+
Thread sync(osPriorityNormal, 1024, NULL);
90+
sync.start(main_sync_runner);
91+
Thread async(osPriorityNormal, 1024, NULL);
92+
async.start(main_async_runner);
9993

10094
size_t count = 0;
101-
102-
while (1)
103-
{
95+
while (1) {
10496
/* Spin forever. */
10597
++count;
10698
}

0 commit comments

Comments
 (0)