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