Tuesday, July 3, 2012

Android Raspberry Pi Kernel Build Walkthrough

I've had some queries on the blog about building the Raspberry Pi kernel I hacked together. Here are the instructions on how to compile the Android kernel for the Raspberry Pi environment.

This kernel should work with the headless Android filesystem.

NOTE: Since I have not been able to get my hands on the hardware I haven't tried it myself. You are on your own with this. I doubt it works right out of the box...

Build Instructions

All build instructions reference the base directory where you want to put the source files. For me I used:
export RASP_SRC_BASE=~/work/raspberry-pi

Get the kernel sources for linux-3.1.9

mkdir -p $RASP_SRC_BASE/kernel/patches
cd $RASP_SRC_BASE/kernel
wget http://www.kernel.org/pub/linux/kernel/v3.0/linux-3.1.9.tar.bz2
tar jxvf linux-3.1.9.tar.bz2


Apply the Raspberry Pi patch

cd $RASP_SRC_BASE/kernel/patches/
wget http://www.cnx-software.com/patch/r-pi_linux_3.1.9.patch.gz
gunzip r-pi_linux_3.1.9.patch.gz
cd $RASP_SRC_BASE/kernel/linux-3.1.9
patch -p1 < ../patches/r-pi_linux_3.1.9.patch


Get the Toolchain

cd $RASP_SRC_BASE/toolchain
wget https://sourcery.mentor.com/sgpp/lite/arm/portal/package9728/public/arm-none-linux-gnueabi/arm-2011.09-70-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2
tar jxvf arm-2011.09-70-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2 


Set up the environment variables

export PATH=$PATH:$RASP_SRC_BASE/toolchain/arm-2011.09/bin
export CROSS_COMPILE=$RASP_SRC_BASE/toolchain/arm-2011.09/bin/arm-none-linux-gnueabi-
export ARCH=arm
export CCACHE_DIR=~/.ccache/
export USE_CCACHE=1


Make sure the build works

At this point, configure and build the Raspberry Pi kernel.

Configure the kernel

cd $RASP_SRC_BASE/kernel/linux-3.1.9
cp arch/arm/configs/bcmrpi_cutdown_defconfig .config
make oldconfig

Make the kernel

make -k -j6


Apply the Android kernel patch

cd $RASP_SRC_BASE/kernel/patches/
wget https://dl.dropbox.com/u/24888185/android_3.1.9_patch.tar.gz
gunzip
android_3.1.9_patch.tar.gz
cd $RASP_SRC_BASE/kernel/linux-3.1.9
patch -p1 < ../patches/linux-3.1.9-stock-to-android.patch

patch -p1 < ../patches/linux-3.1.9-fs-proc-base.patch
patch -p1 < ../patches/linux-3.1.9-yaffs2.patch
patch -p1 < ../patches/linux-3.1.9-power.patch


Build Raspberry Pi Android

You are ready to build the Raspberry Pi Android kernel now.


Configure the kernel

cd $RASP_SRC_BASE/kernel/linux-3.1.9
wget https://dl.dropbox.com/u/24888185/rasp_android_defconfig.gz
gunzip rasp_android_defconfig.gz
mv rasp_android_defconfig .config


Make the kernel

make -k -j6



23 comments:

  1. Hey,
    thanks for the tutorial, i will start as i install a linux virtualpc (i am a linux newbie, but i can tell if it starts or not)
    it is ice cream sandwich, right? (if so, should we try it with aosp or cyanogen rom?)
    however i linked your blog on the raspi forum, hopefully some else will try and hopefully can make improvements if needed:
    http://www.raspberrypi.org/phpBB3/viewtopic.php?f=56&t=8780&p=118832#p118832

    ReplyDelete
  2. fodi,

    Looks like Shawn had some problems with the cross compiler I used on Ubuntu 12.04. He said he thought it was a problem with the difference in 10.04 and was able to get it working.

    I'm certainly interested in the changes he has to make to actually get it to run. If you have a chance to try his kernel let us know if it works.

    ReplyDelete
  3. Casey, where did you get the android patches from?

    Shawn Busker

    ReplyDelete
  4. Well, that's a rather long story Shawn. The trick was to isolate an Android patch that didn't have any other patches from a platform vendor installed. To do that I needed to find a working stock kernel AND an Android kernel on the same version. I wanted to use an ARM patched Android version, but couldn't find a good match up.

    I ended up taking the x86 Android port, which there was a 3.1.9 Android kernel version and created the patch from the stock 3.1.9 x86 kernel. That gave me a relatively clean Android patch with no specific ARM architecture patches mixed in.

    Hope that explanation helps.

    ReplyDelete
    Replies
    1. You should come over to the IRC chat channel for this discussion, we need someone like you to help us get android working. Currently we're having issues with init, but the problem might be kernel related because we're not completely certain what android configuration options should be enabled in the kernel.

      Server: irc.rizon.net
      Channel: #androidpi
      Link: irc://irc.rizon.net/androidpi

      I've tried compiling your kernel and I get the colour screen which indicates a problem with the kernel, do you have a compiled kernel from your instructions? If you do please email it to me at shawnbusker@gmail.com so I can test it.

      Shawn

      Delete
  5. Also, I had an all around question about headless android. Because there's no GUI would it be possible to run something like GNOME or XFCE on top of android for an interface? Android combined with the desktop interface and file manager of GNOME or XFCE would be interesting to see.

    Shawn

    ReplyDelete
    Replies
    1. Hey Shawn,

      Technically it may be possible, but the real problem you are going to run into is glibc compliance with Gnome or XFCE. I'm sure they wouldn't run on bionic very well. So to get them to work you would have to run statically compiled version or build glibc for the system. Neither of them would take advantage of any Android code with out pretty major modification, so it would seem somewhat pointless. You would basically have two systems running in parallel.

      The point of this blog is all about headless systems, so I'm going the opposite direction and not wanting a UI at all.

      The beauty of the open source community though is people can take systems that weren't intended to work together initially and create something new and interesting out of it. While it doesn't make sense to me why you would want to do what you are suggesting, if you feel that it would be something that "floats your boat" then I say go for it!

      Casey

      Delete
  6. I followed your instructions but the kernel file does not boot.

    After running file command on the new compiled kernel and the original kernel from a debian the file format is different.
    Why is this difference?

    ReplyDelete
    Replies
    1. Zenguz,

      What format did file say each of the kernels was in? It doesn't surprise me that it doesn't work, since I haven't been able to try it. Check the kernel configuration and make sure the file format (compressed image type) is set the way you think it should be set.

      Delete
  7. The kernel created following your instructions:
    vmlinux: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, BuildID[sha1]=0x0f6b49ac3574dc5ebf0cee86cc69354fcce48f91, not stripped


    Original kernel from raspberry firmware:
    kernel.img: DBase 3 index file

    ReplyDelete
    Replies
    1. Zenguz,

      Check out the build write up from the good folks at elinux.org.

      http://elinux.org/RPi_Kernel_Compilation

      Scroll down to the section titled "build kernel.img so your RPi can boot from it".

      You will need the mkimage tool from the raspberry pi github repository.

      I didn't describe how to convert the kernel image to one that will boot on the Pi nor how to flash it on the Pi itself. That's an exercise left to the reader. :-)

      Best of luck. Hope this information helps.

      Casey

      Delete
  8. Finally!
    I successfully compiled the kernel, created a kernel.img but after booting I get the colour screen...

    ReplyDelete
    Replies
    1. Excellent! That's great to hear!

      Delete
    2. I also followed your tutorial and got an color screen , so what should i do in order to get the boot screen and to get the android screen ?

      Delete
    3. When i browsed further i got the information that if the colored screes persists the kernel image is failing to boot

      (ref link : http://www.pearltrees.com/#/N-fa=2191657&N-f=1_5303249&N-s=1_5303249&N-p=49347852&N-u=1_206308&N-play=2)

      so i suppose there is some problem with the compilation you mentioned please check it!

      Delete
  9. why do u compile the kernel twice? sorry just beginning with android

    ReplyDelete
    Replies
    1. AVRcore,

      The instructions show to build twice because it's good to know you have all the pristine sources downloaded correctly before the raspberry pi patch is applied. It won't do you any good to apply the patch to a build that doesn't work in the first place. I recommend getting the normal build with out raspberry pi working first, then attempting the pi patch.

      Hope that helps.

      Delete
  10. And
    cd $RASP_SRC_BASE/kernel
    wget https://dl.dropbox.com/u/24888185/rasp_android_defconfig.gz
    gunzip rasp_android_defconfig.gz
    mv rasp_android_defconfig .config

    nothing will happen as there is no source is it a typo or I am missing something

    ReplyDelete
    Replies
    1. AVRcore,

      I'm not sure I follow your question. If you follow the whole walk through you will have the sources from previous steps. You need to follow the whole thing from the beginning though or it won't work.

      Good luck!

      Delete
    2. AVRcore,

      There is a small directory failure within the tutorial.

      cd $RASP_SRC_BASE/kernel/linux-3.1.9
      wget https://dl.dropbox.com/u/24888185/rasp_android_defconfig.gz
      gunzip rasp_android_defconfig.gz
      mv rasp_android_defconfig .config

      You have to change into the linux-3.1.9 directory to include the customized .config file.

      Greetz!

      Delete
    3. AVRcore,

      Thanks for pointing that out. I'll change it in the body of the text.

      Casey

      Delete
  11. Could anyone solve the colored screen issue? I followed all the steps but the kernel image is not working!

    ReplyDelete
  12. Hello,

    Has anybody got resolved colour screen issue.
    How to get init.rc and ueventd.rc file to modify the same to get USB port 777 rights. I have a peculiar requirement of developing a kernel module .ko file and loading same while android boots. Also need to allocate USB port 777 rights while Android boot. When I downloaded the kernel source, do not see init.rc and ueventd.rc any where in the downloaded source code. Are the kernel images (obtained by kernel compiling) are different than actual android images. I bit confused. If someone has got the android screen, must have also included ramdisk image as well along with kernel image. Want to know exact process for the same. Many thanks.

    ReplyDelete