PcAtChipsetPkg/PcRtc: Fix a Y2K bug
[mirror_edk2.git] / PcAtChipsetPkg / PcatRealTimeClockRuntimeDxe / PcRtc.c
CommitLineData
fb0b259e 1/** @file\r
2 RTC Architectural Protocol GUID as defined in DxeCis 0.96.\r
8cd4d17c 3\r
1e5fff63 4Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>\r
95d48e82 5This program and the accompanying materials\r
3cfb790c 6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
8cd4d17c 12\r
fb0b259e 13**/\r
8cd4d17c 14\r
15#include "PcRtc.h"\r
16\r
8d85dc31 17/**\r
18 Compare the Hour, Minute and Second of the From time and the To time.\r
19 \r
20 Only compare H/M/S in EFI_TIME and ignore other fields here.\r
21\r
22 @param From the first time\r
23 @param To the second time\r
24\r
25 @return >0 The H/M/S of the From time is later than those of To time\r
26 @return ==0 The H/M/S of the From time is same as those of To time\r
27 @return <0 The H/M/S of the From time is earlier than those of To time\r
28**/\r
8cd4d17c 29INTN\r
30CompareHMS (\r
31 IN EFI_TIME *From,\r
32 IN EFI_TIME *To\r
33 );\r
34\r
8d85dc31 35/**\r
36 To check if second date is later than first date within 24 hours.\r
37\r
38 @param From the first date\r
39 @param To the second date\r
40\r
41 @retval TRUE From is previous to To within 24 hours.\r
42 @retval FALSE From is later, or it is previous to To more than 24 hours.\r
43**/\r
8cd4d17c 44BOOLEAN\r
45IsWithinOneDay (\r
46 IN EFI_TIME *From,\r
47 IN EFI_TIME *To\r
48 );\r
49\r
8d85dc31 50/**\r
51 Read RTC content through its registers.\r
52\r
53 @param Address Address offset of RTC. It is recommended to use macros such as\r
54 RTC_ADDRESS_SECONDS.\r
55\r
56 @return The data of UINT8 type read from RTC.\r
57**/\r
8cd4d17c 58UINT8\r
59RtcRead (\r
60 IN UINT8 Address\r
61 )\r
8cd4d17c 62{\r
63 IoWrite8 (PCAT_RTC_ADDRESS_REGISTER, (UINT8) (Address | (UINT8) (IoRead8 (PCAT_RTC_ADDRESS_REGISTER) & 0x80)));\r
64 return IoRead8 (PCAT_RTC_DATA_REGISTER);\r
65}\r
66\r
8d85dc31 67/**\r
68 Write RTC through its registers.\r
69\r
70 @param Address Address offset of RTC. It is recommended to use macros such as\r
71 RTC_ADDRESS_SECONDS.\r
72 @param Data The content you want to write into RTC.\r
73\r
74**/\r
8cd4d17c 75VOID\r
76RtcWrite (\r
77 IN UINT8 Address,\r
78 IN UINT8 Data\r
79 )\r
8cd4d17c 80{\r
81 IoWrite8 (PCAT_RTC_ADDRESS_REGISTER, (UINT8) (Address | (UINT8) (IoRead8 (PCAT_RTC_ADDRESS_REGISTER) & 0x80)));\r
82 IoWrite8 (PCAT_RTC_DATA_REGISTER, Data);\r
83}\r
84\r
8d85dc31 85/**\r
86 Initialize RTC.\r
87\r
88 @param Global For global use inside this module.\r
89\r
90 @retval EFI_DEVICE_ERROR Initialization failed due to device error.\r
91 @retval EFI_SUCCESS Initialization successful.\r
92\r
93**/\r
8cd4d17c 94EFI_STATUS\r
95PcRtcInit (\r
96 IN PC_RTC_MODULE_GLOBALS *Global\r
97 )\r
8cd4d17c 98{\r
99 EFI_STATUS Status;\r
100 RTC_REGISTER_A RegisterA;\r
101 RTC_REGISTER_B RegisterB;\r
102 RTC_REGISTER_D RegisterD;\r
8cd4d17c 103 EFI_TIME Time;\r
ec35e997
LG
104 UINTN DataSize;\r
105 UINT32 TimerVar;\r
d431bf6e
EL
106 BOOLEAN Enabled;\r
107 BOOLEAN Pending;\r
8cd4d17c 108\r
109 //\r
110 // Acquire RTC Lock to make access to RTC atomic\r
111 //\r
8cd4d17c 112 if (!EfiAtRuntime ()) {\r
8d85dc31 113 EfiAcquireLock (&Global->RtcLock);\r
8cd4d17c 114 }\r
115 //\r
116 // Initialize RTC Register\r
117 //\r
118 // Make sure Division Chain is properly configured,\r
119 // or RTC clock won't "tick" -- time won't increment\r
120 //\r
121 RegisterA.Data = RTC_INIT_REGISTER_A;\r
122 RtcWrite (RTC_ADDRESS_REGISTER_A, RegisterA.Data);\r
123\r
124 //\r
125 // Read Register B\r
126 //\r
127 RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B);\r
128\r
129 //\r
130 // Clear RTC flag register\r
131 //\r
132 RtcRead (RTC_ADDRESS_REGISTER_C);\r
133\r
134 //\r
135 // Clear RTC register D\r
136 //\r
137 RegisterD.Data = RTC_INIT_REGISTER_D;\r
138 RtcWrite (RTC_ADDRESS_REGISTER_D, RegisterD.Data);\r
139\r
140 //\r
141 // Wait for up to 0.1 seconds for the RTC to be updated\r
142 //\r
f8ea3026 143 Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
8cd4d17c 144 if (EFI_ERROR (Status)) {\r
6ff84d99 145 //\r
146 // Set the variable with default value if the RTC is functioning incorrectly.\r
147 //\r
148 Global->SavedTimeZone = EFI_UNSPECIFIED_TIMEZONE;\r
149 Global->Daylight = 0;\r
8cd4d17c 150 if (!EfiAtRuntime ()) {\r
8d85dc31 151 EfiReleaseLock (&Global->RtcLock);\r
8cd4d17c 152 }\r
153 return EFI_DEVICE_ERROR;\r
154 }\r
155 //\r
156 // Get the Time/Date/Daylight Savings values.\r
157 //\r
158 Time.Second = RtcRead (RTC_ADDRESS_SECONDS);\r
159 Time.Minute = RtcRead (RTC_ADDRESS_MINUTES);\r
160 Time.Hour = RtcRead (RTC_ADDRESS_HOURS);\r
161 Time.Day = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH);\r
162 Time.Month = RtcRead (RTC_ADDRESS_MONTH);\r
163 Time.Year = RtcRead (RTC_ADDRESS_YEAR);\r
164\r
8cd4d17c 165 //\r
166 // Set RTC configuration after get original time\r
ec35e997 167 // The value of bit AIE should be reserved.\r
8cd4d17c 168 //\r
f7033880 169 RtcWrite (RTC_ADDRESS_REGISTER_B, (UINT8)(RTC_INIT_REGISTER_B | (RegisterB.Data & BIT5)));\r
8cd4d17c 170\r
171 //\r
172 // Release RTC Lock.\r
173 //\r
8cd4d17c 174 if (!EfiAtRuntime ()) {\r
8d85dc31 175 EfiReleaseLock (&Global->RtcLock);\r
8cd4d17c 176 }\r
254ba247 177 \r
6bfa178c 178 //\r
179 // Get the data of Daylight saving and time zone, if they have been\r
180 // stored in NV variable during previous boot.\r
181 //\r
182 DataSize = sizeof (UINT32);\r
183 Status = EfiGetVariable (\r
184 L"RTC",\r
185 &gEfiCallerIdGuid,\r
186 NULL,\r
187 &DataSize,\r
188 (VOID *) &TimerVar\r
189 );\r
190 if (!EFI_ERROR (Status)) {\r
191 Time.TimeZone = (INT16) TimerVar;\r
192 Time.Daylight = (UINT8) (TimerVar >> 16);\r
193 } else {\r
194 Time.TimeZone = EFI_UNSPECIFIED_TIMEZONE;\r
195 Time.Daylight = 0; \r
196 }\r
197\r
8cd4d17c 198 //\r
199 // Validate time fields\r
200 //\r
fe320967 201 Status = ConvertRtcTimeToEfiTime (&Time, RegisterB);\r
254ba247 202 if (!EFI_ERROR (Status)) {\r
203 Status = RtcTimeFieldsValid (&Time);\r
204 }\r
8cd4d17c 205 if (EFI_ERROR (Status)) {\r
44d52203 206 //\r
207 // Report Status Code to indicate that the RTC has bad date and time\r
208 //\r
209 REPORT_STATUS_CODE (\r
210 EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
211 (EFI_SOFTWARE_DXE_RT_DRIVER | EFI_SW_EC_BAD_DATE_TIME)\r
212 );\r
8cd4d17c 213 Time.Second = RTC_INIT_SECOND;\r
214 Time.Minute = RTC_INIT_MINUTE;\r
215 Time.Hour = RTC_INIT_HOUR;\r
216 Time.Day = RTC_INIT_DAY;\r
217 Time.Month = RTC_INIT_MONTH;\r
fe320967 218 Time.Year = PcdGet16 (PcdMinimalValidYear);\r
2ba53a4f 219 Time.Nanosecond = 0;\r
9bb1fced 220 Time.TimeZone = EFI_UNSPECIFIED_TIMEZONE;\r
221 Time.Daylight = 0;\r
8cd4d17c 222 }\r
ec35e997 223\r
ec35e997 224 //\r
8cd4d17c 225 // Reset time value according to new RTC configuration\r
226 //\r
2ba53a4f 227 Status = PcRtcSetTime (&Time, Global);\r
d431bf6e
EL
228 if (EFI_ERROR (Status)) {\r
229 return EFI_DEVICE_ERROR;\r
230 }\r
231 \r
232 //\r
233 // Reset wakeup time value to valid state when wakeup alarm is disabled and wakeup time is invalid.\r
234 // Global variable has already had valid SavedTimeZone and Daylight,\r
235 // so we can use them to get and set wakeup time.\r
236 //\r
237 Status = PcRtcGetWakeupTime (&Enabled, &Pending, &Time, Global);\r
238 if ((Enabled) || (!EFI_ERROR (Status))) {\r
2ba53a4f 239 return EFI_SUCCESS;\r
d431bf6e
EL
240 }\r
241 \r
242 //\r
243 // When wakeup time is disabled and invalid, reset wakeup time register to valid state \r
244 // but keep wakeup alarm disabled.\r
245 //\r
246 Time.Second = RTC_INIT_SECOND;\r
247 Time.Minute = RTC_INIT_MINUTE;\r
248 Time.Hour = RTC_INIT_HOUR;\r
249 Time.Day = RTC_INIT_DAY;\r
250 Time.Month = RTC_INIT_MONTH;\r
fe320967 251 Time.Year = PcdGet16 (PcdMinimalValidYear);\r
d431bf6e
EL
252 Time.Nanosecond = 0;\r
253 Time.TimeZone = Global->SavedTimeZone;\r
254 Time.Daylight = Global->Daylight;;\r
255\r
256 //\r
257 // Acquire RTC Lock to make access to RTC atomic\r
258 //\r
259 if (!EfiAtRuntime ()) {\r
260 EfiAcquireLock (&Global->RtcLock);\r
261 }\r
262 //\r
263 // Wait for up to 0.1 seconds for the RTC to be updated\r
264 //\r
265 Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
266 if (EFI_ERROR (Status)) {\r
267 if (!EfiAtRuntime ()) {\r
268 EfiReleaseLock (&Global->RtcLock);\r
269 }\r
2ba53a4f 270 return EFI_DEVICE_ERROR;\r
271 }\r
fe320967
RN
272\r
273 ConvertEfiTimeToRtcTime (&Time, RegisterB);\r
d431bf6e
EL
274\r
275 //\r
276 // Set the Y/M/D info to variable as it has no corresponding hw registers.\r
277 //\r
278 Status = EfiSetVariable (\r
279 L"RTCALARM",\r
280 &gEfiCallerIdGuid,\r
281 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
282 sizeof (Time),\r
283 &Time\r
284 );\r
285 if (EFI_ERROR (Status)) {\r
286 if (!EfiAtRuntime ()) {\r
287 EfiReleaseLock (&Global->RtcLock);\r
288 }\r
289 return EFI_DEVICE_ERROR;\r
290 }\r
291 \r
292 //\r
293 // Inhibit updates of the RTC\r
294 //\r
295 RegisterB.Bits.Set = 1;\r
296 RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
297 \r
298 //\r
299 // Set RTC alarm time registers\r
300 //\r
301 RtcWrite (RTC_ADDRESS_SECONDS_ALARM, Time.Second);\r
302 RtcWrite (RTC_ADDRESS_MINUTES_ALARM, Time.Minute);\r
303 RtcWrite (RTC_ADDRESS_HOURS_ALARM, Time.Hour);\r
304\r
305 //\r
306 // Allow updates of the RTC registers\r
307 //\r
308 RegisterB.Bits.Set = 0;\r
309 RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
310 \r
311 //\r
312 // Release RTC Lock.\r
313 //\r
314 if (!EfiAtRuntime ()) {\r
315 EfiReleaseLock (&Global->RtcLock);\r
316 }\r
317 return EFI_SUCCESS;\r
8cd4d17c 318}\r
319\r
8d85dc31 320/**\r
321 Returns the current time and date information, and the time-keeping capabilities\r
322 of the hardware platform.\r
8cd4d17c 323\r
8d85dc31 324 @param Time A pointer to storage to receive a snapshot of the current time.\r
325 @param Capabilities An optional pointer to a buffer to receive the real time clock\r
326 device's capabilities.\r
327 @param Global For global use inside this module.\r
8cd4d17c 328\r
8d85dc31 329 @retval EFI_SUCCESS The operation completed successfully.\r
330 @retval EFI_INVALID_PARAMETER Time is NULL.\r
331 @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.\r
8cd4d17c 332\r
8d85dc31 333**/\r
334EFI_STATUS\r
335PcRtcGetTime (\r
336 OUT EFI_TIME *Time,\r
337 OUT EFI_TIME_CAPABILITIES *Capabilities, OPTIONAL\r
338 IN PC_RTC_MODULE_GLOBALS *Global\r
339 )\r
8cd4d17c 340{\r
341 EFI_STATUS Status;\r
342 RTC_REGISTER_B RegisterB;\r
8cd4d17c 343\r
344 //\r
345 // Check parameters for null pointer\r
346 //\r
347 if (Time == NULL) {\r
348 return EFI_INVALID_PARAMETER;\r
349\r
350 }\r
351 //\r
352 // Acquire RTC Lock to make access to RTC atomic\r
353 //\r
8cd4d17c 354 if (!EfiAtRuntime ()) {\r
8d85dc31 355 EfiAcquireLock (&Global->RtcLock);\r
8cd4d17c 356 }\r
357 //\r
358 // Wait for up to 0.1 seconds for the RTC to be updated\r
359 //\r
f8ea3026 360 Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
8cd4d17c 361 if (EFI_ERROR (Status)) {\r
8cd4d17c 362 if (!EfiAtRuntime ()) {\r
8d85dc31 363 EfiReleaseLock (&Global->RtcLock);\r
8cd4d17c 364 }\r
365 return Status;\r
366 }\r
367 //\r
368 // Read Register B\r
369 //\r
370 RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B);\r
371\r
372 //\r
373 // Get the Time/Date/Daylight Savings values.\r
374 //\r
375 Time->Second = RtcRead (RTC_ADDRESS_SECONDS);\r
376 Time->Minute = RtcRead (RTC_ADDRESS_MINUTES);\r
377 Time->Hour = RtcRead (RTC_ADDRESS_HOURS);\r
378 Time->Day = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH);\r
379 Time->Month = RtcRead (RTC_ADDRESS_MONTH);\r
380 Time->Year = RtcRead (RTC_ADDRESS_YEAR);\r
381\r
8cd4d17c 382 //\r
383 // Release RTC Lock.\r
384 //\r
8cd4d17c 385 if (!EfiAtRuntime ()) {\r
8d85dc31 386 EfiReleaseLock (&Global->RtcLock);\r
8cd4d17c 387 }\r
6bfa178c 388\r
8cd4d17c 389 //\r
2d4117c0 390 // Get the variable that contains the TimeZone and Daylight fields\r
8cd4d17c 391 //\r
392 Time->TimeZone = Global->SavedTimeZone;\r
393 Time->Daylight = Global->Daylight;\r
394\r
395 //\r
396 // Make sure all field values are in correct range\r
397 //\r
fe320967 398 Status = ConvertRtcTimeToEfiTime (Time, RegisterB);\r
254ba247 399 if (!EFI_ERROR (Status)) {\r
400 Status = RtcTimeFieldsValid (Time);\r
401 }\r
8cd4d17c 402 if (EFI_ERROR (Status)) {\r
403 return EFI_DEVICE_ERROR;\r
404 }\r
6bfa178c 405\r
8cd4d17c 406 //\r
407 // Fill in Capabilities if it was passed in\r
408 //\r
8d85dc31 409 if (Capabilities != NULL) {\r
8cd4d17c 410 Capabilities->Resolution = 1;\r
411 //\r
412 // 1 hertz\r
413 //\r
414 Capabilities->Accuracy = 50000000;\r
415 //\r
416 // 50 ppm\r
417 //\r
418 Capabilities->SetsToZero = FALSE;\r
419 }\r
420\r
421 return EFI_SUCCESS;\r
422}\r
423\r
8d85dc31 424/**\r
425 Sets the current local time and date information.\r
426\r
427 @param Time A pointer to the current time.\r
428 @param Global For global use inside this module.\r
429\r
430 @retval EFI_SUCCESS The operation completed successfully.\r
431 @retval EFI_INVALID_PARAMETER A time field is out of range.\r
432 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.\r
433\r
434**/\r
8cd4d17c 435EFI_STATUS\r
436PcRtcSetTime (\r
437 IN EFI_TIME *Time,\r
438 IN PC_RTC_MODULE_GLOBALS *Global\r
439 )\r
8cd4d17c 440{\r
441 EFI_STATUS Status;\r
442 EFI_TIME RtcTime;\r
443 RTC_REGISTER_B RegisterB;\r
ec35e997 444 UINT32 TimerVar;\r
8cd4d17c 445\r
446 if (Time == NULL) {\r
447 return EFI_INVALID_PARAMETER;\r
448 }\r
449 //\r
450 // Make sure that the time fields are valid\r
451 //\r
452 Status = RtcTimeFieldsValid (Time);\r
453 if (EFI_ERROR (Status)) {\r
454 return Status;\r
455 }\r
456\r
457 CopyMem (&RtcTime, Time, sizeof (EFI_TIME));\r
458\r
459 //\r
460 // Acquire RTC Lock to make access to RTC atomic\r
461 //\r
8cd4d17c 462 if (!EfiAtRuntime ()) {\r
8d85dc31 463 EfiAcquireLock (&Global->RtcLock);\r
8cd4d17c 464 }\r
465 //\r
466 // Wait for up to 0.1 seconds for the RTC to be updated\r
467 //\r
f8ea3026 468 Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
8cd4d17c 469 if (EFI_ERROR (Status)) {\r
8cd4d17c 470 if (!EfiAtRuntime ()) {\r
ca08e40b 471 EfiReleaseLock (&Global->RtcLock);\r
8cd4d17c 472 }\r
473 return Status;\r
474 }\r
0e991a2f
EL
475 \r
476 //\r
477 // Write timezone and daylight to RTC variable\r
478 //\r
479 TimerVar = Time->Daylight;\r
480 TimerVar = (UINT32) ((TimerVar << 16) | (UINT16)(Time->TimeZone));\r
481 Status = EfiSetVariable (\r
482 L"RTC",\r
483 &gEfiCallerIdGuid,\r
484 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
485 sizeof (TimerVar),\r
486 &TimerVar\r
487 );\r
488 if (EFI_ERROR (Status)) {\r
489 if (!EfiAtRuntime ()) {\r
490 EfiReleaseLock (&Global->RtcLock);\r
491 }\r
492 return EFI_DEVICE_ERROR;\r
493 }\r
494\r
8cd4d17c 495 //\r
496 // Read Register B, and inhibit updates of the RTC\r
497 //\r
498 RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B);\r
24115e44 499 RegisterB.Bits.Set = 1;\r
8cd4d17c 500 RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
501\r
fe320967 502 ConvertEfiTimeToRtcTime (&RtcTime, RegisterB);\r
8cd4d17c 503\r
504 RtcWrite (RTC_ADDRESS_SECONDS, RtcTime.Second);\r
505 RtcWrite (RTC_ADDRESS_MINUTES, RtcTime.Minute);\r
506 RtcWrite (RTC_ADDRESS_HOURS, RtcTime.Hour);\r
507 RtcWrite (RTC_ADDRESS_DAY_OF_THE_MONTH, RtcTime.Day);\r
508 RtcWrite (RTC_ADDRESS_MONTH, RtcTime.Month);\r
509 RtcWrite (RTC_ADDRESS_YEAR, (UINT8) RtcTime.Year);\r
8cd4d17c 510\r
511 //\r
512 // Allow updates of the RTC registers\r
513 //\r
24115e44 514 RegisterB.Bits.Set = 0;\r
8cd4d17c 515 RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
516\r
517 //\r
518 // Release RTC Lock.\r
519 //\r
8cd4d17c 520 if (!EfiAtRuntime ()) {\r
8d85dc31 521 EfiReleaseLock (&Global->RtcLock);\r
8cd4d17c 522 }\r
523 //\r
2d4117c0 524 // Set the variable that contains the TimeZone and Daylight fields\r
8cd4d17c 525 //\r
526 Global->SavedTimeZone = Time->TimeZone;\r
527 Global->Daylight = Time->Daylight;\r
ec35e997 528\r
ec35e997 529 return EFI_SUCCESS;\r
8cd4d17c 530}\r
531\r
8d85dc31 532/**\r
533 Returns the current wakeup alarm clock setting.\r
534\r
535 @param Enabled Indicates if the alarm is currently enabled or disabled.\r
2d4117c0 536 @param Pending Indicates if the alarm signal is pending and requires acknowledgment.\r
8d85dc31 537 @param Time The current alarm setting.\r
538 @param Global For global use inside this module.\r
539\r
540 @retval EFI_SUCCESS The alarm settings were returned.\r
541 @retval EFI_INVALID_PARAMETER Enabled is NULL.\r
542 @retval EFI_INVALID_PARAMETER Pending is NULL.\r
543 @retval EFI_INVALID_PARAMETER Time is NULL.\r
544 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.\r
545 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.\r
546\r
547**/\r
8cd4d17c 548EFI_STATUS\r
549PcRtcGetWakeupTime (\r
550 OUT BOOLEAN *Enabled,\r
551 OUT BOOLEAN *Pending,\r
552 OUT EFI_TIME *Time,\r
8d85dc31 553 IN PC_RTC_MODULE_GLOBALS *Global\r
8cd4d17c 554 )\r
8cd4d17c 555{\r
556 EFI_STATUS Status;\r
557 RTC_REGISTER_B RegisterB;\r
558 RTC_REGISTER_C RegisterC;\r
7018623c 559 EFI_TIME RtcTime;\r
560 UINTN DataSize;\r
8cd4d17c 561\r
562 //\r
2d4117c0 563 // Check parameters for null pointers\r
8cd4d17c 564 //\r
565 if ((Enabled == NULL) || (Pending == NULL) || (Time == NULL)) {\r
566 return EFI_INVALID_PARAMETER;\r
567\r
568 }\r
569 //\r
570 // Acquire RTC Lock to make access to RTC atomic\r
571 //\r
8cd4d17c 572 if (!EfiAtRuntime ()) {\r
8d85dc31 573 EfiAcquireLock (&Global->RtcLock);\r
8cd4d17c 574 }\r
575 //\r
576 // Wait for up to 0.1 seconds for the RTC to be updated\r
577 //\r
f8ea3026 578 Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
8cd4d17c 579 if (EFI_ERROR (Status)) {\r
8cd4d17c 580 if (!EfiAtRuntime ()) {\r
581 EfiReleaseLock (&Global->RtcLock);\r
582 }\r
583 return EFI_DEVICE_ERROR;\r
584 }\r
585 //\r
586 // Read Register B and Register C\r
587 //\r
588 RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B);\r
589 RegisterC.Data = RtcRead (RTC_ADDRESS_REGISTER_C);\r
590\r
591 //\r
592 // Get the Time/Date/Daylight Savings values.\r
593 //\r
24115e44 594 *Enabled = RegisterB.Bits.Aie;\r
7018623c 595 *Pending = RegisterC.Bits.Af;\r
596\r
597 Time->Second = RtcRead (RTC_ADDRESS_SECONDS_ALARM);\r
598 Time->Minute = RtcRead (RTC_ADDRESS_MINUTES_ALARM);\r
599 Time->Hour = RtcRead (RTC_ADDRESS_HOURS_ALARM);\r
600 Time->Day = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH);\r
601 Time->Month = RtcRead (RTC_ADDRESS_MONTH);\r
602 Time->Year = RtcRead (RTC_ADDRESS_YEAR);\r
603 Time->TimeZone = Global->SavedTimeZone;\r
604 Time->Daylight = Global->Daylight;\r
8cd4d17c 605\r
8cd4d17c 606 //\r
7018623c 607 // Get the alarm info from variable\r
8cd4d17c 608 //\r
ffa1e783 609 DataSize = sizeof (EFI_TIME);\r
7018623c 610 Status = EfiGetVariable (\r
611 L"RTCALARM",\r
612 &gEfiCallerIdGuid,\r
613 NULL,\r
614 &DataSize,\r
615 &RtcTime\r
616 );\r
617 if (!EFI_ERROR (Status)) {\r
618 //\r
619 // The alarm variable exists. In this case, we read variable to get info.\r
620 //\r
621 Time->Day = RtcTime.Day;\r
622 Time->Month = RtcTime.Month;\r
623 Time->Year = RtcTime.Year;\r
8cd4d17c 624 }\r
6bfa178c 625\r
626 //\r
7018623c 627 // Release RTC Lock.\r
6bfa178c 628 //\r
7018623c 629 if (!EfiAtRuntime ()) {\r
630 EfiReleaseLock (&Global->RtcLock);\r
631 }\r
6bfa178c 632\r
8cd4d17c 633 //\r
634 // Make sure all field values are in correct range\r
635 //\r
fe320967 636 Status = ConvertRtcTimeToEfiTime (Time, RegisterB);\r
254ba247 637 if (!EFI_ERROR (Status)) {\r
638 Status = RtcTimeFieldsValid (Time);\r
639 }\r
8cd4d17c 640 if (EFI_ERROR (Status)) {\r
641 return EFI_DEVICE_ERROR;\r
642 }\r
643\r
8cd4d17c 644 return EFI_SUCCESS;\r
645}\r
646\r
8d85dc31 647/**\r
648 Sets the system wakeup alarm clock time.\r
649\r
650 @param Enabled Enable or disable the wakeup alarm.\r
651 @param Time If Enable is TRUE, the time to set the wakeup alarm for.\r
652 If Enable is FALSE, then this parameter is optional, and may be NULL.\r
653 @param Global For global use inside this module.\r
654\r
655 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled.\r
656 If Enable is FALSE, then the wakeup alarm was disabled.\r
657 @retval EFI_INVALID_PARAMETER A time field is out of range.\r
658 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.\r
659 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.\r
660\r
661**/\r
8cd4d17c 662EFI_STATUS\r
663PcRtcSetWakeupTime (\r
664 IN BOOLEAN Enable,\r
8d85dc31 665 IN EFI_TIME *Time, OPTIONAL\r
8cd4d17c 666 IN PC_RTC_MODULE_GLOBALS *Global\r
667 )\r
8cd4d17c 668{\r
669 EFI_STATUS Status;\r
670 EFI_TIME RtcTime;\r
671 RTC_REGISTER_B RegisterB;\r
8cd4d17c 672 EFI_TIME_CAPABILITIES Capabilities;\r
673\r
aca70749 674 ZeroMem (&RtcTime, sizeof (RtcTime));\r
7018623c 675\r
8cd4d17c 676 if (Enable) {\r
677\r
678 if (Time == NULL) {\r
679 return EFI_INVALID_PARAMETER;\r
680 }\r
681 //\r
682 // Make sure that the time fields are valid\r
683 //\r
684 Status = RtcTimeFieldsValid (Time);\r
685 if (EFI_ERROR (Status)) {\r
686 return EFI_INVALID_PARAMETER;\r
687 }\r
688 //\r
689 // Just support set alarm time within 24 hours\r
690 //\r
691 PcRtcGetTime (&RtcTime, &Capabilities, Global);\r
79864f54 692 Status = RtcTimeFieldsValid (&RtcTime);\r
693 if (EFI_ERROR (Status)) {\r
694 return EFI_DEVICE_ERROR;\r
695 }\r
8cd4d17c 696 if (!IsWithinOneDay (&RtcTime, Time)) {\r
697 return EFI_UNSUPPORTED;\r
698 }\r
699 //\r
700 // Make a local copy of the time and date\r
701 //\r
702 CopyMem (&RtcTime, Time, sizeof (EFI_TIME));\r
703\r
704 }\r
705 //\r
706 // Acquire RTC Lock to make access to RTC atomic\r
707 //\r
8cd4d17c 708 if (!EfiAtRuntime ()) {\r
8d85dc31 709 EfiAcquireLock (&Global->RtcLock);\r
8cd4d17c 710 }\r
711 //\r
712 // Wait for up to 0.1 seconds for the RTC to be updated\r
713 //\r
f8ea3026 714 Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
8cd4d17c 715 if (EFI_ERROR (Status)) {\r
8cd4d17c 716 if (!EfiAtRuntime ()) {\r
717 EfiReleaseLock (&Global->RtcLock);\r
718 }\r
719 return EFI_DEVICE_ERROR;\r
720 }\r
721 //\r
0e991a2f 722 // Read Register B\r
8cd4d17c 723 //\r
0e991a2f 724 RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B);\r
8cd4d17c 725\r
726 if (Enable) {\r
fe320967 727 ConvertEfiTimeToRtcTime (&RtcTime, RegisterB);\r
8cd4d17c 728 } else {\r
7018623c 729 //\r
730 // if the alarm is disable, record the current setting.\r
731 //\r
732 RtcTime.Second = RtcRead (RTC_ADDRESS_SECONDS_ALARM);\r
733 RtcTime.Minute = RtcRead (RTC_ADDRESS_MINUTES_ALARM);\r
734 RtcTime.Hour = RtcRead (RTC_ADDRESS_HOURS_ALARM);\r
735 RtcTime.Day = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH);\r
736 RtcTime.Month = RtcRead (RTC_ADDRESS_MONTH);\r
737 RtcTime.Year = RtcRead (RTC_ADDRESS_YEAR);\r
738 RtcTime.TimeZone = Global->SavedTimeZone;\r
739 RtcTime.Daylight = Global->Daylight;\r
8cd4d17c 740 }\r
8cd4d17c 741\r
7018623c 742 //\r
743 // Set the Y/M/D info to variable as it has no corresponding hw registers.\r
744 //\r
745 Status = EfiSetVariable (\r
746 L"RTCALARM",\r
747 &gEfiCallerIdGuid,\r
748 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
749 sizeof (RtcTime),\r
750 &RtcTime\r
751 );\r
752 if (EFI_ERROR (Status)) {\r
0e991a2f
EL
753 if (!EfiAtRuntime ()) {\r
754 EfiReleaseLock (&Global->RtcLock);\r
755 }\r
7018623c 756 return EFI_DEVICE_ERROR;\r
757 }\r
0e991a2f
EL
758 \r
759 //\r
760 // Inhibit updates of the RTC\r
761 //\r
762 RegisterB.Bits.Set = 1;\r
763 RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
764\r
765 if (Enable) {\r
766 //\r
767 // Set RTC alarm time\r
768 //\r
769 RtcWrite (RTC_ADDRESS_SECONDS_ALARM, RtcTime.Second);\r
770 RtcWrite (RTC_ADDRESS_MINUTES_ALARM, RtcTime.Minute);\r
771 RtcWrite (RTC_ADDRESS_HOURS_ALARM, RtcTime.Hour);\r
772\r
773 RegisterB.Bits.Aie = 1;\r
774\r
775 } else {\r
776 RegisterB.Bits.Aie = 0;\r
777 }\r
778 //\r
779 // Allow updates of the RTC registers\r
780 //\r
781 RegisterB.Bits.Set = 0;\r
782 RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
7018623c 783\r
8cd4d17c 784 //\r
785 // Release RTC Lock.\r
786 //\r
8cd4d17c 787 if (!EfiAtRuntime ()) {\r
8d85dc31 788 EfiReleaseLock (&Global->RtcLock);\r
8cd4d17c 789 }\r
790 return EFI_SUCCESS;\r
791}\r
792\r
8cd4d17c 793\r
254ba247 794/**\r
795 Checks an 8-bit BCD value, and converts to an 8-bit value if valid.\r
796\r
797 This function checks the 8-bit BCD value specified by Value.\r
798 If valid, the function converts it to an 8-bit value and returns it.\r
799 Otherwise, return 0xff.\r
800\r
8d85dc31 801 @param Value The 8-bit BCD value to check and convert\r
254ba247 802\r
8d85dc31 803 @return The 8-bit value converted. Or 0xff if Value is invalid.\r
254ba247 804\r
805**/\r
806UINT8\r
807CheckAndConvertBcd8ToDecimal8 (\r
808 IN UINT8 Value\r
8cd4d17c 809 )\r
254ba247 810{\r
811 if ((Value < 0xa0) && ((Value & 0xf) < 0xa)) {\r
812 return BcdToDecimal8 (Value);\r
813 }\r
8cd4d17c 814\r
254ba247 815 return 0xff;\r
816}\r
8cd4d17c 817\r
254ba247 818/**\r
819 Converts time read from RTC to EFI_TIME format defined by UEFI spec.\r
8cd4d17c 820\r
254ba247 821 This function converts raw time data read from RTC to the EFI_TIME format\r
822 defined by UEFI spec.\r
823 If data mode of RTC is BCD, then converts it to decimal,\r
824 If RTC is in 12-hour format, then converts it to 24-hour format.\r
8cd4d17c 825\r
254ba247 826 @param Time On input, the time data read from RTC to convert\r
827 On output, the time converted to UEFI format\r
254ba247 828 @param RegisterB Value of Register B of RTC, indicating data mode\r
829 and hour format.\r
8cd4d17c 830\r
8d85dc31 831 @retval EFI_INVALID_PARAMETER Parameters passed in are invalid.\r
832 @retval EFI_SUCCESS Convert RTC time to EFI time successfully.\r
833\r
254ba247 834**/\r
835EFI_STATUS\r
836ConvertRtcTimeToEfiTime (\r
837 IN OUT EFI_TIME *Time,\r
254ba247 838 IN RTC_REGISTER_B RegisterB\r
839 )\r
8cd4d17c 840{\r
8d85dc31 841 BOOLEAN IsPM;\r
fe320967 842 UINT8 Century;\r
8cd4d17c 843\r
21176834 844 if ((Time->Hour & 0x80) != 0) {\r
8d85dc31 845 IsPM = TRUE;\r
8cd4d17c 846 } else {\r
8d85dc31 847 IsPM = FALSE;\r
8cd4d17c 848 }\r
849\r
850 Time->Hour = (UINT8) (Time->Hour & 0x7f);\r
851\r
24115e44 852 if (RegisterB.Bits.Dm == 0) {\r
254ba247 853 Time->Year = CheckAndConvertBcd8ToDecimal8 ((UINT8) Time->Year);\r
854 Time->Month = CheckAndConvertBcd8ToDecimal8 (Time->Month);\r
855 Time->Day = CheckAndConvertBcd8ToDecimal8 (Time->Day);\r
856 Time->Hour = CheckAndConvertBcd8ToDecimal8 (Time->Hour);\r
857 Time->Minute = CheckAndConvertBcd8ToDecimal8 (Time->Minute);\r
858 Time->Second = CheckAndConvertBcd8ToDecimal8 (Time->Second);\r
254ba247 859 }\r
860\r
861 if (Time->Year == 0xff || Time->Month == 0xff || Time->Day == 0xff ||\r
fe320967 862 Time->Hour == 0xff || Time->Minute == 0xff || Time->Second == 0xff) {\r
254ba247 863 return EFI_INVALID_PARAMETER;\r
8cd4d17c 864 }\r
254ba247 865\r
fe320967
RN
866 //\r
867 // For minimal/maximum year range [1970, 2069],\r
868 // Century is 19 if RTC year >= 70,\r
869 // Century is 20 otherwise.\r
870 //\r
871 Century = (UINT8) (PcdGet16 (PcdMinimalValidYear) / 100);\r
872 if (Time->Year < PcdGet16 (PcdMinimalValidYear) % 100) {\r
873 Century++;\r
874 }\r
254ba247 875 Time->Year = (UINT16) (Century * 100 + Time->Year);\r
876\r
8cd4d17c 877 //\r
878 // If time is in 12 hour format, convert it to 24 hour format\r
879 //\r
24115e44 880 if (RegisterB.Bits.Mil == 0) {\r
8d85dc31 881 if (IsPM && Time->Hour < 12) {\r
8cd4d17c 882 Time->Hour = (UINT8) (Time->Hour + 12);\r
883 }\r
884\r
8d85dc31 885 if (!IsPM && Time->Hour == 12) {\r
8cd4d17c 886 Time->Hour = 0;\r
887 }\r
888 }\r
889\r
890 Time->Nanosecond = 0;\r
254ba247 891\r
892 return EFI_SUCCESS;\r
8cd4d17c 893}\r
894\r
8d85dc31 895/**\r
896 Wait for a period for the RTC to be ready.\r
897\r
898 @param Timeout Tell how long it should take to wait.\r
899\r
900 @retval EFI_DEVICE_ERROR RTC device error.\r
901 @retval EFI_SUCCESS RTC is updated and ready. \r
902**/\r
8cd4d17c 903EFI_STATUS\r
904RtcWaitToUpdate (\r
905 UINTN Timeout\r
906 )\r
8cd4d17c 907{\r
908 RTC_REGISTER_A RegisterA;\r
909 RTC_REGISTER_D RegisterD;\r
910\r
911 //\r
912 // See if the RTC is functioning correctly\r
913 //\r
914 RegisterD.Data = RtcRead (RTC_ADDRESS_REGISTER_D);\r
915\r
24115e44 916 if (RegisterD.Bits.Vrt == 0) {\r
8cd4d17c 917 return EFI_DEVICE_ERROR;\r
918 }\r
919 //\r
920 // Wait for up to 0.1 seconds for the RTC to be ready.\r
921 //\r
922 Timeout = (Timeout / 10) + 1;\r
923 RegisterA.Data = RtcRead (RTC_ADDRESS_REGISTER_A);\r
24115e44 924 while (RegisterA.Bits.Uip == 1 && Timeout > 0) {\r
8cd4d17c 925 MicroSecondDelay (10);\r
926 RegisterA.Data = RtcRead (RTC_ADDRESS_REGISTER_A);\r
927 Timeout--;\r
928 }\r
929\r
930 RegisterD.Data = RtcRead (RTC_ADDRESS_REGISTER_D);\r
24115e44 931 if (Timeout == 0 || RegisterD.Bits.Vrt == 0) {\r
8cd4d17c 932 return EFI_DEVICE_ERROR;\r
933 }\r
934\r
935 return EFI_SUCCESS;\r
936}\r
937\r
8d85dc31 938/**\r
939 See if all fields of a variable of EFI_TIME type is correct.\r
940\r
941 @param Time The time to be checked.\r
942\r
943 @retval EFI_INVALID_PARAMETER Some fields of Time are not correct.\r
944 @retval EFI_SUCCESS Time is a valid EFI_TIME variable.\r
945\r
946**/\r
8cd4d17c 947EFI_STATUS\r
948RtcTimeFieldsValid (\r
949 IN EFI_TIME *Time\r
950 )\r
8cd4d17c 951{\r
1e5fff63
EL
952 if (Time->Year < PcdGet16 (PcdMinimalValidYear) ||\r
953 Time->Year > PcdGet16 (PcdMaximalValidYear) ||\r
8cd4d17c 954 Time->Month < 1 ||\r
955 Time->Month > 12 ||\r
6bfa178c 956 (!DayValid (Time)) ||\r
8cd4d17c 957 Time->Hour > 23 ||\r
958 Time->Minute > 59 ||\r
959 Time->Second > 59 ||\r
960 Time->Nanosecond > 999999999 ||\r
961 (!(Time->TimeZone == EFI_UNSPECIFIED_TIMEZONE || (Time->TimeZone >= -1440 && Time->TimeZone <= 1440))) ||\r
6bfa178c 962 ((Time->Daylight & (~(EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT))) != 0)) {\r
963 return EFI_INVALID_PARAMETER;\r
8cd4d17c 964 }\r
965\r
966 return EFI_SUCCESS;\r
967}\r
968\r
8d85dc31 969/**\r
970 See if field Day of an EFI_TIME is correct.\r
971\r
972 @param Time Its Day field is to be checked.\r
973\r
974 @retval TRUE Day field of Time is correct.\r
975 @retval FALSE Day field of Time is NOT correct.\r
976**/\r
8cd4d17c 977BOOLEAN\r
978DayValid (\r
979 IN EFI_TIME *Time\r
980 )\r
8cd4d17c 981{\r
558bfebb 982 INTN DayOfMonth[12];\r
983\r
984 DayOfMonth[0] = 31;\r
985 DayOfMonth[1] = 29;\r
986 DayOfMonth[2] = 31;\r
987 DayOfMonth[3] = 30;\r
988 DayOfMonth[4] = 31;\r
989 DayOfMonth[5] = 30;\r
990 DayOfMonth[6] = 31;\r
991 DayOfMonth[7] = 31;\r
992 DayOfMonth[8] = 30;\r
993 DayOfMonth[9] = 31;\r
994 DayOfMonth[10] = 30;\r
995 DayOfMonth[11] = 31;\r
8cd4d17c 996\r
96a5ac5b 997 //\r
998 // The validity of Time->Month field should be checked before\r
999 //\r
1000 ASSERT (Time->Month >=1);\r
1001 ASSERT (Time->Month <=12);\r
8cd4d17c 1002 if (Time->Day < 1 ||\r
1003 Time->Day > DayOfMonth[Time->Month - 1] ||\r
1004 (Time->Month == 2 && (!IsLeapYear (Time) && Time->Day > 28))\r
1005 ) {\r
1006 return FALSE;\r
1007 }\r
1008\r
1009 return TRUE;\r
1010}\r
1011\r
8d85dc31 1012/**\r
2d4117c0 1013 Check if it is a leap year.\r
8d85dc31 1014\r
1015 @param Time The time to be checked.\r
1016\r
2d4117c0 1017 @retval TRUE It is a leap year.\r
1018 @retval FALSE It is NOT a leap year.\r
8d85dc31 1019**/\r
8cd4d17c 1020BOOLEAN\r
1021IsLeapYear (\r
1022 IN EFI_TIME *Time\r
1023 )\r
8cd4d17c 1024{\r
1025 if (Time->Year % 4 == 0) {\r
1026 if (Time->Year % 100 == 0) {\r
1027 if (Time->Year % 400 == 0) {\r
1028 return TRUE;\r
1029 } else {\r
1030 return FALSE;\r
1031 }\r
1032 } else {\r
1033 return TRUE;\r
1034 }\r
1035 } else {\r
1036 return FALSE;\r
1037 }\r
1038}\r
1039\r
8d85dc31 1040/**\r
1041 Converts time from EFI_TIME format defined by UEFI spec to RTC's.\r
8cd4d17c 1042\r
8d85dc31 1043 This function converts time from EFI_TIME format defined by UEFI spec to RTC's.\r
1044 If data mode of RTC is BCD, then converts EFI_TIME to it.\r
1045 If RTC is in 12-hour format, then converts EFI_TIME to it.\r
8cd4d17c 1046\r
8d85dc31 1047 @param Time On input, the time data read from UEFI to convert\r
1048 On output, the time converted to RTC format\r
1049 @param RegisterB Value of Register B of RTC, indicating data mode\r
8d85dc31 1050**/\r
1051VOID\r
1052ConvertEfiTimeToRtcTime (\r
1053 IN OUT EFI_TIME *Time,\r
fe320967 1054 IN RTC_REGISTER_B RegisterB\r
8d85dc31 1055 )\r
8cd4d17c 1056{\r
8d85dc31 1057 BOOLEAN IsPM;\r
8cd4d17c 1058\r
8d85dc31 1059 IsPM = TRUE;\r
8cd4d17c 1060 //\r
8d85dc31 1061 // Adjust hour field if RTC is in 12 hour mode\r
8cd4d17c 1062 //\r
24115e44 1063 if (RegisterB.Bits.Mil == 0) {\r
8cd4d17c 1064 if (Time->Hour < 12) {\r
8d85dc31 1065 IsPM = FALSE;\r
8cd4d17c 1066 }\r
1067\r
1068 if (Time->Hour >= 13) {\r
1069 Time->Hour = (UINT8) (Time->Hour - 12);\r
1070 } else if (Time->Hour == 0) {\r
1071 Time->Hour = 12;\r
1072 }\r
1073 }\r
1074 //\r
fe320967 1075 // Set the Time/Date values.\r
8cd4d17c 1076 //\r
8cd4d17c 1077 Time->Year = (UINT16) (Time->Year % 100);\r
1078\r
24115e44 1079 if (RegisterB.Bits.Dm == 0) {\r
8cd4d17c 1080 Time->Year = DecimalToBcd8 ((UINT8) Time->Year);\r
1081 Time->Month = DecimalToBcd8 (Time->Month);\r
1082 Time->Day = DecimalToBcd8 (Time->Day);\r
1083 Time->Hour = DecimalToBcd8 (Time->Hour);\r
1084 Time->Minute = DecimalToBcd8 (Time->Minute);\r
1085 Time->Second = DecimalToBcd8 (Time->Second);\r
1086 }\r
1087 //\r
1088 // If we are in 12 hour mode and PM is set, then set bit 7 of the Hour field.\r
1089 //\r
24115e44 1090 if (RegisterB.Bits.Mil == 0 && IsPM) {\r
8cd4d17c 1091 Time->Hour = (UINT8) (Time->Hour | 0x80);\r
1092 }\r
1093}\r
1094\r
8d85dc31 1095/**\r
1096 Compare the Hour, Minute and Second of the From time and the To time.\r
1097 \r
1098 Only compare H/M/S in EFI_TIME and ignore other fields here.\r
1099\r
1100 @param From the first time\r
1101 @param To the second time\r
1102\r
1103 @return >0 The H/M/S of the From time is later than those of To time\r
1104 @return ==0 The H/M/S of the From time is same as those of To time\r
1105 @return <0 The H/M/S of the From time is earlier than those of To time\r
1106**/\r
8cd4d17c 1107INTN\r
1108CompareHMS (\r
1109 IN EFI_TIME *From,\r
1110 IN EFI_TIME *To\r
1111 )\r
8cd4d17c 1112{\r
1113 if ((From->Hour > To->Hour) ||\r
1114 ((From->Hour == To->Hour) && (From->Minute > To->Minute)) ||\r
1115 ((From->Hour == To->Hour) && (From->Minute == To->Minute) && (From->Second > To->Second))) {\r
1116 return 1;\r
1117 } else if ((From->Hour == To->Hour) && (From->Minute == To->Minute) && (From->Second == To->Second)) {\r
1118 return 0;\r
1119 } else {\r
1120 return -1;\r
1121 }\r
1122}\r
1123\r
8d85dc31 1124/**\r
1125 To check if second date is later than first date within 24 hours.\r
1126\r
1127 @param From the first date\r
1128 @param To the second date\r
1129\r
1130 @retval TRUE From is previous to To within 24 hours.\r
1131 @retval FALSE From is later, or it is previous to To more than 24 hours.\r
1132**/\r
8cd4d17c 1133BOOLEAN\r
1134IsWithinOneDay (\r
1135 IN EFI_TIME *From,\r
1136 IN EFI_TIME *To\r
1137 )\r
8cd4d17c 1138{\r
558bfebb 1139 UINT8 DayOfMonth[12];\r
1f4cf7b1 1140 BOOLEAN Adjacent;\r
1141\r
558bfebb 1142 DayOfMonth[0] = 31;\r
1143 DayOfMonth[1] = 29;\r
1144 DayOfMonth[2] = 31;\r
1145 DayOfMonth[3] = 30;\r
1146 DayOfMonth[4] = 31;\r
1147 DayOfMonth[5] = 30;\r
1148 DayOfMonth[6] = 31;\r
1149 DayOfMonth[7] = 31;\r
1150 DayOfMonth[8] = 30;\r
1151 DayOfMonth[9] = 31;\r
1152 DayOfMonth[10] = 30;\r
1153 DayOfMonth[11] = 31;\r
1154\r
1f4cf7b1 1155 Adjacent = FALSE;\r
8cd4d17c 1156\r
96a5ac5b 1157 //\r
4cfc3293 1158 // The validity of From->Month field should be checked before\r
96a5ac5b 1159 //\r
1160 ASSERT (From->Month >=1);\r
1161 ASSERT (From->Month <=12);\r
1162 \r
8cd4d17c 1163 if (From->Year == To->Year) {\r
1164 if (From->Month == To->Month) {\r
1165 if ((From->Day + 1) == To->Day) {\r
1166 if ((CompareHMS(From, To) >= 0)) {\r
1167 Adjacent = TRUE;\r
1168 }\r
1169 } else if (From->Day == To->Day) {\r
1170 if ((CompareHMS(From, To) <= 0)) {\r
1171 Adjacent = TRUE;\r
1172 }\r
1173 }\r
1174 } else if (((From->Month + 1) == To->Month) && (To->Day == 1)) {\r
1175 if ((From->Month == 2) && !IsLeapYear(From)) {\r
1176 if (From->Day == 28) {\r
1177 if ((CompareHMS(From, To) >= 0)) {\r
1178 Adjacent = TRUE;\r
1179 }\r
1180 }\r
1181 } else if (From->Day == DayOfMonth[From->Month - 1]) {\r
1182 if ((CompareHMS(From, To) >= 0)) {\r
1183 Adjacent = TRUE;\r
1184 }\r
1185 }\r
1186 }\r
1187 } else if (((From->Year + 1) == To->Year) &&\r
1188 (From->Month == 12) &&\r
1189 (From->Day == 31) &&\r
1190 (To->Month == 1) &&\r
1191 (To->Day == 1)) {\r
1192 if ((CompareHMS(From, To) >= 0)) {\r
1193 Adjacent = TRUE;\r
1194 }\r
1195 }\r
1196\r
1197 return Adjacent;\r
1198}\r
1199\r