2 RTC Architectural Protocol GUID as defined in DxeCis 0.96.
4 Copyright (c) 2006 - 2011, 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
)) {
207 Time
.Second
= RTC_INIT_SECOND
;
208 Time
.Minute
= RTC_INIT_MINUTE
;
209 Time
.Hour
= RTC_INIT_HOUR
;
210 Time
.Day
= RTC_INIT_DAY
;
211 Time
.Month
= RTC_INIT_MONTH
;
212 Time
.Year
= RTC_INIT_YEAR
;
214 Time
.TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
219 // Reset time value according to new RTC configuration
221 Status
= PcRtcSetTime (&Time
, Global
);
222 if(!EFI_ERROR (Status
)) {
225 return EFI_DEVICE_ERROR
;
230 Returns the current time and date information, and the time-keeping capabilities
231 of the hardware platform.
233 @param Time A pointer to storage to receive a snapshot of the current time.
234 @param Capabilities An optional pointer to a buffer to receive the real time clock
235 device's capabilities.
236 @param Global For global use inside this module.
238 @retval EFI_SUCCESS The operation completed successfully.
239 @retval EFI_INVALID_PARAMETER Time is NULL.
240 @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.
246 OUT EFI_TIME_CAPABILITIES
*Capabilities
, OPTIONAL
247 IN PC_RTC_MODULE_GLOBALS
*Global
251 RTC_REGISTER_B RegisterB
;
255 // Check parameters for null pointer
258 return EFI_INVALID_PARAMETER
;
262 // Acquire RTC Lock to make access to RTC atomic
264 if (!EfiAtRuntime ()) {
265 EfiAcquireLock (&Global
->RtcLock
);
268 // Wait for up to 0.1 seconds for the RTC to be updated
270 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
271 if (EFI_ERROR (Status
)) {
272 if (!EfiAtRuntime ()) {
273 EfiReleaseLock (&Global
->RtcLock
);
280 RegisterB
.Data
= RtcRead (RTC_ADDRESS_REGISTER_B
);
283 // Get the Time/Date/Daylight Savings values.
285 Time
->Second
= RtcRead (RTC_ADDRESS_SECONDS
);
286 Time
->Minute
= RtcRead (RTC_ADDRESS_MINUTES
);
287 Time
->Hour
= RtcRead (RTC_ADDRESS_HOURS
);
288 Time
->Day
= RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH
);
289 Time
->Month
= RtcRead (RTC_ADDRESS_MONTH
);
290 Time
->Year
= RtcRead (RTC_ADDRESS_YEAR
);
292 Century
= RtcRead (RTC_ADDRESS_CENTURY
);
297 if (!EfiAtRuntime ()) {
298 EfiReleaseLock (&Global
->RtcLock
);
302 // Get the variable that contains the TimeZone and Daylight fields
304 Time
->TimeZone
= Global
->SavedTimeZone
;
305 Time
->Daylight
= Global
->Daylight
;
308 // Make sure all field values are in correct range
310 Status
= ConvertRtcTimeToEfiTime (Time
, Century
, RegisterB
);
311 if (!EFI_ERROR (Status
)) {
312 Status
= RtcTimeFieldsValid (Time
);
314 if (EFI_ERROR (Status
)) {
315 return EFI_DEVICE_ERROR
;
319 // Fill in Capabilities if it was passed in
321 if (Capabilities
!= NULL
) {
322 Capabilities
->Resolution
= 1;
326 Capabilities
->Accuracy
= 50000000;
330 Capabilities
->SetsToZero
= FALSE
;
337 Sets the current local time and date information.
339 @param Time A pointer to the current time.
340 @param Global For global use inside this module.
342 @retval EFI_SUCCESS The operation completed successfully.
343 @retval EFI_INVALID_PARAMETER A time field is out of range.
344 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
350 IN PC_RTC_MODULE_GLOBALS
*Global
355 RTC_REGISTER_B RegisterB
;
360 return EFI_INVALID_PARAMETER
;
363 // Make sure that the time fields are valid
365 Status
= RtcTimeFieldsValid (Time
);
366 if (EFI_ERROR (Status
)) {
370 CopyMem (&RtcTime
, Time
, sizeof (EFI_TIME
));
373 // Acquire RTC Lock to make access to RTC atomic
375 if (!EfiAtRuntime ()) {
376 EfiAcquireLock (&Global
->RtcLock
);
379 // Wait for up to 0.1 seconds for the RTC to be updated
381 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
382 if (EFI_ERROR (Status
)) {
383 if (!EfiAtRuntime ()) {
384 EfiReleaseLock (&Global
->RtcLock
);
389 // Read Register B, and inhibit updates of the RTC
391 RegisterB
.Data
= RtcRead (RTC_ADDRESS_REGISTER_B
);
392 RegisterB
.Bits
.Set
= 1;
393 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
395 ConvertEfiTimeToRtcTime (&RtcTime
, RegisterB
, &Century
);
397 RtcWrite (RTC_ADDRESS_SECONDS
, RtcTime
.Second
);
398 RtcWrite (RTC_ADDRESS_MINUTES
, RtcTime
.Minute
);
399 RtcWrite (RTC_ADDRESS_HOURS
, RtcTime
.Hour
);
400 RtcWrite (RTC_ADDRESS_DAY_OF_THE_MONTH
, RtcTime
.Day
);
401 RtcWrite (RTC_ADDRESS_MONTH
, RtcTime
.Month
);
402 RtcWrite (RTC_ADDRESS_YEAR
, (UINT8
) RtcTime
.Year
);
403 RtcWrite (RTC_ADDRESS_CENTURY
, Century
);
406 // Allow updates of the RTC registers
408 RegisterB
.Bits
.Set
= 0;
409 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
414 if (!EfiAtRuntime ()) {
415 EfiReleaseLock (&Global
->RtcLock
);
418 // Set the variable that contains the TimeZone and Daylight fields
420 Global
->SavedTimeZone
= Time
->TimeZone
;
421 Global
->Daylight
= Time
->Daylight
;
423 TimerVar
= Time
->Daylight
;
424 TimerVar
= (UINT32
) ((TimerVar
<< 16) | (UINT16
)(Time
->TimeZone
));
425 Status
= EfiSetVariable (
428 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
432 ASSERT_EFI_ERROR (Status
);
438 Returns the current wakeup alarm clock setting.
440 @param Enabled Indicates if the alarm is currently enabled or disabled.
441 @param Pending Indicates if the alarm signal is pending and requires acknowledgment.
442 @param Time The current alarm setting.
443 @param Global For global use inside this module.
445 @retval EFI_SUCCESS The alarm settings were returned.
446 @retval EFI_INVALID_PARAMETER Enabled is NULL.
447 @retval EFI_INVALID_PARAMETER Pending is NULL.
448 @retval EFI_INVALID_PARAMETER Time is NULL.
449 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
450 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
455 OUT BOOLEAN
*Enabled
,
456 OUT BOOLEAN
*Pending
,
458 IN PC_RTC_MODULE_GLOBALS
*Global
462 RTC_REGISTER_B RegisterB
;
463 RTC_REGISTER_C RegisterC
;
469 // Check parameters for null pointers
471 if ((Enabled
== NULL
) || (Pending
== NULL
) || (Time
== NULL
)) {
472 return EFI_INVALID_PARAMETER
;
476 // Acquire RTC Lock to make access to RTC atomic
478 if (!EfiAtRuntime ()) {
479 EfiAcquireLock (&Global
->RtcLock
);
482 // Wait for up to 0.1 seconds for the RTC to be updated
484 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
485 if (EFI_ERROR (Status
)) {
486 if (!EfiAtRuntime ()) {
487 EfiReleaseLock (&Global
->RtcLock
);
489 return EFI_DEVICE_ERROR
;
492 // Read Register B and Register C
494 RegisterB
.Data
= RtcRead (RTC_ADDRESS_REGISTER_B
);
495 RegisterC
.Data
= RtcRead (RTC_ADDRESS_REGISTER_C
);
498 // Get the Time/Date/Daylight Savings values.
500 *Enabled
= RegisterB
.Bits
.Aie
;
501 *Pending
= RegisterC
.Bits
.Af
;
503 Time
->Second
= RtcRead (RTC_ADDRESS_SECONDS_ALARM
);
504 Time
->Minute
= RtcRead (RTC_ADDRESS_MINUTES_ALARM
);
505 Time
->Hour
= RtcRead (RTC_ADDRESS_HOURS_ALARM
);
506 Time
->Day
= RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH
);
507 Time
->Month
= RtcRead (RTC_ADDRESS_MONTH
);
508 Time
->Year
= RtcRead (RTC_ADDRESS_YEAR
);
509 Time
->TimeZone
= Global
->SavedTimeZone
;
510 Time
->Daylight
= Global
->Daylight
;
512 Century
= RtcRead (RTC_ADDRESS_CENTURY
);
515 // Get the alarm info from variable
517 Status
= EfiGetVariable (
524 if (!EFI_ERROR (Status
)) {
526 // The alarm variable exists. In this case, we read variable to get info.
528 Time
->Day
= RtcTime
.Day
;
529 Time
->Month
= RtcTime
.Month
;
530 Time
->Year
= RtcTime
.Year
;
536 if (!EfiAtRuntime ()) {
537 EfiReleaseLock (&Global
->RtcLock
);
541 // Make sure all field values are in correct range
543 Status
= ConvertRtcTimeToEfiTime (Time
, Century
, RegisterB
);
544 if (!EFI_ERROR (Status
)) {
545 Status
= RtcTimeFieldsValid (Time
);
547 if (EFI_ERROR (Status
)) {
548 return EFI_DEVICE_ERROR
;
555 Sets the system wakeup alarm clock time.
557 @param Enabled Enable or disable the wakeup alarm.
558 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
559 If Enable is FALSE, then this parameter is optional, and may be NULL.
560 @param Global For global use inside this module.
562 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled.
563 If Enable is FALSE, then the wakeup alarm was disabled.
564 @retval EFI_INVALID_PARAMETER A time field is out of range.
565 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
566 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
572 IN EFI_TIME
*Time
, OPTIONAL
573 IN PC_RTC_MODULE_GLOBALS
*Global
578 RTC_REGISTER_B RegisterB
;
580 EFI_TIME_CAPABILITIES Capabilities
;
582 ZeroMem (&RtcTime
, sizeof (RtcTime
));
587 return EFI_INVALID_PARAMETER
;
590 // Make sure that the time fields are valid
592 Status
= RtcTimeFieldsValid (Time
);
593 if (EFI_ERROR (Status
)) {
594 return EFI_INVALID_PARAMETER
;
597 // Just support set alarm time within 24 hours
599 PcRtcGetTime (&RtcTime
, &Capabilities
, Global
);
600 Status
= RtcTimeFieldsValid (&RtcTime
);
601 if (EFI_ERROR (Status
)) {
602 return EFI_DEVICE_ERROR
;
604 if (!IsWithinOneDay (&RtcTime
, Time
)) {
605 return EFI_UNSUPPORTED
;
608 // Make a local copy of the time and date
610 CopyMem (&RtcTime
, Time
, sizeof (EFI_TIME
));
614 // Acquire RTC Lock to make access to RTC atomic
616 if (!EfiAtRuntime ()) {
617 EfiAcquireLock (&Global
->RtcLock
);
620 // Wait for up to 0.1 seconds for the RTC to be updated
622 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
623 if (EFI_ERROR (Status
)) {
624 if (!EfiAtRuntime ()) {
625 EfiReleaseLock (&Global
->RtcLock
);
627 return EFI_DEVICE_ERROR
;
630 // Read Register B, and inhibit updates of the RTC
632 RegisterB
.Data
= RtcRead (RTC_ADDRESS_REGISTER_B
);
634 RegisterB
.Bits
.Set
= 1;
635 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
638 ConvertEfiTimeToRtcTime (&RtcTime
, RegisterB
, &Century
);
641 // Set RTC alarm time
643 RtcWrite (RTC_ADDRESS_SECONDS_ALARM
, RtcTime
.Second
);
644 RtcWrite (RTC_ADDRESS_MINUTES_ALARM
, RtcTime
.Minute
);
645 RtcWrite (RTC_ADDRESS_HOURS_ALARM
, RtcTime
.Hour
);
647 RegisterB
.Bits
.Aie
= 1;
650 RegisterB
.Bits
.Aie
= 0;
652 // if the alarm is disable, record the current setting.
654 RtcTime
.Second
= RtcRead (RTC_ADDRESS_SECONDS_ALARM
);
655 RtcTime
.Minute
= RtcRead (RTC_ADDRESS_MINUTES_ALARM
);
656 RtcTime
.Hour
= RtcRead (RTC_ADDRESS_HOURS_ALARM
);
657 RtcTime
.Day
= RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH
);
658 RtcTime
.Month
= RtcRead (RTC_ADDRESS_MONTH
);
659 RtcTime
.Year
= RtcRead (RTC_ADDRESS_YEAR
);
660 RtcTime
.TimeZone
= Global
->SavedTimeZone
;
661 RtcTime
.Daylight
= Global
->Daylight
;
664 // Allow updates of the RTC registers
666 RegisterB
.Bits
.Set
= 0;
667 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
670 // Set the Y/M/D info to variable as it has no corresponding hw registers.
672 Status
= EfiSetVariable (
675 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
679 if (EFI_ERROR (Status
)) {
680 return EFI_DEVICE_ERROR
;
686 if (!EfiAtRuntime ()) {
687 EfiReleaseLock (&Global
->RtcLock
);
694 Checks an 8-bit BCD value, and converts to an 8-bit value if valid.
696 This function checks the 8-bit BCD value specified by Value.
697 If valid, the function converts it to an 8-bit value and returns it.
698 Otherwise, return 0xff.
700 @param Value The 8-bit BCD value to check and convert
702 @return The 8-bit value converted. Or 0xff if Value is invalid.
706 CheckAndConvertBcd8ToDecimal8 (
710 if ((Value
< 0xa0) && ((Value
& 0xf) < 0xa)) {
711 return BcdToDecimal8 (Value
);
718 Converts time read from RTC to EFI_TIME format defined by UEFI spec.
720 This function converts raw time data read from RTC to the EFI_TIME format
721 defined by UEFI spec.
722 If data mode of RTC is BCD, then converts it to decimal,
723 If RTC is in 12-hour format, then converts it to 24-hour format.
725 @param Time On input, the time data read from RTC to convert
726 On output, the time converted to UEFI format
727 @param Century Value of century read from RTC.
728 @param RegisterB Value of Register B of RTC, indicating data mode
731 @retval EFI_INVALID_PARAMETER Parameters passed in are invalid.
732 @retval EFI_SUCCESS Convert RTC time to EFI time successfully.
736 ConvertRtcTimeToEfiTime (
737 IN OUT EFI_TIME
*Time
,
739 IN RTC_REGISTER_B RegisterB
744 if ((Time
->Hour
& 0x80) != 0) {
750 Time
->Hour
= (UINT8
) (Time
->Hour
& 0x7f);
752 if (RegisterB
.Bits
.Dm
== 0) {
753 Time
->Year
= CheckAndConvertBcd8ToDecimal8 ((UINT8
) Time
->Year
);
754 Time
->Month
= CheckAndConvertBcd8ToDecimal8 (Time
->Month
);
755 Time
->Day
= CheckAndConvertBcd8ToDecimal8 (Time
->Day
);
756 Time
->Hour
= CheckAndConvertBcd8ToDecimal8 (Time
->Hour
);
757 Time
->Minute
= CheckAndConvertBcd8ToDecimal8 (Time
->Minute
);
758 Time
->Second
= CheckAndConvertBcd8ToDecimal8 (Time
->Second
);
760 Century
= CheckAndConvertBcd8ToDecimal8 (Century
);
762 if (Time
->Year
== 0xff || Time
->Month
== 0xff || Time
->Day
== 0xff ||
763 Time
->Hour
== 0xff || Time
->Minute
== 0xff || Time
->Second
== 0xff ||
765 return EFI_INVALID_PARAMETER
;
768 Time
->Year
= (UINT16
) (Century
* 100 + Time
->Year
);
771 // If time is in 12 hour format, convert it to 24 hour format
773 if (RegisterB
.Bits
.Mil
== 0) {
774 if (IsPM
&& Time
->Hour
< 12) {
775 Time
->Hour
= (UINT8
) (Time
->Hour
+ 12);
778 if (!IsPM
&& Time
->Hour
== 12) {
783 Time
->Nanosecond
= 0;
789 Wait for a period for the RTC to be ready.
791 @param Timeout Tell how long it should take to wait.
793 @retval EFI_DEVICE_ERROR RTC device error.
794 @retval EFI_SUCCESS RTC is updated and ready.
801 RTC_REGISTER_A RegisterA
;
802 RTC_REGISTER_D RegisterD
;
805 // See if the RTC is functioning correctly
807 RegisterD
.Data
= RtcRead (RTC_ADDRESS_REGISTER_D
);
809 if (RegisterD
.Bits
.Vrt
== 0) {
810 return EFI_DEVICE_ERROR
;
813 // Wait for up to 0.1 seconds for the RTC to be ready.
815 Timeout
= (Timeout
/ 10) + 1;
816 RegisterA
.Data
= RtcRead (RTC_ADDRESS_REGISTER_A
);
817 while (RegisterA
.Bits
.Uip
== 1 && Timeout
> 0) {
818 MicroSecondDelay (10);
819 RegisterA
.Data
= RtcRead (RTC_ADDRESS_REGISTER_A
);
823 RegisterD
.Data
= RtcRead (RTC_ADDRESS_REGISTER_D
);
824 if (Timeout
== 0 || RegisterD
.Bits
.Vrt
== 0) {
825 return EFI_DEVICE_ERROR
;
832 See if all fields of a variable of EFI_TIME type is correct.
834 @param Time The time to be checked.
836 @retval EFI_INVALID_PARAMETER Some fields of Time are not correct.
837 @retval EFI_SUCCESS Time is a valid EFI_TIME variable.
845 if (Time
->Year
< 1998 ||
849 (!DayValid (Time
)) ||
853 Time
->Nanosecond
> 999999999 ||
854 (!(Time
->TimeZone
== EFI_UNSPECIFIED_TIMEZONE
|| (Time
->TimeZone
>= -1440 && Time
->TimeZone
<= 1440))) ||
855 ((Time
->Daylight
& (~(EFI_TIME_ADJUST_DAYLIGHT
| EFI_TIME_IN_DAYLIGHT
))) != 0)) {
856 return EFI_INVALID_PARAMETER
;
863 See if field Day of an EFI_TIME is correct.
865 @param Time Its Day field is to be checked.
867 @retval TRUE Day field of Time is correct.
868 @retval FALSE Day field of Time is NOT correct.
891 // The validity of Time->Month field should be checked before
893 ASSERT (Time
->Month
>=1);
894 ASSERT (Time
->Month
<=12);
896 Time
->Day
> DayOfMonth
[Time
->Month
- 1] ||
897 (Time
->Month
== 2 && (!IsLeapYear (Time
) && Time
->Day
> 28))
906 Check if it is a leap year.
908 @param Time The time to be checked.
910 @retval TRUE It is a leap year.
911 @retval FALSE It is NOT a leap year.
918 if (Time
->Year
% 4 == 0) {
919 if (Time
->Year
% 100 == 0) {
920 if (Time
->Year
% 400 == 0) {
934 Converts time from EFI_TIME format defined by UEFI spec to RTC's.
936 This function converts time from EFI_TIME format defined by UEFI spec to RTC's.
937 If data mode of RTC is BCD, then converts EFI_TIME to it.
938 If RTC is in 12-hour format, then converts EFI_TIME to it.
940 @param Time On input, the time data read from UEFI to convert
941 On output, the time converted to RTC format
942 @param RegisterB Value of Register B of RTC, indicating data mode
943 @param Century It is set according to EFI_TIME Time.
947 ConvertEfiTimeToRtcTime (
948 IN OUT EFI_TIME
*Time
,
949 IN RTC_REGISTER_B RegisterB
,
957 // Adjust hour field if RTC is in 12 hour mode
959 if (RegisterB
.Bits
.Mil
== 0) {
960 if (Time
->Hour
< 12) {
964 if (Time
->Hour
>= 13) {
965 Time
->Hour
= (UINT8
) (Time
->Hour
- 12);
966 } else if (Time
->Hour
== 0) {
971 // Set the Time/Date/Daylight Savings values.
973 *Century
= DecimalToBcd8 ((UINT8
) (Time
->Year
/ 100));
975 Time
->Year
= (UINT16
) (Time
->Year
% 100);
977 if (RegisterB
.Bits
.Dm
== 0) {
978 Time
->Year
= DecimalToBcd8 ((UINT8
) Time
->Year
);
979 Time
->Month
= DecimalToBcd8 (Time
->Month
);
980 Time
->Day
= DecimalToBcd8 (Time
->Day
);
981 Time
->Hour
= DecimalToBcd8 (Time
->Hour
);
982 Time
->Minute
= DecimalToBcd8 (Time
->Minute
);
983 Time
->Second
= DecimalToBcd8 (Time
->Second
);
986 // If we are in 12 hour mode and PM is set, then set bit 7 of the Hour field.
988 if (RegisterB
.Bits
.Mil
== 0 && IsPM
) {
989 Time
->Hour
= (UINT8
) (Time
->Hour
| 0x80);
994 Compare the Hour, Minute and Second of the From time and the To time.
996 Only compare H/M/S in EFI_TIME and ignore other fields here.
998 @param From the first time
999 @param To the second time
1001 @return >0 The H/M/S of the From time is later than those of To time
1002 @return ==0 The H/M/S of the From time is same as those of To time
1003 @return <0 The H/M/S of the From time is earlier than those of To time
1011 if ((From
->Hour
> To
->Hour
) ||
1012 ((From
->Hour
== To
->Hour
) && (From
->Minute
> To
->Minute
)) ||
1013 ((From
->Hour
== To
->Hour
) && (From
->Minute
== To
->Minute
) && (From
->Second
> To
->Second
))) {
1015 } else if ((From
->Hour
== To
->Hour
) && (From
->Minute
== To
->Minute
) && (From
->Second
== To
->Second
)) {
1023 To check if second date is later than first date within 24 hours.
1025 @param From the first date
1026 @param To the second date
1028 @retval TRUE From is previous to To within 24 hours.
1029 @retval FALSE From is later, or it is previous to To more than 24 hours.
1037 UINT8 DayOfMonth
[12];
1050 DayOfMonth
[10] = 30;
1051 DayOfMonth
[11] = 31;
1056 // The validity of From->Month field should be checked before
1058 ASSERT (From
->Month
>=1);
1059 ASSERT (From
->Month
<=12);
1061 if (From
->Year
== To
->Year
) {
1062 if (From
->Month
== To
->Month
) {
1063 if ((From
->Day
+ 1) == To
->Day
) {
1064 if ((CompareHMS(From
, To
) >= 0)) {
1067 } else if (From
->Day
== To
->Day
) {
1068 if ((CompareHMS(From
, To
) <= 0)) {
1072 } else if (((From
->Month
+ 1) == To
->Month
) && (To
->Day
== 1)) {
1073 if ((From
->Month
== 2) && !IsLeapYear(From
)) {
1074 if (From
->Day
== 28) {
1075 if ((CompareHMS(From
, To
) >= 0)) {
1079 } else if (From
->Day
== DayOfMonth
[From
->Month
- 1]) {
1080 if ((CompareHMS(From
, To
) >= 0)) {
1085 } else if (((From
->Year
+ 1) == To
->Year
) &&
1086 (From
->Month
== 12) &&
1087 (From
->Day
== 31) &&
1090 if ((CompareHMS(From
, To
) >= 0)) {