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
88 ASSERT (Time
->Day
>= 1);
89 ASSERT (Time
->Day
<= mDayOfMonth
[Time
->Month
- 1]);
90 ASSERT (Time
->Month
!= 2 || IsLeapYear (Time
) || Time
->Day
<= 28);
93 Time
->Day
> mDayOfMonth
[Time
->Month
- 1] ||
94 (Time
->Month
== 2 && !IsLeapYear (Time
) && Time
->Day
> 28)) {
107 // Check the input parameters are within the range specified by UEFI
108 if (Time
->Year
< 1900 ||
112 !IsDayValid (Time
) ||
116 !IsValidTimeZone (Time
->TimeZone
) ||
117 !IsValidDaylight (Time
->Daylight
)) {
124 Returns the current time and date information, and the time-keeping capabilities
125 of the hardware platform.
127 @param Time A pointer to storage to receive a snapshot of the current time.
128 @param Capabilities An optional pointer to a buffer to receive the real time clock
129 device's capabilities.
131 @retval EFI_SUCCESS The operation completed successfully.
132 @retval EFI_INVALID_PARAMETER Time is NULL.
133 @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.
140 OUT EFI_TIME_CAPABILITIES
*Capabilities
144 return EFI_INVALID_PARAMETER
;
148 // Set these first so the RealTimeClockLib implementation
149 // can override them based on its own settings.
151 Time
->TimeZone
= mTimeSettings
.TimeZone
;
152 Time
->Daylight
= mTimeSettings
.Daylight
;
154 return LibGetTime (Time
, Capabilities
);
160 Sets the current local time and date information.
162 @param Time A pointer to the current time.
164 @retval EFI_SUCCESS The operation completed successfully.
165 @retval EFI_INVALID_PARAMETER A time field is out of range.
166 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
176 BOOLEAN TimeSettingsChanged
;
178 if (Time
== NULL
|| !IsTimeValid (Time
)) {
179 return EFI_INVALID_PARAMETER
;
182 TimeSettingsChanged
= FALSE
;
183 if (mTimeSettings
.TimeZone
!= Time
->TimeZone
||
184 mTimeSettings
.Daylight
!= Time
->Daylight
) {
186 mTimeSettings
.TimeZone
= Time
->TimeZone
;
187 mTimeSettings
.Daylight
= Time
->Daylight
;
188 TimeSettingsChanged
= TRUE
;
191 Status
= LibSetTime (Time
);
192 if (EFI_ERROR (Status
)) {
196 if (TimeSettingsChanged
) {
197 Status
= EfiSetVariable (
198 (CHAR16
*)mTimeSettingsVariableName
,
200 EFI_VARIABLE_NON_VOLATILE
|
201 EFI_VARIABLE_BOOTSERVICE_ACCESS
|
202 EFI_VARIABLE_RUNTIME_ACCESS
,
203 sizeof (mTimeSettings
),
204 (VOID
*)&mTimeSettings
);
205 if (EFI_ERROR (Status
)) {
206 return EFI_DEVICE_ERROR
;
214 Returns the current wakeup alarm clock setting.
216 @param Enabled Indicates if the alarm is currently enabled or disabled.
217 @param Pending Indicates if the alarm signal is pending and requires acknowledgement.
218 @param Time The current alarm setting.
220 @retval EFI_SUCCESS The alarm settings were returned.
221 @retval EFI_INVALID_PARAMETER Any parameter is NULL.
222 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
228 OUT BOOLEAN
*Enabled
,
229 OUT BOOLEAN
*Pending
,
233 return LibGetWakeupTime (Enabled
, Pending
, Time
);
238 Sets the system wakeup alarm clock time.
240 @param Enabled Enable or disable the wakeup alarm.
241 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
243 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If
244 Enable is FALSE, then the wakeup alarm was disabled.
245 @retval EFI_INVALID_PARAMETER A time field is out of range.
246 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
247 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
257 return LibSetWakeupTime (Enabled
, Time
);
263 This is the declaration of an EFI image entry point. This can be the entry point to an application
264 written to this specification, an EFI boot service driver, or an EFI runtime driver.
266 @param ImageHandle Handle that identifies the loaded image.
267 @param SystemTable System Table for this image.
269 @retval EFI_SUCCESS The operation completed successfully.
274 InitializeRealTimeClock (
275 IN EFI_HANDLE ImageHandle
,
276 IN EFI_SYSTEM_TABLE
*SystemTable
282 Status
= LibRtcInitialize (ImageHandle
, SystemTable
);
283 if (EFI_ERROR (Status
)) {
287 Size
= sizeof (mTimeSettings
);
288 Status
= EfiGetVariable ((CHAR16
*)mTimeSettingsVariableName
,
289 &gEfiCallerIdGuid
, NULL
, &Size
, (VOID
*)&mTimeSettings
);
290 if (EFI_ERROR (Status
) ||
291 !IsValidTimeZone (mTimeSettings
.TimeZone
) ||
292 !IsValidDaylight (mTimeSettings
.Daylight
)) {
293 DEBUG ((DEBUG_WARN
, "%a: using default timezone/daylight settings\n",
296 mTimeSettings
.TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
297 mTimeSettings
.Daylight
= 0;
300 SystemTable
->RuntimeServices
->GetTime
= GetTime
;
301 SystemTable
->RuntimeServices
->SetTime
= SetTime
;
302 SystemTable
->RuntimeServices
->GetWakeupTime
= GetWakeupTime
;
303 SystemTable
->RuntimeServices
->SetWakeupTime
= SetWakeupTime
;
305 Status
= gBS
->InstallMultipleProtocolInterfaces (
307 &gEfiRealTimeClockArchProtocolGuid
,