GdbSerialDebugPortLib.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /** @file
  2. Basic serial IO abstraction for GDB
  3. Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <Uefi.h>
  7. #include <Library/GdbSerialLib.h>
  8. #include <Library/PcdLib.h>
  9. #include <Library/IoLib.h>
  10. #include <Library/DebugLib.h>
  11. #include <Library/UefiBootServicesTableLib.h>
  12. #include <Protocol/DebugPort.h>
  13. EFI_DEBUGPORT_PROTOCOL *gDebugPort = NULL;
  14. UINTN gTimeOut = 0;
  15. /**
  16. The constructor function initializes the UART.
  17. @param ImageHandle The firmware allocated handle for the EFI image.
  18. @param SystemTable A pointer to the EFI System Table.
  19. @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
  20. **/
  21. RETURN_STATUS
  22. EFIAPI
  23. GdbSerialLibDebugPortConstructor (
  24. IN EFI_HANDLE ImageHandle,
  25. IN EFI_SYSTEM_TABLE *SystemTable
  26. )
  27. {
  28. EFI_STATUS Status;
  29. Status = gBS->LocateProtocol (&gEfiDebugPortProtocolGuid, NULL, (VOID **)&gDebugPort);
  30. if (!EFI_ERROR (Status)) {
  31. gTimeOut = PcdGet32 (PcdGdbMaxPacketRetryCount);
  32. gDebugPort->Reset (gDebugPort);
  33. }
  34. return Status;
  35. }
  36. /**
  37. Sets the baud rate, receive FIFO depth, transmit/receive time out, parity,
  38. data buts, and stop bits on a serial device. This call is optional as the serial
  39. port will be set up with defaults base on PCD values.
  40. @param BaudRate The requested baud rate. A BaudRate value of 0 will use the the
  41. device's default interface speed.
  42. @param Parity The type of parity to use on this serial device. A Parity value of
  43. DefaultParity will use the device's default parity value.
  44. @param DataBits The number of data bits to use on the serial device. A DataBits
  45. value of 0 will use the device's default data bit setting.
  46. @param StopBits The number of stop bits to use on this serial device. A StopBits
  47. value of DefaultStopBits will use the device's default number of
  48. stop bits.
  49. @retval EFI_SUCCESS The device was configured.
  50. @retval EFI_DEVICE_ERROR The serial device could not be configured.
  51. **/
  52. RETURN_STATUS
  53. EFIAPI
  54. GdbSerialInit (
  55. IN UINT64 BaudRate,
  56. IN UINT8 Parity,
  57. IN UINT8 DataBits,
  58. IN UINT8 StopBits
  59. )
  60. {
  61. EFI_STATUS Status;
  62. Status = gDebugPort->Reset (gDebugPort);
  63. return Status;
  64. }
  65. /**
  66. Check to see if a character is available from GDB. Do not read the character as that is
  67. done via GdbGetChar().
  68. @return TRUE - Character available
  69. @return FALSE - Character not available
  70. **/
  71. BOOLEAN
  72. EFIAPI
  73. GdbIsCharAvailable (
  74. VOID
  75. )
  76. {
  77. EFI_STATUS Status;
  78. Status = gDebugPort->Poll (gDebugPort);
  79. return (Status == EFI_SUCCESS ? TRUE : FALSE);
  80. }
  81. /**
  82. Get a character from GDB. This function must be able to run in interrupt context.
  83. @return A character from GDB
  84. **/
  85. CHAR8
  86. EFIAPI
  87. GdbGetChar (
  88. VOID
  89. )
  90. {
  91. EFI_STATUS Status;
  92. CHAR8 Char;
  93. UINTN BufferSize;
  94. do {
  95. BufferSize = sizeof (Char);
  96. Status = gDebugPort->Read (gDebugPort, gTimeOut, &BufferSize, &Char);
  97. } while (EFI_ERROR (Status) || BufferSize != sizeof (Char));
  98. return Char;
  99. }
  100. /**
  101. Send a character to GDB. This function must be able to run in interrupt context.
  102. @param Char Send a character to GDB
  103. **/
  104. VOID
  105. EFIAPI
  106. GdbPutChar (
  107. IN CHAR8 Char
  108. )
  109. {
  110. EFI_STATUS Status;
  111. UINTN BufferSize;
  112. do {
  113. BufferSize = sizeof (Char);
  114. Status = gDebugPort->Write (gDebugPort, gTimeOut, &BufferSize, &Char);
  115. } while (EFI_ERROR (Status) || BufferSize != sizeof (Char));
  116. return;
  117. }
  118. /**
  119. Send an ASCII string to GDB. This function must be able to run in interrupt context.
  120. @param String Send a string to GDB
  121. **/
  122. VOID
  123. GdbPutString (
  124. IN CHAR8 *String
  125. )
  126. {
  127. // We could performance enhance this function by calling gDebugPort->Write ()
  128. while (*String != '\0') {
  129. GdbPutChar (*String);
  130. String++;
  131. }
  132. }