]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Network/IScsiDxe/IScsiMisc.c
MdeModulePkg: Clean up source files
[mirror_edk2.git] / MdeModulePkg / Universal / Network / IScsiDxe / IScsiMisc.c
index 61c407e6fff780d21dc2b23791635a6e20f3892e..d924a8b82427ef372ad3325cbcf3e0e29ce43518 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Miscellaneous routines for iSCSI driver.\r
 \r
-Copyright (c) 2004 - 2015, Intel Corporation. All rights reserved.<BR>\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
@@ -19,8 +19,8 @@ GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8  IScsiHexString[] = "0123456789ABCDEFa
 /**\r
   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. On return, \r
-                        Str will hold the trimmed string. \r
+  @param[in, out]  Str  Pointer to the null-terminated string to be trimmed. On return,\r
+                        Str will hold the trimmed string.\r
 \r
   @param[in]      CharC Character will be trimmed from str.\r
 **/\r
@@ -32,18 +32,18 @@ StrTrim (
 {\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
@@ -51,19 +51,19 @@ StrTrim (
       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
@@ -109,7 +109,7 @@ IScsiGetSubnetMaskPrefixLength (
 }\r
 \r
 /**\r
-  Convert the hexadecimal encoded LUN string into the 64-bit LUN. \r
+  Convert the hexadecimal encoded LUN string into the 64-bit LUN.\r
 \r
   @param[in]   Str             The hexadecimal encoded LUN string.\r
   @param[out]  Lun             Storage to return the 64-bit LUN.\r
@@ -127,11 +127,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
@@ -146,7 +146,7 @@ IScsiAsciiStrToLun (
         return EFI_INVALID_PARAMETER;\r
       }\r
     }\r
-    \r
+\r
     if ((TemValue == 0) && (TemStr[0] == '-')) {\r
       //\r
       // Next Lun value\r
@@ -163,24 +163,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
@@ -392,7 +392,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
@@ -455,7 +455,7 @@ IScsiHexToBin (
   UINTN   Length;\r
   UINT8   Digit;\r
   CHAR8   TemStr[2];\r
-  \r
+\r
   ZeroMem (TemStr, sizeof (TemStr));\r
 \r
   //\r
@@ -464,7 +464,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
@@ -482,7 +482,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
@@ -587,44 +587,59 @@ 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
 \r
   if (Private->ExtScsiPassThruHandle != NULL) {\r
-    gBS->UninstallProtocolInterface (\r
-          Private->ExtScsiPassThruHandle,\r
-          &gEfiExtScsiPassThruProtocolGuid,\r
-          &Private->IScsiExtScsiPassThru\r
-          );\r
+    Status = gBS->UninstallProtocolInterface (\r
+                    Private->ExtScsiPassThruHandle,\r
+                    &gEfiExtScsiPassThruProtocolGuid,\r
+                    &Private->IScsiExtScsiPassThru\r
+                    );\r
   }\r
 \r
-  gBS->CloseEvent (Private->ExitBootServiceEvent);\r
+EXIT:\r
+  if (Private->ExitBootServiceEvent != NULL) {\r
+    gBS->CloseEvent (Private->ExitBootServiceEvent);\r
+  }\r
 \r
   FreePool (Private);\r
+  return Status;\r
 }\r
 \r
 /**\r
   Check wheather the Controller is configured to use DHCP protocol.\r
 \r
   @param[in]  Controller           The handle of the controller.\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
@@ -811,18 +826,27 @@ IScsiGetTcpConnDevicePath (
         ) {\r
 \r
       DPathNode->Ipv4.LocalPort       = 0;\r
-      DPathNode->Ipv4.StaticIpAddress = \r
+      DPathNode->Ipv4.StaticIpAddress =\r
         (BOOLEAN) (!Session->ConfigData.NvData.InitiatorInfoFromDhcp);\r
 \r
-      IP4_COPY_ADDRESS (\r
-        &DPathNode->Ipv4.GatewayIpAddress,\r
-        &Session->ConfigData.NvData.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
+      if (DevicePathNodeLength (&DPathNode->Ipv4) == IP4_NODE_LEN_NEW_VERSIONS) {\r
 \r
-      IP4_COPY_ADDRESS (\r
-        &DPathNode->Ipv4.SubnetMask,\r
-        &Session->ConfigData.NvData.SubnetMask\r
-        );\r
+        IP4_COPY_ADDRESS (\r
+          &DPathNode->Ipv4.GatewayIpAddress,\r
+          &Session->ConfigData.NvData.Gateway\r
+          );\r
+\r
+        IP4_COPY_ADDRESS (\r
+          &DPathNode->Ipv4.SubnetMask,\r
+          &Session->ConfigData.NvData.SubnetMask\r
+          );\r
+      }\r
 \r
       break;\r
     }\r
@@ -849,7 +873,9 @@ IScsiOnExitBootService (
   ISCSI_DRIVER_DATA *Private;\r
 \r
   Private = (ISCSI_DRIVER_DATA *) Context;\r
+\r
   gBS->CloseEvent (Private->ExitBootServiceEvent);\r
+  Private->ExitBootServiceEvent = NULL;\r
 \r
   IScsiSessionAbort (&Private->Session);\r
 }\r
@@ -861,7 +887,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