UEFI驱动代码

UEFI驱动模型

UEFI一个完整得驱动分为:

  • EFI Driver Binding Protocol
  • 驱动服务
  • EFI Component Protocol 为用户提供接口

EFI Driver Binding Protocol

结构体描述
  1. typedef struct _EFI_DRIVER_BINDING_PROTOCOL EFI_DRIVER_BINDING_PROTOCOL;
  2. struct _EFI_DRIVER_BINDING_PROTOCOL {
  3. EFI_DRIVER_BINDING_SUPPORTED Supported; // 检测某一个设备是否支持该驱动
  4. EFI_DRIVER_BINDING_START Start; // 驱动安装接口
  5. EFI_DRIVER_BINDING_STOP Stop; // 驱动卸载接口
  6. UINT32 Version; // EDBP版本号
  7. EFI_HANDLE ImageHandle; // EDBP映像文件句柄
  8. EFI_HANDLE DriverBindingHandle; // 安装EDBPHandle
  9. };

驱动加载接口

edk2\MdeModulePkg\Core\Dxe\Hand 目录提供了以下几个接口

  1. EFI_STATUS EFIAPI
  2. CoreConnectController (
  3. IN EFI_HANDLE ControllerHandle,
  4. IN EFI_HANDLE *DriverImageHandle OPTIONAL,
  5. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL,
  6. IN BOOLEAN Recursive
  7. );
  8. EFI_STATUS
  9. CoreConnectSingleController (
  10. IN EFI_HANDLE ControllerHandle,
  11. IN EFI_HANDLE *ContextDriverImageHandles OPTIONAL,
  12. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
  13. );
  14. EFI_STATUS
  15. EFIAPI
  16. CoreDisconnectController (
  17. IN EFI_HANDLE ControllerHandle,
  18. IN EFI_HANDLE DriverImageHandle OPTIONAL,
  19. IN EFI_HANDLE ChildHandle OPTIONAL
  20. );

PCI驱动

协议注册

  1. //
  2. // PCI Bus Driver Global Variables
  3. //
  4. EFI_DRIVER_BINDING_PROTOCOL gPciBusDriverBinding = {
  5. PciBusDriverBindingSupported,
  6. PciBusDriverBindingStart,
  7. PciBusDriverBindingStop,
  8. 0xa,
  9. NULL,
  10. NULL
  11. };

初始化代码

  1. PciBusDriverBindingStart
  2. PciEnumerator
  3. PciHostBridgeEnumerator

打印信息

  1. PCI Bus First Scanning
  2. PciBus: Discovered PCI @ [00|00|00]
  3. PciBus: Discovered PCI @ [00|01|00]
  4. PciBus: Discovered PCI @ [00|01|01]
  5. BAR[4]: Type = Io32; Alignment = 0xF; Length = 0x10; Offset = 0x20
  6. PciBus: Discovered PCI @ [00|01|03]
  7. PciBus: Discovered PCI @ [00|02|00]
  8. BAR[0]: Type = PMem32; Alignment = 0xFFFFFF; Length = 0x1000000; Offset = 0x10
  9. BAR[2]: Type = Mem32; Alignment = 0xFFF; Length = 0x1000; Offset = 0x18
  10. PciBus: Discovered PCI @ [00|03|00]
  11. BAR[0]: Type = Mem32; Alignment = 0x1FFFF; Length = 0x20000; Offset = 0x10
  12. BAR[1]: Type = Io32; Alignment = 0x3F; Length = 0x40; Offset = 0x14
  13. PCI Bus Second Scanning
  14. PciBus: Discovered PCI @ [00|00|00]
  15. PciBus: Discovered PCI @ [00|01|00]
  16. PciBus: Discovered PCI @ [00|01|01]
  17. BAR[4]: Type = Io32; Alignment = 0xF; Length = 0x10; Offset = 0x20
  18. PciBus: Discovered PCI @ [00|01|03]
  19. PciBus: Discovered PCI @ [00|02|00]
  20. BAR[0]: Type = PMem32; Alignment = 0xFFFFFF; Length = 0x1000000; Offset = 0x10
  21. BAR[2]: Type = Mem32; Alignment = 0xFFF; Length = 0x1000; Offset = 0x18
  22. PciBus: Discovered PCI @ [00|03|00]
  23. BAR[0]: Type = Mem32; Alignment = 0x1FFFF; Length = 0x20000; Offset = 0x10
  24. BAR[1]: Type = Io32; Alignment = 0x3F; Length = 0x40; Offset = 0x14
  25. PciBus: Discovered PCI @ [00|00|00]
  26. PciBus: Discovered PCI @ [00|01|00]
  27. PciBus: Discovered PCI @ [00|01|01]
  28. BAR[4]: Type = Io32; Alignment = 0xF; Length = 0x10; Offset = 0x20
  29. PciBus: Discovered PCI @ [00|01|03]
  30. PciBus: Discovered PCI @ [00|02|00]
  31. BAR[0]: Type = PMem32; Alignment = 0xFFFFFF; Length = 0x1000000; Offset = 0x10
  32. BAR[2]: Type = Mem32; Alignment = 0xFFF; Length = 0x1000; Offset = 0x18
  33. PciBus: Discovered PCI @ [00|03|00]
  34. BAR[0]: Type = Mem32; Alignment = 0x1FFFF; Length = 0x20000; Offset = 0x10
  35. BAR[1]: Type = Io32; Alignment = 0x3F; Length = 0x40; Offset = 0x14
  36. PciHostBridge: SubmitResources for PciRoot(0x0)
  37. I/O: Granularity/SpecificFlag = 0 / 01
  38. Length/Alignment = 0x1000 / 0xFFF
  39. Mem: Granularity/SpecificFlag = 32 / 00
  40. Length/Alignment = 0x1100000 / 0xFFFFFF
  41. PciBus: HostBridge->SubmitResources() - Success
  42. PciHostBridge: NotifyPhase (AllocateResources)
  43. RootBridge: PciRoot(0x0)
  44. Mem: Base/Length/Alignment = 80000000/1100000/FFFFFF - Success
  45. I/O: Base/Length/Alignment = C000/1000/FFF - Success
  46. PciBus: HostBridge->NotifyPhase(AllocateResources) - Success
  47. PciBus: Resource Map for Root Bridge PciRoot(0x0)
  48. Type = Io16; Base = 0xC000; Length = 0x1000; Alignment = 0xFFF
  49. Base = 0xC000; Length = 0x40; Alignment = 0x3F; Owner = PCI [00|03|00:14]
  50. Base = 0xC040; Length = 0x10; Alignment = 0xF; Owner = PCI [00|01|01:20]
  51. Type = Mem32; Base = 0x80000000; Length = 0x1100000; Alignment = 0xFFFFFF
  52. Base = 0x80000000; Length = 0x1000000; Alignment = 0xFFFFFF; Owner = PCI [00|02|00:10]; Type = PMem32
  53. Base = 0x81000000; Length = 0x20000; Alignment = 0x1FFFF; Owner = PCI [00|03|00:10]
  54. Base = 0x81020000; Length = 0x1000; Alignment = 0xFFF; Owner = PCI [00|02|00:18]