Tom Rini | 70df9d6 | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Heinrich Schuchardt | 18081d4 | 2017-10-18 18:13:04 +0200 | [diff] [blame] | 2 | /* |
| 3 | * EFI watchdog |
| 4 | * |
| 5 | * Copyright (c) 2017 Heinrich Schuchardt |
Heinrich Schuchardt | 18081d4 | 2017-10-18 18:13:04 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
Heinrich Schuchardt | 18081d4 | 2017-10-18 18:13:04 +0200 | [diff] [blame] | 8 | #include <efi_loader.h> |
| 9 | |
| 10 | /* Conversion factor from seconds to multiples of 100ns */ |
| 11 | #define EFI_SECONDS_TO_100NS 10000000ULL |
| 12 | |
| 13 | static struct efi_event *watchdog_timer_event; |
| 14 | |
Heinrich Schuchardt | f011aa4 | 2020-04-10 17:51:56 +0200 | [diff] [blame] | 15 | /** |
| 16 | * efi_watchdog_timer_notify() - resets system upon watchdog event |
| 17 | * |
Heinrich Schuchardt | 18081d4 | 2017-10-18 18:13:04 +0200 | [diff] [blame] | 18 | * Reset the system when the watchdog event is notified. |
| 19 | * |
| 20 | * @event: the watchdog event |
| 21 | * @context: not used |
| 22 | */ |
| 23 | static void EFIAPI efi_watchdog_timer_notify(struct efi_event *event, |
| 24 | void *context) |
| 25 | { |
| 26 | EFI_ENTRY("%p, %p", event, context); |
| 27 | |
| 28 | printf("\nEFI: Watchdog timeout\n"); |
Heinrich Schuchardt | e234518 | 2021-09-09 07:47:05 +0200 | [diff] [blame] | 29 | do_reset(NULL, 0, 0, NULL); |
Heinrich Schuchardt | 18081d4 | 2017-10-18 18:13:04 +0200 | [diff] [blame] | 30 | |
| 31 | EFI_EXIT(EFI_UNSUPPORTED); |
| 32 | } |
| 33 | |
Heinrich Schuchardt | f011aa4 | 2020-04-10 17:51:56 +0200 | [diff] [blame] | 34 | /** |
| 35 | * efi_set_watchdog() - resets the watchdog timer |
Heinrich Schuchardt | 18081d4 | 2017-10-18 18:13:04 +0200 | [diff] [blame] | 36 | * |
| 37 | * This function is used by the SetWatchdogTimer service. |
| 38 | * |
| 39 | * @timeout: seconds before reset by watchdog |
Heinrich Schuchardt | f011aa4 | 2020-04-10 17:51:56 +0200 | [diff] [blame] | 40 | * Return: status code |
Heinrich Schuchardt | 18081d4 | 2017-10-18 18:13:04 +0200 | [diff] [blame] | 41 | */ |
| 42 | efi_status_t efi_set_watchdog(unsigned long timeout) |
| 43 | { |
| 44 | efi_status_t r; |
| 45 | |
| 46 | if (timeout) |
| 47 | /* Reset watchdog */ |
| 48 | r = efi_set_timer(watchdog_timer_event, EFI_TIMER_RELATIVE, |
| 49 | EFI_SECONDS_TO_100NS * timeout); |
| 50 | else |
| 51 | /* Deactivate watchdog */ |
| 52 | r = efi_set_timer(watchdog_timer_event, EFI_TIMER_STOP, 0); |
| 53 | return r; |
| 54 | } |
| 55 | |
Heinrich Schuchardt | f011aa4 | 2020-04-10 17:51:56 +0200 | [diff] [blame] | 56 | /** |
| 57 | * efi_watchdog_register() - initializes the EFI watchdog |
| 58 | * |
| 59 | * This function is called by efi_init_obj_list(). |
Heinrich Schuchardt | 18081d4 | 2017-10-18 18:13:04 +0200 | [diff] [blame] | 60 | * |
Heinrich Schuchardt | f011aa4 | 2020-04-10 17:51:56 +0200 | [diff] [blame] | 61 | * Return: status code |
Heinrich Schuchardt | 18081d4 | 2017-10-18 18:13:04 +0200 | [diff] [blame] | 62 | */ |
Heinrich Schuchardt | f2704cc | 2018-03-03 15:28:57 +0100 | [diff] [blame] | 63 | efi_status_t efi_watchdog_register(void) |
Heinrich Schuchardt | 18081d4 | 2017-10-18 18:13:04 +0200 | [diff] [blame] | 64 | { |
| 65 | efi_status_t r; |
| 66 | |
| 67 | /* |
| 68 | * Create a timer event. |
| 69 | */ |
| 70 | r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK, |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 71 | efi_watchdog_timer_notify, NULL, NULL, |
Heinrich Schuchardt | 18081d4 | 2017-10-18 18:13:04 +0200 | [diff] [blame] | 72 | &watchdog_timer_event); |
| 73 | if (r != EFI_SUCCESS) { |
| 74 | printf("ERROR: Failed to register watchdog event\n"); |
| 75 | return r; |
| 76 | } |
Masahisa Kojima | d250a8f | 2022-02-22 09:58:30 +0900 | [diff] [blame] | 77 | |
Heinrich Schuchardt | f2704cc | 2018-03-03 15:28:57 +0100 | [diff] [blame] | 78 | return EFI_SUCCESS; |
Heinrich Schuchardt | 18081d4 | 2017-10-18 18:13:04 +0200 | [diff] [blame] | 79 | } |