uefi驱动和普通的uefi应用程序一样,只是,驱动入口进入后,马上结束,但是结束后驱动依然挂载在内存中,其它应用程序可以通过guid和protocol安装该驱动,这样其它应用程序就可以使用该驱动中protocol提供的函数了。
就像diskio和blockio可以直接调用,自己写的驱动也可以直接调用,但是diskio和blockio是基础驱动,必须提供,所以bios启动时会自动加载这些基础且重要的驱动,我们自己写的驱动要使用load命令,如load firstdriver.efi。然后我们的firstdriver.efi便挂载到内存,供我们调用。
写驱动程序和应用程序一样,只是主函数入口内要调用InstallProtocolInterface,以使自己的驱动常驻内存(有bootservice 和 runtime两种,一种bootservice在os启动后释放,另一种runtime伴随os消亡)
---------------firstdriver.h文件----------------------
#ifndef __EFI_FIRSTDRIVER_H__
#define __EFI_FIRSTDRIVER_H__
#include <Uefi.h>
//
// Libraries
//
#include <Library/UefiBootServicesTableLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/BaseLib.h>
#include <Library/UefiLib.h>
#include <Library/DevicePathLib.h>
#include <Library/DebugLib.h>
//
// UEFI Driver Model Protocols
//
#include <Protocol/DriverBinding.h>
//
// Consumed Protocols
//
#include <Protocol/PciIo.h>
#define EFI_FIRSTDRIVER_PROTOCOL_GUID \
{0x06cbff21, 0x93df, 0x11e4, {0xb8, 0x25, 0x74, 0xd4, 0x35, 0xde, 0xc6, 0x2b}}
///
/// Forward declaration
///
typedef struct _EFI_FIRSTDRIVER_PROTOCOL EFI_FIRSTDRIVER_PROTOCOL;
///
/// Function prototypes
///
EFI_STATUS Shows(IN EFI_FIRSTDRIVER_PROTOCOL *This);
typedef
EFI_STATUS
(EFIAPI *EFI_FIRSTDRIVER_FUNCTION)(
IN EFI_FIRSTDRIVER_PROTOCOL *This
//
// Place additional function arguments here
//
);
///
/// Protocol structure
///
typedef struct _EFI_FIRSTDRIVER_PROTOCOL {
INT8 ia;
EFI_FIRSTDRIVER_FUNCTION Func1;
//
// Place protocol data fields here
//
};
extern EFI_GUID gEfifirstdriverProtocolGuid;
static EFI_FIRSTDRIVER_PROTOCOL gEfifirstdriverProtocol;
#endif
----------------------------------firstdriver.c文件--------------------
#include "firstdriver.h"
EFI_STATUS Shows(IN EFI_FIRSTDRIVER_PROTOCOL *This)
{
EFI_STATUS Status = EFI_SUCCESS;
Print(L"hello _ world %d\n",This->ia);
return Status;
}
EFI_STATUS
EFIAPI
firstdriverDriverEntryPoint (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
EFI_STATUS Status;// EFI_FIRSTDRIVER_PROTOCOL firstdriver = {20, Shows};
Status = EFI_SUCCESS;
gEfifirstdriverProtocol.Func1 = Shows;
gEfifirstdriverProtocol.ia = 20;
//
// Install UEFI Driver Model protocol(s).
//
Status = gBS->InstallProtocolInterface (
&ImageHandle,
&gEfifirstdriverProtocolGuid,
EFI_NATIVE_INTERFACE,
&gEfifirstdriverProtocol
);
ASSERT_EFI_ERROR (Status);
if(EFI_SUCCESS == Status)
{
Print(L"success to install protocol\n");
}
else
{
Print(L"fialed to install protocol\n");
}
return Status;
}
----------firstdriver.dec文件-------
[Defines]
DEC_SPECIFICATION = 0x00010005
PACKAGE_NAME = firstdriver
PACKAGE_GUID = f3a4e3d1-93de-11e4-b4fe-74d435dec62b
PACKAGE_VERSION = 1.0
[Includes]
Include
[LibraryClasses]
[Protocols]
gEfifirstdriverProtocolGuid = {0x06cbff21, 0x93df, 0x11e4, {0xb8, 0x25, 0x74, 0xd4, 0x35, 0xde, 0xc6, 0x2b}}
------firstdriver.dsc文件-----------
[Defines]
PLATFORM_NAME = firstdriver
PLATFORM_GUID = f3a754cf-93de-11e4-9fec-74d435dec62b
PLATFORM_VERSION = 1.0
DSC_SPECIFICATION = 0x00010005
OUTPUT_DIRECTORY = Build/firstdriver
SUPPORTED_ARCHITECTURES = IA32|IPF|X64|EBC|ARM
BUILD_TARGETS = DEBUG|RELEASE
SKUID_IDENTIFIER = DEFAULT
[LibraryClasses]
UefiDriverEntryPoint|MdePkg/Library/UefiDriverEntryPoint/UefiDriverEntryPoint.inf
UefiApplicationEntryPoint|MdePkg/Library/UefiApplicationEntryPoint/UefiApplicationEntryPoint.inf
UefiBootServicesTableLib|MdePkg/Library/UefiBootServicesTableLib/UefiBootServicesTableLib.inf
UefiLib|MdePkg/Library/UefiLib/UefiLib.inf
UefiRuntimeServicesTableLib|MdePkg/Library/UefiRuntimeServicesTableLib/UefiRuntimeServicesTableLib.inf
UefiRuntimeLib|MdePkg/Library/UefiRuntimeLib/UefiRuntimeLib.inf
MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf
DevicePathLib|MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf
UefiUsbLib|MdePkg/Library/UefiUsbLib/UefiUsbLib.inf
UefiScsiLib|MdePkg/Library/UefiScsiLib/UefiScsiLib.inf
BaseLib|MdePkg/Library/BaseLib/BaseLib.inf
BaseMemoryLib|MdePkg/Library/BaseMemoryLib/BaseMemoryLib.inf
SynchronizationLib|MdePkg/Library/BaseSynchronizationLib/BaseSynchronizationLib.inf
PrintLib|MdePkg/Library/BasePrintLib/BasePrintLib.inf
DebugLib|MdePkg/Library/UefiDebugLibStdErr/UefiDebugLibStdErr.inf
DebugPrintErrorLevelLib|MdePkg/Library/BaseDebugPrintErrorLevelLib/BaseDebugPrintErrorLevelLib.inf
PostCodeLib|MdePkg/Library/BasePostCodeLibPort80/BasePostCodeLibPort80.inf
PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf
[Components]
firstdriver/firstdriver/firstdriver.inf
-----firstdriver.inf文件--------
[Defines]
INF_VERSION = 0x00010005
#BASE_NAME = firstdriverx
BASE_NAME = firstdriver
FILE_GUID = 15ac4900-93df-11e4-a510-74d435dec62b
MODULE_TYPE = UEFI_DRIVER
VERSION_STRING = 1.0
ENTRY_POINT = firstdriverDriverEntryPoint
[Packages]
MdePkg/MdePkg.dec
firstdriver/firstdriver.dec
[Sources]
firstdriver.h
firstdriver.c
[LibraryClasses]
UefiDriverEntryPoint
UefiBootServicesTableLib
MemoryAllocationLib
BaseMemoryLib
BaseLib
UefiLib
DevicePathLib
DebugLib
[Protocols]
gEfifirstdriverProtocolGuid