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
);
504 // Store the century value to RTC before converting to BCD format.
506 if (Global
->CenturyRtcAddress
!= 0) {
507 RtcWrite (Global
->CenturyRtcAddress
, DecimalToBcd8 ((UINT8
) (RtcTime
.Year
/ 100)));
510 ConvertEfiTimeToRtcTime (&RtcTime
, RegisterB
);
512 RtcWrite (RTC_ADDRESS_SECONDS
, RtcTime
.Second
);
513 RtcWrite (RTC_ADDRESS_MINUTES
, RtcTime
.Minute
);
514 RtcWrite (RTC_ADDRESS_HOURS
, RtcTime
.Hour
);
515 RtcWrite (RTC_ADDRESS_DAY_OF_THE_MONTH
, RtcTime
.Day
);
516 RtcWrite (RTC_ADDRESS_MONTH
, RtcTime
.Month
);
517 RtcWrite (RTC_ADDRESS_YEAR
, (UINT8
) RtcTime
.Year
);
520 // Allow updates of the RTC registers
522 RegisterB
.Bits
.Set
= 0;
523 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
528 if (!EfiAtRuntime ()) {
529 EfiReleaseLock (&Global
->RtcLock
);
532 // Set the variable that contains the TimeZone and Daylight fields
534 Global
->SavedTimeZone
= Time
->TimeZone
;
535 Global
->Daylight
= Time
->Daylight
;
541 Returns the current wakeup alarm clock setting.
543 @param Enabled Indicates if the alarm is currently enabled or disabled.
544 @param Pending Indicates if the alarm signal is pending and requires acknowledgment.
545 @param Time The current alarm setting.
546 @param Global For global use inside this module.
548 @retval EFI_SUCCESS The alarm settings were returned.
549 @retval EFI_INVALID_PARAMETER Enabled is NULL.
550 @retval EFI_INVALID_PARAMETER Pending is NULL.
551 @retval EFI_INVALID_PARAMETER Time is NULL.
552 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
553 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
558 OUT BOOLEAN
*Enabled
,
559 OUT BOOLEAN
*Pending
,
561 IN PC_RTC_MODULE_GLOBALS
*Global
565 RTC_REGISTER_B RegisterB
;
566 RTC_REGISTER_C RegisterC
;
571 // Check parameters for null pointers
573 if ((Enabled
== NULL
) || (Pending
== NULL
) || (Time
== NULL
)) {
574 return EFI_INVALID_PARAMETER
;
578 // Acquire RTC Lock to make access to RTC atomic
580 if (!EfiAtRuntime ()) {
581 EfiAcquireLock (&Global
->RtcLock
);
584 // Wait for up to 0.1 seconds for the RTC to be updated
586 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
587 if (EFI_ERROR (Status
)) {
588 if (!EfiAtRuntime ()) {
589 EfiReleaseLock (&Global
->RtcLock
);
591 return EFI_DEVICE_ERROR
;
594 // Read Register B and Register C
596 RegisterB
.Data
= RtcRead (RTC_ADDRESS_REGISTER_B
);
597 RegisterC
.Data
= RtcRead (RTC_ADDRESS_REGISTER_C
);
600 // Get the Time/Date/Daylight Savings values.
602 *Enabled
= RegisterB
.Bits
.Aie
;
603 *Pending
= RegisterC
.Bits
.Af
;
605 Time
->Second
= RtcRead (RTC_ADDRESS_SECONDS_ALARM
);
606 Time
->Minute
= RtcRead (RTC_ADDRESS_MINUTES_ALARM
);
607 Time
->Hour
= RtcRead (RTC_ADDRESS_HOURS_ALARM
);
608 Time
->Day
= RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH
);
609 Time
->Month
= RtcRead (RTC_ADDRESS_MONTH
);
610 Time
->Year
= RtcRead (RTC_ADDRESS_YEAR
);
611 Time
->TimeZone
= Global
->SavedTimeZone
;
612 Time
->Daylight
= Global
->Daylight
;
615 // Get the alarm info from variable
617 DataSize
= sizeof (EFI_TIME
);
618 Status
= EfiGetVariable (
625 if (!EFI_ERROR (Status
)) {
627 // The alarm variable exists. In this case, we read variable to get info.
629 Time
->Day
= RtcTime
.Day
;
630 Time
->Month
= RtcTime
.Month
;
631 Time
->Year
= RtcTime
.Year
;
637 if (!EfiAtRuntime ()) {
638 EfiReleaseLock (&Global
->RtcLock
);
642 // Make sure all field values are in correct range
644 Status
= ConvertRtcTimeToEfiTime (Time
, RegisterB
);
645 if (!EFI_ERROR (Status
)) {
646 Status
= RtcTimeFieldsValid (Time
);
648 if (EFI_ERROR (Status
)) {
649 return EFI_DEVICE_ERROR
;
656 Sets the system wakeup alarm clock time.
658 @param Enabled Enable or disable the wakeup alarm.
659 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
660 If Enable is FALSE, then this parameter is optional, and may be NULL.
661 @param Global For global use inside this module.
663 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled.
664 If Enable is FALSE, then the wakeup alarm was disabled.
665 @retval EFI_INVALID_PARAMETER A time field is out of range.
666 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
667 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
673 IN EFI_TIME
*Time
, OPTIONAL
674 IN PC_RTC_MODULE_GLOBALS
*Global
679 RTC_REGISTER_B RegisterB
;
680 EFI_TIME_CAPABILITIES Capabilities
;
682 ZeroMem (&RtcTime
, sizeof (RtcTime
));
687 return EFI_INVALID_PARAMETER
;
690 // Make sure that the time fields are valid
692 Status
= RtcTimeFieldsValid (Time
);
693 if (EFI_ERROR (Status
)) {
694 return EFI_INVALID_PARAMETER
;
697 // Just support set alarm time within 24 hours
699 PcRtcGetTime (&RtcTime
, &Capabilities
, Global
);
700 Status
= RtcTimeFieldsValid (&RtcTime
);
701 if (EFI_ERROR (Status
)) {
702 return EFI_DEVICE_ERROR
;
704 if (!IsWithinOneDay (&RtcTime
, Time
)) {
705 return EFI_UNSUPPORTED
;
708 // Make a local copy of the time and date
710 CopyMem (&RtcTime
, Time
, sizeof (EFI_TIME
));
714 // Acquire RTC Lock to make access to RTC atomic
716 if (!EfiAtRuntime ()) {
717 EfiAcquireLock (&Global
->RtcLock
);
720 // Wait for up to 0.1 seconds for the RTC to be updated
722 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
723 if (EFI_ERROR (Status
)) {
724 if (!EfiAtRuntime ()) {
725 EfiReleaseLock (&Global
->RtcLock
);
727 return EFI_DEVICE_ERROR
;
732 RegisterB
.Data
= RtcRead (RTC_ADDRESS_REGISTER_B
);
735 ConvertEfiTimeToRtcTime (&RtcTime
, RegisterB
);
738 // if the alarm is disable, record the current setting.
740 RtcTime
.Second
= RtcRead (RTC_ADDRESS_SECONDS_ALARM
);
741 RtcTime
.Minute
= RtcRead (RTC_ADDRESS_MINUTES_ALARM
);
742 RtcTime
.Hour
= RtcRead (RTC_ADDRESS_HOURS_ALARM
);
743 RtcTime
.Day
= RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH
);
744 RtcTime
.Month
= RtcRead (RTC_ADDRESS_MONTH
);
745 RtcTime
.Year
= RtcRead (RTC_ADDRESS_YEAR
);
746 RtcTime
.TimeZone
= Global
->SavedTimeZone
;
747 RtcTime
.Daylight
= Global
->Daylight
;
751 // Set the Y/M/D info to variable as it has no corresponding hw registers.
753 Status
= EfiSetVariable (
756 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
760 if (EFI_ERROR (Status
)) {
761 if (!EfiAtRuntime ()) {
762 EfiReleaseLock (&Global
->RtcLock
);
764 return EFI_DEVICE_ERROR
;
768 // Inhibit updates of the RTC
770 RegisterB
.Bits
.Set
= 1;
771 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
775 // Set RTC alarm time
777 RtcWrite (RTC_ADDRESS_SECONDS_ALARM
, RtcTime
.Second
);
778 RtcWrite (RTC_ADDRESS_MINUTES_ALARM
, RtcTime
.Minute
);
779 RtcWrite (RTC_ADDRESS_HOURS_ALARM
, RtcTime
.Hour
);
781 RegisterB
.Bits
.Aie
= 1;
784 RegisterB
.Bits
.Aie
= 0;
787 // Allow updates of the RTC registers
789 RegisterB
.Bits
.Set
= 0;
790 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
795 if (!EfiAtRuntime ()) {
796 EfiReleaseLock (&Global
->RtcLock
);
803 Checks an 8-bit BCD value, and converts to an 8-bit value if valid.
805 This function checks the 8-bit BCD value specified by Value.
806 If valid, the function converts it to an 8-bit value and returns it.
807 Otherwise, return 0xff.
809 @param Value The 8-bit BCD value to check and convert
811 @return The 8-bit value converted. Or 0xff if Value is invalid.
815 CheckAndConvertBcd8ToDecimal8 (
819 if ((Value
< 0xa0) && ((Value
& 0xf) < 0xa)) {
820 return BcdToDecimal8 (Value
);
827 Converts time read from RTC to EFI_TIME format defined by UEFI spec.
829 This function converts raw time data read from RTC to the EFI_TIME format
830 defined by UEFI spec.
831 If data mode of RTC is BCD, then converts it to decimal,
832 If RTC is in 12-hour format, then converts it to 24-hour format.
834 @param Time On input, the time data read from RTC to convert
835 On output, the time converted to UEFI format
836 @param RegisterB Value of Register B of RTC, indicating data mode
839 @retval EFI_INVALID_PARAMETER Parameters passed in are invalid.
840 @retval EFI_SUCCESS Convert RTC time to EFI time successfully.
844 ConvertRtcTimeToEfiTime (
845 IN OUT EFI_TIME
*Time
,
846 IN RTC_REGISTER_B RegisterB
852 if ((Time
->Hour
& 0x80) != 0) {
858 Time
->Hour
= (UINT8
) (Time
->Hour
& 0x7f);
860 if (RegisterB
.Bits
.Dm
== 0) {
861 Time
->Year
= CheckAndConvertBcd8ToDecimal8 ((UINT8
) Time
->Year
);
862 Time
->Month
= CheckAndConvertBcd8ToDecimal8 (Time
->Month
);
863 Time
->Day
= CheckAndConvertBcd8ToDecimal8 (Time
->Day
);
864 Time
->Hour
= CheckAndConvertBcd8ToDecimal8 (Time
->Hour
);
865 Time
->Minute
= CheckAndConvertBcd8ToDecimal8 (Time
->Minute
);
866 Time
->Second
= CheckAndConvertBcd8ToDecimal8 (Time
->Second
);
869 if (Time
->Year
== 0xff || Time
->Month
== 0xff || Time
->Day
== 0xff ||
870 Time
->Hour
== 0xff || Time
->Minute
== 0xff || Time
->Second
== 0xff) {
871 return EFI_INVALID_PARAMETER
;
875 // For minimal/maximum year range [1970, 2069],
876 // Century is 19 if RTC year >= 70,
877 // Century is 20 otherwise.
879 Century
= (UINT8
) (PcdGet16 (PcdMinimalValidYear
) / 100);
880 if (Time
->Year
< PcdGet16 (PcdMinimalValidYear
) % 100) {
883 Time
->Year
= (UINT16
) (Century
* 100 + Time
->Year
);
886 // If time is in 12 hour format, convert it to 24 hour format
888 if (RegisterB
.Bits
.Mil
== 0) {
889 if (IsPM
&& Time
->Hour
< 12) {
890 Time
->Hour
= (UINT8
) (Time
->Hour
+ 12);
893 if (!IsPM
&& Time
->Hour
== 12) {
898 Time
->Nanosecond
= 0;
904 Wait for a period for the RTC to be ready.
906 @param Timeout Tell how long it should take to wait.
908 @retval EFI_DEVICE_ERROR RTC device error.
909 @retval EFI_SUCCESS RTC is updated and ready.
916 RTC_REGISTER_A RegisterA
;
917 RTC_REGISTER_D RegisterD
;
920 // See if the RTC is functioning correctly
922 RegisterD
.Data
= RtcRead (RTC_ADDRESS_REGISTER_D
);
924 if (RegisterD
.Bits
.Vrt
== 0) {
925 return EFI_DEVICE_ERROR
;
928 // Wait for up to 0.1 seconds for the RTC to be ready.
930 Timeout
= (Timeout
/ 10) + 1;
931 RegisterA
.Data
= RtcRead (RTC_ADDRESS_REGISTER_A
);
932 while (RegisterA
.Bits
.Uip
== 1 && Timeout
> 0) {
933 MicroSecondDelay (10);
934 RegisterA
.Data
= RtcRead (RTC_ADDRESS_REGISTER_A
);
938 RegisterD
.Data
= RtcRead (RTC_ADDRESS_REGISTER_D
);
939 if (Timeout
== 0 || RegisterD
.Bits
.Vrt
== 0) {
940 return EFI_DEVICE_ERROR
;
947 See if all fields of a variable of EFI_TIME type is correct.
949 @param Time The time to be checked.
951 @retval EFI_INVALID_PARAMETER Some fields of Time are not correct.
952 @retval EFI_SUCCESS Time is a valid EFI_TIME variable.
960 if (Time
->Year
< PcdGet16 (PcdMinimalValidYear
) ||
961 Time
->Year
> PcdGet16 (PcdMaximalValidYear
) ||
964 (!DayValid (Time
)) ||
968 Time
->Nanosecond
> 999999999 ||
969 (!(Time
->TimeZone
== EFI_UNSPECIFIED_TIMEZONE
|| (Time
->TimeZone
>= -1440 && Time
->TimeZone
<= 1440))) ||
970 ((Time
->Daylight
& (~(EFI_TIME_ADJUST_DAYLIGHT
| EFI_TIME_IN_DAYLIGHT
))) != 0)) {
971 return EFI_INVALID_PARAMETER
;
978 See if field Day of an EFI_TIME is correct.
980 @param Time Its Day field is to be checked.
982 @retval TRUE Day field of Time is correct.
983 @retval FALSE Day field of Time is NOT correct.
1002 DayOfMonth
[10] = 30;
1003 DayOfMonth
[11] = 31;
1006 // The validity of Time->Month field should be checked before
1008 ASSERT (Time
->Month
>=1);
1009 ASSERT (Time
->Month
<=12);
1010 if (Time
->Day
< 1 ||
1011 Time
->Day
> DayOfMonth
[Time
->Month
- 1] ||
1012 (Time
->Month
== 2 && (!IsLeapYear (Time
) && Time
->Day
> 28))
1021 Check if it is a leap year.
1023 @param Time The time to be checked.
1025 @retval TRUE It is a leap year.
1026 @retval FALSE It is NOT a leap year.
1033 if (Time
->Year
% 4 == 0) {
1034 if (Time
->Year
% 100 == 0) {
1035 if (Time
->Year
% 400 == 0) {
1049 Converts time from EFI_TIME format defined by UEFI spec to RTC's.
1051 This function converts time from EFI_TIME format defined by UEFI spec to RTC's.
1052 If data mode of RTC is BCD, then converts EFI_TIME to it.
1053 If RTC is in 12-hour format, then converts EFI_TIME to it.
1055 @param Time On input, the time data read from UEFI to convert
1056 On output, the time converted to RTC format
1057 @param RegisterB Value of Register B of RTC, indicating data mode
1060 ConvertEfiTimeToRtcTime (
1061 IN OUT EFI_TIME
*Time
,
1062 IN RTC_REGISTER_B RegisterB
1069 // Adjust hour field if RTC is in 12 hour mode
1071 if (RegisterB
.Bits
.Mil
== 0) {
1072 if (Time
->Hour
< 12) {
1076 if (Time
->Hour
>= 13) {
1077 Time
->Hour
= (UINT8
) (Time
->Hour
- 12);
1078 } else if (Time
->Hour
== 0) {
1083 // Set the Time/Date values.
1085 Time
->Year
= (UINT16
) (Time
->Year
% 100);
1087 if (RegisterB
.Bits
.Dm
== 0) {
1088 Time
->Year
= DecimalToBcd8 ((UINT8
) Time
->Year
);
1089 Time
->Month
= DecimalToBcd8 (Time
->Month
);
1090 Time
->Day
= DecimalToBcd8 (Time
->Day
);
1091 Time
->Hour
= DecimalToBcd8 (Time
->Hour
);
1092 Time
->Minute
= DecimalToBcd8 (Time
->Minute
);
1093 Time
->Second
= DecimalToBcd8 (Time
->Second
);
1096 // If we are in 12 hour mode and PM is set, then set bit 7 of the Hour field.
1098 if (RegisterB
.Bits
.Mil
== 0 && IsPM
) {
1099 Time
->Hour
= (UINT8
) (Time
->Hour
| 0x80);
1104 Compare the Hour, Minute and Second of the From time and the To time.
1106 Only compare H/M/S in EFI_TIME and ignore other fields here.
1108 @param From the first time
1109 @param To the second time
1111 @return >0 The H/M/S of the From time is later than those of To time
1112 @return ==0 The H/M/S of the From time is same as those of To time
1113 @return <0 The H/M/S of the From time is earlier than those of To time
1121 if ((From
->Hour
> To
->Hour
) ||
1122 ((From
->Hour
== To
->Hour
) && (From
->Minute
> To
->Minute
)) ||
1123 ((From
->Hour
== To
->Hour
) && (From
->Minute
== To
->Minute
) && (From
->Second
> To
->Second
))) {
1125 } else if ((From
->Hour
== To
->Hour
) && (From
->Minute
== To
->Minute
) && (From
->Second
== To
->Second
)) {
1133 To check if second date is later than first date within 24 hours.
1135 @param From the first date
1136 @param To the second date
1138 @retval TRUE From is previous to To within 24 hours.
1139 @retval FALSE From is later, or it is previous to To more than 24 hours.
1147 UINT8 DayOfMonth
[12];
1160 DayOfMonth
[10] = 30;
1161 DayOfMonth
[11] = 31;
1166 // The validity of From->Month field should be checked before
1168 ASSERT (From
->Month
>=1);
1169 ASSERT (From
->Month
<=12);
1171 if (From
->Year
== To
->Year
) {
1172 if (From
->Month
== To
->Month
) {
1173 if ((From
->Day
+ 1) == To
->Day
) {
1174 if ((CompareHMS(From
, To
) >= 0)) {
1177 } else if (From
->Day
== To
->Day
) {
1178 if ((CompareHMS(From
, To
) <= 0)) {
1182 } else if (((From
->Month
+ 1) == To
->Month
) && (To
->Day
== 1)) {
1183 if ((From
->Month
== 2) && !IsLeapYear(From
)) {
1184 if (From
->Day
== 28) {
1185 if ((CompareHMS(From
, To
) >= 0)) {
1189 } else if (From
->Day
== DayOfMonth
[From
->Month
- 1]) {
1190 if ((CompareHMS(From
, To
) >= 0)) {
1195 } else if (((From
->Year
+ 1) == To
->Year
) &&
1196 (From
->Month
== 12) &&
1197 (From
->Day
== 31) &&
1200 if ((CompareHMS(From
, To
) >= 0)) {
1209 This function find ACPI table with the specified signature in RSDT or XSDT.
1211 @param Sdt ACPI RSDT or XSDT.
1212 @param Signature ACPI table signature.
1213 @param TablePointerSize Size of table pointer: 4 or 8.
1215 @return ACPI table or NULL if not found.
1219 IN EFI_ACPI_DESCRIPTION_HEADER
*Sdt
,
1220 IN UINT32 Signature
,
1221 IN UINTN TablePointerSize
1227 EFI_ACPI_DESCRIPTION_HEADER
*Table
;
1229 EntryCount
= (Sdt
->Length
- sizeof (EFI_ACPI_DESCRIPTION_HEADER
)) / TablePointerSize
;
1231 EntryBase
= (UINTN
) (Sdt
+ 1);
1232 for (Index
= 0; Index
< EntryCount
; Index
++) {
1234 // When TablePointerSize is 4 while sizeof (VOID *) is 8, make sure the upper 4 bytes are zero.
1237 CopyMem (&Table
, (VOID
*) (EntryBase
+ Index
* TablePointerSize
), TablePointerSize
);
1238 if (Table
->Signature
== Signature
) {
1247 Notification function of ACPI Table change.
1249 This is a notification function registered on ACPI Table change event.
1250 It saves the Century address stored in ACPI FADT table.
1252 @param Event Event whose notification function is being invoked.
1253 @param Context Pointer to the notification function's context.
1258 PcRtcAcpiTableChangeCallback (
1264 EFI_ACPI_2_0_ROOT_SYSTEM_DESCRIPTION_POINTER
*Rsdp
;
1265 EFI_ACPI_DESCRIPTION_HEADER
*Rsdt
;
1266 EFI_ACPI_DESCRIPTION_HEADER
*Xsdt
;
1267 EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE
*Fadt
;
1271 Status
= EfiGetSystemConfigurationTable (&gEfiAcpiTableGuid
, (VOID
**) &Rsdp
);
1272 if (EFI_ERROR (Status
)) {
1273 Status
= EfiGetSystemConfigurationTable (&gEfiAcpi10TableGuid
, (VOID
**) &Rsdp
);
1276 if (EFI_ERROR (Status
)) {
1280 ASSERT (Rsdp
!= NULL
);
1283 // Find FADT in XSDT
1286 if (Rsdp
->Revision
>= EFI_ACPI_2_0_ROOT_SYSTEM_DESCRIPTION_POINTER_REVISION
) {
1287 Xsdt
= (EFI_ACPI_DESCRIPTION_HEADER
*) (UINTN
) Rsdp
->XsdtAddress
;
1288 Fadt
= ScanTableInSDT (Xsdt
, EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE
, sizeof (UINT64
));
1293 // Find FADT in RSDT
1295 Rsdt
= (EFI_ACPI_DESCRIPTION_HEADER
*) (UINTN
) Rsdp
->RsdtAddress
;
1296 Fadt
= ScanTableInSDT (Rsdt
, EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE
, sizeof (UINT32
));
1299 if ((Fadt
!= NULL
) &&
1300 (Fadt
->Century
> RTC_ADDRESS_REGISTER_D
) && (Fadt
->Century
< 0x80) &&
1301 (mModuleGlobal
.CenturyRtcAddress
!= Fadt
->Century
)
1303 mModuleGlobal
.CenturyRtcAddress
= Fadt
->Century
;
1304 Status
= PcRtcGetTime (&Time
, NULL
, &mModuleGlobal
);
1305 if (!EFI_ERROR (Status
)) {
1306 Century
= (UINT8
) (Time
.Year
/ 100);
1307 Century
= DecimalToBcd8 (Century
);
1308 DEBUG ((EFI_D_INFO
, "PcRtc: Write 0x%x to CMOS location 0x%x\n", Century
, mModuleGlobal
.CenturyRtcAddress
));
1309 RtcWrite (mModuleGlobal
.CenturyRtcAddress
, Century
);