forked from DarkFlippers/unleashed-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin2dfu.py
executable file
·70 lines (53 loc) · 1.86 KB
/
bin2dfu.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
import os
import struct
from zlib import crc32
from flipper.app import App
class Main(App):
def init(self):
self.parser.add_argument("-i", "--input", help=".bin input path", required=True)
self.parser.add_argument(
"-o", "--output", help=".dfu output path", required=True
)
self.parser.add_argument(
"-a",
"--address",
help="Flash address",
type=lambda x: int(x, 0),
required=True,
)
self.parser.add_argument(
"-l", "--label", help="DFU Target label", required=True
)
self.parser.add_argument(
"--vid", help="USB Vendor ID", default=0x0483, type=lambda x: int(x, 0)
)
self.parser.add_argument(
"--pid", help="USB Product ID", default=0xDF11, type=lambda x: int(x, 0)
)
self.parser.set_defaults(func=self.convert)
def convert(self):
if not os.path.exists(self.args.input):
self.logger.error(f'"{self.args.input}" does not exist')
return 1
with open(self.args.input, mode="rb") as file:
bin = file.read()
data = struct.pack("<II", self.args.address, len(bin)) + bin
# Target prefix
szTargetName = self.args.label.encode("ascii")
data = (
struct.pack("<6sBI255sII", b"Target", 0, 1, szTargetName, len(data), 1)
+ data
)
# Prefix
data = struct.pack("<5sBIB", b"DfuSe", 0x01, len(data) + 11, 1) + data
# Suffix
data += struct.pack(
"<HHHH3sB", 0xFFFF, self.args.pid, self.args.vid, 0x011A, b"UFD", 16
)
dwCRC = ~crc32(data) & 0xFFFFFFFF
data += struct.pack("<I", dwCRC)
open(self.args.output, "wb").write(data)
return 0
if __name__ == "__main__":
Main()()