DebugCommunicationLibSerialPort.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /** @file
  2. Debug Port Library implementation based on serial port.
  3. Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <Base.h>
  7. #include <Library/DebugCommunicationLib.h>
  8. #include <Library/SerialPortLib.h>
  9. #include <Library/DebugLib.h>
  10. /**
  11. Initialize the debug port.
  12. This function will initialize debug port to get it ready for data transmission. If
  13. certain Debug Communication Library instance has to save some private data in the
  14. stack, this function must work on the mode that doesn't return to the caller, then
  15. the caller needs to wrap up all rest of logic after DebugPortInitialize() into one
  16. function and pass it into DebugPortInitialize(). DebugPortInitialize() is
  17. responsible to invoke the passing-in function at the end of DebugPortInitialize().
  18. If the parameter Function is not NULL, Debug Communication Library instance will
  19. invoke it by passing in the Context to be the first parameter. Debug Communication
  20. Library instance could create one debug port handle to be the second parameter
  21. passing into the Function. Debug Communication Library instance also could pass
  22. NULL to be the second parameter if it doesn't create the debug port handle.
  23. If the parameter Function is NULL, and Context is not NULL. At this time, Context
  24. is the debug port handle created by the previous Debug Communication Library
  25. instance.
  26. a) If the instance can understand and continue use the private data of the previous
  27. instance, it could return the same handle as passed in (as Context parameter).
  28. b) If the instance does not understand, or does not want to continue use the
  29. private data of the previous instance, it could ignore the input Context parameter
  30. and create the new handle to be returned.
  31. If Function() is NULL and Context is NULL, Debug Communication Library could create a
  32. new handle and return it. NULL is also a valid handle to be returned.
  33. @param[in] Context Context needed by callback function; it was optional.
  34. @param[in] Function Continue function called by Debug Communication library;
  35. it was optional.
  36. @return The debug port handle created by Debug Communication Library if Function
  37. is not NULL.
  38. **/
  39. DEBUG_PORT_HANDLE
  40. EFIAPI
  41. DebugPortInitialize (
  42. IN VOID *Context,
  43. IN DEBUG_PORT_CONTINUE Function
  44. )
  45. {
  46. RETURN_STATUS Status;
  47. Status = SerialPortInitialize ();
  48. if (RETURN_ERROR (Status)) {
  49. DEBUG ((DEBUG_ERROR, "Debug Serial Port: Initialization failed!\n"));
  50. }
  51. if (Function != NULL) {
  52. Function (Context, NULL);
  53. }
  54. return NULL;
  55. }
  56. /**
  57. Read data from debug device and save the data in a buffer.
  58. Reads NumberOfBytes data bytes from a debug device into the buffer
  59. specified by Buffer. The number of bytes actually read is returned.
  60. If the return value is less than NumberOfBytes, then the rest operation failed.
  61. If NumberOfBytes is zero, then return 0.
  62. @param Handle Debug port handle.
  63. @param Buffer Pointer to the data buffer to store the data read from the debug device.
  64. @param NumberOfBytes Number of bytes which will be read.
  65. @param Timeout Timeout value for reading from debug device. It unit is Microsecond.
  66. @retval 0 Read data failed, no data is to be read.
  67. @retval >0 Actual number of bytes read from debug device.
  68. **/
  69. UINTN
  70. EFIAPI
  71. DebugPortReadBuffer (
  72. IN DEBUG_PORT_HANDLE Handle,
  73. IN UINT8 *Buffer,
  74. IN UINTN NumberOfBytes,
  75. IN UINTN Timeout
  76. )
  77. {
  78. if ((NumberOfBytes != 1) || (Buffer == NULL) || (Timeout != 0)) {
  79. return 0;
  80. }
  81. return SerialPortRead (Buffer, 1);
  82. }
  83. /**
  84. Write data from buffer to debug device.
  85. Writes NumberOfBytes data bytes from Buffer to the debug device.
  86. The number of bytes actually written to the debug device is returned.
  87. If the return value is less than NumberOfBytes, then the write operation failed.
  88. If NumberOfBytes is zero, then return 0.
  89. @param Handle Debug port handle.
  90. @param Buffer Pointer to the data buffer to be written.
  91. @param NumberOfBytes Number of bytes to written to the debug device.
  92. @retval 0 NumberOfBytes is 0.
  93. @retval >0 The number of bytes written to the debug device.
  94. If this value is less than NumberOfBytes, then the read operation failed.
  95. **/
  96. UINTN
  97. EFIAPI
  98. DebugPortWriteBuffer (
  99. IN DEBUG_PORT_HANDLE Handle,
  100. IN UINT8 *Buffer,
  101. IN UINTN NumberOfBytes
  102. )
  103. {
  104. return SerialPortWrite (Buffer, NumberOfBytes);
  105. }
  106. /**
  107. Polls a debug device to see if there is any data waiting to be read.
  108. Polls a debug device to see if there is any data waiting to be read.
  109. If there is data waiting to be read from the debug device, then TRUE is returned.
  110. If there is no data waiting to be read from the debug device, then FALSE is returned.
  111. @param Handle Debug port handle.
  112. @retval TRUE Data is waiting to be read from the debug device.
  113. @retval FALSE There is no data waiting to be read from the serial device.
  114. **/
  115. BOOLEAN
  116. EFIAPI
  117. DebugPortPollBuffer (
  118. IN DEBUG_PORT_HANDLE Handle
  119. )
  120. {
  121. return SerialPortPoll ();
  122. }