\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 Compile and boot a kernel for the emulator
\end{itemize}

\section{Setup}

Go to the \code{$HOME/android-labs/} directory.

\section{Compile a kernel for the Android emulator}

The Android emulator uses QEMU to create a virtual ARMv7 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 stored in the git repository
\url{https://android.googlesource.com/kernel/goldfish.git} using
repo's local manifests. Store it into a folder called \code{kernel}.

Make repo download the kernel, and once done, 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.10 still don't seem to work with the emulator, and you will have to
choose either the 3.4 or the 2.6.29 kernels. Since the latter is quite
ancient, we'll use 3.4.

Edit your local manifest to use the revision you just identified.

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/android-labs/source/prebuilts/gcc/linux-x86/arm/arm-eabi-4.6/bin:$PATH}

Now, configure the kernel for the target platform

\code{ARCH=arm make goldfish_armv7_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/android-labs/source}.

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 aosp_arm-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 $HOME/android-labs/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}

Go back to the \code{$HOME/android-labs/kernel} directory.

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. This corresponds to the kernel version
used by Android for the emulator. It should be 2.6.29 if you chose that
version. Now, let's have a look at the set of commits between the
mainline kernel sources and the Android kernel sources (\code{HEAD}):

\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 wakelock API.

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.
