Skip to content

Commit d81df81

Browse files
authored
Merge pull request #558 from arduino/snu-new-ota-header
[SNU] Extending OTA header to incorporate magic number/version field
2 parents 0b87b1b + 87dc69b commit d81df81

File tree

5 files changed

+2361
-2314
lines changed

5 files changed

+2361
-2314
lines changed

libraries/SNU/extras/NiNaBoot/NiNaBoot.ino

+49-2
Original file line numberDiff line numberDiff line change
@@ -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

libraries/SNU/library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SNU
2-
version=1.0.1
2+
version=1.0.2
33
author=Arduino
44
maintainer=Arduino <[email protected]>
55
sentence=Update the sketch on your board with NiNa W10 wifi module

0 commit comments

Comments
 (0)