You are on page 1of 49

Porting Android to New Hardware

Android Builders Summit April 14 2011 Karim Yaghmour


karim.yaghmour@opersys.com @karimyaghmour
th

A out ...

Author o!"

#ntroduced $inu% &race &oolkit in 1''' (riginated Adeos and relay!s )kernel*relay.c+

1. ,omponents to port 2. ,ross-de.elopment toolchain /. 0orting the ootloader 4. 0orting the $inu% kernel 1. 2e.eloping de.ice dri.ers 3. 4etting the A(S0 5. #mplementing Android hard6are li s 7. ,ustomi8ing the user-space '. Building the A(S0 10. ,omponents to 6rite to !lash 11. 9se!ul :m edded $inu% tricks

1. ,omponents to port
CPU Architecture
GNU toolchain bootloader Kernel Bionic OSS package !al"ik Hardware lib ; ; ; ; ; ; ; ; ;

CPU Model
;

Target Board

; ;

2. ,ross-de.elopment toolchain

<ainly A=< 0re uilt toolchains"


,odersourcery $inaro crosstool-ng Buildroot 0&;dist (pen:m edded

Auto-generating a toolchain"

/. 0orting the ootloader

,heck aosp* oota le* ootloader*legacy


=:A2<: !ast oot>protocol.t%t include*?cpu@*A arch>?cpu@*A Ba.e your pick ...

,09 support"

Board support"

4. 0orting the $inu% kernel


=eCuirements Kernel Architecture Androidisms Dhich kernel to start !rom An intro to kernel source layout 9sing a E&A4 de ugger

4.1. =eCuirements

Kernel is loaded in =A< and run y ootloader Board schematics 0hysical memory map ,hip timings =ecei.ing proper oot parameters !rom ootloader

4.2. Kernel Architecture

4./. Androidisms

Dakelocks lo6mem handler Binder =A< console $ogger ...

4.4. Dhich kernel to start !rom

4oogle"

http"**android.git.kernel.org* http"**666.kernel.org Android kernel is a !ork Ho resolution in sight Cannot use .anilla kernel as-is ... 6akelocks $earn ho6 to use Igit re aseJ

Fanilla"

:ither 6ay ... youGre scre6ed"


4.1. An intro to kernel source layout


arch block Documentation drivers fs include init ipc kernel mm net scripts tools 112MB => 600KB => 17MB => 231MB => 31MB => 20MB => 150KB => 224KB => 4.7MB => 2.2MB => 20MB => 1.1MB => 2.1MB => architecture-dependent functionality block layer main kernel documentation all drivers virtual filesystem and all fs types complete kernel headers kernel startup code System V IPC core kernel code memory management networking core and protocols scripts used to build kernel misc. kernel-related tools

arch*
2.4M 29M 1.4M 5.3M 4.9M 1.4M 856K 4.6M 8.0K 1.4M 5.7M 1.1M 1.2M alpha arm avr32 blackfin cris frv h8300 ia64 Kconfig m32r m68k m68knommu microblaze 11M 1.7M 2.4M 13M 2.4M 636K 5.4M 4.7M 1.9M 1.9M 8.5M 1.4M mips mn10300 parisc powerpc s390 score sh sparc tile um x86 xtensa

arch*arm"
136K 208K 676K 1.1M 252K 96K 1.2M 808K 748K 308K 12K 60K 788K 76K ... boot common configs include lib mach-aaec2000 mach-at91 mach-bcmring mm nwfpe oprofile plat-iop plat-mxc plat-nomadik

dri.ers*
accessibility acpi amba ata atm auxdisplay base block bluetooth cdrom char clocksource connector cpufreq cpuidle crypto dca dio dma edac eisa firewire firmware gpio gpu hid hwmon i2c ide idle ieee802154 infiniband input isdn Kconfig leds lguest macintosh Makefile mca md media memstick message mfd misc mmc mtd net nubus of oprofile parisc parport pci pcmcia platform pnp power pps ps3 rapidio regulator rtc s390 sbus scsi serial sfi sh sn spi ssb staging tc telephony thermal tty uio usb uwb vhost video virtio vlynq w1 watchdog xen zorro

include*
acpi asm-generic config crypto drm Kbuild keys linux math-emu media mtd net pcmcia rdma rxrpc scsi sound trace video xen

$ooking !or something"


&ry grep Ba.e a look at the $inu% ,ross-=e!erencing proKect" 9=$" http"**l%r.linu%.no* ,ode" http"**l%r.source!orge.net* Ad.anced kernel searching*understanding" ,Scope" http"**cscope.source!orge.net* KScope !ront-end" http"**kscope.source!orge.net* :&A4S )emacs+

4.3. 9sing a E&A4 de ugger

Allo6s de ugging o!"


Bootloader :arly kernel code 2e.ice dri.ers A atron $auter ach 4reenBills So!t6are ...

Heed to !ind one that supports $inu% kernel"


1. 2e.eloping de.ice dri.ers

:.erything in 9ni% is a !ileL including de.ices 4et a copy o! $inu% 2e.ice 2ri.ersL /rd ed. B&DL emulator kernel doesnGt allo6 modules M") 9se standard $inu% model A0# &ry a.oiding 6akelocks in dri.ers 9se modules !or de.elopment Build dri.ers in 6hen you ship =emem er" kernel is 40$L dri.ers are ... NNN &ry using user-space Idri.ersJ !or proprietary parts Android actually promotes use o! user-space hard6are li s

1.1. 9ser space .s. kernel space

Separate address space"

Ho e%plicit re!erences to o Kects !rom other space Ho process can directly access or alter other processesG memory areas. Ho process can access anything inside the kernel 0rocesses that attempt die )seg!ault+

<emory protection amongst processes"

<emory protection et6een processes and kernel"


,rossing et6een user space and kernel space is through speci!ic e.ents

1.2. ,onnecting user-space and dri.ers

1./. &ypes o! dri.ers


,har Block Het Su system"


9SB <&2 Orame u!!er #nput

1.4. Kernel primiti.es


&iming #nterrupt handling and de!erral <emory management *sysL hotplugL etc. $ocking mechanisms Bard6are access ...

3. 4etting the A(S0


,ode-drop e.ery P3 months $ocation"

http"**android.git.kernel.org*
Q curl http"**android.git.kernel.org*repo M P* in*repo Q chmod aR% P* in*repo

4et IrepoJ"

Oetch the A(S0"


<ake sure you !etch a tagged release 4inger read"


Q repo init -u git"**android.git.kernel.org*plat!orm*mani!est.git - ginger read Q repo sync

3.1. A(S0 content


ionic oota le uild cts dal.ik de.elopment de.ice e%ternal !rame6orks hard6are li core ndk packages pre uilt sdk system , li rary replacement =e!erence ootloader Build system ,ompati ility &est Suite 2al.ik F< 2e.elopment tools 2e.ice-speci!ic !iles and components ,opy o! e%ternal proKects used y A(S0 System ser.icesL android.AL Android-related cmdsL etc. Bard6are support li s Apache Barmony &he H2K Stock Android appsL pro.idersL etc. 0re uilt inaries &he S2K pieces o! the 6orld that are the core o! the em edded linu% plat!orm at the heart o! Android.

3.2. 9se!ul pointers


See the uild system doc at source.android.com ,heck out de.ice* in A(S0 ,heck out ,yanogenmod ,heck out %da-de.elopers

5. #mplementing Android hard6are li s


Bluetooth 40S Di!i 2isplay Keymaps and Key oards $ights Backlight Key oard Buttons Battery Hoti!ications Attention Audio ,amera 0o6er <anagement Sensors Accelerometer <agnetic Oield (rientation 4yroscope $ight 0ressure &emperature 0ro%imity =adio $ayer #nter!ace BlueS through 2-B9S #0, )to a.oid 40$ contamination it seems+ <anu!acturer-pro.ided li gps.so 6pa>supplicant Std !rame u!!er dri.er )*de.*! 0+ Std input e.ent )*de.*e.ent0+ <anu!acturer-pro.ided li lights.so

<anu!acturer-pro.ided li audio.so )could use A$SA underneath ... at least as illustrated in their porting guide+ <anu!acturer-pro.ided li camera.so )could use F4$2 kernel dri.er underneath ... as illustrated in porting guide+ IDakelocksJ kernel patch <anu!acturer-pro.ided li sensors.so

<anu!acturer-pro.ided li ril-TcompanynameM-T=#$ .ersionM.so

7. ,ustomi8ing the user-space


Boot screen Status ar Het6ork 0reloaded apps Bro6ser ookmarks :mail pro.ider customi8ation &hemes Adding ne6 applications Adding ne6 ser.ices * ne6 hard6are type #nit

7.1. Boot screen


,reate /20%470 image #nstall imagemagick


Q sudo apt-get install imagemagick

,on.ert image to .r !ormat


Q sudo apt-get install imagemagick

,on.ert image to 131 !ormat


Q rg 2131 T screen.rg M screen.131

Drite image to !lash


Q !ast oot !lash splash1 screen.131

7.2. Status ar

$ocation"

!rame6orks* ase*packages*System9#*src*com*android*systemui*status ar

$ook !or"

mSer.ice.set#con)...+ mSer.ice.set#conFisi ility)I?#,(H>HA<:@UL !alse+V

2isa le icons 6ith"

7./. Het6ork

$ocations"

4lo al static"

!rame6orks* ase*core*res*res*%ml*apns.%ml
0=(29,&>,(0Y>O#$:S "W .endor*acme*etc*apns-con!-us.%ml"system*etc*apns-con!.%ml

2e.ice static"

2ynamic"

system*etc*apns-con!.%ml

Oormat"
Tapn carrierWU&-<o ile 9SU mccWU/10U mncWU230U apnWU 6ap..oicestream.comU userWUnoneU ser.erWUAU pass6ordWUnoneU pro%yWU 213.111.131.10U portWU7070U mmscWUhttp"**213.111.154.74*ser.lets*mmsU *M

7.4. 0reloaded apps

See uild*target*products
0=(29,&>0A,KA4:S "W X ouncycastle X com.android.location.pro.ider X com.android.location.pro.ider.%ml X core X core-Kunit X create>test>dmtrace X dal.ik.m X de%deps X ...

7.1. Bro6ser ookmarks

See packages*apps*Bro6ser*res*.alues*strings.%ml
TY-- Bookmarks --M Tstring-array nameWU ookmarksUM TitemM4oogleT*itemM TitemMhttp"**666.google.com*T*itemM TitemMYahooYT*itemM TitemMhttp"**666.yahoo.com*T*itemM TitemM<SHT*itemM TitemMhttp"**666.msn.com*T*itemM TitemM<ySpaceT*itemM TitemMhttp"**666.myspace.com*T*itemM ...

7.3. :mail pro.ider customi8ation

See packages*apps*:mail*res*%ml*pro.iders.%ml
TY-- 4mail .ariants --M Tpro.ider idWUgmailU la elWU4mailU domainWUgmail.comUM Tincoming uriWUimapRsslR"**imap.gmail.comU usernameWUQemailU*M Toutgoing uriWUsmtpRsslR"**smtp.gmail.comU usernameWUQemailU*M T*pro.iderM Tpro.ider idWUgooglemailU la elWU4oogle <ailU domainWUgooglemail.comUM Tincoming uriWUimapRsslR"**imap.googlemail.comU usernameWUQemailU*M Toutgoing uriWUsmtpRsslR"**smtp.googlemail.comU usernameWUQemailU*M T*pro.iderM ... TY-- ,ommon 9S pro.iders --M Tpro.ider idWUaimU la elWUA#<U domainWUaim.comUM Tincoming uriWUimap"**imap.aim.comU la elWU#<A0U usernameWUQemailU*M Toutgoing uriWUsmtp"**smtp.aim.com"175U usernameWUQemailU*M T*pro.iderM Tpro.ider idWUaolU la elWUA($U domainWUaol.comUM Tincoming uriWUimap"**imap.aol.comU la elWU#<A0U usernameWUQemailU*M Toutgoing uriWUsmtp"**smtp.aol.com"175U usernameWUQemailU*M T*pro.iderM ...

7.5. &hemes

See !rame6ork* ase*core*res*res*.alues*styles.%ml

7.7. Adding ne6 applications


Add application in packages*apps ,an use :clipse to create initial .ersion ,opy :clipse proKect to packages*apps Add an appropriate Android.mk !ile to proKect Add proKect to 0=(29,&>0A,KA4:S

7.'. Adding ne6 ser.ices * ne6 hard6are type

Add your code to"

!rame6orks* ase*ser.ices*Ka.a*com*android*ser.er*

Ba.e the SystemSer.er.Ka.a initRreg. your ser.ice 2e!ine hard6are A0# !or apps :%pose through"

!rame6orks* ase*core*Ka.a*android*os*?ser.er@.aidl

,all on nati.e Idri.erJ code through EH# #mplement or connect to appropriate dri.er ,reate an app that calls on ser.ice <ay need to create ne6 S2K ...

7.10. #nit

Android init semantics are di!!erent !rom"


System F init Busy o% init

See IAndroid #nit $anguageJ doc in porting guide See init.rc e%amples"

:mulatorGs init.rc de.ice*?manu!acturer@*?de.ice@*init.rc

4lo al IpropertiesJ that can e set and read ,an e used to t6eak lo6-memory conditions

'. Building the A(S0


=eCuires 34- it 9 untu 10.04 0ackages reCuired" Q sudo apt-get install ia/2-li s ison !le% gper! X M gRR li ia/2 li c3-de.-i/73 li 8-de. li stdcRR X M li stdcRR3 li stdcRR3-/2 ia/2-li stdcRR3 X M ia/2-li stdcRR ia/2-li stdcRR1 ia/2-li s X M li ncurses-de. li /2ncurses-de. X M ia/2-li ncurses-de. ia/2-li ncurses li /2ncurses X M li /2ncurses1-de. 0atch uild*core*droiddoc.mk

https"**groups.google.com*group*androiduilding* ro6se>thread*thread*7//a711'a0e1c13c

Oi% a !e6 sym olic links"


Q sudo ln -s *usr*li /2*li stdcRR.so.3 *usr*li /2*li stdcRR.so Q sudo ln -s *usr*li /2*li 8.so.1 *usr*li /2*li 8.so

Set up uild en.ironment"


Q . uild*en.setup.sh Q lunch

$aunch uild and go 6atch tonightGs hockey game"


Q make -K2

... though you should check your screen at reaks ... Eust launch emulator 6hen itGs done"
Q emulator Z

Some nice tricks"


See uild*en.setup.sh !or commands 9se IlunchJ !rom A(S0 root to set en. .ars

YouGll need that i! you come ack later and 6ant to relaunch emulator !rom A(S0 root.

10. ,omponents to 6rite to !lash


See out*target*product*?product@*A.img &ypically"


Bootloader oot )kernel and ramdisk+ system )*system+ userdata )*data+

11. 9se!ul :m edded $inu% tricks


crosstool-ng Busy o% u,li c

11.1. crosstool-ng

,ross-de.elopment toolchain generator Successor to crosstool A.aila le at" http"**ymorin.is-a-geek.org*proKects*crosstool 2o6nloadsL patchesL uildsL installsL etc. ,omprises #$ steps <enucon!ig- ased Supports u,li cL gli c and egli c Supports A=<L Black!inL <#0SL 0o6er0,L SBL [ Oairly 6ell maintained

11.2. Busy o%

=eplicate $inu% ,$# e%perience


?L ??L acpidL add-shellL addgroupL adduserL adKtime%L arpL arpingL ashL a6kL ase34L asenameL eepL lkidL lockde.L ootchartdL rctlL un8ip2L 8catL 8ip2L calL catL cat.L chatL chattrL chgrpL chmodL cho6nL chpass6dL chpstL chrootL chrtL ch.tL cksumL clearL cmpL commL cpL cpioL crondL cronta L cryptp6L cttyhackL cutL dateL dcL ddL dealloc.tL delgroupL deluserL depmodL de.memL d!L dhcprelayL di!!L dirnameL dmesgL dnsdL dnsdomainnameL dos2uni%L duL dumpkmapL dumpleasesL echoL edL egrepL eKectL en.L en.dirL en.uidgidL ether-6akeL e%pandL e%prL !akeidentdL !alseL ! setL ! splashL !d!lushL !d!ormatL !diskL !gconsoleL !grepL !indL !ind!sL !lockL !oldL !reeL !reeramdiskL !sckL !sck.mini%L !syncL !tpdL !tpgetL !tpputL !userL getoptL gettyL grepL gun8ipL g8ipL haltL hdL hdparmL headL he%dumpL hostidL hostnameL httpdL hushL h6clockL idL i!con!igL i!do6nL i!ensla.eL i!plugdL i!upL inetdL initL insmodL installL ioniceL iostatL ipL ipaddrL ipcalcL ipcrmL ipcsL iplinkL iprouteL ipruleL iptunnelL k d>modeL killL killallL killall1L klogdL lastL lengthL lessL linu%/2L linu%34L linu%rcL lnL load!ontL loadkmapL loggerL loginL lognameL logreadL losetupL lpdL lpCL lprL lsL lsattrL lsmodL lspciL lsus L l8catL l8maL l8opL l8opcatL makede.sL makemimeL manL md1sumL mde.L mesgL microcomL mkdirL mkdos!sL mke2!sL mk!i!oL mk!s.e%t2L mk!s.mini%L mk!s..!atL mknodL mkpass6dL mks6apL mktempL modin!oL modpro eL moreL mountL mountpointL mpstatL mtL m.L namei!L n d-clientL ncL netstatL niceL nmeterL nohupL nslookupL ntpdL odL open.tL pass6dL patchL pgrepL pido!L pingL ping3L pipe>progressL pi.ot>rootL pkillL pmapL popmaildirL po6ero!!L po6ertopL printen.L print!L psL pscanL p6dL raidautorunL rdateL rde.L readaheadL readlinkL readpro!ileL realpathL re ootL re!ormimeL remo.e-shellL reniceL resetL resi8eL re.L rmL rmdirL rmmodL routeL rpmL rpm2cpioL rtc6akeL run-partsL runle.elL runs.L runs.dirL r%L scriptL scriptreplayL sedL sendmailL seCL setarchL setconsoleL set!ontL setkeycodesL setlogconsL setsidL setuidgidL shL sha1sumL sha213sumL sha112sumL sho6keyL slattachL sleepL smemcapL so!tlimitL sortL splitL start-stop-daemonL statL stringsL sttyL suL suloginL sumL s.L s.logdL s6apo!!L s6aponL s6itch>rootL syncL sysctlL syslogdL tacL tailL tarL tcps.dL teeL telnetL telnetdL testL t!tpL t!tpdL timeL timeoutL topL touchL trL tracerouteL traceroute3L trueL ttyL ttysi8eL tunctlL udhcpcL udhcpdL udps.dL umountL unameL une%pandL uniCL uni%2dosL unl8maL unl8opL un%8L un8ipL uptimeL usleepL uudecodeL uuencodeL .con!igL .iL .lockL .olnameL 6allL 6atchL 6atchdogL 6cL 6getL 6hichL 6hoL 6hoamiL %argsL %8L %8catL yesL 8catL 8cip

Some !eatures o! interest"


color-coded !ile lists ta completion UhomeUL UendU grepL sedL 6cL moreL less .i i!con!ig httpd sendmail t!tp top ...

2o6nload BusyBo% )1.17./+ <o.e to the directory !or the rest o! the setup"
$ cd busybox-1.18.3

,on!iguration o! BusyBo%Gs options"


$ make menuconfig

(ptions that must e set"

IBuild (ptionsJ -M I2o you 6ant to uild BusyBo% 6ith a ,ross ,ompilerNJ ,ross-compiler pre!i%" arm-unknown-linux-gnueabiInstallation Options -> I2onGt use *usrJ

#nstallation pre!i%" ${PRJROOT}/rootfs

Build"
$ make

#nstall"
$ make install

,heat sheet" ,ommands to get the ne6 Busy o% onto the root!s"
$ $ $ $ $ adb adb adb adb adb shell mount -o remount,rw rootfs / shell mkdir /bin push busybox /bin/ shell /bin/busybox --install /bin shell

&o do a!ter going into the shell"


# /bin/ash # export PATH=/bin:$PATH

11./. u,li c

(riginates !rom u,linu% e!!ort Support oth ,09s that ha.e and those that lack an <<9 and*or an O09. Allo6s oth static and dynamic linking <ost applications that uild 6ith gli c 6ill uild and 6ork the same 6ith u,li c. A.aila le !rom" http"**ucli c.org*

Thank %ou &&&

karim.yaghmour@opersys.com

666.opersys.com

You might also like