|
1 | | -#include"QSPIFBlockDevice.h" |
| 1 | +#include"BlockDevice.h" |
2 | 2 | #include"MBRBlockDevice.h" |
3 | 3 | #include"LittleFileSystem.h" |
4 | 4 | #include"FATFileSystem.h" |
| 5 | +#include"wiced_resource.h" |
| 6 | +#include"certificates.h" |
5 | 7 |
|
6 | | -#ifndef CORE_CM7 |
| 8 | +#ifndef CORE_CM7 |
7 | 9 | #error Format QSPI flash by uploading the sketch to the M7 core instead of the M4 core. |
8 | 10 | #endif |
9 | 11 |
|
| 12 | +usingnamespacembed; |
10 | 13 |
|
11 | | -QSPIFBlockDeviceroot(QSPI_SO0, QSPI_SO1, QSPI_SO2, QSPI_SO3, QSPI_SCK, QSPI_CS, QSPIF_POLARITY_MODE_1,40000000); |
12 | | -mbed::MBRBlockDevicewifi_data(&root,1); |
13 | | -mbed::MBRBlockDeviceota_data(&root,2); |
14 | | -mbed::MBRBlockDeviceuser_data(&root,3); |
15 | | -mbed::FATFileSystemwifi_data_fs("wlan"); |
16 | | -mbed::FATFileSystemota_data_fs("fs"); |
17 | | -mbed::FileSystem * user_data_fs; |
| 14 | +BlockDevice* root = BlockDevice::get_default_instance(); |
| 15 | +MBRBlockDevicewifi_data(root,1); |
| 16 | +MBRBlockDeviceota_data(root,2); |
| 17 | +MBRBlockDevicekvstore_data(root,3); |
| 18 | +MBRBlockDeviceuser_data(root,4); |
| 19 | +FATFileSystemwifi_data_fs("wlan"); |
| 20 | +FATFileSystemota_data_fs("fs"); |
| 21 | +FileSystem * user_data_fs; |
18 | 22 |
|
19 | 23 | boolwaitResponse() { |
20 | 24 | bool confirmation =false; |
| 25 | +bool proceed =false; |
21 | 26 | while (confirmation ==false) { |
22 | 27 | if (Serial.available()) { |
23 | 28 | char choice = Serial.read(); |
24 | 29 | switch (choice) { |
25 | 30 | case'y': |
26 | 31 | case'Y': |
27 | 32 | confirmation =true; |
28 | | -returntrue; |
| 33 | +proceed =true; |
29 | 34 | break; |
30 | 35 | case'n': |
31 | 36 | case'N': |
32 | 37 | confirmation =true; |
33 | | -returnfalse; |
| 38 | +proceed =false; |
34 | 39 | break; |
35 | 40 | default: |
36 | 41 | continue; |
37 | 42 | } |
38 | 43 | } |
39 | 44 | } |
| 45 | +return proceed; |
| 46 | +} |
| 47 | + |
| 48 | +voidprintProgress(uint32_t offset,uint32_t size,uint32_t threshold,bool reset) { |
| 49 | +staticint percent_done =0; |
| 50 | +if (reset ==true) { |
| 51 | + percent_done =0; |
| 52 | + Serial.println("Flashed" +String(percent_done) +"%"); |
| 53 | + }else { |
| 54 | +uint32_t percent_done_new = offset *100 / size; |
| 55 | +if (percent_done_new >= percent_done + threshold) { |
| 56 | + percent_done = percent_done_new; |
| 57 | + Serial.println("Flashed" +String(percent_done) +"%"); |
| 58 | + } |
| 59 | + } |
40 | 60 | } |
41 | 61 |
|
42 | 62 | voidsetup() { |
43 | 63 |
|
44 | 64 | Serial.begin(115200); |
45 | 65 | while (!Serial); |
46 | 66 |
|
47 | | - Serial.println("Available partition schemes:"); |
48 | | - Serial.println("\nPartition scheme 1"); |
49 | | - Serial.println("Partition 1: WiFi firmware and certificates 1MB"); |
50 | | - Serial.println("Partition 2: OTA and user data 13MB"); |
51 | | - Serial.println("\nPartition scheme 2"); |
| 67 | + Serial.println("\nWARNING! Running the sketch all the content of the QSPI flash will be erased."); |
| 68 | + Serial.println("The following partitions will be created:"); |
52 | 69 | Serial.println("Partition 1: WiFi firmware and certificates 1MB"); |
53 | 70 | Serial.println("Partition 2: OTA 5MB"); |
54 | | - Serial.println("Partition 3: User data 8MB"), |
55 | | - Serial.println("\nDo you want to use partition scheme 1? Y/[n]"); |
56 | | - Serial.println("If No, partition scheme 2 will be used."); |
57 | | -bool default_scheme =waitResponse(); |
58 | | - |
59 | | - Serial.println("\nWARNING! Running the sketch all the content of the QSPI flash will be erased."); |
| 71 | + Serial.println("Partition 3: Provisioning KVStore 1MB"); |
| 72 | + Serial.println("Partition 4: User data / OPTA PLC runtime 7MB"), |
60 | 73 | Serial.println("Do you want to proceed? Y/[n]"); |
61 | 74 |
|
62 | 75 | if (true ==waitResponse()) { |
63 | | -mbed::MBRBlockDevice::partition(&root,1,0x0B,0,1024 *1024); |
64 | | -if(default_scheme) { |
65 | | -mbed::MBRBlockDevice::partition(&root,3,0x0B,14 *1024 *1024,14 *1024 *1024); |
66 | | -mbed::MBRBlockDevice::partition(&root,2,0x0B,1024 *1024,14 *1024 *1024); |
67 | | -// use space from 15.5MB to 16 MB for another fw, memory mapped |
| 76 | +if (root->init() != BD_ERROR_OK) { |
| 77 | + Serial.println(F("Error: QSPI init failure.")); |
| 78 | +return; |
| 79 | + } |
| 80 | + |
| 81 | + Serial.println("Do you want to perform a full erase of the QSPI flash before proceeding? Y/[n]"); |
| 82 | +if (true ==waitResponse()) { |
| 83 | + root->erase(0x0, root->size()); |
68 | 84 | }else { |
69 | | -mbed::MBRBlockDevice::partition(&root,2,0x0B,1024 *1024,6 *1024 *1024); |
70 | | -mbed::MBRBlockDevice::partition(&root,3,0x0B,6 *1024 *1024,14 *1024 *1024); |
71 | | -// use space from 15.5MB to 16 MB for another fw, memory mapped |
| 85 | +// Erase only the first sector containing the MBR |
| 86 | + root->erase(0x0, root->get_erase_size()); |
| 87 | + } |
| 88 | + |
| 89 | +MBRBlockDevice::partition(root,1,0x0B,0,1 *1024 *1024); |
| 90 | +MBRBlockDevice::partition(root,2,0x0B,1 *1024 *1024,6 *1024 *1024); |
| 91 | +MBRBlockDevice::partition(root,3,0x0B,6 *1024 *1024,7 *1024 *1024); |
| 92 | +MBRBlockDevice::partition(root,4,0x0B,7 *1024 *1024,14 *1024 *1024); |
| 93 | +// use space from 15.5MB to 16 MB for another fw, memory mapped |
| 94 | + |
| 95 | +bool reformat =true; |
| 96 | +if(!wifi_data_fs.mount(&wifi_data)) { |
| 97 | + Serial.println("\nPartition 1 already contains a filesystem, do you want to reformat it? Y/[n]"); |
| 98 | + wifi_data_fs.unmount(); |
| 99 | + |
| 100 | + reformat =waitResponse(); |
72 | 101 | } |
73 | 102 |
|
74 | | -int err = wifi_data_fs.reformat(&wifi_data); |
75 | | -if (err) { |
| 103 | +if (reformat && wifi_data_fs.reformat(&wifi_data)) { |
76 | 104 | Serial.println("Error formatting WiFi partition"); |
77 | 105 | return; |
78 | 106 | } |
79 | | - |
80 | | - err = ota_data_fs.reformat(&ota_data); |
81 | | -if (err) { |
| 107 | + |
| 108 | +bool restore =true; |
| 109 | +if (reformat) { |
| 110 | + Serial.println("\nDo you want to restore the WiFi firmware and certificates? Y/[n]"); |
| 111 | + restore =waitResponse(); |
| 112 | + } |
| 113 | + |
| 114 | +if (reformat && restore) { |
| 115 | +flashWiFiFirmwareAndCertificates(); |
| 116 | + } |
| 117 | + |
| 118 | + reformat =true; |
| 119 | +if(!ota_data_fs.mount(&ota_data)) { |
| 120 | + Serial.println("\nPartition 2 already contains a filesystem, do you want to reformat it? Y/[n]"); |
| 121 | + ota_data_fs.unmount(); |
| 122 | + |
| 123 | + reformat =waitResponse(); |
| 124 | + } |
| 125 | + |
| 126 | +if (reformat && ota_data_fs.reformat(&ota_data)) { |
82 | 127 | Serial.println("Error formatting OTA partition"); |
83 | 128 | return; |
84 | 129 | } |
85 | 130 |
|
86 | | -if(!default_scheme) { |
87 | | - Serial.println("\nDo you want to use LittleFS to format user data partition? Y/[n]"); |
88 | | - Serial.println("If No, FatFS will be used to format user partition."); |
| 131 | + Serial.println("\nDo you want to use LittleFS to format user data partition? Y/[n]"); |
| 132 | + Serial.println("If No, FatFS will be used to format user partition."); |
| 133 | + Serial.println("Note: LittleFS is not supported by the OPTA PLC runtime."); |
| 134 | +if (true ==waitResponse()) { |
| 135 | + Serial.println("Formatting user partition with LittleFS."); |
| 136 | + user_data_fs =newmbed::LittleFileSystem("user"); |
| 137 | + }else { |
| 138 | + Serial.println("Formatting user partition with FatFS."); |
| 139 | + user_data_fs =newmbed::FATFileSystem("user"); |
| 140 | + } |
89 | 141 |
|
90 | | -if (true ==waitResponse()) { |
91 | | - Serial.println("Formatting user partition with LittleFS."); |
92 | | - user_data_fs =newmbed::LittleFileSystem("user"); |
93 | | - }else { |
94 | | - Serial.println("Formatting user partition with FatFS."); |
95 | | - user_data_fs =newmbed::FATFileSystem("user"); |
96 | | - } |
| 142 | + reformat =true; |
| 143 | +if(!user_data_fs->mount(&user_data)) { |
| 144 | + Serial.println("\nPartition 4 already contains a filesystem, do you want to reformat it? Y/[n]"); |
| 145 | + user_data_fs->unmount(); |
97 | 146 |
|
98 | | - err = user_data_fs->reformat(&user_data); |
99 | | -if (err) { |
100 | | - Serial.println("Error formatting user partition"); |
101 | | -return; |
102 | | - } |
| 147 | + reformat =waitResponse(); |
| 148 | + } |
| 149 | + |
| 150 | +if (reformat && user_data_fs->reformat(&user_data)) { |
| 151 | + Serial.println("Error formatting user partition"); |
| 152 | +return; |
103 | 153 | } |
| 154 | + |
104 | 155 | Serial.println("\nQSPI Flash formatted!"); |
105 | 156 | } |
106 | 157 |
|
107 | 158 | Serial.println("It's now safe to reboot or disconnect your board."); |
108 | 159 | } |
109 | 160 |
|
| 161 | +voidflashWiFiFirmwareAndCertificates() { |
| 162 | +externconstunsignedchar wifi_firmware_image_data[]; |
| 163 | + FILE* fp =fopen("/wlan/4343WA1.BIN","wb"); |
| 164 | +constuint32_t file_size =421098; |
| 165 | +uint32_t chunck_size =1024; |
| 166 | +uint32_t byte_count =0; |
| 167 | + |
| 168 | + Serial.println("Flashing WiFi firmware"); |
| 169 | +printProgress(byte_count, file_size,10,true); |
| 170 | +while (byte_count < file_size) { |
| 171 | +if(byte_count + chunck_size > file_size) |
| 172 | + chunck_size = file_size - byte_count; |
| 173 | +int ret =fwrite(&wifi_firmware_image_data[byte_count], chunck_size,1, fp); |
| 174 | +if (ret !=1) { |
| 175 | + Serial.println("Error writing firmware data"); |
| 176 | +break; |
| 177 | + } |
| 178 | + byte_count += chunck_size; |
| 179 | +printProgress(byte_count, file_size,10,false); |
| 180 | + } |
| 181 | +fclose(fp); |
| 182 | + |
| 183 | + fp =fopen("/wlan/cacert.pem","wb"); |
| 184 | + |
| 185 | + Serial.println("Flashing certificates"); |
| 186 | + chunck_size =128; |
| 187 | + byte_count =0; |
| 188 | +printProgress(byte_count, cacert_pem_len,10,true); |
| 189 | +while (byte_count < cacert_pem_len) { |
| 190 | +if(byte_count + chunck_size > cacert_pem_len) |
| 191 | + chunck_size = cacert_pem_len - byte_count; |
| 192 | +int ret =fwrite(&cacert_pem[byte_count], chunck_size,1 ,fp); |
| 193 | +if (ret !=1) { |
| 194 | + Serial.println("Error writing certificates"); |
| 195 | +break; |
| 196 | + } |
| 197 | + byte_count += chunck_size; |
| 198 | +printProgress(byte_count, cacert_pem_len,10,false); |
| 199 | + } |
| 200 | +fclose(fp); |
| 201 | +} |
| 202 | + |
110 | 203 | voidloop() { |
111 | 204 |
|
112 | 205 | } |