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