xref: /illumos-gate/usr/src/boot/sys/boot/efi/libefi/time.c (revision 5e9f42ab)
1*5e9f42abSToomas Soome /*
2199767f8SToomas Soome  * Copyright (c) 1999, 2000
3199767f8SToomas Soome  * Intel Corporation.
4199767f8SToomas Soome  * All rights reserved.
5*5e9f42abSToomas Soome  *
6199767f8SToomas Soome  * Redistribution and use in source and binary forms, with or without
7199767f8SToomas Soome  * modification, are permitted provided that the following conditions
8199767f8SToomas Soome  * are met:
9*5e9f42abSToomas Soome  *
10199767f8SToomas Soome  * 1. Redistributions of source code must retain the above copyright
11199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer.
12*5e9f42abSToomas Soome  *
13199767f8SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
14199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
15199767f8SToomas Soome  *    documentation and/or other materials provided with the distribution.
16*5e9f42abSToomas Soome  *
17199767f8SToomas Soome  * 3. All advertising materials mentioning features or use of this software
18199767f8SToomas Soome  *    must display the following acknowledgement:
19*5e9f42abSToomas Soome  *
20199767f8SToomas Soome  *    This product includes software developed by Intel Corporation and
21199767f8SToomas Soome  *    its contributors.
22*5e9f42abSToomas Soome  *
23199767f8SToomas Soome  * 4. Neither the name of Intel Corporation or its contributors may be
24199767f8SToomas Soome  *    used to endorse or promote products derived from this software
25199767f8SToomas Soome  *    without specific prior written permission.
26*5e9f42abSToomas Soome  *
27199767f8SToomas Soome  * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION AND CONTRIBUTORS ``AS IS''
28199767f8SToomas Soome  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29199767f8SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30199767f8SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL INTEL CORPORATION OR CONTRIBUTORS BE
31199767f8SToomas Soome  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32199767f8SToomas Soome  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33199767f8SToomas Soome  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34199767f8SToomas Soome  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35199767f8SToomas Soome  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36199767f8SToomas Soome  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
37199767f8SToomas Soome  * THE POSSIBILITY OF SUCH DAMAGE.
38*5e9f42abSToomas Soome  *
39199767f8SToomas Soome  */
40199767f8SToomas Soome 
41199767f8SToomas Soome #include <sys/cdefs.h>
42199767f8SToomas Soome 
43199767f8SToomas Soome #include <efi.h>
44199767f8SToomas Soome #include <efilib.h>
45199767f8SToomas Soome 
46199767f8SToomas Soome #include <time.h>
47199767f8SToomas Soome #include <sys/time.h>
48199767f8SToomas Soome 
49199767f8SToomas Soome /*
50*5e9f42abSToomas Soome  * Accurate only for the past couple of centuries;
51*5e9f42abSToomas Soome  * that will probably do.
52*5e9f42abSToomas Soome  *
53*5e9f42abSToomas Soome  * (#defines From FreeBSD 3.2 lib/libc/stdtime/tzfile.h)
54*5e9f42abSToomas Soome  */
55*5e9f42abSToomas Soome 
56*5e9f42abSToomas Soome #define	isleap(y)	(((y) % 4) == 0 && \
57*5e9f42abSToomas Soome 			    (((y) % 100) != 0 || ((y) % 400) == 0))
58*5e9f42abSToomas Soome #define	SECSPERHOUR	(60*60)
59*5e9f42abSToomas Soome #define	SECSPERDAY	(24 * SECSPERHOUR)
60199767f8SToomas Soome 
61*5e9f42abSToomas Soome /*
62*5e9f42abSToomas Soome  *  These arrays give the cumulative number of days up to the first of the
63*5e9f42abSToomas Soome  *  month number used as the index (1 -> 12) for regular and leap years.
64*5e9f42abSToomas Soome  *  The value at index 13 is for the whole year.
65*5e9f42abSToomas Soome  */
66*5e9f42abSToomas Soome static const time_t CumulativeDays[2][14] = {
67*5e9f42abSToomas Soome 	{0,
68*5e9f42abSToomas Soome 	0,
69*5e9f42abSToomas Soome 	31,
70*5e9f42abSToomas Soome 	31 + 28,
71*5e9f42abSToomas Soome 	31 + 28 + 31,
72*5e9f42abSToomas Soome 	31 + 28 + 31 + 30,
73*5e9f42abSToomas Soome 	31 + 28 + 31 + 30 + 31,
74*5e9f42abSToomas Soome 	31 + 28 + 31 + 30 + 31 + 30,
75*5e9f42abSToomas Soome 	31 + 28 + 31 + 30 + 31 + 30 + 31,
76*5e9f42abSToomas Soome 	31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
77*5e9f42abSToomas Soome 	31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
78*5e9f42abSToomas Soome 	31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
79*5e9f42abSToomas Soome 	31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
80*5e9f42abSToomas Soome 	31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 },
81*5e9f42abSToomas Soome 	{0,
82*5e9f42abSToomas Soome 	0,
83*5e9f42abSToomas Soome 	31,
84*5e9f42abSToomas Soome 	31 + 29,
85*5e9f42abSToomas Soome 	31 + 29 + 31,
86*5e9f42abSToomas Soome 	31 + 29 + 31 + 30,
87*5e9f42abSToomas Soome 	31 + 29 + 31 + 30 + 31,
88*5e9f42abSToomas Soome 	31 + 29 + 31 + 30 + 31 + 30,
89*5e9f42abSToomas Soome 	31 + 29 + 31 + 30 + 31 + 30 + 31,
90*5e9f42abSToomas Soome 	31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,
91*5e9f42abSToomas Soome 	31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
92*5e9f42abSToomas Soome 	31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
93*5e9f42abSToomas Soome 	31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
94*5e9f42abSToomas Soome 	31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 }};
95199767f8SToomas Soome 
9604f8e093SToomas Soome void
efi_time_init(void)9704f8e093SToomas Soome efi_time_init(void)
9804f8e093SToomas Soome {
9904f8e093SToomas Soome }
10004f8e093SToomas Soome 
10104f8e093SToomas Soome void
efi_time_fini(void)10204f8e093SToomas Soome efi_time_fini(void)
10304f8e093SToomas Soome {
10404f8e093SToomas Soome }
10504f8e093SToomas Soome 
106*5e9f42abSToomas Soome void
to_efi_time(EFI_TIME * efi_time,time_t time)107*5e9f42abSToomas Soome to_efi_time(EFI_TIME *efi_time, time_t time)
108199767f8SToomas Soome {
109*5e9f42abSToomas Soome 	int lyear, month;
110*5e9f42abSToomas Soome 	time_t seconds;
111*5e9f42abSToomas Soome 
112*5e9f42abSToomas Soome 	if (time >= 0) {
113*5e9f42abSToomas Soome 		efi_time->Year = 1970;
114*5e9f42abSToomas Soome 		lyear = isleap(efi_time->Year);
115*5e9f42abSToomas Soome 		month = 13;
116*5e9f42abSToomas Soome 		seconds = CumulativeDays[lyear][month] * SECSPERDAY;
117*5e9f42abSToomas Soome 		while (time > seconds) {
118*5e9f42abSToomas Soome 			time -= seconds;
119*5e9f42abSToomas Soome 			efi_time->Year++;
120*5e9f42abSToomas Soome 			lyear = isleap(efi_time->Year);
121*5e9f42abSToomas Soome 			seconds = CumulativeDays[lyear][month] * SECSPERDAY;
122*5e9f42abSToomas Soome 		}
123*5e9f42abSToomas Soome 
124*5e9f42abSToomas Soome 		efi_time->Month = 0;
125*5e9f42abSToomas Soome 		while (time >
126*5e9f42abSToomas Soome 		    CumulativeDays[lyear][efi_time->Month] * SECSPERDAY) {
127*5e9f42abSToomas Soome 			efi_time->Month++;
128*5e9f42abSToomas Soome 		}
129*5e9f42abSToomas Soome 
130*5e9f42abSToomas Soome 		month = efi_time->Month - 1;
131*5e9f42abSToomas Soome 		time -= CumulativeDays[lyear][month] * SECSPERDAY;
132*5e9f42abSToomas Soome 
133*5e9f42abSToomas Soome 		for (efi_time->Day = 0; time > SECSPERDAY; efi_time->Day++)
134*5e9f42abSToomas Soome 			time -= SECSPERDAY;
135*5e9f42abSToomas Soome 
136*5e9f42abSToomas Soome 		for (efi_time->Hour = 0; time > SECSPERHOUR; efi_time->Hour++)
137*5e9f42abSToomas Soome 			time -= SECSPERHOUR;
138*5e9f42abSToomas Soome 
139*5e9f42abSToomas Soome 		for (efi_time->Minute = 0; time > 60; efi_time->Minute++)
140*5e9f42abSToomas Soome 			time -= 60;
141*5e9f42abSToomas Soome 
142*5e9f42abSToomas Soome 		efi_time->Second = time;
143*5e9f42abSToomas Soome 		efi_time->Nanosecond = 0;
144*5e9f42abSToomas Soome 		efi_time->TimeZone = 0;
145*5e9f42abSToomas Soome 		efi_time->Daylight = 0;
146*5e9f42abSToomas Soome 	} else {
147*5e9f42abSToomas Soome 		memset(efi_time, 0, sizeof (EFI_TIME));
148*5e9f42abSToomas Soome 	}
149*5e9f42abSToomas Soome }
150*5e9f42abSToomas Soome 
151*5e9f42abSToomas Soome time_t
from_efi_time(EFI_TIME * ETime)152*5e9f42abSToomas Soome from_efi_time(EFI_TIME *ETime)
153*5e9f42abSToomas Soome {
154*5e9f42abSToomas Soome 	time_t  UTime;
155*5e9f42abSToomas Soome 	int	Year;
156*5e9f42abSToomas Soome 
157*5e9f42abSToomas Soome 	/*
158*5e9f42abSToomas Soome 	 *  Do a santity check
159*5e9f42abSToomas Soome 	 */
160*5e9f42abSToomas Soome 	if (ETime->Year  <  1998 || ETime->Year   > 2099 ||
161*5e9f42abSToomas Soome 	    ETime->Month ==    0 || ETime->Month  >   12 ||
162*5e9f42abSToomas Soome 	    ETime->Day   ==    0 || ETime->Month  >   31 ||
163*5e9f42abSToomas Soome 	    ETime->Hour   >   23 || ETime->Minute >   59 ||
164*5e9f42abSToomas Soome 	    ETime->Second >   59 || ETime->TimeZone  < -1440 ||
165*5e9f42abSToomas Soome 	    (ETime->TimeZone >  1440 && ETime->TimeZone != 2047)) {
166*5e9f42abSToomas Soome 		return (0);
167*5e9f42abSToomas Soome 	}
168*5e9f42abSToomas Soome 
169*5e9f42abSToomas Soome 	/*
170*5e9f42abSToomas Soome 	 * Years
171*5e9f42abSToomas Soome 	 */
172*5e9f42abSToomas Soome 	UTime = 0;
173*5e9f42abSToomas Soome 	for (Year = 1970; Year != ETime->Year; ++Year) {
174*5e9f42abSToomas Soome 		UTime += (CumulativeDays[isleap(Year)][13] * SECSPERDAY);
175*5e9f42abSToomas Soome 	}
176*5e9f42abSToomas Soome 
177*5e9f42abSToomas Soome 	/*
178*5e9f42abSToomas Soome 	 * UTime should now be set to 00:00:00 on Jan 1 of the file's year.
179*5e9f42abSToomas Soome 	 *
180*5e9f42abSToomas Soome 	 * Months
181*5e9f42abSToomas Soome 	 */
182*5e9f42abSToomas Soome 	UTime += (CumulativeDays[isleap(ETime->Year)][ETime->Month] *
183*5e9f42abSToomas Soome 	    SECSPERDAY);
184*5e9f42abSToomas Soome 
185*5e9f42abSToomas Soome 	/*
186*5e9f42abSToomas Soome 	 * UTime should now be set to 00:00:00 on the first of the file's
187*5e9f42abSToomas Soome 	 * month and year.
188*5e9f42abSToomas Soome 	 *
189*5e9f42abSToomas Soome 	 * Days -- Don't count the file's day
190*5e9f42abSToomas Soome 	 */
191*5e9f42abSToomas Soome 	UTime += (((ETime->Day > 0) ? ETime->Day-1:0) * SECSPERDAY);
192*5e9f42abSToomas Soome 
193*5e9f42abSToomas Soome 	/*
194*5e9f42abSToomas Soome 	 * Hours
195*5e9f42abSToomas Soome 	 */
196*5e9f42abSToomas Soome 	UTime += (ETime->Hour * SECSPERHOUR);
197*5e9f42abSToomas Soome 
198*5e9f42abSToomas Soome 	/*
199*5e9f42abSToomas Soome 	 * Minutes
200*5e9f42abSToomas Soome 	 */
201*5e9f42abSToomas Soome 	UTime += (ETime->Minute * 60);
202*5e9f42abSToomas Soome 
203*5e9f42abSToomas Soome 	/*
204*5e9f42abSToomas Soome 	 * Seconds
205*5e9f42abSToomas Soome 	 */
206*5e9f42abSToomas Soome 	UTime += ETime->Second;
207*5e9f42abSToomas Soome 
208*5e9f42abSToomas Soome 	/*
209*5e9f42abSToomas Soome 	 * EFI time is repored in local time.  Adjust for any time zone
210*5e9f42abSToomas Soome 	 * offset to get true UT
211*5e9f42abSToomas Soome 	 */
212*5e9f42abSToomas Soome 	if (ETime->TimeZone != EFI_UNSPECIFIED_TIMEZONE) {
213*5e9f42abSToomas Soome 		/*
214*5e9f42abSToomas Soome 		 *  TimeZone is kept in minues...
215*5e9f42abSToomas Soome 		 */
216*5e9f42abSToomas Soome 		UTime += (ETime->TimeZone * 60);
217*5e9f42abSToomas Soome 	}
218*5e9f42abSToomas Soome 
219*5e9f42abSToomas Soome 	return (UTime);
220199767f8SToomas Soome }
221199767f8SToomas Soome 
222199767f8SToomas Soome static int
EFI_GetTimeOfDay(OUT struct timeval * tp,OUT struct timezone * tzp)223*5e9f42abSToomas Soome EFI_GetTimeOfDay(OUT struct timeval *tp, OUT struct timezone *tzp)
224199767f8SToomas Soome {
225199767f8SToomas Soome 	EFI_TIME		EfiTime;
226199767f8SToomas Soome 	EFI_TIME_CAPABILITIES	Capabilities;
227199767f8SToomas Soome 	EFI_STATUS		Status;
228199767f8SToomas Soome 
229199767f8SToomas Soome 	/*
230*5e9f42abSToomas Soome 	 *  Get time from EFI
231*5e9f42abSToomas Soome 	 */
232199767f8SToomas Soome 
233199767f8SToomas Soome 	Status = RS->GetTime(&EfiTime, &Capabilities);
234199767f8SToomas Soome 	if (EFI_ERROR(Status))
235199767f8SToomas Soome 		return (-1);
236199767f8SToomas Soome 
237199767f8SToomas Soome 	/*
238*5e9f42abSToomas Soome 	 * Convert to UNIX time (ie seconds since the epoch)
239*5e9f42abSToomas Soome 	 */
240199767f8SToomas Soome 
241*5e9f42abSToomas Soome 	tp->tv_sec  = from_efi_time(&EfiTime);
242199767f8SToomas Soome 	tp->tv_usec = 0; /* EfiTime.Nanosecond * 1000; */
243199767f8SToomas Soome 
244199767f8SToomas Soome 	/*
245*5e9f42abSToomas Soome 	 * Do something with the timezone if needed
246*5e9f42abSToomas Soome 	 */
247199767f8SToomas Soome 
248*5e9f42abSToomas Soome 	if (tzp != NULL) {
249*5e9f42abSToomas Soome 		if (EfiTime.TimeZone == EFI_UNSPECIFIED_TIMEZONE)
250*5e9f42abSToomas Soome 			tzp->tz_minuteswest = 0;
251*5e9f42abSToomas Soome 		else
252*5e9f42abSToomas Soome 			tzp->tz_minuteswest = EfiTime.TimeZone;
253199767f8SToomas Soome 		/*
254*5e9f42abSToomas Soome 		 * This isn't quit right since it doesn't deal with
255*5e9f42abSToomas Soome 		 * EFI_TIME_IN_DAYLIGHT
256*5e9f42abSToomas Soome 		 */
257199767f8SToomas Soome 		tzp->tz_dsttime =
258199767f8SToomas Soome 			EfiTime.Daylight & EFI_TIME_ADJUST_DAYLIGHT ? 1 : 0;
259199767f8SToomas Soome 	}
260199767f8SToomas Soome 
261199767f8SToomas Soome 	return (0);
262199767f8SToomas Soome }
263199767f8SToomas Soome 
264199767f8SToomas Soome time_t
time(time_t * tloc)265199767f8SToomas Soome time(time_t *tloc)
266199767f8SToomas Soome {
267199767f8SToomas Soome 	struct timeval tv;
268*5e9f42abSToomas Soome 
269*5e9f42abSToomas Soome 	memset(&tv, 0, sizeof (tv));
270*5e9f42abSToomas Soome 	EFI_GetTimeOfDay(&tv, NULL);
271*5e9f42abSToomas Soome 
272199767f8SToomas Soome 	if (tloc)
273199767f8SToomas Soome 		*tloc = tv.tv_sec;
274*5e9f42abSToomas Soome 	return (tv.tv_sec);
275199767f8SToomas Soome }
276199767f8SToomas Soome 
277199767f8SToomas Soome time_t
getsecs(void)27804f8e093SToomas Soome getsecs(void)
279199767f8SToomas Soome {
280*5e9f42abSToomas Soome 
281*5e9f42abSToomas Soome 	return (time(NULL));
282199767f8SToomas Soome }
283