]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Network/IScsiDxe/IScsiMisc.c
MdeModulePkg: Refine casting expression result to bigger size
[mirror_edk2.git] / MdeModulePkg / Universal / Network / IScsiDxe / IScsiMisc.c
index 2e43b415ec593b899d16426991d0aca73229805f..ae202c3fe24a1eb0cceee6693811878dc5056112 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Miscellaneous routines for iSCSI driver.\r
 \r
-Copyright (c) 2004 - 2013, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2004 - 2016, 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
@@ -204,7 +204,7 @@ IScsiLunToUnicodeStr (
   for (Index = 0; Index < 4; Index++) {\r
 \r
     if ((Lun[2 * Index] | Lun[2 * Index + 1]) == 0) {\r
-      StrCpy (TempStr, L"0-");\r
+      CopyMem(TempStr, L"0-", sizeof (L"0-"));\r
     } else {\r
       TempStr[0]  = (CHAR16) IScsiHexString[Lun[2 * Index] >> 4];\r
       TempStr[1]  = (CHAR16) IScsiHexString[Lun[2 * Index] & 0x0F];\r
@@ -219,6 +219,7 @@ IScsiLunToUnicodeStr (
     TempStr += StrLen (TempStr);\r
   }\r
 \r
+  ASSERT (StrLen(Str) >= 1);\r
   Str[StrLen (Str) - 1] = 0;\r
 \r
   for (Index = StrLen (Str) - 1; Index > 1; Index = Index - 2) {\r
@@ -586,34 +587,48 @@ 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
+EXIT:\r
+\r
   gBS->CloseEvent (Private->ExitBootServiceEvent);\r
 \r
   FreePool (Private);\r
+  return Status;\r
 }\r
 \r
 /**\r
@@ -813,15 +828,24 @@ IScsiGetTcpConnDevicePath (
       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
@@ -852,3 +876,70 @@ IScsiOnExitBootService (
 \r
   IScsiSessionAbort (&Private->Session);\r
 }\r
+\r
+/**\r
+  Tests whether a controller handle is being managed by IScsi driver.\r
+\r
+  This function tests whether the driver specified by DriverBindingHandle is\r
+  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
+  If ProtocolGuid is NULL, then ASSERT().\r
+\r
+  @param  ControllerHandle     A handle for a controller to test.\r
+  @param  DriverBindingHandle  Specifies the driver binding handle for the\r
+                               driver.\r
+  @param  ProtocolGuid         Specifies the protocol that the driver specified\r
+                               by DriverBindingHandle opens in its Start()\r
+                               function.\r
+\r
+  @retval EFI_SUCCESS          ControllerHandle is managed by the driver\r
+                               specified by DriverBindingHandle.\r
+  @retval EFI_UNSUPPORTED      ControllerHandle is not managed by the driver\r
+                               specified by DriverBindingHandle.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+IScsiTestManagedDevice (\r
+  IN  EFI_HANDLE       ControllerHandle,\r
+  IN  EFI_HANDLE       DriverBindingHandle,\r
+  IN  EFI_GUID         *ProtocolGuid\r
+  )\r
+{\r
+  EFI_STATUS     Status;\r
+  VOID           *ManagedInterface;\r
+  EFI_HANDLE     NicControllerHandle;\r
+\r
+  ASSERT (ProtocolGuid != NULL);\r
+\r
+  NicControllerHandle = NetLibGetNicHandle (ControllerHandle, ProtocolGuid);\r
+  if (NicControllerHandle == NULL) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
+  Status = gBS->OpenProtocol (\r
+                  ControllerHandle,\r
+                  (EFI_GUID *) ProtocolGuid,\r
+                  &ManagedInterface,\r
+                  DriverBindingHandle,\r
+                  NicControllerHandle,\r
+                  EFI_OPEN_PROTOCOL_BY_DRIVER\r
+                  );\r
+  if (!EFI_ERROR (Status)) {\r
+    gBS->CloseProtocol (\r
+           ControllerHandle,\r
+           (EFI_GUID *) ProtocolGuid,\r
+           DriverBindingHandle,\r
+           NicControllerHandle\r
+           );\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
+  if (Status != EFI_ALREADY_STARTED) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r