Android 9(11)

一、启动流程概述

一、 启动流程概述

=========

Android启动流程跟 Linux启动类似,大致分为如下五个阶段。

  • 1.开机上电,加载固化的ROM

  • 2.加载BootLoader,拉起Android OS

  • 3.加载Uboot,初始外设,引导Kernel启动等。

  • 4.启动Kernel,加载驱动,硬件。

  • 5.启动Android,挂载分区,加载驱动、服务,init进程等。

1.Android系统启动大致过程如下


640?wx_fmt=other

Android 启动过程

由于水平有限,无法深入了理解驱动层代码,本文主要对 Android上层启动流程进行分析。

二、Android启动分析

=============

Uboot启动Kernel完成系统设置后,会首先在系统中寻找init.rc文件,并启动init进程。

640?wx_fmt=other

Android 启动分析

三、init 进程启动分析

=============

Init进程是Android启动的第一个进程,进程号为1,是Android的系统启动的核心进程,主要用来创建Zygote、属性服务等。 init.cpp 中的main 函数,是init进程的入口函数,源码主要存在\system\core\init目录下。

1.常见init.xxx.rc 进程


640?wx_fmt=other

常见init.xxx.rc 进程

2.init 进程主要作用


640?wx_fmt=other

init 进程主要作用

3./system/core/init 部分内容如下:


640?wx_fmt=other

/system/core/init 部分内容

4.main 函数主要做的事情


1.创建挂载启动所需的文件系统(tmpfs、 devpts、 proc、 sysfs、 selinuxfs等)init.rc 脚本配置文件,并启动Zygote 进程。

5.init.cpp main 函数实现代码



int main(int argc, char** argv) {

   ... ...

    if (!strcmp(basename(argv[0]), "watchdogd")) {

        //启动看门狗函数

        return watchdogd_main(argc, argv);

    }

   ... ...



    //启动第一阶段

    if (is_first_stage) {

        boot_clock::time_point start_time = boot_clock::now();



        // 清理 umask.

        umask(0);



        clearenv();

        setenv("PATH", _PATH_DEFPATH, 1);



        // 在RAM内存上获取基本的文件系统,剩余的被 rc 文件所用

        mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");

        mkdir("/dev/pts", 0755);

        mkdir("/dev/socket", 0755);

        mount("devpts", "/dev/pts", "devpts", 0, NULL);

        #define MAKE_STR(x) __STRING(x)

        mount("proc", "/proc", "proc", 0, "hidepid=2,gid=" MAKE_STR(AID_READPROC));

        // 非特权应用不能使用 Android 命令行

        chmod("/proc/cmdline", 0440);

        gid_t groups[] = { AID_READPROC };

        setgroups(arraysize(groups), groups);

        mount("sysfs", "/sys", "sysfs", 0, NULL);

        mount("selinuxfs", "/sys/fs/selinux", "selinuxfs", 0, NULL);



        mknod("/dev/kmsg", S_IFCHR | 0600, makedev(1, 11));



        if constexpr (WORLD_WRITABLE_KMSG) {

            mknod("/dev/kmsg_debug", S_IFCHR | 0622, makedev(1, 11));

        }



        mknod("/dev/random", S_IFCHR | 0666, makedev(1, 8));

        mknod("/dev/urandom", S_IFCHR | 0666, makedev(1, 9));



        // Mount staging areas for devices managed by vold

        // See storage config details at http://source.android.com/devices/storage/

        mount("tmpfs", "/mnt", "tmpfs", MS_NOEXEC | MS_NOSUID | MS_NODEV,

              "mode=0755,uid=0,gid=1000");

        //创建可供读写的 vendor目录

        mkdir("/mnt/vendor", 0755);



        // 在/dev目录下挂载好 tmpfs 以及 kmsg

        // 这样就可以初始化 /kernel Log 系统,供用户打印log

        InitKernelLogging(argv);



        LOG(INFO) << "init first stage started!";



        if (!DoFirstStageMount()) {

            LOG(FATAL) << "Failed to mount required partitions early ...";

        }



        SetInitAvbVersionInRecovery();



        // Enable seccomp if global boot option was passed (otherwise it is enabled in zygote).

        global_seccomp();



        // 优先加载selinux log系统, 紧接着初始化selinux

        SelinuxSetupKernelLogging();

        SelinuxInitialize();



        // 添加 selinux 是否启动成功的log

        if (selinux_android_restorecon("/init", 0) == -1) {

            PLOG(FATAL) << "restorecon failed of /init failed";

        }



        setenv("INIT_SECOND_STAGE", "true", 1);



        static constexpr uint32_t kNanosecondsPerMillisecond = 1e6;

        uint64_t start_ms = start_time.time_since_epoch().count() / kNanosecondsPerMillisecond;

        setenv("INIT_STARTED_AT", std::to_string(start_ms).c_str(), 1);



        char* path = argv[0];

        char* args[] = { path, nullptr };

        execv(path, args);



        // execv() only returns if an error happened, in which case we

        // panic and never fall through this conditional.

        PLOG(FATAL) << "execv(\"" << path << "\") failed";

    }



    //启动第二阶段

    InitKernelLogging(argv);

    LOG(INFO) << "init second stage started!";



    // Set up a session keyring that all processes will have access to. It

    // will hold things like FBE encryption keys. No process should override

    // its session keyring.

    keyctl_get_keyring_ID(KEY_SPEC_SESSION_KEYRING, 1);



    // Indicate that booting is in progress to background fw loaders, etc.

    close(open("/dev/.booting", O_WRONLY | O_CREAT | O_CLOEXEC, 0000));

    //初始化属性

    property_init();



    // If arguments are passed both on the command line and in DT,

    // properties set in DT always have priority over the command-line ones.

    process_kernel_dt();

    process_kernel_cmdline();



    // Propagate the kernel variables to internal variables

    // used by init as well as the current required properties.

    export_kernel_boot_props();



    // Make the time that init started available for bootstat to log.

    property_set("ro.boottime.init", getenv("INIT_STARTED_AT"));

    property_set("ro.boottime.init.selinux", getenv("INIT_SELINUX_TOOK"));



    // Set libavb version for Framework-only OTA match in Treble build.

    const char* avb_version = getenv("INIT_AVB_VERSION");

    if (avb_version) property_set("ro.boot.avb_version", avb_version);



    // 清空设置的环境变量

    unsetenv("INIT_SECOND_STAGE");

    unsetenv("INIT_STARTED_AT");

    unsetenv("INIT_SELINUX_TOOK");

    unsetenv("INIT_AVB_VERSION");



    // 设置第二阶段的selinux

    SelinuxSetupKernelLogging();

    SelabelInitialize();

    SelinuxRestoreContext();

    //创建 epoll 句柄

    epoll_fd = epoll_create1(EPOLL_CLOEXEC);

    if (epoll_fd == -1) {

        PLOG(FATAL) << "epoll_create1 failed";

    }

    //设置 子进程处理函数

    sigchld_handler_init();



    if (!IsRebootCapable()) {

        // If init does not have the CAP_SYS_BOOT capability, it is running in a container.

        // In that case, receiving SIGTERM will cause the system to shut down.

        InstallSigtermHandler();

    }



    LoadRscRoProps();

    property_load_boot_defaults();

    export_oem_lock_status();

    //启动属性服务

    start_property_service();

    //为USB存储设置udc Contorller, sys/class/udc

    set_usb_controller();



    const BuiltinFunctionMap function_map;

    Action::set_function_map(&function_map);



    subcontexts = InitializeSubcontexts();



    ActionManager& am = ActionManager::GetInstance();

    ServiceList& sm = ServiceList::GetInstance();



    LoadBootScripts(am, sm);



    // Turning this on and letting the INFO logging be discarded adds 0.2s to

    // Nexus 9 boot time, so it's disabled by default.

    if (false) DumpState();



    am.QueueEventTrigger("early-init");



    // Queue an action that waits for coldboot done so we know ueventd has set up all of /dev...

    am.QueueBuiltinAction(wait_for_coldboot_done_action, "wait_for_coldboot_done");

    // ... so that we can start queuing up actions that require stuff from /dev.

    am.QueueBuiltinAction(MixHwrngIntoLinuxRngAction, "MixHwrngIntoLinuxRng");

    am.QueueBuiltinAction(SetMmapRndBitsAction, "SetMmapRndBits");

    am.QueueBuiltinAction(SetKptrRestrictAction, "SetKptrRestrict");

    am.QueueBuiltinAction(keychord_init_action, "keychord_init");

    am.QueueBuiltinAction(console_init_action, "console_init");



    // Trigger all the boot actions to get us started.

    am.QueueEventTrigger("init");



    // Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random

    // wasn't ready immediately after wait_for_coldboot_done

    am.QueueBuiltinAction(MixHwrngIntoLinuxRngAction, "MixHwrngIntoLinuxRng");



    // Don't mount filesystems or start core system services in charger mode.

    std::string bootmode = GetProperty("ro.bootmode", "");

    if (bootmode == "charger") {

        am.QueueEventTrigger("charger");

    } else {

        am.QueueEventTrigger("late-init");

    }



    // Run all property triggers based on current state of the properties.

    am.QueueBuiltinAction(queue_property_triggers_action, "queue_property_triggers");



    while (true) {

        // By default, sleep until something happens.

        int epoll_timeout_ms = -1;



        if (do_shutdown && !shutting_down) {

            do_shutdown = false;

            if (HandlePowerctlMessage(shutdown_command)) {

                shutting_down = true;

            }

        }




### 最后

总之啊,家里没矿的同学们,如果你们想以后的日子过得好一些,多想想你们的业余时间怎么安排吧;

技术方面的提升肯定是重中之重,但是技术外的一些“软实力”也不能完全忽视,很多时候升职确实是因为你的技术足够强,但也与你的“软实力”密切相关

在这我也分享一份大佬自己收录整理的 **Android学习PDF+架构视频+面试文档+源码笔记** ,还有**高级架构技术进阶脑图、Android开发面试专题资料,高级进阶架构资料**这些都是我闲暇还会反复翻阅并给下属员工学习的精品资料。在脑图中,每个知识点专题都配有相对应的实战项目,可以有效的帮助大家掌握知识点。

总之也是在这里帮助大家学习提升进阶,也节省大家在网上搜索资料的时间来学习,也可以分享给身边好友一起学习

![](https://img-blog.csdnimg.cn/img_convert/088e9543eb92ce36b546ca95c7feaa12.webp?x-oss-process=image/format,png)

![](https://img-blog.csdnimg.cn/img_convert/fb2b0e794ff70deaa4a7e6f17d3c4c00.webp?x-oss-process=image/format,png)

**相信自己,没有做不到的,只有想不到的**

essage(shutdown_command)) {

                shutting_down = true;

            }

        }




### 最后

总之啊,家里没矿的同学们,如果你们想以后的日子过得好一些,多想想你们的业余时间怎么安排吧;

技术方面的提升肯定是重中之重,但是技术外的一些“软实力”也不能完全忽视,很多时候升职确实是因为你的技术足够强,但也与你的“软实力”密切相关

在这我也分享一份大佬自己收录整理的 **Android学习PDF+架构视频+面试文档+源码笔记** ,还有**高级架构技术进阶脑图、Android开发面试专题资料,高级进阶架构资料**这些都是我闲暇还会反复翻阅并给下属员工学习的精品资料。在脑图中,每个知识点专题都配有相对应的实战项目,可以有效的帮助大家掌握知识点。

总之也是在这里帮助大家学习提升进阶,也节省大家在网上搜索资料的时间来学习,也可以分享给身边好友一起学习

[外链图片转存中...(img-9tHNq8gn-1719293766555)]

[外链图片转存中...(img-6JSUsKjN-1719293766555)]

**相信自己,没有做不到的,只有想不到的**

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值