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