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