forked from CruelKernel/samsung-exynos9820
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cruelbuild
executable file
·1299 lines (1120 loc) · 43.3 KB
/
cruelbuild
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 python3
import os, errno
from sys import argv, stdout, stderr
import re
import json
import atexit
from copy import deepcopy
from datetime import datetime, timedelta
from subprocess import CalledProcessError, Popen, run, DEVNULL, PIPE
from shutil import which
from enum import Enum, IntEnum
from collections import namedtuple
from struct import unpack_from, calcsize
from select import poll
from time import sleep
from timeit import default_timer as timer
from ctypes import CDLL, get_errno, c_int
from ctypes.util import find_library
from errno import EINTR
from termios import FIONREAD
from fcntl import ioctl
from io import FileIO
from os import fsencode, fsdecode
CK_DIR = os.path.dirname(os.path.realpath(__file__))
toolchain = {
'default': {
'CROSS_COMPILE': 'toolchain/gcc-cfp/gcc-cfp-jopp-only/aarch64-linux-android-4.9/bin/aarch64-linux-android-',
'CLANG_TRIPLE': 'toolchain/clang/host/linux-x86/clang-4639204-cfp-jopp/bin/aarch64-linux-gnu-',
'CC': 'toolchain/clang/host/linux-x86/clang-4639204-cfp-jopp/bin/clang'
},
'cruel': {
'CROSS_COMPILE': 'toolchain/bin/aarch64-cruel-elf-'
},
'samsung': {
'CROSS_COMPILE': 'toolchain/gcc-cfp/gcc-cfp-jopp-only/aarch64-linux-android-4.9/bin/aarch64-linux-android-',
'CLANG_TRIPLE': 'toolchain/clang/host/linux-x86/clang-r349610-jopp/bin/aarch64-linux-gnu-',
'CC': 'toolchain/clang/host/linux-x86/clang-r349610-jopp/bin/clang'
},
'google': {
'CROSS_COMPILE': 'toolchain/aarch64-linux-android-4.9/bin/aarch64-linux-android-',
'CLANG_TRIPLE': 'toolchain/llvm/bin/aarch64-linux-android-',
'CC': 'toolchain/llvm/bin/clang'
},
'proton': {
'CROSS_COMPILE': 'toolchain/bin/aarch64-linux-gnu-',
'CROSS_COMPILE_ARM32': 'toolchain/bin/arm-linux-gnueabi-',
'CC': 'toolchain/bin/clang',
'LD': 'toolchain/bin/ld.lld',
'AR': 'toolchain/bin/llvm-ar',
'NM': 'toolchain/bin/llvm-nm',
'OBJCOPY': 'toolchain/bin/llvm-objcopy',
'OBJDUMP': 'toolchain/bin/llvm-objdump',
'READELF': 'toolchain/bin/llvm-readelf',
'OBJSIZE': 'toolchain/bin/llvm-size',
'STRIP': 'toolchain/bin/llvm-strip',
'LDGOLD': 'toolchain/bin/aarch64-linux-gnu-ld.gold',
'LLVM_AR': 'toolchain/bin/llvm-ar',
'LLVM_DIS': 'toolchain/bin/llvm-dis'
},
'arter97': {
'CROSS_COMPILE': 'toolchain/bin/aarch64-elf-'
},
'arm': {
'CROSS_COMPILE': 'toolchain/bin/aarch64-none-elf-'
},
'system-gcc': {
'CROSS_COMPILE': 'aarch64-linux-gnu-'
},
'system-clang': {
'CC': 'clang',
'CROSS_COMPILE': 'aarch64-linux-gnu-',
'CROSS_COMPILE_ARM32': 'arm-linux-gnu-'
}
}
models = {
'G970F': {
'config': 'exynos9820-beyond0lte_defconfig'
},
'G973F': {
'config': 'exynos9820-beyond1lte_defconfig'
},
'G975F': {
'config': 'exynos9820-beyond2lte_defconfig'
},
'G977B': {
'config': 'exynos9820-beyondx_defconfig'
},
'N970F': {
'config': 'exynos9820-d1_defconfig'
},
'N975F': {
'config': 'exynos9820-d2s_defconfig'
},
'N976B': {
'config': 'exynos9820-d2x_defconfig'
},
'N976N': {
'config': 'exynos9820-d2xks_defconfig'
}
}
OBJTREE_SIZE_GB = 3
_libc = None
def _libc_call(function, *args):
"""Wrapper which raises errors and retries on EINTR."""
while True:
rc = function(*args)
if rc != -1:
return rc
errno = get_errno()
if errno != EINTR:
raise OSError(errno, os.strerror(errno))
Event = namedtuple('Event', ['wd', 'mask', 'cookie', 'name'])
_EVENT_FMT = 'iIII'
_EVENT_SIZE = calcsize(_EVENT_FMT)
class INotify(FileIO):
fd = property(FileIO.fileno)
inotify_raw_events = []
topdir = 1
paths = {}
event_files = set()
def __init__(self, inheritable=False, nonblocking=False):
try:
libc_so = find_library('c')
except RuntimeError:
libc_so = None
global _libc; _libc = _libc or CDLL(libc_so or 'libc.so.6', use_errno=True)
O_CLOEXEC = getattr(os, 'O_CLOEXEC', 0) # Only defined in Python 3.3+
flags = (not inheritable) * O_CLOEXEC | bool(nonblocking) * os.O_NONBLOCK
FileIO.__init__(self, _libc_call(_libc.inotify_init1, flags), mode='rb')
self._poller = poll()
self._poller.register(self.fileno())
def add_watch(self, path, mask):
path = str(path) if hasattr(path, 'parts') else path
wd = _libc_call(_libc.inotify_add_watch, self.fileno(), fsencode(path), mask)
self.paths[wd] = path
if path == '.':
self.topdir = wd
return wd
def readraw(self, timeout=None, read_delay=None):
data = self._readall()
if not data and timeout != 0 and self._poller.poll(timeout):
if read_delay is not None:
sleep(read_delay / 1000.0)
data = self._readall()
return data
def _readall(self):
bytes_avail = c_int()
ioctl(self, FIONREAD, bytes_avail)
if not bytes_avail.value:
return b''
return os.read(self.fileno(), bytes_avail.value)
def collect_events(self, timeout=1, read_delay=None):
self.inotify_raw_events.append(self.readraw(timeout=timeout, read_delay=read_delay))
@staticmethod
def parse_events(data):
pos = 0
events = []
while pos < len(data):
wd, mask, cookie, namesize = unpack_from(_EVENT_FMT, data, pos)
pos += _EVENT_SIZE + namesize
name = data[pos - namesize : pos].split(b'\x00', 1)[0]
events.append(Event(wd, mask, cookie, fsdecode(name)))
return events
def _gather_event_files(self):
event_files = set()
for data in self.inotify_raw_events:
for event in self.parse_events(data):
if event.wd != -1:
if event.wd == self.topdir:
event_files.add(event.name)
else:
event_files.add(os.path.join(self.paths[event.wd], event.name))
else:
fatal("Missing events with SRC_REDUCE=y, try to use j=1")
inotify_raw_events = []
self.event_files.update(event_files)
def get_event_files(self):
self.collect_events()
self._gather_event_files()
return self.event_files
def run(self, args):
with Popen(args, stdout=stdout, stderr=stderr) as proc:
while proc.poll() is None:
self.collect_events()
if proc.returncode:
exit(proc.returncode)
self._gather_event_files()
class flags(IntEnum):
OPEN = 0x00000020 #: File was opened
Q_OVERFLOW = 0x00004000 #: Event queue overflowed
ONLYDIR = 0x01000000 #: only watch the path if it is a directory
EXCL_UNLINK = 0x04000000 #: exclude events on unlinked objects
inotify = INotify()
watch_flags = flags.OPEN | flags.EXCL_UNLINK | flags.ONLYDIR
unused_files = set()
def get_toolchain_cc(compiler):
cc = ''
if 'CC' in toolchain[compiler]:
cc = toolchain[compiler]['CC']
else:
cc = toolchain[compiler]['CROSS_COMPILE'] + 'gcc'
return cc
def mount_tmpfs(target, req_mem_gb):
if not os.path.ismount(target):
meminfo = dict((i.split()[0].rstrip(':'),int(i.split()[1])) for i in open('/proc/meminfo').readlines())
av_mem_gb = int(meminfo['MemAvailable'] / 1024 ** 2)
if av_mem_gb >= req_mem_gb + 2:
ret = run(['sudo', '--non-interactive',
'mount', '-t', 'tmpfs', '-o', 'rw,noatime,size=' + str(req_mem_gb) + 'G', 'tmpfs', target])
if ret.returncode != 0:
print('BUILD: error mounting tmpfs on ' + target, file=stderr)
else:
print('BUILD: tmpfs is mounted on ' + target)
else:
print('BUILD: will not mount tmpfs on ' + target + ' size ' + str(av_mem_gb) + 'G < ' + str(req_mem_gb + 2) + 'G')
else:
print(target + ' is already used as mountpoint', file=stderr)
def umount_tmpfs(target):
if os.path.ismount(target):
ret = run(['sudo', '--non-interactive', 'umount', target])
if ret.returncode != 0:
print("BUILD: error unmounting " + target, file=stderr)
else:
print("BUILD: " + target + " unmounted")
def inotify_install_watchers(inotify, dirname, watch_flags, exclude_dirs, exclude_files):
inotify.add_watch(dirname, watch_flags)
topdirs, unused_files = scandir(dirname)
for d in exclude_dirs:
topdirs.remove(d)
for f in exclude_files:
unused_files.remove(f)
for dir in topdirs:
for root, dirs, files in os.walk(dir, topdown=False):
unused_files.update({ os.path.join(root, f) for f in files })
for d in dirs:
inotify.add_watch(os.path.join(root, d), watch_flags)
return unused_files
def remove_files(*files):
for f in files:
try:
os.remove(f)
except FileNotFoundError:
pass
def del_dirs(src_dir):
for dirpath, _, _ in os.walk(src_dir, topdown=False):
try:
os.rmdir(dirpath)
except OSError:
pass
def mkdir(dirname):
try:
os.mkdir(dirname)
except FileExistsError:
pass
def scandir(dirname):
topdirs = set()
topfiles = set()
with os.scandir(dirname) as it:
for entry in it:
if entry.is_dir():
topdirs.add(entry.name)
else:
topfiles.add(entry.name)
return topdirs, topfiles
def tool_exists(name):
return which(name) is not None
def get_cores_num():
return len(os.sched_getaffinity(0))
def check_env(var):
isset = False
v = os.environ.get(var, 'n')
if v == 'y' or v == 'Y' or v == 'yes' or v == '1':
isset = True
return isset
def set_env(force=False, **env):
for key, value in env.items():
if force or key not in os.environ:
os.environ[key] = value
value = os.environ[key]
print(key + '="' + value + '"')
def fatal(*args, **kwargs):
print(*args, file=stderr, **kwargs)
exit(1)
def print_usage():
msg = f"""
Usage: {argv[0]} <stage> model=<model> name=<name> [+-]<conf1> [+-]<conf2> ...
<stage>: build stage. Required argument. mkimg by default.
Where <stage> can be one of: config, build, mkimg, pack
(:build, :mkimg, :pack). Each next stage will run all
previous stages first. Prefix ':' means skip all previous
stages.
model=<model> phone model name. Required argument.
The script will try to autodetect connected phone if
model is not specified. Supported models:
{list(models.keys())}
Use model=all to build all available kernels.
name=<name>: optional custom kernel name
Use this switch if you want to change the name in
your kernel.
toolchain=<compiler>: optional toolchain switch
Supported compilers: {list(toolchain.keys())}
os_patch_level=<date>: use patch date (YYYY-MM)
instead of default one from build.mkbootimg.<model>
file. For example: os_patch_level="2020-02"
O=dir will perform out of tree kernel build in dir.
The script will try to mount tmpfs in dir if there
is enough available memory.
[+-]<conf>: optional list of configuration switches.
Use prefix '+' to enable the configuration.
Use prefix '-' to disable the configuration.
You can check full list of switches and default ones in
kernel/configs/cruel*.conf directory.
One can use NODEFAULTS=y {argv[0]} +samsung ... to disable
all enabled by default configs.
If you want to flash the kernel, use: FLASH=y {argv[0]}
"""
print(msg)
def parse_stage():
stages = []
modes = ['config', 'build', 'mkimg', 'pack']
omodes = [':config', ':build', ':mkimg', ':pack']
all_modes = modes + omodes
if len(argv) > 1:
mode = argv[1]
if mode not in all_modes:
if mode[0] == '+' or mode[0] == '-' or '=' in mode:
mode = 'mkimg'
else:
print_usage()
fatal('Please, specify the mode from {}.'.format(all_modes))
else:
argv.pop(1)
else:
mode = 'mkimg'
if mode in omodes:
if mode == ':config':
stages = [] # special model for :config
# don't run make defconfig
# just generate config.json file
else:
stages = [mode[1:]]
else:
stages = modes[0:modes.index(mode)+1]
return stages
def find_configs():
configs = { 'kernel': {}, 'order': [] }
prefix_len = len('cruel')
suffix_len = len('.conf')
nodefaults = check_env('NODEFAULTS')
files = [f for f in os.listdir('kernel/configs/') if re.match('^cruel[+-]?.*\.conf$', f)]
for f in files:
if f == 'cruel.conf':
continue
name = f[prefix_len+1:]
name = name[:-suffix_len]
enabled = True if f[prefix_len:prefix_len+1] == '+' else False
configs['kernel'][name] = {
'path': os.path.join('kernel/configs', f),
'enabled': enabled if not nodefaults else False,
'default': enabled
}
if enabled and not nodefaults:
configs['order'].append(name)
configs['order'] = sorted(configs['order'])
return configs
def save_config(file, configs):
conf = deepcopy(configs)
with open(file, 'w') as fh:
json.dump(conf, fh, sort_keys=True, indent=4)
def load_config(file):
with open(file, 'r') as fh:
return json.load(fh)
def switch_config(opt, enable, configs):
if opt in configs['kernel']:
configs['kernel'][opt]['enabled'] = enable
else:
fatal("Unknown config '{}'.".format(opt))
if enable:
if opt in configs['order']:
configs['order'].remove(opt)
configs['order'].append(opt)
else:
if opt in configs['order']:
configs['order'].remove(opt)
def parse_args():
configs = find_configs()
for arg in argv[1:]:
if arg.find('=') != -1:
(key, value) = arg.split('=', 1)
enable = None
if key[0] == '-' or key[0] == '+':
enable = True if key[0] == '+' else False
key = key[1:]
if key not in [ 'name',
'model',
'os_patch_level',
'toolchain',
'magisk',
'O' ]:
fatal('Unknown config {}.'.format(key))
if enable == None:
if key == 'model':
if value == 'all':
value = list(models.keys())
else:
value = value.split(',')
configs[key] = value
else:
switch_config(key, enable, configs)
if not value:
fatal('Please, use {}="<name>".'.format(key))
elif key == 'model':
for m in value:
if m not in models:
fatal('Unknown device model: ' + m)
elif key == 'os_patch_level':
try:
datetime.strptime(value, '%Y-%m')
except Exception:
fatal('Please, use os_patch_level="YYYY-MM". For example: os_patch_level="2020-02"')
elif key == 'toolchain':
if value not in toolchain:
fatal('Unknown toolchain: ' + value)
elif key == 'magisk':
if value != 'canary' and not re.match('^v\d+\.\d+', value):
fatal('Unknown magisk version: ' + value + ' (example: canary, v20.4, v19.4, ...)')
configs['kernel']['magisk']['version'] = value
else:
switch = arg[0:1]
enable = True if switch == '+' else False
opt = arg[1:]
if switch not in ['+', '-']:
fatal("Unknown switch '{0}'. Please, use '+{0}'/'-{0}' to enable/disable option.".format(arg))
switch_config(opt, enable, configs)
if 'model' not in configs:
first_model = list(models.keys())[0]
if len(models) == 1:
configs['model'] = [ first_model ]
else:
try:
configs['model'] = [ adb_get_device_model() ]
except CalledProcessError:
print_usage()
fatal('Please, use model="<model>". For example: model="{}"'.format(first_model))
return configs
def setup_env(features, configs, model):
set_env(ARCH='arm64', PLATFORM_VERSION='11', ANDROID_MAJOR_VERSION='r')
if features['fake_config']:
defconfig = os.path.join('arch/arm64/configs', models[model]['config'])
set_env(KCONFIG_BUILTINCONFIG=defconfig)
def config_info(configs, model):
name = configs.get('name', 'Cruel')
name = name.replace('#MODEL#', model)
print('Name: ' + name)
print('Model: ' + model)
conf_msg = []
kernel_configs = configs['kernel']
for key in configs['order']:
if kernel_configs[key]['enabled']:
conf_msg.append(key + ' (default: ' + ('On' if kernel_configs[key]['default'] else 'Off') + ')')
if conf_msg:
print('Configuration:')
for i in conf_msg:
print("\t" + i)
else:
print('Configuration: basic')
if 'os_patch_level' in configs:
print('OS Patch Level: ' + configs['os_patch_level'])
else:
with open('cruel/build.mkbootimg.' + model, 'r') as fh:
for line in fh:
(arg, val) = line.split('=', 1)
val = val.rstrip()
if arg == 'os_patch_level':
print('OS Patch Level: ' + val)
break
def config_name(name, config='.config'):
run(['scripts/config',
'--file', config,
'--set-str', 'LOCALVERSION', '-' + name], check=True)
def config_model(model, config='.config'):
run(['scripts/config',
'--file', config,
'--disable', 'CONFIG_MODEL_NONE',
'--enable', 'CONFIG_MODEL_' + model], check=True)
def make_config(features, configs, model):
objtree = configs.get('O', '.')
config = os.path.join(os.path.join(CK_DIR, objtree),
'config.' + model)
set_env(KCONFIG_CONFIG=config)
args = ['scripts/kconfig/merge_config.sh', '-O', objtree,
os.path.join('arch/arm64/configs', models[model]['config']),
'kernel/configs/cruel.conf']
kernel_configs = configs['kernel']
for key in configs['order']:
if kernel_configs[key]['enabled']:
args.append(kernel_configs[key]['path'])
inotify.run(args)
if 'name' in configs:
name = configs['name'].replace('#MODEL#', model)
config_name(name, config)
if features['dtb']:
config_model(model, config)
del os.environ['KCONFIG_CONFIG']
def update_magisk(version):
cmd = ['usr/magisk/update_magisk.sh']
if version:
cmd.append(version)
run(cmd, check=True)
with open('usr/magisk/magisk_version', 'r') as fh:
print('Magisk Version: ' + fh.readline())
def switch_toolchain(compiler):
cc = os.path.abspath(get_toolchain_cc(compiler))
if cc.startswith(os.path.realpath('toolchain')):
branch = run(['git', 'submodule', 'foreach', 'git', 'rev-parse', '--abbrev-ref', 'HEAD'],
check=True, stdout=PIPE).stdout.decode('utf-8').splitlines()[1]
if not (tool_exists(cc) and compiler == branch):
ret = run(['git', 'submodule', 'foreach', 'git', 'rev-parse', '--verify', '--quiet', compiler],
stdout=DEVNULL, stderr=DEVNULL)
if ret.returncode != 0:
try:
run(['git', 'submodule', 'foreach', 'git', 'branch', compiler, 'origin/' + compiler],
check=True, stdout=DEVNULL, stderr=DEVNULL)
except CalledProcessError:
fatal("Can't checkout to toolchain: " + compiler)
run(['git', 'submodule', 'foreach', 'git', 'checkout', compiler], check=True)
def build(compiler, objtree='.'):
env = {}
if compiler in ['system-gcc', 'system-clang']:
env = toolchain[compiler]
else:
env = { k: os.path.abspath(v) for k, v in toolchain[compiler].items() }
if objtree != '.':
env['O'] = objtree
if tool_exists('pigz'):
env['KGZIP']='pigz'
if tool_exists('pbzip2'):
env['KBZIP2']='pbzip2'
arg_threads = []
if check_env('DEBUG'):
arg_threads = ['-j', '1', 'V=1']
else:
arg_threads = ['-j', str(get_cores_num())]
inotify.run(['make',
*arg_threads,
*{ k + '=' + v for k, v in env.items() }])
def mkbootimg(os_patch_level, seadroid, config, output, **files):
if not tool_exists('mkbootimg'):
fatal("Please, install 'mkbootimg'.")
print("Preparing {}...".format(output))
for f in files.values():
if not os.path.isfile(f):
fatal("Can't find file '{}'.".format(f))
args = ['mkbootimg']
with open(config) as fh:
for line in fh:
(arg, val) = line.split('=', 1)
if arg == 'os_patch_level' and os_patch_level:
val = os_patch_level
else:
val = val.rstrip()
args.extend(['--' + arg, val])
for k, v in files.items():
args.extend(['--' + k, v])
args.extend(['--output', output])
run(args, check=True)
if seadroid:
with open(output, 'ab') as img:
img.write('SEANDROIDENFORCE'.encode('ascii'))
def get_dtb_configs(models):
dtb_model = {}
model_dtb = {}
for model in models:
with open(os.path.join('cruel', 'dtb.' + model), 'r') as fh:
l = ''
while not l:
l = fh.readline()
dtb = l.split('.')[0]
if dtb not in dtb_model:
dtb_model[dtb] = [model]
else:
dtb_model[dtb].append(model)
model_dtb[model] = dtb
return {'dtb': dtb_model, 'model': model_dtb}
def mkdtboimg(dtbdir, config, output):
if not tool_exists('mkdtboimg'):
fatal("Please, install 'mkdtboimg'.")
print("Preparing {}...".format(output))
inotify.run(['mkdtboimg', 'cfg_create', '--dtb-dir=' + dtbdir, output, config])
def mkvbmeta(output):
if not tool_exists('avbtool'):
fatal("Please, install 'avbtool'.")
print('Preparing vbmeta...')
run(['avbtool', 'make_vbmeta_image', '--out', output], check=True)
def mkaptar(boot, vbmeta):
if not (tool_exists('tar') and tool_exists('md5sum') and tool_exists('lz4')):
fatal("Please, install 'tar', 'lz4' and 'md5sum'.")
print('Preparing AP.tar.md5...')
run(['lz4', '-m', '-f', '-B6', '--content-size', boot, vbmeta], check=True)
run(['tar', '-H', 'ustar', '-c', '-f', 'AP.tar', boot + '.lz4', vbmeta + '.lz4'], check=True)
run(['md5sum AP.tar >> AP.tar && mv AP.tar AP.tar.md5'], check=True, shell=True)
def adb_get_state():
return run(['adb', 'get-state'], stdout=PIPE, stderr=DEVNULL, check=False).stdout.decode('utf-8').strip()
def adb_wait_for_device():
state = adb_get_state()
if not state:
print('Waiting for the device...')
run(['adb', 'wait-for-device'])
def heimdall_wait_for_device():
print('Waiting for download mode...')
run('until heimdall detect > /dev/null 2>&1; do sleep 1; done', shell=True)
def heimdall_in_download_mode():
return run(['heimdall', 'detect'], stdout=DEVNULL, stderr=DEVNULL).returncode == 0
def heimdall_flash_images(imgs):
args = ['heimdall', 'flash']
for partition, image in imgs.items():
args.extend(['--' + partition.upper(), image])
run(args, check=True)
def adb_reboot_download():
run(['adb', 'reboot', 'download'])
def adb_reboot():
run(['adb', 'reboot'])
def adb_get_kernel_version():
run(['adb', 'shell', 'cat', '/proc/version'])
def adb_uid():
return int(run(['adb', 'shell', 'id', '-u'], stdout=PIPE, check=True).stdout.decode('utf-8'))
def adb_check_su():
try:
run(['adb', 'shell', 'command', '-v', 'su'], check=True)
return True
except CalledProcessError:
return False
def adb_get_device_model():
return (run(['adb', 'shell', 'getprop', 'ro.boot.em.model'], stdout=PIPE, check=True)
.stdout.decode('utf-8')
.strip()[3:])
def adb_get_partitions(cmd_adb):
raw_partitions = run(['adb', 'shell', *cmd_adb('cat /proc/partitions')],
stdout=PIPE, check=True).stdout.decode('utf-8').splitlines()[1:]
aliases = run(['adb', 'shell', 'ls', '-1',
'/dev/block/by-name/*'],
stdout=PIPE, check=True).stdout.decode('utf-8').splitlines()
names = run(['adb', 'shell', 'realpath',
'/dev/block/by-name/*'],
stdout=PIPE, check=True).stdout.decode('utf-8').splitlines()
partitions = {}
map_block = {}
block_prefix_len = len('/dev/block/')
alias_prefix_len = len('/dev/block/by-name/')
for (alias, name) in zip(aliases, names):
if alias and name:
alias = alias[alias_prefix_len:]
name = name[block_prefix_len:]
partitions[alias] = { 'block': name }
map_block[name] = partitions[alias]
for part in raw_partitions:
if part:
major, minor, blocks, name = part.split()
if name in map_block:
map_block[name]['size'] = int(blocks) * 1024
return partitions
def flash(samsung=False, **imgs):
if not tool_exists('adb'):
fatal("Please, install 'adb'")
is_root = False
use_su = False
try:
if not heimdall_in_download_mode():
adb_wait_for_device()
is_root = (adb_uid() == 0)
if not is_root and adb_check_su():
use_su = True
is_root = True
except (FileNotFoundError, CalledProcessError):
pass
if is_root:
#cmd_adb = lambda cmd: ['sh', '-x', '-c', '"' + cmd + '"']
cmd_adb = lambda cmd: [cmd.replace('\\','')]
if use_su:
cmd_adb = lambda cmd: ['su', '-c', '"' + cmd + '"']
state = adb_get_state()
tmpdir = '/data/local/tmp'
if state == 'recovery':
tmpdir = '/tmp'
partitions = adb_get_partitions(cmd_adb)
for part, img in imgs.items():
if part not in partitions:
fatal("Unknown partition " + part + " for " + img)
img_size = os.path.getsize(img)
part_size = partitions[part]['size']
if img_size > part_size:
img_size_mb = img_size / 1024 ** 2
part_size_mb = part_size / 1024 ** 2
fatal("{} is bigger than {} partition ({:0.2f} > {:0.2f} MiB)"
.format(img, part, img_size_mb, part_size_mb))
for part, img in imgs.items():
cleanup = lambda: run(['adb', 'shell',
'rm', '-f', os.path.join(tmpdir, img)])
atexit.register(cleanup)
run(['adb', 'push',
img, tmpdir],
check=True)
run(['adb', 'shell', *cmd_adb(
'dd if=' + os.path.join(tmpdir, img) +
' of=/dev/block/by-name/' + part)],
check=True)
cleanup()
atexit.unregister(cleanup)
adb_reboot()
adb_wait_for_device()
adb_get_kernel_version()
elif samsung and tool_exists('heimdall'):
if not heimdall_in_download_mode():
adb_wait_for_device()
adb_reboot_download()
heimdall_wait_for_device()
heimdall_flash_images(imgs)
adb_wait_for_device()
adb_get_kernel_version()
else:
fatal("Please, use 'adb root' or install 'heimdall'")
def flash_zip(zipfile):
if not tool_exists('adb'):
fatal("Please, install 'adb'")
if heimdall_in_download_mode():
fatal("Can't flash zip file while phone is in DOWNLOAD mode. Please, reboot")
is_root = False
use_su = False
try:
adb_wait_for_device()
is_root = (adb_uid() == 0)
if not is_root and adb_check_su():
use_su = True
is_root = True
except (FileNotFoundError, CalledProcessError):
pass
if not is_root:
fatal("Can't flash zip file if root is not available")
state = adb_get_state()
tmpdir = '/data/local/tmp'
execdir = '/data/adb'
if state == 'recovery':
tmpdir = '/tmp'
execdir = '/tmp'
update_binary = os.path.join(execdir, 'update-binary')
zippath = os.path.join(tmpdir, os.path.basename(zipfile))
#cmd_adb = lambda cmd: ['sh', '-x', '-c', '"' + cmd + '"']
cmd_adb = lambda cmd: [cmd.replace('\\','')]
if use_su:
cmd_adb = lambda cmd: ['su', '-c', '"' + cmd + '"']
cleanup = lambda: run(['adb', 'shell',
*cmd_adb('rm -f /tmp/update-binary ' + os.path.join(tmpdir, zipfile))])
atexit.register(cleanup)
run(['adb', 'push', zipfile, tmpdir],
check=True)
run(['adb', 'shell', *cmd_adb((
'unzip -p {zip}' +
' META-INF/com/google/android/update-binary ' +
'> {update}').format(zip=zippath, update=update_binary))],
check=True)
run(['adb', 'shell', *cmd_adb((
'fgrep -qI \\"\\" {update} && ' + # text file
'[ \\"\$(head -n 1 {update})\\" = \\"#!/sbin/sh\\" ] && ' +
'sed -i \\"1c\#!\$(which sh)\\" {update}').format(update=update_binary))],
check=True)
run(['adb', 'shell', *cmd_adb('chmod +x ' + update_binary)],
check=True)
run(['adb', 'shell', *cmd_adb((
'set -o posix; FIFO=\$(mktemp -p {tmp} -u); mkfifo \$FIFO; exec 3<>\$FIFO; rm -f \$FIFO; ' +
'cd {tmp}; {update} 3 3 {zip}').format(tmp=tmpdir, update=update_binary, zip=zippath))],
check=True)
cleanup()
atexit.unregister(cleanup)
adb_reboot()
adb_wait_for_device()
adb_get_kernel_version()
def archive_xz(name, images):
if not tool_exists('xz'):
fatal("Please, install 'xz'.")
# if len(images) == 1:
# print('Preparing {} ...'.format(images[0] + '.xz'))
# run(['xz', '-9', '--force', images[0]], check=True)
# elif tool_exists('tar'):
if tool_exists('tar'):
print('Preparing ' + name + '...')
set_env(force=True, XZ_OPT='-9')
run(['tar', '-cJf', name, *images], check=True)
else:
fatal("Please, install 'tar'.")
def print_recovery_message(words, margin=1):
if not words:
return []
line_len = len(words[0]) + margin * 2
line = [words[0]]
msg = []
for i in range(1, len(words)):
if line_len + len(words[i]) + len(line) - 1 < 47:
line_len += len(words[i])
line.append(words[i])
else:
msg.append('ui_print "***{:^47}***"'.format(' '.join(line)))
line_len = len(words[i]) + margin * 2
line = [words[i]]
if line:
msg.append('ui_print "***{:^47}***"'.format(' '.join(line)))
return msg
def prepare_updater_script(configs, features, dtb_map):
models = configs['model']
kernel_name = configs.get('name', 'Cruel').replace('#MODEL#', '')
device_check = []
process = lambda t, k: ''.join([
chr(x ^ ord(y))
for x, y in zip(t, k * int(len(t) / len(k) + 1000))])
header = '''\
#!/sbin/sh
set -e
ZIPFILE="$3"
ZIPNAME="${ZIPFILE##*/}"
OUTFD="/proc/self/fd/$2"
tmpdir='/tmp'
execdir='/tmp'
BOOTMODE=false
if ps | grep zygote | grep -qv grep; then
BOOTMODE=true
fi
if ps -A 2>/dev/null | grep zygote | grep -qv grep; then
BOOTMODE=true
fi
if $BOOTMODE; then
if [ -n "$TMPDIR" -a -d "$TMPDIR" ]; then
tmpdir="$TMPDIR"
elif [ -d '/data/local/tmp' ]; then
tmpdir='/data/local/tmp'
fi
if [ -d '/data/adb' ]; then
execdir='/data/adb'
fi
fi
ui_print() {
if $BOOTMODE; then
echo "$1"
else
echo -e "ui_print $1\\nui_print" >> $OUTFD
fi
}
show_progress() {
if ! $BOOTMODE; then
echo "progress $1 $2" >> $OUTFD
fi
}
set_progress() {
if ! $BOOTMODE; then
echo "set_progress $1" >> $OUTFD
fi
}
flash() {
dd if="$1" of="$2" &>/dev/null
rm -f "$1"
}
abort() {
ui_print "$1"
exit 1
}
'''
print_models = [