/* ========================================================================
*FwKeyHobPei.c - Firmware Key HOB PEI Module
*Source: Lenovo HR650X BIOS (UEFI PE32, IA32)
*Module GUID: {61C0F511-A691-4F54-974F-B9A42172CE53}
* ========================================================================
*
*This PEIM locates firmware key FFS files by GUID, extracts their raw
*data sections, and builds GUID-extension HOBs to pass the key material
*to DXE-phase drivers. The keys are identified by a table of GUIDs stored
*in the .data section. Debug output is routed through the PEI debug PPI.
* ========================================================================
*/
#include <Uefi.h>
#include <Pi/PiPeiCis.h>
#include <Library/BaseLib.h>
#include "FwKeyHobPei.h"
//========================================================================
// Memset
//========================================================================
void *Memset(void *buf, unsigned int count, char value)
{
memset(buf, value, count);
return buf;
}
//========================================================================
// Memcpy
//========================================================================
char *Memcpy(char *dst, char *src, unsigned int count_1)
{
unsigned int count;
char *dst_1;
char *src_1;
count = count_1;
if ( src < dst && &src[count_1 - 1] >= dst )
{
src_1 = &src[count_1 - 1];
dst_1 = &dst[count_1 - 1];
}
else
{
count = count_1 & 3;
qmemcpy(dst, src, 4 * (count_1 >> 2));
src_1 = &src[4 * (count_1 >> 2)];
dst_1 = &dst[4 * (count_1 >> 2)];
}
qmemcpy(dst_1, src_1, count);
return dst;
}
//========================================================================
// SetMem32
//========================================================================
int SetMem32(int a1, int a2, int a3, int a4)
{
do
{
*(_DWORD *)(a1 + 8 *a2 - 8) = a3;
*(_DWORD *)(a1 + 8 *a2-- - 4) = a4;
}
while ( a2 );
return a1;
}
//========================================================================
// Memset32
//========================================================================
void *Memset32(void *buf, unsigned int count, int value)
{
memset32(buf, value, count);
return buf;
}
//========================================================================
// ModuleEntryPoint - PEI Entry Point
//========================================================================
EFI_STATUS ModuleEntryPoint(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
{
int ErrorLevel;
int Table; // [esp+4h] [ebp-4h] BYREF if ( *(_DWORD *)(LODWORD(SystemTable->Hdr.Signature) + 8) < 0x1000Au )
{
ErrorLevel = GetDebugLibInfo();
if ( ErrorLevel )
(*(void ( **)(const char *, int, const char *))(ErrorLevel + 4))(
"e:\\hs\\MdePkg\\Library\\PeimEntryPoint\\PeimEntryPoint.c",
46,
"(*PeiServices)->Hdr.Revision >= _gPeimRevision");
}
Table = 0;
InstallFwKeyHob(SystemTable, &Table);
return 0;
}
//========================================================================
// FindKeyFfsAndBuildHob
//========================================================================
int __usercall FindKeyFfsAndBuildHob@<eax>(int a1@<edx>, _DWORD *a2)
{
int v3;
int result;
int v5;
int v6;
int v7;
int v8;
char v9;
int v10;
int v11;
int v12;
int v13;
int v14;
char v15; // [esp+13h] [ebp-Dh]
int v16; // [esp+14h] [ebp-Ch] BYREF int v17; // [esp+18h] [ebp-8h] BYREF int v18; // [esp+1Ch] [ebp-4h]
v16 = 0;
v15 = 0;
if ( !a1 || !a2 )
return -2147483646;
v3 = GetPeiServices();
result = (*(int ( **)(int, _DWORD, int *))(*(_DWORD *)v3 + 56))(v3, 0, &v17);
v18 = 1;
while ( result >= 0 )
{
v6 = v17;
if ( !CompareMem(v5) )
{
v16 = 0;
while ( 1 )
{
v7 = GetPeiServices();
if ( (*(int ( **)(int, int, int, int *))(*(_DWORD *)v7 + 60))(v7, 2, v6, &v16) == -2147483634 )
{
v9 = v15;
goto LABEL_10;
}
if ( !CompareMem(v8) )
break;
v6 = v17;
}
v9 = 1;
v15 = 1;
LABEL_10:
if ( v9 )
{
v12 = v16;
v13 = GetPeiServices();
v14 = (*(int ( **)(int, int, int, int))(*(_DWORD *)v13 + 64))(v13, 25, v12, a1);
if ( v14 >= 0 )
*a2 = (*(_DWORD *)(v16 + 20) & 0xFFFFFF) - 28;
DebugPrint(64, "Find Key Ffs %g, %r\n(addr %X, len 0x%x) [%X...]\n");
return v14;
}
}
v10 = GetPeiServices();
v11 = v18;
result = (*(int ( **)(int, int, int *))(*(_DWORD *)v10 + 56))(v10, v18, &v17);
v18 = v11 + 1;
}
return result;
}
//========================================================================
// InstallFwKeyHob
//========================================================================
int InstallFwKeyHob(EFI_SYSTEM_TABLE *SystemTable, int *HobPtr)
{
int Result;
unsigned __int8 Index;
int Table;
int v8; // [esp+Ch] [ebp-8h]
int Table; // [esp+10h] [ebp-4h] BYREF Result = 0;
DebugPrint(64, "Install pFwKeyHob...\n");
if ( !HobPtr )
return -2147483646;
if ( *HobPtr )
return 0;
Index = 0;
if ( &gKeyGuid4 )
{
while ( 1 )
{
Result = FindKeyFfsAndBuildHob(&Table);
if ( Result >= 0 )
break;
if ( !*(&gKeyGuidTable + ++Index) )
return Result;
}
Result = (*(int ( **)(EFI_SYSTEM_TABLE *, int, int, int *))(LODWORD(SystemTable->Hdr.Signature) + 52))(
SystemTable, 4, 52, HobPtr);
if ( Result >= 0 )
{
if ( *HobPtr )
{
CopyGuid(*HobPtr + 8, &gFwKeyHobGuid);
CopyGuid(*HobPtr + 24, *(&gKeyGuidSubset + Index));
Table = *HobPtr;
*(_DWORD *)(Table + 40) = v8;
*(_DWORD *)(Table + 44) = 0;
*(_DWORD *)(*HobPtr + 48) = Table;
}
}
DebugPrint(64, "KeyHob %X [%X]...\n");
}
return Result;
}
//========================================================================
// CopyGuid
//========================================================================
int CopyGuid(int a1, int a2)
{
__int64 v4;
__int64 v5;
v4 = ReadUnaligned64(a2);
WriteUnaligned64(v4, HIDWORD(v4));
v5 = ReadUnaligned64(a2 + 8);
WriteUnaligned64(v5, HIDWORD(v5));
return a1;
}
//========================================================================
// GetDebugLibInfo
//========================================================================
int GetDebugLibInfo()
{
int Protocol;
int v2; // [esp+0h] [ebp-8h] BYREF int Result; // [esp+4h] [ebp-4h] BYREF Protocol = GetPeiServices();
if ( (*(int ( **)(int, void *, _DWORD, int *, int *))(*(_DWORD *)Protocol + 32))(Protocol, &gDebugPpiGuid, 0, &v2, &Result) >= 0 )
return Result;
else return 0;
}
//========================================================================
// DebugPrint
//========================================================================
int DebugPrint(int ErrorLevel, char *Format, ...)
{
int result;
int ( **v3)(int, char *, char *);
va_list va;
va_start(va, Format);
result = GetDebugLibInfo();
v3 = (int ( **)(int, char *, char *))result;
if ( result )
{
result = DebugPrintEnabled();
if ( (result & ErrorLevel) != 0 )
return (*v3)(ErrorLevel, Format, (char *)va);
}
return result;
}
//========================================================================
// DebugPrintEnabled
//========================================================================
int DebugPrintEnabled()
{
unsigned __int8 v0;
char Result;
char n3_1;
v0 = __inbyte(0x70u);
__outbyte(0x70u, v0 & 0x80 | 0x4A);
Result = __inbyte(0x71u);
n3_1 = Result;
if ( (unsigned __int8)Result <= 3u )
{
LABEL_4:
if ( !n3_1 )
return 0;
goto LABEL_5;
}
n3_1 = Result;
if ( !Result )
{
n3_1 = MEMORY[0xFDAF0490] & 2 | 1;
goto LABEL_4;
}
LABEL_5:
if ( n3_1 != -1 )
return n3_1 != 1 ? -2147483578 : -2147483644;
return 0;
}
//========================================================================
// AssertFail
//========================================================================
int AssertFail(int FileName, int LineNumber, const char *Expression)
{
int result;
result = GetDebugLibInfo();
if ( result )
return (*(int ( **)(int, int, const char *))(result + 4))(
FileName, LineNumber, Expression);
return result;
}
//========================================================================
// ReadUnaligned64
//========================================================================
__int64 __thiscall ReadUnaligned64(void *Buffer)
{
int ErrorLevel;
if ( !Buffer )
{
ErrorLevel = GetDebugLibInfo();
if ( ErrorLevel )
(*(void ( **)(const char *, int, const char *))(ErrorLevel + 4))(
"e:\\hs\\MdePkg\\Library\\BaseLib\\Unaligned.c",
192,
"Buffer != ((void *) 0)");
}
return *(_QWORD *)Buffer;
}
//========================================================================
// WriteUnaligned64
//========================================================================
int WriteUnaligned64(int ValueLow, int ValueHigh)
{
_DWORD *v2;
_DWORD *v3;
int ErrorLevel;
v3 = v2;
if ( !v2 )
{
ErrorLevel = GetDebugLibInfo();
if ( ErrorLevel )
(*(void ( **)(const char *, int, const char *))(ErrorLevel + 4))(
"e:\\hs\\MdePkg\\Library\\BaseLib\\Unaligned.c",
219,
"Buffer != ((void *) 0)");
}
*v3 = ValueLow;
v3[1] = ValueHigh;
return ValueLow;
}
//========================================================================
// CompareMem
//========================================================================
int CompareMem(_BYTE *a1, _BYTE *a2)
{
_BYTE *v2;
unsigned int v3;
int n4;
int v5;
v2 = a1;
v3 = (unsigned int)(a1 + 16);
n4 = (unsigned __int8)a1 & 3;
if ( ((unsigned __int8)a1 & 3) != 0 && n4 == ((unsigned __int8)a2 & 3) )
{
v5 = 4 - n4;
if ( n4 != 4 )
{
do
{
if ( *v2 != *a2 )
break;
++v2;
++a2;
--v5;
}
while ( v5 );
}
}
while ( (unsigned int)v2 <= v3 - 4 && *(_DWORD *)v2 == *(_DWORD *)a2 )
{
v2 += 4;
a2 += 4;
}
while ( 1 )
{
if ( (unsigned int)v2 >= v3 )
return 0;
if ( *v2 != *a2 )
break;
++v2;
++a2;
}
return (char)*v2 - (char)*a2;
}
//========================================================================
// GetPeiServices
//========================================================================
int GetPeiServices()
{
int Result;
IA32_IDTR v2; // [esp+4h] [ebp-8h] BYREF ReadIdtr(&v2);
Result = *(_DWORD *)(*(_DWORD *)&v2.Rsvd - 4);
if ( !Result )
AssertFail("PeiServices != ((void *) 0)");
return Result;
}
//========================================================================
// ReadIdtr
//========================================================================
void *__thiscall ReadIdtr(void *this)
{
void *this_1;
if ( !this )
AssertFail((int)"e:\\hs\\MdePkg\\Library\\BaseLib\\X86ReadIdtr.c", 37, "Idtr != ((void *) 0)");
this_1 = this;
__sidt(this);
return this_1;
}