Use siaddr in DHCP packet, if zero, use option 54 instead.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / UefiPxeBcDxe / PxeBcImpl.c
1 /** @file
2 Interface routines for PxeBc.
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 UINT32 mPxeDhcpTimeout[4] = { 4, 8, 16, 32 };
19
20 /**
21 Get and record the arp cache.
22
23 @param This Pointer to EFI_PXE_BC_PROTOCOL
24
25 @retval EFI_SUCCESS Arp cache updated successfully
26 @retval others If error occurs when getting arp cache
27
28 **/
29 EFI_STATUS
30 UpdateArpCache (
31 IN EFI_PXE_BASE_CODE_PROTOCOL * This
32 )
33 {
34 PXEBC_PRIVATE_DATA *Private;
35 EFI_PXE_BASE_CODE_MODE *Mode;
36 EFI_STATUS Status;
37 UINT32 EntryLength;
38 UINT32 EntryCount;
39 EFI_ARP_FIND_DATA *Entries;
40 UINT32 Index;
41
42 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
43 Mode = Private->PxeBc.Mode;
44
45 Status = Private->Arp->Find (
46 Private->Arp,
47 TRUE,
48 NULL,
49 &EntryLength,
50 &EntryCount,
51 &Entries,
52 TRUE
53 );
54 if (EFI_ERROR (Status)) {
55 return Status;
56 }
57
58 Mode->ArpCacheEntries = MIN (
59 EntryCount,
60 EFI_PXE_BASE_CODE_MAX_ARP_ENTRIES
61 );
62 for (Index = 0; Index < Mode->ArpCacheEntries; Index ++) {
63 CopyMem (
64 &Mode->ArpCache[Index].IpAddr,
65 Entries + 1,
66 Entries->SwAddressLength
67 );
68 CopyMem (
69 &Mode->ArpCache[Index].MacAddr,
70 (UINT8 *) (Entries + 1) + Entries->SwAddressLength,
71 Entries->HwAddressLength
72 );
73 //
74 // Slip to the next FindData.
75 //
76 Entries = (EFI_ARP_FIND_DATA *) ((UINT8 *) Entries + EntryLength);
77 }
78
79 return EFI_SUCCESS;
80 }
81
82 /**
83 Timeout routine to update arp cache.
84
85 @param Event Pointer to EFI_PXE_BC_PROTOCOL
86 @param Context Context of the timer event
87
88 **/
89 VOID
90 EFIAPI
91 ArpCacheUpdateTimeout (
92 IN EFI_EVENT Event,
93 IN VOID *Context
94 )
95 {
96 UpdateArpCache ((EFI_PXE_BASE_CODE_PROTOCOL *) Context);
97 }
98
99 /**
100 Do arp resolution from arp cache in PxeBcMode.
101
102 @param PxeBcMode The PXE BC mode to look into.
103 @param Ip4Addr The Ip4 address for resolution.
104 @param MacAddress The resoluted MAC address if the resolution is successful.
105 The value is undefined if resolution fails.
106
107 @retval TRUE The resolution is successful.
108 @retval FALSE Otherwise.
109
110 **/
111 BOOLEAN
112 FindInArpCache (
113 IN EFI_PXE_BASE_CODE_MODE *PxeBcMode,
114 IN EFI_IPv4_ADDRESS *Ip4Addr,
115 OUT EFI_MAC_ADDRESS *MacAddress
116 )
117 {
118 UINT32 Index;
119
120 for (Index = 0; Index < PxeBcMode->ArpCacheEntries; Index ++) {
121 if (EFI_IP4_EQUAL (&PxeBcMode->ArpCache[Index].IpAddr.v4, Ip4Addr)) {
122 CopyMem (
123 MacAddress,
124 &PxeBcMode->ArpCache[Index].MacAddr,
125 sizeof (EFI_MAC_ADDRESS)
126 );
127 return TRUE;
128 }
129 }
130
131 return FALSE;
132 }
133
134 /**
135 Notify function for the ICMP receive token, used to process
136 the received ICMP packets.
137
138 @param Context The PXEBC private data.
139
140 **/
141 VOID
142 EFIAPI
143 IcmpErrorListenHandlerDpc (
144 IN VOID *Context
145 )
146 {
147 EFI_STATUS Status;
148 EFI_IP4_RECEIVE_DATA *RxData;
149 EFI_IP4_PROTOCOL *Ip4;
150 PXEBC_PRIVATE_DATA *Private;
151 EFI_PXE_BASE_CODE_MODE *Mode;
152 UINTN Index;
153 UINT32 CopiedLen;
154 UINT8 *CopiedPointer;
155
156 Private = (PXEBC_PRIVATE_DATA *) Context;
157 Mode = &Private->Mode;
158 Status = Private->IcmpErrorRcvToken.Status;
159 RxData = Private->IcmpErrorRcvToken.Packet.RxData;
160 Ip4 = Private->Ip4;
161
162 if (Status == EFI_ABORTED) {
163 //
164 // The reception is actively aborted by the consumer, directly return.
165 //
166 return;
167 }
168
169 if (EFI_ERROR (Status) || (RxData == NULL)) {
170 //
171 // Only process the normal packets and the icmp error packets, if RxData is NULL
172 // with Status == EFI_SUCCESS or EFI_ICMP_ERROR, just resume the receive although
173 // this should be a bug of the low layer (IP).
174 //
175 goto Resume;
176 }
177
178 if (EFI_IP4 (RxData->Header->SourceAddress) != 0 &&
179 !Ip4IsUnicast (EFI_NTOHL (RxData->Header->SourceAddress), 0)) {
180 //
181 // The source address is not zero and it's not a unicast IP address, discard it.
182 //
183 goto CleanUp;
184 }
185
186 if (!EFI_IP4_EQUAL (&RxData->Header->DestinationAddress, &Mode->StationIp.v4)) {
187 //
188 // The dest address is not equal to Station Ip address, discard it.
189 //
190 goto CleanUp;
191 }
192
193 //
194 // Constructor ICMP error packet
195 //
196 CopiedLen = 0;
197 CopiedPointer = (UINT8 *) &Mode->IcmpError;
198
199 for (Index = 0; Index < RxData->FragmentCount; Index ++) {
200 CopiedLen += RxData->FragmentTable[Index].FragmentLength;
201 if (CopiedLen <= sizeof (EFI_PXE_BASE_CODE_ICMP_ERROR)) {
202 CopyMem (
203 CopiedPointer,
204 RxData->FragmentTable[Index].FragmentBuffer,
205 RxData->FragmentTable[Index].FragmentLength
206 );
207 } else {
208 CopyMem (
209 CopiedPointer,
210 RxData->FragmentTable[Index].FragmentBuffer,
211 CopiedLen - sizeof (EFI_PXE_BASE_CODE_ICMP_ERROR)
212 );
213 }
214 CopiedPointer += CopiedLen;
215 }
216
217 goto Resume;
218
219 CleanUp:
220 gBS->SignalEvent (RxData->RecycleSignal);
221
222 Resume:
223 Ip4->Receive (Ip4, &(Private->IcmpErrorRcvToken));
224 }
225
226 /**
227 Request IcmpErrorListenHandlerDpc as a DPC at TPL_CALLBACK
228
229 @param Event The event signaled.
230 @param Context The context passed in by the event notifier.
231
232 **/
233 VOID
234 EFIAPI
235 IcmpErrorListenHandler (
236 IN EFI_EVENT Event,
237 IN VOID *Context
238 )
239 {
240 //
241 // Request IpIoListenHandlerDpc as a DPC at TPL_CALLBACK
242 //
243 QueueDpc (TPL_CALLBACK, IcmpErrorListenHandlerDpc, Context);
244 }
245
246 /**
247 Enables the use of the PXE Base Code Protocol functions.
248
249 This function enables the use of the PXE Base Code Protocol functions. If the
250 Started field of the EFI_PXE_BASE_CODE_MODE structure is already TRUE, then
251 EFI_ALREADY_STARTED will be returned. If UseIpv6 is TRUE, then IPv6 formatted
252 addresses will be used in this session. If UseIpv6 is FALSE, then IPv4 formatted
253 addresses will be used in this session. If UseIpv6 is TRUE, and the Ipv6Supported
254 field of the EFI_PXE_BASE_CODE_MODE structure is FALSE, then EFI_UNSUPPORTED will
255 be returned. If there is not enough memory or other resources to start the PXE
256 Base Code Protocol, then EFI_OUT_OF_RESOURCES will be returned. Otherwise, the
257 PXE Base Code Protocol will be started, and all of the fields of the EFI_PXE_BASE_CODE_MODE
258 structure will be initialized as follows:
259 StartedSet to TRUE.
260 Ipv6SupportedUnchanged.
261 Ipv6AvailableUnchanged.
262 UsingIpv6Set to UseIpv6.
263 BisSupportedUnchanged.
264 BisDetectedUnchanged.
265 AutoArpSet to TRUE.
266 SendGUIDSet to FALSE.
267 TTLSet to DEFAULT_TTL.
268 ToSSet to DEFAULT_ToS.
269 DhcpCompletedSet to FALSE.
270 ProxyOfferReceivedSet to FALSE.
271 StationIpSet to an address of all zeros.
272 SubnetMaskSet to a subnet mask of all zeros.
273 DhcpDiscoverZero-filled.
274 DhcpAckZero-filled.
275 ProxyOfferZero-filled.
276 PxeDiscoverValidSet to FALSE.
277 PxeDiscoverZero-filled.
278 PxeReplyValidSet to FALSE.
279 PxeReplyZero-filled.
280 PxeBisReplyValidSet to FALSE.
281 PxeBisReplyZero-filled.
282 IpFilterSet the Filters field to 0 and the IpCnt field to 0.
283 ArpCacheEntriesSet to 0.
284 ArpCacheZero-filled.
285 RouteTableEntriesSet to 0.
286 RouteTableZero-filled.
287 IcmpErrorReceivedSet to FALSE.
288 IcmpErrorZero-filled.
289 TftpErroReceivedSet to FALSE.
290 TftpErrorZero-filled.
291 MakeCallbacksSet to TRUE if the PXE Base Code Callback Protocol is available.
292 Set to FALSE if the PXE Base Code Callback Protocol is not available.
293
294 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
295 @param UseIpv6 Specifies the type of IP addresses that are to be used during the session
296 that is being started. Set to TRUE for IPv6 addresses, and FALSE for
297 IPv4 addresses.
298
299 @retval EFI_SUCCESS The PXE Base Code Protocol was started.
300 @retval EFI_DEVICE_ERROR The network device encountered an error during this oper
301 @retval EFI_UNSUPPORTED UseIpv6 is TRUE, but the Ipv6Supported field of the
302 EFI_PXE_BASE_CODE_MODE structure is FALSE.
303 @retval EFI_ALREADY_STARTED The PXE Base Code Protocol is already in the started state.
304 @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid
305 EFI_PXE_BASE_CODE_PROTOCOL structure.
306 @retval EFI_OUT_OF_RESOURCES Could not allocate enough memory or other resources to start the
307 PXE Base Code Protocol.
308
309 **/
310 EFI_STATUS
311 EFIAPI
312 EfiPxeBcStart (
313 IN EFI_PXE_BASE_CODE_PROTOCOL *This,
314 IN BOOLEAN UseIpv6
315 )
316 {
317 PXEBC_PRIVATE_DATA *Private;
318 EFI_PXE_BASE_CODE_MODE *Mode;
319 EFI_STATUS Status;
320
321 if (This == NULL) {
322 return EFI_INVALID_PARAMETER;
323 }
324
325 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
326 Mode = Private->PxeBc.Mode;
327
328 if (Mode->Started) {
329 return EFI_ALREADY_STARTED;
330 }
331
332 if (UseIpv6) {
333 //
334 // IPv6 is not supported now.
335 //
336 return EFI_UNSUPPORTED;
337 }
338
339 //
340 // Configure the udp4 instance to let it receive data
341 //
342 Status = Private->Udp4Read->Configure (
343 Private->Udp4Read,
344 &Private->Udp4CfgData
345 );
346 if (EFI_ERROR (Status)) {
347 return Status;
348 }
349
350 Private->AddressIsOk = FALSE;
351
352 ZeroMem (Mode, sizeof (EFI_PXE_BASE_CODE_MODE));
353
354 Mode->Started = TRUE;
355 Mode->TTL = DEFAULT_TTL;
356 Mode->ToS = DEFAULT_ToS;
357 Mode->AutoArp = TRUE;
358
359 //
360 // Create the event for Arp Cache checking.
361 //
362 Status = gBS->CreateEvent (
363 EVT_TIMER | EVT_NOTIFY_SIGNAL,
364 TPL_CALLBACK,
365 ArpCacheUpdateTimeout,
366 This,
367 &Private->GetArpCacheEvent
368 );
369 if (EFI_ERROR (Status)) {
370 goto ON_EXIT;
371 }
372
373 //
374 // Start the timeout timer event.
375 //
376 Status = gBS->SetTimer (
377 Private->GetArpCacheEvent,
378 TimerPeriodic,
379 TICKS_PER_SECOND
380 );
381
382 if (EFI_ERROR (Status)) {
383 goto ON_EXIT;
384 }
385
386 //
387 // Create ICMP error receiving event
388 //
389 Status = gBS->CreateEvent (
390 EVT_NOTIFY_SIGNAL,
391 TPL_NOTIFY,
392 IcmpErrorListenHandler,
393 Private,
394 &(Private->IcmpErrorRcvToken.Event)
395 );
396 if (EFI_ERROR (Status)) {
397 goto ON_EXIT;
398 }
399
400 Status = Private->Ip4->Configure (Private->Ip4, &Private->Ip4ConfigData);
401 if (EFI_ERROR (Status)) {
402 goto ON_EXIT;
403 }
404
405 //
406 // start to listen incoming packet
407 //
408 Status = Private->Ip4->Receive (Private->Ip4, &Private->IcmpErrorRcvToken);
409 if (!EFI_ERROR (Status)) {
410 return Status;
411 }
412
413 ON_EXIT:
414 Private->Ip4->Configure (Private->Ip4, NULL);
415
416 if (Private->IcmpErrorRcvToken.Event != NULL) {
417 gBS->CloseEvent (Private->IcmpErrorRcvToken.Event);
418 }
419
420 if (Private->GetArpCacheEvent != NULL) {
421 gBS->SetTimer (Private->GetArpCacheEvent, TimerCancel, 0);
422 gBS->CloseEvent (Private->GetArpCacheEvent);
423 }
424
425 Mode->Started = FALSE;
426 Mode->TTL = 0;
427 Mode->ToS = 0;
428 Mode->AutoArp = FALSE;
429
430 return Status;
431 }
432
433
434 /**
435 Disables the use of the PXE Base Code Protocol functions.
436
437 This function stops all activity on the network device. All the resources allocated
438 in Start() are released, the Started field of the EFI_PXE_BASE_CODE_MODE structure is
439 set to FALSE and EFI_SUCCESS is returned. If the Started field of the EFI_PXE_BASE_CODE_MODE
440 structure is already FALSE, then EFI_NOT_STARTED will be returned.
441
442 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
443
444 @retval EFI_SUCCESS The PXE Base Code Protocol was stopped.
445 @retval EFI_NOT_STARTED The PXE Base Code Protocol is already in the stopped state.
446 @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid
447 EFI_PXE_BASE_CODE_PROTOCOL structure.
448 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
449
450 **/
451 EFI_STATUS
452 EFIAPI
453 EfiPxeBcStop (
454 IN EFI_PXE_BASE_CODE_PROTOCOL *This
455 )
456 {
457 PXEBC_PRIVATE_DATA *Private;
458 EFI_PXE_BASE_CODE_MODE *Mode;
459
460 if (This == NULL) {
461 return EFI_INVALID_PARAMETER;
462 }
463
464 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
465 Mode = Private->PxeBc.Mode;
466
467 if (!Mode->Started) {
468 return EFI_NOT_STARTED;
469 }
470
471 Private->Ip4->Cancel (Private->Ip4, NULL);
472 //
473 // Dispatch the DPCs queued by the NotifyFunction of the canceled rx token's
474 // events.
475 //
476 DispatchDpc ();
477
478 Private->Ip4->Configure (Private->Ip4, NULL);
479
480 //
481 // Close the ICMP error receiving event.
482 //
483 gBS->CloseEvent (Private->IcmpErrorRcvToken.Event);
484
485 //
486 // Cancel the TimeoutEvent timer.
487 //
488 gBS->SetTimer (Private->GetArpCacheEvent, TimerCancel, 0);
489
490 //
491 // Close the TimeoutEvent event.
492 //
493 gBS->CloseEvent (Private->GetArpCacheEvent);
494
495 Mode->Started = FALSE;
496
497 Private->CurrentUdpSrcPort = 0;
498 Private->Udp4Write->Configure (Private->Udp4Write, NULL);
499 Private->Udp4Read->Groups (Private->Udp4Read, FALSE, NULL);
500 Private->Udp4Read->Configure (Private->Udp4Read, NULL);
501
502 Private->Dhcp4->Stop (Private->Dhcp4);
503 Private->Dhcp4->Configure (Private->Dhcp4, NULL);
504
505 Private->FileSize = 0;
506
507 return EFI_SUCCESS;
508 }
509
510
511 /**
512 Attempts to complete a DHCPv4 D.O.R.A. (discover / offer / request / acknowledge) or DHCPv6
513 S.A.R.R (solicit / advertise / request / reply) sequence.
514
515 This function attempts to complete the DHCP sequence. If this sequence is completed,
516 then EFI_SUCCESS is returned, and the DhcpCompleted, ProxyOfferReceived, StationIp,
517 SubnetMask, DhcpDiscover, DhcpAck, and ProxyOffer fields of the EFI_PXE_BASE_CODE_MODE
518 structure are filled in.
519 If SortOffers is TRUE, then the cached DHCP offer packets will be sorted before
520 they are tried. If SortOffers is FALSE, then the cached DHCP offer packets will
521 be tried in the order in which they are received. Please see the Preboot Execution
522 Environment (PXE) Specification for additional details on the implementation of DHCP.
523 This function can take at least 31 seconds to timeout and return control to the
524 caller. If the DHCP sequence does not complete, then EFI_TIMEOUT will be returned.
525 If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,
526 then the DHCP sequence will be stopped and EFI_ABORTED will be returned.
527
528 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
529 @param SortOffers TRUE if the offers received should be sorted. Set to FALSE to try the
530 offers in the order that they are received.
531
532 @retval EFI_SUCCESS Valid DHCP has completed.
533 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
534 @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid
535 EFI_PXE_BASE_CODE_PROTOCOL structure.
536 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
537 @retval EFI_OUT_OF_RESOURCES Could not allocate enough memory to complete the DHCP Protocol.
538 @retval EFI_ABORTED The callback function aborted the DHCP Protocol.
539 @retval EFI_TIMEOUT The DHCP Protocol timed out.
540 @retval EFI_ICMP_ERROR An ICMP error packet was received during the DHCP session.
541 @retval EFI_NO_RESPONSE Valid PXE offer was not received.
542
543 **/
544 EFI_STATUS
545 EFIAPI
546 EfiPxeBcDhcp (
547 IN EFI_PXE_BASE_CODE_PROTOCOL *This,
548 IN BOOLEAN SortOffers
549 )
550 {
551 PXEBC_PRIVATE_DATA *Private;
552 EFI_PXE_BASE_CODE_MODE *Mode;
553 EFI_DHCP4_PROTOCOL *Dhcp4;
554 EFI_DHCP4_CONFIG_DATA Dhcp4CfgData;
555 EFI_DHCP4_MODE_DATA Dhcp4Mode;
556 EFI_DHCP4_PACKET_OPTION *OptList[PXEBC_DHCP4_MAX_OPTION_NUM];
557 UINT32 OptCount;
558 EFI_STATUS Status;
559 EFI_ARP_CONFIG_DATA ArpConfigData;
560
561 if (This == NULL) {
562 return EFI_INVALID_PARAMETER;
563 }
564
565 Status = EFI_SUCCESS;
566 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
567 Mode = Private->PxeBc.Mode;
568 Dhcp4 = Private->Dhcp4;
569 Private->Function = EFI_PXE_BASE_CODE_FUNCTION_DHCP;
570 Private->SortOffers = SortOffers;
571
572 if (!Mode->Started) {
573 return EFI_NOT_STARTED;
574 }
575
576 Mode->IcmpErrorReceived = FALSE;
577
578 //
579 // Initialize the DHCP options and build the option list
580 //
581 OptCount = PxeBcBuildDhcpOptions (Private, OptList, TRUE);
582
583 //
584 // Set the DHCP4 config data.
585 // The four discovery timeouts are 4, 8, 16, 32 seconds respectively.
586 //
587 ZeroMem (&Dhcp4CfgData, sizeof (EFI_DHCP4_CONFIG_DATA));
588 Dhcp4CfgData.OptionCount = OptCount;
589 Dhcp4CfgData.OptionList = OptList;
590 Dhcp4CfgData.Dhcp4Callback = PxeBcDhcpCallBack;
591 Dhcp4CfgData.CallbackContext = Private;
592 Dhcp4CfgData.DiscoverTryCount = 4;
593 Dhcp4CfgData.DiscoverTimeout = mPxeDhcpTimeout;
594
595 Status = Dhcp4->Configure (Dhcp4, &Dhcp4CfgData);
596 if (EFI_ERROR (Status)) {
597 goto ON_EXIT;
598 }
599
600 //
601 // Zero those arrays to record the varies numbers of DHCP OFFERS.
602 //
603 Private->GotProxyOffer = FALSE;
604 Private->NumOffers = 0;
605 Private->BootpIndex = 0;
606 ZeroMem (Private->ServerCount, sizeof (Private->ServerCount));
607 ZeroMem (Private->ProxyIndex, sizeof (Private->ProxyIndex));
608
609 Status = Dhcp4->Start (Dhcp4, NULL);
610 if (EFI_ERROR (Status)) {
611 if (Status == EFI_ICMP_ERROR) {
612 Mode->IcmpErrorReceived = TRUE;
613 }
614 goto ON_EXIT;
615 }
616
617 Status = Dhcp4->GetModeData (Dhcp4, &Dhcp4Mode);
618 if (EFI_ERROR (Status)) {
619 goto ON_EXIT;
620 }
621
622 ASSERT (Dhcp4Mode.State == Dhcp4Bound);
623
624 CopyMem (&Private->StationIp, &Dhcp4Mode.ClientAddress, sizeof (EFI_IPv4_ADDRESS));
625 CopyMem (&Private->SubnetMask, &Dhcp4Mode.SubnetMask, sizeof (EFI_IPv4_ADDRESS));
626 CopyMem (&Private->GatewayIp, &Dhcp4Mode.RouterAddress, sizeof (EFI_IPv4_ADDRESS));
627
628 CopyMem (&Mode->StationIp, &Private->StationIp, sizeof (EFI_IPv4_ADDRESS));
629 CopyMem (&Mode->SubnetMask, &Private->SubnetMask, sizeof (EFI_IPv4_ADDRESS));
630
631 //
632 // Check the selected offer to see whether BINL is required, if no or BINL is
633 // finished, set the various Mode members.
634 //
635 Status = PxeBcCheckSelectedOffer (Private);
636 if (!EFI_ERROR (Status)) {
637 goto ON_EXIT;
638 }
639
640 ON_EXIT:
641 if (EFI_ERROR (Status)) {
642 Dhcp4->Stop (Dhcp4);
643 Dhcp4->Configure (Dhcp4, NULL);
644 } else {
645 //
646 // Remove the previously configured option list and callback function
647 //
648 ZeroMem (&Dhcp4CfgData, sizeof (EFI_DHCP4_CONFIG_DATA));
649 Dhcp4->Configure (Dhcp4, &Dhcp4CfgData);
650
651 Private->AddressIsOk = TRUE;
652
653 if (!Mode->UsingIpv6) {
654 //
655 // If in IPv4 mode, configure the corresponding ARP with this new
656 // station IP address.
657 //
658 ZeroMem (&ArpConfigData, sizeof (EFI_ARP_CONFIG_DATA));
659
660 ArpConfigData.SwAddressType = 0x0800;
661 ArpConfigData.SwAddressLength = sizeof (EFI_IPv4_ADDRESS);
662 ArpConfigData.StationAddress = &Private->StationIp.v4;
663
664 Private->Arp->Configure (Private->Arp, NULL);
665 Private->Arp->Configure (Private->Arp, &ArpConfigData);
666
667 //
668 // Updated the route table. Fill the first entry.
669 //
670 Mode->RouteTableEntries = 1;
671 Mode->RouteTable[0].IpAddr.Addr[0] = Private->StationIp.Addr[0] & Private->SubnetMask.Addr[0];
672 Mode->RouteTable[0].SubnetMask.Addr[0] = Private->SubnetMask.Addr[0];
673 Mode->RouteTable[0].GwAddr.Addr[0] = 0;
674
675 //
676 // Create the default route entry if there is a default router.
677 //
678 if (Private->GatewayIp.Addr[0] != 0) {
679 Mode->RouteTableEntries = 2;
680 Mode->RouteTable[1].IpAddr.Addr[0] = 0;
681 Mode->RouteTable[1].SubnetMask.Addr[0] = 0;
682 Mode->RouteTable[1].GwAddr.Addr[0] = Private->GatewayIp.Addr[0];
683 }
684 }
685 }
686
687 return Status;
688 }
689
690
691 /**
692 Attempts to complete the PXE Boot Server and/or boot image discovery sequence.
693
694 This function attempts to complete the PXE Boot Server and/or boot image discovery
695 sequence. If this sequence is completed, then EFI_SUCCESS is returned, and the
696 PxeDiscoverValid, PxeDiscover, PxeReplyReceived, and PxeReply fields of the
697 EFI_PXE_BASE_CODE_MODE structure are filled in. If UseBis is TRUE, then the
698 PxeBisReplyReceived and PxeBisReply fields of the EFI_PXE_BASE_CODE_MODE structure
699 will also be filled in. If UseBis is FALSE, then PxeBisReplyValid will be set to FALSE.
700 In the structure referenced by parameter Info, the PXE Boot Server list, SrvList[],
701 has two uses: It is the Boot Server IP address list used for unicast discovery
702 (if the UseUCast field is TRUE), and it is the list used for Boot Server verification
703 (if the MustUseList field is TRUE). Also, if the MustUseList field in that structure
704 is TRUE and the AcceptAnyResponse field in the SrvList[] array is TRUE, any Boot
705 Server reply of that type will be accepted. If the AcceptAnyResponse field is
706 FALSE, only responses from Boot Servers with matching IP addresses will be accepted.
707 This function can take at least 10 seconds to timeout and return control to the
708 caller. If the Discovery sequence does not complete, then EFI_TIMEOUT will be
709 returned. Please see the Preboot Execution Environment (PXE) Specification for
710 additional details on the implementation of the Discovery sequence.
711 If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,
712 then the Discovery sequence is stopped and EFI_ABORTED will be returned.
713
714 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
715 @param Type The type of bootstrap to perform.
716 @param Layer Pointer to the boot server layer number to discover, which must be
717 PXE_BOOT_LAYER_INITIAL when a new server type is being
718 discovered.
719 @param UseBis TRUE if Boot Integrity Services are to be used. FALSE otherwise.
720 @param Info Pointer to a data structure that contains additional information on the
721 type of discovery operation that is to be performed.
722
723 @retval EFI_SUCCESS The Discovery sequence has been completed.
724 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
725 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
726 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
727 @retval EFI_OUT_OF_RESOURCES Could not allocate enough memory to complete Discovery.
728 @retval EFI_ABORTED The callback function aborted the Discovery sequence.
729 @retval EFI_TIMEOUT The Discovery sequence timed out.
730 @retval EFI_ICMP_ERROR An ICMP error packet was received during the PXE discovery
731 session.
732
733 **/
734 EFI_STATUS
735 EFIAPI
736 EfiPxeBcDiscover (
737 IN EFI_PXE_BASE_CODE_PROTOCOL *This,
738 IN UINT16 Type,
739 IN UINT16 *Layer,
740 IN BOOLEAN UseBis,
741 IN EFI_PXE_BASE_CODE_DISCOVER_INFO *Info OPTIONAL
742 )
743 {
744 PXEBC_PRIVATE_DATA *Private;
745 EFI_PXE_BASE_CODE_MODE *Mode;
746 EFI_PXE_BASE_CODE_DISCOVER_INFO DefaultInfo;
747 EFI_PXE_BASE_CODE_SRVLIST *SrvList;
748 EFI_PXE_BASE_CODE_SRVLIST DefaultSrvList;
749 PXEBC_CACHED_DHCP4_PACKET *Packet;
750 PXEBC_VENDOR_OPTION *VendorOpt;
751 UINT16 Index;
752 EFI_STATUS Status;
753 PXEBC_BOOT_SVR_ENTRY *BootSvrEntry;
754
755 if (This == NULL) {
756 return EFI_INVALID_PARAMETER;
757 }
758
759 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
760 Mode = Private->PxeBc.Mode;
761 BootSvrEntry = NULL;
762 SrvList = NULL;
763 Status = EFI_DEVICE_ERROR;
764 Private->Function = EFI_PXE_BASE_CODE_FUNCTION_DISCOVER;
765
766 if (!Private->AddressIsOk) {
767 return EFI_INVALID_PARAMETER;
768 }
769
770 if (!Mode->Started) {
771 return EFI_NOT_STARTED;
772 }
773
774 Mode->IcmpErrorReceived = FALSE;
775
776 //
777 // If layer isn't EFI_PXE_BASE_CODE_BOOT_LAYER_INITIAL,
778 // use the previous setting;
779 // If info isn't offered,
780 // use the cached DhcpAck and ProxyOffer packets.
781 //
782 if (*Layer != EFI_PXE_BASE_CODE_BOOT_LAYER_INITIAL) {
783
784 if (!Mode->PxeDiscoverValid || !Mode->PxeReplyReceived || (!Mode->PxeBisReplyReceived && UseBis)) {
785
786 return EFI_INVALID_PARAMETER;
787 }
788
789 DefaultInfo.IpCnt = 1;
790 DefaultInfo.UseUCast = TRUE;
791
792 DefaultSrvList.Type = Type;
793 DefaultSrvList.AcceptAnyResponse = FALSE;
794 DefaultSrvList.IpAddr.Addr[0] = Private->ServerIp.Addr[0];
795
796 SrvList = &DefaultSrvList;
797 Info = &DefaultInfo;
798 } else if (Info == NULL) {
799 //
800 // Create info by the cached packet before
801 //
802 Packet = (Mode->ProxyOfferReceived) ? &Private->ProxyOffer : &Private->Dhcp4Ack;
803 VendorOpt = &Packet->PxeVendorOption;
804
805 if (!Mode->DhcpAckReceived || !IS_VALID_DISCOVER_VENDOR_OPTION (VendorOpt->BitMap)) {
806 //
807 // Address is not acquired or no discovery options.
808 //
809 return EFI_INVALID_PARAMETER;
810 }
811
812 DefaultInfo.UseMCast = (BOOLEAN)!IS_DISABLE_MCAST_DISCOVER (VendorOpt->DiscoverCtrl);
813 DefaultInfo.UseBCast = (BOOLEAN)!IS_DISABLE_BCAST_DISCOVER (VendorOpt->DiscoverCtrl);
814 DefaultInfo.MustUseList = (BOOLEAN) IS_ENABLE_USE_SERVER_LIST (VendorOpt->DiscoverCtrl);
815 DefaultInfo.UseUCast = DefaultInfo.MustUseList;
816
817 if (DefaultInfo.UseMCast) {
818 //
819 // Get the multicast discover ip address from vendor option.
820 //
821 CopyMem (
822 &DefaultInfo.ServerMCastIp.Addr,
823 &VendorOpt->DiscoverMcastIp,
824 sizeof (EFI_IPv4_ADDRESS)
825 );
826 }
827
828 DefaultInfo.IpCnt = 0;
829
830 if (DefaultInfo.MustUseList) {
831 BootSvrEntry = VendorOpt->BootSvr;
832 Status = EFI_INVALID_PARAMETER;
833
834 while (((UINT8) (BootSvrEntry - VendorOpt->BootSvr)) < VendorOpt->BootSvrLen) {
835
836 if (BootSvrEntry->Type == HTONS (Type)) {
837 Status = EFI_SUCCESS;
838 break;
839 }
840
841 BootSvrEntry = GET_NEXT_BOOT_SVR_ENTRY (BootSvrEntry);
842 }
843
844 if (EFI_ERROR (Status)) {
845 return Status;
846 }
847
848 DefaultInfo.IpCnt = BootSvrEntry->IpCnt;
849 }
850
851 Info = &DefaultInfo;
852 } else {
853
854 SrvList = Info->SrvList;
855
856 if (!SrvList[0].AcceptAnyResponse) {
857
858 for (Index = 1; Index < Info->IpCnt; Index++) {
859 if (SrvList[Index].AcceptAnyResponse) {
860 break;
861 }
862 }
863
864 if (Index != Info->IpCnt) {
865 return EFI_INVALID_PARAMETER;
866 }
867 }
868 }
869
870 if ((!Info->UseUCast && !Info->UseBCast && !Info->UseMCast) || (Info->MustUseList && Info->IpCnt == 0)) {
871
872 return EFI_INVALID_PARAMETER;
873 }
874 //
875 // Execute discover by UniCast/BroadCast/MultiCast
876 //
877 if (Info->UseUCast) {
878
879 for (Index = 0; Index < Info->IpCnt; Index++) {
880
881 if (BootSvrEntry == NULL) {
882 Private->ServerIp.Addr[0] = SrvList[Index].IpAddr.Addr[0];
883 } else {
884 CopyMem (
885 &Private->ServerIp,
886 &BootSvrEntry->IpAddr[Index],
887 sizeof (EFI_IPv4_ADDRESS)
888 );
889 }
890
891 Status = PxeBcDiscvBootService (
892 Private,
893 Type,
894 Layer,
895 UseBis,
896 &SrvList[Index].IpAddr,
897 0,
898 NULL,
899 TRUE,
900 &Private->PxeReply.Packet.Ack
901 );
902 }
903
904 } else if (Info->UseMCast) {
905
906 Status = PxeBcDiscvBootService (
907 Private,
908 Type,
909 Layer,
910 UseBis,
911 &Info->ServerMCastIp,
912 0,
913 NULL,
914 TRUE,
915 &Private->PxeReply.Packet.Ack
916 );
917
918 } else if (Info->UseBCast) {
919
920 Status = PxeBcDiscvBootService (
921 Private,
922 Type,
923 Layer,
924 UseBis,
925 NULL,
926 Info->IpCnt,
927 SrvList,
928 TRUE,
929 &Private->PxeReply.Packet.Ack
930 );
931 }
932
933 if (EFI_ERROR (Status) || !Mode->PxeReplyReceived || (!Mode->PxeBisReplyReceived && UseBis)) {
934 if (Status == EFI_ICMP_ERROR) {
935 Mode->IcmpErrorReceived = TRUE;
936 } else {
937 Status = EFI_DEVICE_ERROR;
938 }
939 } else {
940 PxeBcParseCachedDhcpPacket (&Private->PxeReply);
941 }
942
943 if (Mode->PxeBisReplyReceived) {
944 CopyMem (
945 &Private->ServerIp,
946 &Mode->PxeReply.Dhcpv4.BootpSiAddr,
947 sizeof (EFI_IPv4_ADDRESS)
948 );
949 }
950
951 return Status;
952 }
953
954
955 /**
956 Used to perform TFTP and MTFTP services.
957
958 This function is used to perform TFTP and MTFTP services. This includes the
959 TFTP operations to get the size of a file, read a directory, read a file, and
960 write a file. It also includes the MTFTP operations to get the size of a file,
961 read a directory, and read a file. The type of operation is specified by Operation.
962 If the callback function that is invoked during the TFTP/MTFTP operation does
963 not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE, then EFI_ABORTED will
964 be returned.
965 For read operations, the return data will be placed in the buffer specified by
966 BufferPtr. If BufferSize is too small to contain the entire downloaded file,
967 then EFI_BUFFER_TOO_SMALL will be returned and BufferSize will be set to zero
968 or the size of the requested file (the size of the requested file is only returned
969 if the TFTP server supports TFTP options). If BufferSize is large enough for the
970 read operation, then BufferSize will be set to the size of the downloaded file,
971 and EFI_SUCCESS will be returned. Applications using the PxeBc.Mtftp() services
972 should use the get-file-size operations to determine the size of the downloaded
973 file prior to using the read-file operations-especially when downloading large
974 (greater than 64 MB) files-instead of making two calls to the read-file operation.
975 Following this recommendation will save time if the file is larger than expected
976 and the TFTP server does not support TFTP option extensions. Without TFTP option
977 extension support, the client has to download the entire file, counting and discarding
978 the received packets, to determine the file size.
979 For write operations, the data to be sent is in the buffer specified by BufferPtr.
980 BufferSize specifies the number of bytes to send. If the write operation completes
981 successfully, then EFI_SUCCESS will be returned.
982 For TFTP "get file size" operations, the size of the requested file or directory
983 is returned in BufferSize, and EFI_SUCCESS will be returned. If the TFTP server
984 does not support options, the file will be downloaded into a bit bucket and the
985 length of the downloaded file will be returned. For MTFTP "get file size" operations,
986 if the MTFTP server does not support the "get file size" option, EFI_UNSUPPORTED
987 will be returned.
988 This function can take up to 10 seconds to timeout and return control to the caller.
989 If the TFTP sequence does not complete, EFI_TIMEOUT will be returned.
990 If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,
991 then the TFTP sequence is stopped and EFI_ABORTED will be returned.
992 The format of the data returned from a TFTP read directory operation is a null-terminated
993 filename followed by a null-terminated information string, of the form
994 "size year-month-day hour:minute:second" (i.e. %d %d-%d-%d %d:%d:%f - note that
995 the seconds field can be a decimal number), where the date and time are UTC. For
996 an MTFTP read directory command, there is additionally a null-terminated multicast
997 IP address preceding the filename of the form %d.%d.%d.%d for IP v4. The final
998 entry is itself null-terminated, so that the final information string is terminated
999 with two null octets.
1000
1001 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
1002 @param Operation The type of operation to perform.
1003 @param BufferPtr A pointer to the data buffer.
1004 @param Overwrite Only used on write file operations. TRUE if a file on a remote server can
1005 be overwritten.
1006 @param BufferSize For get-file-size operations, *BufferSize returns the size of the
1007 requested file.
1008 @param BlockSize The requested block size to be used during a TFTP transfer.
1009 @param ServerIp The TFTP / MTFTP server IP address.
1010 @param Filename A Null-terminated ASCII string that specifies a directory name or a file
1011 name.
1012 @param Info Pointer to the MTFTP information.
1013 @param DontUseBuffer Set to FALSE for normal TFTP and MTFTP read file operation.
1014
1015 @retval EFI_SUCCESS The TFTP/MTFTP operation was completed.
1016 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
1017 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1018 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
1019 @retval EFI_BUFFER_TOO_SMALL The buffer is not large enough to complete the read operation.
1020 @retval EFI_ABORTED The callback function aborted the TFTP/MTFTP operation.
1021 @retval EFI_TIMEOUT The TFTP/MTFTP operation timed out.
1022 @retval EFI_ICMP_ERROR An ICMP error packet was received during the MTFTP session.
1023 @retval EFI_TFTP_ERROR A TFTP error packet was received during the MTFTP session.
1024
1025 **/
1026 EFI_STATUS
1027 EFIAPI
1028 EfiPxeBcMtftp (
1029 IN EFI_PXE_BASE_CODE_PROTOCOL *This,
1030 IN EFI_PXE_BASE_CODE_TFTP_OPCODE Operation,
1031 IN OUT VOID *BufferPtr,
1032 IN BOOLEAN Overwrite,
1033 IN OUT UINT64 *BufferSize,
1034 IN UINTN *BlockSize OPTIONAL,
1035 IN EFI_IP_ADDRESS *ServerIp,
1036 IN UINT8 *Filename,
1037 IN EFI_PXE_BASE_CODE_MTFTP_INFO *Info OPTIONAL,
1038 IN BOOLEAN DontUseBuffer
1039 )
1040 {
1041 PXEBC_PRIVATE_DATA *Private;
1042 EFI_MTFTP4_CONFIG_DATA Mtftp4Config;
1043 EFI_STATUS Status;
1044 EFI_PXE_BASE_CODE_MODE *Mode;
1045 EFI_MAC_ADDRESS TempMacAddr;
1046
1047 if ((This == NULL) ||
1048 (Filename == NULL) ||
1049 (BufferSize == NULL) ||
1050 ((ServerIp == NULL) || !Ip4IsUnicast (NTOHL (ServerIp->Addr[0]), 0)) ||
1051 ((BufferPtr == NULL) && DontUseBuffer) ||
1052 ((BlockSize != NULL) && (*BlockSize < 512))) {
1053
1054 return EFI_INVALID_PARAMETER;
1055 }
1056
1057 Status = EFI_DEVICE_ERROR;
1058 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
1059 Mode = &Private->Mode;
1060
1061 if (!Mode->AutoArp) {
1062 //
1063 // If AutoArp is set false, check arp cache
1064 //
1065 UpdateArpCache (This);
1066 if (!FindInArpCache (Mode, &ServerIp->v4, &TempMacAddr)) {
1067 return EFI_DEVICE_ERROR;
1068 }
1069 }
1070
1071 Mode->TftpErrorReceived = FALSE;
1072 Mode->IcmpErrorReceived = FALSE;
1073
1074 Mtftp4Config.UseDefaultSetting = FALSE;
1075 Mtftp4Config.TimeoutValue = PXEBC_MTFTP_TIMEOUT;
1076 Mtftp4Config.TryCount = PXEBC_MTFTP_RETRIES;
1077
1078 CopyMem (
1079 &Mtftp4Config.StationIp,
1080 &Private->StationIp,
1081 sizeof (EFI_IPv4_ADDRESS)
1082 );
1083 CopyMem (
1084 &Mtftp4Config.SubnetMask,
1085 &Private->SubnetMask,
1086 sizeof (EFI_IPv4_ADDRESS)
1087 );
1088 CopyMem (
1089 &Mtftp4Config.GatewayIp,
1090 &Private->GatewayIp,
1091 sizeof (EFI_IPv4_ADDRESS)
1092 );
1093 CopyMem (
1094 &Mtftp4Config.ServerIp,
1095 ServerIp,
1096 sizeof (EFI_IPv4_ADDRESS)
1097 );
1098
1099 switch (Operation) {
1100
1101 case EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE:
1102
1103 Status = PxeBcTftpGetFileSize (
1104 Private,
1105 &Mtftp4Config,
1106 Filename,
1107 BlockSize,
1108 BufferSize
1109 );
1110
1111 break;
1112
1113 case EFI_PXE_BASE_CODE_TFTP_READ_FILE:
1114
1115 Status = PxeBcTftpReadFile (
1116 Private,
1117 &Mtftp4Config,
1118 Filename,
1119 BlockSize,
1120 BufferPtr,
1121 BufferSize,
1122 DontUseBuffer
1123 );
1124
1125 break;
1126
1127 case EFI_PXE_BASE_CODE_TFTP_WRITE_FILE:
1128
1129 Status = PxeBcTftpWriteFile (
1130 Private,
1131 &Mtftp4Config,
1132 Filename,
1133 Overwrite,
1134 BlockSize,
1135 BufferPtr,
1136 BufferSize
1137 );
1138
1139 break;
1140
1141 case EFI_PXE_BASE_CODE_TFTP_READ_DIRECTORY:
1142
1143 Status = PxeBcTftpReadDirectory (
1144 Private,
1145 &Mtftp4Config,
1146 Filename,
1147 BlockSize,
1148 BufferPtr,
1149 BufferSize,
1150 DontUseBuffer
1151 );
1152
1153 break;
1154
1155 case EFI_PXE_BASE_CODE_MTFTP_GET_FILE_SIZE:
1156 case EFI_PXE_BASE_CODE_MTFTP_READ_FILE:
1157 case EFI_PXE_BASE_CODE_MTFTP_READ_DIRECTORY:
1158 Status = EFI_UNSUPPORTED;
1159 break;
1160
1161 default:
1162
1163 Status = EFI_INVALID_PARAMETER;
1164 break;
1165 }
1166
1167 if (Status == EFI_ICMP_ERROR) {
1168 Mode->IcmpErrorReceived = TRUE;
1169 }
1170
1171 return Status;
1172 }
1173
1174
1175 /**
1176 Writes a UDP packet to the network interface.
1177
1178 This function writes a UDP packet specified by the (optional HeaderPtr and)
1179 BufferPtr parameters to the network interface. The UDP header is automatically
1180 built by this routine. It uses the parameters OpFlags, DestIp, DestPort, GatewayIp,
1181 SrcIp, and SrcPort to build this header. If the packet is successfully built and
1182 transmitted through the network interface, then EFI_SUCCESS will be returned.
1183 If a timeout occurs during the transmission of the packet, then EFI_TIMEOUT will
1184 be returned. If an ICMP error occurs during the transmission of the packet, then
1185 the IcmpErrorReceived field is set to TRUE, the IcmpError field is filled in and
1186 EFI_ICMP_ERROR will be returned. If the Callback Protocol does not return
1187 EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE, then EFI_ABORTED will be returned.
1188
1189 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
1190 @param OpFlags The UDP operation flags.
1191 @param DestIp The destination IP address.
1192 @param DestPort The destination UDP port number.
1193 @param GatewayIp The gateway IP address.
1194 @param SrcIp The source IP address.
1195 @param SrcPort The source UDP port number.
1196 @param HeaderSize An optional field which may be set to the length of a header at
1197 HeaderPtr to be prefixed to the data at BufferPtr.
1198 @param HeaderPtr If HeaderSize is not NULL, a pointer to a header to be prefixed to the
1199 data at BufferPtr.
1200 @param BufferSize A pointer to the size of the data at BufferPtr.
1201 @param BufferPtr A pointer to the data to be written.
1202
1203 @retval EFI_SUCCESS The UDP Write operation was completed.
1204 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
1205 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1206 @retval EFI_BAD_BUFFER_SIZE The buffer is too long to be transmitted.
1207 @retval EFI_ABORTED The callback function aborted the UDP Write operation.
1208 @retval EFI_TIMEOUT The UDP Write operation timed out.
1209 @retval EFI_ICMP_ERROR An ICMP error packet was received during the UDP write session.
1210
1211 **/
1212 EFI_STATUS
1213 EFIAPI
1214 EfiPxeBcUdpWrite (
1215 IN EFI_PXE_BASE_CODE_PROTOCOL *This,
1216 IN UINT16 OpFlags,
1217 IN EFI_IP_ADDRESS *DestIp,
1218 IN EFI_PXE_BASE_CODE_UDP_PORT *DestPort,
1219 IN EFI_IP_ADDRESS *GatewayIp OPTIONAL,
1220 IN EFI_IP_ADDRESS *SrcIp OPTIONAL,
1221 IN OUT EFI_PXE_BASE_CODE_UDP_PORT *SrcPort OPTIONAL,
1222 IN UINTN *HeaderSize OPTIONAL,
1223 IN VOID *HeaderPtr OPTIONAL,
1224 IN UINTN *BufferSize,
1225 IN VOID *BufferPtr
1226 )
1227 {
1228 PXEBC_PRIVATE_DATA *Private;
1229 EFI_UDP4_PROTOCOL *Udp4;
1230 EFI_UDP4_COMPLETION_TOKEN Token;
1231 EFI_UDP4_TRANSMIT_DATA *Udp4TxData;
1232 UINT32 FragCount;
1233 UINT32 DataLength;
1234 EFI_UDP4_SESSION_DATA Udp4Session;
1235 EFI_STATUS Status;
1236 BOOLEAN IsDone;
1237 EFI_PXE_BASE_CODE_MODE *Mode;
1238 EFI_MAC_ADDRESS TempMacAddr;
1239
1240 IsDone = FALSE;
1241
1242 if ((This == NULL) || (DestIp == NULL) || (DestPort == NULL)) {
1243 return EFI_INVALID_PARAMETER;
1244 }
1245
1246 if ((GatewayIp != NULL) && !Ip4IsUnicast (NTOHL (GatewayIp->Addr[0]), 0)) {
1247 //
1248 // Gateway is provided but it's not a unicast IP address.
1249 //
1250 return EFI_INVALID_PARAMETER;
1251 }
1252
1253 if ((HeaderSize != NULL) && ((*HeaderSize == 0) || (HeaderPtr == NULL))) {
1254 //
1255 // The HeaderSize ptr isn't NULL and: 1. the value is zero; or 2. the HeaderPtr
1256 // is NULL.
1257 //
1258 return EFI_INVALID_PARAMETER;
1259 }
1260
1261 if ((BufferSize == NULL) || ((*BufferSize != 0) && (BufferPtr == NULL))) {
1262 return EFI_INVALID_PARAMETER;
1263 }
1264
1265 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
1266 Udp4 = Private->Udp4Write;
1267 Mode = &Private->Mode;
1268 if (!Mode->Started) {
1269 return EFI_NOT_STARTED;
1270 }
1271
1272 if (!Private->AddressIsOk && (SrcIp == NULL)) {
1273 return EFI_INVALID_PARAMETER;
1274 }
1275
1276 if (!Mode->AutoArp) {
1277 //
1278 // If AutoArp is set false, check arp cache
1279 //
1280 UpdateArpCache (This);
1281 if (!FindInArpCache (Mode, &DestIp->v4, &TempMacAddr)) {
1282 return EFI_DEVICE_ERROR;
1283 }
1284 }
1285
1286 Mode->IcmpErrorReceived = FALSE;
1287
1288 if ((Private->CurrentUdpSrcPort == 0) ||
1289 ((SrcPort != NULL) && (*SrcPort != Private->CurrentUdpSrcPort))) {
1290 //
1291 // Port is changed, (re)configure the Udp4Write instance
1292 //
1293 if (SrcPort != NULL) {
1294 Private->CurrentUdpSrcPort = *SrcPort;
1295 }
1296
1297 Status = PxeBcConfigureUdpWriteInstance (
1298 Udp4,
1299 &Private->StationIp.v4,
1300 &Private->SubnetMask.v4,
1301 &Private->GatewayIp.v4,
1302 &Private->CurrentUdpSrcPort
1303 );
1304 if (EFI_ERROR (Status)) {
1305 Private->CurrentUdpSrcPort = 0;
1306 return EFI_INVALID_PARAMETER;
1307 }
1308 }
1309
1310 ZeroMem (&Token, sizeof (EFI_UDP4_COMPLETION_TOKEN));
1311 ZeroMem (&Udp4Session, sizeof (EFI_UDP4_SESSION_DATA));
1312
1313 CopyMem (&Udp4Session.DestinationAddress, DestIp, sizeof (EFI_IPv4_ADDRESS));
1314 Udp4Session.DestinationPort = *DestPort;
1315 if (SrcIp != NULL) {
1316 CopyMem (&Udp4Session.SourceAddress, SrcIp, sizeof (EFI_IPv4_ADDRESS));
1317 }
1318 if (SrcPort != NULL) {
1319 Udp4Session.SourcePort = *SrcPort;
1320 }
1321
1322 FragCount = (HeaderSize != NULL) ? 2 : 1;
1323 Udp4TxData = (EFI_UDP4_TRANSMIT_DATA *) AllocateZeroPool (sizeof (EFI_UDP4_TRANSMIT_DATA) + (FragCount - 1) * sizeof (EFI_UDP4_FRAGMENT_DATA));
1324 if (Udp4TxData == NULL) {
1325 return EFI_OUT_OF_RESOURCES;
1326 }
1327
1328 Udp4TxData->FragmentCount = FragCount;
1329 Udp4TxData->FragmentTable[FragCount - 1].FragmentLength = (UINT32) *BufferSize;
1330 Udp4TxData->FragmentTable[FragCount - 1].FragmentBuffer = BufferPtr;
1331 DataLength = (UINT32) *BufferSize;
1332
1333 if (FragCount == 2) {
1334
1335 Udp4TxData->FragmentTable[0].FragmentLength = (UINT32) *HeaderSize;
1336 Udp4TxData->FragmentTable[0].FragmentBuffer = HeaderPtr;
1337 DataLength += (UINT32) *HeaderSize;
1338 }
1339
1340 if (GatewayIp != NULL) {
1341 Udp4TxData->GatewayAddress = (EFI_IPv4_ADDRESS *) GatewayIp;
1342 }
1343 Udp4TxData->UdpSessionData = &Udp4Session;
1344 Udp4TxData->DataLength = DataLength;
1345 Token.Packet.TxData = Udp4TxData;
1346
1347 Status = gBS->CreateEvent (
1348 EVT_NOTIFY_SIGNAL,
1349 TPL_NOTIFY,
1350 PxeBcCommonNotify,
1351 &IsDone,
1352 &Token.Event
1353 );
1354 if (EFI_ERROR (Status)) {
1355 goto ON_EXIT;
1356 }
1357
1358 Status = Udp4->Transmit (Udp4, &Token);
1359 if (EFI_ERROR (Status)) {
1360 if (Status == EFI_ICMP_ERROR) {
1361 Mode->IcmpErrorReceived = TRUE;
1362 }
1363 goto ON_EXIT;
1364 }
1365
1366 while (!IsDone) {
1367
1368 Udp4->Poll (Udp4);
1369 }
1370
1371 Status = Token.Status;
1372
1373 ON_EXIT:
1374
1375 if (Token.Event != NULL) {
1376 gBS->CloseEvent (Token.Event);
1377 }
1378
1379 gBS->FreePool (Udp4TxData);
1380
1381 return Status;
1382 }
1383
1384 /**
1385 Decide whether the incoming UDP packet is acceptable per IP filter settings
1386 in provided PxeBcMode.
1387
1388 @param PxeBcMode Pointer to EFI_PXE_BASE_CODE_MODE.
1389 @param Session Received UDP session.
1390
1391 @retval TRUE The UDP package matches IP filters.
1392 @retval FALSE The UDP package doesn't matches IP filters.
1393
1394 **/
1395 BOOLEAN
1396 CheckIpByFilter (
1397 IN EFI_PXE_BASE_CODE_MODE *PxeBcMode,
1398 IN EFI_UDP4_SESSION_DATA *Session
1399 )
1400 {
1401 UINTN Index;
1402 EFI_IPv4_ADDRESS Ip4Address;
1403 EFI_IPv4_ADDRESS DestIp4Address;
1404
1405 if ((PxeBcMode->IpFilter.Filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS) != 0) {
1406 return TRUE;
1407 }
1408
1409 CopyMem (&DestIp4Address, &Session->DestinationAddress, sizeof (DestIp4Address));
1410 if ((PxeBcMode->IpFilter.Filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS_MULTICAST) &&
1411 IP4_IS_MULTICAST (EFI_NTOHL (DestIp4Address))
1412 ) {
1413 return TRUE;
1414 }
1415
1416 if ((PxeBcMode->IpFilter.Filters & EFI_PXE_BASE_CODE_IP_FILTER_BROADCAST) &&
1417 IP4_IS_LOCAL_BROADCAST (EFI_NTOHL (DestIp4Address))
1418 ) {
1419 return TRUE;
1420 }
1421
1422 CopyMem (&Ip4Address, &PxeBcMode->StationIp.v4, sizeof (Ip4Address));
1423 if ((PxeBcMode->IpFilter.Filters & EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP) &&
1424 EFI_IP4_EQUAL (&Ip4Address, &DestIp4Address)
1425 ) {
1426 return TRUE;
1427 }
1428
1429 ASSERT (PxeBcMode->IpFilter.IpCnt < EFI_PXE_BASE_CODE_MAX_IPCNT);
1430
1431 for (Index = 0; Index < PxeBcMode->IpFilter.IpCnt; Index++) {
1432 CopyMem (
1433 &Ip4Address,
1434 &PxeBcMode->IpFilter.IpList[Index].v4,
1435 sizeof (Ip4Address)
1436 );
1437 if (EFI_IP4_EQUAL (&Ip4Address, &DestIp4Address)) {
1438 return TRUE;
1439 }
1440 }
1441
1442 return FALSE;
1443 }
1444
1445 /**
1446 Reads a UDP packet from the network interface.
1447
1448 This function reads a UDP packet from a network interface. The data contents
1449 are returned in (the optional HeaderPtr and) BufferPtr, and the size of the
1450 buffer received is returned in BufferSize . If the input BufferSize is smaller
1451 than the UDP packet received (less optional HeaderSize), it will be set to the
1452 required size, and EFI_BUFFER_TOO_SMALL will be returned. In this case, the
1453 contents of BufferPtr are undefined, and the packet is lost. If a UDP packet is
1454 successfully received, then EFI_SUCCESS will be returned, and the information
1455 from the UDP header will be returned in DestIp, DestPort, SrcIp, and SrcPort if
1456 they are not NULL. Depending on the values of OpFlags and the DestIp, DestPort,
1457 SrcIp, and SrcPort input values, different types of UDP packet receive filtering
1458 will be performed. The following tables summarize these receive filter operations.
1459
1460 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
1461 @param OpFlags The UDP operation flags.
1462 @param DestIp The destination IP address.
1463 @param DestPort The destination UDP port number.
1464 @param SrcIp The source IP address.
1465 @param SrcPort The source UDP port number.
1466 @param HeaderSize An optional field which may be set to the length of a header at
1467 HeaderPtr to be prefixed to the data at BufferPtr.
1468 @param HeaderPtr If HeaderSize is not NULL, a pointer to a header to be prefixed to the
1469 data at BufferPtr.
1470 @param BufferSize A pointer to the size of the data at BufferPtr.
1471 @param BufferPtr A pointer to the data to be read.
1472
1473 @retval EFI_SUCCESS The UDP Read operation was completed.
1474 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
1475 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1476 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
1477 @retval EFI_BUFFER_TOO_SMALL The packet is larger than Buffer can hold.
1478 @retval EFI_ABORTED The callback function aborted the UDP Read operation.
1479 @retval EFI_TIMEOUT The UDP Read operation timed out.
1480
1481 **/
1482 EFI_STATUS
1483 EFIAPI
1484 EfiPxeBcUdpRead (
1485 IN EFI_PXE_BASE_CODE_PROTOCOL *This,
1486 IN UINT16 OpFlags,
1487 IN OUT EFI_IP_ADDRESS *DestIp OPTIONAL,
1488 IN OUT EFI_PXE_BASE_CODE_UDP_PORT *DestPort OPTIONAL,
1489 IN OUT EFI_IP_ADDRESS *SrcIp OPTIONAL,
1490 IN OUT EFI_PXE_BASE_CODE_UDP_PORT *SrcPort OPTIONAL,
1491 IN UINTN *HeaderSize OPTIONAL,
1492 IN VOID *HeaderPtr OPTIONAL,
1493 IN OUT UINTN *BufferSize,
1494 IN VOID *BufferPtr
1495 )
1496 {
1497 PXEBC_PRIVATE_DATA *Private;
1498 EFI_PXE_BASE_CODE_MODE *Mode;
1499 EFI_UDP4_PROTOCOL *Udp4;
1500 EFI_UDP4_COMPLETION_TOKEN Token;
1501 EFI_UDP4_RECEIVE_DATA *RxData;
1502 EFI_UDP4_SESSION_DATA *Session;
1503 EFI_STATUS Status;
1504 BOOLEAN IsDone;
1505 BOOLEAN Matched;
1506 UINTN CopyLen;
1507
1508 if (This == NULL || DestIp == NULL || DestPort == NULL) {
1509 return EFI_INVALID_PARAMETER;
1510 }
1511
1512 if (((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT) == 0 && (DestPort == NULL)) ||
1513 ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT) == 0 && (SrcIp == NULL)) ||
1514 ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT) == 0 && (SrcPort == NULL))) {
1515 return EFI_INVALID_PARAMETER;
1516 }
1517
1518 if (((HeaderSize != NULL) && (*HeaderSize == 0)) || ((HeaderSize != NULL) && (HeaderPtr == NULL))) {
1519 return EFI_INVALID_PARAMETER;
1520 }
1521
1522 if ((BufferSize == NULL) || ((BufferPtr == NULL) && (*BufferSize != 0))) {
1523 return EFI_INVALID_PARAMETER;
1524 }
1525
1526 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
1527 Mode = Private->PxeBc.Mode;
1528 Udp4 = Private->Udp4Read;
1529
1530 if (!Mode->Started) {
1531 return EFI_NOT_STARTED;
1532 }
1533
1534 Mode->IcmpErrorReceived = FALSE;
1535
1536 Status = gBS->CreateEvent (
1537 EVT_NOTIFY_SIGNAL,
1538 TPL_NOTIFY,
1539 PxeBcCommonNotify,
1540 &IsDone,
1541 &Token.Event
1542 );
1543 if (EFI_ERROR (Status)) {
1544 return EFI_OUT_OF_RESOURCES;
1545 }
1546
1547 TRY_AGAIN:
1548
1549 IsDone = FALSE;
1550 Status = Udp4->Receive (Udp4, &Token);
1551 if (EFI_ERROR (Status)) {
1552 if (Status == EFI_ICMP_ERROR) {
1553 Mode->IcmpErrorReceived = TRUE;
1554 }
1555 goto ON_EXIT;
1556 }
1557
1558 Udp4->Poll (Udp4);
1559
1560 if (!IsDone) {
1561 Status = EFI_TIMEOUT;
1562 } else {
1563
1564 //
1565 // check whether this packet matches the filters
1566 //
1567 if (EFI_ERROR (Token.Status)){
1568 goto ON_EXIT;
1569 }
1570
1571 RxData = Token.Packet.RxData;
1572 Session = &RxData->UdpSession;
1573
1574 Matched = TRUE;
1575
1576 if ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_USE_FILTER) != 0) {
1577 Matched = FALSE;
1578 //
1579 // Check UDP package by IP filter settings
1580 //
1581 if (CheckIpByFilter (Mode, Session)) {
1582 Matched = TRUE;
1583 }
1584 }
1585
1586 if (Matched) {
1587 Matched = FALSE;
1588
1589 //
1590 // Match the destination ip of the received udp dgram
1591 //
1592 if ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_IP) != 0) {
1593 Matched = TRUE;
1594
1595 if (DestIp != NULL) {
1596 CopyMem (DestIp, &Session->DestinationAddress, sizeof (EFI_IPv4_ADDRESS));
1597 }
1598 } else {
1599 if (DestIp != NULL) {
1600 if (EFI_IP4_EQUAL (DestIp, &Session->DestinationAddress)) {
1601 Matched = TRUE;
1602 }
1603 } else {
1604 if (EFI_IP4_EQUAL (&Private->StationIp, &Session->DestinationAddress)) {
1605 Matched = TRUE;
1606 }
1607 }
1608 }
1609 }
1610
1611 if (Matched) {
1612 //
1613 // Match the destination port of the received udp dgram
1614 //
1615 if ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT) != 0) {
1616
1617 if (DestPort != NULL) {
1618 *DestPort = Session->DestinationPort;
1619 }
1620 } else {
1621
1622 if (*DestPort != Session->DestinationPort) {
1623 Matched = FALSE;
1624 }
1625 }
1626 }
1627
1628 if (Matched) {
1629 //
1630 // Match the source ip of the received udp dgram
1631 //
1632 if ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP) != 0) {
1633
1634 if (SrcIp != NULL) {
1635 CopyMem (SrcIp, &Session->SourceAddress, sizeof (EFI_IPv4_ADDRESS));
1636 }
1637 } else {
1638
1639 if (!EFI_IP4_EQUAL (SrcIp, &Session->SourceAddress)) {
1640 Matched = FALSE;
1641 }
1642 }
1643 }
1644
1645 if (Matched) {
1646 //
1647 // Match the source port of the received udp dgram
1648 //
1649 if ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT) != 0) {
1650
1651 if (SrcPort != NULL) {
1652 *SrcPort = Session->SourcePort;
1653 }
1654 } else {
1655
1656 if (*SrcPort != Session->SourcePort) {
1657 Matched = FALSE;
1658 }
1659 }
1660 }
1661
1662 if (Matched) {
1663
1664 CopyLen = 0;
1665
1666 if (HeaderSize != NULL) {
1667 CopyLen = MIN (*HeaderSize, RxData->DataLength);
1668 CopyMem (HeaderPtr, RxData->FragmentTable[0].FragmentBuffer, CopyLen);
1669 *HeaderSize = CopyLen;
1670 }
1671
1672 if (RxData->DataLength - CopyLen > *BufferSize) {
1673
1674 Status = EFI_BUFFER_TOO_SMALL;
1675 } else {
1676
1677 *BufferSize = RxData->DataLength - CopyLen;
1678 CopyMem (
1679 BufferPtr,
1680 (UINT8 *) RxData->FragmentTable[0].FragmentBuffer + CopyLen,
1681 *BufferSize
1682 );
1683 }
1684 } else {
1685
1686 Status = EFI_TIMEOUT;
1687 }
1688
1689 //
1690 // Recycle the RxData
1691 //
1692 gBS->SignalEvent (RxData->RecycleSignal);
1693
1694 if (!Matched) {
1695 goto TRY_AGAIN;
1696 }
1697 }
1698
1699 ON_EXIT:
1700
1701 Udp4->Cancel (Udp4, &Token);
1702
1703 gBS->CloseEvent (Token.Event);
1704
1705 return Status;
1706 }
1707
1708 /**
1709 Updates the IP receive filters of a network device and enables software filtering.
1710
1711 The NewFilter field is used to modify the network device's current IP receive
1712 filter settings and to enable a software filter. This function updates the IpFilter
1713 field of the EFI_PXE_BASE_CODE_MODE structure with the contents of NewIpFilter.
1714 The software filter is used when the USE_FILTER in OpFlags is set to UdpRead().
1715 The current hardware filter remains in effect no matter what the settings of OpFlags
1716 are, so that the meaning of ANY_DEST_IP set in OpFlags to UdpRead() is from those
1717 packets whose reception is enabled in hardware-physical NIC address (unicast),
1718 broadcast address, logical address or addresses (multicast), or all (promiscuous).
1719 UdpRead() does not modify the IP filter settings.
1720 Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP receive
1721 filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.
1722 If an application or driver wishes to preserve the IP receive filter settings,
1723 it will have to preserve the IP receive filter settings before these calls, and
1724 use SetIpFilter() to restore them after the calls. If incompatible filtering is
1725 requested (for example, PROMISCUOUS with anything else) or if the device does not
1726 support a requested filter setting and it cannot be accommodated in software
1727 (for example, PROMISCUOUS not supported), EFI_INVALID_PARAMETER will be returned.
1728 The IPlist field is used to enable IPs other than the StationIP. They may be
1729 multicast or unicast. If IPcnt is set as well as EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP,
1730 then both the StationIP and the IPs from the IPlist will be used.
1731
1732 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
1733 @param NewFilter Pointer to the new set of IP receive filters.
1734
1735 @retval EFI_SUCCESS The IP receive filter settings were updated.
1736 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
1737 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1738
1739 **/
1740 EFI_STATUS
1741 EFIAPI
1742 EfiPxeBcSetIpFilter (
1743 IN EFI_PXE_BASE_CODE_PROTOCOL *This,
1744 IN EFI_PXE_BASE_CODE_IP_FILTER *NewFilter
1745 )
1746 {
1747 EFI_STATUS Status;
1748 PXEBC_PRIVATE_DATA *Private;
1749 EFI_PXE_BASE_CODE_MODE *Mode;
1750 UINTN Index;
1751 BOOLEAN PromiscuousNeed;
1752
1753 if (This == NULL) {
1754 DEBUG ((EFI_D_ERROR, "This == NULL.\n"));
1755 return EFI_INVALID_PARAMETER;
1756 }
1757
1758 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
1759 Mode = Private->PxeBc.Mode;
1760
1761 if (NewFilter == NULL) {
1762 DEBUG ((EFI_D_ERROR, "NewFilter == NULL.\n"));
1763 return EFI_INVALID_PARAMETER;
1764 }
1765
1766 if (NewFilter->IpCnt > EFI_PXE_BASE_CODE_MAX_IPCNT) {
1767 DEBUG ((EFI_D_ERROR, "NewFilter->IpCnt > %d.\n", EFI_PXE_BASE_CODE_MAX_IPCNT));
1768 return EFI_INVALID_PARAMETER;
1769 }
1770
1771 if (!Mode->Started) {
1772 DEBUG ((EFI_D_ERROR, "BC was not started.\n"));
1773 return EFI_NOT_STARTED;
1774 }
1775
1776 PromiscuousNeed = FALSE;
1777
1778 for (Index = 0; Index < NewFilter->IpCnt; ++Index) {
1779 if (IP4_IS_LOCAL_BROADCAST (EFI_IP4 (NewFilter->IpList[Index].v4))) {
1780 //
1781 // The IP is a broadcast address.
1782 //
1783 DEBUG ((EFI_D_ERROR, "There is broadcast address in NewFilter.\n"));
1784 return EFI_INVALID_PARAMETER;
1785 }
1786 if (Ip4IsUnicast (EFI_IP4 (NewFilter->IpList[Index].v4), 0) &&
1787 (NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP)
1788 ) {
1789 //
1790 // If EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP is set and IP4 address is in IpList,
1791 // promiscuous mode is needed.
1792 //
1793 PromiscuousNeed = TRUE;
1794 }
1795 }
1796
1797 //
1798 // Clear the UDP instance configuration, all joined groups will be left
1799 // during the operation.
1800 //
1801 Private->Udp4Read->Configure (Private->Udp4Read, NULL);
1802 Private->Udp4CfgData.AcceptPromiscuous = FALSE;
1803 Private->Udp4CfgData.AcceptBroadcast = FALSE;
1804
1805 if (PromiscuousNeed ||
1806 NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS ||
1807 NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS_MULTICAST
1808 ) {
1809 //
1810 // Configure the udp4 filter to receive all packages
1811 //
1812 Private->Udp4CfgData.AcceptPromiscuous = TRUE;
1813
1814 //
1815 // Configure the UDP instance with the new configuration.
1816 //
1817 Status = Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);
1818 if (EFI_ERROR (Status)) {
1819 return Status;
1820 }
1821
1822 } else {
1823
1824 if (NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_BROADCAST) {
1825 //
1826 // Configure the udp4 filter to receive all broadcast packages
1827 //
1828 Private->Udp4CfgData.AcceptBroadcast = TRUE;
1829 }
1830
1831 //
1832 // Configure the UDP instance with the new configuration.
1833 //
1834 Status = Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);
1835 if (EFI_ERROR (Status)) {
1836 return Status;
1837 }
1838
1839 if (NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP) {
1840
1841 for (Index = 0; Index < NewFilter->IpCnt; ++Index) {
1842 if (IP4_IS_MULTICAST (EFI_NTOHL (NewFilter->IpList[Index].v4))) {
1843 //
1844 // Join the mutilcast group
1845 //
1846 Status = Private->Udp4Read->Groups (Private->Udp4Read, TRUE, &NewFilter->IpList[Index].v4);
1847 if (EFI_ERROR (Status)) {
1848 return Status;
1849 }
1850 }
1851 }
1852 }
1853 }
1854
1855
1856 //
1857 // Save the new filter.
1858 //
1859 CopyMem (&Mode->IpFilter, NewFilter, sizeof (Mode->IpFilter));
1860
1861 return EFI_SUCCESS;
1862 }
1863
1864
1865 /**
1866 Uses the ARP protocol to resolve a MAC address.
1867
1868 This function uses the ARP protocol to resolve a MAC address. The UsingIpv6 field
1869 of the EFI_PXE_BASE_CODE_MODE structure is used to determine if IPv4 or IPv6
1870 addresses are being used. The IP address specified by IpAddr is used to resolve
1871 a MAC address. If the ARP protocol succeeds in resolving the specified address,
1872 then the ArpCacheEntries and ArpCache fields of the EFI_PXE_BASE_CODE_MODE structure
1873 are updated, and EFI_SUCCESS is returned. If MacAddr is not NULL, the resolved
1874 MAC address is placed there as well. If the PXE Base Code protocol is in the
1875 stopped state, then EFI_NOT_STARTED is returned. If the ARP protocol encounters
1876 a timeout condition while attempting to resolve an address, then EFI_TIMEOUT is
1877 returned. If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,
1878 then EFI_ABORTED is returned.
1879
1880 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
1881 @param IpAddr Pointer to the IP address that is used to resolve a MAC address.
1882 @param MacAddr If not NULL, a pointer to the MAC address that was resolved with the
1883 ARP protocol.
1884
1885 @retval EFI_SUCCESS The IP or MAC address was resolved.
1886 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
1887 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1888 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
1889 @retval EFI_ICMP_ERROR Something error occur with the ICMP packet message.
1890
1891 **/
1892 EFI_STATUS
1893 EFIAPI
1894 EfiPxeBcArp (
1895 IN EFI_PXE_BASE_CODE_PROTOCOL * This,
1896 IN EFI_IP_ADDRESS * IpAddr,
1897 IN EFI_MAC_ADDRESS * MacAddr OPTIONAL
1898 )
1899 {
1900 PXEBC_PRIVATE_DATA *Private;
1901 EFI_PXE_BASE_CODE_MODE *Mode;
1902 EFI_STATUS Status;
1903 EFI_MAC_ADDRESS TempMacAddr;
1904
1905 if (This == NULL || IpAddr == NULL) {
1906 return EFI_INVALID_PARAMETER;
1907 }
1908
1909 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
1910 Mode = Private->PxeBc.Mode;
1911
1912 if (!Mode->Started) {
1913 return EFI_NOT_STARTED;
1914 }
1915
1916 if (!Private->AddressIsOk || Mode->UsingIpv6) {
1917 //
1918 // We can't resolve the IP address if we don't have a local address now.
1919 // Don't have ARP for IPv6.
1920 //
1921 return EFI_INVALID_PARAMETER;
1922 }
1923
1924 Mode->IcmpErrorReceived = FALSE;
1925
1926 if (!Mode->AutoArp) {
1927 //
1928 // If AutoArp is set false, check arp cache
1929 //
1930 UpdateArpCache (This);
1931 if (!FindInArpCache (Mode, &IpAddr->v4, &TempMacAddr)) {
1932 return EFI_DEVICE_ERROR;
1933 }
1934 } else {
1935 Status = Private->Arp->Request (Private->Arp, &IpAddr->v4, NULL, &TempMacAddr);
1936 if (EFI_ERROR (Status)) {
1937 if (Status == EFI_ICMP_ERROR) {
1938 Mode->IcmpErrorReceived = TRUE;
1939 }
1940 return Status;
1941 }
1942 }
1943
1944 if (MacAddr != NULL) {
1945 CopyMem (MacAddr, &TempMacAddr, sizeof (EFI_MAC_ADDRESS));
1946 }
1947
1948 return EFI_SUCCESS;
1949 }
1950
1951 /**
1952 Updates the parameters that affect the operation of the PXE Base Code Protocol.
1953
1954 This function sets parameters that affect the operation of the PXE Base Code Protocol.
1955 The parameter specified by NewAutoArp is used to control the generation of ARP
1956 protocol packets. If NewAutoArp is TRUE, then ARP Protocol packets will be generated
1957 as required by the PXE Base Code Protocol. If NewAutoArp is FALSE, then no ARP
1958 Protocol packets will be generated. In this case, the only mappings that are
1959 available are those stored in the ArpCache of the EFI_PXE_BASE_CODE_MODE structure.
1960 If there are not enough mappings in the ArpCache to perform a PXE Base Code Protocol
1961 service, then the service will fail. This function updates the AutoArp field of
1962 the EFI_PXE_BASE_CODE_MODE structure to NewAutoArp.
1963 The SetParameters() call must be invoked after a Callback Protocol is installed
1964 to enable the use of callbacks.
1965
1966 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
1967 @param NewAutoArp If not NULL, a pointer to a value that specifies whether to replace the
1968 current value of AutoARP.
1969 @param NewSendGUID If not NULL, a pointer to a value that specifies whether to replace the
1970 current value of SendGUID.
1971 @param NewTTL If not NULL, a pointer to be used in place of the current value of TTL,
1972 the "time to live" field of the IP header.
1973 @param NewToS If not NULL, a pointer to be used in place of the current value of ToS,
1974 the "type of service" field of the IP header.
1975 @param NewMakeCallback If not NULL, a pointer to a value that specifies whether to replace the
1976 current value of the MakeCallback field of the Mode structure.
1977
1978 @retval EFI_SUCCESS The new parameters values were updated.
1979 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
1980 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1981
1982 **/
1983 EFI_STATUS
1984 EFIAPI
1985 EfiPxeBcSetParameters (
1986 IN EFI_PXE_BASE_CODE_PROTOCOL *This,
1987 IN BOOLEAN *NewAutoArp OPTIONAL,
1988 IN BOOLEAN *NewSendGUID OPTIONAL,
1989 IN UINT8 *NewTTL OPTIONAL,
1990 IN UINT8 *NewToS OPTIONAL,
1991 IN BOOLEAN *NewMakeCallback // OPTIONAL
1992 )
1993 {
1994 PXEBC_PRIVATE_DATA *Private;
1995 EFI_PXE_BASE_CODE_MODE *Mode;
1996 EFI_STATUS Status;
1997
1998 Status = EFI_SUCCESS;
1999
2000 if (This == NULL) {
2001 Status = EFI_INVALID_PARAMETER;
2002 goto ON_EXIT;
2003 }
2004
2005 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
2006 Mode = Private->PxeBc.Mode;
2007
2008 if (NewSendGUID != NULL && *NewSendGUID) {
2009 //
2010 // FixMe, cann't locate SendGuid
2011 //
2012 }
2013
2014 if (NewMakeCallback != NULL && *NewMakeCallback) {
2015
2016 Status = gBS->HandleProtocol (
2017 Private->Controller,
2018 &gEfiPxeBaseCodeCallbackProtocolGuid,
2019 (VOID **) &Private->PxeBcCallback
2020 );
2021 if (EFI_ERROR (Status) || (Private->PxeBcCallback->Callback == NULL)) {
2022
2023 Status = EFI_INVALID_PARAMETER;
2024 goto ON_EXIT;
2025 }
2026 }
2027
2028 if (!Mode->Started) {
2029 Status = EFI_NOT_STARTED;
2030 goto ON_EXIT;
2031 }
2032
2033 if (NewMakeCallback != NULL) {
2034
2035 if (*NewMakeCallback) {
2036 //
2037 // Update the Callback protocol.
2038 //
2039 Status = gBS->HandleProtocol (
2040 Private->Controller,
2041 &gEfiPxeBaseCodeCallbackProtocolGuid,
2042 (VOID **) &Private->PxeBcCallback
2043 );
2044
2045 if (EFI_ERROR (Status) || (Private->PxeBcCallback->Callback == NULL)) {
2046 Status = EFI_INVALID_PARAMETER;
2047 goto ON_EXIT;
2048 }
2049 } else {
2050 Private->PxeBcCallback = NULL;
2051 }
2052
2053 Mode->MakeCallbacks = *NewMakeCallback;
2054 }
2055
2056 if (NewAutoArp != NULL) {
2057 Mode->AutoArp = *NewAutoArp;
2058 }
2059
2060 if (NewSendGUID != NULL) {
2061 Mode->SendGUID = *NewSendGUID;
2062 }
2063
2064 if (NewTTL != NULL) {
2065 Mode->TTL = *NewTTL;
2066 }
2067
2068 if (NewToS != NULL) {
2069 Mode->ToS = *NewToS;
2070 }
2071
2072 ON_EXIT:
2073 return Status;
2074 }
2075
2076 /**
2077 Updates the station IP address and/or subnet mask values of a network device.
2078
2079 This function updates the station IP address and/or subnet mask values of a network
2080 device. The NewStationIp field is used to modify the network device's current IP address.
2081 If NewStationIP is NULL, then the current IP address will not be modified. Otherwise,
2082 this function updates the StationIp field of the EFI_PXE_BASE_CODE_MODE structure
2083 with NewStationIp. The NewSubnetMask field is used to modify the network device's current subnet
2084 mask. If NewSubnetMask is NULL, then the current subnet mask will not be modified.
2085 Otherwise, this function updates the SubnetMask field of the EFI_PXE_BASE_CODE_MODE
2086 structure with NewSubnetMask.
2087
2088 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
2089 @param NewStationIp Pointer to the new IP address to be used by the network device.
2090 @param NewSubnetMask Pointer to the new subnet mask to be used by the network device.
2091
2092 @retval EFI_SUCCESS The new station IP address and/or subnet mask were updated.
2093 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
2094 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
2095
2096 **/
2097 EFI_STATUS
2098 EFIAPI
2099 EfiPxeBcSetStationIP (
2100 IN EFI_PXE_BASE_CODE_PROTOCOL * This,
2101 IN EFI_IP_ADDRESS * NewStationIp OPTIONAL,
2102 IN EFI_IP_ADDRESS * NewSubnetMask OPTIONAL
2103 )
2104 {
2105 PXEBC_PRIVATE_DATA *Private;
2106 EFI_PXE_BASE_CODE_MODE *Mode;
2107 EFI_ARP_CONFIG_DATA ArpConfigData;
2108
2109 if (This == NULL) {
2110 return EFI_INVALID_PARAMETER;
2111 }
2112
2113 if (NewStationIp != NULL && !Ip4IsUnicast (NTOHL (NewStationIp->Addr[0]), 0)) {
2114 return EFI_INVALID_PARAMETER;
2115 }
2116
2117 if (NewSubnetMask != NULL && !IP4_IS_VALID_NETMASK (NTOHL (NewSubnetMask->Addr[0]))) {
2118 return EFI_INVALID_PARAMETER;
2119 }
2120
2121 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
2122 Mode = Private->PxeBc.Mode;
2123
2124 if (!Mode->Started) {
2125 return EFI_NOT_STARTED;
2126 }
2127
2128 if (NewStationIp != NULL) {
2129 CopyMem (&Mode->StationIp, NewStationIp, sizeof (EFI_IP_ADDRESS));
2130 CopyMem (&Private->StationIp, NewStationIp, sizeof (EFI_IP_ADDRESS));
2131 }
2132
2133 if (NewSubnetMask != NULL) {
2134 CopyMem (&Mode->SubnetMask, NewSubnetMask, sizeof (EFI_IP_ADDRESS));
2135 CopyMem (&Private->SubnetMask ,NewSubnetMask, sizeof (EFI_IP_ADDRESS));
2136 }
2137
2138 Private->AddressIsOk = TRUE;
2139
2140 if (!Mode->UsingIpv6) {
2141 //
2142 // If in IPv4 mode, configure the corresponding ARP with this new
2143 // station IP address.
2144 //
2145 ZeroMem (&ArpConfigData, sizeof (EFI_ARP_CONFIG_DATA));
2146
2147 ArpConfigData.SwAddressType = 0x0800;
2148 ArpConfigData.SwAddressLength = sizeof (EFI_IPv4_ADDRESS);
2149 ArpConfigData.StationAddress = &Private->StationIp.v4;
2150
2151 Private->Arp->Configure (Private->Arp, NULL);
2152 Private->Arp->Configure (Private->Arp, &ArpConfigData);
2153
2154 //
2155 // Update the route table.
2156 //
2157 Mode->RouteTableEntries = 1;
2158 Mode->RouteTable[0].IpAddr.Addr[0] = Private->StationIp.Addr[0] & Private->SubnetMask.Addr[0];
2159 Mode->RouteTable[0].SubnetMask.Addr[0] = Private->SubnetMask.Addr[0];
2160 Mode->RouteTable[0].GwAddr.Addr[0] = 0;
2161 }
2162
2163 return EFI_SUCCESS;
2164 }
2165
2166 /**
2167 Updates the contents of the cached DHCP and Discover packets.
2168
2169 The pointers to the new packets are used to update the contents of the cached
2170 packets in the EFI_PXE_BASE_CODE_MODE structure.
2171
2172 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
2173 @param NewDhcpDiscoverValid Pointer to a value that will replace the current
2174 DhcpDiscoverValid field.
2175 @param NewDhcpAckReceived Pointer to a value that will replace the current
2176 DhcpAckReceived field.
2177 @param NewProxyOfferReceived Pointer to a value that will replace the current
2178 ProxyOfferReceived field.
2179 @param NewPxeDiscoverValid Pointer to a value that will replace the current
2180 ProxyOfferReceived field.
2181 @param NewPxeReplyReceived Pointer to a value that will replace the current
2182 PxeReplyReceived field.
2183 @param NewPxeBisReplyReceived Pointer to a value that will replace the current
2184 PxeBisReplyReceived field.
2185 @param NewDhcpDiscover Pointer to the new cached DHCP Discover packet contents.
2186 @param NewDhcpAck Pointer to the new cached DHCP Ack packet contents.
2187 @param NewProxyOffer Pointer to the new cached Proxy Offer packet contents.
2188 @param NewPxeDiscover Pointer to the new cached PXE Discover packet contents.
2189 @param NewPxeReply Pointer to the new cached PXE Reply packet contents.
2190 @param NewPxeBisReply Pointer to the new cached PXE BIS Reply packet contents.
2191
2192 @retval EFI_SUCCESS The cached packet contents were updated.
2193 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
2194 @retval EFI_INVALID_PARAMETER This is NULL or not point to a valid EFI_PXE_BASE_CODE_PROTOCOL structure.
2195
2196 **/
2197 EFI_STATUS
2198 EFIAPI
2199 EfiPxeBcSetPackets (
2200 IN EFI_PXE_BASE_CODE_PROTOCOL * This,
2201 IN BOOLEAN * NewDhcpDiscoverValid OPTIONAL,
2202 IN BOOLEAN * NewDhcpAckReceived OPTIONAL,
2203 IN BOOLEAN * NewProxyOfferReceived OPTIONAL,
2204 IN BOOLEAN * NewPxeDiscoverValid OPTIONAL,
2205 IN BOOLEAN * NewPxeReplyReceived OPTIONAL,
2206 IN BOOLEAN * NewPxeBisReplyReceived OPTIONAL,
2207 IN EFI_PXE_BASE_CODE_PACKET * NewDhcpDiscover OPTIONAL,
2208 IN EFI_PXE_BASE_CODE_PACKET * NewDhcpAck OPTIONAL,
2209 IN EFI_PXE_BASE_CODE_PACKET * NewProxyOffer OPTIONAL,
2210 IN EFI_PXE_BASE_CODE_PACKET * NewPxeDiscover OPTIONAL,
2211 IN EFI_PXE_BASE_CODE_PACKET * NewPxeReply OPTIONAL,
2212 IN EFI_PXE_BASE_CODE_PACKET * NewPxeBisReply OPTIONAL
2213 )
2214 {
2215 PXEBC_PRIVATE_DATA *Private;
2216 EFI_PXE_BASE_CODE_MODE *Mode;
2217
2218 if (This == NULL) {
2219 return EFI_INVALID_PARAMETER;
2220 }
2221
2222 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
2223 Mode = Private->PxeBc.Mode;
2224
2225 if (!Mode->Started) {
2226 return EFI_NOT_STARTED;
2227 }
2228
2229 Private->FileSize = 0;
2230
2231 if (NewDhcpDiscoverValid != NULL) {
2232 Mode->DhcpDiscoverValid = *NewDhcpDiscoverValid;
2233 }
2234
2235 if (NewDhcpAckReceived != NULL) {
2236 Mode->DhcpAckReceived = *NewDhcpAckReceived;
2237 }
2238
2239 if (NewProxyOfferReceived != NULL) {
2240 Mode->ProxyOfferReceived = *NewProxyOfferReceived;
2241 }
2242
2243 if (NewPxeDiscoverValid != NULL) {
2244 Mode->PxeDiscoverValid = *NewPxeDiscoverValid;
2245 }
2246
2247 if (NewPxeReplyReceived != NULL) {
2248 Mode->PxeReplyReceived = *NewPxeReplyReceived;
2249 }
2250
2251 if (NewPxeBisReplyReceived != NULL) {
2252 Mode->PxeBisReplyReceived = *NewPxeBisReplyReceived;
2253 }
2254
2255 if (NewDhcpDiscover != NULL) {
2256 CopyMem (&Mode->DhcpDiscover, NewDhcpDiscover, sizeof (EFI_PXE_BASE_CODE_PACKET));
2257 }
2258
2259 if (NewDhcpAck != NULL) {
2260 CopyMem (&Mode->DhcpAck, NewDhcpAck, sizeof (EFI_PXE_BASE_CODE_PACKET));
2261 }
2262
2263 if (NewProxyOffer != NULL) {
2264 CopyMem (&Mode->ProxyOffer, NewProxyOffer, sizeof (EFI_PXE_BASE_CODE_PACKET));
2265 }
2266
2267 if (NewPxeDiscover != NULL) {
2268 CopyMem (&Mode->PxeDiscover, NewPxeDiscover, sizeof (EFI_PXE_BASE_CODE_PACKET));
2269 }
2270
2271 if (NewPxeReply != NULL) {
2272 CopyMem (&Mode->PxeReply, NewPxeReply, sizeof (EFI_PXE_BASE_CODE_PACKET));
2273 }
2274
2275 if (NewPxeBisReply != NULL) {
2276 CopyMem (&Mode->PxeBisReply, NewPxeBisReply, sizeof (EFI_PXE_BASE_CODE_PACKET));
2277 }
2278
2279 return EFI_SUCCESS;
2280 }
2281
2282 EFI_PXE_BASE_CODE_PROTOCOL mPxeBcProtocolTemplate = {
2283 EFI_PXE_BASE_CODE_PROTOCOL_REVISION,
2284 EfiPxeBcStart,
2285 EfiPxeBcStop,
2286 EfiPxeBcDhcp,
2287 EfiPxeBcDiscover,
2288 EfiPxeBcMtftp,
2289 EfiPxeBcUdpWrite,
2290 EfiPxeBcUdpRead,
2291 EfiPxeBcSetIpFilter,
2292 EfiPxeBcArp,
2293 EfiPxeBcSetParameters,
2294 EfiPxeBcSetStationIP,
2295 EfiPxeBcSetPackets,
2296 NULL
2297 };
2298
2299 /**
2300 Callback function that is invoked when the PXE Base Code Protocol is about to transmit, has
2301 received, or is waiting to receive a packet.
2302
2303 This function is invoked when the PXE Base Code Protocol is about to transmit, has received,
2304 or is waiting to receive a packet. Parameters Function and Received specify the type of event.
2305 Parameters PacketLen and Packet specify the packet that generated the event. If these fields
2306 are zero and NULL respectively, then this is a status update callback. If the operation specified
2307 by Function is to continue, then CALLBACK_STATUS_CONTINUE should be returned. If the operation
2308 specified by Function should be aborted, then CALLBACK_STATUS_ABORT should be returned. Due to
2309 the polling nature of UEFI device drivers, a callback function should not execute for more than 5 ms.
2310 The SetParameters() function must be called after a Callback Protocol is installed to enable the
2311 use of callbacks.
2312
2313 @param This Pointer to the EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL instance.
2314 @param Function The PXE Base Code Protocol function that is waiting for an event.
2315 @param Received TRUE if the callback is being invoked due to a receive event. FALSE if
2316 the callback is being invoked due to a transmit event.
2317 @param PacketLength The length, in bytes, of Packet. This field will have a value of zero if
2318 this is a wait for receive event.
2319 @param PacketPtr If Received is TRUE, a pointer to the packet that was just received;
2320 otherwise a pointer to the packet that is about to be transmitted.
2321
2322 @retval EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE if Function specifies a continue operation
2323 @retval EFI_PXE_BASE_CODE_CALLBACK_STATUS_ABORT if Function specifies an abort operation
2324
2325 **/
2326 EFI_PXE_BASE_CODE_CALLBACK_STATUS
2327 EFIAPI
2328 EfiPxeLoadFileCallback (
2329 IN EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL * This,
2330 IN EFI_PXE_BASE_CODE_FUNCTION Function,
2331 IN BOOLEAN Received,
2332 IN UINT32 PacketLength,
2333 IN EFI_PXE_BASE_CODE_PACKET * PacketPtr OPTIONAL
2334 )
2335 {
2336 EFI_INPUT_KEY Key;
2337 EFI_STATUS Status;
2338
2339 //
2340 // Catch Ctrl-C or ESC to abort.
2341 //
2342 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
2343
2344 if (!EFI_ERROR (Status)) {
2345
2346 if (Key.ScanCode == SCAN_ESC || Key.UnicodeChar == (0x1F & 'c')) {
2347
2348 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_ABORT;
2349 }
2350 }
2351 //
2352 // No print if receive packet
2353 //
2354 if (Received) {
2355 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE;
2356 }
2357 //
2358 // Print only for three functions
2359 //
2360 switch (Function) {
2361
2362 case EFI_PXE_BASE_CODE_FUNCTION_MTFTP:
2363 //
2364 // Print only for open MTFTP packets, not every MTFTP packets
2365 //
2366 if (PacketLength != 0 && PacketPtr != NULL) {
2367 if (PacketPtr->Raw[0x1C] != 0x00 || PacketPtr->Raw[0x1D] != 0x01) {
2368 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE;
2369 }
2370 }
2371 break;
2372
2373 case EFI_PXE_BASE_CODE_FUNCTION_DHCP:
2374 case EFI_PXE_BASE_CODE_FUNCTION_DISCOVER:
2375 break;
2376
2377 default:
2378 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE;
2379 }
2380
2381 if (PacketLength != 0 && PacketPtr != NULL) {
2382 //
2383 // Print '.' when transmit a packet
2384 //
2385 AsciiPrint (".");
2386
2387 }
2388
2389 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE;
2390 }
2391
2392 EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL mPxeBcCallBackTemplate = {
2393 EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL_REVISION,
2394 EfiPxeLoadFileCallback
2395 };
2396
2397
2398 /**
2399 Find the boot file.
2400
2401 @param Private Pointer to PxeBc private data.
2402 @param BufferSize Pointer to buffer size.
2403 @param Buffer Pointer to buffer.
2404
2405 @retval EFI_SUCCESS Discover the boot file successfully.
2406 @retval EFI_TIMEOUT The TFTP/MTFTP operation timed out.
2407 @retval EFI_ABORTED PXE bootstrap server, so local boot need abort.
2408 @retval EFI_BUFFER_TOO_SMALL The buffer is too small to load the boot file.
2409
2410 **/
2411 EFI_STATUS
2412 DiscoverBootFile (
2413 IN PXEBC_PRIVATE_DATA *Private,
2414 IN OUT UINT64 *BufferSize,
2415 IN VOID *Buffer
2416 )
2417 {
2418 EFI_PXE_BASE_CODE_PROTOCOL *PxeBc;
2419 EFI_PXE_BASE_CODE_MODE *Mode;
2420 EFI_STATUS Status;
2421 UINT16 Type;
2422 UINT16 Layer;
2423 BOOLEAN UseBis;
2424 UINTN BlockSize;
2425 PXEBC_CACHED_DHCP4_PACKET *Packet;
2426 UINT16 Value;
2427
2428 PxeBc = &Private->PxeBc;
2429 Mode = PxeBc->Mode;
2430 Type = EFI_PXE_BASE_CODE_BOOT_TYPE_BOOTSTRAP;
2431 Layer = EFI_PXE_BASE_CODE_BOOT_LAYER_INITIAL;
2432
2433 //
2434 // do DHCP.
2435 //
2436 Status = PxeBc->Dhcp (PxeBc, TRUE);
2437 if (EFI_ERROR (Status)) {
2438 return Status;
2439 }
2440
2441 //
2442 // Select a boot server
2443 //
2444 Status = PxeBcSelectBootPrompt (Private);
2445
2446 if (Status == EFI_SUCCESS) {
2447 Status = PxeBcSelectBootMenu (Private, &Type, TRUE);
2448 } else if (Status == EFI_TIMEOUT) {
2449 Status = PxeBcSelectBootMenu (Private, &Type, FALSE);
2450 }
2451
2452 if (!EFI_ERROR (Status)) {
2453
2454 if (Type == EFI_PXE_BASE_CODE_BOOT_TYPE_BOOTSTRAP) {
2455 //
2456 // Local boot(PXE bootstrap server) need abort
2457 //
2458 return EFI_ABORTED;
2459 }
2460
2461 UseBis = (BOOLEAN) (Mode->BisSupported && Mode->BisDetected);
2462 Status = PxeBc->Discover (PxeBc, Type, &Layer, UseBis, NULL);
2463 if (EFI_ERROR (Status)) {
2464 return Status;
2465 }
2466 }
2467
2468 *BufferSize = 0;
2469 BlockSize = 0x8000;
2470
2471 //
2472 // Get bootfile name and (m)tftp server ip addresss
2473 //
2474 if (Mode->PxeReplyReceived) {
2475 Packet = &Private->PxeReply;
2476 } else if (Mode->ProxyOfferReceived) {
2477 Packet = &Private->ProxyOffer;
2478 } else {
2479 Packet = &Private->Dhcp4Ack;
2480 }
2481
2482 //
2483 // Use siaddr(next server) in DHCPOFFER packet header, if zero, use option 54(server identifier)
2484 // in DHCPOFFER packet.
2485 // (It does not comply with PXE Spec, Ver2.1)
2486 //
2487 if (EFI_IP4_EQUAL (&Packet->Packet.Offer.Dhcp4.Header.ServerAddr, &mZeroIp4Addr)) {
2488 CopyMem (
2489 &Private->ServerIp,
2490 Packet->Dhcp4Option[PXEBC_DHCP4_TAG_INDEX_SERVER_ID]->Data,
2491 sizeof (EFI_IPv4_ADDRESS)
2492 );
2493 } else {
2494 CopyMem (
2495 &Private->ServerIp,
2496 &Packet->Packet.Offer.Dhcp4.Header.ServerAddr,
2497 sizeof (EFI_IPv4_ADDRESS)
2498 );
2499 }
2500 if (Private->ServerIp.Addr[0] == 0) {
2501 return EFI_DEVICE_ERROR;
2502 }
2503
2504 ASSERT (Packet->Dhcp4Option[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] != NULL);
2505
2506 //
2507 // bootlfile name
2508 //
2509 Private->BootFileName = (CHAR8 *) (Packet->Dhcp4Option[PXEBC_DHCP4_TAG_INDEX_BOOTFILE]->Data);
2510
2511 if (Packet->Dhcp4Option[PXEBC_DHCP4_TAG_INDEX_BOOTFILE_LEN] != NULL) {
2512 //
2513 // Already have the bootfile length option, compute the file size
2514 //
2515 CopyMem (&Value, Packet->Dhcp4Option[PXEBC_DHCP4_TAG_INDEX_BOOTFILE_LEN]->Data, sizeof (Value));
2516 Value = NTOHS (Value);
2517 *BufferSize = 512 * Value;
2518 Status = EFI_BUFFER_TOO_SMALL;
2519 } else {
2520 //
2521 // Get the bootfile size from tftp
2522 //
2523 Status = PxeBc->Mtftp (
2524 PxeBc,
2525 EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE,
2526 Buffer,
2527 FALSE,
2528 BufferSize,
2529 &BlockSize,
2530 &Private->ServerIp,
2531 (UINT8 *) Private->BootFileName,
2532 NULL,
2533 FALSE
2534 );
2535 }
2536
2537 Private->FileSize = (UINTN) *BufferSize;
2538
2539 return Status;
2540 }
2541
2542 /**
2543 Causes the driver to load a specified file.
2544
2545 @param This Protocol instance pointer.
2546 @param FilePath The device specific path of the file to load.
2547 @param BootPolicy If TRUE, indicates that the request originates from the
2548 boot manager is attempting to load FilePath as a boot
2549 selection. If FALSE, then FilePath must match as exact file
2550 to be loaded.
2551 @param BufferSize On input the size of Buffer in bytes. On output with a return
2552 code of EFI_SUCCESS, the amount of data transferred to
2553 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
2554 the size of Buffer required to retrieve the requested file.
2555 @param Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
2556 then no the size of the requested file is returned in
2557 BufferSize.
2558
2559 @retval EFI_SUCCESS The file was loaded.
2560 @retval EFI_UNSUPPORTED The device does not support the provided BootPolicy
2561 @retval EFI_INVALID_PARAMETER FilePath is not a valid device path, or
2562 BufferSize is NULL.
2563 @retval EFI_NO_MEDIA No medium was present to load the file.
2564 @retval EFI_DEVICE_ERROR The file was not loaded due to a device error.
2565 @retval EFI_NO_RESPONSE The remote system did not respond.
2566 @retval EFI_NOT_FOUND The file was not found.
2567 @retval EFI_ABORTED The file load process was manually cancelled.
2568
2569 **/
2570 EFI_STATUS
2571 EFIAPI
2572 EfiPxeLoadFile (
2573 IN EFI_LOAD_FILE_PROTOCOL * This,
2574 IN EFI_DEVICE_PATH_PROTOCOL * FilePath,
2575 IN BOOLEAN BootPolicy,
2576 IN OUT UINTN *BufferSize,
2577 IN VOID *Buffer OPTIONAL
2578 )
2579 {
2580 PXEBC_PRIVATE_DATA *Private;
2581 EFI_PXE_BASE_CODE_PROTOCOL *PxeBc;
2582 BOOLEAN NewMakeCallback;
2583 UINTN BlockSize;
2584 EFI_STATUS Status;
2585 UINT64 TmpBufSize;
2586
2587 Private = PXEBC_PRIVATE_DATA_FROM_LOADFILE (This);
2588 PxeBc = &Private->PxeBc;
2589 NewMakeCallback = FALSE;
2590 BlockSize = 0x8000;
2591 Status = EFI_DEVICE_ERROR;
2592
2593 if (This == NULL || BufferSize == NULL) {
2594
2595 return EFI_INVALID_PARAMETER;
2596 }
2597
2598 //
2599 // Only support BootPolicy
2600 //
2601 if (!BootPolicy) {
2602 return EFI_UNSUPPORTED;
2603 }
2604
2605 Status = PxeBc->Start (PxeBc, FALSE);
2606 if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {
2607 return Status;
2608 }
2609
2610 Status = gBS->HandleProtocol (
2611 Private->Controller,
2612 &gEfiPxeBaseCodeCallbackProtocolGuid,
2613 (VOID **) &Private->PxeBcCallback
2614 );
2615 if (Status == EFI_UNSUPPORTED) {
2616
2617 CopyMem (&Private->LoadFileCallback, &mPxeBcCallBackTemplate, sizeof (Private->LoadFileCallback));
2618
2619 Status = gBS->InstallProtocolInterface (
2620 &Private->Controller,
2621 &gEfiPxeBaseCodeCallbackProtocolGuid,
2622 EFI_NATIVE_INTERFACE,
2623 &Private->LoadFileCallback
2624 );
2625
2626 NewMakeCallback = (BOOLEAN) (Status == EFI_SUCCESS);
2627
2628 Status = PxeBc->SetParameters (PxeBc, NULL, NULL, NULL, NULL, &NewMakeCallback);
2629 if (EFI_ERROR (Status)) {
2630 PxeBc->Stop (PxeBc);
2631 return Status;
2632 }
2633 }
2634
2635 if (Private->FileSize == 0) {
2636 TmpBufSize = 0;
2637 Status = DiscoverBootFile (Private, &TmpBufSize, Buffer);
2638
2639 if (sizeof (UINTN) < sizeof (UINT64) && (TmpBufSize > 0xFFFFFFFF)) {
2640 Status = EFI_DEVICE_ERROR;
2641 } else if (TmpBufSize > 0 && *BufferSize >= (UINTN) TmpBufSize && Buffer != NULL) {
2642 *BufferSize = (UINTN) TmpBufSize;
2643 Status = PxeBc->Mtftp (
2644 PxeBc,
2645 EFI_PXE_BASE_CODE_TFTP_READ_FILE,
2646 Buffer,
2647 FALSE,
2648 &TmpBufSize,
2649 &BlockSize,
2650 &Private->ServerIp,
2651 (UINT8 *) Private->BootFileName,
2652 NULL,
2653 FALSE
2654 );
2655 } else if (TmpBufSize > 0) {
2656 *BufferSize = (UINTN) TmpBufSize;
2657 Status = EFI_BUFFER_TOO_SMALL;
2658 }
2659 } else if (Buffer == NULL || Private->FileSize > *BufferSize) {
2660 *BufferSize = Private->FileSize;
2661 Status = EFI_BUFFER_TOO_SMALL;
2662 } else {
2663 //
2664 // Download the file.
2665 //
2666 TmpBufSize = (UINT64) (*BufferSize);
2667 Status = PxeBc->Mtftp (
2668 PxeBc,
2669 EFI_PXE_BASE_CODE_TFTP_READ_FILE,
2670 Buffer,
2671 FALSE,
2672 &TmpBufSize,
2673 &BlockSize,
2674 &Private->ServerIp,
2675 (UINT8 *) Private->BootFileName,
2676 NULL,
2677 FALSE
2678 );
2679 }
2680 //
2681 // If we added a callback protocol, now is the time to remove it.
2682 //
2683 if (NewMakeCallback) {
2684
2685 NewMakeCallback = FALSE;
2686
2687 PxeBc->SetParameters (PxeBc, NULL, NULL, NULL, NULL, &NewMakeCallback);
2688
2689 gBS->UninstallProtocolInterface (
2690 Private->Controller,
2691 &gEfiPxeBaseCodeCallbackProtocolGuid,
2692 &Private->LoadFileCallback
2693 );
2694 }
2695
2696 //
2697 // Check download status
2698 //
2699 if (Status == EFI_SUCCESS) {
2700 return EFI_SUCCESS;
2701
2702 } else if (Status == EFI_BUFFER_TOO_SMALL) {
2703 if (Buffer != NULL) {
2704 AsciiPrint ("PXE-E05: Download buffer is smaller than requested file.\n");
2705 } else {
2706 return Status;
2707 }
2708
2709 } else if (Status == EFI_DEVICE_ERROR) {
2710 AsciiPrint ("PXE-E07: Network device error.\n");
2711
2712 } else if (Status == EFI_OUT_OF_RESOURCES) {
2713 AsciiPrint ("PXE-E09: Could not allocate I/O buffers.\n");
2714
2715 } else if (Status == EFI_NO_MEDIA) {
2716 AsciiPrint ("PXE-E12: Could not detect network connection.\n");
2717
2718 } else if (Status == EFI_NO_RESPONSE) {
2719 AsciiPrint ("PXE-E16: No offer received.\n");
2720
2721 } else if (Status == EFI_TIMEOUT) {
2722 AsciiPrint ("PXE-E18: Server response timeout.\n");
2723
2724 } else if (Status == EFI_ABORTED) {
2725 AsciiPrint ("PXE-E21: Remote boot cancelled.\n");
2726
2727 } else if (Status == EFI_ICMP_ERROR) {
2728 AsciiPrint ("PXE-E22: Client received ICMP error from server.\n");
2729
2730 } else if (Status == EFI_TFTP_ERROR) {
2731 AsciiPrint ("PXE-E23: Client received TFTP error from server.\n");
2732
2733 } else {
2734 AsciiPrint ("PXE-E99: Unexpected network error.\n");
2735 }
2736
2737 PxeBc->Stop (PxeBc);
2738
2739 return Status;
2740 }
2741
2742 EFI_LOAD_FILE_PROTOCOL mLoadFileProtocolTemplate = { EfiPxeLoadFile };
2743