Author Topic: How I crosscompiled the Android Kernel and flashed it to the Cosmo  (Read 254 times)

drbytes

  • Newbie
  • *
  • Posts: 17
    • View Profile
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:

Code: [Select]
sudo apt install build-essential libncurses-dev bison flex libssl-dev libelf-dev bc git gcc-aarch64-linux-gnu pythonCreate some directories for use later :
Code: [Select]
mkdir cosmo_android
cd cosmo_android
mkdir KERNEL_OUT


Now get mkbootimg and edit the source :
Code: [Select]
git clone https://github.com/osm0sis/mkbootimg
cd mkbooting
nano mkbootimg.c
Find the following lines and change them to these offsets :
Code: [Select]
uint32_t kernel_offset  = 0xEC080100U;
uint32_t ramdisk_offset = 0x01000100U;
uint32_t second_offset  = 0xECF00100U;
save and run
Code: [Select]
make
cd out to cosmo_android root and create a directory called Packing.
Code: [Select]
cd ..
mkdir Packing && cd Packing
Get the PC v23 Custom installer and place the root-boot.img in this directory and unpack the image :
Code: [Select]
../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
Code: [Select]
cd ..
git clone https://github.com/deadman96385/android_kernel_planet_mt6771.git
cd into it and import the latest config
Code: [Select]
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.
Code: [Select]
nano MakefileFind this line and add -w to it :
Code: [Select]
KBUILD_CFLAGS   := -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs \so it looks like
Code: [Select]
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
Code: [Select]
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 :

Code: [Select]
        flags |= MODULE_INIT_IGNORE_MODVERSIONS;
        flags |= MODULE_INIT_IGNORE_VERMAGIC;

When you're done it should look like this :
Code: [Select]
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.
Code: [Select]
nano ../KERNEL_OUT/.configSearch (CTRL+W) for    
Code: [Select]
# CONFIG_MODULE_FORCE_LOAD is not setand change it so it reads:
Code: [Select]
CONFIG_MODULE_FORCE_LOAD=y
Save it and now build the kernel :

Code: [Select]
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 :
Code: [Select]
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 :

Code: [Select]
../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 :
Code: [Select]
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 :

Code: [Select]
#!/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 )
« Last Edit: August 10, 2020, 03:31:52 pm by drbytes »

Varti

  • Administrator
  • Hero Member
  • *****
  • Posts: 1073
    • View Profile
    • http://
Re: How I crosscompiled the Android Kernel and flashed it to the Cosmo
« Reply #1 on: August 07, 2020, 04:57:06 am »
Great job, it surely took a lot of time to figure all this out, but it was worthwhile. I'm pinning this thread for future reference. I'm copy-pasting the URL below too from the other compilation thread, for completeness.

A guide on how to setup the compiler and to compile missing modules:

https://www.mygnu.de/2020/02/exfat-support-for-the-cosmo-communicator/

If anyone has any additional information, feel free to share it here.

Varti
« Last Edit: August 07, 2020, 05:01:01 am by Varti »
Planet Gemini PDA WiFi/LTE with Mediatek x27
SL-C1000 running Arch Linux ARM May2017, K30225 Wi-Fi CF Card, 64GB SDXC card
and many other Zauruses!
#gemini-pda, #zaurus and #alarmz on Freenode: https://webchat.freenode.net/
#planetgemini on matrix: https://riot.im/app/#/room/#planetgemini:matrix.org
Gemini PDA on Telegram

dst6se

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: How I crosscompiled the Android Kernel and flashed it to the Cosmo
« Reply #2 on: August 13, 2020, 07:07:58 am »
And here is a dirty script that do all drbytes steps !

ex

./start.sh

1) Compile_new
2) Compile_update
3) Clean_all
#? 1
Magisk root (y/n)?

it will output a kernel image with the name kernel.img !

Compile_new - does every step including menuconfig
Compile_update - menuconfig again , and recompile
Clean_all - removes every files created by the script

Magisk root - root the image with magisk .

« Last Edit: August 13, 2020, 07:11:46 am by dst6se »

abliss

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: How I crosscompiled the Android Kernel and flashed it to the Cosmo
« Reply #3 on: September 10, 2020, 04:08:04 pm »
Thanks for your hard work putting together this thorough guide.

Where do I "Get the PC v23 Custom installer" ?

And where do I find "PC's rooted android"?

Edit: I was able to successfully flash my own kernel following your inspiration. I downloaded the v23 android update from Planet Computers here:
http://support.planetcom.co.uk/index.php/Cosmo_Android_Firmware_Manual_Installation

After unzipping, I had a file called
Code: [Select]
boot-verified.img which I used with your instructions.

For flashing, I found it much simpler to use the SP Flash Tool, which I downloaded from https://spflashtools.com/linux/sp-flash-tool-v5-2032-for-linux , and the scatter file which I downloaded from https://www.oesf.org/forum/index.php?topic=36336.msg296553#msg296553 .

After that, I followed the Magisk instructions for patching the image file. I never could find an official Planet Computers rooted android image.

Thanks again for your help!
« Last Edit: September 10, 2020, 08:14:42 pm by abliss »

drbytes

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: How I crosscompiled the Android Kernel and flashed it to the Cosmo
« Reply #4 on: September 11, 2020, 04:32:05 am »
See http://support.planetcom.co.uk/index.php/Linux_for_Cosmo#Linux_installation, there is a link there to download the custom installer (v2 at time of writing)

It's all in there.