]> 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 9eff1850bee6cab19d582cbed068fc7772b78548..2e43b415ec593b899d16426991d0aca73229805f 100644 (file)
@@ -1,8 +1,8 @@
 /** @file\r
   Miscellaneous routines for iSCSI driver.\r
 \r
-Copyright (c) 2004 - 2009, Intel Corporation.<BR>\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
@@ -126,7 +126,7 @@ IScsiAsciiStrToLun (
   UINTN   Index, IndexValue, IndexNum, SizeStr;\r
   CHAR8   TemStr[2];\r
   UINT8   TemValue;\r
-  UINT16  Value [4];\r
+  UINT16  Value[4];\r
   \r
   ZeroMem (Lun, 8);\r
   ZeroMem (TemStr, 2);\r
@@ -353,18 +353,21 @@ IScsiAsciiStrToIp (
 /**\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[out] Str The storage to return the mac string.\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
   UINT32  Index;\r
+  CHAR16  *String;\r
 \r
   for (Index = 0; Index < Len; Index++) {\r
     Str[3 * Index]      = (CHAR16) IScsiHexString[(Mac->Addr[Index] >> 4) & 0x0F];\r
@@ -372,7 +375,12 @@ IScsiMacAddrToStr (
     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
@@ -540,7 +548,7 @@ IScsiCreateDriverData (
                   &Private->ExitBootServiceEvent\r
                   );\r
   if (EFI_ERROR (Status)) {\r
-    gBS->FreePool (Private);\r
+    FreePool (Private);\r
     return NULL;\r
   }\r
 \r
@@ -565,7 +573,7 @@ IScsiCreateDriverData (
                   );\r
   if (EFI_ERROR (Status)) {\r
     gBS->CloseEvent (Private->ExitBootServiceEvent);\r
-    gBS->FreePool (Private);\r
+    FreePool (Private);\r
 \r
     return NULL;\r
   }\r
@@ -592,7 +600,7 @@ IScsiCleanDriverData (
           Private->DevicePath\r
           );\r
 \r
-    gBS->FreePool (Private->DevicePath);\r
+    FreePool (Private->DevicePath);\r
   }\r
 \r
   if (Private->ExtScsiPassThruHandle != NULL) {\r
@@ -605,7 +613,60 @@ IScsiCleanDriverData (
 \r
   gBS->CloseEvent (Private->ExitBootServiceEvent);\r
 \r
-  gBS->FreePool (Private);\r
+  FreePool (Private);\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
+  @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
+  //\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
+  //\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
+  FreePool (ConfigDataTmp);\r
+  return FALSE;\r
 }\r
 \r
 /**\r
@@ -625,9 +686,10 @@ IScsiGetConfigData (
   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
@@ -643,21 +705,13 @@ IScsiGetConfigData (
     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
@@ -683,7 +737,7 @@ IScsiGetConfigData (
   BufferSize = sizeof (Session->AuthData.AuthConfig);\r
   Status = gRT->GetVariable (\r
                   MacString,\r
-                  &mIScsiCHAPAuthInfoGuid,\r
+                  &gIScsiCHAPAuthInfoGuid,\r
                   NULL,\r
                   &BufferSize,\r
                   &Session->AuthData.AuthConfig\r
@@ -756,7 +810,19 @@ IScsiGetTcpConnDevicePath (
         ) {\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