aboutsummaryrefslogtreecommitdiff
path: root/lib/efi_loader/efi_watchdog.c
blob: d741076dcdd952290d920e37ac6996e3604ce713 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// SPDX-License-Identifier: GPL-2.0+
/*
 *  EFI watchdog
 *
 *  Copyright (c) 2017 Heinrich Schuchardt
 */

#include <common.h>
#include <efi_loader.h>

/* Conversion factor from seconds to multiples of 100ns */
#define EFI_SECONDS_TO_100NS 10000000ULL

static struct efi_event *watchdog_timer_event;

/**
 * efi_watchdog_timer_notify() - resets system upon watchdog event
 *
 * Reset the system when the watchdog event is notified.
 *
 * @event:	the watchdog event
 * @context:	not used
 */
static void EFIAPI efi_watchdog_timer_notify(struct efi_event *event,
					     void *context)
{
	EFI_ENTRY("%p, %p", event, context);

	printf("\nEFI: Watchdog timeout\n");
	do_reset(NULL, 0, 0, NULL);

	EFI_EXIT(EFI_UNSUPPORTED);
}

/**
 * efi_set_watchdog() - resets the watchdog timer
 *
 * This function is used by the SetWatchdogTimer service.
 *
 * @timeout:		seconds before reset by watchdog
 * Return:		status code
 */
efi_status_t efi_set_watchdog(unsigned long timeout)
{
	efi_status_t r;

	if (timeout)
		/* Reset watchdog */
		r = efi_set_timer(watchdog_timer_event, EFI_TIMER_RELATIVE,
				  EFI_SECONDS_TO_100NS * timeout);
	else
		/* Deactivate watchdog */
		r = efi_set_timer(watchdog_timer_event, EFI_TIMER_STOP, 0);
	return r;
}

/**
 * efi_watchdog_register() - initializes the EFI watchdog
 *
 * This function is called by efi_init_obj_list().
 *
 * Return:	status code
 */
efi_status_t efi_watchdog_register(void)
{
	efi_status_t r;

	/*
	 * Create a timer event.
	 */
	r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
			     efi_watchdog_timer_notify, NULL, NULL,
			     &watchdog_timer_event);
	if (r != EFI_SUCCESS) {
		printf("ERROR: Failed to register watchdog event\n");
		return r;
	}

	return EFI_SUCCESS;
}