首页 > 编程语言 >UEFI原理与编程(三)

UEFI原理与编程(三)

时间:2024-09-19 10:15:38浏览次数:8  
标签:DRIVER 编程 driver EFI UEFI 驱动 device 原理 ControllerHandle

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();函数启动该驱动。

标签:DRIVER,编程,driver,EFI,UEFI,驱动,device,原理,ControllerHandle
From: https://www.cnblogs.com/linhaostudy/p/18419994

相关文章

  • 深入理解Go并发编程:避免Goroutine泄漏与错误处理
    Go语言以其强大的并发模型和高效的协程(goroutine)而闻名。协程的轻量级和易用性使得并发编程变得更加简单。然而,如果不正确管理协程,可能会导致Goroutine泄漏,从而消耗系统资源,影响程序性能。本文将深入探讨如何避免Goroutine泄漏,并提供实用的代码示例和技巧,帮助您编写更加健壮......
  • Go语言并发编程之Channels详解
    并发编程是Go语言的一大特色,而channel(通道)则是Go语言中用于实现并发的核心工具之一。它源于CSP(CommunicatingSequentialProcesses)的概念,旨在让多个goroutine之间能够高效地进行通信和同步。本文将深入探讨channel的用法、原理和最佳实践,通过丰富的示例代码和详细的解释,帮......
  • 鸿蒙(HarmonyOS)--编程语言-ArkTS 语言基础
    目录 ArkTS基础知识1声明1.1变量声明1.2常量声明1.3自动类型推断 2类型2.1基本类型 2.1.1 string2.1.2  number2.1.3boolean2.2引用类型2.2.1Object类型 2.2.2 Array类型2.2.3Void类型 2.3枚举类型 Enum2.4联合类型 Union 2.5 类型别......
  • 【原理图PCB专题】案例:原理图设计检查为什么要检查全局网络?
            本案例发生在新人的PCB设计文件中,当然就算硬件老人们,其实只要不注意也很容易出现这种全局网络乱用的问题。    如下所示是给新人的接口参考图纸,要求使用嘉立创绘制16个相同的接口做一个工装板。同时还要增加单片机实现切换控制功能。可以看到座子的24......
  • C++入门基础知识75(高级)——【关于C++ Web 编程】
    成长路上不孤单......
  • 《深度学习》PyTorch 常用损失函数原理、用法解析
    目录一、常用损失函数1、CrossEntropyLoss(交叉熵损失)        1)原理    2)流程        3)用法示例2、L1Loss(L1损失/平均绝对误差)    1)原理        2)用法示例3、NLLLoss(负对数似然损失)    1)原理    2)用法示例......
  • 使用回调机制Callback和函数式编程码出优雅结构化代码!拒绝一直写CRUD!!!
    整体回调机制流程如下图所示:1.2回调的结构1.3回调的作用2.回调的实现方式2.1通过接口实现回调java可以通过定义一个回调接口,包含需要回调的方法,然后在业务逻辑中通过传递接口的实现类,触发回调。示例如下://1.定义回调接口interfaceCallback{voidonComplete(Stringres......
  • 使用回调机制Callback和函数式编程码出优雅结构化代码!拒绝一直写CRUD!!!
    整体回调机制流程如下图所示:1.2回调的结构1.3回调的作用2.回调的实现方式2.1通过接口实现回调java可以通过定义一个回调接口,包含需要回调的方法,然后在业务逻辑中通过传递接口的实现类,触发回调。示例如下://1.定义回调接口interfaceCallback{voidonComplete(Stringres......
  • AI编程的特点及SCSAI平台在AI编程方面的一些思路
    团长团 AI智造AI编程 2024年09月18日18:25 北京说先来看看AI编程的优缺点,然后我们再看看SCSAI在AI编程方面的一些可能选择使用AI编程的优点‌AI编程的优点包括提升编程效率、降低编程门槛、优化程序结构、加强软件可靠性、促进跨领域融合,而缺点则包括安全性难题、知......
  • C# 面向对象编程的三大支柱:封装、继承与多态
    面向对象编程(OOP)是一种广泛使用的编程范式,它通过封装、继承和多态这三大支柱来构建灵活且可维护的代码结构。本文将详细介绍这三大支柱在C#语言中的应用,并通过示例代码展示它们的具体实现。一、封装(Encapsulation)封装是指将对象的属性(字段)和行为(方法)结合在一起,并对外部隐藏对象的......