RealTimeClock.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*++
  2. Emu RTC Architectural Protocol Driver as defined in PI
  3. Copyright (c) 2004 - 2008, Intel Corporation. All rights reserved.<BR>
  4. Portions copyright (c) 2010 - 2011, Apple Inc. All rights reserved.
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include <PiDxe.h>
  8. #include <Library/BaseLib.h>
  9. #include <Library/DebugLib.h>
  10. #include <Library/UefiLib.h>
  11. #include <Library/UefiDriverEntryPoint.h>
  12. #include <Library/EmuThunkLib.h>
  13. #include <Library/MemoryAllocationLib.h>
  14. #include <Library/UefiBootServicesTableLib.h>
  15. #include <Protocol/RealTimeClock.h>
  16. BOOLEAN
  17. DayValid (
  18. IN EFI_TIME *Time
  19. );
  20. BOOLEAN
  21. IsLeapYear (
  22. IN EFI_TIME *Time
  23. );
  24. EFI_STATUS
  25. RtcTimeFieldsValid (
  26. IN EFI_TIME *Time
  27. );
  28. EFI_STATUS
  29. EFIAPI
  30. InitializeRealTimeClock (
  31. IN EFI_HANDLE ImageHandle,
  32. IN EFI_SYSTEM_TABLE *SystemTable
  33. );
  34. EFI_STATUS
  35. EFIAPI
  36. EmuGetTime (
  37. OUT EFI_TIME *Time,
  38. OUT EFI_TIME_CAPABILITIES *Capabilities OPTIONAL
  39. )
  40. /*++
  41. Routine Description:
  42. Service routine for RealTimeClockInstance->GetTime
  43. Arguments:
  44. Time - A pointer to storage that will receive a snapshot of the current time.
  45. Capabilities - A pointer to storage that will receive the capabilities of the real time clock
  46. in the platform. This includes the real time clock's resolution and accuracy.
  47. All reported device capabilities are rounded up. This is an OPTIONAL argument.
  48. Returns:
  49. EFI_SUCEESS - The underlying GetSystemTime call occurred and returned
  50. Note that in the NT32 emulation, the GetSystemTime call has no return value
  51. thus you will always receive a EFI_SUCCESS on this.
  52. **/
  53. {
  54. //
  55. // Check parameter for null pointer
  56. //
  57. if (Time == NULL) {
  58. return EFI_INVALID_PARAMETER;
  59. }
  60. gEmuThunk->GetTime (Time, Capabilities);
  61. return EFI_SUCCESS;
  62. }
  63. EFI_STATUS
  64. EFIAPI
  65. EmuSetTime (
  66. IN EFI_TIME *Time
  67. )
  68. /*++
  69. Routine Description:
  70. Service routine for RealTimeClockInstance->SetTime
  71. Arguments:
  72. Time - A pointer to storage containing the time and date information to
  73. program into the real time clock.
  74. Returns:
  75. EFI_SUCEESS - The operation completed successfully.
  76. EFI_INVALID_PARAMETER - One of the fields in Time is out of range.
  77. EFI_DEVICE_ERROR - The operation could not be complete due to a device error.
  78. **/
  79. {
  80. EFI_STATUS Status;
  81. if (Time == NULL) {
  82. return EFI_INVALID_PARAMETER;
  83. }
  84. //
  85. // Make sure that the time fields are valid
  86. //
  87. Status = RtcTimeFieldsValid (Time);
  88. if (EFI_ERROR (Status)) {
  89. return Status;
  90. }
  91. return EFI_UNSUPPORTED;
  92. }
  93. EFI_STATUS
  94. EFIAPI
  95. EmuGetWakeupTime (
  96. OUT BOOLEAN *Enabled,
  97. OUT BOOLEAN *Pending,
  98. OUT EFI_TIME *Time
  99. )
  100. /*++
  101. Routine Description:
  102. Service routine for RealTimeClockInstance->GetWakeupTime
  103. Arguments:
  104. This - Indicates the protocol instance structure.
  105. Enabled - Indicates if the alarm is currently enabled or disabled.
  106. Pending - Indicates if the alarm signal is pending and requires
  107. acknowledgement.
  108. Time - The current alarm setting.
  109. Returns:
  110. EFI_SUCEESS - The operation completed successfully.
  111. EFI_DEVICE_ERROR - The operation could not be complete due to a device error.
  112. EFI_UNSUPPORTED - The operation is not supported on this platform.
  113. **/
  114. {
  115. return EFI_UNSUPPORTED;
  116. }
  117. EFI_STATUS
  118. EFIAPI
  119. EmuSetWakeupTime (
  120. IN BOOLEAN Enable,
  121. OUT EFI_TIME *Time
  122. )
  123. /*++
  124. Routine Description:
  125. Service routine for RealTimeClockInstance->SetWakeupTime
  126. Arguments:
  127. Enabled - Enable or disable the wakeup alarm.
  128. Time - If enable is TRUE, the time to set the wakup alarm for.
  129. If enable is FALSE, then this parameter is optional, and
  130. may be NULL.
  131. Returns:
  132. EFI_SUCEESS - The operation completed successfully.
  133. EFI_DEVICE_ERROR - The operation could not be complete due to a device error.
  134. EFI_INVALID_PARAMETER - A field in Time is out of range.
  135. EFI_UNSUPPORTED - The operation is not supported on this platform.
  136. **/
  137. {
  138. return EFI_UNSUPPORTED;
  139. }
  140. EFI_STATUS
  141. EFIAPI
  142. InitializeRealTimeClock (
  143. IN EFI_HANDLE ImageHandle,
  144. IN EFI_SYSTEM_TABLE *SystemTable
  145. )
  146. /*++
  147. Routine Description:
  148. Install Real Time Clock Protocol
  149. Arguments:
  150. ImageHandle - Image Handle
  151. SystemTable - Pointer to system table
  152. Returns:
  153. EFI_SUCEESS - Real Time Clock Services are installed into the Runtime Services Table
  154. **/
  155. {
  156. EFI_STATUS Status;
  157. EFI_HANDLE Handle;
  158. SystemTable->RuntimeServices->GetTime = EmuGetTime;
  159. SystemTable->RuntimeServices->SetTime = EmuSetTime;
  160. SystemTable->RuntimeServices->GetWakeupTime = EmuGetWakeupTime;
  161. SystemTable->RuntimeServices->SetWakeupTime = EmuSetWakeupTime;
  162. Handle = NULL;
  163. Status = gBS->InstallMultipleProtocolInterfaces (
  164. &Handle,
  165. &gEfiRealTimeClockArchProtocolGuid,
  166. NULL,
  167. NULL
  168. );
  169. return Status;
  170. }
  171. EFI_STATUS
  172. RtcTimeFieldsValid (
  173. IN EFI_TIME *Time
  174. )
  175. /*++
  176. Routine Description:
  177. Arguments:
  178. Returns:
  179. **/
  180. {
  181. if ((Time->Year < 1998) ||
  182. (Time->Year > 2099) ||
  183. (Time->Month < 1) ||
  184. (Time->Month > 12) ||
  185. (!DayValid (Time)) ||
  186. (Time->Hour > 23) ||
  187. (Time->Minute > 59) ||
  188. (Time->Second > 59) ||
  189. (Time->Nanosecond > 999999999) ||
  190. (!((Time->TimeZone == EFI_UNSPECIFIED_TIMEZONE) || ((Time->TimeZone >= -1440) && (Time->TimeZone <= 1440)))) ||
  191. (Time->Daylight & (~(EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT)))
  192. )
  193. {
  194. return EFI_INVALID_PARAMETER;
  195. }
  196. return EFI_SUCCESS;
  197. }
  198. BOOLEAN
  199. DayValid (
  200. IN EFI_TIME *Time
  201. )
  202. {
  203. STATIC const INTN DayOfMonth[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  204. if ((Time->Day < 1) ||
  205. (Time->Day > DayOfMonth[Time->Month - 1]) ||
  206. ((Time->Month == 2) && (!IsLeapYear (Time) && (Time->Day > 28)))
  207. )
  208. {
  209. return FALSE;
  210. }
  211. return TRUE;
  212. }
  213. BOOLEAN
  214. IsLeapYear (
  215. IN EFI_TIME *Time
  216. )
  217. {
  218. if (Time->Year % 4 == 0) {
  219. if (Time->Year % 100 == 0) {
  220. if (Time->Year % 400 == 0) {
  221. return TRUE;
  222. } else {
  223. return FALSE;
  224. }
  225. } else {
  226. return TRUE;
  227. }
  228. } else {
  229. return FALSE;
  230. }
  231. }