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
;
111 // Acquire RTC Lock to make access to RTC atomic
113 if (!EfiAtRuntime ()) {
114 EfiAcquireLock (&Global
->RtcLock
);
117 // Initialize RTC Register
119 // Make sure Division Chain is properly configured,
120 // or RTC clock won't "tick" -- time won't increment
122 RegisterA
.Data
= RTC_INIT_REGISTER_A
;
123 RtcWrite (RTC_ADDRESS_REGISTER_A
, RegisterA
.Data
);
128 RegisterB
.Data
= RtcRead (RTC_ADDRESS_REGISTER_B
);
131 // Clear RTC flag register
133 RtcRead (RTC_ADDRESS_REGISTER_C
);
136 // Clear RTC register D
138 RegisterD
.Data
= RTC_INIT_REGISTER_D
;
139 RtcWrite (RTC_ADDRESS_REGISTER_D
, RegisterD
.Data
);
142 // Wait for up to 0.1 seconds for the RTC to be updated
144 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
145 if (EFI_ERROR (Status
)) {
147 // Set the variable with default value if the RTC is functioning incorrectly.
149 Global
->SavedTimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
150 Global
->Daylight
= 0;
151 if (!EfiAtRuntime ()) {
152 EfiReleaseLock (&Global
->RtcLock
);
154 return EFI_DEVICE_ERROR
;
157 // Get the Time/Date/Daylight Savings values.
159 Time
.Second
= RtcRead (RTC_ADDRESS_SECONDS
);
160 Time
.Minute
= RtcRead (RTC_ADDRESS_MINUTES
);
161 Time
.Hour
= RtcRead (RTC_ADDRESS_HOURS
);
162 Time
.Day
= RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH
);
163 Time
.Month
= RtcRead (RTC_ADDRESS_MONTH
);
164 Time
.Year
= RtcRead (RTC_ADDRESS_YEAR
);
166 Century
= RtcRead (RTC_ADDRESS_CENTURY
);
169 // Set RTC configuration after get original time
170 // The value of bit AIE should be reserved.
172 RtcWrite (RTC_ADDRESS_REGISTER_B
, (UINT8
)(RTC_INIT_REGISTER_B
| (RegisterB
.Data
& BIT5
)));
177 if (!EfiAtRuntime ()) {
178 EfiReleaseLock (&Global
->RtcLock
);
182 // Get the data of Daylight saving and time zone, if they have been
183 // stored in NV variable during previous boot.
185 DataSize
= sizeof (UINT32
);
186 Status
= EfiGetVariable (
193 if (!EFI_ERROR (Status
)) {
194 Time
.TimeZone
= (INT16
) TimerVar
;
195 Time
.Daylight
= (UINT8
) (TimerVar
>> 16);
197 Time
.TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
202 // Validate time fields
204 Status
= ConvertRtcTimeToEfiTime (&Time
, Century
, RegisterB
);
205 if (!EFI_ERROR (Status
)) {
206 Status
= RtcTimeFieldsValid (&Time
);
208 if (EFI_ERROR (Status
)) {
210 // Report Status Code to indicate that the RTC has bad date and time
213 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
214 (EFI_SOFTWARE_DXE_RT_DRIVER
| EFI_SW_EC_BAD_DATE_TIME
)
216 Time
.Second
= RTC_INIT_SECOND
;
217 Time
.Minute
= RTC_INIT_MINUTE
;
218 Time
.Hour
= RTC_INIT_HOUR
;
219 Time
.Day
= RTC_INIT_DAY
;
220 Time
.Month
= RTC_INIT_MONTH
;
221 Time
.Year
= RTC_INIT_YEAR
;
223 Time
.TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
228 // Reset time value according to new RTC configuration
230 Status
= PcRtcSetTime (&Time
, Global
);
231 if (EFI_ERROR (Status
)) {
232 return EFI_DEVICE_ERROR
;
236 // Reset wakeup time value to valid state when wakeup alarm is disabled and wakeup time is invalid.
237 // Global variable has already had valid SavedTimeZone and Daylight,
238 // so we can use them to get and set wakeup time.
240 Status
= PcRtcGetWakeupTime (&Enabled
, &Pending
, &Time
, Global
);
241 if ((Enabled
) || (!EFI_ERROR (Status
))) {
246 // When wakeup time is disabled and invalid, reset wakeup time register to valid state
247 // but keep wakeup alarm disabled.
249 Time
.Second
= RTC_INIT_SECOND
;
250 Time
.Minute
= RTC_INIT_MINUTE
;
251 Time
.Hour
= RTC_INIT_HOUR
;
252 Time
.Day
= RTC_INIT_DAY
;
253 Time
.Month
= RTC_INIT_MONTH
;
254 Time
.Year
= RTC_INIT_YEAR
;
256 Time
.TimeZone
= Global
->SavedTimeZone
;
257 Time
.Daylight
= Global
->Daylight
;;
260 // Acquire RTC Lock to make access to RTC atomic
262 if (!EfiAtRuntime ()) {
263 EfiAcquireLock (&Global
->RtcLock
);
266 // Wait for up to 0.1 seconds for the RTC to be updated
268 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
269 if (EFI_ERROR (Status
)) {
270 if (!EfiAtRuntime ()) {
271 EfiReleaseLock (&Global
->RtcLock
);
273 return EFI_DEVICE_ERROR
;
276 ConvertEfiTimeToRtcTime (&Time
, RegisterB
, &Century
);
279 // Set the Y/M/D info to variable as it has no corresponding hw registers.
281 Status
= EfiSetVariable (
284 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
288 if (EFI_ERROR (Status
)) {
289 if (!EfiAtRuntime ()) {
290 EfiReleaseLock (&Global
->RtcLock
);
292 return EFI_DEVICE_ERROR
;
296 // Inhibit updates of the RTC
298 RegisterB
.Bits
.Set
= 1;
299 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
302 // Set RTC alarm time registers
304 RtcWrite (RTC_ADDRESS_SECONDS_ALARM
, Time
.Second
);
305 RtcWrite (RTC_ADDRESS_MINUTES_ALARM
, Time
.Minute
);
306 RtcWrite (RTC_ADDRESS_HOURS_ALARM
, Time
.Hour
);
309 // Allow updates of the RTC registers
311 RegisterB
.Bits
.Set
= 0;
312 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
317 if (!EfiAtRuntime ()) {
318 EfiReleaseLock (&Global
->RtcLock
);
324 Returns the current time and date information, and the time-keeping capabilities
325 of the hardware platform.
327 @param Time A pointer to storage to receive a snapshot of the current time.
328 @param Capabilities An optional pointer to a buffer to receive the real time clock
329 device's capabilities.
330 @param Global For global use inside this module.
332 @retval EFI_SUCCESS The operation completed successfully.
333 @retval EFI_INVALID_PARAMETER Time is NULL.
334 @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.
340 OUT EFI_TIME_CAPABILITIES
*Capabilities
, OPTIONAL
341 IN PC_RTC_MODULE_GLOBALS
*Global
345 RTC_REGISTER_B RegisterB
;
349 // Check parameters for null pointer
352 return EFI_INVALID_PARAMETER
;
356 // Acquire RTC Lock to make access to RTC atomic
358 if (!EfiAtRuntime ()) {
359 EfiAcquireLock (&Global
->RtcLock
);
362 // Wait for up to 0.1 seconds for the RTC to be updated
364 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
365 if (EFI_ERROR (Status
)) {
366 if (!EfiAtRuntime ()) {
367 EfiReleaseLock (&Global
->RtcLock
);
374 RegisterB
.Data
= RtcRead (RTC_ADDRESS_REGISTER_B
);
377 // Get the Time/Date/Daylight Savings values.
379 Time
->Second
= RtcRead (RTC_ADDRESS_SECONDS
);
380 Time
->Minute
= RtcRead (RTC_ADDRESS_MINUTES
);
381 Time
->Hour
= RtcRead (RTC_ADDRESS_HOURS
);
382 Time
->Day
= RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH
);
383 Time
->Month
= RtcRead (RTC_ADDRESS_MONTH
);
384 Time
->Year
= RtcRead (RTC_ADDRESS_YEAR
);
386 Century
= RtcRead (RTC_ADDRESS_CENTURY
);
391 if (!EfiAtRuntime ()) {
392 EfiReleaseLock (&Global
->RtcLock
);
396 // Get the variable that contains the TimeZone and Daylight fields
398 Time
->TimeZone
= Global
->SavedTimeZone
;
399 Time
->Daylight
= Global
->Daylight
;
402 // Make sure all field values are in correct range
404 Status
= ConvertRtcTimeToEfiTime (Time
, Century
, RegisterB
);
405 if (!EFI_ERROR (Status
)) {
406 Status
= RtcTimeFieldsValid (Time
);
408 if (EFI_ERROR (Status
)) {
409 return EFI_DEVICE_ERROR
;
413 // Fill in Capabilities if it was passed in
415 if (Capabilities
!= NULL
) {
416 Capabilities
->Resolution
= 1;
420 Capabilities
->Accuracy
= 50000000;
424 Capabilities
->SetsToZero
= FALSE
;
431 Sets the current local time and date information.
433 @param Time A pointer to the current time.
434 @param Global For global use inside this module.
436 @retval EFI_SUCCESS The operation completed successfully.
437 @retval EFI_INVALID_PARAMETER A time field is out of range.
438 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
444 IN PC_RTC_MODULE_GLOBALS
*Global
449 RTC_REGISTER_B RegisterB
;
454 return EFI_INVALID_PARAMETER
;
457 // Make sure that the time fields are valid
459 Status
= RtcTimeFieldsValid (Time
);
460 if (EFI_ERROR (Status
)) {
464 CopyMem (&RtcTime
, Time
, sizeof (EFI_TIME
));
467 // Acquire RTC Lock to make access to RTC atomic
469 if (!EfiAtRuntime ()) {
470 EfiAcquireLock (&Global
->RtcLock
);
473 // Wait for up to 0.1 seconds for the RTC to be updated
475 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
476 if (EFI_ERROR (Status
)) {
477 if (!EfiAtRuntime ()) {
478 EfiReleaseLock (&Global
->RtcLock
);
484 // Write timezone and daylight to RTC variable
486 TimerVar
= Time
->Daylight
;
487 TimerVar
= (UINT32
) ((TimerVar
<< 16) | (UINT16
)(Time
->TimeZone
));
488 Status
= EfiSetVariable (
491 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
495 if (EFI_ERROR (Status
)) {
496 if (!EfiAtRuntime ()) {
497 EfiReleaseLock (&Global
->RtcLock
);
499 return EFI_DEVICE_ERROR
;
503 // Read Register B, and inhibit updates of the RTC
505 RegisterB
.Data
= RtcRead (RTC_ADDRESS_REGISTER_B
);
506 RegisterB
.Bits
.Set
= 1;
507 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
509 ConvertEfiTimeToRtcTime (&RtcTime
, RegisterB
, &Century
);
511 RtcWrite (RTC_ADDRESS_SECONDS
, RtcTime
.Second
);
512 RtcWrite (RTC_ADDRESS_MINUTES
, RtcTime
.Minute
);
513 RtcWrite (RTC_ADDRESS_HOURS
, RtcTime
.Hour
);
514 RtcWrite (RTC_ADDRESS_DAY_OF_THE_MONTH
, RtcTime
.Day
);
515 RtcWrite (RTC_ADDRESS_MONTH
, RtcTime
.Month
);
516 RtcWrite (RTC_ADDRESS_YEAR
, (UINT8
) RtcTime
.Year
);
517 RtcWrite (RTC_ADDRESS_CENTURY
, Century
);
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
;
572 // Check parameters for null pointers
574 if ((Enabled
== NULL
) || (Pending
== NULL
) || (Time
== NULL
)) {
575 return EFI_INVALID_PARAMETER
;
579 // Acquire RTC Lock to make access to RTC atomic
581 if (!EfiAtRuntime ()) {
582 EfiAcquireLock (&Global
->RtcLock
);
585 // Wait for up to 0.1 seconds for the RTC to be updated
587 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
588 if (EFI_ERROR (Status
)) {
589 if (!EfiAtRuntime ()) {
590 EfiReleaseLock (&Global
->RtcLock
);
592 return EFI_DEVICE_ERROR
;
595 // Read Register B and Register C
597 RegisterB
.Data
= RtcRead (RTC_ADDRESS_REGISTER_B
);
598 RegisterC
.Data
= RtcRead (RTC_ADDRESS_REGISTER_C
);
601 // Get the Time/Date/Daylight Savings values.
603 *Enabled
= RegisterB
.Bits
.Aie
;
604 *Pending
= RegisterC
.Bits
.Af
;
606 Time
->Second
= RtcRead (RTC_ADDRESS_SECONDS_ALARM
);
607 Time
->Minute
= RtcRead (RTC_ADDRESS_MINUTES_ALARM
);
608 Time
->Hour
= RtcRead (RTC_ADDRESS_HOURS_ALARM
);
609 Time
->Day
= RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH
);
610 Time
->Month
= RtcRead (RTC_ADDRESS_MONTH
);
611 Time
->Year
= RtcRead (RTC_ADDRESS_YEAR
);
612 Time
->TimeZone
= Global
->SavedTimeZone
;
613 Time
->Daylight
= Global
->Daylight
;
615 Century
= RtcRead (RTC_ADDRESS_CENTURY
);
618 // Get the alarm info from variable
620 DataSize
= sizeof (EFI_TIME
);
621 Status
= EfiGetVariable (
628 if (!EFI_ERROR (Status
)) {
630 // The alarm variable exists. In this case, we read variable to get info.
632 Time
->Day
= RtcTime
.Day
;
633 Time
->Month
= RtcTime
.Month
;
634 Time
->Year
= RtcTime
.Year
;
640 if (!EfiAtRuntime ()) {
641 EfiReleaseLock (&Global
->RtcLock
);
645 // Make sure all field values are in correct range
647 Status
= ConvertRtcTimeToEfiTime (Time
, Century
, RegisterB
);
648 if (!EFI_ERROR (Status
)) {
649 Status
= RtcTimeFieldsValid (Time
);
651 if (EFI_ERROR (Status
)) {
652 return EFI_DEVICE_ERROR
;
659 Sets the system wakeup alarm clock time.
661 @param Enabled Enable or disable the wakeup alarm.
662 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
663 If Enable is FALSE, then this parameter is optional, and may be NULL.
664 @param Global For global use inside this module.
666 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled.
667 If Enable is FALSE, then the wakeup alarm was disabled.
668 @retval EFI_INVALID_PARAMETER A time field is out of range.
669 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
670 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
676 IN EFI_TIME
*Time
, OPTIONAL
677 IN PC_RTC_MODULE_GLOBALS
*Global
682 RTC_REGISTER_B RegisterB
;
684 EFI_TIME_CAPABILITIES Capabilities
;
686 ZeroMem (&RtcTime
, sizeof (RtcTime
));
691 return EFI_INVALID_PARAMETER
;
694 // Make sure that the time fields are valid
696 Status
= RtcTimeFieldsValid (Time
);
697 if (EFI_ERROR (Status
)) {
698 return EFI_INVALID_PARAMETER
;
701 // Just support set alarm time within 24 hours
703 PcRtcGetTime (&RtcTime
, &Capabilities
, Global
);
704 Status
= RtcTimeFieldsValid (&RtcTime
);
705 if (EFI_ERROR (Status
)) {
706 return EFI_DEVICE_ERROR
;
708 if (!IsWithinOneDay (&RtcTime
, Time
)) {
709 return EFI_UNSUPPORTED
;
712 // Make a local copy of the time and date
714 CopyMem (&RtcTime
, Time
, sizeof (EFI_TIME
));
718 // Acquire RTC Lock to make access to RTC atomic
720 if (!EfiAtRuntime ()) {
721 EfiAcquireLock (&Global
->RtcLock
);
724 // Wait for up to 0.1 seconds for the RTC to be updated
726 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
727 if (EFI_ERROR (Status
)) {
728 if (!EfiAtRuntime ()) {
729 EfiReleaseLock (&Global
->RtcLock
);
731 return EFI_DEVICE_ERROR
;
736 RegisterB
.Data
= RtcRead (RTC_ADDRESS_REGISTER_B
);
739 ConvertEfiTimeToRtcTime (&RtcTime
, RegisterB
, &Century
);
742 // if the alarm is disable, record the current setting.
744 RtcTime
.Second
= RtcRead (RTC_ADDRESS_SECONDS_ALARM
);
745 RtcTime
.Minute
= RtcRead (RTC_ADDRESS_MINUTES_ALARM
);
746 RtcTime
.Hour
= RtcRead (RTC_ADDRESS_HOURS_ALARM
);
747 RtcTime
.Day
= RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH
);
748 RtcTime
.Month
= RtcRead (RTC_ADDRESS_MONTH
);
749 RtcTime
.Year
= RtcRead (RTC_ADDRESS_YEAR
);
750 RtcTime
.TimeZone
= Global
->SavedTimeZone
;
751 RtcTime
.Daylight
= Global
->Daylight
;
755 // Set the Y/M/D info to variable as it has no corresponding hw registers.
757 Status
= EfiSetVariable (
760 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
764 if (EFI_ERROR (Status
)) {
765 if (!EfiAtRuntime ()) {
766 EfiReleaseLock (&Global
->RtcLock
);
768 return EFI_DEVICE_ERROR
;
772 // Inhibit updates of the RTC
774 RegisterB
.Bits
.Set
= 1;
775 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
779 // Set RTC alarm time
781 RtcWrite (RTC_ADDRESS_SECONDS_ALARM
, RtcTime
.Second
);
782 RtcWrite (RTC_ADDRESS_MINUTES_ALARM
, RtcTime
.Minute
);
783 RtcWrite (RTC_ADDRESS_HOURS_ALARM
, RtcTime
.Hour
);
785 RegisterB
.Bits
.Aie
= 1;
788 RegisterB
.Bits
.Aie
= 0;
791 // Allow updates of the RTC registers
793 RegisterB
.Bits
.Set
= 0;
794 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
799 if (!EfiAtRuntime ()) {
800 EfiReleaseLock (&Global
->RtcLock
);
807 Checks an 8-bit BCD value, and converts to an 8-bit value if valid.
809 This function checks the 8-bit BCD value specified by Value.
810 If valid, the function converts it to an 8-bit value and returns it.
811 Otherwise, return 0xff.
813 @param Value The 8-bit BCD value to check and convert
815 @return The 8-bit value converted. Or 0xff if Value is invalid.
819 CheckAndConvertBcd8ToDecimal8 (
823 if ((Value
< 0xa0) && ((Value
& 0xf) < 0xa)) {
824 return BcdToDecimal8 (Value
);
831 Converts time read from RTC to EFI_TIME format defined by UEFI spec.
833 This function converts raw time data read from RTC to the EFI_TIME format
834 defined by UEFI spec.
835 If data mode of RTC is BCD, then converts it to decimal,
836 If RTC is in 12-hour format, then converts it to 24-hour format.
838 @param Time On input, the time data read from RTC to convert
839 On output, the time converted to UEFI format
840 @param Century Value of century read from RTC.
841 @param RegisterB Value of Register B of RTC, indicating data mode
844 @retval EFI_INVALID_PARAMETER Parameters passed in are invalid.
845 @retval EFI_SUCCESS Convert RTC time to EFI time successfully.
849 ConvertRtcTimeToEfiTime (
850 IN OUT EFI_TIME
*Time
,
852 IN RTC_REGISTER_B RegisterB
857 if ((Time
->Hour
& 0x80) != 0) {
863 Time
->Hour
= (UINT8
) (Time
->Hour
& 0x7f);
865 if (RegisterB
.Bits
.Dm
== 0) {
866 Time
->Year
= CheckAndConvertBcd8ToDecimal8 ((UINT8
) Time
->Year
);
867 Time
->Month
= CheckAndConvertBcd8ToDecimal8 (Time
->Month
);
868 Time
->Day
= CheckAndConvertBcd8ToDecimal8 (Time
->Day
);
869 Time
->Hour
= CheckAndConvertBcd8ToDecimal8 (Time
->Hour
);
870 Time
->Minute
= CheckAndConvertBcd8ToDecimal8 (Time
->Minute
);
871 Time
->Second
= CheckAndConvertBcd8ToDecimal8 (Time
->Second
);
873 Century
= CheckAndConvertBcd8ToDecimal8 (Century
);
875 if (Time
->Year
== 0xff || Time
->Month
== 0xff || Time
->Day
== 0xff ||
876 Time
->Hour
== 0xff || Time
->Minute
== 0xff || Time
->Second
== 0xff ||
878 return EFI_INVALID_PARAMETER
;
881 Time
->Year
= (UINT16
) (Century
* 100 + Time
->Year
);
884 // If time is in 12 hour format, convert it to 24 hour format
886 if (RegisterB
.Bits
.Mil
== 0) {
887 if (IsPM
&& Time
->Hour
< 12) {
888 Time
->Hour
= (UINT8
) (Time
->Hour
+ 12);
891 if (!IsPM
&& Time
->Hour
== 12) {
896 Time
->Nanosecond
= 0;
902 Wait for a period for the RTC to be ready.
904 @param Timeout Tell how long it should take to wait.
906 @retval EFI_DEVICE_ERROR RTC device error.
907 @retval EFI_SUCCESS RTC is updated and ready.
914 RTC_REGISTER_A RegisterA
;
915 RTC_REGISTER_D RegisterD
;
918 // See if the RTC is functioning correctly
920 RegisterD
.Data
= RtcRead (RTC_ADDRESS_REGISTER_D
);
922 if (RegisterD
.Bits
.Vrt
== 0) {
923 return EFI_DEVICE_ERROR
;
926 // Wait for up to 0.1 seconds for the RTC to be ready.
928 Timeout
= (Timeout
/ 10) + 1;
929 RegisterA
.Data
= RtcRead (RTC_ADDRESS_REGISTER_A
);
930 while (RegisterA
.Bits
.Uip
== 1 && Timeout
> 0) {
931 MicroSecondDelay (10);
932 RegisterA
.Data
= RtcRead (RTC_ADDRESS_REGISTER_A
);
936 RegisterD
.Data
= RtcRead (RTC_ADDRESS_REGISTER_D
);
937 if (Timeout
== 0 || RegisterD
.Bits
.Vrt
== 0) {
938 return EFI_DEVICE_ERROR
;
945 See if all fields of a variable of EFI_TIME type is correct.
947 @param Time The time to be checked.
949 @retval EFI_INVALID_PARAMETER Some fields of Time are not correct.
950 @retval EFI_SUCCESS Time is a valid EFI_TIME variable.
958 if (Time
->Year
< PcdGet16 (PcdMinimalValidYear
) ||
959 Time
->Year
> PcdGet16 (PcdMaximalValidYear
) ||
962 (!DayValid (Time
)) ||
966 Time
->Nanosecond
> 999999999 ||
967 (!(Time
->TimeZone
== EFI_UNSPECIFIED_TIMEZONE
|| (Time
->TimeZone
>= -1440 && Time
->TimeZone
<= 1440))) ||
968 ((Time
->Daylight
& (~(EFI_TIME_ADJUST_DAYLIGHT
| EFI_TIME_IN_DAYLIGHT
))) != 0)) {
969 return EFI_INVALID_PARAMETER
;
976 See if field Day of an EFI_TIME is correct.
978 @param Time Its Day field is to be checked.
980 @retval TRUE Day field of Time is correct.
981 @retval FALSE Day field of Time is NOT correct.
1000 DayOfMonth
[10] = 30;
1001 DayOfMonth
[11] = 31;
1004 // The validity of Time->Month field should be checked before
1006 ASSERT (Time
->Month
>=1);
1007 ASSERT (Time
->Month
<=12);
1008 if (Time
->Day
< 1 ||
1009 Time
->Day
> DayOfMonth
[Time
->Month
- 1] ||
1010 (Time
->Month
== 2 && (!IsLeapYear (Time
) && Time
->Day
> 28))
1019 Check if it is a leap year.
1021 @param Time The time to be checked.
1023 @retval TRUE It is a leap year.
1024 @retval FALSE It is NOT a leap year.
1031 if (Time
->Year
% 4 == 0) {
1032 if (Time
->Year
% 100 == 0) {
1033 if (Time
->Year
% 400 == 0) {
1047 Converts time from EFI_TIME format defined by UEFI spec to RTC's.
1049 This function converts time from EFI_TIME format defined by UEFI spec to RTC's.
1050 If data mode of RTC is BCD, then converts EFI_TIME to it.
1051 If RTC is in 12-hour format, then converts EFI_TIME to it.
1053 @param Time On input, the time data read from UEFI to convert
1054 On output, the time converted to RTC format
1055 @param RegisterB Value of Register B of RTC, indicating data mode
1056 @param Century It is set according to EFI_TIME Time.
1060 ConvertEfiTimeToRtcTime (
1061 IN OUT EFI_TIME
*Time
,
1062 IN RTC_REGISTER_B RegisterB
,
1070 // Adjust hour field if RTC is in 12 hour mode
1072 if (RegisterB
.Bits
.Mil
== 0) {
1073 if (Time
->Hour
< 12) {
1077 if (Time
->Hour
>= 13) {
1078 Time
->Hour
= (UINT8
) (Time
->Hour
- 12);
1079 } else if (Time
->Hour
== 0) {
1084 // Set the Time/Date/Daylight Savings values.
1086 *Century
= DecimalToBcd8 ((UINT8
) (Time
->Year
/ 100));
1088 Time
->Year
= (UINT16
) (Time
->Year
% 100);
1090 if (RegisterB
.Bits
.Dm
== 0) {
1091 Time
->Year
= DecimalToBcd8 ((UINT8
) Time
->Year
);
1092 Time
->Month
= DecimalToBcd8 (Time
->Month
);
1093 Time
->Day
= DecimalToBcd8 (Time
->Day
);
1094 Time
->Hour
= DecimalToBcd8 (Time
->Hour
);
1095 Time
->Minute
= DecimalToBcd8 (Time
->Minute
);
1096 Time
->Second
= DecimalToBcd8 (Time
->Second
);
1099 // If we are in 12 hour mode and PM is set, then set bit 7 of the Hour field.
1101 if (RegisterB
.Bits
.Mil
== 0 && IsPM
) {
1102 Time
->Hour
= (UINT8
) (Time
->Hour
| 0x80);
1107 Compare the Hour, Minute and Second of the From time and the To time.
1109 Only compare H/M/S in EFI_TIME and ignore other fields here.
1111 @param From the first time
1112 @param To the second time
1114 @return >0 The H/M/S of the From time is later than those of To time
1115 @return ==0 The H/M/S of the From time is same as those of To time
1116 @return <0 The H/M/S of the From time is earlier than those of To time
1124 if ((From
->Hour
> To
->Hour
) ||
1125 ((From
->Hour
== To
->Hour
) && (From
->Minute
> To
->Minute
)) ||
1126 ((From
->Hour
== To
->Hour
) && (From
->Minute
== To
->Minute
) && (From
->Second
> To
->Second
))) {
1128 } else if ((From
->Hour
== To
->Hour
) && (From
->Minute
== To
->Minute
) && (From
->Second
== To
->Second
)) {
1136 To check if second date is later than first date within 24 hours.
1138 @param From the first date
1139 @param To the second date
1141 @retval TRUE From is previous to To within 24 hours.
1142 @retval FALSE From is later, or it is previous to To more than 24 hours.
1150 UINT8 DayOfMonth
[12];
1163 DayOfMonth
[10] = 30;
1164 DayOfMonth
[11] = 31;
1169 // The validity of From->Month field should be checked before
1171 ASSERT (From
->Month
>=1);
1172 ASSERT (From
->Month
<=12);
1174 if (From
->Year
== To
->Year
) {
1175 if (From
->Month
== To
->Month
) {
1176 if ((From
->Day
+ 1) == To
->Day
) {
1177 if ((CompareHMS(From
, To
) >= 0)) {
1180 } else if (From
->Day
== To
->Day
) {
1181 if ((CompareHMS(From
, To
) <= 0)) {
1185 } else if (((From
->Month
+ 1) == To
->Month
) && (To
->Day
== 1)) {
1186 if ((From
->Month
== 2) && !IsLeapYear(From
)) {
1187 if (From
->Day
== 28) {
1188 if ((CompareHMS(From
, To
) >= 0)) {
1192 } else if (From
->Day
== DayOfMonth
[From
->Month
- 1]) {
1193 if ((CompareHMS(From
, To
) >= 0)) {
1198 } else if (((From
->Year
+ 1) == To
->Year
) &&
1199 (From
->Month
== 12) &&
1200 (From
->Day
== 31) &&
1203 if ((CompareHMS(From
, To
) >= 0)) {