2 RTC Architectural Protocol GUID as defined in DxeCis 0.96.
4 Copyright (c) 2006 - 2007, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Compare the Hour, Minute and Second of the From time and the To time.
20 Only compare H/M/S in EFI_TIME and ignore other fields here.
22 @param From the first time
23 @param To the second time
25 @return >0 The H/M/S of the From time is later than those of To time
26 @return ==0 The H/M/S of the From time is same as those of To time
27 @return <0 The H/M/S of the From time is earlier than those of To time
36 To check if second date is later than first date within 24 hours.
38 @param From the first date
39 @param To the second date
41 @retval TRUE From is previous to To within 24 hours.
42 @retval FALSE From is later, or it is previous to To more than 24 hours.
51 Read RTC content through its registers.
53 @param Address Address offset of RTC. It is recommended to use macros such as
56 @return The data of UINT8 type read from RTC.
63 IoWrite8 (PCAT_RTC_ADDRESS_REGISTER
, (UINT8
) (Address
| (UINT8
) (IoRead8 (PCAT_RTC_ADDRESS_REGISTER
) & 0x80)));
64 return IoRead8 (PCAT_RTC_DATA_REGISTER
);
68 Write RTC through its registers.
70 @param Address Address offset of RTC. It is recommended to use macros such as
72 @param Data The content you want to write into RTC.
81 IoWrite8 (PCAT_RTC_ADDRESS_REGISTER
, (UINT8
) (Address
| (UINT8
) (IoRead8 (PCAT_RTC_ADDRESS_REGISTER
) & 0x80)));
82 IoWrite8 (PCAT_RTC_DATA_REGISTER
, Data
);
88 @param Global For global use inside this module.
90 @retval EFI_DEVICE_ERROR Initialization failed due to device error.
91 @retval EFI_SUCCESS Initialization successful.
96 IN PC_RTC_MODULE_GLOBALS
*Global
100 RTC_REGISTER_A RegisterA
;
101 RTC_REGISTER_B RegisterB
;
102 RTC_REGISTER_D RegisterD
;
109 // Acquire RTC Lock to make access to RTC atomic
111 //BugBug: the EfiAtRuntime should be encapsulated in EfiAcquireLock or
112 // provide a new instance for EfiAcquireLock, say, RtEfiAcquireLock
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
)) {
146 //BugBug: the EfiAtRuntime should be encapsulated in EfiAcquireLock or
147 // provide a new instance for EfiAcquireLock, say, RtEfiAcquireLock
148 if (!EfiAtRuntime ()) {
149 EfiReleaseLock (&Global
->RtcLock
);
151 return EFI_DEVICE_ERROR
;
154 // Get the Time/Date/Daylight Savings values.
156 Time
.Second
= RtcRead (RTC_ADDRESS_SECONDS
);
157 Time
.Minute
= RtcRead (RTC_ADDRESS_MINUTES
);
158 Time
.Hour
= RtcRead (RTC_ADDRESS_HOURS
);
159 Time
.Day
= RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH
);
160 Time
.Month
= RtcRead (RTC_ADDRESS_MONTH
);
161 Time
.Year
= RtcRead (RTC_ADDRESS_YEAR
);
163 if (RtcTestCenturyRegister () == EFI_SUCCESS
) {
164 Century
= (UINT8
) (RtcRead (RTC_ADDRESS_CENTURY
) & 0x7f);
166 Century
= RtcRead (RTC_ADDRESS_CENTURY
);
170 // Set RTC configuration after get original time
171 // The value of bit AIE should be reserved.
173 RtcWrite (RTC_ADDRESS_REGISTER_B
, (UINT8
)(RTC_INIT_REGISTER_B
| (RegisterB
.Data
& BIT5
)));
178 //BugBug: the EfiAtRuntime should be encapsulated in EfiAcquireLock or
179 // provide a new instance for EfiAcquireLock, say, RtEfiAcquireLock
181 if (!EfiAtRuntime ()) {
182 EfiReleaseLock (&Global
->RtcLock
);
186 // Validate time fields
188 Status
= ConvertRtcTimeToEfiTime (&Time
, Century
, RegisterB
);
189 if (!EFI_ERROR (Status
)) {
190 Status
= RtcTimeFieldsValid (&Time
);
192 if (EFI_ERROR (Status
)) {
193 Time
.Second
= RTC_INIT_SECOND
;
194 Time
.Minute
= RTC_INIT_MINUTE
;
195 Time
.Hour
= RTC_INIT_HOUR
;
196 Time
.Day
= RTC_INIT_DAY
;
197 Time
.Month
= RTC_INIT_MONTH
;
198 Time
.Year
= RTC_INIT_YEAR
;
201 // Get the data of Daylight saving and time zone, if they have been
202 // stored in NV variable during previous boot.
204 DataSize
= sizeof (UINT32
);
205 Status
= EfiGetVariable (
207 &gEfiGenericPlatformVariableGuid
,
212 if (!EFI_ERROR (Status
)) {
213 Global
->SavedTimeZone
= (INT16
) TimerVar
;
214 Global
->Daylight
= (UINT8
) (TimerVar
>> 16);
216 Time
.TimeZone
= Global
->SavedTimeZone
;
217 Time
.Daylight
= Global
->Daylight
;
220 // Reset time value according to new RTC configuration
222 PcRtcSetTime (&Time
, Global
);
228 Returns the current time and date information, and the time-keeping capabilities
229 of the hardware platform.
231 @param Time A pointer to storage to receive a snapshot of the current time.
232 @param Capabilities An optional pointer to a buffer to receive the real time clock
233 device's capabilities.
234 @param Global For global use inside this module.
236 @retval EFI_SUCCESS The operation completed successfully.
237 @retval EFI_INVALID_PARAMETER Time is NULL.
238 @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.
244 OUT EFI_TIME_CAPABILITIES
*Capabilities
, OPTIONAL
245 IN PC_RTC_MODULE_GLOBALS
*Global
249 RTC_REGISTER_B RegisterB
;
253 // Check parameters for null pointer
256 return EFI_INVALID_PARAMETER
;
260 // Acquire RTC Lock to make access to RTC atomic
262 //BugBug: the EfiAtRuntime should be encapsulated in EfiAcquireLock or
263 // provide a new instance for EfiAcquireLock, say, RtEfiAcquireLock
264 if (!EfiAtRuntime ()) {
265 EfiAcquireLock (&Global
->RtcLock
);
268 // Wait for up to 0.1 seconds for the RTC to be updated
270 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
271 if (EFI_ERROR (Status
)) {
272 //BugBug: the EfiAtRuntime should be encapsulated in EfiReleaseLock or
273 // provide a new instance for EfiReleaseLock, say, RtEfiReleaseLock
274 if (!EfiAtRuntime ()) {
275 EfiReleaseLock (&Global
->RtcLock
);
282 RegisterB
.Data
= RtcRead (RTC_ADDRESS_REGISTER_B
);
285 // Get the Time/Date/Daylight Savings values.
287 Time
->Second
= RtcRead (RTC_ADDRESS_SECONDS
);
288 Time
->Minute
= RtcRead (RTC_ADDRESS_MINUTES
);
289 Time
->Hour
= RtcRead (RTC_ADDRESS_HOURS
);
290 Time
->Day
= RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH
);
291 Time
->Month
= RtcRead (RTC_ADDRESS_MONTH
);
292 Time
->Year
= RtcRead (RTC_ADDRESS_YEAR
);
294 if (RtcTestCenturyRegister () == EFI_SUCCESS
) {
295 Century
= (UINT8
) (RtcRead (RTC_ADDRESS_CENTURY
) & 0x7f);
297 Century
= RtcRead (RTC_ADDRESS_CENTURY
);
303 //BugBug: the EfiAtRuntime should be encapsulated in EfiReleaseLock or
304 // provide a new instance for EfiReleaseLock, say, RtEfiReleaseLock
305 if (!EfiAtRuntime ()) {
306 EfiReleaseLock (&Global
->RtcLock
);
309 // Get the variable that contains the TimeZone and Daylight fields
311 Time
->TimeZone
= Global
->SavedTimeZone
;
312 Time
->Daylight
= Global
->Daylight
;
315 // Make sure all field values are in correct range
317 Status
= ConvertRtcTimeToEfiTime (Time
, Century
, RegisterB
);
318 if (!EFI_ERROR (Status
)) {
319 Status
= RtcTimeFieldsValid (Time
);
321 if (EFI_ERROR (Status
)) {
322 return EFI_DEVICE_ERROR
;
325 // Fill in Capabilities if it was passed in
327 if (Capabilities
!= NULL
) {
328 Capabilities
->Resolution
= 1;
332 Capabilities
->Accuracy
= 50000000;
336 Capabilities
->SetsToZero
= FALSE
;
343 Sets the current local time and date information.
345 @param Time A pointer to the current time.
346 @param Global For global use inside this module.
348 @retval EFI_SUCCESS The operation completed successfully.
349 @retval EFI_INVALID_PARAMETER A time field is out of range.
350 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
356 IN PC_RTC_MODULE_GLOBALS
*Global
361 RTC_REGISTER_B RegisterB
;
366 return EFI_INVALID_PARAMETER
;
369 // Make sure that the time fields are valid
371 Status
= RtcTimeFieldsValid (Time
);
372 if (EFI_ERROR (Status
)) {
376 CopyMem (&RtcTime
, Time
, sizeof (EFI_TIME
));
379 // Acquire RTC Lock to make access to RTC atomic
381 //BugBug: the EfiAtRuntime should be encapsulated in EfiAcquireLock or
382 // provide a new instance for EfiAcquireLock, say, RtEfiAcquireLock
383 if (!EfiAtRuntime ()) {
384 EfiAcquireLock (&Global
->RtcLock
);
387 // Wait for up to 0.1 seconds for the RTC to be updated
389 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
390 if (EFI_ERROR (Status
)) {
391 //BugBug: the EfiAtRuntime should be encapsulated in EfiReleaseLock or
392 // provide a new instance for EfiReleaseLock, say, RtEfiReleaseLock
393 if (!EfiAtRuntime ()) {
394 EfiReleaseLock (&Global
->RtcLock
);
399 // Read Register B, and inhibit updates of the RTC
401 RegisterB
.Data
= RtcRead (RTC_ADDRESS_REGISTER_B
);
402 RegisterB
.Bits
.SET
= 1;
403 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
405 ConvertEfiTimeToRtcTime (&RtcTime
, RegisterB
, &Century
);
407 RtcWrite (RTC_ADDRESS_SECONDS
, RtcTime
.Second
);
408 RtcWrite (RTC_ADDRESS_MINUTES
, RtcTime
.Minute
);
409 RtcWrite (RTC_ADDRESS_HOURS
, RtcTime
.Hour
);
410 RtcWrite (RTC_ADDRESS_DAY_OF_THE_MONTH
, RtcTime
.Day
);
411 RtcWrite (RTC_ADDRESS_MONTH
, RtcTime
.Month
);
412 RtcWrite (RTC_ADDRESS_YEAR
, (UINT8
) RtcTime
.Year
);
413 if (RtcTestCenturyRegister () == EFI_SUCCESS
) {
414 Century
= (UINT8
) ((Century
& 0x7f) | (RtcRead (RTC_ADDRESS_CENTURY
) & 0x80));
417 RtcWrite (RTC_ADDRESS_CENTURY
, Century
);
420 // Allow updates of the RTC registers
422 RegisterB
.Bits
.SET
= 0;
423 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
428 //BugBug: the EfiAtRuntime should be encapsulated in EfiReleaseLock or
429 // provide a new instance for EfiReleaseLock, say, RtEfiReleaseLock
430 if (!EfiAtRuntime ()) {
431 EfiReleaseLock (&Global
->RtcLock
);
434 // Set the variable that contains the TimeZone and Daylight fields
436 Global
->SavedTimeZone
= Time
->TimeZone
;
437 Global
->Daylight
= Time
->Daylight
;
439 TimerVar
= Time
->Daylight
;
440 TimerVar
= (UINT32
) ((TimerVar
<< 16) | Time
->TimeZone
);
441 Status
= EfiSetVariable (
443 &gEfiGenericPlatformVariableGuid
,
444 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
448 ASSERT_EFI_ERROR (Status
);
454 Returns the current wakeup alarm clock setting.
456 @param Enabled Indicates if the alarm is currently enabled or disabled.
457 @param Pending Indicates if the alarm signal is pending and requires acknowledgment.
458 @param Time The current alarm setting.
459 @param Global For global use inside this module.
461 @retval EFI_SUCCESS The alarm settings were returned.
462 @retval EFI_INVALID_PARAMETER Enabled is NULL.
463 @retval EFI_INVALID_PARAMETER Pending is NULL.
464 @retval EFI_INVALID_PARAMETER Time is NULL.
465 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
466 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
471 OUT BOOLEAN
*Enabled
,
472 OUT BOOLEAN
*Pending
,
474 IN PC_RTC_MODULE_GLOBALS
*Global
478 RTC_REGISTER_B RegisterB
;
479 RTC_REGISTER_C RegisterC
;
483 // Check parameters for null pointers
485 if ((Enabled
== NULL
) || (Pending
== NULL
) || (Time
== NULL
)) {
486 return EFI_INVALID_PARAMETER
;
490 // Acquire RTC Lock to make access to RTC atomic
492 //BugBug: the EfiAtRuntime should be encapsulated in EfiAcquireLock or
493 // provide a new instance for EfiAcquireLock, say, RtEfiAcquireLock
494 if (!EfiAtRuntime ()) {
495 EfiAcquireLock (&Global
->RtcLock
);
498 // Wait for up to 0.1 seconds for the RTC to be updated
500 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
501 if (EFI_ERROR (Status
)) {
502 //BugBug: the EfiAtRuntime should be encapsulated in EfiReleaseLock or
503 // provide a new instance for EfiReleaseLock, say, RtEfiReleaseLock
504 if (!EfiAtRuntime ()) {
505 EfiReleaseLock (&Global
->RtcLock
);
507 return EFI_DEVICE_ERROR
;
510 // Read Register B and Register C
512 RegisterB
.Data
= RtcRead (RTC_ADDRESS_REGISTER_B
);
513 RegisterC
.Data
= RtcRead (RTC_ADDRESS_REGISTER_C
);
516 // Get the Time/Date/Daylight Savings values.
518 *Enabled
= RegisterB
.Bits
.AIE
;
520 Time
->Second
= RtcRead (RTC_ADDRESS_SECONDS_ALARM
);
521 Time
->Minute
= RtcRead (RTC_ADDRESS_MINUTES_ALARM
);
522 Time
->Hour
= RtcRead (RTC_ADDRESS_HOURS_ALARM
);
523 Time
->Day
= RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH
);
524 Time
->Month
= RtcRead (RTC_ADDRESS_MONTH
);
525 Time
->Year
= RtcRead (RTC_ADDRESS_YEAR
);
530 Time
->Day
= RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH
);
531 Time
->Month
= RtcRead (RTC_ADDRESS_MONTH
);
532 Time
->Year
= RtcRead (RTC_ADDRESS_YEAR
);
535 if (RtcTestCenturyRegister () == EFI_SUCCESS
) {
536 Century
= (UINT8
) (RtcRead (RTC_ADDRESS_CENTURY
) & 0x7f);
538 Century
= RtcRead (RTC_ADDRESS_CENTURY
);
544 //BugBug: the EfiAtRuntime should be encapsulated in EfiReleaseLock or
545 // provide a new instance for EfiReleaseLock, say, RtEfiReleaseLock
546 if (!EfiAtRuntime ()) {
547 EfiReleaseLock (&Global
->RtcLock
);
550 // Make sure all field values are in correct range
552 Status
= ConvertRtcTimeToEfiTime (Time
, Century
, RegisterB
);
553 if (!EFI_ERROR (Status
)) {
554 Status
= RtcTimeFieldsValid (Time
);
556 if (EFI_ERROR (Status
)) {
557 return EFI_DEVICE_ERROR
;
560 *Pending
= RegisterC
.Bits
.AF
;
566 Sets the system wakeup alarm clock time.
568 @param Enabled Enable or disable the wakeup alarm.
569 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
570 If Enable is FALSE, then this parameter is optional, and may be NULL.
571 @param Global For global use inside this module.
573 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled.
574 If Enable is FALSE, then the wakeup alarm was disabled.
575 @retval EFI_INVALID_PARAMETER A time field is out of range.
576 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
577 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
583 IN EFI_TIME
*Time
, OPTIONAL
584 IN PC_RTC_MODULE_GLOBALS
*Global
589 RTC_REGISTER_B RegisterB
;
591 EFI_TIME_CAPABILITIES Capabilities
;
596 return EFI_INVALID_PARAMETER
;
599 // Make sure that the time fields are valid
601 Status
= RtcTimeFieldsValid (Time
);
602 if (EFI_ERROR (Status
)) {
603 return EFI_INVALID_PARAMETER
;
606 // Just support set alarm time within 24 hours
608 PcRtcGetTime (&RtcTime
, &Capabilities
, Global
);
609 if (!IsWithinOneDay (&RtcTime
, Time
)) {
610 return EFI_UNSUPPORTED
;
613 // Make a local copy of the time and date
615 CopyMem (&RtcTime
, Time
, sizeof (EFI_TIME
));
619 // Acquire RTC Lock to make access to RTC atomic
621 //BugBug: the EfiAtRuntime should be encapsulated in EfiAcquireLock or
622 // provide a new instance for EfiAcquireLock, say, RtEfiAcquireLock
623 if (!EfiAtRuntime ()) {
624 EfiAcquireLock (&Global
->RtcLock
);
627 // Wait for up to 0.1 seconds for the RTC to be updated
629 Status
= RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout
));
630 if (EFI_ERROR (Status
)) {
631 //BugBug: the EfiAtRuntime should be encapsulated in EfiReleaseLock or
632 // provide a new instance for EfiReleaseLock, say, RtEfiReleaseLock
633 if (!EfiAtRuntime ()) {
634 EfiReleaseLock (&Global
->RtcLock
);
636 return EFI_DEVICE_ERROR
;
639 // Read Register B, and inhibit updates of the RTC
641 RegisterB
.Data
= RtcRead (RTC_ADDRESS_REGISTER_B
);
643 RegisterB
.Bits
.SET
= 1;
644 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
647 ConvertEfiTimeToRtcTime (&RtcTime
, RegisterB
, &Century
);
650 // Set RTC alarm time
652 RtcWrite (RTC_ADDRESS_SECONDS_ALARM
, RtcTime
.Second
);
653 RtcWrite (RTC_ADDRESS_MINUTES_ALARM
, RtcTime
.Minute
);
654 RtcWrite (RTC_ADDRESS_HOURS_ALARM
, RtcTime
.Hour
);
656 RegisterB
.Bits
.AIE
= 1;
659 RegisterB
.Bits
.AIE
= 0;
662 // Allow updates of the RTC registers
664 RegisterB
.Bits
.SET
= 0;
665 RtcWrite (RTC_ADDRESS_REGISTER_B
, RegisterB
.Data
);
670 //BugBug: the EfiAtRuntime should be encapsulated in EfiReleaseLock or
671 // provide a new instance for EfiReleaseLock, say, RtEfiReleaseLock
672 if (!EfiAtRuntime ()) {
673 EfiReleaseLock (&Global
->RtcLock
);
679 See if century register of RTC is valid.
681 @retval EFI_SUCCESS Century register is valid.
682 @retval EFI_DEVICE_ERROR Century register is NOT valid.
685 RtcTestCenturyRegister (
692 Century
= RtcRead (RTC_ADDRESS_CENTURY
);
693 Temp
= (UINT8
) (RtcRead (RTC_ADDRESS_CENTURY
) & 0x7f);
694 RtcWrite (RTC_ADDRESS_CENTURY
, Century
);
695 if (Temp
== 0x19 || Temp
== 0x20) {
699 return EFI_DEVICE_ERROR
;
703 Checks an 8-bit BCD value, and converts to an 8-bit value if valid.
705 This function checks the 8-bit BCD value specified by Value.
706 If valid, the function converts it to an 8-bit value and returns it.
707 Otherwise, return 0xff.
709 @param Value The 8-bit BCD value to check and convert
711 @return The 8-bit value converted. Or 0xff if Value is invalid.
715 CheckAndConvertBcd8ToDecimal8 (
719 if ((Value
< 0xa0) && ((Value
& 0xf) < 0xa)) {
720 return BcdToDecimal8 (Value
);
727 Converts time read from RTC to EFI_TIME format defined by UEFI spec.
729 This function converts raw time data read from RTC to the EFI_TIME format
730 defined by UEFI spec.
731 If data mode of RTC is BCD, then converts it to decimal,
732 If RTC is in 12-hour format, then converts it to 24-hour format.
734 @param Time On input, the time data read from RTC to convert
735 On output, the time converted to UEFI format
736 @param Century Value of century read from RTC.
737 @param RegisterB Value of Register B of RTC, indicating data mode
740 @retval EFI_INVALID_PARAMETER Parameters passed in are invalid.
741 @retval EFI_SUCCESS Convert RTC time to EFI time successfully.
745 ConvertRtcTimeToEfiTime (
746 IN OUT EFI_TIME
*Time
,
748 IN RTC_REGISTER_B RegisterB
753 if ((Time
->Hour
& 0x80) != 0) {
759 Time
->Hour
= (UINT8
) (Time
->Hour
& 0x7f);
761 if (RegisterB
.Bits
.DM
== 0) {
762 Time
->Year
= CheckAndConvertBcd8ToDecimal8 ((UINT8
) Time
->Year
);
763 Time
->Month
= CheckAndConvertBcd8ToDecimal8 (Time
->Month
);
764 Time
->Day
= CheckAndConvertBcd8ToDecimal8 (Time
->Day
);
765 Time
->Hour
= CheckAndConvertBcd8ToDecimal8 (Time
->Hour
);
766 Time
->Minute
= CheckAndConvertBcd8ToDecimal8 (Time
->Minute
);
767 Time
->Second
= CheckAndConvertBcd8ToDecimal8 (Time
->Second
);
768 Century
= CheckAndConvertBcd8ToDecimal8 (Century
);
771 if (Time
->Year
== 0xff || Time
->Month
== 0xff || Time
->Day
== 0xff ||
772 Time
->Hour
== 0xff || Time
->Minute
== 0xff || Time
->Second
== 0xff ||
774 return EFI_INVALID_PARAMETER
;
777 Time
->Year
= (UINT16
) (Century
* 100 + Time
->Year
);
780 // If time is in 12 hour format, convert it to 24 hour format
782 if (RegisterB
.Bits
.MIL
== 0) {
783 if (IsPM
&& Time
->Hour
< 12) {
784 Time
->Hour
= (UINT8
) (Time
->Hour
+ 12);
787 if (!IsPM
&& Time
->Hour
== 12) {
792 Time
->Nanosecond
= 0;
793 Time
->TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
800 Wait for a period for the RTC to be ready.
802 @param Timeout Tell how long it should take to wait.
804 @retval EFI_DEVICE_ERROR RTC device error.
805 @retval EFI_SUCCESS RTC is updated and ready.
812 RTC_REGISTER_A RegisterA
;
813 RTC_REGISTER_D RegisterD
;
816 // See if the RTC is functioning correctly
818 RegisterD
.Data
= RtcRead (RTC_ADDRESS_REGISTER_D
);
820 if (RegisterD
.Bits
.VRT
== 0) {
821 return EFI_DEVICE_ERROR
;
824 // Wait for up to 0.1 seconds for the RTC to be ready.
826 Timeout
= (Timeout
/ 10) + 1;
827 RegisterA
.Data
= RtcRead (RTC_ADDRESS_REGISTER_A
);
828 while (RegisterA
.Bits
.UIP
== 1 && Timeout
> 0) {
829 MicroSecondDelay (10);
830 RegisterA
.Data
= RtcRead (RTC_ADDRESS_REGISTER_A
);
834 RegisterD
.Data
= RtcRead (RTC_ADDRESS_REGISTER_D
);
835 if (Timeout
== 0 || RegisterD
.Bits
.VRT
== 0) {
836 return EFI_DEVICE_ERROR
;
843 See if all fields of a variable of EFI_TIME type is correct.
845 @param Time The time to be checked.
847 @retval EFI_INVALID_PARAMETER Some fields of Time are not correct.
848 @retval EFI_SUCCESS Time is a valid EFI_TIME variable.
856 if (Time
->Year
< 1998 ||
860 (!DayValid (Time
)) ||
864 Time
->Nanosecond
> 999999999 ||
865 (!(Time
->TimeZone
== EFI_UNSPECIFIED_TIMEZONE
|| (Time
->TimeZone
>= -1440 && Time
->TimeZone
<= 1440))) ||
866 ((Time
->Daylight
& (~(EFI_TIME_ADJUST_DAYLIGHT
| EFI_TIME_IN_DAYLIGHT
))) != 0)
868 return EFI_INVALID_PARAMETER
;
875 See if field Day of an EFI_TIME is correct.
877 @param Time Its Day field is to be checked.
879 @retval TRUE Day field of Time is correct.
880 @retval FALSE Day field of Time is NOT correct.
903 Time
->Day
> DayOfMonth
[Time
->Month
- 1] ||
904 (Time
->Month
== 2 && (!IsLeapYear (Time
) && Time
->Day
> 28))
913 Check if it is a leap year.
915 @param Time The time to be checked.
917 @retval TRUE It is a leap year.
918 @retval FALSE It is NOT a leap year.
925 if (Time
->Year
% 4 == 0) {
926 if (Time
->Year
% 100 == 0) {
927 if (Time
->Year
% 400 == 0) {
941 Converts time from EFI_TIME format defined by UEFI spec to RTC's.
943 This function converts time from EFI_TIME format defined by UEFI spec to RTC's.
944 If data mode of RTC is BCD, then converts EFI_TIME to it.
945 If RTC is in 12-hour format, then converts EFI_TIME to it.
947 @param Time On input, the time data read from UEFI to convert
948 On output, the time converted to RTC format
949 @param RegisterB Value of Register B of RTC, indicating data mode
950 @param Century It is set according to EFI_TIME Time.
954 ConvertEfiTimeToRtcTime (
955 IN OUT EFI_TIME
*Time
,
956 IN RTC_REGISTER_B RegisterB
,
964 // Adjust hour field if RTC is in 12 hour mode
966 if (RegisterB
.Bits
.MIL
== 0) {
967 if (Time
->Hour
< 12) {
971 if (Time
->Hour
>= 13) {
972 Time
->Hour
= (UINT8
) (Time
->Hour
- 12);
973 } else if (Time
->Hour
== 0) {
978 // Set the Time/Date/Daylight Savings values.
980 *Century
= DecimalToBcd8 ((UINT8
) (Time
->Year
/ 100));
982 Time
->Year
= (UINT16
) (Time
->Year
% 100);
984 if (RegisterB
.Bits
.DM
== 0) {
985 Time
->Year
= DecimalToBcd8 ((UINT8
) Time
->Year
);
986 Time
->Month
= DecimalToBcd8 (Time
->Month
);
987 Time
->Day
= DecimalToBcd8 (Time
->Day
);
988 Time
->Hour
= DecimalToBcd8 (Time
->Hour
);
989 Time
->Minute
= DecimalToBcd8 (Time
->Minute
);
990 Time
->Second
= DecimalToBcd8 (Time
->Second
);
993 // If we are in 12 hour mode and PM is set, then set bit 7 of the Hour field.
995 if (RegisterB
.Bits
.MIL
== 0 && IsPM
) {
996 Time
->Hour
= (UINT8
) (Time
->Hour
| 0x80);
1001 Compare the Hour, Minute and Second of the From time and the To time.
1003 Only compare H/M/S in EFI_TIME and ignore other fields here.
1005 @param From the first time
1006 @param To the second time
1008 @return >0 The H/M/S of the From time is later than those of To time
1009 @return ==0 The H/M/S of the From time is same as those of To time
1010 @return <0 The H/M/S of the From time is earlier than those of To time
1018 if ((From
->Hour
> To
->Hour
) ||
1019 ((From
->Hour
== To
->Hour
) && (From
->Minute
> To
->Minute
)) ||
1020 ((From
->Hour
== To
->Hour
) && (From
->Minute
== To
->Minute
) && (From
->Second
> To
->Second
))) {
1022 } else if ((From
->Hour
== To
->Hour
) && (From
->Minute
== To
->Minute
) && (From
->Second
== To
->Second
)) {
1030 To check if second date is later than first date within 24 hours.
1032 @param From the first date
1033 @param To the second date
1035 @retval TRUE From is previous to To within 24 hours.
1036 @retval FALSE From is later, or it is previous to To more than 24 hours.
1044 UINT8 DayOfMonth
[12];
1057 DayOfMonth
[10] = 30;
1058 DayOfMonth
[11] = 31;
1062 if (From
->Year
== To
->Year
) {
1063 if (From
->Month
== To
->Month
) {
1064 if ((From
->Day
+ 1) == To
->Day
) {
1065 if ((CompareHMS(From
, To
) >= 0)) {
1068 } else if (From
->Day
== To
->Day
) {
1069 if ((CompareHMS(From
, To
) <= 0)) {
1073 } else if (((From
->Month
+ 1) == To
->Month
) && (To
->Day
== 1)) {
1074 if ((From
->Month
== 2) && !IsLeapYear(From
)) {
1075 if (From
->Day
== 28) {
1076 if ((CompareHMS(From
, To
) >= 0)) {
1080 } else if (From
->Day
== DayOfMonth
[From
->Month
- 1]) {
1081 if ((CompareHMS(From
, To
) >= 0)) {
1086 } else if (((From
->Year
+ 1) == To
->Year
) &&
1087 (From
->Month
== 12) &&
1088 (From
->Day
== 31) &&
1091 if ((CompareHMS(From
, To
) >= 0)) {