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