]> git.proxmox.com Git - mirror_edk2.git/blobdiff - NetworkPkg/IScsiDxe/IScsiMisc.c
NetworkPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / NetworkPkg / IScsiDxe / IScsiMisc.c
index 94f3725866611763c70d557348e377a482a44e0d..38ad67917c300d0369c4df0466b421210a07917c 100644 (file)
@@ -2,13 +2,7 @@
   Miscellaneous routines for iSCSI driver.\r
 \r
 Copyright (c) 2004 - 2018, 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
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
@@ -20,7 +14,7 @@ GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8  IScsiHexString[] = "0123456789ABCDEFa
   Removes (trims) specified leading and trailing characters from a string.\r
 \r
   @param[in, out] Str   Pointer to the null-terminated string to be trimmed.\r
-                        On return, Str will hold the trimmed string. \r
+                        On return, Str will hold the trimmed string.\r
 \r
   @param[in]      CharC Character will be trimmed from str.\r
 \r
@@ -33,18 +27,18 @@ IScsiStrTrim (
 {\r
   CHAR16  *Pointer1;\r
   CHAR16  *Pointer2;\r
-  \r
+\r
   if (*Str == 0) {\r
     return ;\r
   }\r
-  \r
+\r
   //\r
   // Trim off the leading and trailing characters c\r
   //\r
   for (Pointer1 = Str; (*Pointer1 != 0) && (*Pointer1 == CharC); Pointer1++) {\r
     ;\r
   }\r
-  \r
+\r
   Pointer2 = Str;\r
   if (Pointer2 == Pointer1) {\r
     while (*Pointer1 != 0) {\r
@@ -52,19 +46,19 @@ IScsiStrTrim (
       Pointer1++;\r
     }\r
   } else {\r
-    while (*Pointer1 != 0) {    \r
-    *Pointer2 = *Pointer1;    \r
+    while (*Pointer1 != 0) {\r
+    *Pointer2 = *Pointer1;\r
     Pointer1++;\r
     Pointer2++;\r
     }\r
     *Pointer2 = 0;\r
   }\r
-  \r
-  \r
+\r
+\r
   for (Pointer1 = Str + StrLen(Str) - 1; Pointer1 >= Str && *Pointer1 == CharC; Pointer1--) {\r
     ;\r
   }\r
-  if  (Pointer1 !=  Str + StrLen(Str) - 1) { \r
+  if  (Pointer1 !=  Str + StrLen(Str) - 1) {\r
     *(Pointer1 + 1) = 0;\r
   }\r
 }\r
@@ -131,11 +125,11 @@ IScsiAsciiStrToLun (
   CHAR8   TemStr[2];\r
   UINT8   TemValue;\r
   UINT16  Value[4];\r
-  \r
+\r
   ZeroMem (Lun, 8);\r
   ZeroMem (TemStr, 2);\r
   ZeroMem ((UINT8 *) Value, sizeof (Value));\r
-  SizeStr    = AsciiStrLen (Str);  \r
+  SizeStr    = AsciiStrLen (Str);\r
   IndexValue = 0;\r
   IndexNum   = 0;\r
 \r
@@ -150,7 +144,7 @@ IScsiAsciiStrToLun (
         return EFI_INVALID_PARAMETER;\r
       }\r
     }\r
-    \r
+\r
     if ((TemValue == 0) && (TemStr[0] == '-')) {\r
       //\r
       // Next Lun value.\r
@@ -167,24 +161,24 @@ IScsiAsciiStrToLun (
       IndexNum = 0;\r
       continue;\r
     }\r
-    \r
+\r
     if (++IndexNum > 4) {\r
-      //     \r
+      //\r
       // Each Lun Str can't exceed size 4, because it will be as UINT16 value.\r
       //\r
       return EFI_INVALID_PARAMETER;\r
     }\r
-    \r
+\r
     //\r
     // Combine UINT16 value.\r
     //\r
     Value[IndexValue] = (UINT16) ((Value[IndexValue] << 4) + TemValue);\r
   }\r
\r
+\r
   for (Index = 0; Index <= IndexValue; Index ++) {\r
     *((UINT16 *) &Lun[Index * 2]) =  HTONS (Value[Index]);\r
   }\r
-  \r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -319,7 +313,7 @@ IScsiMacAddrToStr (
   @param[in, out]  HexStr      Pointer to the string.\r
   @param[in, out]  HexLength   The length of the string.\r
 \r
-  @retval EFI_SUCCESS          The binary data is converted to the hexadecimal string \r
+  @retval EFI_SUCCESS          The binary data is converted to the hexadecimal string\r
                                and the length of the string is updated.\r
   @retval EFI_BUFFER_TOO_SMALL The string is too small.\r
   @retval EFI_INVALID_PARAMETER The IP string is malformatted.\r
@@ -385,7 +379,7 @@ IScsiHexToBin (
   UINTN   Length;\r
   UINT8   Digit;\r
   CHAR8   TemStr[2];\r
-  \r
+\r
   ZeroMem (TemStr, sizeof (TemStr));\r
 \r
   //\r
@@ -394,7 +388,7 @@ IScsiHexToBin (
   if ((HexStr[0] == '0') && ((HexStr[1] == 'x') || (HexStr[1] == 'X'))) {\r
     HexStr += 2;\r
   }\r
-  \r
+\r
   Length = AsciiStrLen (HexStr);\r
 \r
   for (Index = 0; Index < Length; Index ++) {\r
@@ -412,7 +406,7 @@ IScsiHexToBin (
       BinBuffer [Index/2] = (UINT8) ((BinBuffer [Index/2] << 4) + Digit);\r
     }\r
   }\r
-  \r
+\r
   *BinLength = (UINT32) ((Index + 1)/2);\r
 \r
   return EFI_SUCCESS;\r
@@ -492,7 +486,7 @@ IScsiCheckIpv6Support (
   BOOLEAN                          Supported;\r
   VOID                             *InfoBlock;\r
   UINTN                            InfoBlockSize;\r
-  \r
+\r
   EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL *Nii;\r
 \r
   ASSERT (Ipv6Support != NULL);\r
@@ -515,7 +509,7 @@ IScsiCheckIpv6Support (
 \r
   //\r
   // Get the NIC handle by SNP protocol.\r
-  //  \r
+  //\r
   Handle = NetLibGetSnpHandle (ControllerHandle, NULL);\r
   if (Handle == NULL) {\r
     return EFI_NOT_FOUND;\r
@@ -561,11 +555,11 @@ IScsiCheckIpv6Support (
   if (EFI_ERROR (Status) || InfoBlock == NULL) {\r
     FreePool (InfoBlock);\r
     return EFI_NOT_FOUND;\r
-  }  \r
+  }\r
 \r
   *Ipv6Support = ((EFI_ADAPTER_INFO_UNDI_IPV6_SUPPORT *) InfoBlock)->Ipv6Support;\r
   FreePool (InfoBlock);\r
-  \r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -615,19 +609,19 @@ IScsiAddNic (
         CompareMem (&NicInfo->PermanentAddress, MacAddr.Addr, HwAddressSize) == 0 &&\r
         NicInfo->VlanId == VlanId) {\r
       mPrivate->CurrentNic = NicInfo->NicIndex;\r
-         \r
-           //\r
+\r
+      //\r
       // Set IPv6 available flag.\r
-      // \r
+      //\r
       Status = IScsiCheckIpv6Support (Controller, Image, &NicInfo->Ipv6Available);\r
       if (EFI_ERROR (Status)) {\r
         //\r
-        // Fail to get the data whether UNDI supports IPv6. \r
+        // Fail to get the data whether UNDI supports IPv6.\r
         // Set default value to TRUE.\r
         //\r
         NicInfo->Ipv6Available = TRUE;\r
       }\r
-         \r
+\r
       return EFI_SUCCESS;\r
     }\r
 \r
@@ -649,19 +643,19 @@ IScsiAddNic (
   NicInfo->VlanId         = VlanId;\r
   NicInfo->NicIndex       = (UINT8) (mPrivate->MaxNic + 1);\r
   mPrivate->MaxNic        = NicInfo->NicIndex;\r
-  \r
+\r
   //\r
   // Set IPv6 available flag.\r
-  // \r
+  //\r
   Status = IScsiCheckIpv6Support (Controller, Image, &NicInfo->Ipv6Available);\r
   if (EFI_ERROR (Status)) {\r
     //\r
-    // Fail to get the data whether UNDI supports IPv6. \r
+    // Fail to get the data whether UNDI supports IPv6.\r
     // Set default value to TRUE.\r
     //\r
     NicInfo->Ipv6Available = TRUE;\r
   }\r
-  \r
+\r
   //\r
   // Get the PCI location.\r
   //\r
@@ -845,6 +839,10 @@ IScsiCreateAttempts (
                     );\r
     FreePool (AttemptConfigOrder);\r
     if (EFI_ERROR (Status)) {\r
+      DEBUG ((DEBUG_ERROR,\r
+        "%a: Failed to set 'InitialAttemptOrder' with Guid (%g): "\r
+        "%r\n",\r
+        __FUNCTION__, &gIScsiConfigGuid, Status));\r
       return Status;\r
     }\r
 \r
@@ -887,6 +885,10 @@ IScsiCreateAttempts (
                     );\r
     FreePool (AttemptConfigData);\r
     if (EFI_ERROR (Status)) {\r
+        DEBUG ((DEBUG_ERROR,\r
+          "%a: Failed to set variable (mPrivate->PortString) with Guid (%g): "\r
+          "%r\n",\r
+          __FUNCTION__, &gEfiIScsiInitiatorNameProtocolGuid, Status));\r
       return Status;\r
     }\r
   }\r
@@ -1799,7 +1801,7 @@ IScsiCleanDriverData (
 \r
 EXIT:\r
   if (Private->ExitBootServiceEvent != NULL) {\r
-    gBS->CloseEvent (Private->ExitBootServiceEvent); \r
+    gBS->CloseEvent (Private->ExitBootServiceEvent);\r
   }\r
 \r
   mCallbackInfo->Current = NULL;\r
@@ -1813,10 +1815,10 @@ EXIT:
 \r
   @param[in]  Controller           The handle of the controller.\r
   @param[in]  IpVersion            IP_VERSION_4 or IP_VERSION_6.\r
-  \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
 **/\r
 BOOLEAN\r
 IScsiDhcpIsConfigured (\r
@@ -1835,7 +1837,7 @@ IScsiDhcpIsConfigured (
   CHAR16                      MacString[ISCSI_MAX_MAC_STRING_LEN];\r
   CHAR16                      AttemptMacString[ISCSI_MAX_MAC_STRING_LEN];\r
   CHAR16                      AttemptName[ISCSI_NAME_IFR_MAX_SIZE];\r
-  \r
+\r
   AttemptConfigOrder = IScsiGetVariableAndSize (\r
                          L"AttemptOrder",\r
                          &gIScsiConfigGuid,\r
@@ -1844,7 +1846,7 @@ IScsiDhcpIsConfigured (
   if (AttemptConfigOrder == NULL || AttemptConfigOrderSize == 0) {\r
     return FALSE;\r
   }\r
-  \r
+\r
   //\r
   // Get MAC address of this network device.\r
   //\r
@@ -1857,7 +1859,7 @@ IScsiDhcpIsConfigured (
   //\r
   VlanId = NetLibGetVlanId (Controller);\r
   IScsiMacAddrToStr (&MacAddr, (UINT32) HwAddressSize, VlanId, MacString);\r
-  \r
+\r
   for (Index = 0; Index < AttemptConfigOrderSize / sizeof (UINT8); Index++) {\r
     UnicodeSPrint (\r
       AttemptName,\r
@@ -1874,7 +1876,7 @@ IScsiDhcpIsConfigured (
     if(AttemptTmp == NULL || EFI_ERROR (Status)) {\r
       continue;\r
     }\r
-    \r
+\r
     ASSERT (AttemptConfigOrder[Index] == AttemptTmp->AttemptConfigIndex);\r
 \r
     if (AttemptTmp->SessionConfigData.Enabled == ISCSI_DISABLED) {\r
@@ -1882,7 +1884,7 @@ IScsiDhcpIsConfigured (
       continue;\r
     }\r
 \r
-    if (AttemptTmp->SessionConfigData.IpMode != IP_MODE_AUTOCONFIG && \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
@@ -1896,7 +1898,7 @@ IScsiDhcpIsConfigured (
 \r
     if(AttemptTmp->SessionConfigData.IpMode == IP_MODE_AUTOCONFIG ||\r
        AttemptTmp->SessionConfigData.InitiatorInfoFromDhcp == TRUE ||\r
-       AttemptTmp->SessionConfigData.TargetInfoFromDhcp == TRUE) { \r
+       AttemptTmp->SessionConfigData.TargetInfoFromDhcp == TRUE) {\r
       FreePool (AttemptTmp);\r
       FreePool (AttemptConfigOrder);\r
       return TRUE;\r
@@ -1904,7 +1906,7 @@ IScsiDhcpIsConfigured (
 \r
     FreePool (AttemptTmp);\r
   }\r
-  \r
+\r
   FreePool (AttemptConfigOrder);\r
   return FALSE;\r
 }\r
@@ -1913,10 +1915,10 @@ IScsiDhcpIsConfigured (
   Check whether the Controller handle is configured to use DNS protocol.\r
 \r
   @param[in]  Controller           The handle of the controller.\r
-  \r
+\r
   @retval TRUE                     The handle of the controller need the Dns protocol.\r
   @retval FALSE                    The handle of the controller does not need the Dns protocol.\r
-  \r
+\r
 **/\r
 BOOLEAN\r
 IScsiDnsIsConfigured (\r
@@ -1934,7 +1936,7 @@ IScsiDnsIsConfigured (
   CHAR16                      AttemptMacString[ISCSI_MAX_MAC_STRING_LEN];\r
   CHAR16                      MacString[ISCSI_MAX_MAC_STRING_LEN];\r
   CHAR16                      AttemptName[ISCSI_NAME_IFR_MAX_SIZE];\r
-  \r
+\r
   AttemptConfigOrder = IScsiGetVariableAndSize (\r
                          L"AttemptOrder",\r
                          &gIScsiConfigGuid,\r
@@ -1943,7 +1945,7 @@ IScsiDnsIsConfigured (
   if (AttemptConfigOrder == NULL || AttemptConfigOrderSize == 0) {\r
     return FALSE;\r
   }\r
-  \r
+\r
   //\r
   // Get MAC address of this network device.\r
   //\r
@@ -1956,7 +1958,7 @@ IScsiDnsIsConfigured (
   //\r
   VlanId = NetLibGetVlanId (Controller);\r
   IScsiMacAddrToStr (&MacAddr, (UINT32) HwAddressSize, VlanId, MacString);\r
-  \r
+\r
   for (Index = 0; Index < AttemptConfigOrderSize / sizeof (UINT8); Index++) {\r
     UnicodeSPrint (\r
       AttemptName,\r
@@ -1974,7 +1976,7 @@ IScsiDnsIsConfigured (
     if(AttemptTmp == NULL || EFI_ERROR (Status)) {\r
       continue;\r
     }\r
-    \r
+\r
     ASSERT (AttemptConfigOrder[Index] == AttemptTmp->AttemptConfigIndex);\r
 \r
     AsciiStrToUnicodeStrS (AttemptTmp->MacString, AttemptMacString, sizeof (AttemptMacString) / sizeof (AttemptMacString[0]));\r
@@ -1983,8 +1985,8 @@ IScsiDnsIsConfigured (
       FreePool (AttemptTmp);\r
       continue;\r
     }\r
-    \r
-    if (AttemptTmp->SessionConfigData.DnsMode) {\r
+\r
+    if (AttemptTmp->SessionConfigData.DnsMode || AttemptTmp->SessionConfigData.TargetInfoFromDhcp) {\r
       FreePool (AttemptTmp);\r
       FreePool (AttemptConfigOrder);\r
       return TRUE;\r
@@ -2060,7 +2062,7 @@ IScsiGetConfigData (
     //\r
     // Check whether the attempt exists in AttemptConfig.\r
     //\r
-    AttemptTmp = IScsiConfigGetAttemptByConfigIndex (AttemptConfigOrder[Index]);    \r
+    AttemptTmp = IScsiConfigGetAttemptByConfigIndex (AttemptConfigOrder[Index]);\r
     if (AttemptTmp != NULL && AttemptTmp->SessionConfigData.Enabled == ISCSI_DISABLED) {\r
       continue;\r
     } else if (AttemptTmp != NULL && AttemptTmp->SessionConfigData.Enabled != ISCSI_DISABLED) {\r
@@ -2126,11 +2128,11 @@ IScsiGetConfigData (
 \r
           continue;\r
         }\r
-      } else if (AttemptTmp->SessionConfigData.InitiatorInfoFromDhcp && \r
-                 !AttemptTmp->ValidPath && \r
+      } else if (AttemptTmp->SessionConfigData.InitiatorInfoFromDhcp &&\r
+                 !AttemptTmp->ValidPath &&\r
                  AttemptTmp->NicIndex == mPrivate->CurrentNic) {\r
         //\r
-        // If the attempt associates with the current NIC, we can \r
+        // If the attempt associates with the current NIC, we can\r
         // get DHCP information for already added, but failed, attempt.\r
         //\r
         AttemptTmp->DhcpSuccess = FALSE;\r
@@ -2213,7 +2215,7 @@ IScsiGetConfigData (
         (UINT8) (mPrivate->Ipv6Flag ? IP_MODE_AUTOCONFIG_IP6 : IP_MODE_AUTOCONFIG_IP4);\r
       AttemptConfigData->AutoConfigureSuccess = FALSE;\r
     }\r
-    \r
+\r
     //\r
     // Get some information from dhcp server.\r
     //\r
@@ -2384,7 +2386,7 @@ IScsiGetTcpConnDevicePath (
                   Conn->TcpIo.Handle,\r
                   &gEfiDevicePathProtocolGuid,\r
                   (VOID **) &DevicePath\r
-                  );  \r
+                  );\r
   if (EFI_ERROR (Status)) {\r
     return NULL;\r
   }\r
@@ -2403,7 +2405,7 @@ IScsiGetTcpConnDevicePath (
       if (!Conn->Ipv6Flag && DevicePathSubType (&DPathNode->DevPath) == MSG_IPv4_DP) {\r
         DPathNode->Ipv4.LocalPort       = 0;\r
 \r
-        DPathNode->Ipv4.StaticIpAddress = \r
+        DPathNode->Ipv4.StaticIpAddress =\r
           (BOOLEAN) (!Session->ConfigData->SessionConfigData.InitiatorInfoFromDhcp);\r
 \r
         //\r
@@ -2414,9 +2416,9 @@ IScsiGetTcpConnDevicePath (
         //\r
 \r
         PathLen = DevicePathNodeLength (&DPathNode->Ipv4);\r
-        \r
-        if (PathLen == IP4_NODE_LEN_NEW_VERSIONS) {  \r
-            \r
+\r
+        if (PathLen == IP4_NODE_LEN_NEW_VERSIONS) {\r
+\r
           IP4_COPY_ADDRESS (\r
             &DPathNode->Ipv4.GatewayIpAddress,\r
             &Session->ConfigData->SessionConfigData.Gateway\r
@@ -2427,7 +2429,7 @@ IScsiGetTcpConnDevicePath (
             &Session->ConfigData->SessionConfigData.SubnetMask\r
             );\r
         }\r
-        \r
+\r
         break;\r
       } else if (Conn->Ipv6Flag && DevicePathSubType (&DPathNode->DevPath) == MSG_IPv6_DP) {\r
         DPathNode->Ipv6.LocalPort       = 0;\r
@@ -2436,28 +2438,28 @@ IScsiGetTcpConnDevicePath (
         //  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
+        //  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
+        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
+        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
+          //  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
+          *((UINT8 *)(&DPathNode->Ipv6) + IP6_OLD_IPADDRESS_OFFSET) =\r
             (BOOLEAN) (!Session->ConfigData->SessionConfigData.InitiatorInfoFromDhcp);\r
         }\r
-        \r
+\r
         break;\r
       }\r
     }\r
@@ -2486,10 +2488,10 @@ IScsiOnExitBootService (
   ISCSI_DRIVER_DATA *Private;\r
 \r
   Private = (ISCSI_DRIVER_DATA *) Context;\r
-  \r
+\r
   gBS->CloseEvent (Private->ExitBootServiceEvent);\r
   Private->ExitBootServiceEvent = NULL;\r
-  \r
+\r
   if (Private->Session != NULL) {\r
     IScsiSessionAbort (Private->Session);\r
   }\r
@@ -2502,7 +2504,7 @@ IScsiOnExitBootService (
   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
+  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