Compiling a Kernel

From iDroid Project

Jump to: navigation, search

Contents

Open-Source Licenses

The iDroid Project is an Open-Source Project. The kernel is licensed under GPLv2. As such, changes to the source code must be made public.

If you plan to compile the kernel with your own changes to the source code, please make your changes public. What you have changed may be helpful for the iDroid Project and for helping in troubleshooting.

If you don't have a git repo to put you changes, that's ok. You can always create a pastie of the diffs you've created, especially if only 1 line was changed.

Warning

  • This is advanced and you must know what you are doing before you attempt to try it. If you do not know why sudo rm -rf / is a bad idea, STOP and do not attempt this.
  • A bad kernel may not let you boot into iDroid. The worst case scenario may brick your iDevice (but rarely happens). Prepare for a DFU restore if necessary.
  • Back-up all your important files just to be safe.

Getting Started

Android NDK

  1. Android NDK for Linux or Mac OS X. You can download it here only if did not build android yourself. If you have followed the Compiling Android guide to build android, then the NDK is already in your PC and there is no need to download this. Please download the version specific for your operating system.
  2. After you download the Android NDK (example NDK R5b), extract it to your home folder. It should look like /home/username/android-ndk-r5b (in Linux) or /Users/username/android-ndk-r5b (in OS X).

Linux

  1. Install git and libncurses5 (for make menuconfig) if you haven't. In Terminal (Applications > Accessories > Terminal) enter:
    sudo apt-get install git-core libncurses5-dev
  2. Clone the kernel repo. In Terminal enter:
    git clone http://github.com/iDroid-Project/kernel_common.git
    • If you get a server error, replace http:// with git://
    • After ~400MB of more than ~1,643,xxx files have been downloaded, you are going to get a folder called kernel_common in the directory where you ran Terminal and git clone from.

Mac OS X

You may use either Homebrew or MacPorts to setup git. However, it is redundant to use both as this may cause conflicts and errors in the long term.

Homebrew

  1. Install Homebrew if you haven't by entering the following command in a Terminal:
    ruby -e "$(curl -fsSLk https://gist.github.com/raw/323731/install_homebrew.rb)"
  2. After Homebrew has been downloaded and installed, enter the following command in Terminal:
    sudo brew install git
  3. Clone the kernel repo. In Terminal enter:
    git clone http://github.com/iDroid-Project/kernel_common.git
    • If you get a server error, replace http:// with git://
    • After ~400MB of more than 1,643,xxx files have been downloaded, you are going to get a folder called kernel_common in the directory where you ran Terminal and git clone from.

MacPorts

  1. Install MacPorts if you haven't:
    1. Download and run the installer from http://www.macports.org/install.php
    2. Make sure that /opt/local/bin is in your path before /usr/bin by running in Terminal:
      $ echo $PATH
    3. If you don't see /opt/local/bin, edit $HOME/.bash_profile and add the line export PATH=/opt/local/bin:$PATH after any other PATH-related lines. To verify that your path is now correct, open a new Terminal and run echo $PATH again.
  2. Get the following packages from Port:
    sudo port install gitcore
  3. Clone the kernel repo. In Terminal enter:
    git clone http://github.com/iDroid-Project/kernel_common.git
    • If you get a server error, replace http:// with git://
    • After ~400MB of more than 1,643,xxx files have been downloaded, you are going to get a folder called kernel_common in the directory where you ran Terminal and git clone from.

Compiling the kernel

Setting CROSS-COMPILE

  1. First of all, go into kernel_common and look for Makefile. Open it with your favorite/default text editor.
  2. Look for CROSS_COMPILE ?=/path to somewhere, this is usually somewhere near line 185.
  3. The value after CROSS_COMPILE ?= is usually the path to arm-eabi- in the Android NDK folder. If you extracted the Android NDK R5b to your home folder, this line should be set to something like this in Linux:
    CROSS_COMPILE	?= /home/username/android-ndk-r5b/toolchains/arm-eabi-4.4.0/prebuilt/linux-x86/bin/arm-eabi-
    Or in OS X:
    CROSS_COMPILE	?= /Users/username/android-ndk-r5b/toolchains/arm-eabi-4.4.0/prebuilt/darwin-x86/bin/arm-eabi-
    If you have compiled Android using the Compiling Android guide then the path in Linux is:
    CROSS_COMPILE	?= /home/username/pepparkaka/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/arm-eabi-
    Or in OS X
    CROSS_COMPILE	?= /Users/username/pepparkaka/prebuilt/darwin-x86/toolchain/arm-eabi-4.4.0/bin/arm-eabi-
    • Replace username with your username (example: /home/idroid/... or /Users/idroid/...).
    • In OS X, you need to add HOSTCFLAGS="-I/opt/local/include/ -I." after ../arm-eabi- (separated by a space), or else the kernel will no build.
    • Save the file after you have done the necessary revisions.

Mac OS X Fixes/Workarounds

The Kernel will not compile in OS X unless the following fixes / workarounds are done:

malloc.h

  1. Open Terminal, and in the kernel_common (main kernel directory), enter these commands:
    cd scripts/genksyms
    sed -i .backup 's/malloc.h/stdlib.h/' *.c*
    cd .. && cd ..
  2. The above commands will eliminate the malloc.h errors.

elf.h

  1. The included elf.h in the android build system will fix elf.h errors.
  2. Download the elf.h above to your /usr/include folder (you will need root/sudo privileges for this). Alternatively, you can use nano for this:
    sudo nano /usr/include/elf.h
  3. The copy all the text from the elf.h above and paste into nano, then Save/Write-out (control + O) the file.

Configuring the Kernel

  1. Open a Terminal:
    cd kernel_common
    make menuconfig
  2. Wait for menuconfig to start. Here you can play and tinker around with the configurations to save a config file (.config, normally hidden in File Browser).
    • To set the specific device, go to system type > Apple iPhone platform type > select whatever is there > then select whatever you are building the kernel for
    • Also you have to set the correct multitouch drivers, go to Device drivers > Input Devices > Touchscreens > uncheck zephyr2 or zephyr, and check zephyr or zephyr2 if the kernel you're compiling is for 2G or 3G.
  3. Save your configuration and exit menuconfig.

Making the Kernel

  1. Still in Terminal (and in kernel_common directory):
    make -j2
    (-j = num of compile processes, 2 per core)
  2. If this is the first time you are compiling, this will take a while... so multitask, chat on the #idroid-dev channel, sleep, whatever.
  3. After everything is done (the prompt will show up again), in your favorite/default File Manager, go to kernel_common/arch/arm/boot. zImage should be there.

What to do with zImage

  1. The zImage file is the kernel and should be found in kernel_common/arch/arm/boot
  2. In iOS, use SSH, FTP or USB to upload the zImage to your iDevice's /private/var/idroid directory, replacing the previous zImage (Back-it up first before replacing).
  3. Boot into Android/iDroid and see if your newly compiled kernel works.

Errors

  • Sometimes Terminal may display errors about lowmemorykiller.c and you will not get a kernel output. This is usually caused by faulty coding somewhere. Faulty coding will not let you compile a kernel.
  • Task Error
... 
CC kernel/cpuset.o
kernel/cpuset.c: In function 'cpuset_can_attach':
kernel/cpuset.c:1333: error: 'task' undeclared (first use in this function)
kernel/cpuset.c:1333: error: (Each undeclared identifier is reported only once
kernel/cpuset.c:1333: error: for each function it appears in.)
make[1]: *** [kernel/cpuset.o] Error 1
make: *** [kernel] Error 2

Turns out this is a typo in the kernel/cpuset.c file, find the word "task" in the file, and make it "tsk", so line 1333 of cpuset.c will now look like

if ((current != tsk) && (!capable(CAP_SYS_ADMIN))) {

instead of

if ((current != task) && (!capable(CAP_SYS_ADMIN))) {

However disable cpuset will be better.

  • coda.h error
..
CC [M]  fs/coda/psdev.o
In file included from fs/coda/psdev.c:44:
include/linux/coda.h:247: error: expected specifier-qualifier-list before 'u_quad_t'

It seems that it is because of that the arm-embi-gcc in Android is using "Thread model: single"

This solution may help : http://blog.csdn.net/stevenliyong/archive/2009/10/11/4654990.aspx

  • VGA Console Error
...
drivers/video/console/vgacon.c:508: error

Suggested solution: Disable VGA Console in configmenu

  • implicit declaration of function 'cpu_do_idle' Error
In file included from arch/arm/mach-apple_iphone/iphone.c:50:
arch/arm/mach-apple_iphone/include/mach/system.h: In function 'arch_idle':
arch/arm/mach-apple_iphone/include/mach/system.h:33: error: implicit declaration of function 'cpu_do_idle'
make[1]: *** [arch/arm/mach-apple_iphone/iphone.o] Error 1
make: *** [arch/arm/mach-apple_iphone] Error 2

Edit the file:

arch/arm/mach-apple_iphone/iphone.c

and add:

#include <asm/cpu-single.h>

after line 50, right after #include <asm/system.h>:

#include <asm/system.h>
#include <asm/cpu-single.h>
#include <mach/system.h>