|
| 1 | +/* |
| 2 | + Portenta C33 - Certificate uploader |
| 3 | +
|
| 4 | + The sketch uploads and saves network certificates on the system |
| 5 | + partition of the QSPI flash |
| 6 | +
|
| 7 | + This example code is in the public domain. |
| 8 | +*/ |
| 9 | + |
| 10 | +#include "BlockDevice.h" |
| 11 | +#include "MBRBlockDevice.h" |
| 12 | +#include "FATFileSystem.h" |
| 13 | +#include "certificates.h" |
| 14 | +#include "ymodem.h" |
| 15 | + |
| 16 | +BlockDevice* root = BlockDevice::get_default_instance(); |
| 17 | +MBRBlockDevice sys_bd(root, 1); |
| 18 | +FATFileSystem sys_fs("sys"); |
| 19 | + |
| 20 | +char filename[256] = {'\0'}; |
| 21 | + |
| 22 | +long getFileLen(FILE *file) { |
| 23 | + fseek(file, 0, SEEK_END); |
| 24 | + long len = ftell(file); |
| 25 | + fseek(file, 0, SEEK_SET); |
| 26 | + //Decrement len by 1 to remove the CRC from the count |
| 27 | + return len; |
| 28 | +} |
| 29 | + |
| 30 | +long getFileSize(FILE *fp) { |
| 31 | + fseek(fp, 0, SEEK_END); |
| 32 | + int size = ftell(fp); |
| 33 | + fseek(fp, 0, SEEK_SET); |
| 34 | + |
| 35 | + return size; |
| 36 | +} |
| 37 | + |
| 38 | +// void printProgress(uint32_t offset, uint32_t size, uint32_t threshold, bool reset) { |
| 39 | +// static int percent_done = 0; |
| 40 | +// if (reset == true) { |
| 41 | +// percent_done = 0; |
| 42 | +// Serial.println("Flashed " + String(percent_done) + "%"); |
| 43 | +// } else { |
| 44 | +// uint32_t percent_done_new = offset * 100 / size; |
| 45 | +// if (percent_done_new >= percent_done + threshold) { |
| 46 | +// percent_done = percent_done_new; |
| 47 | +// Serial.println("Flashed " + String(percent_done) + "%"); |
| 48 | +// } |
| 49 | +// } |
| 50 | +// } |
| 51 | + |
| 52 | +void setup() { |
| 53 | + |
| 54 | + Serial.begin(115200); |
| 55 | + // while (!Serial); |
| 56 | + |
| 57 | + int err = sys_fs.mount(&sys_bd); |
| 58 | + if (err) { |
| 59 | + // Reformat if we can't mount the filesystem |
| 60 | + // this should only happen on the first boot |
| 61 | + // Serial.println("No filesystem containing the WiFi firmware was found."); |
| 62 | + // Serial.println("Usually that means that the WiFi firmware has not been installed yet" |
| 63 | + // " or was overwritten with another firmware.\n"); |
| 64 | + // Serial.println("Formatting the filsystem to install the firmware and certificates...\n"); |
| 65 | + err = sys_fs.reformat(&sys_bd); |
| 66 | + } |
| 67 | + |
| 68 | + // DIR *dir; |
| 69 | + // struct dirent *ent; |
| 70 | + |
| 71 | + // if ((dir = opendir("/sys")) != NULL) { |
| 72 | + // /* print all the files and directories within directory */ |
| 73 | + // while ((ent = readdir (dir)) != NULL) { |
| 74 | + // Serial.println("Searching for WiFi firmware file " + String(ent->d_name) + " ..."); |
| 75 | + // String fullname = "/sys/" + String(ent->d_name); |
| 76 | + // if (fullname == "/sys/cacert.pem") { |
| 77 | + // Serial.println("A WiFi firmware is already installed. " |
| 78 | + // "Do you want to install the firmware anyway? Y/[n]"); |
| 79 | + // while (1) { |
| 80 | + // if (Serial.available()) { |
| 81 | + // int c = Serial.read(); |
| 82 | + // if (c == 'Y' || c == 'y') { |
| 83 | + // sys_fs.reformat(&sys_bd); |
| 84 | + // break; |
| 85 | + // } |
| 86 | + // if (c == 'N' || c == 'n') { |
| 87 | + // Serial.println("It's now safe to reboot or disconnect your board."); |
| 88 | + // return; |
| 89 | + // } |
| 90 | + // } |
| 91 | + // } |
| 92 | + // } |
| 93 | + // } |
| 94 | + // closedir (dir); |
| 95 | + // } |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + int chunk_size = 128; |
| 100 | + int byte_count = 0; |
| 101 | + FILE* fp = fopen("/sys/cacert.pem", "wb"); |
| 102 | + |
| 103 | + // Serial.println("Flashing certificates"); |
| 104 | + // printProgress(byte_count, cacert_pem_len, 10, true); |
| 105 | + while (byte_count < cacert_pem_len) { |
| 106 | + if(byte_count + chunk_size > cacert_pem_len) |
| 107 | + chunk_size = cacert_pem_len - byte_count; |
| 108 | + int ret = fwrite(&cacert_pem[byte_count], chunk_size, 1 ,fp); |
| 109 | + if (ret != 1) { |
| 110 | + // Serial.println("Error writing certificates"); |
| 111 | + break; |
| 112 | + } |
| 113 | + byte_count += chunk_size; |
| 114 | + // printProgress(byte_count, cacert_pem_len, 10, false); |
| 115 | + } |
| 116 | + fclose(fp); |
| 117 | + |
| 118 | + // fp = fopen("/sys/cacert.pem", "rb"); |
| 119 | + // char buffer[128]; |
| 120 | + // int ret = fread(buffer, 1, 128, fp); |
| 121 | + // Serial.write(buffer, ret); |
| 122 | + // while (ret == 128) { |
| 123 | + // ret = fread(buffer, 1, 128, fp); |
| 124 | + // Serial.write(buffer, ret); |
| 125 | + // } |
| 126 | + // fclose(fp); |
| 127 | + |
| 128 | + // Serial.println("\nFirmware and certificates updated!"); |
| 129 | + // Serial.println("It's now safe to reboot or disconnect your board."); |
| 130 | +} |
| 131 | + |
| 132 | +void loop() { |
| 133 | + |
| 134 | + uint8_t command = 0xFF; |
| 135 | + |
| 136 | + if (Serial.available()) { |
| 137 | + command = Serial.read(); |
| 138 | + } |
| 139 | + |
| 140 | + if (command == 'Y') { |
| 141 | + FILE* f = fopen("/sys/temp.bin", "wb"); |
| 142 | + while (Serial.available()) { |
| 143 | + Serial.read(); |
| 144 | + } |
| 145 | + Serial.print("Y"); |
| 146 | + int ret = Ymodem_Receive(f, 1024 * 1024, filename); |
| 147 | + String name = String(filename); |
| 148 | + if (ret > 0 && name != "") { |
| 149 | + name = "/sys/" + name; |
| 150 | + fclose(f); |
| 151 | + ret = rename("/sys/temp.bin", name.c_str()); |
| 152 | + } |
| 153 | + } |
| 154 | + if (command == 'R') { |
| 155 | + String filename = Serial.readStringUntil('\r'); |
| 156 | + filename.trim(); |
| 157 | + String filename_abs = String("/sys/") + filename; |
| 158 | + FILE* f = fopen(filename_abs.c_str(), "rb"); |
| 159 | + while (Serial.available()) { |
| 160 | + Serial.read(); |
| 161 | + } |
| 162 | + if (f != NULL) { |
| 163 | + Serial.print("R"); |
| 164 | + int ret = Ymodem_Transmit((char*)filename.c_str(), getFileLen(f), f); |
| 165 | + fclose(f); |
| 166 | + } |
| 167 | + } |
| 168 | + if (command == 0xFF) { |
| 169 | + delay(10); |
| 170 | + } |
| 171 | +} |
0 commit comments