@@ -154,14 +154,35 @@ int main() {
154154 {
155155 WiFiStorageFile update_file = WiFiStorage.open (UPDATE_FILE_NAME_LZSS);
156156
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+
157175 union
158176 {
159177 struct __attribute__ ((packed))
160178 {
161179 uint32_t len;
162180 uint32_t crc32;
181+ uint32_t magic_number;
182+ HeaderVersion hdr_version;
163183 } header;
164184 uint8_t buf[sizeof (header)];
185+ static_assert (sizeof (buf) == 20 , " Error: sizeof(HEADER) != 20" );
165186 } ota_header;
166187 uint32_t crc32, bytes_read;
167188 uint8_t crc_buf[128 ];
@@ -170,12 +191,15 @@ int main() {
170191 update_file.read (ota_header.buf , sizeof (ota_header.buf ));
171192
172193 /* ... 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 ))) {
174195 update_file.close ();
175196 update_file.erase ();
176197 goto boot;
177198 }
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 ... */
179203 crc32 = 0xFFFFFFFF ;
180204 /* ... and calculate over file ... */
181205 for (bytes_read = 0 ;
@@ -196,6 +220,29 @@ int main() {
196220 goto boot;
197221 }
198222
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+
199246 /* Rewind to start of LZSS compressed binary. */
200247 update_file.seek (sizeof (ota_header.buf ));
201248
0 commit comments