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