1301306Sandrew/*-
2301306Sandrew * Copyright (c) 2016 Andrew Turner
3301306Sandrew * All rights reserved.
4301306Sandrew *
5301306Sandrew * Redistribution and use in source and binary forms, with or without
6301306Sandrew * modification, are permitted provided that the following conditions
7301306Sandrew * are met:
8301306Sandrew * 1. Redistributions of source code must retain the above copyright
9301306Sandrew *    notice, this list of conditions and the following disclaimer.
10301306Sandrew * 2. Redistributions in binary form must reproduce the above copyright
11301306Sandrew *    notice, this list of conditions and the following disclaimer in the
12301306Sandrew *    documentation and/or other materials provided with the distribution.
13301306Sandrew *
14301306Sandrew * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15301306Sandrew * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16301306Sandrew * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17301306Sandrew * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18301306Sandrew * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19301306Sandrew * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20301306Sandrew * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21301306Sandrew * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22301306Sandrew * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23301306Sandrew * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24301306Sandrew * SUCH DAMAGE.
25301306Sandrew */
26301306Sandrew
27301306Sandrew#include <sys/cdefs.h>
28301306Sandrew__FBSDID("$FreeBSD: stable/11/stand/efi/libefi/time_event.c 329010 2018-02-08 02:44:21Z kevans $");
29301306Sandrew
30301306Sandrew#include <efi.h>
31301306Sandrew#include <efilib.h>
32301306Sandrew
33301306Sandrew#include <time.h>
34301306Sandrew#include <sys/time.h>
35301306Sandrew
36301306Sandrewstatic EFI_EVENT time_event;
37301306Sandrewstatic uint64_t curtime;
38301306Sandrew
39301306Sandrewstatic void
40301306Sandrewtime_update(EFI_EVENT event, void *context)
41301306Sandrew{
42301306Sandrew
43301306Sandrew	curtime += 10;
44301306Sandrew}
45301306Sandrew
46301306Sandrewvoid
47301306Sandrewefi_time_init(void)
48301306Sandrew{
49301306Sandrew
50301306Sandrew	/* Create a timer event */
51301306Sandrew	BS->CreateEvent(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
52301306Sandrew	    time_update, 0, &time_event);
53301306Sandrew	/* Use a 10ms timer */
54301306Sandrew	BS->SetTimer(time_event, TimerPeriodic, 100000);
55301306Sandrew}
56301306Sandrew
57301306Sandrewvoid
58301306Sandrewefi_time_fini(void)
59301306Sandrew{
60301306Sandrew
61301306Sandrew	/* Cancel the timer */
62301306Sandrew	BS->SetTimer(time_event, TimerCancel, 0);
63301306Sandrew	BS->CloseEvent(time_event);
64301306Sandrew}
65301306Sandrew
66301306Sandrewtime_t
67301306Sandrewtime(time_t *tloc)
68301306Sandrew{
69301306Sandrew	time_t t;
70301306Sandrew
71301306Sandrew	t = curtime / 1000;
72301306Sandrew	if (tloc != NULL)
73301306Sandrew		*tloc = t;
74301306Sandrew
75301306Sandrew	return (t);
76301306Sandrew}
77301306Sandrew
78301306Sandrewtime_t
79329010Skevansgetsecs(void)
80301306Sandrew{
81301306Sandrew    return time(0);
82301306Sandrew}
83