[XDA-University] Nick_73:
There are a lot of people out there wondering how to build kernels. It sounds confusing and hard, but once you get the hang of it, it is actually quite easy. I got the jist of it in just a few practices. Now I give all of my information to you, in the simplest, most comprehensible and most noob friendly way I can. In this guide, I will be building the kernel for my phone, the Samsung Galaxy Victory 4G LTE. So make your adjustments where necessary.

What You Will Need:

A linux OS. I now use Ubuntu 14.04 Manjaro, and Manjaro or Arch are preferred. (Ubuntu is easiest)

Patience

(Optional) A boot.img for your device

Required files

Required Files:

Open terminal and paste the following (I assume you already have java and perl and all that jazz):

In Ubuntu/ Debian:

sudo apt-get install abootimg git-core gnupg flex bison gperf libsdl-dev libesd0-dev libwxgtk2.6-dev build-essential zip curl libncurses5-dev zlib1g-dev ia32-libs lib32z1-dev lib32ncurses5-dev gcc-multilib g++-multilib

For Arch/Manjaro, do

sudo pacman -S gcc git gnupg flex bison gperf sdl wxgtk squashfs-tools curl ncurses zlib schedtool perl-switch zip unzip libxslt python2-virtualenv bc

for 32 and 64 bit systems. For only 64 bit, add the following:

sudo pacman -S gcc-multilib lib32-zlib lib32-ncurses lib32-readline

Also, abootimg can be installed using

yaourt -S abootimg-git

For other distros, you may have to modify the command to get the required files, such as using yum

Also, make sure you have setup git and it is ready to go.

Setting Up Folders:

You are going to need a folder to build you kernel in. For this tutorial, I will use my setup: /home/nick/android/kernel/SPH-L300/Kernel. Kernel is the source directory that is made from the source zip and SPH-L300 I will also use a lot, which contains Kernel.

Downloading The Source:

Download the zip from http://opensource.samsung.com/ or from InsanelyCool's stock kernel source. Then I put it in the SPH-L300 folder. Afterwards, unzip it. You should have 2 files, Kernel.tar.gz and Platform.tar.gz. We are going to focus on Kernel.tar.gz, so unzip it. This will make the Kernel folder.

Now, in your terminal, cd to somewhere to keep your toolchains. I keep mine in ~/android/kernel.

Now type

git clone git://github.com/DooMLoRD/android_prebuilt_toolchains.git toolchains

for the toolchains I used. They should be put in the folder toolchains.

Modifying The Kernel:

Now is the time to modify the kernel. You can apply patches and change governors and frequencies etc here.

Applying a Patch:

The kernel version is 3.0.31. You can upgrade it using patches. Patches can be found here. You have to apply one patch at a time, which is why scripts come in handy when patching multiple times. I am going to show you how to upgrade to 3.0.33, since you can do that without error.

First, download patch-3.0.31-32.bz2 (or .gz) and download patch-3.0.32-33.bz2 (or .gz).

Extract both and move the extracted files into the Kernel source folder.

Cd to this folder in your terminal and type

patch -p1 < patch-3.0.31-32

. If you get something about

...assume -R? [n]

just press

enter

then press

y

then

enter

again.

Do the same steps for applying the other patch, just mod the command to work.

Building The Kernel:

Setting Up Environment:

In the Kernel source directory, open the file Makefile and press

ctrl-f

. Then enter CROSS_COMPILE, and look for CROSS_COMPILE = /xxxxx, where xxxxx is a directory. Change that directory to the toolchain of your choice. **Not all toolchains work!** For me, it would look like: CROSS_COMPILE = /home/nick/android/kernel/toolchains/arm-eabi-linaro-4.6.2/bin/arm-eabi- for Linaro 4.6.2. Save the file and exit.

Make sure your terminal is in the Kernel source directory.

Kernel Name:

If you want to change the kernel name, in terminal type

KBUILD_BUILD_VERSION="ROM NAME"

then

export KBUILD_BUILD_VERSION

.

Compiling It:

Next, type

export ARCH=arm

into terminal.

Almost there. The next step is to type

make gogh_defconfig

into terminal.

If you added options such as governors, do

make menuconfig

to activate them.

Otherwise, type

make -jx

where x is the number of processes per core. I use

-j2

for a dual core cpu. This takes about 20-30 mins for me. A quad-core Intel cpu takes 4 mins if you use -j5. For Intel, it is the # of cores + 1.

Actually Putting The Kernel Together:

Now that it has built, assuming you have had no errors and the terminal finishes with

Kernel: arch/arm/boot/zImage is ready

you are ready to assemble the kernel.

Cd

to outside your Kernel source to the folder that contains it. For me that would be SPH-L300. You are going to need a working boot.img for this phone to speed up this process by a lot. Whether its stock, mine, or Insanelycool's, just paste it into the folder.

In terminal, type

abootimg -x boot.img

Then

mkdir initrd && cd initrd && zcat ../initrd.img | cpio -i

And finally

find . | cpio -o -H newc | gzip > ../initrd.img

This splits the boot.img, and puts the ramdisk in the initrd folder.

Now, navigate to (kernel source)/arch/arm/boot and copy the zImage to where you were before (the SPH-L300 folder for me). When you paste, override the pre-existing zImage.

If you would like to change the splash screen before the bootanimation, swap out initlogo.rle in the initrd folder with the image of you choice. It must be a rle image and must be named initlogo to work. I won't tell you how to convert png's to rle's cuz I am sure you know how to Google.

Now, we actually but the boot.img together. In terminal, type

cd ../ && abootimg --create boot.img -k zImage -r initrd.img && abootimg --create boot.img -f bootimg.cfg -k zImage -r initrd.img

and make sure you are in the initrd folder. If you get the error updated is too big for Boot Image, use this command:

cd ../ && abootimg --create boot.img -k zImage -r initrd.img && abootimg --create boot.img -f bootimg.cfg -k zImage -c "bootsize=xxxxxx" -r initrd.img

and replace the x's with the size the kernel wants to be. This will give you a new boot.img in the containing folder (SPH-L300 for me). Now just zip it up and tell the updater-script where to flash it and voila!, you just made you own kernel.

Feel free to ask any questions below.

I WOULD LIKE TO THANK ONE PERSON IN SPECIFIC, AND THAT IS INSANELYCOOL FOR TEACHING ME MOST OF THE STEPS HERE.