From 735899c98507bca3779da0a58e9f6600933fdc8b Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Thu, 27 Aug 2020 14:58:55 +0200 Subject: [PATCH 1/4] Tailor bin2ota script for MKR WiFi 1010 and new OTA header structure --- extras/tools/bin2ota.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/extras/tools/bin2ota.py b/extras/tools/bin2ota.py index 0df4646ce..8ed85520c 100755 --- a/extras/tools/bin2ota.py +++ b/extras/tools/bin2ota.py @@ -15,13 +15,24 @@ bin_data = bytearray(in_file.read()) in_file.close() +# Magic number (VID/PID), hard coded for MKR VIDOR 4000 right now +magic_number = bytearray([0x23, 0x41, 0x80, 0x54]) +print(magic_number) + +# Version field (byte array of size 8) +version = bytearray(8) +print(version) + +# Prepend magic number and version field to payload +bin_data_complete = magic_number + version + bin_data + # Calculate length and CRC32 -bin_data_len = len(bin_data) -bin_data_crc = crccheck.crc.Crc32.calc(bin_data) +bin_data_len = len(bin_data_complete) +bin_data_crc = crccheck.crc.Crc32.calc(bin_data_complete) # Write to outfile out_file = open(ofile, "wb") out_file.write((bin_data_len).to_bytes(4,byteorder='little')) out_file.write((bin_data_crc).to_bytes(4,byteorder='little')) -out_file.write(bin_data) +out_file.write(bin_data_complete) out_file.close() From d08c13497477db2068a79ca48b9ce3c285b39bc3 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Fri, 28 Aug 2020 07:45:46 +0200 Subject: [PATCH 2/4] Adjusting bin2ota.py to support both MKR WIFI 1010 and Nano 33 IOT --- extras/tools/bin2ota.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/extras/tools/bin2ota.py b/extras/tools/bin2ota.py index 8ed85520c..27d56657f 100755 --- a/extras/tools/bin2ota.py +++ b/extras/tools/bin2ota.py @@ -3,12 +3,14 @@ import sys import crccheck -if len(sys.argv) != 3: - print ("Usage: bin2ota.py sketch.bin sketch.ota") +if len(sys.argv) != 4: + print ("Usage: bin2ota.py BOARD sketch.bin sketch.ota") + print (" BOARD = [MKR_WIFI_1010 | NANO_33_IOT]") sys.exit() -ifile = sys.argv[1] -ofile = sys.argv[2] +board = sys.argv[1] +ifile = sys.argv[2] +ofile = sys.argv[3] # Read the binary file in_file = open(ifile, "rb") @@ -16,12 +18,19 @@ in_file.close() # Magic number (VID/PID), hard coded for MKR VIDOR 4000 right now -magic_number = bytearray([0x23, 0x41, 0x80, 0x54]) -print(magic_number) +magic_number_mkr_wifi_1010 = bytearray([0x23, 0x41, 0x80, 0x54]) +magic_number_nano_33_iot = bytearray([0x23, 0x41, 0x80, 0x57]) + +if board == "MKR_WIFI_1010": + magic_number = magic_number_mkr_wifi_1010 +elif board == "NANO_33_IOT": + magic_number = magic_number_nano_33_iot +else: + print ("Error,", board, "is not a supported board type") + sys.exit() # Version field (byte array of size 8) version = bytearray(8) -print(version) # Prepend magic number and version field to payload bin_data_complete = magic_number + version + bin_data From 28104a23f790298836cbb97287114c3c5828e0ca Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Fri, 28 Aug 2020 12:50:46 +0200 Subject: [PATCH 3/4] Deleting bin2json and ota-upload and updating README --- extras/tools/README.md | 34 ++++++++-------------------------- extras/tools/bin2json.py | 28 ---------------------------- extras/tools/ota-upload.sh | 35 ----------------------------------- 3 files changed, 8 insertions(+), 89 deletions(-) delete mode 100755 extras/tools/bin2json.py delete mode 100755 extras/tools/ota-upload.sh diff --git a/extras/tools/README.md b/extras/tools/README.md index ac0a5d2e5..c036b2fae 100644 --- a/extras/tools/README.md +++ b/extras/tools/README.md @@ -7,9 +7,8 @@ Arduino IDE: `Sketch` -> `Export compiled Binary` ```bash cp sketch.bin ~/Arduino/libraries/ArduinoIoTCloud/extras/tools/ cd ~/Arduino/libraries/ArduinoIoTCloud/extras/tools -./bin2ota.py sketch.bin sketch.ota -./bin2json.py sketch.ota sketch.json -./ota-upload.sh CLIENT_ID CLIENT_SECRET DEVICE_ID sketch.json +./lzss.py --encode sketch.bin sketch.lzss +./bin2ota.py [MKR_WIFI_1010 | NANO_33_IOT] sketch.lzss sketch.ota ``` ## `bin2ota.py` @@ -17,25 +16,24 @@ This tool can be used to extend (actually prefix) a binary generated with e.g. t ### How-To-Use ```bash -./bin2ota.py sketch.bin sketch.ota +./bin2ota.py [MKR_WIFI_1010 | NANO_33_IOT] sketch.bin sketch.ota ``` #### `sketch.bin` ```bash 0000000 8000 2000 749D 0000 7485 0000 7485 0000 -0000010 0000 0000 0000 0000 0000 0000 0000 0000 -0000020 0000 0000 0000 0000 0000 0000 7485 0000 0000030 0000 0000 0000 0000 7485 0000 74F1 0000 ... ``` * `length(sketch.bin) = 0x0003'A5E0` * `CRC32(sketch.bin) = 0xA9D1'265B` +* `MAGIC NUMBER(MKR WIFI 1010) = 0x2341'8054` +* `VERSION = 0x0000'0000'0000'0000` #### `sketch.ota` ```bash -0000000 A5E0 0003 265B A9D1 8000 2000 749D 0000 -0000010 7485 0000 7485 0000 0000 0000 0000 0000 -0000020 0000 0000 0000 0000 0000 0000 0000 0000 -0000030 0000 0000 7485 0000 0000 0000 0000 0000 +0000000 A5E0 0003 265B A9D1 2341 8054 0000 0000 +0000010 0000 0000 8000 2000 749D 0000 7485 0000 +... ``` ## `lzss.py` @@ -49,20 +47,4 @@ This tool allows to compress a binary file using the LZSS algorithm. * Decoding (Extracting) ```bash ./lzss.py --decode sketch.lzss sketch.bin -``` - -## `bin2json.py` -This tool converts the binary file into base64 encoded JSON which is necessary for feeding it to the server. - -### How-To-Use -```bash -./bin2json.py sketch.ota sketch.json -``` - -## `ota-upload.sh` -This tool allows to upload a OTA binary to a device via a Arduino cloud server. - -### How-To-Use -```bash -./ota-upload.sh CLIENT_ID CLIENT_SECRET DEVICE_ID sketch.json ``` \ No newline at end of file diff --git a/extras/tools/bin2json.py b/extras/tools/bin2json.py deleted file mode 100755 index 88f45731e..000000000 --- a/extras/tools/bin2json.py +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/python3 - -import sys -import json -import base64 - -CHUNK_SIZE = 256 # This is the chunk size of how the binary is split on the server side for not overloading the embedded device receive buffers. -INTER_CHUNK_DELAY_MS = 500 # This is delay between 2 consecutive chunks so as to not over load the embedded device. - -if len(sys.argv) != 3: - print ("Usage: bin2json.py sketch.ota sketch.json") - sys.exit() - -ifile = sys.argv[1] -ofile = sys.argv[2] - -# Read the binary file -in_file = open(ifile, "rb") -bin_data = in_file.read() -in_file.close() - -# Perform the base64 conversion -base64_data = base64.b64encode(bin_data) - -# Write to outfile -out_file = open(ofile, "w") -json.dump({'binary' : base64_data.decode("ascii"), 'chunk_size' : CHUNK_SIZE, 'delay' : INTER_CHUNK_DELAY_MS}, out_file) -out_file.close() diff --git a/extras/tools/ota-upload.sh b/extras/tools/ota-upload.sh deleted file mode 100755 index d70f1b019..000000000 --- a/extras/tools/ota-upload.sh +++ /dev/null @@ -1,35 +0,0 @@ -#!/bin/bash - -if [ "$#" -ne 4 ]; then - echo "Usage: ota-upload.sh CLIENT_ID CLIENT_SECRET DEVICE_ID sketch.json" - exit 1 -fi - -printf "Obtaining JSON Web Token (JWT) ... " -curl --silent --location --request POST 'http://api-dev.arduino.cc/iot/v1/clients/token' \ - --header 'content-type: application/x-www-form-urlencoded' \ - --header 'Content-Type: application/x-www-form-urlencoded' \ - --data-urlencode 'grant_type=client_credentials' \ - --data-urlencode 'client_id='"$1" \ - --data-urlencode 'client_secret='"$2" \ - --data-urlencode 'audience=https://api2.arduino.cc/iot' \ - | jq -r '.access_token' \ - > access_token -if [ -s "access_token" ]; then - echo "OK" -else - echo "ERROR" -fi - -printf "Uploading to device ... " -access_token_val=$( Date: Wed, 23 Sep 2020 15:35:58 +0200 Subject: [PATCH 4/4] Bugfix: magic number (VID/PID) needs to be encoded in little endian as the rest --- extras/tools/bin2ota.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/extras/tools/bin2ota.py b/extras/tools/bin2ota.py index 27d56657f..48545c83d 100755 --- a/extras/tools/bin2ota.py +++ b/extras/tools/bin2ota.py @@ -17,14 +17,11 @@ bin_data = bytearray(in_file.read()) in_file.close() -# Magic number (VID/PID), hard coded for MKR VIDOR 4000 right now -magic_number_mkr_wifi_1010 = bytearray([0x23, 0x41, 0x80, 0x54]) -magic_number_nano_33_iot = bytearray([0x23, 0x41, 0x80, 0x57]) - +# Magic number (VID/PID) if board == "MKR_WIFI_1010": - magic_number = magic_number_mkr_wifi_1010 + magic_number = 0x23418054.to_bytes(4,byteorder='little') elif board == "NANO_33_IOT": - magic_number = magic_number_nano_33_iot + magic_number = 0x23418057.to_bytes(4,byteorder='little') else: print ("Error,", board, "is not a supported board type") sys.exit()