]> git.proxmox.com Git - mirror_edk2.git/blame - EmbeddedPkg/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c
BaseTools:PackageDocumentTools import lib error occurs.
[mirror_edk2.git] / EmbeddedPkg / Library / VirtualRealTimeClockLib / VirtualRealTimeClockLib.c
CommitLineData
64a17fad
PB
1/** @file\r
2 *\r
3 * Implement virtual EFI RealTimeClock runtime services.\r
4 *\r
5 * Coypright (c) 2019, Pete Batard <pete@akeo.ie>\r
6 * Copyright (c) 2018, Andrei Warkentin <andrey.warkentin@gmail.com>\r
7 * Copyright (c) 2011-2014, ARM Ltd. All rights reserved.\r
8 * Copyright (c) 2008-2010, Apple Inc. All rights reserved.\r
9 * Copyright (c) Microsoft Corporation. All rights reserved.\r
10 *\r
11 * This program and the accompanying materials\r
12 * are licensed and made available under the terms and conditions of the BSD License\r
13 * which accompanies this distribution. The full text of the license may be found at\r
14 * http://opensource.org/licenses/bsd-license.php\r
15 *\r
16 * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
17 * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
18 *\r
19 * Based on ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.inf\r
20 *\r
21 **/\r
22\r
23#include <PiDxe.h>\r
24#include <Library/BaseLib.h>\r
25#include <Library/DebugLib.h>\r
26#include <Library/IoLib.h>\r
27#include <Library/RealTimeClockLib.h>\r
28#include <Library/TimerLib.h>\r
29#include <Library/TimeBaseLib.h>\r
30#include <Library/UefiRuntimeLib.h>\r
31\r
32STATIC CONST CHAR16 mEpochVariableName[] = L"RtcEpochSeconds";\r
33STATIC CONST CHAR16 mTimeZoneVariableName[] = L"RtcTimeZone";\r
34STATIC CONST CHAR16 mDaylightVariableName[] = L"RtcDaylight";\r
35\r
36/**\r
37 Returns the current time and date information, and the time-keeping capabilities\r
38 of the virtual RTC.\r
39\r
40 @param Time A pointer to storage to receive a snapshot of the current time.\r
41 @param Capabilities An optional pointer to a buffer to receive the real time clock\r
42 device's capabilities.\r
43\r
44 @retval EFI_SUCCESS The operation completed successfully.\r
45 @retval EFI_INVALID_PARAMETER Time is NULL.\r
46 @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.\r
47\r
48**/\r
49EFI_STATUS\r
50EFIAPI\r
51LibGetTime (\r
52 OUT EFI_TIME *Time,\r
53 OUT EFI_TIME_CAPABILITIES *Capabilities\r
54 )\r
55{\r
56 EFI_STATUS Status;\r
57 UINT32 EpochSeconds;\r
58 INT16 TimeZone;\r
59 UINT8 Daylight;\r
60 UINT64 Freq;\r
61 UINT64 Counter;\r
62 UINT64 Remainder;\r
63 UINTN ElapsedSeconds;\r
64 UINTN Size;\r
65\r
66 if (Time == NULL) {\r
67 return EFI_INVALID_PARAMETER;\r
68 }\r
69\r
70 // Get the counter frequency\r
71 Freq = GetPerformanceCounterProperties (NULL, NULL);\r
72 if (Freq == 0) {\r
73 return EFI_DEVICE_ERROR;\r
74 }\r
75\r
76 // Get the epoch time from non-volatile storage\r
77 Size = sizeof (UINTN);\r
78 ElapsedSeconds = 0;\r
79 Status = EfiGetVariable (\r
80 (CHAR16 *)mEpochVariableName,\r
81 &gEfiCallerIdGuid,\r
82 NULL,\r
83 &Size,\r
84 (VOID *)&ElapsedSeconds\r
85 );\r
86 // Fall back to compilation-time epoch if not set\r
87 if (EFI_ERROR (Status)) {\r
88 ASSERT(Status != EFI_INVALID_PARAMETER);\r
89 ASSERT(Status != EFI_BUFFER_TOO_SMALL);\r
90 //\r
91 // The following is intended to produce a compilation error on build\r
92 // environments where BUILD_EPOCH can not be set from inline shell.\r
93 // If you are attempting to use this library on such an environment, please\r
94 // contact the edk2 mailing list, so we can try to add support for it.\r
95 //\r
96 ElapsedSeconds = BUILD_EPOCH;\r
97 DEBUG ((\r
98 DEBUG_INFO,\r
99 "LibGetTime: %s non volatile variable was not found - Using compilation time epoch.\n",\r
100 mEpochVariableName\r
101 ));\r
102 }\r
103 Counter = GetPerformanceCounter ();\r
104 ElapsedSeconds += DivU64x64Remainder (Counter, Freq, &Remainder);\r
105\r
106 // Get the current time zone information from non-volatile storage\r
107 Size = sizeof (TimeZone);\r
108 Status = EfiGetVariable (\r
109 (CHAR16 *)mTimeZoneVariableName,\r
110 &gEfiCallerIdGuid,\r
111 NULL,\r
112 &Size,\r
113 (VOID *)&TimeZone\r
114 );\r
115\r
116 if (EFI_ERROR (Status)) {\r
117 ASSERT(Status != EFI_INVALID_PARAMETER);\r
118 ASSERT(Status != EFI_BUFFER_TOO_SMALL);\r
119\r
120 if (Status != EFI_NOT_FOUND) {\r
121 return Status;\r
122 }\r
123\r
124 // The time zone variable does not exist in non-volatile storage, so create it.\r
125 Time->TimeZone = EFI_UNSPECIFIED_TIMEZONE;\r
126 // Store it\r
127 Status = EfiSetVariable (\r
128 (CHAR16 *)mTimeZoneVariableName,\r
129 &gEfiCallerIdGuid,\r
130 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
131 Size,\r
132 (VOID *)&(Time->TimeZone)\r
133 );\r
134 if (EFI_ERROR (Status)) {\r
135 DEBUG ((\r
136 DEBUG_ERROR,\r
137 "LibGetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",\r
138 mTimeZoneVariableName,\r
139 Status\r
140 ));\r
141 return Status;\r
142 }\r
143 } else {\r
144 // Got the time zone\r
145 Time->TimeZone = TimeZone;\r
146\r
147 // Check TimeZone bounds: -1440 to 1440 or 2047\r
148 if (((Time->TimeZone < -1440) || (Time->TimeZone > 1440))\r
149 && (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE)) {\r
150 Time->TimeZone = EFI_UNSPECIFIED_TIMEZONE;\r
151 }\r
152\r
153 // Adjust for the correct time zone\r
154 if (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE) {\r
155 EpochSeconds += Time->TimeZone * SEC_PER_MIN;\r
156 }\r
157 }\r
158\r
159 // Get the current daylight information from non-volatile storage\r
160 Size = sizeof (Daylight);\r
161 Status = EfiGetVariable (\r
162 (CHAR16 *)mDaylightVariableName,\r
163 &gEfiCallerIdGuid,\r
164 NULL,\r
165 &Size,\r
166 (VOID *)&Daylight\r
167 );\r
168\r
169 if (EFI_ERROR (Status)) {\r
170 ASSERT(Status != EFI_INVALID_PARAMETER);\r
171 ASSERT(Status != EFI_BUFFER_TOO_SMALL);\r
172\r
173 if (Status != EFI_NOT_FOUND) {\r
174 return Status;\r
175 }\r
176\r
177 // The daylight variable does not exist in non-volatile storage, so create it.\r
178 Time->Daylight = 0;\r
179 // Store it\r
180 Status = EfiSetVariable (\r
181 (CHAR16 *)mDaylightVariableName,\r
182 &gEfiCallerIdGuid,\r
183 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
184 Size,\r
185 (VOID *)&(Time->Daylight)\r
186 );\r
187 if (EFI_ERROR (Status)) {\r
188 DEBUG ((\r
189 DEBUG_ERROR,\r
190 "LibGetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",\r
191 mDaylightVariableName,\r
192 Status\r
193 ));\r
194 return Status;\r
195 }\r
196 } else {\r
197 // Got the daylight information\r
198 Time->Daylight = Daylight;\r
199\r
200 // Adjust for the correct period\r
201 if ((Time->Daylight & EFI_TIME_IN_DAYLIGHT) == EFI_TIME_IN_DAYLIGHT) {\r
202 // Convert to adjusted time, i.e. spring forwards one hour\r
203 EpochSeconds += SEC_PER_HOUR;\r
204 }\r
205 }\r
206\r
207 EpochToEfiTime (ElapsedSeconds, Time);\r
208\r
209 // Because we use the performance counter, we can fill the Nanosecond attribute\r
210 // provided that the remainder doesn't overflow 64-bit during multiplication.\r
211 if (Remainder <= 18446744073U) {\r
212 Time->Nanosecond = MultU64x64 (Remainder, 1000000000U) / Freq;\r
213 } else {\r
214 DEBUG ((DEBUG_WARN, "LibGetTime: Nanosecond value not set (64-bit overflow).\n"));\r
215 }\r
216\r
217 if (Capabilities) {\r
218 Capabilities->Accuracy = 0;\r
219 Capabilities->Resolution = Freq;\r
220 Capabilities->SetsToZero = FALSE;\r
221 }\r
222\r
223 return EFI_SUCCESS;\r
224}\r
225\r
226/**\r
227 Sets the current local time and date information.\r
228\r
229 @param Time A pointer to the current time.\r
230\r
231 @retval EFI_SUCCESS The operation completed successfully.\r
232 @retval EFI_INVALID_PARAMETER A time field is out of range.\r
233 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.\r
234\r
235**/\r
236EFI_STATUS\r
237EFIAPI\r
238LibSetTime (\r
239 IN EFI_TIME *Time\r
240 )\r
241{\r
242 EFI_STATUS Status;\r
243 UINTN EpochSeconds;\r
244\r
245 if (!IsTimeValid (Time)) {\r
246 return EFI_INVALID_PARAMETER;\r
247 }\r
248\r
249 EpochSeconds = EfiTimeToEpoch (Time);\r
250\r
251 // Adjust for the correct time zone, i.e. convert to UTC time zone\r
252 if (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE) {\r
253 EpochSeconds -= Time->TimeZone * SEC_PER_MIN;\r
254 }\r
255\r
256 // Adjust for the correct period\r
257 if ((Time->Daylight & EFI_TIME_IN_DAYLIGHT) == EFI_TIME_IN_DAYLIGHT) {\r
258 // Convert to un-adjusted time, i.e. fall back one hour\r
259 EpochSeconds -= SEC_PER_HOUR;\r
260 }\r
261\r
262 // Save the current time zone information into non-volatile storage\r
263 Status = EfiSetVariable (\r
264 (CHAR16 *)mTimeZoneVariableName,\r
265 &gEfiCallerIdGuid,\r
266 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
267 sizeof (Time->TimeZone),\r
268 (VOID *)&(Time->TimeZone)\r
269 );\r
270 if (EFI_ERROR (Status)) {\r
271 DEBUG ((\r
272 DEBUG_ERROR,\r
273 "LibSetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",\r
274 mTimeZoneVariableName,\r
275 Status\r
276 ));\r
277 return Status;\r
278 }\r
279\r
280 // Save the current daylight information into non-volatile storage\r
281 Status = EfiSetVariable (\r
282 (CHAR16 *)mDaylightVariableName,\r
283 &gEfiCallerIdGuid,\r
284 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
285 sizeof(Time->Daylight),\r
286 (VOID *)&(Time->Daylight)\r
287 );\r
288 if (EFI_ERROR (Status)) {\r
289 DEBUG ((\r
290 DEBUG_ERROR,\r
291 "LibSetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",\r
292 mDaylightVariableName,\r
293 Status\r
294 ));\r
295 return Status;\r
296 }\r
297\r
298 Status = EfiSetVariable (\r
299 (CHAR16 *)mEpochVariableName,\r
300 &gEfiCallerIdGuid,\r
301 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
302 sizeof (EpochSeconds),\r
303 &EpochSeconds\r
304 );\r
305 if (EFI_ERROR (Status)) {\r
306 DEBUG ((\r
307 DEBUG_ERROR,\r
308 "LibSetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",\r
309 mDaylightVariableName,\r
310 Status\r
311 ));\r
312 return Status;\r
313 }\r
314\r
315 return EFI_SUCCESS;\r
316}\r
317\r
318/**\r
319 Returns the current wakeup alarm clock setting.\r
320\r
321 @param Enabled Indicates if the alarm is currently enabled or disabled.\r
322 @param Pending Indicates if the alarm signal is pending and requires acknowledgement.\r
323 @param Time The current alarm setting.\r
324\r
325 @retval EFI_SUCCESS The alarm settings were returned.\r
326 @retval EFI_INVALID_PARAMETER Any parameter is NULL.\r
327 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.\r
328\r
329**/\r
330EFI_STATUS\r
331EFIAPI\r
332LibGetWakeupTime (\r
333 OUT BOOLEAN *Enabled,\r
334 OUT BOOLEAN *Pending,\r
335 OUT EFI_TIME *Time\r
336 )\r
337{\r
338 return EFI_UNSUPPORTED;\r
339}\r
340\r
341/**\r
342 Sets the system wakeup alarm clock time.\r
343\r
344 @param Enabled Enable or disable the wakeup alarm.\r
345 @param Time If Enable is TRUE, the time to set the wakeup alarm for.\r
346\r
347 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If\r
348 Enable is FALSE, then the wakeup alarm was disabled.\r
349 @retval EFI_INVALID_PARAMETER A time field is out of range.\r
350 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.\r
351 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.\r
352\r
353**/\r
354EFI_STATUS\r
355EFIAPI\r
356LibSetWakeupTime (\r
357 IN BOOLEAN Enabled,\r
358 OUT EFI_TIME *Time\r
359 )\r
360{\r
361 return EFI_UNSUPPORTED;\r
362}\r
363\r
364/**\r
365 This is the declaration of an EFI image entry point. This can be the entry point to an application\r
366 written to this specification, an EFI boot service driver, or an EFI runtime driver.\r
367\r
368 @param ImageHandle Handle that identifies the loaded image.\r
369 @param SystemTable System Table for this image.\r
370\r
371 @retval EFI_SUCCESS The operation completed successfully.\r
372\r
373**/\r
374EFI_STATUS\r
375EFIAPI\r
376LibRtcInitialize (\r
377 IN EFI_HANDLE ImageHandle,\r
378 IN EFI_SYSTEM_TABLE *SystemTable\r
379 )\r
380{\r
381 return EFI_SUCCESS;\r
382}\r
383\r
384/**\r
385 Fixup internal data so that EFI can be call in virtual mode.\r
386 Call the passed in Child Notify event and convert any pointers in\r
387 lib to virtual mode.\r
388\r
389 @param[in] Event The Event that is being processed\r
390 @param[in] Context Event Context\r
391**/\r
392VOID\r
393EFIAPI\r
394LibRtcVirtualNotifyEvent (\r
395 IN EFI_EVENT Event,\r
396 IN VOID *Context\r
397 )\r
398{\r
399 return;\r
400}\r