X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=PcAtChipsetPkg%2FPcatRealTimeClockRuntimeDxe%2FPcRtc.c;h=857918df18b018fd712579678b09df33a45d5f3e;hp=a2a20c8e1cf8c35e388300ed543f4a5df53f6eb0;hb=ea99ba10ff0392554c9f82307949e1349d17888a;hpb=6bfa178ccaa1ec0fa2bfddf0a5ebbb931744e7a4 diff --git a/PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcRtc.c b/PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcRtc.c index a2a20c8e1c..857918df18 100644 --- a/PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcRtc.c +++ b/PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcRtc.c @@ -1,8 +1,8 @@ /** @file RTC Architectural Protocol GUID as defined in DxeCis 0.96. -Copyright (c) 2006 - 2007, Intel Corporation -All rights reserved. This program and the accompanying materials +Copyright (c) 2006 - 2016, 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 @@ -14,6 +14,16 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "PcRtc.h" +// +// Days of month. +// +UINTN mDayOfMonth[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; + +// +// The name of NV variable to store the timezone and daylight saving information. +// +CHAR16 mTimeZoneVariableName[] = L"RTC"; + /** Compare the Hour, Minute and Second of the From time and the To time. @@ -100,10 +110,11 @@ PcRtcInit ( RTC_REGISTER_A RegisterA; RTC_REGISTER_B RegisterB; RTC_REGISTER_D RegisterD; - UINT8 Century; EFI_TIME Time; UINTN DataSize; UINT32 TimerVar; + BOOLEAN Enabled; + BOOLEAN Pending; // // Acquire RTC Lock to make access to RTC atomic @@ -141,6 +152,11 @@ PcRtcInit ( // Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout)); if (EFI_ERROR (Status)) { + // + // Set the variable with default value if the RTC is functioning incorrectly. + // + Global->SavedTimeZone = EFI_UNSPECIFIED_TIMEZONE; + Global->Daylight = 0; if (!EfiAtRuntime ()) { EfiReleaseLock (&Global->RtcLock); } @@ -156,13 +172,12 @@ PcRtcInit ( Time.Month = RtcRead (RTC_ADDRESS_MONTH); Time.Year = RtcRead (RTC_ADDRESS_YEAR); - Century = RtcRead (RTC_ADDRESS_CENTURY); - // // Set RTC configuration after get original time // The value of bit AIE should be reserved. // - RtcWrite (RTC_ADDRESS_REGISTER_B, (UINT8)(RTC_INIT_REGISTER_B | (RegisterB.Data & BIT5))); + RegisterB.Data = RTC_INIT_REGISTER_B | (RegisterB.Data & BIT5); + RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data); // // Release RTC Lock. @@ -177,11 +192,11 @@ PcRtcInit ( // DataSize = sizeof (UINT32); Status = EfiGetVariable ( - L"RTC", + mTimeZoneVariableName, &gEfiCallerIdGuid, NULL, &DataSize, - (VOID *) &TimerVar + &TimerVar ); if (!EFI_ERROR (Status)) { Time.TimeZone = (INT16) TimerVar; @@ -194,24 +209,122 @@ PcRtcInit ( // // Validate time fields // - Status = ConvertRtcTimeToEfiTime (&Time, Century, RegisterB); + Status = ConvertRtcTimeToEfiTime (&Time, RegisterB); if (!EFI_ERROR (Status)) { Status = RtcTimeFieldsValid (&Time); } if (EFI_ERROR (Status)) { + // + // Report Status Code to indicate that the RTC has bad date and time + // + REPORT_STATUS_CODE ( + EFI_ERROR_CODE | EFI_ERROR_MINOR, + (EFI_SOFTWARE_DXE_RT_DRIVER | EFI_SW_EC_BAD_DATE_TIME) + ); Time.Second = RTC_INIT_SECOND; Time.Minute = RTC_INIT_MINUTE; Time.Hour = RTC_INIT_HOUR; Time.Day = RTC_INIT_DAY; Time.Month = RTC_INIT_MONTH; - Time.Year = RTC_INIT_YEAR; + Time.Year = PcdGet16 (PcdMinimalValidYear); + Time.Nanosecond = 0; + Time.TimeZone = EFI_UNSPECIFIED_TIMEZONE; + Time.Daylight = 0; } // // Reset time value according to new RTC configuration // - PcRtcSetTime (&Time, Global); + Status = PcRtcSetTime (&Time, Global); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + // + // Reset wakeup time value to valid state when wakeup alarm is disabled and wakeup time is invalid. + // Global variable has already had valid SavedTimeZone and Daylight, + // so we can use them to get and set wakeup time. + // + Status = PcRtcGetWakeupTime (&Enabled, &Pending, &Time, Global); + if ((Enabled) || (!EFI_ERROR (Status))) { + return EFI_SUCCESS; + } + + // + // When wakeup time is disabled and invalid, reset wakeup time register to valid state + // but keep wakeup alarm disabled. + // + Time.Second = RTC_INIT_SECOND; + Time.Minute = RTC_INIT_MINUTE; + Time.Hour = RTC_INIT_HOUR; + Time.Day = RTC_INIT_DAY; + Time.Month = RTC_INIT_MONTH; + Time.Year = PcdGet16 (PcdMinimalValidYear); + Time.Nanosecond = 0; + Time.TimeZone = Global->SavedTimeZone; + Time.Daylight = Global->Daylight;; + + // + // Acquire RTC Lock to make access to RTC atomic + // + if (!EfiAtRuntime ()) { + EfiAcquireLock (&Global->RtcLock); + } + // + // Wait for up to 0.1 seconds for the RTC to be updated + // + Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout)); + if (EFI_ERROR (Status)) { + if (!EfiAtRuntime ()) { + EfiReleaseLock (&Global->RtcLock); + } + return EFI_DEVICE_ERROR; + } + ConvertEfiTimeToRtcTime (&Time, RegisterB); + + // + // Set the Y/M/D info to variable as it has no corresponding hw registers. + // + Status = EfiSetVariable ( + L"RTCALARM", + &gEfiCallerIdGuid, + EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE, + sizeof (Time), + &Time + ); + if (EFI_ERROR (Status)) { + if (!EfiAtRuntime ()) { + EfiReleaseLock (&Global->RtcLock); + } + return EFI_DEVICE_ERROR; + } + + // + // Inhibit updates of the RTC + // + RegisterB.Bits.Set = 1; + RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data); + + // + // Set RTC alarm time registers + // + RtcWrite (RTC_ADDRESS_SECONDS_ALARM, Time.Second); + RtcWrite (RTC_ADDRESS_MINUTES_ALARM, Time.Minute); + RtcWrite (RTC_ADDRESS_HOURS_ALARM, Time.Hour); + + // + // Allow updates of the RTC registers + // + RegisterB.Bits.Set = 0; + RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data); + + // + // Release RTC Lock. + // + if (!EfiAtRuntime ()) { + EfiReleaseLock (&Global->RtcLock); + } return EFI_SUCCESS; } @@ -238,7 +351,6 @@ PcRtcGetTime ( { EFI_STATUS Status; RTC_REGISTER_B RegisterB; - UINT8 Century; // // Check parameters for null pointer @@ -278,8 +390,6 @@ PcRtcGetTime ( Time->Month = RtcRead (RTC_ADDRESS_MONTH); Time->Year = RtcRead (RTC_ADDRESS_YEAR); - Century = RtcRead (RTC_ADDRESS_CENTURY); - // // Release RTC Lock. // @@ -296,7 +406,7 @@ PcRtcGetTime ( // // Make sure all field values are in correct range // - Status = ConvertRtcTimeToEfiTime (Time, Century, RegisterB); + Status = ConvertRtcTimeToEfiTime (Time, RegisterB); if (!EFI_ERROR (Status)) { Status = RtcTimeFieldsValid (Time); } @@ -342,7 +452,6 @@ PcRtcSetTime ( EFI_STATUS Status; EFI_TIME RtcTime; RTC_REGISTER_B RegisterB; - UINT8 Century; UINT32 TimerVar; if (Time == NULL) { @@ -374,14 +483,55 @@ PcRtcSetTime ( } return Status; } + + // + // Write timezone and daylight to RTC variable + // + if ((Time->TimeZone == EFI_UNSPECIFIED_TIMEZONE) && (Time->Daylight == 0)) { + Status = EfiSetVariable ( + mTimeZoneVariableName, + &gEfiCallerIdGuid, + 0, + 0, + NULL + ); + if (Status == EFI_NOT_FOUND) { + Status = EFI_SUCCESS; + } + } else { + TimerVar = Time->Daylight; + TimerVar = (UINT32) ((TimerVar << 16) | (UINT16)(Time->TimeZone)); + Status = EfiSetVariable ( + mTimeZoneVariableName, + &gEfiCallerIdGuid, + EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE, + sizeof (TimerVar), + &TimerVar + ); + } + + if (EFI_ERROR (Status)) { + if (!EfiAtRuntime ()) { + EfiReleaseLock (&Global->RtcLock); + } + return EFI_DEVICE_ERROR; + } + // // Read Register B, and inhibit updates of the RTC // RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B); - RegisterB.Bits.SET = 1; + RegisterB.Bits.Set = 1; RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data); - ConvertEfiTimeToRtcTime (&RtcTime, RegisterB, &Century); + // + // Store the century value to RTC before converting to BCD format. + // + if (Global->CenturyRtcAddress != 0) { + RtcWrite (Global->CenturyRtcAddress, DecimalToBcd8 ((UINT8) (RtcTime.Year / 100))); + } + + ConvertEfiTimeToRtcTime (&RtcTime, RegisterB); RtcWrite (RTC_ADDRESS_SECONDS, RtcTime.Second); RtcWrite (RTC_ADDRESS_MINUTES, RtcTime.Minute); @@ -389,12 +539,11 @@ PcRtcSetTime ( RtcWrite (RTC_ADDRESS_DAY_OF_THE_MONTH, RtcTime.Day); RtcWrite (RTC_ADDRESS_MONTH, RtcTime.Month); RtcWrite (RTC_ADDRESS_YEAR, (UINT8) RtcTime.Year); - RtcWrite (RTC_ADDRESS_CENTURY, Century); // // Allow updates of the RTC registers // - RegisterB.Bits.SET = 0; + RegisterB.Bits.Set = 0; RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data); // @@ -409,17 +558,6 @@ PcRtcSetTime ( Global->SavedTimeZone = Time->TimeZone; Global->Daylight = Time->Daylight; - 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; } @@ -450,7 +588,8 @@ PcRtcGetWakeupTime ( EFI_STATUS Status; RTC_REGISTER_B RegisterB; RTC_REGISTER_C RegisterC; - UINT8 Century; + EFI_TIME RtcTime; + UINTN DataSize; // // Check parameters for null pointers @@ -484,25 +623,38 @@ PcRtcGetWakeupTime ( // // Get the Time/Date/Daylight Savings values. // - *Enabled = RegisterB.Bits.AIE; - if (*Enabled) { - Time->Second = RtcRead (RTC_ADDRESS_SECONDS_ALARM); - Time->Minute = RtcRead (RTC_ADDRESS_MINUTES_ALARM); - Time->Hour = RtcRead (RTC_ADDRESS_HOURS_ALARM); - Time->Day = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH); - Time->Month = RtcRead (RTC_ADDRESS_MONTH); - Time->Year = RtcRead (RTC_ADDRESS_YEAR); - } else { - Time->Second = 0; - Time->Minute = 0; - Time->Hour = 0; - Time->Day = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH); - Time->Month = RtcRead (RTC_ADDRESS_MONTH); - Time->Year = RtcRead (RTC_ADDRESS_YEAR); + *Enabled = RegisterB.Bits.Aie; + *Pending = RegisterC.Bits.Af; + + Time->Second = RtcRead (RTC_ADDRESS_SECONDS_ALARM); + Time->Minute = RtcRead (RTC_ADDRESS_MINUTES_ALARM); + Time->Hour = RtcRead (RTC_ADDRESS_HOURS_ALARM); + Time->Day = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH); + Time->Month = RtcRead (RTC_ADDRESS_MONTH); + Time->Year = RtcRead (RTC_ADDRESS_YEAR); + Time->TimeZone = Global->SavedTimeZone; + Time->Daylight = Global->Daylight; + + // + // Get the alarm info from variable + // + DataSize = sizeof (EFI_TIME); + Status = EfiGetVariable ( + L"RTCALARM", + &gEfiCallerIdGuid, + NULL, + &DataSize, + &RtcTime + ); + if (!EFI_ERROR (Status)) { + // + // The alarm variable exists. In this case, we read variable to get info. + // + Time->Day = RtcTime.Day; + Time->Month = RtcTime.Month; + Time->Year = RtcTime.Year; } - Century = RtcRead (RTC_ADDRESS_CENTURY); - // // Release RTC Lock. // @@ -510,16 +662,10 @@ PcRtcGetWakeupTime ( EfiReleaseLock (&Global->RtcLock); } - // - // Get the variable that contains the TimeZone and Daylight fields - // - Time->TimeZone = Global->SavedTimeZone; - Time->Daylight = Global->Daylight; - // // Make sure all field values are in correct range // - Status = ConvertRtcTimeToEfiTime (Time, Century, RegisterB); + Status = ConvertRtcTimeToEfiTime (Time, RegisterB); if (!EFI_ERROR (Status)) { Status = RtcTimeFieldsValid (Time); } @@ -527,8 +673,6 @@ PcRtcGetWakeupTime ( return EFI_DEVICE_ERROR; } - *Pending = RegisterC.Bits.AF; - return EFI_SUCCESS; } @@ -557,9 +701,10 @@ PcRtcSetWakeupTime ( EFI_STATUS Status; EFI_TIME RtcTime; RTC_REGISTER_B RegisterB; - UINT8 Century; EFI_TIME_CAPABILITIES Capabilities; + ZeroMem (&RtcTime, sizeof (RtcTime)); + if (Enable) { if (Time == NULL) { @@ -606,16 +751,50 @@ PcRtcSetWakeupTime ( return EFI_DEVICE_ERROR; } // - // Read Register B, and inhibit updates of the RTC + // Read Register B // - RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B); + RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B); + + if (Enable) { + ConvertEfiTimeToRtcTime (&RtcTime, RegisterB); + } else { + // + // if the alarm is disable, record the current setting. + // + RtcTime.Second = RtcRead (RTC_ADDRESS_SECONDS_ALARM); + RtcTime.Minute = RtcRead (RTC_ADDRESS_MINUTES_ALARM); + RtcTime.Hour = RtcRead (RTC_ADDRESS_HOURS_ALARM); + RtcTime.Day = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH); + RtcTime.Month = RtcRead (RTC_ADDRESS_MONTH); + RtcTime.Year = RtcRead (RTC_ADDRESS_YEAR); + RtcTime.TimeZone = Global->SavedTimeZone; + RtcTime.Daylight = Global->Daylight; + } - RegisterB.Bits.SET = 1; + // + // Set the Y/M/D info to variable as it has no corresponding hw registers. + // + Status = EfiSetVariable ( + L"RTCALARM", + &gEfiCallerIdGuid, + EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE, + sizeof (RtcTime), + &RtcTime + ); + if (EFI_ERROR (Status)) { + if (!EfiAtRuntime ()) { + EfiReleaseLock (&Global->RtcLock); + } + return EFI_DEVICE_ERROR; + } + + // + // Inhibit updates of the RTC + // + RegisterB.Bits.Set = 1; RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data); if (Enable) { - ConvertEfiTimeToRtcTime (&RtcTime, RegisterB, &Century); - // // Set RTC alarm time // @@ -623,15 +802,15 @@ PcRtcSetWakeupTime ( RtcWrite (RTC_ADDRESS_MINUTES_ALARM, RtcTime.Minute); RtcWrite (RTC_ADDRESS_HOURS_ALARM, RtcTime.Hour); - RegisterB.Bits.AIE = 1; + RegisterB.Bits.Aie = 1; } else { - RegisterB.Bits.AIE = 0; + RegisterB.Bits.Aie = 0; } // // Allow updates of the RTC registers // - RegisterB.Bits.SET = 0; + RegisterB.Bits.Set = 0; RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data); // @@ -678,7 +857,6 @@ CheckAndConvertBcd8ToDecimal8 ( @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. @@ -689,11 +867,11 @@ CheckAndConvertBcd8ToDecimal8 ( EFI_STATUS ConvertRtcTimeToEfiTime ( IN OUT EFI_TIME *Time, - IN UINT8 Century, IN RTC_REGISTER_B RegisterB ) { BOOLEAN IsPM; + UINT8 Century; if ((Time->Hour & 0x80) != 0) { IsPM = TRUE; @@ -703,7 +881,7 @@ ConvertRtcTimeToEfiTime ( Time->Hour = (UINT8) (Time->Hour & 0x7f); - if (RegisterB.Bits.DM == 0) { + if (RegisterB.Bits.Dm == 0) { Time->Year = CheckAndConvertBcd8ToDecimal8 ((UINT8) Time->Year); Time->Month = CheckAndConvertBcd8ToDecimal8 (Time->Month); Time->Day = CheckAndConvertBcd8ToDecimal8 (Time->Day); @@ -711,20 +889,27 @@ ConvertRtcTimeToEfiTime ( 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) { + Time->Hour == 0xff || Time->Minute == 0xff || Time->Second == 0xff) { return EFI_INVALID_PARAMETER; } + // + // For minimal/maximum year range [1970, 2069], + // Century is 19 if RTC year >= 70, + // Century is 20 otherwise. + // + Century = (UINT8) (PcdGet16 (PcdMinimalValidYear) / 100); + if (Time->Year < PcdGet16 (PcdMinimalValidYear) % 100) { + Century++; + } 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 (RegisterB.Bits.Mil == 0) { if (IsPM && Time->Hour < 12) { Time->Hour = (UINT8) (Time->Hour + 12); } @@ -760,7 +945,7 @@ RtcWaitToUpdate ( // RegisterD.Data = RtcRead (RTC_ADDRESS_REGISTER_D); - if (RegisterD.Bits.VRT == 0) { + if (RegisterD.Bits.Vrt == 0) { return EFI_DEVICE_ERROR; } // @@ -768,14 +953,14 @@ RtcWaitToUpdate ( // Timeout = (Timeout / 10) + 1; RegisterA.Data = RtcRead (RTC_ADDRESS_REGISTER_A); - while (RegisterA.Bits.UIP == 1 && Timeout > 0) { + while (RegisterA.Bits.Uip == 1 && Timeout > 0) { MicroSecondDelay (10); RegisterA.Data = RtcRead (RTC_ADDRESS_REGISTER_A); Timeout--; } RegisterD.Data = RtcRead (RTC_ADDRESS_REGISTER_D); - if (Timeout == 0 || RegisterD.Bits.VRT == 0) { + if (Timeout == 0 || RegisterD.Bits.Vrt == 0) { return EFI_DEVICE_ERROR; } @@ -796,8 +981,8 @@ RtcTimeFieldsValid ( IN EFI_TIME *Time ) { - if (Time->Year < 1998 || - Time->Year > 2099 || + if (Time->Year < PcdGet16 (PcdMinimalValidYear) || + Time->Year > PcdGet16 (PcdMaximalValidYear) || Time->Month < 1 || Time->Month > 12 || (!DayValid (Time)) || @@ -826,28 +1011,13 @@ DayValid ( IN EFI_TIME *Time ) { - INTN DayOfMonth[12]; - - 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; - // // 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->Day > mDayOfMonth[Time->Month - 1] || (Time->Month == 2 && (!IsLeapYear (Time) && Time->Day > 28)) ) { return FALSE; @@ -894,14 +1064,11 @@ IsLeapYear ( @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. - **/ VOID ConvertEfiTimeToRtcTime ( IN OUT EFI_TIME *Time, - IN RTC_REGISTER_B RegisterB, - OUT UINT8 *Century + IN RTC_REGISTER_B RegisterB ) { BOOLEAN IsPM; @@ -910,7 +1077,7 @@ ConvertEfiTimeToRtcTime ( // // Adjust hour field if RTC is in 12 hour mode // - if (RegisterB.Bits.MIL == 0) { + if (RegisterB.Bits.Mil == 0) { if (Time->Hour < 12) { IsPM = FALSE; } @@ -922,13 +1089,11 @@ ConvertEfiTimeToRtcTime ( } } // - // Set the Time/Date/Daylight Savings values. + // Set the Time/Date values. // - *Century = DecimalToBcd8 ((UINT8) (Time->Year / 100)); - Time->Year = (UINT16) (Time->Year % 100); - if (RegisterB.Bits.DM == 0) { + if (RegisterB.Bits.Dm == 0) { Time->Year = DecimalToBcd8 ((UINT8) Time->Year); Time->Month = DecimalToBcd8 (Time->Month); Time->Day = DecimalToBcd8 (Time->Day); @@ -939,7 +1104,7 @@ ConvertEfiTimeToRtcTime ( // // If we are in 12 hour mode and PM is set, then set bit 7 of the Hour field. // - if (RegisterB.Bits.MIL == 0 && IsPM) { + if (RegisterB.Bits.Mil == 0 && IsPM) { Time->Hour = (UINT8) (Time->Hour | 0x80); } } @@ -988,22 +1153,8 @@ IsWithinOneDay ( IN EFI_TIME *To ) { - UINT8 DayOfMonth[12]; BOOLEAN Adjacent; - 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; - Adjacent = FALSE; // @@ -1030,7 +1181,7 @@ IsWithinOneDay ( Adjacent = TRUE; } } - } else if (From->Day == DayOfMonth[From->Month - 1]) { + } else if (From->Day == mDayOfMonth[From->Month - 1]) { if ((CompareHMS(From, To) >= 0)) { Adjacent = TRUE; } @@ -1049,3 +1200,136 @@ IsWithinOneDay ( return Adjacent; } +/** + This function find ACPI table with the specified signature in RSDT or XSDT. + + @param Sdt ACPI RSDT or XSDT. + @param Signature ACPI table signature. + @param TablePointerSize Size of table pointer: 4 or 8. + + @return ACPI table or NULL if not found. +**/ +VOID * +ScanTableInSDT ( + IN EFI_ACPI_DESCRIPTION_HEADER *Sdt, + IN UINT32 Signature, + IN UINTN TablePointerSize + ) +{ + UINTN Index; + UINTN EntryCount; + UINTN EntryBase; + EFI_ACPI_DESCRIPTION_HEADER *Table; + + EntryCount = (Sdt->Length - sizeof (EFI_ACPI_DESCRIPTION_HEADER)) / TablePointerSize; + + EntryBase = (UINTN) (Sdt + 1); + for (Index = 0; Index < EntryCount; Index++) { + // + // When TablePointerSize is 4 while sizeof (VOID *) is 8, make sure the upper 4 bytes are zero. + // + Table = 0; + CopyMem (&Table, (VOID *) (EntryBase + Index * TablePointerSize), TablePointerSize); + + if (Table == NULL) { + continue; + } + + if (Table->Signature == Signature) { + return Table; + } + } + + return NULL; +} + +/** + Get the century RTC address from the ACPI FADT table. + + @return The century RTC address or 0 if not found. +**/ +UINT8 +GetCenturyRtcAddress ( + VOID + ) +{ + EFI_STATUS Status; + EFI_ACPI_2_0_ROOT_SYSTEM_DESCRIPTION_POINTER *Rsdp; + EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE *Fadt; + + Status = EfiGetSystemConfigurationTable (&gEfiAcpiTableGuid, (VOID **) &Rsdp); + if (EFI_ERROR (Status)) { + Status = EfiGetSystemConfigurationTable (&gEfiAcpi10TableGuid, (VOID **) &Rsdp); + } + + if (EFI_ERROR (Status) || (Rsdp == NULL)) { + return 0; + } + + Fadt = NULL; + + // + // Find FADT in XSDT + // + if (Rsdp->Revision >= EFI_ACPI_2_0_ROOT_SYSTEM_DESCRIPTION_POINTER_REVISION && Rsdp->XsdtAddress != 0) { + Fadt = ScanTableInSDT ( + (EFI_ACPI_DESCRIPTION_HEADER *) (UINTN) Rsdp->XsdtAddress, + EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE, + sizeof (UINTN) + ); + } + + // + // Find FADT in RSDT + // + if (Fadt == NULL && Rsdp->RsdtAddress != 0) { + Fadt = ScanTableInSDT ( + (EFI_ACPI_DESCRIPTION_HEADER *) (UINTN) Rsdp->RsdtAddress, + EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE, + sizeof (UINT32) + ); + } + + if ((Fadt != NULL) && + (Fadt->Century > RTC_ADDRESS_REGISTER_D) && (Fadt->Century < 0x80) + ) { + return Fadt->Century; + } else { + return 0; + } +} + +/** + Notification function of ACPI Table change. + + This is a notification function registered on ACPI Table change event. + It saves the Century address stored in ACPI FADT table. + + @param Event Event whose notification function is being invoked. + @param Context Pointer to the notification function's context. + +**/ +VOID +EFIAPI +PcRtcAcpiTableChangeCallback ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + EFI_STATUS Status; + EFI_TIME Time; + UINT8 CenturyRtcAddress; + UINT8 Century; + + CenturyRtcAddress = GetCenturyRtcAddress (); + if ((CenturyRtcAddress != 0) && (mModuleGlobal.CenturyRtcAddress != CenturyRtcAddress)) { + mModuleGlobal.CenturyRtcAddress = CenturyRtcAddress; + Status = PcRtcGetTime (&Time, NULL, &mModuleGlobal); + if (!EFI_ERROR (Status)) { + Century = (UINT8) (Time.Year / 100); + Century = DecimalToBcd8 (Century); + DEBUG ((EFI_D_INFO, "PcRtc: Write 0x%x to CMOS location 0x%x\n", Century, mModuleGlobal.CenturyRtcAddress)); + RtcWrite (mModuleGlobal.CenturyRtcAddress, Century); + } + } +}