]> git.proxmox.com Git - mirror_edk2.git/blobdiff - OvmfPkg/Library/XenConsoleSerialPortLib/XenConsoleSerialPortLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / OvmfPkg / Library / XenConsoleSerialPortLib / XenConsoleSerialPortLib.c
index 98022354cf31003d259ff63ede1961fa73e7bb7b..b29ed3e58e7ae54ee3ad37ba273760360694a83f 100644 (file)
@@ -2,19 +2,13 @@
   Xen console SerialPortLib instance\r
 \r
   Copyright (c) 2015, Linaro Ltd. All rights reserved.<BR>\r
+  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
 \r
-  This program and the accompanying materials\r
-  are licensed and made available under the terms and conditions of the BSD License\r
-  which accompanies this distribution.  The full text of the license may be found at\r
-  http://opensource.org/licenses/bsd-license.php\r
-\r
-  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
 #include <Base.h>\r
-#include <Uefi/UefiBaseType.h>\r
 \r
 #include <Library/BaseLib.h>\r
 #include <Library/SerialPortLib.h>\r
 #include <IndustryStandard/Xen/hvm/params.h>\r
 #include <IndustryStandard/Xen/event_channel.h>\r
 \r
+//\r
+// We can't use DebugLib due to a constructor dependency cycle between DebugLib\r
+// and ourselves.\r
+//\r
+#define ASSERT(Expression)      \\r
+  do {                          \\r
+    if (!(Expression)) {        \\r
+      CpuDeadLoop ();           \\r
+    }                           \\r
+  } while (FALSE)\r
+\r
 //\r
 // The code below expects these global variables to be mutable, even in the case\r
 // that we have been incorporated into SEC or PEIM phase modules (which is\r
 // in general, it is actually fine for the Xen domU (guest) environment that\r
 // this module is intended for, as UEFI always executes from DRAM in that case.\r
 //\r
-STATIC evtchn_send_t              mXenConsoleEventChain;\r
-STATIC struct xencons_interface   *mXenConsoleInterface;\r
+STATIC evtchn_send_t             mXenConsoleEventChain;\r
+STATIC struct xencons_interface  *mXenConsoleInterface;\r
 \r
+/**\r
+  Initialize the serial device hardware.\r
+\r
+  If no initialization is required, then return RETURN_SUCCESS.\r
+  If the serial device was successfully initialized, then return RETURN_SUCCESS.\r
+  If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.\r
+\r
+  @retval RETURN_SUCCESS        The serial device was initialized.\r
+  @retval RETURN_DEVICE_ERROR   The serial device could not be initialized.\r
+\r
+**/\r
 RETURN_STATUS\r
 EFIAPI\r
 SerialPortInitialize (\r
   VOID\r
   )\r
 {\r
+  if (!XenHypercallIsAvailable ()) {\r
+    return RETURN_DEVICE_ERROR;\r
+  }\r
+\r
   if (!mXenConsoleInterface) {\r
     mXenConsoleEventChain.port = (UINT32)XenHypercallHvmGetParam (HVM_PARAM_CONSOLE_EVTCHN);\r
-    mXenConsoleInterface = (struct xencons_interface *)(UINTN)\r
-      (XenHypercallHvmGetParam (HVM_PARAM_CONSOLE_PFN) << EFI_PAGE_SHIFT);\r
+    mXenConsoleInterface       = (struct xencons_interface *)(UINTN)\r
+                                 (XenHypercallHvmGetParam (HVM_PARAM_CONSOLE_PFN) << EFI_PAGE_SHIFT);\r
 \r
     //\r
     // No point in ASSERT'ing here as we won't be seeing the output\r
     //\r
   }\r
+\r
   return RETURN_SUCCESS;\r
 }\r
 \r
 /**\r
-  Write data to serial device.\r
+  Write data from buffer to serial device.\r
+\r
+  Writes NumberOfBytes data bytes from Buffer to the serial device.\r
+  The number of bytes actually written to the serial device is returned.\r
+  If the return value is less than NumberOfBytes, then the write operation failed.\r
+  If Buffer is NULL, then ASSERT().\r
+  If NumberOfBytes is zero, then return 0.\r
 \r
-  @param  Buffer           Point of data buffer which need to be written.\r
-  @param  NumberOfBytes    Number of output bytes which are cached in Buffer.\r
+  @param  Buffer           Pointer to the data buffer to be written.\r
+  @param  NumberOfBytes    Number of bytes to written to the serial device.\r
 \r
-  @retval 0                Write data failed.\r
-  @retval !0               Actual number of bytes written to serial device.\r
+  @retval 0                NumberOfBytes is 0.\r
+  @retval >0               The number of bytes written to the serial device.\r
+                           If this value is less than NumberOfBytes, then the write operation failed.\r
 \r
 **/\r
 UINTN\r
 EFIAPI\r
 SerialPortWrite (\r
-  IN UINT8     *Buffer,\r
-  IN UINTN     NumberOfBytes\r
+  IN UINT8  *Buffer,\r
+  IN UINTN  NumberOfBytes\r
   )\r
 {\r
   XENCONS_RING_IDX  Consumer, Producer;\r
   UINTN             Sent;\r
 \r
+  ASSERT (Buffer != NULL);\r
+\r
+  if (NumberOfBytes == 0) {\r
+    return 0;\r
+  }\r
+\r
   if (!mXenConsoleInterface) {\r
     return 0;\r
   }\r
 \r
-  Consumer = mXenConsoleInterface->out_cons;\r
-  Producer = mXenConsoleInterface->out_prod;\r
+  Sent = 0;\r
+  do {\r
+    Consumer = mXenConsoleInterface->out_cons;\r
+    Producer = mXenConsoleInterface->out_prod;\r
 \r
-  MemoryFence ();\r
+    MemoryFence ();\r
 \r
-  Sent = 0;\r
-  while (Sent < NumberOfBytes && ((Producer - Consumer) < sizeof (mXenConsoleInterface->out)))\r
-    mXenConsoleInterface->out[MASK_XENCONS_IDX(Producer++, mXenConsoleInterface->out)] = Buffer[Sent++];\r
+    while (Sent < NumberOfBytes && ((Producer - Consumer) < sizeof (mXenConsoleInterface->out))) {\r
+      mXenConsoleInterface->out[MASK_XENCONS_IDX (Producer++, mXenConsoleInterface->out)] = Buffer[Sent++];\r
+    }\r
 \r
-  MemoryFence ();\r
+    MemoryFence ();\r
 \r
-  mXenConsoleInterface->out_prod = Producer;\r
+    mXenConsoleInterface->out_prod = Producer;\r
 \r
-  if (Sent > 0) {\r
     XenHypercallEventChannelOp (EVTCHNOP_send, &mXenConsoleEventChain);\r
-  }\r
+  } while (Sent < NumberOfBytes);\r
 \r
   return Sent;\r
 }\r
 \r
 /**\r
-  Read data from serial device and save the data in buffer.\r
+  Read data from serial device and save the datas in buffer.\r
+\r
+  Reads NumberOfBytes data bytes from a serial device into the buffer\r
+  specified by Buffer. The number of bytes actually read is returned.\r
+  If Buffer is NULL, then ASSERT().\r
+  If NumberOfBytes is zero, then return 0.\r
 \r
-  @param  Buffer           Point of data buffer which need to be written.\r
-  @param  NumberOfBytes    Size of Buffer[].\r
+  @param  Buffer           Pointer to the data buffer to store the data read from the serial device.\r
+  @param  NumberOfBytes    Number of bytes which will be read.\r
 \r
-  @retval 0                Read data failed.\r
-  @retval !0               Actual number of bytes read from serial device.\r
+  @retval 0                Read data failed, no data is to be read.\r
+  @retval >0               Actual number of bytes read from serial device.\r
 \r
 **/\r
 UINTN\r
 EFIAPI\r
 SerialPortRead (\r
-  OUT UINT8     *Buffer,\r
-  IN  UINTN     NumberOfBytes\r
-)\r
+  OUT UINT8  *Buffer,\r
+  IN  UINTN  NumberOfBytes\r
+  )\r
 {\r
   XENCONS_RING_IDX  Consumer, Producer;\r
   UINTN             Received;\r
 \r
+  ASSERT (Buffer != NULL);\r
+\r
+  if (NumberOfBytes == 0) {\r
+    return 0;\r
+  }\r
+\r
   if (!mXenConsoleInterface) {\r
     return 0;\r
   }\r
@@ -126,8 +172,9 @@ SerialPortRead (
   MemoryFence ();\r
 \r
   Received = 0;\r
-  while (Received < NumberOfBytes && Consumer < Producer)\r
-     Buffer[Received++] = mXenConsoleInterface->in[MASK_XENCONS_IDX(Consumer++, mXenConsoleInterface->in)];\r
+  while (Received < NumberOfBytes && Consumer < Producer) {\r
+    Buffer[Received++] = mXenConsoleInterface->in[MASK_XENCONS_IDX (Consumer++, mXenConsoleInterface->in)];\r
+  }\r
 \r
   MemoryFence ();\r
 \r
@@ -139,10 +186,10 @@ SerialPortRead (
 }\r
 \r
 /**\r
-  Check to see if any data is available to be read from the debug device.\r
+  Polls a serial device to see if there is any data waiting to be read.\r
 \r
-  @retval TRUE       At least one byte of data is available to be read\r
-  @retval FALSE      No data is available to be read\r
+  @retval TRUE             Data is waiting to be read from the serial device.\r
+  @retval FALSE            There is no data waiting to be read from the serial device.\r
 \r
 **/\r
 BOOLEAN\r
@@ -152,5 +199,99 @@ SerialPortPoll (
   )\r
 {\r
   return mXenConsoleInterface &&\r
-    mXenConsoleInterface->in_cons != mXenConsoleInterface->in_prod;\r
+         mXenConsoleInterface->in_cons != mXenConsoleInterface->in_prod;\r
+}\r
+\r
+/**\r
+  Sets the control bits on a serial device.\r
+\r
+  @param Control                Sets the bits of Control that are settable.\r
+\r
+  @retval RETURN_SUCCESS        The new control bits were set on the serial device.\r
+  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.\r
+  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.\r
+\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SerialPortSetControl (\r
+  IN UINT32  Control\r
+  )\r
+{\r
+  return RETURN_UNSUPPORTED;\r
+}\r
+\r
+/**\r
+  Retrieve the status of the control bits on a serial device.\r
+\r
+  @param Control                A pointer to return the current control signals from the serial device.\r
+\r
+  @retval RETURN_SUCCESS        The control bits were read from the serial device.\r
+  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.\r
+  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.\r
+\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SerialPortGetControl (\r
+  OUT UINT32  *Control\r
+  )\r
+{\r
+  if (!mXenConsoleInterface) {\r
+    return RETURN_UNSUPPORTED;\r
+  }\r
+\r
+  *Control = 0;\r
+  if (!SerialPortPoll ()) {\r
+    *Control = EFI_SERIAL_INPUT_BUFFER_EMPTY;\r
+  }\r
+\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
+/**\r
+  Sets the baud rate, receive FIFO depth, transmit/receive time out, parity,\r
+  data bits, and stop bits on a serial device.\r
+\r
+  @param BaudRate           The requested baud rate. A BaudRate value of 0 will use the\r
+                            device's default interface speed.\r
+                            On output, the value actually set.\r
+  @param ReceiveFifoDepth   The requested depth of the FIFO on the receive side of the\r
+                            serial interface. A ReceiveFifoDepth value of 0 will use\r
+                            the device's default FIFO depth.\r
+                            On output, the value actually set.\r
+  @param Timeout            The requested time out for a single character in microseconds.\r
+                            This timeout applies to both the transmit and receive side of the\r
+                            interface. A Timeout value of 0 will use the device's default time\r
+                            out value.\r
+                            On output, the value actually set.\r
+  @param Parity             The type of parity to use on this serial device. A Parity value of\r
+                            DefaultParity will use the device's default parity value.\r
+                            On output, the value actually set.\r
+  @param DataBits           The number of data bits to use on the serial device. A DataBits\r
+                            value of 0 will use the device's default data bit setting.\r
+                            On output, the value actually set.\r
+  @param StopBits           The number of stop bits to use on this serial device. A StopBits\r
+                            value of DefaultStopBits will use the device's default number of\r
+                            stop bits.\r
+                            On output, the value actually set.\r
+\r
+  @retval RETURN_SUCCESS            The new attributes were set on the serial device.\r
+  @retval RETURN_UNSUPPORTED        The serial device does not support this operation.\r
+  @retval RETURN_INVALID_PARAMETER  One or more of the attributes has an unsupported value.\r
+  @retval RETURN_DEVICE_ERROR       The serial device is not functioning correctly.\r
+\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SerialPortSetAttributes (\r
+  IN OUT UINT64              *BaudRate,\r
+  IN OUT UINT32              *ReceiveFifoDepth,\r
+  IN OUT UINT32              *Timeout,\r
+  IN OUT EFI_PARITY_TYPE     *Parity,\r
+  IN OUT UINT8               *DataBits,\r
+  IN OUT EFI_STOP_BITS_TYPE  *StopBits\r
+  )\r
+{\r
+  return RETURN_UNSUPPORTED;\r
 }\r