blob: c21d8086074333676f226a69d81d12a32888ec8c [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002/*
3 * EFI watchdog
4 *
5 * Copyright (c) 2017 Heinrich Schuchardt
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02006 */
7
Heinrich Schuchardt955a3212025-01-16 20:26:59 +01008#define LOG_CATEGORY LOGC_EFI
9
Heinrich Schuchardt18081d42017-10-18 18:13:04 +020010#include <efi_loader.h>
11
12/* Conversion factor from seconds to multiples of 100ns */
13#define EFI_SECONDS_TO_100NS 10000000ULL
14
15static struct efi_event *watchdog_timer_event;
16
Heinrich Schuchardtf011aa42020-04-10 17:51:56 +020017/**
18 * efi_watchdog_timer_notify() - resets system upon watchdog event
19 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +020020 * Reset the system when the watchdog event is notified.
21 *
22 * @event: the watchdog event
23 * @context: not used
24 */
25static void EFIAPI efi_watchdog_timer_notify(struct efi_event *event,
26 void *context)
27{
28 EFI_ENTRY("%p, %p", event, context);
29
30 printf("\nEFI: Watchdog timeout\n");
Heinrich Schuchardte2345182021-09-09 07:47:05 +020031 do_reset(NULL, 0, 0, NULL);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +020032
33 EFI_EXIT(EFI_UNSUPPORTED);
34}
35
Heinrich Schuchardtf011aa42020-04-10 17:51:56 +020036/**
37 * efi_set_watchdog() - resets the watchdog timer
Heinrich Schuchardt18081d42017-10-18 18:13:04 +020038 *
39 * This function is used by the SetWatchdogTimer service.
40 *
41 * @timeout: seconds before reset by watchdog
Heinrich Schuchardtf011aa42020-04-10 17:51:56 +020042 * Return: status code
Heinrich Schuchardt18081d42017-10-18 18:13:04 +020043 */
44efi_status_t efi_set_watchdog(unsigned long timeout)
45{
46 efi_status_t r;
47
48 if (timeout)
49 /* Reset watchdog */
50 r = efi_set_timer(watchdog_timer_event, EFI_TIMER_RELATIVE,
51 EFI_SECONDS_TO_100NS * timeout);
52 else
53 /* Deactivate watchdog */
54 r = efi_set_timer(watchdog_timer_event, EFI_TIMER_STOP, 0);
55 return r;
56}
57
Heinrich Schuchardtf011aa42020-04-10 17:51:56 +020058/**
59 * efi_watchdog_register() - initializes the EFI watchdog
60 *
61 * This function is called by efi_init_obj_list().
Heinrich Schuchardt18081d42017-10-18 18:13:04 +020062 *
Heinrich Schuchardtf011aa42020-04-10 17:51:56 +020063 * Return: status code
Heinrich Schuchardt18081d42017-10-18 18:13:04 +020064 */
Heinrich Schuchardtf2704cc2018-03-03 15:28:57 +010065efi_status_t efi_watchdog_register(void)
Heinrich Schuchardt18081d42017-10-18 18:13:04 +020066{
67 efi_status_t r;
68
69 /*
70 * Create a timer event.
71 */
72 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +010073 efi_watchdog_timer_notify, NULL, NULL,
Heinrich Schuchardt18081d42017-10-18 18:13:04 +020074 &watchdog_timer_event);
75 if (r != EFI_SUCCESS) {
76 printf("ERROR: Failed to register watchdog event\n");
77 return r;
78 }
Masahisa Kojimad250a8f2022-02-22 09:58:30 +090079
Heinrich Schuchardtf2704cc2018-03-03 15:28:57 +010080 return EFI_SUCCESS;
Heinrich Schuchardt18081d42017-10-18 18:13:04 +020081}