]>
Commit | Line | Data |
---|---|---|
64a17fad PB |
1 | /** @file\r |
2 | *\r | |
3 | * Implement virtual EFI RealTimeClock runtime services.\r | |
4 | *\r | |
5 | * Coypright (c) 2019, Pete Batard <pete@akeo.ie>\r | |
6 | * Copyright (c) 2018, Andrei Warkentin <andrey.warkentin@gmail.com>\r | |
7 | * Copyright (c) 2011-2014, ARM Ltd. All rights reserved.\r | |
8 | * Copyright (c) 2008-2010, Apple Inc. All rights reserved.\r | |
9 | * Copyright (c) Microsoft Corporation. All rights reserved.\r | |
10 | *\r | |
11 | * This program and the accompanying materials\r | |
12 | * are licensed and made available under the terms and conditions of the BSD License\r | |
13 | * which accompanies this distribution. The full text of the license may be found at\r | |
14 | * http://opensource.org/licenses/bsd-license.php\r | |
15 | *\r | |
16 | * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
17 | * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
18 | *\r | |
19 | * Based on ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.inf\r | |
20 | *\r | |
21 | **/\r | |
22 | \r | |
23 | #include <PiDxe.h>\r | |
24 | #include <Library/BaseLib.h>\r | |
25 | #include <Library/DebugLib.h>\r | |
26 | #include <Library/IoLib.h>\r | |
27 | #include <Library/RealTimeClockLib.h>\r | |
28 | #include <Library/TimerLib.h>\r | |
29 | #include <Library/TimeBaseLib.h>\r | |
30 | #include <Library/UefiRuntimeLib.h>\r | |
31 | \r | |
32 | STATIC CONST CHAR16 mEpochVariableName[] = L"RtcEpochSeconds";\r | |
33 | STATIC CONST CHAR16 mTimeZoneVariableName[] = L"RtcTimeZone";\r | |
34 | STATIC CONST CHAR16 mDaylightVariableName[] = L"RtcDaylight";\r | |
35 | \r | |
36 | /**\r | |
37 | Returns the current time and date information, and the time-keeping capabilities\r | |
38 | of the virtual RTC.\r | |
39 | \r | |
40 | @param Time A pointer to storage to receive a snapshot of the current time.\r | |
41 | @param Capabilities An optional pointer to a buffer to receive the real time clock\r | |
42 | device's capabilities.\r | |
43 | \r | |
44 | @retval EFI_SUCCESS The operation completed successfully.\r | |
45 | @retval EFI_INVALID_PARAMETER Time is NULL.\r | |
46 | @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.\r | |
47 | \r | |
48 | **/\r | |
49 | EFI_STATUS\r | |
50 | EFIAPI\r | |
51 | LibGetTime (\r | |
52 | OUT EFI_TIME *Time,\r | |
53 | OUT EFI_TIME_CAPABILITIES *Capabilities\r | |
54 | )\r | |
55 | {\r | |
56 | EFI_STATUS Status;\r | |
64a17fad PB |
57 | INT16 TimeZone;\r |
58 | UINT8 Daylight;\r | |
59 | UINT64 Freq;\r | |
60 | UINT64 Counter;\r | |
61 | UINT64 Remainder;\r | |
1342d767 | 62 | UINTN EpochSeconds;\r |
64a17fad PB |
63 | UINTN Size;\r |
64 | \r | |
65 | if (Time == NULL) {\r | |
66 | return EFI_INVALID_PARAMETER;\r | |
67 | }\r | |
68 | \r | |
69 | // Get the counter frequency\r | |
70 | Freq = GetPerformanceCounterProperties (NULL, NULL);\r | |
71 | if (Freq == 0) {\r | |
72 | return EFI_DEVICE_ERROR;\r | |
73 | }\r | |
74 | \r | |
75 | // Get the epoch time from non-volatile storage\r | |
76 | Size = sizeof (UINTN);\r | |
1342d767 | 77 | EpochSeconds = 0;\r |
64a17fad PB |
78 | Status = EfiGetVariable (\r |
79 | (CHAR16 *)mEpochVariableName,\r | |
80 | &gEfiCallerIdGuid,\r | |
81 | NULL,\r | |
82 | &Size,\r | |
1342d767 | 83 | (VOID *)&EpochSeconds\r |
64a17fad PB |
84 | );\r |
85 | // Fall back to compilation-time epoch if not set\r | |
86 | if (EFI_ERROR (Status)) {\r | |
87 | ASSERT(Status != EFI_INVALID_PARAMETER);\r | |
88 | ASSERT(Status != EFI_BUFFER_TOO_SMALL);\r | |
89 | //\r | |
90 | // The following is intended to produce a compilation error on build\r | |
91 | // environments where BUILD_EPOCH can not be set from inline shell.\r | |
92 | // If you are attempting to use this library on such an environment, please\r | |
93 | // contact the edk2 mailing list, so we can try to add support for it.\r | |
94 | //\r | |
1342d767 | 95 | EpochSeconds = BUILD_EPOCH;\r |
64a17fad PB |
96 | DEBUG ((\r |
97 | DEBUG_INFO,\r | |
98 | "LibGetTime: %s non volatile variable was not found - Using compilation time epoch.\n",\r | |
99 | mEpochVariableName\r | |
100 | ));\r | |
101 | }\r | |
102 | Counter = GetPerformanceCounter ();\r | |
1342d767 | 103 | EpochSeconds += DivU64x64Remainder (Counter, Freq, &Remainder);\r |
64a17fad PB |
104 | \r |
105 | // Get the current time zone information from non-volatile storage\r | |
106 | Size = sizeof (TimeZone);\r | |
107 | Status = EfiGetVariable (\r | |
108 | (CHAR16 *)mTimeZoneVariableName,\r | |
109 | &gEfiCallerIdGuid,\r | |
110 | NULL,\r | |
111 | &Size,\r | |
112 | (VOID *)&TimeZone\r | |
113 | );\r | |
114 | \r | |
115 | if (EFI_ERROR (Status)) {\r | |
116 | ASSERT(Status != EFI_INVALID_PARAMETER);\r | |
117 | ASSERT(Status != EFI_BUFFER_TOO_SMALL);\r | |
118 | \r | |
119 | if (Status != EFI_NOT_FOUND) {\r | |
120 | return Status;\r | |
121 | }\r | |
122 | \r | |
123 | // The time zone variable does not exist in non-volatile storage, so create it.\r | |
124 | Time->TimeZone = EFI_UNSPECIFIED_TIMEZONE;\r | |
125 | // Store it\r | |
126 | Status = EfiSetVariable (\r | |
127 | (CHAR16 *)mTimeZoneVariableName,\r | |
128 | &gEfiCallerIdGuid,\r | |
129 | EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r | |
130 | Size,\r | |
131 | (VOID *)&(Time->TimeZone)\r | |
132 | );\r | |
133 | if (EFI_ERROR (Status)) {\r | |
134 | DEBUG ((\r | |
135 | DEBUG_ERROR,\r | |
136 | "LibGetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",\r | |
137 | mTimeZoneVariableName,\r | |
138 | Status\r | |
139 | ));\r | |
140 | return Status;\r | |
141 | }\r | |
142 | } else {\r | |
143 | // Got the time zone\r | |
144 | Time->TimeZone = TimeZone;\r | |
145 | \r | |
146 | // Check TimeZone bounds: -1440 to 1440 or 2047\r | |
147 | if (((Time->TimeZone < -1440) || (Time->TimeZone > 1440))\r | |
148 | && (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE)) {\r | |
149 | Time->TimeZone = EFI_UNSPECIFIED_TIMEZONE;\r | |
150 | }\r | |
151 | \r | |
152 | // Adjust for the correct time zone\r | |
153 | if (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE) {\r | |
154 | EpochSeconds += Time->TimeZone * SEC_PER_MIN;\r | |
155 | }\r | |
156 | }\r | |
157 | \r | |
158 | // Get the current daylight information from non-volatile storage\r | |
159 | Size = sizeof (Daylight);\r | |
160 | Status = EfiGetVariable (\r | |
161 | (CHAR16 *)mDaylightVariableName,\r | |
162 | &gEfiCallerIdGuid,\r | |
163 | NULL,\r | |
164 | &Size,\r | |
165 | (VOID *)&Daylight\r | |
166 | );\r | |
167 | \r | |
168 | if (EFI_ERROR (Status)) {\r | |
169 | ASSERT(Status != EFI_INVALID_PARAMETER);\r | |
170 | ASSERT(Status != EFI_BUFFER_TOO_SMALL);\r | |
171 | \r | |
172 | if (Status != EFI_NOT_FOUND) {\r | |
173 | return Status;\r | |
174 | }\r | |
175 | \r | |
176 | // The daylight variable does not exist in non-volatile storage, so create it.\r | |
177 | Time->Daylight = 0;\r | |
178 | // Store it\r | |
179 | Status = EfiSetVariable (\r | |
180 | (CHAR16 *)mDaylightVariableName,\r | |
181 | &gEfiCallerIdGuid,\r | |
182 | EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r | |
183 | Size,\r | |
184 | (VOID *)&(Time->Daylight)\r | |
185 | );\r | |
186 | if (EFI_ERROR (Status)) {\r | |
187 | DEBUG ((\r | |
188 | DEBUG_ERROR,\r | |
189 | "LibGetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",\r | |
190 | mDaylightVariableName,\r | |
191 | Status\r | |
192 | ));\r | |
193 | return Status;\r | |
194 | }\r | |
195 | } else {\r | |
196 | // Got the daylight information\r | |
197 | Time->Daylight = Daylight;\r | |
198 | \r | |
199 | // Adjust for the correct period\r | |
200 | if ((Time->Daylight & EFI_TIME_IN_DAYLIGHT) == EFI_TIME_IN_DAYLIGHT) {\r | |
201 | // Convert to adjusted time, i.e. spring forwards one hour\r | |
202 | EpochSeconds += SEC_PER_HOUR;\r | |
203 | }\r | |
204 | }\r | |
205 | \r | |
1342d767 | 206 | EpochToEfiTime (EpochSeconds, Time);\r |
64a17fad PB |
207 | \r |
208 | // Because we use the performance counter, we can fill the Nanosecond attribute\r | |
209 | // provided that the remainder doesn't overflow 64-bit during multiplication.\r | |
210 | if (Remainder <= 18446744073U) {\r | |
211 | Time->Nanosecond = MultU64x64 (Remainder, 1000000000U) / Freq;\r | |
212 | } else {\r | |
213 | DEBUG ((DEBUG_WARN, "LibGetTime: Nanosecond value not set (64-bit overflow).\n"));\r | |
214 | }\r | |
215 | \r | |
216 | if (Capabilities) {\r | |
217 | Capabilities->Accuracy = 0;\r | |
218 | Capabilities->Resolution = Freq;\r | |
219 | Capabilities->SetsToZero = FALSE;\r | |
220 | }\r | |
221 | \r | |
222 | return EFI_SUCCESS;\r | |
223 | }\r | |
224 | \r | |
225 | /**\r | |
226 | Sets the current local time and date information.\r | |
227 | \r | |
228 | @param Time A pointer to the current time.\r | |
229 | \r | |
230 | @retval EFI_SUCCESS The operation completed successfully.\r | |
231 | @retval EFI_INVALID_PARAMETER A time field is out of range.\r | |
232 | @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.\r | |
233 | \r | |
234 | **/\r | |
235 | EFI_STATUS\r | |
236 | EFIAPI\r | |
237 | LibSetTime (\r | |
238 | IN EFI_TIME *Time\r | |
239 | )\r | |
240 | {\r | |
241 | EFI_STATUS Status;\r | |
1342d767 PB |
242 | UINT64 Freq;\r |
243 | UINT64 Counter;\r | |
244 | UINT64 Remainder;\r | |
64a17fad PB |
245 | UINTN EpochSeconds;\r |
246 | \r | |
247 | if (!IsTimeValid (Time)) {\r | |
248 | return EFI_INVALID_PARAMETER;\r | |
249 | }\r | |
250 | \r | |
251 | EpochSeconds = EfiTimeToEpoch (Time);\r | |
252 | \r | |
253 | // Adjust for the correct time zone, i.e. convert to UTC time zone\r | |
1342d767 PB |
254 | if ((Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE)\r |
255 | && (EpochSeconds > Time->TimeZone * SEC_PER_MIN)) {\r | |
64a17fad PB |
256 | EpochSeconds -= Time->TimeZone * SEC_PER_MIN;\r |
257 | }\r | |
258 | \r | |
259 | // Adjust for the correct period\r | |
1342d767 PB |
260 | if (((Time->Daylight & EFI_TIME_IN_DAYLIGHT) == EFI_TIME_IN_DAYLIGHT)\r |
261 | && (EpochSeconds > SEC_PER_HOUR)) {\r | |
64a17fad PB |
262 | // Convert to un-adjusted time, i.e. fall back one hour\r |
263 | EpochSeconds -= SEC_PER_HOUR;\r | |
264 | }\r | |
265 | \r | |
1342d767 PB |
266 | // Use the performance counter to subtract the number of seconds\r |
267 | // since platform reset. Without this, setting time from the shell\r | |
268 | // and immediately reading it back would result in a forward time\r | |
269 | // offset, of the duration during which the platform has been up.\r | |
270 | Freq = GetPerformanceCounterProperties (NULL, NULL);\r | |
271 | if (Freq != 0) {\r | |
272 | Counter = GetPerformanceCounter ();\r | |
273 | if (EpochSeconds > DivU64x64Remainder (Counter, Freq, &Remainder)) {\r | |
274 | EpochSeconds -= DivU64x64Remainder (Counter, Freq, &Remainder);\r | |
275 | }\r | |
276 | }\r | |
277 | \r | |
64a17fad PB |
278 | // Save the current time zone information into non-volatile storage\r |
279 | Status = EfiSetVariable (\r | |
280 | (CHAR16 *)mTimeZoneVariableName,\r | |
281 | &gEfiCallerIdGuid,\r | |
282 | EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r | |
283 | sizeof (Time->TimeZone),\r | |
284 | (VOID *)&(Time->TimeZone)\r | |
285 | );\r | |
286 | if (EFI_ERROR (Status)) {\r | |
287 | DEBUG ((\r | |
288 | DEBUG_ERROR,\r | |
289 | "LibSetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",\r | |
290 | mTimeZoneVariableName,\r | |
291 | Status\r | |
292 | ));\r | |
293 | return Status;\r | |
294 | }\r | |
295 | \r | |
296 | // Save the current daylight information into non-volatile storage\r | |
297 | Status = EfiSetVariable (\r | |
298 | (CHAR16 *)mDaylightVariableName,\r | |
299 | &gEfiCallerIdGuid,\r | |
300 | EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r | |
301 | sizeof(Time->Daylight),\r | |
302 | (VOID *)&(Time->Daylight)\r | |
303 | );\r | |
304 | if (EFI_ERROR (Status)) {\r | |
305 | DEBUG ((\r | |
306 | DEBUG_ERROR,\r | |
307 | "LibSetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",\r | |
308 | mDaylightVariableName,\r | |
309 | Status\r | |
310 | ));\r | |
311 | return Status;\r | |
312 | }\r | |
313 | \r | |
314 | Status = EfiSetVariable (\r | |
315 | (CHAR16 *)mEpochVariableName,\r | |
316 | &gEfiCallerIdGuid,\r | |
317 | EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r | |
318 | sizeof (EpochSeconds),\r | |
319 | &EpochSeconds\r | |
320 | );\r | |
321 | if (EFI_ERROR (Status)) {\r | |
322 | DEBUG ((\r | |
323 | DEBUG_ERROR,\r | |
324 | "LibSetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",\r | |
325 | mDaylightVariableName,\r | |
326 | Status\r | |
327 | ));\r | |
328 | return Status;\r | |
329 | }\r | |
330 | \r | |
331 | return EFI_SUCCESS;\r | |
332 | }\r | |
333 | \r | |
334 | /**\r | |
335 | Returns the current wakeup alarm clock setting.\r | |
336 | \r | |
337 | @param Enabled Indicates if the alarm is currently enabled or disabled.\r | |
338 | @param Pending Indicates if the alarm signal is pending and requires acknowledgement.\r | |
339 | @param Time The current alarm setting.\r | |
340 | \r | |
341 | @retval EFI_SUCCESS The alarm settings were returned.\r | |
342 | @retval EFI_INVALID_PARAMETER Any parameter is NULL.\r | |
343 | @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.\r | |
344 | \r | |
345 | **/\r | |
346 | EFI_STATUS\r | |
347 | EFIAPI\r | |
348 | LibGetWakeupTime (\r | |
349 | OUT BOOLEAN *Enabled,\r | |
350 | OUT BOOLEAN *Pending,\r | |
351 | OUT EFI_TIME *Time\r | |
352 | )\r | |
353 | {\r | |
354 | return EFI_UNSUPPORTED;\r | |
355 | }\r | |
356 | \r | |
357 | /**\r | |
358 | Sets the system wakeup alarm clock time.\r | |
359 | \r | |
360 | @param Enabled Enable or disable the wakeup alarm.\r | |
361 | @param Time If Enable is TRUE, the time to set the wakeup alarm for.\r | |
362 | \r | |
363 | @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If\r | |
364 | Enable is FALSE, then the wakeup alarm was disabled.\r | |
365 | @retval EFI_INVALID_PARAMETER A time field is out of range.\r | |
366 | @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.\r | |
367 | @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.\r | |
368 | \r | |
369 | **/\r | |
370 | EFI_STATUS\r | |
371 | EFIAPI\r | |
372 | LibSetWakeupTime (\r | |
373 | IN BOOLEAN Enabled,\r | |
374 | OUT EFI_TIME *Time\r | |
375 | )\r | |
376 | {\r | |
377 | return EFI_UNSUPPORTED;\r | |
378 | }\r | |
379 | \r | |
380 | /**\r | |
381 | This is the declaration of an EFI image entry point. This can be the entry point to an application\r | |
382 | written to this specification, an EFI boot service driver, or an EFI runtime driver.\r | |
383 | \r | |
384 | @param ImageHandle Handle that identifies the loaded image.\r | |
385 | @param SystemTable System Table for this image.\r | |
386 | \r | |
387 | @retval EFI_SUCCESS The operation completed successfully.\r | |
388 | \r | |
389 | **/\r | |
390 | EFI_STATUS\r | |
391 | EFIAPI\r | |
392 | LibRtcInitialize (\r | |
393 | IN EFI_HANDLE ImageHandle,\r | |
394 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
395 | )\r | |
396 | {\r | |
397 | return EFI_SUCCESS;\r | |
398 | }\r | |
399 | \r | |
400 | /**\r | |
401 | Fixup internal data so that EFI can be call in virtual mode.\r | |
402 | Call the passed in Child Notify event and convert any pointers in\r | |
403 | lib to virtual mode.\r | |
404 | \r | |
405 | @param[in] Event The Event that is being processed\r | |
406 | @param[in] Context Event Context\r | |
407 | **/\r | |
408 | VOID\r | |
409 | EFIAPI\r | |
410 | LibRtcVirtualNotifyEvent (\r | |
411 | IN EFI_EVENT Event,\r | |
412 | IN VOID *Context\r | |
413 | )\r | |
414 | {\r | |
415 | return;\r | |
416 | }\r |