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