Skip to content

Commit 376aabf

Browse files
authored
Merge pull request #1058 from pennam/formatter
QSPIFormat: default partition scheme
2 parents ad3cc68 + a831fd2 commit 376aabf

File tree

2 files changed

+144
-50
lines changed

2 files changed

+144
-50
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,205 @@
1-
#include "QSPIFBlockDevice.h"
1+
#include "BlockDevice.h"
22
#include "MBRBlockDevice.h"
33
#include "LittleFileSystem.h"
44
#include "FATFileSystem.h"
5+
#include "wiced_resource.h"
6+
#include "certificates.h"
57

6-
#ifndef CORE_CM7
8+
#ifndef CORE_CM7
79
#error Format QSPI flash by uploading the sketch to the M7 core instead of the M4 core.
810
#endif
911

12+
using namespace mbed;
1013

11-
QSPIFBlockDevice root(QSPI_SO0, QSPI_SO1, QSPI_SO2, QSPI_SO3, QSPI_SCK, QSPI_CS, QSPIF_POLARITY_MODE_1, 40000000);
12-
mbed::MBRBlockDevice wifi_data(&root, 1);
13-
mbed::MBRBlockDevice ota_data(&root, 2);
14-
mbed::MBRBlockDevice user_data(&root, 3);
15-
mbed::FATFileSystem wifi_data_fs("wlan");
16-
mbed::FATFileSystem ota_data_fs("fs");
17-
mbed::FileSystem * user_data_fs;
14+
BlockDevice* root = BlockDevice::get_default_instance();
15+
MBRBlockDevice wifi_data(root, 1);
16+
MBRBlockDevice ota_data(root, 2);
17+
MBRBlockDevice kvstore_data(root, 3);
18+
MBRBlockDevice user_data(root, 4);
19+
FATFileSystem wifi_data_fs("wlan");
20+
FATFileSystem ota_data_fs("fs");
21+
FileSystem * user_data_fs;
1822

1923
bool waitResponse() {
2024
bool confirmation = false;
25+
bool proceed = false;
2126
while (confirmation == false) {
2227
if (Serial.available()) {
2328
char choice = Serial.read();
2429
switch (choice) {
2530
case 'y':
2631
case 'Y':
2732
confirmation = true;
28-
return true;
33+
proceed = true;
2934
break;
3035
case 'n':
3136
case 'N':
3237
confirmation = true;
33-
return false;
38+
proceed = false;
3439
break;
3540
default:
3641
continue;
3742
}
3843
}
3944
}
45+
return proceed;
46+
}
47+
48+
void printProgress(uint32_t offset, uint32_t size, uint32_t threshold, bool reset) {
49+
static int 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+
}
4060
}
4161

4262
void setup() {
4363

4464
Serial.begin(115200);
4565
while (!Serial);
4666

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:");
5269
Serial.println("Partition 1: WiFi firmware and certificates 1MB");
5370
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"),
6073
Serial.println("Do you want to proceed? Y/[n]");
6174

6275
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());
6884
} 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();
72101
}
73102

74-
int err = wifi_data_fs.reformat(&wifi_data);
75-
if (err) {
103+
if (reformat && wifi_data_fs.reformat(&wifi_data)) {
76104
Serial.println("Error formatting WiFi partition");
77105
return;
78106
}
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)) {
82127
Serial.println("Error formatting OTA partition");
83128
return;
84129
}
85130

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 = new mbed::LittleFileSystem("user");
137+
} else {
138+
Serial.println("Formatting user partition with FatFS.");
139+
user_data_fs = new mbed::FATFileSystem("user");
140+
}
89141

90-
if (true == waitResponse()) {
91-
Serial.println("Formatting user partition with LittleFS.");
92-
user_data_fs = new mbed::LittleFileSystem("user");
93-
} else {
94-
Serial.println("Formatting user partition with FatFS.");
95-
user_data_fs = new mbed::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();
97146

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;
103153
}
154+
104155
Serial.println("\nQSPI Flash formatted!");
105156
}
106157

107158
Serial.println("It's now safe to reboot or disconnect your board.");
108159
}
109160

161+
void flashWiFiFirmwareAndCertificates() {
162+
extern const unsigned char wifi_firmware_image_data[];
163+
FILE* fp = fopen("/wlan/4343WA1.BIN", "wb");
164+
const uint32_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+
110203
void loop() {
111204

112205
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../WiFiFirmwareUpdater/certificates.h

0 commit comments

Comments
 (0)