RealTimeClock.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /** @file
  2. Implement EFI RealTimeClock runtime services via RTC Lib.
  3. Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
  4. Copyright (c) 2017, Linaro, Ltd. All rights reserved.<BR>
  5. Copyright (c) 2021, Ampere Computing LLC. All rights reserved.<BR>
  6. SPDX-License-Identifier: BSD-2-Clause-Patent
  7. **/
  8. #include <PiDxe.h>
  9. #include <Library/DebugLib.h>
  10. #include <Library/RealTimeClockLib.h>
  11. #include <Library/TimeBaseLib.h>
  12. #include <Library/UefiLib.h>
  13. #include <Library/UefiBootServicesTableLib.h>
  14. #include <Library/UefiRuntimeLib.h>
  15. #include <Protocol/RealTimeClock.h>
  16. EFI_HANDLE mHandle = NULL;
  17. //
  18. // These values can be set by SetTime () and need to be returned by GetTime ()
  19. // but cannot usually be kept by the RTC hardware, so we store them in a UEFI
  20. // variable instead.
  21. //
  22. typedef struct {
  23. INT16 TimeZone;
  24. UINT8 Daylight;
  25. } NON_VOLATILE_TIME_SETTINGS;
  26. STATIC CONST CHAR16 mTimeSettingsVariableName[] = L"RtcTimeSettings";
  27. STATIC NON_VOLATILE_TIME_SETTINGS mTimeSettings;
  28. /**
  29. Returns the current time and date information, and the time-keeping capabilities
  30. of the hardware platform.
  31. @param Time A pointer to storage to receive a snapshot of the current time.
  32. @param Capabilities An optional pointer to a buffer to receive the real time clock
  33. device's capabilities.
  34. @retval EFI_SUCCESS The operation completed successfully.
  35. @retval EFI_INVALID_PARAMETER Time is NULL.
  36. @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.
  37. **/
  38. EFI_STATUS
  39. EFIAPI
  40. GetTime (
  41. OUT EFI_TIME *Time,
  42. OUT EFI_TIME_CAPABILITIES *Capabilities
  43. )
  44. {
  45. if (Time == NULL) {
  46. return EFI_INVALID_PARAMETER;
  47. }
  48. //
  49. // Set these first so the RealTimeClockLib implementation
  50. // can override them based on its own settings.
  51. //
  52. Time->TimeZone = mTimeSettings.TimeZone;
  53. Time->Daylight = mTimeSettings.Daylight;
  54. return LibGetTime (Time, Capabilities);
  55. }
  56. /**
  57. Sets the current local time and date information.
  58. @param Time A pointer to the current time.
  59. @retval EFI_SUCCESS The operation completed successfully.
  60. @retval EFI_INVALID_PARAMETER A time field is out of range.
  61. @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
  62. **/
  63. EFI_STATUS
  64. EFIAPI
  65. SetTime (
  66. IN EFI_TIME *Time
  67. )
  68. {
  69. EFI_STATUS Status;
  70. BOOLEAN TimeSettingsChanged;
  71. if ((Time == NULL) || !IsTimeValid (Time)) {
  72. return EFI_INVALID_PARAMETER;
  73. }
  74. TimeSettingsChanged = FALSE;
  75. if ((mTimeSettings.TimeZone != Time->TimeZone) ||
  76. (mTimeSettings.Daylight != Time->Daylight))
  77. {
  78. mTimeSettings.TimeZone = Time->TimeZone;
  79. mTimeSettings.Daylight = Time->Daylight;
  80. TimeSettingsChanged = TRUE;
  81. }
  82. Status = LibSetTime (Time);
  83. if (EFI_ERROR (Status)) {
  84. return Status;
  85. }
  86. if (TimeSettingsChanged) {
  87. Status = EfiSetVariable (
  88. (CHAR16 *)mTimeSettingsVariableName,
  89. &gEfiCallerIdGuid,
  90. EFI_VARIABLE_NON_VOLATILE |
  91. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  92. EFI_VARIABLE_RUNTIME_ACCESS,
  93. sizeof (mTimeSettings),
  94. (VOID *)&mTimeSettings
  95. );
  96. if (EFI_ERROR (Status)) {
  97. return EFI_DEVICE_ERROR;
  98. }
  99. }
  100. return EFI_SUCCESS;
  101. }
  102. /**
  103. Returns the current wakeup alarm clock setting.
  104. @param Enabled Indicates if the alarm is currently enabled or disabled.
  105. @param Pending Indicates if the alarm signal is pending and requires acknowledgement.
  106. @param Time The current alarm setting.
  107. @retval EFI_SUCCESS The alarm settings were returned.
  108. @retval EFI_INVALID_PARAMETER Any parameter is NULL.
  109. @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
  110. **/
  111. EFI_STATUS
  112. EFIAPI
  113. GetWakeupTime (
  114. OUT BOOLEAN *Enabled,
  115. OUT BOOLEAN *Pending,
  116. OUT EFI_TIME *Time
  117. )
  118. {
  119. if ((Time == NULL) || (Enabled == NULL) || (Pending == NULL)) {
  120. return EFI_INVALID_PARAMETER;
  121. }
  122. //
  123. // Set these first so the RealTimeClockLib implementation
  124. // can override them based on its own settings.
  125. //
  126. Time->TimeZone = mTimeSettings.TimeZone;
  127. Time->Daylight = mTimeSettings.Daylight;
  128. return LibGetWakeupTime (Enabled, Pending, Time);
  129. }
  130. /**
  131. Sets the system wakeup alarm clock time.
  132. @param Enabled Enable or disable the wakeup alarm.
  133. @param Time If Enable is TRUE, the time to set the wakeup alarm for.
  134. @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If
  135. Enable is FALSE, then the wakeup alarm was disabled.
  136. @retval EFI_INVALID_PARAMETER A time field is out of range.
  137. @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
  138. @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
  139. **/
  140. EFI_STATUS
  141. EFIAPI
  142. SetWakeupTime (
  143. IN BOOLEAN Enabled,
  144. OUT EFI_TIME *Time
  145. )
  146. {
  147. return LibSetWakeupTime (Enabled, Time);
  148. }
  149. /**
  150. This is the declaration of an EFI image entry point. This can be the entry point to an application
  151. written to this specification, an EFI boot service driver, or an EFI runtime driver.
  152. @param ImageHandle Handle that identifies the loaded image.
  153. @param SystemTable System Table for this image.
  154. @retval EFI_SUCCESS The operation completed successfully.
  155. **/
  156. EFI_STATUS
  157. EFIAPI
  158. InitializeRealTimeClock (
  159. IN EFI_HANDLE ImageHandle,
  160. IN EFI_SYSTEM_TABLE *SystemTable
  161. )
  162. {
  163. EFI_STATUS Status;
  164. UINTN Size;
  165. Status = LibRtcInitialize (ImageHandle, SystemTable);
  166. if (EFI_ERROR (Status)) {
  167. return Status;
  168. }
  169. Size = sizeof (mTimeSettings);
  170. Status = EfiGetVariable (
  171. (CHAR16 *)mTimeSettingsVariableName,
  172. &gEfiCallerIdGuid,
  173. NULL,
  174. &Size,
  175. (VOID *)&mTimeSettings
  176. );
  177. if (EFI_ERROR (Status) ||
  178. !IsValidTimeZone (mTimeSettings.TimeZone) ||
  179. !IsValidDaylight (mTimeSettings.Daylight))
  180. {
  181. DEBUG ((
  182. DEBUG_WARN,
  183. "%a: using default timezone/daylight settings\n",
  184. __FUNCTION__
  185. ));
  186. mTimeSettings.TimeZone = EFI_UNSPECIFIED_TIMEZONE;
  187. mTimeSettings.Daylight = 0;
  188. }
  189. SystemTable->RuntimeServices->GetTime = GetTime;
  190. SystemTable->RuntimeServices->SetTime = SetTime;
  191. SystemTable->RuntimeServices->GetWakeupTime = GetWakeupTime;
  192. SystemTable->RuntimeServices->SetWakeupTime = SetWakeupTime;
  193. Status = gBS->InstallMultipleProtocolInterfaces (
  194. &mHandle,
  195. &gEfiRealTimeClockArchProtocolGuid,
  196. NULL,
  197. NULL
  198. );
  199. return Status;
  200. }