3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 The driver binding for IP4 CONFIG protocol.
24 #include "Ip4Config.h"
28 Stop all the auto configuration when the IP4 configure driver is
31 @param ImageHandle The driver that is being unloaded
33 @retval EFI_SUCCESS The driver has been ready for unload.
39 IN EFI_HANDLE ImageHandle
45 // Stop all the IP4_CONFIG instances
47 for (Index
= 0; Index
< MAX_IP4_CONFIG_IN_VARIABLE
; Index
++) {
48 if (mIp4ConfigNicList
[Index
] == NULL
) {
52 gIp4ConfigDriverBinding
.Stop (
53 &gIp4ConfigDriverBinding
,
54 mIp4ConfigNicList
[Index
]->MnpHandle
,
60 return NetLibDefaultUnload (ImageHandle
);
65 Ip4ConfigDriverEntryPoint (
66 IN EFI_HANDLE ImageHandle
,
67 IN EFI_SYSTEM_TABLE
*SystemTable
73 The entry point for IP4 config driver which install the driver
74 binding and component name protocol on its image.
78 ImageHandle - The Image handle of the driver
79 SystemTable - The system table
83 EFI_SUCCESS - All the related protocols are installed on the driver
84 Others - Failed to install the protocol
88 return EfiLibInstallDriverBindingComponentName2 (
91 &gIp4ConfigDriverBinding
,
93 &gIp4ConfigComponentName
,
94 &gIp4ConfigComponentName2
100 Test to see if this driver supports ControllerHandle.
102 @param This Protocol instance pointer.
103 @param ControllerHandle Handle of device to test
104 @param RemainingDevicePath Optional parameter use to pick a specific child
107 @retval EFI_SUCCES This driver supports this device
108 @retval EFI_ALREADY_STARTED This driver is already running on this device
109 @retval other This driver does not support this device
114 Ip4ConfigDriverBindingSupported (
115 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
116 IN EFI_HANDLE ControllerHandle
,
117 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
122 Status
= gBS
->OpenProtocol (
124 &gEfiManagedNetworkServiceBindingProtocolGuid
,
126 This
->DriverBindingHandle
,
128 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
136 Start this driver on ControllerHandle.
138 @param This Protocol instance pointer.
139 @param ControllerHandle Handle of device to bind driver to
140 @param RemainingDevicePath Optional parameter use to pick a specific child
143 @retval EFI_SUCCES This driver is added to ControllerHandle
144 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
145 @retval other This driver does not support this device
150 Ip4ConfigDriverBindingStart (
151 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
152 IN EFI_HANDLE ControllerHandle
,
153 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
156 EFI_IP4_CONFIG_PROTOCOL
*Ip4Config
;
157 EFI_MANAGED_NETWORK_PROTOCOL
*Mnp
;
158 EFI_HANDLE MnpHandle
;
159 IP4_CONFIG_INSTANCE
*Instance
;
160 EFI_SIMPLE_NETWORK_MODE SnpMode
;
161 IP4_CONFIG_VARIABLE
*Variable
;
162 NIC_IP4_CONFIG_INFO
*NicConfig
;
163 IP4_CONFIG_VARIABLE
*NewVariable
;
168 // Check for multiple start.
170 Status
= gBS
->OpenProtocol (
172 &gEfiIp4ConfigProtocolGuid
,
173 (VOID
**) &Ip4Config
,
174 This
->DriverBindingHandle
,
176 EFI_OPEN_PROTOCOL_GET_PROTOCOL
179 if (!EFI_ERROR (Status
)) {
180 return EFI_ALREADY_STARTED
;
184 // Create a MNP child
190 Status
= NetLibCreateServiceChild (
192 This
->DriverBindingHandle
,
193 &gEfiManagedNetworkServiceBindingProtocolGuid
,
197 if (EFI_ERROR (Status
)) {
201 Status
= gBS
->OpenProtocol (
203 &gEfiManagedNetworkProtocolGuid
,
205 This
->DriverBindingHandle
,
207 EFI_OPEN_PROTOCOL_BY_DRIVER
210 if (EFI_ERROR (Status
)) {
215 // Allocate an instance then initialize it
217 Instance
= NetAllocatePool (sizeof (IP4_CONFIG_INSTANCE
));
219 if (Instance
== NULL
) {
220 Status
= EFI_OUT_OF_RESOURCES
;
224 Instance
->Signature
= IP4_CONFIG_INSTANCE_SIGNATURE
;
225 Instance
->Controller
= ControllerHandle
;
226 Instance
->Image
= This
->DriverBindingHandle
;
228 Instance
->Ip4ConfigProtocol
= mIp4ConfigProtocolTemplate
;
229 Instance
->NicIp4Protocol
= mNicIp4ConfigProtocolTemplate
;
231 Instance
->State
= IP4_CONFIG_STATE_IDLE
;
233 Instance
->MnpHandle
= MnpHandle
;
235 Instance
->DoneEvent
= NULL
;
236 Instance
->ReconfigEvent
= NULL
;
237 Instance
->Result
= EFI_NOT_READY
;
238 Instance
->NicConfig
= NULL
;
240 Instance
->Dhcp4
= NULL
;
241 Instance
->Dhcp4Handle
= NULL
;
242 Instance
->Dhcp4Event
= NULL
;
244 Status
= Mnp
->GetModeData (Mnp
, NULL
, &SnpMode
);
246 if (EFI_ERROR (Status
) && (Status
!= EFI_NOT_STARTED
)) {
250 Instance
->NicAddr
.Type
= (UINT16
) SnpMode
.IfType
;
251 Instance
->NicAddr
.Len
= (UINT8
) SnpMode
.HwAddressSize
;
252 CopyMem (&Instance
->NicAddr
.MacAddr
, &SnpMode
.CurrentAddress
, sizeof (Instance
->NicAddr
.MacAddr
));
255 // Add it to the global list, and compose the name
257 for (Index
= 0; Index
< MAX_IP4_CONFIG_IN_VARIABLE
; Index
++) {
258 if (mIp4ConfigNicList
[Index
] == NULL
) {
259 mIp4ConfigNicList
[Index
] = Instance
;
260 Instance
->NicIndex
= Index
;
262 if (Instance
->NicAddr
.Type
== NET_IFTYPE_ETHERNET
) {
263 Instance
->NicName
[0] = 'e';
264 Instance
->NicName
[1] = 't';
265 Instance
->NicName
[2] = 'h';
266 Instance
->NicName
[3] = (UINT16
) ('0' + Index
);
267 Instance
->NicName
[4] = 0;
269 Instance
->NicName
[0] = 'u';
270 Instance
->NicName
[1] = 'n';
271 Instance
->NicName
[2] = 'k';
272 Instance
->NicName
[3] = (UINT16
) ('0' + Index
);
273 Instance
->NicName
[4] = 0;
280 if (Index
== MAX_IP4_CONFIG_IN_VARIABLE
) {
281 Status
= EFI_OUT_OF_RESOURCES
;
286 // Install the IP4_CONFIG and NIC_IP4CONFIG protocols
288 Status
= gBS
->InstallMultipleProtocolInterfaces (
290 &gEfiIp4ConfigProtocolGuid
,
291 &Instance
->Ip4ConfigProtocol
,
292 &gEfiNicIp4ConfigProtocolGuid
,
293 &Instance
->NicIp4Protocol
,
297 if (EFI_ERROR (Status
)) {
298 mIp4ConfigNicList
[Index
] = NULL
;
303 // Get the previous configure parameters. If an error happend here,
304 // just ignore it because the driver should be able to operate.
306 Variable
= Ip4ConfigReadVariable ();
308 if (Variable
== NULL
) {
312 NicConfig
= Ip4ConfigFindNicVariable (Variable
, &Instance
->NicAddr
);
314 if (NicConfig
== NULL
) {
319 // Don't modify the permant static configuration
321 if (NicConfig
->Perment
&& (NicConfig
->Source
== IP4_CONFIG_SOURCE_STATIC
)) {
326 // Delete the non-permant configuration and remove the previous
327 // acquired DHCP parameters. Only doing DHCP itself is permant
331 if (!NicConfig
->Perment
) {
332 NewVariable
= Ip4ConfigModifyVariable (Variable
, &Instance
->NicAddr
, NULL
);
334 } else if (NicConfig
->Source
== IP4_CONFIG_SOURCE_DHCP
) {
335 NetZeroMem (&NicConfig
->Ip4Info
, sizeof (EFI_IP4_IPCONFIG_DATA
));
336 NewVariable
= Ip4ConfigModifyVariable (Variable
, &Instance
->NicAddr
, NicConfig
);
340 Ip4ConfigWriteVariable (NewVariable
);
342 if (NewVariable
!= NULL
) {
343 NetFreePool (NewVariable
);
347 NetFreePool (Variable
);
349 if (NicConfig
!= NULL
) {
350 NetFreePool (NicConfig
);
356 if (Instance
!= NULL
) {
357 NetFreePool (Instance
);
363 &gEfiManagedNetworkProtocolGuid
,
364 This
->DriverBindingHandle
,
369 NetLibDestroyServiceChild (
371 This
->DriverBindingHandle
,
372 &gEfiManagedNetworkProtocolGuid
,
381 Stop this driver on ControllerHandle.
383 @param This Protocol instance pointer.
384 @param ControllerHandle Handle of device to stop driver on
385 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
386 children is zero stop the entire bus driver.
387 @param ChildHandleBuffer List of Child Handles to Stop.
389 @retval EFI_SUCCES This driver is removed ControllerHandle
390 @retval other This driver was not removed from this device
395 Ip4ConfigDriverBindingStop (
396 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
397 IN EFI_HANDLE ControllerHandle
,
398 IN UINTN NumberOfChildren
,
399 IN EFI_HANDLE
*ChildHandleBuffer
402 IP4_CONFIG_INSTANCE
*Instance
;
403 EFI_IP4_CONFIG_PROTOCOL
*Ip4Config
;
404 EFI_HANDLE NicHandle
;
408 // IP4_CONFIG instance opens an MNP child. It may also create and open
409 // a DHCP child. If this is the DHCP handle, stop the DHCP process. If
410 // it is the MNP child, stop the whole driver.
413 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiDhcp4ProtocolGuid
);
415 if (NicHandle
!= NULL
) {
417 // Get our context back then clean the DHCP up. Notify the user if necessary.
419 Status
= gBS
->OpenProtocol (
421 &gEfiIp4ConfigProtocolGuid
,
422 (VOID
**) &Ip4Config
,
423 This
->DriverBindingHandle
,
425 EFI_OPEN_PROTOCOL_GET_PROTOCOL
428 if (EFI_ERROR (Status
)) {
432 Instance
= IP4_CONFIG_INSTANCE_FROM_IP4CONFIG (Ip4Config
);
433 ASSERT (ControllerHandle
== Instance
->Dhcp4Handle
);
435 Ip4ConfigCleanDhcp4 (Instance
);
437 Instance
->State
= IP4_CONFIG_STATE_CONFIGURED
;
438 Instance
->Result
= EFI_DEVICE_ERROR
;
440 if (Instance
->DoneEvent
!= NULL
) {
441 gBS
->SignalEvent (Instance
->DoneEvent
);
448 // This is a MNP handle, stop the whole driver
450 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiManagedNetworkProtocolGuid
);
452 if (NicHandle
== NULL
) {
457 // Get our context back.
459 Status
= gBS
->OpenProtocol (
461 &gEfiIp4ConfigProtocolGuid
,
462 (VOID
**) &Ip4Config
,
463 This
->DriverBindingHandle
,
465 EFI_OPEN_PROTOCOL_GET_PROTOCOL
468 if (EFI_ERROR (Status
)) {
472 Instance
= IP4_CONFIG_INSTANCE_FROM_IP4CONFIG (Ip4Config
);
475 // Unload the protocols first to inform the top drivers
477 Status
= gBS
->UninstallMultipleProtocolInterfaces (
479 &gEfiIp4ConfigProtocolGuid
,
480 &Instance
->Ip4ConfigProtocol
,
481 &gEfiNicIp4ConfigProtocolGuid
,
482 &Instance
->NicIp4Protocol
,
486 if (EFI_ERROR (Status
)) {
491 // Release all the resources
493 if (Instance
->MnpHandle
!= NULL
) {
496 &gEfiManagedNetworkProtocolGuid
,
497 This
->DriverBindingHandle
,
501 NetLibDestroyServiceChild (
504 &gEfiManagedNetworkServiceBindingProtocolGuid
,
508 Instance
->Mnp
= NULL
;
509 Instance
->MnpHandle
= NULL
;
512 Ip4ConfigCleanConfig (Instance
);
513 mIp4ConfigNicList
[Instance
->NicIndex
] = NULL
;
514 NetFreePool (Instance
);
519 EFI_DRIVER_BINDING_PROTOCOL gIp4ConfigDriverBinding
= {
520 Ip4ConfigDriverBindingSupported
,
521 Ip4ConfigDriverBindingStart
,
522 Ip4ConfigDriverBindingStop
,