2 Functions implementation related with DHCPv4 for UefiPxeBc Driver.
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "PxeBcImpl.h"
13 // This is a map from the interested DHCP4 option tags' index to the tag value.
15 UINT8 mInterestedDhcp4Tags
[PXEBC_DHCP4_TAG_INDEX_MAX
] = {
16 DHCP4_TAG_BOOTFILE_LEN
,
21 DHCP4_TAG_VENDOR_CLASS_ID
,
26 // There are 4 times retries with the value of 4, 8, 16 and 32, refers to PXE2.1 spec.
28 UINT32 mPxeDhcpTimeout
[4] = {4, 8, 16, 32};
32 Parse a certain dhcp4 option by OptTag in Buffer, and return with start pointer.
34 @param[in] Buffer Pointer to the option buffer.
35 @param[in] Length Length of the option buffer.
36 @param[in] OptTag Tag of the required option.
38 @retval NULL Failed to find the required option.
39 @retval Others The position of the required option.
42 EFI_DHCP4_PACKET_OPTION
*
43 PxeBcParseDhcp4Options (
49 EFI_DHCP4_PACKET_OPTION
*Option
;
52 Option
= (EFI_DHCP4_PACKET_OPTION
*) Buffer
;
55 while (Offset
< Length
&& Option
->OpCode
!= DHCP4_TAG_EOP
) {
57 if (Option
->OpCode
== OptTag
) {
59 // Found the required option.
65 // Skip the current option to the next.
67 if (Option
->OpCode
== DHCP4_TAG_PAD
) {
70 Offset
+= Option
->Length
+ 2;
73 Option
= (EFI_DHCP4_PACKET_OPTION
*) (Buffer
+ Offset
);
81 Parse the PXE vender options and extract the information from them.
83 @param[in] Dhcp4Option Pointer to vendor options in buffer.
84 @param[in] VendorOption Pointer to structure to store information in vendor options.
88 PxeBcParseVendorOptions (
89 IN EFI_DHCP4_PACKET_OPTION
*Dhcp4Option
,
90 IN PXEBC_VENDOR_OPTION
*VendorOption
94 UINT8 VendorOptionLen
;
95 EFI_DHCP4_PACKET_OPTION
*PxeOption
;
98 BitMap
= VendorOption
->BitMap
;
99 VendorOptionLen
= Dhcp4Option
->Length
;
100 PxeOption
= (EFI_DHCP4_PACKET_OPTION
*) &Dhcp4Option
->Data
[0];
103 ASSERT (PxeOption
!= NULL
);
105 while ((Offset
< VendorOptionLen
) && (PxeOption
->OpCode
!= DHCP4_TAG_EOP
)) {
107 // Parse all the interesting PXE vendor options one by one.
109 switch (PxeOption
->OpCode
) {
111 case PXEBC_VENDOR_TAG_MTFTP_IP
:
113 CopyMem (&VendorOption
->MtftpIp
, PxeOption
->Data
, sizeof (EFI_IPv4_ADDRESS
));
116 case PXEBC_VENDOR_TAG_MTFTP_CPORT
:
118 CopyMem (&VendorOption
->MtftpCPort
, PxeOption
->Data
, sizeof (VendorOption
->MtftpCPort
));
121 case PXEBC_VENDOR_TAG_MTFTP_SPORT
:
123 CopyMem (&VendorOption
->MtftpSPort
, PxeOption
->Data
, sizeof (VendorOption
->MtftpSPort
));
126 case PXEBC_VENDOR_TAG_MTFTP_TIMEOUT
:
128 VendorOption
->MtftpTimeout
= *PxeOption
->Data
;
131 case PXEBC_VENDOR_TAG_MTFTP_DELAY
:
133 VendorOption
->MtftpDelay
= *PxeOption
->Data
;
136 case PXEBC_VENDOR_TAG_DISCOVER_CTRL
:
138 VendorOption
->DiscoverCtrl
= *PxeOption
->Data
;
141 case PXEBC_VENDOR_TAG_DISCOVER_MCAST
:
143 CopyMem (&VendorOption
->DiscoverMcastIp
, PxeOption
->Data
, sizeof (EFI_IPv4_ADDRESS
));
146 case PXEBC_VENDOR_TAG_BOOT_SERVERS
:
148 VendorOption
->BootSvrLen
= PxeOption
->Length
;
149 VendorOption
->BootSvr
= (PXEBC_BOOT_SVR_ENTRY
*) PxeOption
->Data
;
152 case PXEBC_VENDOR_TAG_BOOT_MENU
:
154 VendorOption
->BootMenuLen
= PxeOption
->Length
;
155 VendorOption
->BootMenu
= (PXEBC_BOOT_MENU_ENTRY
*) PxeOption
->Data
;
158 case PXEBC_VENDOR_TAG_MENU_PROMPT
:
160 VendorOption
->MenuPromptLen
= PxeOption
->Length
;
161 VendorOption
->MenuPrompt
= (PXEBC_MENU_PROMPT
*) PxeOption
->Data
;
164 case PXEBC_VENDOR_TAG_MCAST_ALLOC
:
166 CopyMem (&VendorOption
->McastIpBase
, PxeOption
->Data
, sizeof (EFI_IPv4_ADDRESS
));
167 CopyMem (&VendorOption
->McastIpBlock
, PxeOption
->Data
+ 4, sizeof (VendorOption
->McastIpBlock
));
168 CopyMem (&VendorOption
->McastIpRange
, PxeOption
->Data
+ 6, sizeof (VendorOption
->McastIpRange
));
171 case PXEBC_VENDOR_TAG_CREDENTIAL_TYPES
:
173 VendorOption
->CredTypeLen
= PxeOption
->Length
;
174 VendorOption
->CredType
= (UINT32
*) PxeOption
->Data
;
177 case PXEBC_VENDOR_TAG_BOOT_ITEM
:
179 CopyMem (&VendorOption
->BootSrvType
, PxeOption
->Data
, sizeof (VendorOption
->BootSrvType
));
180 CopyMem (&VendorOption
->BootSrvLayer
, PxeOption
->Data
+ 2, sizeof (VendorOption
->BootSrvLayer
));
185 // Not interesting PXE vendor options.
191 // Set the bit map for the special PXE options.
193 SET_VENDOR_OPTION_BIT_MAP (BitMap
, PxeOption
->OpCode
);
196 // Continue to the next option.
198 if (PxeOption
->OpCode
== DHCP4_TAG_PAD
) {
201 Offset
= (UINT8
) (Offset
+ PxeOption
->Length
+ 2);
204 PxeOption
= (EFI_DHCP4_PACKET_OPTION
*) (Dhcp4Option
->Data
+ Offset
);
210 Build the options buffer for the DHCPv4 request packet.
212 @param[in] Private Pointer to PxeBc private data.
213 @param[out] OptList Pointer to the option pointer array.
214 @param[in] Buffer Pointer to the buffer to contain the option list.
215 @param[in] NeedMsgType If TRUE, it is necessary to include the Msg type option.
216 Otherwise, it is not necessary.
218 @return Index The count of the built-in options.
222 PxeBcBuildDhcp4Options (
223 IN PXEBC_PRIVATE_DATA
*Private
,
224 OUT EFI_DHCP4_PACKET_OPTION
**OptList
,
226 IN BOOLEAN NeedMsgType
230 PXEBC_DHCP4_OPTION_ENTRY OptEnt
;
234 OptList
[0] = (EFI_DHCP4_PACKET_OPTION
*) Buffer
;
238 // Append message type.
240 OptList
[Index
]->OpCode
= DHCP4_TAG_MSG_TYPE
;
241 OptList
[Index
]->Length
= 1;
242 OptEnt
.Mesg
= (PXEBC_DHCP4_OPTION_MESG
*) OptList
[Index
]->Data
;
243 OptEnt
.Mesg
->Type
= PXEBC_DHCP4_MSG_TYPE_REQUEST
;
245 OptList
[Index
] = GET_NEXT_DHCP_OPTION (OptList
[Index
- 1]);
248 // Append max message size.
250 OptList
[Index
]->OpCode
= DHCP4_TAG_MAXMSG
;
251 OptList
[Index
]->Length
= (UINT8
) sizeof (PXEBC_DHCP4_OPTION_MAX_MESG_SIZE
);
252 OptEnt
.MaxMesgSize
= (PXEBC_DHCP4_OPTION_MAX_MESG_SIZE
*) OptList
[Index
]->Data
;
253 Value
= NTOHS (PXEBC_DHCP4_PACKET_MAX_SIZE
);
254 CopyMem (&OptEnt
.MaxMesgSize
->Size
, &Value
, sizeof (UINT16
));
256 OptList
[Index
] = GET_NEXT_DHCP_OPTION (OptList
[Index
- 1]);
260 // Append parameter request list option.
262 OptList
[Index
]->OpCode
= DHCP4_TAG_PARA_LIST
;
263 OptList
[Index
]->Length
= 35;
264 OptEnt
.Para
= (PXEBC_DHCP4_OPTION_PARA
*) OptList
[Index
]->Data
;
265 OptEnt
.Para
->ParaList
[0] = DHCP4_TAG_NETMASK
;
266 OptEnt
.Para
->ParaList
[1] = DHCP4_TAG_TIME_OFFSET
;
267 OptEnt
.Para
->ParaList
[2] = DHCP4_TAG_ROUTER
;
268 OptEnt
.Para
->ParaList
[3] = DHCP4_TAG_TIME_SERVER
;
269 OptEnt
.Para
->ParaList
[4] = DHCP4_TAG_NAME_SERVER
;
270 OptEnt
.Para
->ParaList
[5] = DHCP4_TAG_DNS_SERVER
;
271 OptEnt
.Para
->ParaList
[6] = DHCP4_TAG_HOSTNAME
;
272 OptEnt
.Para
->ParaList
[7] = DHCP4_TAG_BOOTFILE_LEN
;
273 OptEnt
.Para
->ParaList
[8] = DHCP4_TAG_DOMAINNAME
;
274 OptEnt
.Para
->ParaList
[9] = DHCP4_TAG_ROOTPATH
;
275 OptEnt
.Para
->ParaList
[10] = DHCP4_TAG_EXTEND_PATH
;
276 OptEnt
.Para
->ParaList
[11] = DHCP4_TAG_EMTU
;
277 OptEnt
.Para
->ParaList
[12] = DHCP4_TAG_TTL
;
278 OptEnt
.Para
->ParaList
[13] = DHCP4_TAG_BROADCAST
;
279 OptEnt
.Para
->ParaList
[14] = DHCP4_TAG_NIS_DOMAIN
;
280 OptEnt
.Para
->ParaList
[15] = DHCP4_TAG_NIS_SERVER
;
281 OptEnt
.Para
->ParaList
[16] = DHCP4_TAG_NTP_SERVER
;
282 OptEnt
.Para
->ParaList
[17] = DHCP4_TAG_VENDOR
;
283 OptEnt
.Para
->ParaList
[18] = DHCP4_TAG_REQUEST_IP
;
284 OptEnt
.Para
->ParaList
[19] = DHCP4_TAG_LEASE
;
285 OptEnt
.Para
->ParaList
[20] = DHCP4_TAG_SERVER_ID
;
286 OptEnt
.Para
->ParaList
[21] = DHCP4_TAG_T1
;
287 OptEnt
.Para
->ParaList
[22] = DHCP4_TAG_T2
;
288 OptEnt
.Para
->ParaList
[23] = DHCP4_TAG_VENDOR_CLASS_ID
;
289 OptEnt
.Para
->ParaList
[24] = DHCP4_TAG_TFTP
;
290 OptEnt
.Para
->ParaList
[25] = DHCP4_TAG_BOOTFILE
;
291 OptEnt
.Para
->ParaList
[26] = DHCP4_TAG_UUID
;
292 OptEnt
.Para
->ParaList
[27] = 0x80;
293 OptEnt
.Para
->ParaList
[28] = 0x81;
294 OptEnt
.Para
->ParaList
[29] = 0x82;
295 OptEnt
.Para
->ParaList
[30] = 0x83;
296 OptEnt
.Para
->ParaList
[31] = 0x84;
297 OptEnt
.Para
->ParaList
[32] = 0x85;
298 OptEnt
.Para
->ParaList
[33] = 0x86;
299 OptEnt
.Para
->ParaList
[34] = 0x87;
301 OptList
[Index
] = GET_NEXT_DHCP_OPTION (OptList
[Index
- 1]);
304 // Append UUID/Guid-based client identifier option
306 OptList
[Index
]->OpCode
= DHCP4_TAG_UUID
;
307 OptList
[Index
]->Length
= (UINT8
) sizeof (PXEBC_DHCP4_OPTION_UUID
);
308 OptEnt
.Uuid
= (PXEBC_DHCP4_OPTION_UUID
*) OptList
[Index
]->Data
;
309 OptEnt
.Uuid
->Type
= 0;
311 OptList
[Index
] = GET_NEXT_DHCP_OPTION (OptList
[Index
- 1]);
313 if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID
*) OptEnt
.Uuid
->Guid
))) {
315 // Zero the Guid to indicate NOT programable if failed to get system Guid.
317 DEBUG ((EFI_D_WARN
, "PXE: Failed to read system GUID from the smbios table!\n"));
318 ZeroMem (OptEnt
.Uuid
->Guid
, sizeof (EFI_GUID
));
322 // Append client network device interface option
324 OptList
[Index
]->OpCode
= DHCP4_TAG_UNDI
;
325 OptList
[Index
]->Length
= (UINT8
) sizeof (PXEBC_DHCP4_OPTION_UNDI
);
326 OptEnt
.Undi
= (PXEBC_DHCP4_OPTION_UNDI
*) OptList
[Index
]->Data
;
328 if (Private
->Nii
!= NULL
) {
329 OptEnt
.Undi
->Type
= Private
->Nii
->Type
;
330 OptEnt
.Undi
->MajorVer
= Private
->Nii
->MajorVer
;
331 OptEnt
.Undi
->MinorVer
= Private
->Nii
->MinorVer
;
333 OptEnt
.Undi
->Type
= DEFAULT_UNDI_TYPE
;
334 OptEnt
.Undi
->MajorVer
= DEFAULT_UNDI_MAJOR
;
335 OptEnt
.Undi
->MinorVer
= DEFAULT_UNDI_MINOR
;
339 OptList
[Index
] = GET_NEXT_DHCP_OPTION (OptList
[Index
- 1]);
342 // Append client system architecture option
344 OptList
[Index
]->OpCode
= DHCP4_TAG_ARCH
;
345 OptList
[Index
]->Length
= (UINT8
) sizeof (PXEBC_DHCP4_OPTION_ARCH
);
346 OptEnt
.Arch
= (PXEBC_DHCP4_OPTION_ARCH
*) OptList
[Index
]->Data
;
347 Value
= HTONS (EFI_PXE_CLIENT_SYSTEM_ARCHITECTURE
);
348 CopyMem (&OptEnt
.Arch
->Type
, &Value
, sizeof (UINT16
));
350 OptList
[Index
] = GET_NEXT_DHCP_OPTION (OptList
[Index
- 1]);
353 // Append vendor class identify option
355 OptList
[Index
]->OpCode
= DHCP4_TAG_VENDOR_CLASS_ID
;
356 OptList
[Index
]->Length
= (UINT8
) sizeof (PXEBC_DHCP4_OPTION_CLID
);
357 OptEnt
.Clid
= (PXEBC_DHCP4_OPTION_CLID
*) OptList
[Index
]->Data
;
360 DEFAULT_CLASS_ID_DATA
,
361 sizeof (PXEBC_DHCP4_OPTION_CLID
)
363 PxeBcUintnToAscDecWithFormat (
364 EFI_PXE_CLIENT_SYSTEM_ARCHITECTURE
,
365 OptEnt
.Clid
->ArchitectureType
,
366 sizeof (OptEnt
.Clid
->ArchitectureType
)
369 if (Private
->Nii
!= NULL
) {
370 CopyMem (OptEnt
.Clid
->InterfaceName
, Private
->Nii
->StringId
, sizeof (OptEnt
.Clid
->InterfaceName
));
371 PxeBcUintnToAscDecWithFormat (Private
->Nii
->MajorVer
, OptEnt
.Clid
->UndiMajor
, sizeof (OptEnt
.Clid
->UndiMajor
));
372 PxeBcUintnToAscDecWithFormat (Private
->Nii
->MinorVer
, OptEnt
.Clid
->UndiMinor
, sizeof (OptEnt
.Clid
->UndiMinor
));
382 Create a template DHCPv4 packet as a seed.
384 @param[out] Seed Pointer to the seed packet.
385 @param[in] Udp4 Pointer to EFI_UDP4_PROTOCOL.
389 PxeBcSeedDhcp4Packet (
390 OUT EFI_DHCP4_PACKET
*Seed
,
391 IN EFI_UDP4_PROTOCOL
*Udp4
394 EFI_SIMPLE_NETWORK_MODE Mode
;
395 EFI_DHCP4_HEADER
*Header
;
398 // Get IfType and HwAddressSize from SNP mode data.
400 Udp4
->GetModeData (Udp4
, NULL
, NULL
, NULL
, &Mode
);
402 Seed
->Size
= sizeof (EFI_DHCP4_PACKET
);
403 Seed
->Length
= sizeof (Seed
->Dhcp4
);
404 Header
= &Seed
->Dhcp4
.Header
;
405 ZeroMem (Header
, sizeof (EFI_DHCP4_HEADER
));
406 Header
->OpCode
= PXEBC_DHCP4_OPCODE_REQUEST
;
407 Header
->HwType
= Mode
.IfType
;
408 Header
->HwAddrLen
= (UINT8
) Mode
.HwAddressSize
;
409 CopyMem (Header
->ClientHwAddr
, &Mode
.CurrentAddress
, Header
->HwAddrLen
);
411 Seed
->Dhcp4
.Magik
= PXEBC_DHCP4_MAGIC
;
412 Seed
->Dhcp4
.Option
[0] = DHCP4_TAG_EOP
;
417 Cache the DHCPv4 packet.
419 @param[in] Dst Pointer to the cache buffer for DHCPv4 packet.
420 @param[in] Src Pointer to the DHCPv4 packet to be cached.
422 @retval EFI_SUCCESS Packet is copied.
423 @retval EFI_BUFFER_TOO_SMALL Cache buffer is not big enough to hold the packet.
427 PxeBcCacheDhcp4Packet (
428 IN EFI_DHCP4_PACKET
*Dst
,
429 IN EFI_DHCP4_PACKET
*Src
432 if (Dst
->Size
< Src
->Length
) {
433 return EFI_BUFFER_TOO_SMALL
;
436 CopyMem (&Dst
->Dhcp4
, &Src
->Dhcp4
, Src
->Length
);
437 Dst
->Length
= Src
->Length
;
444 Parse the cached DHCPv4 packet, including all the options.
446 @param[in] Cache4 Pointer to cached DHCPv4 packet.
448 @retval EFI_SUCCESS Parsed the DHCPv4 packet successfully.
449 @retval EFI_DEVICE_ERROR Failed to parse and invalid packet.
453 PxeBcParseDhcp4Packet (
454 IN PXEBC_DHCP4_PACKET_CACHE
*Cache4
457 EFI_DHCP4_PACKET
*Offer
;
458 EFI_DHCP4_PACKET_OPTION
**Options
;
459 EFI_DHCP4_PACKET_OPTION
*Option
;
460 PXEBC_OFFER_TYPE OfferType
;
462 BOOLEAN IsProxyOffer
;
465 BOOLEAN FileFieldOverloaded
;
467 IsProxyOffer
= FALSE
;
469 FileFieldOverloaded
= FALSE
;
471 ZeroMem (Cache4
->OptList
, sizeof (Cache4
->OptList
));
472 ZeroMem (&Cache4
->VendorOpt
, sizeof (Cache4
->VendorOpt
));
474 Offer
= &Cache4
->Packet
.Offer
;
475 Options
= Cache4
->OptList
;
478 // Parse DHCPv4 options in this offer, and store the pointers.
479 // First, try to parse DHCPv4 options from the DHCP optional parameters field.
481 for (Index
= 0; Index
< PXEBC_DHCP4_TAG_INDEX_MAX
; Index
++) {
482 Options
[Index
] = PxeBcParseDhcp4Options (
484 GET_OPTION_BUFFER_LEN (Offer
),
485 mInterestedDhcp4Tags
[Index
]
489 // Second, Check if bootfilename and serverhostname is overloaded to carry DHCP options refers to rfc-2132.
490 // If yes, try to parse options from the BootFileName field, then ServerName field.
492 Option
= Options
[PXEBC_DHCP4_TAG_INDEX_OVERLOAD
];
493 if (Option
!= NULL
) {
494 if ((Option
->Data
[0] & PXEBC_DHCP4_OVERLOAD_FILE
) != 0) {
495 FileFieldOverloaded
= TRUE
;
496 for (Index
= 0; Index
< PXEBC_DHCP4_TAG_INDEX_MAX
; Index
++) {
497 if (Options
[Index
] == NULL
) {
498 Options
[Index
] = PxeBcParseDhcp4Options (
499 (UINT8
*) Offer
->Dhcp4
.Header
.BootFileName
,
500 sizeof (Offer
->Dhcp4
.Header
.BootFileName
),
501 mInterestedDhcp4Tags
[Index
]
506 if ((Option
->Data
[0] & PXEBC_DHCP4_OVERLOAD_SERVER_NAME
) != 0) {
507 for (Index
= 0; Index
< PXEBC_DHCP4_TAG_INDEX_MAX
; Index
++) {
508 if (Options
[Index
] == NULL
) {
509 Options
[Index
] = PxeBcParseDhcp4Options (
510 (UINT8
*) Offer
->Dhcp4
.Header
.ServerName
,
511 sizeof (Offer
->Dhcp4
.Header
.ServerName
),
512 mInterestedDhcp4Tags
[Index
]
520 // The offer with zero "yiaddr" is a proxy offer.
522 if (Offer
->Dhcp4
.Header
.YourAddr
.Addr
[0] == 0) {
527 // The offer with "PXEClient" is a PXE offer.
529 Option
= Options
[PXEBC_DHCP4_TAG_INDEX_CLASS_ID
];
530 if ((Option
!= NULL
) && (Option
->Length
>= 9) &&
531 (CompareMem (Option
->Data
, DEFAULT_CLASS_ID_DATA
, 9) == 0)) {
536 // Parse PXE vendor options in this offer, and store the contents/pointers.
538 Option
= Options
[PXEBC_DHCP4_TAG_INDEX_VENDOR
];
539 if (IsPxeOffer
&& Option
!= NULL
) {
540 PxeBcParseVendorOptions (Option
, &Cache4
->VendorOpt
);
544 // Parse PXE boot file name:
545 // According to PXE spec, boot file name should be read from DHCP option 67 (bootfile name) if present.
546 // Otherwise, read from boot file field in DHCP header.
548 if (Options
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] != NULL
) {
550 // RFC 2132, Section 9.5 does not strictly state Bootfile name (option 67) is null
551 // terminated string. So force to append null terminated character at the end of string.
553 Ptr8
= (UINT8
*)&Options
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
]->Data
[0];
554 Ptr8
+= Options
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
]->Length
;
555 if (*(Ptr8
- 1) != '\0') {
558 } else if (!FileFieldOverloaded
&& Offer
->Dhcp4
.Header
.BootFileName
[0] != 0) {
560 // If the bootfile is not present and bootfilename is present in DHCPv4 packet, just parse it.
561 // Do not count dhcp option header here, or else will destroy the serverhostname.
563 Options
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] = (EFI_DHCP4_PACKET_OPTION
*)
564 (&Offer
->Dhcp4
.Header
.BootFileName
[0] -
565 OFFSET_OF (EFI_DHCP4_PACKET_OPTION
, Data
[0]));
570 // Determine offer type of the DHCPv4 packet.
572 Option
= Options
[PXEBC_DHCP4_TAG_INDEX_MSG_TYPE
];
573 if (Option
== NULL
|| Option
->Data
[0] == 0) {
575 // It's a Bootp offer.
577 OfferType
= PxeOfferTypeBootp
;
579 Option
= Cache4
->OptList
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
];
580 if (Option
== NULL
) {
582 // If the Bootp offer without bootfilename, discard it.
584 return EFI_DEVICE_ERROR
;
588 if (IS_VALID_DISCOVER_VENDOR_OPTION (Cache4
->VendorOpt
.BitMap
)) {
590 // It's a PXE10 offer with PXEClient and discover vendor option.
592 OfferType
= IsProxyOffer
? PxeOfferTypeProxyPxe10
: PxeOfferTypeDhcpPxe10
;
593 } else if (IS_VALID_MTFTP_VENDOR_OPTION (Cache4
->VendorOpt
.BitMap
)) {
595 // It's a WFM11a offer with PXEClient and mtftp vendor option.
596 // But multi-cast download is not supported currently, so discard it.
598 return EFI_DEVICE_ERROR
;
599 } else if (IsPxeOffer
) {
601 // It's a BINL offer only with PXEClient.
603 OfferType
= IsProxyOffer
? PxeOfferTypeProxyBinl
: PxeOfferTypeDhcpBinl
;
606 // It's a DHCPv4 only offer, which is a pure DHCPv4 offer packet.
608 OfferType
= PxeOfferTypeDhcpOnly
;
612 Cache4
->OfferType
= OfferType
;
619 Cache the DHCPv4 ack packet, and parse it on demand.
621 @param[in] Private Pointer to PxeBc private data.
622 @param[in] Ack Pointer to the DHCPv4 ack packet.
623 @param[in] Verified If TRUE, parse the ACK packet and store info into mode data.
625 @retval EFI_SUCCESS Cache and parse the packet successfully.
626 @retval EFI_BUFFER_TOO_SMALL Cache buffer is not big enough to hold the packet.
631 IN PXEBC_PRIVATE_DATA
*Private
,
632 IN EFI_DHCP4_PACKET
*Ack
,
636 EFI_PXE_BASE_CODE_MODE
*Mode
;
639 Mode
= Private
->PxeBc
.Mode
;
641 Status
= PxeBcCacheDhcp4Packet (&Private
->DhcpAck
.Dhcp4
.Packet
.Ack
, Ack
);
642 if (EFI_ERROR (Status
)) {
648 // Parse the ack packet and store it into mode data if needed.
650 PxeBcParseDhcp4Packet (&Private
->DhcpAck
.Dhcp4
);
651 CopyMem (&Mode
->DhcpAck
.Dhcpv4
, &Ack
->Dhcp4
, Ack
->Length
);
652 Mode
->DhcpAckReceived
= TRUE
;
660 Cache the DHCPv4 proxy offer packet according to the received order.
662 @param[in] Private Pointer to PxeBc private data.
663 @param[in] OfferIndex The received order of offer packets.
665 @retval EFI_SUCCESS Cache and parse the packet successfully.
666 @retval EFI_BUFFER_TOO_SMALL Cache buffer is not big enough to hold the packet.
670 PxeBcCopyProxyOffer (
671 IN PXEBC_PRIVATE_DATA
*Private
,
675 EFI_PXE_BASE_CODE_MODE
*Mode
;
676 EFI_DHCP4_PACKET
*Offer
;
679 ASSERT (OfferIndex
< Private
->OfferNum
);
680 ASSERT (OfferIndex
< PXEBC_OFFER_MAX_NUM
);
682 Mode
= Private
->PxeBc
.Mode
;
683 Offer
= &Private
->OfferBuffer
[OfferIndex
].Dhcp4
.Packet
.Offer
;
686 // Cache the proxy offer packet and parse it.
688 Status
= PxeBcCacheDhcp4Packet (&Private
->ProxyOffer
.Dhcp4
.Packet
.Offer
, Offer
);
689 if (EFI_ERROR(Status
)) {
693 PxeBcParseDhcp4Packet (&Private
->ProxyOffer
.Dhcp4
);
696 // Store this packet into mode data.
698 CopyMem (&Mode
->ProxyOffer
.Dhcpv4
, &Offer
->Dhcp4
, Offer
->Length
);
699 Mode
->ProxyOfferReceived
= TRUE
;
706 Retry to request bootfile name by the BINL offer.
708 @param[in] Private Pointer to PxeBc private data.
709 @param[in] Index The received order of offer packets.
711 @retval EFI_SUCCESS Successfully retried to request bootfile name.
712 @retval EFI_DEVICE_ERROR Failed to retry bootfile name.
716 PxeBcRetryBinlOffer (
717 IN PXEBC_PRIVATE_DATA
*Private
,
721 EFI_DHCP4_PACKET
*Offer
;
722 EFI_IP_ADDRESS ServerIp
;
724 PXEBC_DHCP4_PACKET_CACHE
*Cache4
;
725 EFI_DHCP4_PACKET
*Reply
;
727 ASSERT (Index
< PXEBC_OFFER_MAX_NUM
);
728 ASSERT (Private
->OfferBuffer
[Index
].Dhcp4
.OfferType
== PxeOfferTypeDhcpBinl
||
729 Private
->OfferBuffer
[Index
].Dhcp4
.OfferType
== PxeOfferTypeProxyBinl
);
731 Offer
= &Private
->OfferBuffer
[Index
].Dhcp4
.Packet
.Offer
;
734 // Prefer to siaddr in header as next server address. If it's zero, then use option 54.
736 if (Offer
->Dhcp4
.Header
.ServerAddr
.Addr
[0] == 0) {
739 Private
->OfferBuffer
[Index
].Dhcp4
.OptList
[PXEBC_DHCP4_TAG_INDEX_SERVER_ID
]->Data
,
740 sizeof (EFI_IPv4_ADDRESS
)
745 &Offer
->Dhcp4
.Header
.ServerAddr
,
746 sizeof (EFI_IPv4_ADDRESS
)
750 Private
->IsDoDiscover
= FALSE
;
751 Cache4
= &Private
->ProxyOffer
.Dhcp4
;
752 Reply
= &Cache4
->Packet
.Offer
;
755 // Send another request packet for bootfile name.
757 Status
= PxeBcDhcp4Discover (
766 if (EFI_ERROR (Status
)) {
771 // Parse the reply for the last request packet.
773 Status
= PxeBcParseDhcp4Packet (Cache4
);
774 if (EFI_ERROR (Status
)) {
778 if (Cache4
->OfferType
!= PxeOfferTypeProxyPxe10
&&
779 Cache4
->OfferType
!= PxeOfferTypeProxyWfm11a
&&
780 Cache4
->OptList
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] == NULL
) {
782 // This BINL ack doesn't have discovery option set or multicast option set
783 // or bootfile name specified.
785 return EFI_DEVICE_ERROR
;
789 // Store the reply into mode data.
791 Private
->PxeBc
.Mode
->ProxyOfferReceived
= TRUE
;
792 CopyMem (&Private
->PxeBc
.Mode
->ProxyOffer
.Dhcpv4
, &Reply
->Dhcp4
, Reply
->Length
);
799 Cache all the received DHCPv4 offers, and set OfferIndex and OfferCount.
801 @param[in] Private Pointer to PxeBc private data.
802 @param[in] RcvdOffer Pointer to the received offer packet.
804 @retval EFI_SUCCESS Cache and parse the packet successfully.
805 @retval Others Operation failed.
809 PxeBcCacheDhcp4Offer (
810 IN PXEBC_PRIVATE_DATA
*Private
,
811 IN EFI_DHCP4_PACKET
*RcvdOffer
814 PXEBC_DHCP4_PACKET_CACHE
*Cache4
;
815 EFI_DHCP4_PACKET
*Offer
;
816 PXEBC_OFFER_TYPE OfferType
;
819 ASSERT (Private
->OfferNum
< PXEBC_OFFER_MAX_NUM
);
820 Cache4
= &Private
->OfferBuffer
[Private
->OfferNum
].Dhcp4
;
821 Offer
= &Cache4
->Packet
.Offer
;
824 // Cache the content of DHCPv4 packet firstly.
826 Status
= PxeBcCacheDhcp4Packet (Offer
, RcvdOffer
);
827 if (EFI_ERROR(Status
)) {
832 // Validate the DHCPv4 packet, and parse the options and offer type.
834 if (EFI_ERROR (PxeBcParseDhcp4Packet (Cache4
))) {
839 // Determine whether cache the current offer by type, and record OfferIndex and OfferCount.
841 OfferType
= Cache4
->OfferType
;
842 ASSERT (OfferType
< PxeOfferTypeMax
);
844 if (OfferType
== PxeOfferTypeBootp
) {
846 // It's a Bootp offer, only cache the first one, and discard the others.
848 if (Private
->OfferCount
[OfferType
] == 0) {
849 Private
->OfferIndex
[OfferType
][0] = Private
->OfferNum
;
850 Private
->OfferCount
[OfferType
] = 1;
855 ASSERT (Private
->OfferCount
[OfferType
] < PXEBC_OFFER_MAX_NUM
);
856 if (IS_PROXY_DHCP_OFFER (Offer
)) {
858 // It's a proxy offer without yiaddr, including PXE10, WFM11a or BINL offer.
860 Private
->IsProxyRecved
= TRUE
;
862 if (OfferType
== PxeOfferTypeProxyBinl
) {
864 // Cache all proxy BINL offers.
866 Private
->OfferIndex
[OfferType
][Private
->OfferCount
[OfferType
]] = Private
->OfferNum
;
867 Private
->OfferCount
[OfferType
]++;
868 } else if ((OfferType
== PxeOfferTypeProxyPxe10
|| OfferType
== PxeOfferTypeProxyWfm11a
) &&
869 Private
->OfferCount
[OfferType
] < 1) {
871 // Only cache the first PXE10/WFM11a offer, and discard the others.
873 Private
->OfferIndex
[OfferType
][0] = Private
->OfferNum
;
874 Private
->OfferCount
[OfferType
] = 1;
880 // It's a DHCPv4 offer with yiaddr, and cache them all.
882 Private
->OfferIndex
[OfferType
][Private
->OfferCount
[OfferType
]] = Private
->OfferNum
;
883 Private
->OfferCount
[OfferType
]++;
894 Select an DHCPv4 offer, and record SelectIndex and SelectProxyType.
896 @param[in] Private Pointer to PxeBc private data.
900 PxeBcSelectDhcp4Offer (
901 IN PXEBC_PRIVATE_DATA
*Private
906 EFI_DHCP4_PACKET
*Offer
;
908 Private
->SelectIndex
= 0;
910 if (Private
->IsOfferSorted
) {
912 // Select offer by default policy.
914 if (Private
->OfferCount
[PxeOfferTypeDhcpPxe10
] > 0) {
916 // 1. DhcpPxe10 offer
918 Private
->SelectIndex
= Private
->OfferIndex
[PxeOfferTypeDhcpPxe10
][0] + 1;
920 } else if (Private
->OfferCount
[PxeOfferTypeDhcpWfm11a
] > 0) {
922 // 2. DhcpWfm11a offer
924 Private
->SelectIndex
= Private
->OfferIndex
[PxeOfferTypeDhcpWfm11a
][0] + 1;
926 } else if (Private
->OfferCount
[PxeOfferTypeDhcpOnly
] > 0 &&
927 Private
->OfferCount
[PxeOfferTypeProxyPxe10
] > 0) {
929 // 3. DhcpOnly offer and ProxyPxe10 offer.
931 Private
->SelectIndex
= Private
->OfferIndex
[PxeOfferTypeDhcpOnly
][0] + 1;
932 Private
->SelectProxyType
= PxeOfferTypeProxyPxe10
;
934 } else if (Private
->OfferCount
[PxeOfferTypeDhcpOnly
] > 0 &&
935 Private
->OfferCount
[PxeOfferTypeProxyWfm11a
] > 0) {
937 // 4. DhcpOnly offer and ProxyWfm11a offer.
939 Private
->SelectIndex
= Private
->OfferIndex
[PxeOfferTypeDhcpOnly
][0] + 1;
940 Private
->SelectProxyType
= PxeOfferTypeProxyWfm11a
;
942 } else if (Private
->OfferCount
[PxeOfferTypeDhcpBinl
] > 0) {
944 // 5. DhcpBinl offer.
946 Private
->SelectIndex
= Private
->OfferIndex
[PxeOfferTypeDhcpBinl
][0] + 1;
948 } else if (Private
->OfferCount
[PxeOfferTypeDhcpOnly
] > 0 &&
949 Private
->OfferCount
[PxeOfferTypeProxyBinl
] > 0) {
951 // 6. DhcpOnly offer and ProxyBinl offer.
953 Private
->SelectIndex
= Private
->OfferIndex
[PxeOfferTypeDhcpOnly
][0] + 1;
954 Private
->SelectProxyType
= PxeOfferTypeProxyBinl
;
958 // 7. DhcpOnly offer with bootfilename.
960 for (Index
= 0; Index
< Private
->OfferCount
[PxeOfferTypeDhcpOnly
]; Index
++) {
961 OfferIndex
= Private
->OfferIndex
[PxeOfferTypeDhcpOnly
][Index
];
962 if (Private
->OfferBuffer
[OfferIndex
].Dhcp4
.OptList
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] != NULL
) {
963 Private
->SelectIndex
= OfferIndex
+ 1;
968 // 8. Bootp offer with bootfilename.
970 OfferIndex
= Private
->OfferIndex
[PxeOfferTypeBootp
][0];
971 if (Private
->SelectIndex
== 0 &&
972 Private
->OfferCount
[PxeOfferTypeBootp
] > 0 &&
973 Private
->OfferBuffer
[OfferIndex
].Dhcp4
.OptList
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] != NULL
) {
974 Private
->SelectIndex
= OfferIndex
+ 1;
979 // Select offer by received order.
981 for (Index
= 0; Index
< Private
->OfferNum
; Index
++) {
983 Offer
= &Private
->OfferBuffer
[Index
].Dhcp4
.Packet
.Offer
;
985 if (IS_PROXY_DHCP_OFFER (Offer
)) {
992 if (!Private
->IsProxyRecved
&&
993 Private
->OfferBuffer
[Index
].Dhcp4
.OfferType
== PxeOfferTypeDhcpOnly
&&
994 Private
->OfferBuffer
[Index
].Dhcp4
.OptList
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] == NULL
) {
996 // Skip if DhcpOnly offer without any other proxy offers or bootfilename.
1002 // Record the index of the select offer.
1004 Private
->SelectIndex
= Index
+ 1;
1012 Handle the DHCPv4 offer packet.
1014 @param[in] Private Pointer to PxeBc private data.
1016 @retval EFI_SUCCESS Handled the DHCPv4 offer packet successfully.
1017 @retval EFI_NO_RESPONSE No response to the following request packet.
1018 @retval EFI_NOT_FOUND No boot filename received.
1019 @retval EFI_BUFFER_TOO_SMALL Can't cache the offer pacet.
1023 PxeBcHandleDhcp4Offer (
1024 IN PXEBC_PRIVATE_DATA
*Private
1027 PXEBC_DHCP4_PACKET_CACHE
*Cache4
;
1028 EFI_DHCP4_PACKET_OPTION
**Options
;
1030 EFI_DHCP4_PACKET
*Offer
;
1031 PXEBC_OFFER_TYPE OfferType
;
1035 EFI_PXE_BASE_CODE_MODE
*Mode
;
1036 EFI_DHCP4_PACKET
*Ack
;
1038 ASSERT (Private
->SelectIndex
> 0);
1039 SelectIndex
= (UINT32
) (Private
->SelectIndex
- 1);
1040 ASSERT (SelectIndex
< PXEBC_OFFER_MAX_NUM
);
1041 Cache4
= &Private
->OfferBuffer
[SelectIndex
].Dhcp4
;
1042 Options
= Cache4
->OptList
;
1043 Status
= EFI_SUCCESS
;
1045 if (Cache4
->OfferType
== PxeOfferTypeDhcpBinl
) {
1047 // DhcpBinl offer is selected, so need try to request bootfilename by this offer.
1049 if (EFI_ERROR (PxeBcRetryBinlOffer (Private
, SelectIndex
))) {
1050 Status
= EFI_NO_RESPONSE
;
1052 } else if (Cache4
->OfferType
== PxeOfferTypeDhcpOnly
) {
1054 if (Private
->IsProxyRecved
) {
1056 // DhcpOnly offer is selected, so need try to request bootfile name.
1059 if (Private
->IsOfferSorted
) {
1061 // The proxy offer should be determined if select by default policy.
1062 // IsOfferSorted means all offers are labeled by OfferIndex.
1064 ASSERT (Private
->SelectProxyType
< PxeOfferTypeMax
);
1065 ASSERT (Private
->OfferCount
[Private
->SelectProxyType
] > 0);
1067 if (Private
->SelectProxyType
== PxeOfferTypeProxyBinl
) {
1069 // Try all the cached ProxyBinl offer one by one to request bootfile name.
1071 for (Index
= 0; Index
< Private
->OfferCount
[Private
->SelectProxyType
]; Index
++) {
1072 ASSERT (Index
< PXEBC_OFFER_MAX_NUM
);
1073 ProxyIndex
= Private
->OfferIndex
[Private
->SelectProxyType
][Index
];
1074 if (!EFI_ERROR (PxeBcRetryBinlOffer (Private
, ProxyIndex
))) {
1078 if (Index
== Private
->OfferCount
[Private
->SelectProxyType
]) {
1079 Status
= EFI_NO_RESPONSE
;
1083 // For other proxy offers, only one is buffered.
1085 ProxyIndex
= Private
->OfferIndex
[Private
->SelectProxyType
][0];
1089 // The proxy offer should not be determined if select by received order.
1091 Status
= EFI_NO_RESPONSE
;
1093 for (Index
= 0; Index
< Private
->OfferNum
; Index
++) {
1094 ASSERT (Index
< PXEBC_OFFER_MAX_NUM
);
1095 Offer
= &Private
->OfferBuffer
[Index
].Dhcp4
.Packet
.Offer
;
1096 OfferType
= Private
->OfferBuffer
[Index
].Dhcp4
.OfferType
;
1097 if (!IS_PROXY_DHCP_OFFER (Offer
)) {
1099 // Skip non proxy DHCPv4 offers.
1104 if (OfferType
== PxeOfferTypeProxyBinl
) {
1106 // Try all the cached ProxyBinl offer one by one to request bootfile name.
1108 if (EFI_ERROR (PxeBcRetryBinlOffer (Private
, Index
))) {
1113 Private
->SelectProxyType
= OfferType
;
1115 Status
= EFI_SUCCESS
;
1120 if (!EFI_ERROR (Status
) && Private
->SelectProxyType
!= PxeOfferTypeProxyBinl
) {
1122 // Success to try to request by a ProxyPxe10 or ProxyWfm11a offer, copy and parse it.
1124 Status
= PxeBcCopyProxyOffer (Private
, ProxyIndex
);
1128 // Othewise, the bootfile name must be included in DhcpOnly offer.
1130 if (Options
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] == NULL
) {
1131 Status
= EFI_NOT_FOUND
;
1136 if (!EFI_ERROR (Status
)) {
1138 // All PXE boot information is ready by now.
1140 Mode
= Private
->PxeBc
.Mode
;
1141 Offer
= &Cache4
->Packet
.Offer
;
1142 Ack
= &Private
->DhcpAck
.Dhcp4
.Packet
.Ack
;
1143 if (Cache4
->OfferType
== PxeOfferTypeBootp
) {
1145 // Bootp is a special case that only 2 packets involved instead of 4. So the bootp's reply
1146 // should be taken as ack.
1151 Status
= PxeBcCopyDhcp4Ack (Private
, Ack
, TRUE
);
1152 if (EFI_ERROR (Status
)) {
1155 Mode
->DhcpDiscoverValid
= TRUE
;
1163 EFI_DHCP4_CALLBACK is provided by the consumer of the EFI DHCPv4 Protocol driver
1164 to intercept events that occurred in the configuration process.
1166 @param[in] This Pointer to the EFI DHCPv4 Protocol.
1167 @param[in] Context Pointer to the context set by EFI_DHCP4_PROTOCOL.Configure().
1168 @param[in] CurrentState The current operational state of the EFI DHCPv4 Protocol driver.
1169 @param[in] Dhcp4Event The event that occurs in the current state, which usually means a
1171 @param[in] Packet The DHCPv4 packet that is going to be sent or already received.
1172 @param[out] NewPacket The packet that is used to replace the above Packet.
1174 @retval EFI_SUCCESS Tells the EFI DHCPv4 Protocol driver to continue the DHCP process.
1175 @retval EFI_NOT_READY Only used in the Dhcp4Selecting state. The EFI DHCPv4 Protocol
1176 driver will continue to wait for more DHCPOFFER packets until the
1177 retry timeout expires.
1178 @retval EFI_ABORTED Tells the EFI DHCPv4 Protocol driver to abort the current process
1179 and return to the Dhcp4Init or Dhcp4InitReboot state.
1184 PxeBcDhcp4CallBack (
1185 IN EFI_DHCP4_PROTOCOL
*This
,
1187 IN EFI_DHCP4_STATE CurrentState
,
1188 IN EFI_DHCP4_EVENT Dhcp4Event
,
1189 IN EFI_DHCP4_PACKET
*Packet OPTIONAL
,
1190 OUT EFI_DHCP4_PACKET
**NewPacket OPTIONAL
1193 PXEBC_PRIVATE_DATA
*Private
;
1194 EFI_PXE_BASE_CODE_MODE
*Mode
;
1195 EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL
*Callback
;
1196 EFI_DHCP4_PACKET_OPTION
*MaxMsgSize
;
1201 if ((Dhcp4Event
!= Dhcp4RcvdOffer
) &&
1202 (Dhcp4Event
!= Dhcp4SelectOffer
) &&
1203 (Dhcp4Event
!= Dhcp4SendDiscover
) &&
1204 (Dhcp4Event
!= Dhcp4RcvdAck
)) {
1208 ASSERT (Packet
!= NULL
);
1210 Private
= (PXEBC_PRIVATE_DATA
*) Context
;
1211 Mode
= Private
->PxeBc
.Mode
;
1212 Callback
= Private
->PxeBcCallback
;
1215 // Override the Maximum DHCP Message Size.
1217 MaxMsgSize
= PxeBcParseDhcp4Options (
1218 Packet
->Dhcp4
.Option
,
1219 GET_OPTION_BUFFER_LEN (Packet
),
1222 if (MaxMsgSize
!= NULL
) {
1223 Value
= HTONS (PXEBC_DHCP4_PACKET_MAX_SIZE
);
1224 CopyMem (MaxMsgSize
->Data
, &Value
, sizeof (Value
));
1228 // Callback to user if any packets sent or received.
1230 if (Dhcp4Event
!= Dhcp4SelectOffer
&& Callback
!= NULL
) {
1231 Received
= (BOOLEAN
) (Dhcp4Event
== Dhcp4RcvdOffer
|| Dhcp4Event
== Dhcp4RcvdAck
);
1232 Status
= Callback
->Callback (
1237 (EFI_PXE_BASE_CODE_PACKET
*) &Packet
->Dhcp4
1239 if (Status
!= EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE
) {
1244 Status
= EFI_SUCCESS
;
1246 switch (Dhcp4Event
) {
1248 case Dhcp4SendDiscover
:
1249 if (Packet
->Length
> PXEBC_DHCP4_PACKET_MAX_SIZE
) {
1251 // If the to be sent packet exceeds the maximum length, abort the DHCP process.
1253 Status
= EFI_ABORTED
;
1258 // Cache the DHCPv4 discover packet to mode data directly.
1259 // It need to check SendGuid as well as Dhcp4SendRequest.
1261 CopyMem (&Mode
->DhcpDiscover
.Dhcpv4
, &Packet
->Dhcp4
, Packet
->Length
);
1263 case Dhcp4SendRequest
:
1264 if (Packet
->Length
> PXEBC_DHCP4_PACKET_MAX_SIZE
) {
1266 // If the to be sent packet exceeds the maximum length, abort the DHCP process.
1268 Status
= EFI_ABORTED
;
1272 if (Mode
->SendGUID
) {
1274 // Send the system Guid instead of the MAC address as the hardware address if required.
1276 if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID
*) Packet
->Dhcp4
.Header
.ClientHwAddr
))) {
1278 // Zero the Guid to indicate NOT programable if failed to get system Guid.
1280 DEBUG ((EFI_D_WARN
, "PXE: Failed to read system GUID from the smbios table!\n"));
1281 ZeroMem (Packet
->Dhcp4
.Header
.ClientHwAddr
, sizeof (EFI_GUID
));
1283 Packet
->Dhcp4
.Header
.HwAddrLen
= (UINT8
) sizeof (EFI_GUID
);
1287 case Dhcp4RcvdOffer
:
1288 Status
= EFI_NOT_READY
;
1289 if (Packet
->Length
> PXEBC_DHCP4_PACKET_MAX_SIZE
) {
1291 // Ignore the incoming packets which exceed the maximum length.
1295 if (Private
->OfferNum
< PXEBC_OFFER_MAX_NUM
) {
1297 // Cache the DHCPv4 offers to OfferBuffer[] for select later, and record
1298 // the OfferIndex and OfferCount.
1299 // If error happens, just ignore this packet and continue to wait more offer.
1301 PxeBcCacheDhcp4Offer (Private
, Packet
);
1305 case Dhcp4SelectOffer
:
1306 ASSERT (NewPacket
!= NULL
);
1309 // Select offer by the default policy or by order, and record the SelectIndex
1310 // and SelectProxyType.
1312 PxeBcSelectDhcp4Offer (Private
);
1314 if (Private
->SelectIndex
== 0) {
1315 Status
= EFI_ABORTED
;
1317 *NewPacket
= &Private
->OfferBuffer
[Private
->SelectIndex
- 1].Dhcp4
.Packet
.Offer
;
1323 // Cache the DHCPv4 ack to Private->Dhcp4Ack, but it's not the final ack in mode data
1324 // without verification.
1326 ASSERT (Private
->SelectIndex
!= 0);
1328 Status
= PxeBcCopyDhcp4Ack (Private
, Packet
, FALSE
);
1329 if (EFI_ERROR (Status
)) {
1330 Status
= EFI_ABORTED
;
1343 Build and send out the request packet for the bootfile, and parse the reply.
1345 @param[in] Private Pointer to PxeBc private data.
1346 @param[in] Type PxeBc option boot item type.
1347 @param[in] Layer Pointer to option boot item layer.
1348 @param[in] UseBis Use BIS or not.
1349 @param[in] DestIp Pointer to the server address.
1350 @param[in] IpCount The total count of the server address.
1351 @param[in] SrvList Pointer to EFI_PXE_BASE_CODE_SRVLIST.
1353 @retval EFI_SUCCESS Successfully discovered boot file.
1354 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource.
1355 @retval EFI_NOT_FOUND Can't get the PXE reply packet.
1356 @retval Others Failed to discover boot file.
1360 PxeBcDhcp4Discover (
1361 IN PXEBC_PRIVATE_DATA
*Private
,
1365 IN EFI_IP_ADDRESS
*DestIp
,
1367 IN EFI_PXE_BASE_CODE_SRVLIST
*SrvList
1370 EFI_PXE_BASE_CODE_UDP_PORT Sport
;
1371 EFI_PXE_BASE_CODE_MODE
*Mode
;
1372 EFI_DHCP4_PROTOCOL
*Dhcp4
;
1373 EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN Token
;
1379 EFI_DHCP4_LISTEN_POINT ListenPoint
;
1380 EFI_DHCP4_PACKET
*Response
;
1381 UINT8 Buffer
[PXEBC_DHCP4_OPTION_MAX_SIZE
];
1382 EFI_DHCP4_PACKET_OPTION
*OptList
[PXEBC_DHCP4_OPTION_MAX_NUM
];
1384 EFI_DHCP4_PACKET_OPTION
*PxeOpt
;
1385 PXEBC_OPTION_BOOT_ITEM
*PxeBootItem
;
1389 Mode
= Private
->PxeBc
.Mode
;
1390 Dhcp4
= Private
->Dhcp4
;
1391 Status
= EFI_SUCCESS
;
1393 ZeroMem (&Token
, sizeof (EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN
));
1396 // Use broadcast if destination address not specified.
1398 if (DestIp
== NULL
) {
1399 Sport
= PXEBC_DHCP4_S_PORT
;
1402 Sport
= PXEBC_BS_DISCOVER_PORT
;
1406 if (!UseBis
&& Layer
!= NULL
) {
1407 *Layer
&= EFI_PXE_BASE_CODE_BOOT_LAYER_MASK
;
1411 // Build all the options for the request packet.
1413 OptCount
= PxeBcBuildDhcp4Options (Private
, OptList
, Buffer
, TRUE
);
1415 if (Private
->IsDoDiscover
) {
1417 // Add vendor option of PXE_BOOT_ITEM
1419 VendorOptLen
= (UINT8
) ((sizeof (EFI_DHCP4_PACKET_OPTION
) - 1) * 2 + sizeof (PXEBC_OPTION_BOOT_ITEM
) + 1);
1420 OptList
[OptCount
] = AllocateZeroPool (VendorOptLen
);
1421 if (OptList
[OptCount
] == NULL
) {
1422 return EFI_OUT_OF_RESOURCES
;
1425 OptList
[OptCount
]->OpCode
= DHCP4_TAG_VENDOR
;
1426 OptList
[OptCount
]->Length
= (UINT8
) (VendorOptLen
- 2);
1427 PxeOpt
= (EFI_DHCP4_PACKET_OPTION
*) OptList
[OptCount
]->Data
;
1428 PxeOpt
->OpCode
= PXEBC_VENDOR_TAG_BOOT_ITEM
;
1429 PxeOpt
->Length
= (UINT8
) sizeof (PXEBC_OPTION_BOOT_ITEM
);
1430 PxeBootItem
= (PXEBC_OPTION_BOOT_ITEM
*) PxeOpt
->Data
;
1431 PxeBootItem
->Type
= HTONS (Type
);
1432 PxeOpt
->Data
[PxeOpt
->Length
] = DHCP4_TAG_EOP
;
1434 if (Layer
!= NULL
) {
1435 PxeBootItem
->Layer
= HTONS (*Layer
);
1442 // Build the request packet with seed packet and option list.
1444 Status
= Dhcp4
->Build (
1446 &Private
->SeedPacket
,
1454 // Free the vendor option of PXE_BOOT_ITEM.
1456 if (Private
->IsDoDiscover
) {
1457 FreePool (OptList
[OptCount
- 1]);
1460 if (EFI_ERROR (Status
)) {
1464 if (Mode
->SendGUID
) {
1465 if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID
*) Token
.Packet
->Dhcp4
.Header
.ClientHwAddr
))) {
1467 // Zero the Guid to indicate NOT programable if failed to get system Guid.
1469 DEBUG ((EFI_D_WARN
, "PXE: Failed to read system GUID from the smbios table!\n"));
1470 ZeroMem (Token
.Packet
->Dhcp4
.Header
.ClientHwAddr
, sizeof (EFI_GUID
));
1472 Token
.Packet
->Dhcp4
.Header
.HwAddrLen
= (UINT8
) sizeof (EFI_GUID
);
1476 // Set fields of the token for the request packet.
1478 Xid
= NET_RANDOM (NetRandomInitSeed ());
1479 Token
.Packet
->Dhcp4
.Header
.Xid
= HTONL (Xid
);
1480 Token
.Packet
->Dhcp4
.Header
.Reserved
= HTONS ((UINT16
) ((IsBCast
) ? 0x8000 : 0x0));
1481 CopyMem (&Token
.Packet
->Dhcp4
.Header
.ClientAddr
, &Private
->StationIp
, sizeof (EFI_IPv4_ADDRESS
));
1483 Token
.RemotePort
= Sport
;
1486 SetMem (&Token
.RemoteAddress
, sizeof (EFI_IPv4_ADDRESS
), 0xff);
1488 CopyMem (&Token
.RemoteAddress
, DestIp
, sizeof (EFI_IPv4_ADDRESS
));
1491 CopyMem (&Token
.GatewayAddress
, &Private
->GatewayIp
, sizeof (EFI_IPv4_ADDRESS
));
1494 Token
.ListenPointCount
= 1;
1495 Token
.ListenPoints
= &ListenPoint
;
1496 Token
.ListenPoints
[0].ListenPort
= PXEBC_BS_DISCOVER_PORT
;
1497 CopyMem (&Token
.ListenPoints
[0].ListenAddress
, &Private
->StationIp
, sizeof(EFI_IPv4_ADDRESS
));
1498 CopyMem (&Token
.ListenPoints
[0].SubnetMask
, &Private
->SubnetMask
, sizeof(EFI_IPv4_ADDRESS
));
1502 // Send out the request packet to discover the bootfile.
1504 for (TryIndex
= 1; TryIndex
<= PXEBC_BOOT_REQUEST_RETRIES
; TryIndex
++) {
1506 Token
.TimeoutValue
= (UINT16
) (PXEBC_BOOT_REQUEST_TIMEOUT
* TryIndex
);
1507 Token
.Packet
->Dhcp4
.Header
.Seconds
= (UINT16
) (PXEBC_BOOT_REQUEST_TIMEOUT
* (TryIndex
- 1));
1509 Status
= Dhcp4
->TransmitReceive (Dhcp4
, &Token
);
1510 if (Token
.Status
!= EFI_TIMEOUT
) {
1515 if (TryIndex
> PXEBC_BOOT_REQUEST_RETRIES
) {
1517 // No server response our PXE request
1519 Status
= EFI_TIMEOUT
;
1522 if (!EFI_ERROR (Status
)) {
1526 Response
= Token
.ResponseList
;
1528 // Find the right PXE Reply according to server address.
1530 while (RepIndex
< Token
.ResponseCount
) {
1531 if (Response
->Length
> PXEBC_DHCP4_PACKET_MAX_SIZE
) {
1534 Response
= (EFI_DHCP4_PACKET
*) ((UINT8
*) Response
+ Response
->Size
);
1538 while (SrvIndex
< IpCount
) {
1539 if (SrvList
[SrvIndex
].AcceptAnyResponse
) {
1542 if ((SrvList
[SrvIndex
].Type
== Type
) &&
1543 EFI_IP4_EQUAL (&Response
->Dhcp4
.Header
.ServerAddr
, &SrvList
[SrvIndex
].IpAddr
)) {
1549 if ((IpCount
!= SrvIndex
) || (IpCount
== 0)) {
1555 Response
= (EFI_DHCP4_PACKET
*) ((UINT8
*) Response
+ Response
->Size
);
1558 if (RepIndex
< Token
.ResponseCount
) {
1560 // Cache the right PXE reply packet here, set valid flag later.
1561 // Especially for PXE discover packet, store it into mode data here.
1563 if (Private
->IsDoDiscover
) {
1564 Status
= PxeBcCacheDhcp4Packet (&Private
->PxeReply
.Dhcp4
.Packet
.Ack
, Response
);
1565 if (EFI_ERROR(Status
)) {
1568 CopyMem (&Mode
->PxeDiscover
, &Token
.Packet
->Dhcp4
, Token
.Packet
->Length
);
1570 Status
= PxeBcCacheDhcp4Packet (&Private
->ProxyOffer
.Dhcp4
.Packet
.Offer
, Response
);
1571 if (EFI_ERROR(Status
)) {
1577 // Not found the right PXE reply packet.
1579 Status
= EFI_NOT_FOUND
;
1584 if (Token
.ResponseList
!= NULL
) {
1585 FreePool (Token
.ResponseList
);
1587 if (Token
.Packet
!= NULL
) {
1588 FreePool (Token
.Packet
);
1594 Switch the Ip4 policy to static.
1596 @param[in] Private The pointer to PXEBC_PRIVATE_DATA.
1598 @retval EFI_SUCCESS The policy is already configured to static.
1599 @retval Others Other error as indicated..
1604 IN PXEBC_PRIVATE_DATA
*Private
1608 EFI_IP4_CONFIG2_PROTOCOL
*Ip4Config2
;
1609 EFI_IP4_CONFIG2_POLICY Policy
;
1612 Ip4Config2
= Private
->Ip4Config2
;
1613 DataSize
= sizeof (EFI_IP4_CONFIG2_POLICY
);
1614 Status
= Ip4Config2
->GetData (
1616 Ip4Config2DataTypePolicy
,
1620 if (EFI_ERROR (Status
)) {
1624 if (Policy
!= Ip4Config2PolicyStatic
) {
1625 Policy
= Ip4Config2PolicyStatic
;
1626 Status
= Ip4Config2
->SetData (
1628 Ip4Config2DataTypePolicy
,
1629 sizeof (EFI_IP4_CONFIG2_POLICY
),
1632 if (EFI_ERROR (Status
)) {
1641 Start the D.O.R.A DHCPv4 process to acquire the IPv4 address and other PXE boot information.
1643 @param[in] Private Pointer to PxeBc private data.
1644 @param[in] Dhcp4 Pointer to the EFI_DHCP4_PROTOCOL
1646 @retval EFI_SUCCESS The D.O.R.A process successfully finished.
1647 @retval Others Failed to finish the D.O.R.A process.
1652 IN PXEBC_PRIVATE_DATA
*Private
,
1653 IN EFI_DHCP4_PROTOCOL
*Dhcp4
1656 EFI_PXE_BASE_CODE_MODE
*PxeMode
;
1657 EFI_DHCP4_CONFIG_DATA Config
;
1658 EFI_DHCP4_MODE_DATA Mode
;
1659 EFI_DHCP4_PACKET_OPTION
*OptList
[PXEBC_DHCP4_OPTION_MAX_NUM
];
1660 UINT8 Buffer
[PXEBC_DHCP4_OPTION_MAX_SIZE
];
1664 ASSERT (Dhcp4
!= NULL
);
1666 Status
= EFI_SUCCESS
;
1667 PxeMode
= Private
->PxeBc
.Mode
;
1670 // Build option list for the request packet.
1672 OptCount
= PxeBcBuildDhcp4Options (Private
, OptList
, Buffer
, FALSE
);
1673 ASSERT (OptCount
> 0);
1675 ZeroMem (&Mode
, sizeof (EFI_DHCP4_MODE_DATA
));
1676 ZeroMem (&Config
, sizeof (EFI_DHCP4_CONFIG_DATA
));
1678 Config
.OptionCount
= OptCount
;
1679 Config
.OptionList
= OptList
;
1680 Config
.Dhcp4Callback
= PxeBcDhcp4CallBack
;
1681 Config
.CallbackContext
= Private
;
1682 Config
.DiscoverTryCount
= PXEBC_DHCP_RETRIES
;
1683 Config
.DiscoverTimeout
= mPxeDhcpTimeout
;
1686 // Configure the DHCPv4 instance for PXE boot.
1688 Status
= Dhcp4
->Configure (Dhcp4
, &Config
);
1689 if (EFI_ERROR (Status
)) {
1694 // Initialize the record fields for DHCPv4 offer in private data.
1696 Private
->IsProxyRecved
= FALSE
;
1697 Private
->OfferNum
= 0;
1698 ZeroMem (Private
->OfferCount
, sizeof (Private
->OfferCount
));
1699 ZeroMem (Private
->OfferIndex
, sizeof (Private
->OfferIndex
));
1701 Status
= Dhcp4
->Start (Dhcp4
, NULL
);
1702 if (EFI_ERROR (Status
)) {
1703 if (Status
== EFI_ICMP_ERROR
) {
1704 PxeMode
->IcmpErrorReceived
= TRUE
;
1707 if (Status
== EFI_TIMEOUT
&& Private
->OfferNum
> 0) {
1708 Status
= EFI_NO_RESPONSE
;
1715 // Get the acquired IPv4 address and store them.
1717 Status
= Dhcp4
->GetModeData (Dhcp4
, &Mode
);
1718 if (EFI_ERROR (Status
)) {
1722 ASSERT (Mode
.State
== Dhcp4Bound
);
1724 CopyMem (&Private
->StationIp
, &Mode
.ClientAddress
, sizeof (EFI_IPv4_ADDRESS
));
1725 CopyMem (&Private
->SubnetMask
, &Mode
.SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
1726 CopyMem (&Private
->GatewayIp
, &Mode
.RouterAddress
, sizeof (EFI_IPv4_ADDRESS
));
1727 CopyMem (&PxeMode
->StationIp
, &Private
->StationIp
, sizeof (EFI_IPv4_ADDRESS
));
1728 CopyMem (&PxeMode
->SubnetMask
, &Private
->SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
1730 Status
= PxeBcFlushStationIp (Private
, &Private
->StationIp
, &Private
->SubnetMask
);
1731 if (EFI_ERROR (Status
)) {
1736 // Check the selected offer whether BINL retry is needed.
1738 Status
= PxeBcHandleDhcp4Offer (Private
);
1740 AsciiPrint ("\n Station IP address is ");
1742 PxeBcShowIp4Addr (&Private
->StationIp
.v4
);
1746 if (EFI_ERROR (Status
)) {
1747 Dhcp4
->Stop (Dhcp4
);
1748 Dhcp4
->Configure (Dhcp4
, NULL
);
1750 ZeroMem (&Config
, sizeof (EFI_DHCP4_CONFIG_DATA
));
1751 Dhcp4
->Configure (Dhcp4
, &Config
);
1752 Private
->IsAddressOk
= TRUE
;