2 RTC Architectural Protocol GUID as defined in DxeCis 0.96.
4 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
5 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
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.
18 Compare the Hour, Minute and Second of the From time and the To time.
20 Only compare H/M/S in EFI_TIME and ignore other fields here.
22 @param From the first time
23 @param To the second time
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
36 To check if second date is later than first date within 24 hours.
38 @param From the first date
39 @param To the second date
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.
51 Read RTC content through its registers.
53 @param Address Address offset of RTC. It is recommended to use macros such as
56 @return The data of UINT8 type read from RTC.
63 IoWrite8 (PCAT_RTC_ADDRESS_REGISTER
, (UINT8
) (Address
| (UINT8
) (IoRead8 (PCAT_RTC_ADDRESS_REGISTER
) & 0x80)));
64 return IoRead8 (PCAT_RTC_DATA_REGISTER
);
68 Write RTC through its registers.
70 @param Address Address offset of RTC. It is recommended to use macros such as
72 @param Data The content you want to write into RTC.
81 IoWrite8 (PCAT_RTC_ADDRESS_REGISTER
, (UINT8
) (Address
| (UINT8
) (IoRead8 (PCAT_RTC_ADDRESS_REGISTER
) & 0x80)));
82 IoWrite8 (PCAT_RTC_DATA_REGISTER
, Data
);
88 @param Global For global use inside this module.
90 @retval EFI_DEVICE_ERROR Initialization failed due to device error.
91 @retval EFI_SUCCESS Initialization successful.
96 IN PC_RTC_MODULE_GLOBALS
*Global
100 RTC_REGISTER_A RegisterA
;
101 RTC_REGISTER_B RegisterB
;
102 RTC_REGISTER_D RegisterD
;
109 // Acquire RTC Lock to make access to RTC atomic
111 if (!EfiAtRuntime ()) {
112 EfiAcquireLock (&Global
->RtcLock
);
115 // Initialize RTC Register
117 // Make sure Division Chain is properly configured,
118 // or RTC clock won't "tick" -- time won't increment
120 RegisterA
.Data
= RTC_INIT_REGISTER_A
;
121 RtcWrite (RTC_ADDRESS_REGISTER_A
, RegisterA
.Data
);
126 RegisterB
.Data
= RtcRead (RTC_ADDRESS_REGISTER_B
);
129 // Clear RTC flag register
131 RtcRead (RTC_ADDRESS_REGISTER_C
);
134 // Clear RTC register D
136 RegisterD
.Data
= RTC_INIT_REGISTER_D
;
137 RtcWrite (RTC_ADDRESS_REGISTER_D
, RegisterD
.Data
);
140 // Wait for up to 0.1 seconds for the RTC to be updated
142 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
143 if (EFI_ERROR (Status
)) {
145 // Set the variable with default value if the RTC is functioning incorrectly.
147 Global
->SavedTimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
148 Global
->Daylight
= 0;
149 if (!EfiAtRuntime ()) {
150 EfiReleaseLock (&Global
->RtcLock
);
152 return EFI_DEVICE_ERROR
;
155 // Get the Time/Date/Daylight Savings values.
157 Time
.Second
= RtcRead (RTC_ADDRESS_SECONDS
);
158 Time
.Minute
= RtcRead (RTC_ADDRESS_MINUTES
);
159 Time
.Hour
= RtcRead (RTC_ADDRESS_HOURS
);
160 Time
.Day
= RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH
);
161 Time
.Month
= RtcRead (RTC_ADDRESS_MONTH
);
162 Time
.Year
= RtcRead (RTC_ADDRESS_YEAR
);
164 Century
= RtcRead (RTC_ADDRESS_CENTURY
);
167 // Set RTC configuration after get original time
168 // The value of bit AIE should be reserved.
170 RtcWrite (RTC_ADDRESS_REGISTER_B
, (UINT8
)(RTC_INIT_REGISTER_B
| (RegisterB
.Data
& BIT5
)));
175 if (!EfiAtRuntime ()) {
176 EfiReleaseLock (&Global
->RtcLock
);
180 // Get the data of Daylight saving and time zone, if they have been
181 // stored in NV variable during previous boot.
183 DataSize
= sizeof (UINT32
);
184 Status
= EfiGetVariable (
191 if (!EFI_ERROR (Status
)) {
192 Time
.TimeZone
= (INT16
) TimerVar
;
193 Time
.Daylight
= (UINT8
) (TimerVar
>> 16);
195 Time
.TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
200 // Validate time fields
202 Status
= ConvertRtcTimeToEfiTime (&Time
, Century
, RegisterB
);
203 if (!EFI_ERROR (Status
)) {
204 Status
= RtcTimeFieldsValid (&Time
);
206 if (EFI_ERROR (Status
)) {
208 // Report Status Code to indicate that the RTC has bad date and time
211 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
212 (EFI_SOFTWARE_DXE_RT_DRIVER
| EFI_SW_EC_BAD_DATE_TIME
)
214 Time
.Second
= RTC_INIT_SECOND
;
215 Time
.Minute
= RTC_INIT_MINUTE
;
216 Time
.Hour
= RTC_INIT_HOUR
;
217 Time
.Day
= RTC_INIT_DAY
;
218 Time
.Month
= RTC_INIT_MONTH
;
219 Time
.Year
= RTC_INIT_YEAR
;
221 Time
.TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
226 // Reset time value according to new RTC configuration
228 Status
= PcRtcSetTime (&Time
, Global
);
229 if(!EFI_ERROR (Status
)) {
232 return EFI_DEVICE_ERROR
;
237 Returns the current time and date information, and the time-keeping capabilities
238 of the hardware platform.
240 @param Time A pointer to storage to receive a snapshot of the current time.
241 @param Capabilities An optional pointer to a buffer to receive the real time clock
242 device's capabilities.
243 @param Global For global use inside this module.
245 @retval EFI_SUCCESS The operation completed successfully.
246 @retval EFI_INVALID_PARAMETER Time is NULL.
247 @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.
253 OUT EFI_TIME_CAPABILITIES
*Capabilities
, OPTIONAL
254 IN PC_RTC_MODULE_GLOBALS
*Global
258 RTC_REGISTER_B RegisterB
;
262 // Check parameters for null pointer
265 return EFI_INVALID_PARAMETER
;
269 // Acquire RTC Lock to make access to RTC atomic
271 if (!EfiAtRuntime ()) {
272 EfiAcquireLock (&Global
->RtcLock
);
275 // Wait for up to 0.1 seconds for the RTC to be updated
277 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
278 if (EFI_ERROR (Status
)) {
279 if (!EfiAtRuntime ()) {
280 EfiReleaseLock (&Global
->RtcLock
);
287 RegisterB
.Data
= RtcRead (RTC_ADDRESS_REGISTER_B
);
290 // Get the Time/Date/Daylight Savings values.
292 Time
->Second
= RtcRead (RTC_ADDRESS_SECONDS
);
293 Time
->Minute
= RtcRead (RTC_ADDRESS_MINUTES
);
294 Time
->Hour
= RtcRead (RTC_ADDRESS_HOURS
);
295 Time
->Day
= RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH
);
296 Time
->Month
= RtcRead (RTC_ADDRESS_MONTH
);
297 Time
->Year
= RtcRead (RTC_ADDRESS_YEAR
);
299 Century
= RtcRead (RTC_ADDRESS_CENTURY
);
304 if (!EfiAtRuntime ()) {
305 EfiReleaseLock (&Global
->RtcLock
);
309 // Get the variable that contains the TimeZone and Daylight fields
311 Time
->TimeZone
= Global
->SavedTimeZone
;
312 Time
->Daylight
= Global
->Daylight
;
315 // Make sure all field values are in correct range
317 Status
= ConvertRtcTimeToEfiTime (Time
, Century
, RegisterB
);
318 if (!EFI_ERROR (Status
)) {
319 Status
= RtcTimeFieldsValid (Time
);
321 if (EFI_ERROR (Status
)) {
322 return EFI_DEVICE_ERROR
;
326 // Fill in Capabilities if it was passed in
328 if (Capabilities
!= NULL
) {
329 Capabilities
->Resolution
= 1;
333 Capabilities
->Accuracy
= 50000000;
337 Capabilities
->SetsToZero
= FALSE
;
344 Sets the current local time and date information.
346 @param Time A pointer to the current time.
347 @param Global For global use inside this module.
349 @retval EFI_SUCCESS The operation completed successfully.
350 @retval EFI_INVALID_PARAMETER A time field is out of range.
351 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
357 IN PC_RTC_MODULE_GLOBALS
*Global
362 RTC_REGISTER_B RegisterB
;
367 return EFI_INVALID_PARAMETER
;
370 // Make sure that the time fields are valid
372 Status
= RtcTimeFieldsValid (Time
);
373 if (EFI_ERROR (Status
)) {
377 CopyMem (&RtcTime
, Time
, sizeof (EFI_TIME
));
380 // Acquire RTC Lock to make access to RTC atomic
382 if (!EfiAtRuntime ()) {
383 EfiAcquireLock (&Global
->RtcLock
);
386 // Wait for up to 0.1 seconds for the RTC to be updated
388 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
389 if (EFI_ERROR (Status
)) {
390 if (!EfiAtRuntime ()) {
391 EfiReleaseLock (&Global
->RtcLock
);
397 // Write timezone and daylight to RTC variable
399 TimerVar
= Time
->Daylight
;
400 TimerVar
= (UINT32
) ((TimerVar
<< 16) | (UINT16
)(Time
->TimeZone
));
401 Status
= EfiSetVariable (
404 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
408 if (EFI_ERROR (Status
)) {
409 if (!EfiAtRuntime ()) {
410 EfiReleaseLock (&Global
->RtcLock
);
412 return EFI_DEVICE_ERROR
;
416 // Read Register B, and inhibit updates of the RTC
418 RegisterB
.Data
= RtcRead (RTC_ADDRESS_REGISTER_B
);
419 RegisterB
.Bits
.Set
= 1;
420 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
422 ConvertEfiTimeToRtcTime (&RtcTime
, RegisterB
, &Century
);
424 RtcWrite (RTC_ADDRESS_SECONDS
, RtcTime
.Second
);
425 RtcWrite (RTC_ADDRESS_MINUTES
, RtcTime
.Minute
);
426 RtcWrite (RTC_ADDRESS_HOURS
, RtcTime
.Hour
);
427 RtcWrite (RTC_ADDRESS_DAY_OF_THE_MONTH
, RtcTime
.Day
);
428 RtcWrite (RTC_ADDRESS_MONTH
, RtcTime
.Month
);
429 RtcWrite (RTC_ADDRESS_YEAR
, (UINT8
) RtcTime
.Year
);
430 RtcWrite (RTC_ADDRESS_CENTURY
, Century
);
433 // Allow updates of the RTC registers
435 RegisterB
.Bits
.Set
= 0;
436 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
441 if (!EfiAtRuntime ()) {
442 EfiReleaseLock (&Global
->RtcLock
);
445 // Set the variable that contains the TimeZone and Daylight fields
447 Global
->SavedTimeZone
= Time
->TimeZone
;
448 Global
->Daylight
= Time
->Daylight
;
454 Returns the current wakeup alarm clock setting.
456 @param Enabled Indicates if the alarm is currently enabled or disabled.
457 @param Pending Indicates if the alarm signal is pending and requires acknowledgment.
458 @param Time The current alarm setting.
459 @param Global For global use inside this module.
461 @retval EFI_SUCCESS The alarm settings were returned.
462 @retval EFI_INVALID_PARAMETER Enabled is NULL.
463 @retval EFI_INVALID_PARAMETER Pending is NULL.
464 @retval EFI_INVALID_PARAMETER Time is NULL.
465 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
466 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
471 OUT BOOLEAN
*Enabled
,
472 OUT BOOLEAN
*Pending
,
474 IN PC_RTC_MODULE_GLOBALS
*Global
478 RTC_REGISTER_B RegisterB
;
479 RTC_REGISTER_C RegisterC
;
485 // Check parameters for null pointers
487 if ((Enabled
== NULL
) || (Pending
== NULL
) || (Time
== NULL
)) {
488 return EFI_INVALID_PARAMETER
;
492 // Acquire RTC Lock to make access to RTC atomic
494 if (!EfiAtRuntime ()) {
495 EfiAcquireLock (&Global
->RtcLock
);
498 // Wait for up to 0.1 seconds for the RTC to be updated
500 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
501 if (EFI_ERROR (Status
)) {
502 if (!EfiAtRuntime ()) {
503 EfiReleaseLock (&Global
->RtcLock
);
505 return EFI_DEVICE_ERROR
;
508 // Read Register B and Register C
510 RegisterB
.Data
= RtcRead (RTC_ADDRESS_REGISTER_B
);
511 RegisterC
.Data
= RtcRead (RTC_ADDRESS_REGISTER_C
);
514 // Get the Time/Date/Daylight Savings values.
516 *Enabled
= RegisterB
.Bits
.Aie
;
517 *Pending
= RegisterC
.Bits
.Af
;
519 Time
->Second
= RtcRead (RTC_ADDRESS_SECONDS_ALARM
);
520 Time
->Minute
= RtcRead (RTC_ADDRESS_MINUTES_ALARM
);
521 Time
->Hour
= RtcRead (RTC_ADDRESS_HOURS_ALARM
);
522 Time
->Day
= RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH
);
523 Time
->Month
= RtcRead (RTC_ADDRESS_MONTH
);
524 Time
->Year
= RtcRead (RTC_ADDRESS_YEAR
);
525 Time
->TimeZone
= Global
->SavedTimeZone
;
526 Time
->Daylight
= Global
->Daylight
;
528 Century
= RtcRead (RTC_ADDRESS_CENTURY
);
531 // Get the alarm info from variable
533 DataSize
= sizeof (EFI_TIME
);
534 Status
= EfiGetVariable (
541 if (!EFI_ERROR (Status
)) {
543 // The alarm variable exists. In this case, we read variable to get info.
545 Time
->Day
= RtcTime
.Day
;
546 Time
->Month
= RtcTime
.Month
;
547 Time
->Year
= RtcTime
.Year
;
553 if (!EfiAtRuntime ()) {
554 EfiReleaseLock (&Global
->RtcLock
);
558 // Make sure all field values are in correct range
560 Status
= ConvertRtcTimeToEfiTime (Time
, Century
, RegisterB
);
561 if (!EFI_ERROR (Status
)) {
562 Status
= RtcTimeFieldsValid (Time
);
564 if (EFI_ERROR (Status
)) {
565 return EFI_DEVICE_ERROR
;
572 Sets the system wakeup alarm clock time.
574 @param Enabled Enable or disable the wakeup alarm.
575 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
576 If Enable is FALSE, then this parameter is optional, and may be NULL.
577 @param Global For global use inside this module.
579 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled.
580 If Enable is FALSE, then the wakeup alarm was disabled.
581 @retval EFI_INVALID_PARAMETER A time field is out of range.
582 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
583 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
589 IN EFI_TIME
*Time
, OPTIONAL
590 IN PC_RTC_MODULE_GLOBALS
*Global
595 RTC_REGISTER_B RegisterB
;
597 EFI_TIME_CAPABILITIES Capabilities
;
599 ZeroMem (&RtcTime
, sizeof (RtcTime
));
604 return EFI_INVALID_PARAMETER
;
607 // Make sure that the time fields are valid
609 Status
= RtcTimeFieldsValid (Time
);
610 if (EFI_ERROR (Status
)) {
611 return EFI_INVALID_PARAMETER
;
614 // Just support set alarm time within 24 hours
616 PcRtcGetTime (&RtcTime
, &Capabilities
, Global
);
617 Status
= RtcTimeFieldsValid (&RtcTime
);
618 if (EFI_ERROR (Status
)) {
619 return EFI_DEVICE_ERROR
;
621 if (!IsWithinOneDay (&RtcTime
, Time
)) {
622 return EFI_UNSUPPORTED
;
625 // Make a local copy of the time and date
627 CopyMem (&RtcTime
, Time
, sizeof (EFI_TIME
));
631 // Acquire RTC Lock to make access to RTC atomic
633 if (!EfiAtRuntime ()) {
634 EfiAcquireLock (&Global
->RtcLock
);
637 // Wait for up to 0.1 seconds for the RTC to be updated
639 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
640 if (EFI_ERROR (Status
)) {
641 if (!EfiAtRuntime ()) {
642 EfiReleaseLock (&Global
->RtcLock
);
644 return EFI_DEVICE_ERROR
;
649 RegisterB
.Data
= RtcRead (RTC_ADDRESS_REGISTER_B
);
652 ConvertEfiTimeToRtcTime (&RtcTime
, RegisterB
, &Century
);
655 // if the alarm is disable, record the current setting.
657 RtcTime
.Second
= RtcRead (RTC_ADDRESS_SECONDS_ALARM
);
658 RtcTime
.Minute
= RtcRead (RTC_ADDRESS_MINUTES_ALARM
);
659 RtcTime
.Hour
= RtcRead (RTC_ADDRESS_HOURS_ALARM
);
660 RtcTime
.Day
= RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH
);
661 RtcTime
.Month
= RtcRead (RTC_ADDRESS_MONTH
);
662 RtcTime
.Year
= RtcRead (RTC_ADDRESS_YEAR
);
663 RtcTime
.TimeZone
= Global
->SavedTimeZone
;
664 RtcTime
.Daylight
= Global
->Daylight
;
668 // Set the Y/M/D info to variable as it has no corresponding hw registers.
670 Status
= EfiSetVariable (
673 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
677 if (EFI_ERROR (Status
)) {
678 if (!EfiAtRuntime ()) {
679 EfiReleaseLock (&Global
->RtcLock
);
681 return EFI_DEVICE_ERROR
;
685 // Inhibit updates of the RTC
687 RegisterB
.Bits
.Set
= 1;
688 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
692 // Set RTC alarm time
694 RtcWrite (RTC_ADDRESS_SECONDS_ALARM
, RtcTime
.Second
);
695 RtcWrite (RTC_ADDRESS_MINUTES_ALARM
, RtcTime
.Minute
);
696 RtcWrite (RTC_ADDRESS_HOURS_ALARM
, RtcTime
.Hour
);
698 RegisterB
.Bits
.Aie
= 1;
701 RegisterB
.Bits
.Aie
= 0;
704 // Allow updates of the RTC registers
706 RegisterB
.Bits
.Set
= 0;
707 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
712 if (!EfiAtRuntime ()) {
713 EfiReleaseLock (&Global
->RtcLock
);
720 Checks an 8-bit BCD value, and converts to an 8-bit value if valid.
722 This function checks the 8-bit BCD value specified by Value.
723 If valid, the function converts it to an 8-bit value and returns it.
724 Otherwise, return 0xff.
726 @param Value The 8-bit BCD value to check and convert
728 @return The 8-bit value converted. Or 0xff if Value is invalid.
732 CheckAndConvertBcd8ToDecimal8 (
736 if ((Value
< 0xa0) && ((Value
& 0xf) < 0xa)) {
737 return BcdToDecimal8 (Value
);
744 Converts time read from RTC to EFI_TIME format defined by UEFI spec.
746 This function converts raw time data read from RTC to the EFI_TIME format
747 defined by UEFI spec.
748 If data mode of RTC is BCD, then converts it to decimal,
749 If RTC is in 12-hour format, then converts it to 24-hour format.
751 @param Time On input, the time data read from RTC to convert
752 On output, the time converted to UEFI format
753 @param Century Value of century read from RTC.
754 @param RegisterB Value of Register B of RTC, indicating data mode
757 @retval EFI_INVALID_PARAMETER Parameters passed in are invalid.
758 @retval EFI_SUCCESS Convert RTC time to EFI time successfully.
762 ConvertRtcTimeToEfiTime (
763 IN OUT EFI_TIME
*Time
,
765 IN RTC_REGISTER_B RegisterB
770 if ((Time
->Hour
& 0x80) != 0) {
776 Time
->Hour
= (UINT8
) (Time
->Hour
& 0x7f);
778 if (RegisterB
.Bits
.Dm
== 0) {
779 Time
->Year
= CheckAndConvertBcd8ToDecimal8 ((UINT8
) Time
->Year
);
780 Time
->Month
= CheckAndConvertBcd8ToDecimal8 (Time
->Month
);
781 Time
->Day
= CheckAndConvertBcd8ToDecimal8 (Time
->Day
);
782 Time
->Hour
= CheckAndConvertBcd8ToDecimal8 (Time
->Hour
);
783 Time
->Minute
= CheckAndConvertBcd8ToDecimal8 (Time
->Minute
);
784 Time
->Second
= CheckAndConvertBcd8ToDecimal8 (Time
->Second
);
786 Century
= CheckAndConvertBcd8ToDecimal8 (Century
);
788 if (Time
->Year
== 0xff || Time
->Month
== 0xff || Time
->Day
== 0xff ||
789 Time
->Hour
== 0xff || Time
->Minute
== 0xff || Time
->Second
== 0xff ||
791 return EFI_INVALID_PARAMETER
;
794 Time
->Year
= (UINT16
) (Century
* 100 + Time
->Year
);
797 // If time is in 12 hour format, convert it to 24 hour format
799 if (RegisterB
.Bits
.Mil
== 0) {
800 if (IsPM
&& Time
->Hour
< 12) {
801 Time
->Hour
= (UINT8
) (Time
->Hour
+ 12);
804 if (!IsPM
&& Time
->Hour
== 12) {
809 Time
->Nanosecond
= 0;
815 Wait for a period for the RTC to be ready.
817 @param Timeout Tell how long it should take to wait.
819 @retval EFI_DEVICE_ERROR RTC device error.
820 @retval EFI_SUCCESS RTC is updated and ready.
827 RTC_REGISTER_A RegisterA
;
828 RTC_REGISTER_D RegisterD
;
831 // See if the RTC is functioning correctly
833 RegisterD
.Data
= RtcRead (RTC_ADDRESS_REGISTER_D
);
835 if (RegisterD
.Bits
.Vrt
== 0) {
836 return EFI_DEVICE_ERROR
;
839 // Wait for up to 0.1 seconds for the RTC to be ready.
841 Timeout
= (Timeout
/ 10) + 1;
842 RegisterA
.Data
= RtcRead (RTC_ADDRESS_REGISTER_A
);
843 while (RegisterA
.Bits
.Uip
== 1 && Timeout
> 0) {
844 MicroSecondDelay (10);
845 RegisterA
.Data
= RtcRead (RTC_ADDRESS_REGISTER_A
);
849 RegisterD
.Data
= RtcRead (RTC_ADDRESS_REGISTER_D
);
850 if (Timeout
== 0 || RegisterD
.Bits
.Vrt
== 0) {
851 return EFI_DEVICE_ERROR
;
858 See if all fields of a variable of EFI_TIME type is correct.
860 @param Time The time to be checked.
862 @retval EFI_INVALID_PARAMETER Some fields of Time are not correct.
863 @retval EFI_SUCCESS Time is a valid EFI_TIME variable.
871 if (Time
->Year
< 1998 ||
875 (!DayValid (Time
)) ||
879 Time
->Nanosecond
> 999999999 ||
880 (!(Time
->TimeZone
== EFI_UNSPECIFIED_TIMEZONE
|| (Time
->TimeZone
>= -1440 && Time
->TimeZone
<= 1440))) ||
881 ((Time
->Daylight
& (~(EFI_TIME_ADJUST_DAYLIGHT
| EFI_TIME_IN_DAYLIGHT
))) != 0)) {
882 return EFI_INVALID_PARAMETER
;
889 See if field Day of an EFI_TIME is correct.
891 @param Time Its Day field is to be checked.
893 @retval TRUE Day field of Time is correct.
894 @retval FALSE Day field of Time is NOT correct.
917 // The validity of Time->Month field should be checked before
919 ASSERT (Time
->Month
>=1);
920 ASSERT (Time
->Month
<=12);
922 Time
->Day
> DayOfMonth
[Time
->Month
- 1] ||
923 (Time
->Month
== 2 && (!IsLeapYear (Time
) && Time
->Day
> 28))
932 Check if it is a leap year.
934 @param Time The time to be checked.
936 @retval TRUE It is a leap year.
937 @retval FALSE It is NOT a leap year.
944 if (Time
->Year
% 4 == 0) {
945 if (Time
->Year
% 100 == 0) {
946 if (Time
->Year
% 400 == 0) {
960 Converts time from EFI_TIME format defined by UEFI spec to RTC's.
962 This function converts time from EFI_TIME format defined by UEFI spec to RTC's.
963 If data mode of RTC is BCD, then converts EFI_TIME to it.
964 If RTC is in 12-hour format, then converts EFI_TIME to it.
966 @param Time On input, the time data read from UEFI to convert
967 On output, the time converted to RTC format
968 @param RegisterB Value of Register B of RTC, indicating data mode
969 @param Century It is set according to EFI_TIME Time.
973 ConvertEfiTimeToRtcTime (
974 IN OUT EFI_TIME
*Time
,
975 IN RTC_REGISTER_B RegisterB
,
983 // Adjust hour field if RTC is in 12 hour mode
985 if (RegisterB
.Bits
.Mil
== 0) {
986 if (Time
->Hour
< 12) {
990 if (Time
->Hour
>= 13) {
991 Time
->Hour
= (UINT8
) (Time
->Hour
- 12);
992 } else if (Time
->Hour
== 0) {
997 // Set the Time/Date/Daylight Savings values.
999 *Century
= DecimalToBcd8 ((UINT8
) (Time
->Year
/ 100));
1001 Time
->Year
= (UINT16
) (Time
->Year
% 100);
1003 if (RegisterB
.Bits
.Dm
== 0) {
1004 Time
->Year
= DecimalToBcd8 ((UINT8
) Time
->Year
);
1005 Time
->Month
= DecimalToBcd8 (Time
->Month
);
1006 Time
->Day
= DecimalToBcd8 (Time
->Day
);
1007 Time
->Hour
= DecimalToBcd8 (Time
->Hour
);
1008 Time
->Minute
= DecimalToBcd8 (Time
->Minute
);
1009 Time
->Second
= DecimalToBcd8 (Time
->Second
);
1012 // If we are in 12 hour mode and PM is set, then set bit 7 of the Hour field.
1014 if (RegisterB
.Bits
.Mil
== 0 && IsPM
) {
1015 Time
->Hour
= (UINT8
) (Time
->Hour
| 0x80);
1020 Compare the Hour, Minute and Second of the From time and the To time.
1022 Only compare H/M/S in EFI_TIME and ignore other fields here.
1024 @param From the first time
1025 @param To the second time
1027 @return >0 The H/M/S of the From time is later than those of To time
1028 @return ==0 The H/M/S of the From time is same as those of To time
1029 @return <0 The H/M/S of the From time is earlier than those of To time
1037 if ((From
->Hour
> To
->Hour
) ||
1038 ((From
->Hour
== To
->Hour
) && (From
->Minute
> To
->Minute
)) ||
1039 ((From
->Hour
== To
->Hour
) && (From
->Minute
== To
->Minute
) && (From
->Second
> To
->Second
))) {
1041 } else if ((From
->Hour
== To
->Hour
) && (From
->Minute
== To
->Minute
) && (From
->Second
== To
->Second
)) {
1049 To check if second date is later than first date within 24 hours.
1051 @param From the first date
1052 @param To the second date
1054 @retval TRUE From is previous to To within 24 hours.
1055 @retval FALSE From is later, or it is previous to To more than 24 hours.
1063 UINT8 DayOfMonth
[12];
1076 DayOfMonth
[10] = 30;
1077 DayOfMonth
[11] = 31;
1082 // The validity of From->Month field should be checked before
1084 ASSERT (From
->Month
>=1);
1085 ASSERT (From
->Month
<=12);
1087 if (From
->Year
== To
->Year
) {
1088 if (From
->Month
== To
->Month
) {
1089 if ((From
->Day
+ 1) == To
->Day
) {
1090 if ((CompareHMS(From
, To
) >= 0)) {
1093 } else if (From
->Day
== To
->Day
) {
1094 if ((CompareHMS(From
, To
) <= 0)) {
1098 } else if (((From
->Month
+ 1) == To
->Month
) && (To
->Day
== 1)) {
1099 if ((From
->Month
== 2) && !IsLeapYear(From
)) {
1100 if (From
->Day
== 28) {
1101 if ((CompareHMS(From
, To
) >= 0)) {
1105 } else if (From
->Day
== DayOfMonth
[From
->Month
- 1]) {
1106 if ((CompareHMS(From
, To
) >= 0)) {
1111 } else if (((From
->Year
+ 1) == To
->Year
) &&
1112 (From
->Month
== 12) &&
1113 (From
->Day
== 31) &&
1116 if ((CompareHMS(From
, To
) >= 0)) {