2 Support for PxeBc dhcp functions.
4 Copyright (c) 2013, Red Hat, Inc.
5 Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "PxeBcImpl.h"
20 // This is a map from the interested DHCP4 option tags' index to the tag value.
22 UINT8 mInterestedDhcp4Tags
[PXEBC_DHCP4_TAG_INDEX_MAX
] = {
23 DHCP4_TAG_BOOTFILE_LEN
,
28 DHCP4_TAG_VENDOR_CLASS_ID
,
34 This function initialize the DHCP4 message instance.
36 This function will pad each item of dhcp4 message packet.
38 @param Seed Pointer to the message instance of the DHCP4 packet.
39 @param Udp4 Pointer to the EFI_UDP4_PROTOCOL instance.
44 IN EFI_DHCP4_PACKET
*Seed
,
45 IN EFI_UDP4_PROTOCOL
*Udp4
48 EFI_SIMPLE_NETWORK_MODE Mode
;
49 EFI_DHCP4_HEADER
*Header
;
51 Udp4
->GetModeData (Udp4
, NULL
, NULL
, NULL
, &Mode
);
53 Seed
->Size
= sizeof (EFI_DHCP4_PACKET
);
54 Seed
->Length
= sizeof (Seed
->Dhcp4
);
56 Header
= &Seed
->Dhcp4
.Header
;
58 ZeroMem (Header
, sizeof (EFI_DHCP4_HEADER
));
59 Header
->OpCode
= PXEBC_DHCP4_OPCODE_REQUEST
;
60 Header
->HwType
= Mode
.IfType
;
61 Header
->HwAddrLen
= (UINT8
) Mode
.HwAddressSize
;
62 CopyMem (Header
->ClientHwAddr
, &Mode
.CurrentAddress
, Header
->HwAddrLen
);
64 Seed
->Dhcp4
.Magik
= PXEBC_DHCP4_MAGIC
;
65 Seed
->Dhcp4
.Option
[0] = DHCP4_TAG_EOP
;
70 Copy the DCHP4 packet from srouce to destination.
72 @param Dst Pointer to the EFI_DHCP4_PROTOCOL instance.
73 @param Src Pointer to the EFI_DHCP4_PROTOCOL instance.
77 PxeBcCopyEfiDhcp4Packet (
78 IN EFI_DHCP4_PACKET
*Dst
,
79 IN EFI_DHCP4_PACKET
*Src
82 ASSERT (Dst
->Size
>= Src
->Length
);
84 CopyMem (&Dst
->Dhcp4
, &Src
->Dhcp4
, Src
->Length
);
85 Dst
->Length
= Src
->Length
;
90 Copy the dhcp4 packet to the PxeBc private data and parse the dhcp4 packet.
92 @param Private Pointer to PxeBc private data.
93 @param OfferIndex Index of cached packets as complements of pxe mode data,
94 the index is maximum offer number.
99 IN PXEBC_PRIVATE_DATA
*Private
,
103 EFI_PXE_BASE_CODE_MODE
*Mode
;
104 EFI_DHCP4_PACKET
*Offer
;
106 ASSERT (OfferIndex
< Private
->NumOffers
);
107 ASSERT (OfferIndex
< PXEBC_MAX_OFFER_NUM
);
109 Mode
= Private
->PxeBc
.Mode
;
110 Offer
= &Private
->Dhcp4Offers
[OfferIndex
].Packet
.Offer
;
112 PxeBcCopyEfiDhcp4Packet (&Private
->ProxyOffer
.Packet
.Offer
, Offer
);
113 CopyMem (&Mode
->ProxyOffer
, &Offer
->Dhcp4
, Offer
->Length
);
114 Mode
->ProxyOfferReceived
= TRUE
;
116 PxeBcParseCachedDhcpPacket (&Private
->ProxyOffer
);
121 Parse the cached dhcp packet.
123 @param CachedPacket Pointer to cached dhcp packet.
125 @retval TRUE Succeed to parse and validation.
126 @retval FALSE Fail to parse or validation.
130 PxeBcParseCachedDhcpPacket (
131 IN PXEBC_CACHED_DHCP4_PACKET
*CachedPacket
134 EFI_DHCP4_PACKET
*Offer
;
135 EFI_DHCP4_PACKET_OPTION
**Options
;
136 EFI_DHCP4_PACKET_OPTION
*Option
;
141 CachedPacket
->IsPxeOffer
= FALSE
;
142 ZeroMem (CachedPacket
->Dhcp4Option
, sizeof (CachedPacket
->Dhcp4Option
));
143 ZeroMem (&CachedPacket
->PxeVendorOption
, sizeof (CachedPacket
->PxeVendorOption
));
145 Offer
= &CachedPacket
->Packet
.Offer
;
146 Options
= CachedPacket
->Dhcp4Option
;
149 // Parse interested dhcp options and store their pointers in CachedPacket->Dhcp4Option.
150 // First, try to parse DHCPv4 options from the DHCP optional parameters field.
152 for (Index
= 0; Index
< PXEBC_DHCP4_TAG_INDEX_MAX
; Index
++) {
153 Options
[Index
] = PxeBcParseExtendOptions (
155 GET_OPTION_BUFFER_LEN (Offer
),
156 mInterestedDhcp4Tags
[Index
]
160 // Second, Check if bootfilename and serverhostname is overloaded to carry DHCP options refers to rfc-2132.
161 // If yes, try to parse options from the BootFileName field, then ServerName field.
163 Option
= Options
[PXEBC_DHCP4_TAG_INDEX_OVERLOAD
];
164 if (Option
!= NULL
) {
165 if ((Option
->Data
[0] & PXEBC_DHCP4_OVERLOAD_FILE
) != 0) {
166 for (Index
= 0; Index
< PXEBC_DHCP4_TAG_INDEX_MAX
; Index
++) {
167 if (Options
[Index
] == NULL
) {
168 Options
[Index
] = PxeBcParseExtendOptions (
169 (UINT8
*) Offer
->Dhcp4
.Header
.BootFileName
,
170 sizeof (Offer
->Dhcp4
.Header
.BootFileName
),
171 mInterestedDhcp4Tags
[Index
]
176 if ((Option
->Data
[0] & PXEBC_DHCP4_OVERLOAD_SERVER_NAME
) != 0) {
177 for (Index
= 0; Index
< PXEBC_DHCP4_TAG_INDEX_MAX
; Index
++) {
178 if (Options
[Index
] == NULL
) {
179 Options
[Index
] = PxeBcParseExtendOptions (
180 (UINT8
*) Offer
->Dhcp4
.Header
.ServerName
,
181 sizeof (Offer
->Dhcp4
.Header
.ServerName
),
182 mInterestedDhcp4Tags
[Index
]
190 // Check whether is an offer with PXEClient or not.
192 Option
= Options
[PXEBC_DHCP4_TAG_INDEX_CLASS_ID
];
193 if ((Option
!= NULL
) && (Option
->Length
>= 9) &&
194 (CompareMem (Option
->Data
, DEFAULT_CLASS_ID_DATA
, 9) == 0)) {
196 CachedPacket
->IsPxeOffer
= TRUE
;
200 // Parse pxe vendor options and store their content/pointers in CachedPacket->PxeVendorOption.
202 Option
= Options
[PXEBC_DHCP4_TAG_INDEX_VENDOR
];
203 if (CachedPacket
->IsPxeOffer
&& (Option
!= NULL
)) {
205 if (!PxeBcParseVendorOptions (Option
, &CachedPacket
->PxeVendorOption
)) {
212 // Parse PXE boot file name:
213 // According to PXE spec, boot file name should be read from DHCP option 67 (bootfile name) if present.
214 // Otherwise, read from boot file field in DHCP header.
216 if (Options
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] != NULL
) {
218 // RFC 2132, Section 9.5 does not strictly state Bootfile name (option 67) is null
219 // terminated string. So force to append null terminated character at the end of string.
221 Ptr8
= (UINT8
*)&Options
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
]->Data
[0];
222 Ptr8
+= Options
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
]->Length
;
223 if (*(Ptr8
- 1) != '\0') {
226 } else if (Offer
->Dhcp4
.Header
.BootFileName
[0] != 0) {
228 // If the bootfile is not present and bootfilename is present in dhcp packet, just parse it.
229 // And do not count dhcp option header, or else will destroy the serverhostname.
231 // Make sure "BootFileName" is not overloaded.
233 if (Options
[PXEBC_DHCP4_TAG_INDEX_OVERLOAD
] == NULL
||
234 (Options
[PXEBC_DHCP4_TAG_INDEX_OVERLOAD
]->Data
[0] & PXEBC_DHCP4_OVERLOAD_FILE
) == 0) {
235 Options
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] = (EFI_DHCP4_PACKET_OPTION
*) (&Offer
->Dhcp4
.Header
.BootFileName
[0] -
236 OFFSET_OF (EFI_DHCP4_PACKET_OPTION
, Data
[0]));
241 // Determine offer type of the dhcp packet.
243 Option
= Options
[PXEBC_DHCP4_TAG_INDEX_MSG_TYPE
];
244 if ((Option
== NULL
) || (Option
->Data
[0] == 0)) {
246 // It's a bootp offer
248 Option
= CachedPacket
->Dhcp4Option
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
];
249 if (Option
== NULL
) {
251 // bootp offer without bootfilename, discard it.
256 OfferType
= DHCP4_PACKET_TYPE_BOOTP
;
260 if (IS_VALID_DISCOVER_VENDOR_OPTION (CachedPacket
->PxeVendorOption
.BitMap
)) {
262 // It's a pxe10 offer with PXEClient and discover vendor option.
264 OfferType
= DHCP4_PACKET_TYPE_PXE10
;
265 } else if (IS_VALID_MTFTP_VENDOR_OPTION (CachedPacket
->PxeVendorOption
.BitMap
)) {
267 // It's a wfm11a offer with PXEClient and mtftp vendor option, and
268 // return false since mtftp not supported currently.
273 // If the binl offer with only PXEClient.
275 OfferType
= (UINT8
) ((CachedPacket
->IsPxeOffer
) ? DHCP4_PACKET_TYPE_BINL
: DHCP4_PACKET_TYPE_DHCP_ONLY
);
279 CachedPacket
->OfferType
= OfferType
;
286 Offer dhcp service with a BINL dhcp offer.
288 @param Private Pointer to PxeBc private data.
289 @param Index Index of cached packets as complements of pxe mode data,
290 the index is maximum offer number.
292 @retval TRUE Offer the service successfully under priority BINL.
293 @retval FALSE Boot Service failed, parse cached dhcp packet failed or this
294 BINL ack cannot find options set or bootfile name specified.
299 IN PXEBC_PRIVATE_DATA
*Private
,
303 EFI_DHCP4_PACKET
*Offer
;
304 EFI_IP_ADDRESS ServerIp
;
306 PXEBC_CACHED_DHCP4_PACKET
*CachedPacket
;
307 EFI_DHCP4_PACKET
*Reply
;
309 ASSERT (Index
< PXEBC_MAX_OFFER_NUM
);
310 ASSERT (Private
->Dhcp4Offers
[Index
].OfferType
== DHCP4_PACKET_TYPE_BINL
);
312 Offer
= &Private
->Dhcp4Offers
[Index
].Packet
.Offer
;
315 // Use siaddr(next server) in DHCPOFFER packet header, if zero, use option 54(server identifier)
316 // in DHCPOFFER packet.
317 // (It does not comply with PXE Spec, Ver2.1)
319 if (EFI_IP4_EQUAL (&Offer
->Dhcp4
.Header
.ServerAddr
.Addr
, &mZeroIp4Addr
)) {
322 Private
->Dhcp4Offers
[Index
].Dhcp4Option
[PXEBC_DHCP4_TAG_INDEX_SERVER_ID
]->Data
,
323 sizeof (EFI_IPv4_ADDRESS
)
328 &Offer
->Dhcp4
.Header
.ServerAddr
,
329 sizeof (EFI_IPv4_ADDRESS
)
332 if (ServerIp
.Addr
[0] == 0) {
336 CachedPacket
= &Private
->ProxyOffer
;
337 Reply
= &CachedPacket
->Packet
.Offer
;
339 Status
= PxeBcDiscvBootService (
350 if (EFI_ERROR (Status
)) {
354 if (!PxeBcParseCachedDhcpPacket (CachedPacket
)) {
358 if ((CachedPacket
->OfferType
!= DHCP4_PACKET_TYPE_PXE10
) &&
359 (CachedPacket
->Dhcp4Option
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] == NULL
)) {
361 // This BINL ack doesn't have discovery options set or bootfile name
367 Private
->PxeBc
.Mode
->ProxyOfferReceived
= TRUE
;
368 CopyMem (&Private
->PxeBc
.Mode
->ProxyOffer
, &Reply
->Dhcp4
, Reply
->Length
);
375 Offer dhcp service for each proxy with a BINL dhcp offer.
377 @param Private Pointer to PxeBc private data
378 @param OfferIndex Pointer to the index of cached packets as complements of
379 pxe mode data, the index is maximum offer number.
381 @return If there is no service needed offer return FALSE, otherwise TRUE.
386 IN PXEBC_PRIVATE_DATA
*Private
,
387 OUT UINT32
*OfferIndex
392 for (Index
= 0; Index
< Private
->ProxyIndex
[DHCP4_PACKET_TYPE_BINL
]; Index
++) {
394 *OfferIndex
= Private
->BinlIndex
[Index
];
396 // Try this BINL proxy offer
398 if (PxeBcTryBinl (Private
, *OfferIndex
)) {
408 This function is to check the selected proxy offer (include BINL dhcp offer and
409 DHCP_ONLY offer ) and set the flag and copy the DHCP packets to the Pxe base code
412 @param Private Pointer to PxeBc private data.
414 @retval EFI_SUCCESS Operational successful.
415 @retval EFI_NO_RESPONSE Offer dhcp service failed.
419 PxeBcCheckSelectedOffer (
420 IN PXEBC_PRIVATE_DATA
*Private
423 PXEBC_CACHED_DHCP4_PACKET
*SelectedOffer
;
424 EFI_DHCP4_PACKET_OPTION
**Options
;
426 EFI_DHCP4_PACKET
*Offer
;
427 UINT32 ProxyOfferIndex
;
429 EFI_PXE_BASE_CODE_MODE
*Mode
;
430 EFI_DHCP4_PACKET
*Ack
;
432 ASSERT (Private
->SelectedOffer
!= 0);
434 Status
= EFI_SUCCESS
;
435 SelectedOffer
= &Private
->Dhcp4Offers
[Private
->SelectedOffer
- 1];
436 Options
= SelectedOffer
->Dhcp4Option
;
438 if (SelectedOffer
->OfferType
== DHCP4_PACKET_TYPE_BINL
) {
440 // The addresses are acquired from a BINL dhcp offer, try BINL to get
443 if (!PxeBcTryBinl (Private
, Private
->SelectedOffer
- 1)) {
444 Status
= EFI_NO_RESPONSE
;
446 } else if (SelectedOffer
->OfferType
== DHCP4_PACKET_TYPE_DHCP_ONLY
) {
448 // The selected offer to finish the D.O.R.A. is a DHCP only offer, we need
449 // try proxy offers if there are some, othewise the bootfile name must be
450 // set in this DHCP only offer.
452 if (Private
->GotProxyOffer
) {
454 // Get rid of the compiler warning.
457 if (Private
->SortOffers
) {
459 // The offers are sorted before selecting, the proxy offer type must be
460 // already determined.
462 ASSERT (Private
->ProxyIndex
[Private
->ProxyOfferType
] > 0);
464 if (Private
->ProxyOfferType
== DHCP4_PACKET_TYPE_BINL
) {
466 // We buffer all received BINL proxy offers, try them all one by one
468 if (!PxeBcTryBinlProxy (Private
, &ProxyOfferIndex
)) {
469 Status
= EFI_NO_RESPONSE
;
473 // For other types, only one proxy offer is buffered.
475 ProxyOfferIndex
= Private
->ProxyIndex
[Private
->ProxyOfferType
] - 1;
479 // The proxy offer type is not determined, choose proxy offer in the
482 Status
= EFI_NO_RESPONSE
;
484 ASSERT (Private
->NumOffers
< PXEBC_MAX_OFFER_NUM
);
485 for (Index
= 0; Index
< Private
->NumOffers
; Index
++) {
487 Offer
= &Private
->Dhcp4Offers
[Index
].Packet
.Offer
;
488 if (!IS_PROXY_DHCP_OFFER (Offer
)) {
490 // Skip non proxy dhcp offers.
495 if (Private
->Dhcp4Offers
[Index
].OfferType
== DHCP4_PACKET_TYPE_BINL
) {
499 if (!PxeBcTryBinl (Private
, Index
)) {
501 // Failed, skip to the next offer
507 Private
->ProxyOfferType
= Private
->Dhcp4Offers
[Index
].OfferType
;
508 ProxyOfferIndex
= Index
;
509 Status
= EFI_SUCCESS
;
514 if (!EFI_ERROR (Status
) && (Private
->ProxyOfferType
!= DHCP4_PACKET_TYPE_BINL
)) {
516 // Copy the proxy offer to Mode and set the flag
518 PxeBcCopyProxyOffer (Private
, ProxyOfferIndex
);
522 // No proxy offer is received, the bootfile name MUST be set.
524 ASSERT (Options
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] != NULL
);
528 if (!EFI_ERROR (Status
)) {
530 // Everything is OK, set the flag and copy the DHCP packets.
532 Mode
= Private
->PxeBc
.Mode
;
533 Offer
= &SelectedOffer
->Packet
.Offer
;
536 // The discover packet is already copied, just set flag here.
538 Mode
->DhcpDiscoverValid
= TRUE
;
540 Ack
= &Private
->Dhcp4Ack
.Packet
.Ack
;
541 if (SelectedOffer
->OfferType
== DHCP4_PACKET_TYPE_BOOTP
) {
543 // Other type of ACK is already cached. Bootp is special that we should
544 // use the bootp reply as the ACK and put it into the DHCP_ONLY buffer.
546 PxeBcCopyEfiDhcp4Packet (&Private
->Dhcp4Ack
.Packet
.Ack
, Offer
);
549 PxeBcParseCachedDhcpPacket (&Private
->Dhcp4Ack
);
551 Mode
->DhcpAckReceived
= TRUE
;
554 // Copy the dhcp ack.
556 CopyMem (&Mode
->DhcpAck
, &Ack
->Dhcp4
, Ack
->Length
);
564 Cache the Dhcp4 packet offer, Parse and validate each option of the packet.
566 @param Private Pointer to PxeBc private data.
567 @param RcvdOffer Pointer to the received Dhcp proxy offer packet.
571 PxeBcCacheDhcpOffer (
572 IN PXEBC_PRIVATE_DATA
*Private
,
573 IN EFI_DHCP4_PACKET
*RcvdOffer
576 PXEBC_CACHED_DHCP4_PACKET
*CachedOffer
;
577 EFI_DHCP4_PACKET
*Offer
;
580 CachedOffer
= &Private
->Dhcp4Offers
[Private
->NumOffers
];
581 Offer
= &CachedOffer
->Packet
.Offer
;
584 // Cache the orignal dhcp packet
586 PxeBcCopyEfiDhcp4Packet (Offer
, RcvdOffer
);
589 // Parse and validate the options (including dhcp option and vendor option)
591 if (!PxeBcParseCachedDhcpPacket (CachedOffer
)) {
595 OfferType
= CachedOffer
->OfferType
;
596 if (OfferType
>= DHCP4_PACKET_TYPE_MAX
) {
600 if (OfferType
== DHCP4_PACKET_TYPE_BOOTP
) {
602 if (Private
->BootpIndex
!= 0) {
604 // Only cache the first bootp offer, discard others.
609 // Take as a dhcp only offer, but record index specifically.
611 Private
->BootpIndex
= Private
->NumOffers
+ 1;
615 if (IS_PROXY_DHCP_OFFER (Offer
)) {
617 // It's a proxy dhcp offer with no your address, including pxe10, wfm11a or binl offer.
619 Private
->GotProxyOffer
= TRUE
;
621 if (OfferType
== DHCP4_PACKET_TYPE_BINL
) {
623 // Cache all binl offers.
625 Private
->BinlIndex
[Private
->ProxyIndex
[DHCP4_PACKET_TYPE_BINL
]] = Private
->NumOffers
;
626 Private
->ProxyIndex
[DHCP4_PACKET_TYPE_BINL
]++;
627 } else if (Private
->ProxyIndex
[OfferType
] != 0) {
629 // Only cache the first pxe10/wfm11a offers each, discard the others.
634 // Record index of the proxy dhcp offer with type other than binl.
636 Private
->ProxyIndex
[OfferType
] = Private
->NumOffers
+ 1;
640 // It's a dhcp offer with your address.
642 ASSERT (Private
->ServerCount
[OfferType
] < PXEBC_MAX_OFFER_NUM
);
643 Private
->OfferIndex
[OfferType
][Private
->ServerCount
[OfferType
]] = Private
->NumOffers
;
644 Private
->ServerCount
[OfferType
]++;
649 // Count the accepted offers.
651 Private
->NumOffers
++;
655 Switch the Ip4 policy to static.
657 @param[in] Private The pointer to PXEBC_PRIVATE_DATA.
659 @retval EFI_SUCCESS The policy is already configured to static.
660 @retval Others Other error as indicated..
665 IN PXEBC_PRIVATE_DATA
*Private
669 EFI_IP4_CONFIG2_PROTOCOL
*Ip4Config2
;
670 EFI_IP4_CONFIG2_POLICY Policy
;
673 Ip4Config2
= Private
->Ip4Config2
;
674 DataSize
= sizeof (EFI_IP4_CONFIG2_POLICY
);
675 Status
= Ip4Config2
->GetData (
677 Ip4Config2DataTypePolicy
,
681 if (EFI_ERROR (Status
)) {
685 if (Policy
!= Ip4Config2PolicyStatic
) {
686 Policy
= Ip4Config2PolicyStatic
;
687 Status
= Ip4Config2
->SetData (
689 Ip4Config2DataTypePolicy
,
690 sizeof (EFI_IP4_CONFIG2_POLICY
),
693 if (EFI_ERROR (Status
)) {
703 Select the specified proxy offer, such as BINL, DHCP_ONLY and so on.
704 If the proxy does not exist, try offers with bootfile.
706 @param Private Pointer to PxeBc private data.
711 IN PXEBC_PRIVATE_DATA
*Private
716 EFI_DHCP4_PACKET
*Offer
;
718 Private
->SelectedOffer
= 0;
720 if (Private
->SortOffers
) {
722 // Select offer according to the priority
724 if (Private
->ServerCount
[DHCP4_PACKET_TYPE_PXE10
] > 0) {
728 Private
->SelectedOffer
= Private
->OfferIndex
[DHCP4_PACKET_TYPE_PXE10
][0] + 1;
730 } else if (Private
->ServerCount
[DHCP4_PACKET_TYPE_WFM11A
] > 0) {
734 Private
->SelectedOffer
= Private
->OfferIndex
[DHCP4_PACKET_TYPE_WFM11A
][0] + 1;
736 } else if ((Private
->ProxyIndex
[DHCP4_PACKET_TYPE_PXE10
] > 0) &&
737 (Private
->ServerCount
[DHCP4_PACKET_TYPE_DHCP_ONLY
] > 0)
740 // DHCP only and proxy DHCP with PXE10
742 Private
->SelectedOffer
= Private
->OfferIndex
[DHCP4_PACKET_TYPE_DHCP_ONLY
][0] + 1;
743 Private
->ProxyOfferType
= DHCP4_PACKET_TYPE_PXE10
;
745 } else if ((Private
->ProxyIndex
[DHCP4_PACKET_TYPE_WFM11A
] > 0) &&
746 (Private
->ServerCount
[DHCP4_PACKET_TYPE_DHCP_ONLY
] > 0)
749 // DHCP only and proxy DHCP with WfM
751 Private
->SelectedOffer
= Private
->OfferIndex
[DHCP4_PACKET_TYPE_DHCP_ONLY
][0] + 1;
752 Private
->ProxyOfferType
= DHCP4_PACKET_TYPE_WFM11A
;
754 } else if (Private
->ServerCount
[DHCP4_PACKET_TYPE_BINL
] > 0) {
758 Private
->SelectedOffer
= Private
->OfferIndex
[DHCP4_PACKET_TYPE_BINL
][0] + 1;
760 } else if ((Private
->ProxyIndex
[DHCP4_PACKET_TYPE_BINL
] > 0) &&
761 (Private
->ServerCount
[DHCP4_PACKET_TYPE_DHCP_ONLY
] > 0)
764 // DHCP only and proxy DHCP with BINL
766 Private
->SelectedOffer
= Private
->OfferIndex
[DHCP4_PACKET_TYPE_DHCP_ONLY
][0] + 1;
767 Private
->ProxyOfferType
= DHCP4_PACKET_TYPE_BINL
;
771 // Try offers with bootfile
773 for (Index
= 0; Index
< Private
->ServerCount
[DHCP4_PACKET_TYPE_DHCP_ONLY
]; Index
++) {
775 // Select the first DHCP only offer with bootfile
777 OfferIndex
= Private
->OfferIndex
[DHCP4_PACKET_TYPE_DHCP_ONLY
][Index
];
778 if (Private
->Dhcp4Offers
[OfferIndex
].Dhcp4Option
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] != NULL
) {
779 Private
->SelectedOffer
= OfferIndex
+ 1;
784 if (Private
->SelectedOffer
== 0) {
786 // Select the Bootp reply with bootfile if any
788 Private
->SelectedOffer
= Private
->BootpIndex
;
793 // Try the offers in the received order.
795 for (Index
= 0; Index
< Private
->NumOffers
; Index
++) {
797 Offer
= &Private
->Dhcp4Offers
[Index
].Packet
.Offer
;
799 if (IS_PROXY_DHCP_OFFER (Offer
)) {
806 if ((Private
->Dhcp4Offers
[Index
].OfferType
== DHCP4_PACKET_TYPE_DHCP_ONLY
) &&
807 ((!Private
->GotProxyOffer
) && (Private
->Dhcp4Offers
[Index
].Dhcp4Option
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] == NULL
))) {
809 // DHCP only offer but no proxy offer received and no bootfile option in this offer
814 Private
->SelectedOffer
= Index
+ 1;
824 EFI_DHCP4_CALLBACK is provided by the consumer of the EFI DHCPv4 Protocol driver
825 to intercept events that occurred in the configuration process. This structure
826 provides advanced control of each state transition of the DHCP process. The
827 returned status code determines the behavior of the EFI DHCPv4 Protocol driver.
828 There are three possible returned values, which are described in the following
831 @param This Pointer to the EFI DHCPv4 Protocol instance that is used to
832 configure this callback function.
833 @param Context Pointer to the context that is initialized by
834 EFI_DHCP4_PROTOCOL.Configure().
835 @param CurrentState The current operational state of the EFI DHCPv4 Protocol
837 @param Dhcp4Event The event that occurs in the current state, which usually means a
839 @param Packet The DHCP packet that is going to be sent or already received.
840 @param NewPacket The packet that is used to replace the above Packet.
842 @retval EFI_SUCCESS Tells the EFI DHCPv4 Protocol driver to continue the DHCP process.
843 @retval EFI_NOT_READY Only used in the Dhcp4Selecting state. The EFI DHCPv4 Protocol
844 driver will continue to wait for more DHCPOFFER packets until the retry
846 @retval EFI_ABORTED Tells the EFI DHCPv4 Protocol driver to abort the current process and
847 return to the Dhcp4Init or Dhcp4InitReboot state.
853 IN EFI_DHCP4_PROTOCOL
* This
,
855 IN EFI_DHCP4_STATE CurrentState
,
856 IN EFI_DHCP4_EVENT Dhcp4Event
,
857 IN EFI_DHCP4_PACKET
* Packet OPTIONAL
,
858 OUT EFI_DHCP4_PACKET
**NewPacket OPTIONAL
861 PXEBC_PRIVATE_DATA
*Private
;
862 EFI_PXE_BASE_CODE_MODE
*Mode
;
863 EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL
*Callback
;
864 EFI_DHCP4_PACKET_OPTION
*MaxMsgSize
;
868 EFI_DHCP4_HEADER
*DhcpHeader
;
870 if ((Dhcp4Event
!= Dhcp4RcvdOffer
) &&
871 (Dhcp4Event
!= Dhcp4SelectOffer
) &&
872 (Dhcp4Event
!= Dhcp4SendDiscover
) &&
873 (Dhcp4Event
!= Dhcp4RcvdAck
) &&
874 (Dhcp4Event
!= Dhcp4SendRequest
)) {
878 Private
= (PXEBC_PRIVATE_DATA
*) Context
;
879 Mode
= Private
->PxeBc
.Mode
;
880 Callback
= Private
->PxeBcCallback
;
883 // Override the Maximum DHCP Message Size.
885 MaxMsgSize
= PxeBcParseExtendOptions (
886 Packet
->Dhcp4
.Option
,
887 GET_OPTION_BUFFER_LEN (Packet
),
890 if (MaxMsgSize
!= NULL
) {
891 Value
= HTONS (PXEBC_DHCP4_MAX_PACKET_SIZE
);
892 CopyMem (MaxMsgSize
->Data
, &Value
, sizeof (Value
));
895 if ((Dhcp4Event
!= Dhcp4SelectOffer
) && (Callback
!= NULL
)) {
896 Received
= (BOOLEAN
) ((Dhcp4Event
== Dhcp4RcvdOffer
) || (Dhcp4Event
== Dhcp4RcvdAck
));
897 Status
= Callback
->Callback (
902 (EFI_PXE_BASE_CODE_PACKET
*) &Packet
->Dhcp4
904 if (Status
!= EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE
) {
909 Status
= EFI_SUCCESS
;
911 switch (Dhcp4Event
) {
913 case Dhcp4SendDiscover
:
914 case Dhcp4SendRequest
:
915 if (Mode
->SendGUID
) {
917 // send the system GUID instead of the MAC address as the hardware address
918 // in the DHCP packet header.
920 DhcpHeader
= &Packet
->Dhcp4
.Header
;
922 if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID
*) DhcpHeader
->ClientHwAddr
))) {
924 // GUID not yet set - send all 0xff's to show programable (via SetVariable)
925 // SetMem(DHCPV4_OPTIONS_BUFFER.DhcpPlatformId.Guid, sizeof(EFI_GUID), 0xff);
926 // GUID not yet set - send all 0's to show not programable
928 ZeroMem (DhcpHeader
->ClientHwAddr
, sizeof (EFI_GUID
));
931 DhcpHeader
->HwAddrLen
= (UINT8
) sizeof (EFI_GUID
);
934 if (Dhcp4Event
== Dhcp4SendDiscover
) {
936 // Cache the dhcp discover packet, of which some information will be used later.
938 CopyMem (Mode
->DhcpDiscover
.Raw
, &Packet
->Dhcp4
, Packet
->Length
);
944 Status
= EFI_NOT_READY
;
945 if (Private
->NumOffers
< PXEBC_MAX_OFFER_NUM
) {
947 // Cache the dhcp offers in Private->Dhcp4Offers[]
949 PxeBcCacheDhcpOffer (Private
, Packet
);
954 case Dhcp4SelectOffer
:
956 // Select an offer, if succeeded, Private->SelectedOffer points to
957 // the index of the selected one.
959 PxeBcSelectOffer (Private
);
961 if (Private
->SelectedOffer
== 0) {
962 Status
= EFI_ABORTED
;
964 *NewPacket
= &Private
->Dhcp4Offers
[Private
->SelectedOffer
- 1].Packet
.Offer
;
973 ASSERT (Private
->SelectedOffer
!= 0);
975 PxeBcCopyEfiDhcp4Packet (&Private
->Dhcp4Ack
.Packet
.Ack
, Packet
);
987 Initialize the DHCP options and build the option list.
989 @param Private Pointer to PxeBc private data.
990 @param OptList Pointer to a DHCP option list.
992 @param IsDhcpDiscover Discover dhcp option or not.
994 @return The index item number of the option list.
998 PxeBcBuildDhcpOptions (
999 IN PXEBC_PRIVATE_DATA
*Private
,
1000 IN EFI_DHCP4_PACKET_OPTION
**OptList
,
1001 IN BOOLEAN IsDhcpDiscover
1005 PXEBC_DHCP4_OPTION_ENTRY OptEnt
;
1009 OptList
[0] = (EFI_DHCP4_PACKET_OPTION
*) Private
->OptionBuffer
;
1011 if (!IsDhcpDiscover
) {
1013 // Append message type.
1015 OptList
[Index
]->OpCode
= DHCP4_TAG_MSG_TYPE
;
1016 OptList
[Index
]->Length
= 1;
1017 OptEnt
.Mesg
= (PXEBC_DHCP4_OPTION_MESG
*) OptList
[Index
]->Data
;
1018 OptEnt
.Mesg
->Type
= PXEBC_DHCP4_MSG_TYPE_REQUEST
;
1020 OptList
[Index
] = GET_NEXT_DHCP_OPTION (OptList
[Index
- 1]);
1023 // Append max message size.
1025 OptList
[Index
]->OpCode
= DHCP4_TAG_MAXMSG
;
1026 OptList
[Index
]->Length
= (UINT8
) sizeof (PXEBC_DHCP4_OPTION_MAX_MESG_SIZE
);
1027 OptEnt
.MaxMesgSize
= (PXEBC_DHCP4_OPTION_MAX_MESG_SIZE
*) OptList
[Index
]->Data
;
1028 Value
= NTOHS (PXEBC_DHCP4_MAX_PACKET_SIZE
);
1029 CopyMem (&OptEnt
.MaxMesgSize
->Size
, &Value
, sizeof (UINT16
));
1031 OptList
[Index
] = GET_NEXT_DHCP_OPTION (OptList
[Index
- 1]);
1034 // Parameter request list option.
1036 OptList
[Index
]->OpCode
= DHCP4_TAG_PARA_LIST
;
1037 OptList
[Index
]->Length
= 35;
1038 OptEnt
.Para
= (PXEBC_DHCP4_OPTION_PARA
*) OptList
[Index
]->Data
;
1039 OptEnt
.Para
->ParaList
[0] = DHCP4_TAG_NETMASK
;
1040 OptEnt
.Para
->ParaList
[1] = DHCP4_TAG_TIME_OFFSET
;
1041 OptEnt
.Para
->ParaList
[2] = DHCP4_TAG_ROUTER
;
1042 OptEnt
.Para
->ParaList
[3] = DHCP4_TAG_TIME_SERVER
;
1043 OptEnt
.Para
->ParaList
[4] = DHCP4_TAG_NAME_SERVER
;
1044 OptEnt
.Para
->ParaList
[5] = DHCP4_TAG_DNS_SERVER
;
1045 OptEnt
.Para
->ParaList
[6] = DHCP4_TAG_HOSTNAME
;
1046 OptEnt
.Para
->ParaList
[7] = DHCP4_TAG_BOOTFILE_LEN
;
1047 OptEnt
.Para
->ParaList
[8] = DHCP4_TAG_DOMAINNAME
;
1048 OptEnt
.Para
->ParaList
[9] = DHCP4_TAG_ROOTPATH
;
1049 OptEnt
.Para
->ParaList
[10] = DHCP4_TAG_EXTEND_PATH
;
1050 OptEnt
.Para
->ParaList
[11] = DHCP4_TAG_EMTU
;
1051 OptEnt
.Para
->ParaList
[12] = DHCP4_TAG_TTL
;
1052 OptEnt
.Para
->ParaList
[13] = DHCP4_TAG_BROADCAST
;
1053 OptEnt
.Para
->ParaList
[14] = DHCP4_TAG_NIS_DOMAIN
;
1054 OptEnt
.Para
->ParaList
[15] = DHCP4_TAG_NIS_SERVER
;
1055 OptEnt
.Para
->ParaList
[16] = DHCP4_TAG_NTP_SERVER
;
1056 OptEnt
.Para
->ParaList
[17] = DHCP4_TAG_VENDOR
;
1057 OptEnt
.Para
->ParaList
[18] = DHCP4_TAG_REQUEST_IP
;
1058 OptEnt
.Para
->ParaList
[19] = DHCP4_TAG_LEASE
;
1059 OptEnt
.Para
->ParaList
[20] = DHCP4_TAG_SERVER_ID
;
1060 OptEnt
.Para
->ParaList
[21] = DHCP4_TAG_T1
;
1061 OptEnt
.Para
->ParaList
[22] = DHCP4_TAG_T2
;
1062 OptEnt
.Para
->ParaList
[23] = DHCP4_TAG_VENDOR_CLASS_ID
;
1063 OptEnt
.Para
->ParaList
[24] = DHCP4_TAG_TFTP
;
1064 OptEnt
.Para
->ParaList
[25] = DHCP4_TAG_BOOTFILE
;
1065 OptEnt
.Para
->ParaList
[26] = DHCP4_TAG_UUID
;
1066 OptEnt
.Para
->ParaList
[27] = 0x80;
1067 OptEnt
.Para
->ParaList
[28] = 0x81;
1068 OptEnt
.Para
->ParaList
[29] = 0x82;
1069 OptEnt
.Para
->ParaList
[30] = 0x83;
1070 OptEnt
.Para
->ParaList
[31] = 0x84;
1071 OptEnt
.Para
->ParaList
[32] = 0x85;
1072 OptEnt
.Para
->ParaList
[33] = 0x86;
1073 OptEnt
.Para
->ParaList
[34] = 0x87;
1075 OptList
[Index
] = GET_NEXT_DHCP_OPTION (OptList
[Index
- 1]);
1078 // Append UUID/Guid-based client identifier option
1080 OptList
[Index
]->OpCode
= DHCP4_TAG_UUID
;
1081 OptList
[Index
]->Length
= (UINT8
) sizeof (PXEBC_DHCP4_OPTION_UUID
);
1082 OptEnt
.Uuid
= (PXEBC_DHCP4_OPTION_UUID
*) OptList
[Index
]->Data
;
1083 OptEnt
.Uuid
->Type
= 0;
1085 OptList
[Index
] = GET_NEXT_DHCP_OPTION (OptList
[Index
- 1]);
1087 if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID
*) OptEnt
.Uuid
->Guid
))) {
1089 // GUID not yet set - send all 0xff's to show programable (via SetVariable)
1090 // SetMem(DHCPV4_OPTIONS_BUFFER.DhcpPlatformId.Guid, sizeof(EFI_GUID), 0xff);
1091 // GUID not yet set - send all 0's to show not programable
1093 ZeroMem (OptEnt
.Uuid
->Guid
, sizeof (EFI_GUID
));
1097 // Append client network device interface option
1099 OptList
[Index
]->OpCode
= DHCP4_TAG_UNDI
;
1100 OptList
[Index
]->Length
= (UINT8
) sizeof (PXEBC_DHCP4_OPTION_UNDI
);
1101 OptEnt
.Undi
= (PXEBC_DHCP4_OPTION_UNDI
*) OptList
[Index
]->Data
;
1102 if (Private
->Nii
!= NULL
) {
1103 OptEnt
.Undi
->Type
= Private
->Nii
->Type
;
1104 OptEnt
.Undi
->MajorVer
= Private
->Nii
->MajorVer
;
1105 OptEnt
.Undi
->MinorVer
= Private
->Nii
->MinorVer
;
1107 OptEnt
.Undi
->Type
= DEFAULT_UNDI_TYPE
;
1108 OptEnt
.Undi
->MajorVer
= DEFAULT_UNDI_MAJOR
;
1109 OptEnt
.Undi
->MinorVer
= DEFAULT_UNDI_MINOR
;
1113 OptList
[Index
] = GET_NEXT_DHCP_OPTION (OptList
[Index
- 1]);
1116 // Append client system architecture option
1118 OptList
[Index
]->OpCode
= DHCP4_TAG_ARCH
;
1119 OptList
[Index
]->Length
= (UINT8
) sizeof (PXEBC_DHCP4_OPTION_ARCH
);
1120 OptEnt
.Arch
= (PXEBC_DHCP4_OPTION_ARCH
*) OptList
[Index
]->Data
;
1121 Value
= HTONS (EFI_PXE_CLIENT_SYSTEM_ARCHITECTURE
);
1122 CopyMem (&OptEnt
.Arch
->Type
, &Value
, sizeof (UINT16
));
1124 OptList
[Index
] = GET_NEXT_DHCP_OPTION (OptList
[Index
- 1]);
1127 // Append client system architecture option
1129 OptList
[Index
]->OpCode
= DHCP4_TAG_VENDOR_CLASS_ID
;
1130 OptList
[Index
]->Length
= (UINT8
) sizeof (PXEBC_DHCP4_OPTION_CLID
);
1131 OptEnt
.Clid
= (PXEBC_DHCP4_OPTION_CLID
*) OptList
[Index
]->Data
;
1132 CopyMem (OptEnt
.Clid
, DEFAULT_CLASS_ID_DATA
, sizeof (PXEBC_DHCP4_OPTION_CLID
));
1133 CvtNum (EFI_PXE_CLIENT_SYSTEM_ARCHITECTURE
, OptEnt
.Clid
->ArchitectureType
, sizeof (OptEnt
.Clid
->ArchitectureType
));
1135 if (Private
->Nii
!= NULL
) {
1137 // If NII protocol exists, update DHCP option data
1139 CopyMem (OptEnt
.Clid
->InterfaceName
, Private
->Nii
->StringId
, sizeof (OptEnt
.Clid
->InterfaceName
));
1140 CvtNum (Private
->Nii
->MajorVer
, OptEnt
.Clid
->UndiMajor
, sizeof (OptEnt
.Clid
->UndiMajor
));
1141 CvtNum (Private
->Nii
->MinorVer
, OptEnt
.Clid
->UndiMinor
, sizeof (OptEnt
.Clid
->UndiMinor
));
1151 Discover the boot of service and initialize the vendor option if exists.
1153 @param Private Pointer to PxeBc private data.
1154 @param Type PxeBc option boot item type
1155 @param Layer PxeBc option boot item layer
1156 @param UseBis Use BIS or not
1157 @param DestIp Ip address for server
1158 @param IpCount The total count of the server ip address
1159 @param SrvList Server list
1160 @param IsDiscv Discover the vendor or not
1161 @param Reply The dhcp4 packet of Pxe reply
1163 @retval EFI_SUCCESS Operation succeeds.
1164 @retval EFI_OUT_OF_RESOURCES Allocate memory pool failed.
1165 @retval EFI_NOT_FOUND There is no vendor option exists.
1166 @retval EFI_TIMEOUT Send Pxe Discover time out.
1170 PxeBcDiscvBootService (
1171 IN PXEBC_PRIVATE_DATA
* Private
,
1175 IN EFI_IP_ADDRESS
* DestIp
,
1177 IN EFI_PXE_BASE_CODE_SRVLIST
* SrvList
,
1179 OUT EFI_DHCP4_PACKET
* Reply OPTIONAL
1182 EFI_PXE_BASE_CODE_UDP_PORT Sport
;
1183 EFI_PXE_BASE_CODE_MODE
*Mode
;
1184 EFI_DHCP4_PROTOCOL
*Dhcp4
;
1185 EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN Token
;
1191 EFI_DHCP4_LISTEN_POINT ListenPoint
;
1192 EFI_DHCP4_PACKET
*Response
;
1193 EFI_DHCP4_PACKET_OPTION
*OptList
[PXEBC_DHCP4_MAX_OPTION_NUM
];
1195 EFI_DHCP4_PACKET_OPTION
*PxeOpt
;
1196 PXEBC_OPTION_BOOT_ITEM
*PxeBootItem
;
1198 EFI_DHCP4_HEADER
*DhcpHeader
;
1201 Mode
= Private
->PxeBc
.Mode
;
1202 Dhcp4
= Private
->Dhcp4
;
1203 Status
= EFI_SUCCESS
;
1205 ZeroMem (&Token
, sizeof (EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN
));
1207 if (DestIp
== NULL
) {
1208 Sport
= PXEBC_DHCP4_S_PORT
;
1211 Sport
= PXEBC_BS_DISCOVER_PORT
;
1215 if (!UseBis
&& Layer
!= NULL
) {
1216 *Layer
&= EFI_PXE_BASE_CODE_BOOT_LAYER_MASK
;
1219 OptCount
= PxeBcBuildDhcpOptions (Private
, OptList
, FALSE
);
1222 ASSERT (Layer
!= NULL
);
1224 // Add vendor option of PXE_BOOT_ITEM
1226 VendorOptLen
= (UINT8
) ((sizeof (EFI_DHCP4_PACKET_OPTION
) - 1) * 2 + sizeof (PXEBC_OPTION_BOOT_ITEM
) + 1);
1227 OptList
[OptCount
] = AllocatePool (VendorOptLen
);
1228 if (OptList
[OptCount
] == NULL
) {
1229 return EFI_OUT_OF_RESOURCES
;
1232 OptList
[OptCount
]->OpCode
= DHCP4_TAG_VENDOR
;
1233 OptList
[OptCount
]->Length
= (UINT8
) (VendorOptLen
- 2);
1234 PxeOpt
= (EFI_DHCP4_PACKET_OPTION
*) OptList
[OptCount
]->Data
;
1235 PxeOpt
->OpCode
= PXEBC_VENDOR_TAG_BOOT_ITEM
;
1236 PxeOpt
->Length
= (UINT8
) sizeof (PXEBC_OPTION_BOOT_ITEM
);
1237 PxeBootItem
= (PXEBC_OPTION_BOOT_ITEM
*) PxeOpt
->Data
;
1238 PxeBootItem
->Type
= HTONS (Type
);
1239 PxeBootItem
->Layer
= HTONS (*Layer
);
1240 PxeOpt
->Data
[PxeOpt
->Length
] = DHCP4_TAG_EOP
;
1245 Status
= Dhcp4
->Build (Dhcp4
, &Private
->SeedPacket
, 0, NULL
, OptCount
, OptList
, &Token
.Packet
);
1248 FreePool (OptList
[OptCount
- 1]);
1251 if (EFI_ERROR (Status
)) {
1255 DhcpHeader
= &Token
.Packet
->Dhcp4
.Header
;
1256 if (Mode
->SendGUID
) {
1257 if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID
*) DhcpHeader
->ClientHwAddr
))) {
1259 // GUID not yet set - send all 0's to show not programable
1261 ZeroMem (DhcpHeader
->ClientHwAddr
, sizeof (EFI_GUID
));
1264 DhcpHeader
->HwAddrLen
= (UINT8
) sizeof (EFI_GUID
);
1267 Xid
= NET_RANDOM (NetRandomInitSeed ());
1268 Token
.Packet
->Dhcp4
.Header
.Xid
= HTONL(Xid
);
1269 Token
.Packet
->Dhcp4
.Header
.Reserved
= HTONS((UINT16
) ((IsBCast
) ? 0x8000 : 0));
1270 CopyMem (&Token
.Packet
->Dhcp4
.Header
.ClientAddr
, &Private
->StationIp
, sizeof (EFI_IPv4_ADDRESS
));
1272 Token
.RemotePort
= Sport
;
1275 SetMem (&Token
.RemoteAddress
, sizeof (EFI_IPv4_ADDRESS
), 0xff);
1277 CopyMem (&Token
.RemoteAddress
, DestIp
, sizeof (EFI_IPv4_ADDRESS
));
1280 CopyMem (&Token
.GatewayAddress
, &Private
->GatewayIp
, sizeof (EFI_IPv4_ADDRESS
));
1283 Token
.ListenPointCount
= 1;
1284 Token
.ListenPoints
= &ListenPoint
;
1285 Token
.ListenPoints
[0].ListenPort
= PXEBC_BS_DISCOVER_PORT
;
1286 CopyMem (&Token
.ListenPoints
[0].ListenAddress
, &Private
->StationIp
, sizeof(EFI_IPv4_ADDRESS
));
1287 CopyMem (&Token
.ListenPoints
[0].SubnetMask
, &Private
->SubnetMask
, sizeof(EFI_IPv4_ADDRESS
));
1290 // Send Pxe Discover
1292 for (TryIndex
= 1; TryIndex
<= PXEBC_BOOT_REQUEST_RETRIES
; TryIndex
++) {
1294 Token
.TimeoutValue
= (UINT16
) (PXEBC_BOOT_REQUEST_TIMEOUT
* TryIndex
);
1295 Token
.Packet
->Dhcp4
.Header
.Seconds
= (UINT16
) (PXEBC_BOOT_REQUEST_TIMEOUT
* (TryIndex
- 1));
1297 Status
= Dhcp4
->TransmitReceive (Dhcp4
, &Token
);
1299 if (Token
.Status
!= EFI_TIMEOUT
) {
1304 if (TryIndex
> PXEBC_BOOT_REQUEST_RETRIES
) {
1306 // No server response our PXE request
1308 Status
= EFI_TIMEOUT
;
1311 if (!EFI_ERROR (Status
)) {
1317 Response
= Token
.ResponseList
;
1319 while (RepIndex
< Token
.ResponseCount
) {
1321 while (SrvIndex
< IpCount
) {
1323 if (SrvList
[SrvIndex
].AcceptAnyResponse
) {
1327 if ((SrvList
[SrvIndex
].Type
== Type
) && EFI_IP4_EQUAL (&(Response
->Dhcp4
.Header
.ServerAddr
), &(Private
->ServerIp
))) {
1334 if ((IpCount
!= SrvIndex
) || (IpCount
== 0)) {
1341 Response
= (EFI_DHCP4_PACKET
*) ((UINT8
*) Response
+ Response
->Size
);
1344 if (RepIndex
< Token
.ResponseCount
) {
1346 if (Reply
!= NULL
) {
1347 PxeBcCopyEfiDhcp4Packet (Reply
, Response
);
1351 CopyMem (&(Mode
->PxeDiscover
), &(Token
.Packet
->Dhcp4
), Token
.Packet
->Length
);
1352 Mode
->PxeDiscoverValid
= TRUE
;
1354 CopyMem (Mode
->PxeReply
.Raw
, &Response
->Dhcp4
, Response
->Length
);
1355 Mode
->PxeReplyReceived
= TRUE
;
1358 Status
= EFI_NOT_FOUND
;
1362 // free the responselist
1364 if (Token
.ResponseList
!= NULL
) {
1365 FreePool (Token
.ResponseList
);
1369 // Free the dhcp packet
1371 FreePool (Token
.Packet
);
1378 Parse interested dhcp options.
1380 @param Buffer Pointer to the dhcp options packet.
1381 @param Length The length of the dhcp options.
1382 @param OptTag The option OpCode.
1384 @return NULL if the buffer length is 0 and OpCode is not
1385 DHCP4_TAG_EOP, or the pointer to the buffer.
1388 EFI_DHCP4_PACKET_OPTION
*
1389 PxeBcParseExtendOptions (
1395 EFI_DHCP4_PACKET_OPTION
*Option
;
1398 Option
= (EFI_DHCP4_PACKET_OPTION
*) Buffer
;
1401 while (Offset
< Length
&& Option
->OpCode
!= DHCP4_TAG_EOP
) {
1403 if (Option
->OpCode
== OptTag
) {
1408 if (Option
->OpCode
== DHCP4_TAG_PAD
) {
1411 Offset
+= Option
->Length
+ 2;
1414 Option
= (EFI_DHCP4_PACKET_OPTION
*) (Buffer
+ Offset
);
1422 This function is to parse and check vendor options.
1424 @param Dhcp4Option Pointer to dhcp options
1425 @param VendorOption Pointer to vendor options
1427 @return TRUE if valid for vendor options, or FALSE.
1431 PxeBcParseVendorOptions (
1432 IN EFI_DHCP4_PACKET_OPTION
*Dhcp4Option
,
1433 IN PXEBC_VENDOR_OPTION
*VendorOption
1437 UINT8 VendorOptionLen
;
1438 EFI_DHCP4_PACKET_OPTION
*PxeOption
;
1441 BitMap
= VendorOption
->BitMap
;
1442 VendorOptionLen
= Dhcp4Option
->Length
;
1443 PxeOption
= (EFI_DHCP4_PACKET_OPTION
*) &Dhcp4Option
->Data
[0];
1446 while ((Offset
< VendorOptionLen
) && (PxeOption
->OpCode
!= DHCP4_TAG_EOP
)) {
1448 // Parse every Vendor Option and set its BitMap
1450 switch (PxeOption
->OpCode
) {
1452 case PXEBC_VENDOR_TAG_MTFTP_IP
:
1454 CopyMem (&VendorOption
->MtftpIp
, PxeOption
->Data
, sizeof (EFI_IPv4_ADDRESS
));
1457 case PXEBC_VENDOR_TAG_MTFTP_CPORT
:
1459 CopyMem (&VendorOption
->MtftpCPort
, PxeOption
->Data
, sizeof (VendorOption
->MtftpCPort
));
1462 case PXEBC_VENDOR_TAG_MTFTP_SPORT
:
1464 CopyMem (&VendorOption
->MtftpSPort
, PxeOption
->Data
, sizeof (VendorOption
->MtftpSPort
));
1467 case PXEBC_VENDOR_TAG_MTFTP_TIMEOUT
:
1469 VendorOption
->MtftpTimeout
= *PxeOption
->Data
;
1472 case PXEBC_VENDOR_TAG_MTFTP_DELAY
:
1474 VendorOption
->MtftpDelay
= *PxeOption
->Data
;
1477 case PXEBC_VENDOR_TAG_DISCOVER_CTRL
:
1479 VendorOption
->DiscoverCtrl
= *PxeOption
->Data
;
1482 case PXEBC_VENDOR_TAG_DISCOVER_MCAST
:
1484 CopyMem (&VendorOption
->DiscoverMcastIp
, PxeOption
->Data
, sizeof (EFI_IPv4_ADDRESS
));
1487 case PXEBC_VENDOR_TAG_BOOT_SERVERS
:
1489 VendorOption
->BootSvrLen
= PxeOption
->Length
;
1490 VendorOption
->BootSvr
= (PXEBC_BOOT_SVR_ENTRY
*) PxeOption
->Data
;
1493 case PXEBC_VENDOR_TAG_BOOT_MENU
:
1495 VendorOption
->BootMenuLen
= PxeOption
->Length
;
1496 VendorOption
->BootMenu
= (PXEBC_BOOT_MENU_ENTRY
*) PxeOption
->Data
;
1499 case PXEBC_VENDOR_TAG_MENU_PROMPT
:
1501 VendorOption
->MenuPromptLen
= PxeOption
->Length
;
1502 VendorOption
->MenuPrompt
= (PXEBC_MENU_PROMPT
*) PxeOption
->Data
;
1505 case PXEBC_VENDOR_TAG_MCAST_ALLOC
:
1507 CopyMem (&VendorOption
->McastIpBase
, PxeOption
->Data
, sizeof (EFI_IPv4_ADDRESS
));
1508 CopyMem (&VendorOption
->McastIpBlock
, PxeOption
->Data
+ 4, sizeof (VendorOption
->McastIpBlock
));
1509 CopyMem (&VendorOption
->McastIpRange
, PxeOption
->Data
+ 6, sizeof (VendorOption
->McastIpRange
));
1512 case PXEBC_VENDOR_TAG_CREDENTIAL_TYPES
:
1514 VendorOption
->CredTypeLen
= PxeOption
->Length
;
1515 VendorOption
->CredType
= (UINT32
*) PxeOption
->Data
;
1518 case PXEBC_VENDOR_TAG_BOOT_ITEM
:
1520 CopyMem (&VendorOption
->BootSrvType
, PxeOption
->Data
, sizeof (VendorOption
->BootSrvType
));
1521 CopyMem (&VendorOption
->BootSrvLayer
, PxeOption
->Data
+ 2, sizeof (VendorOption
->BootSrvLayer
));
1525 SET_VENDOR_OPTION_BIT_MAP (BitMap
, PxeOption
->OpCode
);
1527 if (PxeOption
->OpCode
== DHCP4_TAG_PAD
) {
1530 Offset
= (UINT8
) (Offset
+ PxeOption
->Length
+ 2);
1533 PxeOption
= (EFI_DHCP4_PACKET_OPTION
*) (Dhcp4Option
->Data
+ Offset
);
1537 // FixMe, return falas if invalid of any vendor option
1545 This function display boot item detail.
1547 If the length of the boot item string over 70 Char, just display 70 Char.
1549 @param Str Pointer to a string (boot item string).
1550 @param Len The length of string.
1554 PxeBcDisplayBootItem (
1561 Len
= (UINT8
) MIN (70, Len
);
1564 AsciiPrint ("%a \n", Str
);
1570 Choose the boot prompt.
1572 @param Private Pointer to PxeBc private data.
1574 @retval EFI_SUCCESS Select boot prompt done.
1575 @retval EFI_TIMEOUT Select boot prompt time out.
1576 @retval EFI_NOT_FOUND The proxy offer is not Pxe10.
1577 @retval EFI_ABORTED User cancel the operation.
1578 @retval EFI_NOT_READY Read the input key from the keybroad has not finish.
1582 PxeBcSelectBootPrompt (
1583 IN PXEBC_PRIVATE_DATA
*Private
1586 PXEBC_CACHED_DHCP4_PACKET
*Packet
;
1587 PXEBC_VENDOR_OPTION
*VendorOpt
;
1588 EFI_EVENT TimeoutEvent
;
1589 EFI_EVENT DescendEvent
;
1590 EFI_INPUT_KEY InputKey
;
1598 TimeoutEvent
= NULL
;
1599 DescendEvent
= NULL
;
1601 if (Private
->PxeBc
.Mode
->ProxyOfferReceived
) {
1603 Packet
= &Private
->ProxyOffer
;
1606 Packet
= &Private
->Dhcp4Ack
;
1609 if (Packet
->OfferType
!= DHCP4_PACKET_TYPE_PXE10
) {
1610 return EFI_NOT_FOUND
;
1613 VendorOpt
= &Packet
->PxeVendorOption
;
1615 // According to the PXE specification 2.1, Table 2-1 PXE DHCP Options (Full
1616 // List), we must not consider a boot prompt or boot menu if all of the
1618 // - the PXE_DISCOVERY_CONTROL PXE tag is present inside the Vendor Options
1619 // (=43) DHCP tag, and
1620 // - the PXE_DISCOVERY_CONTROL PXE tag has bit 3 set, and
1621 // - a boot file name has been presented with DHCP option 67.
1623 if (IS_DISABLE_PROMPT_MENU (VendorOpt
->DiscoverCtrl
) &&
1624 Packet
->Dhcp4Option
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] != NULL
) {
1628 if (!IS_VALID_BOOT_PROMPT (VendorOpt
->BitMap
)) {
1632 Timeout
= VendorOpt
->MenuPrompt
->Timeout
;
1633 Prompt
= VendorOpt
->MenuPrompt
->Prompt
;
1634 PromptLen
= (UINT8
) (VendorOpt
->MenuPromptLen
- 1);
1640 if (Timeout
== 255) {
1644 Status
= gBS
->CreateEvent (
1652 if (EFI_ERROR (Status
)) {
1656 Status
= gBS
->SetTimer (
1659 Timeout
* TICKS_PER_SECOND
1662 if (EFI_ERROR (Status
)) {
1666 Status
= gBS
->CreateEvent (
1674 if (EFI_ERROR (Status
)) {
1678 Status
= gBS
->SetTimer (
1684 if (EFI_ERROR (Status
)) {
1688 SecCol
= gST
->ConOut
->Mode
->CursorColumn
;
1689 SecRow
= gST
->ConOut
->Mode
->CursorRow
;
1691 PxeBcDisplayBootItem (Prompt
, PromptLen
);
1693 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, SecCol
+ PromptLen
, SecRow
);
1694 AsciiPrint ("(%d) ", Timeout
--);
1696 while (EFI_ERROR (gBS
->CheckEvent (TimeoutEvent
))) {
1698 if (!EFI_ERROR (gBS
->CheckEvent (DescendEvent
))) {
1699 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, SecCol
+ PromptLen
, SecRow
);
1700 AsciiPrint ("(%d) ", Timeout
--);
1703 if (gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &InputKey
) == EFI_NOT_READY
) {
1705 gBS
->Stall (10 * TICKS_PER_MS
);
1709 if (InputKey
.ScanCode
== 0) {
1711 switch (InputKey
.UnicodeChar
) {
1713 Status
= EFI_ABORTED
;
1719 Status
= EFI_TIMEOUT
;
1727 switch (InputKey
.ScanCode
) {
1729 Status
= EFI_TIMEOUT
;
1733 Status
= EFI_ABORTED
;
1744 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0 , SecRow
+ 1);
1748 if (DescendEvent
!= NULL
) {
1749 gBS
->CloseEvent (DescendEvent
);
1752 if (TimeoutEvent
!= NULL
) {
1753 gBS
->CloseEvent (TimeoutEvent
);
1761 Select the boot menu.
1763 @param Private Pointer to PxeBc private data.
1764 @param Type The type of the menu.
1765 @param UseDefaultItem Use default item or not.
1767 @retval EFI_ABORTED User cancel operation.
1768 @retval EFI_SUCCESS Select the boot menu success.
1769 @retval EFI_NOT_READY Read the input key from the keybroad has not finish.
1773 PxeBcSelectBootMenu (
1774 IN PXEBC_PRIVATE_DATA
*Private
,
1776 IN BOOLEAN UseDefaultItem
1779 PXEBC_CACHED_DHCP4_PACKET
*Packet
;
1780 PXEBC_VENDOR_OPTION
*VendorOpt
;
1781 EFI_INPUT_KEY InputKey
;
1790 PXEBC_BOOT_MENU_ENTRY
*MenuItem
;
1791 PXEBC_BOOT_MENU_ENTRY
*MenuArray
[PXEBC_MAX_MENU_NUM
];
1798 if (Private
->PxeBc
.Mode
->ProxyOfferReceived
) {
1800 Packet
= &Private
->ProxyOffer
;
1803 Packet
= &Private
->Dhcp4Ack
;
1806 ASSERT (Packet
->OfferType
== DHCP4_PACKET_TYPE_PXE10
);
1808 VendorOpt
= &Packet
->PxeVendorOption
;
1810 if (!IS_VALID_BOOT_MENU (VendorOpt
->BitMap
)) {
1814 SetMem (Blank
, sizeof(Blank
), ' ');
1816 MenuSize
= VendorOpt
->BootMenuLen
;
1817 MenuItem
= VendorOpt
->BootMenu
;
1819 if (MenuSize
== 0) {
1820 return EFI_NOT_READY
;
1823 while (MenuSize
> 0) {
1824 MenuArray
[Index
++] = MenuItem
;
1825 MenuSize
= (UINT8
) (MenuSize
- (MenuItem
->DescLen
+ 3));
1826 MenuItem
= (PXEBC_BOOT_MENU_ENTRY
*) ((UINT8
*) MenuItem
+ MenuItem
->DescLen
+ 3);
1827 if (Index
>= PXEBC_MAX_MENU_NUM
) {
1832 if (UseDefaultItem
) {
1833 *Type
= MenuArray
[0]->Type
;
1834 *Type
= NTOHS (*Type
);
1840 for (Index
= 0; Index
< MenuNum
; Index
++) {
1841 PxeBcDisplayBootItem (MenuArray
[Index
]->DescStr
, MenuArray
[Index
]->DescLen
);
1844 TopRow
= gST
->ConOut
->Mode
->CursorRow
- MenuNum
;
1847 ASSERT (Select
< PXEBC_MAX_MENU_NUM
);
1849 // highlight selected row
1851 gST
->ConOut
->SetAttribute (gST
->ConOut
, EFI_TEXT_ATTR (EFI_BLACK
, EFI_LIGHTGRAY
));
1852 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, TopRow
+ Select
);
1853 Blank
[MenuArray
[Select
]->DescLen
] = 0;
1854 AsciiPrint ("%a\r", Blank
);
1855 PxeBcDisplayBootItem (MenuArray
[Select
]->DescStr
, MenuArray
[Select
]->DescLen
);
1856 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, TopRow
+ MenuNum
);
1857 LastSelect
= Select
;
1859 while (gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &InputKey
) == EFI_NOT_READY
) {
1860 gBS
->Stall (10 * TICKS_PER_MS
);
1863 if (InputKey
.ScanCode
!= 0) {
1864 switch (InputKey
.UnicodeChar
) {
1866 InputKey
.ScanCode
= SCAN_ESC
;
1869 case CTRL ('j'): /* linefeed */
1870 case CTRL ('m'): /* return */
1874 case CTRL ('i'): /* tab */
1878 InputKey
.ScanCode
= SCAN_DOWN
;
1881 case CTRL ('h'): /* backspace */
1884 InputKey
.ScanCode
= SCAN_UP
;
1888 InputKey
.ScanCode
= 0;
1892 switch (InputKey
.ScanCode
) {
1903 if (++Select
== MenuNum
) {
1914 case SCAN_PAGE_DOWN
:
1916 Select
= (UINT16
) (MenuNum
- 1);
1923 /* unhighlight last selected row */
1924 gST
->ConOut
->SetAttribute (gST
->ConOut
, EFI_TEXT_ATTR (EFI_LIGHTGRAY
, EFI_BLACK
));
1925 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, TopRow
+ LastSelect
);
1926 Blank
[MenuArray
[LastSelect
]->DescLen
] = 0;
1927 AsciiPrint ("%a\r", Blank
);
1928 PxeBcDisplayBootItem (MenuArray
[LastSelect
]->DescStr
, MenuArray
[LastSelect
]->DescLen
);
1929 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, TopRow
+ MenuNum
);
1932 ASSERT (Select
< PXEBC_MAX_MENU_NUM
);
1935 // Swap the byte order
1937 CopyMem (Type
, &MenuArray
[Select
]->Type
, sizeof (UINT16
));
1938 *Type
= NTOHS (*Type
);