Skip to content

Commit 3295038

Browse files
committed
added binaries headers and updated code
added binaries in external files to make the app file more readable. Added a new subscribe and callback to support the async setup
1 parent 319d731 commit 3295038

File tree

7 files changed

+1532
-253
lines changed

7 files changed

+1532
-253
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Dynamic App Loader (Helper App)
2+
=================================
3+
4+
This app waits upon the user to press a button on the device.
5+
There are three stages to this:
6+
7+
1. Setup Phase
8+
2. Flash Phase
9+
3. Load Phase
10+
11+
#### Setup Phase
12+
During the setup phase, the application passes the size of the new app `(blink)`'s
13+
binary to the app_loader capsule.
14+
15+
The capsule returns with either a success or failure.
16+
17+
On success, the app requests the app_loader capsule to flash the `blink` app.
18+
19+
On Failure, the app exits logs the reason for failure and exits.
20+
21+
#### Flash Phase
22+
The app sends the binary of the `blink` app 512 bytes (this is the size of the shared
23+
buffer between the app and the capsule) at a time along with the offset.
24+
25+
The capsule checks that these values do not violate the bounds dictated by the kernel
26+
and then requests the kernel to flash the app.
27+
28+
The kernel performs more stringent checking on the request to ensure that memory access
29+
violations do not take place, and flashes the app.
30+
31+
The capsule then returns with either a success or failure.
32+
33+
On success, the app requests the app_loader capsule to load the `blink` app.
34+
35+
On Failure, the app exits logs the reason for failure and exits.
36+
37+
#### Load Phase
38+
The app requests the capsule to load the new app. There are only two outcomes:
39+
40+
1. The `blink` app is loaded successfully and functions without requiring a restart.
41+
2. The load process failed somewhere, and the app is erased.

examples/tests/app_loader/main.c

Lines changed: 123 additions & 183 deletions
Large diffs are not rendered by default.

libtock/internal/app_binaries.c

Lines changed: 1304 additions & 0 deletions
Large diffs are not rendered by default.

libtock/internal/app_binaries.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
#ifdef __cplusplus
4+
extern "C"
5+
{
6+
#endif
7+
8+
#include <stdint.h>
9+
10+
// app binaries
11+
extern uint8_t blink[2048];
12+
extern uint8_t adc[16384];
13+
14+
#ifdef __cplusplus
15+
}
16+
#endif

libtock/internal/app_loader.c

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
#include <stdio.h>
2-
#include <string.h>
3-
41
#include "internal/app_loader.h"
52
#include "tock.h"
63

@@ -9,11 +6,16 @@ int app_loader_exists(void) {
96
return tock_command_return_novalue_to_returncode(res);
107
}
118

12-
int app_loader_subscribe(subscribe_upcall cb, void *userdata) {
9+
int app_loader_setup_subscribe(subscribe_upcall cb, void *userdata) {
1310
subscribe_return_t sval = subscribe(DRIVER_NUM_APP_LOADER, 0, cb, userdata);
1411
return tock_subscribe_return_to_returncode(sval);
1512
}
1613

14+
int app_loader_write_subscribe(subscribe_upcall cb, void *userdata) {
15+
subscribe_return_t sval = subscribe(DRIVER_NUM_APP_LOADER, 1, cb, userdata);
16+
return tock_subscribe_return_to_returncode(sval);
17+
}
18+
1719
int app_loader_write_buffer(uint8_t *buffer, uint32_t len) {
1820
allow_ro_return_t aval = allow_readonly(DRIVER_NUM_APP_LOADER, 0, (void*) buffer, len);
1921
return tock_allow_ro_return_to_returncode(aval);
@@ -33,28 +35,3 @@ int app_loader_command_load(void) {
3335
syscall_return_t res = command(DRIVER_NUM_APP_LOADER, 3, 0, 0);
3436
return tock_command_return_novalue_to_returncode(res);
3537
}
36-
37-
int button_subscribe(subscribe_upcall callback, void *ud) {
38-
subscribe_return_t res = subscribe(DRIVER_NUM_BUTTON, 0, callback, ud);
39-
return tock_subscribe_return_to_returncode(res);
40-
}
41-
42-
int button_count(int* count) {
43-
syscall_return_t res = command(DRIVER_NUM_BUTTON, 0, 0, 0);
44-
return tock_command_return_u32_to_returncode(res, (uint32_t*) count);
45-
}
46-
47-
int button_enable_interrupt(int button_num) {
48-
syscall_return_t res = command(DRIVER_NUM_BUTTON, 1, button_num, 0);
49-
return tock_command_return_novalue_to_returncode(res);
50-
}
51-
52-
int button_disable_interrupt(int button_num) {
53-
syscall_return_t res = command(DRIVER_NUM_BUTTON, 2, button_num, 0);
54-
return tock_command_return_novalue_to_returncode(res);
55-
}
56-
57-
int button_read(int button_num, int* button_value) {
58-
syscall_return_t res = command(DRIVER_NUM_BUTTON, 3, button_num, 0);
59-
return tock_command_return_u32_to_returncode(res, (uint32_t*) button_value);
60-
}

libtock/internal/app_loader.h

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,22 @@ extern "C"
99

1010
// Driver number for system call
1111
#define DRIVER_NUM_APP_LOADER 0x50004
12-
#define DRIVER_NUM_BUTTON 0x3
13-
14-
// Button value macros
15-
#define BUTTON_1 0
16-
#define BUTTON_2 1
17-
1812

13+
#define BUTTON1 0
14+
#define BUTTON2 1
1915

2016
/*
2117
* Command to check if the `app_loader` capsule exists.
2218
*/
2319
int app_loader_exists(void);
2420

21+
/*
22+
* Function to setup the callback from capsule.
23+
* This function takes in the function that will be executed
24+
* when the callback is triggered.
25+
*/
26+
int app_loader_setup_subscribe(subscribe_upcall cb, void *userdata);
27+
2528
/*
2629
* Command to request the kernel to set up for a new app.
2730
* This functions takes the size of the new app as the argument.
@@ -33,7 +36,7 @@ int app_loader_command_setup(uint32_t app_length);
3336
* This function takes in the function that will be executed
3437
* when the callback is triggered.
3538
*/
36-
int app_loader_subscribe(subscribe_upcall cb, void *userdata);
39+
int app_loader_write_subscribe(subscribe_upcall cb, void *userdata);
3740

3841
/*
3942
* Function to set up a shared buffer with the capsule.
@@ -57,42 +60,10 @@ int app_loader_command_write(uint32_t flash_offset, uint32_t write_length);
5760
int app_loader_command_load(void);
5861

5962
/*
60-
* Internal function to write new app to flash.
61-
*/
62-
int write_app(void);
63-
64-
65-
/*
66-
* Function to setup the callback to be executed when
67-
* a button is pressed.
68-
*
69-
* This function takes in the function that will be executed
70-
* when the callback is triggered.
71-
*/
72-
int button_subscribe(subscribe_upcall callback, void *ud);
73-
74-
/*
75-
* Function to enable interrupts for the specified button number.
63+
* Internal function to write new app to flash. Takes app size as argument.
7664
*/
77-
int button_enable_interrupt(int button_num);
65+
int write_app(double size, uint8_t binary[]);
7866

79-
/*
80-
* Function to disable interrupts for the specified button number.
81-
*/
82-
int button_disable_interrupt(int button_num);
83-
84-
/*
85-
* Function to read the value of the button.
86-
*
87-
* `button_num` corresponds to which physical button is pressed.
88-
* `button_val` corresponds to whether it is pressed (1) or depressed (0)
89-
*/
90-
int button_read(int button_num, int* button_value);
91-
92-
/*
93-
* Function to count the number of buttons on a device.
94-
*/
95-
int button_count(int* count);
9667

9768
#ifdef __cplusplus
9869
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import os
2+
3+
def read_binary_file(file_name):
4+
parent_directory = os.path.dirname(os.getcwd())
5+
grandparent_directory = os.path.dirname(parent_directory)
6+
file_path = os.path.join(grandparent_directory, file_name)
7+
with open(file_path, 'rb') as file:
8+
binary_data = file.read()
9+
return binary_data
10+
11+
def binary_to_array(binary_data):
12+
hex_array = [hex(byte) for byte in binary_data]
13+
return hex_array
14+
15+
def print_hex_array(hex_array):
16+
print("Hexadecimal Array:")
17+
count = 0
18+
for hex_byte in hex_array:
19+
print(f"0x{hex_byte[2:].zfill(2)}", end=", ") # Add prefix '0x' and leading zero padding
20+
count += 1
21+
if count % 14 == 0:
22+
print() # Add a new line after every 14 elements
23+
if count % 14 != 0:
24+
print() # Add a new line if the last line is not complete
25+
26+
27+
file_name = 'examples/adc/build/cortex-m4/cortex-m4.tbf'
28+
binary_data = read_binary_file(file_name)
29+
hex_array = binary_to_array(binary_data)
30+
print_hex_array(hex_array)

0 commit comments

Comments
 (0)