@@ -154,14 +154,35 @@ int main() {
154
154
{
155
155
WiFiStorageFile update_file = WiFiStorage.open (UPDATE_FILE_NAME_LZSS);
156
156
157
+ union HeaderVersion
158
+ {
159
+ typedef struct __attribute__ ((packed))
160
+ {
161
+ uint32_t header_version : 6 ;
162
+ uint32_t compression : 1 ;
163
+ uint32_t signature : 1 ;
164
+ uint32_t spare : 4 ;
165
+ uint32_t payload_target : 4 ;
166
+ uint32_t payload_major : 8 ;
167
+ uint32_t payload_minor : 8 ;
168
+ uint32_t payload_patch : 8 ;
169
+ uint32_t payload_build_num : 24 ;
170
+ } field;
171
+ uint8_t buf[sizeof (field)];
172
+ static_assert (sizeof (buf) == 8 , " Error: sizeof(HEADER.VERSION) != 8" );
173
+ };
174
+
157
175
union
158
176
{
159
177
struct __attribute__ ((packed))
160
178
{
161
179
uint32_t len;
162
180
uint32_t crc32;
181
+ uint32_t magic_number;
182
+ HeaderVersion hdr_version;
163
183
} header;
164
184
uint8_t buf[sizeof (header)];
185
+ static_assert (sizeof (buf) == 20 , " Error: sizeof(HEADER) != 20" );
165
186
} ota_header;
166
187
uint32_t crc32, bytes_read;
167
188
uint8_t crc_buf[128 ];
@@ -170,12 +191,15 @@ int main() {
170
191
update_file.read (ota_header.buf , sizeof (ota_header.buf ));
171
192
172
193
/* ... and check first length ... */
173
- if (ota_header.header .len != (update_file.size () - sizeof (ota_header.buf ))) {
194
+ if (ota_header.header .len != (update_file.size () - sizeof (ota_header.header . len ) - sizeof (ota_header. header . crc32 ))) {
174
195
update_file.close ();
175
196
update_file.erase ();
176
197
goto boot;
177
198
}
178
- /* ... and the CRC second ... initialize CRC ... */
199
+
200
+ /* ... and the CRC second ... rewind to start of CRC verified header ... */
201
+ update_file.seek (sizeof (ota_header.header .len ) + sizeof (ota_header.header .crc32 ));
202
+ /* ... initialize CRC ... */
179
203
crc32 = 0xFFFFFFFF ;
180
204
/* ... and calculate over file ... */
181
205
for (bytes_read = 0 ;
@@ -196,6 +220,29 @@ int main() {
196
220
goto boot;
197
221
}
198
222
223
+ /* Thirdly verify via magic number if this application is intented for
224
+ * MKR WIFI 1010 or NANO 33 IOT.
225
+ */
226
+ #if defined(ARDUINO_SAMD_MKRWIFI1010)
227
+ if (ota_header.header .magic_number != 0x23418054 ) /* 2341:8054 = VID/PID MKR WIFI 1010 */
228
+ {
229
+ update_file.close ();
230
+ update_file.erase ();
231
+ goto boot;
232
+ }
233
+ #elif defined(ARDUINO_SAMD_NANO_33_IOT)
234
+ if (ota_header.header .magic_number != 0x23418057 ) /* 2341:8057 = VID/PID NANO 33 IOT */
235
+ {
236
+ update_file.close ();
237
+ update_file.erase ();
238
+ goto boot;
239
+ }
240
+ #else
241
+ update_file.close ();
242
+ update_file.erase ();
243
+ goto boot;
244
+ #endif
245
+
199
246
/* Rewind to start of LZSS compressed binary. */
200
247
update_file.seek (sizeof (ota_header.buf ));
201
248
0 commit comments