]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/EfiSocketLib/UseEfiSocketLib.c
b0e8ef671a2575eaff6452aaf01c3abfbfa38b03
[mirror_edk2.git] / StdLib / EfiSocketLib / UseEfiSocketLib.c
1 /** @file
2 Implement the connection to the EFI socket library
3
4 Copyright (c) 2011, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include <socket.h>
16
17
18 CONST EFI_GUID mEslRawServiceGuid = {
19 0xc31bf4a5, 0x2c7, 0x49d2, { 0xa5, 0x58, 0xfe, 0x62, 0x6f, 0x7e, 0xd4, 0x77 }
20 };
21
22 CONST EFI_GUID mEslTcp4ServiceGuid = {
23 0xffc659c2, 0x4ef2, 0x4532, { 0xb8, 0x75, 0xcd, 0x9a, 0xa4, 0x27, 0x4c, 0xde }
24 };
25
26 CONST EFI_GUID mEslUdp4ServiceGuid = {
27 0x44e03a55, 0x8d97, 0x4511, { 0xbf, 0xef, 0xa, 0x8b, 0xc6, 0x2c, 0x25, 0xae }
28 };
29
30
31 /**
32 Connect to the EFI socket library
33
34 @param [in] ppSocketProtocol Address to receive the socket protocol address
35
36 @retval 0 Successfully returned the socket protocol
37 @retval other Value for errno
38 **/
39 int
40 EslServiceGetProtocol (
41 IN EFI_SOCKET_PROTOCOL ** ppSocketProtocol
42 )
43 {
44 EFI_HANDLE ChildHandle;
45 DT_SOCKET * pSocket;
46 int RetVal;
47 EFI_STATUS Status;
48
49 DBG_ENTER ( );
50
51 //
52 // Assume success
53 //
54 RetVal = 0;
55
56 //
57 // Locate the socket protocol
58 //
59 ChildHandle = NULL;
60 Status = EslSocketAllocate ( &ChildHandle,
61 DEBUG_SOCKET,
62 &pSocket );
63 if ( !EFI_ERROR ( Status )) {
64 *ppSocketProtocol = &pSocket->SocketProtocol;
65 }
66 else {
67 //
68 // No resources
69 //
70 RetVal = ENOMEM;
71 }
72
73 //
74 // Return the operation status
75 //
76 DBG_EXIT_DEC ( RetVal );
77 return RetVal;
78 }
79
80
81 /**
82 Connect to the network layer
83
84 @retval EFI_SUCCESS Successfully connected to the network layer
85
86 **/
87 EFI_STATUS
88 EslServiceNetworkConnect (
89 VOID
90 )
91 {
92 UINTN HandleCount;
93 EFI_HANDLE * pHandles;
94 UINTN Index;
95 CONST DT_SOCKET_BINDING * pSocketBinding;
96 CONST DT_SOCKET_BINDING * pEnd;
97 EFI_STATUS Status;
98
99 DBG_ENTER ( );
100
101 //
102 // Initialize the socket layer
103 //
104 Status = EFI_SUCCESS;
105 EslServiceLoad ( gImageHandle );
106
107 //
108 // Connect the network devices
109 //
110 pSocketBinding = &cEslSocketBinding [0];
111 pEnd = &pSocketBinding [ cEslSocketBindingEntries ];
112 while ( pEnd > pSocketBinding ) {
113 //
114 // Attempt to locate the network adapters
115 //
116 HandleCount = 0;
117 pHandles = NULL;
118 Status = gBS->LocateHandleBuffer ( ByProtocol,
119 pSocketBinding->pNetworkBinding,
120 NULL,
121 &HandleCount,
122 &pHandles );
123 if ( EFI_ERROR ( Status )) {
124 break;
125 }
126 if ( NULL != pHandles ) {
127 //
128 // Attempt to connect to this network adapter
129 //
130 for ( Index = 0; HandleCount > Index; Index++ ) {
131 Status = EslServiceConnect ( gImageHandle,
132 pHandles [ Index ]);
133 if ( EFI_ERROR ( Status )) {
134 break;
135 }
136 }
137
138 //
139 // Done with the handles
140 //
141 gBS->FreePool ( pHandles );
142 }
143
144 //
145 // Set the next network protocol
146 //
147 pSocketBinding += 1;
148 }
149
150 //
151 // Return the network connection status
152 //
153 DBG_EXIT_STATUS ( Status );
154 return Status;
155 }
156
157
158 /**
159 Disconnect from the network layer
160
161 @retval EFI_SUCCESS Successfully disconnected from the network layer
162
163 **/
164 EFI_STATUS
165 EslServiceNetworkDisconnect (
166 VOID
167 )
168 {
169 UINTN HandleCount;
170 EFI_HANDLE * pHandles;
171 UINTN Index;
172 CONST DT_SOCKET_BINDING * pSocketBinding;
173 CONST DT_SOCKET_BINDING * pEnd;
174 EFI_STATUS Status;
175
176 DBG_ENTER ( );
177
178 //
179 // Assume success
180 //
181 Status = EFI_SUCCESS;
182
183 //
184 // Disconnect the network devices
185 //
186 pSocketBinding = &cEslSocketBinding [0];
187 pEnd = &pSocketBinding [ cEslSocketBindingEntries ];
188 while ( pEnd > pSocketBinding ) {
189 //
190 // Attempt to locate the network adapters
191 //
192 HandleCount = 0;
193 pHandles = NULL;
194 Status = gBS->LocateHandleBuffer ( ByProtocol,
195 pSocketBinding->pNetworkBinding,
196 NULL,
197 &HandleCount,
198 &pHandles );
199 if ( EFI_ERROR ( Status )) {
200 break;
201 }
202 if ( NULL != pHandles ) {
203 //
204 // Attempt to disconnect from this network adapter
205 //
206 for ( Index = 0; HandleCount > Index; Index++ ) {
207 Status = EslServiceDisconnect ( gImageHandle,
208 pHandles [ Index ]);
209 if ( EFI_ERROR ( Status )) {
210 break;
211 }
212 }
213
214 //
215 // Done with the handles
216 //
217 gBS->FreePool ( pHandles );
218 }
219
220 //
221 // Set the next network protocol
222 //
223 pSocketBinding += 1;
224 }
225
226 //
227 // Finish the disconnect operation
228 //
229 if ( !EFI_ERROR ( Status )) {
230 EslServiceUnload ( );
231 }
232
233 //
234 // Return the network connection status
235 //
236 DBG_EXIT_STATUS ( Status );
237 return Status;
238 }
239
240
241 PFN_ESL_xSTRUCTOR mpfnEslConstructor = EslServiceNetworkConnect;
242 PFN_ESL_xSTRUCTOR mpfnEslDestructor = EslServiceNetworkDisconnect;