]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Network/IScsiDxe/IScsiMisc.c
Fix a K9 error.Result of GetVariable2() function that may return NULL will be derefer...
[mirror_edk2.git] / MdeModulePkg / Universal / Network / IScsiDxe / IScsiMisc.c
index 1683d88a9e9204484928a875719bb8e07c0aa0e7..2e43b415ec593b899d16426991d0aca73229805f 100644 (file)
@@ -1,7 +1,8 @@
-/*++\r
+/** @file\r
+  Miscellaneous routines for iSCSI driver.\r
 \r
-Copyright (c) 2004 - 2007, Intel Corporation\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2004 - 2013, 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
@@ -9,139 +10,76 @@ http://opensource.org/licenses/bsd-license.php
 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
 \r
-Module Name:\r
-\r
-  IScsiMisc.c\r
-\r
-Abstract:\r
-\r
-  Miscellaneous routines for iSCSI driver.\r
-\r
---*/\r
+**/\r
 \r
 #include "IScsiImpl.h"\r
 \r
-STATIC CONST CHAR8  IScsiHexString[] = "0123456789ABCDEFabcdef";\r
-\r
-static\r
-BOOLEAN\r
-IsHexDigit (\r
-  OUT UINT8      *Digit,\r
-  IN  CHAR16      Char\r
-  )\r
-/*++\r
-\r
-  Routine Description:\r
-    Determines if a Unicode character is a hexadecimal digit.\r
-    The test is case insensitive.\r
-\r
-  Arguments:\r
-    Digit - Pointer to byte that receives the value of the hex character.\r
-    Char  - Unicode character to test.\r
-\r
-  Returns:\r
-    TRUE  - If the character is a hexadecimal digit.\r
-    FALSE - Otherwise.\r
-\r
---*/\r
-{\r
-  if ((Char >= L'0') && (Char <= L'9')) {\r
-    *Digit = (UINT8) (Char - L'0');\r
-    return TRUE;\r
-  }\r
-\r
-  if ((Char >= L'A') && (Char <= L'F')) {\r
-    *Digit = (UINT8) (Char - L'A' + 0x0A);\r
-    return TRUE;\r
-  }\r
+GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8  IScsiHexString[] = "0123456789ABCDEFabcdef";\r
 \r
-  if ((Char >= L'a') && (Char <= L'f')) {\r
-    *Digit = (UINT8) (Char - L'a' + 0x0A);\r
-    return TRUE;\r
-  }\r
+/**\r
+  Removes (trims) specified leading and trailing characters from a string.\r
 \r
-  return FALSE;\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
 \r
-static\r
+  @param[in]      CharC Character will be trimmed from str.\r
+**/\r
 VOID\r
 StrTrim (\r
-  IN OUT CHAR16   *str,\r
+  IN OUT CHAR16   *Str,\r
   IN     CHAR16   CharC\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-  \r
-  Removes (trims) specified leading and trailing characters from a string.\r
-  \r
-Arguments: \r
-  \r
-  str     - Pointer to the null-terminated string to be trimmed. On return, \r
-            str will hold the trimmed string. \r
-  CharC       - Character will be trimmed from str.\r
-  \r
-Returns:\r
-\r
---*/\r
 {\r
-  CHAR16  *p1;\r
-  CHAR16  *p2;\r
+  CHAR16  *Pointer1;\r
+  CHAR16  *Pointer2;\r
   \r
-  if (*str == 0) {\r
+  if (*Str == 0) {\r
     return;\r
   }\r
   \r
   //\r
   // Trim off the leading and trailing characters c\r
   //\r
-  for (p1 = str; *p1 && *p1 == CharC; p1++) {\r
+  for (Pointer1 = Str; (*Pointer1 != 0) && (*Pointer1 == CharC); Pointer1++) {\r
     ;\r
   }\r
   \r
-  p2 = str;\r
-  if (p2 == p1) {\r
-    while (*p1) {\r
-      p2++;\r
-      p1++;\r
+  Pointer2 = Str;\r
+  if (Pointer2 == Pointer1) {\r
+    while (*Pointer1 != 0) {\r
+      Pointer2++;\r
+      Pointer1++;\r
     }\r
   } else {\r
-    while (*p1) {    \r
-    *p2 = *p1;    \r
-    p1++;\r
-    p2++;\r
+    while (*Pointer1 != 0) {    \r
+    *Pointer2 = *Pointer1;    \r
+    Pointer1++;\r
+    Pointer2++;\r
     }\r
-    *p2 = 0;\r
+    *Pointer2 = 0;\r
   }\r
   \r
   \r
-  for (p1 = str + StrLen(str) - 1; p1 >= str && *p1 == CharC; p1--) {\r
+  for (Pointer1 = Str + StrLen(Str) - 1; Pointer1 >= Str && *Pointer1 == CharC; Pointer1--) {\r
     ;\r
   }\r
-  if  (p1 !=  str + StrLen(str) - 1) { \r
-    *(p1 + 1) = 0;\r
+  if  (Pointer1 !=  Str + StrLen(Str) - 1) { \r
+    *(Pointer1 + 1) = 0;\r
   }\r
 }\r
 \r
+/**\r
+  Calculate the prefix length of the IPv4 subnet mask.\r
+\r
+  @param[in]  SubnetMask The IPv4 subnet mask.\r
+\r
+  @return The prefix length of the subnet mask.\r
+  @retval 0 Other errors as indicated.\r
+**/\r
 UINT8\r
 IScsiGetSubnetMaskPrefixLength (\r
   IN EFI_IPv4_ADDRESS  *SubnetMask\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Calculate the prefix length of the IPv4 subnet mask.\r
-\r
-Arguments:\r
-\r
-  SubnetMask - The IPv4 subnet mask.\r
-\r
-Returns:\r
-\r
-  The prefix length of the subnet mask.\r
-\r
---*/\r
 {\r
   UINT8   Len;\r
   UINT32  ReverseMask;\r
@@ -156,7 +94,7 @@ Returns:
   //\r
   ReverseMask = ~ReverseMask;\r
 \r
-  if (ReverseMask & (ReverseMask + 1)) {\r
+  if ((ReverseMask & (ReverseMask + 1)) != 0) {\r
     return 0;\r
   }\r
 \r
@@ -170,102 +108,93 @@ Returns:
   return (UINT8) (32 - Len);\r
 }\r
 \r
+/**\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
+\r
+  @retval EFI_SUCCESS           The 64-bit LUN is stored in Lun.\r
+  @retval EFI_INVALID_PARAMETER The string is malformatted.\r
+**/\r
 EFI_STATUS\r
 IScsiAsciiStrToLun (\r
   IN  CHAR8  *Str,\r
   OUT UINT8  *Lun\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Convert the hexadecimal encoded LUN string into the 64-bit LUN. \r
-\r
-Arguments:\r
-\r
-  Str - The hexadecimal encoded LUN string.\r
-  Lun - Storage to return the 64-bit LUN.\r
-\r
-Returns:\r
-\r
-  EFI_SUCCESS           - The 64-bit LUN is stored in Lun.\r
-  EFI_INVALID_PARAMETER - The string is malformatted.\r
-\r
---*/\r
 {\r
-  UINT32  Index;\r
-  CHAR8   *LunUnitStr[4];\r
-  CHAR8   Digit;\r
-  UINTN   Temp;\r
-\r
+  UINTN   Index, IndexValue, IndexNum, SizeStr;\r
+  CHAR8   TemStr[2];\r
+  UINT8   TemValue;\r
+  UINT16  Value[4];\r
+  \r
   ZeroMem (Lun, 8);\r
-  ZeroMem (LunUnitStr, sizeof (LunUnitStr));\r
-\r
-  Index         = 0;\r
-  LunUnitStr[0] = Str;\r
-\r
-  if (!IsHexDigit ((UINT8 *) &Digit, *Str)) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  while (*Str != '\0') {\r
-    //\r
-    // Legal representations of LUN:\r
-    //   4752-3A4F-6b7e-2F99,\r
-    //   6734-9-156f-127,\r
-    //   4186-9\r
-    //\r
-    if (*Str == '-') {\r
-      *Str = '\0';\r
-      Index++;\r
-\r
-      if (*(Str + 1) != '\0') {\r
-        if (!IsHexDigit ((UINT8 *) &Digit, *(Str + 1))) {\r
-          return EFI_INVALID_PARAMETER;\r
-        }\r
-\r
-        LunUnitStr[Index] = Str + 1;\r
+  ZeroMem (TemStr, 2);\r
+  ZeroMem ((UINT8 *) Value, sizeof (Value));\r
+  SizeStr    = AsciiStrLen (Str);  \r
+  IndexValue = 0;\r
+  IndexNum   = 0;\r
+\r
+  for (Index = 0; Index < SizeStr; Index ++) {\r
+    TemStr[0] = Str[Index];\r
+    TemValue = (UINT8) AsciiStrHexToUint64 (TemStr);\r
+    if (TemValue == 0 && TemStr[0] != '0') {\r
+      if ((TemStr[0] != '-') || (IndexNum == 0)) {\r
+        //\r
+        // Invalid Lun Char\r
+        //\r
+        return EFI_INVALID_PARAMETER;\r
       }\r
-    } else if (!IsHexDigit ((UINT8 *) &Digit, *Str)) {\r
-      return EFI_INVALID_PARAMETER;\r
     }\r
-\r
-    Str++;\r
-  }\r
-\r
-  for (Index = 0; (Index < 4) && (LunUnitStr[Index] != NULL); Index++) {\r
-    if (AsciiStrLen (LunUnitStr[Index]) > 4) {\r
+    \r
+    if ((TemValue == 0) && (TemStr[0] == '-')) {\r
+      //\r
+      // Next Lun value\r
+      //\r
+      if (++IndexValue >= 4) {\r
+        //\r
+        // Max 4 Lun value\r
+        //\r
+        return EFI_INVALID_PARAMETER;\r
+      }\r
+      //\r
+      // Restart str index for the next lun value\r
+      //\r
+      IndexNum = 0;\r
+      continue;\r
+    }\r
+    \r
+    if (++IndexNum > 4) {\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
-    Temp = AsciiStrHexToUintn (LunUnitStr[Index]);\r
-    *((UINT16 *) &Lun[Index * 2]) = HTONS (Temp);\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
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Convert the 64-bit LUN into the hexadecimal encoded LUN string.\r
+\r
+  @param[in]   Lun The 64-bit LUN.\r
+  @param[out]  Str The storage to return the hexadecimal encoded LUN string.\r
+**/\r
 VOID\r
 IScsiLunToUnicodeStr (\r
   IN UINT8    *Lun,\r
   OUT CHAR16  *Str\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Convert the 64-bit LUN into the hexadecimal encoded LUN string.\r
-\r
-Arguments:\r
-\r
-  Lun - The 64-bit LUN.\r
-  Str - The storage to return the hexadecimal encoded LUN string.\r
-\r
-Returns:\r
-\r
-  None.\r
-\r
---*/\r
 {\r
   UINTN   Index;\r
   CHAR16  *TempStr;\r
@@ -278,9 +207,9 @@ Returns:
       StrCpy (TempStr, L"0-");\r
     } else {\r
       TempStr[0]  = (CHAR16) IScsiHexString[Lun[2 * Index] >> 4];\r
-      TempStr[1]  = (CHAR16) IScsiHexString[Lun[2 * Index] & 0xf];\r
+      TempStr[1]  = (CHAR16) IScsiHexString[Lun[2 * Index] & 0x0F];\r
       TempStr[2]  = (CHAR16) IScsiHexString[Lun[2 * Index + 1] >> 4];\r
-      TempStr[3]  = (CHAR16) IScsiHexString[Lun[2 * Index + 1] & 0xf];\r
+      TempStr[3]  = (CHAR16) IScsiHexString[Lun[2 * Index + 1] & 0x0F];\r
       TempStr[4]  = L'-';\r
       TempStr[5]  = 0;\r
 \r
@@ -301,27 +230,19 @@ Returns:
   }\r
 }\r
 \r
+/**\r
+  Convert the ASCII string into a UNICODE string.\r
+\r
+  @param[in]   Source      The ASCII string.\r
+  @param[out]  Destination The storage to return the UNICODE string.\r
+\r
+  @return CHAR16 *         Pointer to the UNICODE string.\r
+**/\r
 CHAR16 *\r
 IScsiAsciiStrToUnicodeStr (\r
   IN  CHAR8   *Source,\r
   OUT CHAR16  *Destination\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Convert the ASCII string into a UNICODE string.\r
-\r
-Arguments:\r
-\r
-  Source      - The ASCII string.\r
-  Destination - The storage to return the UNICODE string.\r
-\r
-Returns:\r
-\r
-  Pointer to the UNICODE string.\r
-\r
---*/\r
 {\r
   ASSERT (Destination != NULL);\r
   ASSERT (Source != NULL);\r
@@ -335,27 +256,19 @@ Returns:
   return Destination;\r
 }\r
 \r
+/**\r
+  Convert the UNICODE string into an ASCII string.\r
+\r
+  @param[in]  Source       The UNICODE string.\r
+  @param[out] Destination  The storage to return the ASCII string.\r
+\r
+  @return CHAR8 *          Pointer to the ASCII string.\r
+**/\r
 CHAR8 *\r
 IScsiUnicodeStrToAsciiStr (\r
   IN  CHAR16  *Source,\r
   OUT CHAR8   *Destination\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Convert the UNICODE string into an ASCII string.\r
-\r
-Arguments:\r
-\r
-  Source      - The UNICODE string.\r
-  Destination - The storage to return the ASCII string.\r
-\r
-Returns:\r
-\r
-  Pointer to the ASCII string.\r
-\r
---*/\r
 {\r
   ASSERT (Destination != NULL);\r
   ASSERT (Source != NULL);\r
@@ -374,35 +287,27 @@ Returns:
   return Destination;\r
 }\r
 \r
+/**\r
+  Convert the decimal dotted IPv4 address into the binary IPv4 address.\r
+\r
+  @param[in]   Str             The UNICODE string.\r
+  @param[out]  Ip              The storage to return the ASCII string.\r
+\r
+  @retval EFI_SUCCESS           The binary IP address is returned in Ip.\r
+  @retval EFI_INVALID_PARAMETER The IP string is malformatted.\r
+**/\r
 EFI_STATUS\r
 IScsiAsciiStrToIp (\r
   IN  CHAR8             *Str,\r
   OUT EFI_IPv4_ADDRESS  *Ip\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Convert the decimal dotted IPv4 address into the binary IPv4 address.\r
-\r
-Arguments:\r
-\r
-  Str - The UNICODE string.\r
-  Ip  - The storage to return the ASCII string.\r
-\r
-Returns:\r
-\r
-  EFI_SUCCESS           - The binary IP address is returned in Ip.\r
-  EFI_INVALID_PARAMETER - The IP string is malformatted.\r
-\r
---*/\r
 {\r
   UINTN Index;\r
   UINTN Number;\r
 \r
   Index = 0;\r
 \r
-  while (*Str) {\r
+  while (*Str != 0) {\r
 \r
     if (Index > 3) {\r
       return EFI_INVALID_PARAMETER;\r
@@ -445,41 +350,52 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Convert the mac address into a hexadecimal encoded "-" seperated string.\r
+\r
+  @param[in]  Mac     The mac address.\r
+  @param[in]  Len     Length in bytes of the mac address.\r
+  @param[in]  VlanId  VLAN ID of the network device.\r
+  @param[out] Str     The storage to return the mac string.\r
+**/\r
 VOID\r
 IScsiMacAddrToStr (\r
   IN  EFI_MAC_ADDRESS  *Mac,\r
   IN  UINT32           Len,\r
+  IN  UINT16           VlanId,\r
   OUT CHAR16           *Str\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Convert the mac address into a hexadecimal encoded "-" seperated string.\r
-\r
-Arguments:\r
-\r
-  Mac - The mac address.\r
-  Len - Length in bytes of the mac address.\r
-  Str - The storage to return the mac string.\r
-\r
-Returns:\r
-\r
-  None.\r
-\r
---*/\r
 {\r
   UINT32  Index;\r
+  CHAR16  *String;\r
 \r
   for (Index = 0; Index < Len; Index++) {\r
-    Str[3 * Index]      = NibbleToHexChar ((UINT8) (Mac->Addr[Index] >> 4));\r
-    Str[3 * Index + 1]  = NibbleToHexChar (Mac->Addr[Index]);\r
+    Str[3 * Index]      = (CHAR16) IScsiHexString[(Mac->Addr[Index] >> 4) & 0x0F];\r
+    Str[3 * Index + 1]  = (CHAR16) IScsiHexString[Mac->Addr[Index] & 0x0F];\r
     Str[3 * Index + 2]  = L'-';\r
   }\r
 \r
-  Str[3 * Index - 1] = L'\0';\r
+  String = &Str[3 * Index - 1] ;\r
+  if (VlanId != 0) {\r
+    String += UnicodeSPrint (String, 6 * sizeof (CHAR16), L"\\%04x", (UINTN) VlanId);\r
+  }\r
+\r
+  *String = L'\0';\r
 }\r
 \r
+/**\r
+  Convert the binary encoded buffer into a hexadecimal encoded string.\r
+\r
+  @param[in]       BinBuffer   The buffer containing the binary data.\r
+  @param[in]       BinLength   Length of the binary buffer.\r
+  @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
+                               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
+**/\r
 EFI_STATUS\r
 IScsiBinToHex (\r
   IN     UINT8  *BinBuffer,\r
@@ -487,26 +403,6 @@ IScsiBinToHex (
   IN OUT CHAR8  *HexStr,\r
   IN OUT UINT32 *HexLength\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Convert the binary encoded buffer into a hexadecimal encoded string.\r
-\r
-Arguments:\r
-\r
-  BinBuffer - The buffer containing the binary data.\r
-  BinLength - Length of the binary buffer.\r
-  HexStr    - Pointer to the string.\r
-  HexLength - The length of the string.\r
-\r
-Returns:\r
-\r
-  EFI_SUCCESS          - The binary data is converted to the hexadecimal string \r
-                         and the length of the string is updated.\r
-  EFI_BUFFER_TOO_SMALL - The string is too small.\r
-\r
---*/\r
 {\r
   UINTN Index;\r
 \r
@@ -528,7 +424,7 @@ Returns:
 \r
   for (Index = 0; Index < BinLength; Index++) {\r
     HexStr[Index * 2 + 2] = IScsiHexString[BinBuffer[Index] >> 4];\r
-    HexStr[Index * 2 + 3] = IScsiHexString[BinBuffer[Index] & 0xf];\r
+    HexStr[Index * 2 + 3] = IScsiHexString[BinBuffer[Index] & 0x0F];\r
   }\r
 \r
   HexStr[Index * 2 + 2] = '\0';\r
@@ -536,102 +432,72 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Convert the hexadecimal string into a binary encoded buffer.\r
+\r
+  @param[in, out]  BinBuffer   The binary buffer.\r
+  @param[in, out]  BinLength   Length of the binary buffer.\r
+  @param[in]       HexStr      The hexadecimal string.\r
+\r
+  @retval EFI_SUCCESS          The hexadecimal string is converted into a binary\r
+                               encoded buffer.\r
+  @retval EFI_BUFFER_TOO_SMALL The binary buffer is too small to hold the converted data.\r
+**/\r
 EFI_STATUS\r
 IScsiHexToBin (\r
   IN OUT UINT8  *BinBuffer,\r
   IN OUT UINT32 *BinLength,\r
   IN     CHAR8  *HexStr\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Convert the hexadecimal string into a binary encoded buffer.\r
-\r
-Arguments:\r
-\r
-  BinBuffer - The binary buffer.\r
-  BinLength - Length of the binary buffer.\r
-  HexStr    - The hexadecimal string.\r
-\r
-Returns:\r
-\r
-  EFI_SUCCESS          - The hexadecimal string is converted into a binary \r
-                         encoded buffer.\r
-  EFI_BUFFER_TOO_SMALL - The binary buffer is too small to hold the converted data.s\r
-\r
---*/\r
 {\r
   UINTN   Index;\r
-  UINT32  HexCount;\r
-  CHAR8   *HexBuf;\r
+  UINTN   Length;\r
   UINT8   Digit;\r
-  UINT8   Byte;\r
+  CHAR8   TemStr[2];\r
+  \r
+  ZeroMem (TemStr, sizeof (TemStr));\r
 \r
   //\r
   // Find out how many hex characters the string has.\r
   //\r
-  HexBuf = HexStr;\r
-  if ((HexBuf[0] == '0') && ((HexBuf[1] == 'x') || (HexBuf[1] == 'X'))) {\r
-    HexBuf += 2;\r
+  if ((HexStr[0] == '0') && ((HexStr[1] == 'x') || (HexStr[1] == 'X'))) {\r
+    HexStr += 2;\r
   }\r
+  \r
+  Length = AsciiStrLen (HexStr);\r
 \r
-  for (Index = 0, HexCount = 0; IsHexDigit (&Digit, HexBuf[Index]); Index++, HexCount++)\r
-    ;\r
-\r
-  if (HexCount == 0) {\r
-    *BinLength = 0;\r
-    return EFI_SUCCESS;\r
-  }\r
-  //\r
-  // Test if buffer is passed enough.\r
-  //\r
-  if (((HexCount + 1) / 2) > *BinLength) {\r
-    *BinLength = (HexCount + 1) / 2;\r
-    return EFI_BUFFER_TOO_SMALL;\r
-  }\r
-\r
-  *BinLength = (HexCount + 1) / 2;\r
-\r
-  for (Index = 0; Index < HexCount; Index++) {\r
-\r
-    IsHexDigit (&Digit, HexBuf[HexCount - 1 - Index]);\r
-\r
+  for (Index = 0; Index < Length; Index ++) {\r
+    TemStr[0] = HexStr[Index];\r
+    Digit = (UINT8) AsciiStrHexToUint64 (TemStr);\r
+    if (Digit == 0 && TemStr[0] != '0') {\r
+      //\r
+      // Invalid Lun Char\r
+      //\r
+      break;\r
+    }\r
     if ((Index & 1) == 0) {\r
-      Byte = Digit;\r
+      BinBuffer [Index/2] = Digit;\r
     } else {\r
-      Byte = BinBuffer[*BinLength - 1 - Index / 2];\r
-      Byte &= 0x0F;\r
-      Byte = (UINT8) (Byte | (Digit << 4));\r
+      BinBuffer [Index/2] = (UINT8) ((BinBuffer [Index/2] << 4) + Digit);\r
     }\r
-\r
-    BinBuffer[*BinLength - 1 - Index / 2] = Byte;\r
   }\r
+  \r
+  *BinLength = (UINT32) ((Index + 1)/2);\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Generate random numbers.\r
+\r
+  @param[in, out]  Rand       The buffer to contain random numbers.\r
+  @param[in]       RandLength The length of the Rand buffer.\r
+**/\r
 VOID\r
 IScsiGenRandom (\r
   IN OUT UINT8  *Rand,\r
   IN     UINTN  RandLength\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Generate random numbers.\r
-\r
-Arguments:\r
-\r
-  Rand       - The buffer to contain random numbers.\r
-  RandLength - The length of the Rand buffer.\r
-\r
-Returns:\r
-\r
-  None.\r
-\r
---*/\r
 {\r
   UINT32  Random;\r
 \r
@@ -642,27 +508,20 @@ Returns:
   }\r
 }\r
 \r
+/**\r
+  Create the iSCSI driver data..\r
+\r
+  @param[in] Image      The handle of the driver image.\r
+  @param[in] Controller The handle of the controller.\r
+\r
+  @return The iSCSI driver data created.\r
+  @retval NULL Other errors as indicated.\r
+**/\r
 ISCSI_DRIVER_DATA *\r
 IScsiCreateDriverData (\r
   IN EFI_HANDLE  Image,\r
   IN EFI_HANDLE  Controller\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Create the iSCSI driver data..\r
-\r
-Arguments:\r
-\r
-  Image      - The handle of the driver image.\r
-  Controller - The handle of the controller.\r
-\r
-Returns:\r
-\r
-  The iSCSI driver data created.\r
-\r
---*/\r
 {\r
   ISCSI_DRIVER_DATA *Private;\r
   EFI_STATUS        Status;\r
@@ -680,15 +539,16 @@ Returns:
   // Create an event to be signal when the BS to RT transition is triggerd so\r
   // as to abort the iSCSI session.\r
   //\r
-  Status = gBS->CreateEvent (\r
-                  EFI_EVENT_SIGNAL_EXIT_BOOT_SERVICES,\r
+  Status = gBS->CreateEventEx (\r
+                  EVT_NOTIFY_SIGNAL,\r
                   TPL_CALLBACK,\r
                   IScsiOnExitBootService,\r
                   Private,\r
+                  &gEfiEventExitBootServicesGuid,\r
                   &Private->ExitBootServiceEvent\r
                   );\r
   if (EFI_ERROR (Status)) {\r
-    gBS->FreePool (Private);\r
+    FreePool (Private);\r
     return NULL;\r
   }\r
 \r
@@ -713,7 +573,7 @@ Returns:
                   );\r
   if (EFI_ERROR (Status)) {\r
     gBS->CloseEvent (Private->ExitBootServiceEvent);\r
-    gBS->FreePool (Private);\r
+    FreePool (Private);\r
 \r
     return NULL;\r
   }\r
@@ -723,25 +583,15 @@ Returns:
   return Private;\r
 }\r
 \r
+/**\r
+  Clean the iSCSI driver data.\r
+\r
+  @param[in]  Private The iSCSI driver data.\r
+**/\r
 VOID\r
 IScsiCleanDriverData (\r
   IN ISCSI_DRIVER_DATA  *Private\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Clean the iSCSI driver data.\r
-\r
-Arguments:\r
-\r
-  Private - The iSCSI driver data.\r
-\r
-Returns:\r
-\r
- None.\r
-\r
---*/\r
 {\r
   if (Private->DevicePath != NULL) {\r
     gBS->UninstallProtocolInterface (\r
@@ -750,7 +600,7 @@ Returns:
           Private->DevicePath\r
           );\r
 \r
-    gBS->FreePool (Private->DevicePath);\r
+    FreePool (Private->DevicePath);\r
   }\r
 \r
   if (Private->ExtScsiPassThruHandle != NULL) {\r
@@ -763,36 +613,83 @@ Returns:
 \r
   gBS->CloseEvent (Private->ExitBootServiceEvent);\r
 \r
-  gBS->FreePool (Private);\r
+  FreePool (Private);\r
 }\r
 \r
-EFI_STATUS\r
-IScsiGetConfigData (\r
-  IN ISCSI_DRIVER_DATA  *Private\r
-  )\r
-/*++\r
+/**\r
+  Check wheather the Controller is configured to use DHCP protocol.\r
 \r
-Routine Description:\r
+  @param[in]  Controller           The handle of the controller.\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
+  )\r
+{\r
+  EFI_STATUS                  Status;\r
+  EFI_MAC_ADDRESS             MacAddress;\r
+  UINTN                       HwAddressSize;\r
+  UINT16                      VlanId;\r
+  CHAR16                      MacString[70];\r
+  ISCSI_SESSION_CONFIG_NVDATA *ConfigDataTmp;\r
 \r
-  Get the various configuration data of this iSCSI instance.\r
+  //\r
+  // Get the mac string, it's the name of various variable\r
+  //\r
+  Status = NetLibGetMacAddress (Controller, &MacAddress, &HwAddressSize);\r
+  if (EFI_ERROR (Status)) {\r
+    return FALSE;\r
+  }\r
+  VlanId = NetLibGetVlanId (Controller);\r
+  IScsiMacAddrToStr (&MacAddress, (UINT32) HwAddressSize, VlanId, MacString);\r
 \r
-Arguments:\r
+  //\r
+  // Get the normal configuration.\r
+  //\r
+  Status = GetVariable2 (\r
+             MacString,\r
+             &gEfiIScsiInitiatorNameProtocolGuid,\r
+             (VOID**)&ConfigDataTmp,\r
+             NULL\r
+             );\r
+  if (ConfigDataTmp == NULL || EFI_ERROR (Status)) {\r
+    return FALSE;\r
+  }\r
+\r
+  if (ConfigDataTmp->Enabled && ConfigDataTmp->InitiatorInfoFromDhcp) {\r
+    FreePool (ConfigDataTmp);\r
+    return TRUE;\r
+  }\r
 \r
-  Private - The iSCSI driver data.\r
+  FreePool (ConfigDataTmp);\r
+  return FALSE;\r
+}\r
 \r
-Returns:\r
+/**\r
+  Get the various configuration data of this iSCSI instance.\r
 \r
-  EFI_SUCCESS   - The configuration of this instance is got.\r
-  EFI_NOT_FOUND - This iSCSI instance is not configured yet.\r
+  @param[in]  Private   The iSCSI driver data.\r
 \r
---*/\r
+  @retval EFI_SUCCESS   The configuration of this instance is got.\r
+  @retval EFI_ABORTED   The operation was aborted.\r
+  @retval Others        Other errors as indicated.\r
+**/\r
+EFI_STATUS\r
+IScsiGetConfigData (\r
+  IN ISCSI_DRIVER_DATA  *Private\r
+  )\r
 {\r
   EFI_STATUS                  Status;\r
   ISCSI_SESSION               *Session;\r
   UINTN                       BufferSize;\r
-  EFI_SIMPLE_NETWORK_PROTOCOL *Snp;\r
-  EFI_SIMPLE_NETWORK_MODE     *Mode;\r
-  CHAR16                      MacString[65];\r
+  EFI_MAC_ADDRESS             MacAddress;\r
+  UINTN                       HwAddressSize;\r
+  UINT16                      VlanId;\r
+  CHAR16                      MacString[70];\r
 \r
   //\r
   // get the iSCSI Initiator Name\r
@@ -808,21 +705,13 @@ Returns:
     return Status;\r
   }\r
 \r
-  Status = gBS->HandleProtocol (\r
-                  Private->Controller,\r
-                  &gEfiSimpleNetworkProtocolGuid,\r
-                  (VOID **)&Snp\r
-                  );\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
-  Mode = Snp->Mode;\r
-\r
   //\r
   // Get the mac string, it's the name of various variable\r
   //\r
-  IScsiMacAddrToStr (&Mode->PermanentAddress, Mode->HwAddressSize, MacString);\r
+  Status = NetLibGetMacAddress (Private->Controller, &MacAddress, &HwAddressSize);\r
+  ASSERT (Status == EFI_SUCCESS);\r
+  VlanId = NetLibGetVlanId (Private->Controller);\r
+  IScsiMacAddrToStr (&MacAddress, (UINT32) HwAddressSize, VlanId, MacString);\r
 \r
   //\r
   // Get the normal configuration.\r
@@ -848,7 +737,7 @@ Returns:
   BufferSize = sizeof (Session->AuthData.AuthConfig);\r
   Status = gRT->GetVariable (\r
                   MacString,\r
-                  &mIScsiCHAPAuthInfoGuid,\r
+                  &gIScsiCHAPAuthInfoGuid,\r
                   NULL,\r
                   &BufferSize,\r
                   &Session->AuthData.AuthConfig\r
@@ -864,25 +753,18 @@ Returns:
   return Status;\r
 }\r
 \r
+/**\r
+  Get the device path of the iSCSI tcp connection and update it.\r
+\r
+  @param[in]  Private The iSCSI driver data.\r
+\r
+  @return The updated device path.\r
+  @retval NULL Other errors as indicated.\r
+**/\r
 EFI_DEVICE_PATH_PROTOCOL *\r
 IScsiGetTcpConnDevicePath (\r
   IN ISCSI_DRIVER_DATA  *Private\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Get the device path of the iSCSI tcp connection and update it.\r
-\r
-Arguments:\r
-\r
-  Private - The iSCSI driver data.\r
-\r
-Returns:\r
-\r
-  The updated device path.\r
-\r
---*/\r
 {\r
   ISCSI_SESSION             *Session;\r
   ISCSI_CONNECTION          *Conn;\r
@@ -916,6 +798,9 @@ Returns:
   // Duplicate it.\r
   //\r
   DevicePath  = DuplicateDevicePath (DevicePath);\r
+  if (DevicePath == NULL) {\r
+    return NULL;\r
+  }\r
 \r
   DPathNode   = (EFI_DEV_PATH *) DevicePath;\r
 \r
@@ -925,7 +810,19 @@ Returns:
         ) {\r
 \r
       DPathNode->Ipv4.LocalPort       = 0;\r
-      DPathNode->Ipv4.StaticIpAddress = (BOOLEAN) (!Session->ConfigData.NvData.InitiatorInfoFromDhcp);\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
+      IP4_COPY_ADDRESS (\r
+        &DPathNode->Ipv4.SubnetMask,\r
+        &Session->ConfigData.NvData.SubnetMask\r
+        );\r
+\r
       break;\r
     }\r
 \r
@@ -935,28 +832,18 @@ Returns:
   return DevicePath;\r
 }\r
 \r
+/**\r
+  Abort the session when the transition from BS to RT is initiated.\r
+\r
+  @param[in]   Event  The event signaled.\r
+  @param[in]  Context The iSCSI driver data.\r
+**/\r
 VOID\r
 EFIAPI\r
 IScsiOnExitBootService (\r
   IN EFI_EVENT  Event,\r
   IN VOID       *Context\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Abort the session when the transition from BS to RT is initiated.\r
-\r
-Arguments:\r
-\r
-  Event   - The event signaled.\r
-  Context - The iSCSI driver data.\r
-\r
-Returns:\r
-\r
-  None.\r
-\r
---*/\r
 {\r
   ISCSI_DRIVER_DATA *Private;\r
 \r