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