VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/Firmware/NetworkPkg/UefiPxeBcDxe/PxeBcImpl.c

Last change on this file was 99404, checked in by vboxsync, 20 months ago

Devices/EFI/FirmwareNew: Update to edk2-stable202302 and make it build, bugref:4643

  • Property svn:eol-style set to native
File size: 85.6 KB
Line 
1/** @file
2 This implementation of EFI_PXE_BASE_CODE_PROTOCOL and EFI_LOAD_FILE_PROTOCOL.
3
4 Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8**/
9
10#include "PxeBcImpl.h"
11
12/**
13 Enables the use of the PXE Base Code Protocol functions.
14
15 This function enables the use of the PXE Base Code Protocol functions. If the
16 Started field of the EFI_PXE_BASE_CODE_MODE structure is already TRUE, then
17 EFI_ALREADY_STARTED will be returned. If UseIpv6 is TRUE, then IPv6 formatted
18 addresses will be used in this session. If UseIpv6 is FALSE, then IPv4 formatted
19 addresses will be used in this session. If UseIpv6 is TRUE, and the Ipv6Supported
20 field of the EFI_PXE_BASE_CODE_MODE structure is FALSE, then EFI_UNSUPPORTED will
21 be returned. If there is not enough memory or other resources to start the PXE
22 Base Code Protocol, then EFI_OUT_OF_RESOURCES will be returned. Otherwise, the
23 PXE Base Code Protocol will be started.
24
25 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
26 @param[in] UseIpv6 Specifies the type of IP addresses that are to be
27 used during the session that is being started.
28 Set to TRUE for IPv6, and FALSE for IPv4.
29
30 @retval EFI_SUCCESS The PXE Base Code Protocol was started.
31 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
32 @retval EFI_UNSUPPORTED UseIpv6 is TRUE, but the Ipv6Supported field of the
33 EFI_PXE_BASE_CODE_MODE structure is FALSE.
34 @retval EFI_ALREADY_STARTED The PXE Base Code Protocol is already in the started state.
35 @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid
36 EFI_PXE_BASE_CODE_PROTOCOL structure.
37 @retval EFI_OUT_OF_RESOURCES Could not allocate enough memory or other resources to start the
38 PXE Base Code Protocol.
39
40**/
41EFI_STATUS
42EFIAPI
43EfiPxeBcStart (
44 IN EFI_PXE_BASE_CODE_PROTOCOL *This,
45 IN BOOLEAN UseIpv6
46 )
47{
48 PXEBC_PRIVATE_DATA *Private;
49 EFI_PXE_BASE_CODE_MODE *Mode;
50 UINTN Index;
51 EFI_STATUS Status;
52
53 if (This == NULL) {
54 return EFI_INVALID_PARAMETER;
55 }
56
57 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
58 Mode = Private->PxeBc.Mode;
59
60 if (Mode->Started) {
61 return EFI_ALREADY_STARTED;
62 }
63
64 //
65 // Detect whether using IPv6 or not, and set it into mode data.
66 //
67 if (UseIpv6 && Mode->Ipv6Available && Mode->Ipv6Supported && (Private->Ip6Nic != NULL)) {
68 Mode->UsingIpv6 = TRUE;
69 } else if (!UseIpv6 && (Private->Ip4Nic != NULL)) {
70 Mode->UsingIpv6 = FALSE;
71 } else {
72 return EFI_UNSUPPORTED;
73 }
74
75 if (Mode->UsingIpv6) {
76 AsciiPrint ("\n>>Start PXE over IPv6");
77 //
78 // Configure udp6 instance to receive data.
79 //
80 Status = Private->Udp6Read->Configure (
81 Private->Udp6Read,
82 &Private->Udp6CfgData
83 );
84 if (EFI_ERROR (Status)) {
85 goto ON_ERROR;
86 }
87
88 //
89 // Configure block size for TFTP as a default value to handle all link layers.
90 //
91 Private->BlockSize = Private->Ip6MaxPacketSize -
92 PXEBC_DEFAULT_UDP_OVERHEAD_SIZE - PXEBC_DEFAULT_TFTP_OVERHEAD_SIZE;
93
94 //
95 // PXE over IPv6 starts here, initialize the fields and list header.
96 //
97 Private->Ip6Policy = PXEBC_IP6_POLICY_MAX;
98 Private->ProxyOffer.Dhcp6.Packet.Offer.Size = PXEBC_CACHED_DHCP6_PACKET_MAX_SIZE;
99 Private->DhcpAck.Dhcp6.Packet.Ack.Size = PXEBC_CACHED_DHCP6_PACKET_MAX_SIZE;
100 Private->PxeReply.Dhcp6.Packet.Ack.Size = PXEBC_CACHED_DHCP6_PACKET_MAX_SIZE;
101
102 for (Index = 0; Index < PXEBC_OFFER_MAX_NUM; Index++) {
103 Private->OfferBuffer[Index].Dhcp6.Packet.Offer.Size = PXEBC_CACHED_DHCP6_PACKET_MAX_SIZE;
104 }
105
106 //
107 // Create event and set status for token to capture ICMP6 error message.
108 //
109 Private->Icmp6Token.Status = EFI_NOT_READY;
110 Status = gBS->CreateEvent (
111 EVT_NOTIFY_SIGNAL,
112 TPL_NOTIFY,
113 PxeBcIcmp6ErrorUpdate,
114 Private,
115 &Private->Icmp6Token.Event
116 );
117 if (EFI_ERROR (Status)) {
118 goto ON_ERROR;
119 }
120
121 //
122 // Set Ip6 policy to Automatic to start the IP6 router discovery.
123 //
124 Status = PxeBcSetIp6Policy (Private);
125 if (EFI_ERROR (Status)) {
126 goto ON_ERROR;
127 }
128 } else {
129 AsciiPrint ("\n>>Start PXE over IPv4");
130 //
131 // Configure udp4 instance to receive data.
132 //
133 Status = Private->Udp4Read->Configure (
134 Private->Udp4Read,
135 &Private->Udp4CfgData
136 );
137 if (EFI_ERROR (Status)) {
138 goto ON_ERROR;
139 }
140
141 //
142 // Configure block size for TFTP as a default value to handle all link layers.
143 //
144 Private->BlockSize = Private->Ip4MaxPacketSize -
145 PXEBC_DEFAULT_UDP_OVERHEAD_SIZE - PXEBC_DEFAULT_TFTP_OVERHEAD_SIZE;
146
147 //
148 // PXE over IPv4 starts here, initialize the fields.
149 //
150 Private->ProxyOffer.Dhcp4.Packet.Offer.Size = PXEBC_CACHED_DHCP4_PACKET_MAX_SIZE;
151 Private->DhcpAck.Dhcp4.Packet.Ack.Size = PXEBC_CACHED_DHCP4_PACKET_MAX_SIZE;
152 Private->PxeReply.Dhcp4.Packet.Ack.Size = PXEBC_CACHED_DHCP4_PACKET_MAX_SIZE;
153
154 for (Index = 0; Index < PXEBC_OFFER_MAX_NUM; Index++) {
155 Private->OfferBuffer[Index].Dhcp4.Packet.Offer.Size = PXEBC_CACHED_DHCP4_PACKET_MAX_SIZE;
156 }
157
158 PxeBcSeedDhcp4Packet (&Private->SeedPacket, Private->Udp4Read);
159
160 //
161 // Create the event for Arp cache update.
162 //
163 Status = gBS->CreateEvent (
164 EVT_TIMER | EVT_NOTIFY_SIGNAL,
165 TPL_CALLBACK,
166 PxeBcArpCacheUpdate,
167 Private,
168 &Private->ArpUpdateEvent
169 );
170 if (EFI_ERROR (Status)) {
171 goto ON_ERROR;
172 }
173
174 //
175 // Start a periodic timer by second to update Arp cache.
176 //
177 Status = gBS->SetTimer (
178 Private->ArpUpdateEvent,
179 TimerPeriodic,
180 TICKS_PER_SECOND
181 );
182 if (EFI_ERROR (Status)) {
183 goto ON_ERROR;
184 }
185
186 //
187 // Create event and set status for token to capture ICMP error message.
188 //
189 Private->Icmp6Token.Status = EFI_NOT_READY;
190 Status = gBS->CreateEvent (
191 EVT_NOTIFY_SIGNAL,
192 TPL_NOTIFY,
193 PxeBcIcmpErrorUpdate,
194 Private,
195 &Private->IcmpToken.Event
196 );
197 if (EFI_ERROR (Status)) {
198 goto ON_ERROR;
199 }
200
201 //
202 // DHCP4 service allows only one of its children to be configured in
203 // the active state, If the DHCP4 D.O.R.A started by IP4 auto
204 // configuration and has not been completed, the Dhcp4 state machine
205 // will not be in the right state for the PXE to start a new round D.O.R.A.
206 // so we need to switch its policy to static.
207 //
208 Status = PxeBcSetIp4Policy (Private);
209 if (EFI_ERROR (Status)) {
210 goto ON_ERROR;
211 }
212 }
213
214 //
215 // If PcdTftpBlockSize is set to non-zero, override the default value.
216 //
217 if (PcdGet64 (PcdTftpBlockSize) != 0) {
218 Private->BlockSize = (UINTN)PcdGet64 (PcdTftpBlockSize);
219 }
220
221 //
222 // Create event for UdpRead/UdpWrite timeout since they are both blocking API.
223 //
224 Status = gBS->CreateEvent (
225 EVT_TIMER,
226 TPL_CALLBACK,
227 NULL,
228 NULL,
229 &Private->UdpTimeOutEvent
230 );
231 if (EFI_ERROR (Status)) {
232 goto ON_ERROR;
233 }
234
235 Private->IsAddressOk = FALSE;
236 Mode->Started = TRUE;
237
238 return EFI_SUCCESS;
239
240ON_ERROR:
241 if (Mode->UsingIpv6) {
242 if (Private->Icmp6Token.Event != NULL) {
243 gBS->CloseEvent (Private->Icmp6Token.Event);
244 Private->Icmp6Token.Event = NULL;
245 }
246
247 Private->Udp6Read->Configure (Private->Udp6Read, NULL);
248 Private->Ip6->Configure (Private->Ip6, NULL);
249 } else {
250 if (Private->ArpUpdateEvent != NULL) {
251 gBS->CloseEvent (Private->ArpUpdateEvent);
252 Private->ArpUpdateEvent = NULL;
253 }
254
255 if (Private->IcmpToken.Event != NULL) {
256 gBS->CloseEvent (Private->IcmpToken.Event);
257 Private->IcmpToken.Event = NULL;
258 }
259
260 Private->Udp4Read->Configure (Private->Udp4Read, NULL);
261 Private->Ip4->Configure (Private->Ip4, NULL);
262 }
263
264 return Status;
265}
266
267/**
268 Disable the use of the PXE Base Code Protocol functions.
269
270 This function stops all activity on the network device. All the resources allocated
271 in Start() are released, the Started field of the EFI_PXE_BASE_CODE_MODE structure is
272 set to FALSE, and EFI_SUCCESS is returned. If the Started field of the EFI_PXE_BASE_CODE_MODE
273 structure is already FALSE, then EFI_NOT_STARTED will be returned.
274
275 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
276
277 @retval EFI_SUCCESS The PXE Base Code Protocol was stopped.
278 @retval EFI_NOT_STARTED The PXE Base Code Protocol is already in the stopped state.
279 @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid
280 EFI_PXE_BASE_CODE_PROTOCOL structure.
281 @retval Others
282
283**/
284EFI_STATUS
285EFIAPI
286EfiPxeBcStop (
287 IN EFI_PXE_BASE_CODE_PROTOCOL *This
288 )
289{
290 PXEBC_PRIVATE_DATA *Private;
291 EFI_PXE_BASE_CODE_MODE *Mode;
292 BOOLEAN Ipv6Supported;
293 BOOLEAN Ipv6Available;
294
295 if (This == NULL) {
296 return EFI_INVALID_PARAMETER;
297 }
298
299 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
300 Mode = Private->PxeBc.Mode;
301 Ipv6Supported = Mode->Ipv6Supported;
302 Ipv6Available = Mode->Ipv6Available;
303
304 if (!Mode->Started) {
305 return EFI_NOT_STARTED;
306 }
307
308 if (Mode->UsingIpv6) {
309 //
310 // Configure all the instances for IPv6 as NULL.
311 //
312 ZeroMem (&Private->Udp6CfgData.StationAddress, sizeof (EFI_IPv6_ADDRESS));
313 ZeroMem (&Private->Ip6CfgData.StationAddress, sizeof (EFI_IPv6_ADDRESS));
314 Private->Dhcp6->Stop (Private->Dhcp6);
315 Private->Dhcp6->Configure (Private->Dhcp6, NULL);
316 Private->Udp6Write->Configure (Private->Udp6Write, NULL);
317 Private->Udp6Read->Groups (Private->Udp6Read, FALSE, NULL);
318 Private->Udp6Read->Configure (Private->Udp6Read, NULL);
319 Private->Ip6->Cancel (Private->Ip6, &Private->Icmp6Token);
320 Private->Ip6->Configure (Private->Ip6, NULL);
321 PxeBcUnregisterIp6Address (Private);
322 if (Private->Icmp6Token.Event != NULL) {
323 gBS->CloseEvent (Private->Icmp6Token.Event);
324 Private->Icmp6Token.Event = NULL;
325 }
326
327 if (Private->Dhcp6Request != NULL) {
328 FreePool (Private->Dhcp6Request);
329 Private->Dhcp6Request = NULL;
330 }
331
332 if (Private->BootFileName != NULL) {
333 FreePool (Private->BootFileName);
334 Private->BootFileName = NULL;
335 }
336 } else {
337 //
338 // Configure all the instances for IPv4 as NULL.
339 //
340 ZeroMem (&Private->Udp4CfgData.StationAddress, sizeof (EFI_IPv4_ADDRESS));
341 ZeroMem (&Private->Udp4CfgData.SubnetMask, sizeof (EFI_IPv4_ADDRESS));
342 ZeroMem (&Private->Ip4CfgData.StationAddress, sizeof (EFI_IPv4_ADDRESS));
343 ZeroMem (&Private->Ip4CfgData.SubnetMask, sizeof (EFI_IPv4_ADDRESS));
344 Private->Dhcp4->Stop (Private->Dhcp4);
345 Private->Dhcp4->Configure (Private->Dhcp4, NULL);
346 Private->Udp4Write->Configure (Private->Udp4Write, NULL);
347 Private->Udp4Read->Groups (Private->Udp4Read, FALSE, NULL);
348 Private->Udp4Read->Configure (Private->Udp4Read, NULL);
349 Private->Ip4->Cancel (Private->Ip4, &Private->IcmpToken);
350 Private->Ip4->Configure (Private->Ip4, NULL);
351 if (Private->ArpUpdateEvent != NULL) {
352 gBS->CloseEvent (Private->ArpUpdateEvent);
353 Private->ArpUpdateEvent = NULL;
354 }
355
356 if (Private->IcmpToken.Event != NULL) {
357 gBS->CloseEvent (Private->IcmpToken.Event);
358 Private->IcmpToken.Event = NULL;
359 }
360
361 Private->BootFileName = NULL;
362 }
363
364 gBS->CloseEvent (Private->UdpTimeOutEvent);
365 Private->CurSrcPort = 0;
366 Private->BootFileSize = 0;
367 Private->SolicitTimes = 0;
368 Private->ElapsedTime = 0;
369 ZeroMem (&Private->StationIp, sizeof (EFI_IP_ADDRESS));
370 ZeroMem (&Private->SubnetMask, sizeof (EFI_IP_ADDRESS));
371 ZeroMem (&Private->GatewayIp, sizeof (EFI_IP_ADDRESS));
372 ZeroMem (&Private->ServerIp, sizeof (EFI_IP_ADDRESS));
373
374 //
375 // Reset the mode data.
376 //
377 ZeroMem (Mode, sizeof (EFI_PXE_BASE_CODE_MODE));
378 Mode->Ipv6Available = Ipv6Available;
379 Mode->Ipv6Supported = Ipv6Supported;
380 Mode->AutoArp = TRUE;
381 Mode->TTL = DEFAULT_TTL;
382 Mode->ToS = DEFAULT_ToS;
383
384 return EFI_SUCCESS;
385}
386
387/**
388 Attempts to complete a DHCPv4 D.O.R.A. (discover / offer / request / acknowledge) or DHCPv6
389 S.A.R.R (solicit / advertise / request / reply) sequence.
390
391 If SortOffers is TRUE, then the cached DHCP offer packets will be sorted before
392 they are tried. If SortOffers is FALSE, then the cached DHCP offer packets will
393 be tried in the order in which they are received. Please see the Preboot Execution
394 Environment (PXE) Specification and Unified Extensible Firmware Interface (UEFI)
395 Specification for additional details on the implementation of DHCP.
396 If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,
397 then the DHCP sequence will be stopped and EFI_ABORTED will be returned.
398
399 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
400 @param[in] SortOffers TRUE if the offers received should be sorted. Set to FALSE to
401 try the offers in the order that they are received.
402
403 @retval EFI_SUCCESS Valid DHCP has completed.
404 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
405 @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid
406 EFI_PXE_BASE_CODE_PROTOCOL structure.
407 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
408 @retval EFI_OUT_OF_RESOURCES Could not allocate enough memory to complete the DHCP Protocol.
409 @retval EFI_ABORTED The callback function aborted the DHCP Protocol.
410 @retval EFI_TIMEOUT The DHCP Protocol timed out.
411 @retval EFI_ICMP_ERROR An ICMP error packet was received during the DHCP session.
412 @retval EFI_NO_RESPONSE Valid PXE offer was not received.
413
414**/
415EFI_STATUS
416EFIAPI
417EfiPxeBcDhcp (
418 IN EFI_PXE_BASE_CODE_PROTOCOL *This,
419 IN BOOLEAN SortOffers
420 )
421{
422 PXEBC_PRIVATE_DATA *Private;
423 EFI_PXE_BASE_CODE_MODE *Mode;
424 EFI_STATUS Status;
425 EFI_PXE_BASE_CODE_IP_FILTER IpFilter;
426
427 if (This == NULL) {
428 return EFI_INVALID_PARAMETER;
429 }
430
431 Status = EFI_SUCCESS;
432 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
433 Mode = Private->PxeBc.Mode;
434 Mode->IcmpErrorReceived = FALSE;
435 Private->Function = EFI_PXE_BASE_CODE_FUNCTION_DHCP;
436 Private->IsOfferSorted = SortOffers;
437 Private->SolicitTimes = 0;
438 Private->ElapsedTime = 0;
439
440 if (!Mode->Started) {
441 return EFI_NOT_STARTED;
442 }
443
444 if (Mode->UsingIpv6) {
445 //
446 // Stop Udp6Read instance
447 //
448 Private->Udp6Read->Configure (Private->Udp6Read, NULL);
449
450 //
451 // Start S.A.R.R. process to get a IPv6 address and other boot information.
452 //
453 Status = PxeBcDhcp6Sarr (Private, Private->Dhcp6);
454 } else {
455 //
456 // Stop Udp4Read instance
457 //
458 Private->Udp4Read->Configure (Private->Udp4Read, NULL);
459
460 //
461 // Start D.O.R.A. process to get a IPv4 address and other boot information.
462 //
463 Status = PxeBcDhcp4Dora (Private, Private->Dhcp4);
464 }
465
466 //
467 // Reconfigure the UDP instance with the default configuration.
468 //
469 if (Mode->UsingIpv6) {
470 Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData);
471 } else {
472 Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);
473 }
474
475 //
476 // Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP
477 // receive filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.
478 //
479 ZeroMem (&IpFilter, sizeof (EFI_PXE_BASE_CODE_IP_FILTER));
480 IpFilter.Filters = EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP;
481 This->SetIpFilter (This, &IpFilter);
482
483 return Status;
484}
485
486/**
487 Attempts to complete the PXE Boot Server and/or boot image discovery sequence.
488
489 This function attempts to complete the PXE Boot Server and/or boot image discovery
490 sequence. If this sequence is completed, then EFI_SUCCESS is returned, and the
491 PxeDiscoverValid, PxeDiscover, PxeReplyReceived, and PxeReply fields of the
492 EFI_PXE_BASE_CODE_MODE structure are filled in. If UseBis is TRUE, then the
493 PxeBisReplyReceived and PxeBisReply fields of the EFI_PXE_BASE_CODE_MODE structure
494 will also be filled in. If UseBis is FALSE, then PxeBisReplyValid will be set to FALSE.
495 In the structure referenced by parameter Info, the PXE Boot Server list, SrvList[],
496 has two uses: It is the Boot Server IP address list used for unicast discovery
497 (if the UseUCast field is TRUE), and it is the list used for Boot Server verification
498 (if the MustUseList field is TRUE). Also, if the MustUseList field in that structure
499 is TRUE and the AcceptAnyResponse field in the SrvList[] array is TRUE, any Boot
500 Server reply of that type will be accepted. If the AcceptAnyResponse field is
501 FALSE, only responses from Boot Servers with matching IP addresses will be accepted.
502 This function can take at least 10 seconds to timeout and return control to the
503 caller. If the Discovery sequence does not complete, then EFI_TIMEOUT will be
504 returned. Please see the Preboot Execution Environment (PXE) Specification for
505 additional details on the implementation of the Discovery sequence.
506 If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,
507 then the Discovery sequence is stopped and EFI_ABORTED will be returned.
508
509 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
510 @param[in] Type The type of bootstrap to perform.
511 @param[in] Layer Pointer to the boot server layer number to discover, which must be
512 PXE_BOOT_LAYER_INITIAL when a new server type is being
513 discovered.
514 @param[in] UseBis TRUE if Boot Integrity Services are to be used. FALSE otherwise.
515 @param[in] Info Pointer to a data structure that contains additional information
516 on the type of discovery operation that is to be performed.
517 It is optional.
518
519 @retval EFI_SUCCESS The Discovery sequence has been completed.
520 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
521 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
522 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
523 @retval EFI_OUT_OF_RESOURCES Could not allocate enough memory to complete Discovery.
524 @retval EFI_ABORTED The callback function aborted the Discovery sequence.
525 @retval EFI_TIMEOUT The Discovery sequence timed out.
526 @retval EFI_ICMP_ERROR An ICMP error packet was received during the PXE discovery
527 session.
528
529**/
530EFI_STATUS
531EFIAPI
532EfiPxeBcDiscover (
533 IN EFI_PXE_BASE_CODE_PROTOCOL *This,
534 IN UINT16 Type,
535 IN UINT16 *Layer,
536 IN BOOLEAN UseBis,
537 IN EFI_PXE_BASE_CODE_DISCOVER_INFO *Info OPTIONAL
538 )
539{
540 PXEBC_PRIVATE_DATA *Private;
541 EFI_PXE_BASE_CODE_MODE *Mode;
542 EFI_PXE_BASE_CODE_DISCOVER_INFO DefaultInfo;
543 EFI_PXE_BASE_CODE_SRVLIST *SrvList;
544 PXEBC_BOOT_SVR_ENTRY *BootSvrEntry;
545 UINT16 Index;
546 EFI_STATUS Status;
547 EFI_PXE_BASE_CODE_IP_FILTER IpFilter;
548 EFI_PXE_BASE_CODE_DISCOVER_INFO *NewCreatedInfo;
549
550 if (This == NULL) {
551 return EFI_INVALID_PARAMETER;
552 }
553
554 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
555 Mode = Private->PxeBc.Mode;
556 Mode->IcmpErrorReceived = FALSE;
557 BootSvrEntry = NULL;
558 SrvList = NULL;
559 Status = EFI_DEVICE_ERROR;
560 Private->Function = EFI_PXE_BASE_CODE_FUNCTION_DISCOVER;
561 NewCreatedInfo = NULL;
562
563 if (!Mode->Started) {
564 return EFI_NOT_STARTED;
565 }
566
567 //
568 // Station address should be ready before do discover.
569 //
570 if (!Private->IsAddressOk) {
571 return EFI_INVALID_PARAMETER;
572 }
573
574 if (Mode->UsingIpv6) {
575 //
576 // Stop Udp6Read instance
577 //
578 Private->Udp6Read->Configure (Private->Udp6Read, NULL);
579 } else {
580 //
581 // Stop Udp4Read instance
582 //
583 Private->Udp4Read->Configure (Private->Udp4Read, NULL);
584 }
585
586 //
587 // There are 3 methods to get the information for discover.
588 //
589 ZeroMem (&DefaultInfo, sizeof (EFI_PXE_BASE_CODE_DISCOVER_INFO));
590 if (*Layer != EFI_PXE_BASE_CODE_BOOT_LAYER_INITIAL) {
591 //
592 // 1. Take the previous setting as the discover info.
593 //
594 if (!Mode->PxeDiscoverValid ||
595 !Mode->PxeReplyReceived ||
596 (!Mode->PxeBisReplyReceived && UseBis))
597 {
598 Status = EFI_INVALID_PARAMETER;
599 goto ON_EXIT;
600 }
601
602 Info = &DefaultInfo;
603 Info->IpCnt = 1;
604 Info->UseUCast = TRUE;
605 SrvList = Info->SrvList;
606 SrvList[0].Type = Type;
607 SrvList[0].AcceptAnyResponse = FALSE;
608
609 CopyMem (&SrvList->IpAddr, &Private->ServerIp, sizeof (EFI_IP_ADDRESS));
610 } else if (Info == NULL) {
611 //
612 // 2. Extract the discover information from the cached packets if unspecified.
613 //
614 NewCreatedInfo = &DefaultInfo;
615 Status = PxeBcExtractDiscoverInfo (Private, Type, &NewCreatedInfo, &BootSvrEntry, &SrvList);
616 if (EFI_ERROR (Status)) {
617 goto ON_EXIT;
618 }
619
620 ASSERT (NewCreatedInfo != NULL);
621 Info = NewCreatedInfo;
622 } else {
623 //
624 // 3. Take the pass-in information as the discover info, and validate the server list.
625 //
626 SrvList = Info->SrvList;
627
628 if (!SrvList[0].AcceptAnyResponse) {
629 for (Index = 1; Index < Info->IpCnt; Index++) {
630 if (SrvList[Index].AcceptAnyResponse) {
631 break;
632 }
633 }
634
635 if (Index != Info->IpCnt) {
636 //
637 // It's invalid if the first server doesn't accept any response
638 // but any of the other servers does accept any response.
639 //
640 Status = EFI_INVALID_PARAMETER;
641 goto ON_EXIT;
642 }
643 }
644 }
645
646 //
647 // Info and BootSvrEntry/SrvList are all ready by now, so execute discover by UniCast/BroadCast/MultiCast.
648 //
649 if ((!Info->UseUCast && !Info->UseBCast && !Info->UseMCast) ||
650 (Info->MustUseList && (Info->IpCnt == 0)))
651 {
652 Status = EFI_INVALID_PARAMETER;
653 goto ON_EXIT;
654 }
655
656 Private->IsDoDiscover = TRUE;
657
658 if (Info->UseMCast) {
659 //
660 // Do discover by multicast.
661 //
662 Status = PxeBcDiscoverBootServer (
663 Private,
664 Type,
665 Layer,
666 UseBis,
667 &Info->ServerMCastIp,
668 Info->IpCnt,
669 SrvList
670 );
671 } else if (Info->UseBCast) {
672 //
673 // Do discover by broadcast, but only valid for IPv4.
674 //
675 ASSERT (!Mode->UsingIpv6);
676 Status = PxeBcDiscoverBootServer (
677 Private,
678 Type,
679 Layer,
680 UseBis,
681 NULL,
682 Info->IpCnt,
683 SrvList
684 );
685 } else if (Info->UseUCast) {
686 //
687 // Do discover by unicast.
688 //
689 for (Index = 0; Index < Info->IpCnt; Index++) {
690 if (BootSvrEntry == NULL) {
691 CopyMem (&Private->ServerIp, &SrvList[Index].IpAddr, sizeof (EFI_IP_ADDRESS));
692 } else {
693 ASSERT (!Mode->UsingIpv6);
694 ZeroMem (&Private->ServerIp, sizeof (EFI_IP_ADDRESS));
695 CopyMem (&Private->ServerIp, &BootSvrEntry->IpAddr[Index], sizeof (EFI_IPv4_ADDRESS));
696 }
697
698 Status = PxeBcDiscoverBootServer (
699 Private,
700 Type,
701 Layer,
702 UseBis,
703 &Private->ServerIp,
704 Info->IpCnt,
705 SrvList
706 );
707 }
708 }
709
710 if (!EFI_ERROR (Status)) {
711 //
712 // Parse the cached PXE reply packet, and store it into mode data if valid.
713 //
714 if (Mode->UsingIpv6) {
715 Status = PxeBcParseDhcp6Packet (&Private->PxeReply.Dhcp6);
716 if (!EFI_ERROR (Status)) {
717 CopyMem (
718 &Mode->PxeReply.Dhcpv6,
719 &Private->PxeReply.Dhcp6.Packet.Ack.Dhcp6,
720 Private->PxeReply.Dhcp6.Packet.Ack.Length
721 );
722 Mode->PxeReplyReceived = TRUE;
723 Mode->PxeDiscoverValid = TRUE;
724 }
725 } else {
726 Status = PxeBcParseDhcp4Packet (&Private->PxeReply.Dhcp4);
727 if (!EFI_ERROR (Status)) {
728 CopyMem (
729 &Mode->PxeReply.Dhcpv4,
730 &Private->PxeReply.Dhcp4.Packet.Ack.Dhcp4,
731 Private->PxeReply.Dhcp4.Packet.Ack.Length
732 );
733 Mode->PxeReplyReceived = TRUE;
734 Mode->PxeDiscoverValid = TRUE;
735 }
736 }
737 }
738
739ON_EXIT:
740
741 if ((NewCreatedInfo != NULL) && (NewCreatedInfo != &DefaultInfo)) {
742 FreePool (NewCreatedInfo);
743 }
744
745 if (Mode->UsingIpv6) {
746 Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData);
747 } else {
748 Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);
749 }
750
751 //
752 // Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP
753 // receive filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.
754 //
755 ZeroMem (&IpFilter, sizeof (EFI_PXE_BASE_CODE_IP_FILTER));
756 IpFilter.Filters = EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP;
757 This->SetIpFilter (This, &IpFilter);
758
759 return Status;
760}
761
762/**
763 Used to perform TFTP and MTFTP services.
764
765 This function is used to perform TFTP and MTFTP services. This includes the
766 TFTP operations to get the size of a file, read a directory, read a file, and
767 write a file. It also includes the MTFTP operations to get the size of a file,
768 read a directory, and read a file. The type of operation is specified by Operation.
769 If the callback function that is invoked during the TFTP/MTFTP operation does
770 not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE, then EFI_ABORTED will
771 be returned.
772 For read operations, the return data will be placed in the buffer specified by
773 BufferPtr. If BufferSize is too small to contain the entire downloaded file,
774 then EFI_BUFFER_TOO_SMALL will be returned and BufferSize will be set to zero,
775 or the size of the requested file. (NOTE: the size of the requested file is only returned
776 if the TFTP server supports TFTP options). If BufferSize is large enough for the
777 read operation, then BufferSize will be set to the size of the downloaded file,
778 and EFI_SUCCESS will be returned. Applications using the PxeBc.Mtftp() services
779 should use the get-file-size operations to determine the size of the downloaded
780 file prior to using the read-file operations-especially when downloading large
781 (greater than 64 MB) files-instead of making two calls to the read-file operation.
782 Following this recommendation will save time if the file is larger than expected
783 and the TFTP server does not support TFTP option extensions. Without TFTP option
784 extension support, the client must download the entire file, counting and discarding
785 the received packets, to determine the file size.
786 For write operations, the data to be sent is in the buffer specified by BufferPtr.
787 BufferSize specifies the number of bytes to send. If the write operation completes
788 successfully, then EFI_SUCCESS will be returned.
789 For TFTP "get file size" operations, the size of the requested file or directory
790 is returned in BufferSize, and EFI_SUCCESS will be returned. If the TFTP server
791 does not support options, the file will be downloaded into a bit bucket and the
792 length of the downloaded file will be returned. For MTFTP "get file size" operations,
793 if the MTFTP server does not support the "get file size" option, EFI_UNSUPPORTED
794 will be returned.
795 This function can take up to 10 seconds to timeout and return control to the caller.
796 If the TFTP sequence does not complete, EFI_TIMEOUT will be returned.
797 If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,
798 then the TFTP sequence is stopped and EFI_ABORTED will be returned.
799
800 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
801 @param[in] Operation The type of operation to perform.
802 @param[in, out] BufferPtr A pointer to the data buffer.
803 @param[in] Overwrite Only used on write file operations. TRUE if a file on a remote
804 server can be overwritten.
805 @param[in, out] BufferSize For get-file-size operations, *BufferSize returns the size of the
806 requested file.
807 @param[in] BlockSize The requested block size to be used during a TFTP transfer.
808 @param[in] ServerIp The TFTP / MTFTP server IP address.
809 @param[in] Filename A Null-terminated ASCII string that specifies a directory name
810 or a file name.
811 @param[in] Info Pointer to the MTFTP information.
812 @param[in] DontUseBuffer Set to FALSE for normal TFTP and MTFTP read file operation.
813
814 @retval EFI_SUCCESS The TFTP/MTFTP operation was completed.
815 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
816 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
817 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
818 @retval EFI_BUFFER_TOO_SMALL The buffer is not large enough to complete the read operation.
819 @retval EFI_ABORTED The callback function aborted the TFTP/MTFTP operation.
820 @retval EFI_TIMEOUT The TFTP/MTFTP operation timed out.
821 @retval EFI_ICMP_ERROR An ICMP error packet was received during the MTFTP session.
822 @retval EFI_TFTP_ERROR A TFTP error packet was received during the MTFTP session.
823
824**/
825EFI_STATUS
826EFIAPI
827EfiPxeBcMtftp (
828 IN EFI_PXE_BASE_CODE_PROTOCOL *This,
829 IN EFI_PXE_BASE_CODE_TFTP_OPCODE Operation,
830 IN OUT VOID *BufferPtr OPTIONAL,
831 IN BOOLEAN Overwrite,
832 IN OUT UINT64 *BufferSize,
833 IN UINTN *BlockSize OPTIONAL,
834 IN EFI_IP_ADDRESS *ServerIp,
835 IN UINT8 *Filename,
836 IN EFI_PXE_BASE_CODE_MTFTP_INFO *Info OPTIONAL,
837 IN BOOLEAN DontUseBuffer
838 )
839{
840 PXEBC_PRIVATE_DATA *Private;
841 EFI_PXE_BASE_CODE_MODE *Mode;
842 EFI_MTFTP4_CONFIG_DATA Mtftp4Config;
843 EFI_MTFTP6_CONFIG_DATA Mtftp6Config;
844 VOID *Config;
845 EFI_STATUS Status;
846 EFI_PXE_BASE_CODE_IP_FILTER IpFilter;
847 UINTN WindowSize;
848
849 if ((This == NULL) ||
850 (Filename == NULL) ||
851 (BufferSize == NULL) ||
852 (ServerIp == NULL) ||
853 ((BlockSize != NULL) && (*BlockSize < PXE_MTFTP_DEFAULT_BLOCK_SIZE)))
854 {
855 return EFI_INVALID_PARAMETER;
856 }
857
858 if ((Operation == EFI_PXE_BASE_CODE_TFTP_READ_FILE) ||
859 (Operation == EFI_PXE_BASE_CODE_TFTP_READ_DIRECTORY) ||
860 (Operation == EFI_PXE_BASE_CODE_MTFTP_READ_FILE) ||
861 (Operation == EFI_PXE_BASE_CODE_MTFTP_READ_DIRECTORY))
862 {
863 if ((BufferPtr == NULL) && !DontUseBuffer) {
864 return EFI_INVALID_PARAMETER;
865 }
866 }
867
868 Config = NULL;
869 Status = EFI_DEVICE_ERROR;
870 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
871 Mode = Private->PxeBc.Mode;
872
873 //
874 // Get PcdPxeTftpWindowSize.
875 //
876 WindowSize = (UINTN)PcdGet64 (PcdPxeTftpWindowSize);
877
878 if (Mode->UsingIpv6) {
879 if (!NetIp6IsValidUnicast (&ServerIp->v6)) {
880 return EFI_INVALID_PARAMETER;
881 }
882 } else {
883 if (IP4_IS_UNSPECIFIED (NTOHL (ServerIp->Addr[0])) || IP4_IS_LOCAL_BROADCAST (NTOHL (ServerIp->Addr[0]))) {
884 return EFI_INVALID_PARAMETER;
885 }
886 }
887
888 if (Mode->UsingIpv6) {
889 //
890 // Set configuration data for Mtftp6 instance.
891 //
892 ZeroMem (&Mtftp6Config, sizeof (EFI_MTFTP6_CONFIG_DATA));
893 Config = &Mtftp6Config;
894 Mtftp6Config.TimeoutValue = PXEBC_MTFTP_TIMEOUT;
895 Mtftp6Config.TryCount = PXEBC_MTFTP_RETRIES;
896 CopyMem (&Mtftp6Config.StationIp, &Private->StationIp.v6, sizeof (EFI_IPv6_ADDRESS));
897 CopyMem (&Mtftp6Config.ServerIp, &ServerIp->v6, sizeof (EFI_IPv6_ADDRESS));
898 //
899 // Stop Udp6Read instance
900 //
901 Private->Udp6Read->Configure (Private->Udp6Read, NULL);
902 } else {
903 //
904 // Set configuration data for Mtftp4 instance.
905 //
906 ZeroMem (&Mtftp4Config, sizeof (EFI_MTFTP4_CONFIG_DATA));
907 Config = &Mtftp4Config;
908 Mtftp4Config.UseDefaultSetting = FALSE;
909 Mtftp4Config.TimeoutValue = PXEBC_MTFTP_TIMEOUT;
910 Mtftp4Config.TryCount = PXEBC_MTFTP_RETRIES;
911 CopyMem (&Mtftp4Config.StationIp, &Private->StationIp.v4, sizeof (EFI_IPv4_ADDRESS));
912 CopyMem (&Mtftp4Config.SubnetMask, &Private->SubnetMask.v4, sizeof (EFI_IPv4_ADDRESS));
913 CopyMem (&Mtftp4Config.GatewayIp, &Private->GatewayIp.v4, sizeof (EFI_IPv4_ADDRESS));
914 CopyMem (&Mtftp4Config.ServerIp, &ServerIp->v4, sizeof (EFI_IPv4_ADDRESS));
915 //
916 // Stop Udp4Read instance
917 //
918 Private->Udp4Read->Configure (Private->Udp4Read, NULL);
919 }
920
921 Mode->TftpErrorReceived = FALSE;
922 Mode->IcmpErrorReceived = FALSE;
923
924 switch (Operation) {
925 case EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE:
926 //
927 // Send TFTP request to get file size.
928 //
929 Status = PxeBcTftpGetFileSize (
930 Private,
931 Config,
932 Filename,
933 BlockSize,
934 (WindowSize > 1) ? &WindowSize : NULL,
935 BufferSize
936 );
937
938 break;
939
940 case EFI_PXE_BASE_CODE_TFTP_READ_FILE:
941 //
942 // Send TFTP request to read file.
943 //
944 Status = PxeBcTftpReadFile (
945 Private,
946 Config,
947 Filename,
948 BlockSize,
949 (WindowSize > 1) ? &WindowSize : NULL,
950 BufferPtr,
951 BufferSize,
952 DontUseBuffer
953 );
954
955 break;
956
957 case EFI_PXE_BASE_CODE_TFTP_WRITE_FILE:
958 //
959 // Send TFTP request to write file.
960 //
961 Status = PxeBcTftpWriteFile (
962 Private,
963 Config,
964 Filename,
965 Overwrite,
966 BlockSize,
967 BufferPtr,
968 BufferSize
969 );
970
971 break;
972
973 case EFI_PXE_BASE_CODE_TFTP_READ_DIRECTORY:
974 //
975 // Send TFTP request to read directory.
976 //
977 Status = PxeBcTftpReadDirectory (
978 Private,
979 Config,
980 Filename,
981 BlockSize,
982 (WindowSize > 1) ? &WindowSize : NULL,
983 BufferPtr,
984 BufferSize,
985 DontUseBuffer
986 );
987
988 break;
989
990 case EFI_PXE_BASE_CODE_MTFTP_GET_FILE_SIZE:
991 case EFI_PXE_BASE_CODE_MTFTP_READ_FILE:
992 case EFI_PXE_BASE_CODE_MTFTP_READ_DIRECTORY:
993 Status = EFI_UNSUPPORTED;
994
995 break;
996
997 default:
998 Status = EFI_INVALID_PARAMETER;
999
1000 break;
1001 }
1002
1003 if (Status == EFI_ICMP_ERROR) {
1004 Mode->IcmpErrorReceived = TRUE;
1005 }
1006
1007 //
1008 // Reconfigure the UDP instance with the default configuration.
1009 //
1010 if (Mode->UsingIpv6) {
1011 Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData);
1012 } else {
1013 Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);
1014 }
1015
1016 //
1017 // Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP
1018 // receive filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.
1019 //
1020 ZeroMem (&IpFilter, sizeof (EFI_PXE_BASE_CODE_IP_FILTER));
1021 IpFilter.Filters = EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP;
1022 This->SetIpFilter (This, &IpFilter);
1023
1024 return Status;
1025}
1026
1027/**
1028 Writes a UDP packet to the network interface.
1029
1030 This function writes a UDP packet specified by the (optional HeaderPtr and)
1031 BufferPtr parameters to the network interface. The UDP header is automatically
1032 built by this routine. It uses the parameters OpFlags, DestIp, DestPort, GatewayIp,
1033 SrcIp, and SrcPort to build this header. If the packet is successfully built and
1034 transmitted through the network interface, then EFI_SUCCESS will be returned.
1035 If a timeout occurs during the transmission of the packet, then EFI_TIMEOUT will
1036 be returned. If an ICMP error occurs during the transmission of the packet, then
1037 the IcmpErrorReceived field is set to TRUE, the IcmpError field is filled in and
1038 EFI_ICMP_ERROR will be returned. If the Callback Protocol does not return
1039 EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE, then EFI_ABORTED will be returned.
1040
1041 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
1042 @param[in] OpFlags The UDP operation flags.
1043 @param[in] DestIp The destination IP address.
1044 @param[in] DestPort The destination UDP port number.
1045 @param[in] GatewayIp The gateway IP address.
1046 @param[in] SrcIp The source IP address.
1047 @param[in, out] SrcPort The source UDP port number.
1048 @param[in] HeaderSize An optional field which may be set to the length of a header
1049 at HeaderPtr to be prefixed to the data at BufferPtr.
1050 @param[in] HeaderPtr If HeaderSize is not NULL, a pointer to a header to be
1051 prefixed to the data at BufferPtr.
1052 @param[in] BufferSize A pointer to the size of the data at BufferPtr.
1053 @param[in] BufferPtr A pointer to the data to be written.
1054
1055 @retval EFI_SUCCESS The UDP Write operation completed.
1056 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
1057 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1058 @retval EFI_BAD_BUFFER_SIZE The buffer is too long to be transmitted.
1059 @retval EFI_ABORTED The callback function aborted the UDP Write operation.
1060 @retval EFI_TIMEOUT The UDP Write operation timed out.
1061 @retval EFI_ICMP_ERROR An ICMP error packet was received during the UDP write session.
1062
1063**/
1064EFI_STATUS
1065EFIAPI
1066EfiPxeBcUdpWrite (
1067 IN EFI_PXE_BASE_CODE_PROTOCOL *This,
1068 IN UINT16 OpFlags,
1069 IN EFI_IP_ADDRESS *DestIp,
1070 IN EFI_PXE_BASE_CODE_UDP_PORT *DestPort,
1071 IN EFI_IP_ADDRESS *GatewayIp OPTIONAL,
1072 IN EFI_IP_ADDRESS *SrcIp OPTIONAL,
1073 IN OUT EFI_PXE_BASE_CODE_UDP_PORT *SrcPort OPTIONAL,
1074 IN UINTN *HeaderSize OPTIONAL,
1075 IN VOID *HeaderPtr OPTIONAL,
1076 IN UINTN *BufferSize,
1077 IN VOID *BufferPtr
1078 )
1079{
1080 PXEBC_PRIVATE_DATA *Private;
1081 EFI_PXE_BASE_CODE_MODE *Mode;
1082 EFI_UDP4_SESSION_DATA Udp4Session;
1083 EFI_UDP6_SESSION_DATA Udp6Session;
1084 EFI_STATUS Status;
1085 BOOLEAN DoNotFragment;
1086
1087 if ((This == NULL) || (DestIp == NULL) || (DestPort == NULL)) {
1088 return EFI_INVALID_PARAMETER;
1089 }
1090
1091 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
1092 Mode = Private->PxeBc.Mode;
1093
1094 if ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_MAY_FRAGMENT) != 0) {
1095 DoNotFragment = FALSE;
1096 } else {
1097 DoNotFragment = TRUE;
1098 }
1099
1100 if (!Mode->UsingIpv6 && (GatewayIp != NULL) && (Mode->SubnetMask.Addr[0] != 0) &&
1101 !NetIp4IsUnicast (NTOHL (GatewayIp->Addr[0]), EFI_NTOHL (Mode->SubnetMask)))
1102 {
1103 //
1104 // Gateway is provided but it's not a unicast IPv4 address, while it will be ignored for IPv6.
1105 //
1106 return EFI_INVALID_PARAMETER;
1107 }
1108
1109 if ((HeaderSize != NULL) && ((*HeaderSize == 0) || (HeaderPtr == NULL))) {
1110 return EFI_INVALID_PARAMETER;
1111 }
1112
1113 if ((BufferSize == NULL) || ((*BufferSize != 0) && (BufferPtr == NULL))) {
1114 return EFI_INVALID_PARAMETER;
1115 }
1116
1117 if (!Mode->Started) {
1118 return EFI_NOT_STARTED;
1119 }
1120
1121 if (!Private->IsAddressOk && (SrcIp == NULL)) {
1122 return EFI_INVALID_PARAMETER;
1123 }
1124
1125 if ((Private->CurSrcPort == 0) ||
1126 ((SrcPort != NULL) && (*SrcPort != Private->CurSrcPort)))
1127 {
1128 //
1129 // Reconfigure UDPv4/UDPv6 for UdpWrite if the source port changed.
1130 //
1131 if (SrcPort != NULL) {
1132 Private->CurSrcPort = *SrcPort;
1133 }
1134 }
1135
1136 if (Mode->UsingIpv6) {
1137 Status = PxeBcConfigUdp6Write (
1138 Private->Udp6Write,
1139 &Private->StationIp.v6,
1140 &Private->CurSrcPort
1141 );
1142 } else {
1143 //
1144 // Configure the UDPv4 instance with gateway information from DHCP server as default.
1145 //
1146 Status = PxeBcConfigUdp4Write (
1147 Private->Udp4Write,
1148 &Private->StationIp.v4,
1149 &Private->SubnetMask.v4,
1150 &Private->GatewayIp.v4,
1151 &Private->CurSrcPort,
1152 DoNotFragment,
1153 Private->Mode.TTL,
1154 Private->Mode.ToS
1155 );
1156 }
1157
1158 if (EFI_ERROR (Status)) {
1159 Private->CurSrcPort = 0;
1160 return EFI_INVALID_PARAMETER;
1161 } else if (SrcPort != NULL) {
1162 *SrcPort = Private->CurSrcPort;
1163 }
1164
1165 //
1166 // Start a timer as timeout event for this blocking API.
1167 //
1168 gBS->SetTimer (Private->UdpTimeOutEvent, TimerRelative, PXEBC_UDP_TIMEOUT);
1169
1170 if (Mode->UsingIpv6) {
1171 //
1172 // Construct UDPv6 session data.
1173 //
1174 ZeroMem (&Udp6Session, sizeof (EFI_UDP6_SESSION_DATA));
1175 CopyMem (&Udp6Session.DestinationAddress, DestIp, sizeof (EFI_IPv6_ADDRESS));
1176 Udp6Session.DestinationPort = *DestPort;
1177 if (SrcIp != NULL) {
1178 CopyMem (&Udp6Session.SourceAddress, SrcIp, sizeof (EFI_IPv6_ADDRESS));
1179 }
1180
1181 if (SrcPort != NULL) {
1182 Udp6Session.SourcePort = *SrcPort;
1183 }
1184
1185 Status = PxeBcUdp6Write (
1186 Private->Udp6Write,
1187 &Udp6Session,
1188 Private->UdpTimeOutEvent,
1189 HeaderSize,
1190 HeaderPtr,
1191 BufferSize,
1192 BufferPtr
1193 );
1194 } else {
1195 //
1196 // Construct UDPv4 session data.
1197 //
1198 ZeroMem (&Udp4Session, sizeof (EFI_UDP4_SESSION_DATA));
1199 CopyMem (&Udp4Session.DestinationAddress, DestIp, sizeof (EFI_IPv4_ADDRESS));
1200 Udp4Session.DestinationPort = *DestPort;
1201 if (SrcIp != NULL) {
1202 CopyMem (&Udp4Session.SourceAddress, SrcIp, sizeof (EFI_IPv4_ADDRESS));
1203 }
1204
1205 if (SrcPort != NULL) {
1206 Udp4Session.SourcePort = *SrcPort;
1207 }
1208
1209 //
1210 // Override the gateway information if user specified.
1211 //
1212 Status = PxeBcUdp4Write (
1213 Private->Udp4Write,
1214 &Udp4Session,
1215 Private->UdpTimeOutEvent,
1216 (EFI_IPv4_ADDRESS *)GatewayIp,
1217 HeaderSize,
1218 HeaderPtr,
1219 BufferSize,
1220 BufferPtr
1221 );
1222 }
1223
1224 gBS->SetTimer (Private->UdpTimeOutEvent, TimerCancel, 0);
1225
1226 //
1227 // Reset the UdpWrite instance.
1228 //
1229 if (Mode->UsingIpv6) {
1230 Private->Udp6Write->Configure (Private->Udp6Write, NULL);
1231 } else {
1232 Private->Udp4Write->Configure (Private->Udp4Write, NULL);
1233 }
1234
1235 return Status;
1236}
1237
1238/**
1239 Reads a UDP packet from the network interface.
1240+
1241 This function reads a UDP packet from a network interface. The data contents
1242 are returned in (the optional HeaderPtr and) BufferPtr, and the size of the
1243 buffer received is returned in BufferSize . If the input BufferSize is smaller
1244 than the UDP packet received (less optional HeaderSize), it will be set to the
1245 required size, and EFI_BUFFER_TOO_SMALL will be returned. In this case, the
1246 contents of BufferPtr are undefined, and the packet is lost. If a UDP packet is
1247 successfully received, then EFI_SUCCESS will be returned, and the information
1248 from the UDP header will be returned in DestIp, DestPort, SrcIp, and SrcPort if
1249 they are not NULL. Depending on the values of OpFlags and the DestIp, DestPort,
1250 SrcIp, and SrcPort input values, different types of UDP packet receive filtering
1251 will be performed. The following tables summarize these receive filter operations.
1252
1253 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
1254 @param[in] OpFlags The UDP operation flags.
1255 @param[in, out] DestIp The destination IP address.
1256 @param[in, out] DestPort The destination UDP port number.
1257 @param[in, out] SrcIp The source IP address.
1258 @param[in, out] SrcPort The source UDP port number.
1259 @param[in] HeaderSize An optional field which may be set to the length of a
1260 header at HeaderPtr to be prefixed to the data at BufferPtr.
1261 @param[in] HeaderPtr If HeaderSize is not NULL, a pointer to a header to be
1262 prefixed to the data at BufferPtr.
1263 @param[in, out] BufferSize A pointer to the size of the data at BufferPtr.
1264 @param[in] BufferPtr A pointer to the data to be read.
1265
1266 @retval EFI_SUCCESS The UDP Read operation was completed.
1267 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
1268 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1269 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
1270 @retval EFI_BUFFER_TOO_SMALL The packet is larger than Buffer can hold.
1271 @retval EFI_ABORTED The callback function aborted the UDP Read operation.
1272 @retval EFI_TIMEOUT The UDP Read operation timed out.
1273
1274**/
1275EFI_STATUS
1276EFIAPI
1277EfiPxeBcUdpRead (
1278 IN EFI_PXE_BASE_CODE_PROTOCOL *This,
1279 IN UINT16 OpFlags,
1280 IN OUT EFI_IP_ADDRESS *DestIp OPTIONAL,
1281 IN OUT EFI_PXE_BASE_CODE_UDP_PORT *DestPort OPTIONAL,
1282 IN OUT EFI_IP_ADDRESS *SrcIp OPTIONAL,
1283 IN OUT EFI_PXE_BASE_CODE_UDP_PORT *SrcPort OPTIONAL,
1284 IN UINTN *HeaderSize OPTIONAL,
1285 IN VOID *HeaderPtr OPTIONAL,
1286 IN OUT UINTN *BufferSize,
1287 IN VOID *BufferPtr
1288 )
1289{
1290 PXEBC_PRIVATE_DATA *Private;
1291 EFI_PXE_BASE_CODE_MODE *Mode;
1292 EFI_UDP4_COMPLETION_TOKEN Udp4Token;
1293 EFI_UDP6_COMPLETION_TOKEN Udp6Token;
1294 EFI_UDP4_RECEIVE_DATA *Udp4Rx;
1295 EFI_UDP6_RECEIVE_DATA *Udp6Rx;
1296 EFI_STATUS Status;
1297 BOOLEAN IsDone;
1298 BOOLEAN IsMatched;
1299 UINTN CopiedLen;
1300 UINTN HeaderLen;
1301 UINTN HeaderCopiedLen;
1302 UINTN BufferCopiedLen;
1303 UINT32 FragmentLength;
1304 UINTN FragmentIndex;
1305 UINT8 *FragmentBuffer;
1306
1307 if (This == NULL) {
1308 return EFI_INVALID_PARAMETER;
1309 }
1310
1311 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
1312 Mode = Private->PxeBc.Mode;
1313 IsDone = FALSE;
1314 IsMatched = FALSE;
1315 Udp4Rx = NULL;
1316 Udp6Rx = NULL;
1317
1318 if ((((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT) == 0) && (DestPort == NULL)) ||
1319 (((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP) == 0) && (SrcIp == NULL)) ||
1320 (((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT) == 0) && (SrcPort == NULL)))
1321 {
1322 return EFI_INVALID_PARAMETER;
1323 }
1324
1325 if (((HeaderSize != NULL) && (*HeaderSize == 0)) || ((HeaderSize != NULL) && (HeaderPtr == NULL))) {
1326 return EFI_INVALID_PARAMETER;
1327 }
1328
1329 if ((BufferSize == NULL) || (BufferPtr == NULL)) {
1330 return EFI_INVALID_PARAMETER;
1331 }
1332
1333 if (!Mode->Started) {
1334 return EFI_NOT_STARTED;
1335 }
1336
1337 ZeroMem (&Udp6Token, sizeof (EFI_UDP6_COMPLETION_TOKEN));
1338 ZeroMem (&Udp4Token, sizeof (EFI_UDP4_COMPLETION_TOKEN));
1339
1340 if (Mode->UsingIpv6) {
1341 Status = gBS->CreateEvent (
1342 EVT_NOTIFY_SIGNAL,
1343 TPL_NOTIFY,
1344 PxeBcCommonNotify,
1345 &IsDone,
1346 &Udp6Token.Event
1347 );
1348 if (EFI_ERROR (Status)) {
1349 return EFI_OUT_OF_RESOURCES;
1350 }
1351 } else {
1352 Status = gBS->CreateEvent (
1353 EVT_NOTIFY_SIGNAL,
1354 TPL_NOTIFY,
1355 PxeBcCommonNotify,
1356 &IsDone,
1357 &Udp4Token.Event
1358 );
1359 if (EFI_ERROR (Status)) {
1360 return EFI_OUT_OF_RESOURCES;
1361 }
1362 }
1363
1364 //
1365 // Start a timer as timeout event for this blocking API.
1366 //
1367 gBS->SetTimer (Private->UdpTimeOutEvent, TimerRelative, PXEBC_UDP_TIMEOUT);
1368 Mode->IcmpErrorReceived = FALSE;
1369
1370 //
1371 // Read packet by Udp4Read/Udp6Read until matched or timeout.
1372 //
1373 while (!IsMatched && !EFI_ERROR (Status)) {
1374 if (Mode->UsingIpv6) {
1375 Status = PxeBcUdp6Read (
1376 Private->Udp6Read,
1377 &Udp6Token,
1378 Mode,
1379 Private->UdpTimeOutEvent,
1380 OpFlags,
1381 &IsDone,
1382 &IsMatched,
1383 DestIp,
1384 DestPort,
1385 SrcIp,
1386 SrcPort
1387 );
1388 } else {
1389 Status = PxeBcUdp4Read (
1390 Private->Udp4Read,
1391 &Udp4Token,
1392 Mode,
1393 Private->UdpTimeOutEvent,
1394 OpFlags,
1395 &IsDone,
1396 &IsMatched,
1397 DestIp,
1398 DestPort,
1399 SrcIp,
1400 SrcPort
1401 );
1402 }
1403 }
1404
1405 if ((Status == EFI_ICMP_ERROR) ||
1406 (Status == EFI_NETWORK_UNREACHABLE) ||
1407 (Status == EFI_HOST_UNREACHABLE) ||
1408 (Status == EFI_PROTOCOL_UNREACHABLE) ||
1409 (Status == EFI_PORT_UNREACHABLE))
1410 {
1411 //
1412 // Get different return status for icmp error from Udp, refers to UEFI spec.
1413 //
1414 Mode->IcmpErrorReceived = TRUE;
1415 }
1416
1417 gBS->SetTimer (Private->UdpTimeOutEvent, TimerCancel, 0);
1418
1419 if (IsMatched) {
1420 //
1421 // Copy the received packet to user if matched by filter.
1422 //
1423 if (Mode->UsingIpv6) {
1424 Udp6Rx = Udp6Token.Packet.RxData;
1425 ASSERT (Udp6Rx != NULL);
1426
1427 HeaderLen = 0;
1428 if (HeaderSize != NULL) {
1429 HeaderLen = MIN (*HeaderSize, Udp6Rx->DataLength);
1430 }
1431
1432 if (Udp6Rx->DataLength - HeaderLen > *BufferSize) {
1433 Status = EFI_BUFFER_TOO_SMALL;
1434 } else {
1435 if (HeaderSize != NULL) {
1436 *HeaderSize = HeaderLen;
1437 }
1438
1439 *BufferSize = Udp6Rx->DataLength - HeaderLen;
1440
1441 HeaderCopiedLen = 0;
1442 BufferCopiedLen = 0;
1443 for (FragmentIndex = 0; FragmentIndex < Udp6Rx->FragmentCount; FragmentIndex++) {
1444 FragmentLength = Udp6Rx->FragmentTable[FragmentIndex].FragmentLength;
1445 FragmentBuffer = Udp6Rx->FragmentTable[FragmentIndex].FragmentBuffer;
1446 if (HeaderCopiedLen + FragmentLength < HeaderLen) {
1447 //
1448 // Copy the header part of received data.
1449 //
1450 CopyMem ((UINT8 *)HeaderPtr + HeaderCopiedLen, FragmentBuffer, FragmentLength);
1451 HeaderCopiedLen += FragmentLength;
1452 } else if (HeaderCopiedLen < HeaderLen) {
1453 //
1454 // Copy the header part of received data.
1455 //
1456 CopiedLen = HeaderLen - HeaderCopiedLen;
1457 CopyMem ((UINT8 *)HeaderPtr + HeaderCopiedLen, FragmentBuffer, CopiedLen);
1458 HeaderCopiedLen += CopiedLen;
1459
1460 //
1461 // Copy the other part of received data.
1462 //
1463 CopyMem ((UINT8 *)BufferPtr + BufferCopiedLen, FragmentBuffer + CopiedLen, FragmentLength - CopiedLen);
1464 BufferCopiedLen += (FragmentLength - CopiedLen);
1465 } else {
1466 //
1467 // Copy the other part of received data.
1468 //
1469 CopyMem ((UINT8 *)BufferPtr + BufferCopiedLen, FragmentBuffer, FragmentLength);
1470 BufferCopiedLen += FragmentLength;
1471 }
1472 }
1473 }
1474
1475 //
1476 // Recycle the receiving buffer after copy to user.
1477 //
1478 gBS->SignalEvent (Udp6Rx->RecycleSignal);
1479 } else {
1480 Udp4Rx = Udp4Token.Packet.RxData;
1481 ASSERT (Udp4Rx != NULL);
1482
1483 HeaderLen = 0;
1484 if (HeaderSize != NULL) {
1485 HeaderLen = MIN (*HeaderSize, Udp4Rx->DataLength);
1486 }
1487
1488 if (Udp4Rx->DataLength - HeaderLen > *BufferSize) {
1489 Status = EFI_BUFFER_TOO_SMALL;
1490 } else {
1491 if (HeaderSize != NULL) {
1492 *HeaderSize = HeaderLen;
1493 }
1494
1495 *BufferSize = Udp4Rx->DataLength - HeaderLen;
1496
1497 HeaderCopiedLen = 0;
1498 BufferCopiedLen = 0;
1499 for (FragmentIndex = 0; FragmentIndex < Udp4Rx->FragmentCount; FragmentIndex++) {
1500 FragmentLength = Udp4Rx->FragmentTable[FragmentIndex].FragmentLength;
1501 FragmentBuffer = Udp4Rx->FragmentTable[FragmentIndex].FragmentBuffer;
1502 if (HeaderCopiedLen + FragmentLength < HeaderLen) {
1503 //
1504 // Copy the header part of received data.
1505 //
1506 CopyMem ((UINT8 *)HeaderPtr + HeaderCopiedLen, FragmentBuffer, FragmentLength);
1507 HeaderCopiedLen += FragmentLength;
1508 } else if (HeaderCopiedLen < HeaderLen) {
1509 //
1510 // Copy the header part of received data.
1511 //
1512 CopiedLen = HeaderLen - HeaderCopiedLen;
1513 CopyMem ((UINT8 *)HeaderPtr + HeaderCopiedLen, FragmentBuffer, CopiedLen);
1514 HeaderCopiedLen += CopiedLen;
1515
1516 //
1517 // Copy the other part of received data.
1518 //
1519 CopyMem ((UINT8 *)BufferPtr + BufferCopiedLen, FragmentBuffer + CopiedLen, FragmentLength - CopiedLen);
1520 BufferCopiedLen += (FragmentLength - CopiedLen);
1521 } else {
1522 //
1523 // Copy the other part of received data.
1524 //
1525 CopyMem ((UINT8 *)BufferPtr + BufferCopiedLen, FragmentBuffer, FragmentLength);
1526 BufferCopiedLen += FragmentLength;
1527 }
1528 }
1529 }
1530
1531 //
1532 // Recycle the receiving buffer after copy to user.
1533 //
1534 gBS->SignalEvent (Udp4Rx->RecycleSignal);
1535 }
1536 }
1537
1538 if (Mode->UsingIpv6) {
1539 Private->Udp6Read->Cancel (Private->Udp6Read, &Udp6Token);
1540 gBS->CloseEvent (Udp6Token.Event);
1541 } else {
1542 Private->Udp4Read->Cancel (Private->Udp4Read, &Udp4Token);
1543 gBS->CloseEvent (Udp4Token.Event);
1544 }
1545
1546 return Status;
1547}
1548
1549/**
1550 Updates the IP receive filters of a network device and enables software filtering.
1551
1552 The NewFilter field is used to modify the network device's current IP receive
1553 filter settings and to enable a software filter. This function updates the IpFilter
1554 field of the EFI_PXE_BASE_CODE_MODE structure with the contents of NewIpFilter.
1555 The software filter is used when the USE_FILTER in OpFlags is set to UdpRead().
1556 The current hardware filter remains in effect no matter what the settings of OpFlags.
1557 This is so that the meaning of ANY_DEST_IP set in OpFlags to UdpRead() is from those
1558 packets whose reception is enabled in hardware-physical NIC address (unicast),
1559 broadcast address, logical address or addresses (multicast), or all (promiscuous).
1560 UdpRead() does not modify the IP filter settings.
1561 Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP receive
1562 filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.
1563 If an application or driver wishes to preserve the IP receive filter settings,
1564 it will have to preserve the IP receive filter settings before these calls, and
1565 use SetIpFilter() to restore them after the calls. If incompatible filtering is
1566 requested (for example, PROMISCUOUS with anything else), or if the device does not
1567 support a requested filter setting and it cannot be accommodated in software
1568 (for example, PROMISCUOUS not supported), EFI_INVALID_PARAMETER will be returned.
1569 The IPlist field is used to enable IPs other than the StationIP. They may be
1570 multicast or unicast. If IPcnt is set as well as EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP,
1571 then both the StationIP and the IPs from the IPlist will be used.
1572
1573 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
1574 @param[in] NewFilter Pointer to the new set of IP receive filters.
1575
1576 @retval EFI_SUCCESS The IP receive filter settings were updated.
1577 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
1578 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1579
1580**/
1581EFI_STATUS
1582EFIAPI
1583EfiPxeBcSetIpFilter (
1584 IN EFI_PXE_BASE_CODE_PROTOCOL *This,
1585 IN EFI_PXE_BASE_CODE_IP_FILTER *NewFilter
1586 )
1587{
1588 EFI_STATUS Status;
1589 PXEBC_PRIVATE_DATA *Private;
1590 EFI_PXE_BASE_CODE_MODE *Mode;
1591 EFI_UDP4_CONFIG_DATA *Udp4Cfg;
1592 EFI_UDP6_CONFIG_DATA *Udp6Cfg;
1593 UINTN Index;
1594 BOOLEAN NeedPromiscuous;
1595 BOOLEAN AcceptPromiscuous;
1596 BOOLEAN AcceptBroadcast;
1597 BOOLEAN MultiCastUpdate;
1598
1599 if ((This == NULL) || (NewFilter == NULL)) {
1600 return EFI_INVALID_PARAMETER;
1601 }
1602
1603 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
1604 Mode = Private->PxeBc.Mode;
1605 Status = EFI_SUCCESS;
1606 NeedPromiscuous = FALSE;
1607
1608 if (!Mode->Started) {
1609 return EFI_NOT_STARTED;
1610 }
1611
1612 for (Index = 0; Index < NewFilter->IpCnt; Index++) {
1613 ASSERT (Index < EFI_PXE_BASE_CODE_MAX_IPCNT);
1614 if (!Mode->UsingIpv6 &&
1615 IP4_IS_LOCAL_BROADCAST (EFI_IP4 (NewFilter->IpList[Index].v4)))
1616 {
1617 //
1618 // IPv4 broadcast address should not be in IP filter.
1619 //
1620 return EFI_INVALID_PARAMETER;
1621 }
1622
1623 if (Mode->UsingIpv6) {
1624 if (((NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP) != 0) &&
1625 NetIp6IsValidUnicast (&NewFilter->IpList[Index].v6))
1626 {
1627 NeedPromiscuous = TRUE;
1628 }
1629 } else if ((EFI_NTOHL (Mode->StationIp) != 0) &&
1630 (EFI_NTOHL (Mode->SubnetMask) != 0) &&
1631 IP4_NET_EQUAL (EFI_NTOHL (Mode->StationIp), EFI_NTOHL (NewFilter->IpList[Index].v4), EFI_NTOHL (Mode->SubnetMask.v4)) &&
1632 NetIp4IsUnicast (EFI_IP4 (NewFilter->IpList[Index].v4), EFI_NTOHL (Mode->SubnetMask)) &&
1633 ((NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP) != 0))
1634 {
1635 NeedPromiscuous = TRUE;
1636 }
1637 }
1638
1639 AcceptPromiscuous = FALSE;
1640 AcceptBroadcast = FALSE;
1641 MultiCastUpdate = FALSE;
1642
1643 if (NeedPromiscuous ||
1644 ((NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS) != 0) ||
1645 ((NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS_MULTICAST) != 0))
1646 {
1647 //
1648 // Configure UDPv4/UDPv6 as promiscuous mode to receive all packets.
1649 //
1650 AcceptPromiscuous = TRUE;
1651 } else if ((NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_BROADCAST) != 0) {
1652 //
1653 // Configure UDPv4 to receive all broadcast packets.
1654 //
1655 AcceptBroadcast = TRUE;
1656 }
1657
1658 //
1659 // In multicast condition when Promiscuous FALSE and IpCnt no-zero.
1660 // Here check if there is any update of the multicast ip address. If yes,
1661 // we need leave the old multicast group (by Config UDP instance to NULL),
1662 // and join the new multicast group.
1663 //
1664 if (!AcceptPromiscuous) {
1665 if ((NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP) != 0) {
1666 if (Mode->IpFilter.IpCnt != NewFilter->IpCnt) {
1667 MultiCastUpdate = TRUE;
1668 } else if (CompareMem (Mode->IpFilter.IpList, NewFilter->IpList, NewFilter->IpCnt * sizeof (EFI_IP_ADDRESS)) != 0 ) {
1669 MultiCastUpdate = TRUE;
1670 }
1671 }
1672 }
1673
1674 if (!Mode->UsingIpv6) {
1675 //
1676 // Check whether we need reconfigure the UDP4 instance.
1677 //
1678 Udp4Cfg = &Private->Udp4CfgData;
1679 if ((AcceptPromiscuous != Udp4Cfg->AcceptPromiscuous) ||
1680 (AcceptBroadcast != Udp4Cfg->AcceptBroadcast) || MultiCastUpdate)
1681 {
1682 //
1683 // Clear the UDP4 instance configuration, all joined groups will be left
1684 // during the operation.
1685 //
1686 Private->Udp4Read->Configure (Private->Udp4Read, NULL);
1687
1688 //
1689 // Configure the UDP instance with the new configuration.
1690 //
1691 Udp4Cfg->AcceptPromiscuous = AcceptPromiscuous;
1692 Udp4Cfg->AcceptBroadcast = AcceptBroadcast;
1693 Status = Private->Udp4Read->Configure (Private->Udp4Read, Udp4Cfg);
1694 if (EFI_ERROR (Status)) {
1695 return Status;
1696 }
1697
1698 //
1699 // In not Promiscuous mode, need to join the new multicast group.
1700 //
1701 if (!AcceptPromiscuous) {
1702 for (Index = 0; Index < NewFilter->IpCnt; ++Index) {
1703 if (IP4_IS_MULTICAST (EFI_NTOHL (NewFilter->IpList[Index].v4))) {
1704 //
1705 // Join the multicast group.
1706 //
1707 Status = Private->Udp4Read->Groups (Private->Udp4Read, TRUE, &NewFilter->IpList[Index].v4);
1708 if (EFI_ERROR (Status)) {
1709 return Status;
1710 }
1711 }
1712 }
1713 }
1714 }
1715 } else {
1716 //
1717 // Check whether we need reconfigure the UDP6 instance.
1718 //
1719 Udp6Cfg = &Private->Udp6CfgData;
1720 if ((AcceptPromiscuous != Udp6Cfg->AcceptPromiscuous) || MultiCastUpdate) {
1721 //
1722 // Clear the UDP6 instance configuration, all joined groups will be left
1723 // during the operation.
1724 //
1725 Private->Udp6Read->Configure (Private->Udp6Read, NULL);
1726
1727 //
1728 // Configure the UDP instance with the new configuration.
1729 //
1730 Udp6Cfg->AcceptPromiscuous = AcceptPromiscuous;
1731 Status = Private->Udp6Read->Configure (Private->Udp6Read, Udp6Cfg);
1732 if (EFI_ERROR (Status)) {
1733 return Status;
1734 }
1735
1736 //
1737 // In not Promiscuous mode, need to join the new multicast group.
1738 //
1739 if (!AcceptPromiscuous) {
1740 for (Index = 0; Index < NewFilter->IpCnt; ++Index) {
1741 if (IP6_IS_MULTICAST (&NewFilter->IpList[Index].v6)) {
1742 //
1743 // Join the multicast group.
1744 //
1745 Status = Private->Udp6Read->Groups (Private->Udp6Read, TRUE, &NewFilter->IpList[Index].v6);
1746 if (EFI_ERROR (Status)) {
1747 return Status;
1748 }
1749 }
1750 }
1751 }
1752 }
1753 }
1754
1755 //
1756 // Save the new IP filter into mode data.
1757 //
1758 CopyMem (&Mode->IpFilter, NewFilter, sizeof (Mode->IpFilter));
1759
1760 return Status;
1761}
1762
1763/**
1764 Uses the ARP protocol to resolve a MAC address. It is not supported for IPv6.
1765
1766 This function uses the ARP protocol to resolve a MAC address. The IP address specified
1767 by IpAddr is used to resolve a MAC address. If the ARP protocol succeeds in resolving
1768 the specified address, then the ArpCacheEntries and ArpCache fields of the mode data
1769 are updated, and EFI_SUCCESS is returned. If MacAddr is not NULL, the resolved
1770 MAC address is placed there as well. If the PXE Base Code protocol is in the
1771 stopped state, then EFI_NOT_STARTED is returned. If the ARP protocol encounters
1772 a timeout condition while attempting to resolve an address, then EFI_TIMEOUT is
1773 returned. If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,
1774 then EFI_ABORTED is returned.
1775
1776 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
1777 @param[in] IpAddr Pointer to the IP address that is used to resolve a MAC address.
1778 @param[in] MacAddr If not NULL, a pointer to the MAC address that was resolved with the
1779 ARP protocol.
1780
1781 @retval EFI_SUCCESS The IP or MAC address was resolved.
1782 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
1783 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1784 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
1785 @retval EFI_ICMP_ERROR An error occur with the ICMP packet message.
1786
1787**/
1788EFI_STATUS
1789EFIAPI
1790EfiPxeBcArp (
1791 IN EFI_PXE_BASE_CODE_PROTOCOL *This,
1792 IN EFI_IP_ADDRESS *IpAddr,
1793 IN EFI_MAC_ADDRESS *MacAddr OPTIONAL
1794 )
1795{
1796 PXEBC_PRIVATE_DATA *Private;
1797 EFI_PXE_BASE_CODE_MODE *Mode;
1798 EFI_EVENT ResolvedEvent;
1799 EFI_STATUS Status;
1800 EFI_MAC_ADDRESS TempMac;
1801 EFI_MAC_ADDRESS ZeroMac;
1802 BOOLEAN IsResolved;
1803
1804 if ((This == NULL) || (IpAddr == NULL)) {
1805 return EFI_INVALID_PARAMETER;
1806 }
1807
1808 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
1809 Mode = Private->PxeBc.Mode;
1810 ResolvedEvent = NULL;
1811 Status = EFI_SUCCESS;
1812 IsResolved = FALSE;
1813
1814 if (!Mode->Started) {
1815 return EFI_NOT_STARTED;
1816 }
1817
1818 if (Mode->UsingIpv6) {
1819 return EFI_UNSUPPORTED;
1820 }
1821
1822 //
1823 // Station address should be ready before do arp.
1824 //
1825 if (!Private->IsAddressOk) {
1826 return EFI_INVALID_PARAMETER;
1827 }
1828
1829 Mode->IcmpErrorReceived = FALSE;
1830 ZeroMem (&TempMac, sizeof (EFI_MAC_ADDRESS));
1831 ZeroMem (&ZeroMac, sizeof (EFI_MAC_ADDRESS));
1832
1833 if (!Mode->AutoArp) {
1834 //
1835 // If AutoArp is FALSE, only search in the current Arp cache.
1836 //
1837 PxeBcArpCacheUpdate (NULL, Private);
1838 if (!PxeBcCheckArpCache (Mode, &IpAddr->v4, &TempMac)) {
1839 Status = EFI_DEVICE_ERROR;
1840 goto ON_EXIT;
1841 }
1842 } else {
1843 Status = gBS->CreateEvent (
1844 EVT_NOTIFY_SIGNAL,
1845 TPL_NOTIFY,
1846 PxeBcCommonNotify,
1847 &IsResolved,
1848 &ResolvedEvent
1849 );
1850 if (EFI_ERROR (Status)) {
1851 goto ON_EXIT;
1852 }
1853
1854 //
1855 // If AutoArp is TRUE, try to send Arp request on initiative.
1856 //
1857 Status = Private->Arp->Request (Private->Arp, &IpAddr->v4, ResolvedEvent, &TempMac);
1858 if (EFI_ERROR (Status) && (Status != EFI_NOT_READY)) {
1859 goto ON_EXIT;
1860 }
1861
1862 while (!IsResolved) {
1863 if (CompareMem (&TempMac, &ZeroMac, sizeof (EFI_MAC_ADDRESS)) != 0) {
1864 break;
1865 }
1866 }
1867
1868 if (CompareMem (&TempMac, &ZeroMac, sizeof (EFI_MAC_ADDRESS)) != 0) {
1869 Status = EFI_SUCCESS;
1870 } else {
1871 Status = EFI_TIMEOUT;
1872 }
1873 }
1874
1875 //
1876 // Copy the Mac address to user if needed.
1877 //
1878 if ((MacAddr != NULL) && !EFI_ERROR (Status)) {
1879 CopyMem (MacAddr, &TempMac, sizeof (EFI_MAC_ADDRESS));
1880 }
1881
1882ON_EXIT:
1883 if (ResolvedEvent != NULL) {
1884 gBS->CloseEvent (ResolvedEvent);
1885 }
1886
1887 return Status;
1888}
1889
1890/**
1891 Updates the parameters that affect the operation of the PXE Base Code Protocol.
1892
1893 This function sets parameters that affect the operation of the PXE Base Code Protocol.
1894 The parameter specified by NewAutoArp is used to control the generation of ARP
1895 protocol packets. If NewAutoArp is TRUE, then ARP Protocol packets will be generated
1896 as required by the PXE Base Code Protocol. If NewAutoArp is FALSE, then no ARP
1897 Protocol packets will be generated. In this case, the only mappings that are
1898 available are those stored in the ArpCache of the EFI_PXE_BASE_CODE_MODE structure.
1899 If there are not enough mappings in the ArpCache to perform a PXE Base Code Protocol
1900 service, then the service will fail. This function updates the AutoArp field of
1901 the EFI_PXE_BASE_CODE_MODE structure to NewAutoArp.
1902 The SetParameters() call must be invoked after a Callback Protocol is installed
1903 to enable the use of callbacks.
1904
1905 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
1906 @param[in] NewAutoArp If not NULL, a pointer to a value that specifies whether to replace the
1907 current value of AutoARP.
1908 @param[in] NewSendGUID If not NULL, a pointer to a value that specifies whether to replace the
1909 current value of SendGUID.
1910 @param[in] NewTTL If not NULL, a pointer to be used in place of the current value of TTL,
1911 the "time to live" field of the IP header.
1912 @param[in] NewToS If not NULL, a pointer to be used in place of the current value of ToS,
1913 the "type of service" field of the IP header.
1914 @param[in] NewMakeCallback If not NULL, a pointer to a value that specifies whether to replace the
1915 current value of the MakeCallback field of the Mode structure.
1916
1917 @retval EFI_SUCCESS The new parameters values were updated.
1918 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
1919 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1920
1921**/
1922EFI_STATUS
1923EFIAPI
1924EfiPxeBcSetParameters (
1925 IN EFI_PXE_BASE_CODE_PROTOCOL *This,
1926 IN BOOLEAN *NewAutoArp OPTIONAL,
1927 IN BOOLEAN *NewSendGUID OPTIONAL,
1928 IN UINT8 *NewTTL OPTIONAL,
1929 IN UINT8 *NewToS OPTIONAL,
1930 IN BOOLEAN *NewMakeCallback OPTIONAL
1931 )
1932{
1933 PXEBC_PRIVATE_DATA *Private;
1934 EFI_PXE_BASE_CODE_MODE *Mode;
1935 EFI_GUID SystemGuid;
1936 EFI_STATUS Status;
1937
1938 if (This == NULL) {
1939 return EFI_INVALID_PARAMETER;
1940 }
1941
1942 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
1943 Mode = Private->PxeBc.Mode;
1944
1945 if (!Mode->Started) {
1946 return EFI_NOT_STARTED;
1947 }
1948
1949 if (NewMakeCallback != NULL) {
1950 if (*NewMakeCallback) {
1951 //
1952 // Update the previous PxeBcCallback protocol.
1953 //
1954 Status = gBS->HandleProtocol (
1955 Mode->UsingIpv6 ? Private->Ip6Nic->Controller : Private->Ip4Nic->Controller,
1956 &gEfiPxeBaseCodeCallbackProtocolGuid,
1957 (VOID **)&Private->PxeBcCallback
1958 );
1959
1960 if (EFI_ERROR (Status) || (Private->PxeBcCallback->Callback == NULL)) {
1961 return EFI_INVALID_PARAMETER;
1962 }
1963 } else {
1964 Private->PxeBcCallback = NULL;
1965 }
1966
1967 Mode->MakeCallbacks = *NewMakeCallback;
1968 }
1969
1970 if (NewSendGUID != NULL) {
1971 if (*NewSendGUID && EFI_ERROR (NetLibGetSystemGuid (&SystemGuid))) {
1972 DEBUG ((DEBUG_WARN, "PXE: Failed to read system GUID from the smbios table!\n"));
1973 return EFI_INVALID_PARAMETER;
1974 }
1975
1976 Mode->SendGUID = *NewSendGUID;
1977 }
1978
1979 if (NewAutoArp != NULL) {
1980 Mode->AutoArp = *NewAutoArp;
1981 }
1982
1983 if (NewTTL != NULL) {
1984 Mode->TTL = *NewTTL;
1985 }
1986
1987 if (NewToS != NULL) {
1988 Mode->ToS = *NewToS;
1989 }
1990
1991 return EFI_SUCCESS;
1992}
1993
1994/**
1995 Updates the station IP address and/or subnet mask values of a network device.
1996
1997 This function updates the station IP address and/or subnet mask values of a network
1998 device. The NewStationIp field is used to modify the network device's current IP address.
1999 If NewStationIP is NULL, then the current IP address will not be modified. Otherwise,
2000 this function updates the StationIp field of the EFI_PXE_BASE_CODE_MODE structure
2001 with NewStationIp. The NewSubnetMask field is used to modify the network device's current subnet
2002 mask. If NewSubnetMask is NULL, then the current subnet mask will not be modified.
2003 Otherwise, this function updates the SubnetMask field of the EFI_PXE_BASE_CODE_MODE
2004 structure with NewSubnetMask.
2005
2006 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
2007 @param[in] NewStationIp Pointer to the new IP address to be used by the network device.
2008 @param[in] NewSubnetMask Pointer to the new subnet mask to be used by the network device.
2009
2010 @retval EFI_SUCCESS The new station IP address and/or subnet mask were updated.
2011 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
2012 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
2013
2014**/
2015EFI_STATUS
2016EFIAPI
2017EfiPxeBcSetStationIP (
2018 IN EFI_PXE_BASE_CODE_PROTOCOL *This,
2019 IN EFI_IP_ADDRESS *NewStationIp OPTIONAL,
2020 IN EFI_IP_ADDRESS *NewSubnetMask OPTIONAL
2021 )
2022{
2023 EFI_STATUS Status;
2024 PXEBC_PRIVATE_DATA *Private;
2025 EFI_PXE_BASE_CODE_MODE *Mode;
2026
2027 if (This == NULL) {
2028 return EFI_INVALID_PARAMETER;
2029 }
2030
2031 if ((NewStationIp != NULL) && !NetIp6IsValidUnicast (&NewStationIp->v6)) {
2032 return EFI_INVALID_PARAMETER;
2033 }
2034
2035 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
2036 Mode = Private->PxeBc.Mode;
2037 Status = EFI_SUCCESS;
2038
2039 if (!Mode->UsingIpv6 &&
2040 (NewSubnetMask != NULL) &&
2041 !IP4_IS_VALID_NETMASK (NTOHL (NewSubnetMask->Addr[0])))
2042 {
2043 return EFI_INVALID_PARAMETER;
2044 }
2045
2046 if (!Mode->UsingIpv6 && (NewStationIp != NULL)) {
2047 if (IP4_IS_UNSPECIFIED (NTOHL (NewStationIp->Addr[0])) ||
2048 IP4_IS_LOCAL_BROADCAST (NTOHL (NewStationIp->Addr[0])) ||
2049 ((NewSubnetMask != NULL) && (NewSubnetMask->Addr[0] != 0) && !NetIp4IsUnicast (NTOHL (NewStationIp->Addr[0]), NTOHL (NewSubnetMask->Addr[0]))))
2050 {
2051 return EFI_INVALID_PARAMETER;
2052 }
2053 }
2054
2055 if (!Mode->Started) {
2056 return EFI_NOT_STARTED;
2057 }
2058
2059 if (Mode->UsingIpv6 && (NewStationIp != NULL)) {
2060 //
2061 // Set the IPv6 address by Ip6Config protocol.
2062 //
2063 Status = PxeBcRegisterIp6Address (Private, &NewStationIp->v6);
2064 if (EFI_ERROR (Status)) {
2065 goto ON_EXIT;
2066 }
2067 }
2068
2069 if (NewStationIp != NULL) {
2070 CopyMem (&Mode->StationIp, NewStationIp, sizeof (EFI_IP_ADDRESS));
2071 CopyMem (&Private->StationIp, NewStationIp, sizeof (EFI_IP_ADDRESS));
2072 }
2073
2074 if (!Mode->UsingIpv6 && (NewSubnetMask != NULL)) {
2075 CopyMem (&Mode->SubnetMask, NewSubnetMask, sizeof (EFI_IP_ADDRESS));
2076 CopyMem (&Private->SubnetMask, NewSubnetMask, sizeof (EFI_IP_ADDRESS));
2077 }
2078
2079 Status = PxeBcFlushStationIp (Private, NewStationIp, NewSubnetMask);
2080 if (!EFI_ERROR (Status)) {
2081 Private->IsAddressOk = TRUE;
2082 }
2083
2084ON_EXIT:
2085 return Status;
2086}
2087
2088/**
2089 Updates the contents of the cached DHCP and Discover packets.
2090
2091 The pointers to the new packets are used to update the contents of the cached
2092 packets in the EFI_PXE_BASE_CODE_MODE structure.
2093
2094 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
2095 @param[in] NewDhcpDiscoverValid Pointer to a value that will replace the current
2096 DhcpDiscoverValid field.
2097 @param[in] NewDhcpAckReceived Pointer to a value that will replace the current
2098 DhcpAckReceived field.
2099 @param[in] NewProxyOfferReceived Pointer to a value that will replace the current
2100 ProxyOfferReceived field.
2101 @param[in] NewPxeDiscoverValid Pointer to a value that will replace the current
2102 ProxyOfferReceived field.
2103 @param[in] NewPxeReplyReceived Pointer to a value that will replace the current
2104 PxeReplyReceived field.
2105 @param[in] NewPxeBisReplyReceived Pointer to a value that will replace the current
2106 PxeBisReplyReceived field.
2107 @param[in] NewDhcpDiscover Pointer to the new cached DHCP Discover packet contents.
2108 @param[in] NewDhcpAck Pointer to the new cached DHCP Ack packet contents.
2109 @param[in] NewProxyOffer Pointer to the new cached Proxy Offer packet contents.
2110 @param[in] NewPxeDiscover Pointer to the new cached PXE Discover packet contents.
2111 @param[in] NewPxeReply Pointer to the new cached PXE Reply packet contents.
2112 @param[in] NewPxeBisReply Pointer to the new cached PXE BIS Reply packet contents.
2113
2114 @retval EFI_SUCCESS The cached packet contents were updated.
2115 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
2116 @retval EFI_INVALID_PARAMETER This is NULL or does not point to a valid
2117 EFI_PXE_BASE_CODE_PROTOCOL structure.
2118
2119**/
2120EFI_STATUS
2121EFIAPI
2122EfiPxeBcSetPackets (
2123 IN EFI_PXE_BASE_CODE_PROTOCOL *This,
2124 IN BOOLEAN *NewDhcpDiscoverValid OPTIONAL,
2125 IN BOOLEAN *NewDhcpAckReceived OPTIONAL,
2126 IN BOOLEAN *NewProxyOfferReceived OPTIONAL,
2127 IN BOOLEAN *NewPxeDiscoverValid OPTIONAL,
2128 IN BOOLEAN *NewPxeReplyReceived OPTIONAL,
2129 IN BOOLEAN *NewPxeBisReplyReceived OPTIONAL,
2130 IN EFI_PXE_BASE_CODE_PACKET *NewDhcpDiscover OPTIONAL,
2131 IN EFI_PXE_BASE_CODE_PACKET *NewDhcpAck OPTIONAL,
2132 IN EFI_PXE_BASE_CODE_PACKET *NewProxyOffer OPTIONAL,
2133 IN EFI_PXE_BASE_CODE_PACKET *NewPxeDiscover OPTIONAL,
2134 IN EFI_PXE_BASE_CODE_PACKET *NewPxeReply OPTIONAL,
2135 IN EFI_PXE_BASE_CODE_PACKET *NewPxeBisReply OPTIONAL
2136 )
2137{
2138 PXEBC_PRIVATE_DATA *Private;
2139 EFI_PXE_BASE_CODE_MODE *Mode;
2140
2141 if (This == NULL) {
2142 return EFI_INVALID_PARAMETER;
2143 }
2144
2145 Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
2146 Mode = Private->PxeBc.Mode;
2147
2148 if (!Mode->Started) {
2149 return EFI_NOT_STARTED;
2150 }
2151
2152 if (NewDhcpDiscoverValid != NULL) {
2153 Mode->DhcpDiscoverValid = *NewDhcpDiscoverValid;
2154 }
2155
2156 if (NewDhcpAckReceived != NULL) {
2157 Mode->DhcpAckReceived = *NewDhcpAckReceived;
2158 }
2159
2160 if (NewProxyOfferReceived != NULL) {
2161 Mode->ProxyOfferReceived = *NewProxyOfferReceived;
2162 }
2163
2164 if (NewPxeDiscoverValid != NULL) {
2165 Mode->PxeDiscoverValid = *NewPxeDiscoverValid;
2166 }
2167
2168 if (NewPxeReplyReceived != NULL) {
2169 Mode->PxeReplyReceived = *NewPxeReplyReceived;
2170 }
2171
2172 if (NewPxeBisReplyReceived != NULL) {
2173 Mode->PxeBisReplyReceived = *NewPxeBisReplyReceived;
2174 }
2175
2176 if (NewDhcpDiscover != NULL) {
2177 CopyMem (&Mode->DhcpDiscover, NewDhcpDiscover, sizeof (EFI_PXE_BASE_CODE_PACKET));
2178 }
2179
2180 if (NewDhcpAck != NULL) {
2181 CopyMem (&Mode->DhcpAck, NewDhcpAck, sizeof (EFI_PXE_BASE_CODE_PACKET));
2182 }
2183
2184 if (NewProxyOffer != NULL) {
2185 CopyMem (&Mode->ProxyOffer, NewProxyOffer, sizeof (EFI_PXE_BASE_CODE_PACKET));
2186 }
2187
2188 if (NewPxeDiscover != NULL) {
2189 CopyMem (&Mode->PxeDiscover, NewPxeDiscover, sizeof (EFI_PXE_BASE_CODE_PACKET));
2190 }
2191
2192 if (NewPxeReply != NULL) {
2193 CopyMem (&Mode->PxeReply, NewPxeReply, sizeof (EFI_PXE_BASE_CODE_PACKET));
2194 }
2195
2196 if (NewPxeBisReply != NULL) {
2197 CopyMem (&Mode->PxeBisReply, NewPxeBisReply, sizeof (EFI_PXE_BASE_CODE_PACKET));
2198 }
2199
2200 return EFI_SUCCESS;
2201}
2202
2203EFI_PXE_BASE_CODE_PROTOCOL gPxeBcProtocolTemplate = {
2204 EFI_PXE_BASE_CODE_PROTOCOL_REVISION,
2205 EfiPxeBcStart,
2206 EfiPxeBcStop,
2207 EfiPxeBcDhcp,
2208 EfiPxeBcDiscover,
2209 EfiPxeBcMtftp,
2210 EfiPxeBcUdpWrite,
2211 EfiPxeBcUdpRead,
2212 EfiPxeBcSetIpFilter,
2213 EfiPxeBcArp,
2214 EfiPxeBcSetParameters,
2215 EfiPxeBcSetStationIP,
2216 EfiPxeBcSetPackets,
2217 NULL
2218};
2219
2220/**
2221 Callback function that is invoked when the PXE Base Code Protocol is about to transmit, has
2222 received, or is waiting to receive a packet.
2223
2224 This function is invoked when the PXE Base Code Protocol is about to transmit, has received,
2225 or is waiting to receive a packet. Parameters Function and Received specify the type of event.
2226 Parameters PacketLen and Packet specify the packet that generated the event. If these fields
2227 are zero and NULL respectively, then this is a status update callback. If the operation specified
2228 by Function is to continue, then CALLBACK_STATUS_CONTINUE should be returned. If the operation
2229 specified by Function should be aborted, then CALLBACK_STATUS_ABORT should be returned. Due to
2230 the polling nature of UEFI device drivers, a callback function should not execute for more than 5 ms.
2231 The SetParameters() function must be called after a Callback Protocol is installed to enable the
2232 use of callbacks.
2233
2234 @param[in] This Pointer to the EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL instance.
2235 @param[in] Function The PXE Base Code Protocol function that is waiting for an event.
2236 @param[in] Received TRUE if the callback is being invoked due to a receive event. FALSE if
2237 the callback is being invoked due to a transmit event.
2238 @param[in] PacketLength The length, in bytes, of Packet. This field will have a value of zero if
2239 this is a wait for receive event.
2240 @param[in] PacketPtr If Received is TRUE, a pointer to the packet that was just received;
2241 otherwise a pointer to the packet that is about to be transmitted.
2242
2243 @retval EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE If Function specifies a continue operation.
2244 @retval EFI_PXE_BASE_CODE_CALLBACK_STATUS_ABORT If Function specifies an abort operation.
2245
2246**/
2247EFI_PXE_BASE_CODE_CALLBACK_STATUS
2248EFIAPI
2249EfiPxeLoadFileCallback (
2250 IN EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL *This,
2251 IN EFI_PXE_BASE_CODE_FUNCTION Function,
2252 IN BOOLEAN Received,
2253 IN UINT32 PacketLength,
2254 IN EFI_PXE_BASE_CODE_PACKET *PacketPtr OPTIONAL
2255 )
2256{
2257 EFI_INPUT_KEY Key;
2258 EFI_STATUS Status;
2259
2260 //
2261 // Catch Ctrl-C or ESC to abort.
2262 //
2263 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
2264
2265 if (!EFI_ERROR (Status)) {
2266 if ((Key.ScanCode == SCAN_ESC) || (Key.UnicodeChar == (0x1F & 'c'))) {
2267 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_ABORT;
2268 }
2269 }
2270
2271 //
2272 // No print if receive packet
2273 //
2274 if (Received) {
2275 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE;
2276 }
2277
2278 //
2279 // Print only for three functions
2280 //
2281 switch (Function) {
2282 case EFI_PXE_BASE_CODE_FUNCTION_MTFTP:
2283 //
2284 // Print only for open MTFTP packets, not every MTFTP packets
2285 //
2286 if ((PacketLength != 0) && (PacketPtr != NULL)) {
2287 if ((PacketPtr->Raw[0x1C] != 0x00) || (PacketPtr->Raw[0x1D] != 0x01)) {
2288 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE;
2289 }
2290 }
2291
2292 break;
2293
2294 case EFI_PXE_BASE_CODE_FUNCTION_DHCP:
2295 case EFI_PXE_BASE_CODE_FUNCTION_DISCOVER:
2296 break;
2297
2298 default:
2299 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE;
2300 }
2301
2302 if ((PacketLength != 0) && (PacketPtr != NULL)) {
2303 //
2304 // Print '.' when transmit a packet
2305 //
2306 AsciiPrint (".");
2307 }
2308
2309 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE;
2310}
2311
2312EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL gPxeBcCallBackTemplate = {
2313 EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL_REVISION,
2314 EfiPxeLoadFileCallback
2315};
2316
2317/**
2318 Causes the driver to load a specified file.
2319
2320 @param[in] This Protocol instance pointer.
2321 @param[in] FilePath The device specific path of the file to load.
2322 @param[in] BootPolicy If TRUE, indicates that the request originates from the
2323 boot manager is attempting to load FilePath as a boot
2324 selection. If FALSE, then FilePath must match an exact file
2325 to be loaded.
2326 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return
2327 code of EFI_SUCCESS, the amount of data transferred to
2328 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
2329 the size of Buffer required to retrieve the requested file.
2330 @param[in] Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
2331 then no the size of the requested file is returned in
2332 BufferSize.
2333
2334 @retval EFI_SUCCESS The file was loaded.
2335 @retval EFI_UNSUPPORTED The device does not support the provided BootPolicy.
2336 @retval EFI_INVALID_PARAMETER FilePath is not a valid device path, or
2337 BufferSize is NULL.
2338 @retval EFI_NO_MEDIA No medium was present to load the file.
2339 @retval EFI_DEVICE_ERROR The file was not loaded due to a device error.
2340 @retval EFI_NO_RESPONSE The remote system did not respond.
2341 @retval EFI_NOT_FOUND The file was not found.
2342 @retval EFI_ABORTED The file load process was manually cancelled.
2343
2344**/
2345EFI_STATUS
2346EFIAPI
2347EfiPxeLoadFile (
2348 IN EFI_LOAD_FILE_PROTOCOL *This,
2349 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
2350 IN BOOLEAN BootPolicy,
2351 IN OUT UINTN *BufferSize,
2352 IN VOID *Buffer OPTIONAL
2353 )
2354{
2355 PXEBC_PRIVATE_DATA *Private;
2356 PXEBC_VIRTUAL_NIC *VirtualNic;
2357 EFI_PXE_BASE_CODE_PROTOCOL *PxeBc;
2358 BOOLEAN UsingIpv6;
2359 EFI_STATUS Status;
2360 EFI_STATUS MediaStatus;
2361
2362 if ((This == NULL) || (BufferSize == NULL) || (FilePath == NULL) || !IsDevicePathEnd (FilePath)) {
2363 return EFI_INVALID_PARAMETER;
2364 }
2365
2366 //
2367 // Only support BootPolicy
2368 //
2369 if (!BootPolicy) {
2370 return EFI_UNSUPPORTED;
2371 }
2372
2373 VirtualNic = PXEBC_VIRTUAL_NIC_FROM_LOADFILE (This);
2374 Private = VirtualNic->Private;
2375 PxeBc = &Private->PxeBc;
2376 UsingIpv6 = FALSE;
2377 Status = EFI_DEVICE_ERROR;
2378
2379 //
2380 // Check media status before PXE start
2381 //
2382 MediaStatus = EFI_SUCCESS;
2383 NetLibDetectMediaWaitTimeout (Private->Controller, PXEBC_CHECK_MEDIA_WAITING_TIME, &MediaStatus);
2384 if (MediaStatus != EFI_SUCCESS) {
2385 return EFI_NO_MEDIA;
2386 }
2387
2388 //
2389 // Check whether the virtual nic is using IPv6 or not.
2390 //
2391 if (VirtualNic == Private->Ip6Nic) {
2392 UsingIpv6 = TRUE;
2393 }
2394
2395 //
2396 // Start Pxe Base Code to initialize PXE boot.
2397 //
2398 Status = PxeBc->Start (PxeBc, UsingIpv6);
2399 if ((Status == EFI_ALREADY_STARTED) && (UsingIpv6 != PxeBc->Mode->UsingIpv6)) {
2400 //
2401 // PxeBc protocol has already been started but not on the required IP version, restart it.
2402 //
2403 Status = PxeBc->Stop (PxeBc);
2404 if (!EFI_ERROR (Status)) {
2405 Status = PxeBc->Start (PxeBc, UsingIpv6);
2406 }
2407 }
2408
2409 if ((Status == EFI_SUCCESS) || (Status == EFI_ALREADY_STARTED)) {
2410 Status = PxeBcLoadBootFile (Private, BufferSize, Buffer);
2411 }
2412
2413 if ((Status != EFI_SUCCESS) &&
2414 (Status != EFI_UNSUPPORTED) &&
2415 (Status != EFI_BUFFER_TOO_SMALL))
2416 {
2417 //
2418 // There are three cases, which needn't stop pxebc here.
2419 // 1. success to download file.
2420 // 2. success to get file size.
2421 // 3. unsupported.
2422 //
2423 PxeBc->Stop (PxeBc);
2424 } else {
2425 //
2426 // The DHCP4 can have only one configured child instance so we need to stop
2427 // reset the DHCP4 child before we return. Otherwise these programs which
2428 // also need to use DHCP4 will be impacted.
2429 //
2430 if (!PxeBc->Mode->UsingIpv6) {
2431 Private->Dhcp4->Stop (Private->Dhcp4);
2432 Private->Dhcp4->Configure (Private->Dhcp4, NULL);
2433 }
2434 }
2435
2436 return Status;
2437}
2438
2439EFI_LOAD_FILE_PROTOCOL gLoadFileProtocolTemplate = { EfiPxeLoadFile };
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette