Newer
Older
AMI-Aptio-BIOS-Reversed / KbcEmulDxe / KbcEmulDxe.c
@Ajax Dong Ajax Dong 2 days ago 11 KB Init
/*
 *KbcEmulDxe.c - Decompiled source for KbcEmulDxe.efi
 *
 *Copyright (c) HR650X BIOS Decompilation Project
 */

#include "KbcEmulDxe.h"

EFI_STATUS ModuleEntryPoint(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
{
 __int64 n33; // rcx

 ::ImageHandle = (__int64)ImageHandle; /*0x396*/
 if ( !ImageHandle ) /*0x3a3*/
 DebugPrintAssert( /*0x3b6*/
 (__int64)"e:\\hs\\MdePkg\\Library\\UefiBootServicesTableLib\\UefiBootServicesTableLib.c",
 51,
 (__int64)"gImageHandle != ((void *) 0)");
 ::SystemTable = (__int64)SystemTable; /*0x3bb*/
 if ( !SystemTable ) /*0x3c5*/
 DebugPrintAssert( /*0x3d8*/
 (__int64)"e:\\hs\\MdePkg\\Library\\UefiBootServicesTableLib\\UefiBootServicesTableLib.c",
 57,
 (__int64)"gST != ((void *) 0)");
 BootServices = (__int64)SystemTable->BootServices; /*0x3e1*/
 if ( !BootServices ) /*0x3eb*/
 DebugPrintAssert( /*0x3fe*/
 (__int64)"e:\\hs\\MdePkg\\Library\\UefiBootServicesTableLib\\UefiBootServicesTableLib.c",
 63,
 (__int64)"gBS != ((void *) 0)");
 RuntimeServices = (__int64)SystemTable->RuntimeServices; /*0x407*/
 if ( !RuntimeServices ) /*0x411*/
 DebugPrintAssert( /*0x424*/
 (__int64)"e:\\hs\\MdePkg\\Library\\UefiRuntimeServicesTableLib\\UefiRuntimeServicesTableLib.c",
 47,
 (__int64)"gRT != ((void *) 0)");
 GetHobListFromSystemTable((__int64)ImageHandle); /*0x429*/
 return KbcEmulDriverEntry(n33, SystemTable); /*0x436*/
}

__int64 KbcEmulDriverEntry(__int64 n33, EFI_SYSTEM_TABLE *SystemTable)
{
 EFI_RUNTIME_SERVICES *RuntimeServices; // rax __int64 Result; // rbx __int64 Status; // rax __int64 Status; // rdx __int64 Status; // rax _BYTE v8[56]; // [rsp+30h] [rbp-38h] BYREF __int64 n33_1; // [rsp+70h] [rbp+8h] BYREF n33_1 = n33; /*0x43c*/
 if ( SystemTable_0 ) /*0x44e*/
 {
 RuntimeServices = (EFI_RUNTIME_SERVICES *)RuntimeServices_0; /*0x46f*/
 }
 else
 {
 SystemTable_0 = (__int64)SystemTable; /*0x450*/
 BootServices_0 = (__int64)SystemTable->BootServices; /*0x45b*/
 RuntimeServices = SystemTable->RuntimeServices; /*0x462*/
 RuntimeServices_0 = (__int64)RuntimeServices; /*0x466*/
 }
 n33_1 = 33; /*0x47b*/
 if ( ((__int64 ( *)(const __int16 *, void *, _QWORD, __int64 *, _BYTE *))RuntimeServices->GetVariable)( /*0x4b0*/
 L"UsbSupport",
 &unk_11B0,
 0,
 &n33_1,
 v8) < 0
 || !v8[7] )
 {
 return 0; /*0x5c6*/
 }
 Result = (*(__int64 ( **)(void *, _QWORD, __int64 *))(BootServices_0 + 320))(&unk_1190, 0, &qword_1258); /*0x4d3*/
 if ( Result >= 0 ) /*0x4d9*/
 {
 byte_11F9 = 0; /*0x4f0*/
 Status = (*(__int64 ( **)(void *, void *, _QWORD, __int64 ( **)()))(BootServices_0 + 128))( /*0x505*/
 &unk_1200,
 &unk_1170,
 0,
 off_11C0);
 Result = Status; /*0x50b*/
 if ( Status >= 0 ) /*0x511*/
 {
 if ( byte_11F9 ) /*0x54b*/
 {
 word_11E0 = -29952; /*0x552*/
 n43 = 43; /*0x55e*/
 return Result; /*0x565*/
 }
 Status = (*(__int64 ( **)(__int64, __int64, __int64 (*)()))(BootServices_0 + 368))( /*0x59a*/
 512,
 8,
 KbcEmulTimerCallback);
 Result = Status; /*0x5a0*/
 if ( Status >= 0 ) /*0x5a6*/
 return Result; /*0x5a6*/
 DebugAssertError(0x80000000LL, "\nASSERT_EFI_ERROR (Status = %r)\n", Status); /*0x5b7*/
 Status = 162; /*0x5bc*/
 }
 else
 {
 DebugAssertError(0x80000000LL, "\nASSERT_EFI_ERROR (Status = %r)\n", Status); /*0x522*/
 Status = 143; /*0x527*/
 }
 DebugPrintAssert((__int64)"e:\\hs\\AmiModulePkg\\KbcEmulation\\KbcEmulDxe.c", Status, (__int64)"!EFI_ERROR (Status)"); /*0x53a*/
 }
 return Result; /*0x5cb*/
}

__int64 KbcEmulTimerCallback()
{
 byte_11F8 = 1; /*0x5db*/
 return (*(__int64 (**)(void))(BootServices_0 + 112))();
}

bool KbcEmulIsAnyKeyReady()
{
 unsigned __int16 Protocol; // bx __int64 Index; // r14 unsigned __int16 *v2; // rsi unsigned __int64 n0xC; // rdi Protocol = 0; /*0x601*/
 Index = 0; /*0x60a*/
 v2 = (unsigned __int16 *)&unk_11E8; /*0x60d*/
 n0xC = 0; /*0x610*/
 while ( !KbcEmulEntryIsValid(v2) ) /*0x61c*/
 {
 ++Index; /*0x61e*/
 n0xC += 6LL; /*0x621*/
 v2 += 3; /*0x625*/
 if ( n0xC >= 0xC ) /*0x62d*/
 return (Protocol & (unsigned __int16)n43) != 0; /*0x62d*/
 }
 Protocol = SioProtocolRead(*((unsigned __int16 *)&unk_11E8 + 3 *Index), *((_BYTE *)&unk_11E8 + 6 *Index + 5)); /*0x644*/
 return (Protocol & (unsigned __int16)n43) != 0; /*0x665*/
}

char KbcEmulRead8042Data(__int64 a1)
{
 unsigned __int8 n0xA; // bl char Size; // di unsigned __int8 v3; // al unsigned __int8 v4; // al n0xA = 0; /*0x67d*/
 Size = KbcEmulEnableDisableKeys(1); /*0x67f*/
 if ( Size && !byte_11F9 && byte_11F8 ) /*0x694*/
 {
 do /*0x6c7*/
 {
 v3 = __inbyte(0x64u); /*0x69b*/
 if ( (v3 & 1) == 0 ) /*0x69e*/
 {
 (*(void ( **)(__int64))(BootServices_0 + 248))(500); /*0x6ac*/
 v4 = __inbyte(0x64u); /*0x6b7*/
 if ( (v4 & 1) == 0 ) /*0x6ba*/
 break; /*0x6ba*/
 }
 __inbyte(0x60u); /*0x6c1*/
 ++n0xA; /*0x6c2*/
 }
 while ( n0xA < 0xAu ); /*0x6c7*/
 }
 return Size; /*0x6d1*/
}

__int64 KbcEmulDisableKeyboard()
{
 return sub_6E8(0);
}

__int64 KbcEmulPollingStub()
{
 return 1; /*0x6e5*/
}

char KbcEmulEnableDisableKeys(char a1)
{
 unsigned __int16 *v2; // rdi __int64 n2; // rsi unsigned __int16 Size; // bx __int64 v5; // rbp __int64 Size; // rcx v2 = (unsigned __int16 *)&unk_11E8; /*0x704*/
 n2 = 2; /*0x70b*/
 do /*0x79e*/
 {
 if ( KbcEmulEntryIsValid(v2) ) /*0x713*/
 {
 Size = SioProtocolRead(*v2, *((_BYTE *)v2 + 5)); /*0x72e*/
 if ( (Size & (unsigned __int16)Size) == Size && (Size & (unsigned __int16)word_11E0) != 0 ) /*0x743*/
 word_1250 |= Size; /*0x745*/
 v5 = (*(__int64 ( **)(__int64))(BootServices_0 + 24))(31); /*0x762*/
 KbcEmulWriteAllEntries((unsigned __int16)(Size | word_11E0)); /*0x768*/
 Size = (unsigned __int16)Size; /*0x76d*/
 if ( a1 ) /*0x777*/
 LOWORD(Size) = Size | Size; /*0x779*/
 else LOWORD(Size) = Size & ~Size; /*0x781*/
 KbcEmulWriteAllEntries(Size); /*0x784*/
 (*(void ( **)(__int64))(BootServices_0 + 32))(v5); /*0x793*/
 }
 v2 += 3; /*0x796*/
 --n2; /*0x79a*/
 }
 while ( n2 ); /*0x79e*/
 return 1; /*0x7ba*/
}

__int64 GetDebugLibProtocol()
{
 __int64 result; // rax unsigned __int64 n0x10; // rbx __int64 v2; // rax __int64 Result; // rcx result = qword_1228; /*0x7ce*/
 if ( !qword_1228 ) /*0x7da*/
 {
 n0x10 = (*(__int64 ( **)(__int64))(BootServices + 24))(31); /*0x7f3*/
 (*(void ( **)(unsigned __int64))(BootServices + 32))(n0x10); /*0x7f6*/
 if ( n0x10 <= 0x10 ) /*0x7fd*/
 {
 v2 = (*(__int64 ( **)(void *, _QWORD, __int64 *))(BootServices + 320))(&unk_1160, 0, &qword_1228); /*0x81a*/
 Result = qword_1228; /*0x820*/
 if ( v2 < 0 ) /*0x82a*/
 Result = 0; /*0x82a*/
 qword_1228 = Result; /*0x82e*/
 return Result; /*0x835*/
 }
 else
 {
 return 0; /*0x7ff*/
 }
 }
 return result; /*0x83d*/
}

__int64 DebugAssertError(__int64 a1, const char *_nASSERT_EFI_ERROR_(Status___%r)_n, ...)
{
 __int64 result; // rax __int64 ( **v4)(__int64, const char *, __int64 *); // r10 va_list va; // [rsp+40h] [rbp+18h] BYREF va_start(va, _nASSERT_EFI_ERROR_(Status___%r)_n);
 result = GetDebugLibProtocol(); /*0x85b*/
 if ( result ) /*0x866*/
 {
 result = CmosReadComPortType(); /*0x868*/
 if ( ((unsigned int)result & (unsigned int)a1) != 0 ) /*0x873*/
 return (*v4)(a1, _nASSERT_EFI_ERROR_(Status___%r)_n, (__int64 *)va); /*0x882*/
 }
 return result; /*0x885*/
}

__int64 DebugPrintAssert(__int64 a1, __int64 a2, __int64 a3)
{
 __int64 result; // rax result = GetDebugLibProtocol(); /*0x8a4*/
 if ( result ) /*0x8ac*/
 return (*(__int64 ( **)(__int64, __int64, __int64))(result + 8))(a1, a2, a3); /*0x8b7*/
 return result; /*0x8c4*/
}

__int64 GetHobListFromSystemTable(__int64 ImageHandle)
{
 __int64 result; // rax __int64 SystemTable; // rdi unsigned __int64 Index; // rbx __int64 Protocol; // rsi result = qword_1230; /*0x8db*/
 if ( !qword_1230 ) /*0x8e5*/
 {
 SystemTable = SystemTable; /*0x8eb*/
 Index = 0; /*0x8f2*/
 qword_1230 = 0; /*0x8f4*/
 if ( *(_QWORD *)(SystemTable + 104) ) /*0x8fb*/
 {
 Protocol = 0; /*0x901*/
 while ( !GuidCompare(ImageHandle, Protocol + *(_QWORD *)(SystemTable + 112)) ) /*0x911*/
 {
 ++Index; /*0x913*/
 Protocol += 24; /*0x916*/
 if ( Index >= *(_QWORD *)(SystemTable + 104) ) /*0x91e*/
 goto LABEL_6; /*0x91e*/
 }
 result = *(_QWORD *)(*(_QWORD *)(SystemTable + 112) + 24 *Index + 16); /*0x994*/
 qword_1230 = result; /*0x999*/
 }
 else
 {
LABEL_6:
 DebugAssertError(0x80000000LL, "\nASSERT_EFI_ERROR (Status = %r)\n", 0x800000000000000EuLL); /*0x920*/
 DebugPrintAssert((__int64)"e:\\hs\\MdePkg\\Library\\DxeHobLib\\HobLib.c", 54, (__int64)"!EFI_ERROR (Status)"); /*0x94e*/
 result = qword_1230; /*0x953*/
 }
 if ( !result ) /*0x95d*/
 {
 DebugPrintAssert((__int64)"e:\\hs\\MdePkg\\Library\\DxeHobLib\\HobLib.c", 55, (__int64)"mHobList != ((void *) 0)"); /*0x970*/
 return qword_1230; /*0x975*/
 }
 }
 return result; /*0x986*/
}

bool KbcEmulEntryIsValid(unsigned __int16 *a1)
{
 return *a1 != 0xFFFF /*0x9e5*/
 && (unsigned int)SioProtocolRead(*a1, 0) != -1
 && (unsigned int)SioProtocolRead(*a1, 8) >> 8 == (*(_DWORD *)(a1 + 1) & 0xFFFFFF);
}

char KbcEmulWriteAllEntries(__int64 n43)
{
 unsigned __int16 n43_1; // si unsigned __int16 *v2; // rbx __int64 n2; // rdi char result; // al n43_1 = n43; /*0x9fb*/
 v2 = (unsigned __int16 *)&unk_11E8; /*0x9fe*/
 n2 = 2; /*0xa05*/
 do /*0xa2d*/
 {
 result = KbcEmulEntryIsValid(v2); /*0xa0d*/
 if ( result ) /*0xa14*/
 result = SioProtocolWriteData(*v2, *((_BYTE *)v2 + 5), n43_1); /*0xa20*/
 v2 += 3; /*0xa25*/
 --n2; /*0xa29*/
 }
 while ( n2 ); /*0xa2d*/
 return result; /*0xa39*/
}

__int64 SioProtocolWrite(__int64 a1, int a2)
{
 int v3; // [rsp+48h] [rbp+10h] BYREF v3 = a2; /*0xa40*/
 return (*(__int64 ( **)(__int64, __int64, __int64, __int64, int *))(qword_1258 + 24))( /*0xa6e*/
 qword_1258,
 2,
 3320,
 1,
 &v3);
}

__int64 SioProtocolRead(__int64 a1, char n8)
{
 unsigned int Result; // [rsp+48h] [rbp+10h] BYREF SioProtocolWrite(a1, n8 & 0xFC | ((unsigned __int16)a1 << 8) | 0x80000000); /*0xa90*/
 (*(void ( **)(__int64, __int64, __int64, __int64, unsigned int *))(qword_1258 + 16))( /*0xab8*/
 qword_1258,
 2,
 3324,
 1,
 &Result);
 return Result >> (8 * (n8 & 3)); /*0xac9*/
}

__int64 SioProtocolWriteData(__int64 a1, char a2, unsigned __int16 n43)
{
 unsigned __int16 n43_1; // [rsp+48h] [rbp+10h] BYREF SioProtocolWrite(a1, a2 & 0xFC | ((unsigned __int16)a1 << 8) | 0x80000000); /*0xaf4*/
 n43_1 = n43; /*0xafe*/
 return (*(__int64 ( **)(__int64, __int64, __int64, __int64, unsigned __int16 *))(qword_1258 + 24))( /*0xb32*/
 qword_1258,
 1,
 (a2 & 3) + 3324LL,
 1,
 &n43_1);
}

__int64 CmosReadComPortType()
{
 unsigned __int8 v0; // al char n3; // al char n3_1; // cl __int64 result; // rax v0 = __inbyte(0x70u); /*0xb3d*/
 __outbyte(0x70u, v0 & 0x80 | 0x4B); /*0xb42*/
 n3 = __inbyte(0x71u); /*0xb48*/
 n3_1 = n3; /*0xb49*/
 if ( (unsigned __int8)n3 > 3u ) /*0xb50*/
 {
 n3_1 = n3; /*0xb52*/
 if ( !n3 ) /*0xb5a*/
 n3_1 = MEMORY[0xFDAF0490] & 2 | 1; /*0xb66*/
 }
 if ( (unsigned __int8)(n3_1 - 1) > 0xFDu ) /*0xb70*/
 return 0; /*0xb83*/
 result = 2147483718LL; /*0xb75*/
 if ( n3_1 == 1 ) /*0xb7f*/
 return 2147483652LL; /*0xb7f*/
 return result; /*0xb82*/
}

bool GuidCompare(__int64 ImageHandle, const UINT64 *Buffer)
{
 UINT64 Unaligned64; // rdi UINT64 Unaligned64_1; // rbx UINT64 Buffer; // rbp UINT64 Buffer; // rax Unaligned64 = ReadUnaligned64(&Buffer); /*0xbae*/
 Unaligned64_1 = ReadUnaligned64(Buffer); /*0xbbd*/
 Buffer = ReadUnaligned64(&Buffer_); /*0xbc9*/
 Buffer = ReadUnaligned64(Buffer + 1); /*0xbcc*/
 return Unaligned64 == Unaligned64_1 && Buffer == Buffer; /*0xbf0*/
}

UINT64 ReadUnaligned64(const UINT64 *Buffer)
{
 if ( !Buffer ) /*0xc04*/
 DebugPrintAssert((__int64)"e:\\hs\\MdePkg\\Library\\BaseLib\\Unaligned.c", 192, (__int64)"Buffer != ((void *) 0)"); /*0xc19*/
 return *Buffer; /*0xc21*/
}