这里内核编译主要是为了编译KernelSU内核
内核编译文档:https://source.android.com/docs/setup/build/building-kernels?hl=zh-cn
内核发布文档:https://source.android.com/docs/core/architecture/kernel/gki-android13-5_10-release-builds?hl=zh-cn#march-2023-releases
下载
查询内核版本
1
2
3
4// 解压镜像
unpackbootimg -i boot-5.10.img -o boot-5.10
// 查看内核版本
strings boot-5.10.img-kernel | grep 'Linux version'去内核发布文档中找到对应的工件,然后下载manifest.xml
repo初始化
1
2repo init -u git://mirrors.ustc.edu.cn/aosp/kernel/manifest -b common-android13-5.10-2023-03
创建manifest.xml
1
vim .repo/manifests/manifest.xml
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<manifest>
<remote name="aosp" fetch="git://mirrors.ustc.edu.cn/aosp/" review="git://mirrors.ustc.edu.cn/aosp/"/>
<default upstream="master-kernel-build-2022" dest-branch="master-kernel-build-2022" remote="aosp" sync-j="4"/>
<superproject name="kernel/superproject" remote="aosp" revision="common-android13-5.10-2023-03"/>
<project path="build/kernel" name="kernel/build" revision="92e8d0541d372646caca7d26fc4a1d7804db7420">
<linkfile dest="tools/bazel" src="kleaf/bazel.sh"/>
<linkfile dest="WORKSPACE" src="kleaf/bazel.WORKSPACE"/>
<linkfile dest="build/build.sh" src="build.sh"/>
<linkfile dest="build/build_abi.sh" src="build_abi.sh"/>
<linkfile dest="build/build_test.sh" src="build_test.sh"/>
<linkfile dest="build/build_utils.sh" src="build_utils.sh"/>
<linkfile dest="build/config.sh" src="config.sh"/>
<linkfile dest="build/envsetup.sh" src="envsetup.sh"/>
<linkfile dest="build/_setup_env.sh" src="_setup_env.sh"/>
<linkfile dest="build/multi-switcher.sh" src="multi-switcher.sh"/>
<linkfile dest="build/abi" src="abi"/>
<linkfile dest="build/static_analysis" src="static_analysis"/>
</project>
<project path="hikey-modules" name="kernel/hikey-modules" revision="a068c3379fab21a8719c03e661c7d9de97583244" upstream="android13-5.10" dest-branch="android13-5.10"/>
<project path="common" name="kernel/common" revision="2340d931ca78432e962713ec384bf8176e17ba68" upstream="deprecated/android13-5.10-2023-03" dest-branch="deprecated/android13-5.10-2023-03">
<linkfile dest=".source_date_epoch_dir" src="."/>
</project>
<project path="kernel/tests" name="kernel/tests" revision="42a77670ce44d6e19a6fbb8b93fa0b06f009a3a4"/>
<project path="kernel/configs" name="kernel/configs" revision="b3cc2bc03dab303c54a9ce1f709f8ee315eb311d"/>
<project path="common-modules/virtual-device" name="kernel/common-modules/virtual-device" revision="d8e1867f2148aff332ebeeea93946277f3ebf83d" upstream="android13-5.10" dest-branch="android13-5.10"/>
<project path="prebuilts/clang/host/linux-x86" name="platform/prebuilts/clang/host/linux-x86" clone-depth="1" revision="4bb6424cb1615af408ae80871131f4da8c4ddadf"/>
<project path="prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.17-4.8" name="platform/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.17-4.8" clone-depth="1" revision="007101a451907c5369db5002ddf7b14dcefb7864"/>
<project path="prebuilts/build-tools" name="platform/prebuilts/build-tools" clone-depth="1" revision="f6813860e16b2fa1461a15e7b8a3127dfdee021a"/>
<project path="prebuilts/kernel-build-tools" name="kernel/prebuilts/build-tools" clone-depth="1" revision="083e34d8c2c4239be4967172427268c267585951"/>
<project path="tools/mkbootimg" name="platform/system/tools/mkbootimg" revision="2208a03d874255af1e4eaf6cf7c156fe1dc98943"/>
<project path="prebuilts/bazel/linux-x86_64" name="platform/prebuilts/bazel/linux-x86_64" clone-depth="1" revision="6162318e6458db65f94eed6671e302c3e894b1ac"/>
<project path="prebuilts/jdk/jdk11" name="platform/prebuilts/jdk/jdk11" clone-depth="1" revision="aee26c8cd89b9abb428f676caba168699cd8662f"/>
<project path="prebuilts/ndk-r23" name="toolchain/prebuilts/ndk/r23" clone-depth="1" revision="93532f3052c14fbb337ff57d5732128dc7481ee6"/>
<project path="external/bazel-skylib" name="platform/external/bazel-skylib" revision="f1fb8167b4ed64feb494fd1ea6a8a619bbb549de"/>
<project path="build/bazel_common_rules" name="platform/build/bazel_common_rules" revision="ddd2d82d10e21fb4137d2db3c1b848d6f1832acc"/>
<project path="external/stardoc" name="platform/external/stardoc" revision="b6ef2c6b6e39087f7396aaeb13c83464dfce4a19"/>
<project path="external/python/absl-py" name="platform/external/python/absl-py" revision="63f98de5b158481877489ca39158ed691f7551e1"/>
</manifest>初始化 manifest.xml,这里将会下载manifest.xml中配置代码。
1
repo init -m manifest.xml
下载
1
repo sync
kernelsu配置
1
curl -LSs "https://raw.githubusercontent.com/tiann/KernelSU/main/kernel/setup.sh" | bash -
编译
编译
1
tools/bazel build --lto=thin --config=fast //common:kernel_aarch64_dist
编译成功
产物目录
1 | ➜ kernel_aarch64_gki_artifacts pwd |
安装
- 前置
生成的boot.img的压缩格式有3种:
- 无压缩
- lz4
- gz
而pixel需要的格式为lz4_legacy格式,所以这里需要找到原厂的boot.img,解压替换kernel,然后重新打包即可。
将虚拟机中的boot.img复制到Mac中
1
scp -r juneleo@127.0.0.1:/home/juneleo/android-kernel/bazel-out/k8-fastbuild/bin/common/kernel_aarch64_gki_artifacts/boot.img /Users/juneleo/Documents/tmp/
解压boot.img获取kernel
1
2
3
4
5
6
7
8
9
10
11➜ mkdir boot
➜ ls
➜ /Users/juneleo/.tool/mkbootimg/unpackbootimg -i boot.img -o boot
BOARD_KERNEL_CMDLINE
BOARD_PAGE_SIZE 4096
BOARD_HEADER_VERSION 4
BOARD_HEADER_SIZE 1584
➜ cd boot
➜ ls
boot.img-cmdline boot.img-kernel boot.img-os_version
boot.img-header_version boot.img-os_patch_level boot.img-ramdisk查看内核版本
1
2
3
4
5➜ strings boot.img-kernel | grep 'Linux version'
Linux version %s (%s)
Linux version 5.10.157-maybe-dirty (build-user@build-host)
(Android (8508608, based on r450784e) clang version 14.0.7
(https://android.googlesource.com/toolchain/llvm-project 4c603efb0cca074e9238af8b4106c30add4418f6), LLD 14.0.7) #1 SMP PREEMPT Thu Jan 1 00:00:00 UTC 1970解压原厂boot.img,将kernel替换为编译过的boot.img中的kernel,重新打包。
1
2
3➜ magiskboot unpack magisk_patched-26100_HuyuC.img
➜ mv -f /Users/juneleo/Documents/tmp/boot/boot.img-kernel kernel
➜ magiskboot repack magisk_patched-26100_HuyuC.img查看当前手机内核版本
1
2➜ adb reboot bootloader
➜ fastboot boot new-boot.img // 这里我们不直接刷入,先临时刷入看看有没有问题查看可以正常开机,我们直接刷入
1
➜ fastboot flash boot new-boot.img
Manager安装
构建和安装
1
2➜ ./gradlew :app:assembleDebug -S
➜ adb install -r app/build/outputs/apk/debug/KernelSU_v0.8.1_11563-debug.apk无法root错误日志
1
2
3
4Compat change id reported: 171228096; UID 10414; state: ENABLED
2024-03-03 12:06:54.291 11299-11299 BUILDER me.weishu.kernelsu D exec /data/app/~~GdB38SnvVmCvl6IOGrxLIg==/me.weishu.kernelsu-UFgyNmWpSrTOuFBYntmbyw==/lib/arm64/libksud.so debug su
2024-03-03 12:06:54.295 11299-11299 LIBSU me.weishu.kernelsu D java.io.IOException: Cannot run program "/data/app/~~GdB38SnvVmCvl6IOGrxLIg==/me.weishu.kernelsu-UFgyNmWpSrTOuFBYntmbyw==/lib/arm64/libksud.so": error=2, No such file or directory找不到 /lib/arm64/libksud.so (官方无文档,这里需要自己排查)
查看目录下有一个justfile文件1
2
3➜ brew install just
➜ just build_ksud
➜ just build_ksud问题记录
- 下载慢的问题
替换为中科大源即可,替换中科大源后,可能repo会找不到 kernel/manifest 的分支,指定一个存在的分支即可。
1 | repo init -u git://mirrors.ustc.edu.cn/aosp/kernel/manifest -b common-android13-5.10-2023-03 |
- 分支不存在问题
manifest.xml
1 | <project path="common" name="kernel/common" revision="2340d931ca78432e962713ec384bf8176e17ba68" upstream="android13-5.10-2023-03" dest-branch="android13-5.10-2023-03"> |
查看kernel/common 所有分支发现
1 | remotes/aosp/deprecated/android13-5.10-2023-03 |
所以需要将manifest.xml 中的分支改为deprecated/android13-5.10-2023-03