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"
18 EFI_HTTP_UTILITIES_PROTOCOL
*mHttpUtilities
= NULL
;
21 /// Driver Binding Protocol instance
23 EFI_DRIVER_BINDING_PROTOCOL gHttpDxeDriverBinding
= {
24 HttpDxeDriverBindingSupported
,
25 HttpDxeDriverBindingStart
,
26 HttpDxeDriverBindingStop
,
33 Create a HTTP driver service binding private instance.
35 @param[in] Controller The controller that has TCP4 service binding
37 @param[in] ImageHandle The HTTP driver's image handle.
38 @param[out] ServiceData Point to HTTP driver private instance.
40 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resources.
41 @retval EFI_SUCCESS A new HTTP driver private instance is created.
46 IN EFI_HANDLE Controller
,
47 IN EFI_HANDLE ImageHandle
,
48 OUT HTTP_SERVICE
**ServiceData
51 HTTP_SERVICE
*HttpService
;
53 ASSERT (ServiceData
!= NULL
);
56 HttpService
= AllocateZeroPool (sizeof (HTTP_SERVICE
));
57 if (HttpService
== NULL
) {
58 return EFI_OUT_OF_RESOURCES
;
61 HttpService
->Signature
= HTTP_SERVICE_SIGNATURE
;
62 HttpService
->ServiceBinding
.CreateChild
= HttpServiceBindingCreateChild
;
63 HttpService
->ServiceBinding
.DestroyChild
= HttpServiceBindingDestroyChild
;
64 HttpService
->ImageHandle
= ImageHandle
;
65 HttpService
->ControllerHandle
= Controller
;
66 HttpService
->ChildrenNumber
= 0;
67 InitializeListHead (&HttpService
->ChildrenList
);
69 *ServiceData
= HttpService
;
74 Release all the resource used the HTTP service binding instance.
76 @param HttpService The HTTP private instance.
81 IN HTTP_SERVICE
*HttpService
84 if (HttpService
== NULL
) {
88 if (HttpService
->TcpChildHandle
!= NULL
) {
90 HttpService
->TcpChildHandle
,
91 &gEfiTcp4ProtocolGuid
,
92 HttpService
->ImageHandle
,
93 HttpService
->ControllerHandle
96 NetLibDestroyServiceChild (
97 HttpService
->ControllerHandle
,
98 HttpService
->ImageHandle
,
99 &gEfiTcp4ServiceBindingProtocolGuid
,
100 HttpService
->TcpChildHandle
106 The event process routine when the http utilities protocol is installed
109 @param[in] Event Not used.
110 @param[in] Context The pointer to the IP4 config2 instance data.
115 HttpUtilitiesInstalledCallback (
120 gBS
->LocateProtocol (
121 &gEfiHttpUtilitiesProtocolGuid
,
123 (VOID
**) &mHttpUtilities
127 // Close the event if Http utilities protocol is loacted.
129 if (mHttpUtilities
!= NULL
&& Event
!= NULL
) {
130 gBS
->CloseEvent (Event
);
135 This is the declaration of an EFI image entry point. This entry point is
136 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
137 both device drivers and bus drivers.
139 @param ImageHandle The firmware allocated handle for the UEFI image.
140 @param SystemTable A pointer to the EFI System Table.
142 @retval EFI_SUCCESS The operation completed successfully.
143 @retval Others An unexpected error occurred.
148 HttpDxeDriverEntryPoint (
149 IN EFI_HANDLE ImageHandle
,
150 IN EFI_SYSTEM_TABLE
*SystemTable
155 gBS
->LocateProtocol (
156 &gEfiHttpUtilitiesProtocolGuid
,
158 (VOID
**) &mHttpUtilities
161 if (mHttpUtilities
== NULL
) {
163 // No Http utilities protocol, register a notify.
165 EfiCreateProtocolNotifyEvent (
166 &gEfiHttpUtilitiesProtocolGuid
,
168 HttpUtilitiesInstalledCallback
,
175 // Install UEFI Driver Model protocol(s).
177 return EfiLibInstallDriverBindingComponentName2 (
180 &gHttpDxeDriverBinding
,
182 &gHttpDxeComponentName
,
183 &gHttpDxeComponentName2
188 Callback function which provided by user to remove one node in NetDestroyLinkList process.
190 @param[in] Entry The entry to be removed.
191 @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList.
193 @retval EFI_INVALID_PARAMETER Any input parameter is NULL.
194 @retval EFI_SUCCESS The entry has been removed successfully.
195 @retval Others Fail to remove the entry.
200 HttpDestroyChildEntryInHandleBuffer (
201 IN LIST_ENTRY
*Entry
,
205 HTTP_PROTOCOL
*HttpInstance
;
206 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
207 UINTN NumberOfChildren
;
208 EFI_HANDLE
*ChildHandleBuffer
;
210 if (Entry
== NULL
|| Context
== NULL
) {
211 return EFI_INVALID_PARAMETER
;
214 HttpInstance
= NET_LIST_USER_STRUCT_S (Entry
, HTTP_PROTOCOL
, Link
, HTTP_PROTOCOL_SIGNATURE
);
215 ServiceBinding
= ((HTTP_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT
*) Context
)->ServiceBinding
;
216 NumberOfChildren
= ((HTTP_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT
*) Context
)->NumberOfChildren
;
217 ChildHandleBuffer
= ((HTTP_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT
*) Context
)->ChildHandleBuffer
;
219 if (!NetIsInHandleBuffer (HttpInstance
->Handle
, NumberOfChildren
, ChildHandleBuffer
)) {
223 return ServiceBinding
->DestroyChild (ServiceBinding
, HttpInstance
->Handle
);
227 Tests to see if this driver supports a given controller. If a child device is provided,
228 it further tests to see if this driver supports creating a handle for the specified child device.
230 This function checks to see if the driver specified by This supports the device specified by
231 ControllerHandle. Drivers will typically use the device path attached to
232 ControllerHandle and/or the services from the bus I/O abstraction attached to
233 ControllerHandle to determine if the driver supports ControllerHandle. This function
234 may be called many times during platform initialization. In order to reduce boot times, the tests
235 performed by this function must be very small, and take as little time as possible to execute. This
236 function must not change the state of any hardware devices, and this function must be aware that the
237 device specified by ControllerHandle may already be managed by the same driver or a
238 different driver. This function must match its calls to AllocatePages() with FreePages(),
239 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
240 Because ControllerHandle may have been previously started by the same driver, if a protocol is
241 already in the opened state, then it must not be closed with CloseProtocol(). This is required
242 to guarantee the state of ControllerHandle is not modified by this function.
244 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
245 @param[in] ControllerHandle The handle of the controller to test. This handle
246 must support a protocol interface that supplies
247 an I/O abstraction to the driver.
248 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
249 parameter is ignored by device drivers, and is optional for bus
250 drivers. For bus drivers, if this parameter is not NULL, then
251 the bus driver must determine if the bus controller specified
252 by ControllerHandle and the child controller specified
253 by RemainingDevicePath are both supported by this
256 @retval EFI_SUCCESS The device specified by ControllerHandle and
257 RemainingDevicePath is supported by the driver specified by This.
258 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
259 RemainingDevicePath is already being managed by the driver
261 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
262 RemainingDevicePath is already being managed by a different
263 driver or an application that requires exclusive access.
264 Currently not implemented.
265 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
266 RemainingDevicePath is not supported by the driver specified by This.
270 HttpDxeDriverBindingSupported (
271 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
272 IN EFI_HANDLE ControllerHandle
,
273 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
279 // Test for the HttpServiceBinding protocol.
281 Status
= gBS
->OpenProtocol (
283 &gEfiHttpServiceBindingProtocolGuid
,
285 This
->DriverBindingHandle
,
287 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
289 if (!EFI_ERROR (Status
)) {
290 return EFI_ALREADY_STARTED
;
294 // Test for the Tcp4 Protocol
296 return gBS
->OpenProtocol (
298 &gEfiTcp4ServiceBindingProtocolGuid
,
300 This
->DriverBindingHandle
,
302 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
308 Starts a device controller or a bus controller.
310 The Start() function is designed to be invoked from the EFI boot service ConnectController().
311 As a result, much of the error checking on the parameters to Start() has been moved into this
312 common boot service. It is legal to call Start() from other locations,
313 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
314 1. ControllerHandle must be a valid EFI_HANDLE.
315 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
316 EFI_DEVICE_PATH_PROTOCOL.
317 3. Prior to calling Start(), the Supported() function for the driver specified by This must
318 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
320 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
321 @param[in] ControllerHandle The handle of the controller to start. This handle
322 must support a protocol interface that supplies
323 an I/O abstraction to the driver.
324 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
325 parameter is ignored by device drivers, and is optional for bus
326 drivers. For a bus driver, if this parameter is NULL, then handles
327 for all the children of Controller are created by this driver.
328 If this parameter is not NULL and the first Device Path Node is
329 not the End of Device Path Node, then only the handle for the
330 child device specified by the first Device Path Node of
331 RemainingDevicePath is created by this driver.
332 If the first Device Path Node of RemainingDevicePath is
333 the End of Device Path Node, no child handle is created by this
336 @retval EFI_SUCCESS The device was started.
337 @retval EFI_ALREADY_STARTED This device is already running on ControllerHandle.
338 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
339 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
340 @retval Others The driver failded to start the device.
345 HttpDxeDriverBindingStart (
346 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
347 IN EFI_HANDLE ControllerHandle
,
348 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
352 HTTP_SERVICE
*HttpService
;
356 // Test for the Http service binding protocol
358 Status
= gBS
->OpenProtocol (
360 &gEfiHttpServiceBindingProtocolGuid
,
362 This
->DriverBindingHandle
,
364 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
367 if (Status
== EFI_SUCCESS
) {
368 return EFI_ALREADY_STARTED
;
371 Status
= HttpCreateService (ControllerHandle
, This
->DriverBindingHandle
, &HttpService
);
372 if (EFI_ERROR (Status
)) {
376 ASSERT (HttpService
!= NULL
);
379 // Create a TCP child instance, but do not configure it. This will establish the parent-child relationship.
381 Status
= NetLibCreateServiceChild (
383 This
->DriverBindingHandle
,
384 &gEfiTcp4ServiceBindingProtocolGuid
,
385 &HttpService
->TcpChildHandle
388 if (EFI_ERROR (Status
)) {
392 Status
= gBS
->OpenProtocol (
393 HttpService
->TcpChildHandle
,
394 &gEfiTcp4ProtocolGuid
,
396 This
->DriverBindingHandle
,
398 EFI_OPEN_PROTOCOL_BY_DRIVER
401 if (EFI_ERROR (Status
)) {
406 // Install the HttpServiceBinding Protocol onto Controller
408 Status
= gBS
->InstallMultipleProtocolInterfaces (
410 &gEfiHttpServiceBindingProtocolGuid
,
411 &HttpService
->ServiceBinding
,
415 if (EFI_ERROR (Status
)) {
423 if (HttpService
!= NULL
) {
424 HttpCleanService (HttpService
);
425 FreePool (HttpService
);
432 Stops a device controller or a bus controller.
434 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
435 As a result, much of the error checking on the parameters to Stop() has been moved
436 into this common boot service. It is legal to call Stop() from other locations,
437 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
438 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
439 same driver's Start() function.
440 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
441 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
442 Start() function, and the Start() function must have called OpenProtocol() on
443 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
445 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
446 @param[in] ControllerHandle A handle to the device being stopped. The handle must
447 support a bus specific I/O protocol for the driver
448 to use to stop the device.
449 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
450 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
451 if NumberOfChildren is 0.
453 @retval EFI_SUCCESS The device was stopped.
454 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
459 HttpDxeDriverBindingStop (
460 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
461 IN EFI_HANDLE ControllerHandle
,
462 IN UINTN NumberOfChildren
,
463 IN EFI_HANDLE
*ChildHandleBuffer OPTIONAL
466 EFI_HANDLE NicHandle
;
468 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
469 HTTP_SERVICE
*HttpService
;
471 HTTP_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT Context
;
474 // HTTP driver opens TCP child, So, Controller is a TCP
475 // child handle. Locate the Nic handle first. Then get the
476 // HTTP private data back.
478 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiTcp4ProtocolGuid
);
479 if (NicHandle
== NULL
) {
483 Status
= gBS
->OpenProtocol (
485 &gEfiHttpServiceBindingProtocolGuid
,
486 (VOID
**) &ServiceBinding
,
487 This
->DriverBindingHandle
,
489 EFI_OPEN_PROTOCOL_GET_PROTOCOL
492 if (EFI_ERROR (Status
)) {
493 return EFI_DEVICE_ERROR
;
496 HttpService
= HTTP_SERVICE_FROM_PROTOCOL (ServiceBinding
);
498 if (!IsListEmpty (&HttpService
->ChildrenList
)) {
500 // Destroy the HTTP child instance in ChildHandleBuffer.
502 List
= &HttpService
->ChildrenList
;
503 Context
.ServiceBinding
= ServiceBinding
;
504 Context
.NumberOfChildren
= NumberOfChildren
;
505 Context
.ChildHandleBuffer
= ChildHandleBuffer
;
506 Status
= NetDestroyLinkList (
508 HttpDestroyChildEntryInHandleBuffer
,
514 if (NumberOfChildren
== 0 && IsListEmpty (&HttpService
->ChildrenList
)) {
515 gBS
->UninstallProtocolInterface (
517 &gEfiHttpServiceBindingProtocolGuid
,
521 HttpCleanService (HttpService
);
523 FreePool (HttpService
);
525 Status
= EFI_SUCCESS
;
532 Creates a child handle and installs a protocol.
534 The CreateChild() function installs a protocol on ChildHandle.
535 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
536 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
538 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
539 @param ChildHandle Pointer to the handle of the child to create. If it is NULL,
540 then a new handle is created. If it is a pointer to an existing UEFI handle,
541 then the protocol is added to the existing UEFI handle.
543 @retval EFI_SUCCES The protocol was added to ChildHandle.
544 @retval EFI_INVALID_PARAMETER This is NULL, or ChildHandle is NULL.
545 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
547 @retval other The child handle was not created.
552 HttpServiceBindingCreateChild (
553 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
554 IN OUT EFI_HANDLE
*ChildHandle
557 HTTP_SERVICE
*HttpService
;
558 HTTP_PROTOCOL
*HttpInstance
;
563 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
564 return EFI_INVALID_PARAMETER
;
567 HttpService
= HTTP_SERVICE_FROM_PROTOCOL (This
);
568 HttpInstance
= AllocateZeroPool (sizeof (HTTP_PROTOCOL
));
569 if (HttpInstance
== NULL
) {
570 return EFI_OUT_OF_RESOURCES
;
574 // Install HTTP protocol onto ChildHandle
576 Status
= gBS
->InstallMultipleProtocolInterfaces (
578 &gEfiHttpProtocolGuid
,
583 if (EFI_ERROR (Status
)) {
587 HttpInstance
->Handle
= *ChildHandle
;
589 Status
= HttpInitProtocol (HttpService
, HttpInstance
);
590 if (EFI_ERROR (Status
)) {
595 // Open the default Tcp4 protocol by child.
597 Status
= gBS
->OpenProtocol (
598 HttpService
->TcpChildHandle
,
599 &gEfiTcp4ProtocolGuid
,
600 (VOID
**) &Interface
,
601 gHttpDxeDriverBinding
.DriverBindingHandle
,
602 HttpInstance
->Handle
,
603 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
605 if (EFI_ERROR (Status
)) {
610 // Add it to the HTTP service's child list.
612 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
614 InsertTailList (&HttpService
->ChildrenList
, &HttpInstance
->Link
);
615 HttpService
->ChildrenNumber
++;
617 gBS
->RestoreTPL (OldTpl
);
623 HttpCleanProtocol (HttpInstance
);
624 FreePool (HttpInstance
);
630 Destroys a child handle with a protocol installed on it.
632 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
633 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
634 last protocol on ChildHandle, then ChildHandle is destroyed.
636 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
637 @param ChildHandle Handle of the child to destroy
639 @retval EFI_SUCCES The protocol was removed from ChildHandle.
640 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
641 @retval EFI_INVALID_PARAMETER Child handle is NULL.
642 @retval other The child handle was not destroyed
647 HttpServiceBindingDestroyChild (
648 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
649 IN EFI_HANDLE ChildHandle
652 HTTP_SERVICE
*HttpService
;
653 HTTP_PROTOCOL
*HttpInstance
;
654 EFI_HTTP_PROTOCOL
*Http
;
658 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
659 return EFI_INVALID_PARAMETER
;
662 HttpService
= HTTP_SERVICE_FROM_PROTOCOL (This
);
663 Status
= gBS
->OpenProtocol (
665 &gEfiHttpProtocolGuid
,
667 gHttpDxeDriverBinding
.DriverBindingHandle
,
669 EFI_OPEN_PROTOCOL_GET_PROTOCOL
671 if (EFI_ERROR (Status
)) {
672 return EFI_UNSUPPORTED
;
675 HttpInstance
= HTTP_INSTANCE_FROM_PROTOCOL (Http
);
676 if (HttpInstance
->Service
!= HttpService
) {
677 return EFI_INVALID_PARAMETER
;
680 if (HttpInstance
->InDestroy
) {
685 // Close the Tcp4 protocol.
688 HttpService
->TcpChildHandle
,
689 &gEfiTcp4ProtocolGuid
,
690 gHttpDxeDriverBinding
.DriverBindingHandle
,
694 HttpInstance
->InDestroy
= TRUE
;
697 // Uninstall the HTTP protocol.
699 Status
= gBS
->UninstallProtocolInterface (
701 &gEfiHttpProtocolGuid
,
705 if (EFI_ERROR (Status
)) {
706 HttpInstance
->InDestroy
= FALSE
;
710 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
712 HttpCleanProtocol (HttpInstance
);
714 RemoveEntryList (&HttpInstance
->Link
);
715 HttpService
->ChildrenNumber
--;
717 gBS
->RestoreTPL (OldTpl
);
719 FreePool (HttpInstance
);