]> git.proxmox.com Git - mirror_edk2.git/blobdiff - NetworkPkg/IScsiDxe/IScsiDriver.c
NetworkPkg: Replace UnicodeStrToAsciiStr/AsciiStrToUnicodeStr
[mirror_edk2.git] / NetworkPkg / IScsiDxe / IScsiDriver.c
index 5e5d700e47870d7a5f04f2ff8139475a97978511..7e7eb39ee2304afac633916ffbf43c1fc51abfb6 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   The entry point of IScsi driver.\r
 \r
-Copyright (c) 2004 - 2015, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2004 - 2016, 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
@@ -184,7 +323,9 @@ IScsiSupported (
   @retval EFI_INVALID_PARAMETER Any input parameter is invalid.\r
   @retval EFI_NOT_FOUND         There is no sufficient information to establish\r
                                 the iScsi session.\r
-  @retval EFI_DEVICE_ERROR      Failed to get TCP connection device path.                              \r
+  @retval EFI_DEVICE_ERROR      Failed to get TCP connection device path.\r
+  @retval EFI_ACCESS_DENIED     The protocol could not be removed from the Handle\r
+                                because its interfaces are being used.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -215,6 +356,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 +398,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
@@ -477,7 +633,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
@@ -515,7 +671,7 @@ IScsiStart (
     Session->ConfigData = AttemptConfigData;\r
     Session->AuthType   = AttemptConfigData->AuthenticationType;\r
 \r
-    AsciiStrToUnicodeStr (AttemptConfigData->MacString, MacString);\r
+    AsciiStrToUnicodeStrS (AttemptConfigData->MacString, MacString, sizeof (MacString) / sizeof (MacString[0]));\r
     UnicodeSPrint (\r
       mPrivate->PortString,\r
       (UINTN) ISCSI_NAME_IFR_MAX_SIZE,\r
@@ -576,7 +732,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
@@ -709,7 +865,22 @@ IScsiStart (
           IScsiSessionAbort (ExistPrivate->Session);\r
         }\r
 \r
-        IScsiCleanDriverData (ExistPrivate);\r
+        if (ExistPrivate->DevicePath != NULL) {\r
+          Status = gBS->UninstallProtocolInterface (\r
+                          ExistPrivate->ExtScsiPassThruHandle,\r
+                          &gEfiDevicePathProtocolGuid,\r
+                          ExistPrivate->DevicePath\r
+                          );\r
+          if (EFI_ERROR (Status)) {\r
+            goto ON_ERROR;\r
+          }\r
+\r
+          FreePool (ExistPrivate->DevicePath);\r
+        }\r
+\r
+        gBS->CloseEvent (ExistPrivate->ExitBootServiceEvent);\r
+        FreePool (ExistPrivate);\r
+\r
       }\r
     } else {\r
       //\r
@@ -809,6 +980,9 @@ ON_ERROR:
   \r
   @retval EFI_SUCCESS           The device was stopped.\r
   @retval EFI_DEVICE_ERROR      The device could not be stopped due to a device error.\r
+  @retval EFI_INVALID_PARAMETER Child handle is NULL.\r
+  @retval EFI_ACCESS_DENIED     The protocol could not be removed from the Handle\r
+                                because its interfaces are being used.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -951,7 +1125,11 @@ IScsiStop (
     IScsiSessionAbort (Private->Session);\r
   }\r
 \r
-  IScsiCleanDriverData (Private);\r
+  Status = IScsiCleanDriverData (Private);\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
 \r
   return EFI_SUCCESS;\r
 }\r