]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/UefiPxeBcDxe/PxeBcDriver.c
NetworkPkg: Protocol Uninstallation Cleanup
[mirror_edk2.git] / NetworkPkg / UefiPxeBcDxe / PxeBcDriver.c
1 /** @file
2 Driver Binding functions implementationfor for UefiPxeBc Driver.
3
4 (C) Copyright 2014 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
6
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php.
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17 #include "PxeBcImpl.h"
18
19
20 EFI_DRIVER_BINDING_PROTOCOL gPxeBcIp4DriverBinding = {
21 PxeBcIp4DriverBindingSupported,
22 PxeBcIp4DriverBindingStart,
23 PxeBcIp4DriverBindingStop,
24 0xa,
25 NULL,
26 NULL
27 };
28
29 EFI_DRIVER_BINDING_PROTOCOL gPxeBcIp6DriverBinding = {
30 PxeBcIp6DriverBindingSupported,
31 PxeBcIp6DriverBindingStart,
32 PxeBcIp6DriverBindingStop,
33 0xa,
34 NULL,
35 NULL
36 };
37
38 /**
39 Get the Nic handle using any child handle in the IPv4 stack.
40
41 @param[in] ControllerHandle Pointer to child handle over IPv4.
42
43 @return NicHandle The pointer to the Nic handle.
44
45 **/
46 EFI_HANDLE
47 PxeBcGetNicByIp4Children (
48 IN EFI_HANDLE ControllerHandle
49 )
50 {
51 EFI_HANDLE NicHandle;
52
53 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiArpProtocolGuid);
54 if (NicHandle == NULL) {
55 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiIp4ProtocolGuid);
56 if (NicHandle == NULL) {
57 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiUdp4ProtocolGuid);
58 if (NicHandle == NULL) {
59 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiDhcp4ProtocolGuid);
60 if (NicHandle == NULL) {
61 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiMtftp4ProtocolGuid);
62 if (NicHandle == NULL) {
63 return NULL;
64 }
65 }
66 }
67 }
68 }
69
70 return NicHandle;
71 }
72
73
74 /**
75 Get the Nic handle using any child handle in the IPv6 stack.
76
77 @param[in] ControllerHandle Pointer to child handle over IPv6.
78
79 @return NicHandle The pointer to the Nic handle.
80
81 **/
82 EFI_HANDLE
83 PxeBcGetNicByIp6Children (
84 IN EFI_HANDLE ControllerHandle
85 )
86 {
87 EFI_HANDLE NicHandle;
88
89 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiIp6ProtocolGuid);
90 if (NicHandle == NULL) {
91 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiUdp6ProtocolGuid);
92 if (NicHandle == NULL) {
93 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiDhcp6ProtocolGuid);
94 if (NicHandle == NULL) {
95 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiMtftp6ProtocolGuid);
96 if (NicHandle == NULL) {
97 return NULL;
98 }
99 }
100 }
101 }
102
103 return NicHandle;
104 }
105
106
107 /**
108 Destroy the opened instances based on IPv4.
109
110 @param[in] This Pointer to the EFI_DRIVER_BINDING_PROTOCOL.
111 @param[in] Private Pointer to PXEBC_PRIVATE_DATA.
112
113 **/
114 VOID
115 PxeBcDestroyIp4Children (
116 IN EFI_DRIVER_BINDING_PROTOCOL *This,
117 IN PXEBC_PRIVATE_DATA *Private
118 )
119 {
120 ASSERT(Private != NULL);
121
122 if (Private->ArpChild != NULL) {
123 //
124 // Close Arp for PxeBc->Arp and destroy the instance.
125 //
126 gBS->CloseProtocol (
127 Private->ArpChild,
128 &gEfiArpProtocolGuid,
129 This->DriverBindingHandle,
130 Private->Controller
131 );
132
133 NetLibDestroyServiceChild (
134 Private->Controller,
135 This->DriverBindingHandle,
136 &gEfiArpServiceBindingProtocolGuid,
137 Private->ArpChild
138 );
139 }
140
141 if (Private->Ip4Child != NULL) {
142 //
143 // Close Ip4 for background ICMP error message and destroy the instance.
144 //
145 gBS->CloseProtocol (
146 Private->Ip4Child,
147 &gEfiIp4ProtocolGuid,
148 This->DriverBindingHandle,
149 Private->Controller
150 );
151
152 NetLibDestroyServiceChild (
153 Private->Controller,
154 This->DriverBindingHandle,
155 &gEfiIp4ServiceBindingProtocolGuid,
156 Private->Ip4Child
157 );
158 }
159
160 if (Private->Udp4WriteChild != NULL) {
161 //
162 // Close Udp4 for PxeBc->UdpWrite and destroy the instance.
163 //
164 gBS->CloseProtocol (
165 Private->Udp4WriteChild,
166 &gEfiUdp4ProtocolGuid,
167 This->DriverBindingHandle,
168 Private->Controller
169 );
170
171 NetLibDestroyServiceChild (
172 Private->Controller,
173 This->DriverBindingHandle,
174 &gEfiUdp4ServiceBindingProtocolGuid,
175 Private->Udp4WriteChild
176 );
177 }
178
179 if (Private->Udp4ReadChild != NULL) {
180 //
181 // Close Udp4 for PxeBc->UdpRead and destroy the instance.
182 //
183 gBS->CloseProtocol (
184 Private->Udp4ReadChild,
185 &gEfiUdp4ProtocolGuid,
186 This->DriverBindingHandle,
187 Private->Controller
188 );
189
190 NetLibDestroyServiceChild (
191 Private->Controller,
192 This->DriverBindingHandle,
193 &gEfiUdp4ServiceBindingProtocolGuid,
194 Private->Udp4ReadChild
195 );
196 }
197
198 if (Private->Mtftp4Child != NULL) {
199 //
200 // Close Mtftp4 for PxeBc->Mtftp4 and destroy the instance.
201 //
202 gBS->CloseProtocol (
203 Private->Mtftp4Child,
204 &gEfiMtftp4ProtocolGuid,
205 This->DriverBindingHandle,
206 Private->Controller
207 );
208
209 NetLibDestroyServiceChild (
210 Private->Controller,
211 This->DriverBindingHandle,
212 &gEfiMtftp4ServiceBindingProtocolGuid,
213 Private->Mtftp4Child
214 );
215 }
216
217 if (Private->Dhcp4Child != NULL) {
218 //
219 // Close Dhcp4 for PxeBc->Dhcp4 and destroy the instance.
220 //
221 gBS->CloseProtocol (
222 Private->Dhcp4Child,
223 &gEfiDhcp4ProtocolGuid,
224 This->DriverBindingHandle,
225 Private->Controller
226 );
227
228 NetLibDestroyServiceChild (
229 Private->Controller,
230 This->DriverBindingHandle,
231 &gEfiDhcp4ServiceBindingProtocolGuid,
232 Private->Dhcp4Child
233 );
234 }
235
236 if (Private->Ip4Nic != NULL) {
237 //
238 // Close PxeBcPrivate from the parent Nic handle and destroy the virtual handle.
239 //
240 gBS->CloseProtocol (
241 Private->Controller,
242 &gEfiCallerIdGuid,
243 This->DriverBindingHandle,
244 Private->Ip4Nic->Controller
245 );
246
247 gBS->UninstallMultipleProtocolInterfaces (
248 Private->Ip4Nic->Controller,
249 &gEfiDevicePathProtocolGuid,
250 Private->Ip4Nic->DevicePath,
251 &gEfiLoadFileProtocolGuid,
252 &Private->Ip4Nic->LoadFile,
253 &gEfiPxeBaseCodeProtocolGuid,
254 &Private->PxeBc,
255 NULL
256 );
257 FreePool (Private->Ip4Nic->DevicePath);
258
259 if (Private->Snp != NULL) {
260 //
261 // Close SNP from the child virtual handle
262 //
263 gBS->CloseProtocol (
264 Private->Ip4Nic->Controller,
265 &gEfiSimpleNetworkProtocolGuid,
266 This->DriverBindingHandle,
267 Private->Ip4Nic->Controller
268 );
269
270 gBS->UninstallProtocolInterface (
271 Private->Ip4Nic->Controller,
272 &gEfiSimpleNetworkProtocolGuid,
273 Private->Snp
274 );
275 }
276 FreePool (Private->Ip4Nic);
277 }
278
279 Private->ArpChild = NULL;
280 Private->Ip4Child = NULL;
281 Private->Udp4WriteChild = NULL;
282 Private->Udp4ReadChild = NULL;
283 Private->Mtftp4Child = NULL;
284 Private->Dhcp4Child = NULL;
285 Private->Ip4Nic = NULL;
286 }
287
288
289 /**
290 Destroy the opened instances based on IPv6.
291
292 @param[in] This Pointer to the EFI_DRIVER_BINDING_PROTOCOL.
293 @param[in] Private Pointer to PXEBC_PRIVATE_DATA.
294
295 **/
296 VOID
297 PxeBcDestroyIp6Children (
298 IN EFI_DRIVER_BINDING_PROTOCOL *This,
299 IN PXEBC_PRIVATE_DATA *Private
300 )
301 {
302 ASSERT(Private != NULL);
303
304 if (Private->Ip6Child != NULL) {
305 //
306 // Close Ip6 for Ip6->Ip6Config and destroy the instance.
307 //
308 gBS->CloseProtocol (
309 Private->Ip6Child,
310 &gEfiIp6ProtocolGuid,
311 This->DriverBindingHandle,
312 Private->Controller
313 );
314
315 NetLibDestroyServiceChild (
316 Private->Controller,
317 This->DriverBindingHandle,
318 &gEfiIp6ServiceBindingProtocolGuid,
319 Private->Ip6Child
320 );
321 }
322
323 if (Private->Udp6WriteChild != NULL) {
324 //
325 // Close Udp6 for PxeBc->UdpWrite and destroy the instance.
326 //
327 gBS->CloseProtocol (
328 Private->Udp6WriteChild,
329 &gEfiUdp6ProtocolGuid,
330 This->DriverBindingHandle,
331 Private->Controller
332 );
333 NetLibDestroyServiceChild (
334 Private->Controller,
335 This->DriverBindingHandle,
336 &gEfiUdp6ServiceBindingProtocolGuid,
337 Private->Udp6WriteChild
338 );
339 }
340
341 if (Private->Udp6ReadChild != NULL) {
342 //
343 // Close Udp6 for PxeBc->UdpRead and destroy the instance.
344 //
345 gBS->CloseProtocol (
346 Private->Udp6ReadChild,
347 &gEfiUdp6ProtocolGuid,
348 This->DriverBindingHandle,
349 Private->Controller
350 );
351 NetLibDestroyServiceChild (
352 Private->Controller,
353 This->DriverBindingHandle,
354 &gEfiUdp6ServiceBindingProtocolGuid,
355 Private->Udp6ReadChild
356 );
357 }
358
359 if (Private->Mtftp6Child != NULL) {
360 //
361 // Close Mtftp6 for PxeBc->Mtftp and destroy the instance.
362 //
363 gBS->CloseProtocol (
364 Private->Mtftp6Child,
365 &gEfiMtftp6ProtocolGuid,
366 This->DriverBindingHandle,
367 Private->Controller
368 );
369
370 NetLibDestroyServiceChild (
371 Private->Controller,
372 This->DriverBindingHandle,
373 &gEfiMtftp6ServiceBindingProtocolGuid,
374 Private->Mtftp6Child
375 );
376 }
377
378 if (Private->Dhcp6Child != NULL) {
379 //
380 // Close Dhcp6 for PxeBc->Dhcp and destroy the instance.
381 //
382 gBS->CloseProtocol (
383 Private->Dhcp6Child,
384 &gEfiDhcp6ProtocolGuid,
385 This->DriverBindingHandle,
386 Private->Controller
387 );
388
389 NetLibDestroyServiceChild (
390 Private->Controller,
391 This->DriverBindingHandle,
392 &gEfiDhcp6ServiceBindingProtocolGuid,
393 Private->Dhcp6Child
394 );
395 }
396
397 if (Private->Ip6Nic != NULL) {
398 //
399 // Close PxeBcPrivate from the parent Nic handle and destroy the virtual handle.
400 //
401 gBS->CloseProtocol (
402 Private->Controller,
403 &gEfiCallerIdGuid,
404 This->DriverBindingHandle,
405 Private->Ip6Nic->Controller
406 );
407
408 gBS->UninstallMultipleProtocolInterfaces (
409 Private->Ip6Nic->Controller,
410 &gEfiDevicePathProtocolGuid,
411 Private->Ip6Nic->DevicePath,
412 &gEfiLoadFileProtocolGuid,
413 &Private->Ip6Nic->LoadFile,
414 &gEfiPxeBaseCodeProtocolGuid,
415 &Private->PxeBc,
416 NULL
417 );
418 FreePool (Private->Ip6Nic->DevicePath);
419
420 if (Private->Snp != NULL) {
421 //
422 // Close SNP from the child virtual handle
423 //
424 gBS->CloseProtocol (
425 Private->Ip6Nic->Controller,
426 &gEfiSimpleNetworkProtocolGuid,
427 This->DriverBindingHandle,
428 Private->Ip6Nic->Controller
429 );
430 gBS->UninstallProtocolInterface (
431 Private->Ip6Nic->Controller,
432 &gEfiSimpleNetworkProtocolGuid,
433 Private->Snp
434 );
435 }
436 FreePool (Private->Ip6Nic);
437 }
438
439 Private->Ip6Child = NULL;
440 Private->Udp6WriteChild = NULL;
441 Private->Udp6ReadChild = NULL;
442 Private->Mtftp6Child = NULL;
443 Private->Dhcp6Child = NULL;
444 Private->Ip6Nic = NULL;
445 Private->Mode.Ipv6Available = FALSE;
446 }
447
448 /**
449 Check whether UNDI protocol supports IPv6.
450
451 @param[in] ControllerHandle Controller handle.
452 @param[in] Private Pointer to PXEBC_PRIVATE_DATA.
453 @param[out] Ipv6Support TRUE if UNDI supports IPv6.
454
455 @retval EFI_SUCCESS Get the result whether UNDI supports IPv6 by NII or AIP protocol successfully.
456 @retval EFI_NOT_FOUND Don't know whether UNDI supports IPv6 since NII or AIP is not available.
457
458 **/
459 EFI_STATUS
460 PxeBcCheckIpv6Support (
461 IN EFI_HANDLE ControllerHandle,
462 IN PXEBC_PRIVATE_DATA *Private,
463 OUT BOOLEAN *Ipv6Support
464 )
465 {
466 EFI_HANDLE Handle;
467 EFI_ADAPTER_INFORMATION_PROTOCOL *Aip;
468 EFI_STATUS Status;
469 EFI_GUID *InfoTypesBuffer;
470 UINTN InfoTypeBufferCount;
471 UINTN TypeIndex;
472 BOOLEAN Supported;
473 VOID *InfoBlock;
474 UINTN InfoBlockSize;
475
476 ASSERT (Private != NULL && Ipv6Support != NULL);
477
478 //
479 // Check whether the UNDI supports IPv6 by NII protocol.
480 //
481 if (Private->Nii != NULL) {
482 *Ipv6Support = Private->Nii->Ipv6Supported;
483 return EFI_SUCCESS;
484 }
485
486 //
487 // Check whether the UNDI supports IPv6 by AIP protocol.
488 //
489
490 //
491 // Get the NIC handle by SNP protocol.
492 //
493 Handle = NetLibGetSnpHandle (ControllerHandle, NULL);
494 if (Handle == NULL) {
495 return EFI_NOT_FOUND;
496 }
497
498 Aip = NULL;
499 Status = gBS->HandleProtocol (
500 Handle,
501 &gEfiAdapterInformationProtocolGuid,
502 (VOID *) &Aip
503 );
504 if (EFI_ERROR (Status) || Aip == NULL) {
505 return EFI_NOT_FOUND;
506 }
507
508 InfoTypesBuffer = NULL;
509 InfoTypeBufferCount = 0;
510 Status = Aip->GetSupportedTypes (Aip, &InfoTypesBuffer, &InfoTypeBufferCount);
511 if (EFI_ERROR (Status) || InfoTypesBuffer == NULL) {
512 FreePool (InfoTypesBuffer);
513 return EFI_NOT_FOUND;
514 }
515
516 Supported = FALSE;
517 for (TypeIndex = 0; TypeIndex < InfoTypeBufferCount; TypeIndex++) {
518 if (CompareGuid (&InfoTypesBuffer[TypeIndex], &gEfiAdapterInfoUndiIpv6SupportGuid)) {
519 Supported = TRUE;
520 break;
521 }
522 }
523
524 FreePool (InfoTypesBuffer);
525 if (!Supported) {
526 return EFI_NOT_FOUND;
527 }
528
529 //
530 // We now have adapter information block.
531 //
532 InfoBlock = NULL;
533 InfoBlockSize = 0;
534 Status = Aip->GetInformation (Aip, &gEfiAdapterInfoUndiIpv6SupportGuid, &InfoBlock, &InfoBlockSize);
535 if (EFI_ERROR (Status) || InfoBlock == NULL) {
536 FreePool (InfoBlock);
537 return EFI_NOT_FOUND;
538 }
539
540 *Ipv6Support = ((EFI_ADAPTER_INFO_UNDI_IPV6_SUPPORT *) InfoBlock)->Ipv6Support;
541 FreePool (InfoBlock);
542 return EFI_SUCCESS;
543
544 }
545
546 /**
547 Create the opened instances based on IPv4.
548
549 @param[in] This Pointer to EFI_DRIVER_BINDING_PROTOCOL.
550 @param[in] ControllerHandle Handle of the child to destroy.
551 @param[in] Private Handle Pointer to PXEBC_PRIVATE_DATA.
552
553 @retval EFI_SUCCESS The instances based on IPv4 were all created successfully.
554 @retval Others An unexpected error occurred.
555
556 **/
557 EFI_STATUS
558 PxeBcCreateIp4Children (
559 IN EFI_DRIVER_BINDING_PROTOCOL *This,
560 IN EFI_HANDLE ControllerHandle,
561 IN PXEBC_PRIVATE_DATA *Private
562 )
563 {
564 EFI_STATUS Status;
565 IPv4_DEVICE_PATH Ip4Node;
566 EFI_PXE_BASE_CODE_MODE *Mode;
567 EFI_UDP4_CONFIG_DATA *Udp4CfgData;
568 EFI_IP4_CONFIG_DATA *Ip4CfgData;
569 EFI_IP4_MODE_DATA Ip4ModeData;
570 PXEBC_PRIVATE_PROTOCOL *Id;
571 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
572
573 if (Private->Ip4Nic != NULL) {
574 //
575 // Already created before.
576 //
577 return EFI_SUCCESS;
578 }
579
580 //
581 // Create Dhcp4 child and open Dhcp4 protocol for PxeBc->Dhcp.
582 //
583 Status = NetLibCreateServiceChild (
584 ControllerHandle,
585 This->DriverBindingHandle,
586 &gEfiDhcp4ServiceBindingProtocolGuid,
587 &Private->Dhcp4Child
588 );
589 if (EFI_ERROR (Status)) {
590 goto ON_ERROR;
591 }
592
593 Status = gBS->OpenProtocol (
594 Private->Dhcp4Child,
595 &gEfiDhcp4ProtocolGuid,
596 (VOID **) &Private->Dhcp4,
597 This->DriverBindingHandle,
598 ControllerHandle,
599 EFI_OPEN_PROTOCOL_BY_DRIVER
600 );
601 if (EFI_ERROR (Status)) {
602 goto ON_ERROR;
603 }
604
605 //
606 // Create Mtftp4 child and open Mtftp4 protocol for PxeBc->Mtftp.
607 //
608 Status = NetLibCreateServiceChild (
609 ControllerHandle,
610 This->DriverBindingHandle,
611 &gEfiMtftp4ServiceBindingProtocolGuid,
612 &Private->Mtftp4Child
613 );
614 if (EFI_ERROR (Status)) {
615 goto ON_ERROR;
616 }
617
618 Status = gBS->OpenProtocol (
619 Private->Mtftp4Child,
620 &gEfiMtftp4ProtocolGuid,
621 (VOID **) &Private->Mtftp4,
622 This->DriverBindingHandle,
623 ControllerHandle,
624 EFI_OPEN_PROTOCOL_BY_DRIVER
625 );
626 if (EFI_ERROR (Status)) {
627 goto ON_ERROR;
628 }
629
630 //
631 // Create Udp4 child and open Udp4 protocol for PxeBc->UdpRead.
632 //
633 Status = NetLibCreateServiceChild (
634 ControllerHandle,
635 This->DriverBindingHandle,
636 &gEfiUdp4ServiceBindingProtocolGuid,
637 &Private->Udp4ReadChild
638 );
639 if (EFI_ERROR (Status)) {
640 goto ON_ERROR;
641 }
642
643 Status = gBS->OpenProtocol (
644 Private->Udp4ReadChild,
645 &gEfiUdp4ProtocolGuid,
646 (VOID **) &Private->Udp4Read,
647 This->DriverBindingHandle,
648 ControllerHandle,
649 EFI_OPEN_PROTOCOL_BY_DRIVER
650 );
651 if (EFI_ERROR (Status)) {
652 goto ON_ERROR;
653 }
654
655 //
656 // Create Udp4 child and open Udp4 protocol for PxeBc->UdpWrite.
657 //
658 Status = NetLibCreateServiceChild (
659 ControllerHandle,
660 This->DriverBindingHandle,
661 &gEfiUdp4ServiceBindingProtocolGuid,
662 &Private->Udp4WriteChild
663 );
664 if (EFI_ERROR (Status)) {
665 goto ON_ERROR;
666 }
667
668 Status = gBS->OpenProtocol (
669 Private->Udp4WriteChild,
670 &gEfiUdp4ProtocolGuid,
671 (VOID **) &Private->Udp4Write,
672 This->DriverBindingHandle,
673 ControllerHandle,
674 EFI_OPEN_PROTOCOL_BY_DRIVER
675 );
676 if (EFI_ERROR (Status)) {
677 goto ON_ERROR;
678 }
679
680 //
681 // Create Arp child and open Arp protocol for PxeBc->Arp.
682 //
683 Status = NetLibCreateServiceChild (
684 ControllerHandle,
685 This->DriverBindingHandle,
686 &gEfiArpServiceBindingProtocolGuid,
687 &Private->ArpChild
688 );
689 if (EFI_ERROR (Status)) {
690 goto ON_ERROR;
691 }
692
693 Status = gBS->OpenProtocol (
694 Private->ArpChild,
695 &gEfiArpProtocolGuid,
696 (VOID **) &Private->Arp,
697 This->DriverBindingHandle,
698 ControllerHandle,
699 EFI_OPEN_PROTOCOL_BY_DRIVER
700 );
701 if (EFI_ERROR (Status)) {
702 goto ON_ERROR;
703 }
704
705 //
706 // Create Ip4 child and open Ip4 protocol for background ICMP packets.
707 //
708 Status = NetLibCreateServiceChild (
709 ControllerHandle,
710 This->DriverBindingHandle,
711 &gEfiIp4ServiceBindingProtocolGuid,
712 &Private->Ip4Child
713 );
714 if (EFI_ERROR (Status)) {
715 goto ON_ERROR;
716 }
717
718 Status = gBS->OpenProtocol (
719 Private->Ip4Child,
720 &gEfiIp4ProtocolGuid,
721 (VOID **) &Private->Ip4,
722 This->DriverBindingHandle,
723 ControllerHandle,
724 EFI_OPEN_PROTOCOL_BY_DRIVER
725 );
726 if (EFI_ERROR (Status)) {
727 goto ON_ERROR;
728 }
729
730 //
731 // Get max packet size from Ip4 to calculate block size for Tftp later.
732 //
733 Status = Private->Ip4->GetModeData (Private->Ip4, &Ip4ModeData, NULL, NULL);
734 if (EFI_ERROR (Status)) {
735 goto ON_ERROR;
736 }
737
738 Private->Ip4MaxPacketSize = Ip4ModeData.MaxPacketSize;
739
740 Private->Ip4Nic = AllocateZeroPool (sizeof (PXEBC_VIRTUAL_NIC));
741 if (Private->Ip4Nic == NULL) {
742 return EFI_OUT_OF_RESOURCES;
743 }
744
745 Private->Ip4Nic->Private = Private;
746 Private->Ip4Nic->Signature = PXEBC_VIRTUAL_NIC_SIGNATURE;
747
748 //
749 // Locate Ip4->Ip4Config2 and store it for set IPv4 Policy.
750 //
751 Status = gBS->HandleProtocol (
752 ControllerHandle,
753 &gEfiIp4Config2ProtocolGuid,
754 (VOID **) &Private->Ip4Config2
755 );
756 if (EFI_ERROR (Status)) {
757 goto ON_ERROR;
758 }
759
760 //
761 // Create a device path node for Ipv4 virtual nic, and append it.
762 //
763 ZeroMem (&Ip4Node, sizeof (IPv4_DEVICE_PATH));
764 Ip4Node.Header.Type = MESSAGING_DEVICE_PATH;
765 Ip4Node.Header.SubType = MSG_IPv4_DP;
766 Ip4Node.StaticIpAddress = FALSE;
767
768 SetDevicePathNodeLength (&Ip4Node.Header, sizeof (Ip4Node));
769
770 Private->Ip4Nic->DevicePath = AppendDevicePathNode (Private->DevicePath, &Ip4Node.Header);
771
772 if (Private->Ip4Nic->DevicePath == NULL) {
773 Status = EFI_OUT_OF_RESOURCES;
774 goto ON_ERROR;
775 }
776
777 CopyMem (
778 &Private->Ip4Nic->LoadFile,
779 &gLoadFileProtocolTemplate,
780 sizeof (EFI_LOAD_FILE_PROTOCOL)
781 );
782
783 //
784 // Create a new handle for IPv4 virtual nic,
785 // and install PxeBaseCode, LoadFile and DevicePath protocols.
786 //
787 Status = gBS->InstallMultipleProtocolInterfaces (
788 &Private->Ip4Nic->Controller,
789 &gEfiDevicePathProtocolGuid,
790 Private->Ip4Nic->DevicePath,
791 &gEfiLoadFileProtocolGuid,
792 &Private->Ip4Nic->LoadFile,
793 &gEfiPxeBaseCodeProtocolGuid,
794 &Private->PxeBc,
795 NULL
796 );
797 if (EFI_ERROR (Status)) {
798 goto ON_ERROR;
799 }
800
801 if (Private->Snp != NULL) {
802 //
803 // Install SNP protocol on purpose is for some OS loader backward
804 // compatibility consideration.
805 //
806 Status = gBS->InstallProtocolInterface (
807 &Private->Ip4Nic->Controller,
808 &gEfiSimpleNetworkProtocolGuid,
809 EFI_NATIVE_INTERFACE,
810 Private->Snp
811 );
812 if (EFI_ERROR (Status)) {
813 goto ON_ERROR;
814 }
815
816 //
817 // Open SNP on the child handle BY_DRIVER|EXCLUSIVE. It will prevent any additionally
818 // layering to perform the experiment.
819 //
820 Status = gBS->OpenProtocol (
821 Private->Ip4Nic->Controller,
822 &gEfiSimpleNetworkProtocolGuid,
823 (VOID **) &Snp,
824 This->DriverBindingHandle,
825 Private->Ip4Nic->Controller,
826 EFI_OPEN_PROTOCOL_BY_DRIVER|EFI_OPEN_PROTOCOL_EXCLUSIVE
827 );
828 if (EFI_ERROR (Status)) {
829 goto ON_ERROR;
830 }
831 }
832
833 //
834 // Open PxeBaseCodePrivate protocol by child to setup a parent-child relationship between
835 // real NIC handle and the virtual IPv4 NIC handle.
836 //
837 Status = gBS->OpenProtocol (
838 ControllerHandle,
839 &gEfiCallerIdGuid,
840 (VOID **) &Id,
841 This->DriverBindingHandle,
842 Private->Ip4Nic->Controller,
843 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
844 );
845 if (EFI_ERROR (Status)) {
846 goto ON_ERROR;
847 }
848
849 //
850 // Set default configure data for Udp4Read and Ip4 instance.
851 //
852 Mode = Private->PxeBc.Mode;
853 Udp4CfgData = &Private->Udp4CfgData;
854 Ip4CfgData = &Private->Ip4CfgData;
855
856 Udp4CfgData->AcceptBroadcast = FALSE;
857 Udp4CfgData->AcceptAnyPort = TRUE;
858 Udp4CfgData->AllowDuplicatePort = TRUE;
859 Udp4CfgData->TypeOfService = Mode->ToS;
860 Udp4CfgData->TimeToLive = Mode->TTL;
861 Udp4CfgData->ReceiveTimeout = PXEBC_DEFAULT_LIFETIME;
862 Udp4CfgData->TransmitTimeout = PXEBC_DEFAULT_LIFETIME;
863
864 Ip4CfgData->AcceptIcmpErrors = TRUE;
865 Ip4CfgData->DefaultProtocol = EFI_IP_PROTO_ICMP;
866 Ip4CfgData->TypeOfService = Mode->ToS;
867 Ip4CfgData->TimeToLive = Mode->TTL;
868 Ip4CfgData->ReceiveTimeout = PXEBC_DEFAULT_LIFETIME;
869 Ip4CfgData->TransmitTimeout = PXEBC_DEFAULT_LIFETIME;
870
871 return EFI_SUCCESS;
872
873 ON_ERROR:
874 PxeBcDestroyIp4Children (This, Private);
875 return Status;
876 }
877
878
879 /**
880 Create the opened instances based on IPv6.
881
882 @param[in] This Pointer to EFI_DRIVER_BINDING_PROTOCOL.
883 @param[in] ControllerHandle Handle of the child to destroy.
884 @param[in] Private Handle Pointer to PXEBC_PRIVATE_DATA.
885
886 @retval EFI_SUCCESS The instances based on IPv6 were all created successfully.
887 @retval Others An unexpected error occurred.
888
889 **/
890 EFI_STATUS
891 PxeBcCreateIp6Children (
892 IN EFI_DRIVER_BINDING_PROTOCOL *This,
893 IN EFI_HANDLE ControllerHandle,
894 IN PXEBC_PRIVATE_DATA *Private
895 )
896 {
897 EFI_STATUS Status;
898 IPv6_DEVICE_PATH Ip6Node;
899 EFI_UDP6_CONFIG_DATA *Udp6CfgData;
900 EFI_IP6_CONFIG_DATA *Ip6CfgData;
901 EFI_IP6_MODE_DATA Ip6ModeData;
902 PXEBC_PRIVATE_PROTOCOL *Id;
903 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
904 UINTN Index;
905
906 if (Private->Ip6Nic != NULL) {
907 //
908 // Already created before.
909 //
910 return EFI_SUCCESS;
911 }
912
913 Private->Ip6Nic = AllocateZeroPool (sizeof (PXEBC_VIRTUAL_NIC));
914
915 if (Private->Ip6Nic == NULL) {
916 return EFI_OUT_OF_RESOURCES;
917 }
918
919 Private->Ip6Nic->Private = Private;
920 Private->Ip6Nic->Signature = PXEBC_VIRTUAL_NIC_SIGNATURE;
921
922 //
923 // Create Dhcp6 child and open Dhcp6 protocol for PxeBc->Dhcp.
924 //
925 Status = NetLibCreateServiceChild (
926 ControllerHandle,
927 This->DriverBindingHandle,
928 &gEfiDhcp6ServiceBindingProtocolGuid,
929 &Private->Dhcp6Child
930 );
931 if (EFI_ERROR (Status)) {
932 goto ON_ERROR;
933 }
934
935 Status = gBS->OpenProtocol (
936 Private->Dhcp6Child,
937 &gEfiDhcp6ProtocolGuid,
938 (VOID **) &Private->Dhcp6,
939 This->DriverBindingHandle,
940 ControllerHandle,
941 EFI_OPEN_PROTOCOL_BY_DRIVER
942 );
943 if (EFI_ERROR (Status)) {
944 goto ON_ERROR;
945 }
946
947 //
948 // Generate a random IAID for the Dhcp6 assigned address.
949 //
950 Private->IaId = NET_RANDOM (NetRandomInitSeed ());
951 if (Private->Snp != NULL) {
952 for (Index = 0; Index < Private->Snp->Mode->HwAddressSize; Index++) {
953 Private->IaId |= (Private->Snp->Mode->CurrentAddress.Addr[Index] << ((Index << 3) & 31));
954 }
955 }
956
957 //
958 // Create Mtftp6 child and open Mtftp6 protocol for PxeBc->Mtftp.
959 //
960 Status = NetLibCreateServiceChild (
961 ControllerHandle,
962 This->DriverBindingHandle,
963 &gEfiMtftp6ServiceBindingProtocolGuid,
964 &Private->Mtftp6Child
965 );
966 if (EFI_ERROR (Status)) {
967 goto ON_ERROR;
968 }
969
970 Status = gBS->OpenProtocol (
971 Private->Mtftp6Child,
972 &gEfiMtftp6ProtocolGuid,
973 (VOID **) &Private->Mtftp6,
974 This->DriverBindingHandle,
975 ControllerHandle,
976 EFI_OPEN_PROTOCOL_BY_DRIVER
977 );
978 if (EFI_ERROR (Status)) {
979 goto ON_ERROR;
980 }
981
982 //
983 // Create Udp6 child and open Udp6 protocol for PxeBc->UdpRead.
984 //
985 Status = NetLibCreateServiceChild (
986 ControllerHandle,
987 This->DriverBindingHandle,
988 &gEfiUdp6ServiceBindingProtocolGuid,
989 &Private->Udp6ReadChild
990 );
991 if (EFI_ERROR (Status)) {
992 goto ON_ERROR;
993 }
994
995 Status = gBS->OpenProtocol (
996 Private->Udp6ReadChild,
997 &gEfiUdp6ProtocolGuid,
998 (VOID **) &Private->Udp6Read,
999 This->DriverBindingHandle,
1000 ControllerHandle,
1001 EFI_OPEN_PROTOCOL_BY_DRIVER
1002 );
1003 if (EFI_ERROR (Status)) {
1004 goto ON_ERROR;
1005 }
1006
1007 //
1008 // Create Udp6 child and open Udp6 protocol for PxeBc->UdpWrite.
1009 //
1010 Status = NetLibCreateServiceChild (
1011 ControllerHandle,
1012 This->DriverBindingHandle,
1013 &gEfiUdp6ServiceBindingProtocolGuid,
1014 &Private->Udp6WriteChild
1015 );
1016 if (EFI_ERROR (Status)) {
1017 goto ON_ERROR;
1018 }
1019
1020 Status = gBS->OpenProtocol (
1021 Private->Udp6WriteChild,
1022 &gEfiUdp6ProtocolGuid,
1023 (VOID **) &Private->Udp6Write,
1024 This->DriverBindingHandle,
1025 ControllerHandle,
1026 EFI_OPEN_PROTOCOL_BY_DRIVER
1027 );
1028 if (EFI_ERROR (Status)) {
1029 goto ON_ERROR;
1030 }
1031
1032 //
1033 // Create Ip6 child and open Ip6 protocol for background ICMP6 packets.
1034 //
1035 Status = NetLibCreateServiceChild (
1036 ControllerHandle,
1037 This->DriverBindingHandle,
1038 &gEfiIp6ServiceBindingProtocolGuid,
1039 &Private->Ip6Child
1040 );
1041 if (EFI_ERROR (Status)) {
1042 goto ON_ERROR;
1043 }
1044
1045 Status = gBS->OpenProtocol (
1046 Private->Ip6Child,
1047 &gEfiIp6ProtocolGuid,
1048 (VOID **) &Private->Ip6,
1049 This->DriverBindingHandle,
1050 ControllerHandle,
1051 EFI_OPEN_PROTOCOL_BY_DRIVER
1052 );
1053 if (EFI_ERROR (Status)) {
1054 goto ON_ERROR;
1055 }
1056
1057 //
1058 // Get max packet size from Ip6 to calculate block size for Tftp later.
1059 //
1060 Status = Private->Ip6->GetModeData (Private->Ip6, &Ip6ModeData, NULL, NULL);
1061 if (EFI_ERROR (Status)) {
1062 goto ON_ERROR;
1063 }
1064
1065 Private->Ip6MaxPacketSize = Ip6ModeData.MaxPacketSize;
1066
1067 if (Ip6ModeData.AddressList != NULL) {
1068 FreePool (Ip6ModeData.AddressList);
1069 }
1070
1071 if (Ip6ModeData.GroupTable != NULL) {
1072 FreePool (Ip6ModeData.GroupTable);
1073 }
1074
1075 if (Ip6ModeData.RouteTable != NULL) {
1076 FreePool (Ip6ModeData.RouteTable);
1077 }
1078
1079 if (Ip6ModeData.NeighborCache != NULL) {
1080 FreePool (Ip6ModeData.NeighborCache);
1081 }
1082
1083 if (Ip6ModeData.PrefixTable != NULL) {
1084 FreePool (Ip6ModeData.PrefixTable);
1085 }
1086
1087 if (Ip6ModeData.IcmpTypeList != NULL) {
1088 FreePool (Ip6ModeData.IcmpTypeList);
1089 }
1090
1091 //
1092 // Locate Ip6->Ip6Config and store it for set IPv6 address.
1093 //
1094 Status = gBS->HandleProtocol (
1095 ControllerHandle,
1096 &gEfiIp6ConfigProtocolGuid,
1097 (VOID **) &Private->Ip6Cfg
1098 );
1099 if (EFI_ERROR (Status)) {
1100 goto ON_ERROR;
1101 }
1102
1103 //
1104 // Create a device path node for Ipv6 virtual nic, and append it.
1105 //
1106 ZeroMem (&Ip6Node, sizeof (IPv6_DEVICE_PATH));
1107 Ip6Node.Header.Type = MESSAGING_DEVICE_PATH;
1108 Ip6Node.Header.SubType = MSG_IPv6_DP;
1109 Ip6Node.PrefixLength = IP6_PREFIX_LENGTH;
1110
1111 SetDevicePathNodeLength (&Ip6Node.Header, sizeof (Ip6Node));
1112
1113 Private->Ip6Nic->DevicePath = AppendDevicePathNode (Private->DevicePath, &Ip6Node.Header);
1114
1115 if (Private->Ip6Nic->DevicePath == NULL) {
1116 Status = EFI_OUT_OF_RESOURCES;
1117 goto ON_ERROR;
1118 }
1119
1120 CopyMem (
1121 &Private->Ip6Nic->LoadFile,
1122 &gLoadFileProtocolTemplate,
1123 sizeof (EFI_LOAD_FILE_PROTOCOL)
1124 );
1125
1126 //
1127 // Create a new handle for IPv6 virtual nic,
1128 // and install PxeBaseCode, LoadFile and DevicePath protocols.
1129 //
1130 Status = gBS->InstallMultipleProtocolInterfaces (
1131 &Private->Ip6Nic->Controller,
1132 &gEfiDevicePathProtocolGuid,
1133 Private->Ip6Nic->DevicePath,
1134 &gEfiLoadFileProtocolGuid,
1135 &Private->Ip6Nic->LoadFile,
1136 &gEfiPxeBaseCodeProtocolGuid,
1137 &Private->PxeBc,
1138 NULL
1139 );
1140 if (EFI_ERROR (Status)) {
1141 goto ON_ERROR;
1142 }
1143
1144 if (Private->Snp != NULL) {
1145 //
1146 // Install SNP protocol on purpose is for some OS loader backward
1147 // compatibility consideration.
1148 //
1149 Status = gBS->InstallProtocolInterface (
1150 &Private->Ip6Nic->Controller,
1151 &gEfiSimpleNetworkProtocolGuid,
1152 EFI_NATIVE_INTERFACE,
1153 Private->Snp
1154 );
1155 if (EFI_ERROR (Status)) {
1156 goto ON_ERROR;
1157 }
1158
1159 //
1160 // Open SNP on the child handle BY_DRIVER|EXCLUSIVE. It will prevent any additionally
1161 // layering to perform the experiment.
1162 //
1163 Status = gBS->OpenProtocol (
1164 Private->Ip6Nic->Controller,
1165 &gEfiSimpleNetworkProtocolGuid,
1166 (VOID **) &Snp,
1167 This->DriverBindingHandle,
1168 Private->Ip6Nic->Controller,
1169 EFI_OPEN_PROTOCOL_BY_DRIVER|EFI_OPEN_PROTOCOL_EXCLUSIVE
1170 );
1171 if (EFI_ERROR (Status)) {
1172 goto ON_ERROR;
1173 }
1174 }
1175
1176 //
1177 // Open PxeBaseCodePrivate protocol by child to setup a parent-child relationship between
1178 // real NIC handle and the virtual IPv6 NIC handle.
1179 //
1180 Status = gBS->OpenProtocol (
1181 ControllerHandle,
1182 &gEfiCallerIdGuid,
1183 (VOID **) &Id,
1184 This->DriverBindingHandle,
1185 Private->Ip6Nic->Controller,
1186 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1187 );
1188 if (EFI_ERROR (Status)) {
1189 goto ON_ERROR;
1190 }
1191
1192 //
1193 // Set IPv6 avaiable flag and set default configure data for
1194 // Udp6Read and Ip6 instance.
1195 //
1196 Status = PxeBcCheckIpv6Support (ControllerHandle, Private, &Private->Mode.Ipv6Available);
1197 if (EFI_ERROR (Status)) {
1198 //
1199 // Fail to get the data whether UNDI supports IPv6. Set default value.
1200 //
1201 Private->Mode.Ipv6Available = TRUE;
1202 }
1203
1204 if (!Private->Mode.Ipv6Available) {
1205 goto ON_ERROR;
1206 }
1207
1208 Udp6CfgData = &Private->Udp6CfgData;
1209 Ip6CfgData = &Private->Ip6CfgData;
1210
1211 Udp6CfgData->AcceptAnyPort = TRUE;
1212 Udp6CfgData->AllowDuplicatePort = TRUE;
1213 Udp6CfgData->HopLimit = PXEBC_DEFAULT_HOPLIMIT;
1214 Udp6CfgData->ReceiveTimeout = PXEBC_DEFAULT_LIFETIME;
1215 Udp6CfgData->TransmitTimeout = PXEBC_DEFAULT_LIFETIME;
1216
1217 Ip6CfgData->AcceptIcmpErrors = TRUE;
1218 Ip6CfgData->DefaultProtocol = IP6_ICMP;
1219 Ip6CfgData->HopLimit = PXEBC_DEFAULT_HOPLIMIT;
1220 Ip6CfgData->ReceiveTimeout = PXEBC_DEFAULT_LIFETIME;
1221 Ip6CfgData->TransmitTimeout = PXEBC_DEFAULT_LIFETIME;
1222
1223 return EFI_SUCCESS;
1224
1225 ON_ERROR:
1226 PxeBcDestroyIp6Children (This, Private);
1227 return Status;
1228 }
1229
1230
1231 /**
1232 The entry point for UefiPxeBc driver that installs the driver
1233 binding and component name protocol on its image.
1234
1235 @param[in] ImageHandle The Image handle of the driver.
1236 @param[in] SystemTable The system table.
1237
1238 @return EFI_SUCCESS
1239 @return Others
1240
1241 **/
1242 EFI_STATUS
1243 EFIAPI
1244 PxeBcDriverEntryPoint (
1245 IN EFI_HANDLE ImageHandle,
1246 IN EFI_SYSTEM_TABLE *SystemTable
1247 )
1248 {
1249 EFI_STATUS Status;
1250
1251 Status = EfiLibInstallDriverBindingComponentName2 (
1252 ImageHandle,
1253 SystemTable,
1254 &gPxeBcIp4DriverBinding,
1255 ImageHandle,
1256 &gPxeBcComponentName,
1257 &gPxeBcComponentName2
1258 );
1259 if (EFI_ERROR (Status)) {
1260 return Status;
1261 }
1262
1263 Status = EfiLibInstallDriverBindingComponentName2 (
1264 ImageHandle,
1265 SystemTable,
1266 &gPxeBcIp6DriverBinding,
1267 NULL,
1268 &gPxeBcComponentName,
1269 &gPxeBcComponentName2
1270 );
1271 if (EFI_ERROR (Status)) {
1272 EfiLibUninstallDriverBindingComponentName2 (
1273 &gPxeBcIp4DriverBinding,
1274 &gPxeBcComponentName,
1275 &gPxeBcComponentName2
1276 );
1277 }
1278
1279 return Status;
1280 }
1281
1282 /**
1283 Test to see if this driver supports ControllerHandle. This is the worker function for
1284 PxeBcIp4(6)DriverBindingSupported.
1285
1286 @param[in] This The pointer to the driver binding protocol.
1287 @param[in] ControllerHandle The handle of device to be tested.
1288 @param[in] RemainingDevicePath Optional parameter used to pick a specific child
1289 device to be started.
1290 @param[in] IpVersion IP_VERSION_4 or IP_VERSION_6.
1291
1292 @retval EFI_SUCCESS This driver supports this device.
1293 @retval EFI_UNSUPPORTED This driver does not support this device.
1294
1295 **/
1296 EFI_STATUS
1297 EFIAPI
1298 PxeBcSupported (
1299 IN EFI_DRIVER_BINDING_PROTOCOL *This,
1300 IN EFI_HANDLE ControllerHandle,
1301 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL,
1302 IN UINT8 IpVersion
1303 )
1304 {
1305 EFI_STATUS Status;
1306 EFI_GUID *DhcpServiceBindingGuid;
1307 EFI_GUID *MtftpServiceBindingGuid;
1308
1309 if (IpVersion == IP_VERSION_4) {
1310 DhcpServiceBindingGuid = &gEfiDhcp4ServiceBindingProtocolGuid;
1311 MtftpServiceBindingGuid = &gEfiMtftp4ServiceBindingProtocolGuid;
1312 } else {
1313 DhcpServiceBindingGuid = &gEfiDhcp6ServiceBindingProtocolGuid;
1314 MtftpServiceBindingGuid = &gEfiMtftp6ServiceBindingProtocolGuid;
1315 }
1316
1317 //
1318 // Try to open the Mtftp and Dhcp protocol to test whether IP stack is ready.
1319 //
1320 Status = gBS->OpenProtocol (
1321 ControllerHandle,
1322 DhcpServiceBindingGuid,
1323 NULL,
1324 This->DriverBindingHandle,
1325 ControllerHandle,
1326 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
1327 );
1328 if (!EFI_ERROR (Status)) {
1329 Status = gBS->OpenProtocol (
1330 ControllerHandle,
1331 MtftpServiceBindingGuid,
1332 NULL,
1333 This->DriverBindingHandle,
1334 ControllerHandle,
1335 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
1336 );
1337 }
1338
1339 //
1340 // It's unsupported case if IP stack are not ready.
1341 //
1342 if (EFI_ERROR (Status)) {
1343 return EFI_UNSUPPORTED;
1344 }
1345
1346 return EFI_SUCCESS;
1347 }
1348
1349 /**
1350 Start this driver on ControllerHandle. This is the worker function for
1351 PxeBcIp4(6)DriverBindingStart.
1352
1353 @param[in] This The pointer to the driver binding protocol.
1354 @param[in] ControllerHandle The handle of device to be started.
1355 @param[in] RemainingDevicePath Optional parameter used to pick a specific child
1356 device to be started.
1357 @param[in] IpVersion IP_VERSION_4 or IP_VERSION_6.
1358
1359
1360 @retval EFI_SUCCESS This driver is installed to ControllerHandle.
1361 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle.
1362 @retval other This driver does not support this device.
1363
1364 **/
1365 EFI_STATUS
1366 EFIAPI
1367 PxeBcStart (
1368 IN EFI_DRIVER_BINDING_PROTOCOL *This,
1369 IN EFI_HANDLE ControllerHandle,
1370 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL,
1371 IN UINT8 IpVersion
1372 )
1373 {
1374 PXEBC_PRIVATE_DATA *Private;
1375 EFI_STATUS Status;
1376 PXEBC_PRIVATE_PROTOCOL *Id;
1377 BOOLEAN FirstStart;
1378
1379 FirstStart = FALSE;
1380 Status = gBS->OpenProtocol (
1381 ControllerHandle,
1382 &gEfiCallerIdGuid,
1383 (VOID **) &Id,
1384 This->DriverBindingHandle,
1385 ControllerHandle,
1386 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1387 );
1388 if (!EFI_ERROR (Status)) {
1389 //
1390 // Skip the initialization if the driver has been started already.
1391 //
1392 Private = PXEBC_PRIVATE_DATA_FROM_ID (Id);
1393 } else {
1394 FirstStart = TRUE;
1395 //
1396 // If the driver has not been started yet, it should do initialization.
1397 //
1398 Private = AllocateZeroPool (sizeof (PXEBC_PRIVATE_DATA));
1399 if (Private == NULL) {
1400 return EFI_OUT_OF_RESOURCES;
1401 }
1402
1403 CopyMem (
1404 &Private->PxeBc,
1405 &gPxeBcProtocolTemplate,
1406 sizeof (EFI_PXE_BASE_CODE_PROTOCOL)
1407 );
1408
1409 Private->Signature = PXEBC_PRIVATE_DATA_SIGNATURE;
1410 Private->Controller = ControllerHandle;
1411 Private->Image = This->ImageHandle;
1412 Private->PxeBc.Mode = &Private->Mode;
1413 Private->Mode.Ipv6Supported = TRUE;
1414 Private->Mode.AutoArp = TRUE;
1415 Private->Mode.TTL = DEFAULT_TTL;
1416 Private->Mode.ToS = DEFAULT_ToS;
1417
1418 //
1419 // Open device path to prepare for appending virtual NIC node.
1420 //
1421 Status = gBS->OpenProtocol (
1422 ControllerHandle,
1423 &gEfiDevicePathProtocolGuid,
1424 (VOID **) &Private->DevicePath,
1425 This->DriverBindingHandle,
1426 ControllerHandle,
1427 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1428 );
1429
1430 if (EFI_ERROR (Status)) {
1431 goto ON_ERROR;
1432 }
1433
1434 //
1435 // Get the NII interface if it exists, it's not required.
1436 //
1437 Status = gBS->OpenProtocol (
1438 ControllerHandle,
1439 &gEfiNetworkInterfaceIdentifierProtocolGuid_31,
1440 (VOID **) &Private->Nii,
1441 This->DriverBindingHandle,
1442 ControllerHandle,
1443 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1444 );
1445 if (EFI_ERROR (Status)) {
1446 Private->Nii = NULL;
1447 }
1448
1449 //
1450 // Install PxeBaseCodePrivate protocol onto the real NIC handler.
1451 // PxeBaseCodePrivate protocol is only used to keep the relationship between
1452 // NIC handle and virtual child handles.
1453 // gEfiCallerIdGuid will be used as its protocol guid.
1454 //
1455 Status = gBS->InstallProtocolInterface (
1456 &ControllerHandle,
1457 &gEfiCallerIdGuid,
1458 EFI_NATIVE_INTERFACE,
1459 &Private->Id
1460 );
1461 if (EFI_ERROR (Status)) {
1462 goto ON_ERROR;
1463 }
1464
1465 //
1466 // Try to locate SNP protocol.
1467 //
1468 NetLibGetSnpHandle(ControllerHandle, &Private->Snp);
1469 }
1470
1471 if (IpVersion == IP_VERSION_4) {
1472 //
1473 // Try to create virtual NIC handle for IPv4.
1474 //
1475 Status = PxeBcCreateIp4Children (This, ControllerHandle, Private);
1476 } else {
1477 //
1478 // Try to create virtual NIC handle for IPv6.
1479 //
1480 Status = PxeBcCreateIp6Children (This, ControllerHandle, Private);
1481 }
1482 if (EFI_ERROR (Status)) {
1483 //
1484 // Failed to start PXE driver if IPv4 and IPv6 stack are both not available.
1485 //
1486 Status = EFI_DEVICE_ERROR;
1487 goto ON_ERROR;
1488 }
1489
1490 return EFI_SUCCESS;
1491
1492 ON_ERROR:
1493 if (FirstStart) {
1494 gBS->UninstallProtocolInterface (
1495 ControllerHandle,
1496 &gEfiCallerIdGuid,
1497 &Private->Id
1498 );
1499 }
1500
1501 if (IpVersion == IP_VERSION_4) {
1502 PxeBcDestroyIp4Children (This, Private);
1503 } else {
1504 PxeBcDestroyIp6Children (This, Private);
1505 }
1506
1507 if (FirstStart && Private != NULL) {
1508 FreePool (Private);
1509 }
1510
1511 return Status;
1512 }
1513
1514
1515 /**
1516 Stop this driver on ControllerHandle. This is the worker function for
1517 PxeBcIp4(6)DriverBindingStop.
1518
1519 @param[in] This Protocol instance pointer.
1520 @param[in] ControllerHandle Handle of device to stop driver on.
1521 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
1522 children is zero stop the entire bus driver.
1523 @param[in] ChildHandleBuffer List of Child Handles to Stop.
1524 @param[in] IpVersion IP_VERSION_4 or IP_VERSION_6.
1525
1526 @retval EFI_SUCCESS This driver was removed ControllerHandle.
1527 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1528 @retval Others This driver was not removed from this device
1529
1530 **/
1531 EFI_STATUS
1532 EFIAPI
1533 PxeBcStop (
1534 IN EFI_DRIVER_BINDING_PROTOCOL *This,
1535 IN EFI_HANDLE ControllerHandle,
1536 IN UINTN NumberOfChildren,
1537 IN EFI_HANDLE *ChildHandleBuffer,
1538 IN UINT8 IpVersion
1539 )
1540 {
1541 PXEBC_PRIVATE_DATA *Private;
1542 PXEBC_VIRTUAL_NIC *VirtualNic;
1543 EFI_LOAD_FILE_PROTOCOL *LoadFile;
1544 EFI_STATUS Status;
1545 EFI_HANDLE NicHandle;
1546 PXEBC_PRIVATE_PROTOCOL *Id;
1547
1548 Private = NULL;
1549 NicHandle = NULL;
1550 VirtualNic = NULL;
1551 LoadFile = NULL;
1552 Id = NULL;
1553
1554 Status = gBS->OpenProtocol (
1555 ControllerHandle,
1556 &gEfiLoadFileProtocolGuid,
1557 (VOID **) &LoadFile,
1558 This->DriverBindingHandle,
1559 ControllerHandle,
1560 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1561 );
1562 if (EFI_ERROR (Status)) {
1563 //
1564 // Get the Nic handle by any pass-over service child handle.
1565 //
1566 if (IpVersion == IP_VERSION_4) {
1567 NicHandle = PxeBcGetNicByIp4Children (ControllerHandle);
1568 } else {
1569 NicHandle = PxeBcGetNicByIp6Children (ControllerHandle);
1570 }
1571 if (NicHandle == NULL) {
1572 return EFI_SUCCESS;
1573 }
1574
1575 //
1576 // Try to retrieve the private data by PxeBcPrivate protocol.
1577 //
1578 Status = gBS->OpenProtocol (
1579 NicHandle,
1580 &gEfiCallerIdGuid,
1581 (VOID **) &Id,
1582 This->DriverBindingHandle,
1583 ControllerHandle,
1584 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1585 );
1586 if (EFI_ERROR (Status)) {
1587 return Status;
1588 }
1589 Private = PXEBC_PRIVATE_DATA_FROM_ID (Id);
1590
1591 } else {
1592 //
1593 // It's a virtual handle with LoadFileProtocol.
1594 //
1595 Status = gBS->OpenProtocol (
1596 ControllerHandle,
1597 &gEfiLoadFileProtocolGuid,
1598 (VOID **) &LoadFile,
1599 This->DriverBindingHandle,
1600 ControllerHandle,
1601 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1602 );
1603 if (EFI_ERROR (Status)) {
1604 return Status;
1605 }
1606
1607 VirtualNic = PXEBC_VIRTUAL_NIC_FROM_LOADFILE (LoadFile);
1608 Private = VirtualNic->Private;
1609 NicHandle = Private->Controller;
1610 }
1611
1612 //
1613 // Stop functionality of PXE Base Code protocol
1614 //
1615 Status = Private->PxeBc.Stop (&Private->PxeBc);
1616 if (Status != EFI_SUCCESS && Status != EFI_NOT_STARTED) {
1617 return Status;
1618 }
1619
1620
1621 if (Private->Ip4Nic != NULL && IpVersion == IP_VERSION_4) {
1622 PxeBcDestroyIp4Children (This, Private);
1623 }
1624
1625 if (Private->Ip6Nic != NULL && IpVersion == IP_VERSION_6) {
1626 PxeBcDestroyIp6Children (This, Private);
1627 }
1628
1629 if (Private->Ip4Nic == NULL && Private->Ip6Nic == NULL) {
1630 gBS->UninstallProtocolInterface (
1631 NicHandle,
1632 &gEfiCallerIdGuid,
1633 &Private->Id
1634 );
1635 FreePool (Private);
1636 }
1637
1638 return EFI_SUCCESS;
1639 }
1640
1641 /**
1642 Test to see if this driver supports ControllerHandle. This service
1643 is called by the EFI boot service ConnectController(). In
1644 order to make drivers as small as possible, there are a few calling
1645 restrictions for this service. ConnectController() must
1646 follow these calling restrictions. If any other agent wishes to call
1647 Supported() it must also follow these calling restrictions.
1648
1649 @param[in] This The pointer to the driver binding protocol.
1650 @param[in] ControllerHandle The handle of device to be tested.
1651 @param[in] RemainingDevicePath Optional parameter used to pick a specific child
1652 device to be started.
1653
1654 @retval EFI_SUCCESS This driver supports this device.
1655 @retval EFI_UNSUPPORTED This driver does not support this device.
1656
1657 **/
1658 EFI_STATUS
1659 EFIAPI
1660 PxeBcIp4DriverBindingSupported (
1661 IN EFI_DRIVER_BINDING_PROTOCOL *This,
1662 IN EFI_HANDLE ControllerHandle,
1663 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
1664 )
1665 {
1666 return PxeBcSupported (
1667 This,
1668 ControllerHandle,
1669 RemainingDevicePath,
1670 IP_VERSION_4
1671 );
1672 }
1673
1674 /**
1675 Start this driver on ControllerHandle. This service is called by the
1676 EFI boot service ConnectController(). In order to make
1677 drivers as small as possible, there are a few calling restrictions for
1678 this service. ConnectController() must follow these
1679 calling restrictions. If any other agent wishes to call Start() it
1680 must also follow these calling restrictions.
1681
1682 @param[in] This The pointer to the driver binding protocol.
1683 @param[in] ControllerHandle The handle of device to be started.
1684 @param[in] RemainingDevicePath Optional parameter used to pick a specific child
1685 device to be started.
1686
1687 @retval EFI_SUCCESS This driver is installed to ControllerHandle.
1688 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle.
1689 @retval other This driver does not support this device.
1690
1691 **/
1692 EFI_STATUS
1693 EFIAPI
1694 PxeBcIp4DriverBindingStart (
1695 IN EFI_DRIVER_BINDING_PROTOCOL *This,
1696 IN EFI_HANDLE ControllerHandle,
1697 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
1698 )
1699 {
1700 return PxeBcStart (
1701 This,
1702 ControllerHandle,
1703 RemainingDevicePath,
1704 IP_VERSION_4
1705 );
1706 }
1707
1708 /**
1709 Stop this driver on ControllerHandle. This service is called by the
1710 EFI boot service DisconnectController(). In order to
1711 make drivers as small as possible, there are a few calling
1712 restrictions for this service. DisconnectController()
1713 must follow these calling restrictions. If any other agent wishes
1714 to call Stop() it must also follow these calling restrictions.
1715
1716 @param[in] This Protocol instance pointer.
1717 @param[in] ControllerHandle Handle of device to stop driver on
1718 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
1719 children is zero stop the entire bus driver.
1720 @param[in] ChildHandleBuffer List of Child Handles to Stop.
1721
1722 @retval EFI_SUCCESS This driver is removed ControllerHandle
1723 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1724 @retval Others This driver was not removed from this device.
1725
1726 **/
1727 EFI_STATUS
1728 EFIAPI
1729 PxeBcIp4DriverBindingStop (
1730 IN EFI_DRIVER_BINDING_PROTOCOL *This,
1731 IN EFI_HANDLE ControllerHandle,
1732 IN UINTN NumberOfChildren,
1733 IN EFI_HANDLE *ChildHandleBuffer
1734 )
1735 {
1736 return PxeBcStop (
1737 This,
1738 ControllerHandle,
1739 NumberOfChildren,
1740 ChildHandleBuffer,
1741 IP_VERSION_4
1742 );
1743 }
1744
1745 /**
1746 Test to see if this driver supports ControllerHandle. This service
1747 is called by the EFI boot service ConnectController(). In
1748 order to make drivers as small as possible, there are a few calling
1749 restrictions for this service. ConnectController() must
1750 follow these calling restrictions. If any other agent wishes to call
1751 Supported() it must also follow these calling restrictions.
1752
1753 @param[in] This The pointer to the driver binding protocol.
1754 @param[in] ControllerHandle The handle of device to be tested.
1755 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
1756 device to be started.
1757
1758 @retval EFI_SUCCESS This driver supports this device.
1759 @retval EFI_UNSUPPORTED This driver does not support this device.
1760
1761 **/
1762 EFI_STATUS
1763 EFIAPI
1764 PxeBcIp6DriverBindingSupported (
1765 IN EFI_DRIVER_BINDING_PROTOCOL *This,
1766 IN EFI_HANDLE ControllerHandle,
1767 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
1768 )
1769 {
1770 return PxeBcSupported (
1771 This,
1772 ControllerHandle,
1773 RemainingDevicePath,
1774 IP_VERSION_6
1775 );
1776 }
1777
1778 /**
1779 Start this driver on ControllerHandle. This service is called by the
1780 EFI boot service ConnectController(). In order to make
1781 drivers as small as possible, there are a few calling restrictions for
1782 this service. ConnectController() must follow these
1783 calling restrictions. If any other agent wishes to call Start() it
1784 must also follow these calling restrictions.
1785
1786 @param[in] This The pointer to the driver binding protocol.
1787 @param[in] ControllerHandle The handle of device to be started.
1788 @param[in] RemainingDevicePath Optional parameter used to pick a specific child
1789 device to be started.
1790
1791 @retval EFI_SUCCESS This driver is installed to ControllerHandle.
1792 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle.
1793 @retval other This driver does not support this device.
1794
1795 **/
1796 EFI_STATUS
1797 EFIAPI
1798 PxeBcIp6DriverBindingStart (
1799 IN EFI_DRIVER_BINDING_PROTOCOL *This,
1800 IN EFI_HANDLE ControllerHandle,
1801 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
1802 )
1803 {
1804 return PxeBcStart (
1805 This,
1806 ControllerHandle,
1807 RemainingDevicePath,
1808 IP_VERSION_6
1809 );
1810 }
1811
1812 /**
1813 Stop this driver on ControllerHandle. This service is called by the
1814 EFI boot service DisconnectController(). In order to
1815 make drivers as small as possible, there are a few calling
1816 restrictions for this service. DisconnectController()
1817 must follow these calling restrictions. If any other agent wishes
1818 to call Stop() it must also follow these calling restrictions.
1819
1820 @param[in] This Protocol instance pointer.
1821 @param[in] ControllerHandle Handle of device to stop driver on
1822 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
1823 children is zero stop the entire bus driver.
1824 @param[in] ChildHandleBuffer List of Child Handles to Stop.
1825
1826 @retval EFI_SUCCESS This driver is removed ControllerHandle
1827 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1828 @retval Others This driver was not removed from this device.
1829
1830 **/
1831 EFI_STATUS
1832 EFIAPI
1833 PxeBcIp6DriverBindingStop (
1834 IN EFI_DRIVER_BINDING_PROTOCOL *This,
1835 IN EFI_HANDLE ControllerHandle,
1836 IN UINTN NumberOfChildren,
1837 IN EFI_HANDLE *ChildHandleBuffer
1838 )
1839 {
1840 return PxeBcStop (
1841 This,
1842 ControllerHandle,
1843 NumberOfChildren,
1844 ChildHandleBuffer,
1845 IP_VERSION_6
1846 );
1847 }