一、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博客