]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcDhcp.c
1. Fix a bug in PXE driver that the PXE boot do not restart if a new boot option...
[mirror_edk2.git] / MdeModulePkg / Universal / Network / UefiPxeBcDxe / PxeBcDhcp.c
index de21a444cea01891c70946a1680f356afce7dfe8..fa3594d36b0e17a9f71e79d1d0bea666a33a4d1d 100644 (file)
@@ -1,8 +1,8 @@
 /** @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) 2007 - 2012, 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
@@ -33,7 +33,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
@@ -135,6 +135,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
@@ -181,19 +182,28 @@ PxeBcParseCachedDhcpPacket (
   // serverhostname option.\r
   //\r
   Option = Options[PXEBC_DHCP4_TAG_INDEX_OVERLOAD];\r
-  if ((Option != NULL) && (Option->Data[0] & PXEBC_DHCP4_OVERLOAD_FILE)) {\r
+  if ((Option != NULL) && ((Option->Data[0] & PXEBC_DHCP4_OVERLOAD_FILE) != 0)) {\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
+    // 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
+    if (Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] != NULL) {\r
+      Ptr8 =  (UINT8*)&Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE]->Data[0];\r
+      Ptr8 += Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE]->Length;\r
+      *Ptr8 =  '\0';\r
+    }\r
 \r
   } else if ((Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] == NULL) &&\r
             (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
                                             OFFSET_OF (EFI_DHCP4_PACKET_OPTION, Data[0]));\r
@@ -253,9 +263,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 +297,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 +348,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 +418,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,7 +449,7 @@ 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
@@ -555,7 +565,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
@@ -615,7 +627,7 @@ PxeBcCacheDhcpOffer (
 /**\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 +745,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 +790,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
@@ -833,7 +844,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 +853,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 +913,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 +929,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
@@ -938,7 +948,7 @@ PxeBcBuildDhcpOptions (
     // 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]->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
@@ -993,13 +1003,13 @@ PxeBcBuildDhcpOptions (
   // 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]->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
@@ -1012,7 +1022,7 @@ PxeBcBuildDhcpOptions (
   // 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]->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
@@ -1031,9 +1041,9 @@ PxeBcBuildDhcpOptions (
   // 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]->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
@@ -1042,13 +1052,13 @@ PxeBcBuildDhcpOptions (
   // 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]->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 +1079,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 +1088,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 +1120,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,7 +1148,7 @@ 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
@@ -1149,7 +1158,7 @@ PxeBcDiscvBootService (
     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
@@ -1170,19 +1179,19 @@ 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((IsBCast) ? 0x8000 : 0);\r
+  Token.Packet->Dhcp4.Header.Reserved = HTONS((UINT16) ((IsBCast) ? 0x8000 : 0));\r
   CopyMem (&Token.Packet->Dhcp4.Header.ClientAddr, &Private->StationIp, sizeof (EFI_IPv4_ADDRESS));\r
 \r
   Token.RemotePort = Sport;\r
@@ -1217,6 +1226,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 +1286,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,7 +1306,7 @@ 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
+  @return NULL if the buffer length is 0 and OpCode is not\r
           PXEBC_DHCP4_TAG_EOP, or the pointer to the buffer.\r
 \r
 **/\r
@@ -1451,8 +1469,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 +1497,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
@@ -1657,10 +1675,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 +1689,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 +1772,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 +1841,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