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
8
#ifndef CORE_CM7
7
9
#error Format QSPI flash by uploading the sketch to the M7 core instead of the M4 core.
@@ -41,6 +43,20 @@ bool waitResponse() {
41
43
}
42
44
}
43
45
46
+ void printProgress (uint32_t offset, uint32_t size, uint32_t threshold, bool reset) {
47
+ static int percent_done = 0 ;
48
+ if (reset == true ) {
49
+ percent_done = 0 ;
50
+ Serial.println (" Flashed " + String (percent_done) + " %" );
51
+ } else {
52
+ uint32_t percent_done_new = offset * 100 / size;
53
+ if (percent_done_new >= percent_done + threshold) {
54
+ percent_done = percent_done_new;
55
+ Serial.println (" Flashed " + String (percent_done) + " %" );
56
+ }
57
+ }
58
+ }
59
+
44
60
void setup () {
45
61
46
62
Serial.begin (115200 );
@@ -68,7 +84,6 @@ void setup() {
68
84
// use space from 15.5MB to 16 MB for another fw, memory mapped
69
85
70
86
bool reformat = true ;
71
-
72
87
if (!wifi_data_fs.mount (&wifi_data)) {
73
88
Serial.println (" \n Partition 1 already contains a filesystem, do you want to reformat it? Y/[n]" );
74
89
wifi_data_fs.unmount ();
@@ -81,6 +96,16 @@ void setup() {
81
96
return ;
82
97
}
83
98
99
+ bool restore = true ;
100
+ if (reformat) {
101
+ Serial.println (" \n Do you want to restore the WiFi firmware and certificates? Y/[n]" );
102
+ restore = waitResponse ();
103
+ }
104
+
105
+ if (reformat && restore) {
106
+ flashWiFiFirmwareAndCertificates ();
107
+ }
108
+
84
109
reformat = true ;
85
110
if (!ota_data_fs.mount (&ota_data)) {
86
111
Serial.println (" \n Partition 2 already contains a filesystem, do you want to reformat it? Y/[n]" );
@@ -124,6 +149,49 @@ void setup() {
124
149
Serial.println (" It's now safe to reboot or disconnect your board." );
125
150
}
126
151
152
+ void flashWiFiFirmwareAndCertificates () {
153
+ extern const unsigned char wifi_firmware_image_data[];
154
+ extern const resource_hnd_t wifi_firmware_image;
155
+ FILE* fp = fopen (" /wlan/4343WA1.BIN" , " wb" );
156
+ const int file_size = 421098 ;
157
+ int chunck_size = 1024 ;
158
+ int byte_count = 0 ;
159
+
160
+ Serial.println (" Flashing WiFi firmware" );
161
+ printProgress (byte_count, file_size, 10 , true );
162
+ while (byte_count < file_size) {
163
+ if (byte_count + chunck_size > file_size)
164
+ chunck_size = file_size - byte_count;
165
+ int ret = fwrite (&wifi_firmware_image_data[byte_count], chunck_size, 1 , fp);
166
+ if (ret != 1 ) {
167
+ Serial.println (" Error writing firmware data" );
168
+ break ;
169
+ }
170
+ byte_count += chunck_size;
171
+ printProgress (byte_count, file_size, 10 , false );
172
+ }
173
+ fclose (fp);
174
+
175
+ fp = fopen (" /wlan/cacert.pem" , " wb" );
176
+
177
+ Serial.println (" Flashing certificates" );
178
+ chunck_size = 128 ;
179
+ byte_count = 0 ;
180
+ printProgress (byte_count, cacert_pem_len, 10 , true );
181
+ while (byte_count < cacert_pem_len) {
182
+ if (byte_count + chunck_size > cacert_pem_len)
183
+ chunck_size = cacert_pem_len - byte_count;
184
+ int ret = fwrite (&cacert_pem[byte_count], chunck_size, 1 ,fp);
185
+ if (ret != 1 ) {
186
+ Serial.println (" Error writing certificates" );
187
+ break ;
188
+ }
189
+ byte_count += chunck_size;
190
+ printProgress (byte_count, cacert_pem_len, 10 , false );
191
+ }
192
+ fclose (fp);
193
+ }
194
+
127
195
void loop () {
128
196
129
197
}
0 commit comments