1
- /* mbed Microcontroller Library
2
- * Copyright (c) 2019 ARM Limited
1
+ /*
2
+ * Copyright (c) 2020 Embedded Planet
3
+ * Copyright (c) 2020 ARM Limited
3
4
* SPDX-License-Identifier: Apache-2.0
4
5
*/
5
6
6
7
#include " mbed.h"
7
8
8
- #include " bootutil.h"
9
+ #include " bootutil/bootutil.h"
10
+ #include " FlashIAP/FlashIAPBlockDevice.h"
11
+ #include " drivers/InterruptIn.h"
9
12
10
- // Change which LED and how fast we blink it for update binary
11
- #ifndef MCUBOOT_UPDATE
12
- #define LED LED1
13
- #define BLINKING_RATE 1000ms
14
- #else
15
- #define LED LED2
16
- #define BLINKING_RATE 250ms
17
- #endif
13
+ #define TRACE_GROUP " main"
14
+ #include " mbed-trace/mbed_trace.h"
15
+
16
+ mbed::BlockDevice* get_secondary_bd (void ) {
17
+ mbed::BlockDevice* default_bd = mbed::BlockDevice::get_default_instance ();
18
+ static mbed::SlicingBlockDevice sliced_bd (default_bd, 0x0 , MCUBOOT_SLOT_SIZE);
19
+ return &sliced_bd;
20
+ }
21
+
22
+ const static char version[] = " 1.0" ;
18
23
19
24
int main ()
20
25
{
26
+ // Enable traces from relevant trace groups
27
+ mbed_trace_init ();
28
+ mbed_trace_include_filters_set (" main,MCUb,BL" );
21
29
22
30
/* *
23
31
* Do whatever is needed to verify the firmware is okay
@@ -26,13 +34,97 @@ int main()
26
34
* And then mark that the update succeeded
27
35
*/
28
36
// run_self_test();
29
- boot_set_confirmed ();
37
+ int ret = boot_set_confirmed ();
38
+ if (ret == 0 ) {
39
+ tr_info (" Boot confirmed" );
40
+ } else {
41
+ tr_error (" Failed to confirm boot: %d" , ret);
42
+ }
43
+
44
+ InterruptIn btn (DEMO_BUTTON);
45
+
46
+ tr_info (" Hello version %s" , version);
47
+
48
+ // Erase secondary slot
49
+ // On the first boot, the secondary BlockDevice needs to be clean
50
+ // If the first boot is not normal, please run the erase step, then reboot
30
51
31
- // Initialise the digital pin LED1 as an output
32
- DigitalOut led (LED);
52
+ tr_info (" > Press button to erase secondary slot" );
53
+
54
+ #if DEMO_BUTTON_ACTIVE_LOW
55
+ while (btn) {
56
+ #else
57
+ while (!btn) {
58
+ #endif
59
+ sleep ();
60
+ }
61
+
62
+ BlockDevice *secondary_bd = get_secondary_bd ();
63
+ ret = secondary_bd->init ();
64
+ if (ret == 0 ) {
65
+ tr_info (" Secondary BlockDevice inited" );
66
+ } else {
67
+ tr_error (" Cannot init secondary BlockDevice: %d" , ret);
68
+ }
69
+
70
+ tr_info (" Erasing secondary BlockDevice..." );
71
+ ret = secondary_bd->erase (0 , secondary_bd->size ());
72
+ if (ret == 0 ) {
73
+ tr_info (" Secondary BlockDevice erased" );
74
+ } else {
75
+ tr_error (" Cannot erase secondary BlockDevice: %d" , ret);
76
+ }
77
+
78
+ tr_info (" > Press button to copy update image to secondary BlockDevice" );
79
+
80
+ #if DEMO_BUTTON_ACTIVE_LOW
81
+ while (btn) {
82
+ #else
83
+ while (!btn) {
84
+ #endif
85
+ sleep ();
86
+ }
87
+
88
+ // Copy the update image from internal flash to secondary BlockDevice
89
+ // This is a "hack" that requires you to preload the update image (i.e. with pyocd flash -a <address> <image>.bin)
90
+ // TODO: Use Serial OTA to fetch image
91
+
92
+ FlashIAPBlockDevice fbd (MCUBOOT_PRIMARY_SLOT_START_ADDR + 0x40000 , 0x20000 );
93
+ ret = fbd.init ();
94
+ if (ret == 0 ) {
95
+ tr_info (" FlashIAPBlockDevice inited" );
96
+ } else {
97
+ tr_error (" Cannot init FlashIAPBlockDevice: %d" , ret);
98
+ }
99
+
100
+ static uint8_t buffer[0x1000 ];
101
+ for (size_t offset = 0 ; offset < 0x20000 ; offset+= sizeof (buffer)) {
102
+ ret = fbd.read (buffer, offset, sizeof (buffer));
103
+ if (ret != 0 ) {
104
+ tr_error (" Failed to read FlashIAPBlockDevice at offset %u" , offset);
105
+ }
106
+ ret = secondary_bd->program (buffer, offset, sizeof (buffer));
107
+ if (ret != 0 ) {
108
+ tr_error (" Failed to program secondary BlockDevice at offset %u" , offset);
109
+ }
110
+ }
111
+
112
+ // Activate the image in the secondary BlockDevice
113
+
114
+ tr_info (" > Image copied to secondary BlockDevice, press button to activate" );
115
+
116
+ #if DEMO_BUTTON_ACTIVE_LOW
117
+ while (btn) {
118
+ #else
119
+ while (!btn) {
120
+ #endif
121
+ sleep ();
122
+ }
33
123
34
- while (true ) {
35
- led = !led;
36
- ThisThread::sleep_for (BLINKING_RATE);
124
+ ret = boot_set_pending (false );
125
+ if (ret == 0 ) {
126
+ tr_info (" > Secondary image pending, reboot to update" );
127
+ } else {
128
+ tr_error (" Failed to set secondary image pending: %d" , ret);
37
129
}
38
130
}
0 commit comments