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