debughook.c 2.0 KB

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