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