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