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