]> git.proxmox.com Git - mirror_edk2.git/blob - EmbeddedPkg/RealTimeClockRuntimeDxe/RealTimeClock.c
EmbeddedPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / EmbeddedPkg / RealTimeClockRuntimeDxe / RealTimeClock.c
1 /** @file
2 Implement EFI RealTimeClock runtime services via RTC Lib.
3
4 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
5 Copyright (c) 2017, Linaro, Ltd. All rights reserved.<BR>
6
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10
11 #include <PiDxe.h>
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>
18
19 EFI_HANDLE mHandle = NULL;
20
21 //
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
24 // variable instead.
25 //
26 typedef struct {
27 INT16 TimeZone;
28 UINT8 Daylight;
29 } NON_VOLATILE_TIME_SETTINGS;
30
31 STATIC CONST CHAR16 mTimeSettingsVariableName[] = L"RtcTimeSettings";
32 STATIC NON_VOLATILE_TIME_SETTINGS mTimeSettings;
33
34 STATIC
35 BOOLEAN
36 IsValidTimeZone (
37 IN INT16 TimeZone
38 )
39 {
40 return TimeZone == EFI_UNSPECIFIED_TIMEZONE ||
41 (TimeZone >= -1440 && TimeZone <= 1440);
42 }
43
44 STATIC
45 BOOLEAN
46 IsValidDaylight (
47 IN INT8 Daylight
48 )
49 {
50 return Daylight == 0 ||
51 Daylight == EFI_TIME_ADJUST_DAYLIGHT ||
52 Daylight == (EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT);
53 }
54
55 STATIC
56 BOOLEAN
57 EFIAPI
58 IsLeapYear (
59 IN EFI_TIME *Time
60 )
61 {
62 if (Time->Year % 4 == 0) {
63 if (Time->Year % 100 == 0) {
64 if (Time->Year % 400 == 0) {
65 return TRUE;
66 } else {
67 return FALSE;
68 }
69 } else {
70 return TRUE;
71 }
72 } else {
73 return FALSE;
74 }
75 }
76
77 STATIC CONST INTN mDayOfMonth[12] = {
78 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
79 };
80
81 STATIC
82 BOOLEAN
83 EFIAPI
84 IsDayValid (
85 IN EFI_TIME *Time
86 )
87 {
88 ASSERT (Time->Day >= 1);
89 ASSERT (Time->Day <= mDayOfMonth[Time->Month - 1]);
90 ASSERT (Time->Month != 2 || IsLeapYear (Time) || Time->Day <= 28);
91
92 if (Time->Day < 1 ||
93 Time->Day > mDayOfMonth[Time->Month - 1] ||
94 (Time->Month == 2 && !IsLeapYear (Time) && Time->Day > 28)) {
95 return FALSE;
96 }
97 return TRUE;
98 }
99
100 STATIC
101 BOOLEAN
102 EFIAPI
103 IsTimeValid(
104 IN EFI_TIME *Time
105 )
106 {
107 // Check the input parameters are within the range specified by UEFI
108 if (Time->Year < 1900 ||
109 Time->Year > 9999 ||
110 Time->Month < 1 ||
111 Time->Month > 12 ||
112 !IsDayValid (Time) ||
113 Time->Hour > 23 ||
114 Time->Minute > 59 ||
115 Time->Second > 59 ||
116 !IsValidTimeZone (Time->TimeZone) ||
117 !IsValidDaylight (Time->Daylight)) {
118 return FALSE;
119 }
120 return TRUE;
121 }
122
123 /**
124 Returns the current time and date information, and the time-keeping capabilities
125 of the hardware platform.
126
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.
130
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.
134
135 **/
136 EFI_STATUS
137 EFIAPI
138 GetTime (
139 OUT EFI_TIME *Time,
140 OUT EFI_TIME_CAPABILITIES *Capabilities
141 )
142 {
143 if (Time == NULL) {
144 return EFI_INVALID_PARAMETER;
145 }
146
147 //
148 // Set these first so the RealTimeClockLib implementation
149 // can override them based on its own settings.
150 //
151 Time->TimeZone = mTimeSettings.TimeZone;
152 Time->Daylight = mTimeSettings.Daylight;
153
154 return LibGetTime (Time, Capabilities);
155 }
156
157
158
159 /**
160 Sets the current local time and date information.
161
162 @param Time A pointer to the current time.
163
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.
167
168 **/
169 EFI_STATUS
170 EFIAPI
171 SetTime (
172 IN EFI_TIME *Time
173 )
174 {
175 EFI_STATUS Status;
176 BOOLEAN TimeSettingsChanged;
177
178 if (Time == NULL || !IsTimeValid (Time)) {
179 return EFI_INVALID_PARAMETER;
180 }
181
182 TimeSettingsChanged = FALSE;
183 if (mTimeSettings.TimeZone != Time->TimeZone ||
184 mTimeSettings.Daylight != Time->Daylight) {
185
186 mTimeSettings.TimeZone = Time->TimeZone;
187 mTimeSettings.Daylight = Time->Daylight;
188 TimeSettingsChanged = TRUE;
189 }
190
191 Status = LibSetTime (Time);
192 if (EFI_ERROR (Status)) {
193 return Status;
194 }
195
196 if (TimeSettingsChanged) {
197 Status = EfiSetVariable (
198 (CHAR16 *)mTimeSettingsVariableName,
199 &gEfiCallerIdGuid,
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;
207 }
208 }
209 return EFI_SUCCESS;
210 }
211
212
213 /**
214 Returns the current wakeup alarm clock setting.
215
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.
219
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.
223
224 **/
225 EFI_STATUS
226 EFIAPI
227 GetWakeupTime (
228 OUT BOOLEAN *Enabled,
229 OUT BOOLEAN *Pending,
230 OUT EFI_TIME *Time
231 )
232 {
233 return LibGetWakeupTime (Enabled, Pending, Time);
234 }
235
236
237 /**
238 Sets the system wakeup alarm clock time.
239
240 @param Enabled Enable or disable the wakeup alarm.
241 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
242
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.
248
249 **/
250 EFI_STATUS
251 EFIAPI
252 SetWakeupTime (
253 IN BOOLEAN Enabled,
254 OUT EFI_TIME *Time
255 )
256 {
257 return LibSetWakeupTime (Enabled, Time);
258 }
259
260
261
262 /**
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.
265
266 @param ImageHandle Handle that identifies the loaded image.
267 @param SystemTable System Table for this image.
268
269 @retval EFI_SUCCESS The operation completed successfully.
270
271 **/
272 EFI_STATUS
273 EFIAPI
274 InitializeRealTimeClock (
275 IN EFI_HANDLE ImageHandle,
276 IN EFI_SYSTEM_TABLE *SystemTable
277 )
278 {
279 EFI_STATUS Status;
280 UINTN Size;
281
282 Status = LibRtcInitialize (ImageHandle, SystemTable);
283 if (EFI_ERROR (Status)) {
284 return Status;
285 }
286
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",
294 __FUNCTION__));
295
296 mTimeSettings.TimeZone = EFI_UNSPECIFIED_TIMEZONE;
297 mTimeSettings.Daylight = 0;
298 }
299
300 SystemTable->RuntimeServices->GetTime = GetTime;
301 SystemTable->RuntimeServices->SetTime = SetTime;
302 SystemTable->RuntimeServices->GetWakeupTime = GetWakeupTime;
303 SystemTable->RuntimeServices->SetWakeupTime = SetWakeupTime;
304
305 Status = gBS->InstallMultipleProtocolInterfaces (
306 &mHandle,
307 &gEfiRealTimeClockArchProtocolGuid,
308 NULL,
309 NULL
310 );
311
312 return Status;
313 }
314