+/**\r
+ Seek the address of the first byte of the option header.\r
+\r
+ @param[in] Buf The pointer to the buffer.\r
+ @param[in] SeekLen The length to seek.\r
+ @param[in] OptType The option type.\r
+\r
+ @retval NULL If it failed to seek the option.\r
+ @retval others The position to the option.\r
+\r
+**/\r
+UINT8 *\r
+PxeBcDhcp6SeekOption (\r
+ IN UINT8 *Buf,\r
+ IN UINT32 SeekLen,\r
+ IN UINT16 OptType\r
+ )\r
+{\r
+ UINT8 *Cursor;\r
+ UINT8 *Option;\r
+ UINT16 DataLen;\r
+ UINT16 OpCode;\r
+\r
+ Option = NULL;\r
+ Cursor = Buf;\r
+\r
+ while (Cursor < Buf + SeekLen) {\r
+ OpCode = ReadUnaligned16 ((UINT16 *) Cursor);\r
+ if (OpCode == HTONS (OptType)) {\r
+ Option = Cursor;\r
+ break;\r
+ }\r
+ DataLen = NTOHS (ReadUnaligned16 ((UINT16 *) (Cursor + 2)));\r
+ Cursor += (DataLen + 4);\r
+ }\r
+\r
+ return Option;\r
+}\r
+\r
+\r
+/**\r
+ Build and send out the request packet for the bootfile, and parse the reply.\r
+\r
+ @param[in] Private The pointer to PxeBc private data.\r
+ @param[in] Index PxeBc option boot item type.\r
+\r
+ @retval EFI_SUCCESS Successfully discovered the boot file.\r
+ @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.\r
+ @retval EFI_NOT_FOUND Can't get the PXE reply packet.\r
+ @retval Others Failed to discover the boot file.\r
+\r
+**/\r
+EFI_STATUS\r
+PxeBcRequestBootService (\r
+ IN PXEBC_PRIVATE_DATA *Private,\r
+ IN UINT32 Index\r
+ )\r
+{\r
+ EFI_PXE_BASE_CODE_UDP_PORT SrcPort;\r
+ EFI_PXE_BASE_CODE_UDP_PORT DestPort;\r
+ EFI_PXE_BASE_CODE_MODE *Mode;\r
+ EFI_PXE_BASE_CODE_PROTOCOL *PxeBc;\r
+ EFI_PXE_BASE_CODE_DHCPV6_PACKET *Discover;\r
+ UINTN DiscoverLen;\r
+ EFI_DHCP6_PACKET *Request;\r
+ UINTN RequestLen;\r
+ EFI_DHCP6_PACKET *Reply;\r
+ UINT8 *RequestOpt;\r
+ UINT8 *DiscoverOpt;\r
+ UINTN ReadSize;\r
+ UINT16 OpFlags;\r
+ UINT16 OpCode;\r
+ UINT16 OpLen;\r
+ EFI_STATUS Status;\r
+ EFI_DHCP6_PACKET *ProxyOffer;\r
+ UINT8 *Option;\r
+\r
+ PxeBc = &Private->PxeBc;\r
+ Mode = PxeBc->Mode;\r
+ Request = Private->Dhcp6Request;\r
+ ProxyOffer = &Private->OfferBuffer[Index].Dhcp6.Packet.Offer;\r
+ SrcPort = PXEBC_BS_DISCOVER_PORT;\r
+ DestPort = PXEBC_BS_DISCOVER_PORT;\r
+ OpFlags = 0;\r
+\r
+ if (Request == NULL) {\r
+ return EFI_DEVICE_ERROR;\r
+ }\r
+\r
+ Discover = AllocateZeroPool (sizeof (EFI_PXE_BASE_CODE_DHCPV6_PACKET));\r
+ if (Discover == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ //\r
+ // Build the request packet by the cached request packet before.\r
+ //\r
+ Discover->TransactionId = ProxyOffer->Dhcp6.Header.TransactionId;\r
+ Discover->MessageType = Request->Dhcp6.Header.MessageType;\r
+ RequestOpt = Request->Dhcp6.Option;\r
+ DiscoverOpt = Discover->DhcpOptions;\r
+ DiscoverLen = sizeof (EFI_DHCP6_HEADER);\r
+ RequestLen = DiscoverLen;\r
+\r
+ //\r
+ // Find Server ID Option from ProxyOffer.\r
+ //\r
+ Option = PxeBcDhcp6SeekOption (\r
+ ProxyOffer->Dhcp6.Option,\r
+ ProxyOffer->Length - 4,\r
+ PXEBC_DHCP6_OPT_SERVER_ID\r
+ );\r
+ if (Option == NULL) {\r
+ return EFI_NOT_FOUND;\r
+ }\r
+ \r
+ //\r
+ // Add Server ID Option.\r
+ //\r
+ OpLen = NTOHS (((EFI_DHCP6_PACKET_OPTION *) Option)->OpLen);\r
+ CopyMem (DiscoverOpt, Option, OpLen + 4);\r
+ DiscoverOpt += (OpLen + 4);\r
+ DiscoverLen += (OpLen + 4);\r
+\r
+ while (RequestLen < Request->Length) {\r
+ OpCode = NTOHS (((EFI_DHCP6_PACKET_OPTION *) RequestOpt)->OpCode);\r
+ OpLen = NTOHS (((EFI_DHCP6_PACKET_OPTION *) RequestOpt)->OpLen);\r
+ if (OpCode != EFI_DHCP6_IA_TYPE_NA &&\r
+ OpCode != EFI_DHCP6_IA_TYPE_TA &&\r
+ OpCode != PXEBC_DHCP6_OPT_SERVER_ID\r
+ ) {\r
+ //\r
+ // Copy all the options except IA option and Server ID\r
+ //\r
+ CopyMem (DiscoverOpt, RequestOpt, OpLen + 4);\r
+ DiscoverOpt += (OpLen + 4);\r
+ DiscoverLen += (OpLen + 4);\r
+ }\r
+ RequestOpt += (OpLen + 4);\r
+ RequestLen += (OpLen + 4);\r
+ }\r
+\r
+ //\r
+ // Update Elapsed option in the package \r
+ //\r
+ Option = PxeBcDhcp6SeekOption (\r
+ Discover->DhcpOptions,\r
+ (UINT32)(RequestLen - 4),\r
+ PXEBC_DHCP6_OPT_ELAPSED_TIME\r
+ );\r
+ if (Option != NULL) {\r
+ CalcElapsedTime (Private);\r
+ WriteUnaligned16 ((UINT16*)(Option + 4), HTONS((UINT16) Private->ElapsedTime));\r
+ } \r
+\r
+ Status = PxeBc->UdpWrite (\r
+ PxeBc,\r
+ OpFlags,\r
+ &Private->ServerIp,\r
+ &DestPort,\r
+ NULL,\r
+ &Private->StationIp,\r
+ &SrcPort,\r
+ NULL,\r
+ NULL,\r
+ &DiscoverLen,\r
+ (VOID *) Discover\r
+ );\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ //\r
+ // Cache the right PXE reply packet here, set valid flag later.\r
+ // Especially for PXE discover packet, store it into mode data here.\r
+ //\r
+ Reply = &Private->ProxyOffer.Dhcp6.Packet.Offer;\r
+ ReadSize = (UINTN) Reply->Size;\r
+\r
+ //\r
+ // Start Udp6Read instance\r
+ //\r
+ Status = Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData);\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+ \r
+ Status = PxeBc->UdpRead (\r
+ PxeBc,\r
+ EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP,\r
+ &Private->StationIp,\r
+ &SrcPort,\r
+ &Private->ServerIp,\r
+ &DestPort,\r
+ NULL,\r
+ NULL,\r
+ &ReadSize,\r
+ (VOID *) &Reply->Dhcp6\r
+ );\r
+ //\r
+ // Stop Udp6Read instance\r
+ //\r
+ Private->Udp6Read->Configure (Private->Udp6Read, NULL);\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ //\r
+ // Update length\r
+ //\r
+ Reply->Length = (UINT32) ReadSize;\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r