UEFI EDK2框架学习(三)——protocol

一、Protocol协议

  • 搜索支持特定Protocol的设备,获取其Handle

gBS->LocateHandleBuffer

  • 将内存中的Driver绑定到给定的ControllerHandle

gBS->OpenProtocol

二、代码实现

Protocol.c

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

EFI_STATUS
EFIAPI
UefiMain(
  IN EFI_HANDLE ImageHandle,
  IN EFI_SYSTEM_TABLE *SystemTable
) {
  EFI_STATUS Status = EFI_SUCCESS;
  UINTN NoHandles = 0;
  EFI_HANDLE *Buffer = NULL;
  Status = gBS->LocateHandleBuffer(
    ByProtocol,
    &gEfiGraphicsOutputProtocolGuid,
    NULL,
    &NoHandles,
    &Buffer
  );

  Print(L"Status = %d", Status);
  if (EFI_ERROR(Status)) {
    Print(L"Failed to LocateHandleBuffer. \n");
    return Status;
  }
  Print(L"Hello, Protocol\n");

  EFI_GRAPHICS_OUTPUT_PROTOCOL *Gop;
  Status = gBS->OpenProtocol(
    Buffer[0],
    &gEfiGraphicsOutputProtocolGuid,
    (VOID**)&Gop,
    ImageHandle,
    NULL,
    EFI_OPEN_PROTOCOL_GET_PROTOCOL
  );

  Print(L"Status = %d", Status);
  if (EFI_ERROR(Status)) {
    Print(L"Failed to OpenProtocol. \n");
    return Status;
  }
  return Status;
}

Protocol.inf

[Defines]
  INF_VERSION = 0x00010006
  BASE_NAME = Protocol
  FILE_GUID = 767c2824-dbe2-4a8f-b2f5-8a10f23e7853
  MODULE_TYPE = UEFI_APPLICATION
  VERSION_STRING = 0.1
  ENTRY_POINT = UefiMain

[Sources]
  HelloWorld.c

[Packages]
  MdePkg/MdePkg.dec

[LibraryClasses]
  UefiApplicationEntryPoint
  UefiLib

ProtocolPkg.dsc

[Defines]
  PLATFORM_NAME = ProtocolPkg
  PLATFORM_GUID = 767c2824-dbe2-4a8f-b2f5-8a10f23e7853
  PLATFORM_VERSION = 0.1
  DSC_SPECIFICATION = 0x00010005
  SUPPORTED_ARCHITECTURES = X64
  BUILD_TARGETS = DEBUG|RELEASE
  OUTPUT_DIRECTORY = ProtocolPkg/Build

[LibraryClasses]
  UefiLib|MdePkg/Library/UefiLib/UefiLib.inf
  UefiApplicationEntryPoint|MdePkg/Library/UefiApplicationEntryPoint/UefiApplicationEntryPoint.inf
  PrintLib|MdePkg/Library/BasePrintLib/BasePrintLib.inf
  PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf
  MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf
  DebugLib|MdePkg/Library/UefiDebugLibConOut/UefiDebugLibConOut.inf
  BaseMemoryLib|MdePkg/Library/BaseMemoryLib/BaseMemoryLib.inf
  BaseLib|MdePkg/Library/BaseLib/BaseLib.inf
  UefiBootServicesTableLib|MdePkg/Library/UefiBootServicesTableLib/UefiBootServicesTableLib.inf
  DevicePathLib|MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf
  UefiRuntimeServicesTableLib|MdePkg/Library/UefiRuntimeServicesTableLib/UefiRuntimeServicesTableLib.inf
  RegisterFilterLib|MdePkg/Library/RegisterFilterLibNull/RegisterFilterLibNull.inf
  DebugPrintErrorLevelLib|MdePkg/Library/BaseDebugPrintErrorLevelLib/BaseDebugPrintErrorLevelLib.inf
[Components]
  ProtocolPkg/Protocol.inf

三、参考文献

UEFI——UEFI 基础服务_installprotocolinterface-CSDN博客

UEFI——protocol服务详解_uefi protocol-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

-特立独行的猪-

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

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

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

打赏作者

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

抵扣说明:

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

余额充值