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