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