UEFI开发——调用一个自己编写的protocol

文章详细阐述了如何在UEFI环境中定义一个名为EFI_COMPARE_PROTOCOL的协议,包括生成GUID、编写结构体和函数指针。接着,介绍了如何安装此协议,使用AllocatePool和InstallProtocolInterface函数。最后,展示了如何在其他模块中定位并使用该协议。整个过程涉及UEFI驱动开发的关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

编写一个protocol并调用分三个部分,定义一个protocol,安装这个protocol,使用这个protocol。
源码下载:https://download.csdn.net/download/mao0514/87588583
首先列出我的工程目录:
D:\Mywork\edk2\MdeModulePkg\Application\myProtocol
UEFIStart是我创建的文件夹,在UEFIStart下有两个文件夹:

   分别存放protocol的安装代码和使用代码,每个文件夹有一个.c文件和.inf文件。
   **而定义protocol的文件在G:\edk2-UDK2017\Nt32Pkg\Include\Protocol下,是一个.h文件(Compare.h):**

   同样,对于工程文件的类型比如.inf、.dec、.dsc等文件的作用也不多说,参考《UEFI原理与编程》。

1.定义一个protocol
代码:

#ifndef UEFIMAIN_COMPARE_PROTOCOL_H
#define UEFIMAIN_COMPARE_PROTOCOL_H

#include<Uefi.h>

#define EFI_CPMPARE_PROTOCOL_GUID
{0xee7ba45e, 0x9642, 0x4a97, {0x83, 0xc3, 0x30, 0xeb, 0xed, 0x3f, 0x9d, 0xd6}} //定义这个protocol的GUID

typedef struct _EFI_COMPARE_PROTOCOL EFI_COMPARE_PROTOCOL;

typedef EFI_STATUS (EFIAPI *COMPARE)( //定义一个函数指针,包括EFI_COMPARE_PROTOCOL类型的指针和两个要比较大小的数
IN EFI_COMPARE_PROTOCOL *This,
IN UINT8 a,
IN UINT8 b
);

struct _EFI_COMPARE_PROTOCOL { //定义这个protocol的结构体,因为只实现比较大小,所以成员只有一个函数指针类型的成员
COMPARE compare;
};

extern EFI_GUID gEfiCompareProtocolGuid; //唯一的protocol,后面也会提到

#endif

   在Compare.h文件中extern EFI_GUID gEfiCompareProtocolGuid;这一句的gEfiCompareProtocolGuid是在Nt32Pkg的.dec文件中的[Protocols]中声明的,内容和Compare.h文件中的值一样:

   GUID的值我是用VS2013自带的生成工具生成的,
   必须在该目录下打开,如果光把guidgen.exe移到桌面或其他地方会出错。每个.inf文件也要有guid,也可以用这个生成。

   我之所以直接放在Nt32Pkg下面是因为我对工程文件还不熟悉,Nt32Pkg下面有现成的.dec、.dsc、.fdf文件,我只需要向这些文件中添加必要的内容即可,具体什么内容后面会指出(比如向.dec文件中的[Protocols]添加gEfiCompareProtocolGuid的声明)。

2.安装protocol
代码:

#include<Uefi.h>
#include<Library/UefiBootServicesTableLib.h>
#include<Library/MemoryAllocationLib.h>
#include<Library/DebugLib.h>

#include<Protocol/Compare.h>

EFI_STATUS EFIAPI Compare(IN EFI_COMPARE_PROTOCOL *This,IN UINT8 a,IN UINT8 b) //protocol要实现的功能
{
DEBUG((EFI_D_ERROR, “The Max Number is: %d\n”,(a>b?a:b)));
return EFI_SUCCESS;
}

EFI_STATUS EFIAPI
ProtocolInstallerEntry(
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
EFI_STATUS Status;
EFI_COMPARE_PROTOCOL *Protocol;

Protocol = AllocatePool(sizeof(EFI_COMPARE_PROTOCOL));
if(NULL == Protocol){
  DEBUG((EFI_D_ERROR, "[Compare][%s][%d]:Out of resource.",__FUNCTION__, __LINE__));
  return EFI_OUT_OF_RESOURCES;
}

Protocol->compare = Compare;

Status = gBS->InstallProtocolInterface(
    &ImageHandle,
    &gEfiCompareProtocolGuid,
    EFI_NATIVE_INTERFACE,
    Protocol
);

if(EFI_ERROR(Status)){
  DEBUG((EFI_D_ERROR, "[Compare]Install EFI_COMPARE_PROTOCOL failed. - %d\n", Status));
  FreePool(Protocol);
  return Status;
}

return EFI_SUCCESS;

}

   安装protocol调用两个函数(说函数也行,接口也行,反正就是一个有功能的东西),在这里我把它的函数实现列举出来:
   AllocatePool

/**
Allocates a buffer of type EfiBootServicesData.

Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.

@param AllocationSize The number of bytes to allocate.

@return A pointer to the allocated buffer or NULL if allocation fails.

*/
VOID *
EFIAPI
AllocatePool (
IN UINTN AllocationSize
)
{
return (VOID
) malloc (AllocationSize);
}

   InstallProtocolInterface是一个函数指针类型: EFI_INSTALL_PROTOCOL_INTERFACE InstallProtocolInterface;
   EFI_INSTALL_PROTOCOL_INTERFACE这个函数指针的声明如下:

//
// Protocol handler functions
//
typedef enum {
EFI_NATIVE_INTERFACE
} EFI_INTERFACE_TYPE;

typedef
EFI_BOOTSERVICE
EFI_STATUS
(EFIAPI *EFI_INSTALL_PROTOCOL_INTERFACE) (
IN OUT EFI_HANDLE * Handle,
IN EFI_GUID * Protocol,
IN EFI_INTERFACE_TYPE InterfaceType,
IN VOID *Interface
);

   还有两个函数DEGBUG和FreePool我也列举出来:
   DEGBUG

//
// DEBUG((DebugLevel, “format string”, …)) - if DebugLevel is active do
// the a debug print.
//
#define DEBUG(arg) EfiDebugPrint arg

   这个函数本来是输出错误信息的,但是我为了方便,就调用它显示我的结果,其中EfiDebugPrint 的定义是:

VOID
EfiDebugPrint (
IN UINTN ErrorLevel,
IN CHAR8 *Format,

)

   FreePool

/**
Frees a buffer that was previously allocated with one of the pool allocation functions in the
Memory Allocation Library.

Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
pool allocation services of the Memory Allocation Library. If it is not possible to free pool
resources, then this function will perform no actions.

If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
then ASSERT().

@param Buffer Pointer to the buffer to free.

**/
VOID
EFIAPI
FreePool (
IN VOID *Buffer
)
{
free ((void *) Buffer);
}

   还有就是,调用的函数要声明它的头文件,要找到函数的头文件。

2.使用Protocol
#include <Uefi.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/DebugLib.h>

#include <Protocol/Compare.h>

EFI_STATUS EFIAPI ProtocolUserEntry(
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
EFI_STATUS Status;
EFI_COMPARE_PROTOCOL *Protocol;

Status = gBS->LocateProtocol(&gEfiCompareProtocolGuid, NULL, (VOID **)&Protocol);
if(EFI_ERROR(Status)){
  DEBUG((EFI_D_ERROR, "[BENI]Locate EFI_COMPARE_PROTOCOL failed. - %r\n",Status));
  return Status;
}

Status = Protocol->compare(Protocol,5,9);
if(EFI_ERROR(Status)){
  DEBUG((EFI_D_ERROR, "[BENI]Protocol->Compare failed. - %r\n", Status));
  return EFI_SUCCESS;
}

return EFI_SUCCESS;

}

   用LocateProtocol定位到protocol,然后调用,这里我给compare传的参数是5和9。
   其中LocateProtocol是EFI_LOCATE_PROTOCOL LocateProtocol;
   EFI_LOCATE_PROTOCOL是个函数指针:

typedef
EFI_BOOTSERVICE11
EFI_STATUS
(EFIAPI *EFI_LOCATE_PROTOCOL) (
EFI_GUID * Protocol,
VOID *Registration, OPTIONAL
VOID **Interface
);

   这些做完了以后还要配置Nt32Pkg的.dec、.dsc、.fdf文件,.dec文件已经说过了,在.dsc文件中,要在[Components]下添加:
    Nt32Pkg/UEFIStart/ProtocolInstaller/ProtocolInstaller.inf
    Nt32Pkg/UEFIStart/ProtocolUser/ProtocolUser.inf
    在.fdf文件中,要在DXE Phase modules下添加:
    INF Nt32Pkg/UEFIStart/ProtocolInstaller/ProtocolInstaller.inf
   INF Nt32Pkg/UEFIStart/ProtocolUser/ProtocolUser.inf

3.运行结果

   因为我写的是UEFI_DRIVER所以要用load命令加载。

   比较5和9的大小,并将结果打印出来。

   在写protocol的过程中遇到了很多问题,其中编写.inf文件遇到的问题最多,原因还是因为我对.inf文件不是很了解,以后会继续学习,加油。
   附上两个文件的.inf:
   ①ProtocolInstaller.inf

[Defines]
INF_VERSION = 0x00010005
BASE_NAME = ProtocolInstaller
FILE_GUID = 8D952513-D3B5-43C7-87DF-CD3B382D65F7
MODULE_TYPE = UEFI_DRIVER
VERSION_STRING = 1.0
ENTRY_POINT = ProtocolInstallerEntry

[Sources.common]
ProtocolInstaller.c

[Packages]

Nt32Pkg/Nt32Pkg.dec
MdePkg/MdePkg.dec

[LibraryClasses]

UefiDriverEntryPoint
UefiBootServicesTableLib
MemoryAllocationLib
DebugLib

[Protocols]

gEfiCompareProtocolGuid

[Depex]
TRUE

   ②ProtocolUser.inf

[Defines]
INF_VERSION = 0x00010005
BASE_NAME = ProtocolUser
FILE_GUID = 16BEFBED-60DC-4EA2-8E81-A3430A6CD6D5
MODULE_TYPE = UEFI_DRIVER
VERSION_STRING = 1.0
ENTRY_POINT = ProtocolUserEntry

[Sources.common]
ProtocolUser.c

[Packages]

Nt32Pkg/Nt32Pkg.dec
MdePkg/MdePkg.dec

[LibraryClasses]
UefiDriverEntryPoint
UefiBootServicesTableLib
DebugLib

[Protocols]
gEfiCompareProtocolGuid

[Depex]
TRUE

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

毛毛虫的爹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值