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