2 The driver binding for IP4 CONFIG protocol.
4 Copyright (c) 2006 - 2009, 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"
17 #include "Ip4ConfigNv.h"
19 EFI_DRIVER_BINDING_PROTOCOL gIp4ConfigDriverBinding
= {
20 Ip4ConfigDriverBindingSupported
,
21 Ip4ConfigDriverBindingStart
,
22 Ip4ConfigDriverBindingStop
,
29 Stop all the auto configuration when the IP4 configure driver is
32 @param ImageHandle The driver that is being unloaded
34 @retval EFI_SUCCESS The driver has been ready for unload.
40 IN EFI_HANDLE ImageHandle
45 Ip4ConfigFormUnload ();
48 // Stop all the IP4_CONFIG instances
50 for (Index
= 0; Index
< MAX_IP4_CONFIG_IN_VARIABLE
; Index
++) {
51 if (mIp4ConfigNicList
[Index
] == NULL
) {
55 gIp4ConfigDriverBinding
.Stop (
56 &gIp4ConfigDriverBinding
,
57 mIp4ConfigNicList
[Index
]->MnpHandle
,
63 return NetLibDefaultUnload (ImageHandle
);
67 The entry point for IP4 config driver which install the driver
68 binding and component name protocol on its image.
70 @param ImageHandle The image handle of the driver.
71 @param SystemTable The system table.
73 @retval EFI_SUCCES All the related protocols are installed on the driver.
74 @retval Others Failed to install protocols.
79 Ip4ConfigDriverEntryPoint (
80 IN EFI_HANDLE ImageHandle
,
81 IN EFI_SYSTEM_TABLE
*SystemTable
86 return EfiLibInstallDriverBindingComponentName2 (
89 &gIp4ConfigDriverBinding
,
91 &gIp4ConfigComponentName
,
92 &gIp4ConfigComponentName2
98 Test to see if this driver supports ControllerHandle.
100 @param This Protocol instance pointer.
101 @param ControllerHandle Handle of device to test
102 @param RemainingDevicePath Optional parameter use to pick a specific child
105 @retval EFI_SUCCES This driver supports this device
106 @retval EFI_ALREADY_STARTED This driver is already running on this device
107 @retval other This driver does not support this device
112 Ip4ConfigDriverBindingSupported (
113 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
114 IN EFI_HANDLE ControllerHandle
,
115 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
120 Status
= gBS
->OpenProtocol (
122 &gEfiManagedNetworkServiceBindingProtocolGuid
,
124 This
->DriverBindingHandle
,
126 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
134 Start this driver on ControllerHandle.
136 @param This Protocol instance pointer.
137 @param ControllerHandle Handle of device to bind driver to
138 @param RemainingDevicePath Optional parameter use to pick a specific child
141 @retval EFI_SUCCES This driver is added to ControllerHandle
142 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
143 @retval other This driver does not support this device
148 Ip4ConfigDriverBindingStart (
149 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
150 IN EFI_HANDLE ControllerHandle
,
151 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
154 EFI_IP4_CONFIG_PROTOCOL
*Ip4Config
;
155 EFI_MANAGED_NETWORK_PROTOCOL
*Mnp
;
156 EFI_HANDLE MnpHandle
;
157 IP4_CONFIG_INSTANCE
*Instance
;
158 EFI_SIMPLE_NETWORK_MODE SnpMode
;
159 IP4_CONFIG_VARIABLE
*Variable
;
160 NIC_IP4_CONFIG_INFO
*NicConfig
;
161 IP4_CONFIG_VARIABLE
*NewVariable
;
166 // Check for multiple start.
168 Status
= gBS
->OpenProtocol (
170 &gEfiIp4ConfigProtocolGuid
,
171 (VOID
**) &Ip4Config
,
172 This
->DriverBindingHandle
,
174 EFI_OPEN_PROTOCOL_GET_PROTOCOL
177 if (!EFI_ERROR (Status
)) {
178 return EFI_ALREADY_STARTED
;
182 // Create a MNP child
188 Status
= NetLibCreateServiceChild (
190 This
->DriverBindingHandle
,
191 &gEfiManagedNetworkServiceBindingProtocolGuid
,
195 if (EFI_ERROR (Status
)) {
199 Status
= gBS
->OpenProtocol (
201 &gEfiManagedNetworkProtocolGuid
,
203 This
->DriverBindingHandle
,
205 EFI_OPEN_PROTOCOL_BY_DRIVER
208 if (EFI_ERROR (Status
)) {
213 // Allocate an instance then initialize it
215 Instance
= AllocatePool (sizeof (IP4_CONFIG_INSTANCE
));
217 if (Instance
== NULL
) {
218 Status
= EFI_OUT_OF_RESOURCES
;
222 Instance
->Signature
= IP4_CONFIG_INSTANCE_SIGNATURE
;
223 Instance
->Controller
= ControllerHandle
;
224 Instance
->Image
= This
->DriverBindingHandle
;
226 CopyMem (&Instance
->Ip4ConfigProtocol
, &mIp4ConfigProtocolTemplate
, sizeof (mIp4ConfigProtocolTemplate
));
228 Instance
->State
= IP4_CONFIG_STATE_IDLE
;
230 Instance
->MnpHandle
= MnpHandle
;
232 Instance
->DoneEvent
= NULL
;
233 Instance
->ReconfigEvent
= NULL
;
234 Instance
->Result
= EFI_NOT_READY
;
235 Instance
->NicConfig
= NULL
;
237 Instance
->Dhcp4
= NULL
;
238 Instance
->Dhcp4Handle
= NULL
;
239 Instance
->Dhcp4Event
= NULL
;
241 Status
= Mnp
->GetModeData (Mnp
, NULL
, &SnpMode
);
243 if (EFI_ERROR (Status
) && (Status
!= EFI_NOT_STARTED
)) {
247 Instance
->NicAddr
.Type
= (UINT16
) SnpMode
.IfType
;
248 Instance
->NicAddr
.Len
= (UINT8
) SnpMode
.HwAddressSize
;
249 CopyMem (&Instance
->NicAddr
.MacAddr
, &SnpMode
.CurrentAddress
, Instance
->NicAddr
.Len
);
252 // Add it to the global list, and compose the name
254 for (Index
= 0; Index
< MAX_IP4_CONFIG_IN_VARIABLE
; Index
++) {
255 if (mIp4ConfigNicList
[Index
] == NULL
) {
256 mIp4ConfigNicList
[Index
] = Instance
;
257 Instance
->NicIndex
= Index
;
259 if (Instance
->NicAddr
.Type
== NET_IFTYPE_ETHERNET
) {
260 UnicodeSPrint (Instance
->NicName
, (UINTN
) IP4_NIC_NAME_LENGTH
, L
"eth%d", Index
);
262 UnicodeSPrint (Instance
->NicName
, (UINTN
) IP4_NIC_NAME_LENGTH
, L
"unk%d", Index
);
269 if (Index
== MAX_IP4_CONFIG_IN_VARIABLE
) {
270 Status
= EFI_OUT_OF_RESOURCES
;
274 Status
= Ip4ConfigDeviceInit (Instance
);
275 if (!EFI_ERROR (Status
)) {
277 // Try to add a port configuration page for this controller.
279 Ip4ConfigUpdateForm (Instance
, TRUE
);
283 // Install the IP4_CONFIG and NIC_IP4CONFIG protocols
285 Status
= gBS
->InstallMultipleProtocolInterfaces (
287 &gEfiIp4ConfigProtocolGuid
,
288 &Instance
->Ip4ConfigProtocol
,
292 if (EFI_ERROR (Status
)) {
293 mIp4ConfigNicList
[Index
] = NULL
;
298 // Get the previous configure parameters. If an error happend here,
299 // just ignore it because the driver should be able to operate.
301 Variable
= Ip4ConfigReadVariable ();
303 if (Variable
== NULL
) {
307 NicConfig
= Ip4ConfigFindNicVariable (Variable
, &Instance
->NicAddr
);
309 if (NicConfig
== NULL
) {
314 // Don't modify the permant static configuration
316 if (NicConfig
->Perment
&& (NicConfig
->Source
== IP4_CONFIG_SOURCE_STATIC
)) {
321 // Delete the non-permant configuration and remove the previous
322 // acquired DHCP parameters. Only doing DHCP itself is permant
326 if (!NicConfig
->Perment
) {
327 NewVariable
= Ip4ConfigModifyVariable (Variable
, &Instance
->NicAddr
, NULL
);
329 } else if (NicConfig
->Source
== IP4_CONFIG_SOURCE_DHCP
) {
330 ZeroMem (&NicConfig
->Ip4Info
, sizeof (EFI_IP4_IPCONFIG_DATA
));
331 NewVariable
= Ip4ConfigModifyVariable (Variable
, &Instance
->NicAddr
, NicConfig
);
335 Ip4ConfigWriteVariable (NewVariable
);
337 if (NewVariable
!= NULL
) {
338 gBS
->FreePool (NewVariable
);
342 gBS
->FreePool (Variable
);
344 if (NicConfig
!= NULL
) {
345 gBS
->FreePool (NicConfig
);
351 if (Instance
!= NULL
) {
352 gBS
->FreePool (Instance
);
358 &gEfiManagedNetworkProtocolGuid
,
359 This
->DriverBindingHandle
,
364 NetLibDestroyServiceChild (
366 This
->DriverBindingHandle
,
367 &gEfiManagedNetworkProtocolGuid
,
376 Stop this driver on ControllerHandle.
378 @param This Protocol instance pointer.
379 @param ControllerHandle Handle of device to stop driver on
380 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
381 children is zero stop the entire bus driver.
382 @param ChildHandleBuffer List of Child Handles to Stop.
384 @retval EFI_SUCCES This driver is removed ControllerHandle
385 @retval other This driver was not removed from this device
390 Ip4ConfigDriverBindingStop (
391 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
392 IN EFI_HANDLE ControllerHandle
,
393 IN UINTN NumberOfChildren
,
394 IN EFI_HANDLE
*ChildHandleBuffer
397 IP4_CONFIG_INSTANCE
*Instance
;
398 EFI_IP4_CONFIG_PROTOCOL
*Ip4Config
;
399 EFI_HANDLE NicHandle
;
403 // IP4_CONFIG instance opens an MNP child. It may also create and open
404 // a DHCP child. If this is the DHCP handle, stop the DHCP process. If
405 // it is the MNP child, stop the whole driver.
408 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiDhcp4ProtocolGuid
);
410 if (NicHandle
!= NULL
) {
412 // Get our context back then clean the DHCP up. Notify the user if necessary.
414 Status
= gBS
->OpenProtocol (
416 &gEfiIp4ConfigProtocolGuid
,
417 (VOID
**) &Ip4Config
,
418 This
->DriverBindingHandle
,
420 EFI_OPEN_PROTOCOL_GET_PROTOCOL
423 if (EFI_ERROR (Status
)) {
427 Instance
= IP4_CONFIG_INSTANCE_FROM_IP4CONFIG (Ip4Config
);
428 ASSERT (ControllerHandle
== Instance
->Dhcp4Handle
);
430 Ip4ConfigCleanDhcp4 (Instance
);
432 Instance
->State
= IP4_CONFIG_STATE_CONFIGURED
;
433 Instance
->Result
= EFI_DEVICE_ERROR
;
435 if (Instance
->DoneEvent
!= NULL
) {
436 gBS
->SignalEvent (Instance
->DoneEvent
);
443 // This is a MNP handle, stop the whole driver
445 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiManagedNetworkProtocolGuid
);
447 if (NicHandle
== NULL
) {
452 // Get our context back.
454 Status
= gBS
->OpenProtocol (
456 &gEfiIp4ConfigProtocolGuid
,
457 (VOID
**) &Ip4Config
,
458 This
->DriverBindingHandle
,
460 EFI_OPEN_PROTOCOL_GET_PROTOCOL
463 if (EFI_ERROR (Status
)) {
467 Instance
= IP4_CONFIG_INSTANCE_FROM_IP4CONFIG (Ip4Config
);
469 Ip4ConfigDeviceUnload (Instance
);
472 // Unload the protocols first to inform the top drivers
474 Status
= gBS
->UninstallMultipleProtocolInterfaces (
476 &gEfiIp4ConfigProtocolGuid
,
477 &Instance
->Ip4ConfigProtocol
,
481 if (EFI_ERROR (Status
)) {
486 // Release all the resources
488 if (Instance
->MnpHandle
!= NULL
) {
491 &gEfiManagedNetworkProtocolGuid
,
492 This
->DriverBindingHandle
,
496 NetLibDestroyServiceChild (
499 &gEfiManagedNetworkServiceBindingProtocolGuid
,
503 Instance
->Mnp
= NULL
;
504 Instance
->MnpHandle
= NULL
;
507 Ip4ConfigCleanConfig (Instance
);
508 mIp4ConfigNicList
[Instance
->NicIndex
] = NULL
;
509 gBS
->FreePool (Instance
);