Thursday, July 5, 2012

[Copied]Embedded Linux Images


zImage
  • gzip-ed kernel image
  • same as arch/arm/boot/compressed/vmlinux
  • objcopy of arch/arm/boot/compressed/vmlinux
  • used to reduce storage size of kernel
  • piggy.gzip.o + misc.o + head.o + head-mach*.o = zImage
  • cannot be opened using gzip ’cause it contains code at begining to decompress gzip-ed image
vmlinux
  • uncompressed kernel file
  • it is just an intermediate step and almost never used
  • vm stands for “virtual memory”, suggesting that it supports vm
  • it is objcopy-ed to get Image
arch/arm/boot/compressed/vmlinux
  • it is same as zImage but renamed
  • it is objcopy-ed to get zImage
vmlinuz
  • it is gzip-ed version of vmlinux
  • it is same as zImage but renamed
bzImage
  • it is not compressed using bzip but compressed using gzip same as zImage
  • bzImage stands for big zImage
  • The difference is that the old zImage uncompresses the kernel into low memory (the first 640k), and bzImage uncompresses the kernel into high memory (over 1M)
  • the difference lies between the maximum sizes supported by each, for zImage the kernel image size is limited to 520KB, however this restriction does not exist in the bzImage format.
uImage
  • related to Uboot
bootp
  • contains both kernel(zIamge) and initrd
  • this way bootloader doesnt need to load kernel and initrd seperately into memory. it is taken care by the kernel itself.
Image
  • it is objcopy-ed version of vmlinux
piggy.gzip
  • gzipped version of Image

[Solved]"mkimage" command not found - U-Boot images will not be built



Problem: "mkimage" command not found - U-Boot images will not be built

Solution: sudo apt-get install uboot-mkimage

               make the kernel again

Tuesday, July 3, 2012

[Copied]Building a custom kernel for the Nexus S


There are several reasons why someone would want to build a custom kernel for their Android phone. In my case, this is because I wanted performance counters (those used by the perf tool that comes with the kernel source). In Julian Seward‘s case, he wanted swap support to overcome the limited memory amount on these devices in order to run valgrind. In both cases, the usual suspects (AOSP, CyanogenMod) don’t provide the wanted features in prebuilt ROMs.
There are also several reasons why someone would NOT want to build a complete ROM for their Android phone. In my case, the Nexus S is what I use to work on Firefox Mobile, but it is also my actual mobile phone. It’s a quite painful and long process to create a custom ROM, and another long (but arguably less painful thanks to ROM manager) process to backup the phone data, install the ROM, restore the phone data. And if you happen to like or use the proprietary Google Apps that don’t come with the AOSP sources, you need to add more steps.
There are however tricks that allow to build a custom kernel for the Nexus S and use it with the system already on the phone. Please note that the following procedure has only been tested on two Nexus S with a 2.6.35.7-something kernel (one with a stock ROM, but unlocked, and another one with an AOSP build). Also please note that there are various ways to achieve many of the steps in this procedure, but I’ll only mention one (or two in a few cases). Finally, please note some steps rely on your device being rooted. There may be ways to do without, but I’m pretty sure it requires an unlocked device at the very least. This post doesn’t cover neither rooting nor unlocking.

Preparing a build environment

To build an Android kernel, you need a cross-compiling toolchain. Theoretically, any will do, provided it targets ARM. I just used the one coming in the Android NDK:
$ wget http://dl.google.com/android/ndk/android-ndk-r6b-linux-x86.tar.bz2
$ tar -jxf android-ndk-r6b-linux-x86.tar.bz2
$ export ARCH=arm
$ export CROSS_COMPILE=$(pwd)/android-ndk-r6b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-
For the latter, you need to use a directory path containing prefixed versions (such as arm-eabi-gcc or arm-linux-androideabi-gcc), and include the prefix, but not “gcc”.
You will also need the adb tool coming from the Android SDK. You can install it this way:
$ wget http://dl.google.com/android/android-sdk_r12-linux_x86.tgz
$ tar -zxf android-sdk_r12-linux_x86.tgz
$ android-sdk-linux_x86/tools/android update sdk -u -t platform-tool
$ export PATH=$PATH:$(pwd)/android-sdk-linux_x86/platform-tools

Building the kernel

For the Nexus S, one needs to use the Samsung Android kernel tree, which happens to be unavailable at the moment of writing due to the kernel.org outage. Fortunately, there is a clone used for the B2G project, which also happens to contain the necessary cherry-picked patch to add support for the PMU registers on the Nexus S CPU that are needed for the performance counters.
$ git clone -b devrom-2.6.35 https://github.com/cgjones/samsung-android-kernel
$ cd samsung-android-kernel
You can then either start from the default kernel configuration:
$ make herring_defconfig
or use the one from the B2G project, which enables interesting features such as oprofile:
$ wget -O .config https://raw.github.com/cgjones/B2G/master/config/kernel-nexuss4g
From then, you can use the make menuconfig or similar commands to further configure your kernel.
One of the problems you’d first encounter when booting such a custom kernel image is that the bcm4329 driver module that is shipped in the system partition (and not in the boot image) won’t match the kernel, and won’t be loaded. The unfortunate consequence is the lack of WiFi support.
One way to overcome this problem is to overwrite the kernel module in the system partition, but I didn’t want to have to deal with switching modules when switching kernels.
There is however a trick allowing the existing module to be loaded by the kernel: compile a kernel with the same version string as the one already on the phone. Please note this only really works if the kernel is really about the same. If there are differences in the binary interface between the kernel and the modules, it will fail in possibly dangerous ways.
To use that trick, you first need to know what kernel version is running on your device. Settings > About phone > Kernel version will give you that information on the device itself. You can also retrieve that information with the following command:
$ adb shell cat /proc/version
With my stock ROM, this looks like the following:
Linux version 2.6.35.7-ge382d80 (android-build@apa28.mtv.corp.google.com) (gcc version 4.4.3 (GCC) ) #1 PREEMPT Thu Mar 31 21:11:55 PDT 2011
In the About phone information, it looks like:
2.6.35.7-ge382d80
android-build@apa28
The important part above is -ge382d80, and that is what we will be using in our kernel build. Make sure the part preceding -ge382d80 does match the output of the following command:
$ make kernelversion
The trick is to write that -ge382d80 in a .scmversion file in the kernel source tree (obviously, you need to replace -ge382d80 with whatever your device has):
$ echo -ge382d80 > .scmversion
The kernel can now be built:
$ make -j$(($(grep -c processor /proc/cpuinfo) * 3 / 2))
The -j… part is the general rule I use when choosing the number of parallel processes make can use at the same time. You can pick whatever suits you better.
Before going further, we need to get back to the main directory:
$ cd ..

Getting the current boot image

The Android boot image living in the device doesn’t contain only a kernel. It also contains a ramdisk containing a few scripts and binaries, that starts the system initialization. As we will be using the ramdisk coming with the existing kernel, we need to get that ramdisk from the device flash memory:
$ adb shell cat /proc/mtd | awk -F'[:"]' '$3 == "boot" {print $1}'
The above command will print the mtd device name corresponding to the “boot” partition. On the Nexus S, this should be mtd2.
$ adb shell
$ su
# dd if=/dev/mtd/mtd2 of=/sdcard/boot.img bs=4096
2048+0 records in
2048+0 records out
8388608 bytes transferred in x.xxx secs (xxxxxxxx bytes/sec)
# exit
$ exit
In the above command sequence, replace mtd2 with whatever the previous command did output for you. Now, you can retrieve the boot image:
$ adb pull /sdcard/boot.img

Creating the new boot image

We first want to extract the ramdisk from that boot image. There are various tools to do so, but for convenience, I took unbootimg, on github, and modified it slightly to seemlessly support the page size on the Nexus S. For convenience as well, we’ll use mkbootimg even if fastboot is able to create boot images.
Building unbootimg, as well as the other tools rely on the Android build system, but since I didn’t want to go through setting it up, I figured a minimalistic way to build the tools:
$ git clone https://github.com/glandium/unbootimg.git
$ git clone git://git.linaro.org/android/platform/system/core.git
The latter is a clone of git://android.git.kernel.org/platform/system/core.git, which is down at the moment.
$ gcc -o unbootimg/unbootimg unbootimg/unbootimg.c core/libmincrypt/sha.c -Icore/include -Icore/mkbootimg
$ gcc -o mkbootimg core/mkbootimg/mkbootimg.c core/libmincrypt/sha.c -Icore/include
$ gcc -o fastboot core/fastboot/{protocol,engine,bootimg,fastboot,usb_linux,util_linux}.c core/libzipfile/{centraldir,zipfile}.c -Icore/mkbootimg -Icore/include -lz
Once the tools are built, we can extract the various data from the boot image:
$ unbootimg/unbootimg boot.img
section sizes incorrect
kernel 1000 2b1b84
ramdisk 2b3000 22d55
second 2d6000 0
total 2d6000 800000
...but we can still continue
Don’t worry about the error messages about incorrect section sizes if it tells you “we can still continue”. The unbootimg program creates three files:
  • boot.img-mk, containing the mkbootimg options required to produce a working boot image,
  • boot.img-kernel, containing the kernel image,
  • boot.img-ramdisk.cpio.gz, containing the gzipped ramdisk, which we will reuse as-is.
All that is left to do is to generate the new boot image:
$ eval ./mkbootimg $(sed s,boot.img-kernel,samsung-android-kernel/arch/arm/boot/zImage, boot.img-mk)

Booting the image

There are two ways you can use the resulting boot image: one-time boot or flash. If you want to go for the latter, it is best to actually do both, starting with the one-time boot, to be sure you won’t be leaving your phone useless (though recovery is there to the rescue, but is not covered here).
First, you need to get your device in the “fastboot” mode, a.k.a. boot-loader:
$ adb reboot bootloader
Alternatively, you can power it off, and power it back on while pressing the volume up button.
Once you see the boot-loader screen, you can test the boot image with a one-time boot:
$ ./fastboot boot boot.img
downloading 'boot.img'...
OKAY [ 0.xxxs]
booting...
OKAY [ 0.xxxs]
finished. total time: 0.xxxs
As a side note, if fastboot sits “waiting for device”, it either means your device is not in fastboot mode (or is not connected), or that you have permissions issues on the corresponding USB device in /dev.
Your device should now be starting up, and eventually be usable under your brand new kernel (and WiFi should be working, too). Congratulations.
If you want to use that kernel permanently, you can now flash it after going back in the bootloader:
$ adb reboot bootloader
$ ./fastboot flash boot boot.img
sending 'boot' (2904 KB)...
OKAY [ 0.xxxs]
writing 'boot'...
OKAY [ 0.xxxs]
finished. total time: 0.xxxs
$ ./fastboot reboot
VoilĂ .
2011-09-14 09:23:47+0200
You can leave a response, or trackback from your own site.

7 Responses to “Building a custom kernel for the Nexus S”

  1. foo Says:
    Can we have Iceweasel Mobile in Debian now please?
    Support for Nexus S by Debian would be nice too…
  2. Is there a walk-through on how to recompile an Android kernel, and uploading it back to the device? - Quora Says:
    [...] There are some specific walk-throughs depending on the Android device you use. Please refer:1) http://glandium.org/blog/?p=22142)http://www.nabeelalimemon.com/bl…3) http://wiki.cyanogenmod.com/wiki…This answer .Please [...]
  3. Corrado Says:
    works fine! thanks :)
  4. witek Says:
    kernel.org is back. Where there is a git tree for samsung android?
  5. Apoorva Says:
    Hi I am following the above mentioned steps with a few modifications
    # I am using the latest sdk
    # I had android 2.3.6 already installed on my Nexus S and the kernel version was 2.65.7-gf5f63ef
    I have made the appropriate changes in commands
    However while executing the command
    make -j$(($(grep -c processor /proc/cpuinfo) * 3 / 2))
    I was expecting to get the file zImage at the path
    /samsung-android-kernel/arch/arm/boot/zImage
    However no image is formed there..
    Can u please help me proceed..
    Thanks
  6. Apoorva Says:
    Hey thanks,
    I was able to resolve this issue. Thanks for a wonderful post !!
  7. Kwalla Says:
    I’m trying to build the perf tool using ARCH=arm and a arm cross compiler, and I’m getting:
    Makefile:512: No libdw.h found or old libdw.h found or elfutils is older than 0.138, disables dwarf support. Please install new elfutils-devel/libdw-dev
    Makefile:548: *** No libelf.h/libelf found, please install libelf-dev/elfutils-libelf-devel and glibc-dev[el]. Stop.
    at tool build. I already have a working kernel up and running, and I’ve been searching for a workaround to this, but I’ve been unsuccessful. The last perf tool modification was from 10-20-2010, and I’ve found that this error existed in the past, but no solutions. Since you mentioned it at the beginning of your post, do you know a workaround?
    Thanks!

Leave a Reply

[Solved]adb/fastboot no device attached, on ubuntu

Problem:
When trying to show devices using "adb" or "fastboot", no device is shown attached.
PS: /etc/udev/51-android.list has been edited correctly

Solution:
On your phone, go to Settings->Developer options->USB debugging, check that box, done.

Monday, July 2, 2012

[Solved]/bin/bash: out/host/linux-x86/bin/llvm-rs-cc: Permission denied

Problem: compiling Android 4.0 on Ubuntu 11.10, come across with error "/bin/bash: out/host/linux-x86/bin/llvm-rs-cc: Permission denied".

Solution:
remove /out dir
recompile AOSP

[Solved]"acp: file 'out/host/linux-x86/obj/EXECUTABLES/vm-tests_intermediates/tests/data' does not exist"


Problem:
"acp: file 'out/host/linux-x86/obj/EXECUTABLES/vm-tests_intermediates/tests/data' does not exist"

Solution:
mkdir -p out/host/linux-x86/obj/EXECUTABLES/vm-tests_intermediates/tests/data

[copied]How to BUILD the LINUX KERNEL for the ANDROID EMULATOR (Eclair version)


DOWNLOAD THE KERNEL SOURCE CODE


First we download the kernel source code from http://android.git.kernel.org

Within that page there are kernels for other platforms too. We choose to download kernel/common project from there.

$git clone git://android.git.kernel.org/kernel/common

We check which branch we have downloaded:
$git branch
it shows * android-2.6.27, not the one we are searching for:

To list all remote available branches:
$git branch -r
origin/HEAD -> origin/android-2.6.27
origin/android-2.6.25
origin/android-2.6.27
origin/android-2.6.29
origin/android-2.6.32
origin/android-goldfish-2.6.27
origin/android-goldfish-2.6.29


What does goldfish mean? (from android-kernel mail list)
Goldfish is the kernel hacked branch that supports the qemu based arm emulator for android, so it is the one we need.

Download GOLDFISH kernel version
$git checkout --track -b android-goldfish-2.6.29 origin/android-goldfish-2.6.29
$git branch
android-2.6.27
* android-goldfish-2.6.29


RUNNING THE EMULATOR


Within this link we will find how to get the android emulator, and launch it.
Building Android in Debian Sid

Showing the kernel version running in the emulator
$adb shell
#cat /proc/version
Linux version 2.6.29-00261-g0097074 (digit@digit.mtv.corp.google.com) (gcc version 4.4.0 (GCC) ) #14 Tue Feb 2 15:49:02 PST 2010


OBTAINING KERNEL CONFIGURATION


We are going to obtain the kernel configuration .config file from within our running emulator.

$cd common # we enter in the kernel source directory.
$adb pull /proc/config.gz . # get compressed .config file from the emulator.

$gunzip config.gz # uncompress it.
$cp config .config # rename it into .config

Now you can edit .config file the way it suits you the most.


BUILDING AND COMPILING THE KERNEL


CROSS_COMPILE environment variable stores the path to the arm cross compiling toolchain. I use the one which comes with android source code.

$ARCH=arm CROSS_COMPILE=/path/to/mydroid/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/arm-eabi- make

Executing make will build the kernel.

Last lines will show:
Kernel: arch/arm/boot/Image is ready
Kernel: arch/arm/boot/zImage is ready


So we have obtained Image and zImage kernel binary files.


RUN THE EMULATOR USING THE NEW COMPILED KERNEL IMAGE


We need -kernel option:
$emulator -kernel /path/to/common/arch/arm/boot/zImage -show-kernel -verbose

We can check now the kernel version:
$adb shell
# cat /proc/version
Linux version 2.6.29-00262-gb0d93fb (user@myPC) (gcc version 4.4.0 (GCC) ) #1 Sun May 2 14:27:31 CEST 2010


If we do not specify kernel option it usually uses the prebuilt one:
$emulator -show-kernel -verbose
emulator: argv[01] = "-kernel"
emulator: argv[02] = "/path/to/mydroid/prebuilt/android-arm/kernel/kernel-qemu"


ACTIVATING MODULE LOADING SUPPORT IN THE KERNEL


Module loading support is previously disabled in the kernel, if we want to load modules in the kernel we have to enable it:

edit .config file and set:
CONFIG_MODULES=y

$ ARCH=arm CROSS_COMPILE=/path/to/mydroid/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/arm-eabi- make

I am asked about some options when executing make. I ask yes for module related options.

After compiling I see several modules have been built.

MODPOST 6 modules
CC      drivers/video/fb_sys_fops.mod.o
LD [M]  drivers/video/fb_sys_fops.ko
CC      drivers/video/syscopyarea.mod.o
LD [M]  drivers/video/syscopyarea.ko
CC      drivers/video/sysfillrect.mod.o
LD [M]  drivers/video/sysfillrect.ko
CC      drivers/video/sysimgblt.mod.o
LD [M]  drivers/video/sysimgblt.ko
CC      drivers/video/vfb.mod.o
LD [M]  drivers/video/vfb.ko


We can upload the modules in the emulator and install them:
$adb push drivers/video/fb_sys_fops.ko /data
$adb shell
#insmod /data/fb_sys_fops.ko


REFERENCE


http://www.cianer.com/androidg1/28-building-android-kernel-images


YOU MAY ALSO BE INTERESTED IN:


Building Android in Debian Sid