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