1*4fe48c6eSAndrew Turner /*
204f8e093SToomas Soome  * Copyright (c) 2016 Andrew Turner
304f8e093SToomas Soome  * All rights reserved.
404f8e093SToomas Soome  *
504f8e093SToomas Soome  * Redistribution and use in source and binary forms, with or without
604f8e093SToomas Soome  * modification, are permitted provided that the following conditions
704f8e093SToomas Soome  * are met:
804f8e093SToomas Soome  * 1. Redistributions of source code must retain the above copyright
904f8e093SToomas Soome  *    notice, this list of conditions and the following disclaimer.
1004f8e093SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
1104f8e093SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
1204f8e093SToomas Soome  *    documentation and/or other materials provided with the distribution.
1304f8e093SToomas Soome  *
1404f8e093SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1504f8e093SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1604f8e093SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1704f8e093SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1804f8e093SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1904f8e093SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2004f8e093SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2104f8e093SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2204f8e093SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2304f8e093SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2404f8e093SToomas Soome  * SUCH DAMAGE.
2504f8e093SToomas Soome  */
2604f8e093SToomas Soome 
2704f8e093SToomas Soome #include <sys/cdefs.h>
2804f8e093SToomas Soome 
2904f8e093SToomas Soome #include <efi.h>
3004f8e093SToomas Soome #include <efilib.h>
3104f8e093SToomas Soome 
3204f8e093SToomas Soome #include <time.h>
3304f8e093SToomas Soome #include <sys/time.h>
3404f8e093SToomas Soome 
3504f8e093SToomas Soome static EFI_EVENT time_event;
3604f8e093SToomas Soome static uint64_t curtime;
3704f8e093SToomas Soome 
3804f8e093SToomas Soome static void
time_update(EFI_EVENT event,void * context)3904f8e093SToomas Soome time_update(EFI_EVENT event, void *context)
4004f8e093SToomas Soome {
4104f8e093SToomas Soome 
42*4fe48c6eSAndrew Turner 	curtime++;
4304f8e093SToomas Soome }
4404f8e093SToomas Soome 
4504f8e093SToomas Soome void
efi_time_init(void)4604f8e093SToomas Soome efi_time_init(void)
4704f8e093SToomas Soome {
4804f8e093SToomas Soome 
4904f8e093SToomas Soome 	/* Create a timer event */
5004f8e093SToomas Soome 	BS->CreateEvent(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
5104f8e093SToomas Soome 	    time_update, 0, &time_event);
52*4fe48c6eSAndrew Turner 	/* Use a 1s timer */
53*4fe48c6eSAndrew Turner 	BS->SetTimer(time_event, TimerPeriodic, 10000000);
5404f8e093SToomas Soome }
5504f8e093SToomas Soome 
5604f8e093SToomas Soome void
efi_time_fini(void)5704f8e093SToomas Soome efi_time_fini(void)
5804f8e093SToomas Soome {
5904f8e093SToomas Soome 
6004f8e093SToomas Soome 	/* Cancel the timer */
6104f8e093SToomas Soome 	BS->SetTimer(time_event, TimerCancel, 0);
6204f8e093SToomas Soome 	BS->CloseEvent(time_event);
6304f8e093SToomas Soome }
6404f8e093SToomas Soome 
6504f8e093SToomas Soome time_t
time(time_t * tloc)6604f8e093SToomas Soome time(time_t *tloc)
6704f8e093SToomas Soome {
6804f8e093SToomas Soome 	time_t t;
6904f8e093SToomas Soome 
70*4fe48c6eSAndrew Turner 	t = curtime;
7104f8e093SToomas Soome 	if (tloc != NULL)
7204f8e093SToomas Soome 		*tloc = t;
7304f8e093SToomas Soome 
7404f8e093SToomas Soome 	return (t);
7504f8e093SToomas Soome }
7604f8e093SToomas Soome 
7704f8e093SToomas Soome time_t
getsecs(void)7804f8e093SToomas Soome getsecs(void)
7904f8e093SToomas Soome {
80*4fe48c6eSAndrew Turner 	return (time(0));
8104f8e093SToomas Soome }
82