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