ArmPkg: only attempt buildin MmCommunicationDxe for AArch64
[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
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10
11 #include <PiDxe.h>
12 #include <Library/DebugLib.h>
13 #include <Library/RealTimeClockLib.h>
14 #include <Library/UefiLib.h>
15 #include <Library/UefiBootServicesTableLib.h>
16 #include <Library/UefiRuntimeLib.h>
17 #include <Protocol/RealTimeClock.h>
18
19 EFI_HANDLE mHandle = NULL;
20
21 //
22 // These values can be set by SetTime () and need to be returned by GetTime ()
23 // but cannot usually be kept by the RTC hardware, so we store them in a UEFI
24 // variable instead.
25 //
26 typedef struct {
27 INT16 TimeZone;
28 UINT8 Daylight;
29 } NON_VOLATILE_TIME_SETTINGS;
30
31 STATIC CONST CHAR16 mTimeSettingsVariableName[] = L"RtcTimeSettings";
32 STATIC NON_VOLATILE_TIME_SETTINGS mTimeSettings;
33
34 STATIC
35 BOOLEAN
36 IsValidTimeZone (
37 IN INT16 TimeZone
38 )
39 {
40 return TimeZone == EFI_UNSPECIFIED_TIMEZONE ||
41 (TimeZone >= -1440 && TimeZone <= 1440);
42 }
43
44 STATIC
45 BOOLEAN
46 IsValidDaylight (
47 IN INT8 Daylight
48 )
49 {
50 return Daylight == 0 ||
51 Daylight == EFI_TIME_ADJUST_DAYLIGHT ||
52 Daylight == (EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT);
53 }
54
55 STATIC
56 BOOLEAN
57 EFIAPI
58 IsLeapYear (
59 IN EFI_TIME *Time
60 )
61 {
62 if (Time->Year % 4 == 0) {
63 if (Time->Year % 100 == 0) {
64 if (Time->Year % 400 == 0) {
65 return TRUE;
66 } else {
67 return FALSE;
68 }
69 } else {
70 return TRUE;
71 }
72 } else {
73 return FALSE;
74 }
75 }
76
77 STATIC CONST INTN mDayOfMonth[12] = {
78 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
79 };
80
81 STATIC
82 BOOLEAN
83 EFIAPI
84 IsDayValid (
85 IN EFI_TIME *Time
86 )
87 {
88 if (Time->Day < 1 ||
89 Time->Day > mDayOfMonth[Time->Month - 1] ||
90 (Time->Month == 2 && !IsLeapYear (Time) && Time->Day > 28)) {
91 return FALSE;
92 }
93 return TRUE;
94 }
95
96 STATIC
97 BOOLEAN
98 EFIAPI
99 IsTimeValid(
100 IN EFI_TIME *Time
101 )
102 {
103 // Check the input parameters are within the range specified by UEFI
104 if (Time->Year < 1900 ||
105 Time->Year > 9999 ||
106 Time->Month < 1 ||
107 Time->Month > 12 ||
108 !IsDayValid (Time) ||
109 Time->Hour > 23 ||
110 Time->Minute > 59 ||
111 Time->Second > 59 ||
112 Time->Nanosecond > 999999999 ||
113 !IsValidTimeZone (Time->TimeZone) ||
114 !IsValidDaylight (Time->Daylight)) {
115 return FALSE;
116 }
117 return TRUE;
118 }
119
120 /**
121 Returns the current time and date information, and the time-keeping capabilities
122 of the hardware platform.
123
124 @param Time A pointer to storage to receive a snapshot of the current time.
125 @param Capabilities An optional pointer to a buffer to receive the real time clock
126 device's capabilities.
127
128 @retval EFI_SUCCESS The operation completed successfully.
129 @retval EFI_INVALID_PARAMETER Time is NULL.
130 @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.
131
132 **/
133 EFI_STATUS
134 EFIAPI
135 GetTime (
136 OUT EFI_TIME *Time,
137 OUT EFI_TIME_CAPABILITIES *Capabilities
138 )
139 {
140 if (Time == NULL) {
141 return EFI_INVALID_PARAMETER;
142 }
143
144 //
145 // Set these first so the RealTimeClockLib implementation
146 // can override them based on its own settings.
147 //
148 Time->TimeZone = mTimeSettings.TimeZone;
149 Time->Daylight = mTimeSettings.Daylight;
150
151 return LibGetTime (Time, Capabilities);
152 }
153
154
155
156 /**
157 Sets the current local time and date information.
158
159 @param Time A pointer to the current time.
160
161 @retval EFI_SUCCESS The operation completed successfully.
162 @retval EFI_INVALID_PARAMETER A time field is out of range.
163 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
164
165 **/
166 EFI_STATUS
167 EFIAPI
168 SetTime (
169 IN EFI_TIME *Time
170 )
171 {
172 EFI_STATUS Status;
173 BOOLEAN TimeSettingsChanged;
174
175 if (Time == NULL || !IsTimeValid (Time)) {
176 return EFI_INVALID_PARAMETER;
177 }
178
179 TimeSettingsChanged = FALSE;
180 if (mTimeSettings.TimeZone != Time->TimeZone ||
181 mTimeSettings.Daylight != Time->Daylight) {
182
183 mTimeSettings.TimeZone = Time->TimeZone;
184 mTimeSettings.Daylight = Time->Daylight;
185 TimeSettingsChanged = TRUE;
186 }
187
188 Status = LibSetTime (Time);
189 if (EFI_ERROR (Status)) {
190 return Status;
191 }
192
193 if (TimeSettingsChanged) {
194 Status = EfiSetVariable (
195 (CHAR16 *)mTimeSettingsVariableName,
196 &gEfiCallerIdGuid,
197 EFI_VARIABLE_NON_VOLATILE |
198 EFI_VARIABLE_BOOTSERVICE_ACCESS |
199 EFI_VARIABLE_RUNTIME_ACCESS,
200 sizeof (mTimeSettings),
201 (VOID *)&mTimeSettings);
202 if (EFI_ERROR (Status)) {
203 return EFI_DEVICE_ERROR;
204 }
205 }
206 return EFI_SUCCESS;
207 }
208
209
210 /**
211 Returns the current wakeup alarm clock setting.
212
213 @param Enabled Indicates if the alarm is currently enabled or disabled.
214 @param Pending Indicates if the alarm signal is pending and requires acknowledgement.
215 @param Time The current alarm setting.
216
217 @retval EFI_SUCCESS The alarm settings were returned.
218 @retval EFI_INVALID_PARAMETER Any parameter is NULL.
219 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
220
221 **/
222 EFI_STATUS
223 EFIAPI
224 GetWakeupTime (
225 OUT BOOLEAN *Enabled,
226 OUT BOOLEAN *Pending,
227 OUT EFI_TIME *Time
228 )
229 {
230 return LibGetWakeupTime (Enabled, Pending, Time);
231 }
232
233
234 /**
235 Sets the system wakeup alarm clock time.
236
237 @param Enabled Enable or disable the wakeup alarm.
238 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
239
240 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If
241 Enable is FALSE, then the wakeup alarm was disabled.
242 @retval EFI_INVALID_PARAMETER A time field is out of range.
243 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
244 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
245
246 **/
247 EFI_STATUS
248 EFIAPI
249 SetWakeupTime (
250 IN BOOLEAN Enabled,
251 OUT EFI_TIME *Time
252 )
253 {
254 return LibSetWakeupTime (Enabled, Time);
255 }
256
257
258
259 /**
260 This is the declaration of an EFI image entry point. This can be the entry point to an application
261 written to this specification, an EFI boot service driver, or an EFI runtime driver.
262
263 @param ImageHandle Handle that identifies the loaded image.
264 @param SystemTable System Table for this image.
265
266 @retval EFI_SUCCESS The operation completed successfully.
267
268 **/
269 EFI_STATUS
270 EFIAPI
271 InitializeRealTimeClock (
272 IN EFI_HANDLE ImageHandle,
273 IN EFI_SYSTEM_TABLE *SystemTable
274 )
275 {
276 EFI_STATUS Status;
277 UINTN Size;
278
279 Status = LibRtcInitialize (ImageHandle, SystemTable);
280 if (EFI_ERROR (Status)) {
281 return Status;
282 }
283
284 Size = sizeof (mTimeSettings);
285 Status = EfiGetVariable ((CHAR16 *)mTimeSettingsVariableName,
286 &gEfiCallerIdGuid, NULL, &Size, (VOID *)&mTimeSettings);
287 if (EFI_ERROR (Status) ||
288 !IsValidTimeZone (mTimeSettings.TimeZone) ||
289 !IsValidDaylight (mTimeSettings.Daylight)) {
290 DEBUG ((DEBUG_WARN, "%a: using default timezone/daylight settings\n",
291 __FUNCTION__));
292
293 mTimeSettings.TimeZone = EFI_UNSPECIFIED_TIMEZONE;
294 mTimeSettings.Daylight = 0;
295 }
296
297 SystemTable->RuntimeServices->GetTime = GetTime;
298 SystemTable->RuntimeServices->SetTime = SetTime;
299 SystemTable->RuntimeServices->GetWakeupTime = GetWakeupTime;
300 SystemTable->RuntimeServices->SetWakeupTime = SetWakeupTime;
301
302 Status = gBS->InstallMultipleProtocolInterfaces (
303 &mHandle,
304 &gEfiRealTimeClockArchProtocolGuid,
305 NULL,
306 NULL
307 );
308
309 return Status;
310 }
311