forked from mdxs/ota-dfu-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfu.py
More file actions
executable file
·97 lines (74 loc) · 2.63 KB
/
dfu.py
File metadata and controls
executable file
·97 lines (74 loc) · 2.63 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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python3
"""
------------------------------------------------------------------------------
DFU Server for Nordic nRF52 based systems.
Conforms to nRF51_SDK 15.3 BLE_DFU requirements.
------------------------------------------------------------------------------
"""
import os, re
import sys
import argparse
import time
import math
import traceback
from unpacker import Unpacker
from ble_secure_dfu_controller import BleDfuControllerSecure
def main():
init_msg = """
================================
== ==
== DFU Server ==
== Jurpp Fork ==
== ==
================================
"""
unpacker = None
hexfile = None
datfile = None
print(init_msg)
parser = argparse.ArgumentParser(usage='dfu.py <zip_file> <dfu_target_address>\n\nExample:\n\tdfu.py ./firmware.zip cd:e3:4a:47:1c:e4')
parser.add_argument('zipfile')
parser.add_argument('address')
args = parser.parse_args()
try:
unpacker = Unpacker()
try:
hexfile, datfile = unpacker.unpack_zipfile(args.zipfile)
except Exception as e:
print("[-]")
print(e)
pass
ble_dfu = BleDfuControllerSecure(args.address.upper(), hexfile, datfile)
# Initialize inputs
ble_dfu.input_setup()
# Connect to peer device. Assume application mode.
if ble_dfu.scan_and_connect():
if not ble_dfu.check_DFU_mode():
print("[i] Target needs to switch to DFU mode")
success = ble_dfu.switch_to_dfu_mode()
if not success:
print("[-] Couldn't reconnect")
unpacker.delete()
sys.exit(1)
else:
# The device might already be in DFU mode (MAC + 1)
ble_dfu.target_mac_increase(1)
# Try connection with new address
print("[-] Couldn't connect trying DFU MAC next...")
if not ble_dfu.scan_and_connect():
print("[-] Can't connect to device")
unpacker.delete()
sys.exit(1)
ble_dfu.start()
# Disconnect from peer device if not done already and clean up.
ble_dfu.disconnect()
except Exception as e:
# print traceback.format_exc()
print("[-] Exception at line {}: {}".format(sys.exc_info()[2].tb_lineno, e))
pass
# Delete temporary files
unpacker.delete()
if __name__ == '__main__':
# Do not litter the world with broken .pyc files.
sys.dont_write_bytecode = True
main()