2 Xen console SerialPortLib instance
4 Copyright (c) 2015, Linaro Ltd. All rights reserved.<BR>
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
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.
17 #include <Uefi/UefiBaseType.h>
19 #include <Library/BaseLib.h>
20 #include <Library/SerialPortLib.h>
21 #include <Library/XenHypercallLib.h>
23 #include <IndustryStandard/Xen/io/console.h>
24 #include <IndustryStandard/Xen/hvm/params.h>
25 #include <IndustryStandard/Xen/event_channel.h>
28 // We can't use DebugLib due to a constructor dependency cycle between DebugLib
31 #define ASSERT(Expression) \
33 if (!(Expression)) { \
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.
45 STATIC evtchn_send_t mXenConsoleEventChain
;
46 STATIC
struct xencons_interface
*mXenConsoleInterface
;
49 Initialize the serial device hardware.
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.
55 @retval RETURN_SUCCESS The serial device was initialized.
56 @retval RETURN_DEVICE_ERROR The serial device could not be initialized.
61 SerialPortInitialize (
65 if (! XenHypercallIsAvailable ()) {
66 return RETURN_DEVICE_ERROR
;
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
);
75 // No point in ASSERT'ing here as we won't be seeing the output
78 return RETURN_SUCCESS
;
82 Write data from buffer to serial device.
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.
90 @param Buffer Pointer to the data buffer to be written.
91 @param NumberOfBytes Number of bytes to written to the serial device.
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.
102 IN UINTN NumberOfBytes
105 XENCONS_RING_IDX Consumer
, Producer
;
108 ASSERT (Buffer
!= NULL
);
110 if (NumberOfBytes
== 0) {
114 if (!mXenConsoleInterface
) {
120 Consumer
= mXenConsoleInterface
->out_cons
;
121 Producer
= mXenConsoleInterface
->out_prod
;
125 while (Sent
< NumberOfBytes
&& ((Producer
- Consumer
) < sizeof (mXenConsoleInterface
->out
)))
126 mXenConsoleInterface
->out
[MASK_XENCONS_IDX(Producer
++, mXenConsoleInterface
->out
)] = Buffer
[Sent
++];
130 mXenConsoleInterface
->out_prod
= Producer
;
132 XenHypercallEventChannelOp (EVTCHNOP_send
, &mXenConsoleEventChain
);
134 } while (Sent
< NumberOfBytes
);
140 Read data from serial device and save the datas in buffer.
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.
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.
150 @retval 0 Read data failed, no data is to be read.
151 @retval >0 Actual number of bytes read from serial device.
158 IN UINTN NumberOfBytes
161 XENCONS_RING_IDX Consumer
, Producer
;
164 ASSERT (Buffer
!= NULL
);
166 if (NumberOfBytes
== 0) {
170 if (!mXenConsoleInterface
) {
174 Consumer
= mXenConsoleInterface
->in_cons
;
175 Producer
= mXenConsoleInterface
->in_prod
;
180 while (Received
< NumberOfBytes
&& Consumer
< Producer
)
181 Buffer
[Received
++] = mXenConsoleInterface
->in
[MASK_XENCONS_IDX(Consumer
++, mXenConsoleInterface
->in
)];
185 mXenConsoleInterface
->in_cons
= Consumer
;
187 XenHypercallEventChannelOp (EVTCHNOP_send
, &mXenConsoleEventChain
);
193 Polls a serial device to see if there is any data waiting to be read.
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.
205 return mXenConsoleInterface
&&
206 mXenConsoleInterface
->in_cons
!= mXenConsoleInterface
->in_prod
;