Skip to content

Commit 106583a

Browse files
authored
Merge pull request #197 from arduino-libraries/snu-new-ota-header
[SNU] Extending OTA header to incorporate magic number/version field
2 parents 9e1c63a + 4f02348 commit 106583a

File tree

4 files changed

+32
-96
lines changed

4 files changed

+32
-96
lines changed

extras/tools/README.md

+8-26
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,33 @@ Arduino IDE: `Sketch` -> `Export compiled Binary`
77
```bash
88
cp sketch.bin ~/Arduino/libraries/ArduinoIoTCloud/extras/tools/
99
cd ~/Arduino/libraries/ArduinoIoTCloud/extras/tools
10-
./bin2ota.py sketch.bin sketch.ota
11-
./bin2json.py sketch.ota sketch.json
12-
./ota-upload.sh CLIENT_ID CLIENT_SECRET DEVICE_ID sketch.json
10+
./lzss.py --encode sketch.bin sketch.lzss
11+
./bin2ota.py [MKR_WIFI_1010 | NANO_33_IOT] sketch.lzss sketch.ota
1312
```
1413

1514
## `bin2ota.py`
1615
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.
1716

1817
### How-To-Use
1918
```bash
20-
./bin2ota.py sketch.bin sketch.ota
19+
./bin2ota.py [MKR_WIFI_1010 | NANO_33_IOT] sketch.bin sketch.ota
2120
```
2221
#### `sketch.bin`
2322
```bash
2423
0000000 8000 2000 749D 0000 7485 0000 7485 0000
25-
0000010 0000 0000 0000 0000 0000 0000 0000 0000
26-
0000020 0000 0000 0000 0000 0000 0000 7485 0000
2724
0000030 0000 0000 0000 0000 7485 0000 74F1 0000
2825
...
2926
```
3027
* `length(sketch.bin) = 0x0003'A5E0`
3128
* `CRC32(sketch.bin) = 0xA9D1'265B`
29+
* `MAGIC NUMBER(MKR WIFI 1010) = 0x2341'8054`
30+
* `VERSION = 0x0000'0000'0000'0000`
3231

3332
#### `sketch.ota`
3433
```bash
35-
0000000 A5E0 0003 265B A9D1 8000 2000 749D 0000
36-
0000010 7485 0000 7485 0000 0000 0000 0000 0000
37-
0000020 0000 0000 0000 0000 0000 0000 0000 0000
38-
0000030 0000 0000 7485 0000 0000 0000 0000 0000
34+
0000000 A5E0 0003 265B A9D1 2341 8054 0000 0000
35+
0000010 0000 0000 8000 2000 749D 0000 7485 0000
36+
...
3937
```
4038

4139
## `lzss.py`
@@ -49,20 +47,4 @@ This tool allows to compress a binary file using the LZSS algorithm.
4947
* Decoding (Extracting)
5048
```bash
5149
./lzss.py --decode sketch.lzss sketch.bin
52-
```
53-
54-
## `bin2json.py`
55-
This tool converts the binary file into base64 encoded JSON which is necessary for feeding it to the server.
56-
57-
### How-To-Use
58-
```bash
59-
./bin2json.py sketch.ota sketch.json
60-
```
61-
62-
## `ota-upload.sh`
63-
This tool allows to upload a OTA binary to a device via a Arduino cloud server.
64-
65-
### How-To-Use
66-
```bash
67-
./ota-upload.sh CLIENT_ID CLIENT_SECRET DEVICE_ID sketch.json
6850
```

extras/tools/bin2json.py

-28
This file was deleted.

extras/tools/bin2ota.py

+24-7
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,42 @@
33
import sys
44
import crccheck
55

6-
if len(sys.argv) != 3:
7-
print ("Usage: bin2ota.py sketch.bin sketch.ota")
6+
if len(sys.argv) != 4:
7+
print ("Usage: bin2ota.py BOARD sketch.bin sketch.ota")
8+
print (" BOARD = [MKR_WIFI_1010 | NANO_33_IOT]")
89
sys.exit()
910

10-
ifile = sys.argv[1]
11-
ofile = sys.argv[2]
11+
board = sys.argv[1]
12+
ifile = sys.argv[2]
13+
ofile = sys.argv[3]
1214

1315
# Read the binary file
1416
in_file = open(ifile, "rb")
1517
bin_data = bytearray(in_file.read())
1618
in_file.close()
1719

20+
# Magic number (VID/PID)
21+
if board == "MKR_WIFI_1010":
22+
magic_number = 0x23418054.to_bytes(4,byteorder='little')
23+
elif board == "NANO_33_IOT":
24+
magic_number = 0x23418057.to_bytes(4,byteorder='little')
25+
else:
26+
print ("Error,", board, "is not a supported board type")
27+
sys.exit()
28+
29+
# Version field (byte array of size 8)
30+
version = bytearray(8)
31+
32+
# Prepend magic number and version field to payload
33+
bin_data_complete = magic_number + version + bin_data
34+
1835
# Calculate length and CRC32
19-
bin_data_len = len(bin_data)
20-
bin_data_crc = crccheck.crc.Crc32.calc(bin_data)
36+
bin_data_len = len(bin_data_complete)
37+
bin_data_crc = crccheck.crc.Crc32.calc(bin_data_complete)
2138

2239
# Write to outfile
2340
out_file = open(ofile, "wb")
2441
out_file.write((bin_data_len).to_bytes(4,byteorder='little'))
2542
out_file.write((bin_data_crc).to_bytes(4,byteorder='little'))
26-
out_file.write(bin_data)
43+
out_file.write(bin_data_complete)
2744
out_file.close()

extras/tools/ota-upload.sh

-35
This file was deleted.

0 commit comments

Comments
 (0)