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