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