8559 Add EFI utility functions to libefi
[unleashed.git] / usr / src / boot / sys / boot / efi / libefi / time.c
blobc475ed7a4148f304dfa89f4e85b5147ef827e831
1 /*
2 * Copyright (c) 1999, 2000
3 * Intel Corporation.
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
20 * This product includes software developed by Intel Corporation and
21 * its contributors.
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.
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.
41 #include <sys/cdefs.h>
43 #include <efi.h>
44 #include <efilib.h>
46 #include <time.h>
47 #include <sys/time.h>
50 * Accurate only for the past couple of centuries;
51 * that will probably do.
53 * (#defines From FreeBSD 3.2 lib/libc/stdtime/tzfile.h)
56 #define isleap(y) (((y) % 4) == 0 && \
57 (((y) % 100) != 0 || ((y) % 400) == 0))
58 #define SECSPERHOUR (60*60)
59 #define SECSPERDAY (24 * SECSPERHOUR)
62 * These arrays give the cumulative number of days up to the first of the
63 * month number used as the index (1 -> 12) for regular and leap years.
64 * The value at index 13 is for the whole year.
66 static const time_t CumulativeDays[2][14] = {
67 {0,
69 31,
70 31 + 28,
71 31 + 28 + 31,
72 31 + 28 + 31 + 30,
73 31 + 28 + 31 + 30 + 31,
74 31 + 28 + 31 + 30 + 31 + 30,
75 31 + 28 + 31 + 30 + 31 + 30 + 31,
76 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
77 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
78 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
79 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
80 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 },
81 {0,
83 31,
84 31 + 29,
85 31 + 29 + 31,
86 31 + 29 + 31 + 30,
87 31 + 29 + 31 + 30 + 31,
88 31 + 29 + 31 + 30 + 31 + 30,
89 31 + 29 + 31 + 30 + 31 + 30 + 31,
90 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,
91 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
92 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
93 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
94 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 }};
96 void
97 efi_time_init(void)
101 void
102 efi_time_fini(void)
106 void
107 to_efi_time(EFI_TIME *efi_time, time_t time)
109 int lyear, month;
110 time_t seconds;
112 if (time >= 0) {
113 efi_time->Year = 1970;
114 lyear = isleap(efi_time->Year);
115 month = 13;
116 seconds = CumulativeDays[lyear][month] * SECSPERDAY;
117 while (time > seconds) {
118 time -= seconds;
119 efi_time->Year++;
120 lyear = isleap(efi_time->Year);
121 seconds = CumulativeDays[lyear][month] * SECSPERDAY;
124 efi_time->Month = 0;
125 while (time >
126 CumulativeDays[lyear][efi_time->Month] * SECSPERDAY) {
127 efi_time->Month++;
130 month = efi_time->Month - 1;
131 time -= CumulativeDays[lyear][month] * SECSPERDAY;
133 for (efi_time->Day = 0; time > SECSPERDAY; efi_time->Day++)
134 time -= SECSPERDAY;
136 for (efi_time->Hour = 0; time > SECSPERHOUR; efi_time->Hour++)
137 time -= SECSPERHOUR;
139 for (efi_time->Minute = 0; time > 60; efi_time->Minute++)
140 time -= 60;
142 efi_time->Second = time;
143 efi_time->Nanosecond = 0;
144 efi_time->TimeZone = 0;
145 efi_time->Daylight = 0;
146 } else {
147 memset(efi_time, 0, sizeof (EFI_TIME));
151 time_t
152 from_efi_time(EFI_TIME *ETime)
154 time_t UTime;
155 int Year;
158 * Do a santity check
160 if (ETime->Year < 1998 || ETime->Year > 2099 ||
161 ETime->Month == 0 || ETime->Month > 12 ||
162 ETime->Day == 0 || ETime->Month > 31 ||
163 ETime->Hour > 23 || ETime->Minute > 59 ||
164 ETime->Second > 59 || ETime->TimeZone < -1440 ||
165 (ETime->TimeZone > 1440 && ETime->TimeZone != 2047)) {
166 return (0);
170 * Years
172 UTime = 0;
173 for (Year = 1970; Year != ETime->Year; ++Year) {
174 UTime += (CumulativeDays[isleap(Year)][13] * SECSPERDAY);
178 * UTime should now be set to 00:00:00 on Jan 1 of the file's year.
180 * Months
182 UTime += (CumulativeDays[isleap(ETime->Year)][ETime->Month] *
183 SECSPERDAY);
186 * UTime should now be set to 00:00:00 on the first of the file's
187 * month and year.
189 * Days -- Don't count the file's day
191 UTime += (((ETime->Day > 0) ? ETime->Day-1:0) * SECSPERDAY);
194 * Hours
196 UTime += (ETime->Hour * SECSPERHOUR);
199 * Minutes
201 UTime += (ETime->Minute * 60);
204 * Seconds
206 UTime += ETime->Second;
209 * EFI time is repored in local time. Adjust for any time zone
210 * offset to get true UT
212 if (ETime->TimeZone != EFI_UNSPECIFIED_TIMEZONE) {
214 * TimeZone is kept in minues...
216 UTime += (ETime->TimeZone * 60);
219 return (UTime);
222 static int
223 EFI_GetTimeOfDay(OUT struct timeval *tp, OUT struct timezone *tzp)
225 EFI_TIME EfiTime;
226 EFI_TIME_CAPABILITIES Capabilities;
227 EFI_STATUS Status;
230 * Get time from EFI
233 Status = RS->GetTime(&EfiTime, &Capabilities);
234 if (EFI_ERROR(Status))
235 return (-1);
238 * Convert to UNIX time (ie seconds since the epoch)
241 tp->tv_sec = from_efi_time(&EfiTime);
242 tp->tv_usec = 0; /* EfiTime.Nanosecond * 1000; */
245 * Do something with the timezone if needed
248 if (tzp != NULL) {
249 if (EfiTime.TimeZone == EFI_UNSPECIFIED_TIMEZONE)
250 tzp->tz_minuteswest = 0;
251 else
252 tzp->tz_minuteswest = EfiTime.TimeZone;
254 * This isn't quit right since it doesn't deal with
255 * EFI_TIME_IN_DAYLIGHT
257 tzp->tz_dsttime =
258 EfiTime.Daylight & EFI_TIME_ADJUST_DAYLIGHT ? 1 : 0;
261 return (0);
264 time_t
265 time(time_t *tloc)
267 struct timeval tv;
269 memset(&tv, 0, sizeof (tv));
270 EFI_GetTimeOfDay(&tv, NULL);
272 if (tloc)
273 *tloc = tv.tv_sec;
274 return (tv.tv_sec);
277 time_t
278 getsecs(void)
281 return (time(NULL));