]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - EmbeddedPkg/RealTimeClockRuntimeDxe/RealTimeClock.c
DynamicTablesPkg: Update FADT generator to ACPI 6.3
[mirror_edk2.git] / EmbeddedPkg / RealTimeClockRuntimeDxe / RealTimeClock.c
... / ...
CommitLineData
1/** @file\r
2 Implement EFI RealTimeClock runtime services via RTC Lib.\r
3\r
4 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r
5 Copyright (c) 2017, Linaro, Ltd. All rights reserved.<BR>\r
6\r
7 SPDX-License-Identifier: BSD-2-Clause-Patent\r
8\r
9**/\r
10\r
11#include <PiDxe.h>\r
12#include <Library/DebugLib.h>\r
13#include <Library/RealTimeClockLib.h>\r
14#include <Library/UefiLib.h>\r
15#include <Library/UefiBootServicesTableLib.h>\r
16#include <Library/UefiRuntimeLib.h>\r
17#include <Protocol/RealTimeClock.h>\r
18\r
19EFI_HANDLE mHandle = NULL;\r
20\r
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
26typedef struct {\r
27 INT16 TimeZone;\r
28 UINT8 Daylight;\r
29} NON_VOLATILE_TIME_SETTINGS;\r
30\r
31STATIC CONST CHAR16 mTimeSettingsVariableName[] = L"RtcTimeSettings";\r
32STATIC NON_VOLATILE_TIME_SETTINGS mTimeSettings;\r
33\r
34STATIC\r
35BOOLEAN\r
36IsValidTimeZone (\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
44STATIC\r
45BOOLEAN\r
46IsValidDaylight (\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
54\r
55STATIC\r
56BOOLEAN\r
57EFIAPI\r
58IsLeapYear (\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
77STATIC CONST INTN mDayOfMonth[12] = {\r
78 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31\r
79};\r
80\r
81STATIC\r
82BOOLEAN\r
83EFIAPI\r
84IsDayValid (\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
100STATIC\r
101BOOLEAN\r
102EFIAPI\r
103IsTimeValid(\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
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
136EFI_STATUS\r
137EFIAPI\r
138GetTime (\r
139 OUT EFI_TIME *Time,\r
140 OUT EFI_TIME_CAPABILITIES *Capabilities\r
141 )\r
142{\r
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
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
169EFI_STATUS\r
170EFIAPI\r
171SetTime (\r
172 IN EFI_TIME *Time\r
173 )\r
174{\r
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
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
225EFI_STATUS\r
226EFIAPI\r
227GetWakeupTime (\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
250EFI_STATUS\r
251EFIAPI\r
252SetWakeupTime (\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
272EFI_STATUS\r
273EFIAPI\r
274InitializeRealTimeClock (\r
275 IN EFI_HANDLE ImageHandle,\r
276 IN EFI_SYSTEM_TABLE *SystemTable\r
277 )\r
278{\r
279 EFI_STATUS Status;\r
280 UINTN Size;\r
281\r
282 Status = LibRtcInitialize (ImageHandle, SystemTable);\r
283 if (EFI_ERROR (Status)) {\r
284 return Status;\r
285 }\r
286\r
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
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