| 1 |
#include <efi.h> |
| 2 |
#include <efilib.h> |
| 3 |
|
| 4 |
EFI_STATUS |
| 5 |
GetVariableAttr(CHAR16 *var, UINT8 **data, UINTN *len, EFI_GUID owner, |
| 6 |
UINT32 *attributes) |
| 7 |
{ |
| 8 |
EFI_STATUS efi_status; |
| 9 |
|
| 10 |
*len = 0; |
| 11 |
|
| 12 |
efi_status = uefi_call_wrapper(RT->GetVariable, 5, var, &owner, |
| 13 |
NULL, len, NULL); |
| 14 |
if (efi_status != EFI_BUFFER_TOO_SMALL) |
| 15 |
return efi_status; |
| 16 |
|
| 17 |
*data = AllocateZeroPool(*len); |
| 18 |
if (!*data) |
| 19 |
return EFI_OUT_OF_RESOURCES; |
| 20 |
|
| 21 |
efi_status = uefi_call_wrapper(RT->GetVariable, 5, var, &owner, |
| 22 |
attributes, len, *data); |
| 23 |
|
| 24 |
if (efi_status != EFI_SUCCESS) { |
| 25 |
FreePool(*data); |
| 26 |
*data = NULL; |
| 27 |
} |
| 28 |
return efi_status; |
| 29 |
} |
| 30 |
|
| 31 |
EFI_STATUS |
| 32 |
GetVariable(CHAR16 *var, UINT8 **data, UINTN *len, EFI_GUID owner) |
| 33 |
{ |
| 34 |
return GetVariableAttr(var, data, len, owner, NULL); |
| 35 |
} |
| 36 |
|
| 37 |
EFI_GUID DUMMY_GUID = |
| 38 |
{0x55aad538, 0x8f82, 0x4e2a, {0xa4,0xf0,0xbe, 0x59, 0x13, 0xb6, 0x5f, 0x1e}}; |
| 39 |
|
| 40 |
#if defined(__clang__) |
| 41 |
# define _OPTNONE __attribute__((optnone)) |
| 42 |
#else |
| 43 |
# define _OPTNONE __attribute__((__optimize__("0"))) |
| 44 |
#endif |
| 45 |
|
| 46 |
static _OPTNONE void |
| 47 |
DebugHook(void) |
| 48 |
{ |
| 49 |
EFI_GUID guid = DUMMY_GUID; |
| 50 |
UINT8 *data = NULL; |
| 51 |
UINTN dataSize = 0; |
| 52 |
EFI_STATUS efi_status; |
| 53 |
register volatile unsigned long long x = 0; |
| 54 |
extern char _text, _data; |
| 55 |
|
| 56 |
if (x) |
| 57 |
return; |
| 58 |
|
| 59 |
efi_status = GetVariable(L"DUMMY_DEBUG", &data, &dataSize, guid); |
| 60 |
if (EFI_ERROR(efi_status)) { |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
Print(L"add-symbol-file /usr/lib/debug/boot/efi/debughook.debug " |
| 65 |
L"0x%08x -s .data 0x%08x\n", &_text, &_data); |
| 66 |
|
| 67 |
Print(L"Pausing for debugger attachment.\n"); |
| 68 |
Print(L"To disable this, remove the EFI variable DUMMY_DEBUG-%g .\n", |
| 69 |
&guid); |
| 70 |
x = 1; |
| 71 |
while (x++) { |
| 72 |
/* Make this so it can't /totally/ DoS us. */ |
| 73 |
#if defined(__x86_64__) || defined(__i386__) || defined(__i686__) |
| 74 |
if (x > 4294967294ULL) |
| 75 |
break; |
| 76 |
__asm__ __volatile__("pause"); |
| 77 |
#elif defined(__aarch64__) |
| 78 |
if (x > 1000) |
| 79 |
break; |
| 80 |
__asm__ __volatile__("wfi"); |
| 81 |
#else |
| 82 |
if (x > 12000) |
| 83 |
break; |
| 84 |
uefi_call_wrapper(BS->Stall, 1, 5000); |
| 85 |
#endif |
| 86 |
} |
| 87 |
x = 1; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
EFI_STATUS |
| 92 |
efi_main (EFI_HANDLE image, EFI_SYSTEM_TABLE *systab) |
| 93 |
{ |
| 94 |
InitializeLib(image, systab); |
| 95 |
DebugHook(); |
| 96 |
return EFI_SUCCESS; |
| 97 |
} |