]> git.proxmox.com Git - mirror_edk2.git/blobdiff - NetworkPkg/IScsiDxe/IScsiMisc.c
Fix a K9 error.Result of GetVariable2() function that may return NULL will be derefer...
[mirror_edk2.git] / NetworkPkg / IScsiDxe / IScsiMisc.c
index f3ecdb5f6a1de4d24e6994bd944cc669c382b77e..6ed4974545fba7b35dfa17fab3ba4b5eef74c355 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Miscellaneous routines for iSCSI driver.\r
 \r
-Copyright (c) 2004 - 2011, Intel Corporation. All rights reserved.<BR>\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
@@ -890,6 +890,100 @@ IScsiCleanDriverData (
   FreePool (Private);\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
@@ -1076,10 +1170,12 @@ IScsiGetConfigData (
       (UINTN) AttemptConfigOrder[Index]\r
       );\r
 \r
-    AttemptConfigData = (ISCSI_ATTEMPT_CONFIG_NVDATA *) GetVariable (\r
-                                                          mPrivate->PortString,\r
-                                                          &gEfiIScsiInitiatorNameProtocolGuid\r
-                                                          );\r
+    GetVariable2 (\r
+                 mPrivate->PortString,\r
+                 &gEfiIScsiInitiatorNameProtocolGuid,\r
+                 (VOID**)&AttemptConfigData,\r
+                 NULL\r
+                 );\r
 \r
     if (AttemptConfigData == NULL) {\r
       continue;\r