-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathqubes_fwupdmgr.py
executable file
·1371 lines (1280 loc) · 51 KB
/
qubes_fwupdmgr.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python3
#
# The Qubes OS Project, http://www.qubes-os.org
#
# Copyright (C) 2020 Norbert Kaminski <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
import json
import os
import re
import shutil
import subprocess
import sys
import xml.etree.ElementTree as ET
from pathlib import Path
from distutils.version import LooseVersion as l_ver
if __name__ == "__main__":
from qubes_fwupd_heads import FwupdHeads
else:
from src.qubes_fwupd_heads import FwupdHeads
FWUPD_QUBES_DIR = "/usr/share/qubes-fwupd"
FWUPD_DOM0_UPDATE = os.path.join(FWUPD_QUBES_DIR, "src/fwupd-dom0-update")
FWUPD_DOM0_DIR = "/root/.cache/fwupd"
FWUPD_DOM0_METADATA_DIR = os.path.join(FWUPD_DOM0_DIR, "metadata")
FWUPD_DOM0_UPDATES_DIR = os.path.join(FWUPD_DOM0_DIR, "updates")
FWUPD_DOM0_METADATA_SIGNATURE = os.path.join(
FWUPD_DOM0_METADATA_DIR,
"firmware.xml.gz.asc"
)
FWUPD_DOM0_METADATA_FILE = os.path.join(
FWUPD_DOM0_METADATA_DIR,
"firmware.xml.gz"
)
FWUPD_DOM0_METADATA_JCAT = os.path.join(
FWUPD_DOM0_METADATA_DIR,
"firmware.xml.gz.jcat"
)
FWUPD_USBVM_LOG = os.path.join(FWUPD_DOM0_DIR, "usbvm-devices.log")
FWUPD_USBVM_VALIDATE = "/usr/share/qubes-fwupd/fwupd_usbvm_validate.py"
FWUPD_USBVM_DIR = "/home/user/.cache/fwupd"
FWUPD_USBVM_UPDATES_DIR = os.path.join(FWUPD_USBVM_DIR, "updates")
FWUPD_USBVM_METADATA_DIR = os.path.join(FWUPD_USBVM_DIR, "metadata")
FWUPD_USBVM_METADATA_SIGNATURE = os.path.join(
FWUPD_USBVM_METADATA_DIR,
"firmware.xml.gz.asc"
)
FWUPD_USBVM_METADATA_FILE = os.path.join(
FWUPD_USBVM_METADATA_DIR,
"firmware.xml.gz"
)
FWUPD_USBVM_METADATA_JCAT = os.path.join(
FWUPD_USBVM_METADATA_DIR,
"firmware.xml.gz.jcat"
)
HEADS_UPDATES_DIR = "/boot/updates"
FWUPD_DOWNLOAD_PREFIX = "https://fwupd.org/downloads/"
FWUPDMGR = "/bin/fwupdmgr"
# version > 1.3.8
FWUPDAGENT_NEW = "/bin/fwupdagent"
# version <= 1.3.8
FWUPDAGENT_OLD = "/usr/libexec/fwupd/fwupdagent"
FWUPDNEWS = "/usr/share/doc/fwupd/NEWS"
USBVM_N = "sys-usb"
BIOS_UPDATE_FLAG = os.path.join(FWUPD_DOM0_DIR, "bios_update")
LVFS_TESTING_DOM0_FLAG = os.path.join(FWUPD_DOM0_DIR, "lvfs_testing")
LVFS_TESTING_USBVM_FLAG = os.path.join(FWUPD_USBVM_DIR, "lvfs_testing")
METADATA_REFRESH_REGEX = re.compile(
r"^Successfully refreshed metadata manually$"
)
SPECIAL_CHAR_REGEX = re.compile(r'%20|&|\||#')
HELP = {
"Usage": [
{
"Command": "qubes-fwupdmgr [OPTION…][FLAG..]",
"Example": "qubes-fwupdmgr refresh --whonix --url=<url>\n",
}
],
"Options": [
{
"get-devices": "Get all devices that support firmware updates",
"get-updates": "Get the list of updates for connected hardware",
"refresh": "Refresh metadata from remote server",
"update": "Update chosen device to latest firmware version",
"update-heads": "Updates heads firmware to the latest version",
"downgrade": "Downgrade chosen device to chosen firmware version",
"clean": "Delete all cached update files\n"
}
],
"Flags": [
{
"--whonix": "Download firmware updates via Tor",
"--device": "Specify device for heads update (default - x230)",
"--url": "Address of the custom metadata remote server\n"
}
],
"Help": [
{
"-h --help": "Show help options\n"
}
],
}
EXIT_CODES = {
"ERROR": 1,
"SUCCESS": 0,
"NO_UPDATES": 99,
}
class QubesFwupdmgr(FwupdHeads):
def _download_metadata(self, whonix=False, metadata_url=None):
"""Initialize downloading metadata files.
Keywords arguments:
whonix -- Flag enforces downloading the metadata updates via Tor
metadata_url -- Use custom metadata from the url
"""
cmd_metadata = [
FWUPD_DOM0_UPDATE,
"--metadata"
]
if whonix:
cmd_metadata.append("--whonix")
if metadata_url:
cmd_metadata.append(f"--url={metadata_url}")
p = subprocess.Popen(cmd_metadata)
p.wait()
if p.returncode != 0:
raise Exception("fwudp-qubes: Metadata update failed")
if not os.path.exists(self.metadata_file):
raise Exception("Metadata signature does not exist")
def _validate_usbvm_dirs(self):
"""Validates if sys-ubs updates and metadata directories exist."""
cmd_validate_dirs = [
"qvm-run",
"--pass-io",
USBVM_N,
f'script --quiet --return --command "{FWUPD_USBVM_VALIDATE} dirs"'
]
p = subprocess.Popen(cmd_validate_dirs)
p.wait()
if p.returncode != 0:
raise Exception("Validation of usbvm directories failed.")
def _validate_usbvm_archive(self, arch_name, sha):
"""Validates checksum and gpg signature of the archive file."""
arch_path = os.path.join(FWUPD_USBVM_UPDATES_DIR, arch_name)
arch_validate = f"{FWUPD_USBVM_VALIDATE} updates {arch_path} {sha}"
cmd_validate_arch = [
"qvm-run",
"--pass-io",
USBVM_N,
f'script --quiet --return --command "{arch_validate}"'
]
p = subprocess.Popen(cmd_validate_arch)
p.wait()
if p.returncode != 0:
raise Exception("Validation of the archive file failed.")
def _copy_usbvm_metadata(self):
"""Copies metadata files to usbvm."""
self.metadata_file_usbvm = self.metadata_file.replace(
FWUPD_DOM0_METADATA_DIR,
FWUPD_USBVM_METADATA_DIR
)
self.metadata_file_signature_usbvm = self.metadata_file_usbvm + ".asc"
self.metadata_file_jcat_usbvm = self.metadata_file_usbvm + ".jcat"
cat_file = f"cat > {self.metadata_file_usbvm}"
cmd_copy_file = (
f'cat {self.metadata_file} | '
f'qvm-run --nogui --pass-io {USBVM_N} "{cat_file}"'
)
cat_sig = f"cat > {self.metadata_file_signature_usbvm}"
cmd_copy_sig = (
f'cat {self.metadata_file_signature} | '
f'qvm-run --nogui --pass-io {USBVM_N} "{cat_sig}"'
)
cat_jcat = f"cat > {self.metadata_file_jcat_usbvm}"
cmd_copy_jcat = (
f'cat {self.metadata_file_jcat} | '
f'qvm-run --nogui --pass-io {USBVM_N} "{cat_jcat}"'
)
p = subprocess.Popen(cmd_copy_file, shell=True)
p.wait()
if p.returncode != 0:
raise Exception("Copying metadata file failed.")
p = subprocess.Popen(cmd_copy_sig, shell=True)
p.wait()
if p.returncode != 0:
raise Exception("Copying metadata signature failed.")
p = subprocess.Popen(cmd_copy_jcat, shell=True)
p.wait()
if p.returncode != 0:
raise Exception("Copying metadata jcat failed.")
def _validate_usbvm_metadata(self, metadata_url=None):
"""Checks GPG signature of metadata files in usbvm."""
usbvm_cmd = f'"{FWUPD_USBVM_VALIDATE} metadata"'
if metadata_url:
usbvm_cmd = (
f'"{FWUPD_USBVM_VALIDATE} metadata --url={metadata_url}"'
)
cmd_validate_metadata = [
"qvm-run",
"--pass-io",
USBVM_N,
'script --quiet --return --command'
f' {usbvm_cmd}'
]
p = subprocess.Popen(cmd_validate_metadata)
p.wait()
if p.returncode != 0:
raise Exception("Metadata validation failed")
def _refresh_usbvm_metadata(self):
"""Refreshes metadata in usbvm."""
if self.jcat:
sig_metadata_file = self.metadata_file_jcat_usbvm
else:
sig_metadata_file = self.metadata_file_signature_usbvm
cmd_refresh_metadata = [
"qvm-run",
"--pass-io",
USBVM_N,
(
'script --quiet --return --command '
f'"{FWUPDMGR} refresh {self.metadata_file_usbvm} '
f'{sig_metadata_file} {self.lvfs}"'
)
]
p = subprocess.Popen(cmd_refresh_metadata)
p.wait()
if p.returncode != 0:
raise Exception("Metadata refresh in usbvm failed")
def _copy_firmware_updates(self, arch_name):
"""Copies updates files to usbvm.
Keywords arguments:
arch_name - name of the archive file
"""
arch_path = os.path.join(FWUPD_DOM0_UPDATES_DIR, arch_name)
output_path = os.path.join(FWUPD_USBVM_UPDATES_DIR, arch_name)
cat_file = f"cat > {output_path}"
cmd_copy_file = (
f'cat {arch_path} | '
f'qvm-run --nogui --pass-io {USBVM_N} "{cat_file}"'
)
p = subprocess.Popen(cmd_copy_file, shell=True)
p.wait()
if p.returncode != 0:
raise Exception("Copying metadata file failed.")
def _install_usbvm_firmware_update(self, arch_name):
"""Installs firmware update for specified device in dom0.
Keywords arguments:
arch_name - name of the archive file
"""
arch_path = os.path.join(FWUPD_USBVM_UPDATES_DIR, arch_name)
CMD_update = [
"qvm-run",
"--pass-io",
USBVM_N,
f'script --quiet --return --command'
f' "{FWUPDMGR} install {arch_path}" /dev/null'
]
p = subprocess.Popen(CMD_update)
p.wait()
if p.returncode != 0:
raise Exception("fwudp-qubes: Firmware update failed")
def _install_usbvm_firmware_downgrade(self, arch_name):
"""Installs firmware downgrades for specified device in dom0.
Keywords arguments:
arch_name - name of the archive file
"""
arch_path = os.path.join(FWUPD_USBVM_UPDATES_DIR, arch_name)
CMD_downgrade = [
"qvm-run",
"--pass-io",
USBVM_N,
f'script --quiet --return --command'
f' "{FWUPDMGR} --allow-older install {arch_path}" /dev/null'
]
p = subprocess.Popen(CMD_downgrade)
p.wait()
if p.returncode != 0:
raise Exception("fwudp-qubes: Firmware downgrade failed")
def _clean_usbvm(self):
"""Cleans usbvm directories."""
cmd_clean = [
"qvm-run",
"--pass-io",
USBVM_N,
f'script --quiet --return --command "{FWUPD_USBVM_VALIDATE} clean"'
]
p = subprocess.Popen(cmd_clean)
p.wait()
if p.returncode != 0:
raise Exception("Cleaning usbvm directories failed")
def _enable_lvfs_testing_dom0(self):
"""Checks and enable lvfs-testing for custom metadata in dom0"""
cmd_lvfs_testing = [
FWUPDMGR,
"enable-remote",
"-y",
"lvfs-testing"
]
if not os.path.exists(LVFS_TESTING_DOM0_FLAG):
p = subprocess.Popen(cmd_lvfs_testing)
p.wait()
if p.returncode != 0:
raise Exception("Enabling dom0 lvfs-testing failed!!")
Path(LVFS_TESTING_DOM0_FLAG).touch(mode=0o644, exist_ok=False)
def _enable_lvfs_testing_usbvm(self, usbvm=False):
"""Checks and enable lvfs-testing for custom metadata in usbvm"""
if not usbvm:
return 0
cmd_refresh_metadata = [
"qvm-run",
"--pass-io",
USBVM_N,
(
'script --quiet --return --command '
f'"{FWUPDMGR} enable-remote -y lvfs-testing"'
)
]
cmd_validate_flag = [
"qvm-run",
"--pass-io",
USBVM_N,
(
'script --quiet --return --command '
f'"ls {LVFS_TESTING_USBVM_FLAG} &>/dev/null"'
)
]
cmd_touch_flag = [
"qvm-run",
"--pass-io",
USBVM_N,
(
'script --quiet --return --command '
f'"touch {LVFS_TESTING_USBVM_FLAG}"'
)
]
flag = subprocess.Popen(cmd_validate_flag)
flag.wait()
if flag.returncode != 0:
p = subprocess.Popen(cmd_refresh_metadata)
p.wait()
if p.returncode != 0:
raise Exception("Enabling usbvm lvfs-testing failed!!")
p = subprocess.Popen(cmd_touch_flag)
p.wait()
if p.returncode != 0:
raise Exception("Creating flag failed!!")
def refresh_metadata(self, usbvm=False, whonix=False, metadata_url=None):
"""Updates metadata with downloaded files.
Keyword arguments:
usbvm -- usbvm support flag
whonix -- Flag enforces downloading the metadata updates via Tor
metadata_url -- Use custom metadata from the url
"""
if metadata_url and self.fwupdagent_dom0:
metadata_name = metadata_url.replace(FWUPD_DOWNLOAD_PREFIX, "")
self.metadata_file = os.path.join(
FWUPD_DOM0_METADATA_DIR,
metadata_name
)
self.metadata_file_signature = self.metadata_file + '.asc'
self.metadata_file_jcat = self.metadata_file + '.jcat'
self.lvfs = "lvfs-testing"
self._enable_lvfs_testing_dom0()
self._enable_lvfs_testing_usbvm(usbvm=usbvm)
elif metadata_url and self.fwupdagent_dom0:
print(
f"Custom refresh not supported!!\n"
f"{self.client_version} < 1.2.6"
)
return EXIT_CODES["NO_UPDATES"]
else:
self.metadata_file = FWUPD_DOM0_METADATA_FILE
self.metadata_file_signature = FWUPD_DOM0_METADATA_SIGNATURE
self.metadata_file_jcat = FWUPD_DOM0_METADATA_JCAT
self.lvfs = "lvfs"
self._download_metadata(whonix=whonix, metadata_url=metadata_url)
if usbvm:
self._validate_usbvm_dirs()
self._copy_usbvm_metadata()
self._validate_usbvm_metadata(metadata_url=metadata_url)
self._refresh_usbvm_metadata()
if self.jcat:
sig_metadata_file = self.metadata_file_jcat
else:
sig_metadata_file = self.metadata_file_signature
cmd_refresh = [
FWUPDMGR,
"refresh",
self.metadata_file,
sig_metadata_file,
self.lvfs
]
p = subprocess.Popen(
cmd_refresh,
stdout=subprocess.PIPE
)
self.output = p.communicate()[0].decode()
print(self.output)
if p.returncode != 0:
raise Exception("fwudp-qubes: Refresh failed")
if (
not METADATA_REFRESH_REGEX.match(self.output) and
self.fwupdagent_dom0 == FWUPDAGENT_NEW
):
raise Exception("Manual metadata refresh failed!!!")
def _get_dom0_updates(self):
"""Gathers infromations about available updates."""
if not self.fwupdagent_dom0:
cmd_get_dom0_updates = [
FWUPDMGR,
"get-updates"
]
elif self.fwupdagent_dom0 == FWUPDAGENT_OLD:
cmd_get_dom0_updates = [
self.fwupdagent_dom0,
"get-devices"
]
else:
cmd_get_dom0_updates = [
self.fwupdagent_dom0,
"get-updates"
]
p = subprocess.Popen(
cmd_get_dom0_updates,
stdout=subprocess.PIPE
)
self.dom0_updates_info = p.communicate()[0].decode()
if not self.fwupdagent_dom0:
print(self.dom0_updates_info)
if p.returncode != 0 and p.returncode != 2:
raise Exception("fwudp-qubes: Getting available updates failed")
def _get_usbvm_updates_old(self):
"""Prints possible updates in the usbvm"""
usbvm_cmd = f'"{FWUPDMGR} get-updates"'
cmd_get_usbvm_updates = (
f'qvm-run --nogui --pass-io {USBVM_N} {usbvm_cmd}'
)
p = subprocess.Popen(
cmd_get_usbvm_updates,
shell=True
)
p.wait()
if p.returncode != 0 and p.returncode != 2:
raise Exception("fwudp-qubes: Getting usbvm updates info failed")
def _parse_dom0_updates_info(self, updates_info):
"""Creates dictionary and list with information about updates.
Keywords argument:
updates_info - gathered update information
"""
self.dom0_updates_info_dict = json.loads(updates_info)
self.dom0_updates_list = [
{
"Name": device["Name"],
"Version": device["Version"],
"Releases": [
{
"Version": update["Version"],
"Url": update["Uri"],
"Checksum": update["Checksum"][0],
"Description": update["Description"]
} for update in device["Releases"]
]
} for device in self.dom0_updates_info_dict["Devices"]
]
def _parse_dom0_updates_old_agent(self, devices_info):
"""
Parses dom0 updates for older version of the fwupd
"""
self.dom0_updates_list = []
self.dom0_updates_info_dict = json.loads(devices_info)
for device in self.dom0_updates_info_dict["Devices"]:
if "Releases" in device:
self.dom0_updates_list.append(
{
"Name": device["Name"],
"Version": device["Version"],
"Releases": []
}
)
current_version = device["Version"]
for update in device["Releases"]:
if l_ver(update["Version"]) > current_version:
self.dom0_updates_list[-1]["Releases"].append(
{
"Version": update["Version"],
"Url": update["Uri"],
"Checksum": update["Checksum"][0],
"Description": update["Description"]
}
)
if not self.dom0_updates_list[-1]["Releases"]:
self.dom0_updates_list.pop()
def _download_firmware_updates(self, url, sha, whonix=False):
"""Initializes downloading firmware upadate archive.
Keywords arguments:
url -- url path to the firmware upadate archive
sha -- SHA1 checksum of the firmware update archive
whonix -- Flag enforces downloading the updates via Tor
"""
self.arch_name = url.replace(FWUPD_DOWNLOAD_PREFIX, "")
if SPECIAL_CHAR_REGEX.search(self.arch_name):
self.arch_name = "trusted.cab"
self.arch_path = os.path.join(FWUPD_DOM0_UPDATES_DIR, self.arch_name)
update_path = self.arch_path.replace(".cab", "")
cmd_fwdownload = [
FWUPD_DOM0_UPDATE,
"--update",
f"--url={url}",
f"--sha={sha}"
]
if whonix:
cmd_fwdownload.append("--whonix")
p = subprocess.Popen(cmd_fwdownload)
p.wait()
if p.returncode != 0:
raise Exception("fwudp-qubes: Firmware download failed")
if not os.path.exists(update_path):
raise Exception("Firmware update files do not exist")
def _user_input(self, updates_dict, downgrade=False, usbvm=False):
"""UI for update process.
Keywords arguments:
updates_dict - list of updates for specified device
downgrade -- downgrade flag
"""
decorator = "======================================================"
if usbvm:
updates_list = updates_dict["dom0"] + updates_dict["usbvm"]
else:
updates_list = updates_dict["dom0"]
dom0_updates_num = len(updates_dict["dom0"])
if len(updates_list) == 0:
print("No updates available.")
return EXIT_CODES["NO_UPDATES"]
if downgrade:
print("Available downgrades:")
else:
print("Available updates:")
self._updates_crawler(updates_dict["dom0"])
if usbvm:
self._updates_crawler(
updates_dict["usbvm"],
usbvm=True,
prefix=dom0_updates_num
)
while True:
try:
print("If you want to abandon process press 'N'.")
choice = input("Otherwise choose a device number: ")
if choice == 'N' or choice == 'n':
return EXIT_CODES["NO_UPDATES"]
device_num = int(choice)-1
if 0 <= device_num < len(updates_list):
if not downgrade:
if device_num >= dom0_updates_num:
return "usbvm", device_num-dom0_updates_num
else:
return "dom0", device_num
break
else:
raise ValueError()
except ValueError:
print("Invalid choice.")
if downgrade:
while True:
try:
releases = updates_list[device_num]["Releases"]
for i, fw_dngd in enumerate(releases):
print(decorator)
print(
f" {i+1}. Firmware downgrade version:"
f"\t {fw_dngd['Version']}"
)
description = fw_dngd["Description"].replace("<p>", "")
description = description.replace("<li>", "")
description = description.replace("<ul>", "")
description = description.replace("</ul>", "")
description = description.replace("</p>", "\n ")
description = description.replace("</li>", "\n ")
print(f" Description:{description}")
print("If you want to abandon downgrade process press N.")
choice = input("Otherwise choose downgrade number: ")
if choice == 'N' or choice == 'n':
return EXIT_CODES["NO_UPDATES"]
downgrade_num = int(choice)-1
if 0 <= downgrade_num < len(releases):
if device_num >= dom0_updates_num:
device_abs_num = device_num - dom0_updates_num
return "usbvm", device_abs_num, downgrade_num
else:
return "dom0", device_num, downgrade_num
else:
raise ValueError()
except ValueError:
print("Invalid choice.")
def _parse_parameters(self, updates_dict, vm_name, choice):
"""Parses device name, url, version and SHA1 checksum of the file list.
Keywords arguments:
updates_dict - dictionary of updates for dom0 and usbvm
vm_name - VM name
choice -- number of device to be updated
"""
self.name = updates_dict[vm_name][choice]["Name"]
self.version = updates_dict[vm_name][choice]["Releases"][0]["Version"]
for ver_check in updates_dict[vm_name][choice]["Releases"]:
if l_ver(ver_check["Version"]) >= l_ver(self.version):
self.version = ver_check["Version"]
self.url = ver_check["Url"]
self.sha = ver_check["Checksum"]
def _install_dom0_firmware_update(self, arch_path):
"""Installs firmware update for specified device in dom0.
Keywords arguments:
arch_path - absolute path to firmware update archive
"""
cmd_install = [
FWUPDMGR,
"install",
arch_path
]
p = subprocess.Popen(cmd_install)
p.wait()
if p.returncode != 0:
raise Exception("fwudp-qubes: Firmware update failed")
def _read_dmi(self):
"""Reads BIOS information from DMI."""
cmd_dmidecode_version = [
"dmidecode",
"-s",
"bios-version"
]
p = subprocess.Popen(cmd_dmidecode_version, stdout=subprocess.PIPE)
p.wait()
self.dmi_version = p.communicate()[0].decode()
cmd_dmidecode = [
"dmidecode",
"-t",
"bios"
]
p = subprocess.Popen(cmd_dmidecode, stdout=subprocess.PIPE)
p.wait()
if p.returncode != 0:
raise Exception("dmidecode: Reading DMI failed")
return p.communicate()[0].decode()
def _verify_dmi(self, path, version, downgrade=False):
"""Verifies DMI tables for BIOS updates.
Keywords arguments:
path -- absolute path of the updates files
version -- version of the update
downgrade -- downgrade flag
"""
dmi_info = self._read_dmi()
path_metainfo = os.path.join(path, "firmware.metainfo.xml")
tree = ET.parse(path_metainfo)
root = tree.getroot()
vendor = root.find("developer_name").text
if vendor is None:
raise ValueError("No vendor information in firmware metainfo.")
if vendor not in dmi_info:
raise ValueError("Wrong firmware provider.")
if not downgrade and l_ver(version) <= l_ver(self.dmi_version):
raise ValueError(
f"{version} < {self.dmi_version} Downgrade not allowed"
)
def _get_dom0_devices(self):
"""Gathers information about devices connected in dom0."""
if not self.fwupdagent_dom0:
cmd_get_dom0_devices = [
FWUPDMGR,
"get-devices"
]
else:
cmd_get_dom0_devices = [
self.fwupdagent_dom0,
"get-devices"
]
p = subprocess.Popen(
cmd_get_dom0_devices,
stdout=subprocess.PIPE
)
self.dom0_devices_info = p.communicate()[0].decode()
if not self.fwupdagent_dom0:
print(self.dom0_devices_info)
if p.returncode != 0:
raise Exception("fwudp-qubes: Getting devices info failed")
def _get_usbvm_devices(self):
"""Gathers information about devices connected in usbvm."""
if os.path.exists(FWUPD_USBVM_LOG):
os.remove(FWUPD_USBVM_LOG)
if not self.fwupdagent_usbvm:
usbvm_cmd = f'"{FWUPDMGR} get-devices"'
log_file = ''
else:
usbvm_cmd = f'"{self.fwupdagent_usbvm} get-devices"'
log_file = f" > {FWUPD_USBVM_LOG}"
cmd_get_usbvm_devices = (
f'qvm-run --nogui --pass-io {USBVM_N} {usbvm_cmd}{log_file}'
)
p = subprocess.Popen(
cmd_get_usbvm_devices,
shell=True
)
p.wait()
if (p.returncode != 0 and p.returncode != 2
and not os.path.exists(FWUPD_USBVM_LOG)):
raise Exception("fwudp-qubes: Getting usbvm devices info failed")
if not os.path.exists(FWUPD_USBVM_LOG) and self.fwupdagent_usbvm:
raise Exception("usbvm device info log does not exist")
def _parse_usbvm_updates(self, usbvm_devices_info):
"""Creates dictionary and list with information about updates.
Keywords argument:
usbvm_devices_info - gathered usbvm information
"""
self.usbvm_updates_list = []
if "No detected devices" in usbvm_devices_info:
return EXIT_CODES["NO_UPDATES"]
usbvm_device_info_dict = json.loads(usbvm_devices_info)
for device in usbvm_device_info_dict["Devices"]:
if "Releases" in device:
self.usbvm_updates_list.append(
{
"Name": device["Name"],
"Version": device["Version"],
"Releases": []
}
)
current_version = device["Version"]
for update in device["Releases"]:
if l_ver(update["Version"]) > current_version:
self.usbvm_updates_list[-1]["Releases"].append(
{
"Version": update["Version"],
"Url": update["Uri"],
"Checksum": update["Checksum"][0],
"Description": update["Description"]
}
)
if not self.usbvm_updates_list[-1]["Releases"]:
self.usbvm_updates_list.pop()
def check_fwupd_version(self, usbvm=False):
"""Checks the fwupd client version and sets fwupdagent paths
dynamicly
Keyword arguments:
usbvm -- usbvm support flag
"""
version_check_metadata = 'client version:\t1.4.0'
version_check = 'client version:\t1.3.8'
version_check_old = 'client version:\t1.2.6'
self.jcat = False
self.fwupdagent_dom0 = None
self.fwupdagent_usbvm = None
version_regex = re.compile(
r'client version:\t[0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2}$'
)
cmd_version = [
FWUPDMGR,
"--version"
]
p = subprocess.Popen(
cmd_version,
stdout=subprocess.PIPE
)
self.client_version = p.communicate()[0].decode().split("\n")[0]
if p.returncode != 0 and not os.path.exists(FWUPDNEWS):
raise Exception("Checking version failed")
elif p.returncode != 0 and os.path.exists(FWUPDNEWS):
with open(FWUPDNEWS, "r") as news:
self.client_version = news.readline().replace(
"Version ",
"client version:\t"
)
self.client_version = self.client_version.replace("\n", "")
assert version_regex.match(self.client_version), (
'Version command output has changed!!!'
)
if l_ver(version_check_old) > l_ver(self.client_version):
pass
elif l_ver(version_check) > l_ver(self.client_version):
self.fwupdagent_dom0 = FWUPDAGENT_OLD
elif l_ver(version_check_metadata) > l_ver(self.client_version):
self.fwupdagent_dom0 = FWUPDAGENT_NEW
else:
self.fwupdagent_dom0 = FWUPDAGENT_NEW
self.jcat = True
if usbvm:
cmd_version = f'"{FWUPDMGR}" --version'
cmd_usbvm_version = [
'qvm-run',
'--pass-io',
USBVM_N,
cmd_version
]
p = subprocess.Popen(
cmd_usbvm_version,
stdout=subprocess.PIPE
)
client_version_usbvm = p.communicate()[0].decode().split("\n")[0]
assert version_regex.match(client_version_usbvm), (
'Version command output has changed!!!'
)
if l_ver(version_check_old) > l_ver(client_version_usbvm):
pass
elif l_ver(version_check) > l_ver(client_version_usbvm):
self.fwupdagent_usbvm = FWUPDAGENT_OLD
elif l_ver(version_check_metadata) > l_ver(client_version_usbvm):
self.fwupdagent_usbvm = FWUPDAGENT_NEW
else:
self.fwupdagent_usbvm = FWUPDAGENT_NEW
self.jcat = True
def update_firmware(self, usbvm=False, whonix=False):
"""Updates firmware of the specified device.
Keyword arguments:
usbvm -- usbvm support flag
whonix -- Flag enforces downloading the metadata updates via Tor
"""
if not self.fwupdagent_dom0 and not self.fwupdagent_usbvm:
print(
f"Updates not supported!!\n"
f"{self.client_version} < 1.2.6"
)
return EXIT_CODES["NO_UPDATES"]
if self.fwupdagent_dom0:
self._get_dom0_updates()
self._parse_dom0_updates_info(self.dom0_updates_info)
if self.fwupdagent_usbvm:
self._get_usbvm_devices()
with open(FWUPD_USBVM_LOG) as usbvm_device_info:
raw = usbvm_device_info.read()
self._parse_usbvm_updates(raw)
if self.fwupdagent_usbvm and self.fwupdagent_dom0:
update_dict = {
"usbvm": self.usbvm_updates_list,
"dom0": self.dom0_updates_list
}
ret_input = self._user_input(update_dict, usbvm=True)
elif self.fwupdagent_usbvm and not self.fwupdagent_dom0:
update_dict = {
"dom0": [],
"usbvm": self.usbvm_updates_list
}
ret_input = self._user_input(update_dict, usbvm=True)
elif not self.fwupdagent_usbvm and self.fwupdagent_dom0:
update_dict = {
"dom0": self.dom0_updates_list
}
ret_input = self._user_input(update_dict)
if ret_input == EXIT_CODES["NO_UPDATES"]:
exit(EXIT_CODES["NO_UPDATES"])
vm_name, choice = ret_input
self._parse_parameters(update_dict, vm_name, choice)
self._download_firmware_updates(self.url, self.sha, whonix=whonix)
if self.name == "System Firmware":
Path(BIOS_UPDATE_FLAG).touch(mode=0o644, exist_ok=True)
extracted_path = self.arch_path.replace(".cab", "")
self._verify_dmi(extracted_path, self.version)
if vm_name == "dom0":
self._install_dom0_firmware_update(self.arch_path)
if vm_name == "usbvm":
self._validate_usbvm_dirs()
self._copy_firmware_updates(self.arch_name)
self._install_usbvm_firmware_update(self.arch_name)
def _parse_downgrades(self, device_list):
"""Parses information about possible downgrades.
Keywords argument:
device_list -- list of connected devices
"""
downgrades = []
if "No detected devices" in device_list:
return downgrades
dom0_devices_info_dict = json.loads(device_list)
for device in dom0_devices_info_dict["Devices"]:
if "Releases" in device:
try:
version = device["Version"]
except KeyError:
continue
downgrades.append(
{
"Name": device["Name"],
"Version": device["Version"],
"Releases": [
{
"Version": downgrade["Version"],
"Description": downgrade["Description"],
"Url": downgrade["Uri"],
"Checksum": downgrade["Checksum"][0]
} for downgrade in device["Releases"]
if l_ver(downgrade["Version"]) < l_ver(version)
]
}
)
return downgrades
def _install_dom0_firmware_downgrade(self, arch_path):
"""Installs firmware downgrade for specified device.
Keywords arguments:
arch_path - absolute path to firmware downgrade archive
"""
cmd_install = [
FWUPDMGR,
"--allow-older",
"install",
arch_path
]
p = subprocess.Popen(cmd_install)
p.wait()
if p.returncode != 0:
raise Exception("fwudp-qubes: Firmware downgrade failed")
def downgrade_firmware(self, usbvm=False, whonix=False):
"""Downgrades firmware of the specified device.
Keyword arguments:
usbvm -- usbvm support flag
whonix -- Flag enforces downloading the metadata updates via Tor
"""
if not self.fwupdagent_dom0 and not self.fwupdagent_usbvm:
print(
f"Downgrades not supported!!\n"
f"{self.client_version} < 1.2.6"
)
return EXIT_CODES["NO_UPDATES"]
if self.fwupdagent_dom0:
self._get_dom0_devices()
dom0_downgrades = self._parse_downgrades(self.dom0_devices_info)
if self.fwupdagent_usbvm:
self._get_usbvm_devices()
with open(FWUPD_USBVM_LOG) as usbvm_device_info:
raw = usbvm_device_info.read()
usbvm_downgrades = self._parse_downgrades(raw)
if self.fwupdagent_usbvm and self.fwupdagent_dom0:
downgrade_dict = {