forked from vial-kb/vial-qmk
-
Notifications
You must be signed in to change notification settings - Fork 2
/
vial_generate_vfw.py
51 lines (41 loc) · 1.22 KB
/
vial_generate_vfw.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import sys
import re
import struct
import hashlib
import time
def main():
inp = sys.argv[1]
out = sys.argv[2]
configs = sys.argv[3:]
# identify keyboard UID
uid = None
for config in configs:
with open(config, "r") as inf:
for line in inf:
uid = re.findall(r"#define.*VIAL_KEYBOARD_UID.*{(.*)}", line)
if uid:
break
if uid:
break
if not uid:
print("Cannot identify keyboard UID from configuration files {}, ensure that you have VIAL_KEYBOARD_UID defined!".format(configs))
return 1
uid = uid[0].split(",")
uid = [int(x, 16) for x in uid]
uid = struct.pack("BBBBBBBB", *uid)
# read firmware binary
with open(sys.argv[1], "rb") as inf:
firmware = inf.read()
with open(out, "wb") as outf:
outf.write(b"VIALFW01")
outf.write(uid)
outf.write(struct.pack("<Q", int(time.time())))
outf.write(b"\x00" * 8)
outf.write(hashlib.sha256(firmware).digest())
outf.write(firmware)
print("-" * 80)
print("Vial update package created at {}".format(out))
print("-" * 80)
return 0
if __name__ == "__main__":
sys.exit(main())