@@ -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 ;
0 commit comments