Fixed potential issues to release resources when error occurs.
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / UhciDxe / Uhci.c
index 82142b6..b525100 100644 (file)
@@ -2133,6 +2133,7 @@ UhciDriverBindingStart (
   USB_HC_DEV          *Uhc;\r
   UINT64              Supports;\r
   UINT64              OriginalPciAttributes;\r
+  BOOLEAN             PciAttributesSaved;\r
 \r
   //\r
   // Open PCIIO, then enable the EHC device and turn off emulation\r
@@ -2151,6 +2152,7 @@ UhciDriverBindingStart (
     return Status;\r
   }\r
 \r
+  PciAttributesSaved = FALSE;\r
   //\r
   // Save original PCI attributes\r
   //\r
@@ -2162,8 +2164,9 @@ UhciDriverBindingStart (
                     );\r
 \r
   if (EFI_ERROR (Status)) {\r
-    return Status;\r
+    goto CLOSE_PCIIO;\r
   }\r
+  PciAttributesSaved = TRUE;\r
 \r
   UhciTurnOffUsbEmulation (PciIo);\r
 \r
@@ -2262,15 +2265,17 @@ FREE_UHC:
   UhciFreeDev (Uhc);\r
 \r
 CLOSE_PCIIO:\r
-  //\r
-  // Restore original PCI attributes\r
-  //\r
-  PciIo->Attributes (\r
-                  PciIo,\r
-                  EfiPciIoAttributeOperationSet,\r
-                  OriginalPciAttributes,\r
-                  NULL\r
-                  );\r
+  if (PciAttributesSaved == TRUE) {\r
+    //\r
+    // Restore original PCI attributes\r
+    //\r
+    PciIo->Attributes (\r
+                    PciIo,\r
+                    EfiPciIoAttributeOperationSet,\r
+                    OriginalPciAttributes,\r
+                    NULL\r
+                    );\r
+  }\r
 \r
   gBS->CloseProtocol (\r
         Controller,\r