3 * Implement virtual EFI RealTimeClock runtime services.
5 * Coypright (c) 2019, Pete Batard <pete@akeo.ie>
6 * Copyright (c) 2018, Andrei Warkentin <andrey.warkentin@gmail.com>
7 * Copyright (c) 2011-2021, ARM Ltd. All rights reserved.
8 * Copyright (c) 2008-2010, Apple Inc. All rights reserved.
9 * Copyright (c) Microsoft Corporation. All rights reserved.
11 * SPDX-License-Identifier: BSD-2-Clause-Patent
13 * Based on ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.inf
18 #include <Library/BaseLib.h>
19 #include <Library/DebugLib.h>
20 #include <Library/IoLib.h>
21 #include <Library/RealTimeClockLib.h>
22 #include <Library/TimerLib.h>
23 #include <Library/TimeBaseLib.h>
24 #include <Library/UefiRuntimeLib.h>
26 STATIC CONST CHAR16 mEpochVariableName
[] = L
"RtcEpochSeconds";
27 STATIC CONST CHAR16 mTimeZoneVariableName
[] = L
"RtcTimeZone";
28 STATIC CONST CHAR16 mDaylightVariableName
[] = L
"RtcDaylight";
31 Returns the current time and date information, and the time-keeping capabilities
34 @param Time A pointer to storage to receive a snapshot of the current time.
35 @param Capabilities An optional pointer to a buffer to receive the real time clock
36 device's capabilities.
38 @retval EFI_SUCCESS The operation completed successfully.
39 @retval EFI_INVALID_PARAMETER Time is NULL.
40 @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.
47 OUT EFI_TIME_CAPABILITIES
*Capabilities
60 return EFI_INVALID_PARAMETER
;
63 // Get the counter frequency
64 Freq
= GetPerformanceCounterProperties (NULL
, NULL
);
66 return EFI_DEVICE_ERROR
;
69 // Get the epoch time from non-volatile storage
70 Size
= sizeof (UINTN
);
72 Status
= EfiGetVariable (
73 (CHAR16
*)mEpochVariableName
,
79 // Fall back to compilation-time epoch if not set
80 if (EFI_ERROR (Status
)) {
81 ASSERT (Status
!= EFI_INVALID_PARAMETER
);
82 ASSERT (Status
!= EFI_BUFFER_TOO_SMALL
);
84 // The following is intended to produce a compilation error on build
85 // environments where BUILD_EPOCH can not be set from inline shell.
86 // If you are attempting to use this library on such an environment, please
87 // contact the edk2 mailing list, so we can try to add support for it.
89 EpochSeconds
= BUILD_EPOCH
;
92 "LibGetTime: %s non volatile variable was not found - Using compilation time epoch.\n",
97 (CHAR16
*)mEpochVariableName
,
99 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
100 sizeof (EpochSeconds
),
105 Counter
= GetPerformanceCounter ();
106 EpochSeconds
+= DivU64x64Remainder (Counter
, Freq
, &Remainder
);
108 // Get the current time zone information from non-volatile storage
109 Size
= sizeof (TimeZone
);
110 Status
= EfiGetVariable (
111 (CHAR16
*)mTimeZoneVariableName
,
118 if (EFI_ERROR (Status
)) {
119 ASSERT (Status
!= EFI_INVALID_PARAMETER
);
120 ASSERT (Status
!= EFI_BUFFER_TOO_SMALL
);
122 if (Status
!= EFI_NOT_FOUND
) {
126 // The time zone variable does not exist in non-volatile storage, so create it.
127 Time
->TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
129 Status
= EfiSetVariable (
130 (CHAR16
*)mTimeZoneVariableName
,
132 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
134 (VOID
*)&(Time
->TimeZone
)
136 if (EFI_ERROR (Status
)) {
139 "LibGetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
140 mTimeZoneVariableName
,
147 Time
->TimeZone
= TimeZone
;
149 // Check TimeZone bounds: -1440 to 1440 or 2047
150 if ( ((Time
->TimeZone
< -1440) || (Time
->TimeZone
> 1440))
151 && (Time
->TimeZone
!= EFI_UNSPECIFIED_TIMEZONE
))
153 Time
->TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
156 // Adjust for the correct time zone
157 if (Time
->TimeZone
!= EFI_UNSPECIFIED_TIMEZONE
) {
158 EpochSeconds
+= Time
->TimeZone
* SEC_PER_MIN
;
162 // Get the current daylight information from non-volatile storage
163 Size
= sizeof (Daylight
);
164 Status
= EfiGetVariable (
165 (CHAR16
*)mDaylightVariableName
,
172 if (EFI_ERROR (Status
)) {
173 ASSERT (Status
!= EFI_INVALID_PARAMETER
);
174 ASSERT (Status
!= EFI_BUFFER_TOO_SMALL
);
176 if (Status
!= EFI_NOT_FOUND
) {
180 // The daylight variable does not exist in non-volatile storage, so create it.
183 Status
= EfiSetVariable (
184 (CHAR16
*)mDaylightVariableName
,
186 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
188 (VOID
*)&(Time
->Daylight
)
190 if (EFI_ERROR (Status
)) {
193 "LibGetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
194 mDaylightVariableName
,
200 // Got the daylight information
201 Time
->Daylight
= Daylight
;
203 // Adjust for the correct period
204 if ((Time
->Daylight
& EFI_TIME_IN_DAYLIGHT
) == EFI_TIME_IN_DAYLIGHT
) {
205 // Convert to adjusted time, i.e. spring forwards one hour
206 EpochSeconds
+= SEC_PER_HOUR
;
210 EpochToEfiTime (EpochSeconds
, Time
);
212 // Because we use the performance counter, we can fill the Nanosecond attribute
213 // provided that the remainder doesn't overflow 64-bit during multiplication.
214 if (Remainder
<= 18446744073U) {
215 Time
->Nanosecond
= (UINT32
)(MultU64x64 (Remainder
, 1000000000U) / Freq
);
217 DEBUG ((DEBUG_WARN
, "LibGetTime: Nanosecond value not set (64-bit overflow).\n"));
221 Capabilities
->Accuracy
= 0;
222 Capabilities
->Resolution
= 1;
223 Capabilities
->SetsToZero
= FALSE
;
230 Sets the current local time and date information.
232 @param Time A pointer to the current time.
234 @retval EFI_SUCCESS The operation completed successfully.
235 @retval EFI_INVALID_PARAMETER A time field is out of range.
236 @retval EFI_DEVICE_ERROR The time could not be set due to hardware error.
251 if (!IsTimeValid (Time
)) {
252 return EFI_INVALID_PARAMETER
;
255 EpochSeconds
= EfiTimeToEpoch (Time
);
257 // Adjust for the correct time zone, i.e. convert to UTC time zone
258 if ( (Time
->TimeZone
!= EFI_UNSPECIFIED_TIMEZONE
)
259 && (EpochSeconds
> Time
->TimeZone
* SEC_PER_MIN
))
261 EpochSeconds
-= Time
->TimeZone
* SEC_PER_MIN
;
264 // Adjust for the correct period
265 if ( ((Time
->Daylight
& EFI_TIME_IN_DAYLIGHT
) == EFI_TIME_IN_DAYLIGHT
)
266 && (EpochSeconds
> SEC_PER_HOUR
))
268 // Convert to un-adjusted time, i.e. fall back one hour
269 EpochSeconds
-= SEC_PER_HOUR
;
272 // Use the performance counter to subtract the number of seconds
273 // since platform reset. Without this, setting time from the shell
274 // and immediately reading it back would result in a forward time
275 // offset, of the duration during which the platform has been up.
276 Freq
= GetPerformanceCounterProperties (NULL
, NULL
);
278 Counter
= GetPerformanceCounter ();
279 if (EpochSeconds
> DivU64x64Remainder (Counter
, Freq
, &Remainder
)) {
280 EpochSeconds
-= DivU64x64Remainder (Counter
, Freq
, &Remainder
);
284 // Save the current time zone information into non-volatile storage
285 Status
= EfiSetVariable (
286 (CHAR16
*)mTimeZoneVariableName
,
288 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
289 sizeof (Time
->TimeZone
),
290 (VOID
*)&(Time
->TimeZone
)
292 if (EFI_ERROR (Status
)) {
295 "LibSetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
296 mTimeZoneVariableName
,
302 // Save the current daylight information into non-volatile storage
303 Status
= EfiSetVariable (
304 (CHAR16
*)mDaylightVariableName
,
306 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
307 sizeof (Time
->Daylight
),
308 (VOID
*)&(Time
->Daylight
)
310 if (EFI_ERROR (Status
)) {
313 "LibSetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
314 mDaylightVariableName
,
320 Status
= EfiSetVariable (
321 (CHAR16
*)mEpochVariableName
,
323 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
324 sizeof (EpochSeconds
),
327 if (EFI_ERROR (Status
)) {
330 "LibSetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
341 Returns the current wakeup alarm clock setting.
343 @param Enabled Indicates if the alarm is currently enabled or disabled.
344 @param Pending Indicates if the alarm signal is pending and requires acknowledgement.
345 @param Time The current alarm setting.
347 @retval EFI_SUCCESS The alarm settings were returned.
348 @retval EFI_INVALID_PARAMETER Any parameter is NULL.
349 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
355 OUT BOOLEAN
*Enabled
,
356 OUT BOOLEAN
*Pending
,
360 return EFI_UNSUPPORTED
;
364 Sets the system wakeup alarm clock time.
366 @param Enabled Enable or disable the wakeup alarm.
367 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
369 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If
370 Enable is FALSE, then the wakeup alarm was disabled.
371 @retval EFI_INVALID_PARAMETER A time field is out of range.
372 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
373 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
383 return EFI_UNSUPPORTED
;
387 This is the declaration of an EFI image entry point. This can be the entry point to an application
388 written to this specification, an EFI boot service driver, or an EFI runtime driver.
390 @param ImageHandle Handle that identifies the loaded image.
391 @param SystemTable System Table for this image.
393 @retval EFI_SUCCESS The operation completed successfully.
399 IN EFI_HANDLE ImageHandle
,
400 IN EFI_SYSTEM_TABLE
*SystemTable
407 Fixup internal data so that EFI can be call in virtual mode.
408 Call the passed in Child Notify event and convert any pointers in
411 @param[in] Event The Event that is being processed
412 @param[in] Context Event Context
416 LibRtcVirtualNotifyEvent (