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 // The code below expects these global variables to be mutable, even in the case
29 // that we have been incorporated into SEC or PEIM phase modules (which is
30 // allowed by our INF description). While this is a dangerous assumption to make
31 // in general, it is actually fine for the Xen domU (guest) environment that
32 // this module is intended for, as UEFI always executes from DRAM in that case.
34 STATIC evtchn_send_t mXenConsoleEventChain
;
35 STATIC
struct xencons_interface
*mXenConsoleInterface
;
39 SerialPortInitialize (
43 if (!mXenConsoleInterface
) {
44 mXenConsoleEventChain
.port
= (UINT32
)XenHypercallHvmGetParam (HVM_PARAM_CONSOLE_EVTCHN
);
45 mXenConsoleInterface
= (struct xencons_interface
*)(UINTN
)
46 (XenHypercallHvmGetParam (HVM_PARAM_CONSOLE_PFN
) << EFI_PAGE_SHIFT
);
49 // No point in ASSERT'ing here as we won't be seeing the output
52 return RETURN_SUCCESS
;
56 Write data to serial device.
58 @param Buffer Point of data buffer which need to be written.
59 @param NumberOfBytes Number of output bytes which are cached in Buffer.
61 @retval 0 Write data failed.
62 @retval !0 Actual number of bytes written to serial device.
69 IN UINTN NumberOfBytes
72 XENCONS_RING_IDX Consumer
, Producer
;
75 if (!mXenConsoleInterface
) {
79 Consumer
= mXenConsoleInterface
->out_cons
;
80 Producer
= mXenConsoleInterface
->out_prod
;
85 while (Sent
< NumberOfBytes
&& ((Producer
- Consumer
) < sizeof (mXenConsoleInterface
->out
)))
86 mXenConsoleInterface
->out
[MASK_XENCONS_IDX(Producer
++, mXenConsoleInterface
->out
)] = Buffer
[Sent
++];
90 mXenConsoleInterface
->out_prod
= Producer
;
93 XenHypercallEventChannelOp (EVTCHNOP_send
, &mXenConsoleEventChain
);
100 Read data from serial device and save the data in buffer.
102 @param Buffer Point of data buffer which need to be written.
103 @param NumberOfBytes Size of Buffer[].
105 @retval 0 Read data failed.
106 @retval !0 Actual number of bytes read from serial device.
113 IN UINTN NumberOfBytes
116 XENCONS_RING_IDX Consumer
, Producer
;
119 if (!mXenConsoleInterface
) {
123 Consumer
= mXenConsoleInterface
->in_cons
;
124 Producer
= mXenConsoleInterface
->in_prod
;
129 while (Received
< NumberOfBytes
&& Consumer
< Producer
)
130 Buffer
[Received
++] = mXenConsoleInterface
->in
[MASK_XENCONS_IDX(Consumer
++, mXenConsoleInterface
->in
)];
134 mXenConsoleInterface
->in_cons
= Consumer
;
136 XenHypercallEventChannelOp (EVTCHNOP_send
, &mXenConsoleEventChain
);
142 Check to see if any data is available to be read from the debug device.
144 @retval TRUE At least one byte of data is available to be read
145 @retval FALSE No data is available to be read
154 return mXenConsoleInterface
&&
155 mXenConsoleInterface
->in_cons
!= mXenConsoleInterface
->in_prod
;