]> git.proxmox.com Git - mirror_edk2.git/blame - EmbeddedPkg/RealTimeClockRuntimeDxe/RealTimeClock.c
EmbeddedPkg/RealTimeClockRuntimeDxe: Use helper functions from TimeBaseLib
[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
146 return LibGetWakeupTime (Enabled, Pending, Time);\r
147}\r
148\r
149\r
150/**\r
151 Sets the system wakeup alarm clock time.\r
152\r
153 @param Enabled Enable or disable the wakeup alarm.\r
154 @param Time If Enable is TRUE, the time to set the wakeup alarm for.\r
155\r
156 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If\r
157 Enable is FALSE, then the wakeup alarm was disabled.\r
158 @retval EFI_INVALID_PARAMETER A time field is out of range.\r
159 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.\r
160 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.\r
161\r
162**/\r
163EFI_STATUS\r
164EFIAPI\r
165SetWakeupTime (\r
166 IN BOOLEAN Enabled,\r
167 OUT EFI_TIME *Time\r
168 )\r
169{\r
170 return LibSetWakeupTime (Enabled, Time);\r
171}\r
172\r
173\r
174\r
175/**\r
176 This is the declaration of an EFI image entry point. This can be the entry point to an application\r
177 written to this specification, an EFI boot service driver, or an EFI runtime driver.\r
178\r
179 @param ImageHandle Handle that identifies the loaded image.\r
180 @param SystemTable System Table for this image.\r
181\r
182 @retval EFI_SUCCESS The operation completed successfully.\r
183\r
184**/\r
185EFI_STATUS\r
186EFIAPI\r
187InitializeRealTimeClock (\r
188 IN EFI_HANDLE ImageHandle,\r
189 IN EFI_SYSTEM_TABLE *SystemTable\r
190 )\r
191{\r
192 EFI_STATUS Status;\r
517cf877 193 UINTN Size;\r
2ef2b01e 194\r
da6ae666
LD
195 Status = LibRtcInitialize (ImageHandle, SystemTable);\r
196 if (EFI_ERROR (Status)) {\r
197 return Status;\r
198 }\r
2ef2b01e 199\r
517cf877
AB
200 Size = sizeof (mTimeSettings);\r
201 Status = EfiGetVariable ((CHAR16 *)mTimeSettingsVariableName,\r
202 &gEfiCallerIdGuid, NULL, &Size, (VOID *)&mTimeSettings);\r
203 if (EFI_ERROR (Status) ||\r
204 !IsValidTimeZone (mTimeSettings.TimeZone) ||\r
205 !IsValidDaylight (mTimeSettings.Daylight)) {\r
206 DEBUG ((DEBUG_WARN, "%a: using default timezone/daylight settings\n",\r
207 __FUNCTION__));\r
208\r
209 mTimeSettings.TimeZone = EFI_UNSPECIFIED_TIMEZONE;\r
210 mTimeSettings.Daylight = 0;\r
211 }\r
212\r
2ef2b01e
A
213 SystemTable->RuntimeServices->GetTime = GetTime;\r
214 SystemTable->RuntimeServices->SetTime = SetTime;\r
215 SystemTable->RuntimeServices->GetWakeupTime = GetWakeupTime;\r
216 SystemTable->RuntimeServices->SetWakeupTime = SetWakeupTime;\r
217\r
218 Status = gBS->InstallMultipleProtocolInterfaces (\r
219 &mHandle,\r
220 &gEfiRealTimeClockArchProtocolGuid,\r
221 NULL,\r
222 NULL\r
223 );\r
224\r
225 return Status;\r
226}\r
227\r