You are on page 1of 6

1/10/2016

Building the Android kernel on Linux - Softwarebakery

Softwarebakery
Blog

DriveDroid

WiiDeviceLibrary

Donate

Contact

RSS

BuildingtheAndroidkernelonLinux
byFrozenCow
published:SaturdayDecember19,2014

Whenyouwanttoaddextrafunctionalitytoyourkernel,youwanttodevelopyourownfunctionalityor
justwanttohaveyourownversionofthekernel,youwillwanttobuildkernelcode.
Inthistutorialwewillgothroughthewholeprocessofretrieving,compiling,(optionally)patchingand
flashing kernels that are based on AOSP. As an example we will use the kernel of the Nexus 5
(hammerhead). We'll presume this is all done on a Debian or Ubuntu machine, since that is most
popularwhilestillpractical.

Requirements
OnDebian/Ubuntuyouneedthefollowingpackages:
git
buildessential
abootimg
adbandfastboot
Installtheseusing:
sudo apt-get install git build-essential abootimg

AndinstalltheAndroidSDKasdescribedheretogetadbandfastboot.

Retrievingthekernel
Check what kernel your phone needs on https://source.android.com/source/building
kernels.html.
Inthisexamplewe'llbuildthekernelfortheNexus5.ThecodenameoftheNexus5is hammerhead,
which is listed in the table under 'Figuring out which kernel to build'. Take note of the 'Build
configuration' (in this example: hammerhead_defconfig) and 'Source location' (in this example:
kernel/msm).Weseethesecomingbackinlatersteps.

Next we look up what exact version of the kernel we want to retrieve. We open up Google's git
repositoryofyourdeviceonlinebygoingtothefollowingURL:
https://android.googlesource.com/kernel/msm
Notethatyouneedtoreplace kernel/msmwiththe'Sourcelocation'ofyourdevice.
http://softwarebakery.com/building-the-android-kernel-on-linux

1/6

1/10/2016

Building the Android kernel on Linux - Softwarebakery

Onthatpageyoucanlookunder'Branches'toseethedifferentversionsavailable.Inthisexamplewe
willbeusing android-msm-hammerhead-3.4-lollipop-release.
NextweretrievethekernelfromGooglebyusingthefollowing:

git clone https://android.googlesource.com/kernel/msm --branch android-msm-hammerhead-3.4-loll

Note that you need to replace kernel/msm with the 'Source location' of your device and replace
android-msm-hammerhead-3.4-lollipop-releasewiththebranchofyourdevice.

This retrieves all source code (and its history) from Google and places it all under a new directory
called linux.

Patchingthekernel
Thisstepisn'tneededifyoujustwanttobuildyourownkernel.ContinuewithCompilingthekernelif
that'sthecase.
Anoteonpatches:patchescomeinallkindsofforms.Therearefiles( .patchor .diff)thatsimply
contain changes to certain lines in certain files. Patches are also embedded in version control
systems,likeGit.Therearedifferentwaystoretrievethemanddifferentwaystoapplythem.
Secondly,patcheswon'talwaysfitperfectlyontoyourcurrentcode.Whenapatchcannotbeapplied
to your kernel means that there have been changes made in sections that the patch also touches.
Thesearecalledconflicts.Theseconflictsmustbesolvedbytheuser.Wewillnotcoverconflictsin
thistutorial,butyoucanfindmoreinformationonconflictsonGithub.
In this example we will apply a patch that was submitted to CyanogenMod to add CDrom
functionality to DriveDroid on Nexus 5 devices/kernels. The patch can be found on CyanogenMods
codereviewingsystem:http://review.cyanogenmod.org/#/c/57792/
Firstchangethecurrentdirectorytothekernel:
cd linux

Nowwecanapply gitcommandsonthesourcecodeofLinux.
OnthepageofthepatchonCyanogenModscodereviewingsystemyou'llfindafewoptionsto
downloadthepatch:checkout,pull,cherrypickandpatch.Wewillusethecherrypickoption,sinceit
is most straightforward for our purpose. Click cherrypick and copy the command that shows up
underit.Inthisexamplethecommandisthefollowing:

git fetch http://review.cyanogenmod.org/CyanogenMod/android_kernel_lge_hammerhead refs/changes

Executethiscommand.Itresultsinthefollowingoutput:

http://softwarebakery.com/building-the-android-kernel-on-linux

2/6

1/10/2016

Building the Android kernel on Linux - Softwarebakery

From http://review.cyanogenmod.org/CyanogenMod/android_kernel_lge_hammerhead
* branch refs/changes/92/57792/2 -> FETCH_HEAD
[android-msm-hammerhead-3.4-lollipop-release 3f1a563] usb: gadget: mass_storage: added sysfs e
Date: Thu Jul 4 12:36:57 2013 +0200
2 files changed, 42 insertions(+)

Thismeansthepatchhasbeenapplied.Changethecurrentdirectorybacktotheparenttocontinue
thetutorial:
cd ..

Compilingthekernel
SincemostphonesandtabletsrunonanARMprocessor,weneedacompilerthattargetsARM.It's
besttousetheexactsamecompilerGoogleuses,sowe'llretrievethatusingthefollowing:

git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/arm/arm-eabi-4.6 a

Nextweneedtoinstructthebuildprocesstousethecompiler:
export PATH=$PWD/arm-eabi-4.6/bin:$PATH
export ARCH=arm
export SUBARCH=arm
export CROSS_COMPILE=arm-eabi-

NowweneedtoconfigureLinux,likewhatmodulesitshouldinclude.Inthiscasewewantjustthe
defaultconfigurationfortheNexus5(hammerhead):
make -C linux hammerhead_defconfig

Finallywecancompilethekernel:
make -C linux

This will take a while. When compilation has finished, the kernel is available under
linux/arch/arm/boot/zImage-dtb.

Preparingabootimage
We have the kernel. Next we need to package it up into a bootloader image. A bootloader image
usuallyconsistsof:
Akernelimage
Aramdiskimage
Akernelcommandline
Wealreadyhavethekernelimage,buttheramdiskandkernelcommandlinearealsorequired.These
aredependentontheromyou'reusing.So,weusethesamebootloaderimagefromyourromand
replacethekernel.
http://softwarebakery.com/building-the-android-kernel-on-linux

3/6

1/10/2016

Building the Android kernel on Linux - Softwarebakery

Firstretrievethefactoryimagefrom:https://developers.google.com/android/nexus/images
You can use your own methods to download and extract the image. In this example we use
commandlinetoolstodothis.
In this example we'll presume you're using hammerhead-lrx21o-factory-01315e08.tgz. To
downloadituse:
wget https://dl.google.com/dl/android/aosp/hammerhead-lrx21o-factory-01315e08.tgz

Nextextract image-hammerhead-lrx21o.zipfrom hammerhead-lrx21o-factory-01315e08.tgz:


tar xfz hammerhead-lrx21o-factory-01315e08.tgz image-hammerhead-lrx21o.zip

Andnowextract boot.imgfrom image-hammerhead-lrx21o.zip:


unzip image-hammerhead-lrx21o.zip boot.img

We have an Android boot image which contains a stock kernel, ramdisk and commandline. We
extractalloftheseusing:
abootimg -x boot.img

Thisresultsinthefollowingfiles:
bootimg.cfg:configurationwithaddresses,sizesandthekernelcommandline.
zImage:thestockkernel
initrd.img:thestockramdisk

Wewillusethesameconfigurationfileandramdisk.
We do need to make a small change to the configuration though. I found that my compiled kernel
image was bigger than the stock kernel. The size of the kernel is set in the configuration file
( bootimg.cfg)tothesizeofthestockkernel,sothisneedstochange.Since abootcandetermine
thesizeitselfwhenit'spackingeverythingup,wecanjustremovethebootsizefromtheconfiguration.
Open bootimg.cfgwithyourfavoriteeditorandremovethelinewith bootsize =.Alternativelyuse
thefollowinglinetodothesame:
sed -i '/bootsize =/d' bootimg.cfg

Nowwecancreateourcustombootimage.Wenameit newboot.imgusingthefollowingline:
abootimg --create newboot.img -f bootimg.cfg -k linux/arch/arm/boot/zImage-dtb -r initrd

Yay,wenowhaveanimagethatwecanflashontothephone/tablet.

Bootusingbootimage
Totrythebootimageandkernelyouneedtorebootintoyourbootloaderonyourphone/tablet(aka
fastboot mode). You can usually do this by shutting down your phone and holding down Volume
http://softwarebakery.com/building-the-android-kernel-on-linux

4/6

1/10/2016

Building the Android kernel on Linux - Softwarebakery

Downandthepowerbutton.Alternativelyyoucanuse:
adb reboot bootloader

Youshouldseesomelinesoftextstartingwith FASTBOOT MODE.Nowyoucanbootyourphonewith


yournewbootimage:
fastboot boot newboot.img

Trywhetherthingsworkcorrectly.Ifitdoes,youcanmakethebootimagepermanentbyflashingit
ontoyourphone.

Flashbootimage
Tomakethebootimagepermanentyoucanagainreboottoyourbootloader:
adb reboot bootloader

Andflashthebootimage:
fastboot flash boot newboot.img

Thenexttimeyourebootyourphoneit'llstartusingyourowncompiledkernel.Havefun!

http://softwarebakery.com/building-the-android-kernel-on-linux

5/6

1/10/2016

Building the Android kernel on Linux - Softwarebakery

2Comments
Recommend

Softwarebakery

Share

Login

SortbyBest

Jointhediscussion
21daysago

imeiadblog

Reply Share

21daysago

Reply Share

WHAT'STHIS?

ALSOONSOFTWAREBAKERY

WindowsinstallationonDriveDroid

DriveDroid

76comments3yearsago

169comments3yearsago

MallyRedItsaysnobootabledevice.

FrozenCowYoucandownloadthe

I'musingHTConem8andelementalx
kernelandskydragonrom

APKsalready:
http://softwarebakery.com/apps...
DriveDroidjustisn'topensourceatthe

CopyrunningLinuxsystemstoother
machinesovernetwork
2comments3yearsago

RussianBearSimple&easy.Thanks

Subscribe

InstallHirensBootCDonDriveDroid
9comments3yearsago

GuestOnlydownsideaboutthis

walkthroughisthatforthoseofuswithout
akernelsupportingr/wmounting,we're
unabletousethismethod.I'dlovetosee

AddDisqustoyoursiteAddDisqusAdd

http://softwarebakery.com/building-the-android-kernel-on-linux

Privacy

6/6

You might also like