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-2014, ARM Ltd. All rights reserved.
8 * Copyright (c) 2008-2010, Apple Inc. All rights reserved.
9 * Copyright (c) Microsoft Corporation. All rights reserved.
11 * This program and the accompanying materials
12 * are licensed and made available under the terms and conditions of the BSD License
13 * which accompanies this distribution. The full text of the license may be found at
14 * http://opensource.org/licenses/bsd-license.php
16 * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 * Based on ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.inf
24 #include <Library/BaseLib.h>
25 #include <Library/DebugLib.h>
26 #include <Library/IoLib.h>
27 #include <Library/RealTimeClockLib.h>
28 #include <Library/TimerLib.h>
29 #include <Library/TimeBaseLib.h>
30 #include <Library/UefiRuntimeLib.h>
32 STATIC CONST CHAR16 mEpochVariableName
[] = L
"RtcEpochSeconds";
33 STATIC CONST CHAR16 mTimeZoneVariableName
[] = L
"RtcTimeZone";
34 STATIC CONST CHAR16 mDaylightVariableName
[] = L
"RtcDaylight";
37 Returns the current time and date information, and the time-keeping capabilities
40 @param Time A pointer to storage to receive a snapshot of the current time.
41 @param Capabilities An optional pointer to a buffer to receive the real time clock
42 device's capabilities.
44 @retval EFI_SUCCESS The operation completed successfully.
45 @retval EFI_INVALID_PARAMETER Time is NULL.
46 @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.
53 OUT EFI_TIME_CAPABILITIES
*Capabilities
67 return EFI_INVALID_PARAMETER
;
70 // Get the counter frequency
71 Freq
= GetPerformanceCounterProperties (NULL
, NULL
);
73 return EFI_DEVICE_ERROR
;
76 // Get the epoch time from non-volatile storage
77 Size
= sizeof (UINTN
);
79 Status
= EfiGetVariable (
80 (CHAR16
*)mEpochVariableName
,
84 (VOID
*)&ElapsedSeconds
86 // Fall back to compilation-time epoch if not set
87 if (EFI_ERROR (Status
)) {
88 ASSERT(Status
!= EFI_INVALID_PARAMETER
);
89 ASSERT(Status
!= EFI_BUFFER_TOO_SMALL
);
91 // The following is intended to produce a compilation error on build
92 // environments where BUILD_EPOCH can not be set from inline shell.
93 // If you are attempting to use this library on such an environment, please
94 // contact the edk2 mailing list, so we can try to add support for it.
96 ElapsedSeconds
= BUILD_EPOCH
;
99 "LibGetTime: %s non volatile variable was not found - Using compilation time epoch.\n",
103 Counter
= GetPerformanceCounter ();
104 ElapsedSeconds
+= DivU64x64Remainder (Counter
, Freq
, &Remainder
);
106 // Get the current time zone information from non-volatile storage
107 Size
= sizeof (TimeZone
);
108 Status
= EfiGetVariable (
109 (CHAR16
*)mTimeZoneVariableName
,
116 if (EFI_ERROR (Status
)) {
117 ASSERT(Status
!= EFI_INVALID_PARAMETER
);
118 ASSERT(Status
!= EFI_BUFFER_TOO_SMALL
);
120 if (Status
!= EFI_NOT_FOUND
) {
124 // The time zone variable does not exist in non-volatile storage, so create it.
125 Time
->TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
127 Status
= EfiSetVariable (
128 (CHAR16
*)mTimeZoneVariableName
,
130 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
132 (VOID
*)&(Time
->TimeZone
)
134 if (EFI_ERROR (Status
)) {
137 "LibGetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
138 mTimeZoneVariableName
,
145 Time
->TimeZone
= TimeZone
;
147 // Check TimeZone bounds: -1440 to 1440 or 2047
148 if (((Time
->TimeZone
< -1440) || (Time
->TimeZone
> 1440))
149 && (Time
->TimeZone
!= EFI_UNSPECIFIED_TIMEZONE
)) {
150 Time
->TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
153 // Adjust for the correct time zone
154 if (Time
->TimeZone
!= EFI_UNSPECIFIED_TIMEZONE
) {
155 EpochSeconds
+= Time
->TimeZone
* SEC_PER_MIN
;
159 // Get the current daylight information from non-volatile storage
160 Size
= sizeof (Daylight
);
161 Status
= EfiGetVariable (
162 (CHAR16
*)mDaylightVariableName
,
169 if (EFI_ERROR (Status
)) {
170 ASSERT(Status
!= EFI_INVALID_PARAMETER
);
171 ASSERT(Status
!= EFI_BUFFER_TOO_SMALL
);
173 if (Status
!= EFI_NOT_FOUND
) {
177 // The daylight variable does not exist in non-volatile storage, so create it.
180 Status
= EfiSetVariable (
181 (CHAR16
*)mDaylightVariableName
,
183 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
185 (VOID
*)&(Time
->Daylight
)
187 if (EFI_ERROR (Status
)) {
190 "LibGetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
191 mDaylightVariableName
,
197 // Got the daylight information
198 Time
->Daylight
= Daylight
;
200 // Adjust for the correct period
201 if ((Time
->Daylight
& EFI_TIME_IN_DAYLIGHT
) == EFI_TIME_IN_DAYLIGHT
) {
202 // Convert to adjusted time, i.e. spring forwards one hour
203 EpochSeconds
+= SEC_PER_HOUR
;
207 EpochToEfiTime (ElapsedSeconds
, Time
);
209 // Because we use the performance counter, we can fill the Nanosecond attribute
210 // provided that the remainder doesn't overflow 64-bit during multiplication.
211 if (Remainder
<= 18446744073U) {
212 Time
->Nanosecond
= MultU64x64 (Remainder
, 1000000000U) / Freq
;
214 DEBUG ((DEBUG_WARN
, "LibGetTime: Nanosecond value not set (64-bit overflow).\n"));
218 Capabilities
->Accuracy
= 0;
219 Capabilities
->Resolution
= Freq
;
220 Capabilities
->SetsToZero
= FALSE
;
227 Sets the current local time and date information.
229 @param Time A pointer to the current time.
231 @retval EFI_SUCCESS The operation completed successfully.
232 @retval EFI_INVALID_PARAMETER A time field is out of range.
233 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
245 if (!IsTimeValid (Time
)) {
246 return EFI_INVALID_PARAMETER
;
249 EpochSeconds
= EfiTimeToEpoch (Time
);
251 // Adjust for the correct time zone, i.e. convert to UTC time zone
252 if (Time
->TimeZone
!= EFI_UNSPECIFIED_TIMEZONE
) {
253 EpochSeconds
-= Time
->TimeZone
* SEC_PER_MIN
;
256 // Adjust for the correct period
257 if ((Time
->Daylight
& EFI_TIME_IN_DAYLIGHT
) == EFI_TIME_IN_DAYLIGHT
) {
258 // Convert to un-adjusted time, i.e. fall back one hour
259 EpochSeconds
-= SEC_PER_HOUR
;
262 // Save the current time zone information into non-volatile storage
263 Status
= EfiSetVariable (
264 (CHAR16
*)mTimeZoneVariableName
,
266 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
267 sizeof (Time
->TimeZone
),
268 (VOID
*)&(Time
->TimeZone
)
270 if (EFI_ERROR (Status
)) {
273 "LibSetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
274 mTimeZoneVariableName
,
280 // Save the current daylight information into non-volatile storage
281 Status
= EfiSetVariable (
282 (CHAR16
*)mDaylightVariableName
,
284 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
285 sizeof(Time
->Daylight
),
286 (VOID
*)&(Time
->Daylight
)
288 if (EFI_ERROR (Status
)) {
291 "LibSetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
292 mDaylightVariableName
,
298 Status
= EfiSetVariable (
299 (CHAR16
*)mEpochVariableName
,
301 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
302 sizeof (EpochSeconds
),
305 if (EFI_ERROR (Status
)) {
308 "LibSetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
309 mDaylightVariableName
,
319 Returns the current wakeup alarm clock setting.
321 @param Enabled Indicates if the alarm is currently enabled or disabled.
322 @param Pending Indicates if the alarm signal is pending and requires acknowledgement.
323 @param Time The current alarm setting.
325 @retval EFI_SUCCESS The alarm settings were returned.
326 @retval EFI_INVALID_PARAMETER Any parameter is NULL.
327 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
333 OUT BOOLEAN
*Enabled
,
334 OUT BOOLEAN
*Pending
,
338 return EFI_UNSUPPORTED
;
342 Sets the system wakeup alarm clock time.
344 @param Enabled Enable or disable the wakeup alarm.
345 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
347 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If
348 Enable is FALSE, then the wakeup alarm was disabled.
349 @retval EFI_INVALID_PARAMETER A time field is out of range.
350 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
351 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
361 return EFI_UNSUPPORTED
;
365 This is the declaration of an EFI image entry point. This can be the entry point to an application
366 written to this specification, an EFI boot service driver, or an EFI runtime driver.
368 @param ImageHandle Handle that identifies the loaded image.
369 @param SystemTable System Table for this image.
371 @retval EFI_SUCCESS The operation completed successfully.
377 IN EFI_HANDLE ImageHandle
,
378 IN EFI_SYSTEM_TABLE
*SystemTable
385 Fixup internal data so that EFI can be call in virtual mode.
386 Call the passed in Child Notify event and convert any pointers in
389 @param[in] Event The Event that is being processed
390 @param[in] Context Event Context
394 LibRtcVirtualNotifyEvent (