debughook.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. static EFI_OPTNONE void
  32. DebugHook(void)
  33. {
  34. EFI_GUID guid = DUMMY_GUID;
  35. UINT8 *data = NULL;
  36. UINTN dataSize = 0;
  37. EFI_STATUS efi_status;
  38. register volatile unsigned long long x = 0;
  39. extern char _text, _data;
  40. if (x)
  41. return;
  42. efi_status = GetVariable(L"DUMMY_DEBUG", &data, &dataSize, guid);
  43. if (EFI_ERROR(efi_status)) {
  44. return;
  45. }
  46. Print(L"add-symbol-file /usr/lib/debug/boot/efi/debughook.debug "
  47. L"0x%08x -s .data 0x%08x\n", &_text, &_data);
  48. Print(L"Pausing for debugger attachment.\n");
  49. Print(L"To disable this, remove the EFI variable DUMMY_DEBUG-%g .\n",
  50. &guid);
  51. x = 1;
  52. while (x++) {
  53. /* Make this so it can't /totally/ DoS us. */
  54. #if defined(__x86_64__) || defined(__i386__) || defined(__i686__)
  55. if (x > 4294967294ULL)
  56. break;
  57. __asm__ __volatile__("pause");
  58. #elif defined(__aarch64__)
  59. if (x > 1000)
  60. break;
  61. __asm__ __volatile__("wfi");
  62. #else
  63. if (x > 12000)
  64. break;
  65. uefi_call_wrapper(BS->Stall, 1, 5000);
  66. #endif
  67. }
  68. x = 1;
  69. }
  70. EFI_STATUS
  71. efi_main (EFI_HANDLE image, EFI_SYSTEM_TABLE *systab)
  72. {
  73. InitializeLib(image, systab);
  74. DebugHook();
  75. return EFI_SUCCESS;
  76. }