forked from badabing2005/PixelFlasher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
runtime.py
4560 lines (3889 loc) · 197 KB
/
runtime.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/env python
# This file is part of PixelFlasher https://github.com/badabing2005/PixelFlasher
#
# Copyright (C) 2024 Badabing2005
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 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 Affero General Public License
# for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Also add information on how to contact you by electronic and paper mail.
#
# If your software can interact with users remotely through a computer network,
# you should also make sure that it provides a way for users to get its source.
# For example, if your program is a web application, its interface could
# display a "Source" link that leads users to an archive of the code. There are
# many ways you could offer source, and different solutions will be better for
# different programs; see section 13 for the specific requirements.
#
# You should also get your employer (if you work as a programmer) or school, if
# any, to sign a "copyright disclaimer" for the program, if necessary. For more
# information on this, and how to apply and follow the GNU AGPL, see
# <https://www.gnu.org/licenses/>.
import apk
import binascii
import contextlib
import chardet
import fnmatch
import hashlib
import io
import json
import json5
import math
import ntpath
import os
import re
import shutil
import signal
import sqlite3 as sl
import subprocess
import sys
import random
import tarfile
import tempfile
import threading
import time
import traceback
import zipfile
import psutil
import xml.etree.ElementTree as ET
from datetime import datetime, timezone, timedelta
from os import path
from urllib.parse import urlparse
import xml.etree.ElementTree as ET
from bs4 import BeautifulSoup
from cryptography import x509
from cryptography.fernet import Fernet
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.backends import default_backend
import lz4.frame
import requests
import wx
from packaging.version import parse
from platformdirs import *
from constants import *
from payload_dumper import extract_payload
import cProfile, pstats, io
import avbtool
verbose = False
adb = None
fastboot = None
adb_sha256 = None
fastboot_sha256 = None
phones = []
device_list = []
phone_id = None
advanced_options = False
update_check = True
firmware_model = None
firmware_id = None
custom_rom_id = None
logfile = None
pumlfile = None
sdk_version = None
image_mode = None
image_path = None
custom_rom_file = None
message_box_title = None
message_box_message = None
version = None
db = None
boot = None
system_code_page = None
codepage_setting = False
codepage_value = ''
magisk_package = ''
file_explorer = ''
linux_shell = ''
patched_with = ''
customize_font = False
pf_font_face = ''
pf_font_size = 12
app_labels = {}
xiaomi_list = {}
favorite_pifs = {}
a_only = False
offer_patch_methods = False
use_busybox_shell = False
firmware_hash_valid = False
firmware_has_init_boot = False
rom_has_init_boot = False
dlg_checkbox_values = None
recovery_patch = False
config_path = None
android_versions = {}
android_devices = {}
env_variables = os.environ.copy()
is_ota = False
sdk_is_ok = False
low_memory = False
config = {}
config_file_path = ''
unlocked_devices = []
window_shown = False
puml_enabled = True
# ============================================================================
# Class Boot
# ============================================================================
class Boot():
def __init__(self):
self.boot_id = None
self.boot_hash = None
self.boot_path = None
self.is_patched = None
self.patch_method = None
self.magisk_version = None
self.hardware = None
self.boot_epoch = None
self.package_id = None
self.package_boot_hash = None
self.package_type = None
self.package_sig = None
self.package_path = None
self.package_epoch = None
self.is_odin = None
self.is_stock_boot = None
self.is_init_boot = None
self.patch_source_sha1 = None
self.spl = None
self.fingerprint = None
# ============================================================================
# Class Coords
# ============================================================================
class Coords:
def __init__(self):
self.file_path = get_coords_file_path()
self.data = self.load_data()
def load_data(self):
with contextlib.suppress(Exception):
if path.exists(self.file_path):
with open(self.file_path, "r", encoding='ISO-8859-1', errors="replace") as file:
return json.load(file)
return {}
def save_data(self):
with open(self.file_path, 'w', encoding='ISO-8859-1', errors="replace", newline='\n') as file:
json.dump(self.data, file, indent=4)
def query_entry(self, device, package):
if device in self.data and package in self.data[device]:
return self.data[device][package]
return None
def query_nested_entry(self, device, package, nested_key):
if device in self.data and package in self.data[device] and nested_key in self.data[device][package]:
return self.data[device][package][nested_key]
return None
def update_entry(self, device, package, coordinates):
if device not in self.data:
self.data[device] = {}
self.data[device][package] = coordinates
self.save_data()
def update_nested_entry(self, device, package, nested_key, nested_value):
if device not in self.data:
self.data[device] = {}
if package not in self.data[device]:
self.data[device][package] = {}
self.data[device][package][nested_key] = nested_value
self.save_data()
# ============================================================================
# Class ModuleUpdate
# ============================================================================
class ModuleUpdate():
def __init__(self, url):
self.url = url
# ============================================================================
# Function get_config
# ============================================================================
def get_config():
global config
return config
# ============================================================================
# Function set_config
# ============================================================================
def set_config(value):
global config
config = value
# ============================================================================
# Function get_window_shown
# ============================================================================
def get_window_shown():
global window_shown
return window_shown
# ============================================================================
# Function set_window_shown
# ============================================================================
def set_window_shown(value):
global window_shown
window_shown = value
# ============================================================================
# Function check_for_unlocked
# ============================================================================
def check_for_unlocked(item):
return item in unlocked_devices
# ============================================================================
# Function add_unlocked_device
# ============================================================================
def add_unlocked_device(item):
if item not in unlocked_devices:
unlocked_devices.append(item)
# ============================================================================
# Function remove_unlocked_device
# ============================================================================
def remove_unlocked_device(item):
if item in unlocked_devices:
unlocked_devices.remove(item)
# ============================================================================
# Function set_console_widget
# ============================================================================
def set_console_widget(widget):
global console_widget
console_widget = widget
# ============================================================================
# Function flush_output
# ============================================================================
def flush_output():
global console_widget
if get_window_shown():
wx.YieldIfNeeded()
if console_widget:
sys.stdout.flush()
wx.CallAfter(console_widget.Update)
if get_window_shown():
wx.YieldIfNeeded()
# ============================================================================
# Function get_boot
# ============================================================================
def get_boot():
global boot
return boot
# ============================================================================
# Function set_boot
# ============================================================================
def set_boot(value):
global boot
boot = value
# ============================================================================
# Function get_labels
# ============================================================================
def get_labels():
global app_labels
return app_labels
# ============================================================================
# Function set_labels
# ============================================================================
def set_labels(value):
global app_labels
app_labels = value
# ============================================================================
# Function get_xiaomi
# ============================================================================
def get_xiaomi():
global xiaomi_list
return xiaomi_list
# ============================================================================
# Function set_xiaomi
# ============================================================================
def set_xiaomi(value):
global xiaomi_list
xiaomi_list = value
# ============================================================================
# Function get_favorite_pifs
# ============================================================================
def get_favorite_pifs():
global favorite_pifs
return favorite_pifs
# ============================================================================
# Function set_favorite_pifs
# ============================================================================
def set_favorite_pifs(value):
global favorite_pifs
favorite_pifs = value
# ============================================================================
# Function get_low_memory
# ============================================================================
def get_low_memory():
global low_memory
return low_memory
# ============================================================================
# Function set_low_memory
# ============================================================================
def set_low_memory(value):
global low_memory
low_memory = value
# ============================================================================
# Function get_android_versions
# ============================================================================
def get_android_versions():
global android_versions
return android_versions
# ============================================================================
# Function set_android_versions
# ============================================================================
def set_android_versions(value):
global android_versions
android_versions = value
# ============================================================================
# Function get_android_devices
# ============================================================================
def get_android_devices():
global android_devices
return android_devices
# ============================================================================
# Function set_android_devices
# ============================================================================
def set_android_devices(value):
global android_devices
android_devices = value
# ============================================================================
# Function get_env_variables
# ============================================================================
def get_env_variables():
global env_variables
return env_variables
# ============================================================================
# Function set_env_variables
# ============================================================================
def set_env_variables(value):
global env_variables
env_variables = value
# ============================================================================
# Function get_patched_with
# ============================================================================
def get_patched_with():
global patched_with
return patched_with
# ============================================================================
# Function set_patched_with
# ============================================================================
def set_patched_with(value):
global patched_with
patched_with = value
# ============================================================================
# Function get_db
# ============================================================================
def get_db():
global db
return db
# ============================================================================
# Function set_db
# ============================================================================
def set_db(value):
global db
db = value
# ============================================================================
# Function get_boot_images_dir
# ============================================================================
def get_boot_images_dir():
# boot_images did not change at version 5, so we can keep on using 4
if parse(VERSION) < parse('4.0.0'):
return 'boot_images'
else:
return 'boot_images4'
# ============================================================================
# Function get_factory_images_dir
# ============================================================================
def get_factory_images_dir():
# factory_images only changed after version 5
if parse(VERSION) < parse('9.0.0'):
return 'factory_images'
else:
return 'factory_images9'
# ============================================================================
# Function get_pf_db
# ============================================================================
def get_pf_db():
# we have different db schemas for each of these versions
if parse(VERSION) < parse('4.0.0'):
return 'PixelFlasher.db'
elif parse(VERSION) < parse('9.0.0'):
return 'PixelFlasher4.db'
else:
return 'PixelFlasher9.db'
# ============================================================================
# Function get_verbose
# ============================================================================
def get_verbose():
global verbose
return verbose
# ============================================================================
# Function set_verbose
# ============================================================================
def set_verbose(value):
global verbose
verbose = value
# ============================================================================
# Function get_a_only
# ============================================================================
def get_a_only():
global a_only
return a_only
# ============================================================================
# Function set_a_only
# ============================================================================
def set_a_only(value):
global a_only
a_only = value
# ============================================================================
# Function get_adb
# ============================================================================
def get_adb():
global adb
return adb
# ============================================================================
# Function set_adb
# ============================================================================
def set_adb(value):
global adb
adb = value
# ============================================================================
# Function get_puml_state
# ============================================================================
def get_puml_state():
global puml_enabled
return puml_enabled
# ============================================================================
# Function set_puml_state
# ============================================================================
def set_puml_state(value):
global puml_enabled
puml_enabled = value
# ============================================================================
# Function get_fastboot
# ============================================================================
def get_fastboot():
global fastboot
return fastboot
# ============================================================================
# Function set_fastboot
# ============================================================================
def set_fastboot(value):
global fastboot
fastboot = value
# ============================================================================
# Function get_adb_sha256
# ============================================================================
def get_adb_sha256():
global adb_sha256
return adb_sha256
# ============================================================================
# Function set_adb_sha256
# ============================================================================
def set_adb_sha256(value):
global adb_sha256
adb_sha256 = value
# ============================================================================
# Function get_fastboot_sha256
# ============================================================================
def get_fastboot_sha256():
global fastboot_sha256
return fastboot_sha256
# ============================================================================
# Function set_fastboot_sha256
# ============================================================================
def set_fastboot_sha256(value):
global fastboot_sha256
fastboot_sha256 = value
# ============================================================================
# Function get_phones
# ============================================================================
def get_phones():
global phones
return phones
# ============================================================================
# Function set_phones
# ============================================================================
def set_phones(value):
global phones
phones = value
# ============================================================================
# Function get_device_list
# ============================================================================
def get_device_list():
global device_list
return device_list
# ============================================================================
# Function set_device_list
# ============================================================================
def set_device_list(value):
global device_list
device_list = value
# ============================================================================
# Function get_phone_id
# ============================================================================
def get_phone_id():
global phone_id
return phone_id
# ============================================================================
# Function set_phone_id
# ============================================================================
def set_phone_id(value):
global phone_id
phone_id = value
# ============================================================================
# Function get_phone
# ============================================================================
def get_phone(make_sure_connected=False):
devices = get_phones()
phone_id = get_phone_id()
if phone_id and devices:
for phone in devices:
if phone.id == phone_id:
if make_sure_connected and not phone.is_connected(phone_id):
print(f"\n❌ {datetime.now():%Y-%m-%d %H:%M:%S} ERROR: Device: {phone_id} is not connected.")
return None
return phone
# ============================================================================
# Function get_system_codepage
# ============================================================================
def get_system_codepage():
global system_code_page
return system_code_page
# ============================================================================
# Function set_system_codepage
# ============================================================================
def set_system_codepage(value):
global system_code_page
system_code_page = value
# ============================================================================
# Function get_magisk_package
# ============================================================================
def get_magisk_package():
global magisk_package
return magisk_package
# ============================================================================
# Function set_magisk_package
# ============================================================================
def set_magisk_package(value):
global magisk_package
magisk_package = value
# ============================================================================
# Function get_linux_shell
# ============================================================================
def get_linux_shell():
global linux_shell
return linux_shell
# ============================================================================
# Function set_linux_shell
# ============================================================================
def set_linux_shell(value):
global linux_shell
linux_shell = value
# ============================================================================
# Function get_is_ota
# ============================================================================
def get_ota():
global is_ota
return is_ota
# ============================================================================
# Function set_is_ota
# ============================================================================
def set_ota(self, value):
global is_ota
is_ota = value
self.config.firmware_is_ota = value
if value:
self.enable_disable_radio_button('OTA', True, selected=True, just_select=True)
self.config.flash_mode = 'OTA'
elif self.config.flash_mode == 'OTA':
self.config.flash_mode = 'dryRun'
self.enable_disable_radio_button('dryRun', True, selected=True, just_select=True)
# ============================================================================
# Function get_sdk_state
# ============================================================================
def get_sdk_state():
global sdk_is_ok
return sdk_is_ok
# ============================================================================
# Function set_sdk_state
# ============================================================================
def set_sdk_state(value):
global sdk_is_ok
sdk_is_ok = value
# ============================================================================
# Function get_firmware_hash_validity
# ============================================================================
def get_firmware_hash_validity():
global firmware_hash_valid
return firmware_hash_valid
# ============================================================================
# Function set_firmware_hash_validity
# ============================================================================
def set_firmware_hash_validity(value):
global firmware_hash_valid
firmware_hash_valid = value
# ============================================================================
# Function get_firmware_has_init_boot
# ============================================================================
def get_firmware_has_init_boot():
global firmware_has_init_boot
return firmware_has_init_boot
# ============================================================================
# Function set_firmware_has_init_boot
# ============================================================================
def set_firmware_has_init_boot(value):
global firmware_has_init_boot
firmware_has_init_boot = value
# ============================================================================
# Function get_rom_has_init_boot
# ============================================================================
def get_rom_has_init_boot():
global rom_has_init_boot
return rom_has_init_boot
# ============================================================================
# Function set_rom_has_init_boot
# ============================================================================
def set_rom_has_init_boot(value):
global rom_has_init_boot
rom_has_init_boot = value
# ============================================================================
# Function get_dlg_checkbox_values
# ============================================================================
def get_dlg_checkbox_values():
global dlg_checkbox_values
return dlg_checkbox_values
# ============================================================================
# Function set_dlg_checkbox_values
# ============================================================================
def set_dlg_checkbox_values(value):
global dlg_checkbox_values
dlg_checkbox_values = value
# ============================================================================
# Function get_firmware_model
# ============================================================================
def get_firmware_model():
global firmware_model
return firmware_model
# ============================================================================
# Function set_firmware_model
# ============================================================================
def set_firmware_model(value):
global firmware_model
firmware_model = value
# ============================================================================
# Function get_firmware_id
# ============================================================================
def get_firmware_id():
global firmware_id
return firmware_id
# ============================================================================
# Function set_firmware_id
# ============================================================================
def set_firmware_id(value):
global firmware_id
firmware_id = value
# ============================================================================
# Function get_custom_rom_id
# ============================================================================
def get_custom_rom_id():
global custom_rom_id
return custom_rom_id
# ============================================================================
# Function set_custom_rom_id
# ============================================================================
def set_custom_rom_id(value):
global custom_rom_id
custom_rom_id = value
# ============================================================================
# Function get_logfile
# ============================================================================
def get_logfile():
global logfile
return logfile
# ============================================================================
# Function set_logfile
# ============================================================================
def set_logfile(value):
global logfile
logfile = value
# ============================================================================
# Function get_pumlfile
# ============================================================================
def get_pumlfile():
global pumlfile
return pumlfile
# ============================================================================
# Function set_pumlfile
# ============================================================================
def set_pumlfile(value):
global pumlfile
pumlfile = value
# ============================================================================
# Function get_sdk_version
# ============================================================================
def get_sdk_version():
global sdk_version
return sdk_version
# ============================================================================
# Function set_sdk_version
# ============================================================================
def set_sdk_version(value):
global sdk_version
sdk_version = value
# ============================================================================
# Function get_image_mode
# ============================================================================
def get_image_mode():
global image_mode
return image_mode
# ============================================================================
# Function set_image_mode
# ============================================================================
def set_image_mode(value):
global image_mode
image_mode = value
# ============================================================================
# Function get_image_path
# ============================================================================
def get_image_path():
global image_path
return image_path
# ============================================================================
# Function set_image_path
# ============================================================================
def set_image_path(value):
global image_path
image_path = value
# ============================================================================
# Function get_custom_rom_file
# ============================================================================
def get_custom_rom_file():
global custom_rom_file
return custom_rom_file
# ============================================================================
# Function set_custom_rom_file
# ============================================================================
def set_custom_rom_file(value):
global custom_rom_file
custom_rom_file = value
# ============================================================================
# Function get_message_box_title
# ============================================================================
def get_message_box_title():
global message_box_title
return message_box_title
# ============================================================================
# Function set_message_box_title
# ============================================================================
def set_message_box_title(value):
global message_box_title
message_box_title = value
# ============================================================================
# Function get_message_box_message
# ============================================================================
def get_message_box_message():
global message_box_message
return message_box_message
# ============================================================================
# Function set_message_box_message
# ============================================================================
def set_message_box_message(value):
global message_box_message
message_box_message = value
# ============================================================================
# Function puml
# ============================================================================
def puml(message='', left_ts = False, mode='a'):
if get_puml_state():
with open(get_pumlfile(), mode, encoding="ISO-8859-1", errors="replace") as puml_file:
puml_file.write(message)
if left_ts: