]> git.proxmox.com Git - mirror_edk2.git/blob - EmbeddedPkg/RealTimeClockRuntimeDxe/RealTimeClock.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / EmbeddedPkg / RealTimeClockRuntimeDxe / RealTimeClock.c
1 /** @file
2 Implement EFI RealTimeClock runtime services via RTC Lib.
3
4 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
5 Copyright (c) 2017, Linaro, Ltd. All rights reserved.<BR>
6 Copyright (c) 2021, Ampere Computing LLC. All rights reserved.<BR>
7
8 SPDX-License-Identifier: BSD-2-Clause-Patent
9
10 **/
11
12 #include <PiDxe.h>
13 #include <Library/DebugLib.h>
14 #include <Library/RealTimeClockLib.h>
15 #include <Library/TimeBaseLib.h>
16 #include <Library/UefiLib.h>
17 #include <Library/UefiBootServicesTableLib.h>
18 #include <Library/UefiRuntimeLib.h>
19 #include <Protocol/RealTimeClock.h>
20
21 EFI_HANDLE mHandle = NULL;
22
23 //
24 // These values can be set by SetTime () and need to be returned by GetTime ()
25 // but cannot usually be kept by the RTC hardware, so we store them in a UEFI
26 // variable instead.
27 //
28 typedef struct {
29 INT16 TimeZone;
30 UINT8 Daylight;
31 } NON_VOLATILE_TIME_SETTINGS;
32
33 STATIC CONST CHAR16 mTimeSettingsVariableName[] = L"RtcTimeSettings";
34 STATIC NON_VOLATILE_TIME_SETTINGS mTimeSettings;
35
36 /**
37 Returns the current time and date information, and the time-keeping capabilities
38 of the hardware platform.
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 GetTime (
52 OUT EFI_TIME *Time,
53 OUT EFI_TIME_CAPABILITIES *Capabilities
54 )
55 {
56 if (Time == NULL) {
57 return EFI_INVALID_PARAMETER;
58 }
59
60 //
61 // Set these first so the RealTimeClockLib implementation
62 // can override them based on its own settings.
63 //
64 Time->TimeZone = mTimeSettings.TimeZone;
65 Time->Daylight = mTimeSettings.Daylight;
66
67 return LibGetTime (Time, Capabilities);
68 }
69
70 /**
71 Sets the current local time and date information.
72
73 @param Time A pointer to the current time.
74
75 @retval EFI_SUCCESS The operation completed successfully.
76 @retval EFI_INVALID_PARAMETER A time field is out of range.
77 @retval EFI_DEVICE_ERROR The time could not be set due to hardware error.
78
79 **/
80 EFI_STATUS
81 EFIAPI
82 SetTime (
83 IN EFI_TIME *Time
84 )
85 {
86 EFI_STATUS Status;
87 BOOLEAN TimeSettingsChanged;
88
89 if ((Time == NULL) || !IsTimeValid (Time)) {
90 return EFI_INVALID_PARAMETER;
91 }
92
93 TimeSettingsChanged = FALSE;
94 if ((mTimeSettings.TimeZone != Time->TimeZone) ||
95 (mTimeSettings.Daylight != Time->Daylight))
96 {
97 mTimeSettings.TimeZone = Time->TimeZone;
98 mTimeSettings.Daylight = Time->Daylight;
99 TimeSettingsChanged = TRUE;
100 }
101
102 Status = LibSetTime (Time);
103 if (EFI_ERROR (Status)) {
104 return Status;
105 }
106
107 if (TimeSettingsChanged) {
108 Status = EfiSetVariable (
109 (CHAR16 *)mTimeSettingsVariableName,
110 &gEfiCallerIdGuid,
111 EFI_VARIABLE_NON_VOLATILE |
112 EFI_VARIABLE_BOOTSERVICE_ACCESS |
113 EFI_VARIABLE_RUNTIME_ACCESS,
114 sizeof (mTimeSettings),
115 (VOID *)&mTimeSettings
116 );
117 if (EFI_ERROR (Status)) {
118 return EFI_DEVICE_ERROR;
119 }
120 }
121
122 return EFI_SUCCESS;
123 }
124
125 /**
126 Returns the current wakeup alarm clock setting.
127
128 @param Enabled Indicates if the alarm is currently enabled or disabled.
129 @param Pending Indicates if the alarm signal is pending and requires acknowledgement.
130 @param Time The current alarm setting.
131
132 @retval EFI_SUCCESS The alarm settings were returned.
133 @retval EFI_INVALID_PARAMETER Any parameter is NULL.
134 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
135
136 **/
137 EFI_STATUS
138 EFIAPI
139 GetWakeupTime (
140 OUT BOOLEAN *Enabled,
141 OUT BOOLEAN *Pending,
142 OUT EFI_TIME *Time
143 )
144 {
145 if ((Time == NULL) || (Enabled == NULL) || (Pending == NULL)) {
146 return EFI_INVALID_PARAMETER;
147 }
148
149 //
150 // Set these first so the RealTimeClockLib implementation
151 // can override them based on its own settings.
152 //
153 Time->TimeZone = mTimeSettings.TimeZone;
154 Time->Daylight = mTimeSettings.Daylight;
155
156 return LibGetWakeupTime (Enabled, Pending, Time);
157 }
158
159 /**
160 Sets the system wakeup alarm clock time.
161
162 @param Enabled Enable or disable the wakeup alarm.
163 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
164
165 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If
166 Enable is FALSE, then the wakeup alarm was disabled.
167 @retval EFI_INVALID_PARAMETER A time field is out of range.
168 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
169 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
170
171 **/
172 EFI_STATUS
173 EFIAPI
174 SetWakeupTime (
175 IN BOOLEAN Enabled,
176 OUT EFI_TIME *Time
177 )
178 {
179 return LibSetWakeupTime (Enabled, Time);
180 }
181
182 /**
183 This is the declaration of an EFI image entry point. This can be the entry point to an application
184 written to this specification, an EFI boot service driver, or an EFI runtime driver.
185
186 @param ImageHandle Handle that identifies the loaded image.
187 @param SystemTable System Table for this image.
188
189 @retval EFI_SUCCESS The operation completed successfully.
190
191 **/
192 EFI_STATUS
193 EFIAPI
194 InitializeRealTimeClock (
195 IN EFI_HANDLE ImageHandle,
196 IN EFI_SYSTEM_TABLE *SystemTable
197 )
198 {
199 EFI_STATUS Status;
200 UINTN Size;
201
202 Status = LibRtcInitialize (ImageHandle, SystemTable);
203 if (EFI_ERROR (Status)) {
204 return Status;
205 }
206
207 Size = sizeof (mTimeSettings);
208 Status = EfiGetVariable (
209 (CHAR16 *)mTimeSettingsVariableName,
210 &gEfiCallerIdGuid,
211 NULL,
212 &Size,
213 (VOID *)&mTimeSettings
214 );
215 if (EFI_ERROR (Status) ||
216 !IsValidTimeZone (mTimeSettings.TimeZone) ||
217 !IsValidDaylight (mTimeSettings.Daylight))
218 {
219 DEBUG ((
220 DEBUG_WARN,
221 "%a: using default timezone/daylight settings\n",
222 __FUNCTION__
223 ));
224
225 mTimeSettings.TimeZone = EFI_UNSPECIFIED_TIMEZONE;
226 mTimeSettings.Daylight = 0;
227 }
228
229 SystemTable->RuntimeServices->GetTime = GetTime;
230 SystemTable->RuntimeServices->SetTime = SetTime;
231 SystemTable->RuntimeServices->GetWakeupTime = GetWakeupTime;
232 SystemTable->RuntimeServices->SetWakeupTime = SetWakeupTime;
233
234 Status = gBS->InstallMultipleProtocolInterfaces (
235 &mHandle,
236 &gEfiRealTimeClockArchProtocolGuid,
237 NULL,
238 NULL
239 );
240
241 return Status;
242 }