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>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #include <Library/DebugLib.h>
13 #include <Library/RealTimeClockLib.h>
14 #include <Library/UefiLib.h>
15 #include <Library/UefiBootServicesTableLib.h>
16 #include <Library/UefiRuntimeLib.h>
17 #include <Protocol/RealTimeClock.h>
19 EFI_HANDLE mHandle
= NULL
;
22 // These values can be set by SetTime () and need to be returned by GetTime ()
23 // but cannot usually be kept by the RTC hardware, so we store them in a UEFI
29 } NON_VOLATILE_TIME_SETTINGS
;
31 STATIC CONST CHAR16 mTimeSettingsVariableName
[] = L
"RtcTimeSettings";
32 STATIC NON_VOLATILE_TIME_SETTINGS mTimeSettings
;
40 return TimeZone
== EFI_UNSPECIFIED_TIMEZONE
||
41 (TimeZone
>= -1440 && TimeZone
<= 1440);
50 return Daylight
== 0 ||
51 Daylight
== EFI_TIME_ADJUST_DAYLIGHT
||
52 Daylight
== (EFI_TIME_ADJUST_DAYLIGHT
| EFI_TIME_IN_DAYLIGHT
);
62 if (Time
->Year
% 4 == 0) {
63 if (Time
->Year
% 100 == 0) {
64 if (Time
->Year
% 400 == 0) {
77 STATIC CONST INTN mDayOfMonth
[12] = {
78 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
89 Time
->Day
> mDayOfMonth
[Time
->Month
- 1] ||
90 (Time
->Month
== 2 && !IsLeapYear (Time
) && Time
->Day
> 28)) {
103 // Check the input parameters are within the range specified by UEFI
104 if (Time
->Year
< 1900 ||
108 !IsDayValid (Time
) ||
112 Time
->Nanosecond
> 999999999 ||
113 !IsValidTimeZone (Time
->TimeZone
) ||
114 !IsValidDaylight (Time
->Daylight
)) {
121 Returns the current time and date information, and the time-keeping capabilities
122 of the hardware platform.
124 @param Time A pointer to storage to receive a snapshot of the current time.
125 @param Capabilities An optional pointer to a buffer to receive the real time clock
126 device's capabilities.
128 @retval EFI_SUCCESS The operation completed successfully.
129 @retval EFI_INVALID_PARAMETER Time is NULL.
130 @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.
137 OUT EFI_TIME_CAPABILITIES
*Capabilities
141 return EFI_INVALID_PARAMETER
;
145 // Set these first so the RealTimeClockLib implementation
146 // can override them based on its own settings.
148 Time
->TimeZone
= mTimeSettings
.TimeZone
;
149 Time
->Daylight
= mTimeSettings
.Daylight
;
151 return LibGetTime (Time
, Capabilities
);
157 Sets the current local time and date information.
159 @param Time A pointer to the current time.
161 @retval EFI_SUCCESS The operation completed successfully.
162 @retval EFI_INVALID_PARAMETER A time field is out of range.
163 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
173 BOOLEAN TimeSettingsChanged
;
175 if (Time
== NULL
|| !IsTimeValid (Time
)) {
176 return EFI_INVALID_PARAMETER
;
179 TimeSettingsChanged
= FALSE
;
180 if (mTimeSettings
.TimeZone
!= Time
->TimeZone
||
181 mTimeSettings
.Daylight
!= Time
->Daylight
) {
183 mTimeSettings
.TimeZone
= Time
->TimeZone
;
184 mTimeSettings
.Daylight
= Time
->Daylight
;
185 TimeSettingsChanged
= TRUE
;
188 Status
= LibSetTime (Time
);
189 if (EFI_ERROR (Status
)) {
193 if (TimeSettingsChanged
) {
194 Status
= EfiSetVariable (
195 (CHAR16
*)mTimeSettingsVariableName
,
197 EFI_VARIABLE_NON_VOLATILE
|
198 EFI_VARIABLE_BOOTSERVICE_ACCESS
|
199 EFI_VARIABLE_RUNTIME_ACCESS
,
200 sizeof (mTimeSettings
),
201 (VOID
*)&mTimeSettings
);
202 if (EFI_ERROR (Status
)) {
203 return EFI_DEVICE_ERROR
;
211 Returns the current wakeup alarm clock setting.
213 @param Enabled Indicates if the alarm is currently enabled or disabled.
214 @param Pending Indicates if the alarm signal is pending and requires acknowledgement.
215 @param Time The current alarm setting.
217 @retval EFI_SUCCESS The alarm settings were returned.
218 @retval EFI_INVALID_PARAMETER Any parameter is NULL.
219 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
225 OUT BOOLEAN
*Enabled
,
226 OUT BOOLEAN
*Pending
,
230 return LibGetWakeupTime (Enabled
, Pending
, Time
);
235 Sets the system wakeup alarm clock time.
237 @param Enabled Enable or disable the wakeup alarm.
238 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
240 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If
241 Enable is FALSE, then the wakeup alarm was disabled.
242 @retval EFI_INVALID_PARAMETER A time field is out of range.
243 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
244 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
254 return LibSetWakeupTime (Enabled
, Time
);
260 This is the declaration of an EFI image entry point. This can be the entry point to an application
261 written to this specification, an EFI boot service driver, or an EFI runtime driver.
263 @param ImageHandle Handle that identifies the loaded image.
264 @param SystemTable System Table for this image.
266 @retval EFI_SUCCESS The operation completed successfully.
271 InitializeRealTimeClock (
272 IN EFI_HANDLE ImageHandle
,
273 IN EFI_SYSTEM_TABLE
*SystemTable
279 Status
= LibRtcInitialize (ImageHandle
, SystemTable
);
280 if (EFI_ERROR (Status
)) {
284 Size
= sizeof (mTimeSettings
);
285 Status
= EfiGetVariable ((CHAR16
*)mTimeSettingsVariableName
,
286 &gEfiCallerIdGuid
, NULL
, &Size
, (VOID
*)&mTimeSettings
);
287 if (EFI_ERROR (Status
) ||
288 !IsValidTimeZone (mTimeSettings
.TimeZone
) ||
289 !IsValidDaylight (mTimeSettings
.Daylight
)) {
290 DEBUG ((DEBUG_WARN
, "%a: using default timezone/daylight settings\n",
293 mTimeSettings
.TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
294 mTimeSettings
.Daylight
= 0;
297 SystemTable
->RuntimeServices
->GetTime
= GetTime
;
298 SystemTable
->RuntimeServices
->SetTime
= SetTime
;
299 SystemTable
->RuntimeServices
->GetWakeupTime
= GetWakeupTime
;
300 SystemTable
->RuntimeServices
->SetWakeupTime
= SetWakeupTime
;
302 Status
= gBS
->InstallMultipleProtocolInterfaces (
304 &gEfiRealTimeClockArchProtocolGuid
,