1 /** @file
2   Serial Port Lib that thunks back to Emulator services to write to StdErr.
3   All read functions are stubed out.
4 
5   Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
6   Portions copyright (c) 2011, Apple Inc. All rights reserved.<BR>
7   This program and the accompanying materials
8   are licensed and made available under the terms and conditions of the BSD License
9   which accompanies this distribution.  The full text of the license may be found at
10   http://opensource.org/licenses/bsd-license.php.
11 
12   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 
15 **/
16 
17 
18 #include <PiDxe.h>
19 #include <Library/SerialPortLib.h>
20 #include <Library/EmuThunkLib.h>
21 
22 
23 
24 
25 /**
26   Initialize the serial device hardware.
27 
28   If no initialization is required, then return RETURN_SUCCESS.
29   If the serial device was successfully initialized, then return RETURN_SUCCESS.
30   If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
31 
32   @retval RETURN_SUCCESS        The serial device was initialized.
33   @retval RETURN_DEVICE_ERROR   The serial device could not be initialized.
34 
35 **/
36 RETURN_STATUS
37 EFIAPI
SerialPortInitialize(VOID)38 SerialPortInitialize (
39   VOID
40   )
41 {
42   return gEmuThunk->ConfigStdIn ();
43 }
44 
45 /**
46   Write data from buffer to serial device.
47 
48   Writes NumberOfBytes data bytes from Buffer to the serial device.
49   The number of bytes actually written to the serial device is returned.
50   If the return value is less than NumberOfBytes, then the write operation failed.
51   If Buffer is NULL, then ASSERT().
52   If NumberOfBytes is zero, then return 0.
53 
54   @param  Buffer           The pointer to the data buffer to be written.
55   @param  NumberOfBytes    The number of bytes to written to the serial device.
56 
57   @retval 0                NumberOfBytes is 0.
58   @retval >0               The number of bytes written to the serial device.
59                            If this value is less than NumberOfBytes, then the read operation failed.
60 
61 **/
62 UINTN
63 EFIAPI
SerialPortWrite(IN UINT8 * Buffer,IN UINTN NumberOfBytes)64 SerialPortWrite (
65   IN UINT8     *Buffer,
66   IN UINTN     NumberOfBytes
67   )
68 {
69   return gEmuThunk->WriteStdOut (Buffer, NumberOfBytes);
70 }
71 
72 
73 /**
74   Read data from serial device and save the datas in buffer.
75 
76   Reads NumberOfBytes data bytes from a serial device into the buffer
77   specified by Buffer. The number of bytes actually read is returned.
78   If the return value is less than NumberOfBytes, then the rest operation failed.
79   If Buffer is NULL, then ASSERT().
80   If NumberOfBytes is zero, then return 0.
81 
82   @param  Buffer           The pointer to the data buffer to store the data read from the serial device.
83   @param  NumberOfBytes    The number of bytes which will be read.
84 
85   @retval 0                Read data failed; No data is to be read.
86   @retval >0               The actual number of bytes read from serial device.
87 
88 **/
89 UINTN
90 EFIAPI
SerialPortRead(OUT UINT8 * Buffer,IN UINTN NumberOfBytes)91 SerialPortRead (
92   OUT UINT8     *Buffer,
93   IN  UINTN     NumberOfBytes
94   )
95 {
96   return gEmuThunk->ReadStdIn (Buffer, NumberOfBytes);
97 }
98 
99 /**
100   Polls a serial device to see if there is any data waiting to be read.
101 
102   Polls a serial device to see if there is any data waiting to be read.
103   If there is data waiting to be read from the serial device, then TRUE is returned.
104   If there is no data waiting to be read from the serial device, then FALSE is returned.
105 
106   @retval TRUE             Data is waiting to be read from the serial device.
107   @retval FALSE            There is no data waiting to be read from the serial device.
108 
109 **/
110 BOOLEAN
111 EFIAPI
SerialPortPoll(VOID)112 SerialPortPoll (
113   VOID
114   )
115 {
116   return gEmuThunk->PollStdIn ();
117 }
118 
119 /**
120   Sets the control bits on a serial device.
121 
122   @param Control                Sets the bits of Control that are settable.
123 
124   @retval RETURN_SUCCESS        The new control bits were set on the serial device.
125   @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
126   @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
127 
128 **/
129 RETURN_STATUS
130 EFIAPI
SerialPortSetControl(IN UINT32 Control)131 SerialPortSetControl (
132   IN UINT32 Control
133   )
134 {
135   return RETURN_UNSUPPORTED;
136 }
137 
138 /**
139   Retrieve the status of the control bits on a serial device.
140 
141   @param Control                A pointer to return the current control signals from the serial device.
142 
143   @retval RETURN_SUCCESS        The control bits were read from the serial device.
144   @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
145   @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
146 
147 **/
148 RETURN_STATUS
149 EFIAPI
SerialPortGetControl(OUT UINT32 * Control)150 SerialPortGetControl (
151   OUT UINT32 *Control
152   )
153 {
154   *Control = 0;
155   if (!SerialPortPoll ()) {
156     *Control = EFI_SERIAL_INPUT_BUFFER_EMPTY;
157   }
158   return RETURN_SUCCESS;
159 }
160 
161 /**
162   Sets the baud rate, receive FIFO depth, transmit/receice time out, parity,
163   data bits, and stop bits on a serial device.
164 
165   @param BaudRate           The requested baud rate. A BaudRate value of 0 will use the
166                             device's default interface speed.
167                             On output, the value actually set.
168   @param ReveiveFifoDepth   The requested depth of the FIFO on the receive side of the
169                             serial interface. A ReceiveFifoDepth value of 0 will use
170                             the device's default FIFO depth.
171                             On output, the value actually set.
172   @param Timeout            The requested time out for a single character in microseconds.
173                             This timeout applies to both the transmit and receive side of the
174                             interface. A Timeout value of 0 will use the device's default time
175                             out value.
176                             On output, the value actually set.
177   @param Parity             The type of parity to use on this serial device. A Parity value of
178                             DefaultParity will use the device's default parity value.
179                             On output, the value actually set.
180   @param DataBits           The number of data bits to use on the serial device. A DataBits
181                             vaule of 0 will use the device's default data bit setting.
182                             On output, the value actually set.
183   @param StopBits           The number of stop bits to use on this serial device. A StopBits
184                             value of DefaultStopBits will use the device's default number of
185                             stop bits.
186                             On output, the value actually set.
187 
188   @retval RETURN_SUCCESS            The new attributes were set on the serial device.
189   @retval RETURN_UNSUPPORTED        The serial device does not support this operation.
190   @retval RETURN_INVALID_PARAMETER  One or more of the attributes has an unsupported value.
191   @retval RETURN_DEVICE_ERROR       The serial device is not functioning correctly.
192 
193 **/
194 RETURN_STATUS
195 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)196 SerialPortSetAttributes (
197   IN OUT UINT64             *BaudRate,
198   IN OUT UINT32             *ReceiveFifoDepth,
199   IN OUT UINT32             *Timeout,
200   IN OUT EFI_PARITY_TYPE    *Parity,
201   IN OUT UINT8              *DataBits,
202   IN OUT EFI_STOP_BITS_TYPE *StopBits
203   )
204 {
205   return RETURN_UNSUPPORTED;
206 }
207 
208