]> git.proxmox.com Git - mirror_edk2.git/blame - EmbeddedPkg/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c
EmbeddedPkg/VirtualRealTimeClockLib: Fix SetTime issues
[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
8e6bb64f 7 * Copyright (c) 2011-2021, ARM Ltd. All rights reserved.\r
64a17fad
PB
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 90 DEBUG ((\r
94fa95c8 91 DEBUG_INFO,\r
64a17fad
PB
92 "LibGetTime: %s non volatile variable was not found - Using compilation time epoch.\n",\r
93 mEpochVariableName\r
94 ));\r
94fa95c8
RC
95\r
96 EfiSetVariable (\r
97 (CHAR16 *)mEpochVariableName,\r
98 &gEfiCallerIdGuid,\r
8e6bb64f 99 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
94fa95c8
RC
100 sizeof (EpochSeconds),\r
101 &EpochSeconds\r
102 );\r
64a17fad
PB
103 }\r
104 Counter = GetPerformanceCounter ();\r
1342d767 105 EpochSeconds += DivU64x64Remainder (Counter, Freq, &Remainder);\r
64a17fad
PB
106\r
107 // Get the current time zone information from non-volatile storage\r
108 Size = sizeof (TimeZone);\r
109 Status = EfiGetVariable (\r
110 (CHAR16 *)mTimeZoneVariableName,\r
111 &gEfiCallerIdGuid,\r
112 NULL,\r
113 &Size,\r
114 (VOID *)&TimeZone\r
115 );\r
116\r
117 if (EFI_ERROR (Status)) {\r
118 ASSERT(Status != EFI_INVALID_PARAMETER);\r
119 ASSERT(Status != EFI_BUFFER_TOO_SMALL);\r
120\r
121 if (Status != EFI_NOT_FOUND) {\r
122 return Status;\r
123 }\r
124\r
125 // The time zone variable does not exist in non-volatile storage, so create it.\r
126 Time->TimeZone = EFI_UNSPECIFIED_TIMEZONE;\r
127 // Store it\r
128 Status = EfiSetVariable (\r
129 (CHAR16 *)mTimeZoneVariableName,\r
130 &gEfiCallerIdGuid,\r
131 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
132 Size,\r
133 (VOID *)&(Time->TimeZone)\r
134 );\r
135 if (EFI_ERROR (Status)) {\r
136 DEBUG ((\r
137 DEBUG_ERROR,\r
138 "LibGetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",\r
139 mTimeZoneVariableName,\r
140 Status\r
141 ));\r
142 return Status;\r
143 }\r
144 } else {\r
145 // Got the time zone\r
146 Time->TimeZone = TimeZone;\r
147\r
148 // Check TimeZone bounds: -1440 to 1440 or 2047\r
149 if (((Time->TimeZone < -1440) || (Time->TimeZone > 1440))\r
150 && (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE)) {\r
151 Time->TimeZone = EFI_UNSPECIFIED_TIMEZONE;\r
152 }\r
153\r
154 // Adjust for the correct time zone\r
155 if (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE) {\r
156 EpochSeconds += Time->TimeZone * SEC_PER_MIN;\r
157 }\r
158 }\r
159\r
160 // Get the current daylight information from non-volatile storage\r
161 Size = sizeof (Daylight);\r
162 Status = EfiGetVariable (\r
163 (CHAR16 *)mDaylightVariableName,\r
164 &gEfiCallerIdGuid,\r
165 NULL,\r
166 &Size,\r
167 (VOID *)&Daylight\r
168 );\r
169\r
170 if (EFI_ERROR (Status)) {\r
171 ASSERT(Status != EFI_INVALID_PARAMETER);\r
172 ASSERT(Status != EFI_BUFFER_TOO_SMALL);\r
173\r
174 if (Status != EFI_NOT_FOUND) {\r
175 return Status;\r
176 }\r
177\r
178 // The daylight variable does not exist in non-volatile storage, so create it.\r
179 Time->Daylight = 0;\r
180 // Store it\r
181 Status = EfiSetVariable (\r
182 (CHAR16 *)mDaylightVariableName,\r
183 &gEfiCallerIdGuid,\r
184 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
185 Size,\r
186 (VOID *)&(Time->Daylight)\r
187 );\r
188 if (EFI_ERROR (Status)) {\r
189 DEBUG ((\r
190 DEBUG_ERROR,\r
191 "LibGetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",\r
192 mDaylightVariableName,\r
193 Status\r
194 ));\r
195 return Status;\r
196 }\r
197 } else {\r
198 // Got the daylight information\r
199 Time->Daylight = Daylight;\r
200\r
201 // Adjust for the correct period\r
202 if ((Time->Daylight & EFI_TIME_IN_DAYLIGHT) == EFI_TIME_IN_DAYLIGHT) {\r
203 // Convert to adjusted time, i.e. spring forwards one hour\r
204 EpochSeconds += SEC_PER_HOUR;\r
205 }\r
206 }\r
207\r
1342d767 208 EpochToEfiTime (EpochSeconds, Time);\r
64a17fad
PB
209\r
210 // Because we use the performance counter, we can fill the Nanosecond attribute\r
211 // provided that the remainder doesn't overflow 64-bit during multiplication.\r
212 if (Remainder <= 18446744073U) {\r
24cf7272 213 Time->Nanosecond = (UINT32)(MultU64x64 (Remainder, 1000000000U) / Freq);\r
64a17fad
PB
214 } else {\r
215 DEBUG ((DEBUG_WARN, "LibGetTime: Nanosecond value not set (64-bit overflow).\n"));\r
216 }\r
217\r
218 if (Capabilities) {\r
219 Capabilities->Accuracy = 0;\r
24cf7272 220 Capabilities->Resolution = 1;\r
64a17fad
PB
221 Capabilities->SetsToZero = FALSE;\r
222 }\r
223\r
224 return EFI_SUCCESS;\r
225}\r
226\r
227/**\r
228 Sets the current local time and date information.\r
229\r
230 @param Time A pointer to the current time.\r
231\r
232 @retval EFI_SUCCESS The operation completed successfully.\r
233 @retval EFI_INVALID_PARAMETER A time field is out of range.\r
234 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.\r
235\r
236**/\r
237EFI_STATUS\r
238EFIAPI\r
239LibSetTime (\r
240 IN EFI_TIME *Time\r
241 )\r
242{\r
243 EFI_STATUS Status;\r
1342d767
PB
244 UINT64 Freq;\r
245 UINT64 Counter;\r
246 UINT64 Remainder;\r
64a17fad
PB
247 UINTN EpochSeconds;\r
248\r
249 if (!IsTimeValid (Time)) {\r
250 return EFI_INVALID_PARAMETER;\r
251 }\r
252\r
253 EpochSeconds = EfiTimeToEpoch (Time);\r
254\r
255 // Adjust for the correct time zone, i.e. convert to UTC time zone\r
1342d767
PB
256 if ((Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE)\r
257 && (EpochSeconds > Time->TimeZone * SEC_PER_MIN)) {\r
64a17fad
PB
258 EpochSeconds -= Time->TimeZone * SEC_PER_MIN;\r
259 }\r
260\r
261 // Adjust for the correct period\r
1342d767
PB
262 if (((Time->Daylight & EFI_TIME_IN_DAYLIGHT) == EFI_TIME_IN_DAYLIGHT)\r
263 && (EpochSeconds > SEC_PER_HOUR)) {\r
64a17fad
PB
264 // Convert to un-adjusted time, i.e. fall back one hour\r
265 EpochSeconds -= SEC_PER_HOUR;\r
266 }\r
267\r
1342d767
PB
268 // Use the performance counter to subtract the number of seconds\r
269 // since platform reset. Without this, setting time from the shell\r
270 // and immediately reading it back would result in a forward time\r
271 // offset, of the duration during which the platform has been up.\r
272 Freq = GetPerformanceCounterProperties (NULL, NULL);\r
273 if (Freq != 0) {\r
274 Counter = GetPerformanceCounter ();\r
275 if (EpochSeconds > DivU64x64Remainder (Counter, Freq, &Remainder)) {\r
276 EpochSeconds -= DivU64x64Remainder (Counter, Freq, &Remainder);\r
277 }\r
278 }\r
279\r
64a17fad
PB
280 // Save the current time zone information into non-volatile storage\r
281 Status = EfiSetVariable (\r
282 (CHAR16 *)mTimeZoneVariableName,\r
283 &gEfiCallerIdGuid,\r
284 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
285 sizeof (Time->TimeZone),\r
286 (VOID *)&(Time->TimeZone)\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 mTimeZoneVariableName,\r
293 Status\r
294 ));\r
295 return Status;\r
296 }\r
297\r
298 // Save the current daylight information into non-volatile storage\r
299 Status = EfiSetVariable (\r
300 (CHAR16 *)mDaylightVariableName,\r
301 &gEfiCallerIdGuid,\r
302 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
303 sizeof(Time->Daylight),\r
304 (VOID *)&(Time->Daylight)\r
305 );\r
306 if (EFI_ERROR (Status)) {\r
307 DEBUG ((\r
308 DEBUG_ERROR,\r
309 "LibSetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",\r
310 mDaylightVariableName,\r
311 Status\r
312 ));\r
313 return Status;\r
314 }\r
315\r
316 Status = EfiSetVariable (\r
317 (CHAR16 *)mEpochVariableName,\r
318 &gEfiCallerIdGuid,\r
319 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
320 sizeof (EpochSeconds),\r
321 &EpochSeconds\r
322 );\r
323 if (EFI_ERROR (Status)) {\r
324 DEBUG ((\r
325 DEBUG_ERROR,\r
326 "LibSetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",\r
8e6bb64f 327 mEpochVariableName,\r
64a17fad
PB
328 Status\r
329 ));\r
330 return Status;\r
331 }\r
332\r
333 return EFI_SUCCESS;\r
334}\r
335\r
336/**\r
337 Returns the current wakeup alarm clock setting.\r
338\r
339 @param Enabled Indicates if the alarm is currently enabled or disabled.\r
340 @param Pending Indicates if the alarm signal is pending and requires acknowledgement.\r
341 @param Time The current alarm setting.\r
342\r
343 @retval EFI_SUCCESS The alarm settings were returned.\r
344 @retval EFI_INVALID_PARAMETER Any parameter is NULL.\r
345 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.\r
346\r
347**/\r
348EFI_STATUS\r
349EFIAPI\r
350LibGetWakeupTime (\r
351 OUT BOOLEAN *Enabled,\r
352 OUT BOOLEAN *Pending,\r
353 OUT EFI_TIME *Time\r
354 )\r
355{\r
356 return EFI_UNSUPPORTED;\r
357}\r
358\r
359/**\r
360 Sets the system wakeup alarm clock time.\r
361\r
362 @param Enabled Enable or disable the wakeup alarm.\r
363 @param Time If Enable is TRUE, the time to set the wakeup alarm for.\r
364\r
365 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If\r
366 Enable is FALSE, then the wakeup alarm was disabled.\r
367 @retval EFI_INVALID_PARAMETER A time field is out of range.\r
368 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.\r
369 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.\r
370\r
371**/\r
372EFI_STATUS\r
373EFIAPI\r
374LibSetWakeupTime (\r
375 IN BOOLEAN Enabled,\r
376 OUT EFI_TIME *Time\r
377 )\r
378{\r
379 return EFI_UNSUPPORTED;\r
380}\r
381\r
382/**\r
383 This is the declaration of an EFI image entry point. This can be the entry point to an application\r
384 written to this specification, an EFI boot service driver, or an EFI runtime driver.\r
385\r
386 @param ImageHandle Handle that identifies the loaded image.\r
387 @param SystemTable System Table for this image.\r
388\r
389 @retval EFI_SUCCESS The operation completed successfully.\r
390\r
391**/\r
392EFI_STATUS\r
393EFIAPI\r
394LibRtcInitialize (\r
395 IN EFI_HANDLE ImageHandle,\r
396 IN EFI_SYSTEM_TABLE *SystemTable\r
397 )\r
398{\r
399 return EFI_SUCCESS;\r
400}\r
401\r
402/**\r
403 Fixup internal data so that EFI can be call in virtual mode.\r
404 Call the passed in Child Notify event and convert any pointers in\r
405 lib to virtual mode.\r
406\r
407 @param[in] Event The Event that is being processed\r
408 @param[in] Context Event Context\r
409**/\r
410VOID\r
411EFIAPI\r
412LibRtcVirtualNotifyEvent (\r
413 IN EFI_EVENT Event,\r
414 IN VOID *Context\r
415 )\r
416{\r
417 return;\r
418}\r