Android开机启动流程1072.docx
Android的开机流程 1. 系统引引导boootloaader1) 源码:boottablee/boootloaader/*2) 说明:加电后,CCPU将先先执行boootlooaderr程序,此此处有三种种选择a) 开机按按Cameera+PPowerr启动到ffastbboot,即即命令或SSD卡烧写写模式,不不加载内核核及文件系系统,此处处可以进行行工厂模式式的烧写b) 开机按按Homee+Powwer启动动到reccoverry模式,加加载reccoverry.immg,reecoveery.iimg包含含内核,基基本的文件件系统,用用于工程模模式的烧写写c) 开机按按Poweer,正常常启动系统统,加载bboot.img,bboot.img包包含内核,基基本文件系系统,用于于正常启动动手机(以以下只分析析正常启动动的情况)2. 内核kkerneel1) 源码:kernnel/*2) 说明:kernnel由bbootlloadeer加载3. 文件系系统及应用用initt1) 源码:systtem/ccore/initt/*2) 配置文文件:syystemm/roootdirr/iniit.rcc,3) 说明:initt是一个由由内核启动动的用户级级进程,它它按照innit.rrc中的设设置执行:启动服务务(这里的的服务指llinuxx底层服务务,如addbd提供供adb支支持,voold提供供SD卡挂挂载等),执执行命令和和按其中的的配置语句句执行相应应功能4. 重要的的后台程序序zygoote1) 源码:frammeworrks/bbase/cmdss/appp_maiin.cppp等2) 说明:zygoote是一一个在innit.rrc中被指指定启动的的服务,该该服务对应应的命令是是/sysstem/bin/app_proccessa) 建立JJava Runttime,建建立虚拟机机b) 建立SSockeet接收AActivvityMMananngerSServiice的请请求,用于于Forkk应用程序序c) 启动SSysteem Seerverr5. 系统服服务sysstem servver1) 源码:frammeworrks/bbase/servvicess/javva/coom/anndroiid/seerverr/SysstemSServeer.jaava2) 说明:被zyggote启启动,通过过Systtem MManagger管理理andrroid的的服务(这这里的服务务指fraamewoorks/basee/serrvicees下的服服务,如卫卫星定位服服务,剪切切板服务等等)6. 桌面llaunccher1) 源码:ActiivityyManaagerSServiice.jjava为为入口,ppackaages/appss/lauuncheer*实现现2) 说明:系统启动动成功后SSysteemSerrver使使用xxxx.sysstemRReadyy()通知知各个服务务,系统已已经就绪,桌桌面程序HHome就就是在AcctiviityMaanageerSerrvicee.sysstemRReadyy()通知知的过程中中建立的,最最终调用 ()启llaunccher7. 解锁1) 源码:frammeworrks/ppoliccies/basee/phoone/ccom/aandrooid/iinterrnal/poliicy/iimpl/*locck*2) 说明:系统启动动成功后SSysteemSerrver调调用wm.systtemReeady()通知WWindoowMannagerrServvice,进进而调用PPhoneeWinddowMaanageer,最终终通过LoockPaatterrnKeyyguarrdVieew显示解解锁界面,跟跟踪代码可可以看到解解锁界面并并不是一个个Actiivityy,这是只只是向特定定层上绘图图,其代码码了存放在在特殊的位位置8. 开机自自启动的第第三方应用用程序1) 源码:frammeworrks/bbase/servvicess/javva/coom/anndroiid/seerverr/am/ActiivityyManaagerSServiice.jjava2) 说明:系统启动动成功后SSysteemSerrver调调用ActtivittyMannagerrNatiive.ggetDeefaullt().systtemReeady()通知AActivvityMManagger启动动成功,AActivvityMManagger会通通过置变量量mBoootingg,通知它它的另一线线程,该线线程会发送送广播anndroiid.inntentt.acttion.BOOTT_COMMPLETTED以告告知已注册册的第三方方程序在开开机时自动动启动。9. 总结综上所所述,系统统层次关于于启动最核核心的部分分是zyggote(即appp_proocesss)和syystemm serrver,zzygotte它负责责最基本的的虚拟机的的建立,以以支持各个个应用程序序的启动,而而systtem sserveer用于管管理anddroidd后台服务务,启动步步骤及顺序序。10. 参参考http:/Androoid 启启动过程详详解Androoid从LLinuxx系统启动动有4个步步骤;(1) iinit进进程启动(2) NNativve服务启启动(3) SSysteem Seerverr,Anddroidd服务启动动(4) HHome启启动总体启动框框架图如: 第一步:iinitiial进程程(sysstem/coree/iniit) initt进程,它它是一个由由内核启动动的用户级级进程。内内核自行启启动(已经经被载入内内存,开始始运行,并并已初始化化所有的设设备驱动程程序和数据据结构等)之之后,就通通过启动一一个用户级级程序innit的方方式,完成成引导进程程。iniit始终是是第一个进进程.Init.rcInit.marvvell.rcInit进进程一起来来就根据iinit.rc和iinit.xxx.rc脚本本文件建立立了几个基基本的服务务: servvicemmanammger zygoote 。最后Iniit并不退退出,而是是担当起ppropeerty servvice的的功能。1.1脚本本文件initSysttem/CCore/InittInit.c: pparsee_connfig_filee(Iniit.rcc) pparsee_connfig_filee(Iniit.maarvell.rc)解析脚本文文件:Innit.rrc和Innit.xxxxx.rc(硬硬件平台相相关)Init.rc是AAndrooid自己己规定的初初始化脚本本(Anddroidd Iniit Laanguaage, Systtem/CCore/Initt/reaadme.txt)该脚本包含含四个类型型的声明:Actioons Commaands Serviices Optioons. 1.2 服服务启动机机制我们来看看看Initt是这样解解析.rcc文件开启启服务的。(1)打开开.rc文文件,解析析文件内容容 syystemm/corre/innit/iinit.c将servvice信信息放置到到servvice_listt中。 systtem/ccore/initt parrser.c(2)reestarrt_seervicce() sysstem/coree/iniit/innit.cc servvice_starrt execcve().建立立servvice进进程。第二步 ZZygotte Servvicemmanagger和zzygotte进程就就奠定了AAndrooid的基基础。Zyygotee这个进程程起来才会会建立起真真正的Anndroiid运行空空间,初始始化建立的的Servvice都都是Navvtivee serrvicee.在.rrc脚本文文件中zyygotee的描述:serviice zzygotte /ssysteem/biin/appp_prrocesss -XXzygoote /systtem/bbin -zyggote -sttart-systtem-sserveer所以Zyggote从从mainn()frammeworrks/bbase/cmdss/appp_maiin.cppp开始。(1) mmain()fframeeworkks/baase/ccmds/app_mainn.cppp建立Javva Ruuntimme runtiime.sstartt("coom.anndroiid.innternnal.oos.ZyygoteeInitt", sstarttSysttemSeerverr); (2) rruntiime.sstarttAnddroiddRunttime.cpp建立虚拟机机 运行:coom.anndroiid.innternnal.oos.ZyygoteeInitt:maiin函数。 (3)maain()comm.anddroidd.intternaal.oss.ZyggoteIInit/正真的的Zygoote。regissterZZygotteSoccket();/登记Liistenn端口 starttSysttemSeerverr(); 进入Zyggote服服务框架。 经过这几个个步骤,ZZygotte就建立立好了,利利用Soccket通通讯,接收收ActiivityyManaangerrServvice的的请求,FFork应应用程序。第三步 SSysteem SeerverrstarttSysttemSeerverrcomm.anddroidd.intternaal.oss.ZyggoteIInit在在Zygoote上ffork了了一个进程程: coom.anndroiid.seerverr.SysstemSServeer.于是是SysttemSeerverr(SyystemmServver.jjava)就就建立了。AAndrooid的所所有服务循循环框架都都是建立SSysteemSerrver(SysstemSServeer.jaava)上上。在SyystemmServver.jjava中中看不到循循环结构,只只是可以看看到建立了了initt2的实现现函数,建建立了一大大堆服务,并并AddSServiice到sserviice MManagger。main() com/andrroid/servver/SSysteemSerrver initt1();Init11()是在在Natiive空间间实现的(ccom_aandoiird_sserveer_syystemmServver.ccpp)。我我们一看这这个函数就就知道了,iinit11->syystemm_iniit() Sysstem_initt.cppp在systtem_iinit()我们看看到了循环环闭合管理理框架。 Calll "coom/anndroiid/seerverr/SysstemSServeer", "iniit2" . ProccessSStatee:seelf()->sttartTThreaadPoool(); IPCTThreaadStaate:selff()->>joinnThreeadPoool();init22()SSysteemSerrver.javaa中建立了了Andrroid中中所有要用用到的服务务。这个iniit2()建建立了一个个线程,来来New Servvice和和AddSServiice来建建立服务第三步 HHome启启动在ServverThhreaddSysstemSServeer.jaava后半半段,我们们可以看到到系统在启启动完所有有的Anddroidd服务后,做做了这样一一些动作:(1) 使使用xxxx.sysstemRReadyy()通知知各个服务务,系统已已经就绪。(2) 特别对于于ActiivityyManaagerSServiice.ssysteemReaady(回回调) Widgget.wwallppaperr,immm(输入法法)等reeady通通知。Home就就是在AcctiviityMaanageerSerrvicee.sysstemRReadyy()通知知的过程中中建立的。下下面是AcctiviityMaanageerSerrvicee.sysstemRReadyy()的伪伪代码:systeemReaady()ActtivittyMannagerrServvice.javaa resuumeToopActtivittyLoccked() starrtHommeActtivittyLoccked();/如果是第第一个则启启动HommeActtivitty。 starrtActtivittyLoccked(。)CCATEGGORY_HOMEEAndrooid BBootiing:init is tthe ffirstt proocesss aftter kkerneel sttarteed. TThe ccorreesponndingg souurce codee liees inn: deevicee/sysstem/initt. Itt doees thhe foollowwing taskks sttep bby sttep: 1.Iniitiallize log systtem. 2.Parrse /initt.rc and /iniit.%hhardwware%.rc. 3. Exxecutte eaarly-initt acttion in tthe ttwo ffiless parrsed in sstep 2. 4. Deevicee speecifiic innitiaalizee. Foor exxamplle, mmake all deviice nnode in /dev and downnloadd firrmwarres. 5. Innitiaalizee proopertty syystemm. Acctuallly tthe ppropeerty systtem iis woorkinng ass a ssharee memmory. Loggicallly iit loooks likee a rregisstry undeer Wiindowws syystemm. 6. Exxecutte innit aactioon inn thee twoo filles pparseed inn steep 2. 7. Sttart proppertyy serrvicee. 8. Exxecutte eaarly-boott andd booot acctionns inn thee twoo filles pparseed inn steep 2. 9. Exxecutte prroperrty aactioon inn thee twoo filles pparseed inn steep 2. 10. EEnterr intto ann inddefinnite loopp to waitt forr devvice/proppertyy sett/chiild pproceess eexit evennts. For exammple, if an SSD caard iis plluginned, initt willl reeceivve a deviice aadd eeventt, soo it can makee nodde foor thhe deevicee. Moost oof thhe immporttant proccess is fforkeed inn iniit, sso iff anyy of themm craashedd, innit wwill receeive a SIIGCHLLD thhen ttransslatee it intoo a cchildd proocesss exiit evvent, so in tthe lloop initt cann hanndle the proccess exitt eveent aand eexecuute tthe ccommaands defiined in *.rc(it wwill run commmand onreestarrt). The .rc ffile is aa scrript filee deffinedd by Andrroid. Thee deffaultt is deviice/ssysteem/roootdiir/innit.rrc. WWe caan taake aa looot att thee fille foormatt(devvice/systtem/iinit/readdme.ttxt iis a goodd oveeralll inttroduuctioon off thee scrript). Baasicaally the scriipt ffile conttainss acttionss andd serrvicees. Actioons Actioons aare nnamedd seqquencces oof coommannds. Actiions havee a ttriggger wwhichh is usedd to deteerminne whhen tthe aactioon shhouldd occcur. Wheen ann eveent ooccurrs whhich matcches an aactioon's triggger, thaat acctionn is addeed too thee taiil off a tto-bee-exeecuteed quueue (unlless it iis allreaddy onn thee queeue). Each actiion iin thhe quueue is ddequeeued in ssequeence and eachh commmandd in thatt acttion is eexecuuted in ssequeence. Innit hhandlles ootherr acttivitties (devvice creaationn/desstrucctionn, prroperrty ssettiing, proccess resttartiing) "bettweenn" thhe exxecuttion of tthe ccommaands in aactivvitiees. Actioons ttake the formm of: on <ttriggger> <ccommaand> <ccommaand> <ccommaand> . Serviices Serviices are proggramss whiich iinit launnchess andd (opptionnallyy) reestarrts wwhen theyy exiit. Servvicess takke thhe foorm oof: serviice <<namee> <ppathnname>> <<arguumentt> * <ooptioon> <ooptioon> . Optioons Optioons aare mmodiffierss to servvicess. TThey affeect hhow aand wwhen initt runns thhe seervicce. Trigggers Trigggers are striings whicch caan bee useed too mattch ccertaain kkindss of evennts aand uused to ccausee an actiion tto occcur. The bbuilttin ssuppoortedd commmandds arre deefineed inn devvice/systtem/iinit/keywwordss.h. Commmandss aree impplemeentd in ddevicce/syystemm/iniit/buultinns.c. The iinit proggram onlyy exeecutees fiive kkindss of trigggerss: “eearlyy-iniit”, “iniit”, “earrly-bboot”, “booot”, “prroperrty:*”. TTake a loook aat thhe foollowwing linee in defaault initt.rc. classs_staart ddefauult This linee is a coommannd foor thhe acctionn corrresppondiing tto “bboot” triggger. It willl staart aall sserviices whosse cllass namee equuals to “ddefauult”. By defaault, if no cclasss opttion is ddefinned ffor aa serrvicee, thhe seervicces classs naame iis “ddefauult”. So thiss linne wiill sstartt alll thee serrvicees inn thee ordder oof poositiion iin thhe fiile bby deefaullt. (BTW, youu cann staart aany sserviice uusingg staart ccommaands, if you likee.) AAny sserviice iis ruun ass a fforkeed prrocesss off iniit, ttake a loook aat thhe soourcee codde off serrvicee_staart iin deevicee/sysstem/initt.c. So acccordding to tthe ddefauult iinit.rc, the folllowinng seervicces wwill be eexecuuted stepp by stepp: consoole: starr a sshelll. Thhe soourcee is in ddevicce/syystemm/binn/ashh. adbd: staart aadb ddaemoon. TThe ssourcce iss in deviice/ttoolss/adbbd. BBy deefaullt iss dissableed. serviicemaanageer: sstartt binnder systtem. The sourrce iis inn devvice/commmandss/binnder. mounttd: mmountt alll fs defiined in /systtem/eetc/mmounttd.coonf iif sttarteed, rreceiive ccommaands throough locaal soockett to mounnt anny fss. Thhe soourcee is in ddevicce/syystemm/binn/mouuntd. debugggerdd: sttart debuug syystemm. Thhe soourcee is in ddevicce/syystemm/binn/debbuggeerd. rild: staart rradioo intterfaace llayerr daeemon. Thee souurce is iin deevicee/commmandds/riind. zygotte: sstartt Anddroidd Javva Ruuntimme annd sttart systtem sserveer. IIts the mostt impportaant sserviice. The sourrce iis inn devvice/servvers/app. mediaa: sttart AudiioFliingerr, MeediaPPlayeerSerrvicee andd CammeraSServiice. The sourrce iis inn devvice/commmandss/meddiaseerverr. bootssoundd: pllay tthe ddefauult bboot sounnd /ssysteem/meedia/audiio/uii/booot.mpp3. TThe ssourcce iss in deviice/ccommaands/playymp3. dbus: staart ddbus daemmon, itss onlly ussed bby BllueZ. Thee souurce is iin deevicee/sysstem/Blueetootth/dbbus-ddaemoon. hcid: reddirecct hccidss stddout and stdeerr tto thhe Anndroiid loogginng syystemm. Thhe soourcee is in ddevicce/syystemm/binn/loggwrappper. By defaault is ddisabbled. hfag: staart BBluettoothh hanndsfrree aaudioo gattewayy, itts oonly usedd by BlueeZ. TThe ssourcce iss in deviice/ssysteem/Blluetoooth/blueez-uttils. By defaault is ddisabbled. hsag: staart BBluettoothh heaadsett auddio ggatewway, itss onlly ussed bby BllueZ. Thee souurce is iin deevicee/sysstem/Blueetootth/blluez-utills. BBy deefaullt iss dissableed. instaalld: staart iinstaall ppackaage ddaemoon. TThe ssourcce iss in deviice/sserveers/iinstaalld. flashh_reccoverry: lload /sysstem/recooveryy.imgg. Thhe soourcee is in ddevicce/coommannds/rrecovvery/mtduutilss. Zygotte seervicce dooes tthe ffolloowingg tassks sstep by sstep: 1. Crreatee JAVVA VMM. 2. Reegistter aandrooid nnativve fuunctiion ffor JJAVA VM. 3. Caall tthe mmain funcctionn in the JAVAA claass nnamedd comm.anddroidd.intternaal.oss.ZyggoteIInit whosse soourcee is deviice/jjava/andrroid/com/andrroid/inteernall/os/ZygooteInnit.jjava. a) LLoad ZygooteInnit cclasss b) RRegisster zygoote ssockeet c) LLoad prelload classses(the defaault filee is deviice/jjava/andrroid/prelloadeed-cllassees) d) LLoad prelload resoourcees e) CCall Zygoote:forkkSysttemSeerverr (immplemmenteed inn devvice/dalvvik/vvm/InnternnalNaativee.c) to ffork a neew prrocesss. IIn thhe neew prrocesss, ccall the mainn funnctioon inn thee JAVVA cllass nameed coom.anndroiid.seerverr.SysstemSServeer, wwhosee souurce is iin deevicee/javva/seervicces/ccom/aandrooid/sserveer. i. Loadd libbandrroid_servvers.so iii. CCall JNI natiive iinit11 funnctioon immplemmenteed inn devvice/libss/anddroidd_serrverss/comm_anddroidd_serrver_SysttemSeerverrs. IIt onnly ccallss sysstem_initt impplemeentedd in deviice/sserveers/ssysteem/liibrarry/syystemm_iniit.cppp. l If runnning on ssimullatorr, innstanntiatte AuudioFFlingger, MediiaPlaayerSServiice aand CCamerraSerrvicee herre. l Calll innit2 funcctionn in JAVAA claass nnamedd comm.anddroidd.serrver.SysttemSeerverr, whhose sourrce iis inn devvice/javaa/serrvicees/coom/anndroiid/seerverr. Thhis ffuncttion is vvery critticall forr Anddroidd beccausee it starrt alll off Anddroidd JAVVA seervicces. l If not runnning on ssimullatorr, caall IIPCThhreaddStatte:sself()->jjoinTThreaadPoool() to eenterr intto seervicce diispattcherr. SysteemSerrver:iniit2 wwill starrt a new threead tto sttart all JAVAA serrvicees ass folllowss: Core Servvicess: 1. Starrtingg Powwer MManagger /2. Creaatingg Acttivitty Maanageer 3. Starrtingg Tellephoony RRegisstry /4. Starrtingg Pacckagee Mannagerr /5. Set Actiivityy Mannagerr Serrvicee as Systtem PP