]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/DebugPortDxe/DebugPort.c
Fix GCC build failure.
[mirror_edk2.git] / MdeModulePkg / Universal / DebugPortDxe / DebugPort.c
index b91003f212ea111f8e9ceac435aabaf670e72abf..00889a246d8fc801a6ad4b274d51877f6ef0fdae 100644 (file)
@@ -4,8 +4,8 @@
   ALL CODE IN THE SERIALIO STACK MUST BE RE-ENTRANT AND CALLABLE FROM\r
   INTERRUPT CONTEXT\r
 \r
-Copyright (c) 2006 - 2008, Intel Corporation. <BR>\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\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
@@ -29,100 +29,92 @@ EFI_DRIVER_BINDING_PROTOCOL gDebugPortDriverBinding = {
   NULL\r
 };\r
 \r
-DEBUGPORT_DEVICE  *gDebugPortDevice;\r
+DEBUGPORT_DEVICE mDebugPortDevice = {\r
+  DEBUGPORT_DEVICE_SIGNATURE,\r
+  (EFI_HANDLE) 0,\r
+  (EFI_HANDLE) 0,\r
+  (EFI_DEVICE_PATH_PROTOCOL *) NULL,\r
+  {\r
+    DebugPortReset,\r
+    DebugPortWrite,\r
+    DebugPortRead,\r
+    DebugPortPoll\r
+  },\r
+  (EFI_HANDLE) 0,\r
+  (EFI_SERIAL_IO_PROTOCOL *) NULL,\r
+  DEBUGPORT_UART_DEFAULT_BAUDRATE,\r
+  DEBUGPORT_UART_DEFAULT_FIFO_DEPTH,\r
+  DEBUGPORT_UART_DEFAULT_TIMEOUT,\r
+  (EFI_PARITY_TYPE) DEBUGPORT_UART_DEFAULT_PARITY,\r
+  DEBUGPORT_UART_DEFAULT_DATA_BITS,\r
+  (EFI_STOP_BITS_TYPE) DEBUGPORT_UART_DEFAULT_STOP_BITS\r
+};\r
 \r
 /**\r
   Local worker function to obtain device path information from DebugPort variable.\r
 \r
   Records requested settings in DebugPort device structure.\r
-  \r
-  @param  DebugPortDevice    Pointer to instance of dubug port device.\r
 \r
 **/\r
-VOID\r
+EFI_DEVICE_PATH_PROTOCOL *\r
 GetDebugPortVariable (\r
-  DEBUGPORT_DEVICE            *DebugPortDevice\r
+  VOID\r
   )\r
 {\r
   UINTN                     DataSize;\r
+  EFI_DEVICE_PATH_PROTOCOL  *DebugPortVariable;\r
   EFI_DEVICE_PATH_PROTOCOL  *DevicePath;\r
-  EFI_STATUS                Status;\r
-\r
-  DataSize = 0;\r
-\r
-  Status = gRT->GetVariable (\r
-                  (CHAR16 *) EFI_DEBUGPORT_VARIABLE_NAME,\r
-                  &gEfiDebugPortVariableGuid,\r
-                  NULL,\r
-                  &DataSize,\r
-                  DebugPortDevice->DebugPortVariable\r
-                  );\r
 \r
-  if (Status == EFI_BUFFER_TOO_SMALL) {\r
-    if (gDebugPortDevice->DebugPortVariable != NULL) {\r
-      FreePool (gDebugPortDevice->DebugPortVariable);\r
-    }\r
+  GetVariable2 (EFI_DEBUGPORT_VARIABLE_NAME, &gEfiDebugPortVariableGuid, (VOID **) &DebugPortVariable, &DataSize);\r
+  if (DebugPortVariable == NULL) {\r
+    return NULL;\r
+  }\r
 \r
-    DebugPortDevice->DebugPortVariable = AllocatePool (DataSize);\r
-    if (DebugPortDevice->DebugPortVariable != NULL) {\r
-      gRT->GetVariable (\r
-            (CHAR16 *) EFI_DEBUGPORT_VARIABLE_NAME,\r
-            &gEfiDebugPortVariableGuid,\r
-            NULL,\r
-            &DataSize,\r
-            DebugPortDevice->DebugPortVariable\r
-            );\r
-      DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) DebugPortDevice->DebugPortVariable;\r
-      while (!EfiIsDevicePathEnd (DevicePath) && !IS_UART_DEVICEPATH (DevicePath)) {\r
-        DevicePath = EfiNextDevicePathNode (DevicePath);\r
-      }\r
+  DevicePath = DebugPortVariable;\r
+  while (!IsDevicePathEnd (DevicePath) && !IS_UART_DEVICEPATH (DevicePath)) {\r
+    DevicePath = NextDevicePathNode (DevicePath);\r
+  }\r
 \r
-      if (EfiIsDevicePathEnd (DevicePath)) {\r
-        FreePool (gDebugPortDevice->DebugPortVariable);\r
-        DebugPortDevice->DebugPortVariable = NULL;\r
-      } else {\r
-        CopyMem (\r
-          &DebugPortDevice->BaudRate,\r
-          &((UART_DEVICE_PATH *) DevicePath)->BaudRate,\r
-          sizeof (((UART_DEVICE_PATH *) DevicePath)->BaudRate)\r
-          );\r
-        DebugPortDevice->ReceiveFifoDepth = DEBUGPORT_UART_DEFAULT_FIFO_DEPTH;\r
-        DebugPortDevice->Timeout          = DEBUGPORT_UART_DEFAULT_TIMEOUT;\r
-        CopyMem (\r
-          &DebugPortDevice->Parity,\r
-          &((UART_DEVICE_PATH *) DevicePath)->Parity,\r
-          sizeof (((UART_DEVICE_PATH *) DevicePath)->Parity)\r
-          );\r
-        CopyMem (\r
-          &DebugPortDevice->DataBits,\r
-          &((UART_DEVICE_PATH *) DevicePath)->DataBits,\r
-          sizeof (((UART_DEVICE_PATH *) DevicePath)->DataBits)\r
-          );\r
-        CopyMem (\r
-          &DebugPortDevice->StopBits,\r
-          &((UART_DEVICE_PATH *) DevicePath)->StopBits,\r
-          sizeof (((UART_DEVICE_PATH *) DevicePath)->StopBits)\r
-          );\r
-      }\r
-    }\r
+  if (IsDevicePathEnd (DevicePath)) {\r
+    FreePool (DebugPortVariable);\r
+    return NULL;\r
+  } else {\r
+    CopyMem (\r
+      &mDebugPortDevice.BaudRate,\r
+      &((UART_DEVICE_PATH *) DevicePath)->BaudRate,\r
+      sizeof (((UART_DEVICE_PATH *) DevicePath)->BaudRate)\r
+      );\r
+    mDebugPortDevice.ReceiveFifoDepth = DEBUGPORT_UART_DEFAULT_FIFO_DEPTH;\r
+    mDebugPortDevice.Timeout          = DEBUGPORT_UART_DEFAULT_TIMEOUT;\r
+    CopyMem (\r
+      &mDebugPortDevice.Parity,\r
+      &((UART_DEVICE_PATH *) DevicePath)->Parity,\r
+      sizeof (((UART_DEVICE_PATH *) DevicePath)->Parity)\r
+      );\r
+    CopyMem (\r
+      &mDebugPortDevice.DataBits,\r
+      &((UART_DEVICE_PATH *) DevicePath)->DataBits,\r
+      sizeof (((UART_DEVICE_PATH *) DevicePath)->DataBits)\r
+      );\r
+    CopyMem (\r
+      &mDebugPortDevice.StopBits,\r
+      &((UART_DEVICE_PATH *) DevicePath)->StopBits,\r
+      sizeof (((UART_DEVICE_PATH *) DevicePath)->StopBits)\r
+      );\r
+    return DebugPortVariable;\r
   }\r
 }\r
 \r
-\r
-//\r
-// implementation code\r
-//\r
-\r
 /**\r
-  Debug Port Driver entry pointo. \r
+  Debug Port Driver entry point.\r
 \r
   Reads DebugPort variable to determine what device and settings to use as the\r
   debug port.  Binds exclusively to SerialIo. Reverts to defaults if no variable\r
   is found.\r
 \r
-  @param[in] ImageHandle       The firmware allocated handle for the EFI image.  \r
+  @param[in] ImageHandle       The firmware allocated handle for the EFI image.\r
   @param[in] SystemTable       A pointer to the EFI System Table.\r
-  \r
+\r
   @retval EFI_SUCCESS          The entry point is executed successfully.\r
   @retval EFI_OUT_OF_RESOURCES Fails to allocate memory for device.\r
   @retval other                Some error occurs when executing this entry point.\r
@@ -149,35 +141,12 @@ InitializeDebugPortDriver (
              &gDebugPortComponentName2\r
              );\r
   ASSERT_EFI_ERROR (Status);\r
-  //\r
-  // Allocate and Initialize dev structure\r
-  //\r
-  gDebugPortDevice = AllocateZeroPool (sizeof (DEBUGPORT_DEVICE));\r
-  if (gDebugPortDevice == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-  //\r
-  // Fill in static and default pieces of device structure first.\r
-  //\r
-  gDebugPortDevice->Signature = DEBUGPORT_DEVICE_SIGNATURE;\r
-\r
-  gDebugPortDevice->DebugPortInterface.Reset = DebugPortReset;\r
-  gDebugPortDevice->DebugPortInterface.Read = DebugPortRead;\r
-  gDebugPortDevice->DebugPortInterface.Write = DebugPortWrite;\r
-  gDebugPortDevice->DebugPortInterface.Poll = DebugPortPoll;\r
-\r
-  gDebugPortDevice->BaudRate = DEBUGPORT_UART_DEFAULT_BAUDRATE;\r
-  gDebugPortDevice->ReceiveFifoDepth = DEBUGPORT_UART_DEFAULT_FIFO_DEPTH;\r
-  gDebugPortDevice->Timeout = DEBUGPORT_UART_DEFAULT_TIMEOUT;\r
-  gDebugPortDevice->Parity = (EFI_PARITY_TYPE) DEBUGPORT_UART_DEFAULT_PARITY;\r
-  gDebugPortDevice->DataBits = DEBUGPORT_UART_DEFAULT_DATA_BITS;\r
-  gDebugPortDevice->StopBits = (EFI_STOP_BITS_TYPE) DEBUGPORT_UART_DEFAULT_STOP_BITS;\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
-  Checks to see if there's not already a DebugPort interface somewhere. \r
+  Checks to see if there's not already a DebugPort interface somewhere.\r
 \r
   If there's a DEBUGPORT variable, the device path must match exactly.  If there's\r
   no DEBUGPORT variable, then device path is not checked and does not matter.\r
@@ -206,14 +175,15 @@ DebugPortSupported (
   )\r
 {\r
   EFI_STATUS                Status;\r
-  EFI_DEVICE_PATH_PROTOCOL  *Dp1;\r
-  EFI_DEVICE_PATH_PROTOCOL  *Dp2;\r
+  EFI_DEVICE_PATH_PROTOCOL  *DevicePath;\r
+  EFI_DEVICE_PATH_PROTOCOL  *DebugPortVariable;\r
   EFI_SERIAL_IO_PROTOCOL    *SerialIo;\r
   EFI_DEBUGPORT_PROTOCOL    *DebugPortInterface;\r
   EFI_HANDLE                TempHandle;\r
 \r
   //\r
-  // Check to see that there's not a debugport protocol already published\r
+  // Check to see that there's not a debugport protocol already published,\r
+  // since only one standard UART serial port could be supported by this driver.\r
   //\r
   if (gBS->LocateProtocol (&gEfiDebugPortProtocolGuid, NULL, (VOID **) &DebugPortInterface) != EFI_NOT_FOUND) {\r
     return EFI_UNSUPPORTED;\r
@@ -221,25 +191,19 @@ DebugPortSupported (
   //\r
   // Read DebugPort variable to determine debug port selection and parameters\r
   //\r
-  GetDebugPortVariable (gDebugPortDevice);\r
+  DebugPortVariable = GetDebugPortVariable ();\r
 \r
-  if (gDebugPortDevice->DebugPortVariable != NULL) {\r
+  if (DebugPortVariable != NULL) {\r
     //\r
     // There's a DEBUGPORT variable, so do LocateDevicePath and check to see if\r
     // the closest matching handle matches the controller handle, and if it does,\r
     // check to see that the remaining device path has the DebugPort GUIDed messaging\r
     // device path only.  Otherwise, it's a mismatch and EFI_UNSUPPORTED is returned.\r
     //\r
-    Dp1 = DuplicateDevicePath ((EFI_DEVICE_PATH_PROTOCOL *) gDebugPortDevice->DebugPortVariable);\r
-    if (Dp1 == NULL) {\r
-      return EFI_OUT_OF_RESOURCES;\r
-    }\r
-\r
-    Dp2 = Dp1;\r
-\r
+    DevicePath = DebugPortVariable;\r
     Status = gBS->LocateDevicePath (\r
                     &gEfiSerialIoProtocolGuid,\r
-                    &Dp2,\r
+                    &DevicePath,\r
                     &TempHandle\r
                     );\r
 \r
@@ -247,15 +211,19 @@ DebugPortSupported (
       Status = EFI_UNSUPPORTED;\r
     }\r
 \r
-    if (Status == EFI_SUCCESS && (Dp2->Type != 3 || Dp2->SubType != 10 || *((UINT16 *) Dp2->Length) != 20)) {\r
+    if (Status == EFI_SUCCESS &&\r
+        (DevicePath->Type != MESSAGING_DEVICE_PATH ||\r
+         DevicePath->SubType != MSG_VENDOR_DP ||\r
+         *((UINT16 *) DevicePath->Length) != sizeof (DEBUGPORT_DEVICE_PATH))) {\r
+\r
       Status = EFI_UNSUPPORTED;\r
     }\r
 \r
-    if (Status == EFI_SUCCESS && CompareMem (&gEfiDebugPortDevicePathGuid, Dp2 + 1, sizeof (EFI_GUID))) {\r
+    if (Status == EFI_SUCCESS && !CompareGuid (&gEfiDebugPortDevicePathGuid, (GUID *) (DevicePath + 1))) {\r
       Status = EFI_UNSUPPORTED;\r
     }\r
 \r
-    FreePool (Dp1);\r
+    FreePool (DebugPortVariable);\r
     if (EFI_ERROR (Status)) {\r
       return Status;\r
     }\r
@@ -294,7 +262,7 @@ DebugPortSupported (
 \r
   @retval EFI_SUCCESS          This driver is added to ControllerHandle.\r
   @retval EFI_OUT_OF_RESOURCES Fails to allocate memory for device.\r
-  @retval others               Some error occurs.                \r
+  @retval others               Some error occurs.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -313,7 +281,7 @@ DebugPortStart (
   Status = gBS->OpenProtocol (\r
                   ControllerHandle,\r
                   &gEfiSerialIoProtocolGuid,\r
-                  (VOID **) &gDebugPortDevice->SerialIoBinding,\r
+                  (VOID **) &mDebugPortDevice.SerialIoBinding,\r
                   This->DriverBindingHandle,\r
                   ControllerHandle,\r
                   EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE\r
@@ -322,34 +290,34 @@ DebugPortStart (
     return Status;\r
   }\r
 \r
-  gDebugPortDevice->SerialIoDeviceHandle = ControllerHandle;\r
+  mDebugPortDevice.SerialIoDeviceHandle = ControllerHandle;\r
 \r
   //\r
   // Initialize the Serial Io interface...\r
   //\r
-  Status = gDebugPortDevice->SerialIoBinding->SetAttributes (\r
-                                                gDebugPortDevice->SerialIoBinding,\r
-                                                gDebugPortDevice->BaudRate,\r
-                                                gDebugPortDevice->ReceiveFifoDepth,\r
-                                                gDebugPortDevice->Timeout,\r
-                                                gDebugPortDevice->Parity,\r
-                                                gDebugPortDevice->DataBits,\r
-                                                gDebugPortDevice->StopBits\r
+  Status = mDebugPortDevice.SerialIoBinding->SetAttributes (\r
+                                                mDebugPortDevice.SerialIoBinding,\r
+                                                mDebugPortDevice.BaudRate,\r
+                                                mDebugPortDevice.ReceiveFifoDepth,\r
+                                                mDebugPortDevice.Timeout,\r
+                                                mDebugPortDevice.Parity,\r
+                                                mDebugPortDevice.DataBits,\r
+                                                mDebugPortDevice.StopBits\r
                                                 );\r
   if (EFI_ERROR (Status)) {\r
-    gDebugPortDevice->BaudRate          = 0;\r
-    gDebugPortDevice->Parity            = DefaultParity;\r
-    gDebugPortDevice->DataBits          = 0;\r
-    gDebugPortDevice->StopBits          = DefaultStopBits;\r
-    gDebugPortDevice->ReceiveFifoDepth  = 0;\r
-    Status = gDebugPortDevice->SerialIoBinding->SetAttributes (\r
-                                                  gDebugPortDevice->SerialIoBinding,\r
-                                                  gDebugPortDevice->BaudRate,\r
-                                                  gDebugPortDevice->ReceiveFifoDepth,\r
-                                                  gDebugPortDevice->Timeout,\r
-                                                  gDebugPortDevice->Parity,\r
-                                                  gDebugPortDevice->DataBits,\r
-                                                  gDebugPortDevice->StopBits\r
+    mDebugPortDevice.BaudRate          = 0;\r
+    mDebugPortDevice.Parity            = DefaultParity;\r
+    mDebugPortDevice.DataBits          = 0;\r
+    mDebugPortDevice.StopBits          = DefaultStopBits;\r
+    mDebugPortDevice.ReceiveFifoDepth  = 0;\r
+    Status = mDebugPortDevice.SerialIoBinding->SetAttributes (\r
+                                                  mDebugPortDevice.SerialIoBinding,\r
+                                                  mDebugPortDevice.BaudRate,\r
+                                                  mDebugPortDevice.ReceiveFifoDepth,\r
+                                                  mDebugPortDevice.Timeout,\r
+                                                  mDebugPortDevice.Parity,\r
+                                                  mDebugPortDevice.DataBits,\r
+                                                  mDebugPortDevice.StopBits\r
                                                   );\r
     if (EFI_ERROR (Status)) {\r
       gBS->CloseProtocol (\r
@@ -362,7 +330,7 @@ DebugPortStart (
     }\r
   }\r
 \r
-  gDebugPortDevice->SerialIoBinding->Reset (gDebugPortDevice->SerialIoBinding);\r
+  mDebugPortDevice.SerialIoBinding->Reset (mDebugPortDevice.SerialIoBinding);\r
 \r
   //\r
   // Create device path instance for DebugPort\r
@@ -370,7 +338,7 @@ DebugPortStart (
   DebugPortDP.Header.Type     = MESSAGING_DEVICE_PATH;\r
   DebugPortDP.Header.SubType  = MSG_VENDOR_DP;\r
   SetDevicePathNodeLength (&(DebugPortDP.Header), sizeof (DebugPortDP));\r
-  CopyMem (&DebugPortDP.Guid, &gEfiDebugPortDevicePathGuid, sizeof (EFI_GUID));\r
+  CopyGuid (&DebugPortDP.Guid, &gEfiDebugPortDevicePathGuid);\r
 \r
   Dp1 = DevicePathFromHandle (ControllerHandle);\r
   if (Dp1 == NULL) {\r
@@ -378,19 +346,19 @@ DebugPortStart (
     SetDevicePathEndNode (Dp1);\r
   }\r
 \r
-  gDebugPortDevice->DebugPortDevicePath = AppendDevicePathNode (Dp1, (EFI_DEVICE_PATH_PROTOCOL *) &DebugPortDP);\r
-  if (gDebugPortDevice->DebugPortDevicePath == NULL) {\r
+  mDebugPortDevice.DebugPortDevicePath = AppendDevicePathNode (Dp1, (EFI_DEVICE_PATH_PROTOCOL *) &DebugPortDP);\r
+  if (mDebugPortDevice.DebugPortDevicePath == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
   //\r
   // Publish DebugPort and Device Path protocols\r
   //\r
   Status = gBS->InstallMultipleProtocolInterfaces (\r
-                  &gDebugPortDevice->DebugPortDeviceHandle,\r
+                  &mDebugPortDevice.DebugPortDeviceHandle,\r
                   &gEfiDevicePathProtocolGuid,\r
-                  gDebugPortDevice->DebugPortDevicePath,\r
+                  mDebugPortDevice.DebugPortDevicePath,\r
                   &gEfiDebugPortProtocolGuid,\r
-                  &gDebugPortDevice->DebugPortInterface,\r
+                  &mDebugPortDevice.DebugPortInterface,\r
                   NULL\r
                   );\r
 \r
@@ -409,25 +377,13 @@ DebugPortStart (
   Status = gBS->OpenProtocol (\r
                   ControllerHandle,\r
                   &gEfiSerialIoProtocolGuid,\r
-                  (VOID **) &gDebugPortDevice->SerialIoBinding,\r
+                  (VOID **) &mDebugPortDevice.SerialIoBinding,\r
                   This->DriverBindingHandle,\r
-                  gDebugPortDevice->DebugPortDeviceHandle,\r
+                  mDebugPortDevice.DebugPortDeviceHandle,\r
                   EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
                   );\r
 \r
   if (EFI_ERROR (Status)) {\r
-    DEBUG_CODE_BEGIN ();\r
-      UINTN  BufferSize;\r
-\r
-      BufferSize = 48;\r
-      DebugPortWrite (\r
-        &gDebugPortDevice->DebugPortInterface,\r
-        0,\r
-        &BufferSize,\r
-        "DebugPort driver failed to open child controller\n\n"\r
-        );\r
-    DEBUG_CODE_END ();\r
-\r
     gBS->CloseProtocol (\r
           ControllerHandle,\r
           &gEfiSerialIoProtocolGuid,\r
@@ -437,18 +393,6 @@ DebugPortStart (
     return Status;\r
   }\r
 \r
-  DEBUG_CODE_BEGIN ();\r
-    UINTN  BufferSize;\r
-\r
-    BufferSize = 38;\r
-    DebugPortWrite (\r
-      &gDebugPortDevice->DebugPortInterface,\r
-      0,\r
-      &BufferSize,\r
-      "Hello World from the DebugPort driver\n\n"\r
-      );\r
-  DEBUG_CODE_END ();\r
-\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -488,7 +432,7 @@ DebugPortStop (
           ControllerHandle\r
           );\r
 \r
-    gDebugPortDevice->SerialIoBinding = NULL;\r
+    mDebugPortDevice.SerialIoBinding = NULL;\r
 \r
     gBS->CloseProtocol (\r
           ControllerHandle,\r
@@ -497,7 +441,7 @@ DebugPortStop (
           ControllerHandle\r
           );\r
 \r
-    FreePool (gDebugPortDevice->DebugPortDevicePath);\r
+    FreePool (mDebugPortDevice.DebugPortDevicePath);\r
 \r
     return EFI_SUCCESS;\r
   } else {\r
@@ -505,10 +449,10 @@ DebugPortStop (
     // Disconnect SerialIo child handle\r
     //\r
     Status = gBS->CloseProtocol (\r
-                    gDebugPortDevice->SerialIoDeviceHandle,\r
+                    mDebugPortDevice.SerialIoDeviceHandle,\r
                     &gEfiSerialIoProtocolGuid,\r
                     This->DriverBindingHandle,\r
-                    gDebugPortDevice->DebugPortDeviceHandle\r
+                    mDebugPortDevice.DebugPortDeviceHandle\r
                     );\r
 \r
     if (EFI_ERROR (Status)) {\r
@@ -518,11 +462,11 @@ DebugPortStop (
     // Unpublish our protocols (DevicePath, DebugPort)\r
     //\r
     Status = gBS->UninstallMultipleProtocolInterfaces (\r
-                    gDebugPortDevice->DebugPortDeviceHandle,\r
+                    mDebugPortDevice.DebugPortDeviceHandle,\r
                     &gEfiDevicePathProtocolGuid,\r
-                    gDebugPortDevice->DebugPortDevicePath,\r
+                    mDebugPortDevice.DebugPortDevicePath,\r
                     &gEfiDebugPortProtocolGuid,\r
-                    &gDebugPortDevice->DebugPortInterface,\r
+                    &mDebugPortDevice.DebugPortInterface,\r
                     NULL\r
                     );\r
 \r
@@ -530,13 +474,13 @@ DebugPortStop (
       gBS->OpenProtocol (\r
             ControllerHandle,\r
             &gEfiSerialIoProtocolGuid,\r
-            (VOID **) &gDebugPortDevice->SerialIoBinding,\r
+            (VOID **) &mDebugPortDevice.SerialIoBinding,\r
             This->DriverBindingHandle,\r
-            gDebugPortDevice->DebugPortDeviceHandle,\r
+            mDebugPortDevice.DebugPortDeviceHandle,\r
             EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
             );\r
     } else {\r
-      gDebugPortDevice->DebugPortDeviceHandle = NULL;\r
+      mDebugPortDevice.DebugPortDeviceHandle = NULL;\r
     }\r
   }\r
 \r
@@ -551,11 +495,11 @@ DebugPortStop (
 \r
   The port itself should be fine since it was set up during initialization.\r
 \r
-  @param  This              Protocol instance pointer.   \r
+  @param  This              Protocol instance pointer.\r
 \r
   @return EFI_SUCCESS       Always.\r
 \r
-**/  \r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 DebugPortReset (\r
@@ -583,8 +527,8 @@ DebugPortReset (
                               On output, the amount of data actually written.\r
   @param  Buffer              Pointer to buffer to read.\r
 \r
-  @retval EFI_SUCCESS         \r
-  @retval others              \r
+  @retval EFI_SUCCESS\r
+  @retval others\r
 \r
 **/\r
 EFI_STATUS\r
@@ -604,6 +548,7 @@ DebugPortRead (
   DebugPortDevice = DEBUGPORT_DEVICE_FROM_THIS (This);\r
   BufferPtr       = Buffer;\r
   LocalBufferSize = *BufferSize;\r
+\r
   do {\r
     Status = DebugPortDevice->SerialIoBinding->Read (\r
                                                 DebugPortDevice->SerialIoBinding,\r
@@ -695,7 +640,7 @@ DebugPortWrite (
                               DebugPort interface\r
   @retval EFI_DEVICE_ERROR    A hardware failure occured... (from SerialIo)\r
 \r
-**/   \r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 DebugPortPoll (\r
@@ -733,7 +678,6 @@ DebugPortPoll (
 \r
   @retval EFI_SUCCESS     Unload Debug Port driver successfully.\r
   @retval EFI_ABORTED     Serial IO is still binding.\r
-  @retval others          Fails to unload Debug Port driver.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -743,31 +687,59 @@ ImageUnloadHandler (
   )\r
 {\r
   EFI_STATUS  Status;\r
+  VOID        *ComponentName;\r
+  VOID        *ComponentName2;\r
 \r
-  if (gDebugPortDevice->SerialIoBinding != NULL) {\r
+  if (mDebugPortDevice.SerialIoBinding != NULL) {\r
     return EFI_ABORTED;\r
   }\r
 \r
-  Status = gBS->UninstallMultipleProtocolInterfaces (\r
-                  ImageHandle,\r
-                  &gEfiDriverBindingProtocolGuid,\r
-                  &gDebugPortDevice->DriverBindingInterface,\r
-                  &gEfiComponentNameProtocolGuid,\r
-                  &gDebugPortDevice->ComponentNameInterface,\r
-                  NULL\r
-                  );\r
-\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
   //\r
-  // Clean up allocations\r
+  // Driver is stopped already.\r
   //\r
-  if (gDebugPortDevice->DebugPortVariable != NULL) {\r
-    FreePool (gDebugPortDevice->DebugPortVariable);\r
+  Status = gBS->HandleProtocol (ImageHandle, &gEfiComponentNameProtocolGuid, &ComponentName);\r
+  if (EFI_ERROR (Status)) {\r
+    ComponentName = NULL;\r
   }\r
 \r
-  FreePool (gDebugPortDevice);\r
+  Status = gBS->HandleProtocol (ImageHandle, &gEfiComponentName2ProtocolGuid, &ComponentName2);\r
+  if (EFI_ERROR (Status)) {\r
+    ComponentName2 = NULL;\r
+  }\r
 \r
-  return EFI_SUCCESS;\r
+  if (ComponentName == NULL) {\r
+    if (ComponentName2 == NULL) {\r
+      Status = gBS->UninstallMultipleProtocolInterfaces (\r
+                      ImageHandle,\r
+                      &gEfiDriverBindingProtocolGuid,  &gDebugPortDriverBinding,\r
+                      NULL\r
+                      );\r
+    } else {\r
+      Status = gBS->UninstallMultipleProtocolInterfaces (\r
+                      ImageHandle,\r
+                      &gEfiDriverBindingProtocolGuid,  &gDebugPortDriverBinding,\r
+                      &gEfiComponentName2ProtocolGuid, ComponentName2,\r
+                      NULL\r
+                      );\r
+    }\r
+  } else {\r
+    if (ComponentName2 == NULL) {\r
+      Status = gBS->UninstallMultipleProtocolInterfaces (\r
+                      ImageHandle,\r
+                      &gEfiDriverBindingProtocolGuid,  &gDebugPortDriverBinding,\r
+                      &gEfiComponentNameProtocolGuid,  ComponentName,\r
+                      NULL\r
+                      );\r
+    } else {\r
+      Status = gBS->UninstallMultipleProtocolInterfaces (\r
+                      ImageHandle,\r
+                      &gEfiDriverBindingProtocolGuid,  &gDebugPortDriverBinding,\r
+                      &gEfiComponentNameProtocolGuid,  ComponentName,\r
+                      &gEfiComponentName2ProtocolGuid, ComponentName2,\r
+                      NULL\r
+                      );\r
+    }\r
+  }\r
+\r
+  return Status;\r
 }\r