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