这次的代码是根据之前的文章【参考1】修改而来,将当前系统中 Simple Simple Input Protocol 中的ReadKeyStroke()函数替换为自定的。为了便于验证,如果当前返回了 ‘z’ ,那么会将这个替换为’a’。
完整的代码如下:
#include <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiDriverEntryPoint.h>
#include <Library/UefiLib.h>
#include <PiDxe.h>
#include <Protocol/SimpleFileSystem.h>
extern EFI_SYSTEM_TABLE *gST;
EFI_GUID gEfiSimpleTextInputProtocolGuid = {
0x387477c1,
0x69c7,
0x11d2,
{0x8e, 0x39, 0x0, 0xa0, 0xc9, 0x69, 0x72, 0x3b}};
// EFI_FILE_PROTOCOL *Root;
EFI_SIMPLE_TEXT_INPUT_PROTOCOL *SimpleInput;
EFI_INPUT_READ_KEY OldReadKeyStroke;
// This one will replace the Read function of Read in Simple File System
EFI_STATUS
EFIAPI
MyReadKeyStroke(IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This,
OUT EFI_INPUT_KEY *Key) {
//EFI_INPUT_KEY IKey;
EFI_TPL OldTpl;
EFI_STATUS Status;
//
// Enter critical section
//
OldTpl = gBS->RaiseTPL(TPL_NOTIFY);
Status = (*OldReadKeyStroke)(SimpleInput, Key);
if (Key->UnicodeChar==0x7A) {
Key->UnicodeChar=0x61;
}
//
// Leave critical section and return
//
gBS->RestoreTPL(OldTpl);
return Status;
}
EFI_STATUS
EFIAPI
MyEntryPoint(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable) {
EFI_STATUS Status;
// Look for one Simple Simple Input Protocol
Status =
gBS->LocateProtocol(&gEfiSimpleTextInputProtocolGuid, NULL, &SimpleInput);
if (EFI_ERROR(Status)) {
gST->ConOut->OutputString(gST->ConOut,
L"Can't find Simple Input PROTOCOL\n");
return Status;
}
OldReadKeyStroke = SimpleInput->ReadKeyStroke;
SimpleInput->ReadKeyStroke = MyReadKeyStroke;
return Status;
}
需要注意的上述代码是驱动:
1.在 MdeModulePkg 下面编译通过,在\MdeModulePkg\MdeModulePkg.dsc 添加如下:
MdeModulePkg/Universal/RegularExpressionDxe/RegularExpressionDxe.inf
MdeModulePkg/Universal/SmmCommunicationBufferDxe/SmmCommunicationBufferDxe.inf
MdeModulePkg/Universal/Disk/RamDiskDxe/RamDiskDxe.inf
MdeModulePkg/KSTest/KSTest.inf
[Components.X64]
MdeModulePkg/Universal/CapsulePei/CapsuleX64.inf
[BuildOptions]
2.编译使用
build -a X64 -p MdeModulePkg\MdeModulePkg.dsc -t VS2019
3.运行时使用如下命令加载
load kst.efi
参考:
1.https://www.lab-z.com/stu163rp/ 替换已经存在Protocol中函数的实验