88#include " portenta_bootloader.h"
99#include " portenta_lite_bootloader.h"
1010#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"
1411#define GET_OTP_BOARD_INFO
1512#elif defined(ARDUINO_NICLA_VISION)
1613#include " nicla_vision_bootloader.h"
1714#elif defined(ARDUINO_OPTA)
1815#include " opta_bootloader.h"
19- #include " mcuboot_opta.h"
20- #include " ecdsa-p256-encrypt-key.h"
21- #include " ecdsa-p256-signing-key.h"
2216#endif
2317
2418#ifndef CORE_CM7
2519 #error Update the bootloader by uploading the sketch to the M7 core instead of the M4 core.
2620#endif
2721
2822#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 )
3323
3424mbed::FlashIAP flash;
3525QSPIFBlockDevice root (QSPI_SO0, QSPI_SO1, QSPI_SO2, QSPI_SO3, QSPI_SCK, QSPI_CS, QSPIF_POLARITY_MODE_1, 40000000 );
3626
3727bool writeLoader = false ;
38- bool writeKeys = false ;
3928bool video_available = false ;
4029bool wifi_available = false ;
41- bool MCUboot = false ;
4230
4331uint32_t bootloader_data_offset = 0x1F000 ;
4432uint8_t * bootloader_data = (uint8_t *)(BOOTLOADER_ADDR + bootloader_data_offset);
4533
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;
5136
5237uint8_t * boardInfo ();
5338
@@ -56,13 +41,7 @@ void setup() {
5641 while (!Serial) {}
5742
5843 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- }
6444
65- Serial.println (currentBootloaderIdentifier);
6645 Serial.println (" Magic Number (validation): " + String (bootloader_data[0 ], HEX));
6746 Serial.println (" Bootloader version: " + String (currentBootloaderVersion));
6847 Serial.println (" Clock source: " + getClockSource (bootloader_data[2 ]));
@@ -91,82 +70,35 @@ void setup() {
9170 video_available = bootloader_data[8 ];
9271 wifi_available = bootloader_data[5 ];
9372
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;
11073#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;
11981 }
120- #endif
12182 }
12283#endif
12384
12485 uint8_t availableBootloaderVersion = (bootloader_ptr + bootloader_data_offset)[1 ];
125- String availableBootloaderIdentifier = String (bootloader_ptr + bootloader_identification_offset, 15 );
12686
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) + " )" );
15090 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]" );
15197 }
98+
15299 writeLoader = waitResponse ();
153100
154101 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- }
170102 applyUpdate (BOOTLOADER_ADDR);
171103 } else {
172104 Serial.println (" It's now safe to reboot or disconnect your board." );
@@ -242,53 +174,6 @@ bool waitResponse() {
242174 }
243175}
244176
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-
292177void applyUpdate (uint32_t address) {
293178 long len = bootloader_len;
294179
@@ -332,14 +217,6 @@ void applyUpdate(uint32_t address) {
332217 }
333218 }
334219
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-
343220 Serial.println (" Flashed 100%" );
344221
345222 delete[] page_buffer;
0 commit comments