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