forked from ExpressLRS/Backpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnifiedConfiguration.py
More file actions
44 lines (37 loc) · 1.28 KB
/
UnifiedConfiguration.py
File metadata and controls
44 lines (37 loc) · 1.28 KB
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
#!/usr/bin/python
import json
import struct
import sys
def findFirmwareEnd(f):
f.seek(0, 0)
(magic, segments, _, _, _) = struct.unpack('<BBBBI', f.read(8))
if magic != 0xe9:
sys.stderr.write('The file provided does not the right magic for a firmware file!\n')
exit(1)
is8285 = False
if segments == 2: # we have to assume it's an ESP8266/85
f.seek(0x1000, 0)
(magic, segments, _, _, _) = struct.unpack('<BBBBI', f.read(8))
is8285 = True
else:
f.seek(24, 0)
for _ in range(segments):
(_, size) = struct.unpack('<II', f.read(8))
f.seek(size, 1)
pos = f.tell()
pos = (pos + 16) & ~15
if not is8285:
pos = pos + 32
return pos
def appendToFirmware(firmware_file, defines):
end = findFirmwareEnd(firmware_file)
firmware_file.seek(end, 0)
defines = (defines.encode() + (b'\0' * 512))[0:512]
firmware_file.write(defines)
firmware_file.write(b'\0')
firmware_file.flush()
# firmware_file.truncate(firmware_file.tell()) # Stupid Windoze! (Permission denied)
def appendConfiguration(source, target, env):
defines = json.JSONEncoder().encode(env['OPTIONS_JSON'])
with open(str(target[0]), "r+b") as firmware_file:
appendToFirmware(firmware_file, defines)