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