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