]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/Dhcp4Dxe/Dhcp4Driver.c
Fixed GCC 4.4 build issues due to EFIAPI not being used when required.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Dhcp4Dxe / Dhcp4Driver.c
1 /** @file
2
3 Copyright (c) 2006 - 2010, Intel Corporation.<BR>
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 **/
13
14 #include "Dhcp4Impl.h"
15 #include "Dhcp4Driver.h"
16
17 EFI_DRIVER_BINDING_PROTOCOL gDhcp4DriverBinding = {
18 Dhcp4DriverBindingSupported,
19 Dhcp4DriverBindingStart,
20 Dhcp4DriverBindingStop,
21 0xa,
22 NULL,
23 NULL
24 };
25
26 EFI_SERVICE_BINDING_PROTOCOL mDhcp4ServiceBindingTemplate = {
27 Dhcp4ServiceBindingCreateChild,
28 Dhcp4ServiceBindingDestroyChild
29 };
30
31 /**
32 This is the declaration of an EFI image entry point. This entry point is
33 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
34 both device drivers and bus drivers.
35
36 Entry point of the DHCP driver to install various protocols.
37
38 @param[in] ImageHandle The firmware allocated handle for the UEFI image.
39 @param[in] SystemTable A pointer to the EFI System Table.
40
41 @retval EFI_SUCCESS The operation completed successfully.
42 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
43
44 **/
45 EFI_STATUS
46 EFIAPI
47 Dhcp4DriverEntryPoint (
48 IN EFI_HANDLE ImageHandle,
49 IN EFI_SYSTEM_TABLE *SystemTable
50 )
51 {
52 return EfiLibInstallDriverBindingComponentName2 (
53 ImageHandle,
54 SystemTable,
55 &gDhcp4DriverBinding,
56 ImageHandle,
57 &gDhcp4ComponentName,
58 &gDhcp4ComponentName2
59 );
60 }
61
62
63 /**
64 Test to see if this driver supports ControllerHandle. This service
65 is called by the EFI boot service ConnectController(). In
66 order to make drivers as small as possible, there are a few calling
67 restrictions for this service. ConnectController() must
68 follow these calling restrictions. If any other agent wishes to call
69 Supported() it must also follow these calling restrictions.
70
71 @param[in] This Protocol instance pointer.
72 @param[in] ControllerHandle Handle of device to test
73 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
74 device to start.
75
76 @retval EFI_SUCCESS This driver supports this device
77 @retval EFI_ALREADY_STARTED This driver is already running on this device
78 @retval other This driver does not support this device
79
80 **/
81 EFI_STATUS
82 EFIAPI
83 Dhcp4DriverBindingSupported (
84 IN EFI_DRIVER_BINDING_PROTOCOL *This,
85 IN EFI_HANDLE ControllerHandle,
86 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
87 )
88 {
89 EFI_STATUS Status;
90
91 Status = gBS->OpenProtocol (
92 ControllerHandle,
93 &gEfiUdp4ServiceBindingProtocolGuid,
94 NULL,
95 This->DriverBindingHandle,
96 ControllerHandle,
97 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
98 );
99
100 return Status;
101 }
102
103
104
105 /**
106 Configure the default UDP child to receive all the DHCP traffics
107 on this network interface.
108
109 @param[in] UdpIo The UDP IO to configure
110 @param[in] Context The context to the function
111
112 @retval EFI_SUCCESS The UDP IO is successfully configured.
113 @retval Others Failed to configure the UDP child.
114
115 **/
116 EFI_STATUS
117 EFIAPI
118 DhcpConfigUdpIo (
119 IN UDP_IO *UdpIo,
120 IN VOID *Context
121 )
122 {
123 EFI_UDP4_CONFIG_DATA UdpConfigData;
124
125 UdpConfigData.AcceptBroadcast = TRUE;
126 UdpConfigData.AcceptPromiscuous = FALSE;
127 UdpConfigData.AcceptAnyPort = FALSE;
128 UdpConfigData.AllowDuplicatePort = TRUE;
129 UdpConfigData.TypeOfService = 0;
130 UdpConfigData.TimeToLive = 64;
131 UdpConfigData.DoNotFragment = FALSE;
132 UdpConfigData.ReceiveTimeout = 0;
133 UdpConfigData.TransmitTimeout = 0;
134
135 UdpConfigData.UseDefaultAddress = FALSE;
136 UdpConfigData.StationPort = DHCP_CLIENT_PORT;
137 UdpConfigData.RemotePort = DHCP_SERVER_PORT;
138
139 ZeroMem (&UdpConfigData.StationAddress, sizeof (EFI_IPv4_ADDRESS));
140 ZeroMem (&UdpConfigData.SubnetMask, sizeof (EFI_IPv4_ADDRESS));
141 ZeroMem (&UdpConfigData.RemoteAddress, sizeof (EFI_IPv4_ADDRESS));
142
143 return UdpIo->Protocol.Udp4->Configure (UdpIo->Protocol.Udp4, &UdpConfigData);;
144 }
145
146
147
148 /**
149 Destory the DHCP service. The Dhcp4 service may be partly initialized,
150 or partly destroyed. If a resource is destroyed, it is marked as so in
151 case the destroy failed and being called again later.
152
153 @param[in] DhcpSb The DHCP service instance to destory.
154
155 @retval EFI_SUCCESS Always return success.
156
157 **/
158 EFI_STATUS
159 Dhcp4CloseService (
160 IN DHCP_SERVICE *DhcpSb
161 )
162 {
163 DhcpCleanLease (DhcpSb);
164
165 if (DhcpSb->UdpIo != NULL) {
166 UdpIoFreeIo (DhcpSb->UdpIo);
167 DhcpSb->UdpIo = NULL;
168 }
169
170 if (DhcpSb->Timer != NULL) {
171 gBS->SetTimer (DhcpSb->Timer, TimerCancel, 0);
172 gBS->CloseEvent (DhcpSb->Timer);
173
174 DhcpSb->Timer = NULL;
175 }
176
177 return EFI_SUCCESS;
178 }
179
180
181
182 /**
183 Create a new DHCP service binding instance for the controller.
184
185 @param[in] Controller The controller to install DHCP service binding
186 protocol onto
187 @param[in] ImageHandle The driver's image handle
188 @param[out] Service The variable to receive the created DHCP service
189 instance.
190
191 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource .
192 @retval EFI_SUCCESS The DHCP service instance is created.
193 @retval other Other error occurs.
194
195 **/
196 EFI_STATUS
197 Dhcp4CreateService (
198 IN EFI_HANDLE Controller,
199 IN EFI_HANDLE ImageHandle,
200 OUT DHCP_SERVICE **Service
201 )
202 {
203 DHCP_SERVICE *DhcpSb;
204 EFI_STATUS Status;
205
206 *Service = NULL;
207 DhcpSb = AllocateZeroPool (sizeof (DHCP_SERVICE));
208
209 if (DhcpSb == NULL) {
210 return EFI_OUT_OF_RESOURCES;
211 }
212
213 DhcpSb->Signature = DHCP_SERVICE_SIGNATURE;
214 DhcpSb->ServiceState = DHCP_UNCONFIGED;
215 DhcpSb->InDestory = FALSE;
216 DhcpSb->Controller = Controller;
217 DhcpSb->Image = ImageHandle;
218 InitializeListHead (&DhcpSb->Children);
219 DhcpSb->DhcpState = Dhcp4Stopped;
220 DhcpSb->Xid = NET_RANDOM (NetRandomInitSeed ());
221 CopyMem (
222 &DhcpSb->ServiceBinding,
223 &mDhcp4ServiceBindingTemplate,
224 sizeof (EFI_SERVICE_BINDING_PROTOCOL)
225 );
226 //
227 // Create various resources, UdpIo, Timer, and get Mac address
228 //
229 Status = gBS->CreateEvent (
230 EVT_NOTIFY_SIGNAL | EVT_TIMER,
231 TPL_CALLBACK,
232 DhcpOnTimerTick,
233 DhcpSb,
234 &DhcpSb->Timer
235 );
236
237 if (EFI_ERROR (Status)) {
238 goto ON_ERROR;
239 }
240
241 DhcpSb->UdpIo = UdpIoCreateIo (
242 Controller,
243 ImageHandle,
244 DhcpConfigUdpIo,
245 UDP_IO_UDP4_VERSION,
246 NULL
247 );
248
249 if (DhcpSb->UdpIo == NULL) {
250 Status = EFI_OUT_OF_RESOURCES;
251 goto ON_ERROR;
252 }
253
254 DhcpSb->HwLen = (UINT8) DhcpSb->UdpIo->SnpMode.HwAddressSize;
255 DhcpSb->HwType = DhcpSb->UdpIo->SnpMode.IfType;
256 CopyMem (&DhcpSb->Mac, &DhcpSb->UdpIo->SnpMode.CurrentAddress, sizeof (DhcpSb->Mac));
257
258 *Service = DhcpSb;
259 return EFI_SUCCESS;
260
261 ON_ERROR:
262 Dhcp4CloseService (DhcpSb);
263 FreePool (DhcpSb);
264
265 return Status;
266 }
267
268
269 /**
270 Start this driver on ControllerHandle. This service is called by the
271 EFI boot service ConnectController(). In order to make
272 drivers as small as possible, there are a few calling restrictions for
273 this service. ConnectController() must follow these
274 calling restrictions. If any other agent wishes to call Start() it
275 must also follow these calling restrictions.
276
277 @param[in] This Protocol instance pointer.
278 @param[in] ControllerHandle Handle of device to bind driver to
279 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
280 device to start.
281
282 @retval EFI_SUCCESS This driver is added to ControllerHandle
283 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
284 @retval other This driver does not support this device
285
286 **/
287 EFI_STATUS
288 EFIAPI
289 Dhcp4DriverBindingStart (
290 IN EFI_DRIVER_BINDING_PROTOCOL *This,
291 IN EFI_HANDLE ControllerHandle,
292 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
293 )
294 {
295 DHCP_SERVICE *DhcpSb;
296 EFI_STATUS Status;
297
298 //
299 // First: test for the DHCP4 Protocol
300 //
301 Status = gBS->OpenProtocol (
302 ControllerHandle,
303 &gEfiDhcp4ServiceBindingProtocolGuid,
304 NULL,
305 This->DriverBindingHandle,
306 ControllerHandle,
307 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
308 );
309
310 if (Status == EFI_SUCCESS) {
311 return EFI_ALREADY_STARTED;
312 }
313
314 Status = Dhcp4CreateService (ControllerHandle, This->DriverBindingHandle, &DhcpSb);
315
316 if (EFI_ERROR (Status)) {
317 return Status;
318 }
319 ASSERT (DhcpSb != NULL);
320
321 //
322 // Start the receiving
323 //
324 Status = UdpIoRecvDatagram (DhcpSb->UdpIo, DhcpInput, DhcpSb, 0);
325
326 if (EFI_ERROR (Status)) {
327 goto ON_ERROR;
328 }
329 Status = gBS->SetTimer (DhcpSb->Timer, TimerPeriodic, TICKS_PER_SECOND);
330
331 if (EFI_ERROR (Status)) {
332 goto ON_ERROR;
333 }
334
335 //
336 // Install the Dhcp4ServiceBinding Protocol onto ControlerHandle
337 //
338 Status = gBS->InstallMultipleProtocolInterfaces (
339 &ControllerHandle,
340 &gEfiDhcp4ServiceBindingProtocolGuid,
341 &DhcpSb->ServiceBinding,
342 NULL
343 );
344
345 if (EFI_ERROR (Status)) {
346 goto ON_ERROR;
347 }
348
349 return Status;
350
351 ON_ERROR:
352 Dhcp4CloseService (DhcpSb);
353 FreePool (DhcpSb);
354 return Status;
355 }
356
357
358 /**
359 Stop this driver on ControllerHandle. This service is called by the
360 EFI boot service DisconnectController(). In order to
361 make drivers as small as possible, there are a few calling
362 restrictions for this service. DisconnectController()
363 must follow these calling restrictions. If any other agent wishes
364 to call Stop() it must also follow these calling restrictions.
365
366 @param[in] This Protocol instance pointer.
367 @param[in] ControllerHandle Handle of device to stop driver on
368 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
369 children is zero stop the entire bus driver.
370 @param[in] ChildHandleBuffer List of Child Handles to Stop.
371
372 @retval EFI_SUCCESS This driver is removed ControllerHandle
373 @retval other This driver was not removed from this device
374
375 **/
376 EFI_STATUS
377 EFIAPI
378 Dhcp4DriverBindingStop (
379 IN EFI_DRIVER_BINDING_PROTOCOL *This,
380 IN EFI_HANDLE ControllerHandle,
381 IN UINTN NumberOfChildren,
382 IN EFI_HANDLE *ChildHandleBuffer
383 )
384 {
385 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
386 DHCP_SERVICE *DhcpSb;
387 DHCP_PROTOCOL *Instance;
388 EFI_HANDLE NicHandle;
389 EFI_STATUS Status;
390 EFI_TPL OldTpl;
391
392 //
393 // DHCP driver opens UDP child, So, the ControllerHandle is the
394 // UDP child handle. locate the Nic handle first.
395 //
396 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiUdp4ProtocolGuid);
397
398 if (NicHandle == NULL) {
399 return EFI_DEVICE_ERROR;
400 }
401
402 Status = gBS->OpenProtocol (
403 NicHandle,
404 &gEfiDhcp4ServiceBindingProtocolGuid,
405 (VOID **) &ServiceBinding,
406 This->DriverBindingHandle,
407 NicHandle,
408 EFI_OPEN_PROTOCOL_GET_PROTOCOL
409 );
410
411 if (EFI_ERROR (Status)) {
412 return EFI_DEVICE_ERROR;
413 }
414
415 DhcpSb = DHCP_SERVICE_FROM_THIS (ServiceBinding);
416
417 if (DhcpSb->InDestory) {
418 return EFI_SUCCESS;
419 }
420
421 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
422
423 if (NumberOfChildren == 0) {
424
425 DhcpSb->InDestory = TRUE;
426 DhcpSb->ServiceState = DHCP_DESTORY;
427
428 gBS->UninstallProtocolInterface (
429 NicHandle,
430 &gEfiDhcp4ServiceBindingProtocolGuid,
431 ServiceBinding
432 );
433
434 Dhcp4CloseService (DhcpSb);
435
436 FreePool (DhcpSb);
437 } else {
438 //
439 // Don't use NET_LIST_FOR_EACH_SAFE here, Dhcp4ServiceBindingDestoryChild
440 // may cause other child to be deleted.
441 //
442 while (!IsListEmpty (&DhcpSb->Children)) {
443 Instance = NET_LIST_HEAD (&DhcpSb->Children, DHCP_PROTOCOL, Link);
444 ServiceBinding->DestroyChild (ServiceBinding, Instance->Handle);
445 }
446
447 if (DhcpSb->NumChildren != 0) {
448 Status = EFI_DEVICE_ERROR;
449 }
450 }
451
452 gBS->RestoreTPL (OldTpl);
453
454 return Status;
455 }
456
457
458 /**
459 Initialize a new DHCP instance.
460
461 @param DhcpSb The dhcp service instance
462 @param Instance The dhcp instance to initialize
463
464 **/
465 VOID
466 DhcpInitProtocol (
467 IN DHCP_SERVICE *DhcpSb,
468 IN OUT DHCP_PROTOCOL *Instance
469 )
470 {
471 Instance->Signature = DHCP_PROTOCOL_SIGNATURE;
472 CopyMem (&Instance->Dhcp4Protocol, &mDhcp4ProtocolTemplate, sizeof (Instance->Dhcp4Protocol));
473 InitializeListHead (&Instance->Link);
474 Instance->Handle = NULL;
475 Instance->Service = DhcpSb;
476 Instance->InDestory = FALSE;
477 Instance->CompletionEvent = NULL;
478 Instance->RenewRebindEvent = NULL;
479 Instance->Token = NULL;
480 Instance->UdpIo = NULL;
481 NetbufQueInit (&Instance->ResponseQueue);
482 }
483
484
485 /**
486 Creates a child handle and installs a protocol.
487
488 The CreateChild() function installs a protocol on ChildHandle.
489 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
490 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
491
492 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
493 @param ChildHandle Pointer to the handle of the child to create. If it is NULL,
494 then a new handle is created. If it is a pointer to an existing UEFI handle,
495 then the protocol is added to the existing UEFI handle.
496
497 @retval EFI_SUCCES The protocol was added to ChildHandle.
498 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
499 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
500 the child
501 @retval other The child handle was not created
502
503 **/
504 EFI_STATUS
505 EFIAPI
506 Dhcp4ServiceBindingCreateChild (
507 IN EFI_SERVICE_BINDING_PROTOCOL *This,
508 IN EFI_HANDLE *ChildHandle
509 )
510 {
511 DHCP_SERVICE *DhcpSb;
512 DHCP_PROTOCOL *Instance;
513 EFI_STATUS Status;
514 EFI_TPL OldTpl;
515 VOID *Udp4;
516
517 if ((This == NULL) || (ChildHandle == NULL)) {
518 return EFI_INVALID_PARAMETER;
519 }
520
521 Instance = AllocatePool (sizeof (*Instance));
522
523 if (Instance == NULL) {
524 return EFI_OUT_OF_RESOURCES;
525 }
526
527 DhcpSb = DHCP_SERVICE_FROM_THIS (This);
528 DhcpInitProtocol (DhcpSb, Instance);
529
530 //
531 // Install DHCP4 onto ChildHandle
532 //
533 Status = gBS->InstallMultipleProtocolInterfaces (
534 ChildHandle,
535 &gEfiDhcp4ProtocolGuid,
536 &Instance->Dhcp4Protocol,
537 NULL
538 );
539
540 if (EFI_ERROR (Status)) {
541 FreePool (Instance);
542 return Status;
543 }
544
545 Instance->Handle = *ChildHandle;
546
547 //
548 // Open the Udp4 protocol BY_CHILD.
549 //
550 Status = gBS->OpenProtocol (
551 DhcpSb->UdpIo->UdpHandle,
552 &gEfiUdp4ProtocolGuid,
553 (VOID **) &Udp4,
554 gDhcp4DriverBinding.DriverBindingHandle,
555 Instance->Handle,
556 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
557 );
558 if (EFI_ERROR (Status)) {
559 gBS->UninstallMultipleProtocolInterfaces (
560 Instance->Handle,
561 &gEfiDhcp4ProtocolGuid,
562 &Instance->Dhcp4Protocol,
563 NULL
564 );
565
566 FreePool (Instance);
567 return Status;
568 }
569
570 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
571
572 InsertTailList (&DhcpSb->Children, &Instance->Link);
573 DhcpSb->NumChildren++;
574
575 gBS->RestoreTPL (OldTpl);
576
577 return EFI_SUCCESS;
578 }
579
580
581 /**
582 Destroys a child handle with a protocol installed on it.
583
584 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
585 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
586 last protocol on ChildHandle, then ChildHandle is destroyed.
587
588 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
589 @param ChildHandle Handle of the child to destroy
590
591 @retval EFI_SUCCES The protocol was removed from ChildHandle.
592 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
593 @retval EFI_INVALID_PARAMETER Child handle is not a valid UEFI Handle.
594 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
595 because its services are being used.
596 @retval other The child handle was not destroyed
597
598 **/
599 EFI_STATUS
600 EFIAPI
601 Dhcp4ServiceBindingDestroyChild (
602 IN EFI_SERVICE_BINDING_PROTOCOL *This,
603 IN EFI_HANDLE ChildHandle
604 )
605 {
606 DHCP_SERVICE *DhcpSb;
607 DHCP_PROTOCOL *Instance;
608 EFI_DHCP4_PROTOCOL *Dhcp;
609 EFI_TPL OldTpl;
610 EFI_STATUS Status;
611
612 if ((This == NULL) || (ChildHandle == NULL)) {
613 return EFI_INVALID_PARAMETER;
614 }
615
616 //
617 // Retrieve the private context data structures
618 //
619 Status = gBS->OpenProtocol (
620 ChildHandle,
621 &gEfiDhcp4ProtocolGuid,
622 (VOID **) &Dhcp,
623 gDhcp4DriverBinding.DriverBindingHandle,
624 ChildHandle,
625 EFI_OPEN_PROTOCOL_GET_PROTOCOL
626 );
627
628 if (EFI_ERROR (Status)) {
629 return EFI_UNSUPPORTED;
630 }
631
632 Instance = DHCP_INSTANCE_FROM_THIS (Dhcp);
633 DhcpSb = DHCP_SERVICE_FROM_THIS (This);
634
635 if (Instance->Service != DhcpSb) {
636 return EFI_INVALID_PARAMETER;
637 }
638
639 //
640 // A child can be destroyed more than once. For example,
641 // Dhcp4DriverBindingStop will destroy all of its children.
642 // when caller driver is being stopped, it will destory the
643 // dhcp child it opens.
644 //
645 if (Instance->InDestory) {
646 return EFI_SUCCESS;
647 }
648
649 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
650 Instance->InDestory = TRUE;
651
652 //
653 // Close the Udp4 protocol.
654 //
655 gBS->CloseProtocol (
656 DhcpSb->UdpIo->UdpHandle,
657 &gEfiUdp4ProtocolGuid,
658 gDhcp4DriverBinding.DriverBindingHandle,
659 ChildHandle
660 );
661
662 //
663 // Uninstall the DHCP4 protocol first to enable a top down destruction.
664 //
665 Status = gBS->UninstallProtocolInterface (
666 ChildHandle,
667 &gEfiDhcp4ProtocolGuid,
668 Dhcp
669 );
670
671 if (EFI_ERROR (Status)) {
672 Instance->InDestory = FALSE;
673
674 gBS->RestoreTPL (OldTpl);
675 return Status;
676 }
677
678 if (DhcpSb->ActiveChild == Instance) {
679 DhcpYieldControl (DhcpSb);
680 }
681
682 RemoveEntryList (&Instance->Link);
683 DhcpSb->NumChildren--;
684
685 gBS->RestoreTPL (OldTpl);
686
687 FreePool (Instance);
688 return EFI_SUCCESS;
689 }