efi_watchdog.c 2.0 KB

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