AJFISH | 2ef2b01 | 2009-12-06 01:57:05 +0000 | [diff] [blame] | 1 | /** @file
|
| 2 | Serial IO Abstraction for GDB stub. This allows an EFI consoles that shows up on the system
|
oliviermartin | 7ca9e5a | 2011-08-08 18:29:14 +0000 | [diff] [blame] | 3 | running GDB. One console for error information and another console for user input/output.
|
AJFISH | 2ef2b01 | 2009-12-06 01:57:05 +0000 | [diff] [blame] | 4 |
|
oliviermartin | 7ca9e5a | 2011-08-08 18:29:14 +0000 | [diff] [blame] | 5 | Basic packet format is $packet-data#checksum. So every command has 4 bytes of overhead: $,
|
AJFISH | 2ef2b01 | 2009-12-06 01:57:05 +0000 | [diff] [blame] | 6 | #, 0, 0. The 0 and 0 are the ascii characters for the checksum.
|
| 7 |
|
| 8 |
|
hhtian | 60274cc | 2010-04-29 12:40:51 +0000 | [diff] [blame] | 9 | Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
|
AJFISH | 2ef2b01 | 2009-12-06 01:57:05 +0000 | [diff] [blame] | 10 |
|
hhtian | 60274cc | 2010-04-29 12:40:51 +0000 | [diff] [blame] | 11 | This program and the accompanying materials
|
AJFISH | 2ef2b01 | 2009-12-06 01:57:05 +0000 | [diff] [blame] | 12 | are licensed and made available under the terms and conditions of the BSD License
|
| 13 | which accompanies this distribution. The full text of the license may be found at
|
| 14 | http://opensource.org/licenses/bsd-license.php
|
| 15 |
|
| 16 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
|
| 17 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
| 18 |
|
| 19 | **/
|
| 20 |
|
| 21 | #include <PiDxe.h>
|
| 22 | #include <Library/UefiLib.h>
|
| 23 | #include <Library/UefiBootServicesTableLib.h>
|
| 24 | #include <Library/DebugLib.h>
|
| 25 | #include <Library/SerialPortLib.h>
|
oliviermartin | 7d49ced | 2012-09-28 10:56:12 +0000 | [diff] [blame^] | 26 | #include <Library/SerialPortExtLib.h>
|
andrewfish | 026e30c | 2010-02-15 20:40:51 +0000 | [diff] [blame] | 27 | #include <Library/PcdLib.h>
|
AJFISH | 2ef2b01 | 2009-12-06 01:57:05 +0000 | [diff] [blame] | 28 |
|
| 29 | #include <Protocol/SerialIo.h>
|
| 30 |
|
oliviermartin | 7d49ced | 2012-09-28 10:56:12 +0000 | [diff] [blame^] | 31 | typedef struct {
|
| 32 | VENDOR_DEVICE_PATH Guid;
|
| 33 | UART_DEVICE_PATH Uart;
|
| 34 | EFI_DEVICE_PATH_PROTOCOL End;
|
| 35 | } SIMPLE_TEXT_OUT_DEVICE_PATH;
|
| 36 |
|
| 37 | SIMPLE_TEXT_OUT_DEVICE_PATH mDevicePath = {
|
| 38 | {
|
| 39 | { HARDWARE_DEVICE_PATH, HW_VENDOR_DP, sizeof (VENDOR_DEVICE_PATH), 0},
|
| 40 | EFI_CALLER_ID_GUID // Use the drivers GUID
|
| 41 | },
|
| 42 | {
|
| 43 | { MESSAGING_DEVICE_PATH, MSG_UART_DP, sizeof (UART_DEVICE_PATH), 0},
|
| 44 | 0, // Reserved
|
| 45 | FixedPcdGet64 (PcdUartDefaultBaudRate), // BaudRate
|
| 46 | FixedPcdGet8 (PcdUartDefaultDataBits), // DataBits
|
| 47 | FixedPcdGet8 (PcdUartDefaultParity), // Parity (N)
|
| 48 | FixedPcdGet8 (PcdUartDefaultStopBits) // StopBits
|
| 49 | },
|
| 50 | { END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE, sizeof (EFI_DEVICE_PATH_PROTOCOL), 0}
|
| 51 | };
|
| 52 |
|
| 53 | EFI_HANDLE gHandle = NULL;
|
| 54 |
|
AJFISH | 2ef2b01 | 2009-12-06 01:57:05 +0000 | [diff] [blame] | 55 | /**
|
| 56 | Reset the serial device.
|
| 57 |
|
| 58 | @param This Protocol instance pointer.
|
| 59 |
|
| 60 | @retval EFI_SUCCESS The device was reset.
|
| 61 | @retval EFI_DEVICE_ERROR The serial device could not be reset.
|
| 62 |
|
| 63 | **/
|
| 64 | EFI_STATUS
|
| 65 | EFIAPI
|
| 66 | SerialReset (
|
| 67 | IN EFI_SERIAL_IO_PROTOCOL *This
|
| 68 | )
|
| 69 | {
|
oliviermartin | 7d49ced | 2012-09-28 10:56:12 +0000 | [diff] [blame^] | 70 | EFI_STATUS Status;
|
| 71 | EFI_TPL Tpl;
|
| 72 |
|
| 73 | Status = SerialPortInitialize ();
|
| 74 | if (EFI_ERROR(Status)) {
|
| 75 | return Status;
|
| 76 | }
|
| 77 |
|
| 78 | //
|
| 79 | // Set the Serial I/O mode and update the device path
|
| 80 | //
|
| 81 |
|
| 82 | Tpl = gBS->RaiseTPL (TPL_NOTIFY);
|
| 83 |
|
| 84 | //
|
| 85 | // Set the Serial I/O mode
|
| 86 | //
|
| 87 | This->Mode->ReceiveFifoDepth = 0;
|
| 88 | This->Mode->Timeout = 1000000;
|
| 89 | This->Mode->BaudRate = PcdGet64 (PcdUartDefaultBaudRate);
|
| 90 | This->Mode->DataBits = (UINT32)PcdGet8 (PcdUartDefaultDataBits);
|
| 91 | This->Mode->Parity = (UINT32)PcdGet8 (PcdUartDefaultParity);
|
| 92 | This->Mode->StopBits = (UINT32)PcdGet8 (PcdUartDefaultStopBits);
|
| 93 |
|
| 94 | //
|
| 95 | // Check if the device path has actually changed
|
| 96 | //
|
| 97 | if (mDevicePath.Uart.BaudRate == This->Mode->BaudRate &&
|
| 98 | mDevicePath.Uart.DataBits == (UINT8)This->Mode->DataBits &&
|
| 99 | mDevicePath.Uart.Parity == (UINT8)This->Mode->Parity &&
|
| 100 | mDevicePath.Uart.StopBits == (UINT8)This->Mode->StopBits
|
| 101 | ) {
|
| 102 | gBS->RestoreTPL (Tpl);
|
| 103 | return EFI_SUCCESS;
|
| 104 | }
|
| 105 |
|
| 106 | //
|
| 107 | // Update the device path
|
| 108 | //
|
| 109 | mDevicePath.Uart.BaudRate = This->Mode->BaudRate;
|
| 110 | mDevicePath.Uart.DataBits = (UINT8)This->Mode->DataBits;
|
| 111 | mDevicePath.Uart.Parity = (UINT8)This->Mode->Parity;
|
| 112 | mDevicePath.Uart.StopBits = (UINT8)This->Mode->StopBits;
|
| 113 |
|
| 114 | Status = gBS->ReinstallProtocolInterface (
|
| 115 | gHandle,
|
| 116 | &gEfiDevicePathProtocolGuid,
|
| 117 | &mDevicePath,
|
| 118 | &mDevicePath
|
| 119 | );
|
| 120 |
|
| 121 | gBS->RestoreTPL (Tpl);
|
| 122 |
|
| 123 | return Status;
|
AJFISH | 2ef2b01 | 2009-12-06 01:57:05 +0000 | [diff] [blame] | 124 | }
|
| 125 |
|
| 126 |
|
| 127 | /**
|
oliviermartin | 7ca9e5a | 2011-08-08 18:29:14 +0000 | [diff] [blame] | 128 | Sets the baud rate, receive FIFO depth, transmit/receive time out, parity,
|
AJFISH | 2ef2b01 | 2009-12-06 01:57:05 +0000 | [diff] [blame] | 129 | data buts, and stop bits on a serial device.
|
| 130 |
|
| 131 | @param This Protocol instance pointer.
|
| 132 | @param BaudRate The requested baud rate. A BaudRate value of 0 will use the the
|
| 133 | device's default interface speed.
|
oliviermartin | 7ca9e5a | 2011-08-08 18:29:14 +0000 | [diff] [blame] | 134 | @param ReceiveFifoDepth The requested depth of the FIFO on the receive side of the
|
AJFISH | 2ef2b01 | 2009-12-06 01:57:05 +0000 | [diff] [blame] | 135 | serial interface. A ReceiveFifoDepth value of 0 will use
|
oliviermartin | 7ca9e5a | 2011-08-08 18:29:14 +0000 | [diff] [blame] | 136 | the device's default FIFO depth.
|
AJFISH | 2ef2b01 | 2009-12-06 01:57:05 +0000 | [diff] [blame] | 137 | @param Timeout The requested time out for a single character in microseconds.
|
| 138 | This timeout applies to both the transmit and receive side of the
|
| 139 | interface. A Timeout value of 0 will use the device's default time
|
| 140 | out value.
|
| 141 | @param Parity The type of parity to use on this serial device. A Parity value of
|
| 142 | DefaultParity will use the device's default parity value.
|
| 143 | @param DataBits The number of data bits to use on the serial device. A DataBits
|
oliviermartin | 7ca9e5a | 2011-08-08 18:29:14 +0000 | [diff] [blame] | 144 | value of 0 will use the device's default data bit setting.
|
AJFISH | 2ef2b01 | 2009-12-06 01:57:05 +0000 | [diff] [blame] | 145 | @param StopBits The number of stop bits to use on this serial device. A StopBits
|
| 146 | value of DefaultStopBits will use the device's default number of
|
| 147 | stop bits.
|
| 148 |
|
| 149 | @retval EFI_SUCCESS The device was reset.
|
| 150 | @retval EFI_DEVICE_ERROR The serial device could not be reset.
|
| 151 |
|
| 152 | **/
|
| 153 | EFI_STATUS
|
| 154 | EFIAPI
|
| 155 | SerialSetAttributes (
|
| 156 | IN EFI_SERIAL_IO_PROTOCOL *This,
|
| 157 | IN UINT64 BaudRate,
|
| 158 | IN UINT32 ReceiveFifoDepth,
|
| 159 | IN UINT32 Timeout,
|
| 160 | IN EFI_PARITY_TYPE Parity,
|
| 161 | IN UINT8 DataBits,
|
| 162 | IN EFI_STOP_BITS_TYPE StopBits
|
| 163 | )
|
| 164 | {
|
oliviermartin | 7d49ced | 2012-09-28 10:56:12 +0000 | [diff] [blame^] | 165 | EFI_STATUS Status;
|
| 166 | EFI_TPL Tpl;
|
| 167 |
|
| 168 | Status = SerialPortSetAttributes (BaudRate, ReceiveFifoDepth, Timeout, Parity, DataBits, StopBits);
|
| 169 | if (EFI_ERROR(Status)) {
|
| 170 | return Status;
|
| 171 | }
|
| 172 |
|
| 173 | //
|
| 174 | // Set the Serial I/O mode and update the device path
|
| 175 | //
|
| 176 |
|
| 177 | Tpl = gBS->RaiseTPL (TPL_NOTIFY);
|
| 178 |
|
| 179 | //
|
| 180 | // Set the Serial I/O mode
|
| 181 | //
|
| 182 | This->Mode->BaudRate = BaudRate;
|
| 183 | This->Mode->ReceiveFifoDepth = ReceiveFifoDepth;
|
| 184 | This->Mode->Timeout = Timeout;
|
| 185 | This->Mode->Parity = (UINT32)Parity;
|
| 186 | This->Mode->DataBits = (UINT32)DataBits;
|
| 187 | This->Mode->StopBits = (UINT32)StopBits;
|
| 188 |
|
| 189 | //
|
| 190 | // Check if the device path has actually changed
|
| 191 | //
|
| 192 | if (mDevicePath.Uart.BaudRate == BaudRate &&
|
| 193 | mDevicePath.Uart.Parity == (UINT8)Parity &&
|
| 194 | mDevicePath.Uart.DataBits == DataBits &&
|
| 195 | mDevicePath.Uart.StopBits == (UINT8)StopBits
|
| 196 | ) {
|
| 197 | gBS->RestoreTPL (Tpl);
|
| 198 | return EFI_SUCCESS;
|
| 199 | }
|
| 200 |
|
| 201 | //
|
| 202 | // Update the device path
|
| 203 | //
|
| 204 | mDevicePath.Uart.BaudRate = BaudRate;
|
| 205 | mDevicePath.Uart.DataBits = DataBits;
|
| 206 | mDevicePath.Uart.Parity = (UINT8) Parity;
|
| 207 | mDevicePath.Uart.StopBits = (UINT8) StopBits;
|
| 208 |
|
| 209 | Status = gBS->ReinstallProtocolInterface (
|
| 210 | gHandle,
|
| 211 | &gEfiDevicePathProtocolGuid,
|
| 212 | &mDevicePath,
|
| 213 | &mDevicePath
|
| 214 | );
|
| 215 |
|
| 216 | gBS->RestoreTPL (Tpl);
|
| 217 |
|
| 218 | return Status;
|
AJFISH | 2ef2b01 | 2009-12-06 01:57:05 +0000 | [diff] [blame] | 219 | }
|
| 220 |
|
| 221 |
|
| 222 | /**
|
| 223 | Set the control bits on a serial device
|
| 224 |
|
| 225 | @param This Protocol instance pointer.
|
| 226 | @param Control Set the bits of Control that are settable.
|
| 227 |
|
| 228 | @retval EFI_SUCCESS The new control bits were set on the serial device.
|
| 229 | @retval EFI_UNSUPPORTED The serial device does not support this operation.
|
| 230 | @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
|
| 231 |
|
| 232 | **/
|
| 233 | EFI_STATUS
|
| 234 | EFIAPI
|
| 235 | SerialSetControl (
|
| 236 | IN EFI_SERIAL_IO_PROTOCOL *This,
|
| 237 | IN UINT32 Control
|
| 238 | )
|
| 239 | {
|
oliviermartin | 7d49ced | 2012-09-28 10:56:12 +0000 | [diff] [blame^] | 240 | return SerialPortSetControl(Control);
|
AJFISH | 2ef2b01 | 2009-12-06 01:57:05 +0000 | [diff] [blame] | 241 | }
|
| 242 |
|
| 243 |
|
| 244 | /**
|
oliviermartin | 7ca9e5a | 2011-08-08 18:29:14 +0000 | [diff] [blame] | 245 | Retrieves the status of the control bits on a serial device
|
AJFISH | 2ef2b01 | 2009-12-06 01:57:05 +0000 | [diff] [blame] | 246 |
|
| 247 | @param This Protocol instance pointer.
|
| 248 | @param Control A pointer to return the current Control signals from the serial device.
|
| 249 |
|
| 250 | @retval EFI_SUCCESS The control bits were read from the serial device.
|
| 251 | @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
|
| 252 |
|
| 253 | **/
|
| 254 | EFI_STATUS
|
| 255 | EFIAPI
|
| 256 | SerialGetControl (
|
| 257 | IN EFI_SERIAL_IO_PROTOCOL *This,
|
| 258 | OUT UINT32 *Control
|
| 259 | )
|
| 260 | {
|
oliviermartin | 7d49ced | 2012-09-28 10:56:12 +0000 | [diff] [blame^] | 261 | return SerialPortGetControl(Control);
|
AJFISH | 2ef2b01 | 2009-12-06 01:57:05 +0000 | [diff] [blame] | 262 | }
|
| 263 |
|
| 264 |
|
| 265 | /**
|
| 266 | Writes data to a serial device.
|
| 267 |
|
| 268 | @param This Protocol instance pointer.
|
| 269 | @param BufferSize On input, the size of the Buffer. On output, the amount of
|
| 270 | data actually written.
|
| 271 | @param Buffer The buffer of data to write
|
| 272 |
|
| 273 | @retval EFI_SUCCESS The data was written.
|
| 274 | @retval EFI_DEVICE_ERROR The device reported an error.
|
| 275 | @retval EFI_TIMEOUT The data write was stopped due to a timeout.
|
| 276 |
|
| 277 | **/
|
| 278 | EFI_STATUS
|
| 279 | EFIAPI
|
| 280 | SerialWrite (
|
| 281 | IN EFI_SERIAL_IO_PROTOCOL *This,
|
| 282 | IN OUT UINTN *BufferSize,
|
| 283 | IN VOID *Buffer
|
| 284 | )
|
| 285 | {
|
| 286 | UINTN Count;
|
| 287 |
|
| 288 | Count = SerialPortWrite (Buffer, *BufferSize);
|
oliviermartin | 7d49ced | 2012-09-28 10:56:12 +0000 | [diff] [blame^] | 289 |
|
| 290 | if (Count != *BufferSize) {
|
| 291 | *BufferSize = Count;
|
| 292 | return EFI_TIMEOUT;
|
| 293 | }
|
| 294 |
|
| 295 | return EFI_SUCCESS;
|
AJFISH | 2ef2b01 | 2009-12-06 01:57:05 +0000 | [diff] [blame] | 296 | }
|
| 297 |
|
| 298 | /**
|
oliviermartin | 40ab42d | 2012-05-02 20:04:42 +0000 | [diff] [blame] | 299 | Reads data from a serial device.
|
AJFISH | 2ef2b01 | 2009-12-06 01:57:05 +0000 | [diff] [blame] | 300 |
|
| 301 | @param This Protocol instance pointer.
|
| 302 | @param BufferSize On input, the size of the Buffer. On output, the amount of
|
| 303 | data returned in Buffer.
|
| 304 | @param Buffer The buffer to return the data into.
|
| 305 |
|
| 306 | @retval EFI_SUCCESS The data was read.
|
| 307 | @retval EFI_DEVICE_ERROR The device reported an error.
|
| 308 | @retval EFI_TIMEOUT The data write was stopped due to a timeout.
|
| 309 |
|
| 310 | **/
|
| 311 |
|
| 312 | EFI_STATUS
|
| 313 | EFIAPI
|
| 314 | SerialRead (
|
| 315 | IN EFI_SERIAL_IO_PROTOCOL *This,
|
| 316 | IN OUT UINTN *BufferSize,
|
| 317 | OUT VOID *Buffer
|
| 318 | )
|
| 319 | {
|
oliviermartin | 010bb3d | 2011-06-03 09:14:16 +0000 | [diff] [blame] | 320 | UINTN Count = 0;
|
oliviermartin | 40ab42d | 2012-05-02 20:04:42 +0000 | [diff] [blame] | 321 |
|
oliviermartin | 010bb3d | 2011-06-03 09:14:16 +0000 | [diff] [blame] | 322 | if (SerialPortPoll()) {
|
| 323 | Count = SerialPortRead (Buffer, *BufferSize);
|
oliviermartin | 010bb3d | 2011-06-03 09:14:16 +0000 | [diff] [blame] | 324 | }
|
oliviermartin | 40ab42d | 2012-05-02 20:04:42 +0000 | [diff] [blame] | 325 |
|
| 326 | if (Count != *BufferSize) {
|
| 327 | *BufferSize = Count;
|
| 328 | return EFI_TIMEOUT;
|
| 329 | }
|
| 330 |
|
andrewfish | f65dc3b | 2011-06-08 21:12:11 +0000 | [diff] [blame] | 331 | return EFI_SUCCESS;
|
AJFISH | 2ef2b01 | 2009-12-06 01:57:05 +0000 | [diff] [blame] | 332 | }
|
| 333 |
|
AJFISH | 2ef2b01 | 2009-12-06 01:57:05 +0000 | [diff] [blame] | 334 | //
|
oliviermartin | 7ca9e5a | 2011-08-08 18:29:14 +0000 | [diff] [blame] | 335 | // Template used to initialize the GDB Serial IO protocols
|
AJFISH | 2ef2b01 | 2009-12-06 01:57:05 +0000 | [diff] [blame] | 336 | //
|
| 337 | EFI_SERIAL_IO_MODE gSerialIoMode = {
|
andrewfish | 026e30c | 2010-02-15 20:40:51 +0000 | [diff] [blame] | 338 | 0, // ControlMask
|
| 339 | 0, // Timeout
|
| 340 | FixedPcdGet64 (PcdUartDefaultBaudRate), // BaudRate
|
oliviermartin | 7ca9e5a | 2011-08-08 18:29:14 +0000 | [diff] [blame] | 341 | 1, // ReceiveFifoDepth
|
andrewfish | 026e30c | 2010-02-15 20:40:51 +0000 | [diff] [blame] | 342 | FixedPcdGet8 (PcdUartDefaultDataBits), // DataBits
|
| 343 | FixedPcdGet8 (PcdUartDefaultParity), // Parity
|
| 344 | FixedPcdGet8 (PcdUartDefaultStopBits) // StopBits
|
AJFISH | 2ef2b01 | 2009-12-06 01:57:05 +0000 | [diff] [blame] | 345 | };
|
| 346 |
|
| 347 |
|
| 348 | EFI_SERIAL_IO_PROTOCOL gSerialIoTemplate = {
|
| 349 | SERIAL_IO_INTERFACE_REVISION,
|
| 350 | SerialReset,
|
| 351 | SerialSetAttributes,
|
| 352 | SerialSetControl,
|
| 353 | SerialGetControl,
|
| 354 | SerialWrite,
|
| 355 | SerialRead,
|
| 356 | &gSerialIoMode
|
| 357 | };
|
AJFISH | 2ef2b01 | 2009-12-06 01:57:05 +0000 | [diff] [blame] | 358 |
|
| 359 | /**
|
| 360 | Initialize the state information for the Serial Io Protocol
|
| 361 |
|
| 362 | @param ImageHandle of the loaded driver
|
| 363 | @param SystemTable Pointer to the System Table
|
| 364 |
|
| 365 | @retval EFI_SUCCESS Protocol registered
|
| 366 | @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
|
| 367 | @retval EFI_DEVICE_ERROR Hardware problems
|
| 368 |
|
| 369 | **/
|
| 370 | EFI_STATUS
|
| 371 | EFIAPI
|
| 372 | SerialDxeInitialize (
|
| 373 | IN EFI_HANDLE ImageHandle,
|
| 374 | IN EFI_SYSTEM_TABLE *SystemTable
|
| 375 | )
|
| 376 | {
|
| 377 | EFI_STATUS Status;
|
| 378 |
|
AJFISH | 2ef2b01 | 2009-12-06 01:57:05 +0000 | [diff] [blame] | 379 | // Make a new handle with Serial IO protocol and its device path on it.
|
| 380 | Status = gBS->InstallMultipleProtocolInterfaces (
|
| 381 | &gHandle,
|
| 382 | &gEfiSerialIoProtocolGuid, &gSerialIoTemplate,
|
andrewfish | 026e30c | 2010-02-15 20:40:51 +0000 | [diff] [blame] | 383 | &gEfiDevicePathProtocolGuid, &mDevicePath,
|
AJFISH | 2ef2b01 | 2009-12-06 01:57:05 +0000 | [diff] [blame] | 384 | NULL
|
| 385 | );
|
| 386 | ASSERT_EFI_ERROR (Status);
|
| 387 |
|
| 388 | return Status;
|
| 389 | }
|
| 390 |
|