2 Serial IO Abstraction for GDB stub. This allows an EFI consoles that shows up on the system
3 running GDB. One console for error information and another console for user input/output.
5 Basic packet format is $packet-data#checksum. So every command has 4 bytes of overhead: $,
6 #, 0, 0. The 0 and 0 are the ascii characters for the checksum.
9 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
10 Copyright (c) 2013, ARM Ltd. All rights reserved.<BR>
12 This program and the accompanying materials
13 are licensed and made available under the terms and conditions of the BSD License
14 which accompanies this distribution. The full text of the license may be found at
15 http://opensource.org/licenses/bsd-license.php
17 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
18 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
23 #include <Library/UefiLib.h>
24 #include <Library/UefiBootServicesTableLib.h>
25 #include <Library/DebugLib.h>
26 #include <Library/SerialPortLib.h>
27 #include <Library/SerialPortExtLib.h>
28 #include <Library/PcdLib.h>
30 #include <Protocol/SerialIo.h>
33 VENDOR_DEVICE_PATH Guid
;
34 UART_DEVICE_PATH Uart
;
35 EFI_DEVICE_PATH_PROTOCOL End
;
36 } SIMPLE_TEXT_OUT_DEVICE_PATH
;
38 SIMPLE_TEXT_OUT_DEVICE_PATH mDevicePath
= {
40 { HARDWARE_DEVICE_PATH
, HW_VENDOR_DP
, sizeof (VENDOR_DEVICE_PATH
), 0},
41 EFI_CALLER_ID_GUID
// Use the drivers GUID
44 { MESSAGING_DEVICE_PATH
, MSG_UART_DP
, sizeof (UART_DEVICE_PATH
), 0},
46 FixedPcdGet64 (PcdUartDefaultBaudRate
), // BaudRate
47 FixedPcdGet8 (PcdUartDefaultDataBits
), // DataBits
48 FixedPcdGet8 (PcdUartDefaultParity
), // Parity (N)
49 FixedPcdGet8 (PcdUartDefaultStopBits
) // StopBits
51 { END_DEVICE_PATH_TYPE
, END_ENTIRE_DEVICE_PATH_SUBTYPE
, sizeof (EFI_DEVICE_PATH_PROTOCOL
), 0}
54 EFI_HANDLE gHandle
= NULL
;
57 Reset the serial device.
59 @param This Protocol instance pointer.
61 @retval EFI_SUCCESS The device was reset.
62 @retval EFI_DEVICE_ERROR The serial device could not be reset.
68 IN EFI_SERIAL_IO_PROTOCOL
*This
74 Status
= SerialPortInitialize ();
75 if (EFI_ERROR(Status
)) {
80 // Set the Serial I/O mode and update the device path
83 Tpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
86 // Set the Serial I/O mode
88 This
->Mode
->ReceiveFifoDepth
= 0;
89 This
->Mode
->Timeout
= 1000000;
90 This
->Mode
->BaudRate
= PcdGet64 (PcdUartDefaultBaudRate
);
91 This
->Mode
->DataBits
= (UINT32
)PcdGet8 (PcdUartDefaultDataBits
);
92 This
->Mode
->Parity
= (UINT32
)PcdGet8 (PcdUartDefaultParity
);
93 This
->Mode
->StopBits
= (UINT32
)PcdGet8 (PcdUartDefaultStopBits
);
96 // Check if the device path has actually changed
98 if (mDevicePath
.Uart
.BaudRate
== This
->Mode
->BaudRate
&&
99 mDevicePath
.Uart
.DataBits
== (UINT8
)This
->Mode
->DataBits
&&
100 mDevicePath
.Uart
.Parity
== (UINT8
)This
->Mode
->Parity
&&
101 mDevicePath
.Uart
.StopBits
== (UINT8
)This
->Mode
->StopBits
103 gBS
->RestoreTPL (Tpl
);
108 // Update the device path
110 mDevicePath
.Uart
.BaudRate
= This
->Mode
->BaudRate
;
111 mDevicePath
.Uart
.DataBits
= (UINT8
)This
->Mode
->DataBits
;
112 mDevicePath
.Uart
.Parity
= (UINT8
)This
->Mode
->Parity
;
113 mDevicePath
.Uart
.StopBits
= (UINT8
)This
->Mode
->StopBits
;
115 Status
= gBS
->ReinstallProtocolInterface (
117 &gEfiDevicePathProtocolGuid
,
122 gBS
->RestoreTPL (Tpl
);
129 Sets the baud rate, receive FIFO depth, transmit/receive time out, parity,
130 data buts, and stop bits on a serial device.
132 @param This Protocol instance pointer.
133 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the the
134 device's default interface speed.
135 @param ReceiveFifoDepth The requested depth of the FIFO on the receive side of the
136 serial interface. A ReceiveFifoDepth value of 0 will use
137 the device's default FIFO depth.
138 @param Timeout The requested time out for a single character in microseconds.
139 This timeout applies to both the transmit and receive side of the
140 interface. A Timeout value of 0 will use the device's default time
142 @param Parity The type of parity to use on this serial device. A Parity value of
143 DefaultParity will use the device's default parity value.
144 @param DataBits The number of data bits to use on the serial device. A DataBits
145 value of 0 will use the device's default data bit setting.
146 @param StopBits The number of stop bits to use on this serial device. A StopBits
147 value of DefaultStopBits will use the device's default number of
150 @retval EFI_SUCCESS The device was reset.
151 @retval EFI_DEVICE_ERROR The serial device could not be reset.
156 SerialSetAttributes (
157 IN EFI_SERIAL_IO_PROTOCOL
*This
,
159 IN UINT32 ReceiveFifoDepth
,
161 IN EFI_PARITY_TYPE Parity
,
163 IN EFI_STOP_BITS_TYPE StopBits
169 Status
= SerialPortSetAttributes (&BaudRate
, &ReceiveFifoDepth
, &Timeout
, &Parity
, &DataBits
, &StopBits
);
170 if (EFI_ERROR(Status
)) {
175 // Set the Serial I/O mode and update the device path
178 Tpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
181 // Set the Serial I/O mode
183 This
->Mode
->BaudRate
= BaudRate
;
184 This
->Mode
->ReceiveFifoDepth
= ReceiveFifoDepth
;
185 This
->Mode
->Timeout
= Timeout
;
186 This
->Mode
->Parity
= (UINT32
)Parity
;
187 This
->Mode
->DataBits
= (UINT32
)DataBits
;
188 This
->Mode
->StopBits
= (UINT32
)StopBits
;
191 // Check if the device path has actually changed
193 if (mDevicePath
.Uart
.BaudRate
== BaudRate
&&
194 mDevicePath
.Uart
.Parity
== (UINT8
)Parity
&&
195 mDevicePath
.Uart
.DataBits
== DataBits
&&
196 mDevicePath
.Uart
.StopBits
== (UINT8
)StopBits
198 gBS
->RestoreTPL (Tpl
);
203 // Update the device path
205 mDevicePath
.Uart
.BaudRate
= BaudRate
;
206 mDevicePath
.Uart
.DataBits
= DataBits
;
207 mDevicePath
.Uart
.Parity
= (UINT8
) Parity
;
208 mDevicePath
.Uart
.StopBits
= (UINT8
) StopBits
;
210 Status
= gBS
->ReinstallProtocolInterface (
212 &gEfiDevicePathProtocolGuid
,
217 gBS
->RestoreTPL (Tpl
);
224 Set the control bits on a serial device
226 @param This Protocol instance pointer.
227 @param Control Set the bits of Control that are settable.
229 @retval EFI_SUCCESS The new control bits were set on the serial device.
230 @retval EFI_UNSUPPORTED The serial device does not support this operation.
231 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
237 IN EFI_SERIAL_IO_PROTOCOL
*This
,
241 return SerialPortSetControl(Control
);
246 Retrieves the status of the control bits on a serial device
248 @param This Protocol instance pointer.
249 @param Control A pointer to return the current Control signals from the serial device.
251 @retval EFI_SUCCESS The control bits were read from the serial device.
252 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
258 IN EFI_SERIAL_IO_PROTOCOL
*This
,
262 return SerialPortGetControl(Control
);
267 Writes data to a serial device.
269 @param This Protocol instance pointer.
270 @param BufferSize On input, the size of the Buffer. On output, the amount of
271 data actually written.
272 @param Buffer The buffer of data to write
274 @retval EFI_SUCCESS The data was written.
275 @retval EFI_DEVICE_ERROR The device reported an error.
276 @retval EFI_TIMEOUT The data write was stopped due to a timeout.
282 IN EFI_SERIAL_IO_PROTOCOL
*This
,
283 IN OUT UINTN
*BufferSize
,
289 Count
= SerialPortWrite (Buffer
, *BufferSize
);
291 if (Count
!= *BufferSize
) {
300 Reads data from a serial device.
302 @param This Protocol instance pointer.
303 @param BufferSize On input, the size of the Buffer. On output, the amount of
304 data returned in Buffer.
305 @param Buffer The buffer to return the data into.
307 @retval EFI_SUCCESS The data was read.
308 @retval EFI_DEVICE_ERROR The device reported an error.
309 @retval EFI_TIMEOUT The data write was stopped due to a timeout.
316 IN EFI_SERIAL_IO_PROTOCOL
*This
,
317 IN OUT UINTN
*BufferSize
,
323 if (SerialPortPoll()) {
324 Count
= SerialPortRead (Buffer
, *BufferSize
);
327 if (Count
!= *BufferSize
) {
336 // Template used to initialize the GDB Serial IO protocols
338 EFI_SERIAL_IO_MODE gSerialIoMode
= {
341 FixedPcdGet64 (PcdUartDefaultBaudRate
), // BaudRate
342 1, // ReceiveFifoDepth
343 FixedPcdGet8 (PcdUartDefaultDataBits
), // DataBits
344 FixedPcdGet8 (PcdUartDefaultParity
), // Parity
345 FixedPcdGet8 (PcdUartDefaultStopBits
) // StopBits
349 EFI_SERIAL_IO_PROTOCOL gSerialIoTemplate
= {
350 SERIAL_IO_INTERFACE_REVISION
,
361 Initialize the state information for the Serial Io Protocol
363 @param ImageHandle of the loaded driver
364 @param SystemTable Pointer to the System Table
366 @retval EFI_SUCCESS Protocol registered
367 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
368 @retval EFI_DEVICE_ERROR Hardware problems
373 SerialDxeInitialize (
374 IN EFI_HANDLE ImageHandle
,
375 IN EFI_SYSTEM_TABLE
*SystemTable
380 // Make a new handle with Serial IO protocol and its device path on it.
381 Status
= gBS
->InstallMultipleProtocolInterfaces (
383 &gEfiSerialIoProtocolGuid
, &gSerialIoTemplate
,
384 &gEfiDevicePathProtocolGuid
, &mDevicePath
,
387 ASSERT_EFI_ERROR (Status
);