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 entry point for IP4 config driver which install the driver
31 binding and component name protocol on its image.
33 @param ImageHandle The image handle of the driver.
34 @param SystemTable The system table.
36 @retval EFI_SUCCES All the related protocols are installed on the driver.
37 @retval Others Failed to install protocols.
42 Ip4ConfigDriverEntryPoint (
43 IN EFI_HANDLE ImageHandle
,
44 IN EFI_SYSTEM_TABLE
*SystemTable
47 return EfiLibInstallDriverBindingComponentName2 (
50 &gIp4ConfigDriverBinding
,
52 &gIp4ConfigComponentName
,
53 &gIp4ConfigComponentName2
59 Test to see if this driver supports ControllerHandle.
61 @param This Protocol instance pointer.
62 @param ControllerHandle Handle of device to test
63 @param RemainingDevicePath Optional parameter use to pick a specific child
66 @retval EFI_SUCCES This driver supports this device
67 @retval EFI_ALREADY_STARTED This driver is already running on this device
68 @retval other This driver does not support this device
73 Ip4ConfigDriverBindingSupported (
74 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
75 IN EFI_HANDLE ControllerHandle
,
76 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
81 Status
= gBS
->OpenProtocol (
83 &gEfiManagedNetworkServiceBindingProtocolGuid
,
85 This
->DriverBindingHandle
,
87 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
95 Start this driver on ControllerHandle.
97 @param This Protocol instance pointer.
98 @param ControllerHandle Handle of device to bind driver to
99 @param RemainingDevicePath Optional parameter use to pick a specific child
102 @retval EFI_SUCCES This driver is added to ControllerHandle
103 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
104 @retval other This driver does not support this device
109 Ip4ConfigDriverBindingStart (
110 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
111 IN EFI_HANDLE ControllerHandle
,
112 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
115 EFI_IP4_CONFIG_PROTOCOL
*Ip4Config
;
116 EFI_MANAGED_NETWORK_PROTOCOL
*Mnp
;
117 EFI_HANDLE MnpHandle
;
118 IP4_CONFIG_INSTANCE
*Instance
;
119 EFI_SIMPLE_NETWORK_MODE SnpMode
;
120 NIC_IP4_CONFIG_INFO
*NicConfig
;
122 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
124 Status
= gBS
->HandleProtocol (
126 &gEfiDevicePathProtocolGuid
,
127 (VOID
**) &ParentDevicePath
129 if (EFI_ERROR (Status
)) {
134 // Check for multiple start.
136 Status
= gBS
->OpenProtocol (
138 &gEfiIp4ConfigProtocolGuid
,
139 (VOID
**) &Ip4Config
,
140 This
->DriverBindingHandle
,
142 EFI_OPEN_PROTOCOL_GET_PROTOCOL
145 if (!EFI_ERROR (Status
)) {
146 return EFI_ALREADY_STARTED
;
150 // Create a MNP child
156 Status
= NetLibCreateServiceChild (
158 This
->DriverBindingHandle
,
159 &gEfiManagedNetworkServiceBindingProtocolGuid
,
163 if (EFI_ERROR (Status
)) {
167 Status
= gBS
->OpenProtocol (
169 &gEfiManagedNetworkProtocolGuid
,
171 This
->DriverBindingHandle
,
173 EFI_OPEN_PROTOCOL_BY_DRIVER
176 if (EFI_ERROR (Status
)) {
181 // Allocate an instance then initialize it
183 Instance
= AllocateZeroPool (sizeof (IP4_CONFIG_INSTANCE
));
185 if (Instance
== NULL
) {
186 Status
= EFI_OUT_OF_RESOURCES
;
190 Instance
->Signature
= IP4_CONFIG_INSTANCE_SIGNATURE
;
191 Instance
->Controller
= ControllerHandle
;
192 Instance
->Image
= This
->DriverBindingHandle
;
193 Instance
->ParentDevicePath
= ParentDevicePath
;
195 CopyMem (&Instance
->Ip4ConfigProtocol
, &mIp4ConfigProtocolTemplate
, sizeof (mIp4ConfigProtocolTemplate
));
197 Instance
->State
= IP4_CONFIG_STATE_IDLE
;
199 Instance
->MnpHandle
= MnpHandle
;
201 Instance
->DoneEvent
= NULL
;
202 Instance
->ReconfigEvent
= NULL
;
203 Instance
->Result
= EFI_NOT_READY
;
204 Instance
->NicConfig
= NULL
;
206 Instance
->Dhcp4
= NULL
;
207 Instance
->Dhcp4Handle
= NULL
;
208 Instance
->Dhcp4Event
= NULL
;
210 Status
= Mnp
->GetModeData (Mnp
, NULL
, &SnpMode
);
212 if (EFI_ERROR (Status
) && (Status
!= EFI_NOT_STARTED
)) {
216 Instance
->NicAddr
.Type
= (UINT16
) SnpMode
.IfType
;
217 Instance
->NicAddr
.Len
= (UINT8
) SnpMode
.HwAddressSize
;
218 CopyMem (&Instance
->NicAddr
.MacAddr
, &SnpMode
.CurrentAddress
, Instance
->NicAddr
.Len
);
221 // Add it to the global list, and compose the name
223 Status
= NetLibGetMacString (Instance
->Controller
, Instance
->Image
, &Instance
->MacString
);
224 if (EFI_ERROR (Status
)) {
228 Status
= Ip4ConfigDeviceInit (Instance
);
231 // Install the IP4_CONFIG protocols
233 Status
= gBS
->InstallMultipleProtocolInterfaces (
235 &gEfiIp4ConfigProtocolGuid
,
236 &Instance
->Ip4ConfigProtocol
,
240 if (EFI_ERROR (Status
)) {
245 // Get the previous configure parameters. If an error happend here,
246 // just ignore it because the driver should be able to operate.
248 NicConfig
= Ip4ConfigReadVariable (Instance
);
249 if (NicConfig
!= NULL
) {
250 if (NicConfig
->Perment
) {
251 if (NicConfig
->Source
== IP4_CONFIG_SOURCE_STATIC
) {
253 // Don't modify the permanent static configuration.
255 } else if (NicConfig
->Source
== IP4_CONFIG_SOURCE_DHCP
) {
257 // Remove the previous acquired DHCP parameters.
259 ZeroMem (&NicConfig
->Ip4Info
, sizeof (EFI_IP4_IPCONFIG_DATA
));
260 Ip4ConfigWriteVariable (Instance
, NicConfig
);
264 // Delete the non-permanent configuration.
266 Ip4ConfigWriteVariable (Instance
, NULL
);
269 FreePool (NicConfig
);
275 if (Instance
!= NULL
) {
282 &gEfiManagedNetworkProtocolGuid
,
283 This
->DriverBindingHandle
,
288 NetLibDestroyServiceChild (
290 This
->DriverBindingHandle
,
291 &gEfiManagedNetworkProtocolGuid
,
300 Stop this driver on ControllerHandle.
302 @param This Protocol instance pointer.
303 @param ControllerHandle Handle of device to stop driver on
304 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
305 children is zero stop the entire bus driver.
306 @param ChildHandleBuffer List of Child Handles to Stop.
308 @retval EFI_SUCCES This driver is removed ControllerHandle
309 @retval other This driver was not removed from this device
314 Ip4ConfigDriverBindingStop (
315 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
316 IN EFI_HANDLE ControllerHandle
,
317 IN UINTN NumberOfChildren
,
318 IN EFI_HANDLE
*ChildHandleBuffer
321 IP4_CONFIG_INSTANCE
*Instance
;
322 EFI_IP4_CONFIG_PROTOCOL
*Ip4Config
;
323 EFI_HANDLE NicHandle
;
327 // IP4_CONFIG instance opens an MNP child. It may also create and open
328 // a DHCP child. If this is the DHCP handle, stop the DHCP process. If
329 // it is the MNP child, stop the whole driver.
332 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiDhcp4ProtocolGuid
);
334 if (NicHandle
!= NULL
) {
336 // Get our context back then clean the DHCP up. Notify the user if necessary.
338 Status
= gBS
->OpenProtocol (
340 &gEfiIp4ConfigProtocolGuid
,
341 (VOID
**) &Ip4Config
,
342 This
->DriverBindingHandle
,
344 EFI_OPEN_PROTOCOL_GET_PROTOCOL
347 if (EFI_ERROR (Status
)) {
351 Instance
= IP4_CONFIG_INSTANCE_FROM_IP4CONFIG (Ip4Config
);
352 ASSERT (ControllerHandle
== Instance
->Dhcp4Handle
);
354 Ip4ConfigCleanDhcp4 (Instance
);
356 Instance
->State
= IP4_CONFIG_STATE_CONFIGURED
;
357 Instance
->Result
= EFI_DEVICE_ERROR
;
359 if (Instance
->DoneEvent
!= NULL
) {
360 gBS
->SignalEvent (Instance
->DoneEvent
);
367 // This is a MNP handle, stop the whole driver
369 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiManagedNetworkProtocolGuid
);
371 if (NicHandle
== NULL
) {
376 // Get our context back.
378 Status
= gBS
->OpenProtocol (
380 &gEfiIp4ConfigProtocolGuid
,
381 (VOID
**) &Ip4Config
,
382 This
->DriverBindingHandle
,
384 EFI_OPEN_PROTOCOL_GET_PROTOCOL
387 if (EFI_ERROR (Status
)) {
391 Instance
= IP4_CONFIG_INSTANCE_FROM_IP4CONFIG (Ip4Config
);
393 Ip4ConfigDeviceUnload (Instance
);
396 // Unload the protocols first to inform the top drivers
398 Status
= gBS
->UninstallMultipleProtocolInterfaces (
400 &gEfiIp4ConfigProtocolGuid
,
401 &Instance
->Ip4ConfigProtocol
,
405 if (EFI_ERROR (Status
)) {
410 // Release all the resources
412 if (Instance
->MnpHandle
!= NULL
) {
415 &gEfiManagedNetworkProtocolGuid
,
416 This
->DriverBindingHandle
,
420 NetLibDestroyServiceChild (
423 &gEfiManagedNetworkServiceBindingProtocolGuid
,
427 Instance
->Mnp
= NULL
;
428 Instance
->MnpHandle
= NULL
;
431 if (Instance
->MacString
!= NULL
) {
432 FreePool (Instance
->MacString
);
435 Ip4ConfigCleanConfig (Instance
);