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
);
71 Sets the current local time and date information.
73 @param Time A pointer to the current time.
75 @retval EFI_SUCCESS The operation completed successfully.
76 @retval EFI_INVALID_PARAMETER A time field is out of range.
77 @retval EFI_DEVICE_ERROR The time could not be set due to hardware error.
87 BOOLEAN TimeSettingsChanged
;
89 if ((Time
== NULL
) || !IsTimeValid (Time
)) {
90 return EFI_INVALID_PARAMETER
;
93 TimeSettingsChanged
= FALSE
;
94 if ((mTimeSettings
.TimeZone
!= Time
->TimeZone
) ||
95 (mTimeSettings
.Daylight
!= Time
->Daylight
))
97 mTimeSettings
.TimeZone
= Time
->TimeZone
;
98 mTimeSettings
.Daylight
= Time
->Daylight
;
99 TimeSettingsChanged
= TRUE
;
102 Status
= LibSetTime (Time
);
103 if (EFI_ERROR (Status
)) {
107 if (TimeSettingsChanged
) {
108 Status
= EfiSetVariable (
109 (CHAR16
*)mTimeSettingsVariableName
,
111 EFI_VARIABLE_NON_VOLATILE
|
112 EFI_VARIABLE_BOOTSERVICE_ACCESS
|
113 EFI_VARIABLE_RUNTIME_ACCESS
,
114 sizeof (mTimeSettings
),
115 (VOID
*)&mTimeSettings
117 if (EFI_ERROR (Status
)) {
118 return EFI_DEVICE_ERROR
;
126 Returns the current wakeup alarm clock setting.
128 @param Enabled Indicates if the alarm is currently enabled or disabled.
129 @param Pending Indicates if the alarm signal is pending and requires acknowledgement.
130 @param Time The current alarm setting.
132 @retval EFI_SUCCESS The alarm settings were returned.
133 @retval EFI_INVALID_PARAMETER Any parameter is NULL.
134 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
140 OUT BOOLEAN
*Enabled
,
141 OUT BOOLEAN
*Pending
,
145 if ((Time
== NULL
) || (Enabled
== NULL
) || (Pending
== NULL
)) {
146 return EFI_INVALID_PARAMETER
;
150 // Set these first so the RealTimeClockLib implementation
151 // can override them based on its own settings.
153 Time
->TimeZone
= mTimeSettings
.TimeZone
;
154 Time
->Daylight
= mTimeSettings
.Daylight
;
156 return LibGetWakeupTime (Enabled
, Pending
, Time
);
160 Sets the system wakeup alarm clock time.
162 @param Enabled Enable or disable the wakeup alarm.
163 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
165 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If
166 Enable is FALSE, then the wakeup alarm was disabled.
167 @retval EFI_INVALID_PARAMETER A time field is out of range.
168 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
169 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
179 return LibSetWakeupTime (Enabled
, Time
);
183 This is the declaration of an EFI image entry point. This can be the entry point to an application
184 written to this specification, an EFI boot service driver, or an EFI runtime driver.
186 @param ImageHandle Handle that identifies the loaded image.
187 @param SystemTable System Table for this image.
189 @retval EFI_SUCCESS The operation completed successfully.
194 InitializeRealTimeClock (
195 IN EFI_HANDLE ImageHandle
,
196 IN EFI_SYSTEM_TABLE
*SystemTable
202 Status
= LibRtcInitialize (ImageHandle
, SystemTable
);
203 if (EFI_ERROR (Status
)) {
207 Size
= sizeof (mTimeSettings
);
208 Status
= EfiGetVariable (
209 (CHAR16
*)mTimeSettingsVariableName
,
213 (VOID
*)&mTimeSettings
215 if (EFI_ERROR (Status
) ||
216 !IsValidTimeZone (mTimeSettings
.TimeZone
) ||
217 !IsValidDaylight (mTimeSettings
.Daylight
))
221 "%a: using default timezone/daylight settings\n",
225 mTimeSettings
.TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
226 mTimeSettings
.Daylight
= 0;
229 SystemTable
->RuntimeServices
->GetTime
= GetTime
;
230 SystemTable
->RuntimeServices
->SetTime
= SetTime
;
231 SystemTable
->RuntimeServices
->GetWakeupTime
= GetWakeupTime
;
232 SystemTable
->RuntimeServices
->SetWakeupTime
= SetWakeupTime
;
234 Status
= gBS
->InstallMultipleProtocolInterfaces (
236 &gEfiRealTimeClockArchProtocolGuid
,