CbSerialPortLib.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /** @file
  2. CBMEM console SerialPortLib instance
  3. Copyright (c) 2022, Baruch Binyamin Doron
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <Base.h>
  7. #include <Coreboot.h>
  8. #include <Library/BaseMemoryLib.h>
  9. #include <Library/BlParseLib.h>
  10. #include <Library/SerialPortLib.h>
  11. // Upper nibble contains flags
  12. #define CBMC_CURSOR_MASK ((1 << 28) - 1)
  13. #define CBMC_OVERFLOW (1 << 31)
  14. STATIC struct cbmem_console *mCbConsole = NULL;
  15. /**
  16. Find coreboot record with given Tag.
  17. NOTE: This coreboot-specific function definition is absent
  18. from the common BlParseLib header.
  19. @param Tag The tag id to be found
  20. @retval NULL The Tag is not found.
  21. @retval Others The pointer to the record found.
  22. **/
  23. VOID *
  24. FindCbTag (
  25. IN UINT32 Tag
  26. );
  27. /**
  28. Initialize the serial device hardware.
  29. If no initialization is required, then return RETURN_SUCCESS.
  30. If the serial device was successfully initialized, then return RETURN_SUCCESS.
  31. If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
  32. @retval RETURN_SUCCESS The serial device was initialized.
  33. @retval RETURN_DEVICE_ERROR The serial device could not be initialized.
  34. **/
  35. RETURN_STATUS
  36. EFIAPI
  37. SerialPortInitialize (
  38. VOID
  39. )
  40. {
  41. struct cb_cbmem_ref *CbMemRef;
  42. // The coreboot table contains large structures as references
  43. CbMemRef = FindCbTag (CB_TAG_CBMEM_CONSOLE);
  44. if (CbMemRef == NULL) {
  45. return RETURN_DEVICE_ERROR;
  46. }
  47. mCbConsole = (VOID *)(UINTN)CbMemRef->cbmem_addr; // Support PEI and DXE
  48. if (mCbConsole == NULL) {
  49. return RETURN_DEVICE_ERROR;
  50. }
  51. return RETURN_SUCCESS;
  52. }
  53. /**
  54. Write data from buffer to serial device.
  55. Writes NumberOfBytes data bytes from Buffer to the serial device.
  56. The number of bytes actually written to the serial device is returned.
  57. If the return value is less than NumberOfBytes, then the write operation failed.
  58. If Buffer is NULL, then ASSERT().
  59. If NumberOfBytes is zero, then return 0.
  60. @param Buffer Pointer to the data buffer to be written.
  61. @param NumberOfBytes Number of bytes to written to the serial device.
  62. @retval 0 NumberOfBytes is 0.
  63. @retval >0 The number of bytes written to the serial device.
  64. If this value is less than NumberOfBytes, then the write operation failed.
  65. **/
  66. UINTN
  67. EFIAPI
  68. SerialPortWrite (
  69. IN UINT8 *Buffer,
  70. IN UINTN NumberOfBytes
  71. )
  72. {
  73. UINT32 Cursor;
  74. UINT32 Flags;
  75. if ((Buffer == NULL) || (NumberOfBytes == 0)) {
  76. return 0;
  77. }
  78. if (mCbConsole == NULL) {
  79. return 0;
  80. }
  81. Cursor = mCbConsole->cursor & CBMC_CURSOR_MASK;
  82. Flags = mCbConsole->cursor & ~CBMC_CURSOR_MASK;
  83. if (Cursor >= mCbConsole->size) {
  84. // Already overflowed; bail out. TODO: Is this unnecessarily cautious?
  85. // - Supports old coreboot version with legacy overflow mechanism.
  86. return 0;
  87. }
  88. if (Cursor + NumberOfBytes > mCbConsole->size) {
  89. // Will overflow, zero cursor and set flag.
  90. Cursor = 0;
  91. Flags |= CBMC_OVERFLOW;
  92. }
  93. if (NumberOfBytes > mCbConsole->size) {
  94. // This one debug message is longer than the entire buffer. Truncate it.
  95. // - TODO: Is this unnecessarily cautious?
  96. NumberOfBytes = mCbConsole->size;
  97. }
  98. CopyMem (&mCbConsole->body[Cursor], Buffer, NumberOfBytes);
  99. Cursor += NumberOfBytes;
  100. if (Cursor == mCbConsole->size) {
  101. // Next message will overflow, zero cursor.
  102. // - Set flag preemptively. This could not be determined later.
  103. Cursor = 0;
  104. Flags |= CBMC_OVERFLOW;
  105. }
  106. mCbConsole->cursor = Flags | Cursor;
  107. return NumberOfBytes;
  108. }
  109. /**
  110. Read data from serial device and save the datas in buffer.
  111. Reads NumberOfBytes data bytes from a serial device into the buffer
  112. specified by Buffer. The number of bytes actually read is returned.
  113. If Buffer is NULL, then ASSERT().
  114. If NumberOfBytes is zero, then return 0.
  115. @param Buffer Pointer to the data buffer to store the data read from the serial device.
  116. @param NumberOfBytes Number of bytes which will be read.
  117. @retval 0 Read data failed, no data is to be read.
  118. @retval >0 Actual number of bytes read from serial device.
  119. **/
  120. UINTN
  121. EFIAPI
  122. SerialPortRead (
  123. OUT UINT8 *Buffer,
  124. IN UINTN NumberOfBytes
  125. )
  126. {
  127. return 0;
  128. }
  129. /**
  130. Polls a serial device to see if there is any data waiting to be read.
  131. @retval TRUE Data is waiting to be read from the serial device.
  132. @retval FALSE There is no data waiting to be read from the serial device.
  133. **/
  134. BOOLEAN
  135. EFIAPI
  136. SerialPortPoll (
  137. VOID
  138. )
  139. {
  140. return FALSE;
  141. }
  142. /**
  143. Sets the control bits on a serial device.
  144. @param Control Sets the bits of Control that are settable.
  145. @retval RETURN_SUCCESS The new control bits were set on the serial device.
  146. @retval RETURN_UNSUPPORTED The serial device does not support this operation.
  147. @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
  148. **/
  149. RETURN_STATUS
  150. EFIAPI
  151. SerialPortSetControl (
  152. IN UINT32 Control
  153. )
  154. {
  155. return RETURN_UNSUPPORTED;
  156. }
  157. /**
  158. Retrieve the status of the control bits on a serial device.
  159. @param Control A pointer to return the current control signals from the serial device.
  160. @retval RETURN_SUCCESS The control bits were read from the serial device.
  161. @retval RETURN_UNSUPPORTED The serial device does not support this operation.
  162. @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
  163. **/
  164. RETURN_STATUS
  165. EFIAPI
  166. SerialPortGetControl (
  167. OUT UINT32 *Control
  168. )
  169. {
  170. return RETURN_UNSUPPORTED;
  171. }
  172. /**
  173. Sets the baud rate, receive FIFO depth, transmit/receive time out, parity,
  174. data bits, and stop bits on a serial device.
  175. @param BaudRate The requested baud rate. A BaudRate value of 0 will use the
  176. device's default interface speed.
  177. On output, the value actually set.
  178. @param ReceiveFifoDepth The requested depth of the FIFO on the receive side of the
  179. serial interface. A ReceiveFifoDepth value of 0 will use
  180. the device's default FIFO depth.
  181. On output, the value actually set.
  182. @param Timeout The requested time out for a single character in microseconds.
  183. This timeout applies to both the transmit and receive side of the
  184. interface. A Timeout value of 0 will use the device's default time
  185. out value.
  186. On output, the value actually set.
  187. @param Parity The type of parity to use on this serial device. A Parity value of
  188. DefaultParity will use the device's default parity value.
  189. On output, the value actually set.
  190. @param DataBits The number of data bits to use on the serial device. A DataBits
  191. value of 0 will use the device's default data bit setting.
  192. On output, the value actually set.
  193. @param StopBits The number of stop bits to use on this serial device. A StopBits
  194. value of DefaultStopBits will use the device's default number of
  195. stop bits.
  196. On output, the value actually set.
  197. @retval RETURN_SUCCESS The new attributes were set on the serial device.
  198. @retval RETURN_UNSUPPORTED The serial device does not support this operation.
  199. @retval RETURN_INVALID_PARAMETER One or more of the attributes has an unsupported value.
  200. @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
  201. **/
  202. RETURN_STATUS
  203. EFIAPI
  204. SerialPortSetAttributes (
  205. IN OUT UINT64 *BaudRate,
  206. IN OUT UINT32 *ReceiveFifoDepth,
  207. IN OUT UINT32 *Timeout,
  208. IN OUT EFI_PARITY_TYPE *Parity,
  209. IN OUT UINT8 *DataBits,
  210. IN OUT EFI_STOP_BITS_TYPE *StopBits
  211. )
  212. {
  213. return RETURN_UNSUPPORTED;
  214. }