]> git.proxmox.com Git - mirror_edk2.git/blob - EdkUnixPkg/Dxe/UnixThunk/Chipset/RealTimeClock/RealTimeClock.c
Unix version of EFI emulator
[mirror_edk2.git] / EdkUnixPkg / Dxe / UnixThunk / Chipset / RealTimeClock / RealTimeClock.c
1 /*++
2
3 Copyright (c) 2004 - 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 RealTimeClock.c
15
16 Abstract:
17
18 NT Emulation Architectural Protocol Driver as defined in Tiano
19
20 --*/
21
22 BOOLEAN
23 DayValid (
24 IN EFI_TIME *Time
25 );
26
27 BOOLEAN
28 IsLeapYear (
29 IN EFI_TIME *Time
30 );
31
32 EFI_STATUS
33 RtcTimeFieldsValid (
34 IN EFI_TIME *Time
35 );
36
37 EFI_STATUS
38 EFIAPI
39 InitializeRealTimeClock (
40 IN EFI_HANDLE ImageHandle,
41 IN EFI_SYSTEM_TABLE *SystemTable
42 );
43
44 STATIC
45 EFI_STATUS
46 EFIAPI
47 UnixGetTime (
48 OUT EFI_TIME * Time,
49 OUT EFI_TIME_CAPABILITIES * Capabilities OPTIONAL
50 )
51 /*++
52
53 Routine Description:
54 Service routine for RealTimeClockInstance->GetTime
55
56 Arguments:
57
58 Time - A pointer to storage that will receive a snapshot of the current time.
59
60 Capabilities - A pointer to storage that will receive the capabilities of the real time clock
61 in the platform. This includes the real time clock's resolution and accuracy.
62 All reported device capabilities are rounded up. This is an OPTIONAL argument.
63
64 Returns:
65
66 EFI_SUCEESS - The underlying GetSystemTime call occurred and returned
67 Note that in the NT32 emulation, the GetSystemTime call has no return value
68 thus you will always receive a EFI_SUCCESS on this.
69
70 --*/
71 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
72 {
73
74 //
75 // Check parameter for null pointer
76 //
77 if (Time == NULL) {
78 return EFI_INVALID_PARAMETER;
79
80 }
81
82 gUnix->GetLocalTime (Time);
83
84 if (Capabilities != NULL) {
85 Capabilities->Resolution = 1;
86 Capabilities->Accuracy = 50000000;
87 Capabilities->SetsToZero = FALSE;
88 }
89
90 return EFI_SUCCESS;
91 }
92
93 STATIC
94 EFI_STATUS
95 EFIAPI
96 UnixSetTime (
97 IN EFI_TIME *Time
98 )
99 /*++
100
101 Routine Description:
102 Service routine for RealTimeClockInstance->SetTime
103
104 Arguments:
105
106 Time - A pointer to storage containing the time and date information to
107 program into the real time clock.
108
109 Returns:
110
111 EFI_SUCEESS - The operation completed successfully.
112
113 EFI_INVALID_PARAMETER - One of the fields in Time is out of range.
114
115 EFI_DEVICE_ERROR - The operation could not be complete due to a device error.
116
117 --*/
118 // TODO: EFI_SUCCESS - add return value to function comment
119 {
120 EFI_STATUS Status;
121
122 if (Time == NULL) {
123 return EFI_INVALID_PARAMETER;
124 }
125 //
126 // Make sure that the time fields are valid
127 //
128 Status = RtcTimeFieldsValid (Time);
129 if (EFI_ERROR (Status)) {
130 return Status;
131 }
132 return EFI_UNSUPPORTED;
133 }
134
135 STATIC
136 EFI_STATUS
137 EFIAPI
138 UnixGetWakeupTime (
139 OUT BOOLEAN *Enabled,
140 OUT BOOLEAN *Pending,
141 OUT EFI_TIME *Time
142 )
143 /*++
144
145 Routine Description:
146 Service routine for RealTimeClockInstance->GetWakeupTime
147
148 Arguments:
149 This - Indicates the protocol instance structure.
150
151 Enabled - Indicates if the alarm is currently enabled or disabled.
152
153 Pending - Indicates if the alarm signal is pending and requires
154 acknowledgement.
155
156 Time - The current alarm setting.
157
158 Returns:
159
160 EFI_SUCEESS - The operation completed successfully.
161
162 EFI_DEVICE_ERROR - The operation could not be complete due to a device error.
163
164 EFI_UNSUPPORTED - The operation is not supported on this platform.
165
166 --*/
167 {
168 return EFI_UNSUPPORTED;
169 }
170
171 STATIC
172 EFI_STATUS
173 EFIAPI
174 UnixSetWakeupTime (
175 IN BOOLEAN Enable,
176 OUT EFI_TIME *Time
177 )
178 /*++
179
180 Routine Description:
181 Service routine for RealTimeClockInstance->SetWakeupTime
182
183 Arguments:
184
185 Enabled - Enable or disable the wakeup alarm.
186
187 Time - If enable is TRUE, the time to set the wakup alarm for.
188 If enable is FALSE, then this parameter is optional, and
189 may be NULL.
190
191 Returns:
192
193 EFI_SUCEESS - The operation completed successfully.
194
195 EFI_DEVICE_ERROR - The operation could not be complete due to a device error.
196
197 EFI_INVALID_PARAMETER - A field in Time is out of range.
198
199 EFI_UNSUPPORTED - The operation is not supported on this platform.
200
201 --*/
202 {
203 return EFI_UNSUPPORTED;
204 }
205
206 EFI_STATUS
207 EFIAPI
208 InitializeRealTimeClock (
209 IN EFI_HANDLE ImageHandle,
210 IN EFI_SYSTEM_TABLE *SystemTable
211 )
212 /*++
213
214 Routine Description:
215 Install Real Time Clock Protocol
216
217 Arguments:
218 ImageHandle - Image Handle
219 SystemTable - Pointer to system table
220
221 Returns:
222
223 EFI_SUCEESS - Real Time Clock Services are installed into the Runtime Services Table
224
225 --*/
226 {
227 EFI_STATUS Status;
228 EFI_HANDLE Handle;
229
230 SystemTable->RuntimeServices->GetTime = UnixGetTime;
231 SystemTable->RuntimeServices->SetTime = UnixSetTime;
232 SystemTable->RuntimeServices->GetWakeupTime = UnixGetWakeupTime;
233 SystemTable->RuntimeServices->SetWakeupTime = UnixSetWakeupTime;
234
235 Handle = NULL;
236 Status = gBS->InstallMultipleProtocolInterfaces (
237 &Handle,
238 &gEfiRealTimeClockArchProtocolGuid,
239 NULL,
240 NULL
241 );
242 return Status;
243 }
244
245 EFI_STATUS
246 RtcTimeFieldsValid (
247 IN EFI_TIME *Time
248 )
249 /*++
250
251 Routine Description:
252
253 Arguments:
254
255 Returns:
256 --*/
257 // TODO: Time - add argument and description to function comment
258 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
259 // TODO: EFI_SUCCESS - add return value to function comment
260 {
261 if (Time->Year < 1998 ||
262 Time->Year > 2099 ||
263 Time->Month < 1 ||
264 Time->Month > 12 ||
265 (!DayValid (Time)) ||
266 Time->Hour > 23 ||
267 Time->Minute > 59 ||
268 Time->Second > 59 ||
269 Time->Nanosecond > 999999999 ||
270 (!(Time->TimeZone == EFI_UNSPECIFIED_TIMEZONE || (Time->TimeZone >= -1440 && Time->TimeZone <= 1440))) ||
271 (Time->Daylight & (~(EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT)))
272 ) {
273 return EFI_INVALID_PARAMETER;
274 }
275
276 return EFI_SUCCESS;
277 }
278
279 BOOLEAN
280 DayValid (
281 IN EFI_TIME *Time
282 )
283 /*++
284
285 Routine Description:
286
287 TODO: Add function description
288
289 Arguments:
290
291 Time - TODO: add argument description
292
293 Returns:
294
295 TODO: add return values
296
297 --*/
298 {
299
300 static const INTN DayOfMonth[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
301
302 if (Time->Day < 1 ||
303 Time->Day > DayOfMonth[Time->Month - 1] ||
304 (Time->Month == 2 && (!IsLeapYear (Time) && Time->Day > 28))
305 ) {
306 return FALSE;
307 }
308
309 return TRUE;
310 }
311
312 BOOLEAN
313 IsLeapYear (
314 IN EFI_TIME *Time
315 )
316 /*++
317
318 Routine Description:
319
320 TODO: Add function description
321
322 Arguments:
323
324 Time - TODO: add argument description
325
326 Returns:
327
328 TODO: add return values
329
330 --*/
331 {
332 if (Time->Year % 4 == 0) {
333 if (Time->Year % 100 == 0) {
334 if (Time->Year % 400 == 0) {
335 return TRUE;
336 } else {
337 return FALSE;
338 }
339 } else {
340 return TRUE;
341 }
342 } else {
343 return FALSE;
344 }
345 }