]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/boot/efi/libefi/time.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / boot / efi / libefi / time.c
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 __FBSDID("$FreeBSD$");
43
44 #include <efi.h>
45 #include <efilib.h>
46
47 #include <time.h>
48 #include <sys/time.h>
49
50 /*
51 // Accurate only for the past couple of centuries;
52 // that will probably do.
53 //
54 // (#defines From FreeBSD 3.2 lib/libc/stdtime/tzfile.h)
55 */
56
57 #define isleap(y)       (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
58 #define SECSPERHOUR ( 60*60 )
59 #define SECSPERDAY      (24 * SECSPERHOUR)
60
61 time_t
62 efi_time(EFI_TIME *ETime)
63 {
64     /*
65     //  These arrays give the cumulative number of days up to the first of the
66     //  month number used as the index (1 -> 12) for regular and leap years.
67     //  The value at index 13 is for the whole year.
68     */
69     static time_t CumulativeDays[2][14] = {
70     {0,
71      0,
72      31,
73      31 + 28,
74      31 + 28 + 31,
75      31 + 28 + 31 + 30,
76      31 + 28 + 31 + 30 + 31,
77      31 + 28 + 31 + 30 + 31 + 30,
78      31 + 28 + 31 + 30 + 31 + 30 + 31,
79      31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
80      31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
81      31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
82      31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
83      31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 },
84     {0,
85      0,
86      31,
87      31 + 29,
88      31 + 29 + 31,
89      31 + 29 + 31 + 30,
90      31 + 29 + 31 + 30 + 31,
91      31 + 29 + 31 + 30 + 31 + 30,
92      31 + 29 + 31 + 30 + 31 + 30 + 31,
93      31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,
94      31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
95      31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
96      31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
97      31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 }};
98
99     time_t  UTime; 
100     int     Year;
101
102     /*
103     //  Do a santity check
104     */
105     if ( ETime->Year  <  1998 || ETime->Year   > 2099 ||
106          ETime->Month ==    0 || ETime->Month  >   12 ||
107          ETime->Day   ==    0 || ETime->Month  >   31 ||
108                                  ETime->Hour   >   23 ||
109                                  ETime->Minute >   59 ||
110                                  ETime->Second >   59 ||
111          ETime->TimeZone  < -1440                     ||
112          (ETime->TimeZone >  1440 && ETime->TimeZone != 2047) ) {
113         return (0);
114     }
115
116     /*
117     // Years
118     */
119     UTime = 0;
120     for (Year = 1970; Year != ETime->Year; ++Year) {
121         UTime += (CumulativeDays[isleap(Year)][13] * SECSPERDAY);
122     }
123
124     /*
125     // UTime should now be set to 00:00:00 on Jan 1 of the file's year.
126     //
127     // Months  
128     */
129     UTime += (CumulativeDays[isleap(ETime->Year)][ETime->Month] * SECSPERDAY);
130
131     /*
132     // UTime should now be set to 00:00:00 on the first of the file's month and year
133     //
134     // Days -- Don't count the file's day
135     */
136     UTime += (((ETime->Day > 0) ? ETime->Day-1:0) * SECSPERDAY);
137
138     /*
139     // Hours
140     */
141     UTime += (ETime->Hour * SECSPERHOUR);
142
143     /*
144     // Minutes
145     */
146     UTime += (ETime->Minute * 60);
147
148     /*
149     // Seconds
150     */
151     UTime += ETime->Second;
152
153     /*
154     //  EFI time is repored in local time.  Adjust for any time zone offset to
155     //  get true UT
156     */
157     if ( ETime->TimeZone != EFI_UNSPECIFIED_TIMEZONE ) {
158         /*
159         //  TimeZone is kept in minues...
160         */
161         UTime += (ETime->TimeZone * 60);
162     }
163     
164     return UTime;
165 }
166
167 int
168 EFI_GetTimeOfDay(
169         OUT struct timeval *tp,
170         OUT struct timezone *tzp
171         )
172 {
173         EFI_TIME                EfiTime;
174         EFI_TIME_CAPABILITIES   Capabilities;
175         EFI_STATUS              Status;
176
177         /*
178         //  Get time from EFI
179         */
180
181         Status = RS->GetTime(&EfiTime, &Capabilities);
182         if (EFI_ERROR(Status))
183                 return (-1);
184
185         /*
186         //  Convert to UNIX time (ie seconds since the epoch
187         */
188
189         tp->tv_sec  = efi_time( &EfiTime );
190         tp->tv_usec = 0; /* EfiTime.Nanosecond * 1000; */
191
192         /*
193         //  Do something with the timezone if needed
194         */
195
196         if (tzp) {
197                 tzp->tz_minuteswest =
198                         EfiTime.TimeZone == EFI_UNSPECIFIED_TIMEZONE ? 0 : EfiTime.TimeZone;
199                 /*
200                 //  This isn't quit right since it doesn't deal with EFI_TIME_IN_DAYLIGHT
201                 */
202                 tzp->tz_dsttime =
203                         EfiTime.Daylight & EFI_TIME_ADJUST_DAYLIGHT ? 1 : 0;
204         }
205
206         return (0);
207 }
208
209 time_t
210 time(time_t *tloc)
211 {
212         struct timeval tv;
213         EFI_GetTimeOfDay(&tv, 0);
214         
215         if (tloc)
216                 *tloc = tv.tv_sec;
217         return tv.tv_sec;
218 }
219
220 time_t
221 getsecs()
222 {
223     return time(0);
224 }