]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcDhcp.c
MedmodulePkg: Refine codes related to Dhcpv4 and Dhcpv6 configuration.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / UefiPxeBcDxe / PxeBcDhcp.c
index ed7ee73e91e23ca93624b08305cadf0e51488891..384961352809a8f0d9a0b1e3a8a3ec6cf635e318 100644 (file)
@@ -1,8 +1,9 @@
 /** @file\r
   Support for PxeBc dhcp functions.\r
-  \r
-Copyright (c) 2007 - 2009, Intel Corporation.<BR>                                                         \r
-All rights reserved. This program and the accompanying materials\r
+\r
+Copyright (c) 2013, Red Hat, Inc.\r
+Copyright (c) 2007 - 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
 http://opensource.org/licenses/bsd-license.php\r
@@ -19,13 +20,13 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 // This is a map from the interested DHCP4 option tags' index to the tag value.\r
 //\r
 UINT8 mInterestedDhcp4Tags[PXEBC_DHCP4_TAG_INDEX_MAX] = {\r
-  PXEBC_DHCP4_TAG_BOOTFILE_LEN,\r
-  PXEBC_DHCP4_TAG_VENDOR,\r
-  PXEBC_DHCP4_TAG_OVERLOAD,\r
-  PXEBC_DHCP4_TAG_MSG_TYPE,\r
-  PXEBC_DHCP4_TAG_SERVER_ID,\r
-  PXEBC_DHCP4_TAG_CLASS_ID,\r
-  PXEBC_DHCP4_TAG_BOOTFILE\r
+  DHCP4_TAG_BOOTFILE_LEN,\r
+  DHCP4_TAG_VENDOR,\r
+  DHCP4_TAG_OVERLOAD,\r
+  DHCP4_TAG_MSG_TYPE,\r
+  DHCP4_TAG_SERVER_ID,\r
+  DHCP4_TAG_VENDOR_CLASS_ID,\r
+  DHCP4_TAG_BOOTFILE\r
 };\r
 \r
 \r
@@ -33,7 +34,7 @@ UINT8 mInterestedDhcp4Tags[PXEBC_DHCP4_TAG_INDEX_MAX] = {
   This function initialize the DHCP4 message instance.\r
 \r
   This function will pad each item of dhcp4 message packet.\r
-  \r
+\r
   @param  Seed    Pointer to the message instance of the DHCP4 packet.\r
   @param  Udp4    Pointer to the EFI_UDP4_PROTOCOL instance.\r
 \r
@@ -61,7 +62,7 @@ PxeBcInitSeedPacket (
   CopyMem (Header->ClientHwAddr, &Mode.CurrentAddress, Header->HwAddrLen);\r
 \r
   Seed->Dhcp4.Magik     = PXEBC_DHCP4_MAGIC;\r
-  Seed->Dhcp4.Option[0] = PXEBC_DHCP4_TAG_EOP;\r
+  Seed->Dhcp4.Option[0] = DHCP4_TAG_EOP;\r
 }\r
 \r
 \r
@@ -135,6 +136,7 @@ PxeBcParseCachedDhcpPacket (
   EFI_DHCP4_PACKET_OPTION *Option;\r
   UINT8                   OfferType;\r
   UINTN                   Index;\r
+  UINT8                   *Ptr8;\r
 \r
   CachedPacket->IsPxeOffer = FALSE;\r
   ZeroMem (CachedPacket->Dhcp4Option, sizeof (CachedPacket->Dhcp4Option));\r
@@ -145,13 +147,43 @@ PxeBcParseCachedDhcpPacket (
 \r
   //\r
   // Parse interested dhcp options and store their pointers in CachedPacket->Dhcp4Option.\r
+  // First, try to parse DHCPv4 options from the DHCP optional parameters field.\r
   //\r
   for (Index = 0; Index < PXEBC_DHCP4_TAG_INDEX_MAX; Index++) {\r
     Options[Index] = PxeBcParseExtendOptions (\r
-                      Offer->Dhcp4.Option,\r
-                      GET_OPTION_BUFFER_LEN (Offer),\r
-                      mInterestedDhcp4Tags[Index]\r
-                      );\r
+                       Offer->Dhcp4.Option,\r
+                       GET_OPTION_BUFFER_LEN (Offer),\r
+                       mInterestedDhcp4Tags[Index]\r
+                       );\r
+  }\r
+  //\r
+  // Second, Check if bootfilename and serverhostname is overloaded to carry DHCP options refers to rfc-2132. \r
+  // If yes, try to parse options from the BootFileName field, then ServerName field.\r
+  //\r
+  Option = Options[PXEBC_DHCP4_TAG_INDEX_OVERLOAD];\r
+  if (Option != NULL) {\r
+    if ((Option->Data[0] & PXEBC_DHCP4_OVERLOAD_FILE) != 0) {\r
+      for (Index = 0; Index < PXEBC_DHCP4_TAG_INDEX_MAX; Index++) {\r
+        if (Options[Index] == NULL) {\r
+          Options[Index] = PxeBcParseExtendOptions (\r
+                             (UINT8 *) Offer->Dhcp4.Header.BootFileName,\r
+                             sizeof (Offer->Dhcp4.Header.BootFileName),\r
+                             mInterestedDhcp4Tags[Index]\r
+                             );\r
+        }\r
+      }\r
+    }\r
+    if ((Option->Data[0] & PXEBC_DHCP4_OVERLOAD_SERVER_NAME) != 0) {\r
+      for (Index = 0; Index < PXEBC_DHCP4_TAG_INDEX_MAX; Index++) {\r
+        if (Options[Index] == NULL) {\r
+          Options[Index] = PxeBcParseExtendOptions (\r
+                             (UINT8 *) Offer->Dhcp4.Header.ServerName,\r
+                             sizeof (Offer->Dhcp4.Header.ServerName),\r
+                             mInterestedDhcp4Tags[Index]\r
+                             );\r
+        }\r
+      }\r
+    }\r
   }\r
 \r
   //\r
@@ -175,29 +207,34 @@ PxeBcParseCachedDhcpPacket (
     }\r
   }\r
 \r
+\r
   //\r
-  // Check whether bootfilename/serverhostname overloaded (See details in dhcp spec).\r
-  // If overloaded, parse this buffer as nested dhcp options, or just parse bootfilename/\r
-  // serverhostname option.\r
+  // Parse PXE boot file name:\r
+  // According to PXE spec, boot file name should be read from DHCP option 67 (bootfile name) if present.\r
+  // Otherwise, read from boot file field in DHCP header.\r
   //\r
-  Option = Options[PXEBC_DHCP4_TAG_INDEX_OVERLOAD];\r
-  if ((Option != NULL) && (Option->Data[0] & PXEBC_DHCP4_OVERLOAD_FILE)) {\r
-\r
-    Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] = PxeBcParseExtendOptions (\r
-                                                (UINT8 *) Offer->Dhcp4.Header.BootFileName,\r
-                                                sizeof (Offer->Dhcp4.Header.BootFileName),\r
-                                                PXEBC_DHCP4_TAG_BOOTFILE\r
-                                                );\r
-\r
-  } else if ((Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] == NULL) &&\r
-            (Offer->Dhcp4.Header.BootFileName[0] != 0)) {\r
+  if (Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] != NULL) {\r
+    //\r
+    // RFC 2132, Section 9.5 does not strictly state Bootfile name (option 67) is null \r
+    // terminated string. So force to append null terminated character at the end of string.\r
+    //\r
+    Ptr8 =  (UINT8*)&Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE]->Data[0];\r
+    Ptr8 += Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE]->Length;\r
+    if (*(Ptr8 - 1) != '\0') {\r
+      *Ptr8 = '\0';\r
+    }\r
+  } else if (Offer->Dhcp4.Header.BootFileName[0] != 0) {\r
     //\r
     // If the bootfile is not present and bootfilename is present in dhcp packet, just parse it.\r
-    // And do not count dhcp option header, or else will destory the serverhostname.\r
+    // And do not count dhcp option header, or else will destroy the serverhostname.\r
     //\r
-    Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] = (EFI_DHCP4_PACKET_OPTION *) (&Offer->Dhcp4.Header.BootFileName[0] -\r
+    // Make sure "BootFileName" is not overloaded.\r
+    //\r
+    if (Options[PXEBC_DHCP4_TAG_INDEX_OVERLOAD] == NULL ||\r
+        (Options[PXEBC_DHCP4_TAG_INDEX_OVERLOAD]->Data[0] & PXEBC_DHCP4_OVERLOAD_FILE) == 0) {\r
+      Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] = (EFI_DHCP4_PACKET_OPTION *) (&Offer->Dhcp4.Header.BootFileName[0] -\r
                                             OFFSET_OF (EFI_DHCP4_PACKET_OPTION, Data[0]));\r
-\r
+    }\r
   }\r
 \r
   //\r
@@ -253,9 +290,9 @@ PxeBcParseCachedDhcpPacket (
                     the index is maximum offer number.\r
 \r
   @retval TRUE      Offer the service successfully under priority BINL.\r
-  @retval FALSE     Boot Service failed, parse cached dhcp packet failed or this \r
+  @retval FALSE     Boot Service failed, parse cached dhcp packet failed or this\r
                     BINL ack cannot find options set or bootfile name specified.\r
-  \r
+\r
 **/\r
 BOOLEAN\r
 PxeBcTryBinl (\r
@@ -287,8 +324,8 @@ PxeBcTryBinl (
       );\r
   } else {\r
     CopyMem (\r
-      &ServerIp.Addr[0], \r
-      &Offer->Dhcp4.Header.ServerAddr, \r
+      &ServerIp.Addr[0],\r
+      &Offer->Dhcp4.Header.ServerAddr,\r
       sizeof (EFI_IPv4_ADDRESS)\r
       );\r
   }\r
@@ -338,7 +375,7 @@ PxeBcTryBinl (
   Offer dhcp service for each proxy with a BINL dhcp offer.\r
 \r
   @param  Private     Pointer to PxeBc private data\r
-  @param  OfferIndex  Pointer to the index of cached packets as complements of \r
+  @param  OfferIndex  Pointer to the index of cached packets as complements of\r
                       pxe mode data, the index is maximum offer number.\r
 \r
   @return If there is no service needed offer return FALSE, otherwise TRUE.\r
@@ -408,8 +445,8 @@ PxeBcCheckSelectedOffer (
     }\r
   } else if (SelectedOffer->OfferType == DHCP4_PACKET_TYPE_DHCP_ONLY) {\r
     //\r
-    // The selected offer to finish the D.O.R.A. is a DHCP only offer, we need \r
-    // try proxy offers if there are some, othewise the bootfile name must be \r
+    // The selected offer to finish the D.O.R.A. is a DHCP only offer, we need\r
+    // try proxy offers if there are some, othewise the bootfile name must be\r
     // set in this DHCP only offer.\r
     //\r
     if (Private->GotProxyOffer) {\r
@@ -439,11 +476,12 @@ PxeBcCheckSelectedOffer (
         }\r
       } else {\r
         //\r
-        // The proxy offer type is not determined, choose proxy offer in the \r
+        // The proxy offer type is not determined, choose proxy offer in the\r
         // received order.\r
         //\r
         Status = EFI_NO_RESPONSE;\r
 \r
+        ASSERT (Private->NumOffers < PXEBC_MAX_OFFER_NUM);\r
         for (Index = 0; Index < Private->NumOffers; Index++) {\r
 \r
           Offer = &Private->Dhcp4Offers[Index].Packet.Offer;\r
@@ -555,7 +593,9 @@ PxeBcCacheDhcpOffer (
   }\r
 \r
   OfferType = CachedOffer->OfferType;\r
-  ASSERT (OfferType < DHCP4_PACKET_TYPE_MAX);\r
+  if (OfferType >= DHCP4_PACKET_TYPE_MAX) {\r
+    return ;\r
+  }\r
 \r
   if (OfferType == DHCP4_PACKET_TYPE_BOOTP) {\r
 \r
@@ -611,11 +651,58 @@ PxeBcCacheDhcpOffer (
   Private->NumOffers++;\r
 }\r
 \r
+/**\r
+  Switch the Ip4 policy to static.\r
+\r
+  @param[in]  Private             The pointer to PXEBC_PRIVATE_DATA.\r
+\r
+  @retval     EFI_SUCCESS         The policy is already configured to static.\r
+  @retval     Others              Other error as indicated..\r
+\r
+**/\r
+EFI_STATUS\r
+PxeBcSetIp4Policy (   \r
+  IN PXEBC_PRIVATE_DATA            *Private\r
+  )\r
+{\r
+  EFI_STATUS                   Status;\r
+  EFI_IP4_CONFIG2_PROTOCOL     *Ip4Config2;\r
+  EFI_IP4_CONFIG2_POLICY       Policy;\r
+  UINTN                        DataSize;\r
+\r
+  Ip4Config2 = Private->Ip4Config2;\r
+  DataSize = sizeof (EFI_IP4_CONFIG2_POLICY);\r
+  Status = Ip4Config2->GetData (\r
+                       Ip4Config2,\r
+                       Ip4Config2DataTypePolicy,\r
+                       &DataSize,\r
+                       &Policy\r
+                       );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+  \r
+  if (Policy != Ip4Config2PolicyStatic) {\r
+    Policy = Ip4Config2PolicyStatic;\r
+    Status= Ip4Config2->SetData (\r
+                          Ip4Config2,\r
+                          Ip4Config2DataTypePolicy,\r
+                          sizeof (EFI_IP4_CONFIG2_POLICY),\r
+                          &Policy\r
+                          );\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    } \r
+  }\r
+\r
+  return  EFI_SUCCESS;\r
+}\r
+\r
 \r
 /**\r
   Select the specified proxy offer, such as BINL, DHCP_ONLY and so on.\r
   If the proxy does not exist, try offers with bootfile.\r
-  \r
+\r
   @param  Private   Pointer to PxeBc private data.\r
 \r
 **/\r
@@ -733,7 +820,7 @@ PxeBcSelectOffer (
 \r
 /**\r
   Callback routine.\r
-  \r
+\r
   EFI_DHCP4_CALLBACK is provided by the consumer of the EFI DHCPv4 Protocol driver\r
   to intercept events that occurred in the configuration process. This structure\r
   provides advanced control of each state transition of the DHCP process. The\r
@@ -778,7 +865,6 @@ PxeBcDhcpCallBack (
   UINT16                              Value;\r
   EFI_STATUS                          Status;\r
   BOOLEAN                             Received;\r
-  CHAR8                               *SystemSerialNumber;\r
   EFI_DHCP4_HEADER                    *DhcpHeader;\r
 \r
   if ((Dhcp4Event != Dhcp4RcvdOffer) &&\r
@@ -799,7 +885,7 @@ PxeBcDhcpCallBack (
   MaxMsgSize = PxeBcParseExtendOptions (\r
                 Packet->Dhcp4.Option,\r
                 GET_OPTION_BUFFER_LEN (Packet),\r
-                PXEBC_DHCP4_TAG_MAXMSG\r
+                DHCP4_TAG_MAXMSG\r
                 );\r
   if (MaxMsgSize != NULL) {\r
     Value = HTONS (PXEBC_DHCP4_MAX_PACKET_SIZE);\r
@@ -833,7 +919,7 @@ PxeBcDhcpCallBack (
       //\r
       DhcpHeader = &Packet->Dhcp4.Header;\r
 \r
-      if (EFI_ERROR (GetSmbiosSystemGuidAndSerialNumber ((EFI_GUID *) DhcpHeader->ClientHwAddr, &SystemSerialNumber))) {\r
+      if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID *) DhcpHeader->ClientHwAddr))) {\r
         //\r
         // GUID not yet set - send all 0xff's to show programable (via SetVariable)\r
         // SetMem(DHCPV4_OPTIONS_BUFFER.DhcpPlatformId.Guid, sizeof(EFI_GUID), 0xff);\r
@@ -842,7 +928,7 @@ PxeBcDhcpCallBack (
         ZeroMem (DhcpHeader->ClientHwAddr, sizeof (EFI_GUID));\r
       }\r
 \r
-      DhcpHeader->HwAddrLen = sizeof (EFI_GUID);\r
+      DhcpHeader->HwAddrLen = (UINT8) sizeof (EFI_GUID);\r
     }\r
 \r
     if (Dhcp4Event == Dhcp4SendDiscover) {\r
@@ -902,8 +988,8 @@ PxeBcDhcpCallBack (
 \r
   @param  Private          Pointer to PxeBc private data.\r
   @param  OptList          Pointer to a DHCP option list.\r
-                           \r
-  @param  IsDhcpDiscover   Discover dhcp option or not.     \r
+\r
+  @param  IsDhcpDiscover   Discover dhcp option or not.\r
 \r
   @return The index item number of the option list.\r
 \r
@@ -918,7 +1004,6 @@ PxeBcBuildDhcpOptions (
   UINT32                    Index;\r
   PXEBC_DHCP4_OPTION_ENTRY  OptEnt;\r
   UINT16                    Value;\r
-  CHAR8                     *SystemSerialNumber;\r
 \r
   Index       = 0;\r
   OptList[0]  = (EFI_DHCP4_PACKET_OPTION *) Private->OptionBuffer;\r
@@ -927,7 +1012,7 @@ PxeBcBuildDhcpOptions (
     //\r
     // Append message type.\r
     //\r
-    OptList[Index]->OpCode  = PXEBC_DHCP4_TAG_MSG_TYPE;\r
+    OptList[Index]->OpCode  = DHCP4_TAG_MSG_TYPE;\r
     OptList[Index]->Length  = 1;\r
     OptEnt.Mesg             = (PXEBC_DHCP4_OPTION_MESG *) OptList[Index]->Data;\r
     OptEnt.Mesg->Type       = PXEBC_DHCP4_MSG_TYPE_REQUEST;\r
@@ -937,8 +1022,8 @@ PxeBcBuildDhcpOptions (
     //\r
     // Append max message size.\r
     //\r
-    OptList[Index]->OpCode  = PXEBC_DHCP4_TAG_MAXMSG;\r
-    OptList[Index]->Length  = sizeof (PXEBC_DHCP4_OPTION_MAX_MESG_SIZE);\r
+    OptList[Index]->OpCode  = DHCP4_TAG_MAXMSG;\r
+    OptList[Index]->Length  = (UINT8) sizeof (PXEBC_DHCP4_OPTION_MAX_MESG_SIZE);\r
     OptEnt.MaxMesgSize      = (PXEBC_DHCP4_OPTION_MAX_MESG_SIZE *) OptList[Index]->Data;\r
     Value                   = NTOHS (PXEBC_DHCP4_MAX_PACKET_SIZE);\r
     CopyMem (&OptEnt.MaxMesgSize->Size, &Value, sizeof (UINT16));\r
@@ -948,36 +1033,36 @@ PxeBcBuildDhcpOptions (
   //\r
   // Parameter request list option.\r
   //\r
-  OptList[Index]->OpCode    = PXEBC_DHCP4_TAG_PARA_LIST;\r
+  OptList[Index]->OpCode    = DHCP4_TAG_PARA_LIST;\r
   OptList[Index]->Length    = 35;\r
   OptEnt.Para               = (PXEBC_DHCP4_OPTION_PARA *) OptList[Index]->Data;\r
-  OptEnt.Para->ParaList[0]  = PXEBC_DHCP4_TAG_NETMASK;\r
-  OptEnt.Para->ParaList[1]  = PXEBC_DHCP4_TAG_TIME_OFFSET;\r
-  OptEnt.Para->ParaList[2]  = PXEBC_DHCP4_TAG_ROUTER;\r
-  OptEnt.Para->ParaList[3]  = PXEBC_DHCP4_TAG_TIME_SERVER;\r
-  OptEnt.Para->ParaList[4]  = PXEBC_DHCP4_TAG_NAME_SERVER;\r
-  OptEnt.Para->ParaList[5]  = PXEBC_DHCP4_TAG_DNS_SERVER;\r
-  OptEnt.Para->ParaList[6]  = PXEBC_DHCP4_TAG_HOSTNAME;\r
-  OptEnt.Para->ParaList[7]  = PXEBC_DHCP4_TAG_BOOTFILE_LEN;\r
-  OptEnt.Para->ParaList[8]  = PXEBC_DHCP4_TAG_DOMAINNAME;\r
-  OptEnt.Para->ParaList[9]  = PXEBC_DHCP4_TAG_ROOTPATH;\r
-  OptEnt.Para->ParaList[10] = PXEBC_DHCP4_TAG_EXTEND_PATH;\r
-  OptEnt.Para->ParaList[11] = PXEBC_DHCP4_TAG_EMTU;\r
-  OptEnt.Para->ParaList[12] = PXEBC_DHCP4_TAG_TTL;\r
-  OptEnt.Para->ParaList[13] = PXEBC_DHCP4_TAG_BROADCAST;\r
-  OptEnt.Para->ParaList[14] = PXEBC_DHCP4_TAG_NIS_DOMAIN;\r
-  OptEnt.Para->ParaList[15] = PXEBC_DHCP4_TAG_NIS_SERVER;\r
-  OptEnt.Para->ParaList[16] = PXEBC_DHCP4_TAG_NTP_SERVER;\r
-  OptEnt.Para->ParaList[17] = PXEBC_DHCP4_TAG_VENDOR;\r
-  OptEnt.Para->ParaList[18] = PXEBC_DHCP4_TAG_REQUEST_IP;\r
-  OptEnt.Para->ParaList[19] = PXEBC_DHCP4_TAG_LEASE;\r
-  OptEnt.Para->ParaList[20] = PXEBC_DHCP4_TAG_SERVER_ID;\r
-  OptEnt.Para->ParaList[21] = PXEBC_DHCP4_TAG_T1;\r
-  OptEnt.Para->ParaList[22] = PXEBC_DHCP4_TAG_T2;\r
-  OptEnt.Para->ParaList[23] = PXEBC_DHCP4_TAG_CLASS_ID;\r
-  OptEnt.Para->ParaList[24] = PXEBC_DHCP4_TAG_TFTP;\r
-  OptEnt.Para->ParaList[25] = PXEBC_DHCP4_TAG_BOOTFILE;\r
-  OptEnt.Para->ParaList[26] = PXEBC_PXE_DHCP4_TAG_UUID;\r
+  OptEnt.Para->ParaList[0]  = DHCP4_TAG_NETMASK;\r
+  OptEnt.Para->ParaList[1]  = DHCP4_TAG_TIME_OFFSET;\r
+  OptEnt.Para->ParaList[2]  = DHCP4_TAG_ROUTER;\r
+  OptEnt.Para->ParaList[3]  = DHCP4_TAG_TIME_SERVER;\r
+  OptEnt.Para->ParaList[4]  = DHCP4_TAG_NAME_SERVER;\r
+  OptEnt.Para->ParaList[5]  = DHCP4_TAG_DNS_SERVER;\r
+  OptEnt.Para->ParaList[6]  = DHCP4_TAG_HOSTNAME;\r
+  OptEnt.Para->ParaList[7]  = DHCP4_TAG_BOOTFILE_LEN;\r
+  OptEnt.Para->ParaList[8]  = DHCP4_TAG_DOMAINNAME;\r
+  OptEnt.Para->ParaList[9]  = DHCP4_TAG_ROOTPATH;\r
+  OptEnt.Para->ParaList[10] = DHCP4_TAG_EXTEND_PATH;\r
+  OptEnt.Para->ParaList[11] = DHCP4_TAG_EMTU;\r
+  OptEnt.Para->ParaList[12] = DHCP4_TAG_TTL;\r
+  OptEnt.Para->ParaList[13] = DHCP4_TAG_BROADCAST;\r
+  OptEnt.Para->ParaList[14] = DHCP4_TAG_NIS_DOMAIN;\r
+  OptEnt.Para->ParaList[15] = DHCP4_TAG_NIS_SERVER;\r
+  OptEnt.Para->ParaList[16] = DHCP4_TAG_NTP_SERVER;\r
+  OptEnt.Para->ParaList[17] = DHCP4_TAG_VENDOR;\r
+  OptEnt.Para->ParaList[18] = DHCP4_TAG_REQUEST_IP;\r
+  OptEnt.Para->ParaList[19] = DHCP4_TAG_LEASE;\r
+  OptEnt.Para->ParaList[20] = DHCP4_TAG_SERVER_ID;\r
+  OptEnt.Para->ParaList[21] = DHCP4_TAG_T1;\r
+  OptEnt.Para->ParaList[22] = DHCP4_TAG_T2;\r
+  OptEnt.Para->ParaList[23] = DHCP4_TAG_VENDOR_CLASS_ID;\r
+  OptEnt.Para->ParaList[24] = DHCP4_TAG_TFTP;\r
+  OptEnt.Para->ParaList[25] = DHCP4_TAG_BOOTFILE;\r
+  OptEnt.Para->ParaList[26] = DHCP4_TAG_UUID;\r
   OptEnt.Para->ParaList[27] = 0x80;\r
   OptEnt.Para->ParaList[28] = 0x81;\r
   OptEnt.Para->ParaList[29] = 0x82;\r
@@ -992,14 +1077,14 @@ PxeBcBuildDhcpOptions (
   //\r
   // Append UUID/Guid-based client identifier option\r
   //\r
-  OptList[Index]->OpCode  = PXEBC_PXE_DHCP4_TAG_UUID;\r
-  OptList[Index]->Length  = sizeof (PXEBC_DHCP4_OPTION_UUID);\r
+  OptList[Index]->OpCode  = DHCP4_TAG_UUID;\r
+  OptList[Index]->Length  = (UINT8) sizeof (PXEBC_DHCP4_OPTION_UUID);\r
   OptEnt.Uuid             = (PXEBC_DHCP4_OPTION_UUID *) OptList[Index]->Data;\r
   OptEnt.Uuid->Type       = 0;\r
   Index++;\r
   OptList[Index]          = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);\r
 \r
-  if (EFI_ERROR (GetSmbiosSystemGuidAndSerialNumber ((EFI_GUID *) OptEnt.Uuid->Guid, &SystemSerialNumber))) {\r
+  if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID *) OptEnt.Uuid->Guid))) {\r
     //\r
     // GUID not yet set - send all 0xff's to show programable (via SetVariable)\r
     // SetMem(DHCPV4_OPTIONS_BUFFER.DhcpPlatformId.Guid, sizeof(EFI_GUID), 0xff);\r
@@ -1011,8 +1096,8 @@ PxeBcBuildDhcpOptions (
   //\r
   // Append client network device interface option\r
   //\r
-  OptList[Index]->OpCode  = PXEBC_PXE_DHCP4_TAG_UNDI;\r
-  OptList[Index]->Length  = sizeof (PXEBC_DHCP4_OPTION_UNDI);\r
+  OptList[Index]->OpCode  = DHCP4_TAG_UNDI;\r
+  OptList[Index]->Length  = (UINT8) sizeof (PXEBC_DHCP4_OPTION_UNDI);\r
   OptEnt.Undi             = (PXEBC_DHCP4_OPTION_UNDI *) OptList[Index]->Data;\r
   if (Private->Nii != NULL) {\r
     OptEnt.Undi->Type       = Private->Nii->Type;\r
@@ -1030,10 +1115,10 @@ PxeBcBuildDhcpOptions (
   //\r
   // Append client system architecture option\r
   //\r
-  OptList[Index]->OpCode  = PXEBC_PXE_DHCP4_TAG_ARCH;\r
-  OptList[Index]->Length  = sizeof (PXEBC_DHCP4_OPTION_ARCH);\r
+  OptList[Index]->OpCode  = DHCP4_TAG_ARCH;\r
+  OptList[Index]->Length  = (UINT8) sizeof (PXEBC_DHCP4_OPTION_ARCH);\r
   OptEnt.Arch             = (PXEBC_DHCP4_OPTION_ARCH *) OptList[Index]->Data;\r
-  Value                   = HTONS (SYS_ARCH);\r
+  Value                   = HTONS (EFI_PXE_CLIENT_SYSTEM_ARCHITECTURE);\r
   CopyMem (&OptEnt.Arch->Type, &Value, sizeof (UINT16));\r
   Index++;\r
   OptList[Index]          = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);\r
@@ -1041,14 +1126,14 @@ PxeBcBuildDhcpOptions (
   //\r
   // Append client system architecture option\r
   //\r
-  OptList[Index]->OpCode  = PXEBC_DHCP4_TAG_CLASS_ID;\r
-  OptList[Index]->Length  = sizeof (PXEBC_DHCP4_OPTION_CLID);\r
+  OptList[Index]->OpCode  = DHCP4_TAG_VENDOR_CLASS_ID;\r
+  OptList[Index]->Length  = (UINT8) sizeof (PXEBC_DHCP4_OPTION_CLID);\r
   OptEnt.Clid             = (PXEBC_DHCP4_OPTION_CLID *) OptList[Index]->Data;\r
   CopyMem (OptEnt.Clid, DEFAULT_CLASS_ID_DATA, sizeof (PXEBC_DHCP4_OPTION_CLID));\r
-  CvtNum (SYS_ARCH, OptEnt.Clid->ArchitectureType, sizeof (OptEnt.Clid->ArchitectureType));\r
+  CvtNum (EFI_PXE_CLIENT_SYSTEM_ARCHITECTURE, OptEnt.Clid->ArchitectureType, sizeof (OptEnt.Clid->ArchitectureType));\r
 \r
   if (Private->Nii != NULL) {\r
-    // \r
+    //\r
     // If NII protocol exists, update DHCP option data\r
     //\r
     CopyMem (OptEnt.Clid->InterfaceName, Private->Nii->StringId, sizeof (OptEnt.Clid->InterfaceName));\r
@@ -1069,8 +1154,8 @@ PxeBcBuildDhcpOptions (
   @param  Type                  PxeBc option boot item type\r
   @param  Layer                 PxeBc option boot item layer\r
   @param  UseBis                Use BIS or not\r
-  @param  DestIp                Ip address for server      \r
-  @param  IpCount               The total count of the server ip address    \r
+  @param  DestIp                Ip address for server\r
+  @param  IpCount               The total count of the server ip address\r
   @param  SrvList               Server list\r
   @param  IsDiscv               Discover the vendor or not\r
   @param  Reply                 The dhcp4 packet of Pxe reply\r
@@ -1078,8 +1163,8 @@ PxeBcBuildDhcpOptions (
   @retval EFI_SUCCESS           Operation succeeds.\r
   @retval EFI_OUT_OF_RESOURCES  Allocate memory pool failed.\r
   @retval EFI_NOT_FOUND         There is no vendor option exists.\r
-  @retval EFI_TIMEOUT           Send Pxe Discover time out. \r
-  \r
+  @retval EFI_TIMEOUT           Send Pxe Discover time out.\r
+\r
 **/\r
 EFI_STATUS\r
 PxeBcDiscvBootService (\r
@@ -1110,7 +1195,6 @@ PxeBcDiscvBootService (
   EFI_DHCP4_PACKET_OPTION             *PxeOpt;\r
   PXEBC_OPTION_BOOT_ITEM              *PxeBootItem;\r
   UINT8                               VendorOptLen;\r
-  CHAR8                               *SystemSerialNumber;\r
   EFI_DHCP4_HEADER                    *DhcpHeader;\r
   UINT32                              Xid;\r
 \r
@@ -1139,21 +1223,21 @@ PxeBcDiscvBootService (
     //\r
     // Add vendor option of PXE_BOOT_ITEM\r
     //\r
-    VendorOptLen      = (sizeof (EFI_DHCP4_PACKET_OPTION) - 1) * 2 + sizeof (PXEBC_OPTION_BOOT_ITEM) + 1;\r
+    VendorOptLen = (UINT8) ((sizeof (EFI_DHCP4_PACKET_OPTION) - 1) * 2 + sizeof (PXEBC_OPTION_BOOT_ITEM) + 1);\r
     OptList[OptCount] = AllocatePool (VendorOptLen);\r
     if (OptList[OptCount] == NULL) {\r
       return EFI_OUT_OF_RESOURCES;\r
     }\r
 \r
-    OptList[OptCount]->OpCode     = PXEBC_DHCP4_TAG_VENDOR;\r
+    OptList[OptCount]->OpCode     = DHCP4_TAG_VENDOR;\r
     OptList[OptCount]->Length     = (UINT8) (VendorOptLen - 2);\r
     PxeOpt                        = (EFI_DHCP4_PACKET_OPTION *) OptList[OptCount]->Data;\r
     PxeOpt->OpCode                = PXEBC_VENDOR_TAG_BOOT_ITEM;\r
-    PxeOpt->Length                = sizeof (PXEBC_OPTION_BOOT_ITEM);\r
+    PxeOpt->Length                = (UINT8) sizeof (PXEBC_OPTION_BOOT_ITEM);\r
     PxeBootItem                   = (PXEBC_OPTION_BOOT_ITEM *) PxeOpt->Data;\r
     PxeBootItem->Type             = HTONS (Type);\r
     PxeBootItem->Layer            = HTONS (*Layer);\r
-    PxeOpt->Data[PxeOpt->Length]  = PXEBC_DHCP4_TAG_EOP;\r
+    PxeOpt->Data[PxeOpt->Length]  = DHCP4_TAG_EOP;\r
 \r
     OptCount++;\r
   }\r
@@ -1170,16 +1254,16 @@ PxeBcDiscvBootService (
 \r
   DhcpHeader = &Token.Packet->Dhcp4.Header;\r
   if (Mode->SendGUID) {\r
-    if (EFI_ERROR (GetSmbiosSystemGuidAndSerialNumber ((EFI_GUID *) DhcpHeader->ClientHwAddr, &SystemSerialNumber))) {\r
+    if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID *) DhcpHeader->ClientHwAddr))) {\r
       //\r
       // GUID not yet set - send all 0's to show not programable\r
       //\r
       ZeroMem (DhcpHeader->ClientHwAddr, sizeof (EFI_GUID));\r
     }\r
 \r
-    DhcpHeader->HwAddrLen = sizeof (EFI_GUID);\r
+    DhcpHeader->HwAddrLen = (UINT8) sizeof (EFI_GUID);\r
   }\r
-       \r
+\r
   Xid                                 = NET_RANDOM (NetRandomInitSeed ());\r
   Token.Packet->Dhcp4.Header.Xid      = HTONL(Xid);\r
   Token.Packet->Dhcp4.Header.Reserved = HTONS((UINT16) ((IsBCast) ? 0x8000 : 0));\r
@@ -1217,6 +1301,13 @@ PxeBcDiscvBootService (
     }\r
   }\r
 \r
+  if (TryIndex > PXEBC_BOOT_REQUEST_RETRIES) {\r
+    //\r
+    // No server response our PXE request\r
+    //\r
+    Status = EFI_TIMEOUT;\r
+  }\r
+\r
   if (!EFI_ERROR (Status)) {\r
     //\r
     // Find Pxe Reply\r
@@ -1270,7 +1361,9 @@ PxeBcDiscvBootService (
     //\r
     // free the responselist\r
     //\r
-    FreePool (Token.ResponseList);\r
+    if (Token.ResponseList != NULL) {\r
+      FreePool (Token.ResponseList);\r
+    }\r
   }\r
   //\r
   // Free the dhcp packet\r
@@ -1288,8 +1381,8 @@ PxeBcDiscvBootService (
   @param  Length     The length of the dhcp options.\r
   @param  OptTag     The option OpCode.\r
 \r
-  @return NULL if the buffer length is 0 and OpCode is not \r
-          PXEBC_DHCP4_TAG_EOP, or the pointer to the buffer.\r
+  @return NULL if the buffer length is 0 and OpCode is not\r
+          DHCP4_TAG_EOP, or the pointer to the buffer.\r
 \r
 **/\r
 EFI_DHCP4_PACKET_OPTION *\r
@@ -1305,14 +1398,14 @@ PxeBcParseExtendOptions (
   Option  = (EFI_DHCP4_PACKET_OPTION *) Buffer;\r
   Offset  = 0;\r
 \r
-  while (Offset < Length && Option->OpCode != PXEBC_DHCP4_TAG_EOP) {\r
+  while (Offset < Length && Option->OpCode != DHCP4_TAG_EOP) {\r
 \r
     if (Option->OpCode == OptTag) {\r
 \r
       return Option;\r
     }\r
 \r
-    if (Option->OpCode == PXEBC_DHCP4_TAG_PAD) {\r
+    if (Option->OpCode == DHCP4_TAG_PAD) {\r
       Offset++;\r
     } else {\r
       Offset += Option->Length + 2;\r
@@ -1350,7 +1443,7 @@ PxeBcParseVendorOptions (
   PxeOption       = (EFI_DHCP4_PACKET_OPTION *) &Dhcp4Option->Data[0];\r
   Offset          = 0;\r
 \r
-  while ((Offset < VendorOptionLen) && (PxeOption->OpCode != PXEBC_DHCP4_TAG_EOP)) {\r
+  while ((Offset < VendorOptionLen) && (PxeOption->OpCode != DHCP4_TAG_EOP)) {\r
     //\r
     // Parse every Vendor Option and set its BitMap\r
     //\r
@@ -1431,7 +1524,7 @@ PxeBcParseVendorOptions (
 \r
     SET_VENDOR_OPTION_BIT_MAP (BitMap, PxeOption->OpCode);\r
 \r
-    if (PxeOption->OpCode == PXEBC_DHCP4_TAG_PAD) {\r
+    if (PxeOption->OpCode == DHCP4_TAG_PAD) {\r
       Offset++;\r
     } else {\r
       Offset = (UINT8) (Offset + PxeOption->Length + 2);\r
@@ -1451,8 +1544,8 @@ PxeBcParseVendorOptions (
 /**\r
   This function display boot item detail.\r
 \r
-  If the length of the boot item string over 70 Char, just display 70 Char. \r
-   \r
+  If the length of the boot item string over 70 Char, just display 70 Char.\r
+\r
   @param  Str     Pointer to a string (boot item string).\r
   @param  Len     The length of string.\r
 \r
@@ -1479,11 +1572,11 @@ PxeBcDisplayBootItem (
   @param  Private              Pointer to PxeBc private data.\r
 \r
   @retval EFI_SUCCESS          Select boot prompt done.\r
-  @retval EFI_TIMEOUT          Select boot prompt time out. \r
+  @retval EFI_TIMEOUT          Select boot prompt time out.\r
   @retval EFI_NOT_FOUND        The proxy offer is not Pxe10.\r
   @retval EFI_ABORTED          User cancel the operation.\r
   @retval EFI_NOT_READY        Read the input key from the keybroad has not finish.\r
-  \r
+\r
 **/\r
 EFI_STATUS\r
 PxeBcSelectBootPrompt (\r
@@ -1518,6 +1611,19 @@ PxeBcSelectBootPrompt (
   }\r
 \r
   VendorOpt = &Packet->PxeVendorOption;\r
+  //\r
+  // According to the PXE specification 2.1, Table 2-1 PXE DHCP Options  (Full  \r
+  // List), we must not consider a boot prompt or boot menu if all of the  \r
+  // following hold:\r
+  // - the PXE_DISCOVERY_CONTROL PXE tag is present inside the Vendor Options\r
+  //   (=43) DHCP tag, and\r
+  // - the PXE_DISCOVERY_CONTROL PXE tag has bit 3 set, and  \r
+  // - a boot file name has been presented with DHCP option 67.\r
+  //\r
+  if (IS_DISABLE_PROMPT_MENU (VendorOpt->DiscoverCtrl) &&\r
+      Packet->Dhcp4Option[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] != NULL) {\r
+    return EFI_ABORTED;\r
+  }\r
 \r
   if (!IS_VALID_BOOT_PROMPT (VendorOpt->BitMap)) {\r
     return EFI_SUCCESS;\r
@@ -1657,10 +1763,10 @@ ON_EXIT:
   @param  Private         Pointer to PxeBc private data.\r
   @param  Type            The type of the menu.\r
   @param  UseDefaultItem  Use default item or not.\r
-  \r
+\r
   @retval EFI_ABORTED     User cancel operation.\r
   @retval EFI_SUCCESS     Select the boot menu success.\r
-  @retval EFI_NOT_READY   Read the input key from the keybroad has not finish.    \r
+  @retval EFI_NOT_READY   Read the input key from the keybroad has not finish.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -1671,7 +1777,7 @@ PxeBcSelectBootMenu (
   )\r
 {\r
   PXEBC_CACHED_DHCP4_PACKET  *Packet;\r
-  PXEBC_VENDOR_OPTION       *VendorOpt;\r
+  PXEBC_VENDOR_OPTION        *VendorOpt;\r
   EFI_INPUT_KEY              InputKey;\r
   UINT8                      MenuSize;\r
   UINT8                      MenuNum;\r
@@ -1754,7 +1860,7 @@ PxeBcSelectBootMenu (
       gBS->Stall (10 * TICKS_PER_MS);\r
     }\r
 \r
-    if (!InputKey.ScanCode) {\r
+    if (InputKey.ScanCode != 0) {\r
       switch (InputKey.UnicodeChar) {\r
       case CTRL ('c'):\r
         InputKey.ScanCode = SCAN_ESC;\r
@@ -1823,6 +1929,8 @@ PxeBcSelectBootMenu (
     gST->ConOut->SetCursorPosition (gST->ConOut, 0, TopRow + MenuNum);\r
   } while (!Finish);\r
 \r
+   ASSERT (Select < PXEBC_MAX_MENU_NUM);\r
+\r
   //\r
   // Swap the byte order\r
   //\r