Skip to content

Commit

Permalink
Merge pull request #558 from arduino/snu-new-ota-header
Browse files Browse the repository at this point in the history
[SNU] Extending OTA header to incorporate magic number/version field
  • Loading branch information
aentinger authored Oct 1, 2020
2 parents 0b87b1b + 87dc69b commit d81df81
Show file tree
Hide file tree
Showing 5 changed files with 2,361 additions and 2,314 deletions.
51 changes: 49 additions & 2 deletions libraries/SNU/extras/NiNaBoot/NiNaBoot.ino
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,35 @@ int main() {
{
WiFiStorageFile update_file = WiFiStorage.open(UPDATE_FILE_NAME_LZSS);

union HeaderVersion
{
typedef struct __attribute__((packed))
{
uint32_t header_version : 6;
uint32_t compression : 1;
uint32_t signature : 1;
uint32_t spare : 4;
uint32_t payload_target : 4;
uint32_t payload_major : 8;
uint32_t payload_minor : 8;
uint32_t payload_patch : 8;
uint32_t payload_build_num : 24;
} field;
uint8_t buf[sizeof(field)];
static_assert(sizeof(buf) == 8, "Error: sizeof(HEADER.VERSION) != 8");
};

union
{
struct __attribute__((packed))
{
uint32_t len;
uint32_t crc32;
uint32_t magic_number;
HeaderVersion hdr_version;
} header;
uint8_t buf[sizeof(header)];
static_assert(sizeof(buf) == 20, "Error: sizeof(HEADER) != 20");
} ota_header;
uint32_t crc32, bytes_read;
uint8_t crc_buf[128];
Expand All @@ -170,12 +191,15 @@ int main() {
update_file.read(ota_header.buf, sizeof(ota_header.buf));

/* ... and check first length ... */
if (ota_header.header.len != (update_file.size() - sizeof(ota_header.buf))) {
if (ota_header.header.len != (update_file.size() - sizeof(ota_header.header.len) - sizeof(ota_header.header.crc32))) {
update_file.close();
update_file.erase();
goto boot;
}
/* ... and the CRC second ... initialize CRC ... */

/* ... and the CRC second ... rewind to start of CRC verified header ... */
update_file.seek(sizeof(ota_header.header.len) + sizeof(ota_header.header.crc32));
/* ... initialize CRC ... */
crc32 = 0xFFFFFFFF;
/* ... and calculate over file ... */
for(bytes_read = 0;
Expand All @@ -196,6 +220,29 @@ int main() {
goto boot;
}

/* Thirdly verify via magic number if this application is intented for
* MKR WIFI 1010 or NANO 33 IOT.
*/
#if defined(ARDUINO_SAMD_MKRWIFI1010)
if (ota_header.header.magic_number != 0x23418054) /* 2341:8054 = VID/PID MKR WIFI 1010 */
{
update_file.close();
update_file.erase();
goto boot;
}
#elif defined(ARDUINO_SAMD_NANO_33_IOT)
if (ota_header.header.magic_number != 0x23418057) /* 2341:8057 = VID/PID NANO 33 IOT */
{
update_file.close();
update_file.erase();
goto boot;
}
#else
update_file.close();
update_file.erase();
goto boot;
#endif

/* Rewind to start of LZSS compressed binary. */
update_file.seek(sizeof(ota_header.buf));

Expand Down
2 changes: 1 addition & 1 deletion libraries/SNU/library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=SNU
version=1.0.1
version=1.0.2
author=Arduino
maintainer=Arduino <info@arduino.cc>
sentence=Update the sketch on your board with NiNa W10 wifi module
Expand Down
Loading

0 comments on commit d81df81

Please sign in to comment.