2 Functions implementation related with DHCPv4 for UefiPxeBc Driver.
4 Copyright (c) 2009 - 2018, 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.
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 DHCP4_TAG_BOOTFILE_LEN
,
27 DHCP4_TAG_VENDOR_CLASS_ID
,
32 // There are 4 times retries with the value of 4, 8, 16 and 32, refers to PXE2.1 spec.
34 UINT32 mPxeDhcpTimeout
[4] = {4, 8, 16, 32};
38 Parse a certain dhcp4 option by OptTag in Buffer, and return with start pointer.
40 @param[in] Buffer Pointer to the option buffer.
41 @param[in] Length Length of the option buffer.
42 @param[in] OptTag Tag of the required option.
44 @retval NULL Failed to find the required option.
45 @retval Others The position of the required option.
48 EFI_DHCP4_PACKET_OPTION
*
49 PxeBcParseDhcp4Options (
55 EFI_DHCP4_PACKET_OPTION
*Option
;
58 Option
= (EFI_DHCP4_PACKET_OPTION
*) Buffer
;
61 while (Offset
< Length
&& Option
->OpCode
!= DHCP4_TAG_EOP
) {
63 if (Option
->OpCode
== OptTag
) {
65 // Found the required option.
71 // Skip the current option to the next.
73 if (Option
->OpCode
== DHCP4_TAG_PAD
) {
76 Offset
+= Option
->Length
+ 2;
79 Option
= (EFI_DHCP4_PACKET_OPTION
*) (Buffer
+ Offset
);
87 Parse the PXE vender options and extract the information from them.
89 @param[in] Dhcp4Option Pointer to vendor options in buffer.
90 @param[in] VendorOption Pointer to structure to store information in vendor options.
94 PxeBcParseVendorOptions (
95 IN EFI_DHCP4_PACKET_OPTION
*Dhcp4Option
,
96 IN PXEBC_VENDOR_OPTION
*VendorOption
100 UINT8 VendorOptionLen
;
101 EFI_DHCP4_PACKET_OPTION
*PxeOption
;
104 BitMap
= VendorOption
->BitMap
;
105 VendorOptionLen
= Dhcp4Option
->Length
;
106 PxeOption
= (EFI_DHCP4_PACKET_OPTION
*) &Dhcp4Option
->Data
[0];
109 ASSERT (PxeOption
!= NULL
);
111 while ((Offset
< VendorOptionLen
) && (PxeOption
->OpCode
!= DHCP4_TAG_EOP
)) {
113 // Parse all the interesting PXE vendor options one by one.
115 switch (PxeOption
->OpCode
) {
117 case PXEBC_VENDOR_TAG_MTFTP_IP
:
119 CopyMem (&VendorOption
->MtftpIp
, PxeOption
->Data
, sizeof (EFI_IPv4_ADDRESS
));
122 case PXEBC_VENDOR_TAG_MTFTP_CPORT
:
124 CopyMem (&VendorOption
->MtftpCPort
, PxeOption
->Data
, sizeof (VendorOption
->MtftpCPort
));
127 case PXEBC_VENDOR_TAG_MTFTP_SPORT
:
129 CopyMem (&VendorOption
->MtftpSPort
, PxeOption
->Data
, sizeof (VendorOption
->MtftpSPort
));
132 case PXEBC_VENDOR_TAG_MTFTP_TIMEOUT
:
134 VendorOption
->MtftpTimeout
= *PxeOption
->Data
;
137 case PXEBC_VENDOR_TAG_MTFTP_DELAY
:
139 VendorOption
->MtftpDelay
= *PxeOption
->Data
;
142 case PXEBC_VENDOR_TAG_DISCOVER_CTRL
:
144 VendorOption
->DiscoverCtrl
= *PxeOption
->Data
;
147 case PXEBC_VENDOR_TAG_DISCOVER_MCAST
:
149 CopyMem (&VendorOption
->DiscoverMcastIp
, PxeOption
->Data
, sizeof (EFI_IPv4_ADDRESS
));
152 case PXEBC_VENDOR_TAG_BOOT_SERVERS
:
154 VendorOption
->BootSvrLen
= PxeOption
->Length
;
155 VendorOption
->BootSvr
= (PXEBC_BOOT_SVR_ENTRY
*) PxeOption
->Data
;
158 case PXEBC_VENDOR_TAG_BOOT_MENU
:
160 VendorOption
->BootMenuLen
= PxeOption
->Length
;
161 VendorOption
->BootMenu
= (PXEBC_BOOT_MENU_ENTRY
*) PxeOption
->Data
;
164 case PXEBC_VENDOR_TAG_MENU_PROMPT
:
166 VendorOption
->MenuPromptLen
= PxeOption
->Length
;
167 VendorOption
->MenuPrompt
= (PXEBC_MENU_PROMPT
*) PxeOption
->Data
;
170 case PXEBC_VENDOR_TAG_MCAST_ALLOC
:
172 CopyMem (&VendorOption
->McastIpBase
, PxeOption
->Data
, sizeof (EFI_IPv4_ADDRESS
));
173 CopyMem (&VendorOption
->McastIpBlock
, PxeOption
->Data
+ 4, sizeof (VendorOption
->McastIpBlock
));
174 CopyMem (&VendorOption
->McastIpRange
, PxeOption
->Data
+ 6, sizeof (VendorOption
->McastIpRange
));
177 case PXEBC_VENDOR_TAG_CREDENTIAL_TYPES
:
179 VendorOption
->CredTypeLen
= PxeOption
->Length
;
180 VendorOption
->CredType
= (UINT32
*) PxeOption
->Data
;
183 case PXEBC_VENDOR_TAG_BOOT_ITEM
:
185 CopyMem (&VendorOption
->BootSrvType
, PxeOption
->Data
, sizeof (VendorOption
->BootSrvType
));
186 CopyMem (&VendorOption
->BootSrvLayer
, PxeOption
->Data
+ 2, sizeof (VendorOption
->BootSrvLayer
));
191 // Not interesting PXE vendor options.
197 // Set the bit map for the special PXE options.
199 SET_VENDOR_OPTION_BIT_MAP (BitMap
, PxeOption
->OpCode
);
202 // Continue to the next option.
204 if (PxeOption
->OpCode
== DHCP4_TAG_PAD
) {
207 Offset
= (UINT8
) (Offset
+ PxeOption
->Length
+ 2);
210 PxeOption
= (EFI_DHCP4_PACKET_OPTION
*) (Dhcp4Option
->Data
+ Offset
);
216 Build the options buffer for the DHCPv4 request packet.
218 @param[in] Private Pointer to PxeBc private data.
219 @param[out] OptList Pointer to the option pointer array.
220 @param[in] Buffer Pointer to the buffer to contain the option list.
221 @param[in] NeedMsgType If TRUE, it is necessary to include the Msg type option.
222 Otherwise, it is not necessary.
224 @return Index The count of the built-in options.
228 PxeBcBuildDhcp4Options (
229 IN PXEBC_PRIVATE_DATA
*Private
,
230 OUT EFI_DHCP4_PACKET_OPTION
**OptList
,
232 IN BOOLEAN NeedMsgType
236 PXEBC_DHCP4_OPTION_ENTRY OptEnt
;
240 OptList
[0] = (EFI_DHCP4_PACKET_OPTION
*) Buffer
;
244 // Append message type.
246 OptList
[Index
]->OpCode
= DHCP4_TAG_MSG_TYPE
;
247 OptList
[Index
]->Length
= 1;
248 OptEnt
.Mesg
= (PXEBC_DHCP4_OPTION_MESG
*) OptList
[Index
]->Data
;
249 OptEnt
.Mesg
->Type
= PXEBC_DHCP4_MSG_TYPE_REQUEST
;
251 OptList
[Index
] = GET_NEXT_DHCP_OPTION (OptList
[Index
- 1]);
254 // Append max message size.
256 OptList
[Index
]->OpCode
= DHCP4_TAG_MAXMSG
;
257 OptList
[Index
]->Length
= (UINT8
) sizeof (PXEBC_DHCP4_OPTION_MAX_MESG_SIZE
);
258 OptEnt
.MaxMesgSize
= (PXEBC_DHCP4_OPTION_MAX_MESG_SIZE
*) OptList
[Index
]->Data
;
259 Value
= NTOHS (PXEBC_DHCP4_PACKET_MAX_SIZE
);
260 CopyMem (&OptEnt
.MaxMesgSize
->Size
, &Value
, sizeof (UINT16
));
262 OptList
[Index
] = GET_NEXT_DHCP_OPTION (OptList
[Index
- 1]);
266 // Append parameter request list option.
268 OptList
[Index
]->OpCode
= DHCP4_TAG_PARA_LIST
;
269 OptList
[Index
]->Length
= 35;
270 OptEnt
.Para
= (PXEBC_DHCP4_OPTION_PARA
*) OptList
[Index
]->Data
;
271 OptEnt
.Para
->ParaList
[0] = DHCP4_TAG_NETMASK
;
272 OptEnt
.Para
->ParaList
[1] = DHCP4_TAG_TIME_OFFSET
;
273 OptEnt
.Para
->ParaList
[2] = DHCP4_TAG_ROUTER
;
274 OptEnt
.Para
->ParaList
[3] = DHCP4_TAG_TIME_SERVER
;
275 OptEnt
.Para
->ParaList
[4] = DHCP4_TAG_NAME_SERVER
;
276 OptEnt
.Para
->ParaList
[5] = DHCP4_TAG_DNS_SERVER
;
277 OptEnt
.Para
->ParaList
[6] = DHCP4_TAG_HOSTNAME
;
278 OptEnt
.Para
->ParaList
[7] = DHCP4_TAG_BOOTFILE_LEN
;
279 OptEnt
.Para
->ParaList
[8] = DHCP4_TAG_DOMAINNAME
;
280 OptEnt
.Para
->ParaList
[9] = DHCP4_TAG_ROOTPATH
;
281 OptEnt
.Para
->ParaList
[10] = DHCP4_TAG_EXTEND_PATH
;
282 OptEnt
.Para
->ParaList
[11] = DHCP4_TAG_EMTU
;
283 OptEnt
.Para
->ParaList
[12] = DHCP4_TAG_TTL
;
284 OptEnt
.Para
->ParaList
[13] = DHCP4_TAG_BROADCAST
;
285 OptEnt
.Para
->ParaList
[14] = DHCP4_TAG_NIS_DOMAIN
;
286 OptEnt
.Para
->ParaList
[15] = DHCP4_TAG_NIS_SERVER
;
287 OptEnt
.Para
->ParaList
[16] = DHCP4_TAG_NTP_SERVER
;
288 OptEnt
.Para
->ParaList
[17] = DHCP4_TAG_VENDOR
;
289 OptEnt
.Para
->ParaList
[18] = DHCP4_TAG_REQUEST_IP
;
290 OptEnt
.Para
->ParaList
[19] = DHCP4_TAG_LEASE
;
291 OptEnt
.Para
->ParaList
[20] = DHCP4_TAG_SERVER_ID
;
292 OptEnt
.Para
->ParaList
[21] = DHCP4_TAG_T1
;
293 OptEnt
.Para
->ParaList
[22] = DHCP4_TAG_T2
;
294 OptEnt
.Para
->ParaList
[23] = DHCP4_TAG_VENDOR_CLASS_ID
;
295 OptEnt
.Para
->ParaList
[24] = DHCP4_TAG_TFTP
;
296 OptEnt
.Para
->ParaList
[25] = DHCP4_TAG_BOOTFILE
;
297 OptEnt
.Para
->ParaList
[26] = DHCP4_TAG_UUID
;
298 OptEnt
.Para
->ParaList
[27] = 0x80;
299 OptEnt
.Para
->ParaList
[28] = 0x81;
300 OptEnt
.Para
->ParaList
[29] = 0x82;
301 OptEnt
.Para
->ParaList
[30] = 0x83;
302 OptEnt
.Para
->ParaList
[31] = 0x84;
303 OptEnt
.Para
->ParaList
[32] = 0x85;
304 OptEnt
.Para
->ParaList
[33] = 0x86;
305 OptEnt
.Para
->ParaList
[34] = 0x87;
307 OptList
[Index
] = GET_NEXT_DHCP_OPTION (OptList
[Index
- 1]);
310 // Append UUID/Guid-based client identifier option
312 OptList
[Index
]->OpCode
= DHCP4_TAG_UUID
;
313 OptList
[Index
]->Length
= (UINT8
) sizeof (PXEBC_DHCP4_OPTION_UUID
);
314 OptEnt
.Uuid
= (PXEBC_DHCP4_OPTION_UUID
*) OptList
[Index
]->Data
;
315 OptEnt
.Uuid
->Type
= 0;
317 OptList
[Index
] = GET_NEXT_DHCP_OPTION (OptList
[Index
- 1]);
319 if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID
*) OptEnt
.Uuid
->Guid
))) {
321 // Zero the Guid to indicate NOT programable if failed to get system Guid.
323 DEBUG ((EFI_D_WARN
, "PXE: Failed to read system GUID from the smbios table!\n"));
324 ZeroMem (OptEnt
.Uuid
->Guid
, sizeof (EFI_GUID
));
328 // Append client network device interface option
330 OptList
[Index
]->OpCode
= DHCP4_TAG_UNDI
;
331 OptList
[Index
]->Length
= (UINT8
) sizeof (PXEBC_DHCP4_OPTION_UNDI
);
332 OptEnt
.Undi
= (PXEBC_DHCP4_OPTION_UNDI
*) OptList
[Index
]->Data
;
334 if (Private
->Nii
!= NULL
) {
335 OptEnt
.Undi
->Type
= Private
->Nii
->Type
;
336 OptEnt
.Undi
->MajorVer
= Private
->Nii
->MajorVer
;
337 OptEnt
.Undi
->MinorVer
= Private
->Nii
->MinorVer
;
339 OptEnt
.Undi
->Type
= DEFAULT_UNDI_TYPE
;
340 OptEnt
.Undi
->MajorVer
= DEFAULT_UNDI_MAJOR
;
341 OptEnt
.Undi
->MinorVer
= DEFAULT_UNDI_MINOR
;
345 OptList
[Index
] = GET_NEXT_DHCP_OPTION (OptList
[Index
- 1]);
348 // Append client system architecture option
350 OptList
[Index
]->OpCode
= DHCP4_TAG_ARCH
;
351 OptList
[Index
]->Length
= (UINT8
) sizeof (PXEBC_DHCP4_OPTION_ARCH
);
352 OptEnt
.Arch
= (PXEBC_DHCP4_OPTION_ARCH
*) OptList
[Index
]->Data
;
353 Value
= HTONS (EFI_PXE_CLIENT_SYSTEM_ARCHITECTURE
);
354 CopyMem (&OptEnt
.Arch
->Type
, &Value
, sizeof (UINT16
));
356 OptList
[Index
] = GET_NEXT_DHCP_OPTION (OptList
[Index
- 1]);
359 // Append vendor class identify option
361 OptList
[Index
]->OpCode
= DHCP4_TAG_VENDOR_CLASS_ID
;
362 OptList
[Index
]->Length
= (UINT8
) sizeof (PXEBC_DHCP4_OPTION_CLID
);
363 OptEnt
.Clid
= (PXEBC_DHCP4_OPTION_CLID
*) OptList
[Index
]->Data
;
366 DEFAULT_CLASS_ID_DATA
,
367 sizeof (PXEBC_DHCP4_OPTION_CLID
)
369 PxeBcUintnToAscDecWithFormat (
370 EFI_PXE_CLIENT_SYSTEM_ARCHITECTURE
,
371 OptEnt
.Clid
->ArchitectureType
,
372 sizeof (OptEnt
.Clid
->ArchitectureType
)
375 if (Private
->Nii
!= NULL
) {
376 CopyMem (OptEnt
.Clid
->InterfaceName
, Private
->Nii
->StringId
, sizeof (OptEnt
.Clid
->InterfaceName
));
377 PxeBcUintnToAscDecWithFormat (Private
->Nii
->MajorVer
, OptEnt
.Clid
->UndiMajor
, sizeof (OptEnt
.Clid
->UndiMajor
));
378 PxeBcUintnToAscDecWithFormat (Private
->Nii
->MinorVer
, OptEnt
.Clid
->UndiMinor
, sizeof (OptEnt
.Clid
->UndiMinor
));
388 Create a template DHCPv4 packet as a seed.
390 @param[out] Seed Pointer to the seed packet.
391 @param[in] Udp4 Pointer to EFI_UDP4_PROTOCOL.
395 PxeBcSeedDhcp4Packet (
396 OUT EFI_DHCP4_PACKET
*Seed
,
397 IN EFI_UDP4_PROTOCOL
*Udp4
400 EFI_SIMPLE_NETWORK_MODE Mode
;
401 EFI_DHCP4_HEADER
*Header
;
404 // Get IfType and HwAddressSize from SNP mode data.
406 Udp4
->GetModeData (Udp4
, NULL
, NULL
, NULL
, &Mode
);
408 Seed
->Size
= sizeof (EFI_DHCP4_PACKET
);
409 Seed
->Length
= sizeof (Seed
->Dhcp4
);
410 Header
= &Seed
->Dhcp4
.Header
;
411 ZeroMem (Header
, sizeof (EFI_DHCP4_HEADER
));
412 Header
->OpCode
= PXEBC_DHCP4_OPCODE_REQUEST
;
413 Header
->HwType
= Mode
.IfType
;
414 Header
->HwAddrLen
= (UINT8
) Mode
.HwAddressSize
;
415 CopyMem (Header
->ClientHwAddr
, &Mode
.CurrentAddress
, Header
->HwAddrLen
);
417 Seed
->Dhcp4
.Magik
= PXEBC_DHCP4_MAGIC
;
418 Seed
->Dhcp4
.Option
[0] = DHCP4_TAG_EOP
;
423 Cache the DHCPv4 packet.
425 @param[in] Dst Pointer to the cache buffer for DHCPv4 packet.
426 @param[in] Src Pointer to the DHCPv4 packet to be cached.
428 @retval EFI_SUCCESS Packet is copied.
429 @retval EFI_BUFFER_TOO_SMALL Cache buffer is not big enough to hold the packet.
433 PxeBcCacheDhcp4Packet (
434 IN EFI_DHCP4_PACKET
*Dst
,
435 IN EFI_DHCP4_PACKET
*Src
438 if (Dst
->Size
< Src
->Length
) {
439 return EFI_BUFFER_TOO_SMALL
;
442 CopyMem (&Dst
->Dhcp4
, &Src
->Dhcp4
, Src
->Length
);
443 Dst
->Length
= Src
->Length
;
450 Parse the cached DHCPv4 packet, including all the options.
452 @param[in] Cache4 Pointer to cached DHCPv4 packet.
454 @retval EFI_SUCCESS Parsed the DHCPv4 packet successfully.
455 @retval EFI_DEVICE_ERROR Failed to parse and invalid packet.
459 PxeBcParseDhcp4Packet (
460 IN PXEBC_DHCP4_PACKET_CACHE
*Cache4
463 EFI_DHCP4_PACKET
*Offer
;
464 EFI_DHCP4_PACKET_OPTION
**Options
;
465 EFI_DHCP4_PACKET_OPTION
*Option
;
466 PXEBC_OFFER_TYPE OfferType
;
468 BOOLEAN IsProxyOffer
;
471 BOOLEAN FileFieldOverloaded
;
473 IsProxyOffer
= FALSE
;
475 FileFieldOverloaded
= FALSE
;
477 ZeroMem (Cache4
->OptList
, sizeof (Cache4
->OptList
));
478 ZeroMem (&Cache4
->VendorOpt
, sizeof (Cache4
->VendorOpt
));
480 Offer
= &Cache4
->Packet
.Offer
;
481 Options
= Cache4
->OptList
;
484 // Parse DHCPv4 options in this offer, and store the pointers.
485 // First, try to parse DHCPv4 options from the DHCP optional parameters field.
487 for (Index
= 0; Index
< PXEBC_DHCP4_TAG_INDEX_MAX
; Index
++) {
488 Options
[Index
] = PxeBcParseDhcp4Options (
490 GET_OPTION_BUFFER_LEN (Offer
),
491 mInterestedDhcp4Tags
[Index
]
495 // Second, Check if bootfilename and serverhostname is overloaded to carry DHCP options refers to rfc-2132.
496 // If yes, try to parse options from the BootFileName field, then ServerName field.
498 Option
= Options
[PXEBC_DHCP4_TAG_INDEX_OVERLOAD
];
499 if (Option
!= NULL
) {
500 if ((Option
->Data
[0] & PXEBC_DHCP4_OVERLOAD_FILE
) != 0) {
501 FileFieldOverloaded
= TRUE
;
502 for (Index
= 0; Index
< PXEBC_DHCP4_TAG_INDEX_MAX
; Index
++) {
503 if (Options
[Index
] == NULL
) {
504 Options
[Index
] = PxeBcParseDhcp4Options (
505 (UINT8
*) Offer
->Dhcp4
.Header
.BootFileName
,
506 sizeof (Offer
->Dhcp4
.Header
.BootFileName
),
507 mInterestedDhcp4Tags
[Index
]
512 if ((Option
->Data
[0] & PXEBC_DHCP4_OVERLOAD_SERVER_NAME
) != 0) {
513 for (Index
= 0; Index
< PXEBC_DHCP4_TAG_INDEX_MAX
; Index
++) {
514 if (Options
[Index
] == NULL
) {
515 Options
[Index
] = PxeBcParseDhcp4Options (
516 (UINT8
*) Offer
->Dhcp4
.Header
.ServerName
,
517 sizeof (Offer
->Dhcp4
.Header
.ServerName
),
518 mInterestedDhcp4Tags
[Index
]
526 // The offer with zero "yiaddr" is a proxy offer.
528 if (Offer
->Dhcp4
.Header
.YourAddr
.Addr
[0] == 0) {
533 // The offer with "PXEClient" is a PXE offer.
535 Option
= Options
[PXEBC_DHCP4_TAG_INDEX_CLASS_ID
];
536 if ((Option
!= NULL
) && (Option
->Length
>= 9) &&
537 (CompareMem (Option
->Data
, DEFAULT_CLASS_ID_DATA
, 9) == 0)) {
542 // Parse PXE vendor options in this offer, and store the contents/pointers.
544 Option
= Options
[PXEBC_DHCP4_TAG_INDEX_VENDOR
];
545 if (IsPxeOffer
&& Option
!= NULL
) {
546 PxeBcParseVendorOptions (Option
, &Cache4
->VendorOpt
);
550 // Parse PXE boot file name:
551 // According to PXE spec, boot file name should be read from DHCP option 67 (bootfile name) if present.
552 // Otherwise, read from boot file field in DHCP header.
554 if (Options
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] != NULL
) {
556 // RFC 2132, Section 9.5 does not strictly state Bootfile name (option 67) is null
557 // terminated string. So force to append null terminated character at the end of string.
559 Ptr8
= (UINT8
*)&Options
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
]->Data
[0];
560 Ptr8
+= Options
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
]->Length
;
561 if (*(Ptr8
- 1) != '\0') {
564 } else if (!FileFieldOverloaded
&& Offer
->Dhcp4
.Header
.BootFileName
[0] != 0) {
566 // If the bootfile is not present and bootfilename is present in DHCPv4 packet, just parse it.
567 // Do not count dhcp option header here, or else will destroy the serverhostname.
569 Options
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] = (EFI_DHCP4_PACKET_OPTION
*)
570 (&Offer
->Dhcp4
.Header
.BootFileName
[0] -
571 OFFSET_OF (EFI_DHCP4_PACKET_OPTION
, Data
[0]));
576 // Determine offer type of the DHCPv4 packet.
578 Option
= Options
[PXEBC_DHCP4_TAG_INDEX_MSG_TYPE
];
579 if (Option
== NULL
|| Option
->Data
[0] == 0) {
581 // It's a Bootp offer.
583 OfferType
= PxeOfferTypeBootp
;
585 Option
= Cache4
->OptList
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
];
586 if (Option
== NULL
) {
588 // If the Bootp offer without bootfilename, discard it.
590 return EFI_DEVICE_ERROR
;
594 if (IS_VALID_DISCOVER_VENDOR_OPTION (Cache4
->VendorOpt
.BitMap
)) {
596 // It's a PXE10 offer with PXEClient and discover vendor option.
598 OfferType
= IsProxyOffer
? PxeOfferTypeProxyPxe10
: PxeOfferTypeDhcpPxe10
;
599 } else if (IS_VALID_MTFTP_VENDOR_OPTION (Cache4
->VendorOpt
.BitMap
)) {
601 // It's a WFM11a offer with PXEClient and mtftp vendor option.
602 // But multi-cast download is not supported currently, so discard it.
604 return EFI_DEVICE_ERROR
;
605 } else if (IsPxeOffer
) {
607 // It's a BINL offer only with PXEClient.
609 OfferType
= IsProxyOffer
? PxeOfferTypeProxyBinl
: PxeOfferTypeDhcpBinl
;
612 // It's a DHCPv4 only offer, which is a pure DHCPv4 offer packet.
614 OfferType
= PxeOfferTypeDhcpOnly
;
618 Cache4
->OfferType
= OfferType
;
625 Cache the DHCPv4 ack packet, and parse it on demand.
627 @param[in] Private Pointer to PxeBc private data.
628 @param[in] Ack Pointer to the DHCPv4 ack packet.
629 @param[in] Verified If TRUE, parse the ACK packet and store info into mode data.
631 @retval EFI_SUCCESS Cache and parse the packet successfully.
632 @retval EFI_BUFFER_TOO_SMALL Cache buffer is not big enough to hold the packet.
637 IN PXEBC_PRIVATE_DATA
*Private
,
638 IN EFI_DHCP4_PACKET
*Ack
,
642 EFI_PXE_BASE_CODE_MODE
*Mode
;
645 Mode
= Private
->PxeBc
.Mode
;
647 Status
= PxeBcCacheDhcp4Packet (&Private
->DhcpAck
.Dhcp4
.Packet
.Ack
, Ack
);
648 if (EFI_ERROR (Status
)) {
654 // Parse the ack packet and store it into mode data if needed.
656 PxeBcParseDhcp4Packet (&Private
->DhcpAck
.Dhcp4
);
657 CopyMem (&Mode
->DhcpAck
.Dhcpv4
, &Ack
->Dhcp4
, Ack
->Length
);
658 Mode
->DhcpAckReceived
= TRUE
;
666 Cache the DHCPv4 proxy offer packet according to the received order.
668 @param[in] Private Pointer to PxeBc private data.
669 @param[in] OfferIndex The received order of offer packets.
671 @retval EFI_SUCCESS Cache and parse the packet successfully.
672 @retval EFI_BUFFER_TOO_SMALL Cache buffer is not big enough to hold the packet.
676 PxeBcCopyProxyOffer (
677 IN PXEBC_PRIVATE_DATA
*Private
,
681 EFI_PXE_BASE_CODE_MODE
*Mode
;
682 EFI_DHCP4_PACKET
*Offer
;
685 ASSERT (OfferIndex
< Private
->OfferNum
);
686 ASSERT (OfferIndex
< PXEBC_OFFER_MAX_NUM
);
688 Mode
= Private
->PxeBc
.Mode
;
689 Offer
= &Private
->OfferBuffer
[OfferIndex
].Dhcp4
.Packet
.Offer
;
692 // Cache the proxy offer packet and parse it.
694 Status
= PxeBcCacheDhcp4Packet (&Private
->ProxyOffer
.Dhcp4
.Packet
.Offer
, Offer
);
695 if (EFI_ERROR(Status
)) {
699 PxeBcParseDhcp4Packet (&Private
->ProxyOffer
.Dhcp4
);
702 // Store this packet into mode data.
704 CopyMem (&Mode
->ProxyOffer
.Dhcpv4
, &Offer
->Dhcp4
, Offer
->Length
);
705 Mode
->ProxyOfferReceived
= TRUE
;
712 Retry to request bootfile name by the BINL offer.
714 @param[in] Private Pointer to PxeBc private data.
715 @param[in] Index The received order of offer packets.
717 @retval EFI_SUCCESS Successfully retried to request bootfile name.
718 @retval EFI_DEVICE_ERROR Failed to retry bootfile name.
722 PxeBcRetryBinlOffer (
723 IN PXEBC_PRIVATE_DATA
*Private
,
727 EFI_DHCP4_PACKET
*Offer
;
728 EFI_IP_ADDRESS ServerIp
;
730 PXEBC_DHCP4_PACKET_CACHE
*Cache4
;
731 EFI_DHCP4_PACKET
*Reply
;
733 ASSERT (Index
< PXEBC_OFFER_MAX_NUM
);
734 ASSERT (Private
->OfferBuffer
[Index
].Dhcp4
.OfferType
== PxeOfferTypeDhcpBinl
||
735 Private
->OfferBuffer
[Index
].Dhcp4
.OfferType
== PxeOfferTypeProxyBinl
);
737 Offer
= &Private
->OfferBuffer
[Index
].Dhcp4
.Packet
.Offer
;
740 // Prefer to siaddr in header as next server address. If it's zero, then use option 54.
742 if (Offer
->Dhcp4
.Header
.ServerAddr
.Addr
[0] == 0) {
745 Private
->OfferBuffer
[Index
].Dhcp4
.OptList
[PXEBC_DHCP4_TAG_INDEX_SERVER_ID
]->Data
,
746 sizeof (EFI_IPv4_ADDRESS
)
751 &Offer
->Dhcp4
.Header
.ServerAddr
,
752 sizeof (EFI_IPv4_ADDRESS
)
756 Private
->IsDoDiscover
= FALSE
;
757 Cache4
= &Private
->ProxyOffer
.Dhcp4
;
758 Reply
= &Cache4
->Packet
.Offer
;
761 // Send another request packet for bootfile name.
763 Status
= PxeBcDhcp4Discover (
772 if (EFI_ERROR (Status
)) {
777 // Parse the reply for the last request packet.
779 Status
= PxeBcParseDhcp4Packet (Cache4
);
780 if (EFI_ERROR (Status
)) {
784 if (Cache4
->OfferType
!= PxeOfferTypeProxyPxe10
&&
785 Cache4
->OfferType
!= PxeOfferTypeProxyWfm11a
&&
786 Cache4
->OptList
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] == NULL
) {
788 // This BINL ack doesn't have discovery option set or multicast option set
789 // or bootfile name specified.
791 return EFI_DEVICE_ERROR
;
795 // Store the reply into mode data.
797 Private
->PxeBc
.Mode
->ProxyOfferReceived
= TRUE
;
798 CopyMem (&Private
->PxeBc
.Mode
->ProxyOffer
.Dhcpv4
, &Reply
->Dhcp4
, Reply
->Length
);
805 Cache all the received DHCPv4 offers, and set OfferIndex and OfferCount.
807 @param[in] Private Pointer to PxeBc private data.
808 @param[in] RcvdOffer Pointer to the received offer packet.
810 @retval EFI_SUCCESS Cache and parse the packet successfully.
811 @retval Others Operation failed.
815 PxeBcCacheDhcp4Offer (
816 IN PXEBC_PRIVATE_DATA
*Private
,
817 IN EFI_DHCP4_PACKET
*RcvdOffer
820 PXEBC_DHCP4_PACKET_CACHE
*Cache4
;
821 EFI_DHCP4_PACKET
*Offer
;
822 PXEBC_OFFER_TYPE OfferType
;
825 ASSERT (Private
->OfferNum
< PXEBC_OFFER_MAX_NUM
);
826 Cache4
= &Private
->OfferBuffer
[Private
->OfferNum
].Dhcp4
;
827 Offer
= &Cache4
->Packet
.Offer
;
830 // Cache the content of DHCPv4 packet firstly.
832 Status
= PxeBcCacheDhcp4Packet (Offer
, RcvdOffer
);
833 if (EFI_ERROR(Status
)) {
838 // Validate the DHCPv4 packet, and parse the options and offer type.
840 if (EFI_ERROR (PxeBcParseDhcp4Packet (Cache4
))) {
845 // Determine whether cache the current offer by type, and record OfferIndex and OfferCount.
847 OfferType
= Cache4
->OfferType
;
848 ASSERT (OfferType
< PxeOfferTypeMax
);
850 if (OfferType
== PxeOfferTypeBootp
) {
852 // It's a Bootp offer, only cache the first one, and discard the others.
854 if (Private
->OfferCount
[OfferType
] == 0) {
855 Private
->OfferIndex
[OfferType
][0] = Private
->OfferNum
;
856 Private
->OfferCount
[OfferType
] = 1;
861 ASSERT (Private
->OfferCount
[OfferType
] < PXEBC_OFFER_MAX_NUM
);
862 if (IS_PROXY_DHCP_OFFER (Offer
)) {
864 // It's a proxy offer without yiaddr, including PXE10, WFM11a or BINL offer.
866 Private
->IsProxyRecved
= TRUE
;
868 if (OfferType
== PxeOfferTypeProxyBinl
) {
870 // Cache all proxy BINL offers.
872 Private
->OfferIndex
[OfferType
][Private
->OfferCount
[OfferType
]] = Private
->OfferNum
;
873 Private
->OfferCount
[OfferType
]++;
874 } else if ((OfferType
== PxeOfferTypeProxyPxe10
|| OfferType
== PxeOfferTypeProxyWfm11a
) &&
875 Private
->OfferCount
[OfferType
] < 1) {
877 // Only cache the first PXE10/WFM11a offer, and discard the others.
879 Private
->OfferIndex
[OfferType
][0] = Private
->OfferNum
;
880 Private
->OfferCount
[OfferType
] = 1;
886 // It's a DHCPv4 offer with yiaddr, and cache them all.
888 Private
->OfferIndex
[OfferType
][Private
->OfferCount
[OfferType
]] = Private
->OfferNum
;
889 Private
->OfferCount
[OfferType
]++;
900 Select an DHCPv4 offer, and record SelectIndex and SelectProxyType.
902 @param[in] Private Pointer to PxeBc private data.
906 PxeBcSelectDhcp4Offer (
907 IN PXEBC_PRIVATE_DATA
*Private
912 EFI_DHCP4_PACKET
*Offer
;
914 Private
->SelectIndex
= 0;
916 if (Private
->IsOfferSorted
) {
918 // Select offer by default policy.
920 if (Private
->OfferCount
[PxeOfferTypeDhcpPxe10
] > 0) {
922 // 1. DhcpPxe10 offer
924 Private
->SelectIndex
= Private
->OfferIndex
[PxeOfferTypeDhcpPxe10
][0] + 1;
926 } else if (Private
->OfferCount
[PxeOfferTypeDhcpWfm11a
] > 0) {
928 // 2. DhcpWfm11a offer
930 Private
->SelectIndex
= Private
->OfferIndex
[PxeOfferTypeDhcpWfm11a
][0] + 1;
932 } else if (Private
->OfferCount
[PxeOfferTypeDhcpOnly
] > 0 &&
933 Private
->OfferCount
[PxeOfferTypeProxyPxe10
] > 0) {
935 // 3. DhcpOnly offer and ProxyPxe10 offer.
937 Private
->SelectIndex
= Private
->OfferIndex
[PxeOfferTypeDhcpOnly
][0] + 1;
938 Private
->SelectProxyType
= PxeOfferTypeProxyPxe10
;
940 } else if (Private
->OfferCount
[PxeOfferTypeDhcpOnly
] > 0 &&
941 Private
->OfferCount
[PxeOfferTypeProxyWfm11a
] > 0) {
943 // 4. DhcpOnly offer and ProxyWfm11a offer.
945 Private
->SelectIndex
= Private
->OfferIndex
[PxeOfferTypeDhcpOnly
][0] + 1;
946 Private
->SelectProxyType
= PxeOfferTypeProxyWfm11a
;
948 } else if (Private
->OfferCount
[PxeOfferTypeDhcpBinl
] > 0) {
950 // 5. DhcpBinl offer.
952 Private
->SelectIndex
= Private
->OfferIndex
[PxeOfferTypeDhcpBinl
][0] + 1;
954 } else if (Private
->OfferCount
[PxeOfferTypeDhcpOnly
] > 0 &&
955 Private
->OfferCount
[PxeOfferTypeProxyBinl
] > 0) {
957 // 6. DhcpOnly offer and ProxyBinl offer.
959 Private
->SelectIndex
= Private
->OfferIndex
[PxeOfferTypeDhcpOnly
][0] + 1;
960 Private
->SelectProxyType
= PxeOfferTypeProxyBinl
;
964 // 7. DhcpOnly offer with bootfilename.
966 for (Index
= 0; Index
< Private
->OfferCount
[PxeOfferTypeDhcpOnly
]; Index
++) {
967 OfferIndex
= Private
->OfferIndex
[PxeOfferTypeDhcpOnly
][Index
];
968 if (Private
->OfferBuffer
[OfferIndex
].Dhcp4
.OptList
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] != NULL
) {
969 Private
->SelectIndex
= OfferIndex
+ 1;
974 // 8. Bootp offer with bootfilename.
976 OfferIndex
= Private
->OfferIndex
[PxeOfferTypeBootp
][0];
977 if (Private
->SelectIndex
== 0 &&
978 Private
->OfferCount
[PxeOfferTypeBootp
] > 0 &&
979 Private
->OfferBuffer
[OfferIndex
].Dhcp4
.OptList
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] != NULL
) {
980 Private
->SelectIndex
= OfferIndex
+ 1;
985 // Select offer by received order.
987 for (Index
= 0; Index
< Private
->OfferNum
; Index
++) {
989 Offer
= &Private
->OfferBuffer
[Index
].Dhcp4
.Packet
.Offer
;
991 if (IS_PROXY_DHCP_OFFER (Offer
)) {
998 if (!Private
->IsProxyRecved
&&
999 Private
->OfferBuffer
[Index
].Dhcp4
.OfferType
== PxeOfferTypeDhcpOnly
&&
1000 Private
->OfferBuffer
[Index
].Dhcp4
.OptList
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] == NULL
) {
1002 // Skip if DhcpOnly offer without any other proxy offers or bootfilename.
1008 // Record the index of the select offer.
1010 Private
->SelectIndex
= Index
+ 1;
1018 Handle the DHCPv4 offer packet.
1020 @param[in] Private Pointer to PxeBc private data.
1022 @retval EFI_SUCCESS Handled the DHCPv4 offer packet successfully.
1023 @retval EFI_NO_RESPONSE No response to the following request packet.
1024 @retval EFI_NOT_FOUND No boot filename received.
1025 @retval EFI_BUFFER_TOO_SMALL Can't cache the offer pacet.
1029 PxeBcHandleDhcp4Offer (
1030 IN PXEBC_PRIVATE_DATA
*Private
1033 PXEBC_DHCP4_PACKET_CACHE
*Cache4
;
1034 EFI_DHCP4_PACKET_OPTION
**Options
;
1036 EFI_DHCP4_PACKET
*Offer
;
1037 PXEBC_OFFER_TYPE OfferType
;
1041 EFI_PXE_BASE_CODE_MODE
*Mode
;
1042 EFI_DHCP4_PACKET
*Ack
;
1044 ASSERT (Private
->SelectIndex
> 0);
1045 SelectIndex
= (UINT32
) (Private
->SelectIndex
- 1);
1046 ASSERT (SelectIndex
< PXEBC_OFFER_MAX_NUM
);
1047 Cache4
= &Private
->OfferBuffer
[SelectIndex
].Dhcp4
;
1048 Options
= Cache4
->OptList
;
1049 Status
= EFI_SUCCESS
;
1051 if (Cache4
->OfferType
== PxeOfferTypeDhcpBinl
) {
1053 // DhcpBinl offer is selected, so need try to request bootfilename by this offer.
1055 if (EFI_ERROR (PxeBcRetryBinlOffer (Private
, SelectIndex
))) {
1056 Status
= EFI_NO_RESPONSE
;
1058 } else if (Cache4
->OfferType
== PxeOfferTypeDhcpOnly
) {
1060 if (Private
->IsProxyRecved
) {
1062 // DhcpOnly offer is selected, so need try to request bootfile name.
1065 if (Private
->IsOfferSorted
) {
1067 // The proxy offer should be determined if select by default policy.
1068 // IsOfferSorted means all offers are labeled by OfferIndex.
1070 ASSERT (Private
->SelectProxyType
< PxeOfferTypeMax
);
1071 ASSERT (Private
->OfferCount
[Private
->SelectProxyType
] > 0);
1073 if (Private
->SelectProxyType
== PxeOfferTypeProxyBinl
) {
1075 // Try all the cached ProxyBinl offer one by one to request bootfile name.
1077 for (Index
= 0; Index
< Private
->OfferCount
[Private
->SelectProxyType
]; Index
++) {
1078 ASSERT (Index
< PXEBC_OFFER_MAX_NUM
);
1079 ProxyIndex
= Private
->OfferIndex
[Private
->SelectProxyType
][Index
];
1080 if (!EFI_ERROR (PxeBcRetryBinlOffer (Private
, ProxyIndex
))) {
1084 if (Index
== Private
->OfferCount
[Private
->SelectProxyType
]) {
1085 Status
= EFI_NO_RESPONSE
;
1089 // For other proxy offers, only one is buffered.
1091 ProxyIndex
= Private
->OfferIndex
[Private
->SelectProxyType
][0];
1095 // The proxy offer should not be determined if select by received order.
1097 Status
= EFI_NO_RESPONSE
;
1099 for (Index
= 0; Index
< Private
->OfferNum
; Index
++) {
1100 ASSERT (Index
< PXEBC_OFFER_MAX_NUM
);
1101 Offer
= &Private
->OfferBuffer
[Index
].Dhcp4
.Packet
.Offer
;
1102 OfferType
= Private
->OfferBuffer
[Index
].Dhcp4
.OfferType
;
1103 if (!IS_PROXY_DHCP_OFFER (Offer
)) {
1105 // Skip non proxy DHCPv4 offers.
1110 if (OfferType
== PxeOfferTypeProxyBinl
) {
1112 // Try all the cached ProxyBinl offer one by one to request bootfile name.
1114 if (EFI_ERROR (PxeBcRetryBinlOffer (Private
, Index
))) {
1119 Private
->SelectProxyType
= OfferType
;
1121 Status
= EFI_SUCCESS
;
1126 if (!EFI_ERROR (Status
) && Private
->SelectProxyType
!= PxeOfferTypeProxyBinl
) {
1128 // Success to try to request by a ProxyPxe10 or ProxyWfm11a offer, copy and parse it.
1130 Status
= PxeBcCopyProxyOffer (Private
, ProxyIndex
);
1134 // Othewise, the bootfile name must be included in DhcpOnly offer.
1136 if (Options
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] == NULL
) {
1137 Status
= EFI_NOT_FOUND
;
1142 if (!EFI_ERROR (Status
)) {
1144 // All PXE boot information is ready by now.
1146 Mode
= Private
->PxeBc
.Mode
;
1147 Offer
= &Cache4
->Packet
.Offer
;
1148 Ack
= &Private
->DhcpAck
.Dhcp4
.Packet
.Ack
;
1149 if (Cache4
->OfferType
== PxeOfferTypeBootp
) {
1151 // Bootp is a special case that only 2 packets involved instead of 4. So the bootp's reply
1152 // should be taken as ack.
1157 Status
= PxeBcCopyDhcp4Ack (Private
, Ack
, TRUE
);
1158 if (EFI_ERROR (Status
)) {
1161 Mode
->DhcpDiscoverValid
= TRUE
;
1169 EFI_DHCP4_CALLBACK is provided by the consumer of the EFI DHCPv4 Protocol driver
1170 to intercept events that occurred in the configuration process.
1172 @param[in] This Pointer to the EFI DHCPv4 Protocol.
1173 @param[in] Context Pointer to the context set by EFI_DHCP4_PROTOCOL.Configure().
1174 @param[in] CurrentState The current operational state of the EFI DHCPv4 Protocol driver.
1175 @param[in] Dhcp4Event The event that occurs in the current state, which usually means a
1177 @param[in] Packet The DHCPv4 packet that is going to be sent or already received.
1178 @param[out] NewPacket The packet that is used to replace the above Packet.
1180 @retval EFI_SUCCESS Tells the EFI DHCPv4 Protocol driver to continue the DHCP process.
1181 @retval EFI_NOT_READY Only used in the Dhcp4Selecting state. The EFI DHCPv4 Protocol
1182 driver will continue to wait for more DHCPOFFER packets until the
1183 retry timeout expires.
1184 @retval EFI_ABORTED Tells the EFI DHCPv4 Protocol driver to abort the current process
1185 and return to the Dhcp4Init or Dhcp4InitReboot state.
1190 PxeBcDhcp4CallBack (
1191 IN EFI_DHCP4_PROTOCOL
*This
,
1193 IN EFI_DHCP4_STATE CurrentState
,
1194 IN EFI_DHCP4_EVENT Dhcp4Event
,
1195 IN EFI_DHCP4_PACKET
*Packet OPTIONAL
,
1196 OUT EFI_DHCP4_PACKET
**NewPacket OPTIONAL
1199 PXEBC_PRIVATE_DATA
*Private
;
1200 EFI_PXE_BASE_CODE_MODE
*Mode
;
1201 EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL
*Callback
;
1202 EFI_DHCP4_PACKET_OPTION
*MaxMsgSize
;
1207 if ((Dhcp4Event
!= Dhcp4RcvdOffer
) &&
1208 (Dhcp4Event
!= Dhcp4SelectOffer
) &&
1209 (Dhcp4Event
!= Dhcp4SendDiscover
) &&
1210 (Dhcp4Event
!= Dhcp4RcvdAck
)) {
1214 ASSERT (Packet
!= NULL
);
1216 Private
= (PXEBC_PRIVATE_DATA
*) Context
;
1217 Mode
= Private
->PxeBc
.Mode
;
1218 Callback
= Private
->PxeBcCallback
;
1221 // Override the Maximum DHCP Message Size.
1223 MaxMsgSize
= PxeBcParseDhcp4Options (
1224 Packet
->Dhcp4
.Option
,
1225 GET_OPTION_BUFFER_LEN (Packet
),
1228 if (MaxMsgSize
!= NULL
) {
1229 Value
= HTONS (PXEBC_DHCP4_PACKET_MAX_SIZE
);
1230 CopyMem (MaxMsgSize
->Data
, &Value
, sizeof (Value
));
1234 // Callback to user if any packets sent or received.
1236 if (Dhcp4Event
!= Dhcp4SelectOffer
&& Callback
!= NULL
) {
1237 Received
= (BOOLEAN
) (Dhcp4Event
== Dhcp4RcvdOffer
|| Dhcp4Event
== Dhcp4RcvdAck
);
1238 Status
= Callback
->Callback (
1243 (EFI_PXE_BASE_CODE_PACKET
*) &Packet
->Dhcp4
1245 if (Status
!= EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE
) {
1250 Status
= EFI_SUCCESS
;
1252 switch (Dhcp4Event
) {
1254 case Dhcp4SendDiscover
:
1255 if (Packet
->Length
> PXEBC_DHCP4_PACKET_MAX_SIZE
) {
1257 // If the to be sent packet exceeds the maximum length, abort the DHCP process.
1259 Status
= EFI_ABORTED
;
1264 // Cache the DHCPv4 discover packet to mode data directly.
1265 // It need to check SendGuid as well as Dhcp4SendRequest.
1267 CopyMem (&Mode
->DhcpDiscover
.Dhcpv4
, &Packet
->Dhcp4
, Packet
->Length
);
1269 case Dhcp4SendRequest
:
1270 if (Packet
->Length
> PXEBC_DHCP4_PACKET_MAX_SIZE
) {
1272 // If the to be sent packet exceeds the maximum length, abort the DHCP process.
1274 Status
= EFI_ABORTED
;
1278 if (Mode
->SendGUID
) {
1280 // Send the system Guid instead of the MAC address as the hardware address if required.
1282 if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID
*) Packet
->Dhcp4
.Header
.ClientHwAddr
))) {
1284 // Zero the Guid to indicate NOT programable if failed to get system Guid.
1286 DEBUG ((EFI_D_WARN
, "PXE: Failed to read system GUID from the smbios table!\n"));
1287 ZeroMem (Packet
->Dhcp4
.Header
.ClientHwAddr
, sizeof (EFI_GUID
));
1289 Packet
->Dhcp4
.Header
.HwAddrLen
= (UINT8
) sizeof (EFI_GUID
);
1293 case Dhcp4RcvdOffer
:
1294 Status
= EFI_NOT_READY
;
1295 if (Packet
->Length
> PXEBC_DHCP4_PACKET_MAX_SIZE
) {
1297 // Ignore the incoming packets which exceed the maximum length.
1301 if (Private
->OfferNum
< PXEBC_OFFER_MAX_NUM
) {
1303 // Cache the DHCPv4 offers to OfferBuffer[] for select later, and record
1304 // the OfferIndex and OfferCount.
1305 // If error happens, just ignore this packet and continue to wait more offer.
1307 PxeBcCacheDhcp4Offer (Private
, Packet
);
1311 case Dhcp4SelectOffer
:
1312 ASSERT (NewPacket
!= NULL
);
1315 // Select offer by the default policy or by order, and record the SelectIndex
1316 // and SelectProxyType.
1318 PxeBcSelectDhcp4Offer (Private
);
1320 if (Private
->SelectIndex
== 0) {
1321 Status
= EFI_ABORTED
;
1323 *NewPacket
= &Private
->OfferBuffer
[Private
->SelectIndex
- 1].Dhcp4
.Packet
.Offer
;
1329 // Cache the DHCPv4 ack to Private->Dhcp4Ack, but it's not the final ack in mode data
1330 // without verification.
1332 ASSERT (Private
->SelectIndex
!= 0);
1334 Status
= PxeBcCopyDhcp4Ack (Private
, Packet
, FALSE
);
1335 if (EFI_ERROR (Status
)) {
1336 Status
= EFI_ABORTED
;
1349 Build and send out the request packet for the bootfile, and parse the reply.
1351 @param[in] Private Pointer to PxeBc private data.
1352 @param[in] Type PxeBc option boot item type.
1353 @param[in] Layer Pointer to option boot item layer.
1354 @param[in] UseBis Use BIS or not.
1355 @param[in] DestIp Pointer to the server address.
1356 @param[in] IpCount The total count of the server address.
1357 @param[in] SrvList Pointer to EFI_PXE_BASE_CODE_SRVLIST.
1359 @retval EFI_SUCCESS Successfully discovered boot file.
1360 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource.
1361 @retval EFI_NOT_FOUND Can't get the PXE reply packet.
1362 @retval Others Failed to discover boot file.
1366 PxeBcDhcp4Discover (
1367 IN PXEBC_PRIVATE_DATA
*Private
,
1371 IN EFI_IP_ADDRESS
*DestIp
,
1373 IN EFI_PXE_BASE_CODE_SRVLIST
*SrvList
1376 EFI_PXE_BASE_CODE_UDP_PORT Sport
;
1377 EFI_PXE_BASE_CODE_MODE
*Mode
;
1378 EFI_DHCP4_PROTOCOL
*Dhcp4
;
1379 EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN Token
;
1385 EFI_DHCP4_LISTEN_POINT ListenPoint
;
1386 EFI_DHCP4_PACKET
*Response
;
1387 UINT8 Buffer
[PXEBC_DHCP4_OPTION_MAX_SIZE
];
1388 EFI_DHCP4_PACKET_OPTION
*OptList
[PXEBC_DHCP4_OPTION_MAX_NUM
];
1390 EFI_DHCP4_PACKET_OPTION
*PxeOpt
;
1391 PXEBC_OPTION_BOOT_ITEM
*PxeBootItem
;
1395 Mode
= Private
->PxeBc
.Mode
;
1396 Dhcp4
= Private
->Dhcp4
;
1397 Status
= EFI_SUCCESS
;
1399 ZeroMem (&Token
, sizeof (EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN
));
1402 // Use broadcast if destination address not specified.
1404 if (DestIp
== NULL
) {
1405 Sport
= PXEBC_DHCP4_S_PORT
;
1408 Sport
= PXEBC_BS_DISCOVER_PORT
;
1412 if (!UseBis
&& Layer
!= NULL
) {
1413 *Layer
&= EFI_PXE_BASE_CODE_BOOT_LAYER_MASK
;
1417 // Build all the options for the request packet.
1419 OptCount
= PxeBcBuildDhcp4Options (Private
, OptList
, Buffer
, TRUE
);
1421 if (Private
->IsDoDiscover
) {
1423 // Add vendor option of PXE_BOOT_ITEM
1425 VendorOptLen
= (UINT8
) ((sizeof (EFI_DHCP4_PACKET_OPTION
) - 1) * 2 + sizeof (PXEBC_OPTION_BOOT_ITEM
) + 1);
1426 OptList
[OptCount
] = AllocateZeroPool (VendorOptLen
);
1427 if (OptList
[OptCount
] == NULL
) {
1428 return EFI_OUT_OF_RESOURCES
;
1431 OptList
[OptCount
]->OpCode
= DHCP4_TAG_VENDOR
;
1432 OptList
[OptCount
]->Length
= (UINT8
) (VendorOptLen
- 2);
1433 PxeOpt
= (EFI_DHCP4_PACKET_OPTION
*) OptList
[OptCount
]->Data
;
1434 PxeOpt
->OpCode
= PXEBC_VENDOR_TAG_BOOT_ITEM
;
1435 PxeOpt
->Length
= (UINT8
) sizeof (PXEBC_OPTION_BOOT_ITEM
);
1436 PxeBootItem
= (PXEBC_OPTION_BOOT_ITEM
*) PxeOpt
->Data
;
1437 PxeBootItem
->Type
= HTONS (Type
);
1438 PxeOpt
->Data
[PxeOpt
->Length
] = DHCP4_TAG_EOP
;
1440 if (Layer
!= NULL
) {
1441 PxeBootItem
->Layer
= HTONS (*Layer
);
1448 // Build the request packet with seed packet and option list.
1450 Status
= Dhcp4
->Build (
1452 &Private
->SeedPacket
,
1460 // Free the vendor option of PXE_BOOT_ITEM.
1462 if (Private
->IsDoDiscover
) {
1463 FreePool (OptList
[OptCount
- 1]);
1466 if (EFI_ERROR (Status
)) {
1470 if (Mode
->SendGUID
) {
1471 if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID
*) Token
.Packet
->Dhcp4
.Header
.ClientHwAddr
))) {
1473 // Zero the Guid to indicate NOT programable if failed to get system Guid.
1475 DEBUG ((EFI_D_WARN
, "PXE: Failed to read system GUID from the smbios table!\n"));
1476 ZeroMem (Token
.Packet
->Dhcp4
.Header
.ClientHwAddr
, sizeof (EFI_GUID
));
1478 Token
.Packet
->Dhcp4
.Header
.HwAddrLen
= (UINT8
) sizeof (EFI_GUID
);
1482 // Set fields of the token for the request packet.
1484 Xid
= NET_RANDOM (NetRandomInitSeed ());
1485 Token
.Packet
->Dhcp4
.Header
.Xid
= HTONL (Xid
);
1486 Token
.Packet
->Dhcp4
.Header
.Reserved
= HTONS ((UINT16
) ((IsBCast
) ? 0x8000 : 0x0));
1487 CopyMem (&Token
.Packet
->Dhcp4
.Header
.ClientAddr
, &Private
->StationIp
, sizeof (EFI_IPv4_ADDRESS
));
1489 Token
.RemotePort
= Sport
;
1492 SetMem (&Token
.RemoteAddress
, sizeof (EFI_IPv4_ADDRESS
), 0xff);
1494 CopyMem (&Token
.RemoteAddress
, DestIp
, sizeof (EFI_IPv4_ADDRESS
));
1497 CopyMem (&Token
.GatewayAddress
, &Private
->GatewayIp
, sizeof (EFI_IPv4_ADDRESS
));
1500 Token
.ListenPointCount
= 1;
1501 Token
.ListenPoints
= &ListenPoint
;
1502 Token
.ListenPoints
[0].ListenPort
= PXEBC_BS_DISCOVER_PORT
;
1503 CopyMem (&Token
.ListenPoints
[0].ListenAddress
, &Private
->StationIp
, sizeof(EFI_IPv4_ADDRESS
));
1504 CopyMem (&Token
.ListenPoints
[0].SubnetMask
, &Private
->SubnetMask
, sizeof(EFI_IPv4_ADDRESS
));
1508 // Send out the request packet to discover the bootfile.
1510 for (TryIndex
= 1; TryIndex
<= PXEBC_BOOT_REQUEST_RETRIES
; TryIndex
++) {
1512 Token
.TimeoutValue
= (UINT16
) (PXEBC_BOOT_REQUEST_TIMEOUT
* TryIndex
);
1513 Token
.Packet
->Dhcp4
.Header
.Seconds
= (UINT16
) (PXEBC_BOOT_REQUEST_TIMEOUT
* (TryIndex
- 1));
1515 Status
= Dhcp4
->TransmitReceive (Dhcp4
, &Token
);
1516 if (Token
.Status
!= EFI_TIMEOUT
) {
1521 if (TryIndex
> PXEBC_BOOT_REQUEST_RETRIES
) {
1523 // No server response our PXE request
1525 Status
= EFI_TIMEOUT
;
1528 if (!EFI_ERROR (Status
)) {
1532 Response
= Token
.ResponseList
;
1534 // Find the right PXE Reply according to server address.
1536 while (RepIndex
< Token
.ResponseCount
) {
1537 if (Response
->Length
> PXEBC_DHCP4_PACKET_MAX_SIZE
) {
1540 Response
= (EFI_DHCP4_PACKET
*) ((UINT8
*) Response
+ Response
->Size
);
1544 while (SrvIndex
< IpCount
) {
1545 if (SrvList
[SrvIndex
].AcceptAnyResponse
) {
1548 if ((SrvList
[SrvIndex
].Type
== Type
) &&
1549 EFI_IP4_EQUAL (&Response
->Dhcp4
.Header
.ServerAddr
, &SrvList
[SrvIndex
].IpAddr
)) {
1555 if ((IpCount
!= SrvIndex
) || (IpCount
== 0)) {
1561 Response
= (EFI_DHCP4_PACKET
*) ((UINT8
*) Response
+ Response
->Size
);
1564 if (RepIndex
< Token
.ResponseCount
) {
1566 // Cache the right PXE reply packet here, set valid flag later.
1567 // Especially for PXE discover packet, store it into mode data here.
1569 if (Private
->IsDoDiscover
) {
1570 Status
= PxeBcCacheDhcp4Packet (&Private
->PxeReply
.Dhcp4
.Packet
.Ack
, Response
);
1571 if (EFI_ERROR(Status
)) {
1574 CopyMem (&Mode
->PxeDiscover
, &Token
.Packet
->Dhcp4
, Token
.Packet
->Length
);
1576 Status
= PxeBcCacheDhcp4Packet (&Private
->ProxyOffer
.Dhcp4
.Packet
.Offer
, Response
);
1577 if (EFI_ERROR(Status
)) {
1583 // Not found the right PXE reply packet.
1585 Status
= EFI_NOT_FOUND
;
1590 if (Token
.ResponseList
!= NULL
) {
1591 FreePool (Token
.ResponseList
);
1593 if (Token
.Packet
!= NULL
) {
1594 FreePool (Token
.Packet
);
1600 Switch the Ip4 policy to static.
1602 @param[in] Private The pointer to PXEBC_PRIVATE_DATA.
1604 @retval EFI_SUCCESS The policy is already configured to static.
1605 @retval Others Other error as indicated..
1610 IN PXEBC_PRIVATE_DATA
*Private
1614 EFI_IP4_CONFIG2_PROTOCOL
*Ip4Config2
;
1615 EFI_IP4_CONFIG2_POLICY Policy
;
1618 Ip4Config2
= Private
->Ip4Config2
;
1619 DataSize
= sizeof (EFI_IP4_CONFIG2_POLICY
);
1620 Status
= Ip4Config2
->GetData (
1622 Ip4Config2DataTypePolicy
,
1626 if (EFI_ERROR (Status
)) {
1630 if (Policy
!= Ip4Config2PolicyStatic
) {
1631 Policy
= Ip4Config2PolicyStatic
;
1632 Status
= Ip4Config2
->SetData (
1634 Ip4Config2DataTypePolicy
,
1635 sizeof (EFI_IP4_CONFIG2_POLICY
),
1638 if (EFI_ERROR (Status
)) {
1647 Start the D.O.R.A DHCPv4 process to acquire the IPv4 address and other PXE boot information.
1649 @param[in] Private Pointer to PxeBc private data.
1650 @param[in] Dhcp4 Pointer to the EFI_DHCP4_PROTOCOL
1652 @retval EFI_SUCCESS The D.O.R.A process successfully finished.
1653 @retval Others Failed to finish the D.O.R.A process.
1658 IN PXEBC_PRIVATE_DATA
*Private
,
1659 IN EFI_DHCP4_PROTOCOL
*Dhcp4
1662 EFI_PXE_BASE_CODE_MODE
*PxeMode
;
1663 EFI_DHCP4_CONFIG_DATA Config
;
1664 EFI_DHCP4_MODE_DATA Mode
;
1665 EFI_DHCP4_PACKET_OPTION
*OptList
[PXEBC_DHCP4_OPTION_MAX_NUM
];
1666 UINT8 Buffer
[PXEBC_DHCP4_OPTION_MAX_SIZE
];
1670 ASSERT (Dhcp4
!= NULL
);
1672 Status
= EFI_SUCCESS
;
1673 PxeMode
= Private
->PxeBc
.Mode
;
1676 // Build option list for the request packet.
1678 OptCount
= PxeBcBuildDhcp4Options (Private
, OptList
, Buffer
, FALSE
);
1679 ASSERT (OptCount
> 0);
1681 ZeroMem (&Mode
, sizeof (EFI_DHCP4_MODE_DATA
));
1682 ZeroMem (&Config
, sizeof (EFI_DHCP4_CONFIG_DATA
));
1684 Config
.OptionCount
= OptCount
;
1685 Config
.OptionList
= OptList
;
1686 Config
.Dhcp4Callback
= PxeBcDhcp4CallBack
;
1687 Config
.CallbackContext
= Private
;
1688 Config
.DiscoverTryCount
= PXEBC_DHCP_RETRIES
;
1689 Config
.DiscoverTimeout
= mPxeDhcpTimeout
;
1692 // Configure the DHCPv4 instance for PXE boot.
1694 Status
= Dhcp4
->Configure (Dhcp4
, &Config
);
1695 if (EFI_ERROR (Status
)) {
1700 // Initialize the record fields for DHCPv4 offer in private data.
1702 Private
->IsProxyRecved
= FALSE
;
1703 Private
->OfferNum
= 0;
1704 ZeroMem (Private
->OfferCount
, sizeof (Private
->OfferCount
));
1705 ZeroMem (Private
->OfferIndex
, sizeof (Private
->OfferIndex
));
1707 Status
= Dhcp4
->Start (Dhcp4
, NULL
);
1708 if (EFI_ERROR (Status
)) {
1709 if (Status
== EFI_ICMP_ERROR
) {
1710 PxeMode
->IcmpErrorReceived
= TRUE
;
1713 if (Status
== EFI_TIMEOUT
&& Private
->OfferNum
> 0) {
1714 Status
= EFI_NO_RESPONSE
;
1721 // Get the acquired IPv4 address and store them.
1723 Status
= Dhcp4
->GetModeData (Dhcp4
, &Mode
);
1724 if (EFI_ERROR (Status
)) {
1728 ASSERT (Mode
.State
== Dhcp4Bound
);
1730 CopyMem (&Private
->StationIp
, &Mode
.ClientAddress
, sizeof (EFI_IPv4_ADDRESS
));
1731 CopyMem (&Private
->SubnetMask
, &Mode
.SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
1732 CopyMem (&Private
->GatewayIp
, &Mode
.RouterAddress
, sizeof (EFI_IPv4_ADDRESS
));
1733 CopyMem (&PxeMode
->StationIp
, &Private
->StationIp
, sizeof (EFI_IPv4_ADDRESS
));
1734 CopyMem (&PxeMode
->SubnetMask
, &Private
->SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
1736 Status
= PxeBcFlushStationIp (Private
, &Private
->StationIp
, &Private
->SubnetMask
);
1737 if (EFI_ERROR (Status
)) {
1742 // Check the selected offer whether BINL retry is needed.
1744 Status
= PxeBcHandleDhcp4Offer (Private
);
1746 AsciiPrint ("\n Station IP address is ");
1748 PxeBcShowIp4Addr (&Private
->StationIp
.v4
);
1752 if (EFI_ERROR (Status
)) {
1753 Dhcp4
->Stop (Dhcp4
);
1754 Dhcp4
->Configure (Dhcp4
, NULL
);
1756 ZeroMem (&Config
, sizeof (EFI_DHCP4_CONFIG_DATA
));
1757 Dhcp4
->Configure (Dhcp4
, &Config
);
1758 Private
->IsAddressOk
= TRUE
;