X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=MdeModulePkg%2FUniversal%2FPcatRealTimeClockRuntimeDxe%2FPcRtc.c;h=cf5ed0bf5a7d2870c64e551e59dec2b524508d97;hb=9a1eee23398a27389a2bc35f51cf0ec72710a7e0;hp=a58aee5f8a53d793fdaf72c6d391e78b6f032456;hpb=8cd4d17c68652442f440a52549aa6231da412381;p=mirror_edk2.git diff --git a/MdeModulePkg/Universal/PcatRealTimeClockRuntimeDxe/PcRtc.c b/MdeModulePkg/Universal/PcatRealTimeClockRuntimeDxe/PcRtc.c index a58aee5f8a..cf5ed0bf5a 100644 --- a/MdeModulePkg/Universal/PcatRealTimeClockRuntimeDxe/PcRtc.c +++ b/MdeModulePkg/Universal/PcatRealTimeClockRuntimeDxe/PcRtc.c @@ -1,114 +1,100 @@ -/*++ +/** @file + RTC Architectural Protocol GUID as defined in DxeCis 0.96. -Copyright (c) 2006 - 2007, Intel Corporation. All rights reserved.
-This software and associated documentation (if any) is furnished -under a license and may only be used or copied in accordance -with the terms of the license. Except as permitted by such -license, no part of this software or documentation may be -reproduced, stored in a retrieval system, or transmitted in any -form or by any means without the express written consent of -Intel Corporation. +Copyright (c) 2006 - 2007, Intel Corporation +All rights reserved. This program and the accompanying materials +are licensed and made available under the terms and conditions of the BSD License +which accompanies this distribution. The full text of the license may be found at +http://opensource.org/licenses/bsd-license.php +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +**/ -Module Name: - - PcRtc.c - -Abstract: - - RTC Architectural Protocol GUID as defined in DxeCis 0.96 +#include "PcRtc.h" ---*/ +/** + Compare the Hour, Minute and Second of the From time and the To time. + + Only compare H/M/S in EFI_TIME and ignore other fields here. -#include "PcRtc.h" + @param From the first time + @param To the second time -STATIC + @return >0 The H/M/S of the From time is later than those of To time + @return ==0 The H/M/S of the From time is same as those of To time + @return <0 The H/M/S of the From time is earlier than those of To time +**/ INTN CompareHMS ( IN EFI_TIME *From, IN EFI_TIME *To ); -STATIC +/** + To check if second date is later than first date within 24 hours. + + @param From the first date + @param To the second date + + @retval TRUE From is previous to To within 24 hours. + @retval FALSE From is later, or it is previous to To more than 24 hours. +**/ BOOLEAN IsWithinOneDay ( IN EFI_TIME *From, IN EFI_TIME *To ); -STATIC +/** + Read RTC content through its registers. + + @param Address Address offset of RTC. It is recommended to use macros such as + RTC_ADDRESS_SECONDS. + + @return The data of UINT8 type read from RTC. +**/ UINT8 RtcRead ( IN UINT8 Address ) -/*++ - -Routine Description: - - GC_TODO: Add function description - -Arguments: - - Address - GC_TODO: add argument description - -Returns: - - GC_TODO: add return values - ---*/ { IoWrite8 (PCAT_RTC_ADDRESS_REGISTER, (UINT8) (Address | (UINT8) (IoRead8 (PCAT_RTC_ADDRESS_REGISTER) & 0x80))); return IoRead8 (PCAT_RTC_DATA_REGISTER); } -STATIC +/** + Write RTC through its registers. + + @param Address Address offset of RTC. It is recommended to use macros such as + RTC_ADDRESS_SECONDS. + @param Data The content you want to write into RTC. + +**/ VOID RtcWrite ( IN UINT8 Address, IN UINT8 Data ) -/*++ - -Routine Description: - - GC_TODO: Add function description - -Arguments: - - Address - GC_TODO: add argument description - Data - GC_TODO: add argument description - -Returns: - - GC_TODO: add return values - ---*/ { IoWrite8 (PCAT_RTC_ADDRESS_REGISTER, (UINT8) (Address | (UINT8) (IoRead8 (PCAT_RTC_ADDRESS_REGISTER) & 0x80))); IoWrite8 (PCAT_RTC_DATA_REGISTER, Data); } +/** + Initialize RTC. + + @param Global For global use inside this module. + + @retval EFI_DEVICE_ERROR Initialization failed due to device error. + @retval EFI_SUCCESS Initialization successful. + +**/ EFI_STATUS PcRtcInit ( IN PC_RTC_MODULE_GLOBALS *Global ) -/*++ - -Routine Description: - - GC_TODO: Add function description - -Arguments: - - Global - GC_TODO: add argument description - -Returns: - - EFI_DEVICE_ERROR - GC_TODO: Add description for return value - EFI_SUCCESS - GC_TODO: Add description for return value - ---*/ { EFI_STATUS Status; RTC_REGISTER_A RegisterA; @@ -116,14 +102,15 @@ Returns: RTC_REGISTER_D RegisterD; UINT8 Century; EFI_TIME Time; + UINTN DataSize; + UINT32 TimerVar; // // Acquire RTC Lock to make access to RTC atomic // - //BugBug: the EfiAtRuntime should be encapsulated in EfiAcquireLock or - // provide a new instance for EfiAcquireLock, say, RtEfiAcquireLock + //Code here doesn't consider the runtime environment. if (!EfiAtRuntime ()) { - EfiAcquireLock (&Global->RtcLock); + EfiAcquireLock (&Global->RtcLock); } // // Initialize RTC Register @@ -153,12 +140,11 @@ Returns: // // Wait for up to 0.1 seconds for the RTC to be updated // - Status = RtcWaitToUpdate (100000); + Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout)); if (EFI_ERROR (Status)) { - //BugBug: the EfiAtRuntime should be encapsulated in EfiAcquireLock or - // provide a new instance for EfiAcquireLock, say, RtEfiAcquireLock + //Code here doesn't consider the runtime environment. if (!EfiAtRuntime ()) { - EfiReleaseLock (&Global->RtcLock); + EfiReleaseLock (&Global->RtcLock); } return EFI_DEVICE_ERROR; } @@ -172,33 +158,30 @@ Returns: Time.Month = RtcRead (RTC_ADDRESS_MONTH); Time.Year = RtcRead (RTC_ADDRESS_YEAR); - ConvertRtcTimeToEfiTime (&Time, RegisterB); - - if (RtcTestCenturyRegister () == EFI_SUCCESS) { - Century = BcdToDecimal8 ((UINT8) (RtcRead (RTC_ADDRESS_CENTURY) & 0x7f)); - } else { - Century = BcdToDecimal8 (RtcRead (RTC_ADDRESS_CENTURY)); - } - + Century = RtcRead (RTC_ADDRESS_CENTURY); Time.Year = (UINT16) (Century * 100 + Time.Year); - + // // Set RTC configuration after get original time + // The value of bit AIE should be reserved. // - RtcWrite (RTC_ADDRESS_REGISTER_B, RTC_INIT_REGISTER_B); + RtcWrite (RTC_ADDRESS_REGISTER_B, (UINT8)(RTC_INIT_REGISTER_B | (RegisterB.Data & BIT5))); // // Release RTC Lock. // - //BugBug: the EfiAtRuntime should be encapsulated in EfiAcquireLock or - // provide a new instance for EfiAcquireLock, say, RtEfiAcquireLock + //Code here doesn't consider the runtime environment. if (!EfiAtRuntime ()) { - EfiReleaseLock (&Global->RtcLock); + EfiReleaseLock (&Global->RtcLock); } + // // Validate time fields // - Status = RtcTimeFieldsValid (&Time); + Status = ConvertRtcTimeToEfiTime (&Time, Century, RegisterB); + if (!EFI_ERROR (Status)) { + Status = RtcTimeFieldsValid (&Time); + } if (EFI_ERROR (Status)) { Time.Second = RTC_INIT_SECOND; Time.Minute = RTC_INIT_MINUTE; @@ -208,6 +191,25 @@ Returns: Time.Year = RTC_INIT_YEAR; } // + // Get the data of Daylight saving and time zone, if they have been + // stored in NV variable during previous boot. + // + DataSize = sizeof (UINT32); + Status = EfiGetVariable ( + L"RTC", + &gEfiCallerIdGuid, + NULL, + &DataSize, + (VOID *) &TimerVar + ); + if (!EFI_ERROR (Status)) { + Global->SavedTimeZone = (INT16) TimerVar; + Global->Daylight = (UINT8) (TimerVar >> 16); + + Time.TimeZone = Global->SavedTimeZone; + Time.Daylight = Global->Daylight; + } + // // Reset time value according to new RTC configuration // PcRtcSetTime (&Time, Global); @@ -215,26 +217,26 @@ Returns: return EFI_SUCCESS; } -EFI_STATUS -PcRtcGetTime ( - OUT EFI_TIME *Time, - IN EFI_TIME_CAPABILITIES *Capabilities, - IN PC_RTC_MODULE_GLOBALS *Global - ) -/*++ +/** + Returns the current time and date information, and the time-keeping capabilities + of the hardware platform. -Routine Description: + @param Time A pointer to storage to receive a snapshot of the current time. + @param Capabilities An optional pointer to a buffer to receive the real time clock + device's capabilities. + @param Global For global use inside this module. - Arguments: + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER Time is NULL. + @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error. - Returns: ---*/ -// GC_TODO: Time - add argument and description to function comment -// GC_TODO: Capabilities - add argument and description to function comment -// GC_TODO: Global - add argument and description to function comment -// GC_TODO: EFI_INVALID_PARAMETER - add return value to function comment -// GC_TODO: EFI_DEVICE_ERROR - add return value to function comment -// GC_TODO: EFI_SUCCESS - add return value to function comment +**/ +EFI_STATUS +PcRtcGetTime ( + OUT EFI_TIME *Time, + OUT EFI_TIME_CAPABILITIES *Capabilities, OPTIONAL + IN PC_RTC_MODULE_GLOBALS *Global + ) { EFI_STATUS Status; RTC_REGISTER_B RegisterB; @@ -250,20 +252,18 @@ Routine Description: // // Acquire RTC Lock to make access to RTC atomic // - //BugBug: the EfiAtRuntime should be encapsulated in EfiAcquireLock or - // provide a new instance for EfiAcquireLock, say, RtEfiAcquireLock + //Code here doesn't consider the runtime environment. if (!EfiAtRuntime ()) { - EfiAcquireLock (&Global->RtcLock); + EfiAcquireLock (&Global->RtcLock); } // // Wait for up to 0.1 seconds for the RTC to be updated // - Status = RtcWaitToUpdate (100000); + Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout)); if (EFI_ERROR (Status)) { - //BugBug: the EfiAtRuntime should be encapsulated in EfiReleaseLock or - // provide a new instance for EfiReleaseLock, say, RtEfiReleaseLock + //Code here doesn't consider the runtime environment. if (!EfiAtRuntime ()) { - EfiReleaseLock (&Global->RtcLock); + EfiReleaseLock (&Global->RtcLock); } return Status; } @@ -282,26 +282,18 @@ Routine Description: Time->Month = RtcRead (RTC_ADDRESS_MONTH); Time->Year = RtcRead (RTC_ADDRESS_YEAR); - ConvertRtcTimeToEfiTime (Time, RegisterB); - - if (RtcTestCenturyRegister () == EFI_SUCCESS) { - Century = BcdToDecimal8 ((UINT8) (RtcRead (RTC_ADDRESS_CENTURY) & 0x7f)); - } else { - Century = BcdToDecimal8 (RtcRead (RTC_ADDRESS_CENTURY)); - } - + Century = RtcRead (RTC_ADDRESS_CENTURY); Time->Year = (UINT16) (Century * 100 + Time->Year); - + // // Release RTC Lock. // - //BugBug: the EfiAtRuntime should be encapsulated in EfiReleaseLock or - // provide a new instance for EfiReleaseLock, say, RtEfiReleaseLock + //Code here doesn't consider the runtime environment. if (!EfiAtRuntime ()) { - EfiReleaseLock (&Global->RtcLock); + EfiReleaseLock (&Global->RtcLock); } // - // Get the variable that containts the TimeZone and Daylight fields + // Get the variable that contains the TimeZone and Daylight fields // Time->TimeZone = Global->SavedTimeZone; Time->Daylight = Global->Daylight; @@ -309,14 +301,17 @@ Routine Description: // // Make sure all field values are in correct range // - Status = RtcTimeFieldsValid (Time); + Status = ConvertRtcTimeToEfiTime (Time, Century, RegisterB); + if (!EFI_ERROR (Status)) { + Status = RtcTimeFieldsValid (Time); + } if (EFI_ERROR (Status)) { return EFI_DEVICE_ERROR; } // // Fill in Capabilities if it was passed in // - if (Capabilities) { + if (Capabilities != NULL) { Capabilities->Resolution = 1; // // 1 hertz @@ -331,27 +326,28 @@ Routine Description: return EFI_SUCCESS; } +/** + Sets the current local time and date information. + + @param Time A pointer to the current time. + @param Global For global use inside this module. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER A time field is out of range. + @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error. + +**/ EFI_STATUS PcRtcSetTime ( IN EFI_TIME *Time, IN PC_RTC_MODULE_GLOBALS *Global ) -/*++ - -Routine Description: - - Arguments: - - Returns: ---*/ -// GC_TODO: Time - add argument and description to function comment -// GC_TODO: Global - add argument and description to function comment -// GC_TODO: EFI_INVALID_PARAMETER - add return value to function comment { EFI_STATUS Status; EFI_TIME RtcTime; RTC_REGISTER_B RegisterB; UINT8 Century; + UINT32 TimerVar; if (Time == NULL) { return EFI_INVALID_PARAMETER; @@ -369,20 +365,18 @@ Routine Description: // // Acquire RTC Lock to make access to RTC atomic // - //BugBug: the EfiAtRuntime should be encapsulated in EfiAcquireLock or - // provide a new instance for EfiAcquireLock, say, RtEfiAcquireLock + //Code here doesn't consider the runtime environment. if (!EfiAtRuntime ()) { - EfiAcquireLock (&Global->RtcLock); + EfiAcquireLock (&Global->RtcLock); } // // Wait for up to 0.1 seconds for the RTC to be updated // - Status = RtcWaitToUpdate (100000); + Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout)); if (EFI_ERROR (Status)) { - //BugBug: the EfiAtRuntime should be encapsulated in EfiReleaseLock or - // provide a new instance for EfiReleaseLock, say, RtEfiReleaseLock + //Code here doesn't consider the runtime environment. if (!EfiAtRuntime ()) { - EfiReleaseLock (&Global->RtcLock); + EfiReleaseLock (&Global->RtcLock); } return Status; } @@ -401,10 +395,6 @@ Routine Description: RtcWrite (RTC_ADDRESS_DAY_OF_THE_MONTH, RtcTime.Day); RtcWrite (RTC_ADDRESS_MONTH, RtcTime.Month); RtcWrite (RTC_ADDRESS_YEAR, (UINT8) RtcTime.Year); - if (RtcTestCenturyRegister () == EFI_SUCCESS) { - Century = (UINT8) ((Century & 0x7f) | (RtcRead (RTC_ADDRESS_CENTURY) & 0x80)); - } - RtcWrite (RTC_ADDRESS_CENTURY, Century); // @@ -416,44 +406,53 @@ Routine Description: // // Release RTC Lock. // - //BugBug: the EfiAtRuntime should be encapsulated in EfiReleaseLock or - // provide a new instance for EfiReleaseLock, say, RtEfiReleaseLock + //Code here doesn't consider the runtime environment. if (!EfiAtRuntime ()) { - EfiReleaseLock (&Global->RtcLock); + EfiReleaseLock (&Global->RtcLock); } // - // Set the variable that containts the TimeZone and Daylight fields + // Set the variable that contains the TimeZone and Daylight fields // Global->SavedTimeZone = Time->TimeZone; Global->Daylight = Time->Daylight; - return Status; + + TimerVar = Time->Daylight; + TimerVar = (UINT32) ((TimerVar << 16) | Time->TimeZone); + Status = EfiSetVariable ( + L"RTC", + &gEfiCallerIdGuid, + EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE, + sizeof (TimerVar), + &TimerVar + ); + ASSERT_EFI_ERROR (Status); + + return EFI_SUCCESS; } +/** + Returns the current wakeup alarm clock setting. + + @param Enabled Indicates if the alarm is currently enabled or disabled. + @param Pending Indicates if the alarm signal is pending and requires acknowledgment. + @param Time The current alarm setting. + @param Global For global use inside this module. + + @retval EFI_SUCCESS The alarm settings were returned. + @retval EFI_INVALID_PARAMETER Enabled is NULL. + @retval EFI_INVALID_PARAMETER Pending is NULL. + @retval EFI_INVALID_PARAMETER Time is NULL. + @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error. + @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform. + +**/ EFI_STATUS PcRtcGetWakeupTime ( OUT BOOLEAN *Enabled, OUT BOOLEAN *Pending, OUT EFI_TIME *Time, - IN PC_RTC_MODULE_GLOBALS *Global + IN PC_RTC_MODULE_GLOBALS *Global ) -/*++ - -Routine Description: - - Arguments: - - - -Returns: ---*/ -// GC_TODO: Enabled - add argument and description to function comment -// GC_TODO: Pending - add argument and description to function comment -// GC_TODO: Time - add argument and description to function comment -// GC_TODO: Global - add argument and description to function comment -// GC_TODO: EFI_INVALID_PARAMETER - add return value to function comment -// GC_TODO: EFI_DEVICE_ERROR - add return value to function comment -// GC_TODO: EFI_DEVICE_ERROR - add return value to function comment -// GC_TODO: EFI_SUCCESS - add return value to function comment { EFI_STATUS Status; RTC_REGISTER_B RegisterB; @@ -461,7 +460,7 @@ Returns: UINT8 Century; // - // Check paramters for null pointers + // Check parameters for null pointers // if ((Enabled == NULL) || (Pending == NULL) || (Time == NULL)) { return EFI_INVALID_PARAMETER; @@ -470,18 +469,16 @@ Returns: // // Acquire RTC Lock to make access to RTC atomic // - //BugBug: the EfiAtRuntime should be encapsulated in EfiAcquireLock or - // provide a new instance for EfiAcquireLock, say, RtEfiAcquireLock + //Code here doesn't consider the runtime environment. if (!EfiAtRuntime ()) { - EfiAcquireLock (&Global->RtcLock); + EfiAcquireLock (&Global->RtcLock); } // // Wait for up to 0.1 seconds for the RTC to be updated // - Status = RtcWaitToUpdate (100000); + Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout)); if (EFI_ERROR (Status)) { - //BugBug: the EfiAtRuntime should be encapsulated in EfiReleaseLock or - // provide a new instance for EfiReleaseLock, say, RtEfiReleaseLock + //Code here doesn't consider the runtime environment. if (!EfiAtRuntime ()) { EfiReleaseLock (&Global->RtcLock); } @@ -513,28 +510,23 @@ Returns: Time->Year = RtcRead (RTC_ADDRESS_YEAR); } - ConvertRtcTimeToEfiTime (Time, RegisterB); - - if (RtcTestCenturyRegister () == EFI_SUCCESS) { - Century = BcdToDecimal8 ((UINT8) (RtcRead (RTC_ADDRESS_CENTURY) & 0x7f)); - } else { - Century = BcdToDecimal8 (RtcRead (RTC_ADDRESS_CENTURY)); - } - + Century = RtcRead (RTC_ADDRESS_CENTURY); Time->Year = (UINT16) (Century * 100 + Time->Year); - + // // Release RTC Lock. // - //BugBug: the EfiAtRuntime should be encapsulated in EfiReleaseLock or - // provide a new instance for EfiReleaseLock, say, RtEfiReleaseLock + //Code here doesn't consider the runtime environment. if (!EfiAtRuntime ()) { - EfiReleaseLock (&Global->RtcLock); + EfiReleaseLock (&Global->RtcLock); } // // Make sure all field values are in correct range // - Status = RtcTimeFieldsValid (Time); + Status = ConvertRtcTimeToEfiTime (Time, Century, RegisterB); + if (!EFI_ERROR (Status)) { + Status = RtcTimeFieldsValid (Time); + } if (EFI_ERROR (Status)) { return EFI_DEVICE_ERROR; } @@ -544,30 +536,27 @@ Returns: return EFI_SUCCESS; } +/** + Sets the system wakeup alarm clock time. + + @param Enabled Enable or disable the wakeup alarm. + @param Time If Enable is TRUE, the time to set the wakeup alarm for. + If Enable is FALSE, then this parameter is optional, and may be NULL. + @param Global For global use inside this module. + + @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. + If Enable is FALSE, then the wakeup alarm was disabled. + @retval EFI_INVALID_PARAMETER A time field is out of range. + @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error. + @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform. + +**/ EFI_STATUS PcRtcSetWakeupTime ( IN BOOLEAN Enable, - OUT EFI_TIME *Time, + IN EFI_TIME *Time, OPTIONAL IN PC_RTC_MODULE_GLOBALS *Global ) -/*++ - -Routine Description: - - Arguments: - - - -Returns: ---*/ -// GC_TODO: Enable - add argument and description to function comment -// GC_TODO: Time - add argument and description to function comment -// GC_TODO: Global - add argument and description to function comment -// GC_TODO: EFI_INVALID_PARAMETER - add return value to function comment -// GC_TODO: EFI_INVALID_PARAMETER - add return value to function comment -// GC_TODO: EFI_UNSUPPORTED - add return value to function comment -// GC_TODO: EFI_DEVICE_ERROR - add return value to function comment -// GC_TODO: EFI_SUCCESS - add return value to function comment { EFI_STATUS Status; EFI_TIME RtcTime; @@ -591,6 +580,10 @@ Returns: // Just support set alarm time within 24 hours // PcRtcGetTime (&RtcTime, &Capabilities, Global); + Status = RtcTimeFieldsValid (&RtcTime); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } if (!IsWithinOneDay (&RtcTime, Time)) { return EFI_UNSUPPORTED; } @@ -603,18 +596,16 @@ Returns: // // Acquire RTC Lock to make access to RTC atomic // - //BugBug: the EfiAtRuntime should be encapsulated in EfiAcquireLock or - // provide a new instance for EfiAcquireLock, say, RtEfiAcquireLock + //Code here doesn't consider the runtime environment. if (!EfiAtRuntime ()) { - EfiAcquireLock (&Global->RtcLock); + EfiAcquireLock (&Global->RtcLock); } // // Wait for up to 0.1 seconds for the RTC to be updated // - Status = RtcWaitToUpdate (100000); + Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout)); if (EFI_ERROR (Status)) { - //BugBug: the EfiAtRuntime should be encapsulated in EfiReleaseLock or - // provide a new instance for EfiReleaseLock, say, RtEfiReleaseLock + //Code here doesn't consider the runtime environment. if (!EfiAtRuntime ()) { EfiReleaseLock (&Global->RtcLock); } @@ -652,92 +643,100 @@ Returns: // // Release RTC Lock. // - //BugBug: the EfiAtRuntime should be encapsulated in EfiReleaseLock or - // provide a new instance for EfiReleaseLock, say, RtEfiReleaseLock + //Code here doesn't consider the runtime environment. if (!EfiAtRuntime ()) { - EfiReleaseLock (&Global->RtcLock); + EfiReleaseLock (&Global->RtcLock); } return EFI_SUCCESS; } -EFI_STATUS -RtcTestCenturyRegister ( - VOID - ) -/*++ -Routine Description: +/** + Checks an 8-bit BCD value, and converts to an 8-bit value if valid. - Arguments: + This function checks the 8-bit BCD value specified by Value. + If valid, the function converts it to an 8-bit value and returns it. + Otherwise, return 0xff. + @param Value The 8-bit BCD value to check and convert + @return The 8-bit value converted. Or 0xff if Value is invalid. -Returns: ---*/ -// GC_TODO: EFI_SUCCESS - add return value to function comment -// GC_TODO: EFI_DEVICE_ERROR - add return value to function comment +**/ +UINT8 +CheckAndConvertBcd8ToDecimal8 ( + IN UINT8 Value + ) { - UINT8 Century; - UINT8 Temp; - - Century = RtcRead (RTC_ADDRESS_CENTURY); - // - // RtcWrite (RTC_ADDRESS_CENTURY, 0x00); - // - Temp = (UINT8) (RtcRead (RTC_ADDRESS_CENTURY) & 0x7f); - RtcWrite (RTC_ADDRESS_CENTURY, Century); - if (Temp == 0x19 || Temp == 0x20) { - return EFI_SUCCESS; + if ((Value < 0xa0) && ((Value & 0xf) < 0xa)) { + return BcdToDecimal8 (Value); } - return EFI_DEVICE_ERROR; + return 0xff; } -VOID -ConvertRtcTimeToEfiTime ( - IN EFI_TIME *Time, - IN RTC_REGISTER_B RegisterB - ) -/*++ - -Routine Description: +/** + Converts time read from RTC to EFI_TIME format defined by UEFI spec. - Arguments: + This function converts raw time data read from RTC to the EFI_TIME format + defined by UEFI spec. + If data mode of RTC is BCD, then converts it to decimal, + If RTC is in 12-hour format, then converts it to 24-hour format. + @param Time On input, the time data read from RTC to convert + On output, the time converted to UEFI format + @param Century Value of century read from RTC. + @param RegisterB Value of Register B of RTC, indicating data mode + and hour format. + @retval EFI_INVALID_PARAMETER Parameters passed in are invalid. + @retval EFI_SUCCESS Convert RTC time to EFI time successfully. -Returns: ---*/ -// GC_TODO: Time - add argument and description to function comment -// GC_TODO: RegisterB - add argument and description to function comment +**/ +EFI_STATUS +ConvertRtcTimeToEfiTime ( + IN OUT EFI_TIME *Time, + IN UINT8 Century, + IN RTC_REGISTER_B RegisterB + ) { - BOOLEAN PM; + BOOLEAN IsPM; - if ((Time->Hour) & 0x80) { - PM = TRUE; + if ((Time->Hour & 0x80) != 0) { + IsPM = TRUE; } else { - PM = FALSE; + IsPM = FALSE; } Time->Hour = (UINT8) (Time->Hour & 0x7f); if (RegisterB.Bits.DM == 0) { - Time->Year = BcdToDecimal8 ((UINT8) Time->Year); - Time->Month = BcdToDecimal8 (Time->Month); - Time->Day = BcdToDecimal8 (Time->Day); - Time->Hour = BcdToDecimal8 (Time->Hour); - Time->Minute = BcdToDecimal8 (Time->Minute); - Time->Second = BcdToDecimal8 (Time->Second); + Time->Year = CheckAndConvertBcd8ToDecimal8 ((UINT8) Time->Year); + Time->Month = CheckAndConvertBcd8ToDecimal8 (Time->Month); + Time->Day = CheckAndConvertBcd8ToDecimal8 (Time->Day); + Time->Hour = CheckAndConvertBcd8ToDecimal8 (Time->Hour); + Time->Minute = CheckAndConvertBcd8ToDecimal8 (Time->Minute); + Time->Second = CheckAndConvertBcd8ToDecimal8 (Time->Second); + Century = CheckAndConvertBcd8ToDecimal8 (Century); } + + if (Time->Year == 0xff || Time->Month == 0xff || Time->Day == 0xff || + Time->Hour == 0xff || Time->Minute == 0xff || Time->Second == 0xff || + Century == 0xff) { + return EFI_INVALID_PARAMETER; + } + + Time->Year = (UINT16) (Century * 100 + Time->Year); + // // If time is in 12 hour format, convert it to 24 hour format // if (RegisterB.Bits.MIL == 0) { - if (PM && Time->Hour < 12) { + if (IsPM && Time->Hour < 12) { Time->Hour = (UINT8) (Time->Hour + 12); } - if (!PM && Time->Hour == 12) { + if (!IsPM && Time->Hour == 12) { Time->Hour = 0; } } @@ -745,25 +744,22 @@ Returns: Time->Nanosecond = 0; Time->TimeZone = EFI_UNSPECIFIED_TIMEZONE; Time->Daylight = 0; + + return EFI_SUCCESS; } +/** + Wait for a period for the RTC to be ready. + + @param Timeout Tell how long it should take to wait. + + @retval EFI_DEVICE_ERROR RTC device error. + @retval EFI_SUCCESS RTC is updated and ready. +**/ EFI_STATUS RtcWaitToUpdate ( UINTN Timeout ) -/*++ - -Routine Description: - - Arguments: - - -Returns: ---*/ -// GC_TODO: Timeout - add argument and description to function comment -// GC_TODO: EFI_DEVICE_ERROR - add return value to function comment -// GC_TODO: EFI_DEVICE_ERROR - add return value to function comment -// GC_TODO: EFI_SUCCESS - add return value to function comment { RTC_REGISTER_A RegisterA; RTC_REGISTER_D RegisterD; @@ -795,59 +791,51 @@ Returns: return EFI_SUCCESS; } +/** + See if all fields of a variable of EFI_TIME type is correct. + + @param Time The time to be checked. + + @retval EFI_INVALID_PARAMETER Some fields of Time are not correct. + @retval EFI_SUCCESS Time is a valid EFI_TIME variable. + +**/ EFI_STATUS RtcTimeFieldsValid ( IN EFI_TIME *Time ) -/*++ - -Routine Description: - - Arguments: - - Returns: ---*/ -// GC_TODO: Time - add argument and description to function comment -// GC_TODO: EFI_INVALID_PARAMETER - add return value to function comment -// GC_TODO: EFI_SUCCESS - add return value to function comment { if (Time->Year < 1998 || Time->Year > 2099 || Time->Month < 1 || Time->Month > 12 || - (!DayValid (Time)) || Time->Hour > 23 || Time->Minute > 59 || Time->Second > 59 || Time->Nanosecond > 999999999 || (!(Time->TimeZone == EFI_UNSPECIFIED_TIMEZONE || (Time->TimeZone >= -1440 && Time->TimeZone <= 1440))) || - (Time->Daylight & (~(EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT))) + ((Time->Daylight & (~(EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT))) != 0) ) { - return EFI_INVALID_PARAMETER; + if (!DayValid (Time)) { + return EFI_INVALID_PARAMETER; + } } return EFI_SUCCESS; } +/** + See if field Day of an EFI_TIME is correct. + + @param Time Its Day field is to be checked. + + @retval TRUE Day field of Time is correct. + @retval FALSE Day field of Time is NOT correct. +**/ BOOLEAN DayValid ( IN EFI_TIME *Time ) -/*++ - -Routine Description: - - GC_TODO: Add function description - -Arguments: - - Time - GC_TODO: add argument description - -Returns: - - GC_TODO: add return values - ---*/ { INTN DayOfMonth[12]; @@ -864,6 +852,11 @@ Returns: DayOfMonth[10] = 30; DayOfMonth[11] = 31; + // + // The validity of Time->Month field should be checked before + // + ASSERT (Time->Month >=1); + ASSERT (Time->Month <=12); if (Time->Day < 1 || Time->Day > DayOfMonth[Time->Month - 1] || (Time->Month == 2 && (!IsLeapYear (Time) && Time->Day > 28)) @@ -874,25 +867,18 @@ Returns: return TRUE; } +/** + Check if it is a leap year. + + @param Time The time to be checked. + + @retval TRUE It is a leap year. + @retval FALSE It is NOT a leap year. +**/ BOOLEAN IsLeapYear ( IN EFI_TIME *Time ) -/*++ - -Routine Description: - - GC_TODO: Add function description - -Arguments: - - Time - GC_TODO: add argument description - -Returns: - - GC_TODO: add return values - ---*/ { if (Time->Year % 4 == 0) { if (Time->Year % 100 == 0) { @@ -909,34 +895,35 @@ Returns: } } -VOID -ConvertEfiTimeToRtcTime ( - IN EFI_TIME *Time, - IN RTC_REGISTER_B RegisterB, - IN UINT8 *Century - ) -/*++ - -Routine Description: +/** + Converts time from EFI_TIME format defined by UEFI spec to RTC's. - Arguments: + This function converts time from EFI_TIME format defined by UEFI spec to RTC's. + If data mode of RTC is BCD, then converts EFI_TIME to it. + If RTC is in 12-hour format, then converts EFI_TIME to it. + @param Time On input, the time data read from UEFI to convert + On output, the time converted to RTC format + @param RegisterB Value of Register B of RTC, indicating data mode + @param Century It is set according to EFI_TIME Time. -Returns: ---*/ -// GC_TODO: Time - add argument and description to function comment -// GC_TODO: RegisterB - add argument and description to function comment -// GC_TODO: Century - add argument and description to function comment +**/ +VOID +ConvertEfiTimeToRtcTime ( + IN OUT EFI_TIME *Time, + IN RTC_REGISTER_B RegisterB, + OUT UINT8 *Century + ) { - BOOLEAN PM; + BOOLEAN IsPM; - PM = TRUE; + IsPM = TRUE; // - // Adjust hour field if RTC in in 12 hour mode + // Adjust hour field if RTC is in 12 hour mode // if (RegisterB.Bits.MIL == 0) { if (Time->Hour < 12) { - PM = FALSE; + IsPM = FALSE; } if (Time->Hour >= 13) { @@ -963,36 +950,28 @@ Returns: // // If we are in 12 hour mode and PM is set, then set bit 7 of the Hour field. // - if (RegisterB.Bits.MIL == 0 && PM) { + if (RegisterB.Bits.MIL == 0 && IsPM) { Time->Hour = (UINT8) (Time->Hour | 0x80); } } -STATIC +/** + Compare the Hour, Minute and Second of the From time and the To time. + + Only compare H/M/S in EFI_TIME and ignore other fields here. + + @param From the first time + @param To the second time + + @return >0 The H/M/S of the From time is later than those of To time + @return ==0 The H/M/S of the From time is same as those of To time + @return <0 The H/M/S of the From time is earlier than those of To time +**/ INTN CompareHMS ( IN EFI_TIME *From, IN EFI_TIME *To ) -/*++ - -Routine Description: - - Compare the Hour, Minute and Second of the 'From' time and the 'To' time. - Only compare H/M/S in EFI_TIME and ignore other fields here. - -Arguments: - - From - the first time - To - the second time - -Returns: - - >0 : The H/M/S of the 'From' time is later than those of 'To' time - ==0 : The H/M/S of the 'From' time is same as those of 'To' time - <0 : The H/M/S of the 'From' time is earlier than those of 'To' time - ---*/ { if ((From->Hour > To->Hour) || ((From->Hour == To->Hour) && (From->Minute > To->Minute)) || @@ -1005,33 +984,45 @@ Returns: } } -STATIC +/** + To check if second date is later than first date within 24 hours. + + @param From the first date + @param To the second date + + @retval TRUE From is previous to To within 24 hours. + @retval FALSE From is later, or it is previous to To more than 24 hours. +**/ BOOLEAN IsWithinOneDay ( IN EFI_TIME *From, IN EFI_TIME *To ) -/*++ - -Routine Description: - - Judge whether two days are adjacent. - -Arguments: - - From - the first day - To - the second day - -Returns: +{ + UINT8 DayOfMonth[12]; + BOOLEAN Adjacent; - TRUE - The interval of two days are within one day. - FALSE - The interval of two days exceed ony day or parameter error. + DayOfMonth[0] = 31; + DayOfMonth[1] = 29; + DayOfMonth[2] = 31; + DayOfMonth[3] = 30; + DayOfMonth[4] = 31; + DayOfMonth[5] = 30; + DayOfMonth[6] = 31; + DayOfMonth[7] = 31; + DayOfMonth[8] = 30; + DayOfMonth[9] = 31; + DayOfMonth[10] = 30; + DayOfMonth[11] = 31; ---*/ -{ - UINT8 DayOfMonth[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; - BOOLEAN Adjacent = FALSE; + Adjacent = FALSE; + // + // The validity of From->Month field should be checked before + // + ASSERT (From->Month >=1); + ASSERT (From->Month <=12); + if (From->Year == To->Year) { if (From->Month == To->Month) { if ((From->Day + 1) == To->Day) {