1 /** @file
2   Implement EFI RealTimeClock runtime services via Xen shared info page
3 
4   Copyright (c) 2015, Linaro Ltd. All rights reserved.<BR>
5 
6   This program and the accompanying materials
7   are licensed and made available under the terms and conditions of the BSD License
8   which accompanies this distribution.  The full text of the license may be found at
9   http://opensource.org/licenses/bsd-license.php
10 
11   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 
14 **/
15 
16 #include <Uefi.h>
17 #include <PiDxe.h>
18 #include <Library/BaseLib.h>
19 #include <Library/DebugLib.h>
20 
21 /**
22   Converts Epoch seconds (elapsed since 1970 JANUARY 01, 00:00:00 UTC) to EFI_TIME
23  **/
24 STATIC
25 VOID
EpochToEfiTime(IN UINTN EpochSeconds,OUT EFI_TIME * Time)26 EpochToEfiTime (
27   IN  UINTN     EpochSeconds,
28   OUT EFI_TIME  *Time
29   )
30 {
31   UINTN         a;
32   UINTN         b;
33   UINTN         c;
34   UINTN         d;
35   UINTN         g;
36   UINTN         j;
37   UINTN         m;
38   UINTN         y;
39   UINTN         da;
40   UINTN         db;
41   UINTN         dc;
42   UINTN         dg;
43   UINTN         hh;
44   UINTN         mm;
45   UINTN         ss;
46   UINTN         J;
47 
48   J  = (EpochSeconds / 86400) + 2440588;
49   j  = J + 32044;
50   g  = j / 146097;
51   dg = j % 146097;
52   c  = (((dg / 36524) + 1) * 3) / 4;
53   dc = dg - (c * 36524);
54   b  = dc / 1461;
55   db = dc % 1461;
56   a  = (((db / 365) + 1) * 3) / 4;
57   da = db - (a * 365);
58   y  = (g * 400) + (c * 100) + (b * 4) + a;
59   m  = (((da * 5) + 308) / 153) - 2;
60   d  = da - (((m + 4) * 153) / 5) + 122;
61 
62   Time->Year  = y - 4800 + ((m + 2) / 12);
63   Time->Month = ((m + 2) % 12) + 1;
64   Time->Day   = d + 1;
65 
66   ss = EpochSeconds % 60;
67   a  = (EpochSeconds - ss) / 60;
68   mm = a % 60;
69   b = (a - mm) / 60;
70   hh = b % 24;
71 
72   Time->Hour        = hh;
73   Time->Minute      = mm;
74   Time->Second      = ss;
75   Time->Nanosecond  = 0;
76 
77 }
78 
79 /**
80   Returns the current time and date information, and the time-keeping capabilities
81   of the hardware platform.
82 
83   @param  Time                  A pointer to storage to receive a snapshot of the current time.
84   @param  Capabilities          An optional pointer to a buffer to receive the real time clock
85                                 device's capabilities.
86 
87   @retval EFI_SUCCESS           The operation completed successfully.
88   @retval EFI_INVALID_PARAMETER Time is NULL.
89   @retval EFI_DEVICE_ERROR      The time could not be retrieved due to hardware error.
90 
91 **/
92 EFI_STATUS
93 EFIAPI
LibGetTime(OUT EFI_TIME * Time,OUT EFI_TIME_CAPABILITIES * Capabilities)94 LibGetTime (
95   OUT EFI_TIME                *Time,
96   OUT  EFI_TIME_CAPABILITIES  *Capabilities
97   )
98 {
99   ASSERT (Time != NULL);
100 
101   //
102   // For now, there is nothing that we can do besides returning a bogus time,
103   // as Xen's timekeeping uses a shared info page which cannot be shared
104   // between UEFI and the OS
105   //
106   EpochToEfiTime(1421770011, Time);
107 
108   return EFI_SUCCESS;
109 }
110 
111 /**
112   Sets the current local time and date information.
113 
114   @param  Time                  A pointer to the current time.
115 
116   @retval EFI_SUCCESS           The operation completed successfully.
117   @retval EFI_INVALID_PARAMETER A time field is out of range.
118   @retval EFI_DEVICE_ERROR      The time could not be set due due to hardware error.
119 
120 **/
121 EFI_STATUS
122 EFIAPI
LibSetTime(IN EFI_TIME * Time)123 LibSetTime (
124   IN EFI_TIME                *Time
125   )
126 {
127   return EFI_DEVICE_ERROR;
128 }
129 
130 
131 /**
132   Returns the current wakeup alarm clock setting.
133 
134   @param  Enabled               Indicates if the alarm is currently enabled or disabled.
135   @param  Pending               Indicates if the alarm signal is pending and requires acknowledgement.
136   @param  Time                  The current alarm setting.
137 
138   @retval EFI_SUCCESS           The alarm settings were returned.
139   @retval EFI_INVALID_PARAMETER Any parameter is NULL.
140   @retval EFI_DEVICE_ERROR      The wakeup time could not be retrieved due to a hardware error.
141   @retval EFI_UNSUPPORTED       A wakeup timer is not supported on this platform.
142 
143 **/
144 EFI_STATUS
145 EFIAPI
LibGetWakeupTime(OUT BOOLEAN * Enabled,OUT BOOLEAN * Pending,OUT EFI_TIME * Time)146 LibGetWakeupTime (
147   OUT BOOLEAN     *Enabled,
148   OUT BOOLEAN     *Pending,
149   OUT EFI_TIME    *Time
150   )
151 {
152   return EFI_UNSUPPORTED;
153 }
154 
155 /**
156   Sets the system wakeup alarm clock time.
157 
158   @param  Enabled               Enable or disable the wakeup alarm.
159   @param  Time                  If Enable is TRUE, the time to set the wakeup alarm for.
160 
161   @retval EFI_SUCCESS           If Enable is TRUE, then the wakeup alarm was enabled. If
162                                 Enable is FALSE, then the wakeup alarm was disabled.
163   @retval EFI_INVALID_PARAMETER A time field is out of range.
164   @retval EFI_DEVICE_ERROR      The wakeup time could not be set due to a hardware error.
165   @retval EFI_UNSUPPORTED       A wakeup timer is not supported on this platform.
166 
167 **/
168 EFI_STATUS
169 EFIAPI
LibSetWakeupTime(IN BOOLEAN Enabled,OUT EFI_TIME * Time)170 LibSetWakeupTime (
171   IN BOOLEAN      Enabled,
172   OUT EFI_TIME    *Time
173   )
174 {
175   return EFI_UNSUPPORTED;
176 }
177 
178 /**
179   This is the declaration of an EFI image entry point. This can be the entry point to an application
180   written to this specification, an EFI boot service driver, or an EFI runtime driver.
181 
182   @param  ImageHandle           Handle that identifies the loaded image.
183   @param  SystemTable           System Table for this image.
184 
185   @retval EFI_SUCCESS           The operation completed successfully.
186 
187 **/
188 EFI_STATUS
189 EFIAPI
LibRtcInitialize(IN EFI_HANDLE ImageHandle,IN EFI_SYSTEM_TABLE * SystemTable)190 LibRtcInitialize (
191   IN EFI_HANDLE                            ImageHandle,
192   IN EFI_SYSTEM_TABLE                      *SystemTable
193   )
194 {
195   return EFI_SUCCESS;
196 }
197