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