TianoCore EDK2 master
RealTimeClock.c
Go to the documentation of this file.
1
12#include <PiDxe.h>
13#include <Library/DebugLib.h>
15#include <Library/TimeBaseLib.h>
16#include <Library/UefiLib.h>
20
21EFI_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//
28typedef struct {
29 INT16 TimeZone;
30 UINT8 Daylight;
32
33STATIC CONST CHAR16 mTimeSettingsVariableName[] = L"RtcTimeSettings";
35
50EFIAPI
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
81EFIAPI
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,
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
138EFIAPI
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
173EFIAPI
175 IN BOOLEAN Enabled,
176 OUT EFI_TIME *Time
177 )
178{
179 return LibSetWakeupTime (Enabled, Time);
180}
181
193EFIAPI
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}
UINT64 UINTN
#define NULL
Definition: Base.h:312
#define CONST
Definition: Base.h:259
#define STATIC
Definition: Base.h:264
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define DEBUG(Expression)
Definition: DebugLib.h:417
EFI_STATUS EFIAPI GetWakeupTime(OUT BOOLEAN *Enabled, OUT BOOLEAN *Pending, OUT EFI_TIME *Time)
EFI_STATUS EFIAPI SetTime(IN EFI_TIME *Time)
Definition: RealTimeClock.c:82
EFI_STATUS EFIAPI InitializeRealTimeClock(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
EFI_STATUS EFIAPI SetWakeupTime(IN BOOLEAN Enabled, OUT EFI_TIME *Time)
EFI_STATUS EFIAPI GetTime(OUT EFI_TIME *Time, OUT EFI_TIME_CAPABILITIES *Capabilities)
Definition: RealTimeClock.c:51
EFI_STATUS EFIAPI LibSetWakeupTime(IN BOOLEAN Enabled, OUT EFI_TIME *Time)
EFI_STATUS EFIAPI LibRtcInitialize(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
EFI_STATUS EFIAPI LibGetWakeupTime(OUT BOOLEAN *Enabled, OUT BOOLEAN *Pending, OUT EFI_TIME *Time)
EFI_STATUS EFIAPI LibSetTime(IN EFI_TIME *Time)
EFI_STATUS EFIAPI LibGetTime(OUT EFI_TIME *Time, OUT EFI_TIME_CAPABILITIES *Capabilities)
BOOLEAN EFIAPI IsValidTimeZone(IN INT16 TimeZone)
Definition: TimeBaseLib.c:225
BOOLEAN EFIAPI IsTimeValid(IN EFI_TIME *Time)
Definition: TimeBaseLib.c:269
BOOLEAN EFIAPI IsValidDaylight(IN INT8 Daylight)
Definition: TimeBaseLib.c:249
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:28
VOID * EFI_HANDLE
Definition: UefiBaseType.h:32
#define EFI_SUCCESS
Definition: UefiBaseType.h:111
EFI_BOOT_SERVICES * gBS
#define EFI_VARIABLE_NON_VOLATILE
EFI_STATUS EFIAPI EfiGetVariable(IN CHAR16 *VariableName, IN EFI_GUID *VendorGuid, OUT UINT32 *Attributes OPTIONAL, IN OUT UINTN *DataSize, OUT VOID *Data)
Definition: RuntimeLib.c:394
EFI_STATUS EFIAPI EfiSetVariable(IN CHAR16 *VariableName, IN EFI_GUID *VendorGuid, IN UINT32 Attributes, IN UINTN DataSize, IN VOID *Data)
Definition: RuntimeLib.c:477
#define EFI_UNSPECIFIED_TIMEZONE
Definition: UefiSpec.h:57