What is this?
This is my mostly-working Razer Phone 2 Kernel and TWRP device tree!
I've been reading threads where people are having issues compiling the Razer-provided source for the RP2 and figured I should post what I have so others can use it to help achieve a fully working source-built kernel.
I'm also including a set of scripts to repack a Magisk-patched boot.img so you can iterate on building a new kernel rapidly, and am including a prebuilt magisk-patched boot.img with the custom kernel so we can QA other features while focusing on the known-broken things.
Also also, I'm including an exfat kernel module (not built into kernel for now) so that can be tested too!
Why is this mostly-working?
I've tested many features on this kernel and so far the only feature *not* working is the microphone. (Audio recording, haven't tested in-call audio)
This kernel is a mashup of Razer's source and a few other OSS repos from CAF - specifically the TFA98XX driver, and the audio-kernel repositories, so there's a bit of integration left to do to get all the audio features working.
I also had to edit the defconfig to get the DTB to build, and have to build with some special flags to use the prebuilt DTB compiler (dtc) since the one included in the source isn't capable of compiling the FIH DTBs inside the Razer-provided source (correct me if I'm doing something wrong here, but this was how I ended up getting it to work).
The kernel parameter skip_initramfs has also been renamed to skip__initramfs to break the silly bootloader skipping the ramdisk (and thereby breaking Magisk). This means you don't need to run Magisk's binary patch to rename that kernel argument to make Magisk or TWRP work.
Keep in mind, though, that this means if you use this kernel with a 100% stock ramdisk, you'll only ever get recovery since new Android ramdisks only contain recovery! (Thanks Dees_Troy for the refresher course!)
If you're familiar with the new audio-kernel CAF stuff, please let me know! I could use your assistance getting the microphone working.
It seems like the microphone isn't working because of some mismatches between the in-kernel msm audio drivers and the Razer customized drivers in the vendor partition.
I get errors likein dmesg when attempting to record audio. This seems isolated to the q6afe drivers in the kernel (specifically here: https://github.com/thecubed/android_kernel_razer_sdm845/blob/master/techpack/audio/dsp/q6afe.c#L1593 ) not playing nice with the prebuilt snd_soc_sdm845 kernel module.Code:
[31269.485217] send_afe_cal_type cal_block not found!!
Where do I get this?
See here for the TWRP device tree (and detailed instructions on how to set up your build tree): https://github.com/thecubed/android_device_razer_cheryl2
See here for just the kernel source: https://github.com/thecubed/android_kernel_razer_sdm845
See here for a kernel-less boot.img skeleton that you can drop your newly minted kernel into and repack with mkbootimg: http://downloads.codefi.re/thecubed/aura2/kernel/magisk_custom_kernel_skel.tar.gz
See here for a prebuilt boot.img with this custom kernel which includes Magisk: http://downloads.codefi.re/thecubed/aura2/kernel/boot_magisk_custom_9.img
See here for an exfat module you can insmod from a booted phone with custom kernel to try mounting exfat SD cards: http://downloads.codefi.re/thecubed/aura2/kernel/exfat_module.tar.gz
See here for a stock Magisk-patched boot.img for when you're done playing with this: http://downloads.codefi.re/thecubed/aura2/kernel/boot_magisk.img
How do I use this?
First off, this isn't for the average user. This kernel is *under development* and is not fully working yet. This means don't try this kernel on your mom's phone, your neighbor's phone, or any device that you aren't comfortable developing for.
Secondly, to test this, you'll need to be oem unlocked. There's plenty of guides to do this, but be aware that it erases all data on your phone when doing so. You could try using adb backup first, but I didn't have much success with that.
I'd suggest looking at the first link in the Where do I get this section and reading the README for the device tree as it'll have the most detailed instructions.
The gist is you'll need to download a minimal OmniROM manifest, add some lines to a file in .repo/local_manifests/cheryl2.xml, and run mka kernel to get a kernel image.
After that, you'll want to use mkbootimg to build a bootable magisk-patched boot.img, which you'll flash to your device with fastboot flash boot_X path/to/boot.img (where X is the active boot partition).
Once you've got the Image.gz-dtb file from your $OUT/obj/KERNEL_OBJ/arch/arm64/boot/Image.gz-dtb you'll want to repack it into a magisk-patched boot.img
Repacking the boot.img can be done with this command:
mkbootimg \ --kernel Image.gz-dtb \ --ramdisk boot_magisk.img-ramdisk.gz \ --cmdline 'console=ttyMSM0,115200n8 earlycon=msm_geni_serial,0xA84000 androidboot.hardware=qcom androidboot.console=ttyMSM0 video=vfb:640x400,bpp=32,memsize=3072000 msm_rtb.filter=0x237 ehci-hcd.park=3 lpm_levels.sleep_disabled=1 service_locator.enable=1 swiotlb=2048 androidboot.configfs=true androidboot.usbcontroller=a600000.dwc3 buildvariant=user' \ --base 0x00000000 \ --pagesize 4096 \ --kernel_offset 0x00008000 \ --ramdisk_offset 0x01000000 \ --second_offset 0x00f00000 \ --tags_offset 0x00000100 \ --os_version 8.1.0 \ --os_patch_level 2018-10 \ -o ../boot_magisk_custom.img
Does this have TWRP?
No. This does not have TWRP. The device tree is for TWRP eventually, and to make compiling the kernel easier (since Android kernels don't like compiling outside of Android's makefile system anymore)
Does this have Magisk?
Yes! It's "pre-patched" too, as long as you're using my prebuilt one above.
Do I need the custom kernel to use the exfat module?
Yes. This is because the Razer stock kernel has module signing forced. I don't have Razer's kernel module signing key so I can't compile kernel modules for the stock kernel. My kernel has module signing set to allow mismatched signatures, so both the Razer modules in /vendor/lib/modules (required for sound) and my exfat module can coexist.
Why is exfat not compiled into the kernel?
Laziness, primarily. Actually, in this case I just didn't want to add extra variables to testing the kernel. If adding exfat into the kernel directly causes other issues it could potentially derail my quest to get this kernel up to par with the stock kernel.
Why did other kernels not boot?
Most likely due to incompatible audio-kernel revisions, or dtb issues. I'm not sure, but getting this kernel to compile required a bit more hacking than it should have.
Are you working on TWRP for Razer Phone 2?
Yep. Look for another post once the kernel is good to go. I aim to get encryption support working too, unless Dees_Troy beats me to it
Is audio still not working?
I'll update the post to remove the WIP tag and the section about audio as soon as it's working.
Is anything else not working?
Feel free to download and test to let me know what else isn't working and I'll add it to the OP.
y u quote OP?Acknowledgements
Special thanks to Rashed97 and Dees_Troy for all their help.