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 RtcWrite (RTC_ADDRESS_REGISTER_B
, (UINT8
)(RTC_INIT_REGISTER_B
| (RegisterB
.Data
& BIT5
)));
174 if (!EfiAtRuntime ()) {
175 EfiReleaseLock (&Global
->RtcLock
);
179 // Get the data of Daylight saving and time zone, if they have been
180 // stored in NV variable during previous boot.
182 DataSize
= sizeof (UINT32
);
183 Status
= EfiGetVariable (
190 if (!EFI_ERROR (Status
)) {
191 Time
.TimeZone
= (INT16
) TimerVar
;
192 Time
.Daylight
= (UINT8
) (TimerVar
>> 16);
194 Time
.TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
199 // Validate time fields
201 Status
= ConvertRtcTimeToEfiTime (&Time
, RegisterB
);
202 if (!EFI_ERROR (Status
)) {
203 Status
= RtcTimeFieldsValid (&Time
);
205 if (EFI_ERROR (Status
)) {
207 // Report Status Code to indicate that the RTC has bad date and time
210 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
211 (EFI_SOFTWARE_DXE_RT_DRIVER
| EFI_SW_EC_BAD_DATE_TIME
)
213 Time
.Second
= RTC_INIT_SECOND
;
214 Time
.Minute
= RTC_INIT_MINUTE
;
215 Time
.Hour
= RTC_INIT_HOUR
;
216 Time
.Day
= RTC_INIT_DAY
;
217 Time
.Month
= RTC_INIT_MONTH
;
218 Time
.Year
= PcdGet16 (PcdMinimalValidYear
);
220 Time
.TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
225 // Reset time value according to new RTC configuration
227 Status
= PcRtcSetTime (&Time
, Global
);
228 if (EFI_ERROR (Status
)) {
229 return EFI_DEVICE_ERROR
;
233 // Reset wakeup time value to valid state when wakeup alarm is disabled and wakeup time is invalid.
234 // Global variable has already had valid SavedTimeZone and Daylight,
235 // so we can use them to get and set wakeup time.
237 Status
= PcRtcGetWakeupTime (&Enabled
, &Pending
, &Time
, Global
);
238 if ((Enabled
) || (!EFI_ERROR (Status
))) {
243 // When wakeup time is disabled and invalid, reset wakeup time register to valid state
244 // but keep wakeup alarm disabled.
246 Time
.Second
= RTC_INIT_SECOND
;
247 Time
.Minute
= RTC_INIT_MINUTE
;
248 Time
.Hour
= RTC_INIT_HOUR
;
249 Time
.Day
= RTC_INIT_DAY
;
250 Time
.Month
= RTC_INIT_MONTH
;
251 Time
.Year
= PcdGet16 (PcdMinimalValidYear
);
253 Time
.TimeZone
= Global
->SavedTimeZone
;
254 Time
.Daylight
= Global
->Daylight
;;
257 // Acquire RTC Lock to make access to RTC atomic
259 if (!EfiAtRuntime ()) {
260 EfiAcquireLock (&Global
->RtcLock
);
263 // Wait for up to 0.1 seconds for the RTC to be updated
265 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
266 if (EFI_ERROR (Status
)) {
267 if (!EfiAtRuntime ()) {
268 EfiReleaseLock (&Global
->RtcLock
);
270 return EFI_DEVICE_ERROR
;
273 ConvertEfiTimeToRtcTime (&Time
, RegisterB
);
276 // Set the Y/M/D info to variable as it has no corresponding hw registers.
278 Status
= EfiSetVariable (
281 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
285 if (EFI_ERROR (Status
)) {
286 if (!EfiAtRuntime ()) {
287 EfiReleaseLock (&Global
->RtcLock
);
289 return EFI_DEVICE_ERROR
;
293 // Inhibit updates of the RTC
295 RegisterB
.Bits
.Set
= 1;
296 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
299 // Set RTC alarm time registers
301 RtcWrite (RTC_ADDRESS_SECONDS_ALARM
, Time
.Second
);
302 RtcWrite (RTC_ADDRESS_MINUTES_ALARM
, Time
.Minute
);
303 RtcWrite (RTC_ADDRESS_HOURS_ALARM
, Time
.Hour
);
306 // Allow updates of the RTC registers
308 RegisterB
.Bits
.Set
= 0;
309 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
314 if (!EfiAtRuntime ()) {
315 EfiReleaseLock (&Global
->RtcLock
);
321 Returns the current time and date information, and the time-keeping capabilities
322 of the hardware platform.
324 @param Time A pointer to storage to receive a snapshot of the current time.
325 @param Capabilities An optional pointer to a buffer to receive the real time clock
326 device's capabilities.
327 @param Global For global use inside this module.
329 @retval EFI_SUCCESS The operation completed successfully.
330 @retval EFI_INVALID_PARAMETER Time is NULL.
331 @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.
337 OUT EFI_TIME_CAPABILITIES
*Capabilities
, OPTIONAL
338 IN PC_RTC_MODULE_GLOBALS
*Global
342 RTC_REGISTER_B RegisterB
;
345 // Check parameters for null pointer
348 return EFI_INVALID_PARAMETER
;
352 // Acquire RTC Lock to make access to RTC atomic
354 if (!EfiAtRuntime ()) {
355 EfiAcquireLock (&Global
->RtcLock
);
358 // Wait for up to 0.1 seconds for the RTC to be updated
360 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
361 if (EFI_ERROR (Status
)) {
362 if (!EfiAtRuntime ()) {
363 EfiReleaseLock (&Global
->RtcLock
);
370 RegisterB
.Data
= RtcRead (RTC_ADDRESS_REGISTER_B
);
373 // Get the Time/Date/Daylight Savings values.
375 Time
->Second
= RtcRead (RTC_ADDRESS_SECONDS
);
376 Time
->Minute
= RtcRead (RTC_ADDRESS_MINUTES
);
377 Time
->Hour
= RtcRead (RTC_ADDRESS_HOURS
);
378 Time
->Day
= RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH
);
379 Time
->Month
= RtcRead (RTC_ADDRESS_MONTH
);
380 Time
->Year
= RtcRead (RTC_ADDRESS_YEAR
);
385 if (!EfiAtRuntime ()) {
386 EfiReleaseLock (&Global
->RtcLock
);
390 // Get the variable that contains the TimeZone and Daylight fields
392 Time
->TimeZone
= Global
->SavedTimeZone
;
393 Time
->Daylight
= Global
->Daylight
;
396 // Make sure all field values are in correct range
398 Status
= ConvertRtcTimeToEfiTime (Time
, RegisterB
);
399 if (!EFI_ERROR (Status
)) {
400 Status
= RtcTimeFieldsValid (Time
);
402 if (EFI_ERROR (Status
)) {
403 return EFI_DEVICE_ERROR
;
407 // Fill in Capabilities if it was passed in
409 if (Capabilities
!= NULL
) {
410 Capabilities
->Resolution
= 1;
414 Capabilities
->Accuracy
= 50000000;
418 Capabilities
->SetsToZero
= FALSE
;
425 Sets the current local time and date information.
427 @param Time A pointer to the current time.
428 @param Global For global use inside this module.
430 @retval EFI_SUCCESS The operation completed successfully.
431 @retval EFI_INVALID_PARAMETER A time field is out of range.
432 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
438 IN PC_RTC_MODULE_GLOBALS
*Global
443 RTC_REGISTER_B RegisterB
;
447 return EFI_INVALID_PARAMETER
;
450 // Make sure that the time fields are valid
452 Status
= RtcTimeFieldsValid (Time
);
453 if (EFI_ERROR (Status
)) {
457 CopyMem (&RtcTime
, Time
, sizeof (EFI_TIME
));
460 // Acquire RTC Lock to make access to RTC atomic
462 if (!EfiAtRuntime ()) {
463 EfiAcquireLock (&Global
->RtcLock
);
466 // Wait for up to 0.1 seconds for the RTC to be updated
468 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
469 if (EFI_ERROR (Status
)) {
470 if (!EfiAtRuntime ()) {
471 EfiReleaseLock (&Global
->RtcLock
);
477 // Write timezone and daylight to RTC variable
479 TimerVar
= Time
->Daylight
;
480 TimerVar
= (UINT32
) ((TimerVar
<< 16) | (UINT16
)(Time
->TimeZone
));
481 Status
= EfiSetVariable (
484 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
488 if (EFI_ERROR (Status
)) {
489 if (!EfiAtRuntime ()) {
490 EfiReleaseLock (&Global
->RtcLock
);
492 return EFI_DEVICE_ERROR
;
496 // Read Register B, and inhibit updates of the RTC
498 RegisterB
.Data
= RtcRead (RTC_ADDRESS_REGISTER_B
);
499 RegisterB
.Bits
.Set
= 1;
500 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
502 ConvertEfiTimeToRtcTime (&RtcTime
, RegisterB
);
504 RtcWrite (RTC_ADDRESS_SECONDS
, RtcTime
.Second
);
505 RtcWrite (RTC_ADDRESS_MINUTES
, RtcTime
.Minute
);
506 RtcWrite (RTC_ADDRESS_HOURS
, RtcTime
.Hour
);
507 RtcWrite (RTC_ADDRESS_DAY_OF_THE_MONTH
, RtcTime
.Day
);
508 RtcWrite (RTC_ADDRESS_MONTH
, RtcTime
.Month
);
509 RtcWrite (RTC_ADDRESS_YEAR
, (UINT8
) RtcTime
.Year
);
512 // Allow updates of the RTC registers
514 RegisterB
.Bits
.Set
= 0;
515 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
520 if (!EfiAtRuntime ()) {
521 EfiReleaseLock (&Global
->RtcLock
);
524 // Set the variable that contains the TimeZone and Daylight fields
526 Global
->SavedTimeZone
= Time
->TimeZone
;
527 Global
->Daylight
= Time
->Daylight
;
533 Returns the current wakeup alarm clock setting.
535 @param Enabled Indicates if the alarm is currently enabled or disabled.
536 @param Pending Indicates if the alarm signal is pending and requires acknowledgment.
537 @param Time The current alarm setting.
538 @param Global For global use inside this module.
540 @retval EFI_SUCCESS The alarm settings were returned.
541 @retval EFI_INVALID_PARAMETER Enabled is NULL.
542 @retval EFI_INVALID_PARAMETER Pending is NULL.
543 @retval EFI_INVALID_PARAMETER Time is NULL.
544 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
545 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
550 OUT BOOLEAN
*Enabled
,
551 OUT BOOLEAN
*Pending
,
553 IN PC_RTC_MODULE_GLOBALS
*Global
557 RTC_REGISTER_B RegisterB
;
558 RTC_REGISTER_C RegisterC
;
563 // Check parameters for null pointers
565 if ((Enabled
== NULL
) || (Pending
== NULL
) || (Time
== NULL
)) {
566 return EFI_INVALID_PARAMETER
;
570 // Acquire RTC Lock to make access to RTC atomic
572 if (!EfiAtRuntime ()) {
573 EfiAcquireLock (&Global
->RtcLock
);
576 // Wait for up to 0.1 seconds for the RTC to be updated
578 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
579 if (EFI_ERROR (Status
)) {
580 if (!EfiAtRuntime ()) {
581 EfiReleaseLock (&Global
->RtcLock
);
583 return EFI_DEVICE_ERROR
;
586 // Read Register B and Register C
588 RegisterB
.Data
= RtcRead (RTC_ADDRESS_REGISTER_B
);
589 RegisterC
.Data
= RtcRead (RTC_ADDRESS_REGISTER_C
);
592 // Get the Time/Date/Daylight Savings values.
594 *Enabled
= RegisterB
.Bits
.Aie
;
595 *Pending
= RegisterC
.Bits
.Af
;
597 Time
->Second
= RtcRead (RTC_ADDRESS_SECONDS_ALARM
);
598 Time
->Minute
= RtcRead (RTC_ADDRESS_MINUTES_ALARM
);
599 Time
->Hour
= RtcRead (RTC_ADDRESS_HOURS_ALARM
);
600 Time
->Day
= RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH
);
601 Time
->Month
= RtcRead (RTC_ADDRESS_MONTH
);
602 Time
->Year
= RtcRead (RTC_ADDRESS_YEAR
);
603 Time
->TimeZone
= Global
->SavedTimeZone
;
604 Time
->Daylight
= Global
->Daylight
;
607 // Get the alarm info from variable
609 DataSize
= sizeof (EFI_TIME
);
610 Status
= EfiGetVariable (
617 if (!EFI_ERROR (Status
)) {
619 // The alarm variable exists. In this case, we read variable to get info.
621 Time
->Day
= RtcTime
.Day
;
622 Time
->Month
= RtcTime
.Month
;
623 Time
->Year
= RtcTime
.Year
;
629 if (!EfiAtRuntime ()) {
630 EfiReleaseLock (&Global
->RtcLock
);
634 // Make sure all field values are in correct range
636 Status
= ConvertRtcTimeToEfiTime (Time
, RegisterB
);
637 if (!EFI_ERROR (Status
)) {
638 Status
= RtcTimeFieldsValid (Time
);
640 if (EFI_ERROR (Status
)) {
641 return EFI_DEVICE_ERROR
;
648 Sets the system wakeup alarm clock time.
650 @param Enabled Enable or disable the wakeup alarm.
651 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
652 If Enable is FALSE, then this parameter is optional, and may be NULL.
653 @param Global For global use inside this module.
655 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled.
656 If Enable is FALSE, then the wakeup alarm was disabled.
657 @retval EFI_INVALID_PARAMETER A time field is out of range.
658 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
659 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
665 IN EFI_TIME
*Time
, OPTIONAL
666 IN PC_RTC_MODULE_GLOBALS
*Global
671 RTC_REGISTER_B RegisterB
;
672 EFI_TIME_CAPABILITIES Capabilities
;
674 ZeroMem (&RtcTime
, sizeof (RtcTime
));
679 return EFI_INVALID_PARAMETER
;
682 // Make sure that the time fields are valid
684 Status
= RtcTimeFieldsValid (Time
);
685 if (EFI_ERROR (Status
)) {
686 return EFI_INVALID_PARAMETER
;
689 // Just support set alarm time within 24 hours
691 PcRtcGetTime (&RtcTime
, &Capabilities
, Global
);
692 Status
= RtcTimeFieldsValid (&RtcTime
);
693 if (EFI_ERROR (Status
)) {
694 return EFI_DEVICE_ERROR
;
696 if (!IsWithinOneDay (&RtcTime
, Time
)) {
697 return EFI_UNSUPPORTED
;
700 // Make a local copy of the time and date
702 CopyMem (&RtcTime
, Time
, sizeof (EFI_TIME
));
706 // Acquire RTC Lock to make access to RTC atomic
708 if (!EfiAtRuntime ()) {
709 EfiAcquireLock (&Global
->RtcLock
);
712 // Wait for up to 0.1 seconds for the RTC to be updated
714 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
715 if (EFI_ERROR (Status
)) {
716 if (!EfiAtRuntime ()) {
717 EfiReleaseLock (&Global
->RtcLock
);
719 return EFI_DEVICE_ERROR
;
724 RegisterB
.Data
= RtcRead (RTC_ADDRESS_REGISTER_B
);
727 ConvertEfiTimeToRtcTime (&RtcTime
, RegisterB
);
730 // if the alarm is disable, record the current setting.
732 RtcTime
.Second
= RtcRead (RTC_ADDRESS_SECONDS_ALARM
);
733 RtcTime
.Minute
= RtcRead (RTC_ADDRESS_MINUTES_ALARM
);
734 RtcTime
.Hour
= RtcRead (RTC_ADDRESS_HOURS_ALARM
);
735 RtcTime
.Day
= RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH
);
736 RtcTime
.Month
= RtcRead (RTC_ADDRESS_MONTH
);
737 RtcTime
.Year
= RtcRead (RTC_ADDRESS_YEAR
);
738 RtcTime
.TimeZone
= Global
->SavedTimeZone
;
739 RtcTime
.Daylight
= Global
->Daylight
;
743 // Set the Y/M/D info to variable as it has no corresponding hw registers.
745 Status
= EfiSetVariable (
748 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
752 if (EFI_ERROR (Status
)) {
753 if (!EfiAtRuntime ()) {
754 EfiReleaseLock (&Global
->RtcLock
);
756 return EFI_DEVICE_ERROR
;
760 // Inhibit updates of the RTC
762 RegisterB
.Bits
.Set
= 1;
763 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
767 // Set RTC alarm time
769 RtcWrite (RTC_ADDRESS_SECONDS_ALARM
, RtcTime
.Second
);
770 RtcWrite (RTC_ADDRESS_MINUTES_ALARM
, RtcTime
.Minute
);
771 RtcWrite (RTC_ADDRESS_HOURS_ALARM
, RtcTime
.Hour
);
773 RegisterB
.Bits
.Aie
= 1;
776 RegisterB
.Bits
.Aie
= 0;
779 // Allow updates of the RTC registers
781 RegisterB
.Bits
.Set
= 0;
782 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
787 if (!EfiAtRuntime ()) {
788 EfiReleaseLock (&Global
->RtcLock
);
795 Checks an 8-bit BCD value, and converts to an 8-bit value if valid.
797 This function checks the 8-bit BCD value specified by Value.
798 If valid, the function converts it to an 8-bit value and returns it.
799 Otherwise, return 0xff.
801 @param Value The 8-bit BCD value to check and convert
803 @return The 8-bit value converted. Or 0xff if Value is invalid.
807 CheckAndConvertBcd8ToDecimal8 (
811 if ((Value
< 0xa0) && ((Value
& 0xf) < 0xa)) {
812 return BcdToDecimal8 (Value
);
819 Converts time read from RTC to EFI_TIME format defined by UEFI spec.
821 This function converts raw time data read from RTC to the EFI_TIME format
822 defined by UEFI spec.
823 If data mode of RTC is BCD, then converts it to decimal,
824 If RTC is in 12-hour format, then converts it to 24-hour format.
826 @param Time On input, the time data read from RTC to convert
827 On output, the time converted to UEFI format
828 @param RegisterB Value of Register B of RTC, indicating data mode
831 @retval EFI_INVALID_PARAMETER Parameters passed in are invalid.
832 @retval EFI_SUCCESS Convert RTC time to EFI time successfully.
836 ConvertRtcTimeToEfiTime (
837 IN OUT EFI_TIME
*Time
,
838 IN RTC_REGISTER_B RegisterB
844 if ((Time
->Hour
& 0x80) != 0) {
850 Time
->Hour
= (UINT8
) (Time
->Hour
& 0x7f);
852 if (RegisterB
.Bits
.Dm
== 0) {
853 Time
->Year
= CheckAndConvertBcd8ToDecimal8 ((UINT8
) Time
->Year
);
854 Time
->Month
= CheckAndConvertBcd8ToDecimal8 (Time
->Month
);
855 Time
->Day
= CheckAndConvertBcd8ToDecimal8 (Time
->Day
);
856 Time
->Hour
= CheckAndConvertBcd8ToDecimal8 (Time
->Hour
);
857 Time
->Minute
= CheckAndConvertBcd8ToDecimal8 (Time
->Minute
);
858 Time
->Second
= CheckAndConvertBcd8ToDecimal8 (Time
->Second
);
861 if (Time
->Year
== 0xff || Time
->Month
== 0xff || Time
->Day
== 0xff ||
862 Time
->Hour
== 0xff || Time
->Minute
== 0xff || Time
->Second
== 0xff) {
863 return EFI_INVALID_PARAMETER
;
867 // For minimal/maximum year range [1970, 2069],
868 // Century is 19 if RTC year >= 70,
869 // Century is 20 otherwise.
871 Century
= (UINT8
) (PcdGet16 (PcdMinimalValidYear
) / 100);
872 if (Time
->Year
< PcdGet16 (PcdMinimalValidYear
) % 100) {
875 Time
->Year
= (UINT16
) (Century
* 100 + Time
->Year
);
878 // If time is in 12 hour format, convert it to 24 hour format
880 if (RegisterB
.Bits
.Mil
== 0) {
881 if (IsPM
&& Time
->Hour
< 12) {
882 Time
->Hour
= (UINT8
) (Time
->Hour
+ 12);
885 if (!IsPM
&& Time
->Hour
== 12) {
890 Time
->Nanosecond
= 0;
896 Wait for a period for the RTC to be ready.
898 @param Timeout Tell how long it should take to wait.
900 @retval EFI_DEVICE_ERROR RTC device error.
901 @retval EFI_SUCCESS RTC is updated and ready.
908 RTC_REGISTER_A RegisterA
;
909 RTC_REGISTER_D RegisterD
;
912 // See if the RTC is functioning correctly
914 RegisterD
.Data
= RtcRead (RTC_ADDRESS_REGISTER_D
);
916 if (RegisterD
.Bits
.Vrt
== 0) {
917 return EFI_DEVICE_ERROR
;
920 // Wait for up to 0.1 seconds for the RTC to be ready.
922 Timeout
= (Timeout
/ 10) + 1;
923 RegisterA
.Data
= RtcRead (RTC_ADDRESS_REGISTER_A
);
924 while (RegisterA
.Bits
.Uip
== 1 && Timeout
> 0) {
925 MicroSecondDelay (10);
926 RegisterA
.Data
= RtcRead (RTC_ADDRESS_REGISTER_A
);
930 RegisterD
.Data
= RtcRead (RTC_ADDRESS_REGISTER_D
);
931 if (Timeout
== 0 || RegisterD
.Bits
.Vrt
== 0) {
932 return EFI_DEVICE_ERROR
;
939 See if all fields of a variable of EFI_TIME type is correct.
941 @param Time The time to be checked.
943 @retval EFI_INVALID_PARAMETER Some fields of Time are not correct.
944 @retval EFI_SUCCESS Time is a valid EFI_TIME variable.
952 if (Time
->Year
< PcdGet16 (PcdMinimalValidYear
) ||
953 Time
->Year
> PcdGet16 (PcdMaximalValidYear
) ||
956 (!DayValid (Time
)) ||
960 Time
->Nanosecond
> 999999999 ||
961 (!(Time
->TimeZone
== EFI_UNSPECIFIED_TIMEZONE
|| (Time
->TimeZone
>= -1440 && Time
->TimeZone
<= 1440))) ||
962 ((Time
->Daylight
& (~(EFI_TIME_ADJUST_DAYLIGHT
| EFI_TIME_IN_DAYLIGHT
))) != 0)) {
963 return EFI_INVALID_PARAMETER
;
970 See if field Day of an EFI_TIME is correct.
972 @param Time Its Day field is to be checked.
974 @retval TRUE Day field of Time is correct.
975 @retval FALSE Day field of Time is NOT correct.
998 // The validity of Time->Month field should be checked before
1000 ASSERT (Time
->Month
>=1);
1001 ASSERT (Time
->Month
<=12);
1002 if (Time
->Day
< 1 ||
1003 Time
->Day
> DayOfMonth
[Time
->Month
- 1] ||
1004 (Time
->Month
== 2 && (!IsLeapYear (Time
) && Time
->Day
> 28))
1013 Check if it is a leap year.
1015 @param Time The time to be checked.
1017 @retval TRUE It is a leap year.
1018 @retval FALSE It is NOT a leap year.
1025 if (Time
->Year
% 4 == 0) {
1026 if (Time
->Year
% 100 == 0) {
1027 if (Time
->Year
% 400 == 0) {
1041 Converts time from EFI_TIME format defined by UEFI spec to RTC's.
1043 This function converts time from EFI_TIME format defined by UEFI spec to RTC's.
1044 If data mode of RTC is BCD, then converts EFI_TIME to it.
1045 If RTC is in 12-hour format, then converts EFI_TIME to it.
1047 @param Time On input, the time data read from UEFI to convert
1048 On output, the time converted to RTC format
1049 @param RegisterB Value of Register B of RTC, indicating data mode
1052 ConvertEfiTimeToRtcTime (
1053 IN OUT EFI_TIME
*Time
,
1054 IN RTC_REGISTER_B RegisterB
1061 // Adjust hour field if RTC is in 12 hour mode
1063 if (RegisterB
.Bits
.Mil
== 0) {
1064 if (Time
->Hour
< 12) {
1068 if (Time
->Hour
>= 13) {
1069 Time
->Hour
= (UINT8
) (Time
->Hour
- 12);
1070 } else if (Time
->Hour
== 0) {
1075 // Set the Time/Date values.
1077 Time
->Year
= (UINT16
) (Time
->Year
% 100);
1079 if (RegisterB
.Bits
.Dm
== 0) {
1080 Time
->Year
= DecimalToBcd8 ((UINT8
) Time
->Year
);
1081 Time
->Month
= DecimalToBcd8 (Time
->Month
);
1082 Time
->Day
= DecimalToBcd8 (Time
->Day
);
1083 Time
->Hour
= DecimalToBcd8 (Time
->Hour
);
1084 Time
->Minute
= DecimalToBcd8 (Time
->Minute
);
1085 Time
->Second
= DecimalToBcd8 (Time
->Second
);
1088 // If we are in 12 hour mode and PM is set, then set bit 7 of the Hour field.
1090 if (RegisterB
.Bits
.Mil
== 0 && IsPM
) {
1091 Time
->Hour
= (UINT8
) (Time
->Hour
| 0x80);
1096 Compare the Hour, Minute and Second of the From time and the To time.
1098 Only compare H/M/S in EFI_TIME and ignore other fields here.
1100 @param From the first time
1101 @param To the second time
1103 @return >0 The H/M/S of the From time is later than those of To time
1104 @return ==0 The H/M/S of the From time is same as those of To time
1105 @return <0 The H/M/S of the From time is earlier than those of To time
1113 if ((From
->Hour
> To
->Hour
) ||
1114 ((From
->Hour
== To
->Hour
) && (From
->Minute
> To
->Minute
)) ||
1115 ((From
->Hour
== To
->Hour
) && (From
->Minute
== To
->Minute
) && (From
->Second
> To
->Second
))) {
1117 } else if ((From
->Hour
== To
->Hour
) && (From
->Minute
== To
->Minute
) && (From
->Second
== To
->Second
)) {
1125 To check if second date is later than first date within 24 hours.
1127 @param From the first date
1128 @param To the second date
1130 @retval TRUE From is previous to To within 24 hours.
1131 @retval FALSE From is later, or it is previous to To more than 24 hours.
1139 UINT8 DayOfMonth
[12];
1152 DayOfMonth
[10] = 30;
1153 DayOfMonth
[11] = 31;
1158 // The validity of From->Month field should be checked before
1160 ASSERT (From
->Month
>=1);
1161 ASSERT (From
->Month
<=12);
1163 if (From
->Year
== To
->Year
) {
1164 if (From
->Month
== To
->Month
) {
1165 if ((From
->Day
+ 1) == To
->Day
) {
1166 if ((CompareHMS(From
, To
) >= 0)) {
1169 } else if (From
->Day
== To
->Day
) {
1170 if ((CompareHMS(From
, To
) <= 0)) {
1174 } else if (((From
->Month
+ 1) == To
->Month
) && (To
->Day
== 1)) {
1175 if ((From
->Month
== 2) && !IsLeapYear(From
)) {
1176 if (From
->Day
== 28) {
1177 if ((CompareHMS(From
, To
) >= 0)) {
1181 } else if (From
->Day
== DayOfMonth
[From
->Month
- 1]) {
1182 if ((CompareHMS(From
, To
) >= 0)) {
1187 } else if (((From
->Year
+ 1) == To
->Year
) &&
1188 (From
->Month
== 12) &&
1189 (From
->Day
== 31) &&
1192 if ((CompareHMS(From
, To
) >= 0)) {