X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=PcAtChipsetPkg%2FPcatRealTimeClockRuntimeDxe%2FPcRtc.c;h=5143575e311f9e4d11d222e83e8c45f96afee4cf;hp=23f8d3b56f591a5c8654317372f5b25d54dea266;hb=6f6bf5c77214ae2449bffbe36c33faeca4663e01;hpb=1e5fff631b92436790c27d8f3995710aa15c9232 diff --git a/PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcRtc.c b/PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcRtc.c index 23f8d3b56f..5143575e31 100644 --- a/PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcRtc.c +++ b/PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcRtc.c @@ -1,7 +1,7 @@ /** @file RTC Architectural Protocol GUID as defined in DxeCis 0.96. -Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.
+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 @@ -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,7 +110,6 @@ PcRtcInit ( RTC_REGISTER_A RegisterA; RTC_REGISTER_B RegisterB; RTC_REGISTER_D RegisterD; - UINT8 Century; EFI_TIME Time; UINTN DataSize; UINT32 TimerVar; @@ -163,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. @@ -184,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; @@ -201,7 +209,7 @@ PcRtcInit ( // // Validate time fields // - Status = ConvertRtcTimeToEfiTime (&Time, Century, RegisterB); + Status = ConvertRtcTimeToEfiTime (&Time, RegisterB); if (!EFI_ERROR (Status)) { Status = RtcTimeFieldsValid (&Time); } @@ -218,7 +226,7 @@ PcRtcInit ( 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; @@ -251,7 +259,7 @@ PcRtcInit ( 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 = Global->SavedTimeZone; Time.Daylight = Global->Daylight;; @@ -272,8 +280,8 @@ PcRtcInit ( } return EFI_DEVICE_ERROR; } - - ConvertEfiTimeToRtcTime (&Time, RegisterB, &Century); + + ConvertEfiTimeToRtcTime (&Time, RegisterB); // // Set the Y/M/D info to variable as it has no corresponding hw registers. @@ -343,7 +351,6 @@ PcRtcGetTime ( { EFI_STATUS Status; RTC_REGISTER_B RegisterB; - UINT8 Century; // // Check parameters for null pointer @@ -383,8 +390,6 @@ PcRtcGetTime ( Time->Month = RtcRead (RTC_ADDRESS_MONTH); Time->Year = RtcRead (RTC_ADDRESS_YEAR); - Century = RtcRead (RTC_ADDRESS_CENTURY); - // // Release RTC Lock. // @@ -401,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); } @@ -447,7 +452,6 @@ PcRtcSetTime ( EFI_STATUS Status; EFI_TIME RtcTime; RTC_REGISTER_B RegisterB; - UINT8 Century; UINT32 TimerVar; if (Time == NULL) { @@ -483,15 +487,29 @@ PcRtcSetTime ( // // Write timezone and daylight to RTC variable // - TimerVar = Time->Daylight; - TimerVar = (UINT32) ((TimerVar << 16) | (UINT16)(Time->TimeZone)); - Status = EfiSetVariable ( - L"RTC", - &gEfiCallerIdGuid, - EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE, - sizeof (TimerVar), - &TimerVar - ); + 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); @@ -506,7 +524,14 @@ PcRtcSetTime ( 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); @@ -514,7 +539,6 @@ 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 @@ -564,7 +588,6 @@ PcRtcGetWakeupTime ( EFI_STATUS Status; RTC_REGISTER_B RegisterB; RTC_REGISTER_C RegisterC; - UINT8 Century; EFI_TIME RtcTime; UINTN DataSize; @@ -612,8 +635,6 @@ PcRtcGetWakeupTime ( Time->TimeZone = Global->SavedTimeZone; Time->Daylight = Global->Daylight; - Century = RtcRead (RTC_ADDRESS_CENTURY); - // // Get the alarm info from variable // @@ -644,7 +665,7 @@ PcRtcGetWakeupTime ( // // 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); } @@ -680,7 +701,6 @@ PcRtcSetWakeupTime ( EFI_STATUS Status; EFI_TIME RtcTime; RTC_REGISTER_B RegisterB; - UINT8 Century; EFI_TIME_CAPABILITIES Capabilities; ZeroMem (&RtcTime, sizeof (RtcTime)); @@ -736,7 +756,7 @@ PcRtcSetWakeupTime ( RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B); if (Enable) { - ConvertEfiTimeToRtcTime (&RtcTime, RegisterB, &Century); + ConvertEfiTimeToRtcTime (&RtcTime, RegisterB); } else { // // if the alarm is disable, record the current setting. @@ -837,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. @@ -848,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; @@ -870,14 +889,21 @@ 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); // @@ -985,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; @@ -1053,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; @@ -1081,10 +1089,8 @@ 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) { @@ -1147,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; // @@ -1189,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; } @@ -1208,3 +1200,108 @@ 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->Signature == Signature) { + return Table; + } + } + + return NULL; +} + +/** + 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_ACPI_2_0_ROOT_SYSTEM_DESCRIPTION_POINTER *Rsdp; + EFI_ACPI_DESCRIPTION_HEADER *Rsdt; + EFI_ACPI_DESCRIPTION_HEADER *Xsdt; + EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE *Fadt; + EFI_TIME Time; + UINT8 Century; + + Status = EfiGetSystemConfigurationTable (&gEfiAcpiTableGuid, (VOID **) &Rsdp); + if (EFI_ERROR (Status)) { + Status = EfiGetSystemConfigurationTable (&gEfiAcpi10TableGuid, (VOID **) &Rsdp); + } + + if (EFI_ERROR (Status)) { + return; + } + + ASSERT (Rsdp != NULL); + + // + // Find FADT in XSDT + // + Fadt = NULL; + if (Rsdp->Revision >= EFI_ACPI_2_0_ROOT_SYSTEM_DESCRIPTION_POINTER_REVISION) { + Xsdt = (EFI_ACPI_DESCRIPTION_HEADER *) (UINTN) Rsdp->XsdtAddress; + Fadt = ScanTableInSDT (Xsdt, EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE, sizeof (UINT64)); + } + + if (Fadt == NULL) { + // + // Find FADT in RSDT + // + Rsdt = (EFI_ACPI_DESCRIPTION_HEADER *) (UINTN) Rsdp->RsdtAddress; + Fadt = ScanTableInSDT (Rsdt, EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE, sizeof (UINT32)); + } + + if ((Fadt != NULL) && + (Fadt->Century > RTC_ADDRESS_REGISTER_D) && (Fadt->Century < 0x80) && + (mModuleGlobal.CenturyRtcAddress != Fadt->Century) + ) { + mModuleGlobal.CenturyRtcAddress = Fadt->Century; + 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); + } + } +}