]> git.proxmox.com Git - mirror_edk2.git/blobdiff - NetworkPkg/IScsiDxe/IScsiDriver.c
NetworkPkg: Fix suspicious dereference of pointer before NULL check
[mirror_edk2.git] / NetworkPkg / IScsiDxe / IScsiDriver.c
index 42ac8f0fe778e8b80c8d3335a10c6de0061d5ea3..51ce16911bcb4365a5faf032022f817871d1eab3 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   The entry point of IScsi driver.\r
 \r
-Copyright (c) 2004 - 2014, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2004 - 2015, 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
@@ -74,6 +74,145 @@ IScsiIsDevicePathSupported (
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Check whether an iSCSI HBA adapter already installs an AIP instance with\r
+  network boot policy matching the value specified in PcdIScsiAIPNetworkBootPolicy.\r
+  If yes, return EFI_SUCCESS.\r
+\r
+  @retval EFI_SUCCESS              Found an AIP with matching network boot policy.\r
+  @retval EFI_NOT_FOUND            AIP is unavailable or the network boot policy\r
+                                   not matched.\r
+**/\r
+EFI_STATUS\r
+IScsiCheckAip (\r
+  )\r
+{\r
+  UINTN                            AipHandleCount;\r
+  EFI_HANDLE                       *AipHandleBuffer;\r
+  UINTN                            AipIndex;\r
+  EFI_ADAPTER_INFORMATION_PROTOCOL *Aip;\r
+  EFI_EXT_SCSI_PASS_THRU_PROTOCOL  *ExtScsiPassThru;\r
+  EFI_GUID                         *InfoTypesBuffer;\r
+  UINTN                            InfoTypeBufferCount;\r
+  UINTN                            TypeIndex;\r
+  VOID                             *InfoBlock;\r
+  UINTN                            InfoBlockSize;\r
+  BOOLEAN                          Supported;\r
+  EFI_ADAPTER_INFO_NETWORK_BOOT    *NetworkBoot;\r
+  EFI_STATUS                       Status;\r
+  UINT8                            NetworkBootPolicy;\r
+\r
+  //\r
+  // Check any AIP instances exist in system.\r
+  //\r
+  AipHandleCount  = 0;\r
+  AipHandleBuffer = NULL;\r
+  Status = gBS->LocateHandleBuffer (\r
+                  ByProtocol,\r
+                  &gEfiAdapterInformationProtocolGuid,\r
+                  NULL,\r
+                  &AipHandleCount,\r
+                  &AipHandleBuffer\r
+                  );\r
+  if (EFI_ERROR (Status) || AipHandleCount == 0) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  ASSERT (AipHandleBuffer != NULL);\r
+\r
+  InfoBlock = NULL;\r
+\r
+  for (AipIndex = 0; AipIndex < AipHandleCount; AipIndex++) {\r
+    Status = gBS->HandleProtocol (\r
+                    AipHandleBuffer[AipIndex],\r
+                    &gEfiAdapterInformationProtocolGuid,\r
+                    (VOID *) &Aip\r
+                    );\r
+    ASSERT_EFI_ERROR (Status);\r
+    ASSERT (Aip != NULL);\r
+\r
+    Status = gBS->HandleProtocol (\r
+                    AipHandleBuffer[AipIndex],\r
+                    &gEfiExtScsiPassThruProtocolGuid,\r
+                    (VOID *) &ExtScsiPassThru\r
+                    );\r
+    if (EFI_ERROR (Status) || ExtScsiPassThru == NULL) {\r
+      continue;\r
+    }\r
+\r
+    InfoTypesBuffer     = NULL;\r
+    InfoTypeBufferCount = 0;\r
+    Status = Aip->GetSupportedTypes (Aip, &InfoTypesBuffer, &InfoTypeBufferCount);\r
+    if (EFI_ERROR (Status) || InfoTypesBuffer == NULL) {\r
+      continue;\r
+    }\r
+    //\r
+    // Check whether the AIP instance has Network boot information block.\r
+    //\r
+    Supported = FALSE;\r
+    for (TypeIndex = 0; TypeIndex < InfoTypeBufferCount; TypeIndex++) {\r
+      if (CompareGuid (&InfoTypesBuffer[TypeIndex], &gEfiAdapterInfoNetworkBootGuid)) {\r
+        Supported = TRUE;\r
+        break;\r
+      }\r
+    }\r
+\r
+    FreePool (InfoTypesBuffer);\r
+    if (!Supported) {\r
+      continue;\r
+    }\r
+\r
+    //\r
+    // We now have network boot information block.\r
+    //\r
+    InfoBlock     = NULL;\r
+    InfoBlockSize = 0;\r
+    Status = Aip->GetInformation (Aip, &gEfiAdapterInfoNetworkBootGuid, &InfoBlock, &InfoBlockSize);\r
+    if (EFI_ERROR (Status) || InfoBlock == NULL) {\r
+      continue;\r
+    }\r
+\r
+    //\r
+    // Check whether the network boot policy matches.\r
+    //\r
+    NetworkBoot = (EFI_ADAPTER_INFO_NETWORK_BOOT *) InfoBlock;\r
+    NetworkBootPolicy = PcdGet8 (PcdIScsiAIPNetworkBootPolicy);\r
+\r
+    if (NetworkBootPolicy == STOP_UEFI_ISCSI_IF_HBA_INSTALL_AIP) {\r
+      Status = EFI_SUCCESS;\r
+      goto Exit;\r
+    }\r
+    if (((NetworkBootPolicy & STOP_UEFI_ISCSI_IF_AIP_SUPPORT_IP4) != 0 &&\r
+         !NetworkBoot->iScsiIpv4BootCapablity) ||\r
+         ((NetworkBootPolicy & STOP_UEFI_ISCSI_IF_AIP_SUPPORT_IP6) != 0 &&\r
+         !NetworkBoot->iScsiIpv6BootCapablity) ||\r
+         ((NetworkBootPolicy & STOP_UEFI_ISCSI_IF_AIP_SUPPORT_OFFLOAD) != 0 &&\r
+         !NetworkBoot->OffloadCapability) ||\r
+         ((NetworkBootPolicy & STOP_UEFI_ISCSI_IF_AIP_SUPPORT_MPIO) != 0 &&\r
+         !NetworkBoot->iScsiMpioCapability) ||\r
+         ((NetworkBootPolicy & STOP_UEFI_ISCSI_IF_AIP_CONFIGURED_IP4) != 0 &&\r
+         !NetworkBoot->iScsiIpv4Boot) ||\r
+         ((NetworkBootPolicy & STOP_UEFI_ISCSI_IF_AIP_CONFIGURED_IP6) != 0 &&\r
+         !NetworkBoot->iScsiIpv6Boot)) {\r
+      FreePool (InfoBlock);\r
+      continue;\r
+    }\r
+\r
+    Status = EFI_SUCCESS;\r
+    goto Exit;\r
+  }\r
+\r
+  Status = EFI_NOT_FOUND;\r
+\r
+Exit:\r
+  if (InfoBlock != NULL) {\r
+    FreePool (InfoBlock);\r
+  }\r
+  if (AipHandleBuffer != NULL) {\r
+    FreePool (AipHandleBuffer);\r
+  }\r
+  return Status;\r
+}\r
 \r
 /**\r
   Tests to see if this driver supports a given controller. This is the worker function for\r
@@ -215,6 +354,7 @@ IScsiStart (
   BOOLEAN                         NeedUpdate;\r
   VOID                            *Interface;\r
   EFI_GUID                        *ProtocolGuid;\r
+  UINT8                           NetworkBootPolicy;\r
 \r
   //\r
   // Test to see if iSCSI driver supports the given controller.\r
@@ -256,6 +396,20 @@ IScsiStart (
     return EFI_UNSUPPORTED;\r
   }\r
 \r
+  NetworkBootPolicy = PcdGet8 (PcdIScsiAIPNetworkBootPolicy);\r
+  if (NetworkBootPolicy != ALWAYS_USE_UEFI_ISCSI_AND_IGNORE_AIP) {\r
+    //\r
+    // Check existing iSCSI AIP.\r
+    //\r
+    Status = IScsiCheckAip ();\r
+    if (!EFI_ERROR (Status)) {\r
+      //\r
+      // Find iSCSI AIP with specified network boot policy. return EFI_ABORTED.\r
+      //\r
+      return EFI_ABORTED;\r
+    }\r
+  }\r
+  \r
   //\r
   // Record the incoming NIC info.\r
   //\r
@@ -290,7 +444,7 @@ IScsiStart (
   }\r
 \r
   Status = gBS->OpenProtocol (\r
-                  Private->ChildHandle,\r
+                  Private->ChildHandle, /// Default Tcp child\r
                   ProtocolGuid,\r
                   &Interface,\r
                   Image,\r
@@ -477,7 +631,7 @@ IScsiStart (
     // Don't process the autoconfigure path if it is already established.\r
     //\r
     if (AttemptConfigData->SessionConfigData.IpMode == IP_MODE_AUTOCONFIG &&\r
-        AttemptConfigData->AutoConfigureMode == IP_MODE_AUTOCONFIG_SUCCESS) {\r
+        AttemptConfigData->AutoConfigureSuccess) {\r
       continue;\r
     }\r
 \r
@@ -576,7 +730,7 @@ IScsiStart (
       // IScsi session success. Update the attempt state to NVR.\r
       //\r
       if (AttemptConfigData->SessionConfigData.IpMode == IP_MODE_AUTOCONFIG) {\r
-        AttemptConfigData->AutoConfigureMode = IP_MODE_AUTOCONFIG_SUCCESS;\r
+        AttemptConfigData->AutoConfigureSuccess = TRUE;\r
       }\r
 \r
       gRT->SetVariable (\r
@@ -651,7 +805,9 @@ IScsiStart (
                            &gIScsiConfigGuid,\r
                            &AttemptConfigOrderSize\r
                            );\r
-    ASSERT (AttemptConfigOrder != NULL);\r
+    if (AttemptConfigOrder == NULL) {\r
+      goto ON_ERROR;\r
+    }\r
     for (Index = 0; Index < AttemptConfigOrderSize / sizeof (UINT8); Index++) {\r
       if (AttemptConfigOrder[Index] == mPrivate->BootSelectedIndex ||\r
           AttemptConfigOrder[Index] == BootSelected) {\r
@@ -689,7 +845,9 @@ IScsiStart (
 \r
         goto ON_EXIT;\r
       } else {\r
-        ASSERT (AttemptConfigOrder[Index] == BootSelected);\r
+        if (AttemptConfigOrder[Index] != BootSelected) {\r
+          goto ON_ERROR;\r
+        }\r
         mPrivate->BootSelectedIndex = BootSelected;\r
         //\r
         // Clear the resource in ExistPrivate.\r
@@ -746,6 +904,30 @@ IScsiStart (
     goto ON_ERROR;\r
   }\r
 \r
+  //\r
+  // ISCSI children should share the default Tcp child, just open the default Tcp child via BY_CHILD_CONTROLLER.\r
+  //\r
+  Status = gBS->OpenProtocol (\r
+                  Private->ChildHandle, /// Default Tcp child\r
+                  ProtocolGuid,\r
+                  &Interface,\r
+                  Image,\r
+                  Private->ExtScsiPassThruHandle,\r
+                  EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
+                  );              \r
+  if (EFI_ERROR (Status)) {\r
+    gBS->UninstallMultipleProtocolInterfaces (\r
+           Private->ExtScsiPassThruHandle,\r
+           &gEfiExtScsiPassThruProtocolGuid,\r
+           &Private->IScsiExtScsiPassThru,\r
+           &gEfiDevicePathProtocolGuid,\r
+           Private->DevicePath,\r
+           NULL\r
+           );\r
+    \r
+    goto ON_ERROR;\r
+  }\r
+\r
 ON_EXIT:\r
 \r
   //\r
@@ -834,6 +1016,13 @@ IScsiStop (
       ProtocolGuid = &gEfiTcp6ProtocolGuid;\r
     }\r
 \r
+    gBS->CloseProtocol (\r
+           Private->ChildHandle,\r
+           ProtocolGuid,\r
+           Private->Image,\r
+           Private->ExtScsiPassThruHandle\r
+           );\r
+    \r
     gBS->CloseProtocol (\r
            Conn->TcpIo.Handle,\r
            ProtocolGuid,\r
@@ -843,6 +1032,7 @@ IScsiStop (
 \r
     return EFI_SUCCESS;\r
   }\r
+  \r
   //\r
   // Get the handle of the controller we are controling.\r
   //\r