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