UEFI开发学习7 – DXE Driver

1.什么是DXE Driver

DXE driver只能运行于Boot service 环境,当boot loader调用ExitBootServices()时,所有的boot service将被销毁,此时DXE driver便失效了。一个DXE driver可以当成一个可供调用的库,当我们实现了某个功能然后要与别人共享,但又不想给代码时,我们就可以将它写成一个DXE driver,把编译生成的efi和一个.h文件给出去即可,这一样可以被调用。

2.一个简单的DXE Driver

DriverTest.inf

[Defines]
  INF_VERSION                    = 0x00010005
  BASE_NAME                      = DriverTest
  FILE_GUID                      = 86787701-8fed-11e3-b3ff-f33b73acfec2
  MODULE_TYPE                    = DXE_DRIVER
  VERSION_STRING                 = 1.0
  ENTRY_POINT                    = DriverTestEntryPoint

[Sources.common]
  DriverTest.c

[LibraryClasses]
  UefiDriverEntryPoint
  UefiLib

[Protocols]
  
[Packages]
  MdePkg/MdePkg.dec
  MdeModulePkg/MdeModulePkg.dec

[Depex]
  TRUE

DriverTest.c

#include <Uefi.h>
#include <Library/UefiLib.h>
#include <Library/UefiBootServicesTableLib.h>

EFI_GUID gEfiDriverTestGuid = {0xc56ce2ec, 0x50b5, 0x42bc, {0x82, 0x4b, 0x33, 0xaf, 0xa6, 0x7b, 0x17, 0x98}};

typedef
EFI_STATUS
(*DRIVER_TEST) (
  VOID
  );

typedef struct _EFI_DRIVER_TEST_PROTOCOL
{
    DRIVER_TEST PrintHello;
} EFI_DRIVER_TEST_PROTOCOL;

EFI_STATUS
PrintHello (VOID) 
{
    Print (L"Hello World!\n\r");
    return EFI_SUCCESS;
}

STATIC EFI_DRIVER_TEST_PROTOCOL mDriverTestProtocol = {
    PrintHello
};

EFI_STATUS 
DriverTestEntryPoint (
    IN EFI_HANDLE        ImageHandle,
    IN EFI_SYSTEM_TABLE *SystemTable
    ) 
{
    EFI_STATUS Status;

    Status = gBS->InstallProtocolInterface (
                  &ImageHandle,
                  &gEfiDriverTestGuid,
                  EFI_NATIVE_INTERFACE,
                  &mDriverTestProtocol                    
                );

    return Status;
}

调用该driver的代码,以efi app的格式就可以了:

#include <Uefi.h>
#include <Library/UefiLib.h>
#include <Library/UefiBootServicesTableLib.h>

EFI_GUID gEfiDriverTestGuid = {0xc56ce2ec, 0x50b5, 0x42bc, {0x82, 0x4b, 0x33, 0xaf, 0xa6, 0x7b, 0x17, 0x98}};

typedef
EFI_STATUS
(*DRIVER_TEST) (
  VOID
  );
  
typedef struct _EFI_DRIVER_TEST_PROTOCOL
{
    DRIVER_TEST PrintHello;
} EFI_DRIVER_TEST_PROTOCOL;

EFI_STATUS
EFIAPI
UefiMain (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
    EFI_STATUS                         Status;
    EFI_DRIVER_TEST_PROTOCOL           *DriverTest;

    Status = gBS->LocateProtocol (&gEfiDriverTestGuid, NULL, (VOID **) &DriverTest);
    if (EFI_ERROR (Status)) {
      Print (L"Can not find the protocol\n\r");
      return EFI_UNSUPPORTED;
    }

    DriverTest->PrintHello();

    return EFI_SUCCESS;
}

 

在shell下执行需要先使用load指令将DXE driver加载到内存,然后再运行相应的app,结果如下:

3.DXE Driver的调用

a. 以代码的形式直接将生成的efi编译到固件中,以nt32为例,需要在Nt32Pkg\Nt32Pkg.dsc中加入:

MdeModulePkg/Application/DriverTest/DriverTest.inf
这样将会编译DriverTest.inf

Nt32Pkg\Nt32Pkg.fdf 加入:

INF MdeModulePkg/Application/DriverTest/DriverTest.inf

这将把编译的efi包进fd固件中。这是最原始的方法,此时在shell测试无需再load,直接可调用。

b. 以efi文件的形式编译进固件。这种方法无需代码,只需要一个efi文件即可,常见的undi driver便是如此。inf文件格式如下:

[Defines]
  INF_VERSION    = 0x00010009
  BASE_NAME      = DriverTest
  FILE_GUID      = 961578FE-B6B7-44c3-AF35-6BC705CD2B1F
  MODULE_TYPE    = DXE_DRIVER
  VERSION_STRING = 1.0

#
# The following information is for reference only and not required by the
# build tools.
#
# VALID_ARCHITECTURES = IA32 X64 IPF EBC
#

[Binaries]
  PE32|DriverTest.efi

 

版权声明:
作者:bin
链接:https://ay123.net/mystudy/835/
来源:爱影博客
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>