2 Implement EFI RealTimeClock runtime services via RTC Lib.
4 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
5 Copyright (c) 2017, Linaro, Ltd. All rights reserved.<BR>
6 Copyright (c) 2021, Ampere Computing LLC. All rights reserved.<BR>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
13 #include <Library/DebugLib.h>
14 #include <Library/RealTimeClockLib.h>
15 #include <Library/TimeBaseLib.h>
16 #include <Library/UefiLib.h>
17 #include <Library/UefiBootServicesTableLib.h>
18 #include <Library/UefiRuntimeLib.h>
19 #include <Protocol/RealTimeClock.h>
21 EFI_HANDLE mHandle
= NULL
;
24 // These values can be set by SetTime () and need to be returned by GetTime ()
25 // but cannot usually be kept by the RTC hardware, so we store them in a UEFI
31 } NON_VOLATILE_TIME_SETTINGS
;
33 STATIC CONST CHAR16 mTimeSettingsVariableName
[] = L
"RtcTimeSettings";
34 STATIC NON_VOLATILE_TIME_SETTINGS mTimeSettings
;
37 Returns the current time and date information, and the time-keeping capabilities
38 of the hardware platform.
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
57 return EFI_INVALID_PARAMETER
;
61 // Set these first so the RealTimeClockLib implementation
62 // can override them based on its own settings.
64 Time
->TimeZone
= mTimeSettings
.TimeZone
;
65 Time
->Daylight
= mTimeSettings
.Daylight
;
67 return LibGetTime (Time
, Capabilities
);
73 Sets the current local time and date information.
75 @param Time A pointer to the current time.
77 @retval EFI_SUCCESS The operation completed successfully.
78 @retval EFI_INVALID_PARAMETER A time field is out of range.
79 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
89 BOOLEAN TimeSettingsChanged
;
91 if (Time
== NULL
|| !IsTimeValid (Time
)) {
92 return EFI_INVALID_PARAMETER
;
95 TimeSettingsChanged
= FALSE
;
96 if (mTimeSettings
.TimeZone
!= Time
->TimeZone
||
97 mTimeSettings
.Daylight
!= Time
->Daylight
) {
99 mTimeSettings
.TimeZone
= Time
->TimeZone
;
100 mTimeSettings
.Daylight
= Time
->Daylight
;
101 TimeSettingsChanged
= TRUE
;
104 Status
= LibSetTime (Time
);
105 if (EFI_ERROR (Status
)) {
109 if (TimeSettingsChanged
) {
110 Status
= EfiSetVariable (
111 (CHAR16
*)mTimeSettingsVariableName
,
113 EFI_VARIABLE_NON_VOLATILE
|
114 EFI_VARIABLE_BOOTSERVICE_ACCESS
|
115 EFI_VARIABLE_RUNTIME_ACCESS
,
116 sizeof (mTimeSettings
),
117 (VOID
*)&mTimeSettings
);
118 if (EFI_ERROR (Status
)) {
119 return EFI_DEVICE_ERROR
;
127 Returns the current wakeup alarm clock setting.
129 @param Enabled Indicates if the alarm is currently enabled or disabled.
130 @param Pending Indicates if the alarm signal is pending and requires acknowledgement.
131 @param Time The current alarm setting.
133 @retval EFI_SUCCESS The alarm settings were returned.
134 @retval EFI_INVALID_PARAMETER Any parameter is NULL.
135 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
141 OUT BOOLEAN
*Enabled
,
142 OUT BOOLEAN
*Pending
,
146 return LibGetWakeupTime (Enabled
, Pending
, Time
);
151 Sets the system wakeup alarm clock time.
153 @param Enabled Enable or disable the wakeup alarm.
154 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
156 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If
157 Enable is FALSE, then the wakeup alarm was disabled.
158 @retval EFI_INVALID_PARAMETER A time field is out of range.
159 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
160 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
170 return LibSetWakeupTime (Enabled
, Time
);
176 This is the declaration of an EFI image entry point. This can be the entry point to an application
177 written to this specification, an EFI boot service driver, or an EFI runtime driver.
179 @param ImageHandle Handle that identifies the loaded image.
180 @param SystemTable System Table for this image.
182 @retval EFI_SUCCESS The operation completed successfully.
187 InitializeRealTimeClock (
188 IN EFI_HANDLE ImageHandle
,
189 IN EFI_SYSTEM_TABLE
*SystemTable
195 Status
= LibRtcInitialize (ImageHandle
, SystemTable
);
196 if (EFI_ERROR (Status
)) {
200 Size
= sizeof (mTimeSettings
);
201 Status
= EfiGetVariable ((CHAR16
*)mTimeSettingsVariableName
,
202 &gEfiCallerIdGuid
, NULL
, &Size
, (VOID
*)&mTimeSettings
);
203 if (EFI_ERROR (Status
) ||
204 !IsValidTimeZone (mTimeSettings
.TimeZone
) ||
205 !IsValidDaylight (mTimeSettings
.Daylight
)) {
206 DEBUG ((DEBUG_WARN
, "%a: using default timezone/daylight settings\n",
209 mTimeSettings
.TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
210 mTimeSettings
.Daylight
= 0;
213 SystemTable
->RuntimeServices
->GetTime
= GetTime
;
214 SystemTable
->RuntimeServices
->SetTime
= SetTime
;
215 SystemTable
->RuntimeServices
->GetWakeupTime
= GetWakeupTime
;
216 SystemTable
->RuntimeServices
->SetWakeupTime
= SetWakeupTime
;
218 Status
= gBS
->InstallMultipleProtocolInterfaces (
220 &gEfiRealTimeClockArchProtocolGuid
,