11.5Sjmcneill/* $NetBSD: efigetsecs.c,v 1.5 2021/10/06 10:13:19 jmcneill Exp $ */ 21.1Sjmcneill 31.1Sjmcneill/* 41.1Sjmcneill * Copyright (c) 2015 YASUOKA Masahiko <yasuoka@yasuoka.net> 51.3Sjmcneill * Copyright (c) 2018 Jared McNeill <jmcneill@invisible.ca> 61.1Sjmcneill * 71.1Sjmcneill * Permission to use, copy, modify, and distribute this software for any 81.1Sjmcneill * purpose with or without fee is hereby granted, provided that the above 91.1Sjmcneill * copyright notice and this permission notice appear in all copies. 101.1Sjmcneill * 111.1Sjmcneill * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 121.1Sjmcneill * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 131.1Sjmcneill * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 141.1Sjmcneill * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 151.1Sjmcneill * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 161.1Sjmcneill * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 171.1Sjmcneill * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 181.1Sjmcneill */ 191.1Sjmcneill 201.1Sjmcneill#include "efiboot.h" 211.1Sjmcneill 221.1Sjmcneill#include <lib/libsa/net.h> 231.1Sjmcneill 241.3Sjmcneillstatic EFI_EVENT getsecs_ev = 0; 251.3Sjmcneillstatic satime_t getsecs_val = 0; 261.3Sjmcneill 271.3Sjmcneillstatic satime_t 281.3Sjmcneillgetsecs_rtc(void) 291.1Sjmcneill{ 301.1Sjmcneill static const int daytab[][14] = { 311.1Sjmcneill { 0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 }, 321.1Sjmcneill { 0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, 331.1Sjmcneill }; 341.1Sjmcneill EFI_TIME t; 351.1Sjmcneill satime_t r; 361.1Sjmcneill int y; 371.1Sjmcneill#define isleap(_y) (((_y) % 4) == 0 && (((_y) % 100) != 0 || ((_y) % 400) == 0)) 381.1Sjmcneill 391.1Sjmcneill uefi_call_wrapper(RT->GetTime, 2, &t, NULL); 401.1Sjmcneill 411.1Sjmcneill /* Calc days from UNIX epoch */ 421.1Sjmcneill r = (t.Year - 1970) * 365; 431.1Sjmcneill for (y = 1970; y < t.Year; y++) { 441.1Sjmcneill if (isleap(y)) 451.1Sjmcneill r++; 461.1Sjmcneill } 471.1Sjmcneill r += daytab[isleap(t.Year) ? 1 : 0][t.Month] + t.Day; 481.1Sjmcneill 491.1Sjmcneill /* Calc secs */ 501.1Sjmcneill r *= 60 * 60 * 24; 511.1Sjmcneill r += ((t.Hour * 60) + t.Minute) * 60 + t.Second; 521.1Sjmcneill if (-24 * 60 < t.TimeZone && t.TimeZone < 24 * 60) 531.1Sjmcneill r += t.TimeZone * 60; 541.1Sjmcneill 551.1Sjmcneill return r; 561.1Sjmcneill} 571.3Sjmcneill 581.5Sjmcneillstatic EFIAPI void 591.3Sjmcneillgetsecs_notify_func(EFI_EVENT ev, VOID *context) 601.3Sjmcneill{ 611.3Sjmcneill getsecs_val++; 621.3Sjmcneill} 631.3Sjmcneill 641.3Sjmcneillsatime_t 651.3Sjmcneillgetsecs(void) 661.3Sjmcneill{ 671.3Sjmcneill EFI_STATUS status; 681.3Sjmcneill 691.3Sjmcneill if (getsecs_ev == 0) { 701.3Sjmcneill status = uefi_call_wrapper(BS->CreateEvent, 5, EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK, 711.3Sjmcneill getsecs_notify_func, 0, &getsecs_ev); 721.3Sjmcneill if (EFI_ERROR(status)) 731.4Sjmcneill panic("%s: couldn't create event timer: 0x%lx", __func__, (u_long)status); 741.3Sjmcneill status = uefi_call_wrapper(BS->SetTimer, 3, getsecs_ev, TimerPeriodic, 10000000); /* 1s in "100ns" units */ 751.3Sjmcneill if (EFI_ERROR(status)) 761.4Sjmcneill panic("%s: couldn't start event timer: 0x%lx", __func__, (u_long)status); 771.3Sjmcneill getsecs_val = getsecs_rtc(); 781.3Sjmcneill } 791.3Sjmcneill 801.3Sjmcneill return getsecs_val; 811.3Sjmcneill} 82