1*0e916d8eSjmcneill /* $NetBSD: efigetsecs.c,v 1.5 2021/10/06 10:13:19 jmcneill Exp $ */
213f5d069Sjmcneill
313f5d069Sjmcneill /*
413f5d069Sjmcneill * Copyright (c) 2015 YASUOKA Masahiko <yasuoka@yasuoka.net>
513f5d069Sjmcneill * Copyright (c) 2018 Jared McNeill <jmcneill@invisible.ca>
613f5d069Sjmcneill *
713f5d069Sjmcneill * Permission to use, copy, modify, and distribute this software for any
813f5d069Sjmcneill * purpose with or without fee is hereby granted, provided that the above
913f5d069Sjmcneill * copyright notice and this permission notice appear in all copies.
1013f5d069Sjmcneill *
1113f5d069Sjmcneill * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1213f5d069Sjmcneill * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1313f5d069Sjmcneill * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1413f5d069Sjmcneill * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1513f5d069Sjmcneill * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1613f5d069Sjmcneill * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1713f5d069Sjmcneill * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1813f5d069Sjmcneill */
1913f5d069Sjmcneill
2013f5d069Sjmcneill #include "efiboot.h"
2113f5d069Sjmcneill
2213f5d069Sjmcneill #include <lib/libsa/net.h>
2313f5d069Sjmcneill
2413f5d069Sjmcneill static EFI_EVENT getsecs_ev = 0;
2513f5d069Sjmcneill static satime_t getsecs_val = 0;
2613f5d069Sjmcneill
2713f5d069Sjmcneill static satime_t
getsecs_rtc(void)2813f5d069Sjmcneill getsecs_rtc(void)
2913f5d069Sjmcneill {
3013f5d069Sjmcneill static const int daytab[][14] = {
3113f5d069Sjmcneill { 0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 },
3213f5d069Sjmcneill { 0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
3313f5d069Sjmcneill };
3413f5d069Sjmcneill EFI_TIME t;
3513f5d069Sjmcneill satime_t r;
3613f5d069Sjmcneill int y;
3713f5d069Sjmcneill #define isleap(_y) (((_y) % 4) == 0 && (((_y) % 100) != 0 || ((_y) % 400) == 0))
3813f5d069Sjmcneill
3913f5d069Sjmcneill uefi_call_wrapper(RT->GetTime, 2, &t, NULL);
4013f5d069Sjmcneill
4113f5d069Sjmcneill /* Calc days from UNIX epoch */
4213f5d069Sjmcneill r = (t.Year - 1970) * 365;
4313f5d069Sjmcneill for (y = 1970; y < t.Year; y++) {
4413f5d069Sjmcneill if (isleap(y))
4513f5d069Sjmcneill r++;
4613f5d069Sjmcneill }
4713f5d069Sjmcneill r += daytab[isleap(t.Year) ? 1 : 0][t.Month] + t.Day;
4813f5d069Sjmcneill
4913f5d069Sjmcneill /* Calc secs */
5013f5d069Sjmcneill r *= 60 * 60 * 24;
5113f5d069Sjmcneill r += ((t.Hour * 60) + t.Minute) * 60 + t.Second;
5213f5d069Sjmcneill if (-24 * 60 < t.TimeZone && t.TimeZone < 24 * 60)
5313f5d069Sjmcneill r += t.TimeZone * 60;
5413f5d069Sjmcneill
5513f5d069Sjmcneill return r;
5613f5d069Sjmcneill }
5713f5d069Sjmcneill
58*0e916d8eSjmcneill static EFIAPI void
getsecs_notify_func(EFI_EVENT ev,VOID * context)5913f5d069Sjmcneill getsecs_notify_func(EFI_EVENT ev, VOID *context)
6013f5d069Sjmcneill {
6113f5d069Sjmcneill getsecs_val++;
6213f5d069Sjmcneill }
6313f5d069Sjmcneill
6413f5d069Sjmcneill satime_t
getsecs(void)6513f5d069Sjmcneill getsecs(void)
6613f5d069Sjmcneill {
6713f5d069Sjmcneill EFI_STATUS status;
6813f5d069Sjmcneill
6913f5d069Sjmcneill if (getsecs_ev == 0) {
7013f5d069Sjmcneill status = uefi_call_wrapper(BS->CreateEvent, 5, EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
7113f5d069Sjmcneill getsecs_notify_func, 0, &getsecs_ev);
7213f5d069Sjmcneill if (EFI_ERROR(status))
730ef050d6Sjmcneill panic("%s: couldn't create event timer: 0x%lx", __func__, (u_long)status);
7413f5d069Sjmcneill status = uefi_call_wrapper(BS->SetTimer, 3, getsecs_ev, TimerPeriodic, 10000000); /* 1s in "100ns" units */
7513f5d069Sjmcneill if (EFI_ERROR(status))
760ef050d6Sjmcneill panic("%s: couldn't start event timer: 0x%lx", __func__, (u_long)status);
7713f5d069Sjmcneill getsecs_val = getsecs_rtc();
7813f5d069Sjmcneill }
7913f5d069Sjmcneill
8013f5d069Sjmcneill return getsecs_val;
8113f5d069Sjmcneill }
82