]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/Ip4ConfigDxe/Ip4ConfigDriver.c
1. retired NicIp4ConfigProtocolGuid
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4ConfigDxe / Ip4ConfigDriver.c
1 /** @file
2 The driver binding for IP4 CONFIG protocol.
3
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
9
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.
12
13 **/
14
15
16 #include "Ip4Config.h"
17 #include "Ip4ConfigNV.h"
18
19 EFI_DRIVER_BINDING_PROTOCOL gIp4ConfigDriverBinding = {
20 Ip4ConfigDriverBindingSupported,
21 Ip4ConfigDriverBindingStart,
22 Ip4ConfigDriverBindingStop,
23 0xa,
24 NULL,
25 NULL
26 };
27
28 /**
29 Stop all the auto configuration when the IP4 configure driver is
30 being unloaded.
31
32 @param ImageHandle The driver that is being unloaded
33
34 @retval EFI_SUCCESS The driver has been ready for unload.
35
36 **/
37 EFI_STATUS
38 EFIAPI
39 EfiIp4ConfigUnload (
40 IN EFI_HANDLE ImageHandle
41 )
42 {
43 UINT32 Index;
44
45 Ip4ConfigFormUnload ();
46
47 //
48 // Stop all the IP4_CONFIG instances
49 //
50 for (Index = 0; Index < MAX_IP4_CONFIG_IN_VARIABLE; Index++) {
51 if (mIp4ConfigNicList[Index] == NULL) {
52 continue;
53 }
54
55 gIp4ConfigDriverBinding.Stop (
56 &gIp4ConfigDriverBinding,
57 mIp4ConfigNicList[Index]->MnpHandle,
58 0,
59 NULL
60 );
61 }
62
63 return NetLibDefaultUnload (ImageHandle);
64 }
65
66 /**
67 The entry point for IP4 config driver which install the driver
68 binding and component name protocol on its image.
69
70 @param ImageHandle The image handle of the driver.
71 @param SystemTable The system table.
72
73 @retval EFI_SUCCES All the related protocols are installed on the driver.
74 @retval Others Failed to install protocols.
75
76 **/
77 EFI_STATUS
78 EFIAPI
79 Ip4ConfigDriverEntryPoint (
80 IN EFI_HANDLE ImageHandle,
81 IN EFI_SYSTEM_TABLE *SystemTable
82 )
83 {
84 Ip4ConfigFormInit ();
85
86 return EfiLibInstallDriverBindingComponentName2 (
87 ImageHandle,
88 SystemTable,
89 &gIp4ConfigDriverBinding,
90 ImageHandle,
91 &gIp4ConfigComponentName,
92 &gIp4ConfigComponentName2
93 );
94 }
95
96
97 /**
98 Test to see if this driver supports ControllerHandle.
99
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
103 device to start.
104
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
108
109 **/
110 EFI_STATUS
111 EFIAPI
112 Ip4ConfigDriverBindingSupported (
113 IN EFI_DRIVER_BINDING_PROTOCOL *This,
114 IN EFI_HANDLE ControllerHandle,
115 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
116 )
117 {
118 EFI_STATUS Status;
119
120 Status = gBS->OpenProtocol (
121 ControllerHandle,
122 &gEfiManagedNetworkServiceBindingProtocolGuid,
123 NULL,
124 This->DriverBindingHandle,
125 ControllerHandle,
126 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
127 );
128
129 return Status;
130 }
131
132
133 /**
134 Start this driver on ControllerHandle.
135
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
139 device to start.
140
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
144
145 **/
146 EFI_STATUS
147 EFIAPI
148 Ip4ConfigDriverBindingStart (
149 IN EFI_DRIVER_BINDING_PROTOCOL *This,
150 IN EFI_HANDLE ControllerHandle,
151 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
152 )
153 {
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;
162 EFI_STATUS Status;
163 UINT32 Index;
164
165 //
166 // Check for multiple start.
167 //
168 Status = gBS->OpenProtocol (
169 ControllerHandle,
170 &gEfiIp4ConfigProtocolGuid,
171 (VOID **) &Ip4Config,
172 This->DriverBindingHandle,
173 ControllerHandle,
174 EFI_OPEN_PROTOCOL_GET_PROTOCOL
175 );
176
177 if (!EFI_ERROR (Status)) {
178 return EFI_ALREADY_STARTED;
179 }
180
181 //
182 // Create a MNP child
183 //
184 Mnp = NULL;
185 MnpHandle = NULL;
186 Instance = NULL;
187
188 Status = NetLibCreateServiceChild (
189 ControllerHandle,
190 This->DriverBindingHandle,
191 &gEfiManagedNetworkServiceBindingProtocolGuid,
192 &MnpHandle
193 );
194
195 if (EFI_ERROR (Status)) {
196 return Status;
197 }
198
199 Status = gBS->OpenProtocol (
200 MnpHandle,
201 &gEfiManagedNetworkProtocolGuid,
202 (VOID **) &Mnp,
203 This->DriverBindingHandle,
204 ControllerHandle,
205 EFI_OPEN_PROTOCOL_BY_DRIVER
206 );
207
208 if (EFI_ERROR (Status)) {
209 goto ON_ERROR;
210 }
211
212 //
213 // Allocate an instance then initialize it
214 //
215 Instance = AllocatePool (sizeof (IP4_CONFIG_INSTANCE));
216
217 if (Instance == NULL) {
218 Status = EFI_OUT_OF_RESOURCES;
219 goto ON_ERROR;
220 }
221
222 Instance->Signature = IP4_CONFIG_INSTANCE_SIGNATURE;
223 Instance->Controller = ControllerHandle;
224 Instance->Image = This->DriverBindingHandle;
225
226 CopyMem (&Instance->Ip4ConfigProtocol, &mIp4ConfigProtocolTemplate, sizeof (mIp4ConfigProtocolTemplate));
227
228 Instance->State = IP4_CONFIG_STATE_IDLE;
229 Instance->Mnp = Mnp;
230 Instance->MnpHandle = MnpHandle;
231
232 Instance->DoneEvent = NULL;
233 Instance->ReconfigEvent = NULL;
234 Instance->Result = EFI_NOT_READY;
235 Instance->NicConfig = NULL;
236
237 Instance->Dhcp4 = NULL;
238 Instance->Dhcp4Handle = NULL;
239 Instance->Dhcp4Event = NULL;
240
241 Status = Mnp->GetModeData (Mnp, NULL, &SnpMode);
242
243 if (EFI_ERROR (Status) && (Status != EFI_NOT_STARTED)) {
244 goto ON_ERROR;
245 }
246
247 Instance->NicAddr.Type = (UINT16) SnpMode.IfType;
248 Instance->NicAddr.Len = (UINT8) SnpMode.HwAddressSize;
249 CopyMem (&Instance->NicAddr.MacAddr, &SnpMode.CurrentAddress, Instance->NicAddr.Len);
250
251 //
252 // Add it to the global list, and compose the name
253 //
254 for (Index = 0; Index < MAX_IP4_CONFIG_IN_VARIABLE; Index++) {
255 if (mIp4ConfigNicList[Index] == NULL) {
256 mIp4ConfigNicList[Index] = Instance;
257 Instance->NicIndex = Index;
258
259 if (Instance->NicAddr.Type == NET_IFTYPE_ETHERNET) {
260 Instance->NicName[0] = 'e';
261 Instance->NicName[1] = 't';
262 Instance->NicName[2] = 'h';
263 Instance->NicName[3] = (UINT16) ('0' + Index);
264 Instance->NicName[4] = 0;
265 } else {
266 Instance->NicName[0] = 'u';
267 Instance->NicName[1] = 'n';
268 Instance->NicName[2] = 'k';
269 Instance->NicName[3] = (UINT16) ('0' + Index);
270 Instance->NicName[4] = 0;
271 }
272
273 break;
274 }
275 }
276
277 if (Index == MAX_IP4_CONFIG_IN_VARIABLE) {
278 Status = EFI_OUT_OF_RESOURCES;
279 goto ON_ERROR;
280 }
281
282 Status = Ip4ConfigDeviceInit (Instance);
283 if (!EFI_ERROR (Status)) {
284 //
285 // Try to add a port configuration page for this controller.
286 //
287 Ip4ConfigUpdateForm (Instance, TRUE);
288 }
289
290 //
291 // Install the IP4_CONFIG and NIC_IP4CONFIG protocols
292 //
293 Status = gBS->InstallMultipleProtocolInterfaces (
294 &ControllerHandle,
295 &gEfiIp4ConfigProtocolGuid,
296 &Instance->Ip4ConfigProtocol,
297 NULL
298 );
299
300 if (EFI_ERROR (Status)) {
301 mIp4ConfigNicList[Index] = NULL;
302 goto ON_ERROR;
303 }
304
305 //
306 // Get the previous configure parameters. If an error happend here,
307 // just ignore it because the driver should be able to operate.
308 //
309 Variable = Ip4ConfigReadVariable ();
310
311 if (Variable == NULL) {
312 return EFI_SUCCESS;
313 }
314
315 NicConfig = Ip4ConfigFindNicVariable (Variable, &Instance->NicAddr);
316
317 if (NicConfig == NULL) {
318 goto ON_EXIT;
319 }
320
321 //
322 // Don't modify the permant static configuration
323 //
324 if (NicConfig->Perment && (NicConfig->Source == IP4_CONFIG_SOURCE_STATIC)) {
325 goto ON_EXIT;
326 }
327
328 //
329 // Delete the non-permant configuration and remove the previous
330 // acquired DHCP parameters. Only doing DHCP itself is permant
331 //
332 NewVariable = NULL;
333
334 if (!NicConfig->Perment) {
335 NewVariable = Ip4ConfigModifyVariable (Variable, &Instance->NicAddr, NULL);
336
337 } else if (NicConfig->Source == IP4_CONFIG_SOURCE_DHCP) {
338 ZeroMem (&NicConfig->Ip4Info, sizeof (EFI_IP4_IPCONFIG_DATA));
339 NewVariable = Ip4ConfigModifyVariable (Variable, &Instance->NicAddr, NicConfig);
340
341 }
342
343 Ip4ConfigWriteVariable (NewVariable);
344
345 if (NewVariable != NULL) {
346 gBS->FreePool (NewVariable);
347 }
348
349 ON_EXIT:
350 gBS->FreePool (Variable);
351
352 if (NicConfig != NULL) {
353 gBS->FreePool (NicConfig);
354 }
355
356 return EFI_SUCCESS;
357
358 ON_ERROR:
359 if (Instance != NULL) {
360 gBS->FreePool (Instance);
361 }
362
363 if (Mnp != NULL) {
364 gBS->CloseProtocol (
365 MnpHandle,
366 &gEfiManagedNetworkProtocolGuid,
367 This->DriverBindingHandle,
368 ControllerHandle
369 );
370 }
371
372 NetLibDestroyServiceChild (
373 ControllerHandle,
374 This->DriverBindingHandle,
375 &gEfiManagedNetworkProtocolGuid,
376 MnpHandle
377 );
378
379 return Status;
380 }
381
382
383 /**
384 Stop this driver on ControllerHandle.
385
386 @param This Protocol instance pointer.
387 @param ControllerHandle Handle of device to stop driver on
388 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
389 children is zero stop the entire bus driver.
390 @param ChildHandleBuffer List of Child Handles to Stop.
391
392 @retval EFI_SUCCES This driver is removed ControllerHandle
393 @retval other This driver was not removed from this device
394
395 **/
396 EFI_STATUS
397 EFIAPI
398 Ip4ConfigDriverBindingStop (
399 IN EFI_DRIVER_BINDING_PROTOCOL *This,
400 IN EFI_HANDLE ControllerHandle,
401 IN UINTN NumberOfChildren,
402 IN EFI_HANDLE *ChildHandleBuffer
403 )
404 {
405 IP4_CONFIG_INSTANCE *Instance;
406 EFI_IP4_CONFIG_PROTOCOL *Ip4Config;
407 EFI_HANDLE NicHandle;
408 EFI_STATUS Status;
409
410 //
411 // IP4_CONFIG instance opens an MNP child. It may also create and open
412 // a DHCP child. If this is the DHCP handle, stop the DHCP process. If
413 // it is the MNP child, stop the whole driver.
414 //
415 //
416 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiDhcp4ProtocolGuid);
417
418 if (NicHandle != NULL) {
419 //
420 // Get our context back then clean the DHCP up. Notify the user if necessary.
421 //
422 Status = gBS->OpenProtocol (
423 NicHandle,
424 &gEfiIp4ConfigProtocolGuid,
425 (VOID **) &Ip4Config,
426 This->DriverBindingHandle,
427 ControllerHandle,
428 EFI_OPEN_PROTOCOL_GET_PROTOCOL
429 );
430
431 if (EFI_ERROR (Status)) {
432 return Status;
433 }
434
435 Instance = IP4_CONFIG_INSTANCE_FROM_IP4CONFIG (Ip4Config);
436 ASSERT (ControllerHandle == Instance->Dhcp4Handle);
437
438 Ip4ConfigCleanDhcp4 (Instance);
439
440 Instance->State = IP4_CONFIG_STATE_CONFIGURED;
441 Instance->Result = EFI_DEVICE_ERROR;
442
443 if (Instance->DoneEvent != NULL) {
444 gBS->SignalEvent (Instance->DoneEvent);
445 }
446
447 return EFI_SUCCESS;
448 }
449
450 //
451 // This is a MNP handle, stop the whole driver
452 //
453 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiManagedNetworkProtocolGuid);
454
455 if (NicHandle == NULL) {
456 return EFI_SUCCESS;
457 }
458
459 //
460 // Get our context back.
461 //
462 Status = gBS->OpenProtocol (
463 NicHandle,
464 &gEfiIp4ConfigProtocolGuid,
465 (VOID **) &Ip4Config,
466 This->DriverBindingHandle,
467 ControllerHandle,
468 EFI_OPEN_PROTOCOL_GET_PROTOCOL
469 );
470
471 if (EFI_ERROR (Status)) {
472 return Status;
473 }
474
475 Instance = IP4_CONFIG_INSTANCE_FROM_IP4CONFIG (Ip4Config);
476
477 Ip4ConfigDeviceUnload (Instance);
478
479 //
480 // Unload the protocols first to inform the top drivers
481 //
482 Status = gBS->UninstallMultipleProtocolInterfaces (
483 NicHandle,
484 &gEfiIp4ConfigProtocolGuid,
485 &Instance->Ip4ConfigProtocol,
486 NULL
487 );
488
489 if (EFI_ERROR (Status)) {
490 return Status;
491 }
492
493 //
494 // Release all the resources
495 //
496 if (Instance->MnpHandle != NULL) {
497 gBS->CloseProtocol (
498 Instance->MnpHandle,
499 &gEfiManagedNetworkProtocolGuid,
500 This->DriverBindingHandle,
501 NicHandle
502 );
503
504 NetLibDestroyServiceChild (
505 NicHandle,
506 Instance->Image,
507 &gEfiManagedNetworkServiceBindingProtocolGuid,
508 Instance->MnpHandle
509 );
510
511 Instance->Mnp = NULL;
512 Instance->MnpHandle = NULL;
513 }
514
515 Ip4ConfigCleanConfig (Instance);
516 mIp4ConfigNicList[Instance->NicIndex] = NULL;
517 gBS->FreePool (Instance);
518
519 return EFI_SUCCESS;
520 }
521