Protocol是什么

Protocol就是对每一类设备 抽象后得总称。 IHANDLE就是一个个设备句柄。

Protocol得描述

UEFI在扫描总线后,会 为每一个设备建立一个Controller对象,用于描述和控制这个设备
这个 对象句柄就是 EFI_HANDLE(UEFI中,这个句柄事IHANDLE)

EFI_HANDLE 对象句柄描述

  1. typedef struct {
  2. UINTN Signature; // handle得类别
  3. LIST_ENTRY AllHandles; // 所有设备得IHANDLE 链表, 也就是 所有设备链,遍历设备用。
  4. LIST_ENTRY Protocols; // HandleProtocol链表,查找自己得protocol使用
  5. UINTN LocateRequest;
  6. UINT64 Key;
  7. } IHANDLE;

Prorocol接口描述

  1. ///
  2. /// PROTOCOL_INTERFACE - each protocol installed on a handle is tracked
  3. /// with a protocol interface structure
  4. ///
  5. typedef struct {
  6. UINTN Signature;
  7. /// Link on IHANDLE.Protocols
  8. LIST_ENTRY Link;
  9. /// Back pointer
  10. IHANDLE *Handle;
  11. /// Link on PROTOCOL_ENTRY.Protocols
  12. LIST_ENTRY ByProtocol;
  13. /// The protocol ID
  14. PROTOCOL_ENTRY *Protocol; // 可获取PROTOCOLGUID
  15. /// The interface value
  16. VOID *Interface; // 可获取protocol实例
  17. /// OPEN_PROTOCOL_DATA list
  18. LIST_ENTRY OpenList;
  19. UINTN OpenListCount;
  20. } PROTOCOL_INTERFACE;

EFI_HANDLE和Protocol得关系图

image.png

protocol得使用

gBS中protocol得方法

  1. EFI_BOOT_SERVICES *gBS;
  2. typedef struct { // 协议相关
  3. //
  4. // !!! Open and Close Protocol Services 最常用得就是 打开,使用,关闭 protocol
  5. //
  6. EFI_OPEN_PROTOCOL OpenProtocol;
  7. EFI_CLOSE_PROTOCOL CloseProtocol;
  8. EFI_HANDLE_PROTOCOL HandleProtocol;
  9. // Protocol Handler Services
  10. EFI_INSTALL_PROTOCOL_INTERFACE InstallProtocolInterface;
  11. EFI_REINSTALL_PROTOCOL_INTERFACE ReinstallProtocolInterface;
  12. EFI_UNINSTALL_PROTOCOL_INTERFACE UninstallProtocolInterface;
  13. EFI_HANDLE_PROTOCOL HandleProtocol;
  14. VOID *Reserved;
  15. EFI_REGISTER_PROTOCOL_NOTIFY RegisterProtocolNotify;
  16. EFI_LOCATE_HANDLE LocateHandle;
  17. EFI_LOCATE_DEVICE_PATH LocateDevicePath;
  18. EFI_INSTALL_CONFIGURATION_TABLE InstallConfigurationTable;
  19. // Library Services
  20. EFI_PROTOCOLS_PER_HANDLE ProtocolsPerHandle;
  21. EFI_LOCATE_HANDLE_BUFFER LocateHandleBuffer;
  22. EFI_LOCATE_PROTOCOL LocateProtocol;
  23. EFI_INSTALL_MULTIPLE_PROTOCOL_INTERFACES InstallMultipleProtocolInterfaces;
  24. EFI_UNINSTALL_MULTIPLE_PROTOCOL_INTERFACES UninstallMultipleProtocolInterfaces;
  25. }

EFI_STATUS

  1. @retval EFI_SUCCESS An item was added to the open list for the protocol interface, and the
  2. protocol interface was returned in Interface. 成功打开
  3. @retval EFI_UNSUPPORTED Handle does not support Protocol. handle不支持Protocol
  4. @retval EFI_INVALID_PARAMETER One or more parameters are invalid. 无效参数
  5. @retval EFI_ACCESS_DENIED Required attributes can't be supported in current environment. 当前环境不支持attribute属性打开
  6. @retval EFI_ALREADY_STARTED Item on the open list already has requierd attributes whose agent Protocol已经被同一个AgentHandle打开
  7. handle is the same as AgentHandle.

OPEN 和 CLOSE

  1. /**
  2. Queries a handle to determine if it supports a specified protocol. If the protocol is supported by the
  3. handle, it opens the protocol on behalf of the calling agent.
  4. @param[in] Handle The handle for the protocol interface that is being opened.
  5. @param[in] Protocol The published unique identifier of the protocol.
  6. @param[out] Interface Supplies the address where a pointer to the corresponding Protocol
  7. Interface is returned.
  8. @param[in] AgentHandle The handle of the agent that is opening the protocol interface
  9. specified by Protocol and Interface.
  10. @param[in] ControllerHandle If the agent that is opening a protocol is a driver that follows the
  11. UEFI Driver Model, then this parameter is the controller handle
  12. that requires the protocol interface. If the agent does not follow
  13. the UEFI Driver Model, then this parameter is optional and may
  14. be NULL.
  15. @param[in] Attributes The open mode of the protocol interface specified by Handle
  16. and Protocol.
  17. @retval EFI_SUCCESS An item was added to the open list for the protocol interface, and the
  18. protocol interface was returned in Interface.
  19. @retval EFI_UNSUPPORTED Handle does not support Protocol.
  20. @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
  21. @retval EFI_ACCESS_DENIED Required attributes can't be supported in current environment.
  22. @retval EFI_ALREADY_STARTED Item on the open list already has requierd attributes whose agent
  23. handle is the same as AgentHandle.
  24. **/
  25. typedef
  26. EFI_STATUS
  27. (EFIAPI *EFI_OPEN_PROTOCOL)(
  28. IN EFI_HANDLE Handle, // 指定得handle,
  29. IN EFI_GUID *Protocol, // 要打开GUID对应得protocol
  30. OUT VOID **Interface, OPTIONAL // 返回打开得protocol对象
  31. IN EFI_HANDLE AgentHandle, // 打开protocol得Image
  32. IN EFI_HANDLE ControllerHandle, // 使用此protocol得控制器
  33. IN UINT32 Attributes // 打开protocol得方式
  34. );
  35. typedef
  36. EFI_STATUS
  37. (EFIAPI *EFI_CLOSE_PROTOCOL)(
  38. IN EFI_HANDLE Handle,
  39. IN EFI_GUID *Protocol,
  40. IN EFI_HANDLE AgentHandle,
  41. IN EFI_HANDLE ControllerHandle
  42. );
  43. typedef
  44. EFI_STATUS
  45. (EFIAPI *EFI_HANDLE_PROTOCOL)(
  46. IN EFI_HANDLE Handle,
  47. IN EFI_GUID *Protocol,
  48. OUT VOID **Interface
  49. );

Attributes打开属性

  1. #define EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL 0x00000001
  2. #define EFI_OPEN_PROTOCOL_GET_PROTOCOL 0x00000002
  3. #define EFI_OPEN_PROTOCOL_TEST_PROTOCOL 0x00000004
  4. #define EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER 0x00000008
  5. #define EFI_OPEN_PROTOCOL_BY_DRIVER 0x00000010
  6. #define EFI_OPEN_PROTOCOL_EXCLUSIVE 0x00000020

protocol得开发