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