]>
Commit | Line | Data |
---|---|---|
2ef2b01e A |
1 | /** @file\r |
2 | Implement EFI RealTimeClock runtime services via RTC Lib.\r | |
3402aac7 | 3 | \r |
60274cca | 4 | Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r |
517cf877 | 5 | Copyright (c) 2017, Linaro, Ltd. All rights reserved.<BR>\r |
3402aac7 | 6 | \r |
878b807a | 7 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
2ef2b01e A |
8 | \r |
9 | **/\r | |
10 | \r | |
3402aac7 | 11 | #include <PiDxe.h>\r |
517cf877 AB |
12 | #include <Library/DebugLib.h>\r |
13 | #include <Library/RealTimeClockLib.h>\r | |
2ef2b01e A |
14 | #include <Library/UefiLib.h>\r |
15 | #include <Library/UefiBootServicesTableLib.h>\r | |
517cf877 | 16 | #include <Library/UefiRuntimeLib.h>\r |
2ef2b01e A |
17 | #include <Protocol/RealTimeClock.h>\r |
18 | \r | |
19 | EFI_HANDLE mHandle = NULL;\r | |
20 | \r | |
517cf877 AB |
21 | //\r |
22 | // These values can be set by SetTime () and need to be returned by GetTime ()\r | |
23 | // but cannot usually be kept by the RTC hardware, so we store them in a UEFI\r | |
24 | // variable instead.\r | |
25 | //\r | |
26 | typedef struct {\r | |
27 | INT16 TimeZone;\r | |
28 | UINT8 Daylight;\r | |
29 | } NON_VOLATILE_TIME_SETTINGS;\r | |
30 | \r | |
31 | STATIC CONST CHAR16 mTimeSettingsVariableName[] = L"RtcTimeSettings";\r | |
32 | STATIC NON_VOLATILE_TIME_SETTINGS mTimeSettings;\r | |
33 | \r | |
34 | STATIC\r | |
35 | BOOLEAN\r | |
36 | IsValidTimeZone (\r | |
37 | IN INT16 TimeZone\r | |
38 | )\r | |
39 | {\r | |
40 | return TimeZone == EFI_UNSPECIFIED_TIMEZONE ||\r | |
41 | (TimeZone >= -1440 && TimeZone <= 1440);\r | |
42 | }\r | |
43 | \r | |
44 | STATIC\r | |
45 | BOOLEAN\r | |
46 | IsValidDaylight (\r | |
47 | IN INT8 Daylight\r | |
48 | )\r | |
49 | {\r | |
50 | return Daylight == 0 ||\r | |
51 | Daylight == EFI_TIME_ADJUST_DAYLIGHT ||\r | |
52 | Daylight == (EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT);\r | |
53 | }\r | |
2ef2b01e | 54 | \r |
517cf877 AB |
55 | STATIC\r |
56 | BOOLEAN\r | |
57 | EFIAPI\r | |
58 | IsLeapYear (\r | |
59 | IN EFI_TIME *Time\r | |
60 | )\r | |
61 | {\r | |
62 | if (Time->Year % 4 == 0) {\r | |
63 | if (Time->Year % 100 == 0) {\r | |
64 | if (Time->Year % 400 == 0) {\r | |
65 | return TRUE;\r | |
66 | } else {\r | |
67 | return FALSE;\r | |
68 | }\r | |
69 | } else {\r | |
70 | return TRUE;\r | |
71 | }\r | |
72 | } else {\r | |
73 | return FALSE;\r | |
74 | }\r | |
75 | }\r | |
76 | \r | |
77 | STATIC CONST INTN mDayOfMonth[12] = {\r | |
78 | 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31\r | |
79 | };\r | |
80 | \r | |
81 | STATIC\r | |
82 | BOOLEAN\r | |
83 | EFIAPI\r | |
84 | IsDayValid (\r | |
85 | IN EFI_TIME *Time\r | |
86 | )\r | |
87 | {\r | |
88 | ASSERT (Time->Day >= 1);\r | |
89 | ASSERT (Time->Day <= mDayOfMonth[Time->Month - 1]);\r | |
90 | ASSERT (Time->Month != 2 || IsLeapYear (Time) || Time->Day <= 28);\r | |
91 | \r | |
92 | if (Time->Day < 1 ||\r | |
93 | Time->Day > mDayOfMonth[Time->Month - 1] ||\r | |
94 | (Time->Month == 2 && !IsLeapYear (Time) && Time->Day > 28)) {\r | |
95 | return FALSE;\r | |
96 | }\r | |
97 | return TRUE;\r | |
98 | }\r | |
99 | \r | |
100 | STATIC\r | |
101 | BOOLEAN\r | |
102 | EFIAPI\r | |
103 | IsTimeValid(\r | |
104 | IN EFI_TIME *Time\r | |
105 | )\r | |
106 | {\r | |
107 | // Check the input parameters are within the range specified by UEFI\r | |
108 | if (Time->Year < 1900 ||\r | |
109 | Time->Year > 9999 ||\r | |
110 | Time->Month < 1 ||\r | |
111 | Time->Month > 12 ||\r | |
112 | !IsDayValid (Time) ||\r | |
113 | Time->Hour > 23 ||\r | |
114 | Time->Minute > 59 ||\r | |
115 | Time->Second > 59 ||\r | |
116 | !IsValidTimeZone (Time->TimeZone) ||\r | |
117 | !IsValidDaylight (Time->Daylight)) {\r | |
118 | return FALSE;\r | |
119 | }\r | |
120 | return TRUE;\r | |
121 | }\r | |
2ef2b01e A |
122 | \r |
123 | /**\r | |
124 | Returns the current time and date information, and the time-keeping capabilities\r | |
125 | of the hardware platform.\r | |
126 | \r | |
127 | @param Time A pointer to storage to receive a snapshot of the current time.\r | |
128 | @param Capabilities An optional pointer to a buffer to receive the real time clock\r | |
129 | device's capabilities.\r | |
130 | \r | |
131 | @retval EFI_SUCCESS The operation completed successfully.\r | |
132 | @retval EFI_INVALID_PARAMETER Time is NULL.\r | |
133 | @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.\r | |
134 | \r | |
135 | **/\r | |
136 | EFI_STATUS\r | |
137 | EFIAPI\r | |
138 | GetTime (\r | |
139 | OUT EFI_TIME *Time,\r | |
517cf877 | 140 | OUT EFI_TIME_CAPABILITIES *Capabilities\r |
2ef2b01e A |
141 | )\r |
142 | {\r | |
517cf877 AB |
143 | if (Time == NULL) {\r |
144 | return EFI_INVALID_PARAMETER;\r | |
145 | }\r | |
146 | \r | |
147 | //\r | |
148 | // Set these first so the RealTimeClockLib implementation\r | |
149 | // can override them based on its own settings.\r | |
150 | //\r | |
151 | Time->TimeZone = mTimeSettings.TimeZone;\r | |
152 | Time->Daylight = mTimeSettings.Daylight;\r | |
153 | \r | |
2ef2b01e A |
154 | return LibGetTime (Time, Capabilities);\r |
155 | }\r | |
156 | \r | |
157 | \r | |
158 | \r | |
159 | /**\r | |
160 | Sets the current local time and date information.\r | |
161 | \r | |
162 | @param Time A pointer to the current time.\r | |
163 | \r | |
164 | @retval EFI_SUCCESS The operation completed successfully.\r | |
165 | @retval EFI_INVALID_PARAMETER A time field is out of range.\r | |
166 | @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.\r | |
167 | \r | |
168 | **/\r | |
169 | EFI_STATUS\r | |
170 | EFIAPI\r | |
171 | SetTime (\r | |
172 | IN EFI_TIME *Time\r | |
173 | )\r | |
174 | {\r | |
517cf877 AB |
175 | EFI_STATUS Status;\r |
176 | BOOLEAN TimeSettingsChanged;\r | |
177 | \r | |
178 | if (Time == NULL || !IsTimeValid (Time)) {\r | |
179 | return EFI_INVALID_PARAMETER;\r | |
180 | }\r | |
181 | \r | |
182 | TimeSettingsChanged = FALSE;\r | |
183 | if (mTimeSettings.TimeZone != Time->TimeZone ||\r | |
184 | mTimeSettings.Daylight != Time->Daylight) {\r | |
185 | \r | |
186 | mTimeSettings.TimeZone = Time->TimeZone;\r | |
187 | mTimeSettings.Daylight = Time->Daylight;\r | |
188 | TimeSettingsChanged = TRUE;\r | |
189 | }\r | |
190 | \r | |
191 | Status = LibSetTime (Time);\r | |
192 | if (EFI_ERROR (Status)) {\r | |
193 | return Status;\r | |
194 | }\r | |
195 | \r | |
196 | if (TimeSettingsChanged) {\r | |
197 | Status = EfiSetVariable (\r | |
198 | (CHAR16 *)mTimeSettingsVariableName,\r | |
199 | &gEfiCallerIdGuid,\r | |
200 | EFI_VARIABLE_NON_VOLATILE |\r | |
201 | EFI_VARIABLE_BOOTSERVICE_ACCESS |\r | |
202 | EFI_VARIABLE_RUNTIME_ACCESS,\r | |
203 | sizeof (mTimeSettings),\r | |
204 | (VOID *)&mTimeSettings);\r | |
205 | if (EFI_ERROR (Status)) {\r | |
206 | return EFI_DEVICE_ERROR;\r | |
207 | }\r | |
208 | }\r | |
209 | return EFI_SUCCESS;\r | |
2ef2b01e A |
210 | }\r |
211 | \r | |
212 | \r | |
213 | /**\r | |
214 | Returns the current wakeup alarm clock setting.\r | |
215 | \r | |
216 | @param Enabled Indicates if the alarm is currently enabled or disabled.\r | |
217 | @param Pending Indicates if the alarm signal is pending and requires acknowledgement.\r | |
218 | @param Time The current alarm setting.\r | |
219 | \r | |
220 | @retval EFI_SUCCESS The alarm settings were returned.\r | |
221 | @retval EFI_INVALID_PARAMETER Any parameter is NULL.\r | |
222 | @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.\r | |
223 | \r | |
224 | **/\r | |
225 | EFI_STATUS\r | |
226 | EFIAPI\r | |
227 | GetWakeupTime (\r | |
228 | OUT BOOLEAN *Enabled,\r | |
229 | OUT BOOLEAN *Pending,\r | |
230 | OUT EFI_TIME *Time\r | |
231 | )\r | |
232 | {\r | |
233 | return LibGetWakeupTime (Enabled, Pending, Time);\r | |
234 | }\r | |
235 | \r | |
236 | \r | |
237 | /**\r | |
238 | Sets the system wakeup alarm clock time.\r | |
239 | \r | |
240 | @param Enabled Enable or disable the wakeup alarm.\r | |
241 | @param Time If Enable is TRUE, the time to set the wakeup alarm for.\r | |
242 | \r | |
243 | @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If\r | |
244 | Enable is FALSE, then the wakeup alarm was disabled.\r | |
245 | @retval EFI_INVALID_PARAMETER A time field is out of range.\r | |
246 | @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.\r | |
247 | @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.\r | |
248 | \r | |
249 | **/\r | |
250 | EFI_STATUS\r | |
251 | EFIAPI\r | |
252 | SetWakeupTime (\r | |
253 | IN BOOLEAN Enabled,\r | |
254 | OUT EFI_TIME *Time\r | |
255 | )\r | |
256 | {\r | |
257 | return LibSetWakeupTime (Enabled, Time);\r | |
258 | }\r | |
259 | \r | |
260 | \r | |
261 | \r | |
262 | /**\r | |
263 | This is the declaration of an EFI image entry point. This can be the entry point to an application\r | |
264 | written to this specification, an EFI boot service driver, or an EFI runtime driver.\r | |
265 | \r | |
266 | @param ImageHandle Handle that identifies the loaded image.\r | |
267 | @param SystemTable System Table for this image.\r | |
268 | \r | |
269 | @retval EFI_SUCCESS The operation completed successfully.\r | |
270 | \r | |
271 | **/\r | |
272 | EFI_STATUS\r | |
273 | EFIAPI\r | |
274 | InitializeRealTimeClock (\r | |
275 | IN EFI_HANDLE ImageHandle,\r | |
276 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
277 | )\r | |
278 | {\r | |
279 | EFI_STATUS Status;\r | |
517cf877 | 280 | UINTN Size;\r |
2ef2b01e | 281 | \r |
da6ae666 LD |
282 | Status = LibRtcInitialize (ImageHandle, SystemTable);\r |
283 | if (EFI_ERROR (Status)) {\r | |
284 | return Status;\r | |
285 | }\r | |
2ef2b01e | 286 | \r |
517cf877 AB |
287 | Size = sizeof (mTimeSettings);\r |
288 | Status = EfiGetVariable ((CHAR16 *)mTimeSettingsVariableName,\r | |
289 | &gEfiCallerIdGuid, NULL, &Size, (VOID *)&mTimeSettings);\r | |
290 | if (EFI_ERROR (Status) ||\r | |
291 | !IsValidTimeZone (mTimeSettings.TimeZone) ||\r | |
292 | !IsValidDaylight (mTimeSettings.Daylight)) {\r | |
293 | DEBUG ((DEBUG_WARN, "%a: using default timezone/daylight settings\n",\r | |
294 | __FUNCTION__));\r | |
295 | \r | |
296 | mTimeSettings.TimeZone = EFI_UNSPECIFIED_TIMEZONE;\r | |
297 | mTimeSettings.Daylight = 0;\r | |
298 | }\r | |
299 | \r | |
2ef2b01e A |
300 | SystemTable->RuntimeServices->GetTime = GetTime;\r |
301 | SystemTable->RuntimeServices->SetTime = SetTime;\r | |
302 | SystemTable->RuntimeServices->GetWakeupTime = GetWakeupTime;\r | |
303 | SystemTable->RuntimeServices->SetWakeupTime = SetWakeupTime;\r | |
304 | \r | |
305 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
306 | &mHandle,\r | |
307 | &gEfiRealTimeClockArchProtocolGuid,\r | |
308 | NULL,\r | |
309 | NULL\r | |
310 | );\r | |
311 | \r | |
312 | return Status;\r | |
313 | }\r | |
314 | \r |