2 The driver binding for IP4 CONFIG protocol.
4 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
5 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"
17 #include "Ip4ConfigNv.h"
18 #include "NicIp4Variable.h"
20 EFI_DRIVER_BINDING_PROTOCOL gIp4ConfigDriverBinding
= {
21 Ip4ConfigDriverBindingSupported
,
22 Ip4ConfigDriverBindingStart
,
23 Ip4ConfigDriverBindingStop
,
30 // The intance of template of IP4 Config private data
32 IP4_CONFIG_INSTANCE mIp4ConfigTemplate
= {
33 IP4_CONFIG_INSTANCE_SIGNATURE
,
36 (EFI_DEVICE_PATH_PROTOCOL
*) NULL
,
48 (EFI_DEVICE_PATH_PROTOCOL
*) NULL
,
64 (EFI_MANAGED_NETWORK_PROTOCOL
*) NULL
,
77 (NIC_IP4_CONFIG_INFO
*) NULL
,
78 (EFI_DHCP4_PROTOCOL
*) NULL
,
84 The entry point for IP4 config driver which install the driver
85 binding and component name protocol on its image.
87 @param ImageHandle The image handle of the driver.
88 @param SystemTable The system table.
90 @retval EFI_SUCCES All the related protocols are installed on the driver.
91 @retval Others Failed to install protocols.
96 Ip4ConfigDriverEntryPoint (
97 IN EFI_HANDLE ImageHandle
,
98 IN EFI_SYSTEM_TABLE
*SystemTable
101 return EfiLibInstallDriverBindingComponentName2 (
104 &gIp4ConfigDriverBinding
,
106 &gIp4ConfigComponentName
,
107 &gIp4ConfigComponentName2
113 Test to see if this driver supports ControllerHandle.
115 @param This Protocol instance pointer.
116 @param ControllerHandle Handle of device to test
117 @param RemainingDevicePath Optional parameter use to pick a specific child
120 @retval EFI_SUCCES This driver supports this device
121 @retval EFI_ALREADY_STARTED This driver is already running on this device
122 @retval other This driver does not support this device
127 Ip4ConfigDriverBindingSupported (
128 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
129 IN EFI_HANDLE ControllerHandle
,
130 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
135 Status
= gBS
->OpenProtocol (
137 &gEfiManagedNetworkServiceBindingProtocolGuid
,
139 This
->DriverBindingHandle
,
141 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
149 Start this driver on ControllerHandle.
151 @param This Protocol instance pointer.
152 @param ControllerHandle Handle of device to bind driver to
153 @param RemainingDevicePath Optional parameter use to pick a specific child
156 @retval EFI_SUCCES This driver is added to ControllerHandle
157 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
158 @retval other This driver does not support this device
163 Ip4ConfigDriverBindingStart (
164 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
165 IN EFI_HANDLE ControllerHandle
,
166 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
169 EFI_IP4_CONFIG_PROTOCOL
*Ip4Config
;
170 EFI_MANAGED_NETWORK_PROTOCOL
*Mnp
;
171 EFI_HANDLE MnpHandle
;
172 IP4_CONFIG_INSTANCE
*Instance
;
173 EFI_SIMPLE_NETWORK_MODE SnpMode
;
174 NIC_IP4_CONFIG_INFO
*NicConfig
;
176 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
178 Status
= gBS
->HandleProtocol (
180 &gEfiDevicePathProtocolGuid
,
181 (VOID
**) &ParentDevicePath
183 if (EFI_ERROR (Status
)) {
188 // Check for multiple start.
190 Status
= gBS
->OpenProtocol (
192 &gEfiIp4ConfigProtocolGuid
,
193 (VOID
**) &Ip4Config
,
194 This
->DriverBindingHandle
,
196 EFI_OPEN_PROTOCOL_GET_PROTOCOL
199 if (!EFI_ERROR (Status
)) {
200 return EFI_ALREADY_STARTED
;
204 // Create a MNP child
210 Status
= NetLibCreateServiceChild (
212 This
->DriverBindingHandle
,
213 &gEfiManagedNetworkServiceBindingProtocolGuid
,
217 if (EFI_ERROR (Status
)) {
221 Status
= gBS
->OpenProtocol (
223 &gEfiManagedNetworkProtocolGuid
,
225 This
->DriverBindingHandle
,
227 EFI_OPEN_PROTOCOL_BY_DRIVER
230 if (EFI_ERROR (Status
)) {
235 // Allocate an instance then initialize it
237 Instance
= AllocateCopyPool (sizeof (IP4_CONFIG_INSTANCE
), &mIp4ConfigTemplate
);
239 if (Instance
== NULL
) {
240 Status
= EFI_OUT_OF_RESOURCES
;
244 Instance
->Controller
= ControllerHandle
;
245 Instance
->Image
= This
->DriverBindingHandle
;
246 Instance
->ParentDevicePath
= ParentDevicePath
;
248 CopyMem (&Instance
->Ip4ConfigProtocol
, &mIp4ConfigProtocolTemplate
, sizeof (mIp4ConfigProtocolTemplate
));
250 Instance
->State
= IP4_CONFIG_STATE_IDLE
;
252 Instance
->MnpHandle
= MnpHandle
;
254 Status
= Mnp
->GetModeData (Mnp
, NULL
, &SnpMode
);
256 if (EFI_ERROR (Status
) && (Status
!= EFI_NOT_STARTED
)) {
260 Instance
->NicAddr
.Type
= (UINT16
) SnpMode
.IfType
;
261 Instance
->NicAddr
.Len
= (UINT8
) SnpMode
.HwAddressSize
;
262 CopyMem (&Instance
->NicAddr
.MacAddr
, &SnpMode
.CurrentAddress
, Instance
->NicAddr
.Len
);
265 // Add it to the global list, and compose the name
267 Status
= NetLibGetMacString (Instance
->Controller
, Instance
->Image
, &Instance
->MacString
);
268 if (EFI_ERROR (Status
)) {
272 Status
= Ip4ConfigDeviceInit (Instance
);
275 // Install the IP4_CONFIG protocols
277 Status
= gBS
->InstallMultipleProtocolInterfaces (
279 &gEfiIp4ConfigProtocolGuid
,
280 &Instance
->Ip4ConfigProtocol
,
284 if (EFI_ERROR (Status
)) {
289 // Get the previous configure parameters. If an error happend here,
290 // just ignore it because the driver should be able to operate.
292 NicConfig
= Ip4ConfigReadVariable (Instance
);
293 if (NicConfig
!= NULL
) {
294 if (NicConfig
->Perment
) {
295 if (NicConfig
->Source
== IP4_CONFIG_SOURCE_STATIC
) {
297 // Don't modify the permanent static configuration.
299 } else if (NicConfig
->Source
== IP4_CONFIG_SOURCE_DHCP
) {
301 // Remove the previous acquired DHCP parameters.
303 ZeroMem (&NicConfig
->Ip4Info
, sizeof (EFI_IP4_IPCONFIG_DATA
));
304 Ip4ConfigWriteVariable (Instance
, NicConfig
);
308 // Delete the non-permanent configuration.
310 Ip4ConfigWriteVariable (Instance
, NULL
);
313 FreePool (NicConfig
);
319 if (Instance
!= NULL
) {
326 &gEfiManagedNetworkProtocolGuid
,
327 This
->DriverBindingHandle
,
332 NetLibDestroyServiceChild (
334 This
->DriverBindingHandle
,
335 &gEfiManagedNetworkServiceBindingProtocolGuid
,
344 Stop this driver on ControllerHandle.
346 @param This Protocol instance pointer.
347 @param ControllerHandle Handle of device to stop driver on
348 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
349 children is zero stop the entire bus driver.
350 @param ChildHandleBuffer List of Child Handles to Stop.
352 @retval EFI_SUCCES This driver is removed ControllerHandle
353 @retval other This driver was not removed from this device
358 Ip4ConfigDriverBindingStop (
359 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
360 IN EFI_HANDLE ControllerHandle
,
361 IN UINTN NumberOfChildren
,
362 IN EFI_HANDLE
*ChildHandleBuffer
365 IP4_CONFIG_INSTANCE
*Instance
;
366 EFI_IP4_CONFIG_PROTOCOL
*Ip4Config
;
367 EFI_HANDLE NicHandle
;
371 // IP4_CONFIG instance opens an MNP child. It may also create and open
372 // a DHCP child. If this is the DHCP handle, stop the DHCP process. If
373 // it is the MNP child, stop the whole driver.
376 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiDhcp4ProtocolGuid
);
378 if (NicHandle
!= NULL
) {
380 // Get our context back then clean the DHCP up. Notify the user if necessary.
382 Status
= gBS
->OpenProtocol (
384 &gEfiIp4ConfigProtocolGuid
,
385 (VOID
**) &Ip4Config
,
386 This
->DriverBindingHandle
,
388 EFI_OPEN_PROTOCOL_GET_PROTOCOL
391 if (EFI_ERROR (Status
)) {
395 Instance
= IP4_CONFIG_INSTANCE_FROM_IP4CONFIG (Ip4Config
);
396 ASSERT (ControllerHandle
== Instance
->Dhcp4Handle
);
398 Ip4ConfigCleanDhcp4 (Instance
);
400 Instance
->State
= IP4_CONFIG_STATE_CONFIGURED
;
401 Instance
->Result
= EFI_DEVICE_ERROR
;
403 if (Instance
->DoneEvent
!= NULL
) {
404 gBS
->SignalEvent (Instance
->DoneEvent
);
411 // This is a MNP handle, stop the whole driver
413 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiManagedNetworkProtocolGuid
);
415 if (NicHandle
== NULL
) {
420 // Get our context back.
422 Status
= gBS
->OpenProtocol (
424 &gEfiIp4ConfigProtocolGuid
,
425 (VOID
**) &Ip4Config
,
426 This
->DriverBindingHandle
,
428 EFI_OPEN_PROTOCOL_GET_PROTOCOL
431 if (EFI_ERROR (Status
)) {
435 Instance
= IP4_CONFIG_INSTANCE_FROM_IP4CONFIG (Ip4Config
);
437 Ip4ConfigDeviceUnload (Instance
);
440 // Unload the protocols first to inform the top drivers
442 Status
= gBS
->UninstallMultipleProtocolInterfaces (
444 &gEfiIp4ConfigProtocolGuid
,
445 &Instance
->Ip4ConfigProtocol
,
449 if (EFI_ERROR (Status
)) {
454 // Release all the resources
456 if (Instance
->MnpHandle
!= NULL
) {
459 &gEfiManagedNetworkProtocolGuid
,
460 This
->DriverBindingHandle
,
464 NetLibDestroyServiceChild (
467 &gEfiManagedNetworkServiceBindingProtocolGuid
,
471 Instance
->Mnp
= NULL
;
472 Instance
->MnpHandle
= NULL
;
475 if (Instance
->MacString
!= NULL
) {
476 FreePool (Instance
->MacString
);
479 Ip4ConfigCleanConfig (Instance
);