-
Notifications
You must be signed in to change notification settings - Fork 9
/
kernel_builder.py
585 lines (548 loc) · 21.2 KB
/
kernel_builder.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
import os
import sys
import time
from pathlib import Path
from pydantic import BaseModel
from builder.tools import cleaning as cm, commands as ccmd, fileoperations as fo, messages as msg
from builder.configs import DirectoryConfig as dcfg
from builder.managers import ResourceManager
from builder.interfaces import IKernelBuilder
class KernelBuilder(BaseModel, IKernelBuilder):
"""Kernel builder.
:param str codename: Device codename.
:param str base: Kernel source base.
:param str lkv: Linux kernel version.
:param bool clean_kernel: Flag to clean folder with kernel sources.
:param bool ksu: Flag indicating KernelSU support.
"""
codename: str
base: str
lkv: str
clean_kernel: bool
ksu: bool
rmanager: ResourceManager
@staticmethod
def write_localversion() -> None:
with open("localversion", "w", encoding="utf-8") as f:
f.write("~zero_kernel")
@property
def _ucodename(self) -> str:
"""Unified codename for devices series with identical kernels."""
if self.codename in ("dumpling", "cheeseburger"):
return "dumplinger"
elif "guacamole" in self.codename:
return "guacamoles"
else:
return self.codename
@property
def _defconfig(self) -> Path:
"""Define defconfig."""
# list the defconfigs used
op7_defconfigs = {
"los": "lineage_sm8150_defconfig",
}
op5_defconfigs = {
"los": "lineage_oneplus5_defconfig",
"pa": "vendor/paranoid_defconfig" if self.lkv == "4.14" else "paranoid_defconfig",
"x": "msm8998_oneplus_android_defconfig" if self.lkv == "4.14" else "oneplus5_defconfig"
}
# convert output to path object
if "guacamole" in self.codename:
return Path(op7_defconfigs[self.base])
elif self.codename in ("dumpling", "cheeseburger"):
return Path(op5_defconfigs[self.base])
else:
return Path()
def clean_build(self) -> None:
print("\n", end="")
msg.note("Cleaning the build environment..")
cm.git(self.rmanager.paths[self.codename])
cm.git(self.rmanager.paths["AnyKernel3"])
cm.git(self.rmanager.paths["KernelSU"])
for fn in os.listdir():
if fn == "localversion" or fn.endswith(".zip"):
cm.remove(fn)
msg.done("Done!")
def patch_strict_prototypes(self) -> None:
msg.note("Patching sources for Clang 15+ compatibility..")
data = {
self.rmanager.paths[self.codename] /\
"drivers" /\
"char" /\
"diag" /\
"diagchar_core.c":
("void diag_ws_init()", "void diag_ws_on_notify()", "void diag_ws_release()",),
self.rmanager.paths[self.codename] /\
"drivers" /\
"char" /\
"diag" /\
"diag_mux.c":
("int diag_mux_init()", "void diag_mux_exit()",),
self.rmanager.paths[self.codename] /\
"drivers" /\
"char" /\
"diag" /\
"diag_memorydevice.c":
("void diag_md_open_all()", "void diag_md_close_all()",),
self.rmanager.paths[self.codename] /\
"drivers" /\
"char" /\
"diag" /\
"diag_dci.c":
("void diag_dci_wakeup_clients()",),
self.rmanager.paths[self.codename] /\
"drivers" /\
"char" /\
"diag" /\
"diagfwd_bridge.c":
("void diagfwd_bridge_exit()", "uint16_t diag_get_remote_device_mask()",),
self.rmanager.paths[self.codename] /\
"drivers" /\
"char" /\
"diag" /\
"diagfwd_mhi.c":
("int diag_mhi_init()", "void diag_mhi_exit()",),
self.rmanager.paths[self.codename] /\
"drivers" /\
"media" /\
"platform" /\
"msm" /\
"camera_v2" /\
"common" /\
"msm_camera_tz_util.c":
("struct qseecom_handle *msm_camera_tz_get_ta_handle()",),
self.rmanager.paths[self.codename] /\
"drivers" /\
"media" /\
"platform" /\
"msm" /\
"vidc" /\
"msm_vidc_common.c":
("void msm_comm_handle_thermal_event()",),
self.rmanager.paths[self.codename] /\
"drivers" /\
"soc" /\
"qcom" /\
"msm_bus" /\
"msm_bus_rpm_smd.c":
("static int voice_svc_dummy_reg()",),
self.rmanager.paths[self.codename] /\
"drivers" /\
"soc" /\
"qcom" /\
"msm_bus" /\
"msm_bus_rpm_smd.c":
("void msm_bus_rpm_set_mt_mask()",),
self.rmanager.paths[self.codename] /\
"drivers" /\
"staging" /\
"qca-wifi-host-cmn" /\
"hif" /\
"src" /\
"ce" /\
"ce_service.c":
("struct ce_ops *ce_services_legacy()",),
self.rmanager.paths[self.codename] /\
"drivers" /\
"staging" /\
"qcacld-3.0" /\
"core" /\
"hdd" /\
"src" /\
"wlan_hdd_main.c":
("hdd_adapter_t *hdd_get_first_valid_adapter()",),
self.rmanager.paths[self.codename] /\
"drivers" /\
"video" /\
"fbdev" /\
"msm" /\
"mdss_mdp.c":
("struct irq_info *mdss_intr_line()",),
self.rmanager.paths[self.codename] /\
"drivers" /\
"video" /\
"fbdev" /\
"msm" /\
"mdss_util.c":
("struct mdss_util_intf *mdss_get_util_intf()",)
}
# the following files are not present in 4.14
if self._linux_kernel_version != "4.14":
extra_non_414 = {
self.rmanager.paths[self.codename] /\
"drivers" /\
"soc" /\
"qcom" /\
"qdsp6v2" /\
"voice_svc.c":
("void msm_bus_rpm_set_mt_mask()", "static int voice_svc_dummy_reg()"),
self.rmanager.paths[self.codename] /\
"drivers" /\
"thermal" /\
"msm_thermal-dev.c":
("int msm_thermal_ioctl_init()", "void msm_thermal_ioctl_cleanup()",),
}
data.update(extra_non_414)
# PA needs this, LineageOS does not
if self.base == "pa":
extra_pa = {
self.rmanager.paths[self.codename] /\
"drivers" /\
"staging" /\
"qca-wifi-host-cmn" /\
"target_if" /\
"core" /\
"src" /\
"target_if_main.c":
("struct target_if_ctx *target_if_get_ctx()",),
self.rmanager.paths[self.codename] /\
"drivers" /\
"staging" /\
"qca-wifi-host-cmn" /\
"wlan_cfg" /\
"wlan_cfg.c":
("struct wlan_cfg_dp_soc_ctxt *wlan_cfg_soc_attach()",),
}
data.update(extra_pa)
# start the patching process
contents = ""
for fname, funcnames in data.items():
with open(fname, "r", encoding="utf-8") as f:
contents = f.read()
# replace: "()" -> "(void)"
for func in funcnames:
contents = contents.replace(func, func.replace("()", "(void)"))
with open(fname, "w") as f:
f.write(contents)
msg.done("Done!")
def patch_anykernel3(self) -> None:
cm.remove(self.rmanager.paths["AnyKernel3"] / "ramdisk")
cm.remove(self.rmanager.paths["AnyKernel3"] / "models")
fo.ucopy(
dcfg.root / "builder" / "modifications" / self._ucodename / "anykernel3" / "ramdisk",
self.rmanager.paths["AnyKernel3"] / "ramdisk"
)
fo.ucopy(
dcfg.root / "builder" / "modifications" / self._ucodename / "anykernel3" / "anykernel.sh",
self.rmanager.paths["AnyKernel3"] / "anykernel.sh"
)
def patch_rtl8812au_source_mod_v5642(self) -> None:
# Makefile
fo.replace_lines(
Path("Makefile").absolute(),
(
"#EXTRA_CFLAGS += -Wno-parentheses-equality",
"#EXTRA_CFLAGS += -Wno-pointer-bool-conversion",
"$(MAKE) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) -C $(KSRC) M=$(shell pwd) modules",
"CONFIG_PLATFORM_I386_PC = y",
"CONFIG_PLATFORM_ANDROID_ARM64 = n",
),
(
"EXTRA_CFLAGS += -Wno-parentheses-equality",
"EXTRA_CFLAGS += -Wno-pointer-bool-conversion\nEXTRA_CFLAGS += -Wno-pointer-bool-conversion\nEXTRA_CFLAGS += -Wno-pragma-pack\nEXTRA_CFLAGS += -Wno-unused-variable",
'$(MAKE) ARCH=$(ARCH) SUBARCH=$(ARCH) REAL_CC=${CC_DIR}/clang CLANG_TRIPLE=aarch64-linux-gnu- CROSS_COMPILE=$(CROSS_COMPILE) -C $(KSRC) M=$(shell pwd) O="$(KBUILD_OUTPUT)" modules',
"CONFIG_PLATFORM_I386_PC = n",
"CONFIG_PLATFORM_ANDROID_ARM64 = y\nCONFIG_CONCURRENT_MODE = n",
)
)
# ioctl_cfg80211.h
fo.replace_lines(
Path("os_dep", "linux", "ioctl_cfg80211.h").absolute(),
("#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 26)) && (LINUX_VERSION_CODE < KERNEL_VERSION(4, 7, 0))",),
("#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 26)) && (LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 0))",)
)
# ioctl_cfg80211.c
fo.replace_lines(
Path("os_dep", "linux", "ioctl_cfg80211.c").absolute(),
(
"sinfo->bss_param.flags |= STATION_INFO_BSS_PARAM_SHORT_PREAMBLE;",
"sinfo->bss_param.flags |= STATION_INFO_BSS_PARAM_SHORT_SLOT_TIME;",
"sinfo->bss_param.flags |= STATION_INFO_BSS_PARAM_CTS_PROT;",
"sinfo->bss_param.flags |= STATION_INFO_BSS_PARAM_DTIM_PERIOD;",
),
(
"sinfo->bss_param.flags |= NL80211_STA_BSS_PARAM_SHORT_PREAMBLE;",
"sinfo->bss_param.flags |= NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME;",
"sinfo->bss_param.flags |= NL80211_STA_BSS_PARAM_CTS_PROT;",
"sinfo->bss_param.flags |= NL80211_STA_BSS_PARAM_DTIM_PERIOD;",
)
)
def patch_rtl8812au(self) -> None:
# copy RTL8812AU sources into kernel sources
msg.note("Adding RTL8812AU drivers into the kernel..")
fo.ucopy(
self.rmanager.paths["rtl8812au"],
self.rmanager.paths[self.codename] /\
"drivers" /\
"net" /\
"wireless" /\
"realtek" /\
"rtl8812au"
)
# modify sources depending on driver version
os.chdir(
self.rmanager.paths[self.codename] /\
"drivers" /\
"net" /\
"wireless" /\
"realtek" /\
"rtl8812au"
)
self.patch_rtl8812au_source_mod_v5642()
cm.remove(".git*")
os.chdir(dcfg.root)
# include the driver into build process
makefile = self.rmanager.paths[self.codename] /\
"drivers" /\
"net" /\
"wireless" /\
"realtek" /\
"Makefile"
kconfig = self.rmanager.paths[self.codename] /\
"drivers" /\
"net" /\
"wireless" /\
"Kconfig"
defconfig = self.rmanager.paths[self.codename] /\
"arch" /\
"arm64" /\
"configs" /\
self._defconfig
with open(makefile, "a", encoding="utf-8") as f:
f.write("obj-$(CONFIG_88XXAU) += rtl8812au/")
fo.insert_before_line(
kconfig,
"endif",
"source \"drivers/net/wireless/realtek/rtl8812au/Kconfig\""
)
with open(defconfig, "a", encoding="utf-8") as f:
extra_configs = (
"CONFIG_88XXAU=y",
"CONFIG_MODULE_FORCE_LOAD=y",
"CONFIG_MODULE_FORCE_UNLOAD=y",
"CONFIG_CFG80211_WEXT=y",
"CONFIG_CFG80211_WEXT_EXPORT=y",
"CONFIG_CONCURRENT_MODE=n",
"CONFIG_MAC80211=y",
"CONFIG_RTL8187=y",
"CONFIG_RTLWIFI=y",
)
f.write("\n".join(extra_configs))
f.write("\n")
def patch_ksu(self) -> None:
msg.note("Adding KernelSU into the kernel..")
# extract KSU_GIT_VERSION environment variable manually
goback = Path.cwd()
os.chdir(self.rmanager.paths["KernelSU"])
os.environ["KSU_GIT_VERSION"] = str(
# official formula documented in KernelSU's Makefile
10000 + int(ccmd.launch("git rev-list --count HEAD", get_output=True)) + 200 # type: ignore
)
os.chdir(goback)
makefile = self.rmanager.paths[self.codename] /\
"drivers" /\
"Makefile"
kconfig = self.rmanager.paths[self.codename] /\
"drivers" /\
"Kconfig"
# include into the build process via symlink
os.symlink(
self.rmanager.paths["KernelSU"] / "kernel",
self.rmanager.paths[self.codename] /\
"drivers" /\
"kernelsu"
)
with open(makefile, "a", encoding="utf-8") as f:
f.write("obj-$(CONFIG_KSU) += kernelsu/\n")
fo.insert_before_line(
kconfig,
"endmenu",
"source \"drivers/kernelsu/Kconfig\""
)
# either patch kernel or KernelSU sources, depending on Linux kernel version
target_dir = dcfg.root / "KernelSU" if self._linux_kernel_version == "4.14" else self.rmanager.paths[self.codename]
fo.ucopy(
dcfg.root / "builder" / "modifications" / self._ucodename / self._linux_kernel_version / "kernelsu-compat.patch",
target_dir
)
os.chdir(target_dir)
fo.apply_patch("kernelsu-compat.patch")
os.chdir(goback)
# add configs into defconfig
defconfig = self.rmanager.paths[self.codename] /\
"arch" /\
"arm64" /\
"configs" /\
self._defconfig
with open(defconfig, "a", encoding="utf-8") as f:
extra_configs = (
"CONFIG_KSU=y",
"CONFIG_MODULES=y",
"CONFIG_MODULE_UNLOAD=y",
"CONFIG_MODVERSIONS=y",
"CONFIG_DIAG_CHAR=y",
"CONFIG_KPROBES=y",
"CONFIG_HAVE_KPROBES=y",
"CONFIG_KPROBE_EVENTS=y",
)
f.write("\n".join(extra_configs))
f.write("\n")
def patch_qcacld(self) -> None:
goback = Path.cwd()
fo.ucopy(
dcfg.root / "builder" / "modifications" / self._ucodename / self._linux_kernel_version / "qcacld_pa.patch",
self.rmanager.paths[self.codename]
)
os.chdir(self.rmanager.paths[self.codename])
fo.apply_patch("qcacld_pa.patch")
os.chdir(goback)
def patch_ioctl(self) -> None:
ioctl = self.rmanager.paths[self.codename] /\
"drivers" /\
"platform" /\
"msm" /\
"ipa" /\
"ipa_v3" /\
"ipa.c"
fo.replace_lines(
ioctl.absolute(),
(" u8 header[128] = { 0 };",),
(" u8 header[512] = { 0 };",),
)
def patch_kernel(self) -> None:
# -Wstrict-prototypes patch to build with Clang 15+
clang_cmd = f'{self.rmanager.paths["clang"] / "bin" / "clang"} --version'
clang_ver = str(ccmd.launch(clang_cmd, get_output=True)).split("clang version ")[1].split(".")[0]
if int(clang_ver) >= 15:
self.patch_strict_prototypes()
# apply .patch files
fo.ucopy(
dcfg.root / "builder" / "modifications" / self._ucodename / self._linux_kernel_version,
self.rmanager.paths[self.codename],
("kernelsu-compat.patch", "qcacld_pa.patch")
)
os.chdir(self.rmanager.paths[self.codename])
for pf in Path.cwd().glob("*.patch"):
fo.apply_patch(pf)
# add support for CONFIG_MAC80211 kernel option
data = ""
files = ("tx.c", "mlme.c")
for fn in files:
with open(Path("net", "mac80211", fn), "r", encoding="utf-8") as f:
data = f.read().replace("case IEEE80211_BAND_60GHZ:", "case NL80211_BAND_60GHZ:")
with open(Path("net", "mac80211", fn), "w", encoding="utf-8") as f:
f.write(data)
# some patches only for ParanoidAndroid
if self.base == "pa":
if self._linux_kernel_version == "4.4":
self.patch_qcacld()
self.patch_ioctl()
os.chdir(dcfg.root)
def patch_all(self) -> None:
self.patch_anykernel3()
self.patch_kernel()
# optionally include KernelSU support
if self.ksu:
self.patch_ksu()
self.patch_rtl8812au()
msg.done("Patches added!")
def build(self) -> None:
print("\n", end="")
msg.note("Launching the build..")
os.chdir(self.rmanager.paths[self.codename])
# launch "make"
punits = ccmd.launch("nproc --all", get_output=True)
cmd1 = "make -j{} O=out {} "\
"ARCH=arm64 "\
"SUBARCH=arm64 "\
"LLVM=1 "\
"LLVM_IAS=1"\
.format(punits, self._defconfig)
cmd2 = "make -j{} O=out "\
"ARCH=arm64 "\
"SUBARCH=arm64 "\
"CROSS_COMPILE=llvm- "\
"CROSS_COMPILE_ARM32=arm-linux-androideabi- "\
"CLANG_TRIPLE=aarch64-linux-gnu- "\
"LLVM=1 "\
"LLVM_IAS=1 "\
"CXX=clang++ "\
"AS=llvm-as"\
.format(punits)
# for PA's 4.14, extend the "make" command with additional variables
if (self.base, self._linux_kernel_version) == ("pa", "4.14"):
cmd2 = f"{cmd2} LEX=flex YACC=bison"
# launch and time the build process
time_start = time.time()
ccmd.launch(cmd1)
ccmd.launch(cmd2)
time_stop = time.time()
time_elapsed = time_stop - time_start
# convert elapsed time into human readable format
secs = time_elapsed % (24 * 3600)
hours = secs // 3600
secs %= 3600
mins = secs // 60
secs %= 60
msg.done("Done! Time spent for the build: %02d:%02d:%02d" % (hours, mins, secs))
@property
def _linux_kernel_version(self) -> str:
data = ""
version = []
with open(self.rmanager.paths[self.codename] / "Makefile", encoding="utf-8") as f:
data = f.read()
params = ("VERSION", "PATCHLEVEL")
# find the required lines in a single data run-through
for line in data.splitlines():
for p in params:
if line.split(" =")[0] == p:
version.append(line.split(f"{p} = ")[1])
# stop the loop when all values are found
if len(version) == len(params):
break
return ".".join(version)
def create_zip(self) -> None:
print("\n", end="")
msg.note("Forming final ZIP file..")
fo.ucopy(
self.rmanager.paths[self.codename] /\
"out" /\
"arch" /\
"arm64" /\
"boot" /\
"Image.gz-dtb",
self.rmanager.paths["AnyKernel3"] / "Image.gz-dtb"
)
# define kernel versions: Linux and internal
verbase = self._linux_kernel_version
ver_int = os.getenv("KVERSION")
# create the final ZIP file
name_suffix = "-ksu" if self.ksu else ""
name_full = f'{os.getenv("KNAME", "zero")}-{ver_int}-{self._ucodename}-{self.base}-{verbase}{name_suffix}'
kdir = dcfg.root / dcfg.kernel
if not kdir.is_dir():
os.makedirs(kdir)
os.chdir(self.rmanager.paths["AnyKernel3"])
# this is not the best solution, but is the easiest
cmd = f"zip -r9 {kdir / name_full}.zip . -x *.git* *README* *LICENSE* *placeholder"
ccmd.launch(cmd)
os.chdir(dcfg.root)
msg.done("Done!")
def run(self) -> None:
os.chdir(dcfg.root)
msg.banner("zero kernel builder")
msg.note("Setting up tools and links..")
self.rmanager.read_data()
self.rmanager.generate_paths()
self.rmanager.download()
self.rmanager.export_path()
self.clean_build()
if self.clean_kernel:
sys.exit(0)
self.write_localversion()
msg.done("Done! Tools are configured!")
if self.lkv != self._linux_kernel_version:
msg.error("Linux kernel version in sources is different what was specified in arguments")
self.patch_all()
self.build()
self.create_zip()