2 The driver binding and service binding protocol for HttpDxe driver.
4 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "HttpDriver.h"
19 /// Driver Binding Protocol instance
21 EFI_DRIVER_BINDING_PROTOCOL gHttpDxeDriverBinding
= {
22 HttpDxeDriverBindingSupported
,
23 HttpDxeDriverBindingStart
,
24 HttpDxeDriverBindingStop
,
31 Create a HTTP driver service binding private instance.
33 @param[in] Controller The controller that has TCP4 service binding
35 @param[in] ImageHandle The HTTP driver's image handle.
36 @param[out] ServiceData Point to HTTP driver private instance.
38 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resources.
39 @retval EFI_SUCCESS A new HTTP driver private instance is created.
44 IN EFI_HANDLE Controller
,
45 IN EFI_HANDLE ImageHandle
,
46 OUT HTTP_SERVICE
**ServiceData
49 HTTP_SERVICE
*HttpService
;
51 ASSERT (ServiceData
!= NULL
);
54 HttpService
= AllocateZeroPool (sizeof (HTTP_SERVICE
));
55 if (HttpService
== NULL
) {
56 return EFI_OUT_OF_RESOURCES
;
59 HttpService
->Signature
= HTTP_SERVICE_SIGNATURE
;
60 HttpService
->ServiceBinding
.CreateChild
= HttpServiceBindingCreateChild
;
61 HttpService
->ServiceBinding
.DestroyChild
= HttpServiceBindingDestroyChild
;
62 HttpService
->ImageHandle
= ImageHandle
;
63 HttpService
->ControllerHandle
= Controller
;
64 HttpService
->ChildrenNumber
= 0;
65 InitializeListHead (&HttpService
->ChildrenList
);
67 *ServiceData
= HttpService
;
72 Release all the resource used the HTTP service binding instance.
74 @param HttpService The HTTP private instance.
79 IN HTTP_SERVICE
*HttpService
82 if (HttpService
== NULL
) {
86 if (HttpService
->TcpChildHandle
!= NULL
) {
88 HttpService
->TcpChildHandle
,
89 &gEfiTcp4ProtocolGuid
,
90 HttpService
->ImageHandle
,
91 HttpService
->ControllerHandle
94 NetLibDestroyServiceChild (
95 HttpService
->ControllerHandle
,
96 HttpService
->ImageHandle
,
97 &gEfiTcp4ServiceBindingProtocolGuid
,
98 HttpService
->TcpChildHandle
104 This is the declaration of an EFI image entry point. This entry point is
105 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
106 both device drivers and bus drivers.
108 @param ImageHandle The firmware allocated handle for the UEFI image.
109 @param SystemTable A pointer to the EFI System Table.
111 @retval EFI_SUCCESS The operation completed successfully.
112 @retval Others An unexpected error occurred.
117 HttpDxeDriverEntryPoint (
118 IN EFI_HANDLE ImageHandle
,
119 IN EFI_SYSTEM_TABLE
*SystemTable
123 // Install UEFI Driver Model protocol(s).
125 return EfiLibInstallDriverBindingComponentName2 (
128 &gHttpDxeDriverBinding
,
130 &gHttpDxeComponentName
,
131 &gHttpDxeComponentName2
136 Callback function which provided by user to remove one node in NetDestroyLinkList process.
138 @param[in] Entry The entry to be removed.
139 @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList.
141 @retval EFI_INVALID_PARAMETER Any input parameter is NULL.
142 @retval EFI_SUCCESS The entry has been removed successfully.
143 @retval Others Fail to remove the entry.
148 HttpDestroyChildEntryInHandleBuffer (
149 IN LIST_ENTRY
*Entry
,
153 HTTP_PROTOCOL
*HttpInstance
;
154 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
155 UINTN NumberOfChildren
;
156 EFI_HANDLE
*ChildHandleBuffer
;
158 if (Entry
== NULL
|| Context
== NULL
) {
159 return EFI_INVALID_PARAMETER
;
162 HttpInstance
= NET_LIST_USER_STRUCT_S (Entry
, HTTP_PROTOCOL
, Link
, HTTP_PROTOCOL_SIGNATURE
);
163 ServiceBinding
= ((HTTP_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT
*) Context
)->ServiceBinding
;
164 NumberOfChildren
= ((HTTP_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT
*) Context
)->NumberOfChildren
;
165 ChildHandleBuffer
= ((HTTP_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT
*) Context
)->ChildHandleBuffer
;
167 if (!NetIsInHandleBuffer (HttpInstance
->Handle
, NumberOfChildren
, ChildHandleBuffer
)) {
171 return ServiceBinding
->DestroyChild (ServiceBinding
, HttpInstance
->Handle
);
175 Tests to see if this driver supports a given controller. If a child device is provided,
176 it further tests to see if this driver supports creating a handle for the specified child device.
178 This function checks to see if the driver specified by This supports the device specified by
179 ControllerHandle. Drivers will typically use the device path attached to
180 ControllerHandle and/or the services from the bus I/O abstraction attached to
181 ControllerHandle to determine if the driver supports ControllerHandle. This function
182 may be called many times during platform initialization. In order to reduce boot times, the tests
183 performed by this function must be very small, and take as little time as possible to execute. This
184 function must not change the state of any hardware devices, and this function must be aware that the
185 device specified by ControllerHandle may already be managed by the same driver or a
186 different driver. This function must match its calls to AllocatePages() with FreePages(),
187 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
188 Because ControllerHandle may have been previously started by the same driver, if a protocol is
189 already in the opened state, then it must not be closed with CloseProtocol(). This is required
190 to guarantee the state of ControllerHandle is not modified by this function.
192 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
193 @param[in] ControllerHandle The handle of the controller to test. This handle
194 must support a protocol interface that supplies
195 an I/O abstraction to the driver.
196 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
197 parameter is ignored by device drivers, and is optional for bus
198 drivers. For bus drivers, if this parameter is not NULL, then
199 the bus driver must determine if the bus controller specified
200 by ControllerHandle and the child controller specified
201 by RemainingDevicePath are both supported by this
204 @retval EFI_SUCCESS The device specified by ControllerHandle and
205 RemainingDevicePath is supported by the driver specified by This.
206 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
207 RemainingDevicePath is already being managed by the driver
209 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
210 RemainingDevicePath is already being managed by a different
211 driver or an application that requires exclusive access.
212 Currently not implemented.
213 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
214 RemainingDevicePath is not supported by the driver specified by This.
218 HttpDxeDriverBindingSupported (
219 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
220 IN EFI_HANDLE ControllerHandle
,
221 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
227 // Test for the HttpServiceBinding protocol.
229 Status
= gBS
->OpenProtocol (
231 &gEfiHttpServiceBindingProtocolGuid
,
233 This
->DriverBindingHandle
,
235 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
237 if (!EFI_ERROR (Status
)) {
238 return EFI_ALREADY_STARTED
;
242 // Test for the Tcp4 Protocol
244 return gBS
->OpenProtocol (
246 &gEfiTcp4ServiceBindingProtocolGuid
,
248 This
->DriverBindingHandle
,
250 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
256 Starts a device controller or a bus controller.
258 The Start() function is designed to be invoked from the EFI boot service ConnectController().
259 As a result, much of the error checking on the parameters to Start() has been moved into this
260 common boot service. It is legal to call Start() from other locations,
261 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
262 1. ControllerHandle must be a valid EFI_HANDLE.
263 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
264 EFI_DEVICE_PATH_PROTOCOL.
265 3. Prior to calling Start(), the Supported() function for the driver specified by This must
266 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
268 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
269 @param[in] ControllerHandle The handle of the controller to start. This handle
270 must support a protocol interface that supplies
271 an I/O abstraction to the driver.
272 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
273 parameter is ignored by device drivers, and is optional for bus
274 drivers. For a bus driver, if this parameter is NULL, then handles
275 for all the children of Controller are created by this driver.
276 If this parameter is not NULL and the first Device Path Node is
277 not the End of Device Path Node, then only the handle for the
278 child device specified by the first Device Path Node of
279 RemainingDevicePath is created by this driver.
280 If the first Device Path Node of RemainingDevicePath is
281 the End of Device Path Node, no child handle is created by this
284 @retval EFI_SUCCESS The device was started.
285 @retval EFI_ALREADY_STARTED This device is already running on ControllerHandle.
286 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
287 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
288 @retval Others The driver failded to start the device.
293 HttpDxeDriverBindingStart (
294 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
295 IN EFI_HANDLE ControllerHandle
,
296 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
300 HTTP_SERVICE
*HttpService
;
304 // Test for the Http service binding protocol
306 Status
= gBS
->OpenProtocol (
308 &gEfiHttpServiceBindingProtocolGuid
,
310 This
->DriverBindingHandle
,
312 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
315 if (Status
== EFI_SUCCESS
) {
316 return EFI_ALREADY_STARTED
;
319 Status
= HttpCreateService (ControllerHandle
, This
->DriverBindingHandle
, &HttpService
);
320 if (EFI_ERROR (Status
)) {
324 ASSERT (HttpService
!= NULL
);
327 // Create a TCP child instance, but do not configure it. This will establish the parent-child relationship.
329 Status
= NetLibCreateServiceChild (
331 This
->DriverBindingHandle
,
332 &gEfiTcp4ServiceBindingProtocolGuid
,
333 &HttpService
->TcpChildHandle
336 if (EFI_ERROR (Status
)) {
340 Status
= gBS
->OpenProtocol (
341 HttpService
->TcpChildHandle
,
342 &gEfiTcp4ProtocolGuid
,
344 This
->DriverBindingHandle
,
346 EFI_OPEN_PROTOCOL_BY_DRIVER
349 if (EFI_ERROR (Status
)) {
354 // Install the HttpServiceBinding Protocol onto Controller
356 Status
= gBS
->InstallMultipleProtocolInterfaces (
358 &gEfiHttpServiceBindingProtocolGuid
,
359 &HttpService
->ServiceBinding
,
363 if (EFI_ERROR (Status
)) {
371 if (HttpService
!= NULL
) {
372 HttpCleanService (HttpService
);
373 FreePool (HttpService
);
380 Stops a device controller or a bus controller.
382 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
383 As a result, much of the error checking on the parameters to Stop() has been moved
384 into this common boot service. It is legal to call Stop() from other locations,
385 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
386 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
387 same driver's Start() function.
388 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
389 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
390 Start() function, and the Start() function must have called OpenProtocol() on
391 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
393 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
394 @param[in] ControllerHandle A handle to the device being stopped. The handle must
395 support a bus specific I/O protocol for the driver
396 to use to stop the device.
397 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
398 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
399 if NumberOfChildren is 0.
401 @retval EFI_SUCCESS The device was stopped.
402 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
407 HttpDxeDriverBindingStop (
408 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
409 IN EFI_HANDLE ControllerHandle
,
410 IN UINTN NumberOfChildren
,
411 IN EFI_HANDLE
*ChildHandleBuffer OPTIONAL
414 EFI_HANDLE NicHandle
;
416 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
417 HTTP_SERVICE
*HttpService
;
419 HTTP_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT Context
;
422 // HTTP driver opens TCP child, So, Controller is a TCP
423 // child handle. Locate the Nic handle first. Then get the
424 // HTTP private data back.
426 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiTcp4ProtocolGuid
);
427 if (NicHandle
== NULL
) {
431 Status
= gBS
->OpenProtocol (
433 &gEfiHttpServiceBindingProtocolGuid
,
434 (VOID
**) &ServiceBinding
,
435 This
->DriverBindingHandle
,
437 EFI_OPEN_PROTOCOL_GET_PROTOCOL
440 if (EFI_ERROR (Status
)) {
441 return EFI_DEVICE_ERROR
;
444 HttpService
= HTTP_SERVICE_FROM_PROTOCOL (ServiceBinding
);
446 if (!IsListEmpty (&HttpService
->ChildrenList
)) {
448 // Destroy the HTTP child instance in ChildHandleBuffer.
450 List
= &HttpService
->ChildrenList
;
451 Context
.ServiceBinding
= ServiceBinding
;
452 Context
.NumberOfChildren
= NumberOfChildren
;
453 Context
.ChildHandleBuffer
= ChildHandleBuffer
;
454 Status
= NetDestroyLinkList (
456 HttpDestroyChildEntryInHandleBuffer
,
462 if (NumberOfChildren
== 0 && IsListEmpty (&HttpService
->ChildrenList
)) {
463 gBS
->UninstallProtocolInterface (
465 &gEfiHttpServiceBindingProtocolGuid
,
469 HttpCleanService (HttpService
);
471 FreePool (HttpService
);
473 Status
= EFI_SUCCESS
;
480 Creates a child handle and installs a protocol.
482 The CreateChild() function installs a protocol on ChildHandle.
483 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
484 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
486 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
487 @param ChildHandle Pointer to the handle of the child to create. If it is NULL,
488 then a new handle is created. If it is a pointer to an existing UEFI handle,
489 then the protocol is added to the existing UEFI handle.
491 @retval EFI_SUCCES The protocol was added to ChildHandle.
492 @retval EFI_INVALID_PARAMETER This is NULL, or ChildHandle is NULL.
493 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
495 @retval other The child handle was not created.
500 HttpServiceBindingCreateChild (
501 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
502 IN OUT EFI_HANDLE
*ChildHandle
505 HTTP_SERVICE
*HttpService
;
506 HTTP_PROTOCOL
*HttpInstance
;
511 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
512 return EFI_INVALID_PARAMETER
;
515 HttpService
= HTTP_SERVICE_FROM_PROTOCOL (This
);
516 HttpInstance
= AllocateZeroPool (sizeof (HTTP_PROTOCOL
));
517 if (HttpInstance
== NULL
) {
518 return EFI_OUT_OF_RESOURCES
;
522 // Install HTTP protocol onto ChildHandle
524 Status
= gBS
->InstallMultipleProtocolInterfaces (
526 &gEfiHttpProtocolGuid
,
531 if (EFI_ERROR (Status
)) {
535 HttpInstance
->Handle
= *ChildHandle
;
537 Status
= HttpInitProtocol (HttpService
, HttpInstance
);
538 if (EFI_ERROR (Status
)) {
543 // Open the default Tcp4 protocol by child.
545 Status
= gBS
->OpenProtocol (
546 HttpService
->TcpChildHandle
,
547 &gEfiTcp4ProtocolGuid
,
548 (VOID
**) &Interface
,
549 gHttpDxeDriverBinding
.DriverBindingHandle
,
550 HttpInstance
->Handle
,
551 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
553 if (EFI_ERROR (Status
)) {
558 // Add it to the HTTP service's child list.
560 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
562 InsertTailList (&HttpService
->ChildrenList
, &HttpInstance
->Link
);
563 HttpService
->ChildrenNumber
++;
565 gBS
->RestoreTPL (OldTpl
);
571 HttpCleanProtocol (HttpInstance
);
572 FreePool (HttpInstance
);
578 Destroys a child handle with a protocol installed on it.
580 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
581 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
582 last protocol on ChildHandle, then ChildHandle is destroyed.
584 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
585 @param ChildHandle Handle of the child to destroy
587 @retval EFI_SUCCES The protocol was removed from ChildHandle.
588 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
589 @retval EFI_INVALID_PARAMETER Child handle is NULL.
590 @retval other The child handle was not destroyed
595 HttpServiceBindingDestroyChild (
596 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
597 IN EFI_HANDLE ChildHandle
600 HTTP_SERVICE
*HttpService
;
601 HTTP_PROTOCOL
*HttpInstance
;
602 EFI_HTTP_PROTOCOL
*Http
;
606 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
607 return EFI_INVALID_PARAMETER
;
610 HttpService
= HTTP_SERVICE_FROM_PROTOCOL (This
);
611 Status
= gBS
->OpenProtocol (
613 &gEfiHttpProtocolGuid
,
615 gHttpDxeDriverBinding
.DriverBindingHandle
,
617 EFI_OPEN_PROTOCOL_GET_PROTOCOL
619 if (EFI_ERROR (Status
)) {
620 return EFI_UNSUPPORTED
;
623 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (Http
);
624 if (HttpInstance
->Service
!= HttpService
) {
625 return EFI_INVALID_PARAMETER
;
628 if (HttpInstance
->InDestroy
) {
633 // Close the Tcp4 protocol.
636 HttpService
->TcpChildHandle
,
637 &gEfiTcp4ProtocolGuid
,
638 gHttpDxeDriverBinding
.DriverBindingHandle
,
642 HttpInstance
->InDestroy
= TRUE
;
645 // Uninstall the HTTP protocol.
647 Status
= gBS
->UninstallProtocolInterface (
649 &gEfiHttpProtocolGuid
,
653 if (EFI_ERROR (Status
)) {
654 HttpInstance
->InDestroy
= FALSE
;
658 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
660 HttpCleanProtocol (HttpInstance
);
662 RemoveEntryList (&HttpInstance
->Link
);
663 HttpService
->ChildrenNumber
--;
665 gBS
->RestoreTPL (OldTpl
);
667 FreePool (HttpInstance
);