forked from badabing2005/PixelFlasher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phone.py
3543 lines (3305 loc) · 172 KB
/
phone.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
import contextlib
import re
import subprocess
import time
import traceback
from datetime import datetime
from urllib.parse import urlparse
from constants import *
from runtime import *
# ============================================================================
# Class Package
# ============================================================================
class Package():
def __init__(self, value):
self.value = value
self.type = ''
self.installed = False
self.enabled = False
self.user0 = False
self.magisk_denylist = False
self.details = ''
self.path = ''
self.path2 = ''
self.label = ''
self.icon = ''
self.uid = ''
# ============================================================================
# Class Backup
# ============================================================================
class Backup():
def __init__(self, value):
self.value = value # sha1
self.date = ''
self.firmware = ''
# ============================================================================
# Class Vbmeta
# ============================================================================
class Vbmeta():
def __init__(self):
self.clear()
def clear(self):
self.type = '' # one of ["a_only", "ab", "none"]
self.verity_a = None
self.verity_b = None
self.verification_a = None
self.verification_b = None
# ============================================================================
# Class Magisk
# ============================================================================
class Magisk():
def __init__(self, dirname):
self.dirname = dirname
# ============================================================================
# Class MagiskApk
# ============================================================================
class MagiskApk():
def __init__(self, type):
self.type = type
# ============================================================================
# Class Device
# ============================================================================
class Device():
# Class variable
vendor = "google"
def __init__(self, id, mode, true_mode = None):
# Instance variables
self.id = id
self.mode = mode
if true_mode:
self.true_mode = true_mode
else:
self.true_mode = mode
# The below are for caching.
self._adb_device_info = None
self._fastboot_device_info = None
self._hardware = None
self._build = None
self._api_level = None
self._architecture = None
self._active_slot = None
self._bootloader_version = None
self._sys_oem_unlock_allowed = None
self._ro_boot_flash_locked = None
self._ro_boot_vbmeta_device_state = None
self._vendor_boot_verifiedbootstate = None
self._ro_product_first_api_level = None
self._ro_boot_verifiedbootstate = None
self._ro_boot_veritymode = None
self._vendor_boot_vbmeta_device_state = None
self._ro_boot_warranty_bit = None
self._ro_warranty_bit = None
self._ro_secure = None
self._ro_zygote = None
self._ro_vendor_product_cpu_abilist = None
self._ro_vendor_product_cpu_abilist32 = None
self._rooted = None
self._unlocked = None
self._magisk_version = None
self._magisk_app_version = None
self._magisk_version_code = None
self._magisk_app_version_code = None
self._get_magisk_detailed_modules = None
self._magisk_modules_summary = None
self._magisk_apks = None
self._magisk_config_path = None
self._has_init_boot = None
self._slot_retry_count_a = None
self._slot_unbootable_a = None
self._slot_successful_a = None
self._slot_retry_count_b = None
self._slot_unbootable_b = None
self._slot_successful_b = None
self.packages = {}
self.backups = {}
self.vbmeta = {}
self._ro_kernel_version = None
# Get vbmeta details
self.vbmeta = self.get_vbmeta_details()
# ----------------------------------------------------------------------------
# property adb_device_info
# ----------------------------------------------------------------------------
@property
def adb_device_info(self):
if self.mode == 'adb':
if self._adb_device_info is None:
self._adb_device_info = self.device_info
else:
self._adb_device_info = ''
return self._adb_device_info
# ----------------------------------------------------------------------------
# property unlock_ability
# ----------------------------------------------------------------------------
@property
def unlock_ability(self):
if self.mode == 'adb':
return
try:
theCmd = f"\"{get_fastboot()}\" -s {self.id} flashing get_unlock_ability"
res = run_shell(theCmd)
if res.returncode != 0:
return 'UNKNOWN'
lines = (f"{res.stderr}{res.stdout}").splitlines()
for line in lines:
if "get_unlock_ability:" in line:
value = line.split("get_unlock_ability:")[1].strip()
if value == '1':
return "Yes"
elif value == '0':
return "No"
else:
return "UNKNOWN"
return 'UNKNOWN' # Value not found
except Exception as e:
traceback.print_exc()
print(f"\n{datetime.now():%Y-%m-%d %H:%M:%S} ERROR: Could not get unlock ability.")
puml("#red:ERROR: Could not get unlock ability;\n", True)
return 'UNKNOWN'
# ----------------------------------------------------------------------------
# method get_package_details
# ----------------------------------------------------------------------------
def get_package_details(self, package):
if self.mode != 'adb':
return
try:
theCmd = f"\"{get_adb()}\" -s {self.id} shell dumpsys package {package}"
res = run_shell(theCmd)
if res.returncode == 0:
path = self.get_path_from_details(res.stdout)
return res.stdout, path
else:
return '', ''
except Exception as e:
traceback.print_exc()
print(f"\n{datetime.now():%Y-%m-%d %H:%M:%S} ERROR: Could not get_package_details.")
puml("#red:ERROR: Could not get_package_details;\n", True)
return '', ''
# ----------------------------------------------------------------------------
# method get_battery_details
# ----------------------------------------------------------------------------
def get_battery_details(self):
if self.mode != 'adb':
return
try:
theCmd = f"\"{get_adb()}\" -s {self.id} shell dumpsys battery"
res = run_shell(theCmd)
if res.returncode == 0:
# path = self.get_path_from_details(res.stdout)
return res.stdout #, path
else:
return '', ''
except Exception as e:
traceback.print_exc()
print(f"\n{datetime.now():%Y-%m-%d %H:%M:%S} ERROR: Could not get battery details.")
puml("#red:ERROR: Could not get battery details;\n", True)
return '', ''
# -----------------------------------------------
# Function get_path_from_package_details
# -----------------------------------------------
def get_path_from_details(self, details):
try:
pattern = re.compile(r'(?s)Dexopt state:.*?path:(.*?)\n(?!.*path:)', re.DOTALL)
match = re.search(pattern, details)
if match:
return match[1].strip()
else:
return ''
except Exception as e:
traceback.print_exc()
print(f"\n{datetime.now():%Y-%m-%d %H:%M:%S} ERROR: Could not get_path_from_package_details.")
puml("#red:ERROR: Could not get_path_from_package_details;\n", True)
# ----------------------------------------------------------------------------
# property fastboot_device_info
# ----------------------------------------------------------------------------
@property
def fastboot_device_info(self):
if self.mode == 'f.b':
if self._fastboot_device_info is None:
self._fastboot_device_info = self.device_info
else:
self._fastboot_device_info = ''
return self._fastboot_device_info
# ----------------------------------------------------------------------------
# property device_info
# ----------------------------------------------------------------------------
@property
def device_info(self):
"""
Retrieves device information based on the mode of operation.
If the mode is 'adb', it uses the `getprop` command to fetch the device information using ADB.
If the mode is 'f.b', it uses the `getvar all` command to fetch the device information using Fastboot.
Returns:
str: The device information.
Raises:
RuntimeError: If the ADB or Fastboot command is not found.
Example:
```python
phone = Phone()
info = phone.device_info()
print(info)
```
"""
if self.mode == 'adb':
if get_adb():
theCmd = f"\"{get_adb()}\" -s {self.id} shell /bin/getprop"
device_info = run_shell(theCmd)
if device_info.returncode == 127:
theCmd = f"\"{get_adb()}\" -s {self.id} shell getprop"
device_info = run_shell(theCmd)
return ''.join(device_info.stdout)
else:
print(f"\n{datetime.now():%Y-%m-%d %H:%M:%S} ERROR: adb command is not found!")
puml("#red:ERROR: adb command is not found!;\n", True)
elif self.mode == 'f.b':
if get_fastboot():
theCmd = f"\"{get_fastboot()}\" -s {self.id} getvar all"
device_info = run_shell(theCmd)
if (device_info.stdout == ''):
return ''.join(device_info.stderr)
else:
return ''.join(device_info.stdout)
else:
print(f"\n{datetime.now():%Y-%m-%d %H:%M:%S} ERROR: fastboot command is not found!")
puml("#red:ERROR: fastboot command is not found!;\n", True)
# ----------------------------------------------------------------------------
# Method init
# ----------------------------------------------------------------------------
def init(self, mode):
try:
if mode == 'adb':
device_info = self.adb_device_info
if device_info:
s_active_slot = "ro.boot.slot_suffix"
s_bootloader_version = "ro.bootloader"
s_build = "ro.build.fingerprint"
s_api_level = "ro.build.version.sdk"
s_hardware = "ro.hardware"
s_architecture = "ro.product.cpu.abi"
s_ro_kernel_version = "ro.kernel.version"
# USNF related props
s_sys_oem_unlock_allowed = 'sys.oem_unlock_allowed'
s_ro_boot_flash_locked = 'ro.boot.flash.locked'
s_ro_boot_vbmeta_device_state = 'ro.boot.vbmeta.device_state'
s_vendor_boot_verifiedbootstate = 'vendor.boot.verifiedbootstate'
s_ro_product_first_api_level = 'ro.product.first_api_level'
s_ro_boot_verifiedbootstate = 'ro.boot.verifiedbootstate'
s_ro_boot_veritymode = 'ro.boot.veritymode'
s_vendor_boot_vbmeta_device_state = 'vendor.boot.vbmeta.device_state'
s_ro_boot_warranty_bit = 'ro.boot.warranty_bit'
s_ro_warranty_bit = 'ro.warranty_bit'
s_ro_secure = 'ro.secure'
# Magisk zygote64_32 related props. https://forum.xda-developers.com/t/magisk-magisk-zygote64_32-enabling-32-bit-support-for-apps.4521029/
s_ro_zygote = 'ro.zygote'
s_ro_vendor_product_cpu_abilist = 'ro.vendor.product.cpu.abilist'
s_ro_vendor_product_cpu_abilist32 = 'ro.vendor.product.cpu.abilist32'
# TODO add these.
# pif_product = 'ro.build.name' or 'ro.build.product'
pif_device = 'ro.product.device'
pif_manufacturer = 'ro.product.manufacturer'
pif_brand = 'ro.product.brand'
pif_model = 'ro.product.model'
pif_fingerprint = s_build
pif_security_patch = 'ro.build.version.security_patch'
for line in device_info.split("\n"):
if s_active_slot in line and not self._active_slot:
self._active_slot = self.extract_prop(s_active_slot, line.strip())
self._active_slot = self._active_slot.replace("_", "")
elif s_bootloader_version in line and not self._bootloader_version:
self._bootloader_version = self.extract_prop(s_bootloader_version, line.strip())
elif s_build in line and not self._build:
self._build = self.extract_prop(s_build, line.strip())
self._build = self._build.split('/')[3]
elif s_api_level in line and not self._api_level:
self._api_level = self.extract_prop(s_api_level, line.strip())
elif s_hardware in line and not self._hardware:
self._hardware = self.extract_prop(s_hardware, line.strip())
elif s_architecture in line and not self._architecture:
self._architecture = self.extract_prop(s_architecture, line.strip())
elif s_ro_kernel_version in line and not self._ro_kernel_version:
self._ro_kernel_version = self.extract_prop(s_ro_kernel_version, line.strip())
elif s_sys_oem_unlock_allowed in line and not self._sys_oem_unlock_allowed:
self._sys_oem_unlock_allowed = self.extract_prop(s_sys_oem_unlock_allowed, line.strip())
elif s_ro_boot_flash_locked in line and not self._ro_boot_flash_locked:
self._ro_boot_flash_locked = self.extract_prop(s_ro_boot_flash_locked, line.strip())
if self._ro_boot_flash_locked == '0':
add_unlocked_device(self.id)
elif s_ro_boot_vbmeta_device_state in line and not self._ro_boot_vbmeta_device_state:
self._ro_boot_vbmeta_device_state = self.extract_prop(s_ro_boot_vbmeta_device_state, line.strip())
elif s_vendor_boot_verifiedbootstate in line and not self._vendor_boot_verifiedbootstate:
self._vendor_boot_verifiedbootstate = self.extract_prop(s_vendor_boot_verifiedbootstate, line.strip())
elif s_ro_product_first_api_level in line and not self._ro_product_first_api_level:
self._ro_product_first_api_level = self.extract_prop(s_ro_product_first_api_level, line.strip())
elif s_ro_boot_verifiedbootstate in line and not self._ro_boot_verifiedbootstate:
self._ro_boot_verifiedbootstate = self.extract_prop(s_ro_boot_verifiedbootstate, line.strip())
elif s_ro_boot_veritymode in line and not self._ro_boot_veritymode:
self._ro_boot_veritymode = self.extract_prop(s_ro_boot_veritymode, line.strip())
elif s_vendor_boot_vbmeta_device_state in line and not self._vendor_boot_vbmeta_device_state:
self._vendor_boot_vbmeta_device_state = self.extract_prop(s_vendor_boot_vbmeta_device_state, line.strip())
elif s_ro_boot_warranty_bit in line and not self._ro_boot_warranty_bit:
self._ro_boot_warranty_bit = self.extract_prop(s_ro_boot_warranty_bit, line.strip())
elif s_ro_warranty_bit in line and not self._ro_warranty_bit:
self._ro_warranty_bit = self.extract_prop(s_ro_warranty_bit, line.strip())
elif s_ro_secure in line and not self._ro_secure:
self._ro_secure = self.extract_prop(s_ro_secure, line.strip())
elif s_ro_zygote in line and not self._ro_zygote:
self._ro_zygote = self.extract_prop(s_ro_zygote, line.strip())
elif s_ro_vendor_product_cpu_abilist in line and not self._ro_vendor_product_cpu_abilist:
self._ro_vendor_product_cpu_abilist = self.extract_prop(s_ro_vendor_product_cpu_abilist, line.strip())
elif s_ro_vendor_product_cpu_abilist32 in line and not self._ro_vendor_product_cpu_abilist32:
self._ro_vendor_product_cpu_abilist32 = self.extract_prop(s_ro_vendor_product_cpu_abilist32, line.strip())
elif mode == 'f.b':
device_info = self.fastboot_device_info
if device_info:
s_active_slot = "(bootloader) current-slot"
s_hardware = "(bootloader) product"
s_unlocked = "(bootloader) unlocked"
s_bootloader_version = "(bootloader) version-bootloader"
s_slot_retry_count_a = "(bootloader) slot-retry-count:a"
s_slot_unbootable_a = "(bootloader) slot-unbootable:a"
s_slot_successful_a = "(bootloader) slot-successful:a"
s_slot_retry_count_b = "(bootloader) slot-retry-count:b"
s_slot_unbootable_b = "(bootloader) slot-unbootable:b"
s_slot_successful_b = "(bootloader) slot-successful:b"
for line in device_info.split("\n"):
if s_active_slot in line and not self._active_slot:
self._active_slot = self.extract_prop(s_active_slot, line.strip())
elif s_hardware in line and not self._hardware:
self._hardware = self.extract_prop(s_hardware, line.strip())
elif s_unlocked in line and not self._unlocked:
self._unlocked = self.extract_prop(s_unlocked, line.strip())
if self._unlocked == 'yes':
self._unlocked = True
add_unlocked_device(self.id)
else:
self._unlocked = False
elif s_bootloader_version in line and not self._bootloader_version:
self._bootloader_version = self.extract_prop(s_bootloader_version, line.strip())
elif s_slot_retry_count_a in line and not self._slot_retry_count_a:
self._slot_retry_count_a = self.extract_prop(s_slot_retry_count_a, line.strip())
elif s_slot_unbootable_a in line and not self._slot_unbootable_a:
self._slot_unbootable_a = self.extract_prop(s_slot_unbootable_a, line.strip())
elif s_slot_successful_a in line and not self._slot_successful_a:
self._slot_successful_a = self.extract_prop(s_slot_successful_a, line.strip())
elif s_slot_retry_count_b in line and not self._slot_retry_count_b:
self._slot_retry_count_b = self.extract_prop(s_slot_retry_count_b, line.strip())
elif s_slot_unbootable_b in line and not self._slot_unbootable_b:
self._slot_unbootable_b = self.extract_prop(s_slot_unbootable_b, line.strip())
elif s_slot_successful_b in line and not self._slot_successful_b:
self._slot_successful_b = self.extract_prop(s_slot_successful_b, line.strip())
# set has_init_boot
self._has_init_boot = False
if self.hardware in KNOWN_INIT_BOOT_DEVICES:
self._has_init_boot = True
partitions = self.get_partitions()
if partitions != -1 and ('init_boot' in partitions or 'init_boot_a' in partitions or 'init_boot_b' in partitions):
self._has_init_boot = True
except Exception as e:
traceback.print_exc()
print(f"\n{datetime.now():%Y-%m-%d %H:%M:%S} ERROR: Could not init device class")
puml("#red:ERROR: Could not get_package_details;\n", True)
# ----------------------------------------------------------------------------
# property extract_prop
# ----------------------------------------------------------------------------
def extract_prop(self, search, match):
"""
Extracts a property value based on the search key and match string.
Args:
search (str): The search key to match against.
match (str): The string to match and extract the property value from.
Returns:
str: The extracted property value.
Raises:
None
Example:
```python
phone = Phone()
value = phone.extract_prop("key", "key: value")
print(value)
```
"""
try:
if self.mode == 'adb':
l,r = match.rsplit(": ", 1)
if l.strip() == f"[{search}]":
return r.strip().strip("[").strip("]")
elif self.mode == 'f.b':
l,r = match.rsplit(":", 1)
if l.strip() == f"{search}":
return r.strip()
except Exception as e:
traceback.print_exc()
print(f"\n{datetime.now():%Y-%m-%d %H:%M:%S} ERROR: Could not extract_prop for {search}")
puml("#red:ERROR: Could not extract_prop for {search};\n", True)
return 'ERROR: Could the extract prop value.'
# ----------------------------------------------------------------------------
# property has_init_boot
# ----------------------------------------------------------------------------
@property
def has_init_boot(self):
if self._has_init_boot is None:
return False
else:
return self._has_init_boot
# ----------------------------------------------------------------------------
# property active_slot
# ----------------------------------------------------------------------------
@property
def active_slot(self):
if self._active_slot is None:
return ''
else:
return self._active_slot
# ----------------------------------------------------------------------------
# property inactive_slot
# ----------------------------------------------------------------------------
@property
def inactive_slot(self):
if self.active_slot is None:
return ''
current_slot = self.active_slot
if current_slot == 'a':
return 'b'
else:
return 'a'
# ----------------------------------------------------------------------------
# property bootloader_version
# ----------------------------------------------------------------------------
@property
def bootloader_version(self):
if self._bootloader_version is None:
return ''
else:
return self._bootloader_version
# ----------------------------------------------------------------------------
# property slot_retry_count_a
# ----------------------------------------------------------------------------
@property
def slot_retry_count_a(self):
if self._slot_retry_count_a is None:
return ''
else:
return self._slot_retry_count_a
# ----------------------------------------------------------------------------
# property slot_unbootable_a
# ----------------------------------------------------------------------------
@property
def slot_unbootable_a(self):
if self._slot_unbootable_a is None:
return ''
else:
return self._slot_unbootable_a
# ----------------------------------------------------------------------------
# property slot_successful_a
# ----------------------------------------------------------------------------
@property
def slot_successful_a(self):
if self._slot_successful_a is None:
return ''
else:
return self._slot_successful_a
# ----------------------------------------------------------------------------
# property slot_retry_count_b
# ----------------------------------------------------------------------------
@property
def slot_retry_count_b(self):
if self._slot_retry_count_b is None:
return ''
else:
return self._slot_retry_count_b
# ----------------------------------------------------------------------------
# property slot_unbootable_b
# ----------------------------------------------------------------------------
@property
def slot_unbootable_b(self):
if self._slot_unbootable_b is None:
return ''
else:
return self._slot_unbootable_b
# ----------------------------------------------------------------------------
# property slot_successful_a
# ----------------------------------------------------------------------------
@property
def slot_successful_b(self):
if self._slot_successful_b is None:
return ''
else:
return self._slot_successful_b
# ----------------------------------------------------------------------------
# property build
# ----------------------------------------------------------------------------
@property
def build(self):
if self._build is None:
return ''
else:
return self._build
# ----------------------------------------------------------------------------
# property api_level
# ----------------------------------------------------------------------------
@property
def api_level(self):
if self._api_level is None:
return ''
else:
return self._api_level
# ----------------------------------------------------------------------------
# property hardware
# ----------------------------------------------------------------------------
@property
def hardware(self):
if self._hardware is None:
return ''
else:
return self._hardware
# ----------------------------------------------------------------------------
# property architecture
# ----------------------------------------------------------------------------
@property
def architecture(self):
if self._architecture is None:
return ''
else:
return self._architecture
# ----------------------------------------------------------------------------
# property ro_kernel_version
# ----------------------------------------------------------------------------
@property
def ro_kernel_version(self):
if self._ro_kernel_version is None:
return ''
else:
return self._ro_kernel_version
# ----------------------------------------------------------------------------
# property sys_oem_unlock_allowed
# ----------------------------------------------------------------------------
@property
def sys_oem_unlock_allowed(self):
if self._sys_oem_unlock_allowed is None:
return ''
else:
return self._sys_oem_unlock_allowed
# ----------------------------------------------------------------------------
# property ro_boot_flash_locked
# ----------------------------------------------------------------------------
@property
def ro_boot_flash_locked(self):
if self._ro_boot_flash_locked is None:
return ''
else:
return self._ro_boot_flash_locked
# ----------------------------------------------------------------------------
# property ro_boot_vbmeta_device_state
# ----------------------------------------------------------------------------
@property
def ro_boot_vbmeta_device_state(self):
if self._ro_boot_vbmeta_device_state is None:
return ''
else:
return self._ro_boot_vbmeta_device_state
# ----------------------------------------------------------------------------
# property vendor_boot_verifiedbootstate
# ----------------------------------------------------------------------------
@property
def vendor_boot_verifiedbootstate(self):
if self._vendor_boot_verifiedbootstate is None:
return ''
else:
return self._vendor_boot_verifiedbootstate
# ----------------------------------------------------------------------------
# property ro_product_first_api_level
# ----------------------------------------------------------------------------
@property
def ro_product_first_api_level(self):
if self._ro_product_first_api_level is None:
return ''
else:
return self._ro_product_first_api_level
# ----------------------------------------------------------------------------
# property ro_boot_verifiedbootstate
# ----------------------------------------------------------------------------
@property
def ro_boot_verifiedbootstate(self):
if self._ro_boot_verifiedbootstate is None:
return ''
else:
return self._ro_boot_verifiedbootstate
# ----------------------------------------------------------------------------
# property ro_boot_veritymode
# ----------------------------------------------------------------------------
@property
def ro_boot_veritymode(self):
if self._ro_boot_veritymode is None:
return ''
else:
return self._ro_boot_veritymode
# ----------------------------------------------------------------------------
# property vendor_boot_vbmeta_device_state
# ----------------------------------------------------------------------------
@property
def vendor_boot_vbmeta_device_state(self):
if self._vendor_boot_vbmeta_device_state is None:
return ''
else:
return self._vendor_boot_vbmeta_device_state
# ----------------------------------------------------------------------------
# property ro_boot_warranty_bit
# ----------------------------------------------------------------------------
@property
def ro_boot_warranty_bit(self):
if self._ro_boot_warranty_bit is None:
return ''
else:
return self._ro_boot_warranty_bit
# ----------------------------------------------------------------------------
# property ro_warranty_bit
# ----------------------------------------------------------------------------
@property
def ro_warranty_bit(self):
if self._ro_warranty_bit is None:
return ''
else:
return self._ro_warranty_bit
# ----------------------------------------------------------------------------
# property ro_secure
# ----------------------------------------------------------------------------
@property
def ro_secure(self):
if self._ro_secure is None:
return ''
else:
return self._ro_secure
# ----------------------------------------------------------------------------
# property ro_zygote
# ----------------------------------------------------------------------------
@property
def ro_zygote(self):
if self._ro_zygote is None:
return ''
else:
return self._ro_zygote
# ----------------------------------------------------------------------------
# property ro_vendor_product_cpu_abilist
# ----------------------------------------------------------------------------
@property
def ro_vendor_product_cpu_abilist(self):
if self._ro_vendor_product_cpu_abilist is None:
return ''
else:
return self._ro_vendor_product_cpu_abilist
# ----------------------------------------------------------------------------
# property ro_vendor_product_cpu_abilist32
# ----------------------------------------------------------------------------
@property
def ro_vendor_product_cpu_abilist32(self):
if self._ro_vendor_product_cpu_abilist32 is None:
return ''
else:
return self._ro_vendor_product_cpu_abilist32
# ----------------------------------------------------------------------------
# property unlocked
# ----------------------------------------------------------------------------
@property
def unlocked(self):
if self._unlocked is None:
return ''
else:
add_unlocked_device(self.id)
return self._unlocked
# ----------------------------------------------------------------------------
# property root_symbol
# ----------------------------------------------------------------------------
@property
def root_symbol(self):
if self.mode == 'f.b':
return '?'
elif self.rooted:
return '✓'
else:
return '✗'
# ----------------------------------------------------------------------------
# property magisk_path
# ----------------------------------------------------------------------------
@property
def magisk_path(self):
if self.mode == 'adb' and get_magisk_package():
res = self.get_package_path(get_magisk_package(), True)
if res != -1:
return res
self._rooted = None
return None
# ----------------------------------------------------------------------------
# property magisk_version
# ----------------------------------------------------------------------------
@property
def magisk_version(self):
if self._magisk_version is None and self.mode == 'adb' and self.rooted:
try:
theCmd = f"\"{get_adb()}\" -s {self.id} shell \"su -c \'magisk -c\'\""
res = run_shell(theCmd)
if res.returncode == 0:
regex = re.compile("(.*?):.*\((.*?)\)")
m = re.findall(regex, res.stdout)
if m:
self._magisk_version = f"{m[0][0]}:{m[0][1]}"
self._magisk_version_code = f"{m[0][1]}"
else:
self._magisk_version = res.stdout
self._magisk_version_code = res.stdout
self._magisk_version_code = self._magisk_version.strip(':')
self._magisk_version = self._magisk_version.strip('\n')
except Exception:
try:
theCmd = f"\"{get_adb()}\" -s {self.id} shell \"su -c \'/data/adb/magisk/magisk32 -c\'\""
res = run_shell(theCmd)
if res.returncode == 0:
self._magisk_version = res.stdout.strip('\n')
self._magisk_version_code = self._magisk_version.strip(':')
except Exception:
print(f"\n{datetime.now():%Y-%m-%d %H:%M:%S} ERROR: Could not get magisk version, assuming that it is not rooted.")
traceback.print_exc()
self._rooted = None
return self._magisk_version
# ----------------------------------------------------------------------------
# property magisk_version_code
# ----------------------------------------------------------------------------
@property
def magisk_version_code(self):
if self._magisk_version_code is None:
return ''
else:
return self._magisk_version_code
# ----------------------------------------------------------------------------
# property magisk_config_path
# ----------------------------------------------------------------------------
@property
def magisk_config_path(self):
if self._magisk_config_path is None and self.mode == 'adb' and self.rooted:
try:
theCmd = f"\"{get_adb()}\" -s {self.id} shell \"su -c \'ls -1 $(magisk --path)/.magisk/config\'\""
res = run_shell(theCmd)
if res.returncode == 0:
self._magisk_config_path = res.stdout.strip('\n')
else:
self._magisk_config_path = None
except Exception as e:
traceback.print_exc()
print(f"\n{datetime.now():%Y-%m-%d %H:%M:%S} ERROR: Could not get magisk sha1.")
puml("#red:ERROR: Could not get magisk sha1;\n", True)
self._magisk_config_path = None
return self._magisk_config_path
# ----------------------------------------------------------------------------
# method get_partitions
# ----------------------------------------------------------------------------
def get_partitions(self):
try:
if self.mode != 'adb':
return -1
if self.rooted:
theCmd = f"\"{get_adb()}\" -s {self.id} shell \"su -c \'cd /dev/block/bootdevice/by-name/; ls -1 .\'\""
else:
theCmd = f"\"{get_adb()}\" -s {self.id} shell cd /dev/block/bootdevice/by-name/; ls -1 ."
try:
res = run_shell(theCmd)
if res.returncode == 0:
list = res.stdout.split('\n')
else:
return -1
if not list:
return -1
except Exception as e:
traceback.print_exc()
print(f"\n{datetime.now():%Y-%m-%d %H:%M:%S} ERROR: Could not get partitions list.")
puml("#red:ERROR: Could not get partitions list.;\n", True)
return -1
return list
except Exception as e:
print(f"\n{datetime.now():%Y-%m-%d %H:%M:%S} ERROR: Encountered an error in get_partitions.")
puml("#red:Encountered an error in get_partitions.;\n")
traceback.print_exc()
return -1
# ----------------------------------------------------------------------------
# method get_verity_verification
# ----------------------------------------------------------------------------
def get_verity_verification(self, item):
if self.mode != 'adb':
return -1
if not self.rooted:
return -1
res = self.push_avbctl()
if res != 0:
return -1
try:
theCmd = f"\"{get_adb()}\" -s {self.id} shell \"su -c \'/data/local/tmp/avbctl get-{item}\'\""
print(f"Checking {item} status: ...")
res = run_shell(theCmd)
if res.returncode == 0:
return res.stdout
print(f"Return Code: {res.returncode}.")
print(f"Stdout: {res.stdout}")
print(f"Stderr: {res.stderr}")
return -1
except Exception as e:
traceback.print_exc()
print(f"\n{datetime.now():%Y-%m-%d %H:%M:%S} ERROR: Could not get {item} status.")
puml(f"#red:ERROR: Could not get {item} status.;\n", True)
return -1
# ----------------------------------------------------------------------------
# method get_vbmeta_details
# ----------------------------------------------------------------------------
def get_vbmeta_details(self):
if self.mode != 'adb' or not self.rooted:
return None
try:
self.vbmeta.clear()
vbmeta_a = ''
vbmeta_b = ''
vbmeta_a_only = ''
vbmeta = Vbmeta()
vbmeta.type = 'none'
partitions = self.get_partitions()
if "vbmeta_a" in partitions:
theCmd = f"\"{get_adb()}\" -s {self.id} shell \"su -c \'dd if=/dev/block/bootdevice/by-name/vbmeta_a bs=1 skip=123 count=1 status=none | xxd -p\'\""
res = run_shell(theCmd)
if res.returncode == 0:
vbmeta.type = 'ab'
vbmeta_a = int(res.stdout.strip('\n'))
if "vbmeta_b" in partitions:
theCmd = f"\"{get_adb()}\" -s {self.id} shell \"su -c \'dd if=/dev/block/bootdevice/by-name/vbmeta_b bs=1 skip=123 count=1 status=none | xxd -p\'\""
res = run_shell(theCmd)
if res.returncode == 0:
vbmeta.type = 'ab'
vbmeta_b = int(res.stdout.strip('\n'))
if "vbmeta_a" not in partitions and "vbmeta_b" not in partitions and "vbmeta" in partitions:
theCmd = f"\"{get_adb()}\" -s {self.id} shell \"su -c \'dd if=/dev/block/bootdevice/by-name/vbmeta bs=1 skip=123 count=1 status=none | xxd -p\'\""
res = run_shell(theCmd)
if res.returncode == 0:
vbmeta.type = 'a_only'
vbmeta_a_only = int(res.stdout.strip('\n'))
if vbmeta_a == 0:
vbmeta.verity_a = True
vbmeta.verification_a = True
elif vbmeta_a == 1:
vbmeta.verity_a = False
vbmeta.verification_a = True
elif vbmeta_a == 2:
vbmeta.verity_a = True
vbmeta.verification_a = False
elif vbmeta_a == 3:
vbmeta.verity_a = False
vbmeta.verification_a = False
if vbmeta_b == 0:
vbmeta.verity_b = True
vbmeta.verification_b = True
elif vbmeta_b == 1:
vbmeta.verity_b = False
vbmeta.verification_b = True
elif vbmeta_b == 2:
vbmeta.verity_b = True
vbmeta.verification_b = False
elif vbmeta_b == 3:
vbmeta.verity_b = False
vbmeta.verification_b = False
if vbmeta.type == "a_only":
if vbmeta_a_only == 0:
vbmeta.verity_a = True
vbmeta.verification_a = True
elif vbmeta_a_only == 1:
vbmeta.verity_a = False
vbmeta.verification_a = True
elif vbmeta_a_only == 2:
vbmeta.verity_a = True
vbmeta.verification_a = False
elif vbmeta_a_only == 3:
vbmeta.verity_a = False
vbmeta.verification_a = False
self.vbmeta = vbmeta
except Exception as e:
traceback.print_exc()
print(f"\n{datetime.now():%Y-%m-%d %H:%M:%S} ERROR: Could not get vbmeta details.")
puml("#red:ERROR: Could not get vbmeta details.;\n", True)
return vbmeta
return vbmeta