]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SourceLevelDebugPkg/Library/DebugCommunicationLibUsb/DebugCommunicationLibUsb.c
SourceLevelDebugPkg: Check if PcdUsbEhciPciAddress is set correctly to avoid assertion
[mirror_edk2.git] / SourceLevelDebugPkg / Library / DebugCommunicationLibUsb / DebugCommunicationLibUsb.c
index 59189db0dc4e865073419426f2c1b9108a48eb3f..567e06c9d36e9cfe8c43766a61840b2276183058 100644 (file)
@@ -22,6 +22,7 @@
 #include <Library/DebugCommunicationLib.h>\r
 #include <Library/BaseMemoryLib.h>\r
 #include <Library/BaseLib.h>\r
+#include <Library/DebugLib.h>\r
 \r
 #define SETUP_PID            0x2D\r
 #define INPUT_PID            0x69\r
@@ -108,7 +109,7 @@ typedef struct _USB_DEBUG_PORT_HANDLE{
   // The usb debug port memory BAR number in EHCI configuration space.\r
   //\r
   UINT8        DebugPortBarNumber;\r
-  UINT8        Reserved;\r
+  BOOLEAN      Initialized;\r
   //\r
   // The offset of usb debug port registers in EHCI memory range.\r
   //\r
@@ -163,9 +164,29 @@ CalculateUsbDebugPortBar (
  )\r
 {\r
   UINT16     PciStatus;\r
+  UINT16     VendorId;\r
+  UINT16     DeviceId;\r
+  UINT8      ProgInterface;\r
+  UINT8      SubClassCode;\r
+  UINT8      BaseCode;\r
   UINT8      CapabilityPtr;\r
   UINT8      CapabilityId;\r
 \r
+  VendorId = PciRead16 (PcdGet32(PcdUsbEhciPciAddress) + PCI_VENDOR_ID_OFFSET);\r
+  DeviceId = PciRead16 (PcdGet32(PcdUsbEhciPciAddress) + PCI_DEVICE_ID_OFFSET);\r
+  \r
+  if ((VendorId == 0xFFFF) || (DeviceId == 0xFFFF)) {\r
+    return RETURN_UNSUPPORTED;\r
+  }\r
+\r
+  ProgInterface = PciRead8 (PcdGet32(PcdUsbEhciPciAddress) + PCI_CLASSCODE_OFFSET);\r
+  SubClassCode  = PciRead8 (PcdGet32(PcdUsbEhciPciAddress) + PCI_CLASSCODE_OFFSET + 1);\r
+  BaseCode      = PciRead8 (PcdGet32(PcdUsbEhciPciAddress) + PCI_CLASSCODE_OFFSET + 2);\r
+  \r
+  if ((ProgInterface != PCI_IF_EHCI) || (SubClassCode != PCI_CLASS_SERIAL_USB) || (BaseCode != PCI_CLASS_SERIAL)) {\r
+    return RETURN_UNSUPPORTED;\r
+  }\r
+\r
   //\r
   // Enable Ehci Host Controller MMIO Space.\r
   //\r
@@ -285,7 +306,10 @@ UsbDebugPortIn (
   }\r
 \r
   *Length = (UINT8)(MmioRead32((UINTN)&DebugPortRegister->ControlStatus) & 0xF);\r
-  ASSERT (*Length <= 8);\r
+  if (*Length > 8) {\r
+    return RETURN_DEVICE_ERROR;\r
+  }\r
+\r
   for (Index = 0; Index < *Length; Index++) {\r
     Buffer[Index] = DebugPortRegister->DataBuffer[Index];\r
   }\r
@@ -643,7 +667,11 @@ InitializeUsbDebugHardware (
     //\r
     return Status;\r
   }\r
-  ASSERT (Length == sizeof(USB_DEBUG_PORT_DESCRIPTOR));\r
+\r
+  if (Length != sizeof(USB_DEBUG_PORT_DESCRIPTOR)) {\r
+    return RETURN_DEVICE_ERROR;\r
+  }\r
+\r
   //\r
   // set usb debug device address as 0x7F.\r
   //\r
@@ -716,6 +744,13 @@ DebugPortReadBuffer (
     UsbDebugPortHandle = (USB_DEBUG_PORT_HANDLE *)Handle;\r
   }\r
 \r
+  //\r
+  // Check if debug port is ready\r
+  //\r
+  if (!UsbDebugPortHandle->Initialized) {\r
+    return 0;\r
+  }\r
+\r
   if (NeedReinitializeHardware(UsbDebugPortHandle)) {\r
     Status = InitializeUsbDebugHardware (UsbDebugPortHandle);\r
     if (RETURN_ERROR(Status)) {\r
@@ -861,6 +896,13 @@ DebugPortWriteBuffer (
     UsbDebugPortHandle = (USB_DEBUG_PORT_HANDLE *)Handle;\r
   }\r
 \r
+  //\r
+  // Check if debug port is ready\r
+  //\r
+  if (!UsbDebugPortHandle->Initialized) {\r
+    return 0;\r
+  }\r
+\r
   if (NeedReinitializeHardware(UsbDebugPortHandle)) {\r
     Status = InitializeUsbDebugHardware (UsbDebugPortHandle);\r
     if (RETURN_ERROR(Status)) {\r
@@ -933,6 +975,13 @@ DebugPortPollBuffer (
     UsbDebugPortHandle = (USB_DEBUG_PORT_HANDLE *)Handle;\r
   }\r
 \r
+  //\r
+  // Check if debug port is ready\r
+  //\r
+  if (!UsbDebugPortHandle->Initialized) {\r
+    return 0;\r
+  }\r
+\r
   if (NeedReinitializeHardware(UsbDebugPortHandle)) {\r
     Status = InitializeUsbDebugHardware(UsbDebugPortHandle);\r
     if (RETURN_ERROR(Status)) {\r
@@ -1039,7 +1088,8 @@ DebugPortInitialize (
 \r
   Status = CalculateUsbDebugPortBar(&Handle.DebugPortOffset, &Handle.DebugPortBarNumber);\r
   if (RETURN_ERROR (Status)) {\r
-    return NULL;\r
+    DEBUG ((EFI_D_ERROR, "USB Debug Port: the pci device pointed by PcdUsbEhciPciAddress is not EHCI host controller or does not support debug port capability!\n"));\r
+    goto Exit;\r
   }\r
 \r
   Handle.EhciMemoryBase = 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress) + PCI_BASE_ADDRESSREG_OFFSET);\r
@@ -1064,9 +1114,17 @@ DebugPortInitialize (
 \r
   Status = InitializeUsbDebugHardware (&Handle);\r
   if (RETURN_ERROR(Status)) {\r
-    return NULL;\r
+    DEBUG ((EFI_D_ERROR, "USB Debug Port: Initialization failed, please check if USB debug cable is plugged into EHCI debug port correctly!\n"));\r
+    goto Exit;\r
   }\r
 \r
+  //\r
+  // Set debug port initialized successfully flag\r
+  //\r
+  Handle.Initialized = TRUE;\r
+\r
+Exit:\r
+\r
   if (Function != NULL) {\r
     Function (Context, &Handle);\r
   } else {\r