]>
Commit | Line | Data |
---|---|---|
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 | |
21 | EFI_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 | |
28 | typedef struct {\r | |
e7108d0e MK |
29 | INT16 TimeZone;\r |
30 | UINT8 Daylight;\r | |
517cf877 AB |
31 | } NON_VOLATILE_TIME_SETTINGS;\r |
32 | \r | |
e7108d0e MK |
33 | STATIC CONST CHAR16 mTimeSettingsVariableName[] = L"RtcTimeSettings";\r |
34 | STATIC NON_VOLATILE_TIME_SETTINGS mTimeSettings;\r | |
517cf877 | 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 | |
49 | EFI_STATUS\r | |
50 | EFIAPI\r | |
51 | GetTime (\r | |
e7108d0e MK |
52 | OUT EFI_TIME *Time,\r |
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 | |
2ef2b01e A |
70 | /**\r |
71 | Sets the current local time and date information.\r | |
72 | \r | |
73 | @param Time A pointer to the current time.\r | |
74 | \r | |
75 | @retval EFI_SUCCESS The operation completed successfully.\r | |
76 | @retval EFI_INVALID_PARAMETER A time field is out of range.\r | |
a4037690 | 77 | @retval EFI_DEVICE_ERROR The time could not be set due to hardware error.\r |
2ef2b01e A |
78 | \r |
79 | **/\r | |
80 | EFI_STATUS\r | |
81 | EFIAPI\r | |
82 | SetTime (\r | |
e7108d0e | 83 | IN EFI_TIME *Time\r |
2ef2b01e A |
84 | )\r |
85 | {\r | |
e7108d0e MK |
86 | EFI_STATUS Status;\r |
87 | BOOLEAN TimeSettingsChanged;\r | |
517cf877 | 88 | \r |
e7108d0e | 89 | if ((Time == NULL) || !IsTimeValid (Time)) {\r |
517cf877 AB |
90 | return EFI_INVALID_PARAMETER;\r |
91 | }\r | |
92 | \r | |
93 | TimeSettingsChanged = FALSE;\r | |
e7108d0e MK |
94 | if ((mTimeSettings.TimeZone != Time->TimeZone) ||\r |
95 | (mTimeSettings.Daylight != Time->Daylight))\r | |
96 | {\r | |
517cf877 AB |
97 | mTimeSettings.TimeZone = Time->TimeZone;\r |
98 | mTimeSettings.Daylight = Time->Daylight;\r | |
e7108d0e | 99 | TimeSettingsChanged = TRUE;\r |
517cf877 AB |
100 | }\r |
101 | \r | |
102 | Status = LibSetTime (Time);\r | |
103 | if (EFI_ERROR (Status)) {\r | |
104 | return Status;\r | |
105 | }\r | |
106 | \r | |
107 | if (TimeSettingsChanged) {\r | |
108 | Status = EfiSetVariable (\r | |
109 | (CHAR16 *)mTimeSettingsVariableName,\r | |
110 | &gEfiCallerIdGuid,\r | |
111 | EFI_VARIABLE_NON_VOLATILE |\r | |
112 | EFI_VARIABLE_BOOTSERVICE_ACCESS |\r | |
113 | EFI_VARIABLE_RUNTIME_ACCESS,\r | |
114 | sizeof (mTimeSettings),\r | |
e7108d0e MK |
115 | (VOID *)&mTimeSettings\r |
116 | );\r | |
517cf877 AB |
117 | if (EFI_ERROR (Status)) {\r |
118 | return EFI_DEVICE_ERROR;\r | |
119 | }\r | |
120 | }\r | |
e7108d0e | 121 | \r |
517cf877 | 122 | return EFI_SUCCESS;\r |
2ef2b01e A |
123 | }\r |
124 | \r | |
2ef2b01e A |
125 | /**\r |
126 | Returns the current wakeup alarm clock setting.\r | |
127 | \r | |
128 | @param Enabled Indicates if the alarm is currently enabled or disabled.\r | |
129 | @param Pending Indicates if the alarm signal is pending and requires acknowledgement.\r | |
130 | @param Time The current alarm setting.\r | |
131 | \r | |
132 | @retval EFI_SUCCESS The alarm settings were returned.\r | |
133 | @retval EFI_INVALID_PARAMETER Any parameter is NULL.\r | |
134 | @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.\r | |
135 | \r | |
136 | **/\r | |
137 | EFI_STATUS\r | |
138 | EFIAPI\r | |
139 | GetWakeupTime (\r | |
e7108d0e MK |
140 | OUT BOOLEAN *Enabled,\r |
141 | OUT BOOLEAN *Pending,\r | |
142 | OUT EFI_TIME *Time\r | |
2ef2b01e A |
143 | )\r |
144 | {\r | |
e7108d0e | 145 | if ((Time == NULL) || (Enabled == NULL) || (Pending == NULL)) {\r |
b233eb18 MW |
146 | return EFI_INVALID_PARAMETER;\r |
147 | }\r | |
148 | \r | |
149 | //\r | |
150 | // Set these first so the RealTimeClockLib implementation\r | |
151 | // can override them based on its own settings.\r | |
152 | //\r | |
153 | Time->TimeZone = mTimeSettings.TimeZone;\r | |
154 | Time->Daylight = mTimeSettings.Daylight;\r | |
155 | \r | |
2ef2b01e A |
156 | return LibGetWakeupTime (Enabled, Pending, Time);\r |
157 | }\r | |
158 | \r | |
2ef2b01e A |
159 | /**\r |
160 | Sets the system wakeup alarm clock time.\r | |
161 | \r | |
162 | @param Enabled Enable or disable the wakeup alarm.\r | |
163 | @param Time If Enable is TRUE, the time to set the wakeup alarm for.\r | |
164 | \r | |
165 | @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If\r | |
166 | Enable is FALSE, then the wakeup alarm was disabled.\r | |
167 | @retval EFI_INVALID_PARAMETER A time field is out of range.\r | |
168 | @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.\r | |
169 | @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.\r | |
170 | \r | |
171 | **/\r | |
172 | EFI_STATUS\r | |
173 | EFIAPI\r | |
174 | SetWakeupTime (\r | |
e7108d0e MK |
175 | IN BOOLEAN Enabled,\r |
176 | OUT EFI_TIME *Time\r | |
2ef2b01e A |
177 | )\r |
178 | {\r | |
179 | return LibSetWakeupTime (Enabled, Time);\r | |
180 | }\r | |
181 | \r | |
2ef2b01e A |
182 | /**\r |
183 | This is the declaration of an EFI image entry point. This can be the entry point to an application\r | |
184 | written to this specification, an EFI boot service driver, or an EFI runtime driver.\r | |
185 | \r | |
186 | @param ImageHandle Handle that identifies the loaded image.\r | |
187 | @param SystemTable System Table for this image.\r | |
188 | \r | |
189 | @retval EFI_SUCCESS The operation completed successfully.\r | |
190 | \r | |
191 | **/\r | |
192 | EFI_STATUS\r | |
193 | EFIAPI\r | |
194 | InitializeRealTimeClock (\r | |
e7108d0e MK |
195 | IN EFI_HANDLE ImageHandle,\r |
196 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
2ef2b01e A |
197 | )\r |
198 | {\r | |
199 | EFI_STATUS Status;\r | |
517cf877 | 200 | UINTN Size;\r |
2ef2b01e | 201 | \r |
da6ae666 LD |
202 | Status = LibRtcInitialize (ImageHandle, SystemTable);\r |
203 | if (EFI_ERROR (Status)) {\r | |
204 | return Status;\r | |
205 | }\r | |
2ef2b01e | 206 | \r |
e7108d0e MK |
207 | Size = sizeof (mTimeSettings);\r |
208 | Status = EfiGetVariable (\r | |
209 | (CHAR16 *)mTimeSettingsVariableName,\r | |
210 | &gEfiCallerIdGuid,\r | |
211 | NULL,\r | |
212 | &Size,\r | |
213 | (VOID *)&mTimeSettings\r | |
214 | );\r | |
517cf877 AB |
215 | if (EFI_ERROR (Status) ||\r |
216 | !IsValidTimeZone (mTimeSettings.TimeZone) ||\r | |
e7108d0e MK |
217 | !IsValidDaylight (mTimeSettings.Daylight))\r |
218 | {\r | |
219 | DEBUG ((\r | |
220 | DEBUG_WARN,\r | |
221 | "%a: using default timezone/daylight settings\n",\r | |
222 | __FUNCTION__\r | |
223 | ));\r | |
517cf877 AB |
224 | \r |
225 | mTimeSettings.TimeZone = EFI_UNSPECIFIED_TIMEZONE;\r | |
226 | mTimeSettings.Daylight = 0;\r | |
227 | }\r | |
228 | \r | |
2ef2b01e A |
229 | SystemTable->RuntimeServices->GetTime = GetTime;\r |
230 | SystemTable->RuntimeServices->SetTime = SetTime;\r | |
231 | SystemTable->RuntimeServices->GetWakeupTime = GetWakeupTime;\r | |
232 | SystemTable->RuntimeServices->SetWakeupTime = SetWakeupTime;\r | |
233 | \r | |
234 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
235 | &mHandle,\r | |
236 | &gEfiRealTimeClockArchProtocolGuid,\r | |
237 | NULL,\r | |
238 | NULL\r | |
239 | );\r | |
240 | \r | |
241 | return Status;\r | |
242 | }\r |