xref: /freebsd/stand/efi/libefi/time.c (revision 61ba55bcf70f2340f9c943c9571113b3fd8eda69)
1 /*-
2  * Copyright (c) 1999, 2000
3  * Intel Corporation.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *
20  *    This product includes software developed by Intel Corporation and
21  *    its contributors.
22  *
23  * 4. Neither the name of Intel Corporation or its contributors may be
24  *    used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION AND CONTRIBUTORS ``AS IS''
28  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL INTEL CORPORATION OR CONTRIBUTORS BE
31  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
37  * THE POSSIBILITY OF SUCH DAMAGE.
38  *
39  */
40 
41 #include <sys/cdefs.h>
42 #include <efi.h>
43 #include <efilib.h>
44 
45 #include <time.h>
46 #include <sys/time.h>
47 
48 /*
49  * Accurate only for the past couple of centuries;
50  * that will probably do.
51  *
52  * (#defines From FreeBSD 3.2 lib/libc/stdtime/tzfile.h)
53  */
54 
55 #define	isleap(y)	(((y) % 4) == 0 && \
56 			    (((y) % 100) != 0 || ((y) % 400) == 0))
57 #define	SECSPERHOUR	(60*60)
58 #define	SECSPERDAY	(24 * SECSPERHOUR)
59 
60 /*
61  *  These arrays give the cumulative number of days up to the first of the
62  *  month number used as the index (1 -> 12) for regular and leap years.
63  *  The value at index 13 is for the whole year.
64  */
65 static const time_t CumulativeDays[2][14] = {
66 	{0,
67 	0,
68 	31,
69 	31 + 28,
70 	31 + 28 + 31,
71 	31 + 28 + 31 + 30,
72 	31 + 28 + 31 + 30 + 31,
73 	31 + 28 + 31 + 30 + 31 + 30,
74 	31 + 28 + 31 + 30 + 31 + 30 + 31,
75 	31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
76 	31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
77 	31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
78 	31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
79 	31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 },
80 	{0,
81 	0,
82 	31,
83 	31 + 29,
84 	31 + 29 + 31,
85 	31 + 29 + 31 + 30,
86 	31 + 29 + 31 + 30 + 31,
87 	31 + 29 + 31 + 30 + 31 + 30,
88 	31 + 29 + 31 + 30 + 31 + 30 + 31,
89 	31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,
90 	31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
91 	31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
92 	31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
93 	31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 }};
94 
95 void
96 efi_time_init(void)
97 {
98 }
99 
100 void
101 efi_time_fini(void)
102 {
103 }
104 
105 void
106 to_efi_time(EFI_TIME *efi_time, time_t time)
107 {
108 	int lyear, month;
109 	time_t seconds;
110 
111 	if (time >= 0) {
112 		efi_time->Year = 1970;
113 		lyear = isleap(efi_time->Year);
114 		month = 13;
115 		seconds = CumulativeDays[lyear][month] * SECSPERDAY;
116                 while (time > seconds) {
117 			time -= seconds;
118 			efi_time->Year++;
119 			lyear = isleap(efi_time->Year);
120 			seconds = CumulativeDays[lyear][month] * SECSPERDAY;
121 		}
122 
123 		efi_time->Month = 0;
124                 while (time >
125 		    CumulativeDays[lyear][month] * SECSPERDAY) {
126 			efi_time->Month++;
127 		}
128 
129 		month = efi_time->Month - 1;
130 		time -= CumulativeDays[lyear][month] * SECSPERDAY;
131 
132 		for (efi_time->Day = 0; time > SECSPERDAY; efi_time->Day++)
133 			time -= SECSPERDAY;
134 
135 		for (efi_time->Hour = 0; time > SECSPERHOUR; efi_time->Hour++)
136 			time -= SECSPERHOUR;
137 
138 		for (efi_time->Minute = 0; time > 60; efi_time->Minute++)
139 			time -= 60;
140 
141 		efi_time->Second = time;
142 		efi_time->Nanosecond = 0;
143 		efi_time->TimeZone = 0;
144 		efi_time->Daylight = 0;
145 	} else {
146 		memset(efi_time, 0, sizeof(EFI_TIME));
147 	}
148 }
149 
150 time_t
151 from_efi_time(EFI_TIME *ETime)
152 {
153 	time_t  UTime;
154 	int	Year;
155 
156 	/*
157 	 *  Do a santity check
158 	 */
159 	if (ETime->Year  <  1998 || ETime->Year   > 2099 ||
160 	    ETime->Month ==    0 || ETime->Month  >   12 ||
161 	    ETime->Day   ==    0 || ETime->Month  >   31 ||
162 	    ETime->Hour   >   23 || ETime->Minute >   59 ||
163 	    ETime->Second >   59 || ETime->TimeZone  < -1440 ||
164 	    (ETime->TimeZone >  1440 && ETime->TimeZone != 2047)) {
165 		return (0);
166 	}
167 
168 	/*
169 	 * Years
170 	 */
171 	UTime = 0;
172 	for (Year = 1970; Year != ETime->Year; ++Year) {
173 		UTime += (CumulativeDays[isleap(Year)][13] * SECSPERDAY);
174 	}
175 
176 	/*
177 	 * UTime should now be set to 00:00:00 on Jan 1 of the file's year.
178 	 *
179 	 * Months
180 	 */
181 	UTime += (CumulativeDays[isleap(ETime->Year)][ETime->Month] *
182 	    SECSPERDAY);
183 
184 	/*
185 	 * UTime should now be set to 00:00:00 on the first of the file's
186 	 * month and year.
187 	 *
188 	 * Days -- Don't count the file's day
189 	 */
190 	UTime += (((ETime->Day > 0) ? ETime->Day-1:0) * SECSPERDAY);
191 
192 	/*
193 	 * Hours
194 	 */
195 	UTime += (ETime->Hour * SECSPERHOUR);
196 
197 	/*
198 	 * Minutes
199 	 */
200 	UTime += (ETime->Minute * 60);
201 
202 	/*
203 	 * Seconds
204 	 */
205 	UTime += ETime->Second;
206 
207 	/*
208 	 * EFI time is repored in local time.  Adjust for any time zone
209 	 * offset to get true UT
210 	 */
211 	if (ETime->TimeZone != EFI_UNSPECIFIED_TIMEZONE) {
212 		/*
213 		 * TimeZone is kept in minues...
214 		 */
215 		UTime += (ETime->TimeZone * 60);
216 	}
217 
218 	return (UTime);
219 }
220 
221 static int
222 EFI_GetTimeOfDay(OUT struct timeval *tp, OUT struct timezone *tzp)
223 {
224 	EFI_TIME		EfiTime;
225 	EFI_TIME_CAPABILITIES	Capabilities;
226 	EFI_STATUS		Status;
227 
228 	/*
229 	 *  Get time from EFI
230 	 */
231 
232 	Status = RS->GetTime(&EfiTime, &Capabilities);
233 	if (EFI_ERROR(Status))
234 		return (-1);
235 
236 	/*
237 	 *  Convert to UNIX time (ie seconds since the epoch
238 	 */
239 
240 	tp->tv_sec  = from_efi_time(&EfiTime);
241 	tp->tv_usec = 0; /* EfiTime.Nanosecond * 1000; */
242 
243 	/*
244 	 * Do something with the timezone if needed
245 	 */
246 
247 	if (tzp != NULL) {
248 		if (EfiTime.TimeZone == EFI_UNSPECIFIED_TIMEZONE)
249 			tzp->tz_minuteswest = 0;
250 		else
251 			tzp->tz_minuteswest = EfiTime.TimeZone;
252 		/*
253 		 * This isn't quit right since it doesn't deal with
254 		 * EFI_TIME_IN_DAYLIGHT
255 		 */
256 		tzp->tz_dsttime =
257 			EfiTime.Daylight & EFI_TIME_ADJUST_DAYLIGHT ? 1 : 0;
258 	}
259 
260 	return (0);
261 }
262 
263 time_t
264 time(time_t *tloc)
265 {
266 	struct timeval tv;
267 
268 	memset(&tv, 0, sizeof(tv));
269 	EFI_GetTimeOfDay(&tv, NULL);
270 
271 	if (tloc)
272 		*tloc = tv.tv_sec;
273 	return (tv.tv_sec);
274 }
275 
276 time_t
277 getsecs(void)
278 {
279 
280     return (time(NULL));
281 }
282