]> git.proxmox.com Git - mirror_edk2.git/blob - UnixPkg/RealTimeClockRuntimeDxe/RealTimeClock.c
Port EdkUnixPkg to UnixPkg. The changes are listed as follows:
[mirror_edk2.git] / UnixPkg / RealTimeClockRuntimeDxe / RealTimeClock.c
1 /*++
2
3 Copyright (c) 2004 - 2008, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 RealTimeClock.c
15
16 Abstract:
17
18 UNIX Emulation Architectural Protocol Driver as defined in Tiano
19
20 --*/
21 #include "PiDxe.h"
22 #include "UnixDxe.h"
23 #include <Protocol/RealTimeClock.h>
24
25 #include <Library/BaseLib.h>
26 #include <Library/DebugLib.h>
27 #include <Library/UefiLib.h>
28 #include <Library/UefiDriverEntryPoint.h>
29 #include <Library/UnixLib.h>
30 #include <Library/MemoryAllocationLib.h>
31 #include <Library/UefiBootServicesTableLib.h>
32
33 BOOLEAN
34 DayValid (
35 IN EFI_TIME *Time
36 );
37
38 BOOLEAN
39 IsLeapYear (
40 IN EFI_TIME *Time
41 );
42
43 EFI_STATUS
44 RtcTimeFieldsValid (
45 IN EFI_TIME *Time
46 );
47
48 EFI_STATUS
49 EFIAPI
50 InitializeRealTimeClock (
51 IN EFI_HANDLE ImageHandle,
52 IN EFI_SYSTEM_TABLE *SystemTable
53 );
54
55 STATIC
56 EFI_STATUS
57 EFIAPI
58 UnixGetTime (
59 OUT EFI_TIME * Time,
60 OUT EFI_TIME_CAPABILITIES * Capabilities OPTIONAL
61 )
62 /*++
63
64 Routine Description:
65 Service routine for RealTimeClockInstance->GetTime
66
67 Arguments:
68
69 Time - A pointer to storage that will receive a snapshot of the current time.
70
71 Capabilities - A pointer to storage that will receive the capabilities of the real time clock
72 in the platform. This includes the real time clock's resolution and accuracy.
73 All reported device capabilities are rounded up. This is an OPTIONAL argument.
74
75 Returns:
76
77 EFI_SUCEESS - The underlying GetSystemTime call occurred and returned
78 Note that in the NT32 emulation, the GetSystemTime call has no return value
79 thus you will always receive a EFI_SUCCESS on this.
80
81 --*/
82 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
83 {
84
85 //
86 // Check parameter for null pointer
87 //
88 if (Time == NULL) {
89 return EFI_INVALID_PARAMETER;
90
91 }
92
93 gUnix->GetLocalTime (Time);
94
95 if (Capabilities != NULL) {
96 Capabilities->Resolution = 1;
97 Capabilities->Accuracy = 50000000;
98 Capabilities->SetsToZero = FALSE;
99 }
100
101 return EFI_SUCCESS;
102 }
103
104 STATIC
105 EFI_STATUS
106 EFIAPI
107 UnixSetTime (
108 IN EFI_TIME *Time
109 )
110 /*++
111
112 Routine Description:
113 Service routine for RealTimeClockInstance->SetTime
114
115 Arguments:
116
117 Time - A pointer to storage containing the time and date information to
118 program into the real time clock.
119
120 Returns:
121
122 EFI_SUCEESS - The operation completed successfully.
123
124 EFI_INVALID_PARAMETER - One of the fields in Time is out of range.
125
126 EFI_DEVICE_ERROR - The operation could not be complete due to a device error.
127
128 --*/
129 // TODO: EFI_SUCCESS - add return value to function comment
130 {
131 EFI_STATUS Status;
132
133 if (Time == NULL) {
134 return EFI_INVALID_PARAMETER;
135 }
136 //
137 // Make sure that the time fields are valid
138 //
139 Status = RtcTimeFieldsValid (Time);
140 if (EFI_ERROR (Status)) {
141 return Status;
142 }
143 return EFI_UNSUPPORTED;
144 }
145
146 STATIC
147 EFI_STATUS
148 EFIAPI
149 UnixGetWakeupTime (
150 OUT BOOLEAN *Enabled,
151 OUT BOOLEAN *Pending,
152 OUT EFI_TIME *Time
153 )
154 /*++
155
156 Routine Description:
157 Service routine for RealTimeClockInstance->GetWakeupTime
158
159 Arguments:
160 This - Indicates the protocol instance structure.
161
162 Enabled - Indicates if the alarm is currently enabled or disabled.
163
164 Pending - Indicates if the alarm signal is pending and requires
165 acknowledgement.
166
167 Time - The current alarm setting.
168
169 Returns:
170
171 EFI_SUCEESS - The operation completed successfully.
172
173 EFI_DEVICE_ERROR - The operation could not be complete due to a device error.
174
175 EFI_UNSUPPORTED - The operation is not supported on this platform.
176
177 --*/
178 {
179 return EFI_UNSUPPORTED;
180 }
181
182 STATIC
183 EFI_STATUS
184 EFIAPI
185 UnixSetWakeupTime (
186 IN BOOLEAN Enable,
187 OUT EFI_TIME *Time
188 )
189 /*++
190
191 Routine Description:
192 Service routine for RealTimeClockInstance->SetWakeupTime
193
194 Arguments:
195
196 Enabled - Enable or disable the wakeup alarm.
197
198 Time - If enable is TRUE, the time to set the wakup alarm for.
199 If enable is FALSE, then this parameter is optional, and
200 may be NULL.
201
202 Returns:
203
204 EFI_SUCEESS - The operation completed successfully.
205
206 EFI_DEVICE_ERROR - The operation could not be complete due to a device error.
207
208 EFI_INVALID_PARAMETER - A field in Time is out of range.
209
210 EFI_UNSUPPORTED - The operation is not supported on this platform.
211
212 --*/
213 {
214 return EFI_UNSUPPORTED;
215 }
216
217 EFI_STATUS
218 EFIAPI
219 InitializeRealTimeClock (
220 IN EFI_HANDLE ImageHandle,
221 IN EFI_SYSTEM_TABLE *SystemTable
222 )
223 /*++
224
225 Routine Description:
226 Install Real Time Clock Protocol
227
228 Arguments:
229 ImageHandle - Image Handle
230 SystemTable - Pointer to system table
231
232 Returns:
233
234 EFI_SUCEESS - Real Time Clock Services are installed into the Runtime Services Table
235
236 --*/
237 {
238 EFI_STATUS Status;
239 EFI_HANDLE Handle;
240
241 SystemTable->RuntimeServices->GetTime = UnixGetTime;
242 SystemTable->RuntimeServices->SetTime = UnixSetTime;
243 SystemTable->RuntimeServices->GetWakeupTime = UnixGetWakeupTime;
244 SystemTable->RuntimeServices->SetWakeupTime = UnixSetWakeupTime;
245
246 Handle = NULL;
247 Status = gBS->InstallMultipleProtocolInterfaces (
248 &Handle,
249 &gEfiRealTimeClockArchProtocolGuid,
250 NULL,
251 NULL
252 );
253 return Status;
254 }
255
256 EFI_STATUS
257 RtcTimeFieldsValid (
258 IN EFI_TIME *Time
259 )
260 /*++
261
262 Routine Description:
263
264 Arguments:
265
266 Returns:
267 --*/
268 // TODO: Time - add argument and description to function comment
269 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
270 // TODO: EFI_SUCCESS - add return value to function comment
271 {
272 if (Time->Year < 1998 ||
273 Time->Year > 2099 ||
274 Time->Month < 1 ||
275 Time->Month > 12 ||
276 (!DayValid (Time)) ||
277 Time->Hour > 23 ||
278 Time->Minute > 59 ||
279 Time->Second > 59 ||
280 Time->Nanosecond > 999999999 ||
281 (!(Time->TimeZone == EFI_UNSPECIFIED_TIMEZONE || (Time->TimeZone >= -1440 && Time->TimeZone <= 1440))) ||
282 (Time->Daylight & (~(EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT)))
283 ) {
284 return EFI_INVALID_PARAMETER;
285 }
286
287 return EFI_SUCCESS;
288 }
289
290 BOOLEAN
291 DayValid (
292 IN EFI_TIME *Time
293 )
294 /*++
295
296 Routine Description:
297
298 TODO: Add function description
299
300 Arguments:
301
302 Time - TODO: add argument description
303
304 Returns:
305
306 TODO: add return values
307
308 --*/
309 {
310
311 static const INTN DayOfMonth[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
312
313 if (Time->Day < 1 ||
314 Time->Day > DayOfMonth[Time->Month - 1] ||
315 (Time->Month == 2 && (!IsLeapYear (Time) && Time->Day > 28))
316 ) {
317 return FALSE;
318 }
319
320 return TRUE;
321 }
322
323 BOOLEAN
324 IsLeapYear (
325 IN EFI_TIME *Time
326 )
327 /*++
328
329 Routine Description:
330
331 TODO: Add function description
332
333 Arguments:
334
335 Time - TODO: add argument description
336
337 Returns:
338
339 TODO: add return values
340
341 --*/
342 {
343 if (Time->Year % 4 == 0) {
344 if (Time->Year % 100 == 0) {
345 if (Time->Year % 400 == 0) {
346 return TRUE;
347 } else {
348 return FALSE;
349 }
350 } else {
351 return TRUE;
352 }
353 } else {
354 return FALSE;
355 }
356 }