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