]> git.proxmox.com Git - mirror_edk2.git/blobdiff - PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcRtc.c
BaseTools: Fixed an error reported during generating report.
[mirror_edk2.git] / PcAtChipsetPkg / PcatRealTimeClockRuntimeDxe / PcRtc.c
index d26e92360584ed3ecb4058eaa1b2e7e8c12e955c..23f8d3b56f591a5c8654317372f5b25d54dea266 100644 (file)
@@ -1,8 +1,8 @@
 /** @file\r
   RTC Architectural Protocol GUID as defined in DxeCis 0.96.\r
 \r
-Copyright (c) 2006 - 2007, Intel Corporation\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>\r
+This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
 http://opensource.org/licenses/bsd-license.php\r
@@ -104,11 +104,12 @@ PcRtcInit (
   EFI_TIME        Time;\r
   UINTN           DataSize;\r
   UINT32          TimerVar;\r
+  BOOLEAN         Enabled;\r
+  BOOLEAN         Pending;\r
 \r
   //\r
   // Acquire RTC Lock to make access to RTC atomic\r
   //\r
-  //Code here doesn't consider the runtime environment.\r
   if (!EfiAtRuntime ()) {\r
     EfiAcquireLock (&Global->RtcLock);\r
   }\r
@@ -142,7 +143,11 @@ PcRtcInit (
   //\r
   Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
   if (EFI_ERROR (Status)) {\r
-    //Code here doesn't consider the runtime environment.\r
+    //\r
+    // Set the variable with default value if the RTC is functioning incorrectly.\r
+    //\r
+    Global->SavedTimeZone = EFI_UNSPECIFIED_TIMEZONE;\r
+    Global->Daylight      = 0;\r
     if (!EfiAtRuntime ()) {\r
       EfiReleaseLock (&Global->RtcLock);\r
     }\r
@@ -169,11 +174,30 @@ PcRtcInit (
   //\r
   // Release RTC Lock.\r
   //\r
-  //Code here doesn't consider the runtime environment.\r
   if (!EfiAtRuntime ()) {\r
     EfiReleaseLock (&Global->RtcLock);\r
   }\r
  \r
+  //\r
+  // Get the data of Daylight saving and time zone, if they have been\r
+  // stored in NV variable during previous boot.\r
+  //\r
+  DataSize = sizeof (UINT32);\r
+  Status = EfiGetVariable (\r
+             L"RTC",\r
+             &gEfiCallerIdGuid,\r
+             NULL,\r
+             &DataSize,\r
+             (VOID *) &TimerVar\r
+             );\r
+  if (!EFI_ERROR (Status)) {\r
+    Time.TimeZone = (INT16) TimerVar;\r
+    Time.Daylight = (UINT8) (TimerVar >> 16);\r
+  } else {\r
+    Time.TimeZone = EFI_UNSPECIFIED_TIMEZONE;\r
+    Time.Daylight = 0;  \r
+  }\r
+\r
   //\r
   // Validate time fields\r
   //\r
@@ -182,37 +206,117 @@ PcRtcInit (
     Status = RtcTimeFieldsValid (&Time);\r
   }\r
   if (EFI_ERROR (Status)) {\r
+    //\r
+    // Report Status Code to indicate that the RTC has bad date and time\r
+    //\r
+    REPORT_STATUS_CODE (\r
+      EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
+      (EFI_SOFTWARE_DXE_RT_DRIVER | EFI_SW_EC_BAD_DATE_TIME)\r
+      );\r
     Time.Second = RTC_INIT_SECOND;\r
     Time.Minute = RTC_INIT_MINUTE;\r
     Time.Hour   = RTC_INIT_HOUR;\r
     Time.Day    = RTC_INIT_DAY;\r
     Time.Month  = RTC_INIT_MONTH;\r
     Time.Year   = RTC_INIT_YEAR;\r
+    Time.Nanosecond  = 0;\r
+    Time.TimeZone = EFI_UNSPECIFIED_TIMEZONE;\r
+    Time.Daylight = 0;\r
   }\r
+\r
   //\r
-  // Get the data of Daylight saving and time zone, if they have been\r
-  // stored in NV variable during previous boot.\r
+  // Reset time value according to new RTC configuration\r
   //\r
-  DataSize = sizeof (UINT32);\r
-  Status = EfiGetVariable (\r
-             L"RTC",\r
-             &gEfiCallerIdGuid,\r
-             NULL,\r
-             &DataSize,\r
-             (VOID *) &TimerVar\r
-             );\r
-  if (!EFI_ERROR (Status)) {\r
-    Global->SavedTimeZone = (INT16) TimerVar;\r
-    Global->Daylight      = (UINT8) (TimerVar >> 16);\r
+  Status = PcRtcSetTime (&Time, Global);\r
+  if (EFI_ERROR (Status)) {\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+  \r
+  //\r
+  // Reset wakeup time value to valid state when wakeup alarm is disabled and wakeup time is invalid.\r
+  // Global variable has already had valid SavedTimeZone and Daylight,\r
+  // so we can use them to get and set wakeup time.\r
+  //\r
+  Status = PcRtcGetWakeupTime (&Enabled, &Pending, &Time, Global);\r
+  if ((Enabled) || (!EFI_ERROR (Status))) {\r
+    return EFI_SUCCESS;\r
+  }\r
+  \r
+  //\r
+  // When wakeup time is disabled and invalid, reset wakeup time register to valid state \r
+  // but keep wakeup alarm disabled.\r
+  //\r
+  Time.Second = RTC_INIT_SECOND;\r
+  Time.Minute = RTC_INIT_MINUTE;\r
+  Time.Hour   = RTC_INIT_HOUR;\r
+  Time.Day    = RTC_INIT_DAY;\r
+  Time.Month  = RTC_INIT_MONTH;\r
+  Time.Year   = RTC_INIT_YEAR;\r
+  Time.Nanosecond  = 0;\r
+  Time.TimeZone = Global->SavedTimeZone;\r
+  Time.Daylight = Global->Daylight;;\r
 \r
-    Time.TimeZone = Global->SavedTimeZone;\r
-    Time.Daylight = Global->Daylight;\r
+  //\r
+  // Acquire RTC Lock to make access to RTC atomic\r
+  //\r
+  if (!EfiAtRuntime ()) {\r
+    EfiAcquireLock (&Global->RtcLock);\r
   }\r
   //\r
-  // Reset time value according to new RTC configuration\r
+  // Wait for up to 0.1 seconds for the RTC to be updated\r
+  //\r
+  Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
+  if (EFI_ERROR (Status)) {\r
+    if (!EfiAtRuntime ()) {\r
+    EfiReleaseLock (&Global->RtcLock);\r
+    }\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+  \r
+  ConvertEfiTimeToRtcTime (&Time, RegisterB, &Century);\r
+\r
+  //\r
+  // Set the Y/M/D info to variable as it has no corresponding hw registers.\r
+  //\r
+  Status =  EfiSetVariable (\r
+              L"RTCALARM",\r
+              &gEfiCallerIdGuid,\r
+              EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
+              sizeof (Time),\r
+              &Time\r
+              );\r
+  if (EFI_ERROR (Status)) {\r
+    if (!EfiAtRuntime ()) {\r
+      EfiReleaseLock (&Global->RtcLock);\r
+    }\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+  \r
   //\r
-  PcRtcSetTime (&Time, Global);\r
+  // Inhibit updates of the RTC\r
+  //\r
+  RegisterB.Bits.Set  = 1;\r
+  RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
\r
+  //\r
+  // Set RTC alarm time registers\r
+  //\r
+  RtcWrite (RTC_ADDRESS_SECONDS_ALARM, Time.Second);\r
+  RtcWrite (RTC_ADDRESS_MINUTES_ALARM, Time.Minute);\r
+  RtcWrite (RTC_ADDRESS_HOURS_ALARM, Time.Hour);\r
 \r
+  //\r
+  // Allow updates of the RTC registers\r
+  //\r
+  RegisterB.Bits.Set = 0;\r
+  RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
\r
+  //\r
+  // Release RTC Lock.\r
+  //\r
+  if (!EfiAtRuntime ()) {\r
+    EfiReleaseLock (&Global->RtcLock);\r
+  }\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -251,7 +355,6 @@ PcRtcGetTime (
   //\r
   // Acquire RTC Lock to make access to RTC atomic\r
   //\r
-  //Code here doesn't consider the runtime environment.\r
   if (!EfiAtRuntime ()) {\r
     EfiAcquireLock (&Global->RtcLock);\r
   }\r
@@ -260,7 +363,6 @@ PcRtcGetTime (
   //\r
   Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
   if (EFI_ERROR (Status)) {\r
-      //Code here doesn't consider the runtime environment.\r
       if (!EfiAtRuntime ()) {\r
         EfiReleaseLock (&Global->RtcLock);\r
       }\r
@@ -286,10 +388,10 @@ PcRtcGetTime (
   //\r
   // Release RTC Lock.\r
   //\r
-  //Code here doesn't consider the runtime environment.\r
   if (!EfiAtRuntime ()) {\r
     EfiReleaseLock (&Global->RtcLock);\r
   }\r
+\r
   //\r
   // Get the variable that contains the TimeZone and Daylight fields\r
   //\r
@@ -306,6 +408,7 @@ PcRtcGetTime (
   if (EFI_ERROR (Status)) {\r
     return EFI_DEVICE_ERROR;\r
   }\r
+\r
   //\r
   //  Fill in Capabilities if it was passed in\r
   //\r
@@ -363,7 +466,6 @@ PcRtcSetTime (
   //\r
   // Acquire RTC Lock to make access to RTC atomic\r
   //\r
-  //Code here doesn't consider the runtime environment.\r
   if (!EfiAtRuntime ()) {\r
     EfiAcquireLock (&Global->RtcLock);\r
   }\r
@@ -372,17 +474,36 @@ PcRtcSetTime (
   //\r
   Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
   if (EFI_ERROR (Status)) {\r
-     //Code here doesn't consider the runtime environment.\r
      if (!EfiAtRuntime ()) {\r
        EfiReleaseLock (&Global->RtcLock);\r
      }\r
     return Status;\r
   }\r
+  \r
+  //\r
+  // Write timezone and daylight to RTC variable\r
+  //\r
+  TimerVar = Time->Daylight;\r
+  TimerVar = (UINT32) ((TimerVar << 16) | (UINT16)(Time->TimeZone));\r
+  Status =  EfiSetVariable (\r
+              L"RTC",\r
+              &gEfiCallerIdGuid,\r
+              EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
+              sizeof (TimerVar),\r
+              &TimerVar\r
+              );\r
+  if (EFI_ERROR (Status)) {\r
+    if (!EfiAtRuntime ()) {\r
+      EfiReleaseLock (&Global->RtcLock);\r
+    }\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
   //\r
   // Read Register B, and inhibit updates of the RTC\r
   //\r
   RegisterB.Data      = RtcRead (RTC_ADDRESS_REGISTER_B);\r
-  RegisterB.Bits.SET  = 1;\r
+  RegisterB.Bits.Set  = 1;\r
   RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
 \r
   ConvertEfiTimeToRtcTime (&RtcTime, RegisterB, &Century);\r
@@ -398,13 +519,12 @@ PcRtcSetTime (
   //\r
   // Allow updates of the RTC registers\r
   //\r
-  RegisterB.Bits.SET = 0;\r
+  RegisterB.Bits.Set = 0;\r
   RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
 \r
   //\r
   // Release RTC Lock.\r
   //\r
-  //Code here doesn't consider the runtime environment.\r
   if (!EfiAtRuntime ()) {\r
     EfiReleaseLock (&Global->RtcLock);\r
   }\r
@@ -414,17 +534,6 @@ PcRtcSetTime (
   Global->SavedTimeZone = Time->TimeZone;\r
   Global->Daylight      = Time->Daylight;\r
 \r
-  TimerVar = Time->Daylight;\r
-  TimerVar = (UINT32) ((TimerVar << 16) | Time->TimeZone);\r
-  Status =  EfiSetVariable (\r
-              L"RTC",\r
-              &gEfiCallerIdGuid,\r
-              EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
-              sizeof (TimerVar),\r
-              &TimerVar\r
-              );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -456,6 +565,8 @@ PcRtcGetWakeupTime (
   RTC_REGISTER_B  RegisterB;\r
   RTC_REGISTER_C  RegisterC;\r
   UINT8           Century;\r
+  EFI_TIME        RtcTime;\r
+  UINTN           DataSize;\r
 \r
   //\r
   // Check parameters for null pointers\r
@@ -467,7 +578,6 @@ PcRtcGetWakeupTime (
   //\r
   // Acquire RTC Lock to make access to RTC atomic\r
   //\r
-  //Code here doesn't consider the runtime environment.\r
   if (!EfiAtRuntime ()) {\r
     EfiAcquireLock (&Global->RtcLock);\r
   }\r
@@ -476,7 +586,6 @@ PcRtcGetWakeupTime (
   //\r
   Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
   if (EFI_ERROR (Status)) {\r
-    //Code here doesn't consider the runtime environment.\r
     if (!EfiAtRuntime ()) {\r
     EfiReleaseLock (&Global->RtcLock);\r
     }\r
@@ -491,32 +600,47 @@ PcRtcGetWakeupTime (
   //\r
   // Get the Time/Date/Daylight Savings values.\r
   //\r
-  *Enabled = RegisterB.Bits.AIE;\r
-  if (*Enabled) {\r
-    Time->Second  = RtcRead (RTC_ADDRESS_SECONDS_ALARM);\r
-    Time->Minute  = RtcRead (RTC_ADDRESS_MINUTES_ALARM);\r
-    Time->Hour    = RtcRead (RTC_ADDRESS_HOURS_ALARM);\r
-    Time->Day     = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH);\r
-    Time->Month   = RtcRead (RTC_ADDRESS_MONTH);\r
-    Time->Year    = RtcRead (RTC_ADDRESS_YEAR);\r
-  } else {\r
-    Time->Second  = 0;\r
-    Time->Minute  = 0;\r
-    Time->Hour    = 0;\r
-    Time->Day     = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH);\r
-    Time->Month   = RtcRead (RTC_ADDRESS_MONTH);\r
-    Time->Year    = RtcRead (RTC_ADDRESS_YEAR);\r
-  }\r
+  *Enabled = RegisterB.Bits.Aie;\r
+  *Pending = RegisterC.Bits.Af;\r
+\r
+  Time->Second = RtcRead (RTC_ADDRESS_SECONDS_ALARM);\r
+  Time->Minute = RtcRead (RTC_ADDRESS_MINUTES_ALARM);\r
+  Time->Hour   = RtcRead (RTC_ADDRESS_HOURS_ALARM);\r
+  Time->Day    = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH);\r
+  Time->Month  = RtcRead (RTC_ADDRESS_MONTH);\r
+  Time->Year   = RtcRead (RTC_ADDRESS_YEAR);\r
+  Time->TimeZone = Global->SavedTimeZone;\r
+  Time->Daylight = Global->Daylight;\r
 \r
   Century = RtcRead (RTC_ADDRESS_CENTURY);\r
-  \r
+\r
+  //\r
+  // Get the alarm info from variable\r
+  //\r
+  DataSize = sizeof (EFI_TIME);\r
+  Status = EfiGetVariable (\r
+              L"RTCALARM",\r
+              &gEfiCallerIdGuid,\r
+              NULL,\r
+              &DataSize,\r
+              &RtcTime\r
+              );\r
+  if (!EFI_ERROR (Status)) {\r
+    //\r
+    // The alarm variable exists. In this case, we read variable to get info.\r
+    //\r
+    Time->Day   = RtcTime.Day;\r
+    Time->Month = RtcTime.Month;\r
+    Time->Year  = RtcTime.Year;\r
+  }\r
+\r
   //\r
   // Release RTC Lock.\r
   //\r
-  //Code here doesn't consider the runtime environment.\r
   if (!EfiAtRuntime ()) {\r
     EfiReleaseLock (&Global->RtcLock);\r
   }\r
+\r
   //\r
   // Make sure all field values are in correct range\r
   //\r
@@ -528,8 +652,6 @@ PcRtcGetWakeupTime (
     return EFI_DEVICE_ERROR;\r
   }\r
 \r
-  *Pending = RegisterC.Bits.AF;\r
-\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -561,6 +683,8 @@ PcRtcSetWakeupTime (
   UINT8                 Century;\r
   EFI_TIME_CAPABILITIES Capabilities;\r
 \r
+  ZeroMem (&RtcTime, sizeof (RtcTime));\r
+\r
   if (Enable) {\r
 \r
     if (Time == NULL) {\r
@@ -593,7 +717,6 @@ PcRtcSetWakeupTime (
   //\r
   // Acquire RTC Lock to make access to RTC atomic\r
   //\r
-  //Code here doesn't consider the runtime environment.\r
   if (!EfiAtRuntime ()) {\r
     EfiAcquireLock (&Global->RtcLock);\r
   }\r
@@ -602,23 +725,56 @@ PcRtcSetWakeupTime (
   //\r
   Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
   if (EFI_ERROR (Status)) {\r
-    //Code here doesn't consider the runtime environment.\r
     if (!EfiAtRuntime ()) {\r
     EfiReleaseLock (&Global->RtcLock);\r
     }\r
     return EFI_DEVICE_ERROR;\r
   }\r
   //\r
-  // Read Register B, and inhibit updates of the RTC\r
+  // Read Register B\r
   //\r
-  RegisterB.Data      = RtcRead (RTC_ADDRESS_REGISTER_B);\r
-\r
-  RegisterB.Bits.SET  = 1;\r
-  RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
+  RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B);\r
 \r
   if (Enable) {\r
     ConvertEfiTimeToRtcTime (&RtcTime, RegisterB, &Century);\r
+  } else {\r
+    //\r
+    // if the alarm is disable, record the current setting.\r
+    //\r
+    RtcTime.Second  = RtcRead (RTC_ADDRESS_SECONDS_ALARM);\r
+    RtcTime.Minute  = RtcRead (RTC_ADDRESS_MINUTES_ALARM);\r
+    RtcTime.Hour    = RtcRead (RTC_ADDRESS_HOURS_ALARM);\r
+    RtcTime.Day     = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH);\r
+    RtcTime.Month   = RtcRead (RTC_ADDRESS_MONTH);\r
+    RtcTime.Year    = RtcRead (RTC_ADDRESS_YEAR);\r
+    RtcTime.TimeZone = Global->SavedTimeZone;\r
+    RtcTime.Daylight = Global->Daylight;\r
+  }\r
+\r
+  //\r
+  // Set the Y/M/D info to variable as it has no corresponding hw registers.\r
+  //\r
+  Status =  EfiSetVariable (\r
+              L"RTCALARM",\r
+              &gEfiCallerIdGuid,\r
+              EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
+              sizeof (RtcTime),\r
+              &RtcTime\r
+              );\r
+  if (EFI_ERROR (Status)) {\r
+    if (!EfiAtRuntime ()) {\r
+      EfiReleaseLock (&Global->RtcLock);\r
+    }\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+  \r
+  //\r
+  // Inhibit updates of the RTC\r
+  //\r
+  RegisterB.Bits.Set  = 1;\r
+  RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
 \r
+  if (Enable) {\r
     //\r
     // Set RTC alarm time\r
     //\r
@@ -626,21 +782,20 @@ PcRtcSetWakeupTime (
     RtcWrite (RTC_ADDRESS_MINUTES_ALARM, RtcTime.Minute);\r
     RtcWrite (RTC_ADDRESS_HOURS_ALARM, RtcTime.Hour);\r
 \r
-    RegisterB.Bits.AIE = 1;\r
+    RegisterB.Bits.Aie = 1;\r
 \r
   } else {\r
-    RegisterB.Bits.AIE = 0;\r
+    RegisterB.Bits.Aie = 0;\r
   }\r
   //\r
   // Allow updates of the RTC registers\r
   //\r
-  RegisterB.Bits.SET = 0;\r
+  RegisterB.Bits.Set = 0;\r
   RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
 \r
   //\r
   // Release RTC Lock.\r
   //\r
-  //Code here doesn't consider the runtime environment.\r
   if (!EfiAtRuntime ()) {\r
     EfiReleaseLock (&Global->RtcLock);\r
   }\r
@@ -707,7 +862,7 @@ ConvertRtcTimeToEfiTime (
 \r
   Time->Hour = (UINT8) (Time->Hour & 0x7f);\r
 \r
-  if (RegisterB.Bits.DM == 0) {\r
+  if (RegisterB.Bits.Dm == 0) {\r
     Time->Year    = CheckAndConvertBcd8ToDecimal8 ((UINT8) Time->Year);\r
     Time->Month   = CheckAndConvertBcd8ToDecimal8 (Time->Month);\r
     Time->Day     = CheckAndConvertBcd8ToDecimal8 (Time->Day);\r
@@ -728,7 +883,7 @@ ConvertRtcTimeToEfiTime (
   //\r
   // If time is in 12 hour format, convert it to 24 hour format\r
   //\r
-  if (RegisterB.Bits.MIL == 0) {\r
+  if (RegisterB.Bits.Mil == 0) {\r
     if (IsPM && Time->Hour < 12) {\r
       Time->Hour = (UINT8) (Time->Hour + 12);\r
     }\r
@@ -739,8 +894,6 @@ ConvertRtcTimeToEfiTime (
   }\r
 \r
   Time->Nanosecond  = 0;\r
-  Time->TimeZone    = EFI_UNSPECIFIED_TIMEZONE;\r
-  Time->Daylight    = 0;\r
 \r
   return EFI_SUCCESS;\r
 }\r
@@ -766,7 +919,7 @@ RtcWaitToUpdate (
   //\r
   RegisterD.Data = RtcRead (RTC_ADDRESS_REGISTER_D);\r
 \r
-  if (RegisterD.Bits.VRT == 0) {\r
+  if (RegisterD.Bits.Vrt == 0) {\r
     return EFI_DEVICE_ERROR;\r
   }\r
   //\r
@@ -774,14 +927,14 @@ RtcWaitToUpdate (
   //\r
   Timeout         = (Timeout / 10) + 1;\r
   RegisterA.Data  = RtcRead (RTC_ADDRESS_REGISTER_A);\r
-  while (RegisterA.Bits.UIP == 1 && Timeout > 0) {\r
+  while (RegisterA.Bits.Uip == 1 && Timeout > 0) {\r
     MicroSecondDelay (10);\r
     RegisterA.Data = RtcRead (RTC_ADDRESS_REGISTER_A);\r
     Timeout--;\r
   }\r
 \r
   RegisterD.Data = RtcRead (RTC_ADDRESS_REGISTER_D);\r
-  if (Timeout == 0 || RegisterD.Bits.VRT == 0) {\r
+  if (Timeout == 0 || RegisterD.Bits.Vrt == 0) {\r
     return EFI_DEVICE_ERROR;\r
   }\r
 \r
@@ -802,20 +955,18 @@ RtcTimeFieldsValid (
   IN EFI_TIME *Time\r
   )\r
 {\r
-  if (Time->Year < 1998 ||\r
-      Time->Year > 2099 ||\r
+  if (Time->Year < PcdGet16 (PcdMinimalValidYear) ||\r
+      Time->Year > PcdGet16 (PcdMaximalValidYear) ||\r
       Time->Month < 1 ||\r
       Time->Month > 12 ||\r
+      (!DayValid (Time)) ||\r
       Time->Hour > 23 ||\r
       Time->Minute > 59 ||\r
       Time->Second > 59 ||\r
       Time->Nanosecond > 999999999 ||\r
       (!(Time->TimeZone == EFI_UNSPECIFIED_TIMEZONE || (Time->TimeZone >= -1440 && Time->TimeZone <= 1440))) ||\r
-      ((Time->Daylight & (~(EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT))) != 0)\r
-      ) {\r
-    if (!DayValid (Time)) {\r
-      return EFI_INVALID_PARAMETER;\r
-    }\r
+      ((Time->Daylight & (~(EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT))) != 0)) {\r
+    return EFI_INVALID_PARAMETER;\r
   }\r
 \r
   return EFI_SUCCESS;\r
@@ -918,7 +1069,7 @@ ConvertEfiTimeToRtcTime (
   //\r
   // Adjust hour field if RTC is in 12 hour mode\r
   //\r
-  if (RegisterB.Bits.MIL == 0) {\r
+  if (RegisterB.Bits.Mil == 0) {\r
     if (Time->Hour < 12) {\r
       IsPM = FALSE;\r
     }\r
@@ -936,7 +1087,7 @@ ConvertEfiTimeToRtcTime (
 \r
   Time->Year  = (UINT16) (Time->Year % 100);\r
 \r
-  if (RegisterB.Bits.DM == 0) {\r
+  if (RegisterB.Bits.Dm == 0) {\r
     Time->Year    = DecimalToBcd8 ((UINT8) Time->Year);\r
     Time->Month   = DecimalToBcd8 (Time->Month);\r
     Time->Day     = DecimalToBcd8 (Time->Day);\r
@@ -947,7 +1098,7 @@ ConvertEfiTimeToRtcTime (
   //\r
   // If we are in 12 hour mode and PM is set, then set bit 7 of the Hour field.\r
   //\r
-  if (RegisterB.Bits.MIL == 0 && IsPM) {\r
+  if (RegisterB.Bits.Mil == 0 && IsPM) {\r
     Time->Hour = (UINT8) (Time->Hour | 0x80);\r
   }\r
 }\r