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 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <Library/DebugLib.h>
19 #include <Library/RealTimeClockLib.h>
20 #include <Library/UefiLib.h>
21 #include <Library/UefiBootServicesTableLib.h>
22 #include <Library/UefiRuntimeLib.h>
23 #include <Protocol/RealTimeClock.h>
25 EFI_HANDLE mHandle
= NULL
;
28 // These values can be set by SetTime () and need to be returned by GetTime ()
29 // but cannot usually be kept by the RTC hardware, so we store them in a UEFI
35 } NON_VOLATILE_TIME_SETTINGS
;
37 STATIC CONST CHAR16 mTimeSettingsVariableName
[] = L
"RtcTimeSettings";
38 STATIC NON_VOLATILE_TIME_SETTINGS mTimeSettings
;
46 return TimeZone
== EFI_UNSPECIFIED_TIMEZONE
||
47 (TimeZone
>= -1440 && TimeZone
<= 1440);
56 return Daylight
== 0 ||
57 Daylight
== EFI_TIME_ADJUST_DAYLIGHT
||
58 Daylight
== (EFI_TIME_ADJUST_DAYLIGHT
| EFI_TIME_IN_DAYLIGHT
);
68 if (Time
->Year
% 4 == 0) {
69 if (Time
->Year
% 100 == 0) {
70 if (Time
->Year
% 400 == 0) {
83 STATIC CONST INTN mDayOfMonth
[12] = {
84 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
94 ASSERT (Time
->Day
>= 1);
95 ASSERT (Time
->Day
<= mDayOfMonth
[Time
->Month
- 1]);
96 ASSERT (Time
->Month
!= 2 || IsLeapYear (Time
) || Time
->Day
<= 28);
99 Time
->Day
> mDayOfMonth
[Time
->Month
- 1] ||
100 (Time
->Month
== 2 && !IsLeapYear (Time
) && Time
->Day
> 28)) {
113 // Check the input parameters are within the range specified by UEFI
114 if (Time
->Year
< 1900 ||
118 !IsDayValid (Time
) ||
122 !IsValidTimeZone (Time
->TimeZone
) ||
123 !IsValidDaylight (Time
->Daylight
)) {
130 Returns the current time and date information, and the time-keeping capabilities
131 of the hardware platform.
133 @param Time A pointer to storage to receive a snapshot of the current time.
134 @param Capabilities An optional pointer to a buffer to receive the real time clock
135 device's capabilities.
137 @retval EFI_SUCCESS The operation completed successfully.
138 @retval EFI_INVALID_PARAMETER Time is NULL.
139 @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.
146 OUT EFI_TIME_CAPABILITIES
*Capabilities
150 return EFI_INVALID_PARAMETER
;
154 // Set these first so the RealTimeClockLib implementation
155 // can override them based on its own settings.
157 Time
->TimeZone
= mTimeSettings
.TimeZone
;
158 Time
->Daylight
= mTimeSettings
.Daylight
;
160 return LibGetTime (Time
, Capabilities
);
166 Sets the current local time and date information.
168 @param Time A pointer to the current time.
170 @retval EFI_SUCCESS The operation completed successfully.
171 @retval EFI_INVALID_PARAMETER A time field is out of range.
172 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
182 BOOLEAN TimeSettingsChanged
;
184 if (Time
== NULL
|| !IsTimeValid (Time
)) {
185 return EFI_INVALID_PARAMETER
;
188 TimeSettingsChanged
= FALSE
;
189 if (mTimeSettings
.TimeZone
!= Time
->TimeZone
||
190 mTimeSettings
.Daylight
!= Time
->Daylight
) {
192 mTimeSettings
.TimeZone
= Time
->TimeZone
;
193 mTimeSettings
.Daylight
= Time
->Daylight
;
194 TimeSettingsChanged
= TRUE
;
197 Status
= LibSetTime (Time
);
198 if (EFI_ERROR (Status
)) {
202 if (TimeSettingsChanged
) {
203 Status
= EfiSetVariable (
204 (CHAR16
*)mTimeSettingsVariableName
,
206 EFI_VARIABLE_NON_VOLATILE
|
207 EFI_VARIABLE_BOOTSERVICE_ACCESS
|
208 EFI_VARIABLE_RUNTIME_ACCESS
,
209 sizeof (mTimeSettings
),
210 (VOID
*)&mTimeSettings
);
211 if (EFI_ERROR (Status
)) {
212 return EFI_DEVICE_ERROR
;
220 Returns the current wakeup alarm clock setting.
222 @param Enabled Indicates if the alarm is currently enabled or disabled.
223 @param Pending Indicates if the alarm signal is pending and requires acknowledgement.
224 @param Time The current alarm setting.
226 @retval EFI_SUCCESS The alarm settings were returned.
227 @retval EFI_INVALID_PARAMETER Any parameter is NULL.
228 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
234 OUT BOOLEAN
*Enabled
,
235 OUT BOOLEAN
*Pending
,
239 return LibGetWakeupTime (Enabled
, Pending
, Time
);
244 Sets the system wakeup alarm clock time.
246 @param Enabled Enable or disable the wakeup alarm.
247 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
249 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If
250 Enable is FALSE, then the wakeup alarm was disabled.
251 @retval EFI_INVALID_PARAMETER A time field is out of range.
252 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
253 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
263 return LibSetWakeupTime (Enabled
, Time
);
269 This is the declaration of an EFI image entry point. This can be the entry point to an application
270 written to this specification, an EFI boot service driver, or an EFI runtime driver.
272 @param ImageHandle Handle that identifies the loaded image.
273 @param SystemTable System Table for this image.
275 @retval EFI_SUCCESS The operation completed successfully.
280 InitializeRealTimeClock (
281 IN EFI_HANDLE ImageHandle
,
282 IN EFI_SYSTEM_TABLE
*SystemTable
288 Status
= LibRtcInitialize (ImageHandle
, SystemTable
);
289 if (EFI_ERROR (Status
)) {
293 Size
= sizeof (mTimeSettings
);
294 Status
= EfiGetVariable ((CHAR16
*)mTimeSettingsVariableName
,
295 &gEfiCallerIdGuid
, NULL
, &Size
, (VOID
*)&mTimeSettings
);
296 if (EFI_ERROR (Status
) ||
297 !IsValidTimeZone (mTimeSettings
.TimeZone
) ||
298 !IsValidDaylight (mTimeSettings
.Daylight
)) {
299 DEBUG ((DEBUG_WARN
, "%a: using default timezone/daylight settings\n",
302 mTimeSettings
.TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
303 mTimeSettings
.Daylight
= 0;
306 SystemTable
->RuntimeServices
->GetTime
= GetTime
;
307 SystemTable
->RuntimeServices
->SetTime
= SetTime
;
308 SystemTable
->RuntimeServices
->GetWakeupTime
= GetWakeupTime
;
309 SystemTable
->RuntimeServices
->SetWakeupTime
= SetWakeupTime
;
311 Status
= gBS
->InstallMultipleProtocolInterfaces (
313 &gEfiRealTimeClockArchProtocolGuid
,