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