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