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