]> git.proxmox.com Git - mirror_edk2.git/blobdiff - NetworkPkg/IScsiDxe/IScsiMisc.c
MdeModulePkg-DxeCore: rename CoreGetMemoryMapPropertiesTable
[mirror_edk2.git] / NetworkPkg / IScsiDxe / IScsiMisc.c
index 46e97a9fcd66be2e09e16e491b60a7f6f6b18dc9..a1f2672f3709725a0fa79acddea07535b976f0b2 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Miscellaneous routines for iSCSI driver.\r
 \r
-Copyright (c) 2004 - 2012, 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
@@ -209,7 +209,7 @@ IScsiLunToUnicodeStr (
   for (Index = 0; Index < 4; Index++) {\r
 \r
     if ((Lun[2 * Index] | Lun[2 * Index + 1]) == 0) {\r
-      StrCpy (TempStr, L"0-");\r
+      CopyMem (TempStr, L"0-", sizeof (L"0-"));\r
     } else {\r
       TempStr[0]  = (CHAR16) IScsiHexString[Lun[2 * Index] >> 4];\r
       TempStr[1]  = (CHAR16) IScsiHexString[Lun[2 * Index] & 0x0F];\r
@@ -223,7 +223,10 @@ IScsiLunToUnicodeStr (
 \r
     TempStr += StrLen (TempStr);\r
   }\r
-\r
+  //\r
+  // Remove the last '-'\r
+  //\r
+  ASSERT (StrLen(Str) >= 1);\r
   Str[StrLen (Str) - 1] = 0;\r
 \r
   for (Index = StrLen (Str) - 1; Index > 1; Index = Index - 2) {\r
@@ -680,7 +683,7 @@ IScsiGetNicInfoByIndex (
 \r
 \r
 /**\r
-  Get the NIC's PCI location and return it accroding to the composited\r
+  Get the NIC's PCI location and return it according to the composited\r
   format defined in iSCSI Boot Firmware Table.\r
 \r
   @param[in]   Controller        The handle of the controller.\r
@@ -854,22 +857,30 @@ IScsiCreateDriverData (
 /**\r
   Clean the iSCSI driver data.\r
 \r
-  @param[in]  Private The iSCSI driver data.\r
+  @param[in]              Private The iSCSI driver data.\r
+\r
+  @retval EFI_SUCCESS     The clean operation is successful.\r
+  @retval Others          Other errors as indicated.\r
 \r
 **/\r
-VOID\r
+EFI_STATUS\r
 IScsiCleanDriverData (\r
   IN ISCSI_DRIVER_DATA  *Private\r
   )\r
 {\r
   EFI_STATUS            Status;\r
 \r
+  Status = EFI_SUCCESS;\r
+\r
   if (Private->DevicePath != NULL) {\r
-    gBS->UninstallProtocolInterface (\r
-           Private->ExtScsiPassThruHandle,\r
-           &gEfiDevicePathProtocolGuid,\r
-           Private->DevicePath\r
-           );\r
+    Status = gBS->UninstallProtocolInterface (\r
+                    Private->ExtScsiPassThruHandle,\r
+                    &gEfiDevicePathProtocolGuid,\r
+                    Private->DevicePath\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      goto EXIT;\r
+    }\r
 \r
     FreePool (Private->DevicePath);\r
   }\r
@@ -885,11 +896,108 @@ IScsiCleanDriverData (
     }\r
   }\r
 \r
+EXIT:\r
+\r
   gBS->CloseEvent (Private->ExitBootServiceEvent);\r
 \r
   FreePool (Private);\r
+  return Status;\r
 }\r
 \r
+/**\r
+  Check wheather the Controller handle is configured to use DHCP protocol.\r
+\r
+  @param[in]  Controller           The handle of the controller.\r
+  @param[in]  IpVersion            IP_VERSION_4 or IP_VERSION_6.\r
+  \r
+  @retval TRUE                     The handle of the controller need the Dhcp protocol.\r
+  @retval FALSE                    The handle of the controller does not need the Dhcp protocol.\r
+  \r
+**/\r
+BOOLEAN\r
+IScsiDhcpIsConfigured (\r
+  IN EFI_HANDLE  Controller,\r
+  IN UINT8       IpVersion\r
+  )\r
+{\r
+  ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptTmp;\r
+  UINT8                       *AttemptConfigOrder;\r
+  UINTN                       AttemptConfigOrderSize;\r
+  UINTN                       Index;\r
+  EFI_STATUS                  Status;\r
+  EFI_MAC_ADDRESS             MacAddr;\r
+  UINTN                       HwAddressSize;\r
+  UINT16                      VlanId;\r
+  CHAR16                      MacString[ISCSI_MAX_MAC_STRING_LEN];\r
+  CHAR16                      AttemptName[ISCSI_NAME_IFR_MAX_SIZE];\r
+  \r
+  AttemptConfigOrder = IScsiGetVariableAndSize (\r
+                         L"AttemptOrder",\r
+                         &gIScsiConfigGuid,\r
+                         &AttemptConfigOrderSize\r
+                         );\r
+  if (AttemptConfigOrder == NULL || AttemptConfigOrderSize == 0) {\r
+    return FALSE;\r
+  }\r
+  \r
+  //\r
+  // Get MAC address of this network device.\r
+  //\r
+  Status = NetLibGetMacAddress (Controller, &MacAddr, &HwAddressSize);\r
+  if(EFI_ERROR (Status)) {\r
+    return FALSE;\r
+  }\r
+  //\r
+  // Get VLAN ID of this network device.\r
+  //\r
+  VlanId = NetLibGetVlanId (Controller);\r
+  IScsiMacAddrToStr (&MacAddr, (UINT32) HwAddressSize, VlanId, MacString);\r
+  \r
+  for (Index = 0; Index < AttemptConfigOrderSize / sizeof (UINT8); Index++) {\r
+    UnicodeSPrint (\r
+      AttemptName,\r
+      (UINTN) 128,\r
+      L"%s%d",\r
+      MacString,\r
+      (UINTN) AttemptConfigOrder[Index]\r
+      );\r
+    Status = GetVariable2 (\r
+               AttemptName,\r
+               &gEfiIScsiInitiatorNameProtocolGuid,\r
+               (VOID**)&AttemptTmp,\r
+               NULL\r
+               );\r
+    if(AttemptTmp == NULL || EFI_ERROR (Status)) {\r
+      continue;\r
+    }\r
+    \r
+    ASSERT (AttemptConfigOrder[Index] == AttemptTmp->AttemptConfigIndex);\r
+\r
+    if (AttemptTmp->SessionConfigData.Enabled == ISCSI_DISABLED) {\r
+      FreePool (AttemptTmp);\r
+      continue;\r
+    }\r
+\r
+    if (AttemptTmp->SessionConfigData.IpMode != IP_MODE_AUTOCONFIG && \r
+        AttemptTmp->SessionConfigData.IpMode != ((IpVersion == IP_VERSION_4) ? IP_MODE_IP4 : IP_MODE_IP6)) {\r
+      FreePool (AttemptTmp);\r
+      continue;\r
+    }\r
+    \r
+    if(AttemptTmp->SessionConfigData.IpMode == IP_MODE_AUTOCONFIG ||\r
+       AttemptTmp->SessionConfigData.InitiatorInfoFromDhcp == TRUE ||\r
+       AttemptTmp->SessionConfigData.TargetInfoFromDhcp == TRUE) { \r
+      FreePool (AttemptTmp);\r
+      FreePool (AttemptConfigOrder);\r
+      return TRUE;\r
+    }\r
+\r
+    FreePool (AttemptTmp);\r
+  }\r
+  \r
+  FreePool (AttemptConfigOrder);\r
+  return FALSE;\r
+}\r
 \r
 /**\r
   Get the various configuration data.\r
@@ -957,7 +1065,7 @@ IScsiGetConfigData (
       // Check the autoconfig path to see whether it should be retried.\r
       //\r
       if (AttemptTmp->SessionConfigData.IpMode == IP_MODE_AUTOCONFIG &&\r
-          AttemptTmp->AutoConfigureMode != IP_MODE_AUTOCONFIG_SUCCESS) {\r
+          !AttemptTmp->AutoConfigureSuccess) {\r
         if (mPrivate->Ipv6Flag &&\r
             AttemptTmp->AutoConfigureMode == IP_MODE_AUTOCONFIG_IP6) {\r
           //\r
@@ -1079,7 +1187,7 @@ IScsiGetConfigData (
     GetVariable2 (\r
                  mPrivate->PortString,\r
                  &gEfiIScsiInitiatorNameProtocolGuid,\r
-                 &AttemptConfigData,\r
+                 (VOID**)&AttemptConfigData,\r
                  NULL\r
                  );\r
 \r
@@ -1100,6 +1208,7 @@ IScsiGetConfigData (
 \r
       AttemptConfigData->AutoConfigureMode =\r
         (UINT8) (mPrivate->Ipv6Flag ? IP_MODE_AUTOCONFIG_IP6 : IP_MODE_AUTOCONFIG_IP4);\r
+      AttemptConfigData->AutoConfigureSuccess = FALSE;\r
     }\r
     \r
     //\r
@@ -1255,6 +1364,7 @@ IScsiGetTcpConnDevicePath (
   EFI_DEVICE_PATH_PROTOCOL  *DevicePath;\r
   EFI_STATUS                Status;\r
   EFI_DEV_PATH              *DPathNode;\r
+  UINTN                     PathLen;\r
 \r
   if (Session->State != SESSION_STATE_LOGGED_IN) {\r
     return NULL;\r
@@ -1293,21 +1403,58 @@ IScsiGetTcpConnDevicePath (
         DPathNode->Ipv4.StaticIpAddress = \r
           (BOOLEAN) (!Session->ConfigData->SessionConfigData.InitiatorInfoFromDhcp);\r
 \r
-        IP4_COPY_ADDRESS (\r
-          &DPathNode->Ipv4.GatewayIpAddress,\r
-          &Session->ConfigData->SessionConfigData.Gateway\r
-          );\r
+        //\r
+        //  Add a judgement here to support previous versions of IPv4_DEVICE_PATH.\r
+        //  In previous versions of IPv4_DEVICE_PATH, GatewayIpAddress and SubnetMask\r
+        //  do not exist.\r
+        //  In new version of IPv4_DEVICE_PATH, structcure length is 27.\r
+        //\r
 \r
-        IP4_COPY_ADDRESS (\r
-          &DPathNode->Ipv4.SubnetMask,\r
-          &Session->ConfigData->SessionConfigData.SubnetMask\r
-          );\r
+        PathLen = DevicePathNodeLength (&DPathNode->Ipv4);\r
+        \r
+        if (PathLen == IP4_NODE_LEN_NEW_VERSIONS) {  \r
+            \r
+          IP4_COPY_ADDRESS (\r
+            &DPathNode->Ipv4.GatewayIpAddress,\r
+            &Session->ConfigData->SessionConfigData.Gateway\r
+            );\r
+\r
+          IP4_COPY_ADDRESS (\r
+            &DPathNode->Ipv4.SubnetMask,\r
+            &Session->ConfigData->SessionConfigData.SubnetMask\r
+            );\r
+        }\r
+        \r
         break;\r
       } else if (Conn->Ipv6Flag && DevicePathSubType (&DPathNode->DevPath) == MSG_IPv6_DP) {\r
         DPathNode->Ipv6.LocalPort       = 0;\r
-        DPathNode->Ipv6.IpAddressOrigin = 0;\r
-        DPathNode->Ipv6.PrefixLength    = IP6_PREFIX_LENGTH;\r
-        ZeroMem (&DPathNode->Ipv6.GatewayIpAddress, sizeof (EFI_IPv6_ADDRESS));\r
+\r
+        //\r
+        //  Add a judgement here to support previous versions of IPv6_DEVICE_PATH.\r
+        //  In previous versions of IPv6_DEVICE_PATH, IpAddressOrigin, PrefixLength\r
+        //  and GatewayIpAddress do not exist.\r
+        //  In new version of IPv6_DEVICE_PATH, structure length is 60, while in \r
+        //  old versions, the length is 43.\r
+        //\r
+\r
+        PathLen = DevicePathNodeLength (&DPathNode->Ipv6);\r
+        \r
+        if (PathLen == IP6_NODE_LEN_NEW_VERSIONS ) { \r
+\r
+          DPathNode->Ipv6.IpAddressOrigin = 0;\r
+          DPathNode->Ipv6.PrefixLength    = IP6_PREFIX_LENGTH;\r
+          ZeroMem (&DPathNode->Ipv6.GatewayIpAddress, sizeof (EFI_IPv6_ADDRESS));\r
+        }\r
+        else if (PathLen == IP6_NODE_LEN_OLD_VERSIONS) { \r
+\r
+          //\r
+          //  StaticIPAddress is a field in old versions of IPv6_DEVICE_PATH, while ignored in new \r
+          //  version. Set StaticIPAddress through its' offset in old IPv6_DEVICE_PATH.\r
+          //\r
+          *((UINT8 *)(&DPathNode->Ipv6) + IP6_OLD_IPADDRESS_OFFSET) = \r
+            (BOOLEAN) (!Session->ConfigData->SessionConfigData.InitiatorInfoFromDhcp);\r
+        }\r
+        \r
         break;\r
       }\r
     }\r
@@ -1342,3 +1489,70 @@ IScsiOnExitBootService (
     IScsiSessionAbort (Private->Session);\r
   }\r
 }\r
+\r
+/**\r
+  Tests whether a controller handle is being managed by IScsi driver.\r
+\r
+  This function tests whether the driver specified by DriverBindingHandle is\r
+  currently managing the controller specified by ControllerHandle.  This test\r
+  is performed by evaluating if the the protocol specified by ProtocolGuid is\r
+  present on ControllerHandle and is was opened by DriverBindingHandle and Nic\r
+  Device handle with an attribute of EFI_OPEN_PROTOCOL_BY_DRIVER. \r
+  If ProtocolGuid is NULL, then ASSERT().\r
+\r
+  @param  ControllerHandle     A handle for a controller to test.\r
+  @param  DriverBindingHandle  Specifies the driver binding handle for the\r
+                               driver.\r
+  @param  ProtocolGuid         Specifies the protocol that the driver specified\r
+                               by DriverBindingHandle opens in its Start()\r
+                               function.\r
+\r
+  @retval EFI_SUCCESS          ControllerHandle is managed by the driver\r
+                               specified by DriverBindingHandle.\r
+  @retval EFI_UNSUPPORTED      ControllerHandle is not managed by the driver\r
+                               specified by DriverBindingHandle.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+IScsiTestManagedDevice (\r
+  IN  EFI_HANDLE       ControllerHandle,\r
+  IN  EFI_HANDLE       DriverBindingHandle,\r
+  IN  EFI_GUID         *ProtocolGuid\r
+  )\r
+{\r
+  EFI_STATUS     Status;\r
+  VOID           *ManagedInterface;\r
+  EFI_HANDLE     NicControllerHandle;\r
+\r
+  ASSERT (ProtocolGuid != NULL);\r
+\r
+  NicControllerHandle = NetLibGetNicHandle (ControllerHandle, ProtocolGuid);\r
+  if (NicControllerHandle == NULL) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
+  Status = gBS->OpenProtocol (\r
+                  ControllerHandle,\r
+                  (EFI_GUID *) ProtocolGuid,\r
+                  &ManagedInterface,\r
+                  DriverBindingHandle,\r
+                  NicControllerHandle,\r
+                  EFI_OPEN_PROTOCOL_BY_DRIVER\r
+                  );\r
+  if (!EFI_ERROR (Status)) {\r
+    gBS->CloseProtocol (\r
+           ControllerHandle,\r
+           (EFI_GUID *) ProtocolGuid,\r
+           DriverBindingHandle,\r
+           NicControllerHandle\r
+           );\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
+  if (Status != EFI_ALREADY_STARTED) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r