]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/UefiPxeBcDxe/PxeBcDhcp4.c
MdeModulePkg: Replace ASSERT with error return code in PXE driver.
[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);
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);
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 if (Packet->Length > PXEBC_DHCP4_PACKET_MAX_SIZE) {
1213 //
1214 // If the to be sent packet exceeds the maximum length, abort the DHCP process.
1215 //
1216 Status = EFI_ABORTED;
1217 break;
1218 }
1219
1220 //
1221 // Cache the DHCPv4 discover packet to mode data directly.
1222 // It need to check SendGuid as well as Dhcp4SendRequest.
1223 //
1224 CopyMem (&Mode->DhcpDiscover.Dhcpv4, &Packet->Dhcp4, Packet->Length);
1225
1226 case Dhcp4SendRequest:
1227 if (Packet->Length > PXEBC_DHCP4_PACKET_MAX_SIZE) {
1228 //
1229 // If the to be sent packet exceeds the maximum length, abort the DHCP process.
1230 //
1231 Status = EFI_ABORTED;
1232 break;
1233 }
1234
1235 if (Mode->SendGUID) {
1236 //
1237 // Send the system Guid instead of the MAC address as the hardware address if required.
1238 //
1239 if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID *) Packet->Dhcp4.Header.ClientHwAddr))) {
1240 //
1241 // Zero the Guid to indicate NOT programable if failed to get system Guid.
1242 //
1243 ZeroMem (Packet->Dhcp4.Header.ClientHwAddr, sizeof (EFI_GUID));
1244 }
1245 Packet->Dhcp4.Header.HwAddrLen = (UINT8) sizeof (EFI_GUID);
1246 }
1247 break;
1248
1249 case Dhcp4RcvdOffer:
1250 Status = EFI_NOT_READY;
1251 if (Packet->Length > PXEBC_DHCP4_PACKET_MAX_SIZE) {
1252 //
1253 // Ignore the incoming packets which exceed the maximum length.
1254 //
1255 break;
1256 }
1257 if (Private->OfferNum < PXEBC_OFFER_MAX_NUM) {
1258 //
1259 // Cache the DHCPv4 offers to OfferBuffer[] for select later, and record
1260 // the OfferIndex and OfferCount.
1261 //
1262 PxeBcCacheDhcp4Offer (Private, Packet);
1263 }
1264 break;
1265
1266 case Dhcp4SelectOffer:
1267 //
1268 // Select offer by the default policy or by order, and record the SelectIndex
1269 // and SelectProxyType.
1270 //
1271 PxeBcSelectDhcp4Offer (Private);
1272
1273 if (Private->SelectIndex == 0) {
1274 Status = EFI_ABORTED;
1275 } else {
1276 *NewPacket = &Private->OfferBuffer[Private->SelectIndex - 1].Dhcp4.Packet.Offer;
1277 }
1278 break;
1279
1280 case Dhcp4RcvdAck:
1281 if (Packet->Length > PXEBC_DHCP4_PACKET_MAX_SIZE) {
1282 //
1283 // Abort the DHCP if the ACK packet exceeds the maximum length.
1284 //
1285 Status = EFI_ABORTED;
1286 break;
1287 }
1288
1289 //
1290 // Cache the DHCPv4 ack to Private->Dhcp4Ack, but it's not the final ack in mode data
1291 // without verification.
1292 //
1293 ASSERT (Private->SelectIndex != 0);
1294
1295 PxeBcCopyDhcp4Ack (Private, Packet, FALSE);
1296 break;
1297
1298 default:
1299 break;
1300 }
1301
1302 return Status;
1303 }
1304
1305
1306 /**
1307 Build and send out the request packet for the bootfile, and parse the reply.
1308
1309 @param[in] Private Pointer to PxeBc private data.
1310 @param[in] Type PxeBc option boot item type.
1311 @param[in] Layer Pointer to option boot item layer.
1312 @param[in] UseBis Use BIS or not.
1313 @param[in] DestIp Pointer to the server address.
1314 @param[in] IpCount The total count of the server address.
1315 @param[in] SrvList Pointer to EFI_PXE_BASE_CODE_SRVLIST.
1316
1317 @retval EFI_SUCCESS Successfully discovered boot file.
1318 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource.
1319 @retval EFI_NOT_FOUND Can't get the PXE reply packet.
1320 @retval Others Failed to discover boot file.
1321
1322 **/
1323 EFI_STATUS
1324 PxeBcDhcp4Discover (
1325 IN PXEBC_PRIVATE_DATA *Private,
1326 IN UINT16 Type,
1327 IN UINT16 *Layer,
1328 IN BOOLEAN UseBis,
1329 IN EFI_IP_ADDRESS *DestIp,
1330 IN UINT16 IpCount,
1331 IN EFI_PXE_BASE_CODE_SRVLIST *SrvList
1332 )
1333 {
1334 EFI_PXE_BASE_CODE_UDP_PORT Sport;
1335 EFI_PXE_BASE_CODE_MODE *Mode;
1336 EFI_DHCP4_PROTOCOL *Dhcp4;
1337 EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN Token;
1338 BOOLEAN IsBCast;
1339 EFI_STATUS Status;
1340 UINT16 RepIndex;
1341 UINT16 SrvIndex;
1342 UINT16 TryIndex;
1343 EFI_DHCP4_LISTEN_POINT ListenPoint;
1344 EFI_DHCP4_PACKET *Response;
1345 UINT8 Buffer[PXEBC_DHCP4_OPTION_MAX_SIZE];
1346 EFI_DHCP4_PACKET_OPTION *OptList[PXEBC_DHCP4_OPTION_MAX_NUM];
1347 UINT32 OptCount;
1348 EFI_DHCP4_PACKET_OPTION *PxeOpt;
1349 PXEBC_OPTION_BOOT_ITEM *PxeBootItem;
1350 UINT8 VendorOptLen;
1351 UINT32 Xid;
1352
1353 Mode = Private->PxeBc.Mode;
1354 Dhcp4 = Private->Dhcp4;
1355 Status = EFI_SUCCESS;
1356
1357 ZeroMem (&Token, sizeof (EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN));
1358
1359 //
1360 // Use broadcast if destination address not specified.
1361 //
1362 if (DestIp == NULL) {
1363 Sport = PXEBC_DHCP4_S_PORT;
1364 IsBCast = TRUE;
1365 } else {
1366 Sport = PXEBC_BS_DISCOVER_PORT;
1367 IsBCast = FALSE;
1368 }
1369
1370 if (!UseBis && Layer != NULL) {
1371 *Layer &= EFI_PXE_BASE_CODE_BOOT_LAYER_MASK;
1372 }
1373
1374 //
1375 // Build all the options for the request packet.
1376 //
1377 OptCount = PxeBcBuildDhcp4Options (Private, OptList, Buffer, TRUE);
1378
1379 if (Private->IsDoDiscover) {
1380 //
1381 // Add vendor option of PXE_BOOT_ITEM
1382 //
1383 VendorOptLen = (UINT8) ((sizeof (EFI_DHCP4_PACKET_OPTION) - 1) * 2 + sizeof (PXEBC_OPTION_BOOT_ITEM) + 1);
1384 OptList[OptCount] = AllocateZeroPool (VendorOptLen);
1385 if (OptList[OptCount] == NULL) {
1386 return EFI_OUT_OF_RESOURCES;
1387 }
1388
1389 OptList[OptCount]->OpCode = DHCP4_TAG_VENDOR;
1390 OptList[OptCount]->Length = (UINT8) (VendorOptLen - 2);
1391 PxeOpt = (EFI_DHCP4_PACKET_OPTION *) OptList[OptCount]->Data;
1392 PxeOpt->OpCode = PXEBC_VENDOR_TAG_BOOT_ITEM;
1393 PxeOpt->Length = (UINT8) sizeof (PXEBC_OPTION_BOOT_ITEM);
1394 PxeBootItem = (PXEBC_OPTION_BOOT_ITEM *) PxeOpt->Data;
1395 PxeBootItem->Type = HTONS (Type);
1396 PxeOpt->Data[PxeOpt->Length] = DHCP4_TAG_EOP;
1397
1398 if (Layer != NULL) {
1399 PxeBootItem->Layer = HTONS (*Layer);
1400 }
1401
1402 OptCount++;
1403 }
1404
1405 //
1406 // Build the request packet with seed packet and option list.
1407 //
1408 Status = Dhcp4->Build (
1409 Dhcp4,
1410 &Private->SeedPacket,
1411 0,
1412 NULL,
1413 OptCount,
1414 OptList,
1415 &Token.Packet
1416 );
1417 //
1418 // Free the vendor option of PXE_BOOT_ITEM.
1419 //
1420 if (Private->IsDoDiscover) {
1421 FreePool (OptList[OptCount - 1]);
1422 }
1423
1424 if (EFI_ERROR (Status)) {
1425 return Status;
1426 }
1427
1428 if (Mode->SendGUID) {
1429 if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID *) Token.Packet->Dhcp4.Header.ClientHwAddr))) {
1430 //
1431 // Zero the Guid to indicate NOT programable if failed to get system Guid.
1432 //
1433 ZeroMem (Token.Packet->Dhcp4.Header.ClientHwAddr, sizeof (EFI_GUID));
1434 }
1435 Token.Packet->Dhcp4.Header.HwAddrLen = (UINT8) sizeof (EFI_GUID);
1436 }
1437
1438 //
1439 // Set fields of the token for the request packet.
1440 //
1441 Xid = NET_RANDOM (NetRandomInitSeed ());
1442 Token.Packet->Dhcp4.Header.Xid = HTONL (Xid);
1443 Token.Packet->Dhcp4.Header.Reserved = HTONS ((UINT16) ((IsBCast) ? 0x8000 : 0x0));
1444 CopyMem (&Token.Packet->Dhcp4.Header.ClientAddr, &Private->StationIp, sizeof (EFI_IPv4_ADDRESS));
1445
1446 Token.RemotePort = Sport;
1447
1448 if (IsBCast) {
1449 SetMem (&Token.RemoteAddress, sizeof (EFI_IPv4_ADDRESS), 0xff);
1450 } else {
1451 CopyMem (&Token.RemoteAddress, DestIp, sizeof (EFI_IPv4_ADDRESS));
1452 }
1453
1454 CopyMem (&Token.GatewayAddress, &Private->GatewayIp, sizeof (EFI_IPv4_ADDRESS));
1455
1456 if (!IsBCast) {
1457 Token.ListenPointCount = 1;
1458 Token.ListenPoints = &ListenPoint;
1459 Token.ListenPoints[0].ListenPort = PXEBC_BS_DISCOVER_PORT;
1460 CopyMem (&Token.ListenPoints[0].ListenAddress, &Private->StationIp, sizeof(EFI_IPv4_ADDRESS));
1461 CopyMem (&Token.ListenPoints[0].SubnetMask, &Private->SubnetMask, sizeof(EFI_IPv4_ADDRESS));
1462 }
1463
1464 //
1465 // Send out the request packet to discover the bootfile.
1466 //
1467 for (TryIndex = 1; TryIndex <= PXEBC_BOOT_REQUEST_RETRIES; TryIndex++) {
1468
1469 Token.TimeoutValue = (UINT16) (PXEBC_BOOT_REQUEST_TIMEOUT * TryIndex);
1470 Token.Packet->Dhcp4.Header.Seconds = (UINT16) (PXEBC_BOOT_REQUEST_TIMEOUT * (TryIndex - 1));
1471
1472 Status = Dhcp4->TransmitReceive (Dhcp4, &Token);
1473 if (Token.Status != EFI_TIMEOUT) {
1474 break;
1475 }
1476 }
1477
1478 if (TryIndex > PXEBC_BOOT_REQUEST_RETRIES) {
1479 //
1480 // No server response our PXE request
1481 //
1482 Status = EFI_TIMEOUT;
1483 }
1484
1485 if (!EFI_ERROR (Status)) {
1486
1487 RepIndex = 0;
1488 SrvIndex = 0;
1489 Response = Token.ResponseList;
1490 //
1491 // Find the right PXE Reply according to server address.
1492 //
1493 while (RepIndex < Token.ResponseCount) {
1494
1495 while (SrvIndex < IpCount) {
1496 if (SrvList[SrvIndex].AcceptAnyResponse) {
1497 break;
1498 }
1499 if ((SrvList[SrvIndex].Type == Type) &&
1500 EFI_IP4_EQUAL (&Response->Dhcp4.Header.ServerAddr, &SrvList[SrvIndex].IpAddr)) {
1501 break;
1502 }
1503 SrvIndex++;
1504 }
1505
1506 if ((IpCount != SrvIndex) || (IpCount == 0)) {
1507 break;
1508 }
1509
1510 SrvIndex = 0;
1511 RepIndex++;
1512
1513 Response = (EFI_DHCP4_PACKET *) ((UINT8 *) Response + Response->Size);
1514 }
1515
1516 if (RepIndex < Token.ResponseCount) {
1517 //
1518 // Cache the right PXE reply packet here, set valid flag later.
1519 // Especially for PXE discover packet, store it into mode data here.
1520 //
1521 if (Private->IsDoDiscover) {
1522 PxeBcCacheDhcp4Packet (&Private->PxeReply.Dhcp4.Packet.Ack, Response);
1523 CopyMem (&Mode->PxeDiscover, &Token.Packet->Dhcp4, Token.Packet->Length);
1524 } else {
1525 PxeBcCacheDhcp4Packet (&Private->ProxyOffer.Dhcp4.Packet.Offer, Response);
1526 }
1527 } else {
1528 //
1529 // Not found the right PXE reply packet.
1530 //
1531 Status = EFI_NOT_FOUND;
1532 }
1533 if (Token.ResponseList != NULL) {
1534 FreePool (Token.ResponseList);
1535 }
1536 }
1537
1538 FreePool (Token.Packet);
1539 return Status;
1540 }
1541
1542 /**
1543 Switch the Ip4 policy to static.
1544
1545 @param[in] Private The pointer to PXEBC_PRIVATE_DATA.
1546
1547 @retval EFI_SUCCESS The policy is already configured to static.
1548 @retval Others Other error as indicated..
1549
1550 **/
1551 EFI_STATUS
1552 PxeBcSetIp4Policy (
1553 IN PXEBC_PRIVATE_DATA *Private
1554 )
1555 {
1556 EFI_STATUS Status;
1557 EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2;
1558 EFI_IP4_CONFIG2_POLICY Policy;
1559 UINTN DataSize;
1560
1561 Ip4Config2 = Private->Ip4Config2;
1562 DataSize = sizeof (EFI_IP4_CONFIG2_POLICY);
1563 Status = Ip4Config2->GetData (
1564 Ip4Config2,
1565 Ip4Config2DataTypePolicy,
1566 &DataSize,
1567 &Policy
1568 );
1569 if (EFI_ERROR (Status)) {
1570 return Status;
1571 }
1572
1573 if (Policy != Ip4Config2PolicyStatic) {
1574 Policy = Ip4Config2PolicyStatic;
1575 Status= Ip4Config2->SetData (
1576 Ip4Config2,
1577 Ip4Config2DataTypePolicy,
1578 sizeof (EFI_IP4_CONFIG2_POLICY),
1579 &Policy
1580 );
1581 if (EFI_ERROR (Status)) {
1582 return Status;
1583 }
1584 }
1585
1586 return EFI_SUCCESS;
1587 }
1588
1589 /**
1590 Start the D.O.R.A DHCPv4 process to acquire the IPv4 address and other PXE boot information.
1591
1592 @param[in] Private Pointer to PxeBc private data.
1593 @param[in] Dhcp4 Pointer to the EFI_DHCP4_PROTOCOL
1594
1595 @retval EFI_SUCCESS The D.O.R.A process successfully finished.
1596 @retval Others Failed to finish the D.O.R.A process.
1597
1598 **/
1599 EFI_STATUS
1600 PxeBcDhcp4Dora (
1601 IN PXEBC_PRIVATE_DATA *Private,
1602 IN EFI_DHCP4_PROTOCOL *Dhcp4
1603 )
1604 {
1605 EFI_PXE_BASE_CODE_MODE *PxeMode;
1606 EFI_DHCP4_CONFIG_DATA Config;
1607 EFI_DHCP4_MODE_DATA Mode;
1608 EFI_DHCP4_PACKET_OPTION *OptList[PXEBC_DHCP4_OPTION_MAX_NUM];
1609 UINT8 Buffer[PXEBC_DHCP4_OPTION_MAX_SIZE];
1610 UINT32 OptCount;
1611 EFI_STATUS Status;
1612
1613 ASSERT (Dhcp4 != NULL);
1614
1615 Status = EFI_SUCCESS;
1616 PxeMode = Private->PxeBc.Mode;
1617
1618 //
1619 // Build option list for the request packet.
1620 //
1621 OptCount = PxeBcBuildDhcp4Options (Private, OptList, Buffer, FALSE);
1622 ASSERT (OptCount> 0);
1623
1624 ZeroMem (&Mode, sizeof (EFI_DHCP4_MODE_DATA));
1625 ZeroMem (&Config, sizeof (EFI_DHCP4_CONFIG_DATA));
1626
1627 Config.OptionCount = OptCount;
1628 Config.OptionList = OptList;
1629 Config.Dhcp4Callback = PxeBcDhcp4CallBack;
1630 Config.CallbackContext = Private;
1631 Config.DiscoverTryCount = PXEBC_DHCP_RETRIES;
1632 Config.DiscoverTimeout = mPxeDhcpTimeout;
1633
1634 //
1635 // Configure the DHCPv4 instance for PXE boot.
1636 //
1637 Status = Dhcp4->Configure (Dhcp4, &Config);
1638 if (EFI_ERROR (Status)) {
1639 goto ON_EXIT;
1640 }
1641
1642 //
1643 // Initialize the record fields for DHCPv4 offer in private data.
1644 //
1645 Private->IsProxyRecved = FALSE;
1646 Private->OfferNum = 0;
1647 ZeroMem (Private->OfferCount, sizeof (Private->OfferCount));
1648 ZeroMem (Private->OfferIndex, sizeof (Private->OfferIndex));
1649
1650 //
1651 // Start DHCPv4 D.O.R.A. process to acquire IPv4 address. This may
1652 // have already been done, thus do not leave in error if the return
1653 // code is EFI_ALREADY_STARTED.
1654 //
1655 Status = Dhcp4->Start (Dhcp4, NULL);
1656 if (EFI_ERROR (Status) && Status != EFI_ALREADY_STARTED) {
1657 if (Status == EFI_ICMP_ERROR) {
1658 PxeMode->IcmpErrorReceived = TRUE;
1659 }
1660 goto ON_EXIT;
1661 }
1662
1663 //
1664 // Get the acquired IPv4 address and store them.
1665 //
1666 Status = Dhcp4->GetModeData (Dhcp4, &Mode);
1667 if (EFI_ERROR (Status)) {
1668 goto ON_EXIT;
1669 }
1670
1671 ASSERT (Mode.State == Dhcp4Bound);
1672
1673 CopyMem (&Private->StationIp, &Mode.ClientAddress, sizeof (EFI_IPv4_ADDRESS));
1674 CopyMem (&Private->SubnetMask, &Mode.SubnetMask, sizeof (EFI_IPv4_ADDRESS));
1675 CopyMem (&Private->GatewayIp, &Mode.RouterAddress, sizeof (EFI_IPv4_ADDRESS));
1676 CopyMem (&PxeMode->StationIp, &Private->StationIp, sizeof (EFI_IPv4_ADDRESS));
1677 CopyMem (&PxeMode->SubnetMask, &Private->SubnetMask, sizeof (EFI_IPv4_ADDRESS));
1678
1679 Status = PxeBcFlushStationIp (Private, &Private->StationIp, &Private->SubnetMask);
1680 if (EFI_ERROR (Status)) {
1681 goto ON_EXIT;
1682 }
1683
1684 //
1685 // Check the selected offer whether BINL retry is needed.
1686 //
1687 Status = PxeBcHandleDhcp4Offer (Private);
1688
1689 AsciiPrint ("\n Station IP address is ");
1690
1691 PxeBcShowIp4Addr (&Private->StationIp.v4);
1692 AsciiPrint ("\n");
1693
1694 ON_EXIT:
1695 if (EFI_ERROR (Status)) {
1696 Dhcp4->Stop (Dhcp4);
1697 Dhcp4->Configure (Dhcp4, NULL);
1698 } else {
1699 ZeroMem (&Config, sizeof (EFI_DHCP4_CONFIG_DATA));
1700 Dhcp4->Configure (Dhcp4, &Config);
1701 Private->IsAddressOk = TRUE;
1702 }
1703
1704 return Status;
1705 }