How to automatically build the Linux kernel

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.

Already some years ago, we published a basic guide on How to build a Linux kernel and flash it to the phone. With the Sony kernel sources now on GitHub we feel there is a need for an updated and improved tutorial, and with a special focus on the latest Sony Xperia models.

Please note that 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. Set up the source code and environment
    Select one of the Android build guides (see the AOSP build instructions page) and do the following steps of the guide:

    1. Prepare your Java environment
    2. Install the necessary tools to make an Android build
    3. Download Repo tool and set PATH
    4. Initialise the AOSP tree
    5. Add the necessary patches from the AOSP upstream branch

    Note: If you already have the environment set then steps 1-3 are optional. This automatically rebuild only works with Android 6.0.1 and newer build guides.

  2. Prepare the repository for kernel rebuild
    To rebuild the kernel you have to remove the pre-build kernel folder by typing the command:

    rm –r device/sony/common-kernel

    Note: The open device tree will automatically build the kernel from source if a pre-built kernel is not found.

  3. Rebuild the boot image
    To rebuild the boot image with a kernel built from source, use the following commands:

    source build/envsetup.sh && lunch

    When prompted, pick the number corresponding to your device in the list displayed and press enter.
    To start the build, type:

    make -j <insert the cpu thread number of your computer> bootimage

    Note: The build will take some minutes to complete.

  4. 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
How to manually build the Linux kernel for 64 bit devices

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.

Already some years ago, we published a basic guide on How to build a Linux kernel and flash it to the phone. With the Sony kernel sources now on GitHub we feel there is a need for an updated and improved tutorial, and with a special focus on the latest Sony Xperia models.

Please note that 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.8 cross compiler provided in the Android Open Source Project.Download the GCC 4.8 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.8/

    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.8 cross compiler using the following command:

    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
    cd kernel
    git branch -r
    git checkout 
  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.

    Xperia X

    aosp_loire_suzu_defconfig

    Xperia X Compact

    aosp_loire_kugo_defconfig

    Xperia X Performance

    aosp_tone_dora_defconfig

    Z5_200x130

    Xperia XZ

    aosp_tone_kagura_defconfig

    Z5_premium_200x130

    Xperia Z5 Premium

    aosp_kitakami_suzuran_defconfig

    Z5compact_200x130

    Xperia Z5 Compact

    aosp_kitakami_satsuki_defconfig

    Z5_200x130

    Xperia Z5

    aosp_kitakami_sumire_defconfig

    Z4tablet_200x130

    Xperia Z4 Tablet

    aosp_kitakami_karin_defconfig,
    aosp_kitakami_karin_windy_defconfig

    Z3plus_200x130

    Xperia Z3+

    aosp_kitakami_ivy_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=arm64 CROSS_COMPILE=$CROSS_COMPILE <device_defconfig> 

    Note: Replace <device_defconfig> with the device config file name identified in Step 5.

  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=arm64 CROSS_COMPILE=$CROSS_COMPILE -j <cpu_thread_number>

    Note: The <cpu_thread_number> should correspond to your host architecture. In case of errors, replace <cpu_thread_number> with the numeral 1 for easy debugging.

  8. Copy needed binaries to your Android repository
    For building a new boot image with the compiled kernel, the kernel binary “arch/arm64/boot/Image.gz-dtb” has to be renamed “kernel-<device>” and placed in the device/sony/common-kernel folder.Note: Please note that this is a kernel binary with an appended dtb and there is no need to copy the .dtb separately. Wireless drivers are built into the kernel and there is no kernel module for them.
  9. For environment setup instructions, see the AOSP build instructions. To generate the images enter the following commands:
    source build/envsetup.sh && lunch

    When prompted, pick the number corresponding to your device in the list displayed and press enter.
    To start the build, type:

    make -j <insert the cpu thread number of your computer> bootimage

    Note: The build will take some minutes to complete.

  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 Fastboot installed.
  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.

How to manually build the Linux kernel for 32 bit devices

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.

Already some years ago, we published a basic guide on How to build a Linux kernel and flash it to the phone. With the Sony kernel sources now on GitHub we feel there is a need for an updated and improved tutorial, and with a special focus on the latest Sony Xperia models.

Please note that 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.8 cross compiler provided in the Android Open Source Project.Download the GCC 4.8 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.8/

    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.8 cross compiler using the following command:

    export CROSS_COMPILE=/android/prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/bin/arm-eabi-

    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
    cd kernel
    git branch -r
    git checkout 
  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.

    Z3_200x130

    Xperia Z3

    aosp_shinano_leo_defconfig

    Z3Compact_200x130

    Xperia Z3 Compact

    aosp_shinano_aries_defconfig

    Z3TabletCompact_180x117

    Xperia Z3 Tablet Compact

    aosp_shinano_scorpion_defconfig

    Xperia Z2

    Xperia Z2

    aosp_shinano_sirius_defconfig

    Xperia Z2 Tablet

    Xperia Z2 Tablet

    aosp_shinano_castor_defconfig

    Xperia Z Ultra

    Xperia Z Ultra

    aosp_rhine_togari_defconfig

    Xperia Z1

    Xperia Z1

    aosp_rhine_honami_defconfig

    Xperia Z1 Compact

    Xperia Z1 Compact

    aosp_rhine_amami_defconfig

    E3_200x130

    Xperia E3

    aosp_yukon_flamingo_defconfig

    M2_200x130

    Xperia M2

    aosp_yukon_eagle_defconfig

    T2Ultra_200x130

    Xperia T2 Ultra

    aosp_yukon_tianchi_defconfig

    T3_200x130

    Xperia T3

    aosp_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=arm CROSS_COMPILE=$CROSS_COMPILE <device_defconfig> 

    Note: Replace <device_defconfig> with the device config file name identified in Step 5.

  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=arm CROSS_COMPILE=$CROSS_COMPILE -j <cpu_thread_number>

    Note: The <cpu_thread_number> should correspond to your host architecture. In case of errors, replace <cpu_thread_number> with the numeral 1 for easy debugging.

  8. Copy needed binaries to your Android repository
    For building a new boot image with the compiled kernel, the kernel binary “zImage” has to be renamed “kernel” and placed in the corresponding device/sony/<device> folder. Also the “.dtb” files have to be copied to the corresponding device/sony/<device>/dtbs folder.Note: Wireless drivers are built into the kernel and there is no kernel module for them.
  9. For environment setup instructions, see the AOSP build instructions. To generate the images enter the following commands:
    source build/envsetup.sh && lunch

    When prompted, pick the number corresponding to your device in the list displayed and press enter.
    To start the build, type:

    make -j <insert the cpu thread number of your computer> bootimage

    Note: The build will take some minutes to complete.

  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 Fastboot installed.
  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.