]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Network/IScsiDxe/IScsiDhcp.c
NetworkPkg: Remove some clarification from TcpDxe.inf
[mirror_edk2.git] / MdeModulePkg / Universal / Network / IScsiDxe / IScsiDhcp.c
index ec8d4b2c3ced44b212a33260d58daac535067307..6a876c8b69ddc718fd1f934f6fffea166de67504 100644 (file)
@@ -1,8 +1,8 @@
-/**\r
-  ISci DHCP related configuration routines.\r
+/** @file\r
+  iSCSI DHCP related configuration routines.\r
 \r
-Copyright (c) 2004 - 2007, Intel Corporation\r
-All rights reserved. This program and the accompanying materials\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
 http://opensource.org/licenses/bsd-license.php\r
@@ -10,14 +10,6 @@ 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
-  IScsiDhcp.c\r
-\r
-Abstract:\r
-\r
-  IScsi DHCP related configuration routines.\r
-\r
 **/\r
 \r
 #include "IScsiImpl.h"\r
@@ -25,26 +17,20 @@ Abstract:
 /**\r
   Extract the Root Path option and get the required target information.\r
 \r
-  @param  RootPath[in]          The RootPath.\r
-\r
-  @param  Length[in]            Length of the RootPath option payload.\r
-\r
-  @param  ConfigNvData[in]      The iSCSI session configuration data read from nonvolatile device.\r
+  @param[in]        RootPath         The RootPath.\r
+  @param[in]        Length           Length of the RootPath option payload.\r
+  @param[in, out]   ConfigNvData     The iSCSI session configuration data read from nonvolatile device.\r
 \r
   @retval EFI_SUCCESS           All required information is extracted from the RootPath option.\r
-\r
   @retval EFI_NOT_FOUND         The RootPath is not an iSCSI RootPath.\r
-\r
   @retval EFI_OUT_OF_RESOURCES  Failed to allocate memory.\r
-\r
   @retval EFI_INVALID_PARAMETER The RootPath is mal-formatted.\r
-\r
 **/\r
 EFI_STATUS\r
 IScsiDhcpExtractRootPath (\r
-  IN CHAR8                        *RootPath,\r
-  IN UINT8                        Length,\r
-  IN ISCSI_SESSION_CONFIG_NVDATA  *ConfigNvData\r
+  IN      CHAR8                        *RootPath,\r
+  IN      UINT8                        Length,\r
+  IN OUT  ISCSI_SESSION_CONFIG_NVDATA  *ConfigNvData\r
   )\r
 {\r
   EFI_STATUS            Status;\r
@@ -78,13 +64,13 @@ IScsiDhcpExtractRootPath (
   TmpStr[Length]  = '\0';\r
 \r
   Index           = 0;\r
-  FieldIndex      = 0;\r
+  FieldIndex      = RP_FIELD_IDX_SERVERNAME;\r
   ZeroMem (&Fields[0], sizeof (Fields));\r
 \r
   //\r
   // Extract the fields in the Root Path option string.\r
   //\r
-  for (FieldIndex = 0; (FieldIndex < RP_FIELD_IDX_MAX) && (Index < Length); FieldIndex++) {\r
+  for (FieldIndex = RP_FIELD_IDX_SERVERNAME; (FieldIndex < RP_FIELD_IDX_MAX) && (Index < Length); FieldIndex++) {\r
     if (TmpStr[Index] != ISCSI_ROOT_PATH_FIELD_DELIMITER) {\r
       Fields[FieldIndex].Str = &TmpStr[Index];\r
     }\r
@@ -110,7 +96,7 @@ IScsiDhcpExtractRootPath (
     goto ON_EXIT;\r
   }\r
 \r
-  if ((Fields[RP_FIELD_IDX_SERVERNAME].Str == NULL) ||\r
+    if ((Fields[RP_FIELD_IDX_SERVERNAME].Str == NULL) ||\r
       (Fields[RP_FIELD_IDX_TARGETNAME].Str == NULL) ||\r
       (Fields[RP_FIELD_IDX_PROTOCOL].Len > 1)\r
       ) {\r
@@ -172,11 +158,11 @@ IScsiDhcpExtractRootPath (
     goto ON_EXIT;\r
   }\r
 \r
-  AsciiStrCpy (ConfigNvData->TargetName, Field->Str);\r
+  AsciiStrCpyS (ConfigNvData->TargetName, ISCSI_NAME_MAX_SIZE, Field->Str);\r
 \r
 ON_EXIT:\r
 \r
-  gBS->FreePool (TmpStr);\r
+  FreePool (TmpStr);\r
 \r
   return Status;\r
 }\r
@@ -184,26 +170,21 @@ ON_EXIT:
 /**\r
   The callback function registerd to the DHCP4 instance which is used to select\r
   the qualified DHCP OFFER.\r
-  \r
-  @param  This[in]         The DHCP4 protocol.\r
-\r
-  @param  Context[in]      The context set when configuring the DHCP4 protocol.\r
-\r
-  @param  CurrentState[in] The current state of the DHCP4 protocol.\r
-\r
-  @param  Dhcp4Event[in]   The event occurs in the current state.\r
 \r
-  @param  Packet[in]       The DHCP packet that is to be sent or already received. \r
-\r
-  @param  NewPackt[out]    The packet used to replace the above Packet.\r
-\r
-  @retval EFI_NOT_READY    The DHCP OFFER packet doesn't match our requirements.\r
+  @param[in]  This         The DHCP4 protocol.\r
+  @param[in]  Context      The context set when configuring the DHCP4 protocol.\r
+  @param[in]  CurrentState The current state of the DHCP4 protocol.\r
+  @param[in]  Dhcp4Event   The event occurs in the current state.\r
+  @param[in]  Packet       The DHCP packet that is to be sent or already received.\r
+  @param[out] NewPacket    The packet used to replace the above Packet.\r
 \r
   @retval EFI_SUCCESS      Either the DHCP OFFER is qualified or we're not intereseted\r
                            in the Dhcp4Event.\r
-\r
+  @retval EFI_NOT_READY    The DHCP OFFER packet doesn't match our requirements.\r
+  @retval Others           Other errors as indicated.\r
 **/\r
 EFI_STATUS\r
+EFIAPI\r
 IScsiDhcpSelectOffer (\r
   IN  EFI_DHCP4_PROTOCOL  * This,\r
   IN  VOID                *Context,\r
@@ -236,12 +217,12 @@ IScsiDhcpSelectOffer (
 \r
   Status = This->Parse (This, Packet, &OptionCount, OptionList);\r
   if (EFI_ERROR (Status)) {\r
-    gBS->FreePool (OptionList);\r
+    FreePool (OptionList);\r
     return EFI_NOT_READY;\r
   }\r
 \r
   for (Index = 0; Index < OptionCount; Index++) {\r
-    if (OptionList[Index]->OpCode != DHCP4_TAG_ROOT_PATH) {\r
+    if (OptionList[Index]->OpCode != DHCP4_TAG_ROOTPATH) {\r
       continue;\r
     }\r
 \r
@@ -254,11 +235,11 @@ IScsiDhcpSelectOffer (
     break;\r
   }\r
 \r
-  if ((Index == OptionCount)) {\r
+  if (Index == OptionCount) {\r
     Status = EFI_NOT_READY;\r
   }\r
 \r
-  gBS->FreePool (OptionList);\r
+  FreePool (OptionList);\r
 \r
   return Status;\r
 }\r
@@ -266,23 +247,18 @@ IScsiDhcpSelectOffer (
 /**\r
   Parse the DHCP ACK to get the address configuration and DNS information.\r
 \r
-  @param  Dhcp4[in]             The DHCP4 protocol.\r
-\r
-  @param  ConfigData[in]        The session configuration data.\r
+  @param[in]       Dhcp4        The DHCP4 protocol.\r
+  @param[in, out]  ConfigData   The session configuration data.\r
 \r
   @retval EFI_SUCCESS           The DNS information is got from the DHCP ACK.\r
-\r
   @retval EFI_NO_MAPPING        DHCP failed to acquire address and other information.\r
-\r
   @retval EFI_INVALID_PARAMETER The DHCP ACK's DNS option is mal-formatted.\r
-\r
-  @retval EFI_DEVICE_ERROR      Some unexpected error happened.\r
-\r
+  @retval EFI_DEVICE_ERROR      Other errors as indicated.\r
 **/\r
 EFI_STATUS\r
 IScsiParseDhcpAck (\r
-  IN EFI_DHCP4_PROTOCOL         *Dhcp4,\r
-  IN ISCSI_SESSION_CONFIG_DATA  *ConfigData\r
+  IN      EFI_DHCP4_PROTOCOL         *Dhcp4,\r
+  IN OUT  ISCSI_SESSION_CONFIG_DATA  *ConfigData\r
   )\r
 {\r
   EFI_STATUS              Status;\r
@@ -319,7 +295,7 @@ IScsiParseDhcpAck (
 \r
   Status = Dhcp4->Parse (Dhcp4, Dhcp4ModeData.ReplyPacket, &OptionCount, OptionList);\r
   if (EFI_ERROR (Status)) {\r
-    gBS->FreePool (OptionList);\r
+    FreePool (OptionList);\r
     return EFI_DEVICE_ERROR;\r
   }\r
 \r
@@ -327,7 +303,7 @@ IScsiParseDhcpAck (
     //\r
     // Get DNS server addresses and DHCP server address from this offer.\r
     //\r
-    if (OptionList[Index]->OpCode == DHCP4_TAG_DNS) {\r
+    if (OptionList[Index]->OpCode == DHCP4_TAG_DNS_SERVER) {\r
 \r
       if (((OptionList[Index]->Length & 0x3) != 0) || (OptionList[Index]->Length == 0)) {\r
         Status = EFI_INVALID_PARAMETER;\r
@@ -354,34 +330,29 @@ IScsiParseDhcpAck (
     }\r
   }\r
 \r
-  gBS->FreePool (OptionList);\r
+  FreePool (OptionList);\r
 \r
   return Status;\r
 }\r
 \r
 /**\r
   Parse the DHCP ACK to get the address configuration and DNS information.\r
-  \r
-  @param  Image[in]             The handle of the driver image.\r
-\r
-  @param  Controller[in]        The handle of the controller;\r
 \r
-  @param  ConfigData[in]        The session configuration data.\r
+  @param[in]       Image            The handle of the driver image.\r
+  @param[in]       Controller       The handle of the controller;\r
+  @param[in, out]  ConfigData       The session configuration data.\r
 \r
   @retval EFI_SUCCESS           The DNS information is got from the DHCP ACK.\r
-\r
-  @retval EFI_NO_MAPPING        DHCP failed to acquire address and other information.\r
-\r
-  @retval EFI_INVALID_PARAMETER The DHCP ACK's DNS option is mal-formatted.\r
-\r
-  @retval EFI_DEVICE_ERROR      Some unexpected error happened.\r
+  @retval EFI_OUT_OF_RESOURCES  Failed to allocate memory.\r
+  @retval EFI_NO_MEDIA          There was a media error.\r
+  @retval Others                Other errors as indicated.\r
 \r
 **/\r
 EFI_STATUS\r
 IScsiDoDhcp (\r
-  IN EFI_HANDLE                 Image,\r
-  IN EFI_HANDLE                 Controller,\r
-  IN ISCSI_SESSION_CONFIG_DATA  *ConfigData\r
+  IN     EFI_HANDLE                 Image,\r
+  IN     EFI_HANDLE                 Controller,\r
+  IN OUT ISCSI_SESSION_CONFIG_DATA  *ConfigData\r
   )\r
 {\r
   EFI_HANDLE              Dhcp4Handle;\r
@@ -389,11 +360,22 @@ IScsiDoDhcp (
   EFI_STATUS              Status;\r
   EFI_DHCP4_PACKET_OPTION *ParaList;\r
   EFI_DHCP4_CONFIG_DATA   Dhcp4ConfigData;\r
+  EFI_STATUS              MediaStatus;\r
+  UINT8                   *Data;\r
 \r
   Dhcp4Handle = NULL;\r
   Dhcp4       = NULL;\r
   ParaList    = NULL;\r
 \r
+  //\r
+  // Check media status before do DHCP\r
+  //\r
+  MediaStatus = EFI_SUCCESS;\r
+  NetLibDetectMediaWaitTimeout (Controller, ISCSI_CHECK_MEDIA_GET_DHCP_WAITING_TIME, &MediaStatus);\r
+  if (MediaStatus != EFI_SUCCESS) {\r
+    return EFI_NO_MEDIA;\r
+  }\r
+\r
   //\r
   // Create a DHCP4 child instance and get the protocol.\r
   //\r
@@ -429,10 +411,11 @@ IScsiDoDhcp (
   //\r
   ParaList->OpCode  = DHCP4_TAG_PARA_LIST;\r
   ParaList->Length  = (UINT8) (ConfigData->NvData.TargetInfoFromDhcp ? 4 : 3);\r
-  ParaList->Data[0] = DHCP4_TAG_NETMASK;\r
-  ParaList->Data[1] = DHCP4_TAG_ROUTER;\r
-  ParaList->Data[2] = DHCP4_TAG_DNS;\r
-  ParaList->Data[3] = DHCP4_TAG_ROOT_PATH;\r
+  Data = &ParaList->Data[0];\r
+  Data[0] = DHCP4_TAG_NETMASK;\r
+  Data[1] = DHCP4_TAG_ROUTER;\r
+  Data[2] = DHCP4_TAG_DNS_SERVER;\r
+  Data[3] = DHCP4_TAG_ROOTPATH;\r
 \r
   ZeroMem (&Dhcp4ConfigData, sizeof (EFI_DHCP4_CONFIG_DATA));\r
   Dhcp4ConfigData.OptionCount = 1;\r
@@ -463,7 +446,7 @@ IScsiDoDhcp (
 ON_EXIT:\r
 \r
   if (ParaList != NULL) {\r
-    gBS->FreePool (ParaList);\r
+    FreePool (ParaList);\r
   }\r
 \r
   if (Dhcp4 != NULL) {\r