1 开发UEFI服务

本质Protocol 就是包含属性和函数指针的结构体,功能上来说就是提供者和使用者对服务的一种约定。

2 开发UEFI驱动

一个设备/总线驱动程序在安装时首要找到对应的硬件设备(UEFI中是要找到对应的控制器),然后执行安装操作,将驱动程序安装到硬件设备的控制器上。

一个完整的驱动程序框架需要三个部分:

  1. Findout(): 找出对应的硬件设备
  2. Install()/Start(): 安装驱动到指定的硬件设备。
  3. Uninstall()/Stop(): 从硬件设备中卸载驱动

1 UEFI驱动模型

UEFI驱动模型核心通过 EFI Driver Binding Protocol管理驱动程序。完整的驱动程序包含两个核心部分:EFI Driver Binding Protocol 和 驱动服务本身。

//file:  MdePkg\Include\Protocol\DriverBinding.h
typedef struct _EFI_DRIVER_BINDING_PROTOCOL { ///
/// This protocol provides the services required to determine if a driver supports a given controller.
/// If a controller is supported, then it also provides routines to start and stop the controller.
///
struct _EFI_DRIVER_BINDING_PROTOCOL {
EFI_DRIVER_BINDING_SUPPORTED Supported; // 检测一个设备是否支持该驱动
EFI_DRIVER_BINDING_START Start; // 用于将驱动安装到设备上
EFI_DRIVER_BINDING_STOP Stop; // 用于将驱动从设备上卸载 ///
/// The version number of the UEFI driver that produced the
/// EFI_DRIVER_BINDING_PROTOCOL. This field is used by
/// the EFI boot service ConnectController() to determine
/// the order that driver's Supported() service will be used when
/// a controller needs to be started. EFI Driver Binding Protocol
/// instances with higher Version values will be used before ones
/// with lower Version values. The Version values of 0x0-
/// 0x0f and 0xfffffff0-0xffffffff are reserved for
/// platform/OEM specific drivers. The Version values of 0x10-
/// 0xffffffef are reserved for IHV-developed drivers.
///
UINT32 Version; // EDBP版本号 ///
/// The image handle of the UEFI driver that produced this instance
/// of the EFI_DRIVER_BINDING_PROTOCOL.
///
EFI_HANDLE ImageHandle; // ImageHandle是生成EDBP映像文件句柄 ///
/// The handle on which this instance of the
/// EFI_DRIVER_BINDING_PROTOCOL is installed. In most
/// cases, this is the same handle as ImageHandle. However, for
/// UEFI drivers that produce more than one instance of the
/// EFI_DRIVER_BINDING_PROTOCOL, this value may not be
/// the same as ImageHandle.
///
EFI_HANDLE DriverBindingHandle; // DriverBindingHandle是安装了EDBP的Handle
};
  • Supported函数

用于检测一个设备是否支持该驱动,支持返回EFI_SUCCESS,否则返回其他。

//@file:   MdePkg\Include\Protocol\DriverBinding.h
/**
Tests to see if this driver supports a given controller. If a child device is provided,
it further tests to see if this driver supports creating a handle for the specified child device. This function checks to see if the driver specified by This supports the device specified by
ControllerHandle. Drivers will typically use the device path attached to
ControllerHandle and/or the services from the bus I/O abstraction attached to
ControllerHandle to determine if the driver supports ControllerHandle. This function
may be called many times during platform initialization. In order to reduce boot times, the tests
performed by this function must be very small, and take as little time as possible to execute. This
function must not change the state of any hardware devices, and this function must be aware that the
device specified by ControllerHandle may already be managed by the same driver or a
different driver. This function must match its calls to AllocatePages() with FreePages(),
AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
Because ControllerHandle may have been previously started by the same driver, if a protocol is
already in the opened state, then it must not be closed with CloseProtocol(). This is required
to guarantee the state of ControllerHandle is not modified by this function. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
@param[in] ControllerHandle The handle of the controller to test. This handle
must support a protocol interface that supplies
an I/O abstraction to the driver.
@param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
parameter is ignored by device drivers, and is optional for bus
drivers. For bus drivers, if this parameter is not NULL, then
the bus driver must determine if the bus controller specified
by ControllerHandle and the child controller specified
by RemainingDevicePath are both supported by this
bus driver. @retval EFI_SUCCESS The device specified by ControllerHandle and
RemainingDevicePath is supported by the driver specified by This.
@retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
RemainingDevicePath is already being managed by the driver
specified by This.
@retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
RemainingDevicePath is already being managed by a different
driver or an application that requires exclusive access.
Currently not implemented.
@retval EFI_UNSUPPORTED The device specified by ControllerHandle and
RemainingDevicePath is not supported by the driver specified by This.
**/
typedef
EFI_STATUS
(EFIAPI *EFI_DRIVER_BINDING_SUPPORTED)(
IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE ControllerHandle, //检查这个驱动是否支持这个控制器
IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
);
  • Start函数

Start函数用来将驱动安装到设备上并启动硬件设备,函数最重要的事情是调用 InstallProtocolInterface() 或者 InstallMultipleProtocolInterfaces() 在ControllerHandle 上安装驱动Protocol

//@file:   MdePkg\Include\Protocol\DriverBinding.h
/**
Starts a device controller or a bus controller. The Start() function is designed to be invoked from the EFI boot service ConnectController().
As a result, much of the error checking on the parameters to Start() has been moved into this
common boot service. It is legal to call Start() from other locations,
but the following calling restrictions must be followed, or the system behavior will not be deterministic.
1. ControllerHandle must be a valid EFI_HANDLE.
2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
EFI_DEVICE_PATH_PROTOCOL.
3. Prior to calling Start(), the Supported() function for the driver specified by This must
have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
@param[in] ControllerHandle The handle of the controller to start. This handle
must support a protocol interface that supplies
an I/O abstraction to the driver.
@param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
parameter is ignored by device drivers, and is optional for bus
drivers. For a bus driver, if this parameter is NULL, then handles
for all the children of Controller are created by this driver.
If this parameter is not NULL and the first Device Path Node is
not the End of Device Path Node, then only the handle for the
child device specified by the first Device Path Node of
RemainingDevicePath is created by this driver.
If the first Device Path Node of RemainingDevicePath is
the End of Device Path Node, no child handle is created by this
driver. @retval EFI_SUCCESS The device was started.
@retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
@retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
@retval Others The driver failded to start the device. **/
typedef
EFI_STATUS
(EFIAPI *EFI_DRIVER_BINDING_START)(
IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE ControllerHandle, //驱动将被安装到这个Handles上
IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
);
  • Stop函数

Stop函数用于停止硬件设备并卸载驱动(调用函数 UninstallProtocolInterface() 或者 UninstallMultipleProtocolInterfaces()从ControllerHandle 卸载驱动协议 )

//@file:   MdePkg\Include\Protocol\DriverBinding.h
/**
Stops a device controller or a bus controller. The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
As a result, much of the error checking on the parameters to Stop() has been moved
into this common boot service. It is legal to call Stop() from other locations,
but the following calling restrictions must be followed, or the system behavior will not be deterministic.
1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
same driver's Start() function.
2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
EFI_HANDLE. In addition, all of these handles must have been created in this driver's
Start() function, and the Start() function must have called OpenProtocol() on
ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
@param[in] ControllerHandle A handle to the device being stopped. The handle must
support a bus specific I/O protocol for the driver
to use to stop the device.
@param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
@param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
if NumberOfChildren is 0. @retval EFI_SUCCESS The device was stopped.
@retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. **/
typedef
EFI_STATUS
(EFIAPI *EFI_DRIVER_BINDING_STOP)(
IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE ControllerHandle, //停止对这个控制器上对应的驱动
IN UINTN NumberOfChildren, // 子控制器数量
IN EFI_HANDLE *ChildHandleBuffer OPTIONAL //子控制器数组
);

对设备驱动来说 NumberOfChildren为0 , ChildHandleBuffer为NULL ; 对Bus Drive来说 如果NumberOfChildren不为0,那么ChildHandleBuffer子节点都要被释放。

根据是否满足UEFI Driver Model来区分:一种是普通的Driver,一种就是满足UEFI Driver Model的驱动

前者是再编写驱动的时候就主动去寻找设备并初始化它,前者的实现一般在驱动运行的时候就直接完成了(DEX阶段)

后者是系统服务自己根据设备来寻到到合适的驱动然后初始化。而后者需要先注册驱动,然后再后续(通常是BDS阶段)通过调用系统服务来完成,这个系统复位就是EFI_BOOT_SERVICES.ConnectController() (第五章启动服务的驱动管理服务)。

2 编写驱动的步骤 驱动分为两个部分:

  • 硬件相关部分,用于驱动硬件设备,为用户提供服务,以协议形式出现,如DiskIo BlockIo

  • 框架部分: 需要实现Driver Binding Protocol,主要是三个接口(Support,Start, Stop)这部分主要用于驱动的安装卸载。

入口函数: InitializeGigUNDIDriver调用uefi驱动模型库函数: EfiLibInstallDriverBinding这个函数主要用来: 安装并完成驱动程序绑定协议实例的初始化(_EFI_DRIVER_BINDING_PROTOCOL的三个属性)。调用函数: InstallMultipleProtocolInterfaces

将新的协议安装到ControllerHandle上。

Support函数(GigUndiDriverSupported):

1 通过gBS服务中OpenProtocol() 打开所有需要Protocol,标准驱动要用EFI_OPEN_PROTOCOL_BY_DRIVER属性打开Protocol, 若OpenProtocol() 返回错误,则调用CloseProtocol()关闭已经打开的Protocol 并返回错误代码。

2 所需所有Protocol成功打开后,测试这个Drive是否支持此Controller. 测试失败则关闭所有打开的Protocol,

返回EFI_UNSUPPORTED, 成功则调用 CloseProtocol()关闭所有打开的Protocol,返回EFI_SUCCESS 。

Start函数(GigUndiDriverStart):

1 通过gBS中OpenProtocol()打开所有需要Protocol, 若OpenProtocol() 返回错误,直接返回错误代码。

Stop函数(GigUndiDriverStop):卸载安装的Protocol关闭所有打开的Protocol释放所有申请的资源

将驱动程序(Support();、Start();、Stop();函数)注册到Image Handle上,仅是注册,驱动程序不会执行,即不会操作任何硬件,DXE阶段驱动程序注册完成后,BDS阶段当查询到一个控制器后,会利用ConnectController() 函数为该控制器寻找最好的驱动,通过调用该驱动的Support();函数确认是否支持该控制器,调用Start();函数启动该驱动。

UEFI原理与编程(三)的更多相关文章

  1. 【读书笔记】UEFI原理与编程(1)概述及开发环境的搭建

    一.概述: 0.为什么会有这篇文章 说实在的,在2016初的时候,我就萌生了写一个操作系统的念头,但是这对于我一个菜鸟来说,犹如登天. 既然想了就去写,即使最后做不完,也不后悔. 抱着这样的念头,我开 ...

  2. trident原理及编程指南

    目录 trident原理及编程指南 一.理论介绍 1.trident是什么? 2.trident处理单位 3.事务类型 二.编程指南 1.定义输入流 2.统计单词数量 3.输出统计结果 4.split ...

  3. 超级干货:动态防御WAF技术原理及编程实战!

    本文带给大家的内容是动态防御WAF的技术原理及编程实战. 将通过介绍ShareWAF的核心技术点,向大家展示动态防御的优势.实现思路,并以编程实战的方式向大家展示如何在WAF产品开发过程中应用动态防御 ...

  4. Linux网络编程(三)

    Linux网络编程(三) wait()还是waitpid() Linux网络编程(二)存在客户端断开连接后,服务器端存在大量僵尸进程.这是由于服务器子进程终止后,发送SIGCHLD信号给父进程,而父进 ...

  5. Java并发编程三个性质:原子性、可见性、有序性

      并发编程 并发程序要正确地执行,必须要保证其具备原子性.可见性以及有序性:只要有一个没有被保证,就有可能会导致程序运行不正确  线程不安全在编译.测试甚至上线使用时,并不一定能发现,因为受到当时的 ...

  6. WebSocket原理与实践(三)--解析数据帧

    WebSocket原理与实践(三)--解析数据帧 1-1 理解数据帧的含义:   在WebSocket协议中,数据是通过帧序列来传输的.为了数据安全原因,客户端必须掩码(mask)它发送到服务器的所有 ...

  7. Contiki源码+原理+功能+编程+移植+驱动+网络(转)

    源:Contiki源码+原理+功能+编程+移植+驱动+网络 请链接:http://www.rimelink.com/nd.jsp? id=31&_np=105_315 假设您对于用Contik ...

  8. 数据库系统原理之SQL(三)

    数据库系统原理之SQL(三) 1. SQL的组成 1. 数据查询 2. 数据定义 3. 数据操作 4. 数据控制 2. 数据定义语言 CREATE创建数据库或数据库对象 创建数据库 ~~~ CREAT ...

  9. Vue双向绑定的实现原理系列(三):监听器Observer和订阅者Watcher

    监听器Observer和订阅者Watcher 实现简单版Vue的过程,主要实现{{}}.v-model和事件指令的功能 主要分为三个部分 github源码 1.数据监听器Observer,能够对数据对 ...

  10. Java并发编程原理与实战三十五:并发容器ConcurrentLinkedQueue原理与使用

    一.简介 一个基于链接节点的无界线程安全队列.此队列按照 FIFO(先进先出)原则对元素进行排序.队列的头部 是队列中时间最长的元素.队列的尾部 是队列中时间最短的元素.新的元素插入到队列的尾部,队列 ...

随机推荐

  1. 【Java】Spring5学习

    基础环境与文档资料: 见黑马视频: https://www.bilibili.com/video/BV1P44y1N7QG 依赖坐标: <?xml version="1.0" ...

  2. Jax计算框架的NamedSharding的reshape —— namedsharding-gives-a-way-to-express-shardings-with-names

    官方文档参考: https://jax.readthedocs.io/en/latest/notebooks/Distributed_arrays_and_automatic_parallelizat ...

  3. 如何将 Vim 剪贴板里面的东西粘贴到 Vim 之外的地方? (Ubuntu18.04系统亲测)

    主要参考内容: https://www.zhihu.com/question/19863631 在vim中剪贴中的内容是难以在vim之外使用的,那么怎么修改这个问题呢? =============== ...

  4. js 实现俄罗斯方块(一)

    上学的时候就想尝试写个游戏了,不过那时一直想的都是些不太切合自身能力的高级游戏(需要花很多时间学习引擎,需要美工等等)最终都没有实现. 最近突然又想写了就利用业余时间写了个俄罗斯方块js写的画面也比较 ...

  5. 《最新出炉》系列初窥篇-Python+Playwright自动化测试-64 - Canvas和SVG元素推拽

    1.简介 今天宏哥分享的在实际测试工作中很少遇到,比较生僻,如果突然遇到我们可能会脑大.懵逼,一时之间不知道怎么办?所以宏哥这里提供一种思路供大家学习和参考. 2.SVG简介 svg也是html5新增 ...

  6. Apache DolphinScheduler中ZooKeeperCDH不兼容问题的解决方案

    背景 看到Apache DolphinScheduler社区群有很多用户反馈和讨论这块问题,针对不兼容的问题,不仅需要自己重新编译各一个新包,而且因为默认是使用zk-3.8的配置,所以会出现不兼容问题 ...

  7. redhat8 rhel8 启动grub损坏修复

    环境:redhat8.4 RHEL8.4 服务器:华为G560 问题描述:调整了/etc/default/grub文件,重新生成/boot/grub2/grub.cfg导致机器启动失败,直接进入了re ...

  8. 抖音集团 FlinkSQL 性能优化探索及实践

    本文作者:李精卫   更多技术交流.求职机会,欢迎关注字节跳动数据平台微信公众号,回复[1]进入官方交流群   背景 随着抖音集团内部对流式任务的需求不断增长,Flink SQL作为一种低成本接入手段 ...

  9. MFC树形控件加载Access数据库数据

    研究了好几天,今天终于弄好了,下面是代码 [dlg.cpp] 在初始函数OnInitDialog()中加 HICON hIcon[3]; HTREEITEM hRoot; //加载三个图标 hIcon ...

  10. 最全!嵌入式STM32单片机开发环境配置教学Win/Mac!!!

    嵌入式STM32单片机开发环境配置教学Win/Mac · 本教程支持Windows和Mac · Windows可选的开发软件为Keil.Clion.STM32CubeMX,可自由选择开发方式 · Ma ...