Skip to content

[SNU] Extending OTA header to incorporate magic number/version field #558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <[email protected]>
sentence=Update the sketch on your board with NiNa W10 wifi module
Expand Down
Loading