Skip to content

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

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 4 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
34 changes: 8 additions & 26 deletions extras/tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,33 @@ 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`
This tool can be used to extend (actually prefix) a binary generated with e.g. the Arduino IDE with the required length and crc values required to perform an OTA (Over-The-Air) update of the firmware.

### 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`
Expand All @@ -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
```
28 changes: 0 additions & 28 deletions extras/tools/bin2json.py

This file was deleted.

31 changes: 24 additions & 7 deletions extras/tools/bin2ota.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,42 @@
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")
bin_data = bytearray(in_file.read())
in_file.close()

# Magic number (VID/PID)
if board == "MKR_WIFI_1010":
magic_number = 0x23418054.to_bytes(4,byteorder='little')
elif board == "NANO_33_IOT":
magic_number = 0x23418057.to_bytes(4,byteorder='little')
else:
print ("Error,", board, "is not a supported board type")
sys.exit()

# Version field (byte array of size 8)
version = bytearray(8)

# 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()
35 changes: 0 additions & 35 deletions extras/tools/ota-upload.sh

This file was deleted.