2 Implement the connection to the socket driver
4 Copyright (c) 2011, Intel Corporation. All rights reserved.
5 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Library/DebugLib.h>
12 #include <Library/UefiBootServicesTableLib.h>
13 #include <Library/UefiLib.h>
15 #include <Protocol/EfiSocket.h>
16 #include <Protocol/ServiceBinding.h>
20 Free the socket resources
22 This releases the socket resources allocated by calling
23 EslServiceGetProtocol.
25 This routine is called from the ::close routine in BsdSocketLib
26 to release the socket resources.
28 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL
31 @return Value for ::errno, zero (0) indicates success.
35 EslServiceFreeProtocol (
36 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
39 EFI_SERVICE_BINDING_PROTOCOL
* pServiceBinding
;
49 // Locate the socket protocol
51 Status
= gBS
->LocateProtocol ( &gEfiSocketServiceBindingProtocolGuid
,
53 (VOID
**) &pServiceBinding
);
54 if ( !EFI_ERROR ( Status
)) {
58 Status
= pServiceBinding
->DestroyChild ( pServiceBinding
,
59 pSocketProtocol
->SocketHandle
);
61 if ( EFI_ERROR ( Status
)) {
66 // Return the operation status
73 Connect to the EFI socket library
75 This routine establishes a connection to the socket driver
76 and returns the API (::EFI_SOCKET_PROTOCOL address) to the
77 socket file system layer in BsdSocketLib. This routine looks for
78 the gEfiSocketServiceBindingProtocolGuid to locate the socket
79 driver. This routine then creates a child handle and locates
80 the gEfiSocketProtocolGuid protocol on that handle to get the
81 ::EFI_SOCKET_PROTOCOL structure address.
83 This routine is called from the ::socket routine in BsdSocketLib
84 to create the data structure and initialize the API for a socket.
85 Note that this implementation is only used by socket applications
86 that link directly to UseSocketDxe.
88 @param [in] ppSocketProtocol Address to receive the ::EFI_SOCKET_PROTOCOL
91 @return Value for ::errno, zero (0) indicates success.
95 EslServiceGetProtocol (
96 IN EFI_SOCKET_PROTOCOL
** ppSocketProtocol
99 EFI_SERVICE_BINDING_PROTOCOL
* pServiceBinding
;
101 EFI_HANDLE SocketHandle
;
105 // Locate the socket protocol
107 Status
= gBS
->LocateProtocol ( &gEfiSocketServiceBindingProtocolGuid
,
109 (VOID
**)&pServiceBinding
);
110 if ( !EFI_ERROR ( Status
)) {
112 // Create a new socket
115 Status
= pServiceBinding
->CreateChild ( pServiceBinding
,
117 if ( !EFI_ERROR ( Status
)) {
119 // Get the socket protocol
121 Status
= gBS
->OpenProtocol ( SocketHandle
,
122 &gEfiSocketProtocolGuid
,
123 (VOID
**)ppSocketProtocol
,
126 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
);
127 if ( !EFI_ERROR ( Status
)) {
134 DEBUG (( DEBUG_ERROR
,
135 "ERROR - No socket protocol on 0x%08x, Status: %r\r\n",
143 // Translate the error
145 DEBUG (( DEBUG_ERROR
,
146 "ERROR - CreateChild failed, Status: %r\r\n",
153 case EFI_ACCESS_DENIED
:
154 case EFI_WRITE_PROTECTED
:
158 case EFI_NO_RESPONSE
:
159 RetVal
= EHOSTUNREACH
;
162 case EFI_BAD_BUFFER_SIZE
:
163 case EFI_BUFFER_TOO_SMALL
:
164 case EFI_INVALID_PARAMETER
:
168 case EFI_DEVICE_ERROR
:
169 case EFI_MEDIA_CHANGED
:
171 case EFI_VOLUME_CORRUPTED
:
180 case EFI_OUT_OF_RESOURCES
:
184 case EFI_VOLUME_FULL
:
188 case EFI_UNSUPPORTED
:
205 RetVal
= EWOULDBLOCK
;
211 DEBUG (( DEBUG_ERROR
,
212 "ERROR - Socket driver not loaded, Status: %r\r\n",
218 // Return the operation status