]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/UefiPxeBcDxe/PxeBcDhcp4.c
NetworkPkg: Refine codes related to Dhcpv4 and Dhcpv6 configuration.
[mirror_edk2.git] / NetworkPkg / UefiPxeBcDxe / PxeBcDhcp4.c
1 /** @file
2 Functions implementation related with DHCPv4 for UefiPxeBc Driver.
3
4 Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>
5
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.
10
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.
13
14 **/
15
16 #include "PxeBcImpl.h"
17
18 //
19 // This is a map from the interested DHCP4 option tags' index to the tag value.
20 //
21 UINT8 mInterestedDhcp4Tags[PXEBC_DHCP4_TAG_INDEX_MAX] = {
22 DHCP4_TAG_BOOTFILE_LEN,
23 DHCP4_TAG_VENDOR,
24 DHCP4_TAG_OVERLOAD,
25 DHCP4_TAG_MSG_TYPE,
26 DHCP4_TAG_SERVER_ID,
27 DHCP4_TAG_VENDOR_CLASS_ID,
28 DHCP4_TAG_BOOTFILE
29 };
30
31 //
32 // There are 4 times retries with the value of 4, 8, 16 and 32, refers to PXE2.1 spec.
33 //
34 UINT32 mPxeDhcpTimeout[4] = {4, 8, 16, 32};
35
36
37 /**
38 Parse a certain dhcp4 option by OptTag in Buffer, and return with start pointer.
39
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.
43
44 @retval NULL Failed to find the required option.
45 @retval Others The position of the required option.
46
47 **/
48 EFI_DHCP4_PACKET_OPTION *
49 PxeBcParseDhcp4Options (
50 IN UINT8 *Buffer,
51 IN UINT32 Length,
52 IN UINT8 OptTag
53 )
54 {
55 EFI_DHCP4_PACKET_OPTION *Option;
56 UINT32 Offset;
57
58 Option = (EFI_DHCP4_PACKET_OPTION *) Buffer;
59 Offset = 0;
60
61 while (Offset < Length && Option->OpCode != DHCP4_TAG_EOP) {
62
63 if (Option->OpCode == OptTag) {
64 //
65 // Found the required option.
66 //
67 return Option;
68 }
69
70 //
71 // Skip the current option to the next.
72 //
73 if (Option->OpCode == DHCP4_TAG_PAD) {
74 Offset++;
75 } else {
76 Offset += Option->Length + 2;
77 }
78
79 Option = (EFI_DHCP4_PACKET_OPTION *) (Buffer + Offset);
80 }
81
82 return NULL;
83 }
84
85
86 /**
87 Parse the PXE vender options and extract the information from them.
88
89 @param[in] Dhcp4Option Pointer to vendor options in buffer.
90 @param[in] VendorOption Pointer to structure to store information in vendor options.
91
92 **/
93 VOID
94 PxeBcParseVendorOptions (
95 IN EFI_DHCP4_PACKET_OPTION *Dhcp4Option,
96 IN PXEBC_VENDOR_OPTION *VendorOption
97 )
98 {
99 UINT32 *BitMap;
100 UINT8 VendorOptionLen;
101 EFI_DHCP4_PACKET_OPTION *PxeOption;
102 UINT8 Offset;
103
104 BitMap = VendorOption->BitMap;
105 VendorOptionLen = Dhcp4Option->Length;
106 PxeOption = (EFI_DHCP4_PACKET_OPTION *) &Dhcp4Option->Data[0];
107 Offset = 0;
108
109 ASSERT (PxeOption != NULL);
110
111 while ((Offset < VendorOptionLen) && (PxeOption->OpCode != DHCP4_TAG_EOP)) {
112 //
113 // Parse all the interesting PXE vendor options one by one.
114 //
115 switch (PxeOption->OpCode) {
116
117 case PXEBC_VENDOR_TAG_MTFTP_IP:
118
119 CopyMem (&VendorOption->MtftpIp, PxeOption->Data, sizeof (EFI_IPv4_ADDRESS));
120 break;
121
122 case PXEBC_VENDOR_TAG_MTFTP_CPORT:
123
124 CopyMem (&VendorOption->MtftpCPort, PxeOption->Data, sizeof (VendorOption->MtftpCPort));
125 break;
126
127 case PXEBC_VENDOR_TAG_MTFTP_SPORT:
128
129 CopyMem (&VendorOption->MtftpSPort, PxeOption->Data, sizeof (VendorOption->MtftpSPort));
130 break;
131
132 case PXEBC_VENDOR_TAG_MTFTP_TIMEOUT:
133
134 VendorOption->MtftpTimeout = *PxeOption->Data;
135 break;
136
137 case PXEBC_VENDOR_TAG_MTFTP_DELAY:
138
139 VendorOption->MtftpDelay = *PxeOption->Data;
140 break;
141
142 case PXEBC_VENDOR_TAG_DISCOVER_CTRL:
143
144 VendorOption->DiscoverCtrl = *PxeOption->Data;
145 break;
146
147 case PXEBC_VENDOR_TAG_DISCOVER_MCAST:
148
149 CopyMem (&VendorOption->DiscoverMcastIp, PxeOption->Data, sizeof (EFI_IPv4_ADDRESS));
150 break;
151
152 case PXEBC_VENDOR_TAG_BOOT_SERVERS:
153
154 VendorOption->BootSvrLen = PxeOption->Length;
155 VendorOption->BootSvr = (PXEBC_BOOT_SVR_ENTRY *) PxeOption->Data;
156 break;
157
158 case PXEBC_VENDOR_TAG_BOOT_MENU:
159
160 VendorOption->BootMenuLen = PxeOption->Length;
161 VendorOption->BootMenu = (PXEBC_BOOT_MENU_ENTRY *) PxeOption->Data;
162 break;
163
164 case PXEBC_VENDOR_TAG_MENU_PROMPT:
165
166 VendorOption->MenuPromptLen = PxeOption->Length;
167 VendorOption->MenuPrompt = (PXEBC_MENU_PROMPT *) PxeOption->Data;
168 break;
169
170 case PXEBC_VENDOR_TAG_MCAST_ALLOC:
171
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));
175 break;
176
177 case PXEBC_VENDOR_TAG_CREDENTIAL_TYPES:
178
179 VendorOption->CredTypeLen = PxeOption->Length;
180 VendorOption->CredType = (UINT32 *) PxeOption->Data;
181 break;
182
183 case PXEBC_VENDOR_TAG_BOOT_ITEM:
184
185 CopyMem (&VendorOption->BootSrvType, PxeOption->Data, sizeof (VendorOption->BootSrvType));
186 CopyMem (&VendorOption->BootSrvLayer, PxeOption->Data + 2, sizeof (VendorOption->BootSrvLayer));
187 break;
188
189 default:
190 //
191 // Not interesting PXE vendor options.
192 //
193 break;
194 }
195
196 //
197 // Set the bit map for the special PXE options.
198 //
199 SET_VENDOR_OPTION_BIT_MAP (BitMap, PxeOption->OpCode);
200
201 //
202 // Continue to the next option.
203 //
204 if (PxeOption->OpCode == DHCP4_TAG_PAD) {
205 Offset++;
206 } else {
207 Offset = (UINT8) (Offset + PxeOption->Length + 2);
208 }
209
210 PxeOption = (EFI_DHCP4_PACKET_OPTION *) (Dhcp4Option->Data + Offset);
211 }
212 }
213
214
215 /**
216 Build the options buffer for the DHCPv4 request packet.
217
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.
223
224 @return Index The count of the built-in options.
225
226 **/
227 UINT32
228 PxeBcBuildDhcp4Options (
229 IN PXEBC_PRIVATE_DATA *Private,
230 OUT EFI_DHCP4_PACKET_OPTION **OptList,
231 IN UINT8 *Buffer,
232 IN BOOLEAN NeedMsgType
233 )
234 {
235 UINT32 Index;
236 PXEBC_DHCP4_OPTION_ENTRY OptEnt;
237 UINT16 Value;
238
239 Index = 0;
240 OptList[0] = (EFI_DHCP4_PACKET_OPTION *) Buffer;
241
242 if (NeedMsgType) {
243 //
244 // Append message type.
245 //
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;
250 Index++;
251 OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);
252
253 //
254 // Append max message size.
255 //
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 - 8);
260 CopyMem (&OptEnt.MaxMesgSize->Size, &Value, sizeof (UINT16));
261 Index++;
262 OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);
263 }
264
265 //
266 // Append parameter request list option.
267 //
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;
306 Index++;
307 OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);
308
309 //
310 // Append UUID/Guid-based client identifier option
311 //
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;
316 Index++;
317 OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);
318
319 if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID *) OptEnt.Uuid->Guid))) {
320 //
321 // Zero the Guid to indicate NOT programable if failed to get system Guid.
322 //
323 ZeroMem (OptEnt.Uuid->Guid, sizeof (EFI_GUID));
324 }
325
326 //
327 // Append client network device interface option
328 //
329 OptList[Index]->OpCode = DHCP4_TAG_UNDI;
330 OptList[Index]->Length = (UINT8) sizeof (PXEBC_DHCP4_OPTION_UNDI);
331 OptEnt.Undi = (PXEBC_DHCP4_OPTION_UNDI *) OptList[Index]->Data;
332
333 if (Private->Nii != NULL) {
334 OptEnt.Undi->Type = Private->Nii->Type;
335 OptEnt.Undi->MajorVer = Private->Nii->MajorVer;
336 OptEnt.Undi->MinorVer = Private->Nii->MinorVer;
337 } else {
338 OptEnt.Undi->Type = DEFAULT_UNDI_TYPE;
339 OptEnt.Undi->MajorVer = DEFAULT_UNDI_MAJOR;
340 OptEnt.Undi->MinorVer = DEFAULT_UNDI_MINOR;
341 }
342
343 Index++;
344 OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);
345
346 //
347 // Append client system architecture option
348 //
349 OptList[Index]->OpCode = DHCP4_TAG_ARCH;
350 OptList[Index]->Length = (UINT8) sizeof (PXEBC_DHCP4_OPTION_ARCH);
351 OptEnt.Arch = (PXEBC_DHCP4_OPTION_ARCH *) OptList[Index]->Data;
352 Value = HTONS (EFI_PXE_CLIENT_SYSTEM_ARCHITECTURE);
353 CopyMem (&OptEnt.Arch->Type, &Value, sizeof (UINT16));
354 Index++;
355 OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);
356
357 //
358 // Append vendor class identify option
359 //
360 OptList[Index]->OpCode = DHCP4_TAG_VENDOR_CLASS_ID;
361 OptList[Index]->Length = (UINT8) sizeof (PXEBC_DHCP4_OPTION_CLID);
362 OptEnt.Clid = (PXEBC_DHCP4_OPTION_CLID *) OptList[Index]->Data;
363 CopyMem (
364 OptEnt.Clid,
365 DEFAULT_CLASS_ID_DATA,
366 sizeof (PXEBC_DHCP4_OPTION_CLID)
367 );
368 PxeBcUintnToAscDecWithFormat (
369 EFI_PXE_CLIENT_SYSTEM_ARCHITECTURE,
370 OptEnt.Clid->ArchitectureType,
371 sizeof (OptEnt.Clid->ArchitectureType)
372 );
373
374 if (Private->Nii != NULL) {
375 CopyMem (OptEnt.Clid->InterfaceName, Private->Nii->StringId, sizeof (OptEnt.Clid->InterfaceName));
376 PxeBcUintnToAscDecWithFormat (Private->Nii->MajorVer, OptEnt.Clid->UndiMajor, sizeof (OptEnt.Clid->UndiMajor));
377 PxeBcUintnToAscDecWithFormat (Private->Nii->MinorVer, OptEnt.Clid->UndiMinor, sizeof (OptEnt.Clid->UndiMinor));
378 }
379
380 Index++;
381
382 return Index;
383 }
384
385
386 /**
387 Create a template DHCPv4 packet as a seed.
388
389 @param[out] Seed Pointer to the seed packet.
390 @param[in] Udp4 Pointer to EFI_UDP4_PROTOCOL.
391
392 **/
393 VOID
394 PxeBcSeedDhcp4Packet (
395 OUT EFI_DHCP4_PACKET *Seed,
396 IN EFI_UDP4_PROTOCOL *Udp4
397 )
398 {
399 EFI_SIMPLE_NETWORK_MODE Mode;
400 EFI_DHCP4_HEADER *Header;
401
402 //
403 // Get IfType and HwAddressSize from SNP mode data.
404 //
405 Udp4->GetModeData (Udp4, NULL, NULL, NULL, &Mode);
406
407 Seed->Size = sizeof (EFI_DHCP4_PACKET);
408 Seed->Length = sizeof (Seed->Dhcp4);
409 Header = &Seed->Dhcp4.Header;
410 ZeroMem (Header, sizeof (EFI_DHCP4_HEADER));
411 Header->OpCode = PXEBC_DHCP4_OPCODE_REQUEST;
412 Header->HwType = Mode.IfType;
413 Header->HwAddrLen = (UINT8) Mode.HwAddressSize;
414 CopyMem (Header->ClientHwAddr, &Mode.CurrentAddress, Header->HwAddrLen);
415
416 Seed->Dhcp4.Magik = PXEBC_DHCP4_MAGIC;
417 Seed->Dhcp4.Option[0] = DHCP4_TAG_EOP;
418 }
419
420
421 /**
422 Cache the DHCPv4 packet.
423
424 @param[in] Dst Pointer to the cache buffer for DHCPv4 packet.
425 @param[in] Src Pointer to the DHCPv4 packet to be cached.
426
427 **/
428 VOID
429 PxeBcCacheDhcp4Packet (
430 IN EFI_DHCP4_PACKET *Dst,
431 IN EFI_DHCP4_PACKET *Src
432 )
433 {
434 ASSERT (Dst->Size >= Src->Length);
435
436 CopyMem (&Dst->Dhcp4, &Src->Dhcp4, Src->Length);
437 Dst->Length = Src->Length;
438 }
439
440
441 /**
442 Parse the cached DHCPv4 packet, including all the options.
443
444 @param[in] Cache4 Pointer to cached DHCPv4 packet.
445
446 @retval EFI_SUCCESS Parsed the DHCPv4 packet successfully.
447 @retval EFI_DEVICE_ERROR Failed to parse and invalid packet.
448
449 **/
450 EFI_STATUS
451 PxeBcParseDhcp4Packet (
452 IN PXEBC_DHCP4_PACKET_CACHE *Cache4
453 )
454 {
455 EFI_DHCP4_PACKET *Offer;
456 EFI_DHCP4_PACKET_OPTION **Options;
457 EFI_DHCP4_PACKET_OPTION *Option;
458 PXEBC_OFFER_TYPE OfferType;
459 UINTN Index;
460 BOOLEAN IsProxyOffer;
461 BOOLEAN IsPxeOffer;
462 UINT8 *Ptr8;
463 BOOLEAN FileFieldOverloaded;
464
465 IsProxyOffer = FALSE;
466 IsPxeOffer = FALSE;
467 FileFieldOverloaded = FALSE;
468
469 ZeroMem (Cache4->OptList, sizeof (Cache4->OptList));
470 ZeroMem (&Cache4->VendorOpt, sizeof (Cache4->VendorOpt));
471
472 Offer = &Cache4->Packet.Offer;
473 Options = Cache4->OptList;
474
475 //
476 // Parse DHCPv4 options in this offer, and store the pointers.
477 // First, try to parse DHCPv4 options from the DHCP optional parameters field.
478 //
479 for (Index = 0; Index < PXEBC_DHCP4_TAG_INDEX_MAX; Index++) {
480 Options[Index] = PxeBcParseDhcp4Options (
481 Offer->Dhcp4.Option,
482 GET_OPTION_BUFFER_LEN (Offer),
483 mInterestedDhcp4Tags[Index]
484 );
485 }
486 //
487 // Second, Check if bootfilename and serverhostname is overloaded to carry DHCP options refers to rfc-2132.
488 // If yes, try to parse options from the BootFileName field, then ServerName field.
489 //
490 Option = Options[PXEBC_DHCP4_TAG_INDEX_OVERLOAD];
491 if (Option != NULL) {
492 if ((Option->Data[0] & PXEBC_DHCP4_OVERLOAD_FILE) != 0) {
493 FileFieldOverloaded = TRUE;
494 for (Index = 0; Index < PXEBC_DHCP4_TAG_INDEX_MAX; Index++) {
495 if (Options[Index] == NULL) {
496 Options[Index] = PxeBcParseDhcp4Options (
497 (UINT8 *) Offer->Dhcp4.Header.BootFileName,
498 sizeof (Offer->Dhcp4.Header.BootFileName),
499 mInterestedDhcp4Tags[Index]
500 );
501 }
502 }
503 }
504 if ((Option->Data[0] & PXEBC_DHCP4_OVERLOAD_SERVER_NAME) != 0) {
505 for (Index = 0; Index < PXEBC_DHCP4_TAG_INDEX_MAX; Index++) {
506 if (Options[Index] == NULL) {
507 Options[Index] = PxeBcParseDhcp4Options (
508 (UINT8 *) Offer->Dhcp4.Header.ServerName,
509 sizeof (Offer->Dhcp4.Header.ServerName),
510 mInterestedDhcp4Tags[Index]
511 );
512 }
513 }
514 }
515 }
516
517 //
518 // The offer with zero "yiaddr" is a proxy offer.
519 //
520 if (Offer->Dhcp4.Header.YourAddr.Addr[0] == 0) {
521 IsProxyOffer = TRUE;
522 }
523
524 //
525 // The offer with "PXEClient" is a PXE offer.
526 //
527 Option = Options[PXEBC_DHCP4_TAG_INDEX_CLASS_ID];
528 if ((Option != NULL) && (Option->Length >= 9) &&
529 (CompareMem (Option->Data, DEFAULT_CLASS_ID_DATA, 9) == 0)) {
530 IsPxeOffer = TRUE;
531 }
532
533 //
534 // Parse PXE vendor options in this offer, and store the contents/pointers.
535 //
536 Option = Options[PXEBC_DHCP4_TAG_INDEX_VENDOR];
537 if (IsPxeOffer && Option != NULL) {
538 PxeBcParseVendorOptions (Option, &Cache4->VendorOpt);
539 }
540
541 //
542 // Parse PXE boot file name:
543 // According to PXE spec, boot file name should be read from DHCP option 67 (bootfile name) if present.
544 // Otherwise, read from boot file field in DHCP header.
545 //
546 if (Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] != NULL) {
547 //
548 // RFC 2132, Section 9.5 does not strictly state Bootfile name (option 67) is null
549 // terminated string. So force to append null terminated character at the end of string.
550 //
551 Ptr8 = (UINT8*)&Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE]->Data[0];
552 Ptr8 += Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE]->Length;
553 if (*(Ptr8 - 1) != '\0') {
554 *Ptr8 = '\0';
555 }
556 } else if (!FileFieldOverloaded && Offer->Dhcp4.Header.BootFileName[0] != 0) {
557 //
558 // If the bootfile is not present and bootfilename is present in DHCPv4 packet, just parse it.
559 // Do not count dhcp option header here, or else will destroy the serverhostname.
560 //
561 Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] = (EFI_DHCP4_PACKET_OPTION *)
562 (&Offer->Dhcp4.Header.BootFileName[0] -
563 OFFSET_OF (EFI_DHCP4_PACKET_OPTION, Data[0]));
564
565 }
566
567 //
568 // Determine offer type of the DHCPv4 packet.
569 //
570 Option = Options[PXEBC_DHCP4_TAG_INDEX_MSG_TYPE];
571 if (Option == NULL || Option->Data[0] == 0) {
572 //
573 // It's a Bootp offer.
574 //
575 OfferType = PxeOfferTypeBootp;
576
577 Option = Cache4->OptList[PXEBC_DHCP4_TAG_INDEX_BOOTFILE];
578 if (Option == NULL) {
579 //
580 // If the Bootp offer without bootfilename, discard it.
581 //
582 return EFI_DEVICE_ERROR;
583 }
584 } else {
585
586 if (IS_VALID_DISCOVER_VENDOR_OPTION (Cache4->VendorOpt.BitMap)) {
587 //
588 // It's a PXE10 offer with PXEClient and discover vendor option.
589 //
590 OfferType = IsProxyOffer ? PxeOfferTypeProxyPxe10 : PxeOfferTypeDhcpPxe10;
591 } else if (IS_VALID_MTFTP_VENDOR_OPTION (Cache4->VendorOpt.BitMap)) {
592 //
593 // It's a WFM11a offer with PXEClient and mtftp vendor option.
594 // But multi-cast download is not supported currently, so discard it.
595 //
596 return EFI_DEVICE_ERROR;
597 } else if (IsPxeOffer) {
598 //
599 // It's a BINL offer only with PXEClient.
600 //
601 OfferType = IsProxyOffer ? PxeOfferTypeProxyBinl : PxeOfferTypeDhcpBinl;
602 } else {
603 //
604 // It's a DHCPv4 only offer, which is a pure DHCPv4 offer packet.
605 //
606 OfferType = PxeOfferTypeDhcpOnly;
607 }
608 }
609
610 Cache4->OfferType = OfferType;
611
612 return EFI_SUCCESS;
613 }
614
615
616 /**
617 Cache the DHCPv4 ack packet, and parse it on demand.
618
619 @param[in] Private Pointer to PxeBc private data.
620 @param[in] Ack Pointer to the DHCPv4 ack packet.
621 @param[in] Verified If TRUE, parse the ACK packet and store info into mode data.
622
623 **/
624 VOID
625 PxeBcCopyDhcp4Ack (
626 IN PXEBC_PRIVATE_DATA *Private,
627 IN EFI_DHCP4_PACKET *Ack,
628 IN BOOLEAN Verified
629 )
630 {
631 EFI_PXE_BASE_CODE_MODE *Mode;
632
633 Mode = Private->PxeBc.Mode;
634
635 PxeBcCacheDhcp4Packet (&Private->DhcpAck.Dhcp4.Packet.Ack, Ack);
636
637 if (Verified) {
638 //
639 // Parse the ack packet and store it into mode data if needed.
640 //
641 PxeBcParseDhcp4Packet (&Private->DhcpAck.Dhcp4);
642 CopyMem (&Mode->DhcpAck.Dhcpv4, &Ack->Dhcp4, Ack->Length);
643 Mode->DhcpAckReceived = TRUE;
644 }
645 }
646
647
648 /**
649 Cache the DHCPv4 proxy offer packet according to the received order.
650
651 @param[in] Private Pointer to PxeBc private data.
652 @param[in] OfferIndex The received order of offer packets.
653
654 **/
655 VOID
656 PxeBcCopyProxyOffer (
657 IN PXEBC_PRIVATE_DATA *Private,
658 IN UINT32 OfferIndex
659 )
660 {
661 EFI_PXE_BASE_CODE_MODE *Mode;
662 EFI_DHCP4_PACKET *Offer;
663
664 ASSERT (OfferIndex < Private->OfferNum);
665 ASSERT (OfferIndex < PXEBC_OFFER_MAX_NUM);
666
667 Mode = Private->PxeBc.Mode;
668 Offer = &Private->OfferBuffer[OfferIndex].Dhcp4.Packet.Offer;
669
670 //
671 // Cache the proxy offer packet and parse it.
672 //
673 PxeBcCacheDhcp4Packet (&Private->ProxyOffer.Dhcp4.Packet.Offer, Offer);
674 PxeBcParseDhcp4Packet (&Private->ProxyOffer.Dhcp4);
675
676 //
677 // Store this packet into mode data.
678 //
679 CopyMem (&Mode->ProxyOffer.Dhcpv4, &Offer->Dhcp4, Offer->Length);
680 Mode->ProxyOfferReceived = TRUE;
681 }
682
683
684 /**
685 Retry to request bootfile name by the BINL offer.
686
687 @param[in] Private Pointer to PxeBc private data.
688 @param[in] Index The received order of offer packets.
689
690 @retval EFI_SUCCESS Successfully retried to request bootfile name.
691 @retval EFI_DEVICE_ERROR Failed to retry bootfile name.
692
693 **/
694 EFI_STATUS
695 PxeBcRetryBinlOffer (
696 IN PXEBC_PRIVATE_DATA *Private,
697 IN UINT32 Index
698 )
699 {
700 EFI_DHCP4_PACKET *Offer;
701 EFI_IP_ADDRESS ServerIp;
702 EFI_STATUS Status;
703 PXEBC_DHCP4_PACKET_CACHE *Cache4;
704 EFI_DHCP4_PACKET *Reply;
705
706 ASSERT (Index < PXEBC_OFFER_MAX_NUM);
707 ASSERT (Private->OfferBuffer[Index].Dhcp4.OfferType == PxeOfferTypeDhcpBinl ||
708 Private->OfferBuffer[Index].Dhcp4.OfferType == PxeOfferTypeProxyBinl);
709
710 Offer = &Private->OfferBuffer[Index].Dhcp4.Packet.Offer;
711
712 //
713 // Prefer to siaddr in header as next server address. If it's zero, then use option 54.
714 //
715 if (Offer->Dhcp4.Header.ServerAddr.Addr[0] == 0) {
716 CopyMem (
717 &ServerIp.Addr[0],
718 Private->OfferBuffer[Index].Dhcp4.OptList[PXEBC_DHCP4_TAG_INDEX_SERVER_ID]->Data,
719 sizeof (EFI_IPv4_ADDRESS)
720 );
721 } else {
722 CopyMem (
723 &ServerIp.Addr[0],
724 &Offer->Dhcp4.Header.ServerAddr,
725 sizeof (EFI_IPv4_ADDRESS)
726 );
727 }
728
729 Private->IsDoDiscover = FALSE;
730 Cache4 = &Private->ProxyOffer.Dhcp4;
731 Reply = &Cache4->Packet.Offer;
732
733 //
734 // Send another request packet for bootfile name.
735 //
736 Status = PxeBcDhcp4Discover (
737 Private,
738 0,
739 NULL,
740 FALSE,
741 &ServerIp,
742 0,
743 NULL
744 );
745 if (EFI_ERROR (Status)) {
746 return Status;
747 }
748
749 //
750 // Parse the reply for the last request packet.
751 //
752 Status = PxeBcParseDhcp4Packet (Cache4);
753 if (EFI_ERROR (Status)) {
754 return Status;
755 }
756
757 if (Cache4->OfferType != PxeOfferTypeProxyPxe10 &&
758 Cache4->OfferType != PxeOfferTypeProxyWfm11a &&
759 Cache4->OptList[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] == NULL) {
760 //
761 // This BINL ack doesn't have discovery option set or multicast option set
762 // or bootfile name specified.
763 //
764 return EFI_DEVICE_ERROR;
765 }
766
767 //
768 // Store the reply into mode data.
769 //
770 Private->PxeBc.Mode->ProxyOfferReceived = TRUE;
771 CopyMem (&Private->PxeBc.Mode->ProxyOffer.Dhcpv4, &Reply->Dhcp4, Reply->Length);
772
773 return EFI_SUCCESS;
774 }
775
776
777 /**
778 Cache all the received DHCPv4 offers, and set OfferIndex and OfferCount.
779
780 @param[in] Private Pointer to PxeBc private data.
781 @param[in] RcvdOffer Pointer to the received offer packet.
782
783 **/
784 VOID
785 PxeBcCacheDhcp4Offer (
786 IN PXEBC_PRIVATE_DATA *Private,
787 IN EFI_DHCP4_PACKET *RcvdOffer
788 )
789 {
790 PXEBC_DHCP4_PACKET_CACHE *Cache4;
791 EFI_DHCP4_PACKET *Offer;
792 PXEBC_OFFER_TYPE OfferType;
793
794 ASSERT (Private->OfferNum < PXEBC_OFFER_MAX_NUM);
795 Cache4 = &Private->OfferBuffer[Private->OfferNum].Dhcp4;
796 Offer = &Cache4->Packet.Offer;
797
798 //
799 // Cache the content of DHCPv4 packet firstly.
800 //
801 PxeBcCacheDhcp4Packet (Offer, RcvdOffer);
802
803 //
804 // Validate the DHCPv4 packet, and parse the options and offer type.
805 //
806 if (EFI_ERROR (PxeBcParseDhcp4Packet (Cache4))) {
807 return;
808 }
809
810 //
811 // Determine whether cache the current offer by type, and record OfferIndex and OfferCount.
812 //
813 OfferType = Cache4->OfferType;
814 ASSERT (OfferType < PxeOfferTypeMax);
815
816 if (OfferType == PxeOfferTypeBootp) {
817 //
818 // It's a Bootp offer, only cache the first one, and discard the others.
819 //
820 if (Private->OfferCount[OfferType] == 0) {
821 Private->OfferIndex[OfferType][0] = Private->OfferNum;
822 Private->OfferCount[OfferType] = 1;
823 } else {
824 return;
825 }
826 } else {
827 ASSERT (Private->OfferCount[OfferType] < PXEBC_OFFER_MAX_NUM);
828 if (IS_PROXY_DHCP_OFFER (Offer)) {
829 //
830 // It's a proxy offer without yiaddr, including PXE10, WFM11a or BINL offer.
831 //
832 Private->IsProxyRecved = TRUE;
833
834 if (OfferType == PxeOfferTypeProxyBinl) {
835 //
836 // Cache all proxy BINL offers.
837 //
838 Private->OfferIndex[OfferType][Private->OfferCount[OfferType]] = Private->OfferNum;
839 Private->OfferCount[OfferType]++;
840 } else if (Private->OfferCount[OfferType] > 0) {
841 //
842 // Only cache the first PXE10/WFM11a offer, and discard the others.
843 //
844 Private->OfferIndex[OfferType][0] = Private->OfferNum;
845 Private->OfferCount[OfferType] = 1;
846 } else {
847 return ;
848 }
849 } else {
850 //
851 // It's a DHCPv4 offer with yiaddr, and cache them all.
852 //
853 Private->OfferIndex[OfferType][Private->OfferCount[OfferType]] = Private->OfferNum;
854 Private->OfferCount[OfferType]++;
855 }
856 }
857
858 Private->OfferNum++;
859 }
860
861
862 /**
863 Select an DHCPv4 offer, and record SelectIndex and SelectProxyType.
864
865 @param[in] Private Pointer to PxeBc private data.
866
867 **/
868 VOID
869 PxeBcSelectDhcp4Offer (
870 IN PXEBC_PRIVATE_DATA *Private
871 )
872 {
873 UINT32 Index;
874 UINT32 OfferIndex;
875 EFI_DHCP4_PACKET *Offer;
876
877 Private->SelectIndex = 0;
878
879 if (Private->IsOfferSorted) {
880 //
881 // Select offer by default policy.
882 //
883 if (Private->OfferCount[PxeOfferTypeDhcpPxe10] > 0) {
884 //
885 // 1. DhcpPxe10 offer
886 //
887 Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpPxe10][0] + 1;
888
889 } else if (Private->OfferCount[PxeOfferTypeDhcpWfm11a] > 0) {
890 //
891 // 2. DhcpWfm11a offer
892 //
893 Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpWfm11a][0] + 1;
894
895 } else if (Private->OfferCount[PxeOfferTypeDhcpOnly] > 0 &&
896 Private->OfferCount[PxeOfferTypeProxyPxe10] > 0) {
897 //
898 // 3. DhcpOnly offer and ProxyPxe10 offer.
899 //
900 Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpOnly][0] + 1;
901 Private->SelectProxyType = PxeOfferTypeProxyPxe10;
902
903 } else if (Private->OfferCount[PxeOfferTypeDhcpOnly] > 0 &&
904 Private->OfferCount[PxeOfferTypeProxyWfm11a] > 0) {
905 //
906 // 4. DhcpOnly offer and ProxyWfm11a offer.
907 //
908 Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpOnly][0] + 1;
909 Private->SelectProxyType = PxeOfferTypeProxyWfm11a;
910
911 } else if (Private->OfferCount[PxeOfferTypeDhcpBinl] > 0) {
912 //
913 // 5. DhcpBinl offer.
914 //
915 Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpBinl][0] + 1;
916
917 } else if (Private->OfferCount[PxeOfferTypeDhcpOnly] > 0 &&
918 Private->OfferCount[PxeOfferTypeProxyBinl] > 0) {
919 //
920 // 6. DhcpOnly offer and ProxyBinl offer.
921 //
922 Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpOnly][0] + 1;
923 Private->SelectProxyType = PxeOfferTypeProxyBinl;
924
925 } else {
926 //
927 // 7. DhcpOnly offer with bootfilename.
928 //
929 for (Index = 0; Index < Private->OfferCount[PxeOfferTypeDhcpOnly]; Index++) {
930 OfferIndex = Private->OfferIndex[PxeOfferTypeDhcpOnly][Index];
931 if (Private->OfferBuffer[OfferIndex].Dhcp4.OptList[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] != NULL) {
932 Private->SelectIndex = OfferIndex + 1;
933 break;
934 }
935 }
936 //
937 // 8. Bootp offer with bootfilename.
938 //
939 OfferIndex = Private->OfferIndex[PxeOfferTypeBootp][0];
940 if (Private->SelectIndex == 0 &&
941 Private->OfferCount[PxeOfferTypeBootp] > 0 &&
942 Private->OfferBuffer[OfferIndex].Dhcp4.OptList[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] != NULL) {
943 Private->SelectIndex = OfferIndex + 1;
944 }
945 }
946 } else {
947 //
948 // Select offer by received order.
949 //
950 for (Index = 0; Index < Private->OfferNum; Index++) {
951
952 Offer = &Private->OfferBuffer[Index].Dhcp4.Packet.Offer;
953
954 if (IS_PROXY_DHCP_OFFER (Offer)) {
955 //
956 // Skip proxy offers
957 //
958 continue;
959 }
960
961 if (!Private->IsProxyRecved &&
962 Private->OfferBuffer[Index].Dhcp4.OfferType == PxeOfferTypeDhcpOnly &&
963 Private->OfferBuffer[Index].Dhcp4.OptList[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] == NULL) {
964 //
965 // Skip if DhcpOnly offer without any other proxy offers or bootfilename.
966 //
967 continue;
968 }
969
970 //
971 // Record the index of the select offer.
972 //
973 Private->SelectIndex = Index + 1;
974 break;
975 }
976 }
977 }
978
979
980 /**
981 Handle the DHCPv4 offer packet.
982
983 @param[in] Private Pointer to PxeBc private data.
984
985 @retval EFI_SUCCESS Handled the DHCPv4 offer packet successfully.
986 @retval EFI_NO_RESPONSE No response to the following request packet.
987 @retval EFI_NOT_FOUND No boot filename received.
988
989 **/
990 EFI_STATUS
991 PxeBcHandleDhcp4Offer (
992 IN PXEBC_PRIVATE_DATA *Private
993 )
994 {
995 PXEBC_DHCP4_PACKET_CACHE *Cache4;
996 EFI_DHCP4_PACKET_OPTION **Options;
997 UINT32 Index;
998 EFI_DHCP4_PACKET *Offer;
999 PXEBC_OFFER_TYPE OfferType;
1000 UINT32 ProxyIndex;
1001 UINT32 SelectIndex;
1002 EFI_STATUS Status;
1003 EFI_PXE_BASE_CODE_MODE *Mode;
1004 EFI_DHCP4_PACKET *Ack;
1005
1006 ASSERT (Private->SelectIndex > 0);
1007 SelectIndex = (UINT32) (Private->SelectIndex - 1);
1008 ASSERT (SelectIndex < PXEBC_OFFER_MAX_NUM);
1009 Cache4 = &Private->OfferBuffer[SelectIndex].Dhcp4;
1010 Options = Cache4->OptList;
1011 Status = EFI_SUCCESS;
1012
1013 if (Cache4->OfferType == PxeOfferTypeDhcpBinl) {
1014 //
1015 // DhcpBinl offer is selected, so need try to request bootfilename by this offer.
1016 //
1017 if (EFI_ERROR (PxeBcRetryBinlOffer (Private, SelectIndex))) {
1018 Status = EFI_NO_RESPONSE;
1019 }
1020 } else if (Cache4->OfferType == PxeOfferTypeDhcpOnly) {
1021
1022 if (Private->IsProxyRecved) {
1023 //
1024 // DhcpOnly offer is selected, so need try to request bootfile name.
1025 //
1026 ProxyIndex = 0;
1027 if (Private->IsOfferSorted) {
1028 //
1029 // The proxy offer should be determined if select by default policy.
1030 // IsOfferSorted means all offers are labeled by OfferIndex.
1031 //
1032 ASSERT (Private->SelectProxyType < PxeOfferTypeMax);
1033 ASSERT (Private->OfferCount[Private->SelectProxyType] > 0);
1034
1035 if (Private->SelectProxyType == PxeOfferTypeProxyBinl) {
1036 //
1037 // Try all the cached ProxyBinl offer one by one to request bootfile name.
1038 //
1039 for (Index = 0; Index < Private->OfferCount[Private->SelectProxyType]; Index++) {
1040 ASSERT (Index < PXEBC_OFFER_MAX_NUM);
1041 ProxyIndex = Private->OfferIndex[Private->SelectProxyType][Index];
1042 if (!EFI_ERROR (PxeBcRetryBinlOffer (Private, ProxyIndex))) {
1043 break;
1044 }
1045 }
1046 if (Index == Private->OfferCount[Private->SelectProxyType]) {
1047 Status = EFI_NO_RESPONSE;
1048 }
1049 } else {
1050 //
1051 // For other proxy offers, only one is buffered.
1052 //
1053 ProxyIndex = Private->OfferIndex[Private->SelectProxyType][0];
1054 }
1055 } else {
1056 //
1057 // The proxy offer should not be determined if select by received order.
1058 //
1059 Status = EFI_NO_RESPONSE;
1060
1061 for (Index = 0; Index < Private->OfferNum; Index++) {
1062 ASSERT (Index < PXEBC_OFFER_MAX_NUM);
1063 Offer = &Private->OfferBuffer[Index].Dhcp4.Packet.Offer;
1064 OfferType = Private->OfferBuffer[Index].Dhcp4.OfferType;
1065 if (!IS_PROXY_DHCP_OFFER (Offer)) {
1066 //
1067 // Skip non proxy DHCPv4 offers.
1068 //
1069 continue;
1070 }
1071
1072 if (OfferType == PxeOfferTypeProxyBinl) {
1073 //
1074 // Try all the cached ProxyBinl offer one by one to request bootfile name.
1075 //
1076 if (EFI_ERROR (PxeBcRetryBinlOffer (Private, Index))) {
1077 continue;
1078 }
1079 }
1080
1081 Private->SelectProxyType = OfferType;
1082 ProxyIndex = Index;
1083 Status = EFI_SUCCESS;
1084 break;
1085 }
1086 }
1087
1088 if (!EFI_ERROR (Status) && Private->SelectProxyType != PxeOfferTypeProxyBinl) {
1089 //
1090 // Success to try to request by a ProxyPxe10 or ProxyWfm11a offer, copy and parse it.
1091 //
1092 PxeBcCopyProxyOffer (Private, ProxyIndex);
1093 }
1094 } else {
1095 //
1096 // Othewise, the bootfile name must be included in DhcpOnly offer.
1097 //
1098 if (Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] == NULL) {
1099 Status = EFI_NOT_FOUND;
1100 }
1101 }
1102 }
1103
1104 if (!EFI_ERROR (Status)) {
1105 //
1106 // All PXE boot information is ready by now.
1107 //
1108 Mode = Private->PxeBc.Mode;
1109 Offer = &Cache4->Packet.Offer;
1110 Ack = &Private->DhcpAck.Dhcp4.Packet.Ack;
1111 if (Cache4->OfferType == PxeOfferTypeBootp) {
1112 //
1113 // Bootp is a special case that only 2 packets involved instead of 4. So the bootp's reply
1114 // should be taken as ack.
1115 //
1116 Ack = Offer;
1117 }
1118
1119 PxeBcCopyDhcp4Ack (Private, Ack, TRUE);
1120 Mode->DhcpDiscoverValid = TRUE;
1121 }
1122
1123 return Status;
1124 }
1125
1126
1127 /**
1128 EFI_DHCP4_CALLBACK is provided by the consumer of the EFI DHCPv4 Protocol driver
1129 to intercept events that occurred in the configuration process.
1130
1131 @param[in] This Pointer to the EFI DHCPv4 Protocol.
1132 @param[in] Context Pointer to the context set by EFI_DHCP4_PROTOCOL.Configure().
1133 @param[in] CurrentState The current operational state of the EFI DHCPv4 Protocol driver.
1134 @param[in] Dhcp4Event The event that occurs in the current state, which usually means a
1135 state transition.
1136 @param[in] Packet The DHCPv4 packet that is going to be sent or already received.
1137 @param[out] NewPacket The packet that is used to replace the above Packet.
1138
1139 @retval EFI_SUCCESS Tells the EFI DHCPv4 Protocol driver to continue the DHCP process.
1140 @retval EFI_NOT_READY Only used in the Dhcp4Selecting state. The EFI DHCPv4 Protocol
1141 driver will continue to wait for more DHCPOFFER packets until the
1142 retry timeout expires.
1143 @retval EFI_ABORTED Tells the EFI DHCPv4 Protocol driver to abort the current process
1144 and return to the Dhcp4Init or Dhcp4InitReboot state.
1145
1146 **/
1147 EFI_STATUS
1148 EFIAPI
1149 PxeBcDhcp4CallBack (
1150 IN EFI_DHCP4_PROTOCOL *This,
1151 IN VOID *Context,
1152 IN EFI_DHCP4_STATE CurrentState,
1153 IN EFI_DHCP4_EVENT Dhcp4Event,
1154 IN EFI_DHCP4_PACKET *Packet OPTIONAL,
1155 OUT EFI_DHCP4_PACKET **NewPacket OPTIONAL
1156 )
1157 {
1158 PXEBC_PRIVATE_DATA *Private;
1159 EFI_PXE_BASE_CODE_MODE *Mode;
1160 EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL *Callback;
1161 EFI_DHCP4_PACKET_OPTION *MaxMsgSize;
1162 UINT16 Value;
1163 EFI_STATUS Status;
1164 BOOLEAN Received;
1165
1166 if ((Dhcp4Event != Dhcp4RcvdOffer) &&
1167 (Dhcp4Event != Dhcp4SelectOffer) &&
1168 (Dhcp4Event != Dhcp4SendDiscover) &&
1169 (Dhcp4Event != Dhcp4RcvdAck)) {
1170 return EFI_SUCCESS;
1171 }
1172
1173 Private = (PXEBC_PRIVATE_DATA *) Context;
1174 Mode = Private->PxeBc.Mode;
1175 Callback = Private->PxeBcCallback;
1176
1177 //
1178 // Override the Maximum DHCP Message Size.
1179 //
1180 MaxMsgSize = PxeBcParseDhcp4Options (
1181 Packet->Dhcp4.Option,
1182 GET_OPTION_BUFFER_LEN (Packet),
1183 DHCP4_TAG_MAXMSG
1184 );
1185 if (MaxMsgSize != NULL) {
1186 Value = HTONS (PXEBC_DHCP4_PACKET_MAX_SIZE - 8);
1187 CopyMem (MaxMsgSize->Data, &Value, sizeof (Value));
1188 }
1189
1190 //
1191 // Callback to user if any packets sent or received.
1192 //
1193 if (Dhcp4Event != Dhcp4SelectOffer && Callback != NULL) {
1194 Received = (BOOLEAN) (Dhcp4Event == Dhcp4RcvdOffer || Dhcp4Event == Dhcp4RcvdAck);
1195 Status = Callback->Callback (
1196 Callback,
1197 Private->Function,
1198 Received,
1199 Packet->Length,
1200 (EFI_PXE_BASE_CODE_PACKET *) &Packet->Dhcp4
1201 );
1202 if (Status != EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE) {
1203 return EFI_ABORTED;
1204 }
1205 }
1206
1207 Status = EFI_SUCCESS;
1208
1209 switch (Dhcp4Event) {
1210
1211 case Dhcp4SendDiscover:
1212 //
1213 // Cache the DHCPv4 discover packet to mode data directly.
1214 // It need to check SendGuid as well as Dhcp4SendRequest.
1215 //
1216 CopyMem (&Mode->DhcpDiscover.Dhcpv4, &Packet->Dhcp4, Packet->Length);
1217
1218 case Dhcp4SendRequest:
1219 if (Mode->SendGUID) {
1220 //
1221 // Send the system Guid instead of the MAC address as the hardware address if required.
1222 //
1223 if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID *) Packet->Dhcp4.Header.ClientHwAddr))) {
1224 //
1225 // Zero the Guid to indicate NOT programable if failed to get system Guid.
1226 //
1227 ZeroMem (Packet->Dhcp4.Header.ClientHwAddr, sizeof (EFI_GUID));
1228 }
1229 Packet->Dhcp4.Header.HwAddrLen = (UINT8) sizeof (EFI_GUID);
1230 }
1231 break;
1232
1233 case Dhcp4RcvdOffer:
1234 Status = EFI_NOT_READY;
1235 if (Private->OfferNum < PXEBC_OFFER_MAX_NUM) {
1236 //
1237 // Cache the DHCPv4 offers to OfferBuffer[] for select later, and record
1238 // the OfferIndex and OfferCount.
1239 //
1240 PxeBcCacheDhcp4Offer (Private, Packet);
1241 }
1242 break;
1243
1244 case Dhcp4SelectOffer:
1245 //
1246 // Select offer by the default policy or by order, and record the SelectIndex
1247 // and SelectProxyType.
1248 //
1249 PxeBcSelectDhcp4Offer (Private);
1250
1251 if (Private->SelectIndex == 0) {
1252 Status = EFI_ABORTED;
1253 } else {
1254 *NewPacket = &Private->OfferBuffer[Private->SelectIndex - 1].Dhcp4.Packet.Offer;
1255 }
1256 break;
1257
1258 case Dhcp4RcvdAck:
1259 //
1260 // Cache the DHCPv4 ack to Private->Dhcp4Ack, but it's not the final ack in mode data
1261 // without verification.
1262 //
1263 ASSERT (Private->SelectIndex != 0);
1264
1265 PxeBcCopyDhcp4Ack (Private, Packet, FALSE);
1266 break;
1267
1268 default:
1269 break;
1270 }
1271
1272 return Status;
1273 }
1274
1275
1276 /**
1277 Build and send out the request packet for the bootfile, and parse the reply.
1278
1279 @param[in] Private Pointer to PxeBc private data.
1280 @param[in] Type PxeBc option boot item type.
1281 @param[in] Layer Pointer to option boot item layer.
1282 @param[in] UseBis Use BIS or not.
1283 @param[in] DestIp Pointer to the server address.
1284 @param[in] IpCount The total count of the server address.
1285 @param[in] SrvList Pointer to EFI_PXE_BASE_CODE_SRVLIST.
1286
1287 @retval EFI_SUCCESS Successfully discovered boot file.
1288 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource.
1289 @retval EFI_NOT_FOUND Can't get the PXE reply packet.
1290 @retval Others Failed to discover boot file.
1291
1292 **/
1293 EFI_STATUS
1294 PxeBcDhcp4Discover (
1295 IN PXEBC_PRIVATE_DATA *Private,
1296 IN UINT16 Type,
1297 IN UINT16 *Layer,
1298 IN BOOLEAN UseBis,
1299 IN EFI_IP_ADDRESS *DestIp,
1300 IN UINT16 IpCount,
1301 IN EFI_PXE_BASE_CODE_SRVLIST *SrvList
1302 )
1303 {
1304 EFI_PXE_BASE_CODE_UDP_PORT Sport;
1305 EFI_PXE_BASE_CODE_MODE *Mode;
1306 EFI_DHCP4_PROTOCOL *Dhcp4;
1307 EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN Token;
1308 BOOLEAN IsBCast;
1309 EFI_STATUS Status;
1310 UINT16 RepIndex;
1311 UINT16 SrvIndex;
1312 UINT16 TryIndex;
1313 EFI_DHCP4_LISTEN_POINT ListenPoint;
1314 EFI_DHCP4_PACKET *Response;
1315 UINT8 Buffer[PXEBC_DHCP4_OPTION_MAX_SIZE];
1316 EFI_DHCP4_PACKET_OPTION *OptList[PXEBC_DHCP4_OPTION_MAX_NUM];
1317 UINT32 OptCount;
1318 EFI_DHCP4_PACKET_OPTION *PxeOpt;
1319 PXEBC_OPTION_BOOT_ITEM *PxeBootItem;
1320 UINT8 VendorOptLen;
1321 UINT32 Xid;
1322
1323 Mode = Private->PxeBc.Mode;
1324 Dhcp4 = Private->Dhcp4;
1325 Status = EFI_SUCCESS;
1326
1327 ZeroMem (&Token, sizeof (EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN));
1328
1329 //
1330 // Use broadcast if destination address not specified.
1331 //
1332 if (DestIp == NULL) {
1333 Sport = PXEBC_DHCP4_S_PORT;
1334 IsBCast = TRUE;
1335 } else {
1336 Sport = PXEBC_BS_DISCOVER_PORT;
1337 IsBCast = FALSE;
1338 }
1339
1340 if (!UseBis && Layer != NULL) {
1341 *Layer &= EFI_PXE_BASE_CODE_BOOT_LAYER_MASK;
1342 }
1343
1344 //
1345 // Build all the options for the request packet.
1346 //
1347 OptCount = PxeBcBuildDhcp4Options (Private, OptList, Buffer, TRUE);
1348
1349 if (Private->IsDoDiscover) {
1350 //
1351 // Add vendor option of PXE_BOOT_ITEM
1352 //
1353 VendorOptLen = (UINT8) ((sizeof (EFI_DHCP4_PACKET_OPTION) - 1) * 2 + sizeof (PXEBC_OPTION_BOOT_ITEM) + 1);
1354 OptList[OptCount] = AllocateZeroPool (VendorOptLen);
1355 if (OptList[OptCount] == NULL) {
1356 return EFI_OUT_OF_RESOURCES;
1357 }
1358
1359 OptList[OptCount]->OpCode = DHCP4_TAG_VENDOR;
1360 OptList[OptCount]->Length = (UINT8) (VendorOptLen - 2);
1361 PxeOpt = (EFI_DHCP4_PACKET_OPTION *) OptList[OptCount]->Data;
1362 PxeOpt->OpCode = PXEBC_VENDOR_TAG_BOOT_ITEM;
1363 PxeOpt->Length = (UINT8) sizeof (PXEBC_OPTION_BOOT_ITEM);
1364 PxeBootItem = (PXEBC_OPTION_BOOT_ITEM *) PxeOpt->Data;
1365 PxeBootItem->Type = HTONS (Type);
1366 PxeOpt->Data[PxeOpt->Length] = DHCP4_TAG_EOP;
1367
1368 if (Layer != NULL) {
1369 PxeBootItem->Layer = HTONS (*Layer);
1370 }
1371
1372 OptCount++;
1373 }
1374
1375 //
1376 // Build the request packet with seed packet and option list.
1377 //
1378 Status = Dhcp4->Build (
1379 Dhcp4,
1380 &Private->SeedPacket,
1381 0,
1382 NULL,
1383 OptCount,
1384 OptList,
1385 &Token.Packet
1386 );
1387 //
1388 // Free the vendor option of PXE_BOOT_ITEM.
1389 //
1390 if (Private->IsDoDiscover) {
1391 FreePool (OptList[OptCount - 1]);
1392 }
1393
1394 if (EFI_ERROR (Status)) {
1395 return Status;
1396 }
1397
1398 if (Mode->SendGUID) {
1399 if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID *) Token.Packet->Dhcp4.Header.ClientHwAddr))) {
1400 //
1401 // Zero the Guid to indicate NOT programable if failed to get system Guid.
1402 //
1403 ZeroMem (Token.Packet->Dhcp4.Header.ClientHwAddr, sizeof (EFI_GUID));
1404 }
1405 Token.Packet->Dhcp4.Header.HwAddrLen = (UINT8) sizeof (EFI_GUID);
1406 }
1407
1408 //
1409 // Set fields of the token for the request packet.
1410 //
1411 Xid = NET_RANDOM (NetRandomInitSeed ());
1412 Token.Packet->Dhcp4.Header.Xid = HTONL (Xid);
1413 Token.Packet->Dhcp4.Header.Reserved = HTONS ((UINT16) ((IsBCast) ? 0x8000 : 0x0));
1414 CopyMem (&Token.Packet->Dhcp4.Header.ClientAddr, &Private->StationIp, sizeof (EFI_IPv4_ADDRESS));
1415
1416 Token.RemotePort = Sport;
1417
1418 if (IsBCast) {
1419 SetMem (&Token.RemoteAddress, sizeof (EFI_IPv4_ADDRESS), 0xff);
1420 } else {
1421 CopyMem (&Token.RemoteAddress, DestIp, sizeof (EFI_IPv4_ADDRESS));
1422 }
1423
1424 CopyMem (&Token.GatewayAddress, &Private->GatewayIp, sizeof (EFI_IPv4_ADDRESS));
1425
1426 if (!IsBCast) {
1427 Token.ListenPointCount = 1;
1428 Token.ListenPoints = &ListenPoint;
1429 Token.ListenPoints[0].ListenPort = PXEBC_BS_DISCOVER_PORT;
1430 CopyMem (&Token.ListenPoints[0].ListenAddress, &Private->StationIp, sizeof(EFI_IPv4_ADDRESS));
1431 CopyMem (&Token.ListenPoints[0].SubnetMask, &Private->SubnetMask, sizeof(EFI_IPv4_ADDRESS));
1432 }
1433
1434 //
1435 // Send out the request packet to discover the bootfile.
1436 //
1437 for (TryIndex = 1; TryIndex <= PXEBC_BOOT_REQUEST_RETRIES; TryIndex++) {
1438
1439 Token.TimeoutValue = (UINT16) (PXEBC_BOOT_REQUEST_TIMEOUT * TryIndex);
1440 Token.Packet->Dhcp4.Header.Seconds = (UINT16) (PXEBC_BOOT_REQUEST_TIMEOUT * (TryIndex - 1));
1441
1442 Status = Dhcp4->TransmitReceive (Dhcp4, &Token);
1443 if (Token.Status != EFI_TIMEOUT) {
1444 break;
1445 }
1446 }
1447
1448 if (TryIndex > PXEBC_BOOT_REQUEST_RETRIES) {
1449 //
1450 // No server response our PXE request
1451 //
1452 Status = EFI_TIMEOUT;
1453 }
1454
1455 if (!EFI_ERROR (Status)) {
1456
1457 RepIndex = 0;
1458 SrvIndex = 0;
1459 Response = Token.ResponseList;
1460 //
1461 // Find the right PXE Reply according to server address.
1462 //
1463 while (RepIndex < Token.ResponseCount) {
1464
1465 while (SrvIndex < IpCount) {
1466 if (SrvList[SrvIndex].AcceptAnyResponse) {
1467 break;
1468 }
1469 if ((SrvList[SrvIndex].Type == Type) &&
1470 EFI_IP4_EQUAL (&Response->Dhcp4.Header.ServerAddr, &SrvList[SrvIndex].IpAddr)) {
1471 break;
1472 }
1473 SrvIndex++;
1474 }
1475
1476 if ((IpCount != SrvIndex) || (IpCount == 0)) {
1477 break;
1478 }
1479
1480 SrvIndex = 0;
1481 RepIndex++;
1482
1483 Response = (EFI_DHCP4_PACKET *) ((UINT8 *) Response + Response->Size);
1484 }
1485
1486 if (RepIndex < Token.ResponseCount) {
1487 //
1488 // Cache the right PXE reply packet here, set valid flag later.
1489 // Especially for PXE discover packet, store it into mode data here.
1490 //
1491 if (Private->IsDoDiscover) {
1492 PxeBcCacheDhcp4Packet (&Private->PxeReply.Dhcp4.Packet.Ack, Response);
1493 CopyMem (&Mode->PxeDiscover, &Token.Packet->Dhcp4, Token.Packet->Length);
1494 } else {
1495 PxeBcCacheDhcp4Packet (&Private->ProxyOffer.Dhcp4.Packet.Offer, Response);
1496 }
1497 } else {
1498 //
1499 // Not found the right PXE reply packet.
1500 //
1501 Status = EFI_NOT_FOUND;
1502 }
1503 if (Token.ResponseList != NULL) {
1504 FreePool (Token.ResponseList);
1505 }
1506 }
1507
1508 FreePool (Token.Packet);
1509 return Status;
1510 }
1511
1512 /**
1513 Switch the Ip4 policy to static.
1514
1515 @param[in] Private The pointer to PXEBC_PRIVATE_DATA.
1516
1517 @retval EFI_SUCCESS The policy is already configured to static.
1518 @retval Others Other error as indicated..
1519
1520 **/
1521 EFI_STATUS
1522 PxeBcSetIp4Policy (
1523 IN PXEBC_PRIVATE_DATA *Private
1524 )
1525 {
1526 EFI_STATUS Status;
1527 EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2;
1528 EFI_IP4_CONFIG2_POLICY Policy;
1529 UINTN DataSize;
1530
1531 Ip4Config2 = Private->Ip4Config2;
1532 DataSize = sizeof (EFI_IP4_CONFIG2_POLICY);
1533 Status = Ip4Config2->GetData (
1534 Ip4Config2,
1535 Ip4Config2DataTypePolicy,
1536 &DataSize,
1537 &Policy
1538 );
1539 if (EFI_ERROR (Status)) {
1540 return Status;
1541 }
1542
1543 if (Policy != Ip4Config2PolicyStatic) {
1544 Policy = Ip4Config2PolicyStatic;
1545 Status= Ip4Config2->SetData (
1546 Ip4Config2,
1547 Ip4Config2DataTypePolicy,
1548 sizeof (EFI_IP4_CONFIG2_POLICY),
1549 &Policy
1550 );
1551 if (EFI_ERROR (Status)) {
1552 return Status;
1553 }
1554 }
1555
1556 return EFI_SUCCESS;
1557 }
1558
1559 /**
1560 Start the D.O.R.A DHCPv4 process to acquire the IPv4 address and other PXE boot information.
1561
1562 @param[in] Private Pointer to PxeBc private data.
1563 @param[in] Dhcp4 Pointer to the EFI_DHCP4_PROTOCOL
1564
1565 @retval EFI_SUCCESS The D.O.R.A process successfully finished.
1566 @retval Others Failed to finish the D.O.R.A process.
1567
1568 **/
1569 EFI_STATUS
1570 PxeBcDhcp4Dora (
1571 IN PXEBC_PRIVATE_DATA *Private,
1572 IN EFI_DHCP4_PROTOCOL *Dhcp4
1573 )
1574 {
1575 EFI_PXE_BASE_CODE_MODE *PxeMode;
1576 EFI_DHCP4_CONFIG_DATA Config;
1577 EFI_DHCP4_MODE_DATA Mode;
1578 EFI_DHCP4_PACKET_OPTION *OptList[PXEBC_DHCP4_OPTION_MAX_NUM];
1579 UINT8 Buffer[PXEBC_DHCP4_OPTION_MAX_SIZE];
1580 UINT32 OptCount;
1581 EFI_STATUS Status;
1582
1583 ASSERT (Dhcp4 != NULL);
1584
1585 Status = EFI_SUCCESS;
1586 PxeMode = Private->PxeBc.Mode;
1587
1588 //
1589 // Build option list for the request packet.
1590 //
1591 OptCount = PxeBcBuildDhcp4Options (Private, OptList, Buffer, FALSE);
1592 ASSERT (OptCount> 0);
1593
1594 ZeroMem (&Mode, sizeof (EFI_DHCP4_MODE_DATA));
1595 ZeroMem (&Config, sizeof (EFI_DHCP4_CONFIG_DATA));
1596
1597 Config.OptionCount = OptCount;
1598 Config.OptionList = OptList;
1599 Config.Dhcp4Callback = PxeBcDhcp4CallBack;
1600 Config.CallbackContext = Private;
1601 Config.DiscoverTryCount = PXEBC_DHCP_RETRIES;
1602 Config.DiscoverTimeout = mPxeDhcpTimeout;
1603
1604 //
1605 // Configure the DHCPv4 instance for PXE boot.
1606 //
1607 Status = Dhcp4->Configure (Dhcp4, &Config);
1608 if (EFI_ERROR (Status)) {
1609 goto ON_EXIT;
1610 }
1611
1612 //
1613 // Initialize the record fields for DHCPv4 offer in private data.
1614 //
1615 Private->IsProxyRecved = FALSE;
1616 Private->OfferNum = 0;
1617 ZeroMem (Private->OfferCount, sizeof (Private->OfferCount));
1618 ZeroMem (Private->OfferIndex, sizeof (Private->OfferIndex));
1619
1620 //
1621 // Start DHCPv4 D.O.R.A. process to acquire IPv4 address. This may
1622 // have already been done, thus do not leave in error if the return
1623 // code is EFI_ALREADY_STARTED.
1624 //
1625 Status = Dhcp4->Start (Dhcp4, NULL);
1626 if (EFI_ERROR (Status) && Status != EFI_ALREADY_STARTED) {
1627 if (Status == EFI_ICMP_ERROR) {
1628 PxeMode->IcmpErrorReceived = TRUE;
1629 }
1630 goto ON_EXIT;
1631 }
1632
1633 //
1634 // Get the acquired IPv4 address and store them.
1635 //
1636 Status = Dhcp4->GetModeData (Dhcp4, &Mode);
1637 if (EFI_ERROR (Status)) {
1638 goto ON_EXIT;
1639 }
1640
1641 ASSERT (Mode.State == Dhcp4Bound);
1642
1643 CopyMem (&Private->StationIp, &Mode.ClientAddress, sizeof (EFI_IPv4_ADDRESS));
1644 CopyMem (&Private->SubnetMask, &Mode.SubnetMask, sizeof (EFI_IPv4_ADDRESS));
1645 CopyMem (&Private->GatewayIp, &Mode.RouterAddress, sizeof (EFI_IPv4_ADDRESS));
1646 CopyMem (&PxeMode->StationIp, &Private->StationIp, sizeof (EFI_IPv4_ADDRESS));
1647 CopyMem (&PxeMode->SubnetMask, &Private->SubnetMask, sizeof (EFI_IPv4_ADDRESS));
1648
1649 Status = PxeBcFlushStationIp (Private, &Private->StationIp, &Private->SubnetMask);
1650 if (EFI_ERROR (Status)) {
1651 goto ON_EXIT;
1652 }
1653
1654 //
1655 // Check the selected offer whether BINL retry is needed.
1656 //
1657 Status = PxeBcHandleDhcp4Offer (Private);
1658
1659 AsciiPrint ("\n Station IP address is ");
1660
1661 PxeBcShowIp4Addr (&Private->StationIp.v4);
1662 AsciiPrint ("\n");
1663
1664 ON_EXIT:
1665 if (EFI_ERROR (Status)) {
1666 Dhcp4->Stop (Dhcp4);
1667 Dhcp4->Configure (Dhcp4, NULL);
1668 } else {
1669 ZeroMem (&Config, sizeof (EFI_DHCP4_CONFIG_DATA));
1670 Dhcp4->Configure (Dhcp4, &Config);
1671 Private->IsAddressOk = TRUE;
1672 }
1673
1674 return Status;
1675 }