2 Support for PxeBc dhcp functions.
4 Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "PxeBcImpl.h"
19 // This is a map from the interested DHCP4 option tags' index to the tag value.
21 UINT8 mInterestedDhcp4Tags
[PXEBC_DHCP4_TAG_INDEX_MAX
] = {
22 PXEBC_DHCP4_TAG_BOOTFILE_LEN
,
23 PXEBC_DHCP4_TAG_VENDOR
,
24 PXEBC_DHCP4_TAG_OVERLOAD
,
25 PXEBC_DHCP4_TAG_MSG_TYPE
,
26 PXEBC_DHCP4_TAG_SERVER_ID
,
27 PXEBC_DHCP4_TAG_CLASS_ID
,
28 PXEBC_DHCP4_TAG_BOOTFILE
33 This function initialize the DHCP4 message instance.
35 This function will pad each item of dhcp4 message packet.
37 @param Seed Pointer to the message instance of the DHCP4 packet.
38 @param Udp4 Pointer to the EFI_UDP4_PROTOCOL instance.
43 IN EFI_DHCP4_PACKET
*Seed
,
44 IN EFI_UDP4_PROTOCOL
*Udp4
47 EFI_SIMPLE_NETWORK_MODE Mode
;
48 EFI_DHCP4_HEADER
*Header
;
50 Udp4
->GetModeData (Udp4
, NULL
, NULL
, NULL
, &Mode
);
52 Seed
->Size
= sizeof (EFI_DHCP4_PACKET
);
53 Seed
->Length
= sizeof (Seed
->Dhcp4
);
55 Header
= &Seed
->Dhcp4
.Header
;
57 ZeroMem (Header
, sizeof (EFI_DHCP4_HEADER
));
58 Header
->OpCode
= PXEBC_DHCP4_OPCODE_REQUEST
;
59 Header
->HwType
= Mode
.IfType
;
60 Header
->HwAddrLen
= (UINT8
) Mode
.HwAddressSize
;
61 CopyMem (Header
->ClientHwAddr
, &Mode
.CurrentAddress
, Header
->HwAddrLen
);
63 Seed
->Dhcp4
.Magik
= PXEBC_DHCP4_MAGIC
;
64 Seed
->Dhcp4
.Option
[0] = PXEBC_DHCP4_TAG_EOP
;
69 Copy the DCHP4 packet from srouce to destination.
71 @param Dst Pointer to the EFI_DHCP4_PROTOCOL instance.
72 @param Src Pointer to the EFI_DHCP4_PROTOCOL instance.
76 PxeBcCopyEfiDhcp4Packet (
77 IN EFI_DHCP4_PACKET
*Dst
,
78 IN EFI_DHCP4_PACKET
*Src
81 ASSERT (Dst
->Size
>= Src
->Length
);
83 CopyMem (&Dst
->Dhcp4
, &Src
->Dhcp4
, Src
->Length
);
84 Dst
->Length
= Src
->Length
;
89 Copy the dhcp4 packet to the PxeBc private data and parse the dhcp4 packet.
91 @param Private Pointer to PxeBc private data.
92 @param OfferIndex Index of cached packets as complements of pxe mode data,
93 the index is maximum offer number.
98 IN PXEBC_PRIVATE_DATA
*Private
,
102 EFI_PXE_BASE_CODE_MODE
*Mode
;
103 EFI_DHCP4_PACKET
*Offer
;
105 ASSERT (OfferIndex
< Private
->NumOffers
);
106 ASSERT (OfferIndex
< PXEBC_MAX_OFFER_NUM
);
108 Mode
= Private
->PxeBc
.Mode
;
109 Offer
= &Private
->Dhcp4Offers
[OfferIndex
].Packet
.Offer
;
111 PxeBcCopyEfiDhcp4Packet (&Private
->ProxyOffer
.Packet
.Offer
, Offer
);
112 CopyMem (&Mode
->ProxyOffer
, &Offer
->Dhcp4
, Offer
->Length
);
113 Mode
->ProxyOfferReceived
= TRUE
;
115 PxeBcParseCachedDhcpPacket (&Private
->ProxyOffer
);
120 Parse the cached dhcp packet.
122 @param CachedPacket Pointer to cached dhcp packet.
124 @retval TRUE Succeed to parse and validation.
125 @retval FALSE Fail to parse or validation.
129 PxeBcParseCachedDhcpPacket (
130 IN PXEBC_CACHED_DHCP4_PACKET
*CachedPacket
133 EFI_DHCP4_PACKET
*Offer
;
134 EFI_DHCP4_PACKET_OPTION
**Options
;
135 EFI_DHCP4_PACKET_OPTION
*Option
;
140 CachedPacket
->IsPxeOffer
= FALSE
;
141 ZeroMem (CachedPacket
->Dhcp4Option
, sizeof (CachedPacket
->Dhcp4Option
));
142 ZeroMem (&CachedPacket
->PxeVendorOption
, sizeof (CachedPacket
->PxeVendorOption
));
144 Offer
= &CachedPacket
->Packet
.Offer
;
145 Options
= CachedPacket
->Dhcp4Option
;
148 // Parse interested dhcp options and store their pointers in CachedPacket->Dhcp4Option.
150 for (Index
= 0; Index
< PXEBC_DHCP4_TAG_INDEX_MAX
; Index
++) {
151 Options
[Index
] = PxeBcParseExtendOptions (
153 GET_OPTION_BUFFER_LEN (Offer
),
154 mInterestedDhcp4Tags
[Index
]
159 // Check whether is an offer with PXEClient or not.
161 Option
= Options
[PXEBC_DHCP4_TAG_INDEX_CLASS_ID
];
162 if ((Option
!= NULL
) && (Option
->Length
>= 9) &&
163 (CompareMem (Option
->Data
, DEFAULT_CLASS_ID_DATA
, 9) == 0)) {
165 CachedPacket
->IsPxeOffer
= TRUE
;
169 // Parse pxe vendor options and store their content/pointers in CachedPacket->PxeVendorOption.
171 Option
= Options
[PXEBC_DHCP4_TAG_INDEX_VENDOR
];
172 if (CachedPacket
->IsPxeOffer
&& (Option
!= NULL
)) {
174 if (!PxeBcParseVendorOptions (Option
, &CachedPacket
->PxeVendorOption
)) {
180 // Check whether bootfilename/serverhostname overloaded (See details in dhcp spec).
181 // If overloaded, parse this buffer as nested dhcp options, or just parse bootfilename/
182 // serverhostname option.
184 Option
= Options
[PXEBC_DHCP4_TAG_INDEX_OVERLOAD
];
185 if ((Option
!= NULL
) && ((Option
->Data
[0] & PXEBC_DHCP4_OVERLOAD_FILE
) != 0)) {
187 Options
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] = PxeBcParseExtendOptions (
188 (UINT8
*) Offer
->Dhcp4
.Header
.BootFileName
,
189 sizeof (Offer
->Dhcp4
.Header
.BootFileName
),
190 PXEBC_DHCP4_TAG_BOOTFILE
193 // RFC 2132, Section 9.5 does not strictly state Bootfile name (option 67) is null
194 // terminated string. So force to append null terminated character at the end of string.
196 if (Options
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] != NULL
) {
197 Ptr8
= (UINT8
*)&Options
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
]->Data
[0];
198 Ptr8
+= Options
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
]->Length
;
202 } else if ((Options
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] == NULL
) &&
203 (Offer
->Dhcp4
.Header
.BootFileName
[0] != 0)) {
205 // If the bootfile is not present and bootfilename is present in dhcp packet, just parse it.
206 // And do not count dhcp option header, or else will destory the serverhostname.
208 Options
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] = (EFI_DHCP4_PACKET_OPTION
*) (&Offer
->Dhcp4
.Header
.BootFileName
[0] -
209 OFFSET_OF (EFI_DHCP4_PACKET_OPTION
, Data
[0]));
214 // Determine offer type of the dhcp packet.
216 Option
= Options
[PXEBC_DHCP4_TAG_INDEX_MSG_TYPE
];
217 if ((Option
== NULL
) || (Option
->Data
[0] == 0)) {
219 // It's a bootp offer
221 Option
= CachedPacket
->Dhcp4Option
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
];
222 if (Option
== NULL
) {
224 // bootp offer without bootfilename, discard it.
229 OfferType
= DHCP4_PACKET_TYPE_BOOTP
;
233 if (IS_VALID_DISCOVER_VENDOR_OPTION (CachedPacket
->PxeVendorOption
.BitMap
)) {
235 // It's a pxe10 offer with PXEClient and discover vendor option.
237 OfferType
= DHCP4_PACKET_TYPE_PXE10
;
238 } else if (IS_VALID_MTFTP_VENDOR_OPTION (CachedPacket
->PxeVendorOption
.BitMap
)) {
240 // It's a wfm11a offer with PXEClient and mtftp vendor option, and
241 // return false since mtftp not supported currently.
246 // If the binl offer with only PXEClient.
248 OfferType
= (UINT8
) ((CachedPacket
->IsPxeOffer
) ? DHCP4_PACKET_TYPE_BINL
: DHCP4_PACKET_TYPE_DHCP_ONLY
);
252 CachedPacket
->OfferType
= OfferType
;
259 Offer dhcp service with a BINL dhcp offer.
261 @param Private Pointer to PxeBc private data.
262 @param Index Index of cached packets as complements of pxe mode data,
263 the index is maximum offer number.
265 @retval TRUE Offer the service successfully under priority BINL.
266 @retval FALSE Boot Service failed, parse cached dhcp packet failed or this
267 BINL ack cannot find options set or bootfile name specified.
272 IN PXEBC_PRIVATE_DATA
*Private
,
276 EFI_DHCP4_PACKET
*Offer
;
277 EFI_IP_ADDRESS ServerIp
;
279 PXEBC_CACHED_DHCP4_PACKET
*CachedPacket
;
280 EFI_DHCP4_PACKET
*Reply
;
282 ASSERT (Index
< PXEBC_MAX_OFFER_NUM
);
283 ASSERT (Private
->Dhcp4Offers
[Index
].OfferType
== DHCP4_PACKET_TYPE_BINL
);
285 Offer
= &Private
->Dhcp4Offers
[Index
].Packet
.Offer
;
288 // Use siaddr(next server) in DHCPOFFER packet header, if zero, use option 54(server identifier)
289 // in DHCPOFFER packet.
290 // (It does not comply with PXE Spec, Ver2.1)
292 if (EFI_IP4_EQUAL (&Offer
->Dhcp4
.Header
.ServerAddr
.Addr
, &mZeroIp4Addr
)) {
295 Private
->Dhcp4Offers
[Index
].Dhcp4Option
[PXEBC_DHCP4_TAG_INDEX_SERVER_ID
]->Data
,
296 sizeof (EFI_IPv4_ADDRESS
)
301 &Offer
->Dhcp4
.Header
.ServerAddr
,
302 sizeof (EFI_IPv4_ADDRESS
)
305 if (ServerIp
.Addr
[0] == 0) {
309 CachedPacket
= &Private
->ProxyOffer
;
310 Reply
= &CachedPacket
->Packet
.Offer
;
312 Status
= PxeBcDiscvBootService (
323 if (EFI_ERROR (Status
)) {
327 if (!PxeBcParseCachedDhcpPacket (CachedPacket
)) {
331 if ((CachedPacket
->OfferType
!= DHCP4_PACKET_TYPE_PXE10
) &&
332 (CachedPacket
->Dhcp4Option
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] == NULL
)) {
334 // This BINL ack doesn't have discovery options set or bootfile name
340 Private
->PxeBc
.Mode
->ProxyOfferReceived
= TRUE
;
341 CopyMem (&Private
->PxeBc
.Mode
->ProxyOffer
, &Reply
->Dhcp4
, Reply
->Length
);
348 Offer dhcp service for each proxy with a BINL dhcp offer.
350 @param Private Pointer to PxeBc private data
351 @param OfferIndex Pointer to the index of cached packets as complements of
352 pxe mode data, the index is maximum offer number.
354 @return If there is no service needed offer return FALSE, otherwise TRUE.
359 IN PXEBC_PRIVATE_DATA
*Private
,
360 OUT UINT32
*OfferIndex
365 for (Index
= 0; Index
< Private
->ProxyIndex
[DHCP4_PACKET_TYPE_BINL
]; Index
++) {
367 *OfferIndex
= Private
->BinlIndex
[Index
];
369 // Try this BINL proxy offer
371 if (PxeBcTryBinl (Private
, *OfferIndex
)) {
381 This function is to check the selected proxy offer (include BINL dhcp offer and
382 DHCP_ONLY offer ) and set the flag and copy the DHCP packets to the Pxe base code
385 @param Private Pointer to PxeBc private data.
387 @retval EFI_SUCCESS Operational successful.
388 @retval EFI_NO_RESPONSE Offer dhcp service failed.
392 PxeBcCheckSelectedOffer (
393 IN PXEBC_PRIVATE_DATA
*Private
396 PXEBC_CACHED_DHCP4_PACKET
*SelectedOffer
;
397 EFI_DHCP4_PACKET_OPTION
**Options
;
399 EFI_DHCP4_PACKET
*Offer
;
400 UINT32 ProxyOfferIndex
;
402 EFI_PXE_BASE_CODE_MODE
*Mode
;
403 EFI_DHCP4_PACKET
*Ack
;
405 ASSERT (Private
->SelectedOffer
!= 0);
407 Status
= EFI_SUCCESS
;
408 SelectedOffer
= &Private
->Dhcp4Offers
[Private
->SelectedOffer
- 1];
409 Options
= SelectedOffer
->Dhcp4Option
;
411 if (SelectedOffer
->OfferType
== DHCP4_PACKET_TYPE_BINL
) {
413 // The addresses are acquired from a BINL dhcp offer, try BINL to get
416 if (!PxeBcTryBinl (Private
, Private
->SelectedOffer
- 1)) {
417 Status
= EFI_NO_RESPONSE
;
419 } else if (SelectedOffer
->OfferType
== DHCP4_PACKET_TYPE_DHCP_ONLY
) {
421 // The selected offer to finish the D.O.R.A. is a DHCP only offer, we need
422 // try proxy offers if there are some, othewise the bootfile name must be
423 // set in this DHCP only offer.
425 if (Private
->GotProxyOffer
) {
427 // Get rid of the compiler warning.
430 if (Private
->SortOffers
) {
432 // The offers are sorted before selecting, the proxy offer type must be
433 // already determined.
435 ASSERT (Private
->ProxyIndex
[Private
->ProxyOfferType
] > 0);
437 if (Private
->ProxyOfferType
== DHCP4_PACKET_TYPE_BINL
) {
439 // We buffer all received BINL proxy offers, try them all one by one
441 if (!PxeBcTryBinlProxy (Private
, &ProxyOfferIndex
)) {
442 Status
= EFI_NO_RESPONSE
;
446 // For other types, only one proxy offer is buffered.
448 ProxyOfferIndex
= Private
->ProxyIndex
[Private
->ProxyOfferType
] - 1;
452 // The proxy offer type is not determined, choose proxy offer in the
455 Status
= EFI_NO_RESPONSE
;
457 for (Index
= 0; Index
< Private
->NumOffers
; Index
++) {
459 Offer
= &Private
->Dhcp4Offers
[Index
].Packet
.Offer
;
460 if (!IS_PROXY_DHCP_OFFER (Offer
)) {
462 // Skip non proxy dhcp offers.
467 if (Private
->Dhcp4Offers
[Index
].OfferType
== DHCP4_PACKET_TYPE_BINL
) {
471 if (!PxeBcTryBinl (Private
, Index
)) {
473 // Failed, skip to the next offer
479 Private
->ProxyOfferType
= Private
->Dhcp4Offers
[Index
].OfferType
;
480 ProxyOfferIndex
= Index
;
481 Status
= EFI_SUCCESS
;
486 if (!EFI_ERROR (Status
) && (Private
->ProxyOfferType
!= DHCP4_PACKET_TYPE_BINL
)) {
488 // Copy the proxy offer to Mode and set the flag
490 PxeBcCopyProxyOffer (Private
, ProxyOfferIndex
);
494 // No proxy offer is received, the bootfile name MUST be set.
496 ASSERT (Options
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] != NULL
);
500 if (!EFI_ERROR (Status
)) {
502 // Everything is OK, set the flag and copy the DHCP packets.
504 Mode
= Private
->PxeBc
.Mode
;
505 Offer
= &SelectedOffer
->Packet
.Offer
;
508 // The discover packet is already copied, just set flag here.
510 Mode
->DhcpDiscoverValid
= TRUE
;
512 Ack
= &Private
->Dhcp4Ack
.Packet
.Ack
;
513 if (SelectedOffer
->OfferType
== DHCP4_PACKET_TYPE_BOOTP
) {
515 // Other type of ACK is already cached. Bootp is special that we should
516 // use the bootp reply as the ACK and put it into the DHCP_ONLY buffer.
518 PxeBcCopyEfiDhcp4Packet (&Private
->Dhcp4Ack
.Packet
.Ack
, Offer
);
521 PxeBcParseCachedDhcpPacket (&Private
->Dhcp4Ack
);
523 Mode
->DhcpAckReceived
= TRUE
;
526 // Copy the dhcp ack.
528 CopyMem (&Mode
->DhcpAck
, &Ack
->Dhcp4
, Ack
->Length
);
536 Cache the Dhcp4 packet offer, Parse and validate each option of the packet.
538 @param Private Pointer to PxeBc private data.
539 @param RcvdOffer Pointer to the received Dhcp proxy offer packet.
543 PxeBcCacheDhcpOffer (
544 IN PXEBC_PRIVATE_DATA
*Private
,
545 IN EFI_DHCP4_PACKET
*RcvdOffer
548 PXEBC_CACHED_DHCP4_PACKET
*CachedOffer
;
549 EFI_DHCP4_PACKET
*Offer
;
552 CachedOffer
= &Private
->Dhcp4Offers
[Private
->NumOffers
];
553 Offer
= &CachedOffer
->Packet
.Offer
;
556 // Cache the orignal dhcp packet
558 PxeBcCopyEfiDhcp4Packet (Offer
, RcvdOffer
);
561 // Parse and validate the options (including dhcp option and vendor option)
563 if (!PxeBcParseCachedDhcpPacket (CachedOffer
)) {
567 OfferType
= CachedOffer
->OfferType
;
568 ASSERT (OfferType
< DHCP4_PACKET_TYPE_MAX
);
570 if (OfferType
== DHCP4_PACKET_TYPE_BOOTP
) {
572 if (Private
->BootpIndex
!= 0) {
574 // Only cache the first bootp offer, discard others.
579 // Take as a dhcp only offer, but record index specifically.
581 Private
->BootpIndex
= Private
->NumOffers
+ 1;
585 if (IS_PROXY_DHCP_OFFER (Offer
)) {
587 // It's a proxy dhcp offer with no your address, including pxe10, wfm11a or binl offer.
589 Private
->GotProxyOffer
= TRUE
;
591 if (OfferType
== DHCP4_PACKET_TYPE_BINL
) {
593 // Cache all binl offers.
595 Private
->BinlIndex
[Private
->ProxyIndex
[DHCP4_PACKET_TYPE_BINL
]] = Private
->NumOffers
;
596 Private
->ProxyIndex
[DHCP4_PACKET_TYPE_BINL
]++;
597 } else if (Private
->ProxyIndex
[OfferType
] != 0) {
599 // Only cache the first pxe10/wfm11a offers each, discard the others.
604 // Record index of the proxy dhcp offer with type other than binl.
606 Private
->ProxyIndex
[OfferType
] = Private
->NumOffers
+ 1;
610 // It's a dhcp offer with your address.
612 ASSERT (Private
->ServerCount
[OfferType
] < PXEBC_MAX_OFFER_NUM
);
613 Private
->OfferIndex
[OfferType
][Private
->ServerCount
[OfferType
]] = Private
->NumOffers
;
614 Private
->ServerCount
[OfferType
]++;
619 // Count the accepted offers.
621 Private
->NumOffers
++;
626 Select the specified proxy offer, such as BINL, DHCP_ONLY and so on.
627 If the proxy does not exist, try offers with bootfile.
629 @param Private Pointer to PxeBc private data.
634 IN PXEBC_PRIVATE_DATA
*Private
639 EFI_DHCP4_PACKET
*Offer
;
641 Private
->SelectedOffer
= 0;
643 if (Private
->SortOffers
) {
645 // Select offer according to the priority
647 if (Private
->ServerCount
[DHCP4_PACKET_TYPE_PXE10
] > 0) {
651 Private
->SelectedOffer
= Private
->OfferIndex
[DHCP4_PACKET_TYPE_PXE10
][0] + 1;
653 } else if (Private
->ServerCount
[DHCP4_PACKET_TYPE_WFM11A
] > 0) {
657 Private
->SelectedOffer
= Private
->OfferIndex
[DHCP4_PACKET_TYPE_WFM11A
][0] + 1;
659 } else if ((Private
->ProxyIndex
[DHCP4_PACKET_TYPE_PXE10
] > 0) &&
660 (Private
->ServerCount
[DHCP4_PACKET_TYPE_DHCP_ONLY
] > 0)
663 // DHCP only and proxy DHCP with PXE10
665 Private
->SelectedOffer
= Private
->OfferIndex
[DHCP4_PACKET_TYPE_DHCP_ONLY
][0] + 1;
666 Private
->ProxyOfferType
= DHCP4_PACKET_TYPE_PXE10
;
668 } else if ((Private
->ProxyIndex
[DHCP4_PACKET_TYPE_WFM11A
] > 0) &&
669 (Private
->ServerCount
[DHCP4_PACKET_TYPE_DHCP_ONLY
] > 0)
672 // DHCP only and proxy DHCP with WfM
674 Private
->SelectedOffer
= Private
->OfferIndex
[DHCP4_PACKET_TYPE_DHCP_ONLY
][0] + 1;
675 Private
->ProxyOfferType
= DHCP4_PACKET_TYPE_WFM11A
;
677 } else if (Private
->ServerCount
[DHCP4_PACKET_TYPE_BINL
] > 0) {
681 Private
->SelectedOffer
= Private
->OfferIndex
[DHCP4_PACKET_TYPE_BINL
][0] + 1;
683 } else if ((Private
->ProxyIndex
[DHCP4_PACKET_TYPE_BINL
] > 0) &&
684 (Private
->ServerCount
[DHCP4_PACKET_TYPE_DHCP_ONLY
] > 0)
687 // DHCP only and proxy DHCP with BINL
689 Private
->SelectedOffer
= Private
->OfferIndex
[DHCP4_PACKET_TYPE_DHCP_ONLY
][0] + 1;
690 Private
->ProxyOfferType
= DHCP4_PACKET_TYPE_BINL
;
694 // Try offers with bootfile
696 for (Index
= 0; Index
< Private
->ServerCount
[DHCP4_PACKET_TYPE_DHCP_ONLY
]; Index
++) {
698 // Select the first DHCP only offer with bootfile
700 OfferIndex
= Private
->OfferIndex
[DHCP4_PACKET_TYPE_DHCP_ONLY
][Index
];
701 if (Private
->Dhcp4Offers
[OfferIndex
].Dhcp4Option
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] != NULL
) {
702 Private
->SelectedOffer
= OfferIndex
+ 1;
707 if (Private
->SelectedOffer
== 0) {
709 // Select the Bootp reply with bootfile if any
711 Private
->SelectedOffer
= Private
->BootpIndex
;
716 // Try the offers in the received order.
718 for (Index
= 0; Index
< Private
->NumOffers
; Index
++) {
720 Offer
= &Private
->Dhcp4Offers
[Index
].Packet
.Offer
;
722 if (IS_PROXY_DHCP_OFFER (Offer
)) {
729 if ((Private
->Dhcp4Offers
[Index
].OfferType
== DHCP4_PACKET_TYPE_DHCP_ONLY
) &&
730 ((!Private
->GotProxyOffer
) && (Private
->Dhcp4Offers
[Index
].Dhcp4Option
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] == NULL
))) {
732 // DHCP only offer but no proxy offer received and no bootfile option in this offer
737 Private
->SelectedOffer
= Index
+ 1;
747 EFI_DHCP4_CALLBACK is provided by the consumer of the EFI DHCPv4 Protocol driver
748 to intercept events that occurred in the configuration process. This structure
749 provides advanced control of each state transition of the DHCP process. The
750 returned status code determines the behavior of the EFI DHCPv4 Protocol driver.
751 There are three possible returned values, which are described in the following
754 @param This Pointer to the EFI DHCPv4 Protocol instance that is used to
755 configure this callback function.
756 @param Context Pointer to the context that is initialized by
757 EFI_DHCP4_PROTOCOL.Configure().
758 @param CurrentState The current operational state of the EFI DHCPv4 Protocol
760 @param Dhcp4Event The event that occurs in the current state, which usually means a
762 @param Packet The DHCP packet that is going to be sent or already received.
763 @param NewPacket The packet that is used to replace the above Packet.
765 @retval EFI_SUCCESS Tells the EFI DHCPv4 Protocol driver to continue the DHCP process.
766 @retval EFI_NOT_READY Only used in the Dhcp4Selecting state. The EFI DHCPv4 Protocol
767 driver will continue to wait for more DHCPOFFER packets until the retry
769 @retval EFI_ABORTED Tells the EFI DHCPv4 Protocol driver to abort the current process and
770 return to the Dhcp4Init or Dhcp4InitReboot state.
776 IN EFI_DHCP4_PROTOCOL
* This
,
778 IN EFI_DHCP4_STATE CurrentState
,
779 IN EFI_DHCP4_EVENT Dhcp4Event
,
780 IN EFI_DHCP4_PACKET
* Packet OPTIONAL
,
781 OUT EFI_DHCP4_PACKET
**NewPacket OPTIONAL
784 PXEBC_PRIVATE_DATA
*Private
;
785 EFI_PXE_BASE_CODE_MODE
*Mode
;
786 EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL
*Callback
;
787 EFI_DHCP4_PACKET_OPTION
*MaxMsgSize
;
791 CHAR8
*SystemSerialNumber
;
792 EFI_DHCP4_HEADER
*DhcpHeader
;
794 if ((Dhcp4Event
!= Dhcp4RcvdOffer
) &&
795 (Dhcp4Event
!= Dhcp4SelectOffer
) &&
796 (Dhcp4Event
!= Dhcp4SendDiscover
) &&
797 (Dhcp4Event
!= Dhcp4RcvdAck
) &&
798 (Dhcp4Event
!= Dhcp4SendRequest
)) {
802 Private
= (PXEBC_PRIVATE_DATA
*) Context
;
803 Mode
= Private
->PxeBc
.Mode
;
804 Callback
= Private
->PxeBcCallback
;
807 // Override the Maximum DHCP Message Size.
809 MaxMsgSize
= PxeBcParseExtendOptions (
810 Packet
->Dhcp4
.Option
,
811 GET_OPTION_BUFFER_LEN (Packet
),
812 PXEBC_DHCP4_TAG_MAXMSG
814 if (MaxMsgSize
!= NULL
) {
815 Value
= HTONS (PXEBC_DHCP4_MAX_PACKET_SIZE
);
816 CopyMem (MaxMsgSize
->Data
, &Value
, sizeof (Value
));
819 if ((Dhcp4Event
!= Dhcp4SelectOffer
) && (Callback
!= NULL
)) {
820 Received
= (BOOLEAN
) ((Dhcp4Event
== Dhcp4RcvdOffer
) || (Dhcp4Event
== Dhcp4RcvdAck
));
821 Status
= Callback
->Callback (
826 (EFI_PXE_BASE_CODE_PACKET
*) &Packet
->Dhcp4
828 if (Status
!= EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE
) {
833 Status
= EFI_SUCCESS
;
835 switch (Dhcp4Event
) {
837 case Dhcp4SendDiscover
:
838 case Dhcp4SendRequest
:
839 if (Mode
->SendGUID
) {
841 // send the system GUID instead of the MAC address as the hardware address
842 // in the DHCP packet header.
844 DhcpHeader
= &Packet
->Dhcp4
.Header
;
846 if (EFI_ERROR (GetSmbiosSystemGuidAndSerialNumber ((EFI_GUID
*) DhcpHeader
->ClientHwAddr
, &SystemSerialNumber
))) {
848 // GUID not yet set - send all 0xff's to show programable (via SetVariable)
849 // SetMem(DHCPV4_OPTIONS_BUFFER.DhcpPlatformId.Guid, sizeof(EFI_GUID), 0xff);
850 // GUID not yet set - send all 0's to show not programable
852 ZeroMem (DhcpHeader
->ClientHwAddr
, sizeof (EFI_GUID
));
855 DhcpHeader
->HwAddrLen
= sizeof (EFI_GUID
);
858 if (Dhcp4Event
== Dhcp4SendDiscover
) {
860 // Cache the dhcp discover packet, of which some information will be used later.
862 CopyMem (Mode
->DhcpDiscover
.Raw
, &Packet
->Dhcp4
, Packet
->Length
);
868 Status
= EFI_NOT_READY
;
869 if (Private
->NumOffers
< PXEBC_MAX_OFFER_NUM
) {
871 // Cache the dhcp offers in Private->Dhcp4Offers[]
873 PxeBcCacheDhcpOffer (Private
, Packet
);
878 case Dhcp4SelectOffer
:
880 // Select an offer, if succeeded, Private->SelectedOffer points to
881 // the index of the selected one.
883 PxeBcSelectOffer (Private
);
885 if (Private
->SelectedOffer
== 0) {
886 Status
= EFI_ABORTED
;
888 *NewPacket
= &Private
->Dhcp4Offers
[Private
->SelectedOffer
- 1].Packet
.Offer
;
897 ASSERT (Private
->SelectedOffer
!= 0);
899 PxeBcCopyEfiDhcp4Packet (&Private
->Dhcp4Ack
.Packet
.Ack
, Packet
);
911 Initialize the DHCP options and build the option list.
913 @param Private Pointer to PxeBc private data.
914 @param OptList Pointer to a DHCP option list.
916 @param IsDhcpDiscover Discover dhcp option or not.
918 @return The index item number of the option list.
922 PxeBcBuildDhcpOptions (
923 IN PXEBC_PRIVATE_DATA
*Private
,
924 IN EFI_DHCP4_PACKET_OPTION
**OptList
,
925 IN BOOLEAN IsDhcpDiscover
929 PXEBC_DHCP4_OPTION_ENTRY OptEnt
;
931 CHAR8
*SystemSerialNumber
;
934 OptList
[0] = (EFI_DHCP4_PACKET_OPTION
*) Private
->OptionBuffer
;
936 if (!IsDhcpDiscover
) {
938 // Append message type.
940 OptList
[Index
]->OpCode
= PXEBC_DHCP4_TAG_MSG_TYPE
;
941 OptList
[Index
]->Length
= 1;
942 OptEnt
.Mesg
= (PXEBC_DHCP4_OPTION_MESG
*) OptList
[Index
]->Data
;
943 OptEnt
.Mesg
->Type
= PXEBC_DHCP4_MSG_TYPE_REQUEST
;
945 OptList
[Index
] = GET_NEXT_DHCP_OPTION (OptList
[Index
- 1]);
948 // Append max message size.
950 OptList
[Index
]->OpCode
= PXEBC_DHCP4_TAG_MAXMSG
;
951 OptList
[Index
]->Length
= sizeof (PXEBC_DHCP4_OPTION_MAX_MESG_SIZE
);
952 OptEnt
.MaxMesgSize
= (PXEBC_DHCP4_OPTION_MAX_MESG_SIZE
*) OptList
[Index
]->Data
;
953 Value
= NTOHS (PXEBC_DHCP4_MAX_PACKET_SIZE
);
954 CopyMem (&OptEnt
.MaxMesgSize
->Size
, &Value
, sizeof (UINT16
));
956 OptList
[Index
] = GET_NEXT_DHCP_OPTION (OptList
[Index
- 1]);
959 // Parameter request list option.
961 OptList
[Index
]->OpCode
= PXEBC_DHCP4_TAG_PARA_LIST
;
962 OptList
[Index
]->Length
= 35;
963 OptEnt
.Para
= (PXEBC_DHCP4_OPTION_PARA
*) OptList
[Index
]->Data
;
964 OptEnt
.Para
->ParaList
[0] = PXEBC_DHCP4_TAG_NETMASK
;
965 OptEnt
.Para
->ParaList
[1] = PXEBC_DHCP4_TAG_TIME_OFFSET
;
966 OptEnt
.Para
->ParaList
[2] = PXEBC_DHCP4_TAG_ROUTER
;
967 OptEnt
.Para
->ParaList
[3] = PXEBC_DHCP4_TAG_TIME_SERVER
;
968 OptEnt
.Para
->ParaList
[4] = PXEBC_DHCP4_TAG_NAME_SERVER
;
969 OptEnt
.Para
->ParaList
[5] = PXEBC_DHCP4_TAG_DNS_SERVER
;
970 OptEnt
.Para
->ParaList
[6] = PXEBC_DHCP4_TAG_HOSTNAME
;
971 OptEnt
.Para
->ParaList
[7] = PXEBC_DHCP4_TAG_BOOTFILE_LEN
;
972 OptEnt
.Para
->ParaList
[8] = PXEBC_DHCP4_TAG_DOMAINNAME
;
973 OptEnt
.Para
->ParaList
[9] = PXEBC_DHCP4_TAG_ROOTPATH
;
974 OptEnt
.Para
->ParaList
[10] = PXEBC_DHCP4_TAG_EXTEND_PATH
;
975 OptEnt
.Para
->ParaList
[11] = PXEBC_DHCP4_TAG_EMTU
;
976 OptEnt
.Para
->ParaList
[12] = PXEBC_DHCP4_TAG_TTL
;
977 OptEnt
.Para
->ParaList
[13] = PXEBC_DHCP4_TAG_BROADCAST
;
978 OptEnt
.Para
->ParaList
[14] = PXEBC_DHCP4_TAG_NIS_DOMAIN
;
979 OptEnt
.Para
->ParaList
[15] = PXEBC_DHCP4_TAG_NIS_SERVER
;
980 OptEnt
.Para
->ParaList
[16] = PXEBC_DHCP4_TAG_NTP_SERVER
;
981 OptEnt
.Para
->ParaList
[17] = PXEBC_DHCP4_TAG_VENDOR
;
982 OptEnt
.Para
->ParaList
[18] = PXEBC_DHCP4_TAG_REQUEST_IP
;
983 OptEnt
.Para
->ParaList
[19] = PXEBC_DHCP4_TAG_LEASE
;
984 OptEnt
.Para
->ParaList
[20] = PXEBC_DHCP4_TAG_SERVER_ID
;
985 OptEnt
.Para
->ParaList
[21] = PXEBC_DHCP4_TAG_T1
;
986 OptEnt
.Para
->ParaList
[22] = PXEBC_DHCP4_TAG_T2
;
987 OptEnt
.Para
->ParaList
[23] = PXEBC_DHCP4_TAG_CLASS_ID
;
988 OptEnt
.Para
->ParaList
[24] = PXEBC_DHCP4_TAG_TFTP
;
989 OptEnt
.Para
->ParaList
[25] = PXEBC_DHCP4_TAG_BOOTFILE
;
990 OptEnt
.Para
->ParaList
[26] = PXEBC_PXE_DHCP4_TAG_UUID
;
991 OptEnt
.Para
->ParaList
[27] = 0x80;
992 OptEnt
.Para
->ParaList
[28] = 0x81;
993 OptEnt
.Para
->ParaList
[29] = 0x82;
994 OptEnt
.Para
->ParaList
[30] = 0x83;
995 OptEnt
.Para
->ParaList
[31] = 0x84;
996 OptEnt
.Para
->ParaList
[32] = 0x85;
997 OptEnt
.Para
->ParaList
[33] = 0x86;
998 OptEnt
.Para
->ParaList
[34] = 0x87;
1000 OptList
[Index
] = GET_NEXT_DHCP_OPTION (OptList
[Index
- 1]);
1003 // Append UUID/Guid-based client identifier option
1005 OptList
[Index
]->OpCode
= PXEBC_PXE_DHCP4_TAG_UUID
;
1006 OptList
[Index
]->Length
= sizeof (PXEBC_DHCP4_OPTION_UUID
);
1007 OptEnt
.Uuid
= (PXEBC_DHCP4_OPTION_UUID
*) OptList
[Index
]->Data
;
1008 OptEnt
.Uuid
->Type
= 0;
1010 OptList
[Index
] = GET_NEXT_DHCP_OPTION (OptList
[Index
- 1]);
1012 if (EFI_ERROR (GetSmbiosSystemGuidAndSerialNumber ((EFI_GUID
*) OptEnt
.Uuid
->Guid
, &SystemSerialNumber
))) {
1014 // GUID not yet set - send all 0xff's to show programable (via SetVariable)
1015 // SetMem(DHCPV4_OPTIONS_BUFFER.DhcpPlatformId.Guid, sizeof(EFI_GUID), 0xff);
1016 // GUID not yet set - send all 0's to show not programable
1018 ZeroMem (OptEnt
.Uuid
->Guid
, sizeof (EFI_GUID
));
1022 // Append client network device interface option
1024 OptList
[Index
]->OpCode
= PXEBC_PXE_DHCP4_TAG_UNDI
;
1025 OptList
[Index
]->Length
= sizeof (PXEBC_DHCP4_OPTION_UNDI
);
1026 OptEnt
.Undi
= (PXEBC_DHCP4_OPTION_UNDI
*) OptList
[Index
]->Data
;
1027 if (Private
->Nii
!= NULL
) {
1028 OptEnt
.Undi
->Type
= Private
->Nii
->Type
;
1029 OptEnt
.Undi
->MajorVer
= Private
->Nii
->MajorVer
;
1030 OptEnt
.Undi
->MinorVer
= Private
->Nii
->MinorVer
;
1032 OptEnt
.Undi
->Type
= DEFAULT_UNDI_TYPE
;
1033 OptEnt
.Undi
->MajorVer
= DEFAULT_UNDI_MAJOR
;
1034 OptEnt
.Undi
->MinorVer
= DEFAULT_UNDI_MINOR
;
1038 OptList
[Index
] = GET_NEXT_DHCP_OPTION (OptList
[Index
- 1]);
1041 // Append client system architecture option
1043 OptList
[Index
]->OpCode
= PXEBC_PXE_DHCP4_TAG_ARCH
;
1044 OptList
[Index
]->Length
= sizeof (PXEBC_DHCP4_OPTION_ARCH
);
1045 OptEnt
.Arch
= (PXEBC_DHCP4_OPTION_ARCH
*) OptList
[Index
]->Data
;
1046 Value
= HTONS (EFI_PXE_CLIENT_SYSTEM_ARCHITECTURE
);
1047 CopyMem (&OptEnt
.Arch
->Type
, &Value
, sizeof (UINT16
));
1049 OptList
[Index
] = GET_NEXT_DHCP_OPTION (OptList
[Index
- 1]);
1052 // Append client system architecture option
1054 OptList
[Index
]->OpCode
= PXEBC_DHCP4_TAG_CLASS_ID
;
1055 OptList
[Index
]->Length
= sizeof (PXEBC_DHCP4_OPTION_CLID
);
1056 OptEnt
.Clid
= (PXEBC_DHCP4_OPTION_CLID
*) OptList
[Index
]->Data
;
1057 CopyMem (OptEnt
.Clid
, DEFAULT_CLASS_ID_DATA
, sizeof (PXEBC_DHCP4_OPTION_CLID
));
1058 CvtNum (EFI_PXE_CLIENT_SYSTEM_ARCHITECTURE
, OptEnt
.Clid
->ArchitectureType
, sizeof (OptEnt
.Clid
->ArchitectureType
));
1060 if (Private
->Nii
!= NULL
) {
1062 // If NII protocol exists, update DHCP option data
1064 CopyMem (OptEnt
.Clid
->InterfaceName
, Private
->Nii
->StringId
, sizeof (OptEnt
.Clid
->InterfaceName
));
1065 CvtNum (Private
->Nii
->MajorVer
, OptEnt
.Clid
->UndiMajor
, sizeof (OptEnt
.Clid
->UndiMajor
));
1066 CvtNum (Private
->Nii
->MinorVer
, OptEnt
.Clid
->UndiMinor
, sizeof (OptEnt
.Clid
->UndiMinor
));
1076 Discover the boot of service and initialize the vendor option if exists.
1078 @param Private Pointer to PxeBc private data.
1079 @param Type PxeBc option boot item type
1080 @param Layer PxeBc option boot item layer
1081 @param UseBis Use BIS or not
1082 @param DestIp Ip address for server
1083 @param IpCount The total count of the server ip address
1084 @param SrvList Server list
1085 @param IsDiscv Discover the vendor or not
1086 @param Reply The dhcp4 packet of Pxe reply
1088 @retval EFI_SUCCESS Operation succeeds.
1089 @retval EFI_OUT_OF_RESOURCES Allocate memory pool failed.
1090 @retval EFI_NOT_FOUND There is no vendor option exists.
1091 @retval EFI_TIMEOUT Send Pxe Discover time out.
1095 PxeBcDiscvBootService (
1096 IN PXEBC_PRIVATE_DATA
* Private
,
1100 IN EFI_IP_ADDRESS
* DestIp
,
1102 IN EFI_PXE_BASE_CODE_SRVLIST
* SrvList
,
1104 OUT EFI_DHCP4_PACKET
* Reply OPTIONAL
1107 EFI_PXE_BASE_CODE_UDP_PORT Sport
;
1108 EFI_PXE_BASE_CODE_MODE
*Mode
;
1109 EFI_DHCP4_PROTOCOL
*Dhcp4
;
1110 EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN Token
;
1116 EFI_DHCP4_LISTEN_POINT ListenPoint
;
1117 EFI_DHCP4_PACKET
*Response
;
1118 EFI_DHCP4_PACKET_OPTION
*OptList
[PXEBC_DHCP4_MAX_OPTION_NUM
];
1120 EFI_DHCP4_PACKET_OPTION
*PxeOpt
;
1121 PXEBC_OPTION_BOOT_ITEM
*PxeBootItem
;
1123 CHAR8
*SystemSerialNumber
;
1124 EFI_DHCP4_HEADER
*DhcpHeader
;
1127 Mode
= Private
->PxeBc
.Mode
;
1128 Dhcp4
= Private
->Dhcp4
;
1129 Status
= EFI_SUCCESS
;
1131 ZeroMem (&Token
, sizeof (EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN
));
1133 if (DestIp
== NULL
) {
1134 Sport
= PXEBC_DHCP4_S_PORT
;
1137 Sport
= PXEBC_BS_DISCOVER_PORT
;
1141 if (!UseBis
&& Layer
!= NULL
) {
1142 *Layer
&= EFI_PXE_BASE_CODE_BOOT_LAYER_MASK
;
1145 OptCount
= PxeBcBuildDhcpOptions (Private
, OptList
, FALSE
);
1148 ASSERT (Layer
!= NULL
);
1150 // Add vendor option of PXE_BOOT_ITEM
1152 VendorOptLen
= (sizeof (EFI_DHCP4_PACKET_OPTION
) - 1) * 2 + sizeof (PXEBC_OPTION_BOOT_ITEM
) + 1;
1153 OptList
[OptCount
] = AllocatePool (VendorOptLen
);
1154 if (OptList
[OptCount
] == NULL
) {
1155 return EFI_OUT_OF_RESOURCES
;
1158 OptList
[OptCount
]->OpCode
= PXEBC_DHCP4_TAG_VENDOR
;
1159 OptList
[OptCount
]->Length
= (UINT8
) (VendorOptLen
- 2);
1160 PxeOpt
= (EFI_DHCP4_PACKET_OPTION
*) OptList
[OptCount
]->Data
;
1161 PxeOpt
->OpCode
= PXEBC_VENDOR_TAG_BOOT_ITEM
;
1162 PxeOpt
->Length
= sizeof (PXEBC_OPTION_BOOT_ITEM
);
1163 PxeBootItem
= (PXEBC_OPTION_BOOT_ITEM
*) PxeOpt
->Data
;
1164 PxeBootItem
->Type
= HTONS (Type
);
1165 PxeBootItem
->Layer
= HTONS (*Layer
);
1166 PxeOpt
->Data
[PxeOpt
->Length
] = PXEBC_DHCP4_TAG_EOP
;
1171 Status
= Dhcp4
->Build (Dhcp4
, &Private
->SeedPacket
, 0, NULL
, OptCount
, OptList
, &Token
.Packet
);
1174 FreePool (OptList
[OptCount
- 1]);
1177 if (EFI_ERROR (Status
)) {
1181 DhcpHeader
= &Token
.Packet
->Dhcp4
.Header
;
1182 if (Mode
->SendGUID
) {
1183 if (EFI_ERROR (GetSmbiosSystemGuidAndSerialNumber ((EFI_GUID
*) DhcpHeader
->ClientHwAddr
, &SystemSerialNumber
))) {
1185 // GUID not yet set - send all 0's to show not programable
1187 ZeroMem (DhcpHeader
->ClientHwAddr
, sizeof (EFI_GUID
));
1190 DhcpHeader
->HwAddrLen
= sizeof (EFI_GUID
);
1193 Xid
= NET_RANDOM (NetRandomInitSeed ());
1194 Token
.Packet
->Dhcp4
.Header
.Xid
= HTONL(Xid
);
1195 Token
.Packet
->Dhcp4
.Header
.Reserved
= HTONS((UINT16
) ((IsBCast
) ? 0x8000 : 0));
1196 CopyMem (&Token
.Packet
->Dhcp4
.Header
.ClientAddr
, &Private
->StationIp
, sizeof (EFI_IPv4_ADDRESS
));
1198 Token
.RemotePort
= Sport
;
1201 SetMem (&Token
.RemoteAddress
, sizeof (EFI_IPv4_ADDRESS
), 0xff);
1203 CopyMem (&Token
.RemoteAddress
, DestIp
, sizeof (EFI_IPv4_ADDRESS
));
1206 CopyMem (&Token
.GatewayAddress
, &Private
->GatewayIp
, sizeof (EFI_IPv4_ADDRESS
));
1209 Token
.ListenPointCount
= 1;
1210 Token
.ListenPoints
= &ListenPoint
;
1211 Token
.ListenPoints
[0].ListenPort
= PXEBC_BS_DISCOVER_PORT
;
1212 CopyMem (&Token
.ListenPoints
[0].ListenAddress
, &Private
->StationIp
, sizeof(EFI_IPv4_ADDRESS
));
1213 CopyMem (&Token
.ListenPoints
[0].SubnetMask
, &Private
->SubnetMask
, sizeof(EFI_IPv4_ADDRESS
));
1216 // Send Pxe Discover
1218 for (TryIndex
= 1; TryIndex
<= PXEBC_BOOT_REQUEST_RETRIES
; TryIndex
++) {
1220 Token
.TimeoutValue
= (UINT16
) (PXEBC_BOOT_REQUEST_TIMEOUT
* TryIndex
);
1221 Token
.Packet
->Dhcp4
.Header
.Seconds
= (UINT16
) (PXEBC_BOOT_REQUEST_TIMEOUT
* (TryIndex
- 1));
1223 Status
= Dhcp4
->TransmitReceive (Dhcp4
, &Token
);
1225 if (Token
.Status
!= EFI_TIMEOUT
) {
1230 if (TryIndex
> PXEBC_BOOT_REQUEST_RETRIES
) {
1232 // No server response our PXE request
1234 Status
= EFI_TIMEOUT
;
1237 if (!EFI_ERROR (Status
)) {
1243 Response
= Token
.ResponseList
;
1245 while (RepIndex
< Token
.ResponseCount
) {
1247 while (SrvIndex
< IpCount
) {
1249 if (SrvList
[SrvIndex
].AcceptAnyResponse
) {
1253 if ((SrvList
[SrvIndex
].Type
== Type
) && EFI_IP4_EQUAL (&(Response
->Dhcp4
.Header
.ServerAddr
), &(Private
->ServerIp
))) {
1260 if ((IpCount
!= SrvIndex
) || (IpCount
== 0)) {
1267 Response
= (EFI_DHCP4_PACKET
*) ((UINT8
*) Response
+ Response
->Size
);
1270 if (RepIndex
< Token
.ResponseCount
) {
1272 if (Reply
!= NULL
) {
1273 PxeBcCopyEfiDhcp4Packet (Reply
, Response
);
1277 CopyMem (&(Mode
->PxeDiscover
), &(Token
.Packet
->Dhcp4
), Token
.Packet
->Length
);
1278 Mode
->PxeDiscoverValid
= TRUE
;
1280 CopyMem (Mode
->PxeReply
.Raw
, &Response
->Dhcp4
, Response
->Length
);
1281 Mode
->PxeReplyReceived
= TRUE
;
1284 Status
= EFI_NOT_FOUND
;
1288 // free the responselist
1290 if (Token
.ResponseList
!= NULL
) {
1291 FreePool (Token
.ResponseList
);
1295 // Free the dhcp packet
1297 FreePool (Token
.Packet
);
1304 Parse interested dhcp options.
1306 @param Buffer Pointer to the dhcp options packet.
1307 @param Length The length of the dhcp options.
1308 @param OptTag The option OpCode.
1310 @return NULL if the buffer length is 0 and OpCode is not
1311 PXEBC_DHCP4_TAG_EOP, or the pointer to the buffer.
1314 EFI_DHCP4_PACKET_OPTION
*
1315 PxeBcParseExtendOptions (
1321 EFI_DHCP4_PACKET_OPTION
*Option
;
1324 Option
= (EFI_DHCP4_PACKET_OPTION
*) Buffer
;
1327 while (Offset
< Length
&& Option
->OpCode
!= PXEBC_DHCP4_TAG_EOP
) {
1329 if (Option
->OpCode
== OptTag
) {
1334 if (Option
->OpCode
== PXEBC_DHCP4_TAG_PAD
) {
1337 Offset
+= Option
->Length
+ 2;
1340 Option
= (EFI_DHCP4_PACKET_OPTION
*) (Buffer
+ Offset
);
1348 This function is to parse and check vendor options.
1350 @param Dhcp4Option Pointer to dhcp options
1351 @param VendorOption Pointer to vendor options
1353 @return TRUE if valid for vendor options, or FALSE.
1357 PxeBcParseVendorOptions (
1358 IN EFI_DHCP4_PACKET_OPTION
*Dhcp4Option
,
1359 IN PXEBC_VENDOR_OPTION
*VendorOption
1363 UINT8 VendorOptionLen
;
1364 EFI_DHCP4_PACKET_OPTION
*PxeOption
;
1367 BitMap
= VendorOption
->BitMap
;
1368 VendorOptionLen
= Dhcp4Option
->Length
;
1369 PxeOption
= (EFI_DHCP4_PACKET_OPTION
*) &Dhcp4Option
->Data
[0];
1372 while ((Offset
< VendorOptionLen
) && (PxeOption
->OpCode
!= PXEBC_DHCP4_TAG_EOP
)) {
1374 // Parse every Vendor Option and set its BitMap
1376 switch (PxeOption
->OpCode
) {
1378 case PXEBC_VENDOR_TAG_MTFTP_IP
:
1380 CopyMem (&VendorOption
->MtftpIp
, PxeOption
->Data
, sizeof (EFI_IPv4_ADDRESS
));
1383 case PXEBC_VENDOR_TAG_MTFTP_CPORT
:
1385 CopyMem (&VendorOption
->MtftpCPort
, PxeOption
->Data
, sizeof (VendorOption
->MtftpCPort
));
1388 case PXEBC_VENDOR_TAG_MTFTP_SPORT
:
1390 CopyMem (&VendorOption
->MtftpSPort
, PxeOption
->Data
, sizeof (VendorOption
->MtftpSPort
));
1393 case PXEBC_VENDOR_TAG_MTFTP_TIMEOUT
:
1395 VendorOption
->MtftpTimeout
= *PxeOption
->Data
;
1398 case PXEBC_VENDOR_TAG_MTFTP_DELAY
:
1400 VendorOption
->MtftpDelay
= *PxeOption
->Data
;
1403 case PXEBC_VENDOR_TAG_DISCOVER_CTRL
:
1405 VendorOption
->DiscoverCtrl
= *PxeOption
->Data
;
1408 case PXEBC_VENDOR_TAG_DISCOVER_MCAST
:
1410 CopyMem (&VendorOption
->DiscoverMcastIp
, PxeOption
->Data
, sizeof (EFI_IPv4_ADDRESS
));
1413 case PXEBC_VENDOR_TAG_BOOT_SERVERS
:
1415 VendorOption
->BootSvrLen
= PxeOption
->Length
;
1416 VendorOption
->BootSvr
= (PXEBC_BOOT_SVR_ENTRY
*) PxeOption
->Data
;
1419 case PXEBC_VENDOR_TAG_BOOT_MENU
:
1421 VendorOption
->BootMenuLen
= PxeOption
->Length
;
1422 VendorOption
->BootMenu
= (PXEBC_BOOT_MENU_ENTRY
*) PxeOption
->Data
;
1425 case PXEBC_VENDOR_TAG_MENU_PROMPT
:
1427 VendorOption
->MenuPromptLen
= PxeOption
->Length
;
1428 VendorOption
->MenuPrompt
= (PXEBC_MENU_PROMPT
*) PxeOption
->Data
;
1431 case PXEBC_VENDOR_TAG_MCAST_ALLOC
:
1433 CopyMem (&VendorOption
->McastIpBase
, PxeOption
->Data
, sizeof (EFI_IPv4_ADDRESS
));
1434 CopyMem (&VendorOption
->McastIpBlock
, PxeOption
->Data
+ 4, sizeof (VendorOption
->McastIpBlock
));
1435 CopyMem (&VendorOption
->McastIpRange
, PxeOption
->Data
+ 6, sizeof (VendorOption
->McastIpRange
));
1438 case PXEBC_VENDOR_TAG_CREDENTIAL_TYPES
:
1440 VendorOption
->CredTypeLen
= PxeOption
->Length
;
1441 VendorOption
->CredType
= (UINT32
*) PxeOption
->Data
;
1444 case PXEBC_VENDOR_TAG_BOOT_ITEM
:
1446 CopyMem (&VendorOption
->BootSrvType
, PxeOption
->Data
, sizeof (VendorOption
->BootSrvType
));
1447 CopyMem (&VendorOption
->BootSrvLayer
, PxeOption
->Data
+ 2, sizeof (VendorOption
->BootSrvLayer
));
1451 SET_VENDOR_OPTION_BIT_MAP (BitMap
, PxeOption
->OpCode
);
1453 if (PxeOption
->OpCode
== PXEBC_DHCP4_TAG_PAD
) {
1456 Offset
= (UINT8
) (Offset
+ PxeOption
->Length
+ 2);
1459 PxeOption
= (EFI_DHCP4_PACKET_OPTION
*) (Dhcp4Option
->Data
+ Offset
);
1463 // FixMe, return falas if invalid of any vendor option
1471 This function display boot item detail.
1473 If the length of the boot item string over 70 Char, just display 70 Char.
1475 @param Str Pointer to a string (boot item string).
1476 @param Len The length of string.
1480 PxeBcDisplayBootItem (
1487 Len
= (UINT8
) MIN (70, Len
);
1490 AsciiPrint ("%a \n", Str
);
1496 Choose the boot prompt.
1498 @param Private Pointer to PxeBc private data.
1500 @retval EFI_SUCCESS Select boot prompt done.
1501 @retval EFI_TIMEOUT Select boot prompt time out.
1502 @retval EFI_NOT_FOUND The proxy offer is not Pxe10.
1503 @retval EFI_ABORTED User cancel the operation.
1504 @retval EFI_NOT_READY Read the input key from the keybroad has not finish.
1508 PxeBcSelectBootPrompt (
1509 IN PXEBC_PRIVATE_DATA
*Private
1512 PXEBC_CACHED_DHCP4_PACKET
*Packet
;
1513 PXEBC_VENDOR_OPTION
*VendorOpt
;
1514 EFI_EVENT TimeoutEvent
;
1515 EFI_EVENT DescendEvent
;
1516 EFI_INPUT_KEY InputKey
;
1524 TimeoutEvent
= NULL
;
1525 DescendEvent
= NULL
;
1527 if (Private
->PxeBc
.Mode
->ProxyOfferReceived
) {
1529 Packet
= &Private
->ProxyOffer
;
1532 Packet
= &Private
->Dhcp4Ack
;
1535 if (Packet
->OfferType
!= DHCP4_PACKET_TYPE_PXE10
) {
1536 return EFI_NOT_FOUND
;
1539 VendorOpt
= &Packet
->PxeVendorOption
;
1541 if (!IS_VALID_BOOT_PROMPT (VendorOpt
->BitMap
)) {
1545 Timeout
= VendorOpt
->MenuPrompt
->Timeout
;
1546 Prompt
= VendorOpt
->MenuPrompt
->Prompt
;
1547 PromptLen
= (UINT8
) (VendorOpt
->MenuPromptLen
- 1);
1553 if (Timeout
== 255) {
1557 Status
= gBS
->CreateEvent (
1565 if (EFI_ERROR (Status
)) {
1569 Status
= gBS
->SetTimer (
1572 Timeout
* TICKS_PER_SECOND
1575 if (EFI_ERROR (Status
)) {
1579 Status
= gBS
->CreateEvent (
1587 if (EFI_ERROR (Status
)) {
1591 Status
= gBS
->SetTimer (
1597 if (EFI_ERROR (Status
)) {
1601 SecCol
= gST
->ConOut
->Mode
->CursorColumn
;
1602 SecRow
= gST
->ConOut
->Mode
->CursorRow
;
1604 PxeBcDisplayBootItem (Prompt
, PromptLen
);
1606 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, SecCol
+ PromptLen
, SecRow
);
1607 AsciiPrint ("(%d) ", Timeout
--);
1609 while (EFI_ERROR (gBS
->CheckEvent (TimeoutEvent
))) {
1611 if (!EFI_ERROR (gBS
->CheckEvent (DescendEvent
))) {
1612 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, SecCol
+ PromptLen
, SecRow
);
1613 AsciiPrint ("(%d) ", Timeout
--);
1616 if (gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &InputKey
) == EFI_NOT_READY
) {
1618 gBS
->Stall (10 * TICKS_PER_MS
);
1622 if (InputKey
.ScanCode
== 0) {
1624 switch (InputKey
.UnicodeChar
) {
1626 Status
= EFI_ABORTED
;
1632 Status
= EFI_TIMEOUT
;
1640 switch (InputKey
.ScanCode
) {
1642 Status
= EFI_TIMEOUT
;
1646 Status
= EFI_ABORTED
;
1657 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0 , SecRow
+ 1);
1661 if (DescendEvent
!= NULL
) {
1662 gBS
->CloseEvent (DescendEvent
);
1665 if (TimeoutEvent
!= NULL
) {
1666 gBS
->CloseEvent (TimeoutEvent
);
1674 Select the boot menu.
1676 @param Private Pointer to PxeBc private data.
1677 @param Type The type of the menu.
1678 @param UseDefaultItem Use default item or not.
1680 @retval EFI_ABORTED User cancel operation.
1681 @retval EFI_SUCCESS Select the boot menu success.
1682 @retval EFI_NOT_READY Read the input key from the keybroad has not finish.
1686 PxeBcSelectBootMenu (
1687 IN PXEBC_PRIVATE_DATA
*Private
,
1689 IN BOOLEAN UseDefaultItem
1692 PXEBC_CACHED_DHCP4_PACKET
*Packet
;
1693 PXEBC_VENDOR_OPTION
*VendorOpt
;
1694 EFI_INPUT_KEY InputKey
;
1703 PXEBC_BOOT_MENU_ENTRY
*MenuItem
;
1704 PXEBC_BOOT_MENU_ENTRY
*MenuArray
[PXEBC_MAX_MENU_NUM
];
1711 if (Private
->PxeBc
.Mode
->ProxyOfferReceived
) {
1713 Packet
= &Private
->ProxyOffer
;
1716 Packet
= &Private
->Dhcp4Ack
;
1719 ASSERT (Packet
->OfferType
== DHCP4_PACKET_TYPE_PXE10
);
1721 VendorOpt
= &Packet
->PxeVendorOption
;
1723 if (!IS_VALID_BOOT_MENU (VendorOpt
->BitMap
)) {
1727 SetMem (Blank
, sizeof(Blank
), ' ');
1729 MenuSize
= VendorOpt
->BootMenuLen
;
1730 MenuItem
= VendorOpt
->BootMenu
;
1732 if (MenuSize
== 0) {
1733 return EFI_NOT_READY
;
1736 while (MenuSize
> 0) {
1737 MenuArray
[Index
++] = MenuItem
;
1738 MenuSize
= (UINT8
) (MenuSize
- (MenuItem
->DescLen
+ 3));
1739 MenuItem
= (PXEBC_BOOT_MENU_ENTRY
*) ((UINT8
*) MenuItem
+ MenuItem
->DescLen
+ 3);
1740 if (Index
>= PXEBC_MAX_MENU_NUM
) {
1745 if (UseDefaultItem
) {
1746 *Type
= MenuArray
[0]->Type
;
1747 *Type
= NTOHS (*Type
);
1753 for (Index
= 0; Index
< MenuNum
; Index
++) {
1754 PxeBcDisplayBootItem (MenuArray
[Index
]->DescStr
, MenuArray
[Index
]->DescLen
);
1757 TopRow
= gST
->ConOut
->Mode
->CursorRow
- MenuNum
;
1760 ASSERT (Select
< PXEBC_MAX_MENU_NUM
);
1762 // highlight selected row
1764 gST
->ConOut
->SetAttribute (gST
->ConOut
, EFI_TEXT_ATTR (EFI_BLACK
, EFI_LIGHTGRAY
));
1765 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, TopRow
+ Select
);
1766 Blank
[MenuArray
[Select
]->DescLen
] = 0;
1767 AsciiPrint ("%a\r", Blank
);
1768 PxeBcDisplayBootItem (MenuArray
[Select
]->DescStr
, MenuArray
[Select
]->DescLen
);
1769 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, TopRow
+ MenuNum
);
1770 LastSelect
= Select
;
1772 while (gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &InputKey
) == EFI_NOT_READY
) {
1773 gBS
->Stall (10 * TICKS_PER_MS
);
1776 if (InputKey
.ScanCode
!= 0) {
1777 switch (InputKey
.UnicodeChar
) {
1779 InputKey
.ScanCode
= SCAN_ESC
;
1782 case CTRL ('j'): /* linefeed */
1783 case CTRL ('m'): /* return */
1787 case CTRL ('i'): /* tab */
1791 InputKey
.ScanCode
= SCAN_DOWN
;
1794 case CTRL ('h'): /* backspace */
1797 InputKey
.ScanCode
= SCAN_UP
;
1801 InputKey
.ScanCode
= 0;
1805 switch (InputKey
.ScanCode
) {
1816 if (++Select
== MenuNum
) {
1827 case SCAN_PAGE_DOWN
:
1829 Select
= (UINT16
) (MenuNum
- 1);
1836 /* unhighlight last selected row */
1837 gST
->ConOut
->SetAttribute (gST
->ConOut
, EFI_TEXT_ATTR (EFI_LIGHTGRAY
, EFI_BLACK
));
1838 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, TopRow
+ LastSelect
);
1839 Blank
[MenuArray
[LastSelect
]->DescLen
] = 0;
1840 AsciiPrint ("%a\r", Blank
);
1841 PxeBcDisplayBootItem (MenuArray
[LastSelect
]->DescStr
, MenuArray
[LastSelect
]->DescLen
);
1842 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, TopRow
+ MenuNum
);
1845 ASSERT (Select
< PXEBC_MAX_MENU_NUM
);
1848 // Swap the byte order
1850 CopyMem (Type
, &MenuArray
[Select
]->Type
, sizeof (UINT16
));
1851 *Type
= NTOHS (*Type
);