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