efi_watchdog.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * EFI watchdog
  4. *
  5. * Copyright (c) 2017 Heinrich Schuchardt
  6. */
  7. #include <common.h>
  8. #include <efi_loader.h>
  9. /* Conversion factor from seconds to multiples of 100ns */
  10. #define EFI_SECONDS_TO_100NS 10000000ULL
  11. static struct efi_event *watchdog_timer_event;
  12. /*
  13. * Reset the system when the watchdog event is notified.
  14. *
  15. * @event: the watchdog event
  16. * @context: not used
  17. */
  18. static void EFIAPI efi_watchdog_timer_notify(struct efi_event *event,
  19. void *context)
  20. {
  21. EFI_ENTRY("%p, %p", event, context);
  22. printf("\nEFI: Watchdog timeout\n");
  23. EFI_CALL_VOID(efi_runtime_services.reset_system(EFI_RESET_COLD,
  24. EFI_SUCCESS, 0, NULL));
  25. EFI_EXIT(EFI_UNSUPPORTED);
  26. }
  27. /*
  28. * Reset the watchdog timer.
  29. *
  30. * This function is used by the SetWatchdogTimer service.
  31. *
  32. * @timeout: seconds before reset by watchdog
  33. * @return: status code
  34. */
  35. efi_status_t efi_set_watchdog(unsigned long timeout)
  36. {
  37. efi_status_t r;
  38. if (timeout)
  39. /* Reset watchdog */
  40. r = efi_set_timer(watchdog_timer_event, EFI_TIMER_RELATIVE,
  41. EFI_SECONDS_TO_100NS * timeout);
  42. else
  43. /* Deactivate watchdog */
  44. r = efi_set_timer(watchdog_timer_event, EFI_TIMER_STOP, 0);
  45. return r;
  46. }
  47. /*
  48. * Initialize the EFI watchdog.
  49. *
  50. * This function is called by efi_init_obj_list()
  51. */
  52. efi_status_t efi_watchdog_register(void)
  53. {
  54. efi_status_t r;
  55. /*
  56. * Create a timer event.
  57. */
  58. r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
  59. efi_watchdog_timer_notify, NULL, NULL,
  60. &watchdog_timer_event);
  61. if (r != EFI_SUCCESS) {
  62. printf("ERROR: Failed to register watchdog event\n");
  63. return r;
  64. }
  65. /*
  66. * The UEFI standard requires that the watchdog timer is set to five
  67. * minutes when invoking an EFI boot option.
  68. *
  69. * Unified Extensible Firmware Interface (UEFI), version 2.7 Errata A
  70. * 7.5. Miscellaneous Boot Services - EFI_BOOT_SERVICES.SetWatchdogTimer
  71. */
  72. r = efi_set_watchdog(300);
  73. if (r != EFI_SUCCESS) {
  74. printf("ERROR: Failed to set watchdog timer\n");
  75. return r;
  76. }
  77. return EFI_SUCCESS;
  78. }