|
| 1 | +#include "FlashIAP.h" |
| 2 | +#include "QSPIFBlockDevice.h" |
| 3 | +#include "MBRBlockDevice.h" |
| 4 | +#include "LittleFileSystem.h" |
| 5 | +#include "FATFileSystem.h" |
| 6 | +#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_OPTA) || defined(ARDUINO_GIGA) |
| 7 | +#include "ecdsa-p256-encrypt-key.h" |
| 8 | +#include "ecdsa-p256-signing-key.h" |
| 9 | +#else |
| 10 | +#error "Security features not available for this board" |
| 11 | +#endif |
| 12 | + |
| 13 | +#ifndef CORE_CM7 |
| 14 | + #error Update the bootloader by uploading the sketch to the M7 core instead of the M4 core. |
| 15 | +#endif |
| 16 | + |
| 17 | +#define BOOTLOADER_ADDR (0x8000000) |
| 18 | +#define SIGNING_KEY_ADDR (0x8000300) |
| 19 | +#define ENCRYPT_KEY_ADDR (0x8000400) |
| 20 | +#define ENCRYPT_KEY_SIZE (0x0000100) |
| 21 | +#define SIGNING_KEY_SIZE (0x0000100) |
| 22 | + |
| 23 | +mbed::FlashIAP flash; |
| 24 | +QSPIFBlockDevice root(QSPI_SO0, QSPI_SO1, QSPI_SO2, QSPI_SO3, QSPI_SCK, QSPI_CS, QSPIF_POLARITY_MODE_1, 40000000); |
| 25 | + |
| 26 | +bool writeKeys = false; |
| 27 | +uint32_t bootloader_data_offset = 0x1F000; |
| 28 | +uint8_t* bootloader_data = (uint8_t*)(BOOTLOADER_ADDR + bootloader_data_offset); |
| 29 | + |
| 30 | +uint32_t bootloader_identification_offset = 0x2F0; |
| 31 | +uint8_t* bootloader_identification = (uint8_t*)(BOOTLOADER_ADDR + bootloader_identification_offset); |
| 32 | + |
| 33 | +void setup() { |
| 34 | + Serial.begin(115200); |
| 35 | + while (!Serial) {} |
| 36 | + |
| 37 | + uint8_t currentBootloaderVersion = bootloader_data[1]; |
| 38 | + String currentBootloaderIdentifier = String(bootloader_identification, 15); |
| 39 | + |
| 40 | + if((currentBootloaderVersion > 24) && (currentBootloaderIdentifier.equals("MCUboot Arduino"))) { |
| 41 | + Serial.println("\nThe sketch comes with a set of default keys to evaluate signing and encryption process"); |
| 42 | + Serial.println("If you load the keys, you will need to upload the future sketches with Security Settings -> Signing + Encryption."); |
| 43 | + Serial.println("If you select Security Settings -> None, the sketches will not be executed."); |
| 44 | + Serial.println("Do you want to load the keys? Y/[n]"); |
| 45 | + if (waitResponse()) { |
| 46 | + Serial.println("\nPlease notice that loading the keys will enable MCUboot Sketch swap. This will increase the sketch update time after the upload."); |
| 47 | + Serial.println("A violet LED will blink until the sketch is ready to run."); |
| 48 | + Serial.println("Do you want to proceed loading the default keys? Y/[n]"); |
| 49 | + writeKeys = waitResponse(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + if (writeKeys) { |
| 54 | + applyUpdate(); |
| 55 | + } |
| 56 | + Serial.println("It's now safe to reboot or disconnect your board."); |
| 57 | +} |
| 58 | + |
| 59 | +void printProgress(uint32_t offset, uint32_t size, uint32_t threshold, bool reset) { |
| 60 | + static int percent_done = 0; |
| 61 | + if (reset == true) { |
| 62 | + percent_done = 0; |
| 63 | + Serial.println("Flashed " + String(percent_done) + "%"); |
| 64 | + } else { |
| 65 | + uint32_t percent_done_new = offset * 100 / size; |
| 66 | + if (percent_done_new >= percent_done + threshold) { |
| 67 | + percent_done = percent_done_new; |
| 68 | + Serial.println("Flashed " + String(percent_done) + "%"); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +bool waitResponse() { |
| 74 | + bool confirmation = false; |
| 75 | + while (confirmation == false) { |
| 76 | + if (Serial.available()) { |
| 77 | + char choice = Serial.read(); |
| 78 | + switch (choice) { |
| 79 | + case 'y': |
| 80 | + case 'Y': |
| 81 | + confirmation = true; |
| 82 | + return true; |
| 83 | + break; |
| 84 | + case 'n': |
| 85 | + case 'N': |
| 86 | + confirmation = true; |
| 87 | + return false; |
| 88 | + break; |
| 89 | + default: |
| 90 | + continue; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +void setupMCUBootOTAData() { |
| 97 | + mbed::MBRBlockDevice ota_data(&root, 2); |
| 98 | + mbed::FATFileSystem ota_data_fs("fs"); |
| 99 | + |
| 100 | + int err = ota_data_fs.reformat(&ota_data); |
| 101 | + if (err) { |
| 102 | + Serial.println("Error creating MCUboot files in OTA partition."); |
| 103 | + Serial.println("Run QSPIformat.ino sketch to format the QSPI flash and fix the issue."); |
| 104 | + } |
| 105 | + |
| 106 | + FILE* fp = fopen("/fs/scratch.bin", "wb"); |
| 107 | + const int scratch_file_size = 128 * 1024; |
| 108 | + const char buffer[128] = {0xFF}; |
| 109 | + int size = 0; |
| 110 | + |
| 111 | + Serial.println("\nCreating scratch file"); |
| 112 | + printProgress(size, scratch_file_size, 10, true); |
| 113 | + while (size < scratch_file_size) { |
| 114 | + int ret = fwrite(buffer, sizeof(buffer), 1, fp); |
| 115 | + if (ret != 1) { |
| 116 | + Serial.println("Error writing scratch file"); |
| 117 | + break; |
| 118 | + } |
| 119 | + size += sizeof(buffer); |
| 120 | + printProgress(size, scratch_file_size, 10, false); |
| 121 | + } |
| 122 | + fclose(fp); |
| 123 | + |
| 124 | + fp = fopen("/fs/update.bin", "wb"); |
| 125 | + const int update_file_size = 15 * 128 * 1024; |
| 126 | + size = 0; |
| 127 | + |
| 128 | + Serial.println("\nCreating update file"); |
| 129 | + printProgress(size, update_file_size, 10, true); |
| 130 | + while (size < update_file_size) { |
| 131 | + int ret = fwrite(buffer, sizeof(buffer), 1, fp); |
| 132 | + if (ret != 1) { |
| 133 | + Serial.println("Error writing scratch file"); |
| 134 | + break; |
| 135 | + } |
| 136 | + size += sizeof(buffer); |
| 137 | + printProgress(size, update_file_size, 5, false); |
| 138 | + } |
| 139 | + |
| 140 | + fclose(fp); |
| 141 | +} |
| 142 | + |
| 143 | +void applyUpdate() { |
| 144 | + flash.init(); |
| 145 | + setupMCUBootOTAData(); |
| 146 | + flash.program(&enc_priv_key, ENCRYPT_KEY_ADDR, ENCRYPT_KEY_SIZE); |
| 147 | + flash.program(&ecdsa_pub_key, SIGNING_KEY_ADDR, SIGNING_KEY_SIZE); |
| 148 | + Serial.println("Flashed 100%"); |
| 149 | + flash.deinit(); |
| 150 | + Serial.println("\nBootloader update complete. It's now safe to reboot or disconnect your board."); |
| 151 | +} |
| 152 | + |
| 153 | +void loop() { |
| 154 | + delay(1000); |
| 155 | +} |
0 commit comments