]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/EfiSocketLib/UseEfiSocketLib.c
StdLib: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / StdLib / EfiSocketLib / UseEfiSocketLib.c
CommitLineData
d7ce7006 1/** @file\r
2 Implement the connection to the EFI socket library\r
3\r
0a2530ea
MK
4 Copyright (c) 2011, Intel Corporation. All rights reserved.\r
5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
d7ce7006 6\r
7**/\r
8\r
eb222aea 9#include "Socket.h"\r
d7ce7006 10\r
11\r
a88c3163 12/**\r
13 The following GUID values are only used when an application links\r
14 against EfiSocketLib. An alternative set of values exists in\r
15 SocketDxe\EntryUnload.c which the SocketDxe driver uses to coexist\r
16 with socket applications.\r
17 \r
18 Tag GUID - IPv4 in use by an application using EfiSocketLib\r
19**/\r
18757e70 20CONST EFI_GUID mEslIp4ServiceGuid __attribute__((weak)) = {\r
a88c3163 21 0x9c756011, 0x5d44, 0x4ee0, { 0xbc, 0xe7, 0xc3, 0x82, 0x18, 0xfe, 0x39, 0x8d }\r
d7ce7006 22};\r
23\r
a88c3163 24\r
3bdf9aae 25/**\r
26 Tag GUID - IPv6 in use by an application using EfiSocketLib\r
27**/\r
18757e70 28CONST EFI_GUID mEslIp6ServiceGuid __attribute__((weak)) = {\r
3bdf9aae 29 0xc51b2761, 0xc476, 0x45fe, { 0xbe, 0x61, 0xba, 0x4b, 0xcc, 0x32, 0xf2, 0x34 }\r
30};\r
31\r
32\r
a88c3163 33/**\r
34 Tag GUID - TCPv4 in use by an application using EfiSocketLib\r
35**/\r
18757e70 36CONST EFI_GUID mEslTcp4ServiceGuid __attribute__((weak)) = {\r
d7ce7006 37 0xffc659c2, 0x4ef2, 0x4532, { 0xb8, 0x75, 0xcd, 0x9a, 0xa4, 0x27, 0x4c, 0xde }\r
38};\r
39\r
a88c3163 40\r
3bdf9aae 41/**\r
42 Tag GUID - TCPv6 in use by an application using EfiSocketLib\r
43**/\r
18757e70 44CONST EFI_GUID mEslTcp6ServiceGuid __attribute__((weak)) = {\r
3bdf9aae 45 0x279858a4, 0x4e9e, 0x4e53, { 0x93, 0x22, 0xf2, 0x54, 0xe0, 0x7e, 0xef, 0xd4 }\r
46};\r
47\r
48\r
a88c3163 49/**\r
50 Tag GUID - UDPv4 in use by an application using EfiSocketLib\r
51**/\r
18757e70 52CONST EFI_GUID mEslUdp4ServiceGuid __attribute__((weak)) = {\r
d7ce7006 53 0x44e03a55, 0x8d97, 0x4511, { 0xbf, 0xef, 0xa, 0x8b, 0xc6, 0x2c, 0x25, 0xae }\r
54};\r
55\r
56\r
3bdf9aae 57/**\r
58 Tag GUID - UDPv6 in use by an application using EfiSocketLib\r
59**/\r
18757e70 60CONST EFI_GUID mEslUdp6ServiceGuid __attribute__((weak)) = {\r
3bdf9aae 61 0xaa4af677, 0x6efe, 0x477c, { 0x96, 0x68, 0xe8, 0x13, 0x9d, 0x2, 0xfd, 0x9b }\r
62};\r
63\r
64\r
4652be0c 65/**\r
66 Free the socket resources\r
67\r
68 This releases the socket resources allocated by calling\r
69 EslServiceGetProtocol.\r
70\r
71 This routine is called from the ::close routine in BsdSocketLib\r
72 to release the socket resources.\r
73\r
74 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL\r
75 structure\r
76\r
77 @return Value for ::errno, zero (0) indicates success.\r
78\r
79 **/\r
80int\r
81EslServiceFreeProtocol (\r
82 IN EFI_SOCKET_PROTOCOL * pSocketProtocol\r
83 )\r
84{\r
85 int RetVal;\r
86\r
87 //\r
88 // Release the socket resources\r
89 //\r
90 EslSocketFree ( pSocketProtocol, &RetVal );\r
91\r
92 //\r
93 // Return the operation status\r
94 //\r
95 return RetVal;\r
96}\r
97\r
98\r
d7ce7006 99/**\r
100 Connect to the EFI socket library\r
101\r
a88c3163 102 This routine creates the ::ESL_SOCKET structure and returns\r
103 the API (::EFI_SOCKET_PROTOCOL address) to the socket file\r
104 system layer in BsdSocketLib.\r
105\r
106 This routine is called from the ::socket routine in BsdSocketLib\r
107 to create the data structure and initialize the API for a socket.\r
108 Note that this implementation is only used by socket applications\r
109 that link directly to EslSocketLib.\r
110\r
111 @param [in] ppSocketProtocol Address to receive the ::EFI_SOCKET_PROTOCOL\r
112 structure address\r
113\r
114 @return Value for ::errno, zero (0) indicates success.\r
d7ce7006 115\r
d7ce7006 116 **/\r
117int\r
118EslServiceGetProtocol (\r
119 IN EFI_SOCKET_PROTOCOL ** ppSocketProtocol\r
120 )\r
121{\r
122 EFI_HANDLE ChildHandle;\r
a88c3163 123 ESL_SOCKET * pSocket;\r
d7ce7006 124 int RetVal;\r
125 EFI_STATUS Status;\r
126\r
127 DBG_ENTER ( );\r
128\r
129 //\r
130 // Assume success\r
131 //\r
132 RetVal = 0;\r
133\r
134 //\r
135 // Locate the socket protocol\r
136 //\r
137 ChildHandle = NULL;\r
138 Status = EslSocketAllocate ( &ChildHandle,\r
139 DEBUG_SOCKET,\r
140 &pSocket );\r
141 if ( !EFI_ERROR ( Status )) {\r
142 *ppSocketProtocol = &pSocket->SocketProtocol;\r
143 }\r
144 else {\r
145 //\r
146 // No resources\r
147 //\r
148 RetVal = ENOMEM;\r
149 }\r
150\r
151 //\r
152 // Return the operation status\r
153 //\r
154 DBG_EXIT_DEC ( RetVal );\r
155 return RetVal;\r
156}\r
157\r
158\r
159/**\r
160 Connect to the network layer\r
161\r
a88c3163 162 This routine is the constructor for the EfiSocketLib when the\r
163 library is linked directly to an application. This routine\r
164 walks the ::cEslSocketBinding table to create ::ESL_SERVICE\r
165 structures, associated with the network adapters, which this\r
166 routine links to the ::ESL_LAYER structure.\r
167\r
168 This routine is called from ::EslConstructor as a result of the\r
169 constructor redirection in ::mpfnEslConstructor at the end of this\r
170 file.\r
171\r
d7ce7006 172 @retval EFI_SUCCESS Successfully connected to the network layer\r
173\r
174 **/\r
175EFI_STATUS\r
176EslServiceNetworkConnect (\r
177 VOID\r
178 )\r
179{\r
a88c3163 180 BOOLEAN bSomethingFound;\r
d7ce7006 181 UINTN HandleCount;\r
d7ce7006 182 UINTN Index;\r
a88c3163 183 CONST ESL_SOCKET_BINDING * pEnd;\r
184 EFI_HANDLE * pHandles;\r
185 CONST ESL_SOCKET_BINDING * pSocketBinding;\r
d7ce7006 186 EFI_STATUS Status;\r
187\r
188 DBG_ENTER ( );\r
189\r
190 //\r
191 // Initialize the socket layer\r
192 //\r
193 Status = EFI_SUCCESS;\r
a88c3163 194 bSomethingFound = FALSE;\r
d7ce7006 195 EslServiceLoad ( gImageHandle );\r
196\r
197 //\r
198 // Connect the network devices\r
199 //\r
a88c3163 200 pSocketBinding = &cEslSocketBinding[0];\r
201 pEnd = &pSocketBinding[ cEslSocketBindingEntries ];\r
d7ce7006 202 while ( pEnd > pSocketBinding ) {\r
203 //\r
204 // Attempt to locate the network adapters\r
205 //\r
206 HandleCount = 0;\r
207 pHandles = NULL;\r
208 Status = gBS->LocateHandleBuffer ( ByProtocol,\r
209 pSocketBinding->pNetworkBinding,\r
210 NULL,\r
211 &HandleCount,\r
212 &pHandles );\r
213 if ( EFI_ERROR ( Status )) {\r
a88c3163 214 DEBUG (( DEBUG_ERROR,\r
215 "ERROR with %s layer, Status: %r\r\n",\r
216 pSocketBinding->pName,\r
217 Status ));\r
d7ce7006 218 }\r
a88c3163 219 else {\r
220 if ( NULL != pHandles ) {\r
221 //\r
222 // Attempt to connect to this network adapter\r
223 //\r
224 for ( Index = 0; HandleCount > Index; Index++ ) {\r
225 Status = EslServiceConnect ( gImageHandle,\r
226 pHandles[ Index ]);\r
441f48f5 227 if ( !EFI_ERROR ( Status )) {\r
228 bSomethingFound = TRUE;\r
229 }\r
230 else {\r
231 if ( EFI_OUT_OF_RESOURCES == Status ) {\r
232 //\r
233 // Pointless to continue without memory\r
234 //\r
235 break;\r
236 }\r
a88c3163 237 }\r
d7ce7006 238 }\r
d7ce7006 239\r
a88c3163 240 //\r
241 // Done with the handles\r
242 //\r
243 gBS->FreePool ( pHandles );\r
244 }\r
d7ce7006 245 }\r
246\r
247 //\r
248 // Set the next network protocol\r
249 //\r
250 pSocketBinding += 1;\r
251 }\r
252\r
253 //\r
254 // Return the network connection status\r
255 //\r
a88c3163 256 if ( bSomethingFound ) {\r
257 Status = EFI_SUCCESS;\r
258 }\r
d7ce7006 259 DBG_EXIT_STATUS ( Status );\r
260 return Status;\r
261}\r
262\r
263\r
264/**\r
265 Disconnect from the network layer\r
266\r
a88c3163 267 Destructor for the EfiSocketLib when the library is linked\r
268 directly to an application. This routine walks the\r
269 ::cEslSocketBinding table to remove the ::ESL_SERVICE\r
270 structures (network connections) from the ::ESL_LAYER structure.\r
271\r
272 This routine is called from ::EslDestructor as a result of the\r
273 destructor redirection in ::mpfnEslDestructor at the end of this\r
274 file.\r
275\r
d7ce7006 276 @retval EFI_SUCCESS Successfully disconnected from the network layer\r
277\r
278 **/\r
279EFI_STATUS\r
280EslServiceNetworkDisconnect (\r
281 VOID\r
282 )\r
283{\r
284 UINTN HandleCount;\r
d7ce7006 285 UINTN Index;\r
a88c3163 286 CONST ESL_SOCKET_BINDING * pEnd;\r
287 EFI_HANDLE * pHandles;\r
288 CONST ESL_SOCKET_BINDING * pSocketBinding;\r
d7ce7006 289 EFI_STATUS Status;\r
290\r
291 DBG_ENTER ( );\r
292\r
293 //\r
294 // Assume success\r
295 //\r
296 Status = EFI_SUCCESS;\r
297\r
298 //\r
299 // Disconnect the network devices\r
300 //\r
a88c3163 301 pSocketBinding = &cEslSocketBinding[0];\r
302 pEnd = &pSocketBinding[ cEslSocketBindingEntries ];\r
d7ce7006 303 while ( pEnd > pSocketBinding ) {\r
304 //\r
305 // Attempt to locate the network adapters\r
306 //\r
307 HandleCount = 0;\r
308 pHandles = NULL;\r
309 Status = gBS->LocateHandleBuffer ( ByProtocol,\r
310 pSocketBinding->pNetworkBinding,\r
311 NULL,\r
312 &HandleCount,\r
313 &pHandles );\r
3bdf9aae 314 if (( !EFI_ERROR ( Status ))\r
315 && ( NULL != pHandles )) {\r
d7ce7006 316 //\r
317 // Attempt to disconnect from this network adapter\r
318 //\r
319 for ( Index = 0; HandleCount > Index; Index++ ) {\r
320 Status = EslServiceDisconnect ( gImageHandle,\r
a88c3163 321 pHandles[ Index ]);\r
d7ce7006 322 if ( EFI_ERROR ( Status )) {\r
323 break;\r
324 }\r
325 }\r
326\r
327 //\r
328 // Done with the handles\r
329 //\r
330 gBS->FreePool ( pHandles );\r
331 }\r
332\r
333 //\r
334 // Set the next network protocol\r
335 //\r
336 pSocketBinding += 1;\r
3bdf9aae 337 Status = EFI_SUCCESS;\r
d7ce7006 338 }\r
339\r
340 //\r
341 // Finish the disconnect operation\r
342 //\r
343 if ( !EFI_ERROR ( Status )) {\r
344 EslServiceUnload ( );\r
345 }\r
346\r
347 //\r
348 // Return the network connection status\r
349 //\r
350 DBG_EXIT_STATUS ( Status );\r
351 return Status;\r
352}\r
353\r
354\r
a88c3163 355/**\r
356 Socket layer's service binding protocol delcaration.\r
357**/\r
18757e70 358CONST EFI_SERVICE_BINDING_PROTOCOL mEfiServiceBinding __attribute__((weak)) = {\r
a88c3163 359 NULL,\r
360 NULL\r
361};\r
362\r
363\r
364/**\r
365 The following entries redirect the constructor and destructor\r
366 for any socket application that links against the EfiSocketLib.\r
367 Note that the SocketDxe driver uses different redirection.\r
368**/\r
18757e70 369PFN_ESL_xSTRUCTOR mpfnEslConstructor __attribute__((weak)) = EslServiceNetworkConnect; ///< Constructor for EfiSocketLib\r
370PFN_ESL_xSTRUCTOR mpfnEslDestructor __attribute__((weak)) = EslServiceNetworkDisconnect; ///< Destructor for EfiSocketLib\r