add security check.
[edk2.git] / MdeModulePkg / Universal / Network / UefiPxeBcDxe / PxeBcImpl.c
blob4a91f1c3d16d7862186fc7864eed07b12dbd8459
1 /** @file
2 Interface routines for PxeBc.
4 Copyright (c) 2007 - 2009, 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
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.
13 **/
16 #include "PxeBcImpl.h"
18 UINT32 mPxeDhcpTimeout[4] = { 4, 8, 16, 32 };
20 /**
21 Get and record the arp cache.
23 @param This Pointer to EFI_PXE_BC_PROTOCOL
25 @retval EFI_SUCCESS Arp cache updated successfully
26 @retval others If error occurs when getting arp cache
28 **/
29 EFI_STATUS
30 UpdateArpCache (
31 IN EFI_PXE_BASE_CODE_PROTOCOL * This
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;
42 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
43 Mode = Private->PxeBc.Mode;
45 Status = Private->Arp->Find (
46 Private->Arp,
47 TRUE,
48 NULL,
49 &EntryLength,
50 &EntryCount,
51 &Entries,
52 TRUE
54 if (EFI_ERROR (Status)) {
55 return Status;
58 Mode->ArpCacheEntries = MIN (
59 EntryCount,
60 EFI_PXE_BASE_CODE_MAX_ARP_ENTRIES
62 for (Index = 0; Index < Mode->ArpCacheEntries; Index ++) {
63 CopyMem (
64 &Mode->ArpCache[Index].IpAddr,
65 Entries + 1,
66 Entries->SwAddressLength
68 CopyMem (
69 &Mode->ArpCache[Index].MacAddr,
70 (UINT8 *) (Entries + 1) + Entries->SwAddressLength,
71 Entries->HwAddressLength
74 // Slip to the next FindData.
76 Entries = (EFI_ARP_FIND_DATA *) ((UINT8 *) Entries + EntryLength);
79 return EFI_SUCCESS;
82 /**
83 Timeout routine to update arp cache.
85 @param Event Pointer to EFI_PXE_BC_PROTOCOL
86 @param Context Context of the timer event
88 **/
89 VOID
90 EFIAPI
91 ArpCacheUpdateTimeout (
92 IN EFI_EVENT Event,
93 IN VOID *Context
96 UpdateArpCache ((EFI_PXE_BASE_CODE_PROTOCOL *) Context);
99 /**
100 Do arp resolution from arp cache in PxeBcMode.
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.
107 @retval TRUE The resolution is successful.
108 @retval FALSE Otherwise.
111 BOOLEAN
112 FindInArpCache (
113 IN EFI_PXE_BASE_CODE_MODE *PxeBcMode,
114 IN EFI_IPv4_ADDRESS *Ip4Addr,
115 OUT EFI_MAC_ADDRESS *MacAddress
118 UINT32 Index;
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)
127 return TRUE;
131 return FALSE;
135 Notify function for the ICMP receive token, used to process
136 the received ICMP packets.
138 @param Context The PXEBC private data.
141 VOID
142 EFIAPI
143 IcmpErrorListenHandlerDpc (
144 IN VOID *Context
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;
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;
162 if (Status == EFI_ABORTED) {
164 // The reception is actively aborted by the consumer, directly return.
166 return;
169 if (EFI_ERROR (Status) || (RxData == NULL)) {
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).
175 goto Resume;
178 if (EFI_IP4 (RxData->Header->SourceAddress) != 0 &&
179 !Ip4IsUnicast (EFI_NTOHL (RxData->Header->SourceAddress), 0)) {
181 // The source address is not zero and it's not a unicast IP address, discard it.
183 goto CleanUp;
186 if (!EFI_IP4_EQUAL (&RxData->Header->DestinationAddress, &Mode->StationIp.v4)) {
188 // The dest address is not equal to Station Ip address, discard it.
190 goto CleanUp;
194 // Constructor ICMP error packet
196 CopiedLen = 0;
197 CopiedPointer = (UINT8 *) &Mode->IcmpError;
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
207 } else {
208 CopyMem (
209 CopiedPointer,
210 RxData->FragmentTable[Index].FragmentBuffer,
211 CopiedLen - sizeof (EFI_PXE_BASE_CODE_ICMP_ERROR)
214 CopiedPointer += CopiedLen;
217 goto Resume;
219 CleanUp:
220 gBS->SignalEvent (RxData->RecycleSignal);
222 Resume:
223 Ip4->Receive (Ip4, &(Private->IcmpErrorRcvToken));
227 Request IcmpErrorListenHandlerDpc as a DPC at TPL_CALLBACK
229 @param Event The event signaled.
230 @param Context The context passed in by the event notifier.
233 VOID
234 EFIAPI
235 IcmpErrorListenHandler (
236 IN EFI_EVENT Event,
237 IN VOID *Context
241 // Request IpIoListenHandlerDpc as a DPC at TPL_CALLBACK
243 NetLibQueueDpc (TPL_CALLBACK, IcmpErrorListenHandlerDpc, Context);
246 /**
247 Enables the use of the PXE Base Code Protocol functions.
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.
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.
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.
310 EFI_STATUS
311 EFIAPI
312 EfiPxeBcStart (
313 IN EFI_PXE_BASE_CODE_PROTOCOL *This,
314 IN BOOLEAN UseIpv6
317 PXEBC_PRIVATE_DATA *Private;
318 EFI_PXE_BASE_CODE_MODE *Mode;
319 EFI_STATUS Status;
321 if (This == NULL) {
322 return EFI_INVALID_PARAMETER;
325 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
326 Mode = Private->PxeBc.Mode;
328 if (Mode->Started) {
329 return EFI_ALREADY_STARTED;
332 if (UseIpv6) {
334 // IPv6 is not supported now.
336 return EFI_UNSUPPORTED;
340 // Configure the udp4 instance to let it receive data
342 Status = Private->Udp4Read->Configure (
343 Private->Udp4Read,
344 &Private->Udp4CfgData
346 if (EFI_ERROR (Status)) {
347 return Status;
350 Private->AddressIsOk = FALSE;
352 ZeroMem (Mode, sizeof (EFI_PXE_BASE_CODE_MODE));
354 Mode->Started = TRUE;
355 Mode->TTL = DEFAULT_TTL;
356 Mode->ToS = DEFAULT_ToS;
357 Mode->AutoArp = TRUE;
360 // Create the event for Arp Cache checking.
362 Status = gBS->CreateEvent (
363 EVT_TIMER | EVT_NOTIFY_SIGNAL,
364 TPL_CALLBACK,
365 ArpCacheUpdateTimeout,
366 This,
367 &Private->GetArpCacheEvent
369 if (EFI_ERROR (Status)) {
370 goto ON_EXIT;
374 // Start the timeout timer event.
376 Status = gBS->SetTimer (
377 Private->GetArpCacheEvent,
378 TimerPeriodic,
379 TICKS_PER_SECOND
382 if (EFI_ERROR (Status)) {
383 goto ON_EXIT;
387 // Create ICMP error receiving event
389 Status = gBS->CreateEvent (
390 EVT_NOTIFY_SIGNAL,
391 TPL_NOTIFY,
392 IcmpErrorListenHandler,
393 Private,
394 &(Private->IcmpErrorRcvToken.Event)
396 if (EFI_ERROR (Status)) {
397 goto ON_EXIT;
400 Status = Private->Ip4->Configure (Private->Ip4, &Private->Ip4ConfigData);
401 if (EFI_ERROR (Status)) {
402 goto ON_EXIT;
406 // start to listen incoming packet
408 Status = Private->Ip4->Receive (Private->Ip4, &Private->IcmpErrorRcvToken);
409 if (!EFI_ERROR (Status)) {
410 return Status;
413 ON_EXIT:
414 Private->Ip4->Configure (Private->Ip4, NULL);
416 if (Private->IcmpErrorRcvToken.Event != NULL) {
417 gBS->CloseEvent (Private->IcmpErrorRcvToken.Event);
420 if (Private->GetArpCacheEvent != NULL) {
421 gBS->SetTimer (Private->GetArpCacheEvent, TimerCancel, 0);
422 gBS->CloseEvent (Private->GetArpCacheEvent);
425 Mode->Started = FALSE;
426 Mode->TTL = 0;
427 Mode->ToS = 0;
428 Mode->AutoArp = FALSE;
430 return Status;
434 /**
435 Disables the use of the PXE Base Code Protocol functions.
437 This function stops all activity on the network device. All the resources allocated
438 in Start() are released, the Started field of the EFI_PXE_BASE_CODE_MODE structure is
439 set to FALSE and EFI_SUCCESS is returned. If the Started field of the EFI_PXE_BASE_CODE_MODE
440 structure is already FALSE, then EFI_NOT_STARTED will be returned.
442 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
444 @retval EFI_SUCCESS The PXE Base Code Protocol was stopped.
445 @retval EFI_NOT_STARTED The PXE Base Code Protocol is already in the stopped state.
446 @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid
447 EFI_PXE_BASE_CODE_PROTOCOL structure.
448 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
451 EFI_STATUS
452 EFIAPI
453 EfiPxeBcStop (
454 IN EFI_PXE_BASE_CODE_PROTOCOL *This
457 PXEBC_PRIVATE_DATA *Private;
458 EFI_PXE_BASE_CODE_MODE *Mode;
460 if (This == NULL) {
461 return EFI_INVALID_PARAMETER;
464 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
465 Mode = Private->PxeBc.Mode;
467 if (!Mode->Started) {
468 return EFI_NOT_STARTED;
471 Private->Ip4->Cancel (Private->Ip4, NULL);
473 // Dispatch the DPCs queued by the NotifyFunction of the canceled rx token's
474 // events.
476 NetLibDispatchDpc ();
478 Private->Ip4->Configure (Private->Ip4, NULL);
481 // Close the ICMP error receiving event.
483 gBS->CloseEvent (Private->IcmpErrorRcvToken.Event);
486 // Cancel the TimeoutEvent timer.
488 gBS->SetTimer (Private->GetArpCacheEvent, TimerCancel, 0);
491 // Close the TimeoutEvent event.
493 gBS->CloseEvent (Private->GetArpCacheEvent);
495 Mode->Started = FALSE;
497 Private->CurrentUdpSrcPort = 0;
498 Private->Udp4Write->Configure (Private->Udp4Write, NULL);
499 Private->Udp4Read->Groups (Private->Udp4Read, FALSE, NULL);
500 Private->Udp4Read->Configure (Private->Udp4Read, NULL);
502 Private->Dhcp4->Stop (Private->Dhcp4);
503 Private->Dhcp4->Configure (Private->Dhcp4, NULL);
505 Private->FileSize = 0;
507 return EFI_SUCCESS;
511 /**
512 Attempts to complete a DHCPv4 D.O.R.A. (discover / offer / request / acknowledge) or DHCPv6
513 S.A.R.R (solicit / advertise / request / reply) sequence.
515 This function attempts to complete the DHCP sequence. If this sequence is completed,
516 then EFI_SUCCESS is returned, and the DhcpCompleted, ProxyOfferReceived, StationIp,
517 SubnetMask, DhcpDiscover, DhcpAck, and ProxyOffer fields of the EFI_PXE_BASE_CODE_MODE
518 structure are filled in.
519 If SortOffers is TRUE, then the cached DHCP offer packets will be sorted before
520 they are tried. If SortOffers is FALSE, then the cached DHCP offer packets will
521 be tried in the order in which they are received. Please see the Preboot Execution
522 Environment (PXE) Specification for additional details on the implementation of DHCP.
523 This function can take at least 31 seconds to timeout and return control to the
524 caller. If the DHCP sequence does not complete, then EFI_TIMEOUT will be returned.
525 If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,
526 then the DHCP sequence will be stopped and EFI_ABORTED will be returned.
528 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
529 @param SortOffers TRUE if the offers received should be sorted. Set to FALSE to try the
530 offers in the order that they are received.
532 @retval EFI_SUCCESS Valid DHCP has completed.
533 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
534 @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid
535 EFI_PXE_BASE_CODE_PROTOCOL structure.
536 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
537 @retval EFI_OUT_OF_RESOURCES Could not allocate enough memory to complete the DHCP Protocol.
538 @retval EFI_ABORTED The callback function aborted the DHCP Protocol.
539 @retval EFI_TIMEOUT The DHCP Protocol timed out.
540 @retval EFI_ICMP_ERROR An ICMP error packet was received during the DHCP session.
541 @retval EFI_NO_RESPONSE Valid PXE offer was not received.
544 EFI_STATUS
545 EFIAPI
546 EfiPxeBcDhcp (
547 IN EFI_PXE_BASE_CODE_PROTOCOL *This,
548 IN BOOLEAN SortOffers
551 PXEBC_PRIVATE_DATA *Private;
552 EFI_PXE_BASE_CODE_MODE *Mode;
553 EFI_DHCP4_PROTOCOL *Dhcp4;
554 EFI_DHCP4_CONFIG_DATA Dhcp4CfgData;
555 EFI_DHCP4_MODE_DATA Dhcp4Mode;
556 EFI_DHCP4_PACKET_OPTION *OptList[PXEBC_DHCP4_MAX_OPTION_NUM];
557 UINT32 OptCount;
558 EFI_STATUS Status;
559 EFI_ARP_CONFIG_DATA ArpConfigData;
561 if (This == NULL) {
562 return EFI_INVALID_PARAMETER;
565 Status = EFI_SUCCESS;
566 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
567 Mode = Private->PxeBc.Mode;
568 Dhcp4 = Private->Dhcp4;
569 Private->Function = EFI_PXE_BASE_CODE_FUNCTION_DHCP;
570 Private->SortOffers = SortOffers;
572 if (!Mode->Started) {
573 return EFI_NOT_STARTED;
576 Mode->IcmpErrorReceived = FALSE;
579 // Initialize the DHCP options and build the option list
581 OptCount = PxeBcBuildDhcpOptions (Private, OptList, TRUE);
584 // Set the DHCP4 config data.
585 // The four discovery timeouts are 4, 8, 16, 32 seconds respectively.
587 ZeroMem (&Dhcp4CfgData, sizeof (EFI_DHCP4_CONFIG_DATA));
588 Dhcp4CfgData.OptionCount = OptCount;
589 Dhcp4CfgData.OptionList = OptList;
590 Dhcp4CfgData.Dhcp4Callback = PxeBcDhcpCallBack;
591 Dhcp4CfgData.CallbackContext = Private;
592 Dhcp4CfgData.DiscoverTryCount = 4;
593 Dhcp4CfgData.DiscoverTimeout = mPxeDhcpTimeout;
595 Status = Dhcp4->Configure (Dhcp4, &Dhcp4CfgData);
596 if (EFI_ERROR (Status)) {
597 goto ON_EXIT;
601 // Zero those arrays to record the varies numbers of DHCP OFFERS.
603 Private->GotProxyOffer = FALSE;
604 Private->NumOffers = 0;
605 Private->BootpIndex = 0;
606 ZeroMem (Private->ServerCount, sizeof (Private->ServerCount));
607 ZeroMem (Private->ProxyIndex, sizeof (Private->ProxyIndex));
609 Status = Dhcp4->Start (Dhcp4, NULL);
610 if (EFI_ERROR (Status)) {
611 if (Status == EFI_ICMP_ERROR) {
612 Mode->IcmpErrorReceived = TRUE;
614 goto ON_EXIT;
617 Status = Dhcp4->GetModeData (Dhcp4, &Dhcp4Mode);
618 if (EFI_ERROR (Status)) {
619 goto ON_EXIT;
622 ASSERT (Dhcp4Mode.State == Dhcp4Bound);
624 CopyMem (&Private->StationIp, &Dhcp4Mode.ClientAddress, sizeof (EFI_IPv4_ADDRESS));
625 CopyMem (&Private->SubnetMask, &Dhcp4Mode.SubnetMask, sizeof (EFI_IPv4_ADDRESS));
626 CopyMem (&Private->GatewayIp, &Dhcp4Mode.RouterAddress, sizeof (EFI_IPv4_ADDRESS));
628 CopyMem (&Mode->StationIp, &Private->StationIp, sizeof (EFI_IPv4_ADDRESS));
629 CopyMem (&Mode->SubnetMask, &Private->SubnetMask, sizeof (EFI_IPv4_ADDRESS));
632 // Check the selected offer to see whether BINL is required, if no or BINL is
633 // finished, set the various Mode members.
635 Status = PxeBcCheckSelectedOffer (Private);
636 if (!EFI_ERROR (Status)) {
637 goto ON_EXIT;
640 ON_EXIT:
641 if (EFI_ERROR (Status)) {
642 Dhcp4->Stop (Dhcp4);
643 Dhcp4->Configure (Dhcp4, NULL);
644 } else {
646 // Remove the previously configured option list and callback function
648 ZeroMem (&Dhcp4CfgData, sizeof (EFI_DHCP4_CONFIG_DATA));
649 Dhcp4->Configure (Dhcp4, &Dhcp4CfgData);
651 Private->AddressIsOk = TRUE;
653 if (!Mode->UsingIpv6) {
655 // If in IPv4 mode, configure the corresponding ARP with this new
656 // station IP address.
658 ZeroMem (&ArpConfigData, sizeof (EFI_ARP_CONFIG_DATA));
660 ArpConfigData.SwAddressType = 0x0800;
661 ArpConfigData.SwAddressLength = sizeof (EFI_IPv4_ADDRESS);
662 ArpConfigData.StationAddress = &Private->StationIp.v4;
664 Private->Arp->Configure (Private->Arp, NULL);
665 Private->Arp->Configure (Private->Arp, &ArpConfigData);
668 // Updated the route table. Fill the first entry.
670 Mode->RouteTableEntries = 1;
671 Mode->RouteTable[0].IpAddr.Addr[0] = Private->StationIp.Addr[0] & Private->SubnetMask.Addr[0];
672 Mode->RouteTable[0].SubnetMask.Addr[0] = Private->SubnetMask.Addr[0];
673 Mode->RouteTable[0].GwAddr.Addr[0] = 0;
676 // Create the default route entry if there is a default router.
678 if (Private->GatewayIp.Addr[0] != 0) {
679 Mode->RouteTableEntries = 2;
680 Mode->RouteTable[1].IpAddr.Addr[0] = 0;
681 Mode->RouteTable[1].SubnetMask.Addr[0] = 0;
682 Mode->RouteTable[1].GwAddr.Addr[0] = Private->GatewayIp.Addr[0];
687 return Status;
691 /**
692 Attempts to complete the PXE Boot Server and/or boot image discovery sequence.
694 This function attempts to complete the PXE Boot Server and/or boot image discovery
695 sequence. If this sequence is completed, then EFI_SUCCESS is returned, and the
696 PxeDiscoverValid, PxeDiscover, PxeReplyReceived, and PxeReply fields of the
697 EFI_PXE_BASE_CODE_MODE structure are filled in. If UseBis is TRUE, then the
698 PxeBisReplyReceived and PxeBisReply fields of the EFI_PXE_BASE_CODE_MODE structure
699 will also be filled in. If UseBis is FALSE, then PxeBisReplyValid will be set to FALSE.
700 In the structure referenced by parameter Info, the PXE Boot Server list, SrvList[],
701 has two uses: It is the Boot Server IP address list used for unicast discovery
702 (if the UseUCast field is TRUE), and it is the list used for Boot Server verification
703 (if the MustUseList field is TRUE). Also, if the MustUseList field in that structure
704 is TRUE and the AcceptAnyResponse field in the SrvList[] array is TRUE, any Boot
705 Server reply of that type will be accepted. If the AcceptAnyResponse field is
706 FALSE, only responses from Boot Servers with matching IP addresses will be accepted.
707 This function can take at least 10 seconds to timeout and return control to the
708 caller. If the Discovery sequence does not complete, then EFI_TIMEOUT will be
709 returned. Please see the Preboot Execution Environment (PXE) Specification for
710 additional details on the implementation of the Discovery sequence.
711 If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,
712 then the Discovery sequence is stopped and EFI_ABORTED will be returned.
714 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
715 @param Type The type of bootstrap to perform.
716 @param Layer Pointer to the boot server layer number to discover, which must be
717 PXE_BOOT_LAYER_INITIAL when a new server type is being
718 discovered.
719 @param UseBis TRUE if Boot Integrity Services are to be used. FALSE otherwise.
720 @param Info Pointer to a data structure that contains additional information on the
721 type of discovery operation that is to be performed.
723 @retval EFI_SUCCESS The Discovery sequence has been completed.
724 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
725 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
726 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
727 @retval EFI_OUT_OF_RESOURCES Could not allocate enough memory to complete Discovery.
728 @retval EFI_ABORTED The callback function aborted the Discovery sequence.
729 @retval EFI_TIMEOUT The Discovery sequence timed out.
730 @retval EFI_ICMP_ERROR An ICMP error packet was received during the PXE discovery
731 session.
734 EFI_STATUS
735 EFIAPI
736 EfiPxeBcDiscover (
737 IN EFI_PXE_BASE_CODE_PROTOCOL *This,
738 IN UINT16 Type,
739 IN UINT16 *Layer,
740 IN BOOLEAN UseBis,
741 IN EFI_PXE_BASE_CODE_DISCOVER_INFO *Info OPTIONAL
744 PXEBC_PRIVATE_DATA *Private;
745 EFI_PXE_BASE_CODE_MODE *Mode;
746 EFI_PXE_BASE_CODE_DISCOVER_INFO DefaultInfo;
747 EFI_PXE_BASE_CODE_SRVLIST *SrvList;
748 EFI_PXE_BASE_CODE_SRVLIST DefaultSrvList;
749 PXEBC_CACHED_DHCP4_PACKET *Packet;
750 PXEBC_VENDOR_OPTION *VendorOpt;
751 UINT16 Index;
752 EFI_STATUS Status;
753 PXEBC_BOOT_SVR_ENTRY *BootSvrEntry;
755 if (This == NULL) {
756 return EFI_INVALID_PARAMETER;
759 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
760 Mode = Private->PxeBc.Mode;
761 BootSvrEntry = NULL;
762 SrvList = NULL;
763 Status = EFI_DEVICE_ERROR;
764 Private->Function = EFI_PXE_BASE_CODE_FUNCTION_DISCOVER;
766 if (!Private->AddressIsOk) {
767 return EFI_INVALID_PARAMETER;
770 if (!Mode->Started) {
771 return EFI_NOT_STARTED;
774 Mode->IcmpErrorReceived = FALSE;
777 // If layer isn't EFI_PXE_BASE_CODE_BOOT_LAYER_INITIAL,
778 // use the previous setting;
779 // If info isn't offered,
780 // use the cached DhcpAck and ProxyOffer packets.
782 if (*Layer != EFI_PXE_BASE_CODE_BOOT_LAYER_INITIAL) {
784 if (!Mode->PxeDiscoverValid || !Mode->PxeReplyReceived || (!Mode->PxeBisReplyReceived && UseBis)) {
786 return EFI_INVALID_PARAMETER;
789 DefaultInfo.IpCnt = 1;
790 DefaultInfo.UseUCast = TRUE;
792 DefaultSrvList.Type = Type;
793 DefaultSrvList.AcceptAnyResponse = FALSE;
794 DefaultSrvList.IpAddr.Addr[0] = Private->ServerIp.Addr[0];
796 SrvList = &DefaultSrvList;
797 Info = &DefaultInfo;
798 } else if (Info == NULL) {
800 // Create info by the cached packet before
802 Packet = (Mode->ProxyOfferReceived) ? &Private->ProxyOffer : &Private->Dhcp4Ack;
803 VendorOpt = &Packet->PxeVendorOption;
805 if (!Mode->DhcpAckReceived || !IS_VALID_DISCOVER_VENDOR_OPTION (VendorOpt->BitMap)) {
807 // Address is not acquired or no discovery options.
809 return EFI_INVALID_PARAMETER;
812 DefaultInfo.UseMCast = (BOOLEAN)!IS_DISABLE_MCAST_DISCOVER (VendorOpt->DiscoverCtrl);
813 DefaultInfo.UseBCast = (BOOLEAN)!IS_DISABLE_BCAST_DISCOVER (VendorOpt->DiscoverCtrl);
814 DefaultInfo.MustUseList = (BOOLEAN) IS_ENABLE_USE_SERVER_LIST (VendorOpt->DiscoverCtrl);
815 DefaultInfo.UseUCast = DefaultInfo.MustUseList;
817 if (DefaultInfo.UseMCast) {
819 // Get the multicast discover ip address from vendor option.
821 CopyMem (
822 &DefaultInfo.ServerMCastIp.Addr,
823 &VendorOpt->DiscoverMcastIp,
824 sizeof (EFI_IPv4_ADDRESS)
828 DefaultInfo.IpCnt = 0;
830 if (DefaultInfo.MustUseList) {
831 BootSvrEntry = VendorOpt->BootSvr;
832 Status = EFI_INVALID_PARAMETER;
834 while (((UINT8) (BootSvrEntry - VendorOpt->BootSvr)) < VendorOpt->BootSvrLen) {
836 if (BootSvrEntry->Type == HTONS (Type)) {
837 Status = EFI_SUCCESS;
838 break;
841 BootSvrEntry = GET_NEXT_BOOT_SVR_ENTRY (BootSvrEntry);
844 if (EFI_ERROR (Status)) {
845 return Status;
848 DefaultInfo.IpCnt = BootSvrEntry->IpCnt;
851 Info = &DefaultInfo;
852 } else {
854 SrvList = Info->SrvList;
856 if (!SrvList[0].AcceptAnyResponse) {
858 for (Index = 1; Index < Info->IpCnt; Index++) {
859 if (SrvList[Index].AcceptAnyResponse) {
860 break;
864 if (Index != Info->IpCnt) {
865 return EFI_INVALID_PARAMETER;
870 if ((!Info->UseUCast && !Info->UseBCast && !Info->UseMCast) || (Info->MustUseList && Info->IpCnt == 0)) {
872 return EFI_INVALID_PARAMETER;
875 // Execute discover by UniCast/BroadCast/MultiCast
877 if (Info->UseUCast) {
879 for (Index = 0; Index < Info->IpCnt; Index++) {
881 if (BootSvrEntry == NULL) {
882 Private->ServerIp.Addr[0] = SrvList[Index].IpAddr.Addr[0];
883 } else {
884 CopyMem (
885 &Private->ServerIp,
886 &BootSvrEntry->IpAddr[Index],
887 sizeof (EFI_IPv4_ADDRESS)
891 Status = PxeBcDiscvBootService (
892 Private,
893 Type,
894 Layer,
895 UseBis,
896 &SrvList[Index].IpAddr,
898 NULL,
899 TRUE,
900 &Private->PxeReply.Packet.Ack
904 } else if (Info->UseMCast) {
906 Status = PxeBcDiscvBootService (
907 Private,
908 Type,
909 Layer,
910 UseBis,
911 &Info->ServerMCastIp,
913 NULL,
914 TRUE,
915 &Private->PxeReply.Packet.Ack
918 } else if (Info->UseBCast) {
920 Status = PxeBcDiscvBootService (
921 Private,
922 Type,
923 Layer,
924 UseBis,
925 NULL,
926 Info->IpCnt,
927 SrvList,
928 TRUE,
929 &Private->PxeReply.Packet.Ack
933 if (EFI_ERROR (Status) || !Mode->PxeReplyReceived || (!Mode->PxeBisReplyReceived && UseBis)) {
934 if (Status == EFI_ICMP_ERROR) {
935 Mode->IcmpErrorReceived = TRUE;
936 } else {
937 Status = EFI_DEVICE_ERROR;
939 } else {
940 PxeBcParseCachedDhcpPacket (&Private->PxeReply);
943 if (Mode->PxeBisReplyReceived) {
944 CopyMem (
945 &Private->ServerIp,
946 &Mode->PxeReply.Dhcpv4.BootpSiAddr,
947 sizeof (EFI_IPv4_ADDRESS)
951 return Status;
955 /**
956 Used to perform TFTP and MTFTP services.
958 This function is used to perform TFTP and MTFTP services. This includes the
959 TFTP operations to get the size of a file, read a directory, read a file, and
960 write a file. It also includes the MTFTP operations to get the size of a file,
961 read a directory, and read a file. The type of operation is specified by Operation.
962 If the callback function that is invoked during the TFTP/MTFTP operation does
963 not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE, then EFI_ABORTED will
964 be returned.
965 For read operations, the return data will be placed in the buffer specified by
966 BufferPtr. If BufferSize is too small to contain the entire downloaded file,
967 then EFI_BUFFER_TOO_SMALL will be returned and BufferSize will be set to zero
968 or the size of the requested file (the size of the requested file is only returned
969 if the TFTP server supports TFTP options). If BufferSize is large enough for the
970 read operation, then BufferSize will be set to the size of the downloaded file,
971 and EFI_SUCCESS will be returned. Applications using the PxeBc.Mtftp() services
972 should use the get-file-size operations to determine the size of the downloaded
973 file prior to using the read-file operations-especially when downloading large
974 (greater than 64 MB) files-instead of making two calls to the read-file operation.
975 Following this recommendation will save time if the file is larger than expected
976 and the TFTP server does not support TFTP option extensions. Without TFTP option
977 extension support, the client has to download the entire file, counting and discarding
978 the received packets, to determine the file size.
979 For write operations, the data to be sent is in the buffer specified by BufferPtr.
980 BufferSize specifies the number of bytes to send. If the write operation completes
981 successfully, then EFI_SUCCESS will be returned.
982 For TFTP "get file size" operations, the size of the requested file or directory
983 is returned in BufferSize, and EFI_SUCCESS will be returned. If the TFTP server
984 does not support options, the file will be downloaded into a bit bucket and the
985 length of the downloaded file will be returned. For MTFTP "get file size" operations,
986 if the MTFTP server does not support the "get file size" option, EFI_UNSUPPORTED
987 will be returned.
988 This function can take up to 10 seconds to timeout and return control to the caller.
989 If the TFTP sequence does not complete, EFI_TIMEOUT will be returned.
990 If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,
991 then the TFTP sequence is stopped and EFI_ABORTED will be returned.
992 The format of the data returned from a TFTP read directory operation is a null-terminated
993 filename followed by a null-terminated information string, of the form
994 "size year-month-day hour:minute:second" (i.e. %d %d-%d-%d %d:%d:%f - note that
995 the seconds field can be a decimal number), where the date and time are UTC. For
996 an MTFTP read directory command, there is additionally a null-terminated multicast
997 IP address preceding the filename of the form %d.%d.%d.%d for IP v4. The final
998 entry is itself null-terminated, so that the final information string is terminated
999 with two null octets.
1001 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
1002 @param Operation The type of operation to perform.
1003 @param BufferPtr A pointer to the data buffer.
1004 @param Overwrite Only used on write file operations. TRUE if a file on a remote server can
1005 be overwritten.
1006 @param BufferSize For get-file-size operations, *BufferSize returns the size of the
1007 requested file.
1008 @param BlockSize The requested block size to be used during a TFTP transfer.
1009 @param ServerIp The TFTP / MTFTP server IP address.
1010 @param Filename A Null-terminated ASCII string that specifies a directory name or a file
1011 name.
1012 @param Info Pointer to the MTFTP information.
1013 @param DontUseBuffer Set to FALSE for normal TFTP and MTFTP read file operation.
1015 @retval EFI_SUCCESS The TFTP/MTFTP operation was completed.
1016 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
1017 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1018 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
1019 @retval EFI_BUFFER_TOO_SMALL The buffer is not large enough to complete the read operation.
1020 @retval EFI_ABORTED The callback function aborted the TFTP/MTFTP operation.
1021 @retval EFI_TIMEOUT The TFTP/MTFTP operation timed out.
1022 @retval EFI_ICMP_ERROR An ICMP error packet was received during the MTFTP session.
1023 @retval EFI_TFTP_ERROR A TFTP error packet was received during the MTFTP session.
1026 EFI_STATUS
1027 EFIAPI
1028 EfiPxeBcMtftp (
1029 IN EFI_PXE_BASE_CODE_PROTOCOL *This,
1030 IN EFI_PXE_BASE_CODE_TFTP_OPCODE Operation,
1031 IN OUT VOID *BufferPtr,
1032 IN BOOLEAN Overwrite,
1033 IN OUT UINT64 *BufferSize,
1034 IN UINTN *BlockSize OPTIONAL,
1035 IN EFI_IP_ADDRESS *ServerIp,
1036 IN UINT8 *Filename,
1037 IN EFI_PXE_BASE_CODE_MTFTP_INFO *Info OPTIONAL,
1038 IN BOOLEAN DontUseBuffer
1041 PXEBC_PRIVATE_DATA *Private;
1042 EFI_MTFTP4_CONFIG_DATA Mtftp4Config;
1043 EFI_STATUS Status;
1044 EFI_PXE_BASE_CODE_MODE *Mode;
1045 EFI_MAC_ADDRESS TempMacAddr;
1047 if ((This == NULL) ||
1048 (Filename == NULL) ||
1049 (BufferSize == NULL) ||
1050 ((ServerIp == NULL) || !Ip4IsUnicast (NTOHL (ServerIp->Addr[0]), 0)) ||
1051 ((BufferPtr == NULL) && DontUseBuffer) ||
1052 ((BlockSize != NULL) && (*BlockSize < 512))) {
1054 return EFI_INVALID_PARAMETER;
1057 Status = EFI_DEVICE_ERROR;
1058 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
1059 Mode = &Private->Mode;
1061 if (!Mode->AutoArp) {
1063 // If AutoArp is set false, check arp cache
1065 UpdateArpCache (This);
1066 if (!FindInArpCache (Mode, &ServerIp->v4, &TempMacAddr)) {
1067 return EFI_DEVICE_ERROR;
1071 Mode->TftpErrorReceived = FALSE;
1072 Mode->IcmpErrorReceived = FALSE;
1074 Mtftp4Config.UseDefaultSetting = FALSE;
1075 Mtftp4Config.TimeoutValue = PXEBC_MTFTP_TIMEOUT;
1076 Mtftp4Config.TryCount = PXEBC_MTFTP_RETRIES;
1078 CopyMem (
1079 &Mtftp4Config.StationIp,
1080 &Private->StationIp,
1081 sizeof (EFI_IPv4_ADDRESS)
1083 CopyMem (
1084 &Mtftp4Config.SubnetMask,
1085 &Private->SubnetMask,
1086 sizeof (EFI_IPv4_ADDRESS)
1088 CopyMem (
1089 &Mtftp4Config.GatewayIp,
1090 &Private->GatewayIp,
1091 sizeof (EFI_IPv4_ADDRESS)
1093 CopyMem (
1094 &Mtftp4Config.ServerIp,
1095 ServerIp,
1096 sizeof (EFI_IPv4_ADDRESS)
1099 switch (Operation) {
1101 case EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE:
1103 Status = PxeBcTftpGetFileSize (
1104 Private,
1105 &Mtftp4Config,
1106 Filename,
1107 BlockSize,
1108 BufferSize
1111 break;
1113 case EFI_PXE_BASE_CODE_TFTP_READ_FILE:
1115 Status = PxeBcTftpReadFile (
1116 Private,
1117 &Mtftp4Config,
1118 Filename,
1119 BlockSize,
1120 BufferPtr,
1121 BufferSize,
1122 DontUseBuffer
1125 break;
1127 case EFI_PXE_BASE_CODE_TFTP_WRITE_FILE:
1129 Status = PxeBcTftpWriteFile (
1130 Private,
1131 &Mtftp4Config,
1132 Filename,
1133 Overwrite,
1134 BlockSize,
1135 BufferPtr,
1136 BufferSize
1139 break;
1141 case EFI_PXE_BASE_CODE_TFTP_READ_DIRECTORY:
1143 Status = PxeBcTftpReadDirectory (
1144 Private,
1145 &Mtftp4Config,
1146 Filename,
1147 BlockSize,
1148 BufferPtr,
1149 BufferSize,
1150 DontUseBuffer
1153 break;
1155 case EFI_PXE_BASE_CODE_MTFTP_GET_FILE_SIZE:
1156 case EFI_PXE_BASE_CODE_MTFTP_READ_FILE:
1157 case EFI_PXE_BASE_CODE_MTFTP_READ_DIRECTORY:
1158 Status = EFI_UNSUPPORTED;
1159 break;
1161 default:
1163 Status = EFI_INVALID_PARAMETER;
1164 break;
1167 if (Status == EFI_ICMP_ERROR) {
1168 Mode->IcmpErrorReceived = TRUE;
1171 return Status;
1175 /**
1176 Writes a UDP packet to the network interface.
1178 This function writes a UDP packet specified by the (optional HeaderPtr and)
1179 BufferPtr parameters to the network interface. The UDP header is automatically
1180 built by this routine. It uses the parameters OpFlags, DestIp, DestPort, GatewayIp,
1181 SrcIp, and SrcPort to build this header. If the packet is successfully built and
1182 transmitted through the network interface, then EFI_SUCCESS will be returned.
1183 If a timeout occurs during the transmission of the packet, then EFI_TIMEOUT will
1184 be returned. If an ICMP error occurs during the transmission of the packet, then
1185 the IcmpErrorReceived field is set to TRUE, the IcmpError field is filled in and
1186 EFI_ICMP_ERROR will be returned. If the Callback Protocol does not return
1187 EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE, then EFI_ABORTED will be returned.
1189 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
1190 @param OpFlags The UDP operation flags.
1191 @param DestIp The destination IP address.
1192 @param DestPort The destination UDP port number.
1193 @param GatewayIp The gateway IP address.
1194 @param SrcIp The source IP address.
1195 @param SrcPort The source UDP port number.
1196 @param HeaderSize An optional field which may be set to the length of a header at
1197 HeaderPtr to be prefixed to the data at BufferPtr.
1198 @param HeaderPtr If HeaderSize is not NULL, a pointer to a header to be prefixed to the
1199 data at BufferPtr.
1200 @param BufferSize A pointer to the size of the data at BufferPtr.
1201 @param BufferPtr A pointer to the data to be written.
1203 @retval EFI_SUCCESS The UDP Write operation was completed.
1204 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
1205 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1206 @retval EFI_BAD_BUFFER_SIZE The buffer is too long to be transmitted.
1207 @retval EFI_ABORTED The callback function aborted the UDP Write operation.
1208 @retval EFI_TIMEOUT The UDP Write operation timed out.
1209 @retval EFI_ICMP_ERROR An ICMP error packet was received during the UDP write session.
1212 EFI_STATUS
1213 EFIAPI
1214 EfiPxeBcUdpWrite (
1215 IN EFI_PXE_BASE_CODE_PROTOCOL *This,
1216 IN UINT16 OpFlags,
1217 IN EFI_IP_ADDRESS *DestIp,
1218 IN EFI_PXE_BASE_CODE_UDP_PORT *DestPort,
1219 IN EFI_IP_ADDRESS *GatewayIp OPTIONAL,
1220 IN EFI_IP_ADDRESS *SrcIp OPTIONAL,
1221 IN OUT EFI_PXE_BASE_CODE_UDP_PORT *SrcPort OPTIONAL,
1222 IN UINTN *HeaderSize OPTIONAL,
1223 IN VOID *HeaderPtr OPTIONAL,
1224 IN UINTN *BufferSize,
1225 IN VOID *BufferPtr
1228 PXEBC_PRIVATE_DATA *Private;
1229 EFI_UDP4_PROTOCOL *Udp4;
1230 EFI_UDP4_COMPLETION_TOKEN Token;
1231 EFI_UDP4_TRANSMIT_DATA *Udp4TxData;
1232 UINT32 FragCount;
1233 UINT32 DataLength;
1234 EFI_UDP4_SESSION_DATA Udp4Session;
1235 EFI_STATUS Status;
1236 BOOLEAN IsDone;
1237 EFI_PXE_BASE_CODE_MODE *Mode;
1238 EFI_MAC_ADDRESS TempMacAddr;
1240 IsDone = FALSE;
1242 if ((This == NULL) || (DestIp == NULL) || (DestPort == NULL)) {
1243 return EFI_INVALID_PARAMETER;
1246 if ((GatewayIp != NULL) && !Ip4IsUnicast (NTOHL (GatewayIp->Addr[0]), 0)) {
1248 // Gateway is provided but it's not a unicast IP address.
1250 return EFI_INVALID_PARAMETER;
1253 if ((HeaderSize != NULL) && ((*HeaderSize == 0) || (HeaderPtr == NULL))) {
1255 // The HeaderSize ptr isn't NULL and: 1. the value is zero; or 2. the HeaderPtr
1256 // is NULL.
1258 return EFI_INVALID_PARAMETER;
1261 if ((BufferSize == NULL) || ((*BufferSize != 0) && (BufferPtr == NULL))) {
1262 return EFI_INVALID_PARAMETER;
1265 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
1266 Udp4 = Private->Udp4Write;
1267 Mode = &Private->Mode;
1268 if (!Mode->Started) {
1269 return EFI_NOT_STARTED;
1272 if (!Private->AddressIsOk && (SrcIp == NULL)) {
1273 return EFI_INVALID_PARAMETER;
1276 if (!Mode->AutoArp) {
1278 // If AutoArp is set false, check arp cache
1280 UpdateArpCache (This);
1281 if (!FindInArpCache (Mode, &DestIp->v4, &TempMacAddr)) {
1282 return EFI_DEVICE_ERROR;
1286 Mode->IcmpErrorReceived = FALSE;
1288 if ((Private->CurrentUdpSrcPort == 0) ||
1289 ((SrcPort != NULL) && (*SrcPort != Private->CurrentUdpSrcPort))) {
1291 // Port is changed, (re)configure the Udp4Write instance
1293 if (SrcPort != NULL) {
1294 Private->CurrentUdpSrcPort = *SrcPort;
1297 Status = PxeBcConfigureUdpWriteInstance (
1298 Udp4,
1299 &Private->StationIp.v4,
1300 &Private->SubnetMask.v4,
1301 &Private->GatewayIp.v4,
1302 &Private->CurrentUdpSrcPort
1304 if (EFI_ERROR (Status)) {
1305 Private->CurrentUdpSrcPort = 0;
1306 return EFI_INVALID_PARAMETER;
1310 ZeroMem (&Token, sizeof (EFI_UDP4_COMPLETION_TOKEN));
1311 ZeroMem (&Udp4Session, sizeof (EFI_UDP4_SESSION_DATA));
1313 CopyMem (&Udp4Session.DestinationAddress, DestIp, sizeof (EFI_IPv4_ADDRESS));
1314 Udp4Session.DestinationPort = *DestPort;
1315 if (SrcIp != NULL) {
1316 CopyMem (&Udp4Session.SourceAddress, SrcIp, sizeof (EFI_IPv4_ADDRESS));
1318 if (SrcPort != NULL) {
1319 Udp4Session.SourcePort = *SrcPort;
1322 FragCount = (HeaderSize != NULL) ? 2 : 1;
1323 Udp4TxData = (EFI_UDP4_TRANSMIT_DATA *) AllocateZeroPool (sizeof (EFI_UDP4_TRANSMIT_DATA) + (FragCount - 1) * sizeof (EFI_UDP4_FRAGMENT_DATA));
1324 if (Udp4TxData == NULL) {
1325 return EFI_OUT_OF_RESOURCES;
1328 Udp4TxData->FragmentCount = FragCount;
1329 Udp4TxData->FragmentTable[FragCount - 1].FragmentLength = (UINT32) *BufferSize;
1330 Udp4TxData->FragmentTable[FragCount - 1].FragmentBuffer = BufferPtr;
1331 DataLength = (UINT32) *BufferSize;
1333 if (FragCount == 2) {
1335 Udp4TxData->FragmentTable[0].FragmentLength = (UINT32) *HeaderSize;
1336 Udp4TxData->FragmentTable[0].FragmentBuffer = HeaderPtr;
1337 DataLength += (UINT32) *HeaderSize;
1340 if (GatewayIp != NULL) {
1341 Udp4TxData->GatewayAddress = (EFI_IPv4_ADDRESS *) GatewayIp;
1343 Udp4TxData->UdpSessionData = &Udp4Session;
1344 Udp4TxData->DataLength = DataLength;
1345 Token.Packet.TxData = Udp4TxData;
1347 Status = gBS->CreateEvent (
1348 EVT_NOTIFY_SIGNAL,
1349 TPL_NOTIFY,
1350 PxeBcCommonNotify,
1351 &IsDone,
1352 &Token.Event
1354 if (EFI_ERROR (Status)) {
1355 goto ON_EXIT;
1358 Status = Udp4->Transmit (Udp4, &Token);
1359 if (EFI_ERROR (Status)) {
1360 if (Status == EFI_ICMP_ERROR) {
1361 Mode->IcmpErrorReceived = TRUE;
1363 goto ON_EXIT;
1366 while (!IsDone) {
1368 Udp4->Poll (Udp4);
1371 Status = Token.Status;
1373 ON_EXIT:
1375 if (Token.Event != NULL) {
1376 gBS->CloseEvent (Token.Event);
1379 gBS->FreePool (Udp4TxData);
1381 return Status;
1385 Decide whether the incoming UDP packet is acceptable per IP filter settings
1386 in provided PxeBcMode.
1388 @param PxeBcMode Pointer to EFI_PXE_BASE_CODE_MODE.
1389 @param Session Received UDP session.
1391 @retval TRUE The UDP package matches IP filters.
1392 @retval FALSE The UDP package doesn't matches IP filters.
1395 BOOLEAN
1396 CheckIpByFilter (
1397 IN EFI_PXE_BASE_CODE_MODE *PxeBcMode,
1398 IN EFI_UDP4_SESSION_DATA *Session
1401 UINTN Index;
1402 EFI_IPv4_ADDRESS Ip4Address;
1403 EFI_IPv4_ADDRESS DestIp4Address;
1405 if ((PxeBcMode->IpFilter.Filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS) != 0) {
1406 return TRUE;
1409 CopyMem (&DestIp4Address, &Session->DestinationAddress, sizeof (DestIp4Address));
1410 if ((PxeBcMode->IpFilter.Filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS_MULTICAST) &&
1411 IP4_IS_MULTICAST (EFI_NTOHL (DestIp4Address))
1413 return TRUE;
1416 if ((PxeBcMode->IpFilter.Filters & EFI_PXE_BASE_CODE_IP_FILTER_BROADCAST) &&
1417 IP4_IS_LOCAL_BROADCAST (EFI_NTOHL (DestIp4Address))
1419 return TRUE;
1422 CopyMem (&Ip4Address, &PxeBcMode->StationIp.v4, sizeof (Ip4Address));
1423 if ((PxeBcMode->IpFilter.Filters & EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP) &&
1424 EFI_IP4_EQUAL (&Ip4Address, &DestIp4Address)
1426 return TRUE;
1429 ASSERT (PxeBcMode->IpFilter.IpCnt < EFI_PXE_BASE_CODE_MAX_IPCNT);
1431 for (Index = 0; Index < PxeBcMode->IpFilter.IpCnt; Index++) {
1432 CopyMem (
1433 &Ip4Address,
1434 &PxeBcMode->IpFilter.IpList[Index].v4,
1435 sizeof (Ip4Address)
1437 if (EFI_IP4_EQUAL (&Ip4Address, &DestIp4Address)) {
1438 return TRUE;
1442 return FALSE;
1445 /**
1446 Reads a UDP packet from the network interface.
1448 This function reads a UDP packet from a network interface. The data contents
1449 are returned in (the optional HeaderPtr and) BufferPtr, and the size of the
1450 buffer received is returned in BufferSize . If the input BufferSize is smaller
1451 than the UDP packet received (less optional HeaderSize), it will be set to the
1452 required size, and EFI_BUFFER_TOO_SMALL will be returned. In this case, the
1453 contents of BufferPtr are undefined, and the packet is lost. If a UDP packet is
1454 successfully received, then EFI_SUCCESS will be returned, and the information
1455 from the UDP header will be returned in DestIp, DestPort, SrcIp, and SrcPort if
1456 they are not NULL. Depending on the values of OpFlags and the DestIp, DestPort,
1457 SrcIp, and SrcPort input values, different types of UDP packet receive filtering
1458 will be performed. The following tables summarize these receive filter operations.
1460 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
1461 @param OpFlags The UDP operation flags.
1462 @param DestIp The destination IP address.
1463 @param DestPort The destination UDP port number.
1464 @param SrcIp The source IP address.
1465 @param SrcPort The source UDP port number.
1466 @param HeaderSize An optional field which may be set to the length of a header at
1467 HeaderPtr to be prefixed to the data at BufferPtr.
1468 @param HeaderPtr If HeaderSize is not NULL, a pointer to a header to be prefixed to the
1469 data at BufferPtr.
1470 @param BufferSize A pointer to the size of the data at BufferPtr.
1471 @param BufferPtr A pointer to the data to be read.
1473 @retval EFI_SUCCESS The UDP Read operation was completed.
1474 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
1475 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1476 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
1477 @retval EFI_BUFFER_TOO_SMALL The packet is larger than Buffer can hold.
1478 @retval EFI_ABORTED The callback function aborted the UDP Read operation.
1479 @retval EFI_TIMEOUT The UDP Read operation timed out.
1482 EFI_STATUS
1483 EFIAPI
1484 EfiPxeBcUdpRead (
1485 IN EFI_PXE_BASE_CODE_PROTOCOL *This,
1486 IN UINT16 OpFlags,
1487 IN OUT EFI_IP_ADDRESS *DestIp OPTIONAL,
1488 IN OUT EFI_PXE_BASE_CODE_UDP_PORT *DestPort OPTIONAL,
1489 IN OUT EFI_IP_ADDRESS *SrcIp OPTIONAL,
1490 IN OUT EFI_PXE_BASE_CODE_UDP_PORT *SrcPort OPTIONAL,
1491 IN UINTN *HeaderSize OPTIONAL,
1492 IN VOID *HeaderPtr OPTIONAL,
1493 IN OUT UINTN *BufferSize,
1494 IN VOID *BufferPtr
1497 PXEBC_PRIVATE_DATA *Private;
1498 EFI_PXE_BASE_CODE_MODE *Mode;
1499 EFI_UDP4_PROTOCOL *Udp4;
1500 EFI_UDP4_COMPLETION_TOKEN Token;
1501 EFI_UDP4_RECEIVE_DATA *RxData;
1502 EFI_UDP4_SESSION_DATA *Session;
1503 EFI_STATUS Status;
1504 BOOLEAN IsDone;
1505 BOOLEAN Matched;
1506 UINTN CopyLen;
1508 if (This == NULL || DestIp == NULL || DestPort == NULL) {
1509 return EFI_INVALID_PARAMETER;
1512 if (((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT) == 0 && (DestPort == NULL)) ||
1513 ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT) == 0 && (SrcIp == NULL)) ||
1514 ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT) == 0 && (SrcPort == NULL))) {
1515 return EFI_INVALID_PARAMETER;
1518 if (((HeaderSize != NULL) && (*HeaderSize == 0)) || ((HeaderSize != NULL) && (HeaderPtr == NULL))) {
1519 return EFI_INVALID_PARAMETER;
1522 if ((BufferSize == NULL) || ((BufferPtr == NULL) && (*BufferSize != 0))) {
1523 return EFI_INVALID_PARAMETER;
1526 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
1527 Mode = Private->PxeBc.Mode;
1528 Udp4 = Private->Udp4Read;
1530 if (!Mode->Started) {
1531 return EFI_NOT_STARTED;
1534 Mode->IcmpErrorReceived = FALSE;
1536 Status = gBS->CreateEvent (
1537 EVT_NOTIFY_SIGNAL,
1538 TPL_NOTIFY,
1539 PxeBcCommonNotify,
1540 &IsDone,
1541 &Token.Event
1543 if (EFI_ERROR (Status)) {
1544 return EFI_OUT_OF_RESOURCES;
1547 TRY_AGAIN:
1549 IsDone = FALSE;
1550 Status = Udp4->Receive (Udp4, &Token);
1551 if (EFI_ERROR (Status)) {
1552 if (Status == EFI_ICMP_ERROR) {
1553 Mode->IcmpErrorReceived = TRUE;
1555 goto ON_EXIT;
1558 Udp4->Poll (Udp4);
1560 if (!IsDone) {
1561 Status = EFI_TIMEOUT;
1562 } else {
1565 // check whether this packet matches the filters
1567 if (EFI_ERROR (Token.Status)){
1568 goto ON_EXIT;
1571 RxData = Token.Packet.RxData;
1572 Session = &RxData->UdpSession;
1574 Matched = TRUE;
1576 if ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_USE_FILTER) != 0) {
1577 Matched = FALSE;
1579 // Check UDP package by IP filter settings
1581 if (CheckIpByFilter (Mode, Session)) {
1582 Matched = TRUE;
1586 if (Matched) {
1587 Matched = FALSE;
1590 // Match the destination ip of the received udp dgram
1592 if ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_IP) != 0) {
1593 Matched = TRUE;
1595 if (DestIp != NULL) {
1596 CopyMem (DestIp, &Session->DestinationAddress, sizeof (EFI_IPv4_ADDRESS));
1598 } else {
1599 if (DestIp != NULL) {
1600 if (EFI_IP4_EQUAL (DestIp, &Session->DestinationAddress)) {
1601 Matched = TRUE;
1603 } else {
1604 if (EFI_IP4_EQUAL (&Private->StationIp, &Session->DestinationAddress)) {
1605 Matched = TRUE;
1611 if (Matched) {
1613 // Match the destination port of the received udp dgram
1615 if ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT) != 0) {
1617 if (DestPort != NULL) {
1618 *DestPort = Session->DestinationPort;
1620 } else {
1622 if (*DestPort != Session->DestinationPort) {
1623 Matched = FALSE;
1628 if (Matched) {
1630 // Match the source ip of the received udp dgram
1632 if ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP) != 0) {
1634 if (SrcIp != NULL) {
1635 CopyMem (SrcIp, &Session->SourceAddress, sizeof (EFI_IPv4_ADDRESS));
1637 } else {
1639 if (!EFI_IP4_EQUAL (SrcIp, &Session->SourceAddress)) {
1640 Matched = FALSE;
1645 if (Matched) {
1647 // Match the source port of the received udp dgram
1649 if ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT) != 0) {
1651 if (SrcPort != NULL) {
1652 *SrcPort = Session->SourcePort;
1654 } else {
1656 if (*SrcPort != Session->SourcePort) {
1657 Matched = FALSE;
1662 if (Matched) {
1664 CopyLen = 0;
1666 if (HeaderSize != NULL) {
1667 CopyLen = MIN (*HeaderSize, RxData->DataLength);
1668 CopyMem (HeaderPtr, RxData->FragmentTable[0].FragmentBuffer, CopyLen);
1669 *HeaderSize = CopyLen;
1672 if (RxData->DataLength - CopyLen > *BufferSize) {
1674 Status = EFI_BUFFER_TOO_SMALL;
1675 } else {
1677 *BufferSize = RxData->DataLength - CopyLen;
1678 CopyMem (
1679 BufferPtr,
1680 (UINT8 *) RxData->FragmentTable[0].FragmentBuffer + CopyLen,
1681 *BufferSize
1684 } else {
1686 Status = EFI_TIMEOUT;
1690 // Recycle the RxData
1692 gBS->SignalEvent (RxData->RecycleSignal);
1694 if (!Matched) {
1695 goto TRY_AGAIN;
1699 ON_EXIT:
1701 Udp4->Cancel (Udp4, &Token);
1703 gBS->CloseEvent (Token.Event);
1705 return Status;
1708 /**
1709 Updates the IP receive filters of a network device and enables software filtering.
1711 The NewFilter field is used to modify the network device's current IP receive
1712 filter settings and to enable a software filter. This function updates the IpFilter
1713 field of the EFI_PXE_BASE_CODE_MODE structure with the contents of NewIpFilter.
1714 The software filter is used when the USE_FILTER in OpFlags is set to UdpRead().
1715 The current hardware filter remains in effect no matter what the settings of OpFlags
1716 are, so that the meaning of ANY_DEST_IP set in OpFlags to UdpRead() is from those
1717 packets whose reception is enabled in hardware-physical NIC address (unicast),
1718 broadcast address, logical address or addresses (multicast), or all (promiscuous).
1719 UdpRead() does not modify the IP filter settings.
1720 Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP receive
1721 filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.
1722 If an application or driver wishes to preserve the IP receive filter settings,
1723 it will have to preserve the IP receive filter settings before these calls, and
1724 use SetIpFilter() to restore them after the calls. If incompatible filtering is
1725 requested (for example, PROMISCUOUS with anything else) or if the device does not
1726 support a requested filter setting and it cannot be accommodated in software
1727 (for example, PROMISCUOUS not supported), EFI_INVALID_PARAMETER will be returned.
1728 The IPlist field is used to enable IPs other than the StationIP. They may be
1729 multicast or unicast. If IPcnt is set as well as EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP,
1730 then both the StationIP and the IPs from the IPlist will be used.
1732 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
1733 @param NewFilter Pointer to the new set of IP receive filters.
1735 @retval EFI_SUCCESS The IP receive filter settings were updated.
1736 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
1737 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1740 EFI_STATUS
1741 EFIAPI
1742 EfiPxeBcSetIpFilter (
1743 IN EFI_PXE_BASE_CODE_PROTOCOL *This,
1744 IN EFI_PXE_BASE_CODE_IP_FILTER *NewFilter
1747 EFI_STATUS Status;
1748 PXEBC_PRIVATE_DATA *Private;
1749 EFI_PXE_BASE_CODE_MODE *Mode;
1750 UINTN Index;
1751 BOOLEAN PromiscuousNeed;
1753 if (This == NULL) {
1754 DEBUG ((EFI_D_ERROR, "This == NULL.\n"));
1755 return EFI_INVALID_PARAMETER;
1758 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
1759 Mode = Private->PxeBc.Mode;
1761 if (NewFilter == NULL) {
1762 DEBUG ((EFI_D_ERROR, "NewFilter == NULL.\n"));
1763 return EFI_INVALID_PARAMETER;
1766 if (NewFilter->IpCnt > EFI_PXE_BASE_CODE_MAX_IPCNT) {
1767 DEBUG ((EFI_D_ERROR, "NewFilter->IpCnt > %d.\n", EFI_PXE_BASE_CODE_MAX_IPCNT));
1768 return EFI_INVALID_PARAMETER;
1771 if (!Mode->Started) {
1772 DEBUG ((EFI_D_ERROR, "BC was not started.\n"));
1773 return EFI_NOT_STARTED;
1776 PromiscuousNeed = FALSE;
1778 for (Index = 0; Index < NewFilter->IpCnt; ++Index) {
1779 if (IP4_IS_LOCAL_BROADCAST (EFI_IP4 (NewFilter->IpList[Index].v4))) {
1781 // The IP is a broadcast address.
1783 DEBUG ((EFI_D_ERROR, "There is broadcast address in NewFilter.\n"));
1784 return EFI_INVALID_PARAMETER;
1786 if (Ip4IsUnicast (EFI_IP4 (NewFilter->IpList[Index].v4), 0) &&
1787 (NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP)
1790 // If EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP is set and IP4 address is in IpList,
1791 // promiscuous mode is needed.
1793 PromiscuousNeed = TRUE;
1798 // Clear the UDP instance configuration, all joined groups will be left
1799 // during the operation.
1801 Private->Udp4Read->Configure (Private->Udp4Read, NULL);
1802 Private->Udp4CfgData.AcceptPromiscuous = FALSE;
1803 Private->Udp4CfgData.AcceptBroadcast = FALSE;
1805 if (PromiscuousNeed ||
1806 NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS ||
1807 NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS_MULTICAST
1810 // Configure the udp4 filter to receive all packages
1812 Private->Udp4CfgData.AcceptPromiscuous = TRUE;
1815 // Configure the UDP instance with the new configuration.
1817 Status = Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);
1818 if (EFI_ERROR (Status)) {
1819 return Status;
1822 } else {
1824 if (NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_BROADCAST) {
1826 // Configure the udp4 filter to receive all broadcast packages
1828 Private->Udp4CfgData.AcceptBroadcast = TRUE;
1832 // Configure the UDP instance with the new configuration.
1834 Status = Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);
1835 if (EFI_ERROR (Status)) {
1836 return Status;
1839 if (NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP) {
1841 for (Index = 0; Index < NewFilter->IpCnt; ++Index) {
1842 if (IP4_IS_MULTICAST (EFI_NTOHL (NewFilter->IpList[Index].v4))) {
1844 // Join the mutilcast group
1846 Status = Private->Udp4Read->Groups (Private->Udp4Read, TRUE, &NewFilter->IpList[Index].v4);
1847 if (EFI_ERROR (Status)) {
1848 return Status;
1857 // Save the new filter.
1859 CopyMem (&Mode->IpFilter, NewFilter, sizeof (Mode->IpFilter));
1861 return EFI_SUCCESS;
1865 /**
1866 Uses the ARP protocol to resolve a MAC address.
1868 This function uses the ARP protocol to resolve a MAC address. The UsingIpv6 field
1869 of the EFI_PXE_BASE_CODE_MODE structure is used to determine if IPv4 or IPv6
1870 addresses are being used. The IP address specified by IpAddr is used to resolve
1871 a MAC address. If the ARP protocol succeeds in resolving the specified address,
1872 then the ArpCacheEntries and ArpCache fields of the EFI_PXE_BASE_CODE_MODE structure
1873 are updated, and EFI_SUCCESS is returned. If MacAddr is not NULL, the resolved
1874 MAC address is placed there as well. If the PXE Base Code protocol is in the
1875 stopped state, then EFI_NOT_STARTED is returned. If the ARP protocol encounters
1876 a timeout condition while attempting to resolve an address, then EFI_TIMEOUT is
1877 returned. If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,
1878 then EFI_ABORTED is returned.
1880 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
1881 @param IpAddr Pointer to the IP address that is used to resolve a MAC address.
1882 @param MacAddr If not NULL, a pointer to the MAC address that was resolved with the
1883 ARP protocol.
1885 @retval EFI_SUCCESS The IP or MAC address was resolved.
1886 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
1887 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1888 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
1889 @retval EFI_ICMP_ERROR Something error occur with the ICMP packet message.
1892 EFI_STATUS
1893 EFIAPI
1894 EfiPxeBcArp (
1895 IN EFI_PXE_BASE_CODE_PROTOCOL * This,
1896 IN EFI_IP_ADDRESS * IpAddr,
1897 IN EFI_MAC_ADDRESS * MacAddr OPTIONAL
1900 PXEBC_PRIVATE_DATA *Private;
1901 EFI_PXE_BASE_CODE_MODE *Mode;
1902 EFI_STATUS Status;
1903 EFI_MAC_ADDRESS TempMacAddr;
1905 if (This == NULL || IpAddr == NULL) {
1906 return EFI_INVALID_PARAMETER;
1909 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
1910 Mode = Private->PxeBc.Mode;
1912 if (!Mode->Started) {
1913 return EFI_NOT_STARTED;
1916 if (!Private->AddressIsOk || Mode->UsingIpv6) {
1918 // We can't resolve the IP address if we don't have a local address now.
1919 // Don't have ARP for IPv6.
1921 return EFI_INVALID_PARAMETER;
1924 Mode->IcmpErrorReceived = FALSE;
1926 if (!Mode->AutoArp) {
1928 // If AutoArp is set false, check arp cache
1930 UpdateArpCache (This);
1931 if (!FindInArpCache (Mode, &IpAddr->v4, &TempMacAddr)) {
1932 return EFI_DEVICE_ERROR;
1934 } else {
1935 Status = Private->Arp->Request (Private->Arp, &IpAddr->v4, NULL, &TempMacAddr);
1936 if (EFI_ERROR (Status)) {
1937 if (Status == EFI_ICMP_ERROR) {
1938 Mode->IcmpErrorReceived = TRUE;
1940 return Status;
1944 if (MacAddr != NULL) {
1945 CopyMem (MacAddr, &TempMacAddr, sizeof (EFI_MAC_ADDRESS));
1948 return EFI_SUCCESS;
1951 /**
1952 Updates the parameters that affect the operation of the PXE Base Code Protocol.
1954 This function sets parameters that affect the operation of the PXE Base Code Protocol.
1955 The parameter specified by NewAutoArp is used to control the generation of ARP
1956 protocol packets. If NewAutoArp is TRUE, then ARP Protocol packets will be generated
1957 as required by the PXE Base Code Protocol. If NewAutoArp is FALSE, then no ARP
1958 Protocol packets will be generated. In this case, the only mappings that are
1959 available are those stored in the ArpCache of the EFI_PXE_BASE_CODE_MODE structure.
1960 If there are not enough mappings in the ArpCache to perform a PXE Base Code Protocol
1961 service, then the service will fail. This function updates the AutoArp field of
1962 the EFI_PXE_BASE_CODE_MODE structure to NewAutoArp.
1963 The SetParameters() call must be invoked after a Callback Protocol is installed
1964 to enable the use of callbacks.
1966 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
1967 @param NewAutoArp If not NULL, a pointer to a value that specifies whether to replace the
1968 current value of AutoARP.
1969 @param NewSendGUID If not NULL, a pointer to a value that specifies whether to replace the
1970 current value of SendGUID.
1971 @param NewTTL If not NULL, a pointer to be used in place of the current value of TTL,
1972 the "time to live" field of the IP header.
1973 @param NewToS If not NULL, a pointer to be used in place of the current value of ToS,
1974 the "type of service" field of the IP header.
1975 @param NewMakeCallback If not NULL, a pointer to a value that specifies whether to replace the
1976 current value of the MakeCallback field of the Mode structure.
1978 @retval EFI_SUCCESS The new parameters values were updated.
1979 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
1980 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1983 EFI_STATUS
1984 EFIAPI
1985 EfiPxeBcSetParameters (
1986 IN EFI_PXE_BASE_CODE_PROTOCOL *This,
1987 IN BOOLEAN *NewAutoArp OPTIONAL,
1988 IN BOOLEAN *NewSendGUID OPTIONAL,
1989 IN UINT8 *NewTTL OPTIONAL,
1990 IN UINT8 *NewToS OPTIONAL,
1991 IN BOOLEAN *NewMakeCallback // OPTIONAL
1994 PXEBC_PRIVATE_DATA *Private;
1995 EFI_PXE_BASE_CODE_MODE *Mode;
1996 EFI_STATUS Status;
1998 Status = EFI_SUCCESS;
2000 if (This == NULL) {
2001 Status = EFI_INVALID_PARAMETER;
2002 goto ON_EXIT;
2005 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
2006 Mode = Private->PxeBc.Mode;
2008 if (NewSendGUID != NULL && *NewSendGUID) {
2010 // FixMe, cann't locate SendGuid
2014 if (NewMakeCallback != NULL && *NewMakeCallback) {
2016 Status = gBS->HandleProtocol (
2017 Private->Controller,
2018 &gEfiPxeBaseCodeCallbackProtocolGuid,
2019 (VOID **) &Private->PxeBcCallback
2021 if (EFI_ERROR (Status) || (Private->PxeBcCallback->Callback == NULL)) {
2023 Status = EFI_INVALID_PARAMETER;
2024 goto ON_EXIT;
2028 if (!Mode->Started) {
2029 Status = EFI_NOT_STARTED;
2030 goto ON_EXIT;
2033 if (NewMakeCallback != NULL) {
2035 if (*NewMakeCallback) {
2037 // Update the Callback protocol.
2039 Status = gBS->HandleProtocol (
2040 Private->Controller,
2041 &gEfiPxeBaseCodeCallbackProtocolGuid,
2042 (VOID **) &Private->PxeBcCallback
2045 if (EFI_ERROR (Status) || (Private->PxeBcCallback->Callback == NULL)) {
2046 Status = EFI_INVALID_PARAMETER;
2047 goto ON_EXIT;
2049 } else {
2050 Private->PxeBcCallback = NULL;
2053 Mode->MakeCallbacks = *NewMakeCallback;
2056 if (NewAutoArp != NULL) {
2057 Mode->AutoArp = *NewAutoArp;
2060 if (NewSendGUID != NULL) {
2061 Mode->SendGUID = *NewSendGUID;
2064 if (NewTTL != NULL) {
2065 Mode->TTL = *NewTTL;
2068 if (NewToS != NULL) {
2069 Mode->ToS = *NewToS;
2072 ON_EXIT:
2073 return Status;
2076 /**
2077 Updates the station IP address and/or subnet mask values of a network device.
2079 This function updates the station IP address and/or subnet mask values of a network
2080 device. The NewStationIp field is used to modify the network device's current IP address.
2081 If NewStationIP is NULL, then the current IP address will not be modified. Otherwise,
2082 this function updates the StationIp field of the EFI_PXE_BASE_CODE_MODE structure
2083 with NewStationIp. The NewSubnetMask field is used to modify the network device's current subnet
2084 mask. If NewSubnetMask is NULL, then the current subnet mask will not be modified.
2085 Otherwise, this function updates the SubnetMask field of the EFI_PXE_BASE_CODE_MODE
2086 structure with NewSubnetMask.
2088 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
2089 @param NewStationIp Pointer to the new IP address to be used by the network device.
2090 @param NewSubnetMask Pointer to the new subnet mask to be used by the network device.
2092 @retval EFI_SUCCESS The new station IP address and/or subnet mask were updated.
2093 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
2094 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
2097 EFI_STATUS
2098 EFIAPI
2099 EfiPxeBcSetStationIP (
2100 IN EFI_PXE_BASE_CODE_PROTOCOL * This,
2101 IN EFI_IP_ADDRESS * NewStationIp OPTIONAL,
2102 IN EFI_IP_ADDRESS * NewSubnetMask OPTIONAL
2105 PXEBC_PRIVATE_DATA *Private;
2106 EFI_PXE_BASE_CODE_MODE *Mode;
2107 EFI_ARP_CONFIG_DATA ArpConfigData;
2109 if (This == NULL) {
2110 return EFI_INVALID_PARAMETER;
2113 if (NewStationIp != NULL && !Ip4IsUnicast (NTOHL (NewStationIp->Addr[0]), 0)) {
2114 return EFI_INVALID_PARAMETER;
2117 if (NewSubnetMask != NULL && !IP4_IS_VALID_NETMASK (NTOHL (NewSubnetMask->Addr[0]))) {
2118 return EFI_INVALID_PARAMETER;
2121 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
2122 Mode = Private->PxeBc.Mode;
2124 if (!Mode->Started) {
2125 return EFI_NOT_STARTED;
2128 if (NewStationIp != NULL) {
2129 CopyMem (&Mode->StationIp, NewStationIp, sizeof (EFI_IP_ADDRESS));
2130 CopyMem (&Private->StationIp, NewStationIp, sizeof (EFI_IP_ADDRESS));
2133 if (NewSubnetMask != NULL) {
2134 CopyMem (&Mode->SubnetMask, NewSubnetMask, sizeof (EFI_IP_ADDRESS));
2135 CopyMem (&Private->SubnetMask ,NewSubnetMask, sizeof (EFI_IP_ADDRESS));
2138 Private->AddressIsOk = TRUE;
2140 if (!Mode->UsingIpv6) {
2142 // If in IPv4 mode, configure the corresponding ARP with this new
2143 // station IP address.
2145 ZeroMem (&ArpConfigData, sizeof (EFI_ARP_CONFIG_DATA));
2147 ArpConfigData.SwAddressType = 0x0800;
2148 ArpConfigData.SwAddressLength = sizeof (EFI_IPv4_ADDRESS);
2149 ArpConfigData.StationAddress = &Private->StationIp.v4;
2151 Private->Arp->Configure (Private->Arp, NULL);
2152 Private->Arp->Configure (Private->Arp, &ArpConfigData);
2155 // Update the route table.
2157 Mode->RouteTableEntries = 1;
2158 Mode->RouteTable[0].IpAddr.Addr[0] = Private->StationIp.Addr[0] & Private->SubnetMask.Addr[0];
2159 Mode->RouteTable[0].SubnetMask.Addr[0] = Private->SubnetMask.Addr[0];
2160 Mode->RouteTable[0].GwAddr.Addr[0] = 0;
2163 return EFI_SUCCESS;
2166 /**
2167 Updates the contents of the cached DHCP and Discover packets.
2169 The pointers to the new packets are used to update the contents of the cached
2170 packets in the EFI_PXE_BASE_CODE_MODE structure.
2172 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
2173 @param NewDhcpDiscoverValid Pointer to a value that will replace the current
2174 DhcpDiscoverValid field.
2175 @param NewDhcpAckReceived Pointer to a value that will replace the current
2176 DhcpAckReceived field.
2177 @param NewProxyOfferReceived Pointer to a value that will replace the current
2178 ProxyOfferReceived field.
2179 @param NewPxeDiscoverValid Pointer to a value that will replace the current
2180 ProxyOfferReceived field.
2181 @param NewPxeReplyReceived Pointer to a value that will replace the current
2182 PxeReplyReceived field.
2183 @param NewPxeBisReplyReceived Pointer to a value that will replace the current
2184 PxeBisReplyReceived field.
2185 @param NewDhcpDiscover Pointer to the new cached DHCP Discover packet contents.
2186 @param NewDhcpAck Pointer to the new cached DHCP Ack packet contents.
2187 @param NewProxyOffer Pointer to the new cached Proxy Offer packet contents.
2188 @param NewPxeDiscover Pointer to the new cached PXE Discover packet contents.
2189 @param NewPxeReply Pointer to the new cached PXE Reply packet contents.
2190 @param NewPxeBisReply Pointer to the new cached PXE BIS Reply packet contents.
2192 @retval EFI_SUCCESS The cached packet contents were updated.
2193 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
2194 @retval EFI_INVALID_PARAMETER This is NULL or not point to a valid EFI_PXE_BASE_CODE_PROTOCOL structure.
2197 EFI_STATUS
2198 EFIAPI
2199 EfiPxeBcSetPackets (
2200 IN EFI_PXE_BASE_CODE_PROTOCOL * This,
2201 IN BOOLEAN * NewDhcpDiscoverValid OPTIONAL,
2202 IN BOOLEAN * NewDhcpAckReceived OPTIONAL,
2203 IN BOOLEAN * NewProxyOfferReceived OPTIONAL,
2204 IN BOOLEAN * NewPxeDiscoverValid OPTIONAL,
2205 IN BOOLEAN * NewPxeReplyReceived OPTIONAL,
2206 IN BOOLEAN * NewPxeBisReplyReceived OPTIONAL,
2207 IN EFI_PXE_BASE_CODE_PACKET * NewDhcpDiscover OPTIONAL,
2208 IN EFI_PXE_BASE_CODE_PACKET * NewDhcpAck OPTIONAL,
2209 IN EFI_PXE_BASE_CODE_PACKET * NewProxyOffer OPTIONAL,
2210 IN EFI_PXE_BASE_CODE_PACKET * NewPxeDiscover OPTIONAL,
2211 IN EFI_PXE_BASE_CODE_PACKET * NewPxeReply OPTIONAL,
2212 IN EFI_PXE_BASE_CODE_PACKET * NewPxeBisReply OPTIONAL
2215 PXEBC_PRIVATE_DATA *Private;
2216 EFI_PXE_BASE_CODE_MODE *Mode;
2218 if (This == NULL) {
2219 return EFI_INVALID_PARAMETER;
2222 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
2223 Mode = Private->PxeBc.Mode;
2225 if (!Mode->Started) {
2226 return EFI_NOT_STARTED;
2229 Private->FileSize = 0;
2231 if (NewDhcpDiscoverValid != NULL) {
2232 Mode->DhcpDiscoverValid = *NewDhcpDiscoverValid;
2235 if (NewDhcpAckReceived != NULL) {
2236 Mode->DhcpAckReceived = *NewDhcpAckReceived;
2239 if (NewProxyOfferReceived != NULL) {
2240 Mode->ProxyOfferReceived = *NewProxyOfferReceived;
2243 if (NewPxeDiscoverValid != NULL) {
2244 Mode->PxeDiscoverValid = *NewPxeDiscoverValid;
2247 if (NewPxeReplyReceived != NULL) {
2248 Mode->PxeReplyReceived = *NewPxeReplyReceived;
2251 if (NewPxeBisReplyReceived != NULL) {
2252 Mode->PxeBisReplyReceived = *NewPxeBisReplyReceived;
2255 if (NewDhcpDiscover != NULL) {
2256 CopyMem (&Mode->DhcpDiscover, NewDhcpDiscover, sizeof (EFI_PXE_BASE_CODE_PACKET));
2259 if (NewDhcpAck != NULL) {
2260 CopyMem (&Mode->DhcpAck, NewDhcpAck, sizeof (EFI_PXE_BASE_CODE_PACKET));
2263 if (NewProxyOffer != NULL) {
2264 CopyMem (&Mode->ProxyOffer, NewProxyOffer, sizeof (EFI_PXE_BASE_CODE_PACKET));
2267 if (NewPxeDiscover != NULL) {
2268 CopyMem (&Mode->PxeDiscover, NewPxeDiscover, sizeof (EFI_PXE_BASE_CODE_PACKET));
2271 if (NewPxeReply != NULL) {
2272 CopyMem (&Mode->PxeReply, NewPxeReply, sizeof (EFI_PXE_BASE_CODE_PACKET));
2275 if (NewPxeBisReply != NULL) {
2276 CopyMem (&Mode->PxeBisReply, NewPxeBisReply, sizeof (EFI_PXE_BASE_CODE_PACKET));
2279 return EFI_SUCCESS;
2282 EFI_PXE_BASE_CODE_PROTOCOL mPxeBcProtocolTemplate = {
2283 EFI_PXE_BASE_CODE_PROTOCOL_REVISION,
2284 EfiPxeBcStart,
2285 EfiPxeBcStop,
2286 EfiPxeBcDhcp,
2287 EfiPxeBcDiscover,
2288 EfiPxeBcMtftp,
2289 EfiPxeBcUdpWrite,
2290 EfiPxeBcUdpRead,
2291 EfiPxeBcSetIpFilter,
2292 EfiPxeBcArp,
2293 EfiPxeBcSetParameters,
2294 EfiPxeBcSetStationIP,
2295 EfiPxeBcSetPackets,
2296 NULL
2299 /**
2300 Callback function that is invoked when the PXE Base Code Protocol is about to transmit, has
2301 received, or is waiting to receive a packet.
2303 This function is invoked when the PXE Base Code Protocol is about to transmit, has received,
2304 or is waiting to receive a packet. Parameters Function and Received specify the type of event.
2305 Parameters PacketLen and Packet specify the packet that generated the event. If these fields
2306 are zero and NULL respectively, then this is a status update callback. If the operation specified
2307 by Function is to continue, then CALLBACK_STATUS_CONTINUE should be returned. If the operation
2308 specified by Function should be aborted, then CALLBACK_STATUS_ABORT should be returned. Due to
2309 the polling nature of UEFI device drivers, a callback function should not execute for more than 5 ms.
2310 The SetParameters() function must be called after a Callback Protocol is installed to enable the
2311 use of callbacks.
2313 @param This Pointer to the EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL instance.
2314 @param Function The PXE Base Code Protocol function that is waiting for an event.
2315 @param Received TRUE if the callback is being invoked due to a receive event. FALSE if
2316 the callback is being invoked due to a transmit event.
2317 @param PacketLength The length, in bytes, of Packet. This field will have a value of zero if
2318 this is a wait for receive event.
2319 @param PacketPtr If Received is TRUE, a pointer to the packet that was just received;
2320 otherwise a pointer to the packet that is about to be transmitted.
2322 @retval EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE if Function specifies a continue operation
2323 @retval EFI_PXE_BASE_CODE_CALLBACK_STATUS_ABORT if Function specifies an abort operation
2326 EFI_PXE_BASE_CODE_CALLBACK_STATUS
2327 EFIAPI
2328 EfiPxeLoadFileCallback (
2329 IN EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL * This,
2330 IN EFI_PXE_BASE_CODE_FUNCTION Function,
2331 IN BOOLEAN Received,
2332 IN UINT32 PacketLength,
2333 IN EFI_PXE_BASE_CODE_PACKET * PacketPtr OPTIONAL
2336 EFI_INPUT_KEY Key;
2337 EFI_STATUS Status;
2340 // Catch Ctrl-C or ESC to abort.
2342 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
2344 if (!EFI_ERROR (Status)) {
2346 if (Key.ScanCode == SCAN_ESC || Key.UnicodeChar == (0x1F & 'c')) {
2348 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_ABORT;
2352 // No print if receive packet
2354 if (Received) {
2355 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE;
2358 // Print only for three functions
2360 switch (Function) {
2362 case EFI_PXE_BASE_CODE_FUNCTION_MTFTP:
2364 // Print only for open MTFTP packets, not every MTFTP packets
2366 if (PacketLength != 0 && PacketPtr != NULL) {
2367 if (PacketPtr->Raw[0x1C] != 0x00 || PacketPtr->Raw[0x1D] != 0x01) {
2368 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE;
2371 break;
2373 case EFI_PXE_BASE_CODE_FUNCTION_DHCP:
2374 case EFI_PXE_BASE_CODE_FUNCTION_DISCOVER:
2375 break;
2377 default:
2378 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE;
2381 if (PacketLength != 0 && PacketPtr != NULL) {
2383 // Print '.' when transmit a packet
2385 AsciiPrint (".");
2389 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE;
2392 EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL mPxeBcCallBackTemplate = {
2393 EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL_REVISION,
2394 EfiPxeLoadFileCallback
2399 Find the boot file.
2401 @param Private Pointer to PxeBc private data.
2402 @param BufferSize Pointer to buffer size.
2403 @param Buffer Pointer to buffer.
2405 @retval EFI_SUCCESS Discover the boot file successfully.
2406 @retval EFI_TIMEOUT The TFTP/MTFTP operation timed out.
2407 @retval EFI_ABORTED PXE bootstrap server, so local boot need abort.
2408 @retval EFI_BUFFER_TOO_SMALL The buffer is too small to load the boot file.
2411 EFI_STATUS
2412 DiscoverBootFile (
2413 IN PXEBC_PRIVATE_DATA *Private,
2414 IN OUT UINT64 *BufferSize,
2415 IN VOID *Buffer
2418 EFI_PXE_BASE_CODE_PROTOCOL *PxeBc;
2419 EFI_PXE_BASE_CODE_MODE *Mode;
2420 EFI_STATUS Status;
2421 UINT16 Type;
2422 UINT16 Layer;
2423 BOOLEAN UseBis;
2424 UINTN BlockSize;
2425 PXEBC_CACHED_DHCP4_PACKET *Packet;
2426 UINT16 Value;
2428 PxeBc = &Private->PxeBc;
2429 Mode = PxeBc->Mode;
2430 Type = EFI_PXE_BASE_CODE_BOOT_TYPE_BOOTSTRAP;
2431 Layer = EFI_PXE_BASE_CODE_BOOT_LAYER_INITIAL;
2434 // do DHCP.
2436 Status = PxeBc->Dhcp (PxeBc, TRUE);
2437 if (EFI_ERROR (Status)) {
2438 return Status;
2442 // Select a boot server
2444 Status = PxeBcSelectBootPrompt (Private);
2446 if (Status == EFI_SUCCESS) {
2447 Status = PxeBcSelectBootMenu (Private, &Type, TRUE);
2448 } else if (Status == EFI_TIMEOUT) {
2449 Status = PxeBcSelectBootMenu (Private, &Type, FALSE);
2452 if (!EFI_ERROR (Status)) {
2454 if (Type == EFI_PXE_BASE_CODE_BOOT_TYPE_BOOTSTRAP) {
2456 // Local boot(PXE bootstrap server) need abort
2458 return EFI_ABORTED;
2461 UseBis = (BOOLEAN) (Mode->BisSupported && Mode->BisDetected);
2462 Status = PxeBc->Discover (PxeBc, Type, &Layer, UseBis, NULL);
2463 if (EFI_ERROR (Status)) {
2464 return Status;
2468 *BufferSize = 0;
2469 BlockSize = 0x8000;
2472 // Get bootfile name and (m)tftp server ip addresss
2474 if (Mode->PxeReplyReceived) {
2475 Packet = &Private->PxeReply;
2476 } else if (Mode->ProxyOfferReceived) {
2477 Packet = &Private->ProxyOffer;
2478 } else {
2479 Packet = &Private->Dhcp4Ack;
2483 // use option 54, if zero, use siaddr in header
2485 if (Packet->Dhcp4Option[PXEBC_DHCP4_TAG_INDEX_SERVER_ID] != NULL) {
2486 CopyMem (
2487 &Private->ServerIp,
2488 Packet->Dhcp4Option[PXEBC_DHCP4_TAG_INDEX_SERVER_ID]->Data,
2489 sizeof (EFI_IPv4_ADDRESS)
2491 } else {
2492 CopyMem (
2493 &Private->ServerIp,
2494 &Packet->Packet.Offer.Dhcp4.Header.ServerAddr,
2495 sizeof (EFI_IPv4_ADDRESS)
2498 if (Private->ServerIp.Addr[0] == 0) {
2499 return EFI_DEVICE_ERROR;
2502 ASSERT (Packet->Dhcp4Option[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] != NULL);
2505 // bootlfile name
2507 Private->BootFileName = (CHAR8 *) (Packet->Dhcp4Option[PXEBC_DHCP4_TAG_INDEX_BOOTFILE]->Data);
2509 if (Packet->Dhcp4Option[PXEBC_DHCP4_TAG_INDEX_BOOTFILE_LEN] != NULL) {
2511 // Already have the bootfile length option, compute the file size
2513 CopyMem (&Value, Packet->Dhcp4Option[PXEBC_DHCP4_TAG_INDEX_BOOTFILE_LEN]->Data, sizeof (Value));
2514 Value = NTOHS (Value);
2515 *BufferSize = 512 * Value;
2516 Status = EFI_BUFFER_TOO_SMALL;
2517 } else {
2519 // Get the bootfile size from tftp
2521 Status = PxeBc->Mtftp (
2522 PxeBc,
2523 EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE,
2524 Buffer,
2525 FALSE,
2526 BufferSize,
2527 &BlockSize,
2528 &Private->ServerIp,
2529 (UINT8 *) Private->BootFileName,
2530 NULL,
2531 FALSE
2535 Private->FileSize = (UINTN) *BufferSize;
2537 return Status;
2541 Causes the driver to load a specified file.
2543 @param This Protocol instance pointer.
2544 @param FilePath The device specific path of the file to load.
2545 @param BootPolicy If TRUE, indicates that the request originates from the
2546 boot manager is attempting to load FilePath as a boot
2547 selection. If FALSE, then FilePath must match as exact file
2548 to be loaded.
2549 @param BufferSize On input the size of Buffer in bytes. On output with a return
2550 code of EFI_SUCCESS, the amount of data transferred to
2551 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
2552 the size of Buffer required to retrieve the requested file.
2553 @param Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
2554 then no the size of the requested file is returned in
2555 BufferSize.
2557 @retval EFI_SUCCESS The file was loaded.
2558 @retval EFI_UNSUPPORTED The device does not support the provided BootPolicy
2559 @retval EFI_INVALID_PARAMETER FilePath is not a valid device path, or
2560 BufferSize is NULL.
2561 @retval EFI_NO_MEDIA No medium was present to load the file.
2562 @retval EFI_DEVICE_ERROR The file was not loaded due to a device error.
2563 @retval EFI_NO_RESPONSE The remote system did not respond.
2564 @retval EFI_NOT_FOUND The file was not found.
2565 @retval EFI_ABORTED The file load process was manually cancelled.
2568 EFI_STATUS
2569 EFIAPI
2570 EfiPxeLoadFile (
2571 IN EFI_LOAD_FILE_PROTOCOL * This,
2572 IN EFI_DEVICE_PATH_PROTOCOL * FilePath,
2573 IN BOOLEAN BootPolicy,
2574 IN OUT UINTN *BufferSize,
2575 IN VOID *Buffer OPTIONAL
2578 PXEBC_PRIVATE_DATA *Private;
2579 EFI_PXE_BASE_CODE_PROTOCOL *PxeBc;
2580 BOOLEAN NewMakeCallback;
2581 UINTN BlockSize;
2582 EFI_STATUS Status;
2583 UINT64 TmpBufSize;
2585 Private = PXEBC_PRIVATE_DATA_FROM_LOADFILE (This);
2586 PxeBc = &Private->PxeBc;
2587 NewMakeCallback = FALSE;
2588 BlockSize = 0x8000;
2589 Status = EFI_DEVICE_ERROR;
2591 if (This == NULL || BufferSize == NULL) {
2593 return EFI_INVALID_PARAMETER;
2597 // Only support BootPolicy
2599 if (!BootPolicy) {
2600 return EFI_UNSUPPORTED;
2603 Status = PxeBc->Start (PxeBc, FALSE);
2604 if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {
2605 return Status;
2608 Status = gBS->HandleProtocol (
2609 Private->Controller,
2610 &gEfiPxeBaseCodeCallbackProtocolGuid,
2611 (VOID **) &Private->PxeBcCallback
2613 if (Status == EFI_UNSUPPORTED) {
2615 CopyMem (&Private->LoadFileCallback, &mPxeBcCallBackTemplate, sizeof (Private->LoadFileCallback));
2617 Status = gBS->InstallProtocolInterface (
2618 &Private->Controller,
2619 &gEfiPxeBaseCodeCallbackProtocolGuid,
2620 EFI_NATIVE_INTERFACE,
2621 &Private->LoadFileCallback
2624 NewMakeCallback = (BOOLEAN) (Status == EFI_SUCCESS);
2626 Status = PxeBc->SetParameters (PxeBc, NULL, NULL, NULL, NULL, &NewMakeCallback);
2627 if (EFI_ERROR (Status)) {
2628 PxeBc->Stop (PxeBc);
2629 return Status;
2633 if (Private->FileSize == 0) {
2634 TmpBufSize = 0;
2635 Status = DiscoverBootFile (Private, &TmpBufSize, Buffer);
2637 if (sizeof (UINTN) < sizeof (UINT64) && (TmpBufSize > 0xFFFFFFFF)) {
2638 Status = EFI_DEVICE_ERROR;
2639 } else if (TmpBufSize > 0 && *BufferSize >= (UINTN) TmpBufSize && Buffer != NULL) {
2640 *BufferSize = (UINTN) TmpBufSize;
2641 Status = PxeBc->Mtftp (
2642 PxeBc,
2643 EFI_PXE_BASE_CODE_TFTP_READ_FILE,
2644 Buffer,
2645 FALSE,
2646 &TmpBufSize,
2647 &BlockSize,
2648 &Private->ServerIp,
2649 (UINT8 *) Private->BootFileName,
2650 NULL,
2651 FALSE
2653 } else if (TmpBufSize > 0) {
2654 *BufferSize = (UINTN) TmpBufSize;
2655 Status = EFI_BUFFER_TOO_SMALL;
2657 } else if (Buffer == NULL || Private->FileSize > *BufferSize) {
2658 *BufferSize = Private->FileSize;
2659 Status = EFI_BUFFER_TOO_SMALL;
2660 } else {
2662 // Download the file.
2664 TmpBufSize = (UINT64) (*BufferSize);
2665 Status = PxeBc->Mtftp (
2666 PxeBc,
2667 EFI_PXE_BASE_CODE_TFTP_READ_FILE,
2668 Buffer,
2669 FALSE,
2670 &TmpBufSize,
2671 &BlockSize,
2672 &Private->ServerIp,
2673 (UINT8 *) Private->BootFileName,
2674 NULL,
2675 FALSE
2679 // If we added a callback protocol, now is the time to remove it.
2681 if (NewMakeCallback) {
2683 NewMakeCallback = FALSE;
2685 PxeBc->SetParameters (PxeBc, NULL, NULL, NULL, NULL, &NewMakeCallback);
2687 gBS->UninstallProtocolInterface (
2688 Private->Controller,
2689 &gEfiPxeBaseCodeCallbackProtocolGuid,
2690 &Private->LoadFileCallback
2695 // Check download status
2697 if (Status == EFI_SUCCESS) {
2698 return EFI_SUCCESS;
2700 } else if (Status == EFI_BUFFER_TOO_SMALL) {
2701 if (Buffer != NULL) {
2702 AsciiPrint ("PXE-E05: Download buffer is smaller than requested file.\n");
2703 } else {
2704 return Status;
2707 } else if (Status == EFI_DEVICE_ERROR) {
2708 AsciiPrint ("PXE-E07: Network device error.\n");
2710 } else if (Status == EFI_OUT_OF_RESOURCES) {
2711 AsciiPrint ("PXE-E09: Could not allocate I/O buffers.\n");
2713 } else if (Status == EFI_NO_MEDIA) {
2714 AsciiPrint ("PXE-E12: Could not detect network connection.\n");
2716 } else if (Status == EFI_NO_RESPONSE) {
2717 AsciiPrint ("PXE-E16: No offer received.\n");
2719 } else if (Status == EFI_TIMEOUT) {
2720 AsciiPrint ("PXE-E18: Server response timeout.\n");
2722 } else if (Status == EFI_ABORTED) {
2723 AsciiPrint ("PXE-E21: Remote boot cancelled.\n");
2725 } else if (Status == EFI_ICMP_ERROR) {
2726 AsciiPrint ("PXE-E22: Client received ICMP error from server.\n");
2728 } else if (Status == EFI_TFTP_ERROR) {
2729 AsciiPrint ("PXE-E23: Client received TFTP error from server.\n");
2731 } else {
2732 AsciiPrint ("PXE-E99: Unexpected network error.\n");
2735 PxeBc->Stop (PxeBc);
2737 return Status;
2740 EFI_LOAD_FILE_PROTOCOL mLoadFileProtocolTemplate = { EfiPxeLoadFile };