]> git.proxmox.com Git - mirror_edk2.git/blobdiff - EmbeddedPkg/RealTimeClockRuntimeDxe/RealTimeClock.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / EmbeddedPkg / RealTimeClockRuntimeDxe / RealTimeClock.c
index 9564cf55eead4bf8f65f814c911edd0fc9a4dcd8..7adb7324057e2175357e98b160daa49fc7c8173f 100644 (file)
@@ -1,30 +1,37 @@
 /** @file\r
   Implement EFI RealTimeClock runtime services via RTC Lib.\r
-  \r
-  Currently this driver does not support runtime virtual calling.\r
-\r
 \r
   Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r
-  \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
+  Copyright (c) 2017, Linaro, Ltd. All rights reserved.<BR>\r
+  Copyright (c) 2021, Ampere Computing LLC. All rights reserved.<BR>\r
 \r
-  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
-#include <PiDxe.h> \r
+#include <PiDxe.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/RealTimeClockLib.h>\r
+#include <Library/TimeBaseLib.h>\r
 #include <Library/UefiLib.h>\r
 #include <Library/UefiBootServicesTableLib.h>\r
-#include <Library/RealTimeClockLib.h>\r
+#include <Library/UefiRuntimeLib.h>\r
 #include <Protocol/RealTimeClock.h>\r
 \r
 EFI_HANDLE  mHandle = NULL;\r
 \r
+//\r
+// These values can be set by SetTime () and need to be returned by GetTime ()\r
+// but cannot usually be kept by the RTC hardware, so we store them in a UEFI\r
+// variable instead.\r
+//\r
+typedef struct {\r
+  INT16    TimeZone;\r
+  UINT8    Daylight;\r
+} NON_VOLATILE_TIME_SETTINGS;\r
 \r
+STATIC CONST CHAR16                mTimeSettingsVariableName[] = L"RtcTimeSettings";\r
+STATIC NON_VOLATILE_TIME_SETTINGS  mTimeSettings;\r
 \r
 /**\r
   Returns the current time and date information, and the time-keeping capabilities\r
@@ -42,14 +49,23 @@ EFI_HANDLE  mHandle = NULL;
 EFI_STATUS\r
 EFIAPI\r
 GetTime (\r
-  OUT EFI_TIME                *Time,\r
-  OUT  EFI_TIME_CAPABILITIES  *Capabilities\r
+  OUT EFI_TIME               *Time,\r
+  OUT EFI_TIME_CAPABILITIES  *Capabilities\r
   )\r
 {\r
-  return LibGetTime (Time, Capabilities);\r
-}\r
+  if (Time == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
 \r
+  //\r
+  // Set these first so the RealTimeClockLib implementation\r
+  // can override them based on its own settings.\r
+  //\r
+  Time->TimeZone = mTimeSettings.TimeZone;\r
+  Time->Daylight = mTimeSettings.Daylight;\r
 \r
+  return LibGetTime (Time, Capabilities);\r
+}\r
 \r
 /**\r
   Sets the current local time and date information.\r
@@ -58,19 +74,54 @@ GetTime (
 \r
   @retval EFI_SUCCESS           The operation completed successfully.\r
   @retval EFI_INVALID_PARAMETER A time field is out of range.\r
-  @retval EFI_DEVICE_ERROR      The time could not be set due due to hardware error.\r
+  @retval EFI_DEVICE_ERROR      The time could not be set due to hardware error.\r
 \r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 SetTime (\r
-  IN EFI_TIME                *Time\r
+  IN EFI_TIME  *Time\r
   )\r
 {\r
-  return LibSetTime (Time);\r
+  EFI_STATUS  Status;\r
+  BOOLEAN     TimeSettingsChanged;\r
+\r
+  if ((Time == NULL) || !IsTimeValid (Time)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  TimeSettingsChanged = FALSE;\r
+  if ((mTimeSettings.TimeZone != Time->TimeZone) ||\r
+      (mTimeSettings.Daylight != Time->Daylight))\r
+  {\r
+    mTimeSettings.TimeZone = Time->TimeZone;\r
+    mTimeSettings.Daylight = Time->Daylight;\r
+    TimeSettingsChanged    = TRUE;\r
+  }\r
+\r
+  Status = LibSetTime (Time);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  if (TimeSettingsChanged) {\r
+    Status = EfiSetVariable (\r
+               (CHAR16 *)mTimeSettingsVariableName,\r
+               &gEfiCallerIdGuid,\r
+               EFI_VARIABLE_NON_VOLATILE |\r
+               EFI_VARIABLE_BOOTSERVICE_ACCESS |\r
+               EFI_VARIABLE_RUNTIME_ACCESS,\r
+               sizeof (mTimeSettings),\r
+               (VOID *)&mTimeSettings\r
+               );\r
+    if (EFI_ERROR (Status)) {\r
+      return EFI_DEVICE_ERROR;\r
+    }\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
 }\r
 \r
-\r
 /**\r
   Returns the current wakeup alarm clock setting.\r
 \r
@@ -86,15 +137,25 @@ SetTime (
 EFI_STATUS\r
 EFIAPI\r
 GetWakeupTime (\r
-  OUT BOOLEAN     *Enabled,\r
-  OUT BOOLEAN     *Pending,\r
-  OUT EFI_TIME    *Time\r
+  OUT BOOLEAN   *Enabled,\r
+  OUT BOOLEAN   *Pending,\r
+  OUT EFI_TIME  *Time\r
   )\r
 {\r
+  if ((Time == NULL) || (Enabled == NULL) || (Pending == NULL)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  //\r
+  // Set these first so the RealTimeClockLib implementation\r
+  // can override them based on its own settings.\r
+  //\r
+  Time->TimeZone = mTimeSettings.TimeZone;\r
+  Time->Daylight = mTimeSettings.Daylight;\r
+\r
   return LibGetWakeupTime (Enabled, Pending, Time);\r
 }\r
 \r
-\r
 /**\r
   Sets the system wakeup alarm clock time.\r
 \r
@@ -111,15 +172,13 @@ GetWakeupTime (
 EFI_STATUS\r
 EFIAPI\r
 SetWakeupTime (\r
-  IN BOOLEAN      Enabled,\r
-  OUT EFI_TIME    *Time\r
+  IN BOOLEAN    Enabled,\r
+  OUT EFI_TIME  *Time\r
   )\r
 {\r
   return LibSetWakeupTime (Enabled, Time);\r
 }\r
 \r
-\r
-\r
 /**\r
   This is the declaration of an EFI image entry point. This can be the entry point to an application\r
   written to this specification, an EFI boot service driver, or an EFI runtime driver.\r
@@ -133,13 +192,39 @@ SetWakeupTime (
 EFI_STATUS\r
 EFIAPI\r
 InitializeRealTimeClock (\r
-  IN EFI_HANDLE                            ImageHandle,\r
-  IN EFI_SYSTEM_TABLE                      *SystemTable\r
+  IN EFI_HANDLE        ImageHandle,\r
+  IN EFI_SYSTEM_TABLE  *SystemTable\r
   )\r
 {\r
   EFI_STATUS  Status;\r
-\r
-  LibRtcInitialize (ImageHandle, SystemTable);\r
+  UINTN       Size;\r
+\r
+  Status = LibRtcInitialize (ImageHandle, SystemTable);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  Size   = sizeof (mTimeSettings);\r
+  Status = EfiGetVariable (\r
+             (CHAR16 *)mTimeSettingsVariableName,\r
+             &gEfiCallerIdGuid,\r
+             NULL,\r
+             &Size,\r
+             (VOID *)&mTimeSettings\r
+             );\r
+  if (EFI_ERROR (Status) ||\r
+      !IsValidTimeZone (mTimeSettings.TimeZone) ||\r
+      !IsValidDaylight (mTimeSettings.Daylight))\r
+  {\r
+    DEBUG ((\r
+      DEBUG_WARN,\r
+      "%a: using default timezone/daylight settings\n",\r
+      __FUNCTION__\r
+      ));\r
+\r
+    mTimeSettings.TimeZone = EFI_UNSPECIFIED_TIMEZONE;\r
+    mTimeSettings.Daylight = 0;\r
+  }\r
 \r
   SystemTable->RuntimeServices->GetTime       = GetTime;\r
   SystemTable->RuntimeServices->SetTime       = SetTime;\r
@@ -155,4 +240,3 @@ InitializeRealTimeClock (
 \r
   return Status;\r
 }\r
-\r