8
8
#include " portenta_bootloader.h"
9
9
#include " portenta_lite_bootloader.h"
10
10
#include " portenta_lite_connected_bootloader.h"
11
- #include " mcuboot_bootloader.h"
12
- #include " ecdsa-p256-encrypt-key.h"
13
- #include " ecdsa-p256-signing-key.h"
14
11
#define GET_OTP_BOARD_INFO
15
12
#elif defined(ARDUINO_NICLA_VISION)
16
13
#include " nicla_vision_bootloader.h"
17
14
#elif defined(ARDUINO_OPTA)
18
15
#include " opta_bootloader.h"
19
- #include " mcuboot_opta.h"
20
- #include " ecdsa-p256-encrypt-key.h"
21
- #include " ecdsa-p256-signing-key.h"
22
16
#endif
23
17
24
18
#ifndef CORE_CM7
25
19
#error Update the bootloader by uploading the sketch to the M7 core instead of the M4 core.
26
20
#endif
27
21
28
22
#define BOOTLOADER_ADDR (0x8000000 )
29
- #define SIGNING_KEY_ADDR (0x8000300 )
30
- #define ENCRYPT_KEY_ADDR (0x8000400 )
31
- #define ENCRYPT_KEY_SIZE (0x0000100 )
32
- #define SIGNING_KEY_SIZE (0x0000100 )
33
23
34
24
mbed::FlashIAP flash;
35
25
QSPIFBlockDevice root (QSPI_SO0, QSPI_SO1, QSPI_SO2, QSPI_SO3, QSPI_SCK, QSPI_CS, QSPIF_POLARITY_MODE_1, 40000000 );
36
26
37
27
bool writeLoader = false ;
38
- bool writeKeys = false ;
39
28
bool video_available = false ;
40
29
bool wifi_available = false ;
41
- bool MCUboot = false ;
42
30
43
31
uint32_t bootloader_data_offset = 0x1F000 ;
44
32
uint8_t * bootloader_data = (uint8_t *)(BOOTLOADER_ADDR + bootloader_data_offset);
45
33
46
- uint32_t bootloader_identification_offset = 0x2F0 ;
47
- uint8_t * bootloader_identification = (uint8_t *)(BOOTLOADER_ADDR + bootloader_identification_offset);
48
-
49
- const unsigned char * bootloader_ptr = &bootloader_mbed_bin[0 ];
50
- long bootloader_len = bootloader_mbed_bin_len;
34
+ const unsigned char * bootloader_ptr = &bootloader_bin[0 ];
35
+ long bootloader_len = bootloader_bin_len;
51
36
52
37
uint8_t * boardInfo ();
53
38
@@ -56,13 +41,7 @@ void setup() {
56
41
while (!Serial) {}
57
42
58
43
uint8_t currentBootloaderVersion = bootloader_data[1 ];
59
- String currentBootloaderIdentifier = String (bootloader_identification, 15 );
60
-
61
- if (!currentBootloaderIdentifier.equals (" MCUboot Arduino" )) {
62
- currentBootloaderIdentifier = " Arduino loader" ;
63
- }
64
44
65
- Serial.println (currentBootloaderIdentifier);
66
45
Serial.println (" Magic Number (validation): " + String (bootloader_data[0 ], HEX));
67
46
Serial.println (" Bootloader version: " + String (currentBootloaderVersion));
68
47
Serial.println (" Clock source: " + getClockSource (bootloader_data[2 ]));
@@ -91,82 +70,35 @@ void setup() {
91
70
video_available = bootloader_data[8 ];
92
71
wifi_available = bootloader_data[5 ];
93
72
94
- #if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_OPTA)
95
- Serial.println (" \n Do you want to install/update the default Arduino bootloader? Y/[n]" );
96
- Serial.println (" Choosing \" No\" , will install/update the MCUboot bootloader." );
97
- if (!waitResponse ()) {
98
- Serial.println (" \n MCUboot has been selected. Do you want to proceed? Y/[n]" );
99
- if (waitResponse ()) {
100
- MCUboot = true ;
101
- bootloader_ptr = &mcuboot_bin[0 ];
102
- bootloader_len = mcuboot_bin_len;
103
- } else {
104
- Serial.println (" \n Proceeding with the default Arduino bootloader..." );
105
- }
106
- }
107
- if (!MCUboot) {
108
- bootloader_ptr = &bootloader_mbed_bin[0 ];
109
- bootloader_len = bootloader_mbed_bin_len;
110
73
#if defined(ARDUINO_PORTENTA_H7_M7)
111
- if (!video_available) {
112
- if (wifi_available) {
113
- bootloader_ptr = &bootloader_mbed_lite_connected_bin[0 ];
114
- bootloader_len = bootloader_mbed_lite_connected_bin_len;
115
- } else {
116
- bootloader_ptr = &bootloader_mbed_lite_bin[0 ];
117
- bootloader_len = bootloader_mbed_lite_bin_len;
118
- }
74
+ if (!video_available) {
75
+ if (wifi_available) {
76
+ bootloader_ptr = &bootloader_lite_connected_bin[0 ];
77
+ bootloader_len = bootloader_lite_connected_bin_len;
78
+ } else {
79
+ bootloader_ptr = &bootloader_lite_bin[0 ];
80
+ bootloader_len = bootloader_lite_bin_len;
119
81
}
120
- #endif
121
82
}
122
83
#endif
123
84
124
85
uint8_t availableBootloaderVersion = (bootloader_ptr + bootloader_data_offset)[1 ];
125
- String availableBootloaderIdentifier = String (bootloader_ptr + bootloader_identification_offset, 15 );
126
86
127
- if (!availableBootloaderIdentifier.equals (" MCUboot Arduino" )) {
128
- availableBootloaderIdentifier = " Arduino loader" ;
129
- }
130
-
131
- if (currentBootloaderIdentifier == availableBootloaderIdentifier) {
132
- if (bootloader_data[0 ] != 0xA0 ) {
133
- Serial.println (" \n A new bootloader version (v" + String (availableBootloaderVersion) + " ) is available." );
134
- Serial.println (" Do you want to update the bootloader? Y/[n]" );
135
- } else {
136
- if (availableBootloaderVersion > currentBootloaderVersion) {
137
- Serial.print (" \n A new bootloader version is available: v" + String (availableBootloaderVersion));
138
- Serial.println (" (Your version: v" + String (currentBootloaderVersion) + " )" );
139
- Serial.println (" Do you want to update the bootloader? Y/[n]" );
140
- } else if (availableBootloaderVersion < currentBootloaderVersion) {
141
- Serial.println (" \n A newer bootloader version is already installed: v" + String (currentBootloaderVersion));
142
- Serial.println (" Do you want to downgrade the bootloader to v" + String (availableBootloaderVersion) + " ? Y/[n]" );
143
- } else {
144
- Serial.println (" \n The latest version of the bootloader is already installed (v" + String (currentBootloaderVersion) + " )." );
145
- Serial.println (" Do you want to update the bootloader anyway? Y/[n]" );
146
- }
147
- }
148
- } else {
149
- Serial.println (" \n A different bootloader type is available: v" + String (availableBootloaderVersion));
87
+ if (availableBootloaderVersion > currentBootloaderVersion) {
88
+ Serial.print (" \n A new bootloader version is available: v" + String (availableBootloaderVersion));
89
+ Serial.println (" (Your version: v" + String (currentBootloaderVersion) + " )" );
150
90
Serial.println (" Do you want to update the bootloader? Y/[n]" );
91
+ } else if (availableBootloaderVersion < currentBootloaderVersion) {
92
+ Serial.println (" \n A newer bootloader version is already installed: v" + String (currentBootloaderVersion));
93
+ Serial.println (" Do you want to downgrade the bootloader to v" + String (availableBootloaderVersion) + " ? Y/[n]" );
94
+ } else {
95
+ Serial.println (" \n The latest version of the bootloader is already installed (v" + String (currentBootloaderVersion) + " )." );
96
+ Serial.println (" Do you want to update the bootloader anyway? Y/[n]" );
151
97
}
98
+
152
99
writeLoader = waitResponse ();
153
100
154
101
if (writeLoader) {
155
- if (availableBootloaderIdentifier.equals (" MCUboot Arduino" )) {
156
-
157
- Serial.println (" \n The bootloader comes with a set of default keys to evaluate signing and encryption process" );
158
- Serial.println (" If you load the keys, you will need to upload the future sketches with Security Settings -> Signing + Encryption." );
159
- Serial.println (" If you select Security Settings -> None, the sketches will not be executed." );
160
- Serial.println (" Do you want to load the keys? Y/[n]" );
161
- if (waitResponse ()) {
162
- Serial.println (" \n Please notice that loading the keys will enable MCUboot Sketch swap. This will increase the sketch update time after the upload." );
163
- Serial.println (" A violet LED will blink until the sketch is ready to run." );
164
- Serial.println (" Do you want to proceed loading the default keys? Y/[n]" );
165
- writeKeys = waitResponse ();
166
- } else {
167
- writeKeys = false ;
168
- }
169
- }
170
102
applyUpdate (BOOTLOADER_ADDR);
171
103
} else {
172
104
Serial.println (" It's now safe to reboot or disconnect your board." );
@@ -242,53 +174,6 @@ bool waitResponse() {
242
174
}
243
175
}
244
176
245
- void setupMCUBootOTAData () {
246
- mbed::MBRBlockDevice ota_data (&root, 2 );
247
- mbed::FATFileSystem ota_data_fs (" fs" );
248
-
249
- int err = ota_data_fs.reformat (&ota_data);
250
- if (err) {
251
- Serial.println (" Error creating MCUboot files in OTA partition." );
252
- Serial.println (" Run QSPIformat.ino sketch to format the QSPI flash and fix the issue." );
253
- }
254
-
255
- FILE* fp = fopen (" /fs/scratch.bin" , " wb" );
256
- const int scratch_file_size = 128 * 1024 ;
257
- const char buffer[128 ] = {0xFF };
258
- int size = 0 ;
259
-
260
- Serial.println (" \n Creating scratch file" );
261
- printProgress (size, scratch_file_size, 10 , true );
262
- while (size < scratch_file_size) {
263
- int ret = fwrite (buffer, sizeof (buffer), 1 , fp);
264
- if (ret != 1 ) {
265
- Serial.println (" Error writing scratch file" );
266
- break ;
267
- }
268
- size += sizeof (buffer);
269
- printProgress (size, scratch_file_size, 10 , false );
270
- }
271
- fclose (fp);
272
-
273
- fp = fopen (" /fs/update.bin" , " wb" );
274
- const int update_file_size = 15 * 128 * 1024 ;
275
- size = 0 ;
276
-
277
- Serial.println (" \n Creating update file" );
278
- printProgress (size, update_file_size, 10 , true );
279
- while (size < update_file_size) {
280
- int ret = fwrite (buffer, sizeof (buffer), 1 , fp);
281
- if (ret != 1 ) {
282
- Serial.println (" Error writing scratch file" );
283
- break ;
284
- }
285
- size += sizeof (buffer);
286
- printProgress (size, update_file_size, 5 , false );
287
- }
288
-
289
- fclose (fp);
290
- }
291
-
292
177
void applyUpdate (uint32_t address) {
293
178
long len = bootloader_len;
294
179
@@ -332,14 +217,6 @@ void applyUpdate(uint32_t address) {
332
217
}
333
218
}
334
219
335
- #if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_OPTA)
336
- if (writeKeys) {
337
- setupMCUBootOTAData ();
338
- flash.program (&enc_priv_key, ENCRYPT_KEY_ADDR, ENCRYPT_KEY_SIZE);
339
- flash.program (&ecdsa_pub_key, SIGNING_KEY_ADDR, SIGNING_KEY_SIZE);
340
- }
341
- #endif
342
-
343
220
Serial.println (" Flashed 100%" );
344
221
345
222
delete[] page_buffer;
0 commit comments