-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathfritzflash.py
executable file
·394 lines (336 loc) · 12.2 KB
/
fritzflash.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#! /usr/bin/env python3
import argparse
import ipaddress
import os
import socket
import time
from ftplib import FTP
AUTODISCOVER_TIMEOUT = 1
FTP_TIMEOUT = 2
FTP_MAX_RETRY = 10
class FritzFTP(FTP):
class ConnectionTimeout(Exception):
pass
def __init__(self, ip, username='adam2', password='adam2', timeout=1, max_retry=0, retry_cb=None):
i = 1
while i <= max_retry:
try:
retry_cb(i, max_retry)
super().__init__(ip, user=username, passwd=password, timeout=timeout)
break
except socket.timeout:
i += 1
except OSError as e:
time.sleep(1)
i += 1
if i > max_retry:
raise FritzFTP.ConnectionTimeout()
self.set_pasv(True)
def getenv(self):
env = [b'']
fritzenv = {}
def storeenv(x):
env[0] += x
self.voidcmd('MEDIA SDRAM')
try:
self.retrbinary('RETR env', storeenv)
except socket.timeout:
pass
for line in env[0].decode('ascii').splitlines():
l = line.split()
fritzenv[l[0]] = l[1]
return fritzenv
def set_flash_timeout(self):
self.sock.settimeout(60 * 5)
def upload_image(self, image):
self.set_flash_timeout()
self.voidcmd('MEDIA FLSH')
self.storbinary('STOR mtd1', image)
def reboot(self):
self.voidcmd('REBOOT')
self.close()
def connection_refused_message():
print("\nIt seems you have a booted-up AVM device running in your Network.\n"
"This might be because you missed the 10 second window after powering on your AVM device.\n"
"In this case: Powercycle your device and retry.\n"
"If this problem persits, check if you might have connections to another AVM device, e.g. via WiFi/WLAN.\n\n")
def start_message(ip_address):
print(
"This program will help you installing Gluon, a widely used Firmware for Freifunk networks, onto your AVM device.\n"
"You can always find the most current version of this script at https://www.github.com/freifunk-darmstadt/fritz-tools\n\n"
"It is strongly recommended to only connect your computer to the device you want to flash.\n"
"Try to disable all other connections (Ethernet, WiFi/WLAN, VMs) if detection fails.\n\n"
"Sometimes an unmanaged switch between your AVM device and your computer is helpfull.\n\n"
"Before we start, make sure you have assigned your PC a static IP Address in the Subnet of the device you want to flash.\n"
"The following example would be a completely fine option:\n")
print("IP-Address: %s" % str(ipaddress.ip_address(ip_address) + 1))
print("Subnet: 255.255.255.0")
print("Gateway: %s" % str(ipaddress.ip_address(ip_address)))
print("DNS Servers: Leave blank\n")
print("Once you're ready to flash, press enter, disconnect power from your AVM device and reconnect the power-supply.")
def connect_message():
print("We will now connect to your devices bootloader.")
def flash_message():
print("\nWriting Gluon image to your AVM device...\n"
"This process may take a lot of time.\n\n"
"First, the device will erase its current Operating System.\n"
"Next, the device will write the Gluon image to its memory.\n"
"The red Info LED will illuminate in this step. Don't worry, this is expected behavior.\n\n"
"Do *not* turn of the device!\n\n"
"We will tell you when your device has finished installing Gluon (this may take a while).")
def finish_message():
print("\n== Congratulations! ==\n\n"
"Your device is now running Gluon.\n"
"It will restart and in 2-5 minutes you will be able to visit its config-mode.\n"
"Remember to reconfigure your interface to automatically obtain an IP-address!\n"
"You can reach config-mode by typing in http://192.168.1.1/ in your preferred Webbrowser.\n")
print("Press any key to exit.")
input()
def retry_status(current_try, max_try):
print("--> Try %d of %d" % (current_try, max_try))
def autodiscover_avm_ip():
sender = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
sender.bind(('192.168.178.2', 0))
except OSError as e:
if e.errno == 99:
print('\rIP address 192.168.178.2 is not configured on any interface.')
exit(1)
else:
raise e from None
sender.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sender.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sender.settimeout(1)
receiver = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
receiver.settimeout(AUTODISCOVER_TIMEOUT)
receiver.bind(('192.168.178.2', 5035))
i = 1
while True:
try:
print("\rTry %d..." % i, end='')
receiver.sendto(b'aa', ("192.168.178.1", 5035)) # Dirty hack to add conntrack entry
sender.sendto(bytearray.fromhex("0000120101000000c0a8b20100000000"), ('255.255.255.255', 5035))
while 1:
data, addr = receiver.recvfrom(64)
if addr[0] == '192.168.178.1':
print("\rFritzBox found at %s" % addr[0])
return addr[0]
except socket.timeout:
i += 1
except OSError:
i += 1
time.sleep(0.1)
except KeyboardInterrupt:
print('\rAborting...\n')
return None
return None
def determine_image_name(env_string):
models = {
"172": {
"gluon": [
"avm-fritz-box-7320-sysupgrade.bin"
],
"openwrt": [
"avm_fritz7320-squashfs-sysupgrade.bin"
],
},
"173": {
"gluon": [
"avm-fritz-wlan-repeater-300e-sysupgrade.bin"
],
"openwrt": [
"fritz300e-squashfs-sysupgrade.bin"
],
},
"179": {
"gluon": [
"avm-fritz-box-7330-sysupgrade.bin"
],
"openwrt": [
"avm_fritz7320-squashfs-sysupgrade.bin"
],
},
"181": {
"gluon": [
"avm-fritz-box-7360-sl-sysupgrade.bin"
],
"openwrt": [
"avm_fritz7360sl-squashfs-sysupgrade.bin"
],
},
"183": {
"gluon": [
"avm-fritz-box-7360-v1-sysupgrade.bin"
],
"openwrt": [
"avm_fritz7360sl-squashfs-sysupgrade.bin"
],
},
"188": {
"gluon": [
"avm-fritz-box-7330-sl-sysupgrade.bin"
],
"openwrt": [
"avm_fritz7320-squashfs-sysupgrade.bin"
],
},
"189": {
"gluon": [
"avm-fritz-box-7312-sysupgrade.bin"
],
"openwrt": [
"avm_fritz7312-squashfs-sysupgrade.bin"
],
},
"196": {
"gluon": [
"avm-fritz-box-7360-v2-sysupgrade.bin"
],
"openwrt": [
"avm_fritz7360sl-squashfs-sysupgrade.bin"
],
},
"200": {
"gluon": [
"avm-fritz-wlan-repeater-450e-sysupgrade.bin"
],
"openwrt": [
"fritz450e-squashfs-sysupgrade.bin"
]
},
"219": {
"gluon": [
"avm-fritz-box-4020-sysupgrade.bin"
],
"openwrt": [
"fritz4020-squashfs-sysupgrade.bin",
"avm_fritz4020-squashfs-sysupgrade.bin"
]
},
"227": {
"gluon": [
"avm-fritz-box-4040-bootloader.bin"
],
"openwrt": [
"avm_fritzbox-4040-squashfs-eva.bin"
]
}
}
for model in models.keys():
if model == env_string:
image_names = []
if "gluon" in models[model]:
image_names += models[model]["gluon"]
if "openwrt" in models[model]:
image_names += models[model]["openwrt"]
return image_names
return None
def autoload_image(ip):
print("\nStarting automatic image-selection!")
print("-> Establishing connection to device!")
try:
ftp = FritzFTP(ip, timeout=FTP_TIMEOUT, max_retry=FTP_MAX_RETRY, retry_cb=retry_status)
except FritzFTP.ConnectionTimeout:
print("-> Max retrys exceeded! Check connection and try again.")
exit(1)
except ConnectionRefusedError:
connection_refused_message()
exit(1)
env = ftp.getenv()
ftp.close()
if 'HWRevision' not in env:
print("\nAutomatic image-selection unsuccessful!")
print("-> No model saved on device!")
exit(1)
image_names = determine_image_name(env["HWRevision"])
if image_names is None:
print("\nAutomatic image-selection unsuccessful!")
print("-> Unknown Model %s!" % env["HWRevision"])
print("Press any key to exit.")
input()
exit(1)
dir_content = os.listdir(os.getcwd())
files = []
for file in dir_content:
cwd = os.getcwd()
file = os.path.join(cwd, file)
if not os.path.isfile(file):
continue
for image_name in image_names:
if image_name in file:
files.append(file)
if len(files) > 1:
print("\nAutomatic image-selection unsuccessful!")
print("-> Multiple potential image files found!")
for file in files:
print("--> %s" % file)
print("\nPlease specify the image via `--image` parameter.")
print("Press any key to exit.")
input()
exit(1)
if not files:
print("\nAutomatic image-selection unsuccessful!")
print("--> No potential image file found!")
print("\nPlease download and specify the image via `--image` parameter.")
print("Press any key to exit.")
input()
exit(1)
print("-> Automatic image-selection successful!")
print("--> Will flash %s" % files[0])
return open(files[0], 'rb')
def perform_flash(ip, file):
print("-> Establishing connection to device!")
try:
ftp = FritzFTP(ip, timeout=FTP_TIMEOUT, max_retry=FTP_MAX_RETRY, retry_cb=retry_status)
except FritzFTP.ConnectionTimeout:
print("-> Max retries exceeded! Check connection and try again.")
print("Press any key to exit.")
input()
exit(1)
except ConnectionRefusedError:
connection_refused_message()
print("Press any key to exit.")
input()
exit(1)
print("-> Flash image")
flash_message()
ftp.upload_image(file)
print("-> Image write successful")
print("-> Performing reboot")
ftp.reboot()
finish_message()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Flash Gluon image to AVM devices using EVA.')
parser.add_argument('--ip', type=str, help='IP Address of device. Autodiscovery if not specified.')
parser.add_argument('--image', type=str, help='Image file to transfer.')
args = parser.parse_args()
imagefile = None
if args.ip:
try:
socket.inet_aton(args.ip)
except socket.error:
print("%s is not a valid IPv4 address!" % args.ip_address)
exit(1)
if args.image:
try:
imagefile = open(args.image, 'rb')
except FileNotFoundError:
print("Image file \"%s\" does not exist!" % os.path.abspath(args.image_path))
exit(1)
start_message("192.168.178.1")
input()
ip = args.ip
if ip is None:
print("Trying to autodiscover! Abort via Ctrl-c.")
ip = autodiscover_avm_ip()
if ip is None:
print("\nAutodiscovery failed!")
print("Press any key to exit.")
input()
exit(1)
print("\nAutodiscovery succesful!")
print("-> Device detected at %s." % ip)
if args.image is None:
# Try to automatically locate an image to use
imagefile = autoload_image(ip)
perform_flash(ip, imagefile)