]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - EmbeddedPkg/RealTimeClockRuntimeDxe/RealTimeClock.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[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 if (Time->Day < 1 ||\r
89 Time->Day > mDayOfMonth[Time->Month - 1] ||\r
90 (Time->Month == 2 && !IsLeapYear (Time) && Time->Day > 28)) {\r
91 return FALSE;\r
92 }\r
93 return TRUE;\r
94}\r
95\r
96STATIC\r
97BOOLEAN\r
98EFIAPI\r
99IsTimeValid(\r
100 IN EFI_TIME *Time\r
101 )\r
102{\r
103 // Check the input parameters are within the range specified by UEFI\r
104 if (Time->Year < 1900 ||\r
105 Time->Year > 9999 ||\r
106 Time->Month < 1 ||\r
107 Time->Month > 12 ||\r
108 !IsDayValid (Time) ||\r
109 Time->Hour > 23 ||\r
110 Time->Minute > 59 ||\r
111 Time->Second > 59 ||\r
112 Time->Nanosecond > 999999999 ||\r
113 !IsValidTimeZone (Time->TimeZone) ||\r
114 !IsValidDaylight (Time->Daylight)) {\r
115 return FALSE;\r
116 }\r
117 return TRUE;\r
118}\r
119\r
120/**\r
121 Returns the current time and date information, and the time-keeping capabilities\r
122 of the hardware platform.\r
123\r
124 @param Time A pointer to storage to receive a snapshot of the current time.\r
125 @param Capabilities An optional pointer to a buffer to receive the real time clock\r
126 device's capabilities.\r
127\r
128 @retval EFI_SUCCESS The operation completed successfully.\r
129 @retval EFI_INVALID_PARAMETER Time is NULL.\r
130 @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.\r
131\r
132**/\r
133EFI_STATUS\r
134EFIAPI\r
135GetTime (\r
136 OUT EFI_TIME *Time,\r
137 OUT EFI_TIME_CAPABILITIES *Capabilities\r
138 )\r
139{\r
140 if (Time == NULL) {\r
141 return EFI_INVALID_PARAMETER;\r
142 }\r
143\r
144 //\r
145 // Set these first so the RealTimeClockLib implementation\r
146 // can override them based on its own settings.\r
147 //\r
148 Time->TimeZone = mTimeSettings.TimeZone;\r
149 Time->Daylight = mTimeSettings.Daylight;\r
150\r
151 return LibGetTime (Time, Capabilities);\r
152}\r
153\r
154\r
155\r
156/**\r
157 Sets the current local time and date information.\r
158\r
159 @param Time A pointer to the current time.\r
160\r
161 @retval EFI_SUCCESS The operation completed successfully.\r
162 @retval EFI_INVALID_PARAMETER A time field is out of range.\r
163 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.\r
164\r
165**/\r
166EFI_STATUS\r
167EFIAPI\r
168SetTime (\r
169 IN EFI_TIME *Time\r
170 )\r
171{\r
172 EFI_STATUS Status;\r
173 BOOLEAN TimeSettingsChanged;\r
174\r
175 if (Time == NULL || !IsTimeValid (Time)) {\r
176 return EFI_INVALID_PARAMETER;\r
177 }\r
178\r
179 TimeSettingsChanged = FALSE;\r
180 if (mTimeSettings.TimeZone != Time->TimeZone ||\r
181 mTimeSettings.Daylight != Time->Daylight) {\r
182\r
183 mTimeSettings.TimeZone = Time->TimeZone;\r
184 mTimeSettings.Daylight = Time->Daylight;\r
185 TimeSettingsChanged = TRUE;\r
186 }\r
187\r
188 Status = LibSetTime (Time);\r
189 if (EFI_ERROR (Status)) {\r
190 return Status;\r
191 }\r
192\r
193 if (TimeSettingsChanged) {\r
194 Status = EfiSetVariable (\r
195 (CHAR16 *)mTimeSettingsVariableName,\r
196 &gEfiCallerIdGuid,\r
197 EFI_VARIABLE_NON_VOLATILE |\r
198 EFI_VARIABLE_BOOTSERVICE_ACCESS |\r
199 EFI_VARIABLE_RUNTIME_ACCESS,\r
200 sizeof (mTimeSettings),\r
201 (VOID *)&mTimeSettings);\r
202 if (EFI_ERROR (Status)) {\r
203 return EFI_DEVICE_ERROR;\r
204 }\r
205 }\r
206 return EFI_SUCCESS;\r
207}\r
208\r
209\r
210/**\r
211 Returns the current wakeup alarm clock setting.\r
212\r
213 @param Enabled Indicates if the alarm is currently enabled or disabled.\r
214 @param Pending Indicates if the alarm signal is pending and requires acknowledgement.\r
215 @param Time The current alarm setting.\r
216\r
217 @retval EFI_SUCCESS The alarm settings were returned.\r
218 @retval EFI_INVALID_PARAMETER Any parameter is NULL.\r
219 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.\r
220\r
221**/\r
222EFI_STATUS\r
223EFIAPI\r
224GetWakeupTime (\r
225 OUT BOOLEAN *Enabled,\r
226 OUT BOOLEAN *Pending,\r
227 OUT EFI_TIME *Time\r
228 )\r
229{\r
230 return LibGetWakeupTime (Enabled, Pending, Time);\r
231}\r
232\r
233\r
234/**\r
235 Sets the system wakeup alarm clock time.\r
236\r
237 @param Enabled Enable or disable the wakeup alarm.\r
238 @param Time If Enable is TRUE, the time to set the wakeup alarm for.\r
239\r
240 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If\r
241 Enable is FALSE, then the wakeup alarm was disabled.\r
242 @retval EFI_INVALID_PARAMETER A time field is out of range.\r
243 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.\r
244 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.\r
245\r
246**/\r
247EFI_STATUS\r
248EFIAPI\r
249SetWakeupTime (\r
250 IN BOOLEAN Enabled,\r
251 OUT EFI_TIME *Time\r
252 )\r
253{\r
254 return LibSetWakeupTime (Enabled, Time);\r
255}\r
256\r
257\r
258\r
259/**\r
260 This is the declaration of an EFI image entry point. This can be the entry point to an application\r
261 written to this specification, an EFI boot service driver, or an EFI runtime driver.\r
262\r
263 @param ImageHandle Handle that identifies the loaded image.\r
264 @param SystemTable System Table for this image.\r
265\r
266 @retval EFI_SUCCESS The operation completed successfully.\r
267\r
268**/\r
269EFI_STATUS\r
270EFIAPI\r
271InitializeRealTimeClock (\r
272 IN EFI_HANDLE ImageHandle,\r
273 IN EFI_SYSTEM_TABLE *SystemTable\r
274 )\r
275{\r
276 EFI_STATUS Status;\r
277 UINTN Size;\r
278\r
279 Status = LibRtcInitialize (ImageHandle, SystemTable);\r
280 if (EFI_ERROR (Status)) {\r
281 return Status;\r
282 }\r
283\r
284 Size = sizeof (mTimeSettings);\r
285 Status = EfiGetVariable ((CHAR16 *)mTimeSettingsVariableName,\r
286 &gEfiCallerIdGuid, NULL, &Size, (VOID *)&mTimeSettings);\r
287 if (EFI_ERROR (Status) ||\r
288 !IsValidTimeZone (mTimeSettings.TimeZone) ||\r
289 !IsValidDaylight (mTimeSettings.Daylight)) {\r
290 DEBUG ((DEBUG_WARN, "%a: using default timezone/daylight settings\n",\r
291 __FUNCTION__));\r
292\r
293 mTimeSettings.TimeZone = EFI_UNSPECIFIED_TIMEZONE;\r
294 mTimeSettings.Daylight = 0;\r
295 }\r
296\r
297 SystemTable->RuntimeServices->GetTime = GetTime;\r
298 SystemTable->RuntimeServices->SetTime = SetTime;\r
299 SystemTable->RuntimeServices->GetWakeupTime = GetWakeupTime;\r
300 SystemTable->RuntimeServices->SetWakeupTime = SetWakeupTime;\r
301\r
302 Status = gBS->InstallMultipleProtocolInterfaces (\r
303 &mHandle,\r
304 &gEfiRealTimeClockArchProtocolGuid,\r
305 NULL,\r
306 NULL\r
307 );\r
308\r
309 return Status;\r
310}\r
311\r