]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/UseSocketDxe/UseSocketDxe.c
EmbeddedPkg: Extend NvVarStoreFormattedLib LIBRARY_CLASS
[mirror_edk2.git] / StdLib / UseSocketDxe / UseSocketDxe.c
1 /** @file
2 Implement the connection to the socket driver
3
4 Copyright (c) 2011, Intel Corporation. All rights reserved.
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <Uefi.h>
10
11 #include <Library/DebugLib.h>
12 #include <Library/UefiBootServicesTableLib.h>
13 #include <Library/UefiLib.h>
14
15 #include <Protocol/EfiSocket.h>
16 #include <Protocol/ServiceBinding.h>
17
18
19 /**
20 Free the socket resources
21
22 This releases the socket resources allocated by calling
23 EslServiceGetProtocol.
24
25 This routine is called from the ::close routine in BsdSocketLib
26 to release the socket resources.
27
28 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL
29 structure
30
31 @return Value for ::errno, zero (0) indicates success.
32
33 **/
34 int
35 EslServiceFreeProtocol (
36 IN EFI_SOCKET_PROTOCOL * pSocketProtocol
37 )
38 {
39 EFI_SERVICE_BINDING_PROTOCOL * pServiceBinding;
40 int RetVal;
41 EFI_STATUS Status;
42
43 //
44 // Assume success
45 //
46 RetVal = 0;
47
48 //
49 // Locate the socket protocol
50 //
51 Status = gBS->LocateProtocol ( &gEfiSocketServiceBindingProtocolGuid,
52 NULL,
53 (VOID **) &pServiceBinding );
54 if ( !EFI_ERROR ( Status )) {
55 //
56 // Release the handle
57 //
58 Status = pServiceBinding->DestroyChild ( pServiceBinding,
59 pSocketProtocol->SocketHandle );
60 }
61 if ( EFI_ERROR ( Status )) {
62 RetVal = EIO;
63 }
64
65 //
66 // Return the operation status
67 //
68 return RetVal;
69 }
70
71
72 /**
73 Connect to the EFI socket library
74
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.
82
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.
87
88 @param [in] ppSocketProtocol Address to receive the ::EFI_SOCKET_PROTOCOL
89 structure address
90
91 @return Value for ::errno, zero (0) indicates success.
92
93 **/
94 int
95 EslServiceGetProtocol (
96 IN EFI_SOCKET_PROTOCOL ** ppSocketProtocol
97 )
98 {
99 EFI_SERVICE_BINDING_PROTOCOL * pServiceBinding;
100 int RetVal;
101 EFI_HANDLE SocketHandle;
102 EFI_STATUS Status;
103
104 //
105 // Locate the socket protocol
106 //
107 Status = gBS->LocateProtocol ( &gEfiSocketServiceBindingProtocolGuid,
108 NULL,
109 (VOID **)&pServiceBinding );
110 if ( !EFI_ERROR ( Status )) {
111 //
112 // Create a new socket
113 //
114 SocketHandle = NULL;
115 Status = pServiceBinding->CreateChild ( pServiceBinding,
116 &SocketHandle );
117 if ( !EFI_ERROR ( Status )) {
118 //
119 // Get the socket protocol
120 //
121 Status = gBS->OpenProtocol ( SocketHandle,
122 &gEfiSocketProtocolGuid,
123 (VOID **)ppSocketProtocol,
124 NULL,
125 NULL,
126 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL );
127 if ( !EFI_ERROR ( Status )) {
128 //
129 // Success!
130 //
131 RetVal = 0;
132 }
133 else {
134 DEBUG (( DEBUG_ERROR,
135 "ERROR - No socket protocol on 0x%08x, Status: %r\r\n",
136 SocketHandle,
137 Status ));
138 RetVal = ENODEV;
139 }
140 }
141 else {
142 //
143 // Translate the error
144 //
145 DEBUG (( DEBUG_ERROR,
146 "ERROR - CreateChild failed, Status: %r\r\n",
147 Status ));
148 switch ( Status ) {
149 case EFI_SUCCESS:
150 RetVal = 0;
151 break;
152
153 case EFI_ACCESS_DENIED:
154 case EFI_WRITE_PROTECTED:
155 RetVal = EACCES;
156 break;
157
158 case EFI_NO_RESPONSE:
159 RetVal = EHOSTUNREACH;
160 break;
161
162 case EFI_BAD_BUFFER_SIZE:
163 case EFI_BUFFER_TOO_SMALL:
164 case EFI_INVALID_PARAMETER:
165 RetVal = EINVAL;
166 break;
167
168 case EFI_DEVICE_ERROR:
169 case EFI_MEDIA_CHANGED:
170 case EFI_NO_MEDIA:
171 case EFI_VOLUME_CORRUPTED:
172 RetVal = EIO;
173 break;
174
175 case EFI_NOT_FOUND:
176 RetVal = ENOENT;
177 break;
178
179 default:
180 case EFI_OUT_OF_RESOURCES:
181 RetVal = ENOMEM;
182 break;
183
184 case EFI_VOLUME_FULL:
185 RetVal = ENOSPC;
186 break;
187
188 case EFI_UNSUPPORTED:
189 RetVal = ENOSYS;
190 break;
191
192 case EFI_NO_MAPPING:
193 RetVal = ENXIO;
194 break;
195
196 case EFI_LOAD_ERROR:
197 RetVal = ESRCH;
198 break;
199
200 case EFI_TIMEOUT:
201 RetVal = ETIMEDOUT;
202 break;
203
204 case EFI_NOT_READY:
205 RetVal = EWOULDBLOCK;
206 break;
207 }
208 }
209 }
210 else {
211 DEBUG (( DEBUG_ERROR,
212 "ERROR - Socket driver not loaded, Status: %r\r\n",
213 Status ));
214 RetVal = ENODEV;
215 }
216
217 //
218 // Return the operation status
219 //
220 return RetVal;
221 }