xref: /netbsd-src/sys/stand/efiboot/efigetsecs.c (revision 0e916d8ec69b8343eccb75e0d1374b1c99d594e2)
1 /*	$NetBSD: efigetsecs.c,v 1.5 2021/10/06 10:13:19 jmcneill Exp $	*/
2 
3 /*
4  * Copyright (c) 2015 YASUOKA Masahiko <yasuoka@yasuoka.net>
5  * Copyright (c) 2018 Jared McNeill <jmcneill@invisible.ca>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "efiboot.h"
21 
22 #include <lib/libsa/net.h>
23 
24 static EFI_EVENT getsecs_ev = 0;
25 static satime_t getsecs_val = 0;
26 
27 static satime_t
getsecs_rtc(void)28 getsecs_rtc(void)
29 {
30 	static const int daytab[][14] = {
31 	    { 0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 },
32 	    { 0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
33 	};
34 	EFI_TIME t;
35 	satime_t r;
36 	int y;
37 #define isleap(_y) (((_y) % 4) == 0 && (((_y) % 100) != 0 || ((_y) % 400) == 0))
38 
39 	uefi_call_wrapper(RT->GetTime, 2, &t, NULL);
40 
41 	/* Calc days from UNIX epoch */
42 	r = (t.Year - 1970) * 365;
43 	for (y = 1970; y < t.Year; y++) {
44 		if (isleap(y))
45 			r++;
46 	}
47 	r += daytab[isleap(t.Year) ? 1 : 0][t.Month] + t.Day;
48 
49 	/* Calc secs */
50 	r *= 60 * 60 * 24;
51 	r += ((t.Hour * 60) + t.Minute) * 60 + t.Second;
52 	if (-24 * 60 < t.TimeZone && t.TimeZone < 24 * 60)
53 		r += t.TimeZone * 60;
54 
55 	return r;
56 }
57 
58 static EFIAPI void
getsecs_notify_func(EFI_EVENT ev,VOID * context)59 getsecs_notify_func(EFI_EVENT ev, VOID *context)
60 {
61 	getsecs_val++;
62 }
63 
64 satime_t
getsecs(void)65 getsecs(void)
66 {
67 	EFI_STATUS status;
68 
69 	if (getsecs_ev == 0) {
70 		status = uefi_call_wrapper(BS->CreateEvent, 5, EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
71 		    getsecs_notify_func, 0, &getsecs_ev);
72 		if (EFI_ERROR(status))
73 			panic("%s: couldn't create event timer: 0x%lx", __func__, (u_long)status);
74 		status = uefi_call_wrapper(BS->SetTimer, 3, getsecs_ev, TimerPeriodic, 10000000);	/* 1s in "100ns" units */
75 		if (EFI_ERROR(status))
76 			panic("%s: couldn't start event timer: 0x%lx", __func__, (u_long)status);
77 		getsecs_val = getsecs_rtc();
78 	}
79 
80 	return getsecs_val;
81 }
82