TianoCore EDK2 master
Loading...
Searching...
No Matches
XenConsoleSerialPortLib.c
Go to the documentation of this file.
1
11#include <Base.h>
12
13#include <Library/BaseLib.h>
16
17#include <IndustryStandard/Xen/io/console.h>
18#include <IndustryStandard/Xen/hvm/params.h>
19#include <IndustryStandard/Xen/event_channel.h>
20
21//
22// We can't use DebugLib due to a constructor dependency cycle between DebugLib
23// and ourselves.
24//
25#define ASSERT(Expression) \
26 do { \
27 if (!(Expression)) { \
28 CpuDeadLoop (); \
29 } \
30 } while (FALSE)
31
32//
33// The code below expects these global variables to be mutable, even in the case
34// that we have been incorporated into SEC or PEIM phase modules (which is
35// allowed by our INF description). While this is a dangerous assumption to make
36// in general, it is actually fine for the Xen domU (guest) environment that
37// this module is intended for, as UEFI always executes from DRAM in that case.
38//
39STATIC evtchn_send_t mXenConsoleEventChain;
40STATIC struct xencons_interface *mXenConsoleInterface;
41
53RETURN_STATUS
54EFIAPI
56 VOID
57 )
58{
61 }
62
63 if (!mXenConsoleInterface) {
64 mXenConsoleEventChain.port = (UINT32)XenHypercallHvmGetParam (HVM_PARAM_CONSOLE_EVTCHN);
65 mXenConsoleInterface = (struct xencons_interface *)(UINTN)
66 (XenHypercallHvmGetParam (HVM_PARAM_CONSOLE_PFN) << EFI_PAGE_SHIFT);
67
68 //
69 // No point in ASSERT'ing here as we won't be seeing the output
70 //
71 }
72
73 return RETURN_SUCCESS;
74}
75
94EFIAPI
96 IN UINT8 *Buffer,
97 IN UINTN NumberOfBytes
98 )
99{
100 XENCONS_RING_IDX Consumer, Producer;
101 UINTN Sent;
102
103 ASSERT (Buffer != NULL);
104
105 if (NumberOfBytes == 0) {
106 return 0;
107 }
108
109 if (!mXenConsoleInterface) {
110 return 0;
111 }
112
113 Sent = 0;
114 do {
115 Consumer = mXenConsoleInterface->out_cons;
116 Producer = mXenConsoleInterface->out_prod;
117
118 MemoryFence ();
119
120 while (Sent < NumberOfBytes && ((Producer - Consumer) < sizeof (mXenConsoleInterface->out))) {
121 mXenConsoleInterface->out[MASK_XENCONS_IDX (Producer++, mXenConsoleInterface->out)] = Buffer[Sent++];
122 }
123
124 MemoryFence ();
125
126 mXenConsoleInterface->out_prod = Producer;
127
128 XenHypercallEventChannelOp (EVTCHNOP_send, &mXenConsoleEventChain);
129 } while (Sent < NumberOfBytes);
130
131 return Sent;
132}
133
149UINTN
150EFIAPI
152 OUT UINT8 *Buffer,
153 IN UINTN NumberOfBytes
154 )
155{
156 XENCONS_RING_IDX Consumer, Producer;
157 UINTN Received;
158
159 ASSERT (Buffer != NULL);
160
161 if (NumberOfBytes == 0) {
162 return 0;
163 }
164
165 if (!mXenConsoleInterface) {
166 return 0;
167 }
168
169 Consumer = mXenConsoleInterface->in_cons;
170 Producer = mXenConsoleInterface->in_prod;
171
172 MemoryFence ();
173
174 Received = 0;
175 while (Received < NumberOfBytes && Consumer < Producer) {
176 Buffer[Received++] = mXenConsoleInterface->in[MASK_XENCONS_IDX (Consumer++, mXenConsoleInterface->in)];
177 }
178
179 MemoryFence ();
180
181 mXenConsoleInterface->in_cons = Consumer;
182
183 XenHypercallEventChannelOp (EVTCHNOP_send, &mXenConsoleEventChain);
184
185 return Received;
186}
187
195BOOLEAN
196EFIAPI
198 VOID
199 )
200{
201 return mXenConsoleInterface &&
202 mXenConsoleInterface->in_cons != mXenConsoleInterface->in_prod;
203}
204
215RETURN_STATUS
216EFIAPI
218 IN UINT32 Control
219 )
220{
221 return RETURN_UNSUPPORTED;
222}
223
234RETURN_STATUS
235EFIAPI
237 OUT UINT32 *Control
238 )
239{
240 if (!mXenConsoleInterface) {
241 return RETURN_UNSUPPORTED;
242 }
243
244 *Control = 0;
245 if (!SerialPortPoll ()) {
246 *Control = EFI_SERIAL_INPUT_BUFFER_EMPTY;
247 }
248
249 return RETURN_SUCCESS;
250}
251
285RETURN_STATUS
286EFIAPI
288 IN OUT UINT64 *BaudRate,
289 IN OUT UINT32 *ReceiveFifoDepth,
290 IN OUT UINT32 *Timeout,
291 IN OUT EFI_PARITY_TYPE *Parity,
292 IN OUT UINT8 *DataBits,
293 IN OUT EFI_STOP_BITS_TYPE *StopBits
294 )
295{
296 return RETURN_UNSUPPORTED;
297}
UINT64 UINTN
VOID EFIAPI MemoryFence(VOID)
Definition: CpuBreakpoint.c:42
#define NULL
Definition: Base.h:319
#define STATIC
Definition: Base.h:264
#define RETURN_DEVICE_ERROR
Definition: Base.h:1104
#define RETURN_UNSUPPORTED
Definition: Base.h:1081
#define RETURN_SUCCESS
Definition: Base.h:1066
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
EFI_STOP_BITS_TYPE
Definition: SerialIo.h:53
EFI_PARITY_TYPE
Definition: SerialIo.h:41
BOOLEAN EFIAPI SerialPortPoll(VOID)
RETURN_STATUS EFIAPI SerialPortSetAttributes(IN OUT UINT64 *BaudRate, IN OUT UINT32 *ReceiveFifoDepth, IN OUT UINT32 *Timeout, IN OUT EFI_PARITY_TYPE *Parity, IN OUT UINT8 *DataBits, IN OUT EFI_STOP_BITS_TYPE *StopBits)
UINTN EFIAPI SerialPortRead(OUT UINT8 *Buffer, IN UINTN NumberOfBytes)
RETURN_STATUS EFIAPI SerialPortInitialize(VOID)
RETURN_STATUS EFIAPI SerialPortSetControl(IN UINT32 Control)
RETURN_STATUS EFIAPI SerialPortGetControl(OUT UINT32 *Control)
UINTN EFIAPI SerialPortWrite(IN UINT8 *Buffer, IN UINTN NumberOfBytes)
INTN EFIAPI XenHypercallEventChannelOp(IN INTN Operation, IN OUT VOID *Arguments)
Definition: XenHypercall.c:79
BOOLEAN EFIAPI XenHypercallIsAvailable(VOID)
UINT64 EFIAPI XenHypercallHvmGetParam(UINT32 Index)