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