2 The driver binding for IP4 CONFIG protocol.
4 Copyright (c) 2006 - 2008, Intel Corporation.<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at<BR>
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "Ip4Config.h"
18 EFI_DRIVER_BINDING_PROTOCOL gIp4ConfigDriverBinding
= {
19 Ip4ConfigDriverBindingSupported
,
20 Ip4ConfigDriverBindingStart
,
21 Ip4ConfigDriverBindingStop
,
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
);
64 The entry point for IP4 config driver which install the driver
65 binding and component name protocol on its image.
67 @param ImageHandle The image handle of the driver.
68 @param SystemTable The system table.
70 @retval EFI_SUCCES All the related protocols are installed on the driver.
71 @retval Others Failed to install protocols.
75 Ip4ConfigDriverEntryPoint (
76 IN EFI_HANDLE ImageHandle
,
77 IN EFI_SYSTEM_TABLE
*SystemTable
80 return EfiLibInstallDriverBindingComponentName2 (
83 &gIp4ConfigDriverBinding
,
85 &gIp4ConfigComponentName
,
86 &gIp4ConfigComponentName2
92 Test to see if this driver supports ControllerHandle.
94 @param This Protocol instance pointer.
95 @param ControllerHandle Handle of device to test
96 @param RemainingDevicePath Optional parameter use to pick a specific child
99 @retval EFI_SUCCES This driver supports this device
100 @retval EFI_ALREADY_STARTED This driver is already running on this device
101 @retval other This driver does not support this device
106 Ip4ConfigDriverBindingSupported (
107 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
108 IN EFI_HANDLE ControllerHandle
,
109 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
114 Status
= gBS
->OpenProtocol (
116 &gEfiManagedNetworkServiceBindingProtocolGuid
,
118 This
->DriverBindingHandle
,
120 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
128 Start this driver on ControllerHandle.
130 @param This Protocol instance pointer.
131 @param ControllerHandle Handle of device to bind driver to
132 @param RemainingDevicePath Optional parameter use to pick a specific child
135 @retval EFI_SUCCES This driver is added to ControllerHandle
136 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
137 @retval other This driver does not support this device
142 Ip4ConfigDriverBindingStart (
143 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
144 IN EFI_HANDLE ControllerHandle
,
145 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
148 EFI_IP4_CONFIG_PROTOCOL
*Ip4Config
;
149 EFI_MANAGED_NETWORK_PROTOCOL
*Mnp
;
150 EFI_HANDLE MnpHandle
;
151 IP4_CONFIG_INSTANCE
*Instance
;
152 EFI_SIMPLE_NETWORK_MODE SnpMode
;
153 IP4_CONFIG_VARIABLE
*Variable
;
154 NIC_IP4_CONFIG_INFO
*NicConfig
;
155 IP4_CONFIG_VARIABLE
*NewVariable
;
160 // Check for multiple start.
162 Status
= gBS
->OpenProtocol (
164 &gEfiIp4ConfigProtocolGuid
,
165 (VOID
**) &Ip4Config
,
166 This
->DriverBindingHandle
,
168 EFI_OPEN_PROTOCOL_GET_PROTOCOL
171 if (!EFI_ERROR (Status
)) {
172 return EFI_ALREADY_STARTED
;
176 // Create a MNP child
182 Status
= NetLibCreateServiceChild (
184 This
->DriverBindingHandle
,
185 &gEfiManagedNetworkServiceBindingProtocolGuid
,
189 if (EFI_ERROR (Status
)) {
193 Status
= gBS
->OpenProtocol (
195 &gEfiManagedNetworkProtocolGuid
,
197 This
->DriverBindingHandle
,
199 EFI_OPEN_PROTOCOL_BY_DRIVER
202 if (EFI_ERROR (Status
)) {
207 // Allocate an instance then initialize it
209 Instance
= AllocatePool (sizeof (IP4_CONFIG_INSTANCE
));
211 if (Instance
== NULL
) {
212 Status
= EFI_OUT_OF_RESOURCES
;
216 Instance
->Signature
= IP4_CONFIG_INSTANCE_SIGNATURE
;
217 Instance
->Controller
= ControllerHandle
;
218 Instance
->Image
= This
->DriverBindingHandle
;
220 CopyMem (&Instance
->Ip4ConfigProtocol
, &mIp4ConfigProtocolTemplate
, sizeof (mIp4ConfigProtocolTemplate
));
221 CopyMem (&Instance
->NicIp4Protocol
, &mNicIp4ConfigProtocolTemplate
, sizeof (mNicIp4ConfigProtocolTemplate
));
223 Instance
->State
= IP4_CONFIG_STATE_IDLE
;
225 Instance
->MnpHandle
= MnpHandle
;
227 Instance
->DoneEvent
= NULL
;
228 Instance
->ReconfigEvent
= NULL
;
229 Instance
->Result
= EFI_NOT_READY
;
230 Instance
->NicConfig
= NULL
;
232 Instance
->Dhcp4
= NULL
;
233 Instance
->Dhcp4Handle
= NULL
;
234 Instance
->Dhcp4Event
= NULL
;
236 Status
= Mnp
->GetModeData (Mnp
, NULL
, &SnpMode
);
238 if (EFI_ERROR (Status
) && (Status
!= EFI_NOT_STARTED
)) {
242 Instance
->NicAddr
.Type
= (UINT16
) SnpMode
.IfType
;
243 Instance
->NicAddr
.Len
= (UINT8
) SnpMode
.HwAddressSize
;
244 CopyMem (&Instance
->NicAddr
.MacAddr
, &SnpMode
.CurrentAddress
, sizeof (Instance
->NicAddr
.MacAddr
));
247 // Add it to the global list, and compose the name
249 for (Index
= 0; Index
< MAX_IP4_CONFIG_IN_VARIABLE
; Index
++) {
250 if (mIp4ConfigNicList
[Index
] == NULL
) {
251 mIp4ConfigNicList
[Index
] = Instance
;
252 Instance
->NicIndex
= Index
;
254 if (Instance
->NicAddr
.Type
== NET_IFTYPE_ETHERNET
) {
255 Instance
->NicName
[0] = 'e';
256 Instance
->NicName
[1] = 't';
257 Instance
->NicName
[2] = 'h';
258 Instance
->NicName
[3] = (UINT16
) ('0' + Index
);
259 Instance
->NicName
[4] = 0;
261 Instance
->NicName
[0] = 'u';
262 Instance
->NicName
[1] = 'n';
263 Instance
->NicName
[2] = 'k';
264 Instance
->NicName
[3] = (UINT16
) ('0' + Index
);
265 Instance
->NicName
[4] = 0;
272 if (Index
== MAX_IP4_CONFIG_IN_VARIABLE
) {
273 Status
= EFI_OUT_OF_RESOURCES
;
278 // Install the IP4_CONFIG and NIC_IP4CONFIG protocols
280 Status
= gBS
->InstallMultipleProtocolInterfaces (
282 &gEfiIp4ConfigProtocolGuid
,
283 &Instance
->Ip4ConfigProtocol
,
284 &gEfiNicIp4ConfigProtocolGuid
,
285 &Instance
->NicIp4Protocol
,
289 if (EFI_ERROR (Status
)) {
290 mIp4ConfigNicList
[Index
] = NULL
;
295 // Get the previous configure parameters. If an error happend here,
296 // just ignore it because the driver should be able to operate.
298 Variable
= Ip4ConfigReadVariable ();
300 if (Variable
== NULL
) {
304 NicConfig
= Ip4ConfigFindNicVariable (Variable
, &Instance
->NicAddr
);
306 if (NicConfig
== NULL
) {
311 // Don't modify the permant static configuration
313 if (NicConfig
->Perment
&& (NicConfig
->Source
== IP4_CONFIG_SOURCE_STATIC
)) {
318 // Delete the non-permant configuration and remove the previous
319 // acquired DHCP parameters. Only doing DHCP itself is permant
323 if (!NicConfig
->Perment
) {
324 NewVariable
= Ip4ConfigModifyVariable (Variable
, &Instance
->NicAddr
, NULL
);
326 } else if (NicConfig
->Source
== IP4_CONFIG_SOURCE_DHCP
) {
327 ZeroMem (&NicConfig
->Ip4Info
, sizeof (EFI_IP4_IPCONFIG_DATA
));
328 NewVariable
= Ip4ConfigModifyVariable (Variable
, &Instance
->NicAddr
, NicConfig
);
332 Ip4ConfigWriteVariable (NewVariable
);
334 if (NewVariable
!= NULL
) {
335 gBS
->FreePool (NewVariable
);
339 gBS
->FreePool (Variable
);
341 if (NicConfig
!= NULL
) {
342 gBS
->FreePool (NicConfig
);
348 if (Instance
!= NULL
) {
349 gBS
->FreePool (Instance
);
355 &gEfiManagedNetworkProtocolGuid
,
356 This
->DriverBindingHandle
,
361 NetLibDestroyServiceChild (
363 This
->DriverBindingHandle
,
364 &gEfiManagedNetworkProtocolGuid
,
373 Stop this driver on ControllerHandle.
375 @param This Protocol instance pointer.
376 @param ControllerHandle Handle of device to stop driver on
377 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
378 children is zero stop the entire bus driver.
379 @param ChildHandleBuffer List of Child Handles to Stop.
381 @retval EFI_SUCCES This driver is removed ControllerHandle
382 @retval other This driver was not removed from this device
387 Ip4ConfigDriverBindingStop (
388 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
389 IN EFI_HANDLE ControllerHandle
,
390 IN UINTN NumberOfChildren
,
391 IN EFI_HANDLE
*ChildHandleBuffer
394 IP4_CONFIG_INSTANCE
*Instance
;
395 EFI_IP4_CONFIG_PROTOCOL
*Ip4Config
;
396 EFI_HANDLE NicHandle
;
400 // IP4_CONFIG instance opens an MNP child. It may also create and open
401 // a DHCP child. If this is the DHCP handle, stop the DHCP process. If
402 // it is the MNP child, stop the whole driver.
405 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiDhcp4ProtocolGuid
);
407 if (NicHandle
!= NULL
) {
409 // Get our context back then clean the DHCP up. Notify the user if necessary.
411 Status
= gBS
->OpenProtocol (
413 &gEfiIp4ConfigProtocolGuid
,
414 (VOID
**) &Ip4Config
,
415 This
->DriverBindingHandle
,
417 EFI_OPEN_PROTOCOL_GET_PROTOCOL
420 if (EFI_ERROR (Status
)) {
424 Instance
= IP4_CONFIG_INSTANCE_FROM_IP4CONFIG (Ip4Config
);
425 ASSERT (ControllerHandle
== Instance
->Dhcp4Handle
);
427 Ip4ConfigCleanDhcp4 (Instance
);
429 Instance
->State
= IP4_CONFIG_STATE_CONFIGURED
;
430 Instance
->Result
= EFI_DEVICE_ERROR
;
432 if (Instance
->DoneEvent
!= NULL
) {
433 gBS
->SignalEvent (Instance
->DoneEvent
);
440 // This is a MNP handle, stop the whole driver
442 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiManagedNetworkProtocolGuid
);
444 if (NicHandle
== NULL
) {
449 // Get our context back.
451 Status
= gBS
->OpenProtocol (
453 &gEfiIp4ConfigProtocolGuid
,
454 (VOID
**) &Ip4Config
,
455 This
->DriverBindingHandle
,
457 EFI_OPEN_PROTOCOL_GET_PROTOCOL
460 if (EFI_ERROR (Status
)) {
464 Instance
= IP4_CONFIG_INSTANCE_FROM_IP4CONFIG (Ip4Config
);
467 // Unload the protocols first to inform the top drivers
469 Status
= gBS
->UninstallMultipleProtocolInterfaces (
471 &gEfiIp4ConfigProtocolGuid
,
472 &Instance
->Ip4ConfigProtocol
,
473 &gEfiNicIp4ConfigProtocolGuid
,
474 &Instance
->NicIp4Protocol
,
478 if (EFI_ERROR (Status
)) {
483 // Release all the resources
485 if (Instance
->MnpHandle
!= NULL
) {
488 &gEfiManagedNetworkProtocolGuid
,
489 This
->DriverBindingHandle
,
493 NetLibDestroyServiceChild (
496 &gEfiManagedNetworkServiceBindingProtocolGuid
,
500 Instance
->Mnp
= NULL
;
501 Instance
->MnpHandle
= NULL
;
504 Ip4ConfigCleanConfig (Instance
);
505 mIp4ConfigNicList
[Instance
->NicIndex
] = NULL
;
506 gBS
->FreePool (Instance
);