]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Library/XenConsoleSerialPortLib/XenConsoleSerialPortLib.c
OvmfPkg: XenConsoleSerialPortLib: deal with output overflow
[mirror_edk2.git] / OvmfPkg / Library / XenConsoleSerialPortLib / XenConsoleSerialPortLib.c
1 /** @file
2 Xen console SerialPortLib instance
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 <Base.h>
17 #include <Uefi/UefiBaseType.h>
18
19 #include <Library/BaseLib.h>
20 #include <Library/SerialPortLib.h>
21 #include <Library/XenHypercallLib.h>
22
23 #include <IndustryStandard/Xen/io/console.h>
24 #include <IndustryStandard/Xen/hvm/params.h>
25 #include <IndustryStandard/Xen/event_channel.h>
26
27 //
28 // We can't use DebugLib due to a constructor dependency cycle between DebugLib
29 // and ourselves.
30 //
31 #define ASSERT(Expression) \
32 do { \
33 if (!(Expression)) { \
34 CpuDeadLoop (); \
35 } \
36 } while (FALSE)
37
38 //
39 // The code below expects these global variables to be mutable, even in the case
40 // that we have been incorporated into SEC or PEIM phase modules (which is
41 // allowed by our INF description). While this is a dangerous assumption to make
42 // in general, it is actually fine for the Xen domU (guest) environment that
43 // this module is intended for, as UEFI always executes from DRAM in that case.
44 //
45 STATIC evtchn_send_t mXenConsoleEventChain;
46 STATIC struct xencons_interface *mXenConsoleInterface;
47
48 /**
49 Initialize the serial device hardware.
50
51 If no initialization is required, then return RETURN_SUCCESS.
52 If the serial device was successfully initialized, then return RETURN_SUCCESS.
53 If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
54
55 @retval RETURN_SUCCESS The serial device was initialized.
56 @retval RETURN_DEVICE_ERROR The serial device could not be initialized.
57
58 **/
59 RETURN_STATUS
60 EFIAPI
61 SerialPortInitialize (
62 VOID
63 )
64 {
65 if (! XenHypercallIsAvailable ()) {
66 return RETURN_DEVICE_ERROR;
67 }
68
69 if (!mXenConsoleInterface) {
70 mXenConsoleEventChain.port = (UINT32)XenHypercallHvmGetParam (HVM_PARAM_CONSOLE_EVTCHN);
71 mXenConsoleInterface = (struct xencons_interface *)(UINTN)
72 (XenHypercallHvmGetParam (HVM_PARAM_CONSOLE_PFN) << EFI_PAGE_SHIFT);
73
74 //
75 // No point in ASSERT'ing here as we won't be seeing the output
76 //
77 }
78 return RETURN_SUCCESS;
79 }
80
81 /**
82 Write data from buffer to serial device.
83
84 Writes NumberOfBytes data bytes from Buffer to the serial device.
85 The number of bytes actually written to the serial device is returned.
86 If the return value is less than NumberOfBytes, then the write operation failed.
87 If Buffer is NULL, then ASSERT().
88 If NumberOfBytes is zero, then return 0.
89
90 @param Buffer Pointer to the data buffer to be written.
91 @param NumberOfBytes Number of bytes to written to the serial device.
92
93 @retval 0 NumberOfBytes is 0.
94 @retval >0 The number of bytes written to the serial device.
95 If this value is less than NumberOfBytes, then the write operation failed.
96
97 **/
98 UINTN
99 EFIAPI
100 SerialPortWrite (
101 IN UINT8 *Buffer,
102 IN UINTN NumberOfBytes
103 )
104 {
105 XENCONS_RING_IDX Consumer, Producer;
106 UINTN Sent;
107
108 ASSERT (Buffer != NULL);
109
110 if (NumberOfBytes == 0) {
111 return 0;
112 }
113
114 if (!mXenConsoleInterface) {
115 return 0;
116 }
117
118 Sent = 0;
119 do {
120 Consumer = mXenConsoleInterface->out_cons;
121 Producer = mXenConsoleInterface->out_prod;
122
123 MemoryFence ();
124
125 while (Sent < NumberOfBytes && ((Producer - Consumer) < sizeof (mXenConsoleInterface->out)))
126 mXenConsoleInterface->out[MASK_XENCONS_IDX(Producer++, mXenConsoleInterface->out)] = Buffer[Sent++];
127
128 MemoryFence ();
129
130 mXenConsoleInterface->out_prod = Producer;
131
132 XenHypercallEventChannelOp (EVTCHNOP_send, &mXenConsoleEventChain);
133
134 } while (Sent < NumberOfBytes);
135
136 return Sent;
137 }
138
139 /**
140 Read data from serial device and save the datas in buffer.
141
142 Reads NumberOfBytes data bytes from a serial device into the buffer
143 specified by Buffer. The number of bytes actually read is returned.
144 If Buffer is NULL, then ASSERT().
145 If NumberOfBytes is zero, then return 0.
146
147 @param Buffer Pointer to the data buffer to store the data read from the serial device.
148 @param NumberOfBytes Number of bytes which will be read.
149
150 @retval 0 Read data failed, no data is to be read.
151 @retval >0 Actual number of bytes read from serial device.
152
153 **/
154 UINTN
155 EFIAPI
156 SerialPortRead (
157 OUT UINT8 *Buffer,
158 IN UINTN NumberOfBytes
159 )
160 {
161 XENCONS_RING_IDX Consumer, Producer;
162 UINTN Received;
163
164 ASSERT (Buffer != NULL);
165
166 if (NumberOfBytes == 0) {
167 return 0;
168 }
169
170 if (!mXenConsoleInterface) {
171 return 0;
172 }
173
174 Consumer = mXenConsoleInterface->in_cons;
175 Producer = mXenConsoleInterface->in_prod;
176
177 MemoryFence ();
178
179 Received = 0;
180 while (Received < NumberOfBytes && Consumer < Producer)
181 Buffer[Received++] = mXenConsoleInterface->in[MASK_XENCONS_IDX(Consumer++, mXenConsoleInterface->in)];
182
183 MemoryFence ();
184
185 mXenConsoleInterface->in_cons = Consumer;
186
187 XenHypercallEventChannelOp (EVTCHNOP_send, &mXenConsoleEventChain);
188
189 return Received;
190 }
191
192 /**
193 Polls a serial device to see if there is any data waiting to be read.
194
195 @retval TRUE Data is waiting to be read from the serial device.
196 @retval FALSE There is no data waiting to be read from the serial device.
197
198 **/
199 BOOLEAN
200 EFIAPI
201 SerialPortPoll (
202 VOID
203 )
204 {
205 return mXenConsoleInterface &&
206 mXenConsoleInterface->in_cons != mXenConsoleInterface->in_prod;
207 }