efi_watchdog.c 2.2 KB

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