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