Author Topic: Howto compile custom kernel  (Read 18923 times)

Offline Slayra

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 77
  • Karma: 17
    • View Profile
Howto compile custom kernel
« on: June 01, 2011, 06:45:40 PM »

Well, after spending a few days trying to figure how to compile a kernel for my Zero, I thought it would be nice to write a guide for people who would like to do it on their own.

I had some experience compiling kernels for home use linux machines, but I never done anything similar to handheld devices.
So I figured this out by myself, and some steps might be stupid for people with further skills and experience.
Any suggestions are always welcome, as I've done this to learn!

After reading a lot of guides from others, searching google and adapting what I thought would be necessary, these are the steps I took to build my custom kernel based on Ricardo Cerqueira's (rmcc) upgraded source featured on cyanogenmod github. All credit and thanks to him!


Please note that I'm not responsible if you break your device trying anything written below!!


All these steps were performed in a linux machine (Slackware, to be more specific), and the resulting kernel was tested on CyanogenMod nightly 84.
(I believe these specific steps will work for stock rom with a few tweaks here and there)


Before I begin, I'll assume you have adb and fastboot properly installed and working.


1- First of all, you need the kernel sources! You may download them using git by doing:

git clone git://github.com/CyanogenMod/geeksphone-kernel-zero.git

This will create a directory named geeksphone-kernel-zero with all kernel sources inside.

Note: for some reason, when compiling the kernel I had some error related with a missing pm.h file. I don't know if it is suposed to be missing or if there was some problem when Ricardo uploaded the files to github.
Anyway, I've attached the file to this post and if you have an error on step 6 regarding this specific file, just copy it to:

cp pm.h geeksphone-kernel-zero/include/linux/mmc/pm.h


2- Now you need to configure your kernel.

As you probably won't want to start from scratch, I recommend you to use the configuration in use with your Zero as a starting point.
To retrieve the configuration file do the following:

adb pull /proc/config.gz

Now that you have the config.gz file in your working directory, you have to decompress it by doing:

gunzip config.gz

Next, we copy the resulting file to the root of the kernel sources doing:

cp config geeksphone-kernel-zero/.config


3- You'll also need an arm compiler.

You may download it from http://www.codesourcery.com/sgpp/lite/arm/portal/release1293
(I chose the one named "IA32 GNU/Linux TAR" in "Advanced Packages")

Untar the downloaded file:

tar -xjvf arm-2010q1-202-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2

And move the resulting directory (named arm-2010q1) to the root of the kernel sources:

mv arm-2010q1/ geeksphone-kernel-zero/


4- Now we need to make sure we're building sources for the arm architecture.

To do so, enter the kernel sources directory:

cd geeksphone-kernel-zero

And edit the Makefile (using vim or other editor of your choice):

vim Makefile

Now you'll need to find the following lines:

ARCH                          ?=$(SUBARCH)
CROSS_COMPILE   ?=


and change them to:

ARCH                          ?=arm
CROSS_COMPILE   ?=arm-2010q1/bin/arm-none-linux-gnueabi-


Save and close the editor.


5- Now you can proceed to customizing your kernel.

Type the following command:

make menuconfig

and you'll see a menu with different options regarding different areas of the kernel.
In this step you can make your changes but be aware that some won't work and as a consequence, your Zero won't boot.


6- After all changes have been made in step 5, now we only have to compile the kernel to build us a zImage and the modules we eventually have.

(If you're compiling the kernel without any changes made in step 5, you should get a kernel similar to the one used in cyanogenMod and perfectly working. Notice that the wireless driver is built as a module so, in this case, after compiling the kernel, you'll have a zImage and a module called dhd.ko)

Just type:

make

and go get something to eat or drink, because this step will take a while (unless there's an error or something).

You should see something like this in the end:

------------------------------
  LD      vmlinux
  SYSMAP  System.map
  SYSMAP  .tmp_System.map
  OBJCOPY arch/arm/boot/Image
  Kernel: arch/arm/boot/Image is ready
  GZIP    arch/arm/boot/compressed/piggy.gz
  AS      arch/arm/boot/compressed/piggy.o
  LD      arch/arm/boot/compressed/vmlinux
  OBJCOPY arch/arm/boot/zImage
  Kernel: arch/arm/boot/zImage is ready      <--- Kernel zImage
  Building modules, stage 2.
  MODPOST 2 modules
  CC      drivers/net/wireless/bcm4329/dhd.mod.o
  LD [M]  drivers/net/wireless/bcm4329/dhd.ko   <--- Wireless driver module
  CC      drivers/scsi/scsi_wait_scan.mod.o
  LD [M]  drivers/scsi/scsi_wait_scan.ko
------------------------------


7- Now that you have a zImage and modules ready, you need to know what to do with them.

To use your custom kernel (zImage), you need to repack the boot image on your device.

I won't go into great detail, because there's a nice tutorial/explanation regarding this issue on the following link:
 
http://android-dls.com/wiki/index.php?title=HOWTO:_Unpack,_Edit,_and_Re-Pack_Boot_Images

--> I STRONLGY recomend you to read the tutorial as some tools needed for the following steps are there  <--

That being said, I'll just show you the steps.


7.a- Extracting boot.img from your device to "kernel" directory on your sdcard:

adb shell
mkdir /sdcard/kernel
cat /dev/mtd/mtd0 > /sdcard/kernel/boot.img
exit


Now you need to copy boot.img to your pc (you can use adb or simply mount your sdcard and find the file we just created).
Don't delete this boot.img file, as it will be used if your kernel fails to boot on step 8!


7.b- Unpack your current boot.img to find a suitable ramdisk.gz using the following command:

perl split_bootimg.pl boot.img

and you should see something like this:

---------------------
Page size: 2048 (0x00000800)
Kernel size: 2781864 (0x002a72a8)
Ramdisk size: 154808 (0x00025cb8)
Second size: 0 (0x00000000)
Board name:
Command line: mem=212M console=null androidboot.hardware=zero
Writing boot.img-kernel ... complete.
Writing boot.img-ramdisk.gz ... complete.
---------------------

If it all went well, you should now have two new files: boot.img-kernel and boot.img-ramdisk.gz

Just pay attention to the line "Command Line" as you'll need those arguments for the 7c step!
This line changes if you're building a kernel for stock rom, or a kernel for cyanogenmod.


7.c- Now that you have your kernel (zImage) and a ramdisk file, you just have to build your own boot.img.

Just type the following command but pay attention to the arguments of "--cmdline". You should use the same as you got in step 7b.

mkbootimg --base 0x00200000 --cmdline 'mem=212M console=null androidboot.hardware=zero' --kernel /path/to/zImage --ramdisk boot.img-ramdisk.gz -o new_boot.img


Now your kernel is repacked with the ramdisk.gz in file new_boot.img


8- If you managed to get to this step, then you have everything you need to try your kernel in your Zero!

We will use fastboot to upload/flash our new_boot.img built in step 7c.

Just reboot your Zero in bootloader/fastboot mode and type:

fastboot flash boot /path/to/new_boot.img

Once it is done, just reboot your device:

fastboot reboot

Now wait for a few seconds.

If you see the Geeksphone white splash-screen, then all went good and you can jump to step 9.

Otherwise, if there's no Geeksphone white splash-screen, then your kernel has some kind of problem...

To ressuscitate your semi-dead Zero, just enter fastboot mode manually, and then flash the old boot.img you got in step 7a.

Enter fastboot manually:

a- remove battery for a few seconds and put it back in;
b- press Volume Up  for a second;
c- with Volume Up still pressed, press On/Off button for about 4 seconds;
d- release button On/Off, and then release Volume Up.

Flash the old boot.img (used in 7a):

fastboot flash boot /path/to/old/boot.img

fastboot reboot

And all should be OK.

Now you'll have to check your options/settings on step 5 and try to figure what went wrong.


--> I also would like some tips on how to properly debug a kernel in a device like our Zero as I'm new to this kind of "adventure", I don't really know how to do it without being trial and error which can be a big problem... <--


9- Note: In this step, I used the wifi module as an example, but if you have aditional modules, I believe the procedure is identical.

Now that your kernel is working, you'll probably find that your wifi won't switch on and will return an error.
That's because you're not using the module you compiled but using the old one.

So we'll just have to install the new module to the device, but first we'll backup the old one:

adb shell mount -o remount,rw /system
adb shell cp /system/lib/modules/dhd.ko /sdcard/kernel/dhd.ko
adb push geeksphone-kernel-zero/drivers/net/wireless/bcm4329/dhd.ko /system/lib/modules/dhd.ko
adb reboot


Wait for your Zero to boot, and you should now have a fully working system.

As this is my first tutorial, all suggestions are welcomed!
Hope this helps some of us, proud owners of Geeksphone Zero!

Offline kacer2k

  • Sr. Member
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 372
  • Karma: 8
  • Estudiante de telecomunicaciones en la UVA
    • View Profile
Re: Howto compile custom kernel
« Reply #1 on: June 01, 2011, 08:14:15 PM »
thanks alot for your tutorial. that's really great!!

Offline silverius

  • Sr. Member
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 293
  • Karma: 0
    • View Profile
Re: Howto compile custom kernel
« Reply #2 on: June 01, 2011, 11:46:46 PM »
Slayra, thanks... is a great tutorial.
GP1 rulando SuperAOSP 8.6

Offline aitorTheRed

  • Administrator
  • Top Member
  • *****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1631
  • Karma: 58
    • View Profile
    • Ich Weiss nicht
Re: Howto compile custom kernel
« Reply #3 on: June 02, 2011, 04:35:31 AM »
Great tutorial. I'll make it sticky for the ones who want to try it ;)
I'm not Geeksphone Staff. Only Forum Administrator.
"Los README son para los gallinas. Se valiente y ejecuta"
Follow me on twitter! http://twitter.com/#!/aitorTheRed

Offline n8fr8

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
  • Karma: 1
  • check it out https://guardianproject.info
    • View Profile
    • The Guardian Project
Re: Howto compile custom kernel
« Reply #4 on: June 08, 2011, 11:37:11 PM »
Thanks for the fantastic walkthrough. I've successfully built a new kernel, that I've enabled all of the iptables features for owner module, REDIRECT and more, to enable transparenty proxying via Tor ("Orbot" in the market).

You can get my kernel here if you'd like:
https://guardianproject.info/downloads/n8fr8-zero-kernel-cm-2.6.32.9-boot.img

or just view my config:
https://guardianproject.info/downloads/zero-boot-config-transproxy
open-source mobile security, privacy, anonymity and more at https://guardianproject.info

Offline richardski

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 25
  • Karma: 1
    • View Profile
    • RSTS
Re: Howto compile custom kernel
« Reply #5 on: October 15, 2012, 04:30:49 AM »
Hi n8fr8,

Very impressed with The Guardian Project.  I can see this as a very good fit for the GeeksPhone products:)

Are you able to update the below links please:

You can get my kernel here if you'd like:
https://guardianproject.info/downloads/n8fr8-zero-kernel-cm-2.6.32.9-boot.img

or just view my config:
https://guardianproject.info/downloads/zero-boot-config-transproxy

Many thanks,

Richard