]> git.proxmox.com Git - mirror_edk2.git/blob - EdkNt32Pkg/Dxe/WinNtThunk/Chipset/RealTimeClock/RealTimeClock.c
Fix VS 2005 compatibility issue
[mirror_edk2.git] / EdkNt32Pkg / Dxe / WinNtThunk / Chipset / RealTimeClock / RealTimeClock.c
1 /*++
2
3 Copyright (c) 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
23
24 BOOLEAN
25 DayValid (
26 IN EFI_TIME *Time
27 );
28
29 BOOLEAN
30 IsLeapYear (
31 IN EFI_TIME *Time
32 );
33
34 EFI_STATUS
35 RtcTimeFieldsValid (
36 IN EFI_TIME *Time
37 );
38
39 EFI_STATUS
40 EFIAPI
41 InitializeRealTimeClock (
42 IN EFI_HANDLE ImageHandle,
43 IN EFI_SYSTEM_TABLE *SystemTable
44 );
45
46 STATIC
47 EFI_STATUS
48 EFIAPI
49 WinNtGetTime (
50 OUT EFI_TIME *Time,
51 OUT EFI_TIME_CAPABILITIES *Capabilities OPTIONAL
52 )
53 /*++
54
55 Routine Description:
56 Service routine for RealTimeClockInstance->GetTime
57
58 Arguments:
59
60 Time - A pointer to storage that will receive a snapshot of the current time.
61
62 Capabilities - A pointer to storage that will receive the capabilities of the real time clock
63 in the platform. This includes the real time clock's resolution and accuracy.
64 All reported device capabilities are rounded up. This is an OPTIONAL argument.
65
66 Returns:
67
68 EFI_SUCEESS - The underlying GetSystemTime call occurred and returned
69 Note that in the NT32 emulation, the GetSystemTime call has no return value
70 thus you will always receive a EFI_SUCCESS on this.
71
72 --*/
73 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
74 {
75 SYSTEMTIME SystemTime;
76 TIME_ZONE_INFORMATION TimeZone;
77
78 //
79 // Check parameter for null pointer
80 //
81 if (Time == NULL) {
82 return EFI_INVALID_PARAMETER;
83
84 }
85
86 gWinNt->GetLocalTime (&SystemTime);
87 gWinNt->GetTimeZoneInformation (&TimeZone);
88
89 Time->Year = (UINT16) SystemTime.wYear;
90 Time->Month = (UINT8) SystemTime.wMonth;
91 Time->Day = (UINT8) SystemTime.wDay;
92 Time->Hour = (UINT8) SystemTime.wHour;
93 Time->Minute = (UINT8) SystemTime.wMinute;
94 Time->Second = (UINT8) SystemTime.wSecond;
95 Time->Nanosecond = (UINT32) (SystemTime.wMilliseconds * 1000000);
96 Time->TimeZone = (INT16) TimeZone.Bias;
97
98 if (Capabilities != NULL) {
99 Capabilities->Resolution = 1;
100 Capabilities->Accuracy = 50000000;
101 Capabilities->SetsToZero = FALSE;
102 }
103
104 Time->Daylight = 0;
105 if (TimeZone.StandardDate.wMonth) {
106 Time->Daylight = EFI_TIME_ADJUST_DAYLIGHT;
107 }
108
109 return EFI_SUCCESS;
110 }
111
112 STATIC
113 EFI_STATUS
114 EFIAPI
115 WinNtSetTime (
116 IN EFI_TIME *Time
117 )
118 /*++
119
120 Routine Description:
121 Service routine for RealTimeClockInstance->SetTime
122
123 Arguments:
124
125 Time - A pointer to storage containing the time and date information to
126 program into the real time clock.
127
128 Returns:
129
130 EFI_SUCEESS - The operation completed successfully.
131
132 EFI_INVALID_PARAMETER - One of the fields in Time is out of range.
133
134 EFI_DEVICE_ERROR - The operation could not be complete due to a device error.
135
136 --*/
137 // TODO: EFI_SUCCESS - add return value to function comment
138 {
139 TIME_ZONE_INFORMATION TimeZone;
140 EFI_STATUS Status;
141 SYSTEMTIME SystemTime;
142 BOOL Flag;
143
144 if (Time == NULL) {
145 return EFI_INVALID_PARAMETER;
146 }
147 //
148 // Make sure that the time fields are valid
149 //
150 Status = RtcTimeFieldsValid (Time);
151 if (EFI_ERROR (Status)) {
152 return Status;
153 }
154 //
155 // Set Daylight savings time information and Time Zone
156 //
157 gWinNt->GetTimeZoneInformation (&TimeZone);
158 TimeZone.StandardDate.wMonth = Time->Daylight;
159 TimeZone.Bias = Time->TimeZone;
160 gWinNt->SetTimeZoneInformation (&TimeZone);
161
162 SystemTime.wYear = Time->Year;
163 SystemTime.wMonth = Time->Month;
164 SystemTime.wDay = Time->Day;
165 SystemTime.wHour = Time->Hour;
166 SystemTime.wMinute = Time->Minute;
167 SystemTime.wSecond = Time->Second;
168 SystemTime.wMilliseconds = (INT16) (Time->Nanosecond / 1000000);
169
170 Flag = gWinNt->SetLocalTime (&SystemTime);
171
172 if (!Flag) {
173 return EFI_DEVICE_ERROR;
174 } else {
175 return EFI_SUCCESS;
176 }
177 }
178
179 STATIC
180 EFI_STATUS
181 EFIAPI
182 WinNtGetWakeupTime (
183 OUT BOOLEAN *Enabled,
184 OUT BOOLEAN *Pending,
185 OUT EFI_TIME *Time
186 )
187 /*++
188
189 Routine Description:
190 Service routine for RealTimeClockInstance->GetWakeupTime
191
192 Arguments:
193 This - Indicates the protocol instance structure.
194
195 Enabled - Indicates if the alarm is currently enabled or disabled.
196
197 Pending - Indicates if the alarm signal is pending and requires
198 acknowledgement.
199
200 Time - The current alarm setting.
201
202 Returns:
203
204 EFI_SUCEESS - The operation completed successfully.
205
206 EFI_DEVICE_ERROR - The operation could not be complete due to a device error.
207
208 EFI_UNSUPPORTED - The operation is not supported on this platform.
209
210 --*/
211 {
212 return EFI_UNSUPPORTED;
213 }
214
215 STATIC
216 EFI_STATUS
217 EFIAPI
218 WinNtSetWakeupTime (
219 IN BOOLEAN Enable,
220 OUT EFI_TIME *Time
221 )
222 /*++
223
224 Routine Description:
225 Service routine for RealTimeClockInstance->SetWakeupTime
226
227 Arguments:
228
229 Enabled - Enable or disable the wakeup alarm.
230
231 Time - If enable is TRUE, the time to set the wakup alarm for.
232 If enable is FALSE, then this parameter is optional, and
233 may be NULL.
234
235 Returns:
236
237 EFI_SUCEESS - The operation completed successfully.
238
239 EFI_DEVICE_ERROR - The operation could not be complete due to a device error.
240
241 EFI_INVALID_PARAMETER - A field in Time is out of range.
242
243 EFI_UNSUPPORTED - The operation is not supported on this platform.
244
245 --*/
246 {
247 return EFI_UNSUPPORTED;
248 }
249
250 EFI_STATUS
251 EFIAPI
252 InitializeRealTimeClock (
253 IN EFI_HANDLE ImageHandle,
254 IN EFI_SYSTEM_TABLE *SystemTable
255 )
256 /*++
257
258 Routine Description:
259 Install Real Time Clock Protocol
260
261 Arguments:
262 (Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT)
263
264 Returns:
265
266 EFI_SUCEESS - Real Time Clock Services are installed into the Runtime Services Table
267
268 --*/
269 // TODO: ImageHandle - add argument and description to function comment
270 // TODO: SystemTable - add argument and description to function comment
271 {
272 EFI_STATUS Status;
273 EFI_HANDLE Handle;
274
275
276 SystemTable->RuntimeServices->GetTime = WinNtGetTime;
277 SystemTable->RuntimeServices->SetTime = WinNtSetTime;
278 SystemTable->RuntimeServices->GetWakeupTime = WinNtGetWakeupTime;
279 SystemTable->RuntimeServices->SetWakeupTime = WinNtSetWakeupTime;
280
281 Handle = NULL;
282 Status = gBS->InstallMultipleProtocolInterfaces (
283 &Handle,
284 &gEfiRealTimeClockArchProtocolGuid,
285 NULL,
286 NULL
287 );
288 return Status;
289 }
290
291 EFI_STATUS
292 RtcTimeFieldsValid (
293 IN EFI_TIME *Time
294 )
295 /*++
296
297 Routine Description:
298
299 Arguments:
300
301 Returns:
302 --*/
303 // TODO: Time - add argument and description to function comment
304 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
305 // TODO: EFI_SUCCESS - add return value to function comment
306 {
307 if (Time->Year < 1998 ||
308 Time->Year > 2099 ||
309 Time->Month < 1 ||
310 Time->Month > 12 ||
311 (!DayValid (Time)) ||
312 Time->Hour > 23 ||
313 Time->Minute > 59 ||
314 Time->Second > 59 ||
315 Time->Nanosecond > 999999999 ||
316 (!(Time->TimeZone == EFI_UNSPECIFIED_TIMEZONE || (Time->TimeZone >= -1440 && Time->TimeZone <= 1440))) ||
317 (Time->Daylight & (~(EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT)))
318 ) {
319 return EFI_INVALID_PARAMETER;
320 }
321
322 return EFI_SUCCESS;
323 }
324
325 BOOLEAN
326 DayValid (
327 IN EFI_TIME *Time
328 )
329 /*++
330
331 Routine Description:
332
333 TODO: Add function description
334
335 Arguments:
336
337 Time - TODO: add argument description
338
339 Returns:
340
341 TODO: add return values
342
343 --*/
344 {
345
346 INTN DayOfMonth[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
347
348 if (Time->Day < 1 ||
349 Time->Day > DayOfMonth[Time->Month - 1] ||
350 (Time->Month == 2 && (!IsLeapYear (Time) && Time->Day > 28))
351 ) {
352 return FALSE;
353 }
354
355 return TRUE;
356 }
357
358 BOOLEAN
359 IsLeapYear (
360 IN EFI_TIME *Time
361 )
362 /*++
363
364 Routine Description:
365
366 TODO: Add function description
367
368 Arguments:
369
370 Time - TODO: add argument description
371
372 Returns:
373
374 TODO: add return values
375
376 --*/
377 {
378 if (Time->Year % 4 == 0) {
379 if (Time->Year % 100 == 0) {
380 if (Time->Year % 400 == 0) {
381 return TRUE;
382 } else {
383 return FALSE;
384 }
385 } else {
386 return TRUE;
387 }
388 } else {
389 return FALSE;
390 }
391 }