PageRenderTime 64ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/EmbeddedPkg/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c

https://github.com/tianocore/edk2
C | 422 lines | 272 code | 37 blank | 113 comment | 45 complexity | 2b0798d3c9a7af1ea723d7287dd4ce6e MD5 | raw file
  1. /** @file
  2. *
  3. * Implement virtual EFI RealTimeClock runtime services.
  4. *
  5. * Coypright (c) 2019, Pete Batard <pete@akeo.ie>
  6. * Copyright (c) 2018, Andrei Warkentin <andrey.warkentin@gmail.com>
  7. * Copyright (c) 2011-2021, ARM Ltd. All rights reserved.
  8. * Copyright (c) 2008-2010, Apple Inc. All rights reserved.
  9. * Copyright (c) Microsoft Corporation. All rights reserved.
  10. *
  11. * SPDX-License-Identifier: BSD-2-Clause-Patent
  12. *
  13. * Based on ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.inf
  14. *
  15. **/
  16. #include <PiDxe.h>
  17. #include <Library/BaseLib.h>
  18. #include <Library/DebugLib.h>
  19. #include <Library/IoLib.h>
  20. #include <Library/RealTimeClockLib.h>
  21. #include <Library/TimerLib.h>
  22. #include <Library/TimeBaseLib.h>
  23. #include <Library/UefiRuntimeLib.h>
  24. STATIC CONST CHAR16 mEpochVariableName[] = L"RtcEpochSeconds";
  25. STATIC CONST CHAR16 mTimeZoneVariableName[] = L"RtcTimeZone";
  26. STATIC CONST CHAR16 mDaylightVariableName[] = L"RtcDaylight";
  27. /**
  28. Returns the current time and date information, and the time-keeping capabilities
  29. of the virtual RTC.
  30. @param Time A pointer to storage to receive a snapshot of the current time.
  31. @param Capabilities An optional pointer to a buffer to receive the real time clock
  32. device's capabilities.
  33. @retval EFI_SUCCESS The operation completed successfully.
  34. @retval EFI_INVALID_PARAMETER Time is NULL.
  35. @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.
  36. **/
  37. EFI_STATUS
  38. EFIAPI
  39. LibGetTime (
  40. OUT EFI_TIME *Time,
  41. OUT EFI_TIME_CAPABILITIES *Capabilities
  42. )
  43. {
  44. EFI_STATUS Status;
  45. INT16 TimeZone;
  46. UINT8 Daylight;
  47. UINT64 Freq;
  48. UINT64 Counter;
  49. UINT64 Remainder;
  50. UINTN EpochSeconds;
  51. UINTN Size;
  52. if (Time == NULL) {
  53. return EFI_INVALID_PARAMETER;
  54. }
  55. // Get the counter frequency
  56. Freq = GetPerformanceCounterProperties (NULL, NULL);
  57. if (Freq == 0) {
  58. return EFI_DEVICE_ERROR;
  59. }
  60. // Get the epoch time from non-volatile storage
  61. Size = sizeof (UINTN);
  62. EpochSeconds = 0;
  63. Status = EfiGetVariable (
  64. (CHAR16 *)mEpochVariableName,
  65. &gEfiCallerIdGuid,
  66. NULL,
  67. &Size,
  68. (VOID *)&EpochSeconds
  69. );
  70. // Fall back to compilation-time epoch if not set
  71. if (EFI_ERROR (Status)) {
  72. ASSERT (Status != EFI_INVALID_PARAMETER);
  73. ASSERT (Status != EFI_BUFFER_TOO_SMALL);
  74. //
  75. // The following is intended to produce a compilation error on build
  76. // environments where BUILD_EPOCH can not be set from inline shell.
  77. // If you are attempting to use this library on such an environment, please
  78. // contact the edk2 mailing list, so we can try to add support for it.
  79. //
  80. EpochSeconds = BUILD_EPOCH;
  81. DEBUG ((
  82. DEBUG_INFO,
  83. "LibGetTime: %s non volatile variable was not found - Using compilation time epoch.\n",
  84. mEpochVariableName
  85. ));
  86. EfiSetVariable (
  87. (CHAR16 *)mEpochVariableName,
  88. &gEfiCallerIdGuid,
  89. EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
  90. sizeof (EpochSeconds),
  91. &EpochSeconds
  92. );
  93. }
  94. Counter = GetPerformanceCounter ();
  95. EpochSeconds += DivU64x64Remainder (Counter, Freq, &Remainder);
  96. // Get the current time zone information from non-volatile storage
  97. Size = sizeof (TimeZone);
  98. Status = EfiGetVariable (
  99. (CHAR16 *)mTimeZoneVariableName,
  100. &gEfiCallerIdGuid,
  101. NULL,
  102. &Size,
  103. (VOID *)&TimeZone
  104. );
  105. if (EFI_ERROR (Status)) {
  106. ASSERT (Status != EFI_INVALID_PARAMETER);
  107. ASSERT (Status != EFI_BUFFER_TOO_SMALL);
  108. if (Status != EFI_NOT_FOUND) {
  109. return Status;
  110. }
  111. // The time zone variable does not exist in non-volatile storage, so create it.
  112. Time->TimeZone = EFI_UNSPECIFIED_TIMEZONE;
  113. // Store it
  114. Status = EfiSetVariable (
  115. (CHAR16 *)mTimeZoneVariableName,
  116. &gEfiCallerIdGuid,
  117. EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
  118. Size,
  119. (VOID *)&(Time->TimeZone)
  120. );
  121. if (EFI_ERROR (Status)) {
  122. DEBUG ((
  123. DEBUG_ERROR,
  124. "LibGetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
  125. mTimeZoneVariableName,
  126. Status
  127. ));
  128. return Status;
  129. }
  130. } else {
  131. // Got the time zone
  132. Time->TimeZone = TimeZone;
  133. // Check TimeZone bounds: -1440 to 1440 or 2047
  134. if ( ((Time->TimeZone < -1440) || (Time->TimeZone > 1440))
  135. && (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE))
  136. {
  137. Time->TimeZone = EFI_UNSPECIFIED_TIMEZONE;
  138. }
  139. // Adjust for the correct time zone
  140. if (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE) {
  141. EpochSeconds += Time->TimeZone * SEC_PER_MIN;
  142. }
  143. }
  144. // Get the current daylight information from non-volatile storage
  145. Size = sizeof (Daylight);
  146. Status = EfiGetVariable (
  147. (CHAR16 *)mDaylightVariableName,
  148. &gEfiCallerIdGuid,
  149. NULL,
  150. &Size,
  151. (VOID *)&Daylight
  152. );
  153. if (EFI_ERROR (Status)) {
  154. ASSERT (Status != EFI_INVALID_PARAMETER);
  155. ASSERT (Status != EFI_BUFFER_TOO_SMALL);
  156. if (Status != EFI_NOT_FOUND) {
  157. return Status;
  158. }
  159. // The daylight variable does not exist in non-volatile storage, so create it.
  160. Time->Daylight = 0;
  161. // Store it
  162. Status = EfiSetVariable (
  163. (CHAR16 *)mDaylightVariableName,
  164. &gEfiCallerIdGuid,
  165. EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
  166. Size,
  167. (VOID *)&(Time->Daylight)
  168. );
  169. if (EFI_ERROR (Status)) {
  170. DEBUG ((
  171. DEBUG_ERROR,
  172. "LibGetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
  173. mDaylightVariableName,
  174. Status
  175. ));
  176. return Status;
  177. }
  178. } else {
  179. // Got the daylight information
  180. Time->Daylight = Daylight;
  181. // Adjust for the correct period
  182. if ((Time->Daylight & EFI_TIME_IN_DAYLIGHT) == EFI_TIME_IN_DAYLIGHT) {
  183. // Convert to adjusted time, i.e. spring forwards one hour
  184. EpochSeconds += SEC_PER_HOUR;
  185. }
  186. }
  187. EpochToEfiTime (EpochSeconds, Time);
  188. // Because we use the performance counter, we can fill the Nanosecond attribute
  189. // provided that the remainder doesn't overflow 64-bit during multiplication.
  190. if (Remainder <= 18446744073U) {
  191. Time->Nanosecond = (UINT32)(MultU64x64 (Remainder, 1000000000U) / Freq);
  192. } else {
  193. DEBUG ((DEBUG_WARN, "LibGetTime: Nanosecond value not set (64-bit overflow).\n"));
  194. }
  195. if (Capabilities) {
  196. Capabilities->Accuracy = 0;
  197. Capabilities->Resolution = 1;
  198. Capabilities->SetsToZero = FALSE;
  199. }
  200. return EFI_SUCCESS;
  201. }
  202. /**
  203. Sets the current local time and date information.
  204. @param Time A pointer to the current time.
  205. @retval EFI_SUCCESS The operation completed successfully.
  206. @retval EFI_INVALID_PARAMETER A time field is out of range.
  207. @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
  208. **/
  209. EFI_STATUS
  210. EFIAPI
  211. LibSetTime (
  212. IN EFI_TIME *Time
  213. )
  214. {
  215. EFI_STATUS Status;
  216. UINT64 Freq;
  217. UINT64 Counter;
  218. UINT64 Remainder;
  219. UINTN EpochSeconds;
  220. if (!IsTimeValid (Time)) {
  221. return EFI_INVALID_PARAMETER;
  222. }
  223. EpochSeconds = EfiTimeToEpoch (Time);
  224. // Adjust for the correct time zone, i.e. convert to UTC time zone
  225. if ( (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE)
  226. && (EpochSeconds > Time->TimeZone * SEC_PER_MIN))
  227. {
  228. EpochSeconds -= Time->TimeZone * SEC_PER_MIN;
  229. }
  230. // Adjust for the correct period
  231. if ( ((Time->Daylight & EFI_TIME_IN_DAYLIGHT) == EFI_TIME_IN_DAYLIGHT)
  232. && (EpochSeconds > SEC_PER_HOUR))
  233. {
  234. // Convert to un-adjusted time, i.e. fall back one hour
  235. EpochSeconds -= SEC_PER_HOUR;
  236. }
  237. // Use the performance counter to subtract the number of seconds
  238. // since platform reset. Without this, setting time from the shell
  239. // and immediately reading it back would result in a forward time
  240. // offset, of the duration during which the platform has been up.
  241. Freq = GetPerformanceCounterProperties (NULL, NULL);
  242. if (Freq != 0) {
  243. Counter = GetPerformanceCounter ();
  244. if (EpochSeconds > DivU64x64Remainder (Counter, Freq, &Remainder)) {
  245. EpochSeconds -= DivU64x64Remainder (Counter, Freq, &Remainder);
  246. }
  247. }
  248. // Save the current time zone information into non-volatile storage
  249. Status = EfiSetVariable (
  250. (CHAR16 *)mTimeZoneVariableName,
  251. &gEfiCallerIdGuid,
  252. EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
  253. sizeof (Time->TimeZone),
  254. (VOID *)&(Time->TimeZone)
  255. );
  256. if (EFI_ERROR (Status)) {
  257. DEBUG ((
  258. DEBUG_ERROR,
  259. "LibSetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
  260. mTimeZoneVariableName,
  261. Status
  262. ));
  263. return Status;
  264. }
  265. // Save the current daylight information into non-volatile storage
  266. Status = EfiSetVariable (
  267. (CHAR16 *)mDaylightVariableName,
  268. &gEfiCallerIdGuid,
  269. EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
  270. sizeof (Time->Daylight),
  271. (VOID *)&(Time->Daylight)
  272. );
  273. if (EFI_ERROR (Status)) {
  274. DEBUG ((
  275. DEBUG_ERROR,
  276. "LibSetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
  277. mDaylightVariableName,
  278. Status
  279. ));
  280. return Status;
  281. }
  282. Status = EfiSetVariable (
  283. (CHAR16 *)mEpochVariableName,
  284. &gEfiCallerIdGuid,
  285. EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
  286. sizeof (EpochSeconds),
  287. &EpochSeconds
  288. );
  289. if (EFI_ERROR (Status)) {
  290. DEBUG ((
  291. DEBUG_ERROR,
  292. "LibSetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
  293. mEpochVariableName,
  294. Status
  295. ));
  296. return Status;
  297. }
  298. return EFI_SUCCESS;
  299. }
  300. /**
  301. Returns the current wakeup alarm clock setting.
  302. @param Enabled Indicates if the alarm is currently enabled or disabled.
  303. @param Pending Indicates if the alarm signal is pending and requires acknowledgement.
  304. @param Time The current alarm setting.
  305. @retval EFI_SUCCESS The alarm settings were returned.
  306. @retval EFI_INVALID_PARAMETER Any parameter is NULL.
  307. @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
  308. **/
  309. EFI_STATUS
  310. EFIAPI
  311. LibGetWakeupTime (
  312. OUT BOOLEAN *Enabled,
  313. OUT BOOLEAN *Pending,
  314. OUT EFI_TIME *Time
  315. )
  316. {
  317. return EFI_UNSUPPORTED;
  318. }
  319. /**
  320. Sets the system wakeup alarm clock time.
  321. @param Enabled Enable or disable the wakeup alarm.
  322. @param Time If Enable is TRUE, the time to set the wakeup alarm for.
  323. @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If
  324. Enable is FALSE, then the wakeup alarm was disabled.
  325. @retval EFI_INVALID_PARAMETER A time field is out of range.
  326. @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
  327. @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
  328. **/
  329. EFI_STATUS
  330. EFIAPI
  331. LibSetWakeupTime (
  332. IN BOOLEAN Enabled,
  333. OUT EFI_TIME *Time
  334. )
  335. {
  336. return EFI_UNSUPPORTED;
  337. }
  338. /**
  339. This is the declaration of an EFI image entry point. This can be the entry point to an application
  340. written to this specification, an EFI boot service driver, or an EFI runtime driver.
  341. @param ImageHandle Handle that identifies the loaded image.
  342. @param SystemTable System Table for this image.
  343. @retval EFI_SUCCESS The operation completed successfully.
  344. **/
  345. EFI_STATUS
  346. EFIAPI
  347. LibRtcInitialize (
  348. IN EFI_HANDLE ImageHandle,
  349. IN EFI_SYSTEM_TABLE *SystemTable
  350. )
  351. {
  352. return EFI_SUCCESS;
  353. }
  354. /**
  355. Fixup internal data so that EFI can be call in virtual mode.
  356. Call the passed in Child Notify event and convert any pointers in
  357. lib to virtual mode.
  358. @param[in] Event The Event that is being processed
  359. @param[in] Context Event Context
  360. **/
  361. VOID
  362. EFIAPI
  363. LibRtcVirtualNotifyEvent (
  364. IN EFI_EVENT Event,
  365. IN VOID *Context
  366. )
  367. {
  368. return;
  369. }