-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirmware_update.py
349 lines (291 loc) · 12.7 KB
/
firmware_update.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
import datetime
import hashlib
import json
import re
import shutil
import subprocess
import time
from urllib.parse import urljoin, urlparse
import requests
import lxml.html
import zipfile
import io
import os
import tqdm
class CustomZipFile(zipfile.ZipFile):
def _get_traget_file_path(self, targetpath, filename):
"""
Copied from zipfile.ZipFile._extract_member()
"""
# build the destination pathname, replacing
# forward slashes to platform specific separators.
arcname = filename.replace('/', os.path.sep)
if os.path.altsep:
arcname = arcname.replace(os.path.altsep, os.path.sep)
# interpret absolute pathname as relative, remove drive letter or
# UNC path, redundant separators, "." and ".." components.
arcname = os.path.splitdrive(arcname)[1]
invalid_path_parts = ('', os.path.curdir, os.path.pardir)
arcname = os.path.sep.join(x for x in arcname.split(os.path.sep)
if x not in invalid_path_parts)
if os.path.sep == '\\':
# filter illegal characters on Windows
arcname = self._sanitize_windows_name(arcname, os.path.sep)
targetpath = os.path.join(targetpath, arcname)
return os.path.normpath(targetpath)
def _extract_member(self, member, targetpath, pwd):
"""
Copied from zipfile.ZipFile._extract_member()
"""
# Create all upper directories if necessary.
upperdirs = os.path.dirname(targetpath)
if upperdirs and not os.path.exists(upperdirs):
os.makedirs(upperdirs)
if member.is_dir():
if not os.path.isdir(targetpath):
os.mkdir(targetpath)
return targetpath
with self.open(member, pwd=pwd) as source, \
open(targetpath, "wb") as target:
shutil.copyfileobj(source, target)
return targetpath
@staticmethod
def find_leading_dirs(member_filenames):
leading_dirs = []
while True:
current_dirs = set()
new_member_filenames = []
for f in member_filenames:
if "/" in f:
d, rest = f.split("/", maxsplit=1)
current_dirs.add(d)
if len(current_dirs) > 1:
return "/".join(leading_dirs)
if rest:
new_member_filenames.append(rest)
if current_dirs:
assert len(current_dirs) == 1
leading_dirs.append(next(iter(current_dirs)))
else:
return "/".join(leading_dirs)
member_filenames = new_member_filenames
def extractall(self, path=None, members=None, pwd=None, strip_leading_dirs=False):
"""Extract all members from the archive to the current working
directory. `path' specifies a different directory to extract to.
`members' is optional and must be a subset of the list returned
by namelist().
Copied from zipfile.ZipFile.extractall()
"""
if members is None:
members = self.infolist()
leading_dirs = None
if strip_leading_dirs:
leading_dirs = self.find_leading_dirs([m.filename for m in members])
if path is None:
path = os.getcwd()
else:
path = os.fspath(path)
for zipinfo in members:
targetfile = zipinfo.filename
if leading_dirs:
assert targetfile.startswith(leading_dirs)
targetfile = targetfile[len(leading_dirs):].lstrip("/")
targetpath = self._get_traget_file_path(path, targetfile)
self._extract_member(zipinfo, targetpath, pwd)
date_time = time.mktime(zipinfo.date_time + (0, 0, -1))
os.utime(targetpath, (date_time, date_time))
yield targetpath
def get_dir_metadata(dir_path):
return {
"type": "directory",
"mtime": os.path.getmtime(dir_path)
}
def get_file_metadata(file_path, bufsize=4 * 1024):
md5 = hashlib.md5()
with open(file_path, "rb") as fp:
while True:
b = fp.read(bufsize)
if not len(b):
break
md5.update(b)
return {
"type": "file",
"mtime": os.path.getmtime(file_path),
"size": os.path.getsize(file_path),
"md5": md5.hexdigest()
}
def get_recursive_metadata(dirname, base_dirname):
file_metadata = {}
for path, dirs, files in os.walk(os.path.join(base_dirname, dirname)):
assert path.startswith(base_dirname)
relpath = path[len(base_dirname):].lstrip("/")
for entries, fn in [(dirs, get_dir_metadata), (files, get_file_metadata)]:
for i in entries:
file_metadata[os.path.join(relpath, i)] = fn(os.path.join(path, i))
return file_metadata
def get_metadata_for_dirs(dirs, base):
return {k: v for d in dirs for k, v in get_recursive_metadata(d, base).items()}
def get_mounts():
with open("/etc/mtab", "r") as fp:
mounts = {}
for l in fp.readlines():
l = l.rstrip("\n")
if l:
dev, mountpount, _ = l.split(maxsplit=2)
mounts[dev] = mountpount.replace("\\040", " ")
return mounts
def mount_sdcard(sdcard_fslabel):
print(f"Identifying block device for '{sdcard_fslabel}' ... ", end="", flush=True)
try:
sdcard_partition_dev = subprocess.check_output(["blkid", "--label", sdcard_fslabel]).decode().strip()
except subprocess.CalledProcessError:
print(f"ERROR: unable to identfy block device with filesystem label '{sdcard_fslabel}'")
return None, None
print(sdcard_partition_dev)
mounts = get_mounts()
if sdcard_partition_dev not in mounts:
print(f"Mounting {sdcard_partition_dev} ... ", end="", flush=True)
subprocess.check_output(["udisksctl", "mount", "-b", sdcard_partition_dev])
print("done.")
mounts = get_mounts()
assert sdcard_partition_dev in mounts, f"ERROR: Could not find {sdcard_partition_dev} in /etc/mtab"
mountpoint = mounts[sdcard_partition_dev]
print(f"{sdcard_partition_dev} mounted on {mountpoint}")
return mountpoint, sdcard_partition_dev
def download_firmware(sdcard_base):
update_dir = os.path.join(sdcard_base, "update")
os.makedirs(update_dir, exist_ok=True)
print("Looking for latest RogueMaster firmware release ... ", end="", flush=True)
roguemaster_latest_release = requests.get(
"https://api.github.com/repos/RogueMaster/flipperzero-firmware-wPlugins/releases/latest"
).json()
print(roguemaster_latest_release["name"])
roguemaster_download_url = roguemaster_latest_release["assets"][0]["browser_download_url"]
print(f"Downloading RogueMaster release from {roguemaster_download_url} ... ", end="", flush=True)
roguemaster_release_bytes = requests.get(roguemaster_download_url).content
print("done.")
with CustomZipFile(io.BytesIO(roguemaster_release_bytes)) as z:
for _ in tqdm.tqdm(z.extractall(update_dir, strip_leading_dirs=True),
desc="Extracting RogueMaster release",
total=len(z.infolist())):
pass
tail_regex = re.compile(r'\s*files? to (?:the )?SD/(\S+)')
def parse_file_list_links(url="https://flipper.pingywon.com/"):
print(f"Getting asset file directories from {url} ... ", end="", flush=True)
links = lxml.html.fromstring(
requests.get(url).content
).xpath("//h2[contains(text(),'MISC NOTES')]/following-sibling::ul/li/a")
print("done.")
assert links, f"ERROR: No asset file directories parsed at {url}. Maybe something changed on the page?"
for i in tqdm.tqdm(links, desc="Parsing asset file directory listings"):
tail = i.tail.strip()
match = tail_regex.search(tail)
if match:
yield i.attrib["href"], match.group(1)
else:
print(f"'{tail}' doesn't match '{tail_regex.pattern}'")
def parse_file_list(url):
def parse_tr(tr):
assert len(tr) == 3
a = tr[0][0]
assert a.tag == 'a'
href = a.attrib["href"]
if href != "../":
return {
"url": urljoin(url, href),
"size": tr[1].text.strip(),
"date": datetime.datetime.strptime(tr[2].text, "%Y-%b-%d %H:%M"),
}
for tr in lxml.html.fromstring(requests.get(url).content).xpath("//table[@id='list']/tbody/tr"):
file_info = parse_tr(tr)
if file_info:
yield file_info
def parse_all_file_lists():
return [
{**file_info, "path": path}
for url, path in
parse_file_list_links()
for file_info in
parse_file_list(url)
]
def download_files(file_infos, basedir, bufsize=4 * 1024):
for f in tqdm.tqdm(file_infos, desc="Downloading asset files"):
resp = requests.get(f["url"], stream=True)
dir_path = os.path.join(basedir, f["path"])
os.makedirs(dir_path, exist_ok=True)
file_path = os.path.join(dir_path, urlparse(f["url"]).path.split("/")[-1])
with open(file_path, "wb") as fp:
for chunk in resp.iter_content(bufsize):
fp.write(chunk)
date_time = time.mktime(f["date"].timetuple())
os.utime(file_path, (date_time, date_time))
def compare_file_metadata(old, new):
print("Comparing previous and new file metadata ... ", end="", flush=True)
old_filenames = set(old)
new_filenames = set(new)
result = {}
result.update({k: {**old[k], "state": "deleted"} for k in old_filenames - new_filenames})
result.update({k: {**new[k], "state": "new"} for k in new_filenames - old_filenames})
for f in old_filenames & new_filenames:
new_fileinfo = new[f]
old_fileinfo = old[f]
assert new_fileinfo["type"] == old_fileinfo["type"] # we currently don't support type change
changed = {}
for k in ["mtime", "size", "md5"]:
if new_fileinfo[k] != old_fileinfo[k]:
changed[k] = {"old": old_fileinfo[k], "new": new_fileinfo[k]}
if changed:
result[f] = {**changed, "state": "modified"}
print("done.")
return result
def dump_file_metadata_diff(diff):
filename = f"flipper-{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}.json"
print(f"Dumping file metadata diff in {filename} ... ", end="", flush=True)
with open(filename, "w") as fp:
json.dump(diff, fp)
print("done.")
if __name__ == '__main__':
sdcard_base, sdcard_partition_dev = mount_sdcard("Flipper SD")
if not sdcard_base:
exit(1)
file_infos = parse_all_file_lists()
dirs = {f["path"] for f in file_infos} | {'update'}
old_files = get_metadata_for_dirs(dirs, sdcard_base)
download_firmware(sdcard_base)
download_files(file_infos, sdcard_base)
new_files = get_metadata_for_dirs(dirs, sdcard_base)
diff = compare_file_metadata(old_files, new_files)
dump_file_metadata_diff(diff)
firmware_changed = False
for k, v in diff.items():
dirname, _ = k.split("/", maxsplit=1)
if dirname == "update" and (v["state"] == "new" or (v["state"] == "modified" and "md5" in v)):
firmware_changed = True
break
if firmware_changed:
print(f"Un-mounting {sdcard_partition_dev} ... ", end="", flush=True)
subprocess.check_output(["udisksctl", "unmount", "-b", sdcard_partition_dev])
print("done.")
print("Remove SD card from computer card reader, "
"insert SD card in Flipper Zero, "
"run update in Flipper Zero, "
"umount SD card in Flipper Zero, "
"remove SD card from Flipper Zero, "
"insert SD card in computer card reader, "
"then press Enter")
input()
sdcard_base, sdcard_partition_dev = mount_sdcard("Flipper SD")
settings_user_filename = "subghz/assets/setting_user"
print(f"Replacing Add_standard_frequencies: false with true in {settings_user_filename} ... ", end="", flush=True)
subghz_setting_user_path = os.path.join(sdcard_base, settings_user_filename)
with open(subghz_setting_user_path, "r") as fp:
subghz_setting_user = fp.read()
subghz_setting_user_new = subghz_setting_user.replace("Add_standard_frequencies: false",
"Add_standard_frequencies: true")
with open(subghz_setting_user_path, "w") as fp:
fp.write(subghz_setting_user_new)
print("done.")
print(f"Un-mounting {sdcard_partition_dev} ... ", end="", flush=True)
subprocess.check_output(["udisksctl", "unmount", "-b", sdcard_partition_dev])
print("done.")
print("ALL DONE.")