欢迎来到淘文阁 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
淘文阁 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

    Android 开机启动流程.doc

    • 资源ID:52735093       资源大小:141KB        全文页数:13页
    • 资源格式: DOC        下载积分:20金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要20金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    Android 开机启动流程.doc

    Android的开机流程 1.    系统引导bootloader1)        源码:bootable/bootloader/*2)        说明:加电后,CPU将先执行bootloader程序,此处有三种选择a)        开机按Camera+Power启动到fastboot,即命令或SD卡烧写模式,不加载内核及文件系统,此处可以进行工厂模式的烧写b)        开机按Home+Power启动到recovery模式,加载recovery.img,recovery.img包含内核,基本的文件系统,用于工程模式的烧写c)        开机按Power,正常启动系统,加载boot.img,boot.img包含内核,基本文件系统,用于正常启动手机(以下只分析正常启动的情况)2.    内核kernel1)        源码:kernel/*2)        说明:kernel由bootloader加载3.    文件系统及应用init1)        源码:system/core/init/*2)        配置文件:system/rootdir/init.rc,3)        说明:init是一个由内核启动的用户级进程,它按照init.rc中的设置执行:启动服务(这里的服务指linux底层服务,如adbd提供adb支持,vold提供SD卡挂载等),执行命令和按其中的配置语句执行相应功能4.    重要的后台程序zygote1)        源码:frameworks/base/cmds/app_main.cpp等2)        说明:zygote是一个在init.rc中被指定启动的服务,该服务对应的命令是/system/bin/app_processa)        建立Java Runtime,建立虚拟机b)        建立Socket接收ActivityManangerService的请求,用于Fork应用程序c)        启动System Server5.    系统服务system server1)        源码:frameworks/base/services/java/com/android/server/SystemServer.java2)        说明:被zygote启动,通过System Manager管理android的服务(这里的服务指frameworks/base/services下的服务,如卫星定位服务,剪切板服务等)6.    桌面launcher1)        源码:ActivityManagerService.java为入口,packages/apps/launcher*实现2)        说明:系统启动成功后SystemServer使用xxx.systemReady()通知各个服务,系统已经就绪,桌面程序Home就是在ActivityManagerService.systemReady()通知的过程中建立的,最终调用 ()启launcher7.    解锁1)        源码:frameworks/policies/base/phone/com/android/internal/policy/impl/*lock*2)        说明:系统启动成功后SystemServer调用wm.systemReady()通知WindowManagerService,进而调用PhoneWindowManager,最终通过LockPatternKeyguardView显示解锁界面,跟踪代码可以看到解锁界面并不是一个Activity,这是只是向特定层上绘图,其代码了存放在特殊的位置8.    开机自启动的第三方应用程序1)        源码:frameworks/base/services/java/com/android/server/am/ActivityManagerService.java2)        说明:系统启动成功后SystemServer调用ActivityManagerNative.getDefault().systemReady()通知ActivityManager启动成功,ActivityManager会通过置变量mBooting,通知它的另一线程,该线程会发送广播android.intent.action.BOOT_COMPLETED以告知已注册的第三方程序在开机时自动启动。9.    总结综上所述,系统层次关于启动最核心的部分是zygote(即app_process)和system server,zygote它负责最基本的虚拟机的建立,以支持各个应用程序的启动,而system server用于管理android后台服务,启动步骤及顺序。10. 参考Android 启动过程详解Android从Linux系统启动有4个步骤;(1) init进程启动(2) Native服务启动(3) System Server,Android服务启动(4) Home启动总体启动框架图如: 第一步:initial进程(system/core/init)     init进程,它是一个由内核启动的用户级进程。内核自行启动(已经被载入内存,开始运行,并已初始化所有的设备驱动程序和数据结构等)之后,就通过启动一个用户级程序init的方式,完成引导进程。init始终是第一个进程.Init.rcInit.marvell.rcInit进程一起来就根据init.rc和init.xxx.rc脚本文件建立了几个基本的服务: servicemanamger  zygote 。最后Init并不退出,而是担当起property service的功能。1.1脚本文件initSystem/Core/InitInit.c: parse_config_file(Init.rc)  parse_config_file(Init.marvel.rc)解析脚本文件:Init.rc和Init.xxxx.rc(硬件平台相关)Init.rc是Android自己规定的初始化脚本(Android Init Language, System/Core/Init/readme.txt)该脚本包含四个类型的声明:Actions Commands Services Options. 1.2 服务启动机制我们来看看Init是这样解析.rc文件开启服务的。(1)打开.rc文件,解析文件内容 system/core/init/init.c将service信息放置到service_list中。 system/core/init parser.c(2)restart_service() system/core/init/init.c service_start execve().建立service进程。第二步 Zygote Servicemanager和zygote进程就奠定了Android的基础。Zygote这个进程起来才会建立起真正的Android运行空间,初始化建立的Service都是Navtive service.在.rc脚本文件中zygote的描述:service zygote /system/bin/app_process -Xzygote /system/bin -zygote -start-system-server所以Zygote从main()frameworks/base/cmds/app_main.cpp开始。(1) main()frameworks/base/cmds/app_main.cpp建立Java Runtime runtime.start("com.android.internal.os.ZygoteInit", startSystemServer); (2) runtime.startAndroidRuntime.cpp建立虚拟机 运行:com.android.internal.os.ZygoteInit:main函数。 (3)main()com.android.internal.os.ZygoteInit/正真的Zygote。registerZygoteSocket();/登记Listen端口 startSystemServer(); 进入Zygote服务框架。 经过这几个步骤,Zygote就建立好了,利用Socket通讯,接收ActivityManangerService的请求,Fork应用程序。第三步 System ServerstartSystemServercom.android.internal.os.ZygoteInit在Zygote上fork了一个进程: com.android.server.SystemServer.于是SystemServer(SystemServer.java)就建立了。Android的所有服务循环框架都是建立SystemServer(SystemServer.java)上。在SystemServer.java中看不到循环结构,只是可以看到建立了init2的实现函数,建立了一大堆服务,并AddService到service Manager。main() com/android/server/SystemServer init1();Init1()是在Native空间实现的(com_andoird_server_systemServer.cpp)。我们一看这个函数就知道了,init1->system_init() System_init.cpp在system_init()我们看到了循环闭合管理框架。 Call "com/android/server/SystemServer", "init2" . ProcessState:self()->startThreadPool();    IPCThreadState:self()->joinThreadPool();init2()SystemServer.java中建立了Android中所有要用到的服务。这个init2()建立了一个线程,来New Service和AddService来建立服务第三步 Home启动在ServerThreadSystemServer.java后半段,我们可以看到系统在启动完所有的Android服务后,做了这样一些动作:(1) 使用xxx.systemReady()通知各个服务,系统已经就绪。(2)  特别对于ActivityManagerService.systemReady(回调) Widget.wallpaper,imm(输入法)等ready通知。Home就是在ActivityManagerService.systemReady()通知的过程中建立的。下面是ActivityManagerService.systemReady()的伪代码:systemReady()ActivityManagerService.java resumeTopActivityLocked() startHomeActivityLocked();/如果是第一个则启动HomeActivity。 startActivityLocked(。)CATEGORY_HOMEAndroid Booting:init is the first process after kernel started. The corresponding source code lies in: device/system/init. It does the following tasks step by step: 1.Initialize log system. 2.Parse /init.rc and /init.%hardware%.rc. 3. Execute early-init action in the two files parsed in step 2. 4. Device specific initialize. For example, make all device node in /dev and download firmwares. 5. Initialize property system. Actually the property system is working as a share memory. Logically it looks like a registry under Windows system. 6. Execute init action in the two files parsed in step 2. 7. Start property service. 8. Execute early-boot and boot actions in the two files parsed in step 2. 9. Execute property action in the two files parsed in step 2. 10. Enter into an indefinite loop to wait for device/property set/child process exit events. For example, if an SD card is plugined, init will receive a device add event, so it can make node for the device. Most of the important process is forked in init, so if any of them crashed, init will receive a SIGCHLD then translate it into a child process exit event, so in the loop init can handle the process exit event and execute the commands defined in *.rc(it will run command onrestart).   The .rc file is a script file defined by Android. The default is device/system/rootdir/init.rc. We can take a loot at the file format(device/system/init/readme.txt is a good overall introduction of the script). Basically the script file contains actions and services. Actions Actions are named sequences of commands. Actions have a trigger which is used to determine when the action should occur.  When an event occurs which matches an action's trigger, that action is added to the tail of a to-be-executed queue (unless it is already on the queue). Each action in the queue is dequeued in sequence and each command in that action is executed in sequence.  Init handles other activities (device creation/destruction, property setting, process restarting) "between" the execution of the commands in activities. Actions take the form of: on <trigger>    <command>    <command>    <command> . Services Services are programs which init launches and (optionally) restarts when they exit.  Services take the form of: service <name> <pathname> <argument> *    <option>    <option>    . Options Options are modifiers to services.  They affect how and when init runs the service.   Triggers Triggers are strings which can be used to match certain kinds of events and used to cause an action to occur.   The builtin supported commands are defined in device/system/init/keywords.h. Commands are implementd in device/system/init/bultins.c.   The init program only executes five kinds of triggers: “early-init”, “init”, “early-boot”, “boot”, “property:*”. Take a look at the following line in default init.rc. class_start default This line is a command for the action corresponding to “boot” trigger. It will start all services whose class name equals to “default”. By default, if no class option is defined for a service, the services class name is “default”. So this line will start all the services in the order of position in the file by default. (BTW, you can start any service using start commands, if you like.) Any service is run as a forked process of init, take a look at the source code of service_start in device/system/init.c.   So according to the default init.rc, the following services will be executed step by step: console: star a shell. The source is in device/system/bin/ash. adbd: start adb daemon. The source is in device/tools/adbd. By default is disabled. servicemanager: start binder system. The source is in device/commands/binder. mountd: mount all fs defined in /system/etc/mountd.conf if started, receive commands through local socket to mount any fs. The source is in device/system/bin/mountd. debuggerd: start debug system. The source is in device/system/bin/debuggerd. rild: start radio interface layer daemon. The source is in device/commands/rind. zygote: start Android Java Runtime and start system server. Its the most important service. The source is in device/servers/app. media: start AudioFlinger, MediaPlayerService and CameraService. The source is in device/commands/mediaserver. bootsound: play the default boot sound /system/media/audio/ui/boot.mp3. The source is in device/commands/playmp3. dbus: start dbus daemon, its only used by BlueZ. The source is in device/system/Bluetooth/dbus-daemon. hcid: redirect hcids stdout and stderr to the Android logging system. The source is in device/system/bin/logwrapper. By default is disabled. hfag: start Bluetooth handsfree audio gateway, its only used by BlueZ. The source is in device/system/Bluetooth/bluez-utils. By default is disabled. hsag: start Bluetooth headset audio gateway, its only used by BlueZ. The source is in device/system/Bluetooth/bluez-utils. By default is disabled. installd: start install package daemon. The source is in device/servers/installd. flash_recovery: load /system/recovery.img. The source is in device/commands/recovery/mtdutils.   Zygote service does the following tasks step by step: 1. Create JAVA VM. 2. Register android native function for JAVA VM. 3. Call the main function in the JAVA class named com.android.internal.os.ZygoteInit whose source is device/java/android/com/android/internal/os/ZygoteInit.java. a)  Load ZygoteInit class b)  Register zygote socket c)  Load preload classes(the default file is device/java/android/preloaded-classes) d)  Load preload resources e)  Call Zygote:forkSystemServer (implemented in device/dalvik/vm/InternalNative.c) to fork a new process. In the new process, call the main function in the JAVA class named com.android.server.SystemServer, whose source is in device/java/services/com/android/server.                          i.              Load libandroid_servers.so                        ii.              Call JNI native init1 function implemented in device/libs/android_servers/com_android_server_SystemServers. It only calls system_init implemented in device/servers/system/library/system_init.cpp. l         If running on simulator, instantiate AudioFlinger, MediaPlayerService and CameraService here. l         Call init2 function in JAVA class named com.android.server.SystemServer, whose source is in device/java/services/com/android/server. This function is very critical for Android because it start all of Android JAVA services. l         If not running on simulator, call IPCThreadState:self()->joinThreadPool() to enter into service dispatcher.   SystemServer:init2 will start a new thread to start all JAVA services as follows: Core Services: 1.       Starting Power Manager /2.       Creating Activity Manager 3.       Starting Telephony Registry /4.       Starting Package Manager /5.       Set Activity Manager Service as System Process 6.       Starting Context Manager 7.       Starting System Context Providers 8.       Starting Battery Service /9.       Starting Alarm Manager /10.   Starting Sensor Service 11.   Starting Window Manager 12.   Starting Bluetooth Service /13.   Starting Mount Service /Other services /1.       Starting Status Bar Service 2.       Starting Hardware Service 3.       Starting NetStat Service 4.       Starting Connectivity Service 5.       Starting Notification Manager 6.       Starting DeviceStorageMonitor Service 7.       Starting Location Manager 8.       Starting Search Service 9.       Starting Clipboard Service 10.   Starting Checkin Service 11.   Starting Wallpaper Service 12.   Starting Audio Service 13.   Starting HeadsetObserver 14.   Starting AdbSettingsObserver Finally SystemServer:init2 will call ActivityManagerService.systemReady to launch the first activity by senting Intent.CATEGORY_HOME intent.   There is another way to start system server, which is through a program named system_server whose source is device/servers/system/system_main.cpp. It also calls system_init to start system services. So there is a question: why does Android have two methods to start system services? My guess is that directly start system_server may have synchronous problem with zygote because system_server will call JNI to start SystemServer:init2, while at that time zygote may not start JAVA VM yet. So Android uses another method. After zynote is initialized, fork a new process to start system services. Android 启动过程  1. Boot系统初始化, 具体过程参见(systemcoreinitInit.c)中的main函数,这时候,手机或者模拟器出现的画面是一个console,显示“ANDROID”msg。   2. 初始化成功后,就开始mounting系统,具体参见(systemcoremountdMountd.c)  中的main函数。   3接下来运行ndroidRuntime,并开始启动java虚拟机dalvikvm。   4. Java虚拟机启动成功后,开始系统初始化。系统初始的第一步是用JNI方式实现的,对应java代码为(frameworksbaseservicesjavacomandroidserverSystemServer.java) init1(Native)函数,对应的JNI C+代码为(frameworksbasecorejniservercom_android_server_SystemServer.cpp),而实现的C+代码为 (frameworksbasecmdssystem_serverlibrary System_init.cpp)   中的system_init()函数。   5. system_init调用SurfaceFlinger,SurfaceFlinger的readyToRun()函数用BootAnimation来实现开机动画,这时候手机或者模拟器显示是一副背景图加一个动态的小机器人。   6. 系统初始化的第二步,将启动ServerThread进程,参见SystemServer.init2()。ServerThread将启动各种系统服务,如Power Manager、Activity Manager等等,具体参见ServerThread的run函数,ServerThread同在SystemServer.java中。   7.这之后的事,应该就是进入系统了。(这部分没有调查过)。对Android启动过程的进一步研究对于关注Android底层的朋友来说,其具体的启动过程应该是比较吸引我们的。但是很多启动文件什么的,都得adb push到host上来看,挺不方便的,都怪Android自带的Toolbox太简略了。所以在深入了解Android的启动流程之前,我们来把Busybox安装到Android上去,这样,就有很多工具供我们使用了。 首先去busybox主页 下载最新版本的源代码,然后用arm的交叉编译器编译出busybox的可执行程序,编译的时候需要注意一些设置选项,例如Build Op

    注意事项

    本文(Android 开机启动流程.doc)为本站会员(飞****2)主动上传,淘文阁 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知淘文阁 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于淘文阁 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

    本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

    工信部备案号:黑ICP备15003705号 © 2020-2023 www.taowenge.com 淘文阁 

    收起
    展开