\subchapter{Compile and boot an Android Kernel}{Learn how to build an Android
Kernel and test it}

After this lab, you will be able to:
\begin{itemize}
  \item Extract the kernel patchset from Android Kernel
  \item Apply it on a vanilla kernel
  \item Compile and boot a kernel for the emulator
\end{itemize}

\section{Setup}

Go to the \code{/home/<user>/felabs/android/kernel} directory.

\section{Compile a kernel for the Android emulator}

The Android emulator uses QEMU to create a virtual ARM9 SoC called Goldfish.

In the standard Android source code we fetched, the kernel source was not included.
So the first thing to do is download the Android kernel sources.

\code{git clone https://android.googlesource.com/kernel/goldfish.git kernel}

Go to the \code{kernel} directory. That's where the kernel source repository
was cloned.

Using \code{git branch -a}, find the name of the most recent stable kernel version that 
supports the Goldfish platform. At the time of this writing, Linux 3.0 still doesn't
seem to work with the emulator, and you will have to choose the 2.6.29 kernel.

Switch to this branch as follows:

\code{git checkout <branch>}.

Now that we have kernel sources, we now need a toolchain to compile the kernel. Fortunately,
Android provides one. First, add the toolchain from the previous lab to your \code{PATH}.

\code{PATH=$HOME/felabs/android/aosp/android/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin:$PATH}

Now, configure the kernel for the target platform

\code{ARCH=arm make goldfish_defconfig}

Then, configure the kernel using the tool of your choice to add a custom suffix
to the kernel version and compile the kernel!

\code{ARCH=arm CROSS_COMPILE=arm-eabi- make -j 4}

A few minutes later, you have a kernel image in \code{arch/arm/boot}.

\section{Boot your new kernel}

To run the emulator again, this time with your new kernel image, you
have to restore the environment that you had after building Android from
sources. This is needed every time you reboot your workstation, and every time
you work with a new terminal.

Go back to \code{$HOME/felabs/android/aosp/android/}.

All you need to do is source the environment and run the \code{lunch}
command to specify which product you're working with
\footnote{Remember that Android sources allow you to work with
multiple products}:

\begin{verbatim}
source build/envsetup.sh
lunch generic-eng
\end{verbatim}

Now, test that you can still run the emulator as you did in the
previous lab.

To run the emulator with your own kernel, you can use the \code{-kernel}
option:

\begin{verbatim}
emulator -kernel ~/felabs/android/kernel/kernel/arch/arm/boot/zImage
\end{verbatim}

Once again, check the kernel version in the Android settings. Make sure
that the kernel was built today.

\section{Generate a patchset from the Android Kernel}

To compare the Android kernel sources with the official kernel ones,
we need to fetch the latter sources too, allowing us to see which 
commits differ. Git is great for that and offers everything we need.

First, add the \code{linux-stable} repository from Greg Kroah-Hartmann to our
repository:

\code{git remote add stable git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git}

Now, we need to fetch the changes from this remote repository:

\code{git fetch stable}

A while later, you should have all the changes in your own repository, browsable
offline!

Now, open the main \code{Makefile} with your favorite editor and get the kernel version
that is in it. Now that we have it, we can generate the whole set of commits that
differ from the linux kernel by doing:

\code{git log v<kernel-version>..HEAD}

Now, extract the corresponding set of patches\footnote{Git generates one patch per commit.}:

\code{git format-patch v<kernel-version>..HEAD}

In this set of patches, find the one that adds the wakelocks.

Congratuations! You now have all the patches to apply to a vanilla kernel version to obtain a
full Android kernel.

In this lab, we showed useful Git commands, but didn't go into details, as this is not a Git course. Don't
hesitate to ask your instructor for more details about Git.
