Fixed potential issues to release resources when error occurs.
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / UndiRuntimeDxe / Init.c
index 3e3391e..413ef32 100644 (file)
@@ -1,6 +1,6 @@
 /*++\r
 \r
-Copyright (c) 2006, Intel Corporation\r
+Copyright (c) 2006 - 2007, Intel Corporation\r
 All rights reserved. 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
@@ -26,10 +26,9 @@ Revision History
 //\r
 // Global Variables\r
 //\r
-PXE_SW_UNDI             *pxe = 0;     // 3.0 entry point\r
-PXE_SW_UNDI             *pxe_31 = 0;  // 3.1 entry\r
-UNDI32_DEV              *UNDI32DeviceList[MAX_NIC_INTERFACES];\r
 \r
+PXE_SW_UNDI             *pxe_31 = NULL;  // 3.1 entry\r
+UNDI32_DEV              *UNDI32DeviceList[MAX_NIC_INTERFACES];\r
 NII_TABLE               *UndiDataPointer = NULL;\r
 \r
 VOID\r
@@ -288,6 +287,7 @@ Returns:
   EFI_PCI_IO_PROTOCOL       *PciIoFncs;\r
   UINTN                     Len;\r
   UINT64                    Supports;\r
+  BOOLEAN                   PciAttributesSaved;\r
 \r
   Status = gBS->OpenProtocol (\r
                   Controller,\r
@@ -322,6 +322,8 @@ Returns:
     return Status;\r
   }\r
 \r
+  PciAttributesSaved = FALSE;\r
+\r
   Status = gBS->AllocatePool (\r
                   EfiRuntimeServicesData,\r
                   sizeof (UNDI32_DEV),\r
@@ -345,8 +347,9 @@ Returns:
                     );\r
 \r
   if (EFI_ERROR (Status)) {\r
-    return Status;\r
+    goto UndiErrorDeleteDevice;\r
   }\r
+  PciAttributesSaved = TRUE;\r
 \r
   //\r
   // allocate and initialize both (old and new) the !pxe structures here,\r
@@ -381,16 +384,10 @@ Returns:
     } else {\r
       pxe_31 = (PXE_SW_UNDI *) TmpPxePointer;\r
     }\r
-    //\r
-    // assuming that the sizeof pxe_31 is a 16 byte multiple\r
-    //\r
-    pxe = (PXE_SW_UNDI *) ((CHAR8 *) (pxe_31) + sizeof (PXE_SW_UNDI));\r
 \r
-    PxeStructInit (pxe, 0x30);\r
-    PxeStructInit (pxe_31, 0x31);\r
+    PxeStructInit (pxe_31);\r
   }\r
 \r
-  UNDI32Device->NIIProtocol.ID    = (UINT64) (UINTN) (pxe);\r
   UNDI32Device->NIIProtocol_31.ID = (UINT64) (UINTN) (pxe_31);\r
 \r
   Status = PciIoFncs->Attributes (\r
@@ -454,15 +451,13 @@ Returns:
   // the IfNum index for the current interface will be the total number\r
   // of interfaces initialized so far\r
   //\r
-  UNDI32Device->NIIProtocol.IfNum     = pxe->IFcnt;\r
   UNDI32Device->NIIProtocol_31.IfNum  = pxe_31->IFcnt;\r
 \r
-  PxeUpdate (&UNDI32Device->NicInfo, pxe);\r
   PxeUpdate (&UNDI32Device->NicInfo, pxe_31);\r
 \r
-  UNDI32Device->NicInfo.Io_Function                 = PciIoFncs;\r
-  UNDI32DeviceList[UNDI32Device->NIIProtocol.IfNum] = UNDI32Device;\r
-  UNDI32Device->Undi32BaseDevPath                   = UndiDevicePath;\r
+  UNDI32Device->NicInfo.Io_Function                    = PciIoFncs;\r
+  UNDI32DeviceList[UNDI32Device->NIIProtocol_31.IfNum] = UNDI32Device;\r
+  UNDI32Device->Undi32BaseDevPath                      = UndiDevicePath;\r
 \r
   Status = AppendMac2DevPath (\r
             &UNDI32Device->Undi32DevPath,\r
@@ -476,19 +471,6 @@ Returns:
 \r
   UNDI32Device->Signature                     = UNDI_DEV_SIGNATURE;\r
 \r
-  UNDI32Device->NIIProtocol.Revision          = EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL_REVISION;\r
-  UNDI32Device->NIIProtocol.Type              = EfiNetworkInterfaceUndi;\r
-  UNDI32Device->NIIProtocol.MajorVer          = PXE_ROMID_MAJORVER;\r
-  UNDI32Device->NIIProtocol.MinorVer          = PXE_ROMID_MINORVER;\r
-  UNDI32Device->NIIProtocol.ImageSize         = 0;\r
-  UNDI32Device->NIIProtocol.ImageAddr         = 0;\r
-  UNDI32Device->NIIProtocol.Ipv6Supported     = FALSE;\r
-\r
-  UNDI32Device->NIIProtocol.StringId[0]       = 'U';\r
-  UNDI32Device->NIIProtocol.StringId[1]       = 'N';\r
-  UNDI32Device->NIIProtocol.StringId[2]       = 'D';\r
-  UNDI32Device->NIIProtocol.StringId[3]       = 'I';\r
-\r
   UNDI32Device->NIIProtocol_31.Revision       = EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL_REVISION_31;\r
   UNDI32Device->NIIProtocol_31.Type           = EfiNetworkInterfaceUndi;\r
   UNDI32Device->NIIProtocol_31.MajorVer       = PXE_ROMID_MAJORVER;\r
@@ -511,8 +493,6 @@ Returns:
                   &UNDI32Device->DeviceHandle,\r
                   &gEfiNetworkInterfaceIdentifierProtocolGuid_31,\r
                   &UNDI32Device->NIIProtocol_31,\r
-                  &gEfiNetworkInterfaceIdentifierProtocolGuid,\r
-                  &UNDI32Device->NIIProtocol,\r
                   &gEfiDevicePathProtocolGuid,\r
                   UNDI32Device->Undi32DevPath,\r
                   NULL\r
@@ -557,19 +537,16 @@ UndiErrorAllocDataPointer:
                   &UNDI32Device->DeviceHandle,\r
                   &gEfiNetworkInterfaceIdentifierProtocolGuid_31,\r
                   &UNDI32Device->NIIProtocol_31,\r
-                  &gEfiNetworkInterfaceIdentifierProtocolGuid,\r
-                  &UNDI32Device->NIIProtocol,\r
                   &gEfiDevicePathProtocolGuid,\r
                   UNDI32Device->Undi32DevPath,\r
                   NULL\r
                   );\r
 \r
 UndiErrorDeleteDevicePath:\r
-  UNDI32DeviceList[UNDI32Device->NIIProtocol.IfNum] = NULL;\r
+  UNDI32DeviceList[UNDI32Device->NIIProtocol_31.IfNum] = NULL;\r
   gBS->FreePool (UNDI32Device->Undi32DevPath);\r
 \r
 UndiErrorDeletePxe:\r
-  PxeUpdate (NULL, pxe);\r
   PxeUpdate (NULL, pxe_31);\r
   if (TmpPxePointer != NULL) {\r
     gBS->FreePool (TmpPxePointer);\r
@@ -577,15 +554,17 @@ UndiErrorDeletePxe:
   }\r
 \r
 UndiErrorDeleteDevice:\r
-  //\r
-  // Restore original PCI attributes\r
-  //\r
-  PciIoFncs->Attributes (\r
-                  PciIoFncs,\r
-                  EfiPciIoAttributeOperationSet,\r
-                  UNDI32Device->NicInfo.OriginalPciAttributes,\r
-                  NULL\r
-                  );\r
+  if (PciAttributesSaved == TRUE) {\r
+    //\r
+    // Restore original PCI attributes\r
+    //\r
+    PciIoFncs->Attributes (\r
+                    PciIoFncs,\r
+                    EfiPciIoAttributeOperationSet,\r
+                    UNDI32Device->NicInfo.OriginalPciAttributes,\r
+                    NULL\r
+                    );\r
+  }\r
 \r
   gBS->FreePool (UNDI32Device);\r
 \r
@@ -673,7 +652,7 @@ Returns:
 \r
     Status = gBS->OpenProtocol (\r
                     ChildHandleBuffer[Index],\r
-                    &gEfiNetworkInterfaceIdentifierProtocolGuid,\r
+                    &gEfiNetworkInterfaceIdentifierProtocolGuid_31,\r
                     (VOID **) &NIIProtocol,\r
                     This->DriverBindingHandle,\r
                     Controller,\r
@@ -707,8 +686,6 @@ Returns:
                       UNDI32Device->Undi32DevPath,\r
                       &gEfiNetworkInterfaceIdentifierProtocolGuid_31,\r
                       &UNDI32Device->NIIProtocol_31,\r
-                      &gEfiNetworkInterfaceIdentifierProtocolGuid,\r
-                      &UNDI32Device->NIIProtocol,\r
                       NULL\r
                       );\r
 \r