2 RTC Architectural Protocol GUID as defined in DxeCis 0.96.
4 Copyright (c) 2006 - 2015, 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
;
110 // Acquire RTC Lock to make access to RTC atomic
112 if (!EfiAtRuntime ()) {
113 EfiAcquireLock (&Global
->RtcLock
);
116 // Initialize RTC Register
118 // Make sure Division Chain is properly configured,
119 // or RTC clock won't "tick" -- time won't increment
121 RegisterA
.Data
= RTC_INIT_REGISTER_A
;
122 RtcWrite (RTC_ADDRESS_REGISTER_A
, RegisterA
.Data
);
127 RegisterB
.Data
= RtcRead (RTC_ADDRESS_REGISTER_B
);
130 // Clear RTC flag register
132 RtcRead (RTC_ADDRESS_REGISTER_C
);
135 // Clear RTC register D
137 RegisterD
.Data
= RTC_INIT_REGISTER_D
;
138 RtcWrite (RTC_ADDRESS_REGISTER_D
, RegisterD
.Data
);
141 // Wait for up to 0.1 seconds for the RTC to be updated
143 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
144 if (EFI_ERROR (Status
)) {
146 // Set the variable with default value if the RTC is functioning incorrectly.
148 Global
->SavedTimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
149 Global
->Daylight
= 0;
150 if (!EfiAtRuntime ()) {
151 EfiReleaseLock (&Global
->RtcLock
);
153 return EFI_DEVICE_ERROR
;
156 // Get the Time/Date/Daylight Savings values.
158 Time
.Second
= RtcRead (RTC_ADDRESS_SECONDS
);
159 Time
.Minute
= RtcRead (RTC_ADDRESS_MINUTES
);
160 Time
.Hour
= RtcRead (RTC_ADDRESS_HOURS
);
161 Time
.Day
= RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH
);
162 Time
.Month
= RtcRead (RTC_ADDRESS_MONTH
);
163 Time
.Year
= RtcRead (RTC_ADDRESS_YEAR
);
166 // Set RTC configuration after get original time
167 // The value of bit AIE should be reserved.
169 RegisterB
.Data
= RTC_INIT_REGISTER_B
| (RegisterB
.Data
& BIT5
);
170 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
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
, 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
= PcdGet16 (PcdMinimalValidYear
);
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
)) {
230 return EFI_DEVICE_ERROR
;
234 // Reset wakeup time value to valid state when wakeup alarm is disabled and wakeup time is invalid.
235 // Global variable has already had valid SavedTimeZone and Daylight,
236 // so we can use them to get and set wakeup time.
238 Status
= PcRtcGetWakeupTime (&Enabled
, &Pending
, &Time
, Global
);
239 if ((Enabled
) || (!EFI_ERROR (Status
))) {
244 // When wakeup time is disabled and invalid, reset wakeup time register to valid state
245 // but keep wakeup alarm disabled.
247 Time
.Second
= RTC_INIT_SECOND
;
248 Time
.Minute
= RTC_INIT_MINUTE
;
249 Time
.Hour
= RTC_INIT_HOUR
;
250 Time
.Day
= RTC_INIT_DAY
;
251 Time
.Month
= RTC_INIT_MONTH
;
252 Time
.Year
= PcdGet16 (PcdMinimalValidYear
);
254 Time
.TimeZone
= Global
->SavedTimeZone
;
255 Time
.Daylight
= Global
->Daylight
;;
258 // Acquire RTC Lock to make access to RTC atomic
260 if (!EfiAtRuntime ()) {
261 EfiAcquireLock (&Global
->RtcLock
);
264 // Wait for up to 0.1 seconds for the RTC to be updated
266 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
267 if (EFI_ERROR (Status
)) {
268 if (!EfiAtRuntime ()) {
269 EfiReleaseLock (&Global
->RtcLock
);
271 return EFI_DEVICE_ERROR
;
274 ConvertEfiTimeToRtcTime (&Time
, RegisterB
);
277 // Set the Y/M/D info to variable as it has no corresponding hw registers.
279 Status
= EfiSetVariable (
282 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
286 if (EFI_ERROR (Status
)) {
287 if (!EfiAtRuntime ()) {
288 EfiReleaseLock (&Global
->RtcLock
);
290 return EFI_DEVICE_ERROR
;
294 // Inhibit updates of the RTC
296 RegisterB
.Bits
.Set
= 1;
297 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
300 // Set RTC alarm time registers
302 RtcWrite (RTC_ADDRESS_SECONDS_ALARM
, Time
.Second
);
303 RtcWrite (RTC_ADDRESS_MINUTES_ALARM
, Time
.Minute
);
304 RtcWrite (RTC_ADDRESS_HOURS_ALARM
, Time
.Hour
);
307 // Allow updates of the RTC registers
309 RegisterB
.Bits
.Set
= 0;
310 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
315 if (!EfiAtRuntime ()) {
316 EfiReleaseLock (&Global
->RtcLock
);
322 Returns the current time and date information, and the time-keeping capabilities
323 of the hardware platform.
325 @param Time A pointer to storage to receive a snapshot of the current time.
326 @param Capabilities An optional pointer to a buffer to receive the real time clock
327 device's capabilities.
328 @param Global For global use inside this module.
330 @retval EFI_SUCCESS The operation completed successfully.
331 @retval EFI_INVALID_PARAMETER Time is NULL.
332 @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.
338 OUT EFI_TIME_CAPABILITIES
*Capabilities
, OPTIONAL
339 IN PC_RTC_MODULE_GLOBALS
*Global
343 RTC_REGISTER_B RegisterB
;
346 // Check parameters for null pointer
349 return EFI_INVALID_PARAMETER
;
353 // Acquire RTC Lock to make access to RTC atomic
355 if (!EfiAtRuntime ()) {
356 EfiAcquireLock (&Global
->RtcLock
);
359 // Wait for up to 0.1 seconds for the RTC to be updated
361 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
362 if (EFI_ERROR (Status
)) {
363 if (!EfiAtRuntime ()) {
364 EfiReleaseLock (&Global
->RtcLock
);
371 RegisterB
.Data
= RtcRead (RTC_ADDRESS_REGISTER_B
);
374 // Get the Time/Date/Daylight Savings values.
376 Time
->Second
= RtcRead (RTC_ADDRESS_SECONDS
);
377 Time
->Minute
= RtcRead (RTC_ADDRESS_MINUTES
);
378 Time
->Hour
= RtcRead (RTC_ADDRESS_HOURS
);
379 Time
->Day
= RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH
);
380 Time
->Month
= RtcRead (RTC_ADDRESS_MONTH
);
381 Time
->Year
= RtcRead (RTC_ADDRESS_YEAR
);
386 if (!EfiAtRuntime ()) {
387 EfiReleaseLock (&Global
->RtcLock
);
391 // Get the variable that contains the TimeZone and Daylight fields
393 Time
->TimeZone
= Global
->SavedTimeZone
;
394 Time
->Daylight
= Global
->Daylight
;
397 // Make sure all field values are in correct range
399 Status
= ConvertRtcTimeToEfiTime (Time
, RegisterB
);
400 if (!EFI_ERROR (Status
)) {
401 Status
= RtcTimeFieldsValid (Time
);
403 if (EFI_ERROR (Status
)) {
404 return EFI_DEVICE_ERROR
;
408 // Fill in Capabilities if it was passed in
410 if (Capabilities
!= NULL
) {
411 Capabilities
->Resolution
= 1;
415 Capabilities
->Accuracy
= 50000000;
419 Capabilities
->SetsToZero
= FALSE
;
426 Sets the current local time and date information.
428 @param Time A pointer to the current time.
429 @param Global For global use inside this module.
431 @retval EFI_SUCCESS The operation completed successfully.
432 @retval EFI_INVALID_PARAMETER A time field is out of range.
433 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
439 IN PC_RTC_MODULE_GLOBALS
*Global
444 RTC_REGISTER_B RegisterB
;
448 return EFI_INVALID_PARAMETER
;
451 // Make sure that the time fields are valid
453 Status
= RtcTimeFieldsValid (Time
);
454 if (EFI_ERROR (Status
)) {
458 CopyMem (&RtcTime
, Time
, sizeof (EFI_TIME
));
461 // Acquire RTC Lock to make access to RTC atomic
463 if (!EfiAtRuntime ()) {
464 EfiAcquireLock (&Global
->RtcLock
);
467 // Wait for up to 0.1 seconds for the RTC to be updated
469 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
470 if (EFI_ERROR (Status
)) {
471 if (!EfiAtRuntime ()) {
472 EfiReleaseLock (&Global
->RtcLock
);
478 // Write timezone and daylight to RTC variable
480 TimerVar
= Time
->Daylight
;
481 TimerVar
= (UINT32
) ((TimerVar
<< 16) | (UINT16
)(Time
->TimeZone
));
482 Status
= EfiSetVariable (
485 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
489 if (EFI_ERROR (Status
)) {
490 if (!EfiAtRuntime ()) {
491 EfiReleaseLock (&Global
->RtcLock
);
493 return EFI_DEVICE_ERROR
;
497 // Read Register B, and inhibit updates of the RTC
499 RegisterB
.Data
= RtcRead (RTC_ADDRESS_REGISTER_B
);
500 RegisterB
.Bits
.Set
= 1;
501 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
503 ConvertEfiTimeToRtcTime (&RtcTime
, RegisterB
);
505 RtcWrite (RTC_ADDRESS_SECONDS
, RtcTime
.Second
);
506 RtcWrite (RTC_ADDRESS_MINUTES
, RtcTime
.Minute
);
507 RtcWrite (RTC_ADDRESS_HOURS
, RtcTime
.Hour
);
508 RtcWrite (RTC_ADDRESS_DAY_OF_THE_MONTH
, RtcTime
.Day
);
509 RtcWrite (RTC_ADDRESS_MONTH
, RtcTime
.Month
);
510 RtcWrite (RTC_ADDRESS_YEAR
, (UINT8
) RtcTime
.Year
);
513 // Allow updates of the RTC registers
515 RegisterB
.Bits
.Set
= 0;
516 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
521 if (!EfiAtRuntime ()) {
522 EfiReleaseLock (&Global
->RtcLock
);
525 // Set the variable that contains the TimeZone and Daylight fields
527 Global
->SavedTimeZone
= Time
->TimeZone
;
528 Global
->Daylight
= Time
->Daylight
;
534 Returns the current wakeup alarm clock setting.
536 @param Enabled Indicates if the alarm is currently enabled or disabled.
537 @param Pending Indicates if the alarm signal is pending and requires acknowledgment.
538 @param Time The current alarm setting.
539 @param Global For global use inside this module.
541 @retval EFI_SUCCESS The alarm settings were returned.
542 @retval EFI_INVALID_PARAMETER Enabled is NULL.
543 @retval EFI_INVALID_PARAMETER Pending is NULL.
544 @retval EFI_INVALID_PARAMETER Time is NULL.
545 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
546 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
551 OUT BOOLEAN
*Enabled
,
552 OUT BOOLEAN
*Pending
,
554 IN PC_RTC_MODULE_GLOBALS
*Global
558 RTC_REGISTER_B RegisterB
;
559 RTC_REGISTER_C RegisterC
;
564 // Check parameters for null pointers
566 if ((Enabled
== NULL
) || (Pending
== NULL
) || (Time
== NULL
)) {
567 return EFI_INVALID_PARAMETER
;
571 // Acquire RTC Lock to make access to RTC atomic
573 if (!EfiAtRuntime ()) {
574 EfiAcquireLock (&Global
->RtcLock
);
577 // Wait for up to 0.1 seconds for the RTC to be updated
579 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
580 if (EFI_ERROR (Status
)) {
581 if (!EfiAtRuntime ()) {
582 EfiReleaseLock (&Global
->RtcLock
);
584 return EFI_DEVICE_ERROR
;
587 // Read Register B and Register C
589 RegisterB
.Data
= RtcRead (RTC_ADDRESS_REGISTER_B
);
590 RegisterC
.Data
= RtcRead (RTC_ADDRESS_REGISTER_C
);
593 // Get the Time/Date/Daylight Savings values.
595 *Enabled
= RegisterB
.Bits
.Aie
;
596 *Pending
= RegisterC
.Bits
.Af
;
598 Time
->Second
= RtcRead (RTC_ADDRESS_SECONDS_ALARM
);
599 Time
->Minute
= RtcRead (RTC_ADDRESS_MINUTES_ALARM
);
600 Time
->Hour
= RtcRead (RTC_ADDRESS_HOURS_ALARM
);
601 Time
->Day
= RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH
);
602 Time
->Month
= RtcRead (RTC_ADDRESS_MONTH
);
603 Time
->Year
= RtcRead (RTC_ADDRESS_YEAR
);
604 Time
->TimeZone
= Global
->SavedTimeZone
;
605 Time
->Daylight
= Global
->Daylight
;
608 // Get the alarm info from variable
610 DataSize
= sizeof (EFI_TIME
);
611 Status
= EfiGetVariable (
618 if (!EFI_ERROR (Status
)) {
620 // The alarm variable exists. In this case, we read variable to get info.
622 Time
->Day
= RtcTime
.Day
;
623 Time
->Month
= RtcTime
.Month
;
624 Time
->Year
= RtcTime
.Year
;
630 if (!EfiAtRuntime ()) {
631 EfiReleaseLock (&Global
->RtcLock
);
635 // Make sure all field values are in correct range
637 Status
= ConvertRtcTimeToEfiTime (Time
, RegisterB
);
638 if (!EFI_ERROR (Status
)) {
639 Status
= RtcTimeFieldsValid (Time
);
641 if (EFI_ERROR (Status
)) {
642 return EFI_DEVICE_ERROR
;
649 Sets the system wakeup alarm clock time.
651 @param Enabled Enable or disable the wakeup alarm.
652 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
653 If Enable is FALSE, then this parameter is optional, and may be NULL.
654 @param Global For global use inside this module.
656 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled.
657 If Enable is FALSE, then the wakeup alarm was disabled.
658 @retval EFI_INVALID_PARAMETER A time field is out of range.
659 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
660 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
666 IN EFI_TIME
*Time
, OPTIONAL
667 IN PC_RTC_MODULE_GLOBALS
*Global
672 RTC_REGISTER_B RegisterB
;
673 EFI_TIME_CAPABILITIES Capabilities
;
675 ZeroMem (&RtcTime
, sizeof (RtcTime
));
680 return EFI_INVALID_PARAMETER
;
683 // Make sure that the time fields are valid
685 Status
= RtcTimeFieldsValid (Time
);
686 if (EFI_ERROR (Status
)) {
687 return EFI_INVALID_PARAMETER
;
690 // Just support set alarm time within 24 hours
692 PcRtcGetTime (&RtcTime
, &Capabilities
, Global
);
693 Status
= RtcTimeFieldsValid (&RtcTime
);
694 if (EFI_ERROR (Status
)) {
695 return EFI_DEVICE_ERROR
;
697 if (!IsWithinOneDay (&RtcTime
, Time
)) {
698 return EFI_UNSUPPORTED
;
701 // Make a local copy of the time and date
703 CopyMem (&RtcTime
, Time
, sizeof (EFI_TIME
));
707 // Acquire RTC Lock to make access to RTC atomic
709 if (!EfiAtRuntime ()) {
710 EfiAcquireLock (&Global
->RtcLock
);
713 // Wait for up to 0.1 seconds for the RTC to be updated
715 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
716 if (EFI_ERROR (Status
)) {
717 if (!EfiAtRuntime ()) {
718 EfiReleaseLock (&Global
->RtcLock
);
720 return EFI_DEVICE_ERROR
;
725 RegisterB
.Data
= RtcRead (RTC_ADDRESS_REGISTER_B
);
728 ConvertEfiTimeToRtcTime (&RtcTime
, RegisterB
);
731 // if the alarm is disable, record the current setting.
733 RtcTime
.Second
= RtcRead (RTC_ADDRESS_SECONDS_ALARM
);
734 RtcTime
.Minute
= RtcRead (RTC_ADDRESS_MINUTES_ALARM
);
735 RtcTime
.Hour
= RtcRead (RTC_ADDRESS_HOURS_ALARM
);
736 RtcTime
.Day
= RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH
);
737 RtcTime
.Month
= RtcRead (RTC_ADDRESS_MONTH
);
738 RtcTime
.Year
= RtcRead (RTC_ADDRESS_YEAR
);
739 RtcTime
.TimeZone
= Global
->SavedTimeZone
;
740 RtcTime
.Daylight
= Global
->Daylight
;
744 // Set the Y/M/D info to variable as it has no corresponding hw registers.
746 Status
= EfiSetVariable (
749 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
753 if (EFI_ERROR (Status
)) {
754 if (!EfiAtRuntime ()) {
755 EfiReleaseLock (&Global
->RtcLock
);
757 return EFI_DEVICE_ERROR
;
761 // Inhibit updates of the RTC
763 RegisterB
.Bits
.Set
= 1;
764 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
768 // Set RTC alarm time
770 RtcWrite (RTC_ADDRESS_SECONDS_ALARM
, RtcTime
.Second
);
771 RtcWrite (RTC_ADDRESS_MINUTES_ALARM
, RtcTime
.Minute
);
772 RtcWrite (RTC_ADDRESS_HOURS_ALARM
, RtcTime
.Hour
);
774 RegisterB
.Bits
.Aie
= 1;
777 RegisterB
.Bits
.Aie
= 0;
780 // Allow updates of the RTC registers
782 RegisterB
.Bits
.Set
= 0;
783 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
788 if (!EfiAtRuntime ()) {
789 EfiReleaseLock (&Global
->RtcLock
);
796 Checks an 8-bit BCD value, and converts to an 8-bit value if valid.
798 This function checks the 8-bit BCD value specified by Value.
799 If valid, the function converts it to an 8-bit value and returns it.
800 Otherwise, return 0xff.
802 @param Value The 8-bit BCD value to check and convert
804 @return The 8-bit value converted. Or 0xff if Value is invalid.
808 CheckAndConvertBcd8ToDecimal8 (
812 if ((Value
< 0xa0) && ((Value
& 0xf) < 0xa)) {
813 return BcdToDecimal8 (Value
);
820 Converts time read from RTC to EFI_TIME format defined by UEFI spec.
822 This function converts raw time data read from RTC to the EFI_TIME format
823 defined by UEFI spec.
824 If data mode of RTC is BCD, then converts it to decimal,
825 If RTC is in 12-hour format, then converts it to 24-hour format.
827 @param Time On input, the time data read from RTC to convert
828 On output, the time converted to UEFI format
829 @param RegisterB Value of Register B of RTC, indicating data mode
832 @retval EFI_INVALID_PARAMETER Parameters passed in are invalid.
833 @retval EFI_SUCCESS Convert RTC time to EFI time successfully.
837 ConvertRtcTimeToEfiTime (
838 IN OUT EFI_TIME
*Time
,
839 IN RTC_REGISTER_B RegisterB
845 if ((Time
->Hour
& 0x80) != 0) {
851 Time
->Hour
= (UINT8
) (Time
->Hour
& 0x7f);
853 if (RegisterB
.Bits
.Dm
== 0) {
854 Time
->Year
= CheckAndConvertBcd8ToDecimal8 ((UINT8
) Time
->Year
);
855 Time
->Month
= CheckAndConvertBcd8ToDecimal8 (Time
->Month
);
856 Time
->Day
= CheckAndConvertBcd8ToDecimal8 (Time
->Day
);
857 Time
->Hour
= CheckAndConvertBcd8ToDecimal8 (Time
->Hour
);
858 Time
->Minute
= CheckAndConvertBcd8ToDecimal8 (Time
->Minute
);
859 Time
->Second
= CheckAndConvertBcd8ToDecimal8 (Time
->Second
);
862 if (Time
->Year
== 0xff || Time
->Month
== 0xff || Time
->Day
== 0xff ||
863 Time
->Hour
== 0xff || Time
->Minute
== 0xff || Time
->Second
== 0xff) {
864 return EFI_INVALID_PARAMETER
;
868 // For minimal/maximum year range [1970, 2069],
869 // Century is 19 if RTC year >= 70,
870 // Century is 20 otherwise.
872 Century
= (UINT8
) (PcdGet16 (PcdMinimalValidYear
) / 100);
873 if (Time
->Year
< PcdGet16 (PcdMinimalValidYear
) % 100) {
876 Time
->Year
= (UINT16
) (Century
* 100 + Time
->Year
);
879 // If time is in 12 hour format, convert it to 24 hour format
881 if (RegisterB
.Bits
.Mil
== 0) {
882 if (IsPM
&& Time
->Hour
< 12) {
883 Time
->Hour
= (UINT8
) (Time
->Hour
+ 12);
886 if (!IsPM
&& Time
->Hour
== 12) {
891 Time
->Nanosecond
= 0;
897 Wait for a period for the RTC to be ready.
899 @param Timeout Tell how long it should take to wait.
901 @retval EFI_DEVICE_ERROR RTC device error.
902 @retval EFI_SUCCESS RTC is updated and ready.
909 RTC_REGISTER_A RegisterA
;
910 RTC_REGISTER_D RegisterD
;
913 // See if the RTC is functioning correctly
915 RegisterD
.Data
= RtcRead (RTC_ADDRESS_REGISTER_D
);
917 if (RegisterD
.Bits
.Vrt
== 0) {
918 return EFI_DEVICE_ERROR
;
921 // Wait for up to 0.1 seconds for the RTC to be ready.
923 Timeout
= (Timeout
/ 10) + 1;
924 RegisterA
.Data
= RtcRead (RTC_ADDRESS_REGISTER_A
);
925 while (RegisterA
.Bits
.Uip
== 1 && Timeout
> 0) {
926 MicroSecondDelay (10);
927 RegisterA
.Data
= RtcRead (RTC_ADDRESS_REGISTER_A
);
931 RegisterD
.Data
= RtcRead (RTC_ADDRESS_REGISTER_D
);
932 if (Timeout
== 0 || RegisterD
.Bits
.Vrt
== 0) {
933 return EFI_DEVICE_ERROR
;
940 See if all fields of a variable of EFI_TIME type is correct.
942 @param Time The time to be checked.
944 @retval EFI_INVALID_PARAMETER Some fields of Time are not correct.
945 @retval EFI_SUCCESS Time is a valid EFI_TIME variable.
953 if (Time
->Year
< PcdGet16 (PcdMinimalValidYear
) ||
954 Time
->Year
> PcdGet16 (PcdMaximalValidYear
) ||
957 (!DayValid (Time
)) ||
961 Time
->Nanosecond
> 999999999 ||
962 (!(Time
->TimeZone
== EFI_UNSPECIFIED_TIMEZONE
|| (Time
->TimeZone
>= -1440 && Time
->TimeZone
<= 1440))) ||
963 ((Time
->Daylight
& (~(EFI_TIME_ADJUST_DAYLIGHT
| EFI_TIME_IN_DAYLIGHT
))) != 0)) {
964 return EFI_INVALID_PARAMETER
;
971 See if field Day of an EFI_TIME is correct.
973 @param Time Its Day field is to be checked.
975 @retval TRUE Day field of Time is correct.
976 @retval FALSE Day field of Time is NOT correct.
999 // The validity of Time->Month field should be checked before
1001 ASSERT (Time
->Month
>=1);
1002 ASSERT (Time
->Month
<=12);
1003 if (Time
->Day
< 1 ||
1004 Time
->Day
> DayOfMonth
[Time
->Month
- 1] ||
1005 (Time
->Month
== 2 && (!IsLeapYear (Time
) && Time
->Day
> 28))
1014 Check if it is a leap year.
1016 @param Time The time to be checked.
1018 @retval TRUE It is a leap year.
1019 @retval FALSE It is NOT a leap year.
1026 if (Time
->Year
% 4 == 0) {
1027 if (Time
->Year
% 100 == 0) {
1028 if (Time
->Year
% 400 == 0) {
1042 Converts time from EFI_TIME format defined by UEFI spec to RTC's.
1044 This function converts time from EFI_TIME format defined by UEFI spec to RTC's.
1045 If data mode of RTC is BCD, then converts EFI_TIME to it.
1046 If RTC is in 12-hour format, then converts EFI_TIME to it.
1048 @param Time On input, the time data read from UEFI to convert
1049 On output, the time converted to RTC format
1050 @param RegisterB Value of Register B of RTC, indicating data mode
1053 ConvertEfiTimeToRtcTime (
1054 IN OUT EFI_TIME
*Time
,
1055 IN RTC_REGISTER_B RegisterB
1062 // Adjust hour field if RTC is in 12 hour mode
1064 if (RegisterB
.Bits
.Mil
== 0) {
1065 if (Time
->Hour
< 12) {
1069 if (Time
->Hour
>= 13) {
1070 Time
->Hour
= (UINT8
) (Time
->Hour
- 12);
1071 } else if (Time
->Hour
== 0) {
1076 // Set the Time/Date values.
1078 Time
->Year
= (UINT16
) (Time
->Year
% 100);
1080 if (RegisterB
.Bits
.Dm
== 0) {
1081 Time
->Year
= DecimalToBcd8 ((UINT8
) Time
->Year
);
1082 Time
->Month
= DecimalToBcd8 (Time
->Month
);
1083 Time
->Day
= DecimalToBcd8 (Time
->Day
);
1084 Time
->Hour
= DecimalToBcd8 (Time
->Hour
);
1085 Time
->Minute
= DecimalToBcd8 (Time
->Minute
);
1086 Time
->Second
= DecimalToBcd8 (Time
->Second
);
1089 // If we are in 12 hour mode and PM is set, then set bit 7 of the Hour field.
1091 if (RegisterB
.Bits
.Mil
== 0 && IsPM
) {
1092 Time
->Hour
= (UINT8
) (Time
->Hour
| 0x80);
1097 Compare the Hour, Minute and Second of the From time and the To time.
1099 Only compare H/M/S in EFI_TIME and ignore other fields here.
1101 @param From the first time
1102 @param To the second time
1104 @return >0 The H/M/S of the From time is later than those of To time
1105 @return ==0 The H/M/S of the From time is same as those of To time
1106 @return <0 The H/M/S of the From time is earlier than those of To time
1114 if ((From
->Hour
> To
->Hour
) ||
1115 ((From
->Hour
== To
->Hour
) && (From
->Minute
> To
->Minute
)) ||
1116 ((From
->Hour
== To
->Hour
) && (From
->Minute
== To
->Minute
) && (From
->Second
> To
->Second
))) {
1118 } else if ((From
->Hour
== To
->Hour
) && (From
->Minute
== To
->Minute
) && (From
->Second
== To
->Second
)) {
1126 To check if second date is later than first date within 24 hours.
1128 @param From the first date
1129 @param To the second date
1131 @retval TRUE From is previous to To within 24 hours.
1132 @retval FALSE From is later, or it is previous to To more than 24 hours.
1140 UINT8 DayOfMonth
[12];
1153 DayOfMonth
[10] = 30;
1154 DayOfMonth
[11] = 31;
1159 // The validity of From->Month field should be checked before
1161 ASSERT (From
->Month
>=1);
1162 ASSERT (From
->Month
<=12);
1164 if (From
->Year
== To
->Year
) {
1165 if (From
->Month
== To
->Month
) {
1166 if ((From
->Day
+ 1) == To
->Day
) {
1167 if ((CompareHMS(From
, To
) >= 0)) {
1170 } else if (From
->Day
== To
->Day
) {
1171 if ((CompareHMS(From
, To
) <= 0)) {
1175 } else if (((From
->Month
+ 1) == To
->Month
) && (To
->Day
== 1)) {
1176 if ((From
->Month
== 2) && !IsLeapYear(From
)) {
1177 if (From
->Day
== 28) {
1178 if ((CompareHMS(From
, To
) >= 0)) {
1182 } else if (From
->Day
== DayOfMonth
[From
->Month
- 1]) {
1183 if ((CompareHMS(From
, To
) >= 0)) {
1188 } else if (((From
->Year
+ 1) == To
->Year
) &&
1189 (From
->Month
== 12) &&
1190 (From
->Day
== 31) &&
1193 if ((CompareHMS(From
, To
) >= 0)) {