OpenHarmony南向设备应用程序启动流程分析 精华
一、用户程序示例
以qihang开发板gpio_led程序为例,为何单板上电后LedTask()会自动运行,SYS_RUN宏在背后是如何起作用的?
二、第二阶段(应用)启动流程
在上一篇文章中"OpenHarmony南向设备开发建构编译分析",有提到qihang板产品配置文件中相关子系统及组件如下;
vendor/isoftstone/qihang/config.json
可以看到,qihang板使用startup子系统中的bootstrap_lite组件和syspara_lite组件。重点看一下bootstrap_lite组件,位于SDK的base/startup/bootstrap_lite目录。引用该组件的readme文件说明如下:
bootstrap启动引导组件,提供了各服务和功能的启动入口标识。在SAMGR启动时,会调用boostrap标识的入口函数,并启动系统服务。
samgr_lite组件是针对Hi3861这类硬件资源有限的轻量化系统服务框架,代码位于foundation/distributedschedule/samgr_lite目录。该组件的功能引用如下:
系统服务框架基于面向服务的架构,提供了服务开发、服务的子功能开发、对外接口的开发、以及多服务共进程的开发框架。
device/soc/hisilicon/hi3861v100/sdk_liteos/app/wifiiot_app/src/app_main.c
device/soc/hisilicon/hi3861v100/sdk_liteos/app/wifiiot_app/src/ohos_main.c
base/startup/bootstrap_lite/services/source/system_init.c
foundation/distributedschedule/samgr_lite/samgr/source/samgr_lite.c
在用户应用程序组件的代码中,会包含下述声明:
上述宏的说明引用如下:
/**
- @brief Identifies the entry for initializing and starting an application-layer service by the
- priority 2.
- This macro is used to identify the entry called at the priority 2 of the application-layer
- service phase of the startup process. \n
- @param func Indicates the entry function for initializing and starting an application-layer
- service. The type is void (*)(void).
*/
#define APP_SERVICE_INIT(func) LAYER_INITCALL_DEF(func, app_service, “app.service”)
/**
- @brief Identifies the entry for initializing and starting a system running phase by the
- priority 2.
- This macro is used to identify the entry called at the priority 2 in the system startup
- phase of the startup process. \n
- @param func Indicates the entry function for initializing and starting a system running phase.
- The type is void (*)(void).
- #define SYS_RUN(func) LAYER_INITCALL_DEF(func, run, “run”)
*/
总结以上分析,程序第二阶段启动流程如下图图所示:
三、第一阶段(上电)启动流程
请参考本文最后延申阅读第二篇文章的具体介绍,程序加载由3个boot程序前后配合完成:
- romboot:
- 芯片内部自带的上电引导程序,引导loaderboot
- loaderboot (device/soc/hisilicon/hi3861v100/sdk_liteos/boot/loaderboot)
- 与HiBurn通讯,下载镜像到flash
- 烧写EFUSE(芯片配置信息)
- 校验并引导flashboot
- flashboot (device/soc/hisilicon/hi3861v100/sdk_liteos/boot/flashboot)
- 升级固件
- 校验并引导固件(主程序)
loaderboot/common/cmd_loop.c 定义了从hiburn接收并处理的操作:
其中:
loader_download_image就是接收hiburn传来的升级文件,并烧录到flash中。
flashboot/startup目录下有两个重要文件:
- riscv_init_flshboot.S 汇编语言格式,RISC-V启动代码
- main.c
在最后build应用生成的map文件,可看到内存布局如下:
对比可以看到,KERNEL_START_ADDR与应用程序的起始地址一致,基本可推断flashboot最后操作为调用应用程序。通过ecall指令,实现RISC-V处理器( Hi3861使用 )从User Mode( 禁止不可信代码执行特权指令 )切换为Machine Mode( 最高特权模式 )。
四、小结
本文采用倒序的方式,初步梳理了从Hi3861芯片上电到OpenHarmony应用程序启动运行的流程。还有很多内容都没有涉及,包括芯片安全启动,Flash的存储分布等,boot部分说明也比较粗浅,同时或也有理解错误,欢迎大家批评指正。
延申阅读:
- SYS_RUN()和MODULE_INIT()之间的那些事
https://ost.51cto.com/posts/2017 - 鸿蒙芯片Hi3861启动流程介绍
https://ost.51cto.com/posts/7825
楼主可以给自己写的文章标下原创,这样可能会吸引到更多小伙伴来学习