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