2 Support for PxeBc dhcp functions.
4 Copyright (c) 2013, Red Hat, Inc.
5 Copyright (c) 2007 - 2014, 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 PXEBC_DHCP4_TAG_BOOTFILE_LEN
,
24 PXEBC_DHCP4_TAG_VENDOR
,
25 PXEBC_DHCP4_TAG_OVERLOAD
,
26 PXEBC_DHCP4_TAG_MSG_TYPE
,
27 PXEBC_DHCP4_TAG_SERVER_ID
,
28 PXEBC_DHCP4_TAG_CLASS_ID
,
29 PXEBC_DHCP4_TAG_BOOTFILE
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] = PXEBC_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 Options
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] = (EFI_DHCP4_PACKET_OPTION
*) (&Offer
->Dhcp4
.Header
.BootFileName
[0] -
232 OFFSET_OF (EFI_DHCP4_PACKET_OPTION
, Data
[0]));
237 // Determine offer type of the dhcp packet.
239 Option
= Options
[PXEBC_DHCP4_TAG_INDEX_MSG_TYPE
];
240 if ((Option
== NULL
) || (Option
->Data
[0] == 0)) {
242 // It's a bootp offer
244 Option
= CachedPacket
->Dhcp4Option
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
];
245 if (Option
== NULL
) {
247 // bootp offer without bootfilename, discard it.
252 OfferType
= DHCP4_PACKET_TYPE_BOOTP
;
256 if (IS_VALID_DISCOVER_VENDOR_OPTION (CachedPacket
->PxeVendorOption
.BitMap
)) {
258 // It's a pxe10 offer with PXEClient and discover vendor option.
260 OfferType
= DHCP4_PACKET_TYPE_PXE10
;
261 } else if (IS_VALID_MTFTP_VENDOR_OPTION (CachedPacket
->PxeVendorOption
.BitMap
)) {
263 // It's a wfm11a offer with PXEClient and mtftp vendor option, and
264 // return false since mtftp not supported currently.
269 // If the binl offer with only PXEClient.
271 OfferType
= (UINT8
) ((CachedPacket
->IsPxeOffer
) ? DHCP4_PACKET_TYPE_BINL
: DHCP4_PACKET_TYPE_DHCP_ONLY
);
275 CachedPacket
->OfferType
= OfferType
;
282 Offer dhcp service with a BINL dhcp offer.
284 @param Private Pointer to PxeBc private data.
285 @param Index Index of cached packets as complements of pxe mode data,
286 the index is maximum offer number.
288 @retval TRUE Offer the service successfully under priority BINL.
289 @retval FALSE Boot Service failed, parse cached dhcp packet failed or this
290 BINL ack cannot find options set or bootfile name specified.
295 IN PXEBC_PRIVATE_DATA
*Private
,
299 EFI_DHCP4_PACKET
*Offer
;
300 EFI_IP_ADDRESS ServerIp
;
302 PXEBC_CACHED_DHCP4_PACKET
*CachedPacket
;
303 EFI_DHCP4_PACKET
*Reply
;
305 ASSERT (Index
< PXEBC_MAX_OFFER_NUM
);
306 ASSERT (Private
->Dhcp4Offers
[Index
].OfferType
== DHCP4_PACKET_TYPE_BINL
);
308 Offer
= &Private
->Dhcp4Offers
[Index
].Packet
.Offer
;
311 // Use siaddr(next server) in DHCPOFFER packet header, if zero, use option 54(server identifier)
312 // in DHCPOFFER packet.
313 // (It does not comply with PXE Spec, Ver2.1)
315 if (EFI_IP4_EQUAL (&Offer
->Dhcp4
.Header
.ServerAddr
.Addr
, &mZeroIp4Addr
)) {
318 Private
->Dhcp4Offers
[Index
].Dhcp4Option
[PXEBC_DHCP4_TAG_INDEX_SERVER_ID
]->Data
,
319 sizeof (EFI_IPv4_ADDRESS
)
324 &Offer
->Dhcp4
.Header
.ServerAddr
,
325 sizeof (EFI_IPv4_ADDRESS
)
328 if (ServerIp
.Addr
[0] == 0) {
332 CachedPacket
= &Private
->ProxyOffer
;
333 Reply
= &CachedPacket
->Packet
.Offer
;
335 Status
= PxeBcDiscvBootService (
346 if (EFI_ERROR (Status
)) {
350 if (!PxeBcParseCachedDhcpPacket (CachedPacket
)) {
354 if ((CachedPacket
->OfferType
!= DHCP4_PACKET_TYPE_PXE10
) &&
355 (CachedPacket
->Dhcp4Option
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] == NULL
)) {
357 // This BINL ack doesn't have discovery options set or bootfile name
363 Private
->PxeBc
.Mode
->ProxyOfferReceived
= TRUE
;
364 CopyMem (&Private
->PxeBc
.Mode
->ProxyOffer
, &Reply
->Dhcp4
, Reply
->Length
);
371 Offer dhcp service for each proxy with a BINL dhcp offer.
373 @param Private Pointer to PxeBc private data
374 @param OfferIndex Pointer to the index of cached packets as complements of
375 pxe mode data, the index is maximum offer number.
377 @return If there is no service needed offer return FALSE, otherwise TRUE.
382 IN PXEBC_PRIVATE_DATA
*Private
,
383 OUT UINT32
*OfferIndex
388 for (Index
= 0; Index
< Private
->ProxyIndex
[DHCP4_PACKET_TYPE_BINL
]; Index
++) {
390 *OfferIndex
= Private
->BinlIndex
[Index
];
392 // Try this BINL proxy offer
394 if (PxeBcTryBinl (Private
, *OfferIndex
)) {
404 This function is to check the selected proxy offer (include BINL dhcp offer and
405 DHCP_ONLY offer ) and set the flag and copy the DHCP packets to the Pxe base code
408 @param Private Pointer to PxeBc private data.
410 @retval EFI_SUCCESS Operational successful.
411 @retval EFI_NO_RESPONSE Offer dhcp service failed.
415 PxeBcCheckSelectedOffer (
416 IN PXEBC_PRIVATE_DATA
*Private
419 PXEBC_CACHED_DHCP4_PACKET
*SelectedOffer
;
420 EFI_DHCP4_PACKET_OPTION
**Options
;
422 EFI_DHCP4_PACKET
*Offer
;
423 UINT32 ProxyOfferIndex
;
425 EFI_PXE_BASE_CODE_MODE
*Mode
;
426 EFI_DHCP4_PACKET
*Ack
;
428 ASSERT (Private
->SelectedOffer
!= 0);
430 Status
= EFI_SUCCESS
;
431 SelectedOffer
= &Private
->Dhcp4Offers
[Private
->SelectedOffer
- 1];
432 Options
= SelectedOffer
->Dhcp4Option
;
434 if (SelectedOffer
->OfferType
== DHCP4_PACKET_TYPE_BINL
) {
436 // The addresses are acquired from a BINL dhcp offer, try BINL to get
439 if (!PxeBcTryBinl (Private
, Private
->SelectedOffer
- 1)) {
440 Status
= EFI_NO_RESPONSE
;
442 } else if (SelectedOffer
->OfferType
== DHCP4_PACKET_TYPE_DHCP_ONLY
) {
444 // The selected offer to finish the D.O.R.A. is a DHCP only offer, we need
445 // try proxy offers if there are some, othewise the bootfile name must be
446 // set in this DHCP only offer.
448 if (Private
->GotProxyOffer
) {
450 // Get rid of the compiler warning.
453 if (Private
->SortOffers
) {
455 // The offers are sorted before selecting, the proxy offer type must be
456 // already determined.
458 ASSERT (Private
->ProxyIndex
[Private
->ProxyOfferType
] > 0);
460 if (Private
->ProxyOfferType
== DHCP4_PACKET_TYPE_BINL
) {
462 // We buffer all received BINL proxy offers, try them all one by one
464 if (!PxeBcTryBinlProxy (Private
, &ProxyOfferIndex
)) {
465 Status
= EFI_NO_RESPONSE
;
469 // For other types, only one proxy offer is buffered.
471 ProxyOfferIndex
= Private
->ProxyIndex
[Private
->ProxyOfferType
] - 1;
475 // The proxy offer type is not determined, choose proxy offer in the
478 Status
= EFI_NO_RESPONSE
;
480 ASSERT (Private
->NumOffers
< PXEBC_MAX_OFFER_NUM
);
481 for (Index
= 0; Index
< Private
->NumOffers
; Index
++) {
483 Offer
= &Private
->Dhcp4Offers
[Index
].Packet
.Offer
;
484 if (!IS_PROXY_DHCP_OFFER (Offer
)) {
486 // Skip non proxy dhcp offers.
491 if (Private
->Dhcp4Offers
[Index
].OfferType
== DHCP4_PACKET_TYPE_BINL
) {
495 if (!PxeBcTryBinl (Private
, Index
)) {
497 // Failed, skip to the next offer
503 Private
->ProxyOfferType
= Private
->Dhcp4Offers
[Index
].OfferType
;
504 ProxyOfferIndex
= Index
;
505 Status
= EFI_SUCCESS
;
510 if (!EFI_ERROR (Status
) && (Private
->ProxyOfferType
!= DHCP4_PACKET_TYPE_BINL
)) {
512 // Copy the proxy offer to Mode and set the flag
514 PxeBcCopyProxyOffer (Private
, ProxyOfferIndex
);
518 // No proxy offer is received, the bootfile name MUST be set.
520 ASSERT (Options
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] != NULL
);
524 if (!EFI_ERROR (Status
)) {
526 // Everything is OK, set the flag and copy the DHCP packets.
528 Mode
= Private
->PxeBc
.Mode
;
529 Offer
= &SelectedOffer
->Packet
.Offer
;
532 // The discover packet is already copied, just set flag here.
534 Mode
->DhcpDiscoverValid
= TRUE
;
536 Ack
= &Private
->Dhcp4Ack
.Packet
.Ack
;
537 if (SelectedOffer
->OfferType
== DHCP4_PACKET_TYPE_BOOTP
) {
539 // Other type of ACK is already cached. Bootp is special that we should
540 // use the bootp reply as the ACK and put it into the DHCP_ONLY buffer.
542 PxeBcCopyEfiDhcp4Packet (&Private
->Dhcp4Ack
.Packet
.Ack
, Offer
);
545 PxeBcParseCachedDhcpPacket (&Private
->Dhcp4Ack
);
547 Mode
->DhcpAckReceived
= TRUE
;
550 // Copy the dhcp ack.
552 CopyMem (&Mode
->DhcpAck
, &Ack
->Dhcp4
, Ack
->Length
);
560 Cache the Dhcp4 packet offer, Parse and validate each option of the packet.
562 @param Private Pointer to PxeBc private data.
563 @param RcvdOffer Pointer to the received Dhcp proxy offer packet.
567 PxeBcCacheDhcpOffer (
568 IN PXEBC_PRIVATE_DATA
*Private
,
569 IN EFI_DHCP4_PACKET
*RcvdOffer
572 PXEBC_CACHED_DHCP4_PACKET
*CachedOffer
;
573 EFI_DHCP4_PACKET
*Offer
;
576 CachedOffer
= &Private
->Dhcp4Offers
[Private
->NumOffers
];
577 Offer
= &CachedOffer
->Packet
.Offer
;
580 // Cache the orignal dhcp packet
582 PxeBcCopyEfiDhcp4Packet (Offer
, RcvdOffer
);
585 // Parse and validate the options (including dhcp option and vendor option)
587 if (!PxeBcParseCachedDhcpPacket (CachedOffer
)) {
591 OfferType
= CachedOffer
->OfferType
;
592 if (OfferType
>= DHCP4_PACKET_TYPE_MAX
) {
596 if (OfferType
== DHCP4_PACKET_TYPE_BOOTP
) {
598 if (Private
->BootpIndex
!= 0) {
600 // Only cache the first bootp offer, discard others.
605 // Take as a dhcp only offer, but record index specifically.
607 Private
->BootpIndex
= Private
->NumOffers
+ 1;
611 if (IS_PROXY_DHCP_OFFER (Offer
)) {
613 // It's a proxy dhcp offer with no your address, including pxe10, wfm11a or binl offer.
615 Private
->GotProxyOffer
= TRUE
;
617 if (OfferType
== DHCP4_PACKET_TYPE_BINL
) {
619 // Cache all binl offers.
621 Private
->BinlIndex
[Private
->ProxyIndex
[DHCP4_PACKET_TYPE_BINL
]] = Private
->NumOffers
;
622 Private
->ProxyIndex
[DHCP4_PACKET_TYPE_BINL
]++;
623 } else if (Private
->ProxyIndex
[OfferType
] != 0) {
625 // Only cache the first pxe10/wfm11a offers each, discard the others.
630 // Record index of the proxy dhcp offer with type other than binl.
632 Private
->ProxyIndex
[OfferType
] = Private
->NumOffers
+ 1;
636 // It's a dhcp offer with your address.
638 ASSERT (Private
->ServerCount
[OfferType
] < PXEBC_MAX_OFFER_NUM
);
639 Private
->OfferIndex
[OfferType
][Private
->ServerCount
[OfferType
]] = Private
->NumOffers
;
640 Private
->ServerCount
[OfferType
]++;
645 // Count the accepted offers.
647 Private
->NumOffers
++;
652 Select the specified proxy offer, such as BINL, DHCP_ONLY and so on.
653 If the proxy does not exist, try offers with bootfile.
655 @param Private Pointer to PxeBc private data.
660 IN PXEBC_PRIVATE_DATA
*Private
665 EFI_DHCP4_PACKET
*Offer
;
667 Private
->SelectedOffer
= 0;
669 if (Private
->SortOffers
) {
671 // Select offer according to the priority
673 if (Private
->ServerCount
[DHCP4_PACKET_TYPE_PXE10
] > 0) {
677 Private
->SelectedOffer
= Private
->OfferIndex
[DHCP4_PACKET_TYPE_PXE10
][0] + 1;
679 } else if (Private
->ServerCount
[DHCP4_PACKET_TYPE_WFM11A
] > 0) {
683 Private
->SelectedOffer
= Private
->OfferIndex
[DHCP4_PACKET_TYPE_WFM11A
][0] + 1;
685 } else if ((Private
->ProxyIndex
[DHCP4_PACKET_TYPE_PXE10
] > 0) &&
686 (Private
->ServerCount
[DHCP4_PACKET_TYPE_DHCP_ONLY
] > 0)
689 // DHCP only and proxy DHCP with PXE10
691 Private
->SelectedOffer
= Private
->OfferIndex
[DHCP4_PACKET_TYPE_DHCP_ONLY
][0] + 1;
692 Private
->ProxyOfferType
= DHCP4_PACKET_TYPE_PXE10
;
694 } else if ((Private
->ProxyIndex
[DHCP4_PACKET_TYPE_WFM11A
] > 0) &&
695 (Private
->ServerCount
[DHCP4_PACKET_TYPE_DHCP_ONLY
] > 0)
698 // DHCP only and proxy DHCP with WfM
700 Private
->SelectedOffer
= Private
->OfferIndex
[DHCP4_PACKET_TYPE_DHCP_ONLY
][0] + 1;
701 Private
->ProxyOfferType
= DHCP4_PACKET_TYPE_WFM11A
;
703 } else if (Private
->ServerCount
[DHCP4_PACKET_TYPE_BINL
] > 0) {
707 Private
->SelectedOffer
= Private
->OfferIndex
[DHCP4_PACKET_TYPE_BINL
][0] + 1;
709 } else if ((Private
->ProxyIndex
[DHCP4_PACKET_TYPE_BINL
] > 0) &&
710 (Private
->ServerCount
[DHCP4_PACKET_TYPE_DHCP_ONLY
] > 0)
713 // DHCP only and proxy DHCP with BINL
715 Private
->SelectedOffer
= Private
->OfferIndex
[DHCP4_PACKET_TYPE_DHCP_ONLY
][0] + 1;
716 Private
->ProxyOfferType
= DHCP4_PACKET_TYPE_BINL
;
720 // Try offers with bootfile
722 for (Index
= 0; Index
< Private
->ServerCount
[DHCP4_PACKET_TYPE_DHCP_ONLY
]; Index
++) {
724 // Select the first DHCP only offer with bootfile
726 OfferIndex
= Private
->OfferIndex
[DHCP4_PACKET_TYPE_DHCP_ONLY
][Index
];
727 if (Private
->Dhcp4Offers
[OfferIndex
].Dhcp4Option
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] != NULL
) {
728 Private
->SelectedOffer
= OfferIndex
+ 1;
733 if (Private
->SelectedOffer
== 0) {
735 // Select the Bootp reply with bootfile if any
737 Private
->SelectedOffer
= Private
->BootpIndex
;
742 // Try the offers in the received order.
744 for (Index
= 0; Index
< Private
->NumOffers
; Index
++) {
746 Offer
= &Private
->Dhcp4Offers
[Index
].Packet
.Offer
;
748 if (IS_PROXY_DHCP_OFFER (Offer
)) {
755 if ((Private
->Dhcp4Offers
[Index
].OfferType
== DHCP4_PACKET_TYPE_DHCP_ONLY
) &&
756 ((!Private
->GotProxyOffer
) && (Private
->Dhcp4Offers
[Index
].Dhcp4Option
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] == NULL
))) {
758 // DHCP only offer but no proxy offer received and no bootfile option in this offer
763 Private
->SelectedOffer
= Index
+ 1;
773 EFI_DHCP4_CALLBACK is provided by the consumer of the EFI DHCPv4 Protocol driver
774 to intercept events that occurred in the configuration process. This structure
775 provides advanced control of each state transition of the DHCP process. The
776 returned status code determines the behavior of the EFI DHCPv4 Protocol driver.
777 There are three possible returned values, which are described in the following
780 @param This Pointer to the EFI DHCPv4 Protocol instance that is used to
781 configure this callback function.
782 @param Context Pointer to the context that is initialized by
783 EFI_DHCP4_PROTOCOL.Configure().
784 @param CurrentState The current operational state of the EFI DHCPv4 Protocol
786 @param Dhcp4Event The event that occurs in the current state, which usually means a
788 @param Packet The DHCP packet that is going to be sent or already received.
789 @param NewPacket The packet that is used to replace the above Packet.
791 @retval EFI_SUCCESS Tells the EFI DHCPv4 Protocol driver to continue the DHCP process.
792 @retval EFI_NOT_READY Only used in the Dhcp4Selecting state. The EFI DHCPv4 Protocol
793 driver will continue to wait for more DHCPOFFER packets until the retry
795 @retval EFI_ABORTED Tells the EFI DHCPv4 Protocol driver to abort the current process and
796 return to the Dhcp4Init or Dhcp4InitReboot state.
802 IN EFI_DHCP4_PROTOCOL
* This
,
804 IN EFI_DHCP4_STATE CurrentState
,
805 IN EFI_DHCP4_EVENT Dhcp4Event
,
806 IN EFI_DHCP4_PACKET
* Packet OPTIONAL
,
807 OUT EFI_DHCP4_PACKET
**NewPacket OPTIONAL
810 PXEBC_PRIVATE_DATA
*Private
;
811 EFI_PXE_BASE_CODE_MODE
*Mode
;
812 EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL
*Callback
;
813 EFI_DHCP4_PACKET_OPTION
*MaxMsgSize
;
817 EFI_DHCP4_HEADER
*DhcpHeader
;
819 if ((Dhcp4Event
!= Dhcp4RcvdOffer
) &&
820 (Dhcp4Event
!= Dhcp4SelectOffer
) &&
821 (Dhcp4Event
!= Dhcp4SendDiscover
) &&
822 (Dhcp4Event
!= Dhcp4RcvdAck
) &&
823 (Dhcp4Event
!= Dhcp4SendRequest
)) {
827 Private
= (PXEBC_PRIVATE_DATA
*) Context
;
828 Mode
= Private
->PxeBc
.Mode
;
829 Callback
= Private
->PxeBcCallback
;
832 // Override the Maximum DHCP Message Size.
834 MaxMsgSize
= PxeBcParseExtendOptions (
835 Packet
->Dhcp4
.Option
,
836 GET_OPTION_BUFFER_LEN (Packet
),
837 PXEBC_DHCP4_TAG_MAXMSG
839 if (MaxMsgSize
!= NULL
) {
840 Value
= HTONS (PXEBC_DHCP4_MAX_PACKET_SIZE
);
841 CopyMem (MaxMsgSize
->Data
, &Value
, sizeof (Value
));
844 if ((Dhcp4Event
!= Dhcp4SelectOffer
) && (Callback
!= NULL
)) {
845 Received
= (BOOLEAN
) ((Dhcp4Event
== Dhcp4RcvdOffer
) || (Dhcp4Event
== Dhcp4RcvdAck
));
846 Status
= Callback
->Callback (
851 (EFI_PXE_BASE_CODE_PACKET
*) &Packet
->Dhcp4
853 if (Status
!= EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE
) {
858 Status
= EFI_SUCCESS
;
860 switch (Dhcp4Event
) {
862 case Dhcp4SendDiscover
:
863 case Dhcp4SendRequest
:
864 if (Mode
->SendGUID
) {
866 // send the system GUID instead of the MAC address as the hardware address
867 // in the DHCP packet header.
869 DhcpHeader
= &Packet
->Dhcp4
.Header
;
871 if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID
*) DhcpHeader
->ClientHwAddr
))) {
873 // GUID not yet set - send all 0xff's to show programable (via SetVariable)
874 // SetMem(DHCPV4_OPTIONS_BUFFER.DhcpPlatformId.Guid, sizeof(EFI_GUID), 0xff);
875 // GUID not yet set - send all 0's to show not programable
877 ZeroMem (DhcpHeader
->ClientHwAddr
, sizeof (EFI_GUID
));
880 DhcpHeader
->HwAddrLen
= (UINT8
) sizeof (EFI_GUID
);
883 if (Dhcp4Event
== Dhcp4SendDiscover
) {
885 // Cache the dhcp discover packet, of which some information will be used later.
887 CopyMem (Mode
->DhcpDiscover
.Raw
, &Packet
->Dhcp4
, Packet
->Length
);
893 Status
= EFI_NOT_READY
;
894 if (Private
->NumOffers
< PXEBC_MAX_OFFER_NUM
) {
896 // Cache the dhcp offers in Private->Dhcp4Offers[]
898 PxeBcCacheDhcpOffer (Private
, Packet
);
903 case Dhcp4SelectOffer
:
905 // Select an offer, if succeeded, Private->SelectedOffer points to
906 // the index of the selected one.
908 PxeBcSelectOffer (Private
);
910 if (Private
->SelectedOffer
== 0) {
911 Status
= EFI_ABORTED
;
913 *NewPacket
= &Private
->Dhcp4Offers
[Private
->SelectedOffer
- 1].Packet
.Offer
;
922 ASSERT (Private
->SelectedOffer
!= 0);
924 PxeBcCopyEfiDhcp4Packet (&Private
->Dhcp4Ack
.Packet
.Ack
, Packet
);
936 Initialize the DHCP options and build the option list.
938 @param Private Pointer to PxeBc private data.
939 @param OptList Pointer to a DHCP option list.
941 @param IsDhcpDiscover Discover dhcp option or not.
943 @return The index item number of the option list.
947 PxeBcBuildDhcpOptions (
948 IN PXEBC_PRIVATE_DATA
*Private
,
949 IN EFI_DHCP4_PACKET_OPTION
**OptList
,
950 IN BOOLEAN IsDhcpDiscover
954 PXEBC_DHCP4_OPTION_ENTRY OptEnt
;
958 OptList
[0] = (EFI_DHCP4_PACKET_OPTION
*) Private
->OptionBuffer
;
960 if (!IsDhcpDiscover
) {
962 // Append message type.
964 OptList
[Index
]->OpCode
= PXEBC_DHCP4_TAG_MSG_TYPE
;
965 OptList
[Index
]->Length
= 1;
966 OptEnt
.Mesg
= (PXEBC_DHCP4_OPTION_MESG
*) OptList
[Index
]->Data
;
967 OptEnt
.Mesg
->Type
= PXEBC_DHCP4_MSG_TYPE_REQUEST
;
969 OptList
[Index
] = GET_NEXT_DHCP_OPTION (OptList
[Index
- 1]);
972 // Append max message size.
974 OptList
[Index
]->OpCode
= PXEBC_DHCP4_TAG_MAXMSG
;
975 OptList
[Index
]->Length
= (UINT8
) sizeof (PXEBC_DHCP4_OPTION_MAX_MESG_SIZE
);
976 OptEnt
.MaxMesgSize
= (PXEBC_DHCP4_OPTION_MAX_MESG_SIZE
*) OptList
[Index
]->Data
;
977 Value
= NTOHS (PXEBC_DHCP4_MAX_PACKET_SIZE
);
978 CopyMem (&OptEnt
.MaxMesgSize
->Size
, &Value
, sizeof (UINT16
));
980 OptList
[Index
] = GET_NEXT_DHCP_OPTION (OptList
[Index
- 1]);
983 // Parameter request list option.
985 OptList
[Index
]->OpCode
= PXEBC_DHCP4_TAG_PARA_LIST
;
986 OptList
[Index
]->Length
= 35;
987 OptEnt
.Para
= (PXEBC_DHCP4_OPTION_PARA
*) OptList
[Index
]->Data
;
988 OptEnt
.Para
->ParaList
[0] = PXEBC_DHCP4_TAG_NETMASK
;
989 OptEnt
.Para
->ParaList
[1] = PXEBC_DHCP4_TAG_TIME_OFFSET
;
990 OptEnt
.Para
->ParaList
[2] = PXEBC_DHCP4_TAG_ROUTER
;
991 OptEnt
.Para
->ParaList
[3] = PXEBC_DHCP4_TAG_TIME_SERVER
;
992 OptEnt
.Para
->ParaList
[4] = PXEBC_DHCP4_TAG_NAME_SERVER
;
993 OptEnt
.Para
->ParaList
[5] = PXEBC_DHCP4_TAG_DNS_SERVER
;
994 OptEnt
.Para
->ParaList
[6] = PXEBC_DHCP4_TAG_HOSTNAME
;
995 OptEnt
.Para
->ParaList
[7] = PXEBC_DHCP4_TAG_BOOTFILE_LEN
;
996 OptEnt
.Para
->ParaList
[8] = PXEBC_DHCP4_TAG_DOMAINNAME
;
997 OptEnt
.Para
->ParaList
[9] = PXEBC_DHCP4_TAG_ROOTPATH
;
998 OptEnt
.Para
->ParaList
[10] = PXEBC_DHCP4_TAG_EXTEND_PATH
;
999 OptEnt
.Para
->ParaList
[11] = PXEBC_DHCP4_TAG_EMTU
;
1000 OptEnt
.Para
->ParaList
[12] = PXEBC_DHCP4_TAG_TTL
;
1001 OptEnt
.Para
->ParaList
[13] = PXEBC_DHCP4_TAG_BROADCAST
;
1002 OptEnt
.Para
->ParaList
[14] = PXEBC_DHCP4_TAG_NIS_DOMAIN
;
1003 OptEnt
.Para
->ParaList
[15] = PXEBC_DHCP4_TAG_NIS_SERVER
;
1004 OptEnt
.Para
->ParaList
[16] = PXEBC_DHCP4_TAG_NTP_SERVER
;
1005 OptEnt
.Para
->ParaList
[17] = PXEBC_DHCP4_TAG_VENDOR
;
1006 OptEnt
.Para
->ParaList
[18] = PXEBC_DHCP4_TAG_REQUEST_IP
;
1007 OptEnt
.Para
->ParaList
[19] = PXEBC_DHCP4_TAG_LEASE
;
1008 OptEnt
.Para
->ParaList
[20] = PXEBC_DHCP4_TAG_SERVER_ID
;
1009 OptEnt
.Para
->ParaList
[21] = PXEBC_DHCP4_TAG_T1
;
1010 OptEnt
.Para
->ParaList
[22] = PXEBC_DHCP4_TAG_T2
;
1011 OptEnt
.Para
->ParaList
[23] = PXEBC_DHCP4_TAG_CLASS_ID
;
1012 OptEnt
.Para
->ParaList
[24] = PXEBC_DHCP4_TAG_TFTP
;
1013 OptEnt
.Para
->ParaList
[25] = PXEBC_DHCP4_TAG_BOOTFILE
;
1014 OptEnt
.Para
->ParaList
[26] = PXEBC_PXE_DHCP4_TAG_UUID
;
1015 OptEnt
.Para
->ParaList
[27] = 0x80;
1016 OptEnt
.Para
->ParaList
[28] = 0x81;
1017 OptEnt
.Para
->ParaList
[29] = 0x82;
1018 OptEnt
.Para
->ParaList
[30] = 0x83;
1019 OptEnt
.Para
->ParaList
[31] = 0x84;
1020 OptEnt
.Para
->ParaList
[32] = 0x85;
1021 OptEnt
.Para
->ParaList
[33] = 0x86;
1022 OptEnt
.Para
->ParaList
[34] = 0x87;
1024 OptList
[Index
] = GET_NEXT_DHCP_OPTION (OptList
[Index
- 1]);
1027 // Append UUID/Guid-based client identifier option
1029 OptList
[Index
]->OpCode
= PXEBC_PXE_DHCP4_TAG_UUID
;
1030 OptList
[Index
]->Length
= (UINT8
) sizeof (PXEBC_DHCP4_OPTION_UUID
);
1031 OptEnt
.Uuid
= (PXEBC_DHCP4_OPTION_UUID
*) OptList
[Index
]->Data
;
1032 OptEnt
.Uuid
->Type
= 0;
1034 OptList
[Index
] = GET_NEXT_DHCP_OPTION (OptList
[Index
- 1]);
1036 if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID
*) OptEnt
.Uuid
->Guid
))) {
1038 // GUID not yet set - send all 0xff's to show programable (via SetVariable)
1039 // SetMem(DHCPV4_OPTIONS_BUFFER.DhcpPlatformId.Guid, sizeof(EFI_GUID), 0xff);
1040 // GUID not yet set - send all 0's to show not programable
1042 ZeroMem (OptEnt
.Uuid
->Guid
, sizeof (EFI_GUID
));
1046 // Append client network device interface option
1048 OptList
[Index
]->OpCode
= PXEBC_PXE_DHCP4_TAG_UNDI
;
1049 OptList
[Index
]->Length
= (UINT8
) sizeof (PXEBC_DHCP4_OPTION_UNDI
);
1050 OptEnt
.Undi
= (PXEBC_DHCP4_OPTION_UNDI
*) OptList
[Index
]->Data
;
1051 if (Private
->Nii
!= NULL
) {
1052 OptEnt
.Undi
->Type
= Private
->Nii
->Type
;
1053 OptEnt
.Undi
->MajorVer
= Private
->Nii
->MajorVer
;
1054 OptEnt
.Undi
->MinorVer
= Private
->Nii
->MinorVer
;
1056 OptEnt
.Undi
->Type
= DEFAULT_UNDI_TYPE
;
1057 OptEnt
.Undi
->MajorVer
= DEFAULT_UNDI_MAJOR
;
1058 OptEnt
.Undi
->MinorVer
= DEFAULT_UNDI_MINOR
;
1062 OptList
[Index
] = GET_NEXT_DHCP_OPTION (OptList
[Index
- 1]);
1065 // Append client system architecture option
1067 OptList
[Index
]->OpCode
= PXEBC_PXE_DHCP4_TAG_ARCH
;
1068 OptList
[Index
]->Length
= (UINT8
) sizeof (PXEBC_DHCP4_OPTION_ARCH
);
1069 OptEnt
.Arch
= (PXEBC_DHCP4_OPTION_ARCH
*) OptList
[Index
]->Data
;
1070 Value
= HTONS (EFI_PXE_CLIENT_SYSTEM_ARCHITECTURE
);
1071 CopyMem (&OptEnt
.Arch
->Type
, &Value
, sizeof (UINT16
));
1073 OptList
[Index
] = GET_NEXT_DHCP_OPTION (OptList
[Index
- 1]);
1076 // Append client system architecture option
1078 OptList
[Index
]->OpCode
= PXEBC_DHCP4_TAG_CLASS_ID
;
1079 OptList
[Index
]->Length
= (UINT8
) sizeof (PXEBC_DHCP4_OPTION_CLID
);
1080 OptEnt
.Clid
= (PXEBC_DHCP4_OPTION_CLID
*) OptList
[Index
]->Data
;
1081 CopyMem (OptEnt
.Clid
, DEFAULT_CLASS_ID_DATA
, sizeof (PXEBC_DHCP4_OPTION_CLID
));
1082 CvtNum (EFI_PXE_CLIENT_SYSTEM_ARCHITECTURE
, OptEnt
.Clid
->ArchitectureType
, sizeof (OptEnt
.Clid
->ArchitectureType
));
1084 if (Private
->Nii
!= NULL
) {
1086 // If NII protocol exists, update DHCP option data
1088 CopyMem (OptEnt
.Clid
->InterfaceName
, Private
->Nii
->StringId
, sizeof (OptEnt
.Clid
->InterfaceName
));
1089 CvtNum (Private
->Nii
->MajorVer
, OptEnt
.Clid
->UndiMajor
, sizeof (OptEnt
.Clid
->UndiMajor
));
1090 CvtNum (Private
->Nii
->MinorVer
, OptEnt
.Clid
->UndiMinor
, sizeof (OptEnt
.Clid
->UndiMinor
));
1100 Discover the boot of service and initialize the vendor option if exists.
1102 @param Private Pointer to PxeBc private data.
1103 @param Type PxeBc option boot item type
1104 @param Layer PxeBc option boot item layer
1105 @param UseBis Use BIS or not
1106 @param DestIp Ip address for server
1107 @param IpCount The total count of the server ip address
1108 @param SrvList Server list
1109 @param IsDiscv Discover the vendor or not
1110 @param Reply The dhcp4 packet of Pxe reply
1112 @retval EFI_SUCCESS Operation succeeds.
1113 @retval EFI_OUT_OF_RESOURCES Allocate memory pool failed.
1114 @retval EFI_NOT_FOUND There is no vendor option exists.
1115 @retval EFI_TIMEOUT Send Pxe Discover time out.
1119 PxeBcDiscvBootService (
1120 IN PXEBC_PRIVATE_DATA
* Private
,
1124 IN EFI_IP_ADDRESS
* DestIp
,
1126 IN EFI_PXE_BASE_CODE_SRVLIST
* SrvList
,
1128 OUT EFI_DHCP4_PACKET
* Reply OPTIONAL
1131 EFI_PXE_BASE_CODE_UDP_PORT Sport
;
1132 EFI_PXE_BASE_CODE_MODE
*Mode
;
1133 EFI_DHCP4_PROTOCOL
*Dhcp4
;
1134 EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN Token
;
1140 EFI_DHCP4_LISTEN_POINT ListenPoint
;
1141 EFI_DHCP4_PACKET
*Response
;
1142 EFI_DHCP4_PACKET_OPTION
*OptList
[PXEBC_DHCP4_MAX_OPTION_NUM
];
1144 EFI_DHCP4_PACKET_OPTION
*PxeOpt
;
1145 PXEBC_OPTION_BOOT_ITEM
*PxeBootItem
;
1147 EFI_DHCP4_HEADER
*DhcpHeader
;
1150 Mode
= Private
->PxeBc
.Mode
;
1151 Dhcp4
= Private
->Dhcp4
;
1152 Status
= EFI_SUCCESS
;
1154 ZeroMem (&Token
, sizeof (EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN
));
1156 if (DestIp
== NULL
) {
1157 Sport
= PXEBC_DHCP4_S_PORT
;
1160 Sport
= PXEBC_BS_DISCOVER_PORT
;
1164 if (!UseBis
&& Layer
!= NULL
) {
1165 *Layer
&= EFI_PXE_BASE_CODE_BOOT_LAYER_MASK
;
1168 OptCount
= PxeBcBuildDhcpOptions (Private
, OptList
, FALSE
);
1171 ASSERT (Layer
!= NULL
);
1173 // Add vendor option of PXE_BOOT_ITEM
1175 VendorOptLen
= (UINT8
) ((sizeof (EFI_DHCP4_PACKET_OPTION
) - 1) * 2 + sizeof (PXEBC_OPTION_BOOT_ITEM
) + 1);
1176 OptList
[OptCount
] = AllocatePool (VendorOptLen
);
1177 if (OptList
[OptCount
] == NULL
) {
1178 return EFI_OUT_OF_RESOURCES
;
1181 OptList
[OptCount
]->OpCode
= PXEBC_DHCP4_TAG_VENDOR
;
1182 OptList
[OptCount
]->Length
= (UINT8
) (VendorOptLen
- 2);
1183 PxeOpt
= (EFI_DHCP4_PACKET_OPTION
*) OptList
[OptCount
]->Data
;
1184 PxeOpt
->OpCode
= PXEBC_VENDOR_TAG_BOOT_ITEM
;
1185 PxeOpt
->Length
= (UINT8
) sizeof (PXEBC_OPTION_BOOT_ITEM
);
1186 PxeBootItem
= (PXEBC_OPTION_BOOT_ITEM
*) PxeOpt
->Data
;
1187 PxeBootItem
->Type
= HTONS (Type
);
1188 PxeBootItem
->Layer
= HTONS (*Layer
);
1189 PxeOpt
->Data
[PxeOpt
->Length
] = PXEBC_DHCP4_TAG_EOP
;
1194 Status
= Dhcp4
->Build (Dhcp4
, &Private
->SeedPacket
, 0, NULL
, OptCount
, OptList
, &Token
.Packet
);
1197 FreePool (OptList
[OptCount
- 1]);
1200 if (EFI_ERROR (Status
)) {
1204 DhcpHeader
= &Token
.Packet
->Dhcp4
.Header
;
1205 if (Mode
->SendGUID
) {
1206 if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID
*) DhcpHeader
->ClientHwAddr
))) {
1208 // GUID not yet set - send all 0's to show not programable
1210 ZeroMem (DhcpHeader
->ClientHwAddr
, sizeof (EFI_GUID
));
1213 DhcpHeader
->HwAddrLen
= (UINT8
) sizeof (EFI_GUID
);
1216 Xid
= NET_RANDOM (NetRandomInitSeed ());
1217 Token
.Packet
->Dhcp4
.Header
.Xid
= HTONL(Xid
);
1218 Token
.Packet
->Dhcp4
.Header
.Reserved
= HTONS((UINT16
) ((IsBCast
) ? 0x8000 : 0));
1219 CopyMem (&Token
.Packet
->Dhcp4
.Header
.ClientAddr
, &Private
->StationIp
, sizeof (EFI_IPv4_ADDRESS
));
1221 Token
.RemotePort
= Sport
;
1224 SetMem (&Token
.RemoteAddress
, sizeof (EFI_IPv4_ADDRESS
), 0xff);
1226 CopyMem (&Token
.RemoteAddress
, DestIp
, sizeof (EFI_IPv4_ADDRESS
));
1229 CopyMem (&Token
.GatewayAddress
, &Private
->GatewayIp
, sizeof (EFI_IPv4_ADDRESS
));
1232 Token
.ListenPointCount
= 1;
1233 Token
.ListenPoints
= &ListenPoint
;
1234 Token
.ListenPoints
[0].ListenPort
= PXEBC_BS_DISCOVER_PORT
;
1235 CopyMem (&Token
.ListenPoints
[0].ListenAddress
, &Private
->StationIp
, sizeof(EFI_IPv4_ADDRESS
));
1236 CopyMem (&Token
.ListenPoints
[0].SubnetMask
, &Private
->SubnetMask
, sizeof(EFI_IPv4_ADDRESS
));
1239 // Send Pxe Discover
1241 for (TryIndex
= 1; TryIndex
<= PXEBC_BOOT_REQUEST_RETRIES
; TryIndex
++) {
1243 Token
.TimeoutValue
= (UINT16
) (PXEBC_BOOT_REQUEST_TIMEOUT
* TryIndex
);
1244 Token
.Packet
->Dhcp4
.Header
.Seconds
= (UINT16
) (PXEBC_BOOT_REQUEST_TIMEOUT
* (TryIndex
- 1));
1246 Status
= Dhcp4
->TransmitReceive (Dhcp4
, &Token
);
1248 if (Token
.Status
!= EFI_TIMEOUT
) {
1253 if (TryIndex
> PXEBC_BOOT_REQUEST_RETRIES
) {
1255 // No server response our PXE request
1257 Status
= EFI_TIMEOUT
;
1260 if (!EFI_ERROR (Status
)) {
1266 Response
= Token
.ResponseList
;
1268 while (RepIndex
< Token
.ResponseCount
) {
1270 while (SrvIndex
< IpCount
) {
1272 if (SrvList
[SrvIndex
].AcceptAnyResponse
) {
1276 if ((SrvList
[SrvIndex
].Type
== Type
) && EFI_IP4_EQUAL (&(Response
->Dhcp4
.Header
.ServerAddr
), &(Private
->ServerIp
))) {
1283 if ((IpCount
!= SrvIndex
) || (IpCount
== 0)) {
1290 Response
= (EFI_DHCP4_PACKET
*) ((UINT8
*) Response
+ Response
->Size
);
1293 if (RepIndex
< Token
.ResponseCount
) {
1295 if (Reply
!= NULL
) {
1296 PxeBcCopyEfiDhcp4Packet (Reply
, Response
);
1300 CopyMem (&(Mode
->PxeDiscover
), &(Token
.Packet
->Dhcp4
), Token
.Packet
->Length
);
1301 Mode
->PxeDiscoverValid
= TRUE
;
1303 CopyMem (Mode
->PxeReply
.Raw
, &Response
->Dhcp4
, Response
->Length
);
1304 Mode
->PxeReplyReceived
= TRUE
;
1307 Status
= EFI_NOT_FOUND
;
1311 // free the responselist
1313 if (Token
.ResponseList
!= NULL
) {
1314 FreePool (Token
.ResponseList
);
1318 // Free the dhcp packet
1320 FreePool (Token
.Packet
);
1327 Parse interested dhcp options.
1329 @param Buffer Pointer to the dhcp options packet.
1330 @param Length The length of the dhcp options.
1331 @param OptTag The option OpCode.
1333 @return NULL if the buffer length is 0 and OpCode is not
1334 PXEBC_DHCP4_TAG_EOP, or the pointer to the buffer.
1337 EFI_DHCP4_PACKET_OPTION
*
1338 PxeBcParseExtendOptions (
1344 EFI_DHCP4_PACKET_OPTION
*Option
;
1347 Option
= (EFI_DHCP4_PACKET_OPTION
*) Buffer
;
1350 while (Offset
< Length
&& Option
->OpCode
!= PXEBC_DHCP4_TAG_EOP
) {
1352 if (Option
->OpCode
== OptTag
) {
1357 if (Option
->OpCode
== PXEBC_DHCP4_TAG_PAD
) {
1360 Offset
+= Option
->Length
+ 2;
1363 Option
= (EFI_DHCP4_PACKET_OPTION
*) (Buffer
+ Offset
);
1371 This function is to parse and check vendor options.
1373 @param Dhcp4Option Pointer to dhcp options
1374 @param VendorOption Pointer to vendor options
1376 @return TRUE if valid for vendor options, or FALSE.
1380 PxeBcParseVendorOptions (
1381 IN EFI_DHCP4_PACKET_OPTION
*Dhcp4Option
,
1382 IN PXEBC_VENDOR_OPTION
*VendorOption
1386 UINT8 VendorOptionLen
;
1387 EFI_DHCP4_PACKET_OPTION
*PxeOption
;
1390 BitMap
= VendorOption
->BitMap
;
1391 VendorOptionLen
= Dhcp4Option
->Length
;
1392 PxeOption
= (EFI_DHCP4_PACKET_OPTION
*) &Dhcp4Option
->Data
[0];
1395 while ((Offset
< VendorOptionLen
) && (PxeOption
->OpCode
!= PXEBC_DHCP4_TAG_EOP
)) {
1397 // Parse every Vendor Option and set its BitMap
1399 switch (PxeOption
->OpCode
) {
1401 case PXEBC_VENDOR_TAG_MTFTP_IP
:
1403 CopyMem (&VendorOption
->MtftpIp
, PxeOption
->Data
, sizeof (EFI_IPv4_ADDRESS
));
1406 case PXEBC_VENDOR_TAG_MTFTP_CPORT
:
1408 CopyMem (&VendorOption
->MtftpCPort
, PxeOption
->Data
, sizeof (VendorOption
->MtftpCPort
));
1411 case PXEBC_VENDOR_TAG_MTFTP_SPORT
:
1413 CopyMem (&VendorOption
->MtftpSPort
, PxeOption
->Data
, sizeof (VendorOption
->MtftpSPort
));
1416 case PXEBC_VENDOR_TAG_MTFTP_TIMEOUT
:
1418 VendorOption
->MtftpTimeout
= *PxeOption
->Data
;
1421 case PXEBC_VENDOR_TAG_MTFTP_DELAY
:
1423 VendorOption
->MtftpDelay
= *PxeOption
->Data
;
1426 case PXEBC_VENDOR_TAG_DISCOVER_CTRL
:
1428 VendorOption
->DiscoverCtrl
= *PxeOption
->Data
;
1431 case PXEBC_VENDOR_TAG_DISCOVER_MCAST
:
1433 CopyMem (&VendorOption
->DiscoverMcastIp
, PxeOption
->Data
, sizeof (EFI_IPv4_ADDRESS
));
1436 case PXEBC_VENDOR_TAG_BOOT_SERVERS
:
1438 VendorOption
->BootSvrLen
= PxeOption
->Length
;
1439 VendorOption
->BootSvr
= (PXEBC_BOOT_SVR_ENTRY
*) PxeOption
->Data
;
1442 case PXEBC_VENDOR_TAG_BOOT_MENU
:
1444 VendorOption
->BootMenuLen
= PxeOption
->Length
;
1445 VendorOption
->BootMenu
= (PXEBC_BOOT_MENU_ENTRY
*) PxeOption
->Data
;
1448 case PXEBC_VENDOR_TAG_MENU_PROMPT
:
1450 VendorOption
->MenuPromptLen
= PxeOption
->Length
;
1451 VendorOption
->MenuPrompt
= (PXEBC_MENU_PROMPT
*) PxeOption
->Data
;
1454 case PXEBC_VENDOR_TAG_MCAST_ALLOC
:
1456 CopyMem (&VendorOption
->McastIpBase
, PxeOption
->Data
, sizeof (EFI_IPv4_ADDRESS
));
1457 CopyMem (&VendorOption
->McastIpBlock
, PxeOption
->Data
+ 4, sizeof (VendorOption
->McastIpBlock
));
1458 CopyMem (&VendorOption
->McastIpRange
, PxeOption
->Data
+ 6, sizeof (VendorOption
->McastIpRange
));
1461 case PXEBC_VENDOR_TAG_CREDENTIAL_TYPES
:
1463 VendorOption
->CredTypeLen
= PxeOption
->Length
;
1464 VendorOption
->CredType
= (UINT32
*) PxeOption
->Data
;
1467 case PXEBC_VENDOR_TAG_BOOT_ITEM
:
1469 CopyMem (&VendorOption
->BootSrvType
, PxeOption
->Data
, sizeof (VendorOption
->BootSrvType
));
1470 CopyMem (&VendorOption
->BootSrvLayer
, PxeOption
->Data
+ 2, sizeof (VendorOption
->BootSrvLayer
));
1474 SET_VENDOR_OPTION_BIT_MAP (BitMap
, PxeOption
->OpCode
);
1476 if (PxeOption
->OpCode
== PXEBC_DHCP4_TAG_PAD
) {
1479 Offset
= (UINT8
) (Offset
+ PxeOption
->Length
+ 2);
1482 PxeOption
= (EFI_DHCP4_PACKET_OPTION
*) (Dhcp4Option
->Data
+ Offset
);
1486 // FixMe, return falas if invalid of any vendor option
1494 This function display boot item detail.
1496 If the length of the boot item string over 70 Char, just display 70 Char.
1498 @param Str Pointer to a string (boot item string).
1499 @param Len The length of string.
1503 PxeBcDisplayBootItem (
1510 Len
= (UINT8
) MIN (70, Len
);
1513 AsciiPrint ("%a \n", Str
);
1519 Choose the boot prompt.
1521 @param Private Pointer to PxeBc private data.
1523 @retval EFI_SUCCESS Select boot prompt done.
1524 @retval EFI_TIMEOUT Select boot prompt time out.
1525 @retval EFI_NOT_FOUND The proxy offer is not Pxe10.
1526 @retval EFI_ABORTED User cancel the operation.
1527 @retval EFI_NOT_READY Read the input key from the keybroad has not finish.
1531 PxeBcSelectBootPrompt (
1532 IN PXEBC_PRIVATE_DATA
*Private
1535 PXEBC_CACHED_DHCP4_PACKET
*Packet
;
1536 PXEBC_VENDOR_OPTION
*VendorOpt
;
1537 EFI_EVENT TimeoutEvent
;
1538 EFI_EVENT DescendEvent
;
1539 EFI_INPUT_KEY InputKey
;
1547 TimeoutEvent
= NULL
;
1548 DescendEvent
= NULL
;
1550 if (Private
->PxeBc
.Mode
->ProxyOfferReceived
) {
1552 Packet
= &Private
->ProxyOffer
;
1555 Packet
= &Private
->Dhcp4Ack
;
1558 if (Packet
->OfferType
!= DHCP4_PACKET_TYPE_PXE10
) {
1559 return EFI_NOT_FOUND
;
1562 VendorOpt
= &Packet
->PxeVendorOption
;
1564 // According to the PXE specification 2.1, Table 2-1 PXE DHCP Options (Full
1565 // List), we must not consider a boot prompt or boot menu if all of the
1567 // - the PXE_DISCOVERY_CONTROL PXE tag is present inside the Vendor Options
1568 // (=43) DHCP tag, and
1569 // - the PXE_DISCOVERY_CONTROL PXE tag has bit 3 set, and
1570 // - a boot file name has been presented with DHCP option 67.
1572 if (IS_DISABLE_PROMPT_MENU (VendorOpt
->DiscoverCtrl
) &&
1573 Packet
->Dhcp4Option
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] != NULL
) {
1577 if (!IS_VALID_BOOT_PROMPT (VendorOpt
->BitMap
)) {
1581 Timeout
= VendorOpt
->MenuPrompt
->Timeout
;
1582 Prompt
= VendorOpt
->MenuPrompt
->Prompt
;
1583 PromptLen
= (UINT8
) (VendorOpt
->MenuPromptLen
- 1);
1589 if (Timeout
== 255) {
1593 Status
= gBS
->CreateEvent (
1601 if (EFI_ERROR (Status
)) {
1605 Status
= gBS
->SetTimer (
1608 Timeout
* TICKS_PER_SECOND
1611 if (EFI_ERROR (Status
)) {
1615 Status
= gBS
->CreateEvent (
1623 if (EFI_ERROR (Status
)) {
1627 Status
= gBS
->SetTimer (
1633 if (EFI_ERROR (Status
)) {
1637 SecCol
= gST
->ConOut
->Mode
->CursorColumn
;
1638 SecRow
= gST
->ConOut
->Mode
->CursorRow
;
1640 PxeBcDisplayBootItem (Prompt
, PromptLen
);
1642 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, SecCol
+ PromptLen
, SecRow
);
1643 AsciiPrint ("(%d) ", Timeout
--);
1645 while (EFI_ERROR (gBS
->CheckEvent (TimeoutEvent
))) {
1647 if (!EFI_ERROR (gBS
->CheckEvent (DescendEvent
))) {
1648 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, SecCol
+ PromptLen
, SecRow
);
1649 AsciiPrint ("(%d) ", Timeout
--);
1652 if (gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &InputKey
) == EFI_NOT_READY
) {
1654 gBS
->Stall (10 * TICKS_PER_MS
);
1658 if (InputKey
.ScanCode
== 0) {
1660 switch (InputKey
.UnicodeChar
) {
1662 Status
= EFI_ABORTED
;
1668 Status
= EFI_TIMEOUT
;
1676 switch (InputKey
.ScanCode
) {
1678 Status
= EFI_TIMEOUT
;
1682 Status
= EFI_ABORTED
;
1693 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0 , SecRow
+ 1);
1697 if (DescendEvent
!= NULL
) {
1698 gBS
->CloseEvent (DescendEvent
);
1701 if (TimeoutEvent
!= NULL
) {
1702 gBS
->CloseEvent (TimeoutEvent
);
1710 Select the boot menu.
1712 @param Private Pointer to PxeBc private data.
1713 @param Type The type of the menu.
1714 @param UseDefaultItem Use default item or not.
1716 @retval EFI_ABORTED User cancel operation.
1717 @retval EFI_SUCCESS Select the boot menu success.
1718 @retval EFI_NOT_READY Read the input key from the keybroad has not finish.
1722 PxeBcSelectBootMenu (
1723 IN PXEBC_PRIVATE_DATA
*Private
,
1725 IN BOOLEAN UseDefaultItem
1728 PXEBC_CACHED_DHCP4_PACKET
*Packet
;
1729 PXEBC_VENDOR_OPTION
*VendorOpt
;
1730 EFI_INPUT_KEY InputKey
;
1739 PXEBC_BOOT_MENU_ENTRY
*MenuItem
;
1740 PXEBC_BOOT_MENU_ENTRY
*MenuArray
[PXEBC_MAX_MENU_NUM
];
1747 if (Private
->PxeBc
.Mode
->ProxyOfferReceived
) {
1749 Packet
= &Private
->ProxyOffer
;
1752 Packet
= &Private
->Dhcp4Ack
;
1755 ASSERT (Packet
->OfferType
== DHCP4_PACKET_TYPE_PXE10
);
1757 VendorOpt
= &Packet
->PxeVendorOption
;
1759 if (!IS_VALID_BOOT_MENU (VendorOpt
->BitMap
)) {
1763 SetMem (Blank
, sizeof(Blank
), ' ');
1765 MenuSize
= VendorOpt
->BootMenuLen
;
1766 MenuItem
= VendorOpt
->BootMenu
;
1768 if (MenuSize
== 0) {
1769 return EFI_NOT_READY
;
1772 while (MenuSize
> 0) {
1773 MenuArray
[Index
++] = MenuItem
;
1774 MenuSize
= (UINT8
) (MenuSize
- (MenuItem
->DescLen
+ 3));
1775 MenuItem
= (PXEBC_BOOT_MENU_ENTRY
*) ((UINT8
*) MenuItem
+ MenuItem
->DescLen
+ 3);
1776 if (Index
>= PXEBC_MAX_MENU_NUM
) {
1781 if (UseDefaultItem
) {
1782 *Type
= MenuArray
[0]->Type
;
1783 *Type
= NTOHS (*Type
);
1789 for (Index
= 0; Index
< MenuNum
; Index
++) {
1790 PxeBcDisplayBootItem (MenuArray
[Index
]->DescStr
, MenuArray
[Index
]->DescLen
);
1793 TopRow
= gST
->ConOut
->Mode
->CursorRow
- MenuNum
;
1796 ASSERT (Select
< PXEBC_MAX_MENU_NUM
);
1798 // highlight selected row
1800 gST
->ConOut
->SetAttribute (gST
->ConOut
, EFI_TEXT_ATTR (EFI_BLACK
, EFI_LIGHTGRAY
));
1801 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, TopRow
+ Select
);
1802 Blank
[MenuArray
[Select
]->DescLen
] = 0;
1803 AsciiPrint ("%a\r", Blank
);
1804 PxeBcDisplayBootItem (MenuArray
[Select
]->DescStr
, MenuArray
[Select
]->DescLen
);
1805 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, TopRow
+ MenuNum
);
1806 LastSelect
= Select
;
1808 while (gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &InputKey
) == EFI_NOT_READY
) {
1809 gBS
->Stall (10 * TICKS_PER_MS
);
1812 if (InputKey
.ScanCode
!= 0) {
1813 switch (InputKey
.UnicodeChar
) {
1815 InputKey
.ScanCode
= SCAN_ESC
;
1818 case CTRL ('j'): /* linefeed */
1819 case CTRL ('m'): /* return */
1823 case CTRL ('i'): /* tab */
1827 InputKey
.ScanCode
= SCAN_DOWN
;
1830 case CTRL ('h'): /* backspace */
1833 InputKey
.ScanCode
= SCAN_UP
;
1837 InputKey
.ScanCode
= 0;
1841 switch (InputKey
.ScanCode
) {
1852 if (++Select
== MenuNum
) {
1863 case SCAN_PAGE_DOWN
:
1865 Select
= (UINT16
) (MenuNum
- 1);
1872 /* unhighlight last selected row */
1873 gST
->ConOut
->SetAttribute (gST
->ConOut
, EFI_TEXT_ATTR (EFI_LIGHTGRAY
, EFI_BLACK
));
1874 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, TopRow
+ LastSelect
);
1875 Blank
[MenuArray
[LastSelect
]->DescLen
] = 0;
1876 AsciiPrint ("%a\r", Blank
);
1877 PxeBcDisplayBootItem (MenuArray
[LastSelect
]->DescStr
, MenuArray
[LastSelect
]->DescLen
);
1878 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, TopRow
+ MenuNum
);
1881 ASSERT (Select
< PXEBC_MAX_MENU_NUM
);
1884 // Swap the byte order
1886 CopyMem (Type
, &MenuArray
[Select
]->Type
, sizeof (UINT16
));
1887 *Type
= NTOHS (*Type
);