forked from badabing2005/PixelFlasher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.py
5951 lines (5134 loc) · 258 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
import urllib3
import warnings
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
_magisk_apks = None
# ============================================================================
# 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 BetaData
# ============================================================================
class BetaData:
def __init__(self, release_date, build, emulator_support, security_patch_level, google_play_services, beta_expiry_date, incremental, security_patch, devices):
self.release_date = release_date
self.build = build
self.emulator_support = emulator_support
self.security_patch_level = security_patch_level
self.google_play_services = google_play_services
self.beta_expiry_date = beta_expiry_date
self.incremental = incremental
self.security_patch = security_patch
self.devices = devices
# ============================================================================
# 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
# ============================================================================
# Class MagiskApk
# ============================================================================
class MagiskApk():
def __init__(self, type):
self.type = type
# ============================================================================
# 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):
global _unlocked_devices
if item in _unlocked_devices:
return True
else:
return False
# ============================================================================
# Function add_unlocked_device
# ============================================================================
def add_unlocked_device(item):
global _unlocked_devices
if item not in _unlocked_devices:
_unlocked_devices.append(item)
# ============================================================================
# Function remove_unlocked_device
# ============================================================================
def remove_unlocked_device(item):
global _unlocked_devices
if item in _unlocked_devices:
_unlocked_devices.remove(item)
# ============================================================================
# Function get_unlocked_device
# ============================================================================
def get_unlocked_device():
global _unlocked_devices
return _unlocked_devices
# ============================================================================
# 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_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):