]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/PcatRealTimeClockRuntimeDxe/PcRtc.c
Fix date/time not work for the return value of RtcRead not need type convert.
[mirror_edk2.git] / MdeModulePkg / Universal / PcatRealTimeClockRuntimeDxe / PcRtc.c
index 7535cd538e18417dcf87987523fabd98496b5375..cf5ed0bf5a7d2870c64e551e59dec2b524508d97 100644 (file)
@@ -108,8 +108,7 @@ PcRtcInit (
   //\r
   // Acquire RTC Lock to make access to RTC atomic\r
   //\r
-  //BugBug: the EfiAtRuntime should be encapsulated in EfiAcquireLock or\r
-  //        provide a new instance for EfiAcquireLock, say, RtEfiAcquireLock\r
+  //Code here doesn't consider the runtime environment.\r
   if (!EfiAtRuntime ()) {\r
     EfiAcquireLock (&Global->RtcLock);\r
   }\r
@@ -143,8 +142,7 @@ PcRtcInit (
   //\r
   Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
   if (EFI_ERROR (Status)) {\r
-       //BugBug: the EfiAtRuntime should be encapsulated in EfiAcquireLock or\r
-    //        provide a new instance for EfiAcquireLock, say, RtEfiAcquireLock\r
+    //Code here doesn't consider the runtime environment.\r
     if (!EfiAtRuntime ()) {\r
       EfiReleaseLock (&Global->RtcLock);\r
     }\r
@@ -160,12 +158,9 @@ PcRtcInit (
   Time.Month  = RtcRead (RTC_ADDRESS_MONTH);\r
   Time.Year   = RtcRead (RTC_ADDRESS_YEAR);\r
 \r
-  if (RtcTestCenturyRegister () == EFI_SUCCESS) {\r
-    Century = (UINT8) (RtcRead (RTC_ADDRESS_CENTURY) & 0x7f);\r
-  } else {\r
-    Century = RtcRead (RTC_ADDRESS_CENTURY);\r
-  }\r
-\r
+  Century = RtcRead (RTC_ADDRESS_CENTURY);\r
+  Time.Year = (UINT16) (Century * 100 + Time.Year);\r
+  \r
   //\r
   // Set RTC configuration after get original time\r
   // The value of bit AIE should be reserved.\r
@@ -175,9 +170,7 @@ PcRtcInit (
   //\r
   // Release RTC Lock.\r
   //\r
-  //BugBug: the EfiAtRuntime should be encapsulated in EfiAcquireLock or\r
-  //        provide a new instance for EfiAcquireLock, say, RtEfiAcquireLock\r
-  //\r
+  //Code here doesn't consider the runtime environment.\r
   if (!EfiAtRuntime ()) {\r
     EfiReleaseLock (&Global->RtcLock);\r
   }\r
@@ -203,8 +196,8 @@ PcRtcInit (
   //\r
   DataSize = sizeof (UINT32);\r
   Status = EfiGetVariable (\r
-             L"TimerVar",\r
-             &gEfiGenericPlatformVariableGuid,\r
+             L"RTC",\r
+             &gEfiCallerIdGuid,\r
              NULL,\r
              &DataSize,\r
              (VOID *) &TimerVar\r
@@ -259,8 +252,7 @@ PcRtcGetTime (
   //\r
   // Acquire RTC Lock to make access to RTC atomic\r
   //\r
-  //BugBug: the EfiAtRuntime should be encapsulated in EfiAcquireLock or\r
-  //        provide a new instance for EfiAcquireLock, say, RtEfiAcquireLock\r
+  //Code here doesn't consider the runtime environment.\r
   if (!EfiAtRuntime ()) {\r
     EfiAcquireLock (&Global->RtcLock);\r
   }\r
@@ -269,8 +261,7 @@ PcRtcGetTime (
   //\r
   Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
   if (EFI_ERROR (Status)) {\r
-         //BugBug: the EfiAtRuntime should be encapsulated in EfiReleaseLock or\r
-      //        provide a new instance for EfiReleaseLock, say, RtEfiReleaseLock\r
+      //Code here doesn't consider the runtime environment.\r
       if (!EfiAtRuntime ()) {\r
         EfiReleaseLock (&Global->RtcLock);\r
       }\r
@@ -291,22 +282,18 @@ PcRtcGetTime (
   Time->Month   = RtcRead (RTC_ADDRESS_MONTH);\r
   Time->Year    = RtcRead (RTC_ADDRESS_YEAR);\r
 \r
-  if (RtcTestCenturyRegister () == EFI_SUCCESS) {\r
-    Century = (UINT8) (RtcRead (RTC_ADDRESS_CENTURY) & 0x7f);\r
-  } else {\r
-    Century = RtcRead (RTC_ADDRESS_CENTURY);\r
-  }\r
-\r
+  Century = RtcRead (RTC_ADDRESS_CENTURY);\r
+  Time->Year = (UINT16) (Century * 100 + Time->Year);\r
+  \r
   //\r
   // Release RTC Lock.\r
   //\r
-  //BugBug: the EfiAtRuntime should be encapsulated in EfiReleaseLock or\r
-  //        provide a new instance for EfiReleaseLock, say, RtEfiReleaseLock\r
+  //Code here doesn't consider the runtime environment.\r
   if (!EfiAtRuntime ()) {\r
     EfiReleaseLock (&Global->RtcLock);\r
   }\r
   //\r
-  // Get the variable that containts the TimeZone and Daylight fields\r
+  // Get the variable that contains the TimeZone and Daylight fields\r
   //\r
   Time->TimeZone  = Global->SavedTimeZone;\r
   Time->Daylight  = Global->Daylight;\r
@@ -378,8 +365,7 @@ PcRtcSetTime (
   //\r
   // Acquire RTC Lock to make access to RTC atomic\r
   //\r
-  //BugBug: the EfiAtRuntime should be encapsulated in EfiAcquireLock or\r
-  //        provide a new instance for EfiAcquireLock, say, RtEfiAcquireLock\r
+  //Code here doesn't consider the runtime environment.\r
   if (!EfiAtRuntime ()) {\r
     EfiAcquireLock (&Global->RtcLock);\r
   }\r
@@ -388,10 +374,9 @@ PcRtcSetTime (
   //\r
   Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
   if (EFI_ERROR (Status)) {\r
-        //BugBug: the EfiAtRuntime should be encapsulated in EfiReleaseLock or\r
-     //        provide a new instance for EfiReleaseLock, say, RtEfiReleaseLock\r
+     //Code here doesn't consider the runtime environment.\r
      if (!EfiAtRuntime ()) {\r
-    EfiReleaseLock (&Global->RtcLock);\r
+       EfiReleaseLock (&Global->RtcLock);\r
      }\r
     return Status;\r
   }\r
@@ -410,10 +395,6 @@ PcRtcSetTime (
   RtcWrite (RTC_ADDRESS_DAY_OF_THE_MONTH, RtcTime.Day);\r
   RtcWrite (RTC_ADDRESS_MONTH, RtcTime.Month);\r
   RtcWrite (RTC_ADDRESS_YEAR, (UINT8) RtcTime.Year);\r
-  if (RtcTestCenturyRegister () == EFI_SUCCESS) {\r
-    Century = (UINT8) ((Century & 0x7f) | (RtcRead (RTC_ADDRESS_CENTURY) & 0x80));\r
-  }\r
-\r
   RtcWrite (RTC_ADDRESS_CENTURY, Century);\r
 \r
   //\r
@@ -425,13 +406,12 @@ PcRtcSetTime (
   //\r
   // Release RTC Lock.\r
   //\r
-  //BugBug: the EfiAtRuntime should be encapsulated in EfiReleaseLock or\r
-  //        provide a new instance for EfiReleaseLock, say, RtEfiReleaseLock\r
+  //Code here doesn't consider the runtime environment.\r
   if (!EfiAtRuntime ()) {\r
     EfiReleaseLock (&Global->RtcLock);\r
   }\r
   //\r
-  // Set the variable that containts the TimeZone and Daylight fields\r
+  // Set the variable that contains the TimeZone and Daylight fields\r
   //\r
   Global->SavedTimeZone = Time->TimeZone;\r
   Global->Daylight      = Time->Daylight;\r
@@ -439,8 +419,8 @@ PcRtcSetTime (
   TimerVar = Time->Daylight;\r
   TimerVar = (UINT32) ((TimerVar << 16) | Time->TimeZone);\r
   Status =  EfiSetVariable (\r
-              L"TimerVar",\r
-              &gEfiGenericPlatformVariableGuid,\r
+              L"RTC",\r
+              &gEfiCallerIdGuid,\r
               EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
               sizeof (TimerVar),\r
               &TimerVar\r
@@ -454,7 +434,7 @@ PcRtcSetTime (
   Returns the current wakeup alarm clock setting.\r
 \r
   @param  Enabled  Indicates if the alarm is currently enabled or disabled.\r
-  @param  Pending  Indicates if the alarm signal is pending and requires acknowledgement.\r
+  @param  Pending  Indicates if the alarm signal is pending and requires acknowledgment.\r
   @param  Time     The current alarm setting.\r
   @param  Global   For global use inside this module.\r
 \r
@@ -480,7 +460,7 @@ PcRtcGetWakeupTime (
   UINT8           Century;\r
 \r
   //\r
-  // Check paramters for null pointers\r
+  // Check parameters for null pointers\r
   //\r
   if ((Enabled == NULL) || (Pending == NULL) || (Time == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -489,8 +469,7 @@ PcRtcGetWakeupTime (
   //\r
   // Acquire RTC Lock to make access to RTC atomic\r
   //\r
-  //BugBug: the EfiAtRuntime should be encapsulated in EfiAcquireLock or\r
-  //        provide a new instance for EfiAcquireLock, say, RtEfiAcquireLock\r
+  //Code here doesn't consider the runtime environment.\r
   if (!EfiAtRuntime ()) {\r
     EfiAcquireLock (&Global->RtcLock);\r
   }\r
@@ -499,8 +478,7 @@ PcRtcGetWakeupTime (
   //\r
   Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
   if (EFI_ERROR (Status)) {\r
-       //BugBug: the EfiAtRuntime should be encapsulated in EfiReleaseLock or\r
-    //        provide a new instance for EfiReleaseLock, say, RtEfiReleaseLock\r
+    //Code here doesn't consider the runtime environment.\r
     if (!EfiAtRuntime ()) {\r
     EfiReleaseLock (&Global->RtcLock);\r
     }\r
@@ -532,17 +510,13 @@ PcRtcGetWakeupTime (
     Time->Year    = RtcRead (RTC_ADDRESS_YEAR);\r
   }\r
 \r
-  if (RtcTestCenturyRegister () == EFI_SUCCESS) {\r
-    Century = (UINT8) (RtcRead (RTC_ADDRESS_CENTURY) & 0x7f);\r
-  } else {\r
-    Century = RtcRead (RTC_ADDRESS_CENTURY);\r
-  }\r
-\r
+  Century = RtcRead (RTC_ADDRESS_CENTURY);\r
+  Time->Year = (UINT16) (Century * 100 + Time->Year);\r
+  \r
   //\r
   // Release RTC Lock.\r
   //\r
-  //BugBug: the EfiAtRuntime should be encapsulated in EfiReleaseLock or\r
-  //        provide a new instance for EfiReleaseLock, say, RtEfiReleaseLock\r
+  //Code here doesn't consider the runtime environment.\r
   if (!EfiAtRuntime ()) {\r
     EfiReleaseLock (&Global->RtcLock);\r
   }\r
@@ -606,6 +580,10 @@ PcRtcSetWakeupTime (
     // Just support set alarm time within 24 hours\r
     //\r
     PcRtcGetTime (&RtcTime, &Capabilities, Global);\r
+    Status = RtcTimeFieldsValid (&RtcTime);\r
+    if (EFI_ERROR (Status)) {\r
+      return EFI_DEVICE_ERROR;\r
+    }\r
     if (!IsWithinOneDay (&RtcTime, Time)) {\r
       return EFI_UNSUPPORTED;\r
     }\r
@@ -618,8 +596,7 @@ PcRtcSetWakeupTime (
   //\r
   // Acquire RTC Lock to make access to RTC atomic\r
   //\r
-  //BugBug: the EfiAtRuntime should be encapsulated in EfiAcquireLock or\r
-  //        provide a new instance for EfiAcquireLock, say, RtEfiAcquireLock\r
+  //Code here doesn't consider the runtime environment.\r
   if (!EfiAtRuntime ()) {\r
     EfiAcquireLock (&Global->RtcLock);\r
   }\r
@@ -628,8 +605,7 @@ PcRtcSetWakeupTime (
   //\r
   Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
   if (EFI_ERROR (Status)) {\r
-    //BugBug: the EfiAtRuntime should be encapsulated in EfiReleaseLock or\r
-    //        provide a new instance for EfiReleaseLock, say, RtEfiReleaseLock\r
+    //Code here doesn't consider the runtime environment.\r
     if (!EfiAtRuntime ()) {\r
     EfiReleaseLock (&Global->RtcLock);\r
     }\r
@@ -667,37 +643,13 @@ PcRtcSetWakeupTime (
   //\r
   // Release RTC Lock.\r
   //\r
-  //BugBug: the EfiAtRuntime should be encapsulated in EfiReleaseLock or\r
-  //        provide a new instance for EfiReleaseLock, say, RtEfiReleaseLock\r
+  //Code here doesn't consider the runtime environment.\r
   if (!EfiAtRuntime ()) {\r
     EfiReleaseLock (&Global->RtcLock);\r
   }\r
   return EFI_SUCCESS;\r
 }\r
 \r
-/**\r
-  See if centry register of RTC is valid.\r
-\r
-  @retval  EFI_SUCCESS       Century register is valid.\r
-  @retval  EFI_DEVICE_ERROR  Century register is NOT valid.\r
-**/\r
-EFI_STATUS\r
-RtcTestCenturyRegister (\r
-  VOID\r
-  )\r
-{\r
-  UINT8 Century;\r
-  UINT8 Temp;\r
-\r
-  Century = RtcRead (RTC_ADDRESS_CENTURY);\r
-  Temp = (UINT8) (RtcRead (RTC_ADDRESS_CENTURY) & 0x7f);\r
-  RtcWrite (RTC_ADDRESS_CENTURY, Century);\r
-  if (Temp == 0x19 || Temp == 0x20) {\r
-    return EFI_SUCCESS;\r
-  }\r
-\r
-  return EFI_DEVICE_ERROR;\r
-}\r
 \r
 /**\r
   Checks an 8-bit BCD value, and converts to an 8-bit value if valid.\r
@@ -857,7 +809,6 @@ RtcTimeFieldsValid (
       Time->Year > 2099 ||\r
       Time->Month < 1 ||\r
       Time->Month > 12 ||\r
-      (!DayValid (Time)) ||\r
       Time->Hour > 23 ||\r
       Time->Minute > 59 ||\r
       Time->Second > 59 ||\r
@@ -865,7 +816,9 @@ RtcTimeFieldsValid (
       (!(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
-    return EFI_INVALID_PARAMETER;\r
+    if (!DayValid (Time)) {\r
+      return EFI_INVALID_PARAMETER;\r
+    }\r
   }\r
 \r
   return EFI_SUCCESS;\r
@@ -899,6 +852,11 @@ DayValid (
   DayOfMonth[10] = 30;\r
   DayOfMonth[11] = 31;\r
 \r
+  //\r
+  // The validity of Time->Month field should be checked before\r
+  //\r
+  ASSERT (Time->Month >=1);\r
+  ASSERT (Time->Month <=12);\r
   if (Time->Day < 1 ||\r
       Time->Day > DayOfMonth[Time->Month - 1] ||\r
       (Time->Month == 2 && (!IsLeapYear (Time) && Time->Day > 28))\r
@@ -910,12 +868,12 @@ DayValid (
 }\r
 \r
 /**\r
-  Check if it is a leapyear.\r
+  Check if it is a leap year.\r
 \r
   @param    Time   The time to be checked.\r
 \r
-  @retval   TRUE   It is a leapyear.\r
-  @retval   FALSE  It is NOT a leapyear.\r
+  @retval   TRUE   It is a leap year.\r
+  @retval   FALSE  It is NOT a leap year.\r
 **/\r
 BOOLEAN\r
 IsLeapYear (\r
@@ -1059,6 +1017,12 @@ IsWithinOneDay (
 \r
   Adjacent = FALSE;\r
 \r
+  //\r
+  // The validity of From->Month field should be checked before\r
+  //\r
+  ASSERT (From->Month >=1);\r
+  ASSERT (From->Month <=12);\r
+  \r
   if (From->Year == To->Year) {\r
     if (From->Month == To->Month) {\r
       if ((From->Day + 1) == To->Day) {\r