These are the steps I took to compile a kernel for the Cosmo, if you'd like one, follow along.
I'll assume you have access to a linux machine, I'm using debian.
Install the following packages:
sudo apt install build-essential libncurses-dev bison flex libssl-dev libelf-dev bc git gcc-aarch64-linux-gnu python
Create some directories for use later :
mkdir cosmo_android
cd cosmo_android
mkdir KERNEL_OUT
Now get mkbootimg and edit the source :
git clone https://github.com/osm0sis/mkbootimg
cd mkbooting
nano mkbootimg.c
Find the following lines and change them to these offsets :
uint32_t kernel_offset = 0xEC080100U;
uint32_t ramdisk_offset = 0x01000100U;
uint32_t second_offset = 0xECF00100U;
save and run
make
cd out to cosmo_android root and create a directory called Packing.
cd ..
mkdir Packing && cd Packing
Get the PC v23 Custom installer and place the root-boot.img in this directory and unpack the image :
../mkbootimg/unpackbootimg -i root-boot.img -o .
We're specifically after the RAM disk root-boot.img-ramdisk.gz to pack a boot.img later on.
cd out and get the cosmo android kernel
cd ..
git clone https://github.com/deadman96385/android_kernel_planet_mt6771.git
cd into it and import the latest config
cd android_kernel_planet_mt6771
make O=../KERNEL_OUT ARCH=arm64 k71v1_64_bsp_defconfig
Edit the Makefile to get rid of some errors. If anyone knows how to get this compiling without editing this Makefile, let me know.
nano Makefile
Find this line and add -w to it :
KBUILD_CFLAGS := -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs \
so it looks like
KBUILD_CFLAGS := -w -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs \
Next we need to do some more editing to get the Bluetooth, Wifi, etc drivers to load.
To get these to load you need to edit module.c located in android_kernel_planet_mt6771/kernel/module.c
nano kernel/module.c
Go to line ~3477 (CTRL+_), that should drop you in function load_module(...), it's there you need to add these two lines :
flags |= MODULE_INIT_IGNORE_MODVERSIONS;
flags |= MODULE_INIT_IGNORE_VERMAGIC;
When you're done it should look like this :
static int load_module(struct load_info *info, const char __user *uargs,
int flags)
{
struct module *mod;
long err;
char *after_dashes;
flags |= MODULE_INIT_IGNORE_MODVERSIONS;
flags |= MODULE_INIT_IGNORE_VERMAGIC;
err = module_sig_check(info, flags);
if (err)
goto free_copy;
.... etc ....
We're not done yet, we need to configure the kernel so it force loads modules, you could do it via menuconfig but you can also edit the .config file in the KERNEL_OUT directory.
nano ../KERNEL_OUT/.config
Search (CTRL+W) for
# CONFIG_MODULE_FORCE_LOAD is not set
and change it so it reads:
CONFIG_MODULE_FORCE_LOAD=y
Save it and now build the kernel :
make O=../KERNEL_OUT ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-
When that is finished we're going to take the resulting Image and pack it with the modified mkbootimg*.
Go to the packing directory and copy the result of the build into the directory :
cd ../Packing
cp ../KERNEL_OUT/arch/arm64/boot/Image.gz-dtb .
Now repack it, make sure to use the modified mkbootimg and the ramdisk that we unpacked earlier :
../mkbootimg/mkbootimg --kernel Image.gz-dtb --ramdisk root-boot.img-ramdisk.gz --base 0x53ffff00 --cmdline 'bootopt=64S3,32N2,64N2 buildvariant=user veritykeyid=id:7e4333f9bba00adfe0ede979e28ed1920492b40f' -o drbytes.img
You now have a drbytes.img which you can use to flash the kernel to one of the boot slots on the cosmo.
Here's how I flash it, I have the "cosmo-customos-installer" directory on my SD Card, so I added this line to the list_installers.sh :
echo "DrBytes Kernel,$MBF/Cosmo_Installer_DrBytes_Kernel.sh" >> $INSTALLER_FILE
I also have added another file called Cosmo_Installer_DrBytes_Kernel.sh, it's a copy paste of the Cosmo_Installer_Rooted_Android.sh by PC but adapted to take the drbytes.img :
#!/system/bin/sh
PARTED="/sbin/parted_static"
DD="/system/bin/dd"
MBF="/sdcard/cosmo-customos-installer"
BOOT_PARTITION="/dev/block/mmcblk0p"
LINUX_ROOTFS="/dev/block/mmcblk0p43"
OUTPUT="/tmp/output.txt"
ERROR="/tmp/error.txt"
echo "Installing DrBytes Kernel..." > $OUTPUT
log () {
echo -n "$1 " >> $OUTPUT
}
execute() {
log "Running \"$1\""
R=$($1 2> $ERROR)
if [ "$?" -eq "0" ]
then
log "OK\n"
else
log "ERROR: `cat /tmp/error`\n"
fi
}
# Installing boot image into user-selected boot partition
execute "$DD if=$MBF/drbytes.img of=$BOOT_PARTITION$1 bs=1m"
# Rename partition to specific OS
execute "$PARTED /dev/block/mmcblk0 name $1 DrBytes_Kernel"
Lastly, don't forget to copy the drbytes.img you created to the sdcard.
Reboot to the android recovery, the same procedure as if you were to install Linux or Rooted Android but select the new entry and flash it to the Rooted Android partition.
Reboot and test it out.
To undo it, reboot to android recovery as before and flash PC's Rooted android to the Rooted Android Partition.
When you followed all the steps you should be left with your own kernel booting and everything should work.
Note : You can patch drbytes.img with Magisk so that you have root available.
This is how I got an android kernel booting, I'm sure there are better ways and if you know how, please share.
*: Regarding the modified mkbootimg, we've changed the offsets of the kernel and the ramdisk. These values aren't just a guess, I've gotten these from a tool called unmkbootimg, hosted on XDA. This tool does the same thing as the mkbootimg project but it is able to summarize the results of the unpacking which can be used when we create a new boot image.
Took some asking around, the people over at GeminiPDA on telegram were very helpful; TheKit, Varti, Sean, etc.
Big thx to @Deadman96385 for pointing me to a commit that had the relevant lines of code to skip the module checking.
(
https://github.com/LineageOS/android_kernel_asus_sm8150/commit/66976d9f1bbbaf58b81c8ddb1847ef9ca622de3e )