debughook.c 1.9 KB

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