efi_watchdog.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. * efi_watchdog_timer_notify() - resets system upon watchdog event
  14. *
  15. * Reset the system when the watchdog event is notified.
  16. *
  17. * @event: the watchdog event
  18. * @context: not used
  19. */
  20. static void EFIAPI efi_watchdog_timer_notify(struct efi_event *event,
  21. void *context)
  22. {
  23. EFI_ENTRY("%p, %p", event, context);
  24. printf("\nEFI: Watchdog timeout\n");
  25. do_reset(NULL, 0, 0, NULL);
  26. EFI_EXIT(EFI_UNSUPPORTED);
  27. }
  28. /**
  29. * efi_set_watchdog() - resets the watchdog timer
  30. *
  31. * This function is used by the SetWatchdogTimer service.
  32. *
  33. * @timeout: seconds before reset by watchdog
  34. * Return: status code
  35. */
  36. efi_status_t efi_set_watchdog(unsigned long timeout)
  37. {
  38. efi_status_t r;
  39. if (timeout)
  40. /* Reset watchdog */
  41. r = efi_set_timer(watchdog_timer_event, EFI_TIMER_RELATIVE,
  42. EFI_SECONDS_TO_100NS * timeout);
  43. else
  44. /* Deactivate watchdog */
  45. r = efi_set_timer(watchdog_timer_event, EFI_TIMER_STOP, 0);
  46. return r;
  47. }
  48. /**
  49. * efi_watchdog_register() - initializes the EFI watchdog
  50. *
  51. * This function is called by efi_init_obj_list().
  52. *
  53. * Return: status code
  54. */
  55. efi_status_t efi_watchdog_register(void)
  56. {
  57. efi_status_t r;
  58. /*
  59. * Create a timer event.
  60. */
  61. r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
  62. efi_watchdog_timer_notify, NULL, NULL,
  63. &watchdog_timer_event);
  64. if (r != EFI_SUCCESS) {
  65. printf("ERROR: Failed to register watchdog event\n");
  66. return r;
  67. }
  68. /*
  69. * The UEFI standard requires that the watchdog timer is set to five
  70. * minutes when invoking an EFI boot option.
  71. *
  72. * Unified Extensible Firmware Interface (UEFI), version 2.7 Errata A
  73. * 7.5. Miscellaneous Boot Services - EFI_BOOT_SERVICES.SetWatchdogTimer
  74. */
  75. r = efi_set_watchdog(300);
  76. if (r != EFI_SUCCESS) {
  77. printf("ERROR: Failed to set watchdog timer\n");
  78. return r;
  79. }
  80. return EFI_SUCCESS;
  81. }