How to build and flash a Linux kernel from Sony copyleft archives
Why rebuild the kernel?
There can be several reasons for why you want to rebuild and compile a custom kernel. You might, for instance, be a kernel developer, or you might have custom hardware that the standard kernel doesn’t support. Rebuilding a kernel enables you to make modifications to your device that are normally not supported by the manufacturer.
Note: If you’re not an experienced developer, Sony advises you not to rebuild the original kernel, as it is normally not needed. At Sony, we’re proud to deliver great software experiences through our rigorously tested and official software releases.
Prerequisites
This tutorial assumes that you are running Linux or Mac. On the Android Open Source Project website, you can find detailed instructions on how to set up and initialise a Linux build environment. You will probably also want to use a version control system like Git to handle your project. You can download Git from the Git webpage.
Rebuild the kernel
In order to rebuild the Linux kernel for your Sony Xperia device, you need to carefully follow the steps below. All code commands should be entered in the command prompt.
1. Unlock the boot loader
All the work on building and compiling a kernel is based on you having unlocked the boot loader of your device. Instructions on how to do this can be found in our new and improved unlocking boot loader pages. Please remember that this is only for advanced kernel developers. We recommend standard users not to unlock the boot loader, as it is not needed.
Note: There is no turning back after unlocking the boot loader. You may void the warranty of your device, and you will not be able to revert it back to a locked and original state again.
2. Download a cross compiler
A cross compiler is used to build ARM binaries on a different architecture, such as x86. This allows you to compile the kernel into a format that the device can run. We recommend the use of the GCC 4.6 cross compiler provided in the Android Open Source Project.Download the GCC 4.6 cross compiler using the following commands:
mkdir /android && cd /android
git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/arm/arm-eabi-4.6/
Note: Any ARM cross compiler capable of compiling Linux kernels can be used, such as CodeSourcery Lite, and Linaro.
3. Export the cross compiler
The cross compiler needs to be exported to specify which compiler to be used by the build system.Export the GCC 4.6 cross compiler using the following command:
For 32 bit devices:
export CROSS_COMPILE=/android/prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/bin/arm-eabi-
For 64 bit devices:
export CROSS_COMPILE=/android/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/bin/aarch64-linux-android-
Note: In case of using another cross compiler than GCC 4.8, replace /android/prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/bin/arm-eabi- with the correct search path of the cross compiler of your choice.
4. Download the kernel source from GitHub
The Sony kernel sources for flagship devices are available on the SonyXperiaDev GitHub. More sources for other devices will be added over time. Download by entering:
git clone https://github.com/sonyxperiadev/kernel-copyleft
cd kernel-copyleft
git branch -r
git checkout
Note: As an alternative, the Sony kernel source can also be downloaded from the Open source download area on Developer World.
5. Identify your device’s config file name
In order to configure the kernel, you need first to identify your device’s config file name. Please note that only devices featured in the Sony GitHub release and Sony GitHub unified kernels are shown here.
Device | Config file name |
---|---|
Device Xperia Z5 Premium | Config file name kitakami_suzuran_defconfig |
Device Xperia Z5 Compact | Config file name kitakami_satsuki_defconfig |
Device Xperia Z5 | Config file name kitakami_sumire_defconfig |
Device Xperia Z4 Tablet | Config file name kitakami_karin_defconfig, kitakami_karin_windy_defconfig |
Device Xperia Z3+ | Config file name kitakami_ivy_defconfig |
Device Xperia Z3 | Config file name shinano_leo_defconfig |
Device Xperia Z3 Compact | Config file name shinano_aries_defconfig |
Device Xperia Z3 Tablet Compact | Config file name shinano_scorpion_defconfig |
Device Xperia Z2 | Config file name shinano_sirius_defconfig |
Device Xperia Z2 Tablet | Config file name shinano_castor_defconfig |
Device Xperia Z Ultra | Config file name rhine_togari_row_defconfig |
Device Xperia Z1 | Config file name rhine_honami_row_defconfig |
Device Xperia Z1 Compact | Config file name rhine_amami_row_defconfig |
Device Xperia Z | Config file name fusion3_odin_defconfig |
Device Xperia ZR | Config file name fusion3_dogo_defconfig |
Device Xperia Tablet Z | Config file name fusion3_pollux_defconfig |
Device Xperia Tablet Z Wi-Fi | Config file name fusion3_pollux_windy_defconfig |
Device Xperia T | Config file name blue_mint_defconfig |
Device Xperia TX | Config file name blue_hayabusa_defconfig |
Device Xperia V | Config file name blue_tsubasa_defconfig |
Device Xperia E3 | Config file name yukon_flamingo_defconfig |
Device Xperia M2 | Config file name yukon_eagle_defconfig |
Device Xperia T2 Ultra | Config file name yukon_tianchi_defconfig |
Device Xperia T3 | Config file name yukon_seagull_defconfig |
6. Configure the kernel
Configure the kernel is a required step before building it. Kernel drivers can be turned on or off, or be built as modules. This has to be configured in order for the kernel to function as desired. Configure the kernel by entering:
make ARCH=<arch> CROSS_COMPILE=$CROSS_COMPILE <device_defconfig>
Note: Replace <arch> with arm or arm64 depending on your device architecture and with the device config file name identified in previous step.
7. Build the kernel and needed modules
When building the kernel a compressed kernel image file is created and placed in the default target arch/arm/boot/zImage on your computer. To start the build, enter:
make ARCH=<arch> CROSS_COMPILE=$CROSS_COMPILE -j <cpu_thread_number>
Note: Replace <arch> with arm or arm64 depending on your device architecture. The should correspond to your host architecture. In case of errors, replace with the numeral 1 for easy debugging.
For devices with Qualcomm wireless hardware, you need to build a Prima or Pronto WLAN module in order to get Wi-Fi working. Follow the instructions below to do this. Download wireless source by entering the following command:
git clone https://github.com/sonyxperiadev/prima
Then compile the module by entering the device specific command listed below.
Xperia Z1 Compact, Xperia Z1, Xperia Z Ultra
make ARCH=arm CROSS_COMPILE=$CROSS_COMPILE -j12 -C M=$PWD CONFIG_PRONTO_WLAN=m CONFIG_PRIMA_WLAN_LFR=y KERNEL_BUILD=1 WLAN_ROOT=$PWD
Xperia ZR, Xperia Z, Xperia Tablet Z, Xperia Tablet Z Wi-Fi, Xperia TX, Xperia T, Xperia V
make ARCH=arm CROSS_COMPILE=$CROSS_COMPILE -j12 -C M=$PWD CONFIG_PRIMA_WLAN=m CONFIG_PRIMA_WLAN_LFR=y KERNEL_BUILD=1 WLAN_ROOT=$PWD
Xperia E3, Xperia M2, Experia T2 Ultra, Xperia T3
make ARCH=arm CROSS_COMPILE=$CROSS_COMPILE -j12 -C M=$PWD CONFIG_PRONTO_WLAN=m CONFIG_PRIMA_WLAN_LFR=y KERNEL_BUILD=1 WLAN_ROOT=$PWD
Once the compilation is finished, the wlan.ko module can be found in the prima folder.
8. Getting a RAM image file
The RAM disk is the initial file system that the kernel will mount before transferring control to user space. Use the following instructions to unpack and pack the file.If you have a ramdisk.img, and want to create a directory of files out of it, unpack the file by entering:
gzip -d - cpio -idm
If you have a directory of files, and want to create a ramdisk.img from it, pack the file by entering:
find . | cpio --quiet -H newc -o | gzip > ramdisk.img
Note: The build will take some minutes to complete.Next
9. Assemble the boot image file
The kernel image (from Step 7) and the RAM image (from Step 8) need to be assembled in a boot image file. The boot image file will then be flashed to your phone or tablet. Depending on your device, you need to use the tool mkqcdtbootimg, provided by SonyXperiaDev on GitHub, or mkbootimg, provided by the Android Open Source Project, to create the boot image. Please note that the program must be compiled differently depending on which system you are running.To start assemble the boot image file, enter:
# path to kernel sources
export KERNEL_SRC_PATH=~/kernel-copyleft/
Then enter your device specific commands below.
Xperia Z3, Xperia Z3 Compact, Xperia Z3 Tablet Compact, Xperia Z2 Tablet, Xperia Z2
Note: Copy ramdisk.cpio.gz to the same folder before packing the boot.img.
./mkqcdtbootimg --kernel $(KERNEL_SRC_PATH)/arch/arm/boot/zImage --ramdisk ramdisk.cpio.gz --base 0x00000000 --ramdisk_offset 0x02000000 --tags_offset 0x01E00000 --pagesize 2048 --cmdline "androidboot.hardware=qcom user_debug=31 msm_rtb.filter=0x37 ehci-hcd.park=3 dwc3.maximum_speed=high dwc3_msm.prop_chg_detect=Y" --dt_dir $(KERNEL_SRC_PATH)/arch/arm/boot/ --dt_version 2 -o boot.img
Xperia Z1 Compact, Xperia Z1, Xperia Z Ultra
Note: Copy ramdisk.cpio.gz to the same folder before packing the boot.img.
./mkqcdtbootimg --kernel $(KERNEL_SRC_PATH)/arch/arm/boot/zImage --ramdisk ramdisk.cpio.gz --base 0x00000000 --ramdisk_offset 0x02000000 --tags_offset 0x01E00000 --pagesize 2048 --cmdline "androidboot.hardware=qcom user_debug=31 maxcpus=2 msm_rtb.filter=0x3F ehci-hcd.park=3 msm_rtb.enable=0 lpj=192598 dwc3.maximum_speed=high dwc3_msm.prop_chg_detect=Y" --dt_dir $(KERNEL_SRC_PATH)/arch/arm/boot/ -o boot.img
Xperia ZR, Xperia Z, Xperia Tablet Z, Xperia Tablet Z Wi-Fi
Note: Copy ramdisk.cpio.gz zImage to the same folder before packing the boot.img.
./mkbootimg --base 0x80200000 --kernel zImage --ramdisk_offset 0x02000000 --cmdline "androidboot.hardware=qcom user_debug=31 msm_rtb.filter=0x3F ehci-hcd.park=3 vmalloc=400M androidboot.emmc=true" --ramdisk ramdisk.cpio.gz -o boot.img
Xperia TX, Xperia T, Xperia V
Note: Copy ramdisk.cpio.gz zImage to the same folder before packing the boot.img.
python mkelf.py -o kernel.elf zImage@0x80208000 ramdisk.cpio.gz@0x81400000,ramdisk RPM_JB.bin@0x00020000,rpm bootcmd_JB@0x00000000,cmdline
Xperia E3, Xperia M2, Experia T2 Ultra, Xperia T3
Note: Copy ramdisk.cpio.gz to the same folder before packing the boot.img.
./mkqcdtbootimg --kernel $(KERNEL_SRC_PATH)/arch/arm/boot/zImage --ramdisk ramdisk.cpio.gz --base 0x00000000 --ramdisk_offset 0x02000000 --tags_offset 0x01E00000 --pagesize 2048 --cmdline "androidboot.hardware=qcom user_debug=31 msm_rtb.filter=0x37 selinux=0" --dt_dir $(KERNEL_SRC_PATH)/arch/arm/boot/ --dt_version 2 -o boot.img
10. Download the Fastboot protocol
Fastboot is a diagnostic protocol included in the Android SDK. Make sure you have the Android SDK installed before proceeding. Fastboot will be used to modify the flash file system on your device via a USB connection from the computer. If you unlocked your boot loader in Step 1, you will already have Fastbootinstalled.
11. Flash the boot image file
Use the Fastboot protocol to flash the boot image file to your phone or tablet. Enter the following command:
fastboot flash boot out/target/product/<device>/boot.img
When prompted to connect your device, first make sure that your device is turned off. Then, hold down the Volume Up button and connect the device to your computer with a USB cable. Your device’s notification light should shine blue to confirm it’s in Fastboot mode.
When the flashing is completed, reboot your device by entering:
fastboot reboot
You have now gone through the process of how to download a Sony kernel source, and then how to build and flash the kernel to your device. However, we have not gone into detail on how to modify the kernel itself. For more details, we recommend you to deep dive into forums and websites such as XDA Developers and XDA University, where there are many skilled people sharing their knowledge.