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