class="hide-preCode-box">

1.4 配置编译时链接bootstrap库

另外,bootstrap_lite部件会编译//base/startup/bootstrap_lite/services/source/bootstrap_service.c,该文件中,通过SYS_SERVICE_INIT将Init函数符号指定到__zinitcall_sys_service_start和__zinitcall_sys_service_end段中,由于没有显式调用Init函数,所以需要将它强制链接到最终的镜像。可以参考device\board\goodix\gr5515_sk\liteos_m\config.gni文件中的链接选项。恒玄的开发板适配时,是配置到vendor\bestechnic\display_demo\config.json文件中的自己定义的配置项force_link_libs里,该配置项在device\soc\bestechnic\bes2600\BUILD.gn中被解析、链接。

board_ld_flags = [
....
  "-lbootstrap",
 class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">

1.5 调用OHOS_SystemInit接口

函数void OHOS_SystemInit(void)定义在文件base\startup\bootstrap_lite\services\source\system_init.c中,在移植适配时,需要调用该接口。可以参考文件device\soc\bestechnic\bes2600\liteos_m\sdk\bsp\rtos\liteos\liteos_m\board.c中的使用示例。

int main(void);
extern void OHOS_SystemInit(void);
    ......
        OHOS_SystemInit();
    ......
    while (1) {
        osDelay(1000);
        TRACE(0, "main idle");
    }
}
 class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">

2、bootstrap_lite服务启动引导部件实现原理之system_init

bootstrap_lite服务启动部件实现了服务的自动初始化,即服务的初始化函数无需显式调用,它是使用宏定义的方式申明,在系统启动时自动被执行。实现原理是将服务启动的函数通过宏申明之后,放在预定义好的zInit代码段中,系统启动的时候调用OHOS_SystemInit接口,遍历该代码段并调用其中的函数。因此在适配移植时,需要在device/soc/下面具体的芯片的链接脚本中添加zInit段,并且在main函数里调用OHOS_SystemInit接口。

2.1 bootstrap_lite服务启动引导部件的初始化宏

bootstrap_lite服务启动引导部件的初始化宏定义在文件utils\native\lite\include\ohos_init.h,片段如下。初始化函数宏SYS_SERVICE_INIT(func)用于标识核心系统服务的初始化入口,该宏识别的函数在启动过程中核心系统服务优先级2阶段被调用;初始化宏SYS_SERVICE_INIT_PRI(func, priority)可以指定优先级数值,优先级的取值范围为[0,5),调用顺序为0, 1, 2, 3, 4。

  /**
  * @brief Identifies the entry for initializing and starting a core system service by the
  * priority 2.
  *
  * This macro is used to identify the entry called at the priority 2 in the core system
  * service phase of the startup process. \n
  *
  * @param func Indicates the entry function for initializing and starting a core system service.
  * The type is void (*)(void).
  */
  #define SYS_SERVICE_INIT(func) LAYER_INITCALL_DEF(func, sys_service, "sys.service")
  /**
  * @brief Identifies the entry for initializing and starting a core system service by the
  * specified priority.
  *
  * This macro is used to identify the entry called at the specified priority in the core system
  * service phase of the startup process. \n
  *
  * @param func Indicates the entry function for initializing and starting a core system service.
  * The type is void (*)(void).
  * @param priority Indicates the calling priority when starting the core system service in the
  * startup phase. The value range is [0,5), and the calling sequence is 0, 1, 2, 3, and 4.
  */
  #define SYS_SERVICE_INIT_PRI(func, priority) LAYER_INITCALL(func, sys_service, "sys.service", priority)
 class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}"> class="hide-preCode-box">

更多的初始化宏见下文的列表,处理这些初始化宏,还有可以指定优先级的版本XXX_PRI。

class="table-box">
初始化宏名称描述
CORE_INIT(func)标识核心阶段的初始化启动入口;Samgr初始化启动时调用该宏。
SYS_SERVICE_INIT(func)标识核心系统服务的初始化启动入口;启动过程中的核心系统服务阶段调用该宏标识的函数。
SYS_FEATURE_INIT(func)标识核心系统功能的初始化启动入口;启动过程中的核心系统功能阶段调用该宏标识的函数。
SYS_RUN(func)标识系统启动阶段的初始化启动入口;启动过程中的系统启动能阶段调用该宏标识的函数。
SYSEX_SERVICE_INIT(func)标识系统服务的初始化启动入口;启动过程中的系统服务阶段调用该宏标识的函数。
SYSEX_FEATURE_INIT(func)标识系统功能的初始化启动入口;启动过程中的系统功能阶段调用该宏标识的函数。
APP_SERVICE_INIT(func)标识应用层服务的初始化启动入口;启动过程中的应用层服务阶段调用该宏标识的函数。
APP_FEATURE_INIT(func)标识应用层功能的初始化启动入口;启动过程中的应用层功能阶段调用该宏标识的函数。

2.2 LAYER_INITCALL宏定义

从上文已知,bootstrap_lite服务启动引导部件的初始化宏会调用LAYER_INITCALL_DEF和LAYER_INITCALL宏。这些宏的定义在文件utils\native\lite\include\ohos_init.h,代码片段如下。⑴处声明函数类型,无参无返回值。⑵处处理定义分层初始化共享库宏LAYER_INIT_SHARED_LIB的情况,如果没有定义该宏,则执行⑹。在文件foundation/distributedschedule/samgr_lite/samgr/BUILD.gn中定义了该宏,在移植适配芯片开发板时,没有定义这个宏。⑶处定义5个分层初始化级别,⑷处定义7个构建值(constructor value,简称CTOR Value)。⑸处是宏LAYER_INITCALL的定义,该宏需要4个参数,分别是初始化服务或功能函数func;layer是分层名称,支持的取值为device、core、sys_service、sys_feature、app_service、app_feature和run,拼装为CTOR_VALUE_XXX;clayer参数在定义宏LAYER_INIT_SHARED_LIB时未使用;priority是优先级参数。attribute((constructor))表示这段代码将在main函数前调用。当传入参数为(myFunc, sys_feature, “sys.feature”, 2)时,函数宏替换为:static __attribute__((constructor(130 + 2))) void BOOT_sys_featurer2myFunc {myFunc();}。等于定义个一个新的启动引导函数BOOT_sys_featurer2myFunc()

当没有定义LAYER_INIT_SHARED_LIB宏时,执行⑹,当传入参数为(myFunc, sys_feature, “sys.feature”, 2)时,函数宏替换为:static const InitCall __attribute__((used)) __zinitcall_sys_feature_myFunc __attribute__((section(".zinitcall.sys.feature2.init"))) = myFunc,除了__attribute__部分,等于声明一个函数类型InitCall的变量__zinitcall_sys_feature_myFunc。该函数变量放入section段".zinitcall.sys.feature2.init"内,所以移植适配时,需要在芯片开发板的链接脚本里添加zInit代码段。

⑴  typedef void (*InitCall)(void);

    #define USED_ATTR __attribute__((used))

⑵  #ifdef LAYER_INIT_SHARED_LIB
⑶  #define LAYER_INIT_LEVEL_0 0
    #define LAYER_INIT_LEVEL_1 1
    #define LAYER_INIT_LEVEL_2 2
    #define LAYER_INIT_LEVEL_3 3
    #define LAYER_INIT_LEVEL_4 4
⑷  #define CTOR_VALUE_device 100
    #define CTOR_VALUE_core 110
    #define CTOR_VALUE_sys_service 120
    #define CTOR_VALUE_sys_feature 130
    #define CTOR_VALUE_app_service 140
    #define CTOR_VALUE_app_feature 150
    #define CTOR_VALUE_run  700
⑸  #define LAYER_INITCALL(func, layer, clayer, priority)                                     \
        static __attribute__((constructor(CTOR_VALUE_##layer + LAYER_INIT_LEVEL_##priority))) \
            void BOOT_##layer##priority##func() {func();}
    #else
⑹  #define LAYER_INITCALL(func, layer, clayer, priority)            \
        static const InitCall USED_ATTR __zinitcall_##layer##_##func \
            __attribute__((section(".zinitcall." clayer #priority ".init"))) = func
    #endif
    // Default priority is 2, priority range is [0, 4]
    #define LAYER_INITCALL_DEF(func, layer, clayer) \
        LAYER_INITCALL(func, layer, clayer, 2)
 class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}"> class="hide-preCode-box">

如果大家想更加深入的学习 OpenHarmony 开发的内容,不妨可以参考以下相关学习文档进行学习,助你快速提升自己:

OpenHarmony 开发环境搭建:https://qr18.cn/CgxrRy

《OpenHarmony源码解析》:https://qr18.cn/CgxrRy

系统架构分析:https://qr18.cn/CgxrRy

OpenHarmony 设备开发学习手册:https://qr18.cn/CgxrRy

在这里插入图片描述

OpenHarmony面试题(内含参考答案):https://qr18.cn/CgxrRy

写在最后

data-report-view="{"mod":"1585297308_001","spm":"1001.2101.3001.6548","dest":"https://blog.csdn.net/maniuT/article/details/139771632","extend1":"pc","ab":"new"}">> id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"> class="blog_extension blog_extension_type2" id="blog_extension"> class="extension_official" data-report-click="{"spm":"1001.2101.3001.6471"}" data-report-view="{"spm":"1001.2101.3001.6471"}"> class="blog_extension_card_left"> class="blog_extension_card_cont"> 鸿蒙开发学习资料领取!!! class="blog_extension_card_cont_r"> 微信名片
注:本文转载自blog.csdn.net的沧海一笑-dj的文章"https://blog.csdn.net/dengjin20104042056/article/details/96177043"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接

评论记录:

未查询到任何数据!