]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcDhcp.c
1d5557f7a9fc88bded9b3cf429ffbc3aba38c874
[mirror_edk2.git] / MdeModulePkg / Universal / Network / UefiPxeBcDxe / PxeBcDhcp.c
1 /** @file
2 Support for PxeBc dhcp functions.
3
4 Copyright (c) 2007 - 2009, Intel Corporation.<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
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 PXEBC_DHCP4_TAG_BOOTFILE_LEN,
23 PXEBC_DHCP4_TAG_VENDOR,
24 PXEBC_DHCP4_TAG_OVERLOAD,
25 PXEBC_DHCP4_TAG_MSG_TYPE,
26 PXEBC_DHCP4_TAG_SERVER_ID,
27 PXEBC_DHCP4_TAG_CLASS_ID,
28 PXEBC_DHCP4_TAG_BOOTFILE
29 };
30
31
32 /**
33 This function initialize the DHCP4 message instance.
34
35 This function will pad each item of dhcp4 message packet.
36
37 @param Seed Pointer to the message instance of the DHCP4 packet.
38 @param Udp4 Pointer to the EFI_UDP4_PROTOCOL instance.
39
40 **/
41 VOID
42 PxeBcInitSeedPacket (
43 IN EFI_DHCP4_PACKET *Seed,
44 IN EFI_UDP4_PROTOCOL *Udp4
45 )
46 {
47 EFI_SIMPLE_NETWORK_MODE Mode;
48 EFI_DHCP4_HEADER *Header;
49
50 Udp4->GetModeData (Udp4, NULL, NULL, NULL, &Mode);
51
52 Seed->Size = sizeof (EFI_DHCP4_PACKET);
53 Seed->Length = sizeof (Seed->Dhcp4);
54
55 Header = &Seed->Dhcp4.Header;
56
57 ZeroMem (Header, sizeof (EFI_DHCP4_HEADER));
58 Header->OpCode = PXEBC_DHCP4_OPCODE_REQUEST;
59 Header->HwType = Mode.IfType;
60 Header->HwAddrLen = (UINT8) Mode.HwAddressSize;
61 CopyMem (Header->ClientHwAddr, &Mode.CurrentAddress, Header->HwAddrLen);
62
63 Seed->Dhcp4.Magik = PXEBC_DHCP4_MAGIC;
64 Seed->Dhcp4.Option[0] = PXEBC_DHCP4_TAG_EOP;
65 }
66
67
68 /**
69 Copy the DCHP4 packet from srouce to destination.
70
71 @param Dst Pointer to the EFI_DHCP4_PROTOCOL instance.
72 @param Src Pointer to the EFI_DHCP4_PROTOCOL instance.
73
74 **/
75 VOID
76 PxeBcCopyEfiDhcp4Packet (
77 IN EFI_DHCP4_PACKET *Dst,
78 IN EFI_DHCP4_PACKET *Src
79 )
80 {
81 ASSERT (Dst->Size >= Src->Length);
82
83 CopyMem (&Dst->Dhcp4, &Src->Dhcp4, Src->Length);
84 Dst->Length = Src->Length;
85 }
86
87
88 /**
89 Copy the dhcp4 packet to the PxeBc private data and parse the dhcp4 packet.
90
91 @param Private Pointer to PxeBc private data.
92 @param OfferIndex Index of cached packets as complements of pxe mode data,
93 the index is maximum offer number.
94
95 **/
96 VOID
97 PxeBcCopyProxyOffer (
98 IN PXEBC_PRIVATE_DATA *Private,
99 IN UINT32 OfferIndex
100 )
101 {
102 EFI_PXE_BASE_CODE_MODE *Mode;
103 EFI_DHCP4_PACKET *Offer;
104
105 ASSERT (OfferIndex < Private->NumOffers);
106 ASSERT (OfferIndex < PXEBC_MAX_OFFER_NUM);
107
108 Mode = Private->PxeBc.Mode;
109 Offer = &Private->Dhcp4Offers[OfferIndex].Packet.Offer;
110
111 PxeBcCopyEfiDhcp4Packet (&Private->ProxyOffer.Packet.Offer, Offer);
112 CopyMem (&Mode->ProxyOffer, &Offer->Dhcp4, Offer->Length);
113 Mode->ProxyOfferReceived = TRUE;
114
115 PxeBcParseCachedDhcpPacket (&Private->ProxyOffer);
116 }
117
118
119 /**
120 Parse the cached dhcp packet.
121
122 @param CachedPacket Pointer to cached dhcp packet.
123
124 @retval TRUE Succeed to parse and validation.
125 @retval FALSE Fail to parse or validation.
126
127 **/
128 BOOLEAN
129 PxeBcParseCachedDhcpPacket (
130 IN PXEBC_CACHED_DHCP4_PACKET *CachedPacket
131 )
132 {
133 EFI_DHCP4_PACKET *Offer;
134 EFI_DHCP4_PACKET_OPTION **Options;
135 EFI_DHCP4_PACKET_OPTION *Option;
136 UINT8 OfferType;
137 UINTN Index;
138
139 CachedPacket->IsPxeOffer = FALSE;
140 ZeroMem (CachedPacket->Dhcp4Option, sizeof (CachedPacket->Dhcp4Option));
141 ZeroMem (&CachedPacket->PxeVendorOption, sizeof (CachedPacket->PxeVendorOption));
142
143 Offer = &CachedPacket->Packet.Offer;
144 Options = CachedPacket->Dhcp4Option;
145
146 //
147 // Parse interested dhcp options and store their pointers in CachedPacket->Dhcp4Option.
148 //
149 for (Index = 0; Index < PXEBC_DHCP4_TAG_INDEX_MAX; Index++) {
150 Options[Index] = PxeBcParseExtendOptions (
151 Offer->Dhcp4.Option,
152 GET_OPTION_BUFFER_LEN (Offer),
153 mInterestedDhcp4Tags[Index]
154 );
155 }
156
157 //
158 // Check whether is an offer with PXEClient or not.
159 //
160 Option = Options[PXEBC_DHCP4_TAG_INDEX_CLASS_ID];
161 if ((Option != NULL) && (Option->Length >= 9) &&
162 (CompareMem (Option->Data, DEFAULT_CLASS_ID_DATA, 9) == 0)) {
163
164 CachedPacket->IsPxeOffer = TRUE;
165 }
166
167 //
168 // Parse pxe vendor options and store their content/pointers in CachedPacket->PxeVendorOption.
169 //
170 Option = Options[PXEBC_DHCP4_TAG_INDEX_VENDOR];
171 if (CachedPacket->IsPxeOffer && (Option != NULL)) {
172
173 if (!PxeBcParseVendorOptions (Option, &CachedPacket->PxeVendorOption)) {
174 return FALSE;
175 }
176 }
177
178 //
179 // Check whether bootfilename/serverhostname overloaded (See details in dhcp spec).
180 // If overloaded, parse this buffer as nested dhcp options, or just parse bootfilename/
181 // serverhostname option.
182 //
183 Option = Options[PXEBC_DHCP4_TAG_INDEX_OVERLOAD];
184 if ((Option != NULL) && (Option->Data[0] & PXEBC_DHCP4_OVERLOAD_FILE)) {
185
186 Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] = PxeBcParseExtendOptions (
187 (UINT8 *) Offer->Dhcp4.Header.BootFileName,
188 sizeof (Offer->Dhcp4.Header.BootFileName),
189 PXEBC_DHCP4_TAG_BOOTFILE
190 );
191
192 } else if ((Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] == NULL) &&
193 (Offer->Dhcp4.Header.BootFileName[0] != 0)) {
194 //
195 // If the bootfile is not present and bootfilename is present in dhcp packet, just parse it.
196 // And do not count dhcp option header, or else will destory the serverhostname.
197 //
198 Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] = (EFI_DHCP4_PACKET_OPTION *) (&Offer->Dhcp4.Header.BootFileName[0] -
199 OFFSET_OF (EFI_DHCP4_PACKET_OPTION, Data[0]));
200
201 }
202
203 //
204 // Determine offer type of the dhcp packet.
205 //
206 Option = Options[PXEBC_DHCP4_TAG_INDEX_MSG_TYPE];
207 if ((Option == NULL) || (Option->Data[0] == 0)) {
208 //
209 // It's a bootp offer
210 //
211 Option = CachedPacket->Dhcp4Option[PXEBC_DHCP4_TAG_INDEX_BOOTFILE];
212 if (Option == NULL) {
213 //
214 // bootp offer without bootfilename, discard it.
215 //
216 return FALSE;
217 }
218
219 OfferType = DHCP4_PACKET_TYPE_BOOTP;
220
221 } else {
222
223 if (IS_VALID_DISCOVER_VENDOR_OPTION (CachedPacket->PxeVendorOption.BitMap)) {
224 //
225 // It's a pxe10 offer with PXEClient and discover vendor option.
226 //
227 OfferType = DHCP4_PACKET_TYPE_PXE10;
228 } else if (IS_VALID_MTFTP_VENDOR_OPTION (CachedPacket->PxeVendorOption.BitMap)) {
229 //
230 // It's a wfm11a offer with PXEClient and mtftp vendor option, and
231 // return false since mtftp not supported currently.
232 //
233 return FALSE;
234 } else {
235 //
236 // If the binl offer with only PXEClient.
237 //
238 OfferType = (UINT8) ((CachedPacket->IsPxeOffer) ? DHCP4_PACKET_TYPE_BINL : DHCP4_PACKET_TYPE_DHCP_ONLY);
239 }
240 }
241
242 CachedPacket->OfferType = OfferType;
243
244 return TRUE;
245 }
246
247
248 /**
249 Offer dhcp service with a BINL dhcp offer.
250
251 @param Private Pointer to PxeBc private data.
252 @param Index Index of cached packets as complements of pxe mode data,
253 the index is maximum offer number.
254
255 @retval TRUE Offer the service successfully under priority BINL.
256 @retval FALSE Boot Service failed, parse cached dhcp packet failed or this
257 BINL ack cannot find options set or bootfile name specified.
258
259 **/
260 BOOLEAN
261 PxeBcTryBinl (
262 IN PXEBC_PRIVATE_DATA *Private,
263 IN UINT32 Index
264 )
265 {
266 EFI_DHCP4_PACKET *Offer;
267 EFI_IP_ADDRESS ServerIp;
268 EFI_STATUS Status;
269 PXEBC_CACHED_DHCP4_PACKET *CachedPacket;
270 EFI_DHCP4_PACKET *Reply;
271
272 ASSERT (Index < PXEBC_MAX_OFFER_NUM);
273 ASSERT (Private->Dhcp4Offers[Index].OfferType == DHCP4_PACKET_TYPE_BINL);
274
275 Offer = &Private->Dhcp4Offers[Index].Packet.Offer;
276
277 //
278 // use option 54, if zero, use siaddr in header
279 //
280 ZeroMem (&ServerIp, sizeof(EFI_IP_ADDRESS));
281 if (Private->Dhcp4Offers[Index].Dhcp4Option[PXEBC_DHCP4_TAG_INDEX_SERVER_ID] != NULL) {
282 CopyMem (
283 &ServerIp.Addr[0],
284 Private->Dhcp4Offers[Index].Dhcp4Option[PXEBC_DHCP4_TAG_INDEX_SERVER_ID]->Data,
285 sizeof (EFI_IPv4_ADDRESS)
286 );
287 } else {
288 CopyMem (
289 &ServerIp.Addr[0],
290 &Offer->Dhcp4.Header.ServerAddr,
291 sizeof (EFI_IPv4_ADDRESS)
292 );
293 }
294 if (ServerIp.Addr[0] == 0) {
295 return FALSE;
296 }
297
298 CachedPacket = &Private->ProxyOffer;
299 Reply = &CachedPacket->Packet.Offer;
300
301 Status = PxeBcDiscvBootService (
302 Private,
303 0,
304 NULL,
305 FALSE,
306 &ServerIp,
307 0,
308 NULL,
309 FALSE,
310 Reply
311 );
312 if (EFI_ERROR (Status)) {
313 return FALSE;
314 }
315
316 if (!PxeBcParseCachedDhcpPacket (CachedPacket)) {
317 return FALSE;
318 }
319
320 if ((CachedPacket->OfferType != DHCP4_PACKET_TYPE_PXE10) &&
321 (CachedPacket->Dhcp4Option[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] == NULL)) {
322 //
323 // This BINL ack doesn't have discovery options set or bootfile name
324 // specified.
325 //
326 return FALSE;
327 }
328
329 Private->PxeBc.Mode->ProxyOfferReceived = TRUE;
330 CopyMem (&Private->PxeBc.Mode->ProxyOffer, &Reply->Dhcp4, Reply->Length);
331
332 return TRUE;
333 }
334
335
336 /**
337 Offer dhcp service for each proxy with a BINL dhcp offer.
338
339 @param Private Pointer to PxeBc private data
340 @param OfferIndex Pointer to the index of cached packets as complements of
341 pxe mode data, the index is maximum offer number.
342
343 @return If there is no service needed offer return FALSE, otherwise TRUE.
344
345 **/
346 BOOLEAN
347 PxeBcTryBinlProxy (
348 IN PXEBC_PRIVATE_DATA *Private,
349 OUT UINT32 *OfferIndex
350 )
351 {
352 UINT32 Index;
353
354 for (Index = 0; Index < Private->ProxyIndex[DHCP4_PACKET_TYPE_BINL]; Index++) {
355
356 *OfferIndex = Private->BinlIndex[Index];
357 //
358 // Try this BINL proxy offer
359 //
360 if (PxeBcTryBinl (Private, *OfferIndex)) {
361 return TRUE;
362 }
363 }
364
365 return FALSE;
366 }
367
368
369 /**
370 This function is to check the selected proxy offer (include BINL dhcp offer and
371 DHCP_ONLY offer ) and set the flag and copy the DHCP packets to the Pxe base code
372 mode structure.
373
374 @param Private Pointer to PxeBc private data.
375
376 @retval EFI_SUCCESS Operational successful.
377 @retval EFI_NO_RESPONSE Offer dhcp service failed.
378
379 **/
380 EFI_STATUS
381 PxeBcCheckSelectedOffer (
382 IN PXEBC_PRIVATE_DATA *Private
383 )
384 {
385 PXEBC_CACHED_DHCP4_PACKET *SelectedOffer;
386 EFI_DHCP4_PACKET_OPTION **Options;
387 UINT32 Index;
388 EFI_DHCP4_PACKET *Offer;
389 UINT32 ProxyOfferIndex;
390 EFI_STATUS Status;
391 EFI_PXE_BASE_CODE_MODE *Mode;
392 EFI_DHCP4_PACKET *Ack;
393
394 ASSERT (Private->SelectedOffer != 0);
395
396 Status = EFI_SUCCESS;
397 SelectedOffer = &Private->Dhcp4Offers[Private->SelectedOffer - 1];
398 Options = SelectedOffer->Dhcp4Option;
399
400 if (SelectedOffer->OfferType == DHCP4_PACKET_TYPE_BINL) {
401 //
402 // The addresses are acquired from a BINL dhcp offer, try BINL to get
403 // the bootfile name
404 //
405 if (!PxeBcTryBinl (Private, Private->SelectedOffer - 1)) {
406 Status = EFI_NO_RESPONSE;
407 }
408 } else if (SelectedOffer->OfferType == DHCP4_PACKET_TYPE_DHCP_ONLY) {
409 //
410 // The selected offer to finish the D.O.R.A. is a DHCP only offer, we need
411 // try proxy offers if there are some, othewise the bootfile name must be
412 // set in this DHCP only offer.
413 //
414 if (Private->GotProxyOffer) {
415 //
416 // Get rid of the compiler warning.
417 //
418 ProxyOfferIndex = 0;
419 if (Private->SortOffers) {
420 //
421 // The offers are sorted before selecting, the proxy offer type must be
422 // already determined.
423 //
424 ASSERT (Private->ProxyIndex[Private->ProxyOfferType] > 0);
425
426 if (Private->ProxyOfferType == DHCP4_PACKET_TYPE_BINL) {
427 //
428 // We buffer all received BINL proxy offers, try them all one by one
429 //
430 if (!PxeBcTryBinlProxy (Private, &ProxyOfferIndex)) {
431 Status = EFI_NO_RESPONSE;
432 }
433 } else {
434 //
435 // For other types, only one proxy offer is buffered.
436 //
437 ProxyOfferIndex = Private->ProxyIndex[Private->ProxyOfferType] - 1;
438 }
439 } else {
440 //
441 // The proxy offer type is not determined, choose proxy offer in the
442 // received order.
443 //
444 Status = EFI_NO_RESPONSE;
445
446 for (Index = 0; Index < Private->NumOffers; Index++) {
447
448 Offer = &Private->Dhcp4Offers[Index].Packet.Offer;
449 if (!IS_PROXY_DHCP_OFFER (Offer)) {
450 //
451 // Skip non proxy dhcp offers.
452 //
453 continue;
454 }
455
456 if (Private->Dhcp4Offers[Index].OfferType == DHCP4_PACKET_TYPE_BINL) {
457 //
458 // Try BINL
459 //
460 if (!PxeBcTryBinl (Private, Index)) {
461 //
462 // Failed, skip to the next offer
463 //
464 continue;
465 }
466 }
467
468 Private->ProxyOfferType = Private->Dhcp4Offers[Index].OfferType;
469 ProxyOfferIndex = Index;
470 Status = EFI_SUCCESS;
471 break;
472 }
473 }
474
475 if (!EFI_ERROR (Status) && (Private->ProxyOfferType != DHCP4_PACKET_TYPE_BINL)) {
476 //
477 // Copy the proxy offer to Mode and set the flag
478 //
479 PxeBcCopyProxyOffer (Private, ProxyOfferIndex);
480 }
481 } else {
482 //
483 // No proxy offer is received, the bootfile name MUST be set.
484 //
485 ASSERT (Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] != NULL);
486 }
487 }
488
489 if (!EFI_ERROR (Status)) {
490 //
491 // Everything is OK, set the flag and copy the DHCP packets.
492 //
493 Mode = Private->PxeBc.Mode;
494 Offer = &SelectedOffer->Packet.Offer;
495
496 //
497 // The discover packet is already copied, just set flag here.
498 //
499 Mode->DhcpDiscoverValid = TRUE;
500
501 Ack = &Private->Dhcp4Ack.Packet.Ack;
502 if (SelectedOffer->OfferType == DHCP4_PACKET_TYPE_BOOTP) {
503 //
504 // Other type of ACK is already cached. Bootp is special that we should
505 // use the bootp reply as the ACK and put it into the DHCP_ONLY buffer.
506 //
507 PxeBcCopyEfiDhcp4Packet (&Private->Dhcp4Ack.Packet.Ack, Offer);
508 }
509
510 PxeBcParseCachedDhcpPacket (&Private->Dhcp4Ack);
511
512 Mode->DhcpAckReceived = TRUE;
513
514 //
515 // Copy the dhcp ack.
516 //
517 CopyMem (&Mode->DhcpAck, &Ack->Dhcp4, Ack->Length);
518 }
519
520 return Status;
521 }
522
523
524 /**
525 Cache the Dhcp4 packet offer, Parse and validate each option of the packet.
526
527 @param Private Pointer to PxeBc private data.
528 @param RcvdOffer Pointer to the received Dhcp proxy offer packet.
529
530 **/
531 VOID
532 PxeBcCacheDhcpOffer (
533 IN PXEBC_PRIVATE_DATA *Private,
534 IN EFI_DHCP4_PACKET *RcvdOffer
535 )
536 {
537 PXEBC_CACHED_DHCP4_PACKET *CachedOffer;
538 EFI_DHCP4_PACKET *Offer;
539 UINT8 OfferType;
540
541 CachedOffer = &Private->Dhcp4Offers[Private->NumOffers];
542 Offer = &CachedOffer->Packet.Offer;
543
544 //
545 // Cache the orignal dhcp packet
546 //
547 PxeBcCopyEfiDhcp4Packet (Offer, RcvdOffer);
548
549 //
550 // Parse and validate the options (including dhcp option and vendor option)
551 //
552 if (!PxeBcParseCachedDhcpPacket (CachedOffer)) {
553 return ;
554 }
555
556 OfferType = CachedOffer->OfferType;
557 ASSERT (OfferType < DHCP4_PACKET_TYPE_MAX);
558
559 if (OfferType == DHCP4_PACKET_TYPE_BOOTP) {
560
561 if (Private->BootpIndex != 0) {
562 //
563 // Only cache the first bootp offer, discard others.
564 //
565 return ;
566 } else {
567 //
568 // Take as a dhcp only offer, but record index specifically.
569 //
570 Private->BootpIndex = Private->NumOffers + 1;
571 }
572 } else {
573
574 if (IS_PROXY_DHCP_OFFER (Offer)) {
575 //
576 // It's a proxy dhcp offer with no your address, including pxe10, wfm11a or binl offer.
577 //
578 Private->GotProxyOffer = TRUE;
579
580 if (OfferType == DHCP4_PACKET_TYPE_BINL) {
581 //
582 // Cache all binl offers.
583 //
584 Private->BinlIndex[Private->ProxyIndex[DHCP4_PACKET_TYPE_BINL]] = Private->NumOffers;
585 Private->ProxyIndex[DHCP4_PACKET_TYPE_BINL]++;
586 } else if (Private->ProxyIndex[OfferType] != 0) {
587 //
588 // Only cache the first pxe10/wfm11a offers each, discard the others.
589 //
590 return ;
591 } else {
592 //
593 // Record index of the proxy dhcp offer with type other than binl.
594 //
595 Private->ProxyIndex[OfferType] = Private->NumOffers + 1;
596 }
597 } else {
598 //
599 // It's a dhcp offer with your address.
600 //
601 ASSERT (Private->ServerCount[OfferType] < PXEBC_MAX_OFFER_NUM);
602 Private->OfferIndex[OfferType][Private->ServerCount[OfferType]] = Private->NumOffers;
603 Private->ServerCount[OfferType]++;
604 }
605 }
606
607 //
608 // Count the accepted offers.
609 //
610 Private->NumOffers++;
611 }
612
613
614 /**
615 Select the specified proxy offer, such as BINL, DHCP_ONLY and so on.
616 If the proxy does not exist, try offers with bootfile.
617
618 @param Private Pointer to PxeBc private data.
619
620 **/
621 VOID
622 PxeBcSelectOffer (
623 IN PXEBC_PRIVATE_DATA *Private
624 )
625 {
626 UINT32 Index;
627 UINT32 OfferIndex;
628 EFI_DHCP4_PACKET *Offer;
629
630 Private->SelectedOffer = 0;
631
632 if (Private->SortOffers) {
633 //
634 // Select offer according to the priority
635 //
636 if (Private->ServerCount[DHCP4_PACKET_TYPE_PXE10] > 0) {
637 //
638 // DHCP with PXE10
639 //
640 Private->SelectedOffer = Private->OfferIndex[DHCP4_PACKET_TYPE_PXE10][0] + 1;
641
642 } else if (Private->ServerCount[DHCP4_PACKET_TYPE_WFM11A] > 0) {
643 //
644 // DHCP with WfM
645 //
646 Private->SelectedOffer = Private->OfferIndex[DHCP4_PACKET_TYPE_WFM11A][0] + 1;
647
648 } else if ((Private->ProxyIndex[DHCP4_PACKET_TYPE_PXE10] > 0) &&
649 (Private->ServerCount[DHCP4_PACKET_TYPE_DHCP_ONLY] > 0)
650 ) {
651 //
652 // DHCP only and proxy DHCP with PXE10
653 //
654 Private->SelectedOffer = Private->OfferIndex[DHCP4_PACKET_TYPE_DHCP_ONLY][0] + 1;
655 Private->ProxyOfferType = DHCP4_PACKET_TYPE_PXE10;
656
657 } else if ((Private->ProxyIndex[DHCP4_PACKET_TYPE_WFM11A] > 0) &&
658 (Private->ServerCount[DHCP4_PACKET_TYPE_DHCP_ONLY] > 0)
659 ) {
660 //
661 // DHCP only and proxy DHCP with WfM
662 //
663 Private->SelectedOffer = Private->OfferIndex[DHCP4_PACKET_TYPE_DHCP_ONLY][0] + 1;
664 Private->ProxyOfferType = DHCP4_PACKET_TYPE_WFM11A;
665
666 } else if (Private->ServerCount[DHCP4_PACKET_TYPE_BINL] > 0) {
667 //
668 // DHCP with BINL
669 //
670 Private->SelectedOffer = Private->OfferIndex[DHCP4_PACKET_TYPE_BINL][0] + 1;
671
672 } else if ((Private->ProxyIndex[DHCP4_PACKET_TYPE_BINL] > 0) &&
673 (Private->ServerCount[DHCP4_PACKET_TYPE_DHCP_ONLY] > 0)
674 ) {
675 //
676 // DHCP only and proxy DHCP with BINL
677 //
678 Private->SelectedOffer = Private->OfferIndex[DHCP4_PACKET_TYPE_DHCP_ONLY][0] + 1;
679 Private->ProxyOfferType = DHCP4_PACKET_TYPE_BINL;
680
681 } else {
682 //
683 // Try offers with bootfile
684 //
685 for (Index = 0; Index < Private->ServerCount[DHCP4_PACKET_TYPE_DHCP_ONLY]; Index++) {
686 //
687 // Select the first DHCP only offer with bootfile
688 //
689 OfferIndex = Private->OfferIndex[DHCP4_PACKET_TYPE_DHCP_ONLY][Index];
690 if (Private->Dhcp4Offers[OfferIndex].Dhcp4Option[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] != NULL) {
691 Private->SelectedOffer = OfferIndex + 1;
692 break;
693 }
694 }
695
696 if (Private->SelectedOffer == 0) {
697 //
698 // Select the Bootp reply with bootfile if any
699 //
700 Private->SelectedOffer = Private->BootpIndex;
701 }
702 }
703 } else {
704 //
705 // Try the offers in the received order.
706 //
707 for (Index = 0; Index < Private->NumOffers; Index++) {
708
709 Offer = &Private->Dhcp4Offers[Index].Packet.Offer;
710
711 if (IS_PROXY_DHCP_OFFER (Offer)) {
712 //
713 // Skip proxy offers
714 //
715 continue;
716 }
717
718 if ((Private->Dhcp4Offers[Index].OfferType == DHCP4_PACKET_TYPE_DHCP_ONLY) &&
719 ((!Private->GotProxyOffer) && (Private->Dhcp4Offers[Index].Dhcp4Option[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] == NULL))) {
720 //
721 // DHCP only offer but no proxy offer received and no bootfile option in this offer
722 //
723 continue;
724 }
725
726 Private->SelectedOffer = Index + 1;
727 break;
728 }
729 }
730 }
731
732
733 /**
734 Callback routine.
735
736 EFI_DHCP4_CALLBACK is provided by the consumer of the EFI DHCPv4 Protocol driver
737 to intercept events that occurred in the configuration process. This structure
738 provides advanced control of each state transition of the DHCP process. The
739 returned status code determines the behavior of the EFI DHCPv4 Protocol driver.
740 There are three possible returned values, which are described in the following
741 table.
742
743 @param This Pointer to the EFI DHCPv4 Protocol instance that is used to
744 configure this callback function.
745 @param Context Pointer to the context that is initialized by
746 EFI_DHCP4_PROTOCOL.Configure().
747 @param CurrentState The current operational state of the EFI DHCPv4 Protocol
748 driver.
749 @param Dhcp4Event The event that occurs in the current state, which usually means a
750 state transition.
751 @param Packet The DHCP packet that is going to be sent or already received.
752 @param NewPacket The packet that is used to replace the above Packet.
753
754 @retval EFI_SUCCESS Tells the EFI DHCPv4 Protocol driver to continue the DHCP process.
755 @retval EFI_NOT_READY Only used in the Dhcp4Selecting state. The EFI DHCPv4 Protocol
756 driver will continue to wait for more DHCPOFFER packets until the retry
757 timeout expires.
758 @retval EFI_ABORTED Tells the EFI DHCPv4 Protocol driver to abort the current process and
759 return to the Dhcp4Init or Dhcp4InitReboot state.
760
761 **/
762 EFI_STATUS
763 EFIAPI
764 PxeBcDhcpCallBack (
765 IN EFI_DHCP4_PROTOCOL * This,
766 IN VOID *Context,
767 IN EFI_DHCP4_STATE CurrentState,
768 IN EFI_DHCP4_EVENT Dhcp4Event,
769 IN EFI_DHCP4_PACKET * Packet OPTIONAL,
770 OUT EFI_DHCP4_PACKET **NewPacket OPTIONAL
771 )
772 {
773 PXEBC_PRIVATE_DATA *Private;
774 EFI_PXE_BASE_CODE_MODE *Mode;
775 EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL *Callback;
776 EFI_DHCP4_PACKET_OPTION *MaxMsgSize;
777 UINT16 Value;
778 EFI_STATUS Status;
779 BOOLEAN Received;
780 CHAR8 *SystemSerialNumber;
781 EFI_DHCP4_HEADER *DhcpHeader;
782
783 if ((Dhcp4Event != Dhcp4RcvdOffer) &&
784 (Dhcp4Event != Dhcp4SelectOffer) &&
785 (Dhcp4Event != Dhcp4SendDiscover) &&
786 (Dhcp4Event != Dhcp4RcvdAck) &&
787 (Dhcp4Event != Dhcp4SendRequest)) {
788 return EFI_SUCCESS;
789 }
790
791 Private = (PXEBC_PRIVATE_DATA *) Context;
792 Mode = Private->PxeBc.Mode;
793 Callback = Private->PxeBcCallback;
794
795 //
796 // Override the Maximum DHCP Message Size.
797 //
798 MaxMsgSize = PxeBcParseExtendOptions (
799 Packet->Dhcp4.Option,
800 GET_OPTION_BUFFER_LEN (Packet),
801 PXEBC_DHCP4_TAG_MAXMSG
802 );
803 if (MaxMsgSize != NULL) {
804 Value = HTONS (PXEBC_DHCP4_MAX_PACKET_SIZE);
805 CopyMem (MaxMsgSize->Data, &Value, sizeof (Value));
806 }
807
808 if ((Dhcp4Event != Dhcp4SelectOffer) && (Callback != NULL)) {
809 Received = (BOOLEAN) ((Dhcp4Event == Dhcp4RcvdOffer) || (Dhcp4Event == Dhcp4RcvdAck));
810 Status = Callback->Callback (
811 Callback,
812 Private->Function,
813 Received,
814 Packet->Length,
815 (EFI_PXE_BASE_CODE_PACKET *) &Packet->Dhcp4
816 );
817 if (Status != EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE) {
818 return EFI_ABORTED;
819 }
820 }
821
822 Status = EFI_SUCCESS;
823
824 switch (Dhcp4Event) {
825
826 case Dhcp4SendDiscover:
827 case Dhcp4SendRequest:
828 if (Mode->SendGUID) {
829 //
830 // send the system GUID instead of the MAC address as the hardware address
831 // in the DHCP packet header.
832 //
833 DhcpHeader = &Packet->Dhcp4.Header;
834
835 if (EFI_ERROR (GetSmbiosSystemGuidAndSerialNumber ((EFI_GUID *) DhcpHeader->ClientHwAddr, &SystemSerialNumber))) {
836 //
837 // GUID not yet set - send all 0xff's to show programable (via SetVariable)
838 // SetMem(DHCPV4_OPTIONS_BUFFER.DhcpPlatformId.Guid, sizeof(EFI_GUID), 0xff);
839 // GUID not yet set - send all 0's to show not programable
840 //
841 ZeroMem (DhcpHeader->ClientHwAddr, sizeof (EFI_GUID));
842 }
843
844 DhcpHeader->HwAddrLen = sizeof (EFI_GUID);
845 }
846
847 if (Dhcp4Event == Dhcp4SendDiscover) {
848 //
849 // Cache the dhcp discover packet, of which some information will be used later.
850 //
851 CopyMem (Mode->DhcpDiscover.Raw, &Packet->Dhcp4, Packet->Length);
852 }
853
854 break;
855
856 case Dhcp4RcvdOffer:
857 Status = EFI_NOT_READY;
858 if (Private->NumOffers < PXEBC_MAX_OFFER_NUM) {
859 //
860 // Cache the dhcp offers in Private->Dhcp4Offers[]
861 //
862 PxeBcCacheDhcpOffer (Private, Packet);
863 }
864
865 break;
866
867 case Dhcp4SelectOffer:
868 //
869 // Select an offer, if succeeded, Private->SelectedOffer points to
870 // the index of the selected one.
871 //
872 PxeBcSelectOffer (Private);
873
874 if (Private->SelectedOffer == 0) {
875 Status = EFI_ABORTED;
876 } else {
877 *NewPacket = &Private->Dhcp4Offers[Private->SelectedOffer - 1].Packet.Offer;
878 }
879
880 break;
881
882 case Dhcp4RcvdAck:
883 //
884 // Cache Ack
885 //
886 ASSERT (Private->SelectedOffer != 0);
887
888 PxeBcCopyEfiDhcp4Packet (&Private->Dhcp4Ack.Packet.Ack, Packet);
889 break;
890
891 default:
892 break;
893 }
894
895 return Status;
896 }
897
898
899 /**
900 Initialize the DHCP options and build the option list.
901
902 @param Private Pointer to PxeBc private data.
903 @param OptList Pointer to a DHCP option list.
904
905 @param IsDhcpDiscover Discover dhcp option or not.
906
907 @return The index item number of the option list.
908
909 **/
910 UINT32
911 PxeBcBuildDhcpOptions (
912 IN PXEBC_PRIVATE_DATA *Private,
913 IN EFI_DHCP4_PACKET_OPTION **OptList,
914 IN BOOLEAN IsDhcpDiscover
915 )
916 {
917 UINT32 Index;
918 PXEBC_DHCP4_OPTION_ENTRY OptEnt;
919 UINT16 Value;
920 CHAR8 *SystemSerialNumber;
921
922 Index = 0;
923 OptList[0] = (EFI_DHCP4_PACKET_OPTION *) Private->OptionBuffer;
924
925 if (!IsDhcpDiscover) {
926 //
927 // Append message type.
928 //
929 OptList[Index]->OpCode = PXEBC_DHCP4_TAG_MSG_TYPE;
930 OptList[Index]->Length = 1;
931 OptEnt.Mesg = (PXEBC_DHCP4_OPTION_MESG *) OptList[Index]->Data;
932 OptEnt.Mesg->Type = PXEBC_DHCP4_MSG_TYPE_REQUEST;
933 Index++;
934 OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);
935
936 //
937 // Append max message size.
938 //
939 OptList[Index]->OpCode = PXEBC_DHCP4_TAG_MAXMSG;
940 OptList[Index]->Length = sizeof (PXEBC_DHCP4_OPTION_MAX_MESG_SIZE);
941 OptEnt.MaxMesgSize = (PXEBC_DHCP4_OPTION_MAX_MESG_SIZE *) OptList[Index]->Data;
942 Value = NTOHS (PXEBC_DHCP4_MAX_PACKET_SIZE);
943 CopyMem (&OptEnt.MaxMesgSize->Size, &Value, sizeof (UINT16));
944 Index++;
945 OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);
946 }
947 //
948 // Parameter request list option.
949 //
950 OptList[Index]->OpCode = PXEBC_DHCP4_TAG_PARA_LIST;
951 OptList[Index]->Length = 35;
952 OptEnt.Para = (PXEBC_DHCP4_OPTION_PARA *) OptList[Index]->Data;
953 OptEnt.Para->ParaList[0] = PXEBC_DHCP4_TAG_NETMASK;
954 OptEnt.Para->ParaList[1] = PXEBC_DHCP4_TAG_TIME_OFFSET;
955 OptEnt.Para->ParaList[2] = PXEBC_DHCP4_TAG_ROUTER;
956 OptEnt.Para->ParaList[3] = PXEBC_DHCP4_TAG_TIME_SERVER;
957 OptEnt.Para->ParaList[4] = PXEBC_DHCP4_TAG_NAME_SERVER;
958 OptEnt.Para->ParaList[5] = PXEBC_DHCP4_TAG_DNS_SERVER;
959 OptEnt.Para->ParaList[6] = PXEBC_DHCP4_TAG_HOSTNAME;
960 OptEnt.Para->ParaList[7] = PXEBC_DHCP4_TAG_BOOTFILE_LEN;
961 OptEnt.Para->ParaList[8] = PXEBC_DHCP4_TAG_DOMAINNAME;
962 OptEnt.Para->ParaList[9] = PXEBC_DHCP4_TAG_ROOTPATH;
963 OptEnt.Para->ParaList[10] = PXEBC_DHCP4_TAG_EXTEND_PATH;
964 OptEnt.Para->ParaList[11] = PXEBC_DHCP4_TAG_EMTU;
965 OptEnt.Para->ParaList[12] = PXEBC_DHCP4_TAG_TTL;
966 OptEnt.Para->ParaList[13] = PXEBC_DHCP4_TAG_BROADCAST;
967 OptEnt.Para->ParaList[14] = PXEBC_DHCP4_TAG_NIS_DOMAIN;
968 OptEnt.Para->ParaList[15] = PXEBC_DHCP4_TAG_NIS_SERVER;
969 OptEnt.Para->ParaList[16] = PXEBC_DHCP4_TAG_NTP_SERVER;
970 OptEnt.Para->ParaList[17] = PXEBC_DHCP4_TAG_VENDOR;
971 OptEnt.Para->ParaList[18] = PXEBC_DHCP4_TAG_REQUEST_IP;
972 OptEnt.Para->ParaList[19] = PXEBC_DHCP4_TAG_LEASE;
973 OptEnt.Para->ParaList[20] = PXEBC_DHCP4_TAG_SERVER_ID;
974 OptEnt.Para->ParaList[21] = PXEBC_DHCP4_TAG_T1;
975 OptEnt.Para->ParaList[22] = PXEBC_DHCP4_TAG_T2;
976 OptEnt.Para->ParaList[23] = PXEBC_DHCP4_TAG_CLASS_ID;
977 OptEnt.Para->ParaList[24] = PXEBC_DHCP4_TAG_TFTP;
978 OptEnt.Para->ParaList[25] = PXEBC_DHCP4_TAG_BOOTFILE;
979 OptEnt.Para->ParaList[26] = PXEBC_PXE_DHCP4_TAG_UUID;
980 OptEnt.Para->ParaList[27] = 0x80;
981 OptEnt.Para->ParaList[28] = 0x81;
982 OptEnt.Para->ParaList[29] = 0x82;
983 OptEnt.Para->ParaList[30] = 0x83;
984 OptEnt.Para->ParaList[31] = 0x84;
985 OptEnt.Para->ParaList[32] = 0x85;
986 OptEnt.Para->ParaList[33] = 0x86;
987 OptEnt.Para->ParaList[34] = 0x87;
988 Index++;
989 OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);
990
991 //
992 // Append UUID/Guid-based client identifier option
993 //
994 OptList[Index]->OpCode = PXEBC_PXE_DHCP4_TAG_UUID;
995 OptList[Index]->Length = sizeof (PXEBC_DHCP4_OPTION_UUID);
996 OptEnt.Uuid = (PXEBC_DHCP4_OPTION_UUID *) OptList[Index]->Data;
997 OptEnt.Uuid->Type = 0;
998 Index++;
999 OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);
1000
1001 if (EFI_ERROR (GetSmbiosSystemGuidAndSerialNumber ((EFI_GUID *) OptEnt.Uuid->Guid, &SystemSerialNumber))) {
1002 //
1003 // GUID not yet set - send all 0xff's to show programable (via SetVariable)
1004 // SetMem(DHCPV4_OPTIONS_BUFFER.DhcpPlatformId.Guid, sizeof(EFI_GUID), 0xff);
1005 // GUID not yet set - send all 0's to show not programable
1006 //
1007 ZeroMem (OptEnt.Uuid->Guid, sizeof (EFI_GUID));
1008 }
1009
1010 //
1011 // Append client network device interface option
1012 //
1013 OptList[Index]->OpCode = PXEBC_PXE_DHCP4_TAG_UNDI;
1014 OptList[Index]->Length = sizeof (PXEBC_DHCP4_OPTION_UNDI);
1015 OptEnt.Undi = (PXEBC_DHCP4_OPTION_UNDI *) OptList[Index]->Data;
1016 if (Private->Nii != NULL) {
1017 OptEnt.Undi->Type = Private->Nii->Type;
1018 OptEnt.Undi->MajorVer = Private->Nii->MajorVer;
1019 OptEnt.Undi->MinorVer = Private->Nii->MinorVer;
1020 } else {
1021 OptEnt.Undi->Type = DEFAULT_UNDI_TYPE;
1022 OptEnt.Undi->MajorVer = DEFAULT_UNDI_MAJOR;
1023 OptEnt.Undi->MinorVer = DEFAULT_UNDI_MINOR;
1024 }
1025
1026 Index++;
1027 OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);
1028
1029 //
1030 // Append client system architecture option
1031 //
1032 OptList[Index]->OpCode = PXEBC_PXE_DHCP4_TAG_ARCH;
1033 OptList[Index]->Length = sizeof (PXEBC_DHCP4_OPTION_ARCH);
1034 OptEnt.Arch = (PXEBC_DHCP4_OPTION_ARCH *) OptList[Index]->Data;
1035 Value = HTONS (SYS_ARCH);
1036 CopyMem (&OptEnt.Arch->Type, &Value, sizeof (UINT16));
1037 Index++;
1038 OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);
1039
1040 //
1041 // Append client system architecture option
1042 //
1043 OptList[Index]->OpCode = PXEBC_DHCP4_TAG_CLASS_ID;
1044 OptList[Index]->Length = sizeof (PXEBC_DHCP4_OPTION_CLID);
1045 OptEnt.Clid = (PXEBC_DHCP4_OPTION_CLID *) OptList[Index]->Data;
1046 CopyMem (OptEnt.Clid, DEFAULT_CLASS_ID_DATA, sizeof (PXEBC_DHCP4_OPTION_CLID));
1047 CvtNum (SYS_ARCH, OptEnt.Clid->ArchitectureType, sizeof (OptEnt.Clid->ArchitectureType));
1048
1049 if (Private->Nii != NULL) {
1050 //
1051 // If NII protocol exists, update DHCP option data
1052 //
1053 CopyMem (OptEnt.Clid->InterfaceName, Private->Nii->StringId, sizeof (OptEnt.Clid->InterfaceName));
1054 CvtNum (Private->Nii->MajorVer, OptEnt.Clid->UndiMajor, sizeof (OptEnt.Clid->UndiMajor));
1055 CvtNum (Private->Nii->MinorVer, OptEnt.Clid->UndiMinor, sizeof (OptEnt.Clid->UndiMinor));
1056 }
1057
1058 Index++;
1059
1060 return Index;
1061 }
1062
1063
1064 /**
1065 Discover the boot of service and initialize the vendor option if exists.
1066
1067 @param Private Pointer to PxeBc private data.
1068 @param Type PxeBc option boot item type
1069 @param Layer PxeBc option boot item layer
1070 @param UseBis Use BIS or not
1071 @param DestIp Ip address for server
1072 @param IpCount The total count of the server ip address
1073 @param SrvList Server list
1074 @param IsDiscv Discover the vendor or not
1075 @param Reply The dhcp4 packet of Pxe reply
1076
1077 @retval EFI_SUCCESS Operation succeeds.
1078 @retval EFI_OUT_OF_RESOURCES Allocate memory pool failed.
1079 @retval EFI_NOT_FOUND There is no vendor option exists.
1080 @retval EFI_TIMEOUT Send Pxe Discover time out.
1081
1082 **/
1083 EFI_STATUS
1084 PxeBcDiscvBootService (
1085 IN PXEBC_PRIVATE_DATA * Private,
1086 IN UINT16 Type,
1087 IN UINT16 *Layer,
1088 IN BOOLEAN UseBis,
1089 IN EFI_IP_ADDRESS * DestIp,
1090 IN UINT16 IpCount,
1091 IN EFI_PXE_BASE_CODE_SRVLIST * SrvList,
1092 IN BOOLEAN IsDiscv,
1093 OUT EFI_DHCP4_PACKET * Reply OPTIONAL
1094 )
1095 {
1096 EFI_PXE_BASE_CODE_UDP_PORT Sport;
1097 EFI_PXE_BASE_CODE_MODE *Mode;
1098 EFI_DHCP4_PROTOCOL *Dhcp4;
1099 EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN Token;
1100 BOOLEAN IsBCast;
1101 EFI_STATUS Status;
1102 UINT16 RepIndex;
1103 UINT16 SrvIndex;
1104 UINT16 TryIndex;
1105 EFI_DHCP4_LISTEN_POINT ListenPoint;
1106 EFI_DHCP4_PACKET *Response;
1107 EFI_DHCP4_PACKET_OPTION *OptList[PXEBC_DHCP4_MAX_OPTION_NUM];
1108 UINT32 OptCount;
1109 EFI_DHCP4_PACKET_OPTION *PxeOpt;
1110 PXEBC_OPTION_BOOT_ITEM *PxeBootItem;
1111 UINT8 VendorOptLen;
1112 CHAR8 *SystemSerialNumber;
1113 EFI_DHCP4_HEADER *DhcpHeader;
1114 UINT32 Xid;
1115
1116 Mode = Private->PxeBc.Mode;
1117 Dhcp4 = Private->Dhcp4;
1118 Status = EFI_SUCCESS;
1119
1120 ZeroMem (&Token, sizeof (EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN));
1121
1122 if (DestIp == NULL) {
1123 Sport = PXEBC_DHCP4_S_PORT;
1124 IsBCast = TRUE;
1125 } else {
1126 Sport = PXEBC_BS_DISCOVER_PORT;
1127 IsBCast = FALSE;
1128 }
1129
1130 if (!UseBis && Layer != NULL) {
1131 *Layer &= EFI_PXE_BASE_CODE_BOOT_LAYER_MASK;
1132 }
1133
1134 OptCount = PxeBcBuildDhcpOptions (Private, OptList, FALSE);
1135
1136 if (IsDiscv) {
1137 ASSERT (Layer != NULL);
1138 //
1139 // Add vendor option of PXE_BOOT_ITEM
1140 //
1141 VendorOptLen = (sizeof (EFI_DHCP4_PACKET_OPTION) - 1) * 2 + sizeof (PXEBC_OPTION_BOOT_ITEM) + 1;
1142 OptList[OptCount] = AllocatePool (VendorOptLen);
1143 if (OptList[OptCount] == NULL) {
1144 return EFI_OUT_OF_RESOURCES;
1145 }
1146
1147 OptList[OptCount]->OpCode = PXEBC_DHCP4_TAG_VENDOR;
1148 OptList[OptCount]->Length = (UINT8) (VendorOptLen - 2);
1149 PxeOpt = (EFI_DHCP4_PACKET_OPTION *) OptList[OptCount]->Data;
1150 PxeOpt->OpCode = PXEBC_VENDOR_TAG_BOOT_ITEM;
1151 PxeOpt->Length = sizeof (PXEBC_OPTION_BOOT_ITEM);
1152 PxeBootItem = (PXEBC_OPTION_BOOT_ITEM *) PxeOpt->Data;
1153 PxeBootItem->Type = HTONS (Type);
1154 PxeBootItem->Layer = HTONS (*Layer);
1155 PxeOpt->Data[PxeOpt->Length] = PXEBC_DHCP4_TAG_EOP;
1156
1157 OptCount++;
1158 }
1159
1160 Status = Dhcp4->Build (Dhcp4, &Private->SeedPacket, 0, NULL, OptCount, OptList, &Token.Packet);
1161
1162 if (IsDiscv) {
1163 gBS->FreePool (OptList[OptCount - 1]);
1164 }
1165
1166 if (EFI_ERROR (Status)) {
1167 return Status;
1168 }
1169
1170 DhcpHeader = &Token.Packet->Dhcp4.Header;
1171 if (Mode->SendGUID) {
1172 if (EFI_ERROR (GetSmbiosSystemGuidAndSerialNumber ((EFI_GUID *) DhcpHeader->ClientHwAddr, &SystemSerialNumber))) {
1173 //
1174 // GUID not yet set - send all 0's to show not programable
1175 //
1176 ZeroMem (DhcpHeader->ClientHwAddr, sizeof (EFI_GUID));
1177 }
1178
1179 DhcpHeader->HwAddrLen = sizeof (EFI_GUID);
1180 }
1181
1182 Xid = NET_RANDOM (NetRandomInitSeed ());
1183 Token.Packet->Dhcp4.Header.Xid = HTONL(Xid);
1184 Token.Packet->Dhcp4.Header.Reserved = HTONS((IsBCast) ? 0x8000 : 0);
1185 CopyMem (&Token.Packet->Dhcp4.Header.ClientAddr, &Private->StationIp, sizeof (EFI_IPv4_ADDRESS));
1186
1187 Token.RemotePort = Sport;
1188
1189 if (IsBCast) {
1190 SetMem (&Token.RemoteAddress, sizeof (EFI_IPv4_ADDRESS), 0xff);
1191 } else {
1192 CopyMem (&Token.RemoteAddress, DestIp, sizeof (EFI_IPv4_ADDRESS));
1193 }
1194
1195 CopyMem (&Token.GatewayAddress, &Private->GatewayIp, sizeof (EFI_IPv4_ADDRESS));
1196
1197 if (!IsBCast) {
1198 Token.ListenPointCount = 1;
1199 Token.ListenPoints = &ListenPoint;
1200 Token.ListenPoints[0].ListenPort = PXEBC_BS_DISCOVER_PORT;
1201 CopyMem (&Token.ListenPoints[0].ListenAddress, &Private->StationIp, sizeof(EFI_IPv4_ADDRESS));
1202 CopyMem (&Token.ListenPoints[0].SubnetMask, &Private->SubnetMask, sizeof(EFI_IPv4_ADDRESS));
1203 }
1204 //
1205 // Send Pxe Discover
1206 //
1207 for (TryIndex = 1; TryIndex <= PXEBC_BOOT_REQUEST_RETRIES; TryIndex++) {
1208
1209 Token.TimeoutValue = (UINT16) (PXEBC_BOOT_REQUEST_TIMEOUT * TryIndex);
1210 Token.Packet->Dhcp4.Header.Seconds = (UINT16) (PXEBC_BOOT_REQUEST_TIMEOUT * (TryIndex - 1));
1211
1212 Status = Dhcp4->TransmitReceive (Dhcp4, &Token);
1213
1214 if (Token.Status != EFI_TIMEOUT) {
1215 break;
1216 }
1217 }
1218
1219 if (!EFI_ERROR (Status)) {
1220 //
1221 // Find Pxe Reply
1222 //
1223 RepIndex = 0;
1224 SrvIndex = 0;
1225 Response = Token.ResponseList;
1226
1227 while (RepIndex < Token.ResponseCount) {
1228
1229 while (SrvIndex < IpCount) {
1230
1231 if (SrvList[SrvIndex].AcceptAnyResponse) {
1232 break;
1233 }
1234
1235 if ((SrvList[SrvIndex].Type == Type) && EFI_IP4_EQUAL (&(Response->Dhcp4.Header.ServerAddr), &(Private->ServerIp))) {
1236 break;
1237 }
1238
1239 SrvIndex++;
1240 }
1241
1242 if ((IpCount != SrvIndex) || (IpCount == 0)) {
1243 break;
1244 }
1245
1246 SrvIndex = 0;
1247 RepIndex++;
1248
1249 Response = (EFI_DHCP4_PACKET *) ((UINT8 *) Response + Response->Size);
1250 }
1251
1252 if (RepIndex < Token.ResponseCount) {
1253
1254 if (Reply != NULL) {
1255 PxeBcCopyEfiDhcp4Packet (Reply, Response);
1256 }
1257
1258 if (IsDiscv) {
1259 CopyMem (&(Mode->PxeDiscover), &(Token.Packet->Dhcp4), Token.Packet->Length);
1260 Mode->PxeDiscoverValid = TRUE;
1261
1262 CopyMem (Mode->PxeReply.Raw, &Response->Dhcp4, Response->Length);
1263 Mode->PxeReplyReceived = TRUE;
1264 }
1265 } else {
1266 Status = EFI_NOT_FOUND;
1267 }
1268
1269 //
1270 // free the responselist
1271 //
1272 gBS->FreePool (Token.ResponseList);
1273 }
1274 //
1275 // Free the dhcp packet
1276 //
1277 gBS->FreePool (Token.Packet);
1278
1279 return Status;
1280 }
1281
1282
1283 /**
1284 Parse interested dhcp options.
1285
1286 @param Buffer Pointer to the dhcp options packet.
1287 @param Length The length of the dhcp options.
1288 @param OptTag The option OpCode.
1289
1290 @return NULL if the buffer length is 0 and OpCode is not
1291 PXEBC_DHCP4_TAG_EOP, or the pointer to the buffer.
1292
1293 **/
1294 EFI_DHCP4_PACKET_OPTION *
1295 PxeBcParseExtendOptions (
1296 IN UINT8 *Buffer,
1297 IN UINT32 Length,
1298 IN UINT8 OptTag
1299 )
1300 {
1301 EFI_DHCP4_PACKET_OPTION *Option;
1302 UINT32 Offset;
1303
1304 Option = (EFI_DHCP4_PACKET_OPTION *) Buffer;
1305 Offset = 0;
1306
1307 while (Offset < Length && Option->OpCode != PXEBC_DHCP4_TAG_EOP) {
1308
1309 if (Option->OpCode == OptTag) {
1310
1311 return Option;
1312 }
1313
1314 if (Option->OpCode == PXEBC_DHCP4_TAG_PAD) {
1315 Offset++;
1316 } else {
1317 Offset += Option->Length + 2;
1318 }
1319
1320 Option = (EFI_DHCP4_PACKET_OPTION *) (Buffer + Offset);
1321 }
1322
1323 return NULL;
1324 }
1325
1326
1327 /**
1328 This function is to parse and check vendor options.
1329
1330 @param Dhcp4Option Pointer to dhcp options
1331 @param VendorOption Pointer to vendor options
1332
1333 @return TRUE if valid for vendor options, or FALSE.
1334
1335 **/
1336 BOOLEAN
1337 PxeBcParseVendorOptions (
1338 IN EFI_DHCP4_PACKET_OPTION *Dhcp4Option,
1339 IN PXEBC_VENDOR_OPTION *VendorOption
1340 )
1341 {
1342 UINT32 *BitMap;
1343 UINT8 VendorOptionLen;
1344 EFI_DHCP4_PACKET_OPTION *PxeOption;
1345 UINT8 Offset;
1346
1347 BitMap = VendorOption->BitMap;
1348 VendorOptionLen = Dhcp4Option->Length;
1349 PxeOption = (EFI_DHCP4_PACKET_OPTION *) &Dhcp4Option->Data[0];
1350 Offset = 0;
1351
1352 while ((Offset < VendorOptionLen) && (PxeOption->OpCode != PXEBC_DHCP4_TAG_EOP)) {
1353 //
1354 // Parse every Vendor Option and set its BitMap
1355 //
1356 switch (PxeOption->OpCode) {
1357
1358 case PXEBC_VENDOR_TAG_MTFTP_IP:
1359
1360 CopyMem (&VendorOption->MtftpIp, PxeOption->Data, sizeof (EFI_IPv4_ADDRESS));
1361 break;
1362
1363 case PXEBC_VENDOR_TAG_MTFTP_CPORT:
1364
1365 CopyMem (&VendorOption->MtftpCPort, PxeOption->Data, sizeof (VendorOption->MtftpCPort));
1366 break;
1367
1368 case PXEBC_VENDOR_TAG_MTFTP_SPORT:
1369
1370 CopyMem (&VendorOption->MtftpSPort, PxeOption->Data, sizeof (VendorOption->MtftpSPort));
1371 break;
1372
1373 case PXEBC_VENDOR_TAG_MTFTP_TIMEOUT:
1374
1375 VendorOption->MtftpTimeout = *PxeOption->Data;
1376 break;
1377
1378 case PXEBC_VENDOR_TAG_MTFTP_DELAY:
1379
1380 VendorOption->MtftpDelay = *PxeOption->Data;
1381 break;
1382
1383 case PXEBC_VENDOR_TAG_DISCOVER_CTRL:
1384
1385 VendorOption->DiscoverCtrl = *PxeOption->Data;
1386 break;
1387
1388 case PXEBC_VENDOR_TAG_DISCOVER_MCAST:
1389
1390 CopyMem (&VendorOption->DiscoverMcastIp, PxeOption->Data, sizeof (EFI_IPv4_ADDRESS));
1391 break;
1392
1393 case PXEBC_VENDOR_TAG_BOOT_SERVERS:
1394
1395 VendorOption->BootSvrLen = PxeOption->Length;
1396 VendorOption->BootSvr = (PXEBC_BOOT_SVR_ENTRY *) PxeOption->Data;
1397 break;
1398
1399 case PXEBC_VENDOR_TAG_BOOT_MENU:
1400
1401 VendorOption->BootMenuLen = PxeOption->Length;
1402 VendorOption->BootMenu = (PXEBC_BOOT_MENU_ENTRY *) PxeOption->Data;
1403 break;
1404
1405 case PXEBC_VENDOR_TAG_MENU_PROMPT:
1406
1407 VendorOption->MenuPromptLen = PxeOption->Length;
1408 VendorOption->MenuPrompt = (PXEBC_MENU_PROMPT *) PxeOption->Data;
1409 break;
1410
1411 case PXEBC_VENDOR_TAG_MCAST_ALLOC:
1412
1413 CopyMem (&VendorOption->McastIpBase, PxeOption->Data, sizeof (EFI_IPv4_ADDRESS));
1414 CopyMem (&VendorOption->McastIpBlock, PxeOption->Data + 4, sizeof (VendorOption->McastIpBlock));
1415 CopyMem (&VendorOption->McastIpRange, PxeOption->Data + 6, sizeof (VendorOption->McastIpRange));
1416 break;
1417
1418 case PXEBC_VENDOR_TAG_CREDENTIAL_TYPES:
1419
1420 VendorOption->CredTypeLen = PxeOption->Length;
1421 VendorOption->CredType = (UINT32 *) PxeOption->Data;
1422 break;
1423
1424 case PXEBC_VENDOR_TAG_BOOT_ITEM:
1425
1426 CopyMem (&VendorOption->BootSrvType, PxeOption->Data, sizeof (VendorOption->BootSrvType));
1427 CopyMem (&VendorOption->BootSrvLayer, PxeOption->Data + 2, sizeof (VendorOption->BootSrvLayer));
1428 break;
1429 }
1430
1431 SET_VENDOR_OPTION_BIT_MAP (BitMap, PxeOption->OpCode);
1432
1433 if (PxeOption->OpCode == PXEBC_DHCP4_TAG_PAD) {
1434 Offset++;
1435 } else {
1436 Offset = (UINT8) (Offset + PxeOption->Length + 2);
1437 }
1438
1439 PxeOption = (EFI_DHCP4_PACKET_OPTION *) (Dhcp4Option->Data + Offset);
1440 }
1441
1442 //
1443 // FixMe, return falas if invalid of any vendor option
1444 //
1445
1446 return TRUE;
1447 }
1448
1449
1450 /**
1451 This function display boot item detail.
1452
1453 If the length of the boot item string over 70 Char, just display 70 Char.
1454
1455 @param Str Pointer to a string (boot item string).
1456 @param Len The length of string.
1457
1458 **/
1459 VOID
1460 PxeBcDisplayBootItem (
1461 IN UINT8 *Str,
1462 IN UINT8 Len
1463 )
1464 {
1465 UINT8 Tmp;
1466
1467 Len = (UINT8) MIN (70, Len);
1468 Tmp = Str[Len];
1469 Str[Len] = 0;
1470 AsciiPrint ("%a \n", Str);
1471 Str[Len] = Tmp;
1472 }
1473
1474
1475 /**
1476 Choose the boot prompt.
1477
1478 @param Private Pointer to PxeBc private data.
1479
1480 @retval EFI_SUCCESS Select boot prompt done.
1481 @retval EFI_TIMEOUT Select boot prompt time out.
1482 @retval EFI_NOT_FOUND The proxy offer is not Pxe10.
1483 @retval EFI_ABORTED User cancel the operation.
1484 @retval EFI_NOT_READY Read the input key from the keybroad has not finish.
1485
1486 **/
1487 EFI_STATUS
1488 PxeBcSelectBootPrompt (
1489 IN PXEBC_PRIVATE_DATA *Private
1490 )
1491 {
1492 PXEBC_CACHED_DHCP4_PACKET *Packet;
1493 PXEBC_VENDOR_OPTION *VendorOpt;
1494 EFI_EVENT TimeoutEvent;
1495 EFI_EVENT DescendEvent;
1496 EFI_INPUT_KEY InputKey;
1497 EFI_STATUS Status;
1498 UINT8 Timeout;
1499 UINT8 *Prompt;
1500 UINT8 PromptLen;
1501 INT32 SecCol;
1502 INT32 SecRow;
1503
1504 TimeoutEvent = NULL;
1505 DescendEvent = NULL;
1506
1507 if (Private->PxeBc.Mode->ProxyOfferReceived) {
1508
1509 Packet = &Private->ProxyOffer;
1510 } else {
1511
1512 Packet = &Private->Dhcp4Ack;
1513 }
1514
1515 if (Packet->OfferType != DHCP4_PACKET_TYPE_PXE10) {
1516 return EFI_NOT_FOUND;
1517 }
1518
1519 VendorOpt = &Packet->PxeVendorOption;
1520
1521 if (!IS_VALID_BOOT_PROMPT (VendorOpt->BitMap)) {
1522 return EFI_SUCCESS;
1523 }
1524
1525 Timeout = VendorOpt->MenuPrompt->Timeout;
1526 Prompt = VendorOpt->MenuPrompt->Prompt;
1527 PromptLen = (UINT8) (VendorOpt->MenuPromptLen - 1);
1528
1529 if (Timeout == 0) {
1530 return EFI_SUCCESS;
1531 }
1532
1533 if (Timeout == 255) {
1534 return EFI_TIMEOUT;
1535 }
1536
1537 Status = gBS->CreateEvent (
1538 EVT_TIMER,
1539 TPL_CALLBACK,
1540 NULL,
1541 NULL,
1542 &TimeoutEvent
1543 );
1544
1545 if (EFI_ERROR (Status)) {
1546 return Status;
1547 }
1548
1549 Status = gBS->SetTimer (
1550 TimeoutEvent,
1551 TimerRelative,
1552 Timeout * TICKS_PER_SECOND
1553 );
1554
1555 if (EFI_ERROR (Status)) {
1556 goto ON_EXIT;
1557 }
1558
1559 Status = gBS->CreateEvent (
1560 EVT_TIMER,
1561 TPL_CALLBACK,
1562 NULL,
1563 NULL,
1564 &DescendEvent
1565 );
1566
1567 if (EFI_ERROR (Status)) {
1568 goto ON_EXIT;
1569 }
1570
1571 Status = gBS->SetTimer (
1572 DescendEvent,
1573 TimerPeriodic,
1574 TICKS_PER_SECOND
1575 );
1576
1577 if (EFI_ERROR (Status)) {
1578 goto ON_EXIT;
1579 }
1580
1581 SecCol = gST->ConOut->Mode->CursorColumn;
1582 SecRow = gST->ConOut->Mode->CursorRow;
1583
1584 PxeBcDisplayBootItem (Prompt, PromptLen);
1585
1586 gST->ConOut->SetCursorPosition (gST->ConOut, SecCol + PromptLen, SecRow);
1587 AsciiPrint ("(%d) ", Timeout--);
1588
1589 while (EFI_ERROR (gBS->CheckEvent (TimeoutEvent))) {
1590
1591 if (!EFI_ERROR (gBS->CheckEvent (DescendEvent))) {
1592 gST->ConOut->SetCursorPosition (gST->ConOut, SecCol + PromptLen, SecRow);
1593 AsciiPrint ("(%d) ", Timeout--);
1594 }
1595
1596 if (gST->ConIn->ReadKeyStroke (gST->ConIn, &InputKey) == EFI_NOT_READY) {
1597
1598 gBS->Stall (10 * TICKS_PER_MS);
1599 continue;
1600 }
1601
1602 if (InputKey.ScanCode == 0) {
1603
1604 switch (InputKey.UnicodeChar) {
1605 case CTRL ('c'):
1606 Status = EFI_ABORTED;
1607 break;
1608
1609 case CTRL ('m'):
1610 case 'm':
1611 case 'M':
1612 Status = EFI_TIMEOUT;
1613 break;
1614
1615 default:
1616 continue;
1617 }
1618 } else {
1619
1620 switch (InputKey.ScanCode) {
1621 case SCAN_F8:
1622 Status = EFI_TIMEOUT;
1623 break;
1624
1625 case SCAN_ESC:
1626 Status = EFI_ABORTED;
1627 break;
1628
1629 default:
1630 continue;
1631 }
1632 }
1633
1634 break;
1635 }
1636
1637 gST->ConOut->SetCursorPosition (gST->ConOut, 0 , SecRow + 1);
1638
1639 ON_EXIT:
1640
1641 if (DescendEvent != NULL) {
1642 gBS->CloseEvent (DescendEvent);
1643 }
1644
1645 if (TimeoutEvent != NULL) {
1646 gBS->CloseEvent (TimeoutEvent);
1647 }
1648
1649 return Status;
1650 }
1651
1652
1653 /**
1654 Select the boot menu.
1655
1656 @param Private Pointer to PxeBc private data.
1657 @param Type The type of the menu.
1658 @param UseDefaultItem Use default item or not.
1659
1660 @retval EFI_ABORTED User cancel operation.
1661 @retval EFI_SUCCESS Select the boot menu success.
1662 @retval EFI_NOT_READY Read the input key from the keybroad has not finish.
1663
1664 **/
1665 EFI_STATUS
1666 PxeBcSelectBootMenu (
1667 IN PXEBC_PRIVATE_DATA *Private,
1668 OUT UINT16 *Type,
1669 IN BOOLEAN UseDefaultItem
1670 )
1671 {
1672 PXEBC_CACHED_DHCP4_PACKET *Packet;
1673 PXEBC_VENDOR_OPTION *VendorOpt;
1674 EFI_INPUT_KEY InputKey;
1675 UINT8 MenuSize;
1676 UINT8 MenuNum;
1677 INT32 TopRow;
1678 UINT16 Select;
1679 UINT16 LastSelect;
1680 UINT8 Index;
1681 BOOLEAN Finish;
1682 CHAR8 Blank[70];
1683 PXEBC_BOOT_MENU_ENTRY *MenuItem;
1684 PXEBC_BOOT_MENU_ENTRY *MenuArray[PXEBC_MAX_MENU_NUM];
1685
1686 Finish = FALSE;
1687 Select = 1;
1688 Index = 0;
1689 *Type = 0;
1690
1691 if (Private->PxeBc.Mode->ProxyOfferReceived) {
1692
1693 Packet = &Private->ProxyOffer;
1694 } else {
1695
1696 Packet = &Private->Dhcp4Ack;
1697 }
1698
1699 ASSERT (Packet->OfferType == DHCP4_PACKET_TYPE_PXE10);
1700
1701 VendorOpt = &Packet->PxeVendorOption;
1702
1703 if (!IS_VALID_BOOT_MENU (VendorOpt->BitMap)) {
1704 return EFI_SUCCESS;
1705 }
1706
1707 SetMem (Blank, sizeof(Blank), ' ');
1708
1709 MenuSize = VendorOpt->BootMenuLen;
1710 MenuItem = VendorOpt->BootMenu;
1711
1712 if (MenuSize == 0) {
1713 return EFI_NOT_READY;
1714 }
1715
1716 while (MenuSize > 0) {
1717 MenuArray[Index++] = MenuItem;
1718 MenuSize = (UINT8) (MenuSize - (MenuItem->DescLen + 3));
1719 MenuItem = (PXEBC_BOOT_MENU_ENTRY *) ((UINT8 *) MenuItem + MenuItem->DescLen + 3);
1720 if (Index >= PXEBC_MAX_MENU_NUM) {
1721 break;
1722 }
1723 }
1724
1725 if (UseDefaultItem) {
1726 *Type = MenuArray[0]->Type;
1727 *Type = NTOHS (*Type);
1728 return EFI_SUCCESS;
1729 }
1730
1731 MenuNum = Index;
1732
1733 for (Index = 0; Index < MenuNum; Index++) {
1734 PxeBcDisplayBootItem (MenuArray[Index]->DescStr, MenuArray[Index]->DescLen);
1735 }
1736
1737 TopRow = gST->ConOut->Mode->CursorRow - MenuNum;
1738
1739 do {
1740 ASSERT (Select < PXEBC_MAX_MENU_NUM);
1741 //
1742 // highlight selected row
1743 //
1744 gST->ConOut->SetAttribute (gST->ConOut, EFI_TEXT_ATTR (EFI_BLACK, EFI_LIGHTGRAY));
1745 gST->ConOut->SetCursorPosition (gST->ConOut, 0, TopRow + Select);
1746 Blank[MenuArray[Select]->DescLen] = 0;
1747 AsciiPrint ("%a\r", Blank);
1748 PxeBcDisplayBootItem (MenuArray[Select]->DescStr, MenuArray[Select]->DescLen);
1749 gST->ConOut->SetCursorPosition (gST->ConOut, 0, TopRow + MenuNum);
1750 LastSelect = Select;
1751
1752 while (gST->ConIn->ReadKeyStroke (gST->ConIn, &InputKey) == EFI_NOT_READY) {
1753 gBS->Stall (10 * TICKS_PER_MS);
1754 }
1755
1756 if (!InputKey.ScanCode) {
1757 switch (InputKey.UnicodeChar) {
1758 case CTRL ('c'):
1759 InputKey.ScanCode = SCAN_ESC;
1760 break;
1761
1762 case CTRL ('j'): /* linefeed */
1763 case CTRL ('m'): /* return */
1764 Finish = TRUE;
1765 break;
1766
1767 case CTRL ('i'): /* tab */
1768 case ' ':
1769 case 'd':
1770 case 'D':
1771 InputKey.ScanCode = SCAN_DOWN;
1772 break;
1773
1774 case CTRL ('h'): /* backspace */
1775 case 'u':
1776 case 'U':
1777 InputKey.ScanCode = SCAN_UP;
1778 break;
1779
1780 default:
1781 InputKey.ScanCode = 0;
1782 }
1783 }
1784
1785 switch (InputKey.ScanCode) {
1786 case SCAN_LEFT:
1787 case SCAN_UP:
1788 if (Select > 0) {
1789 --Select;
1790 }
1791
1792 break;
1793
1794 case SCAN_DOWN:
1795 case SCAN_RIGHT:
1796 if (++Select == MenuNum) {
1797 --Select;
1798 }
1799
1800 break;
1801
1802 case SCAN_PAGE_UP:
1803 case SCAN_HOME:
1804 Select = 0;
1805 break;
1806
1807 case SCAN_PAGE_DOWN:
1808 case SCAN_END:
1809 Select = (UINT16) (MenuNum - 1);
1810 break;
1811
1812 case SCAN_ESC:
1813 return EFI_ABORTED;
1814 }
1815
1816 /* unhighlight last selected row */
1817 gST->ConOut->SetAttribute (gST->ConOut, EFI_TEXT_ATTR (EFI_LIGHTGRAY, EFI_BLACK));
1818 gST->ConOut->SetCursorPosition (gST->ConOut, 0, TopRow + LastSelect);
1819 Blank[MenuArray[LastSelect]->DescLen] = 0;
1820 AsciiPrint ("%a\r", Blank);
1821 PxeBcDisplayBootItem (MenuArray[LastSelect]->DescStr, MenuArray[LastSelect]->DescLen);
1822 gST->ConOut->SetCursorPosition (gST->ConOut, 0, TopRow + MenuNum);
1823 } while (!Finish);
1824
1825 //
1826 // Swap the byte order
1827 //
1828 CopyMem (Type, &MenuArray[Select]->Type, sizeof (UINT16));
1829 *Type = NTOHS (*Type);
1830
1831 return EFI_SUCCESS;
1832 }
1833