summaryrefslogtreecommitdiff
path: root/StdLib/SocketDxe/EntryUnload.c
blob: 1f550c95b358ff2c6ec56f35761c495de89b6f22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/** @file
  Implement the entry and unload for the socket driver.

  Copyright (c) 2011, Intel Corporation
  All rights reserved. This program and the accompanying materials
  are licensed and made available under the terms and conditions of the BSD License
  which accompanies this distribution.  The full text of the license may be found at
  http://opensource.org/licenses/bsd-license.php

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#include "Socket.h"


/**
  The following GUID values are only used by the SocketDxe driver.  An
  alternative set of values exists in EfiSocketLib\UseEfiSocketLib.c
  which an application uses when it links against EfiSocketLib.  These
  two sets of values allow the SocketDxe driver to coexist with socket
  applications.

  Tag GUID - IPv4 in use by SocketDxe
**/
CONST EFI_GUID mEslIp4ServiceGuid = {
  0x4e3a82e6, 0xe43f, 0x460a, { 0x86, 0x6e, 0x9b, 0x5a, 0xab, 0x80, 0x44, 0x48 }
};


/**
  Tag GUID - TCPv4 in use by SocketDxe
**/
CONST EFI_GUID mEslTcp4ServiceGuid = {
    0x4dcaab0a, 0x1990, 0x4352, { 0x8d, 0x2f, 0x2d, 0x8f, 0x13, 0x55, 0x98, 0xa5 }
};


/**
  Tag GUID - UDPv4 in use by SocketDxe
**/
CONST EFI_GUID mEslUdp4ServiceGuid = {
    0x43a110ce, 0x9ccd, 0x402b, { 0x8c, 0x29, 0x4a, 0x6d, 0x8a, 0xf7, 0x79, 0x90 }
};


/**
  Socket driver unload routine.

  @param [in] ImageHandle       Handle for the image.

  @retval EFI_SUCCESS           Image may be unloaded

**/
EFI_STATUS
EFIAPI
DriverUnload (
  IN EFI_HANDLE ImageHandle
  )
{
  UINTN BufferSize;
  UINTN Index;
  UINTN Max;
  EFI_HANDLE * pHandle;
  EFI_STATUS Status;

  //
  //  Determine which devices are using this driver
  //
  BufferSize = 0;
  pHandle = NULL;
  Status = gBS->LocateHandle (
                  ByProtocol,
                  &gEfiCallerIdGuid,
                  NULL,
                  &BufferSize,
                  NULL );
  if ( EFI_BUFFER_TOO_SMALL == Status ) {
    for ( ; ; ) {
      //
      //  One or more block IO devices are present
      //
      Status = gBS->AllocatePool (
                      EfiRuntimeServicesData,
                      BufferSize,
                      (VOID **) &pHandle
                      );
      if ( EFI_ERROR ( Status )) {
        DEBUG (( DEBUG_ERROR | DEBUG_POOL | DEBUG_INIT | DEBUG_INFO,
                  "Insufficient memory, failed handle buffer allocation\r\n" ));
        break;
      }

      //
      //  Locate the block IO devices
      //
      Status = gBS->LocateHandle (
                      ByProtocol,
                      &gEfiCallerIdGuid,
                      NULL,
                      &BufferSize,
                      pHandle );
      if ( EFI_ERROR ( Status )) {
        //
        //  Error getting handles
        //
        DEBUG (( DEBUG_ERROR | DEBUG_INIT | DEBUG_INFO,
                "Failure getting Telnet handles\r\n" ));
        break;
      }
      
      //
      //  Remove any use of the driver
      //
      Max = BufferSize / sizeof ( pHandle[ 0 ]);
      for ( Index = 0; Max > Index; Index++ ) {
        Status = DriverStop ( &mDriverBinding,
                              pHandle[ Index ],
                              0,
                              NULL );
        if ( EFI_ERROR ( Status )) {
          DEBUG (( DEBUG_WARN | DEBUG_INIT | DEBUG_INFO,
                    "WARNING - Failed to shutdown the driver on handle %08x\r\n", pHandle[ Index ]));
          break;
        }
      }
      break;
    }
  }
  else {
    if ( EFI_NOT_FOUND == Status ) {
      //
      //  No devices were found
      //
      Status = EFI_SUCCESS;
    }
  }

  //
  //  Free the handle array
  //
  if ( NULL != pHandle ) {
    gBS->FreePool ( pHandle );
  }

  //
  //  Done with the socket layer
  //
  if ( !EFI_ERROR ( Status )) {
    Status = EslDxeUninstall ( ImageHandle );
    if ( !EFI_ERROR ( Status )) {
      //
      //  Remove the protocols installed by the EntryPoint routine.
      //
      Status = gBS->UninstallMultipleProtocolInterfaces (
                  ImageHandle,
                  &gEfiDriverBindingProtocolGuid,
                  &mDriverBinding,
                  &gEfiComponentNameProtocolGuid,
                  &mComponentName,
                  &gEfiComponentName2ProtocolGuid,
                  &mComponentName2,
                  NULL
                  );
      if ( !EFI_ERROR ( Status )) {
        DEBUG (( DEBUG_POOL | DEBUG_INIT | DEBUG_INFO,
                "Removed:   gEfiComponentName2ProtocolGuid from 0x%08x\r\n",
                ImageHandle ));
        DEBUG (( DEBUG_POOL | DEBUG_INIT | DEBUG_INFO,
                  "Removed:   gEfiComponentNameProtocolGuid from 0x%08x\r\n",
                  ImageHandle ));
        DEBUG (( DEBUG_POOL | DEBUG_INIT | DEBUG_INFO,
                  "Removed:   gEfiDriverBindingProtocolGuid from 0x%08x\r\n",
                  ImageHandle ));
      }
      else {
        DEBUG (( DEBUG_ERROR | DEBUG_POOL | DEBUG_INIT,
                    "ERROR - Failed to remove gEfiDriverBindingProtocolGuid from 0x%08x, Status: %r\r\n",
                    ImageHandle,
                    Status ));
      }
    }
  }

  //
  //  Disconnect the network services
  //
  if ( !EFI_ERROR ( Status )) {
    EslServiceUnload ( );
  }

  //
  //  Return the unload status
  //
  return Status;
}


/**
Socket driver entry point.

@param [in] ImageHandle       Handle for the image.
@param [in] pSystemTable      Address of the system table.

@retval EFI_SUCCESS           Image successfully loaded.

**/
EFI_STATUS
EFIAPI
EntryPoint (
  IN EFI_HANDLE ImageHandle,
  IN EFI_SYSTEM_TABLE * pSystemTable
  )
{
  EFI_LOADED_IMAGE_PROTOCOL * pLoadedImage;
  EFI_STATUS    Status;

  DBG_ENTER ( );

  //
  //  Display the image handle
  //
  DEBUG (( DEBUG_INFO,
            "ImageHandle: 0x%08x\r\n",
            ImageHandle ));

  //
  //  Enable unload support
  //
  Status = gBS->HandleProtocol (
                  gImageHandle,
                  &gEfiLoadedImageProtocolGuid,
                  (VOID **)&pLoadedImage
                  );
  if (!EFI_ERROR (Status)) {
    pLoadedImage->Unload = DriverUnload;

    //
    //  Add the driver to the list of drivers
    //
    Status = EfiLibInstallDriverBindingComponentName2 (
               ImageHandle,
               pSystemTable,
               &mDriverBinding,
               ImageHandle,
               &mComponentName,
               &mComponentName2
               );
    if ( !EFI_ERROR ( Status )) {
      DEBUG (( DEBUG_POOL | DEBUG_INIT | DEBUG_INFO,
                "Installed: gEfiDriverBindingProtocolGuid on   0x%08x\r\n",
                ImageHandle ));
      DEBUG (( DEBUG_POOL | DEBUG_INIT | DEBUG_INFO,
                "Installed: gEfiComponentNameProtocolGuid on   0x%08x\r\n",
                ImageHandle ));
      DEBUG (( DEBUG_POOL | DEBUG_INIT | DEBUG_INFO,
                "Installed: gEfiComponentName2ProtocolGuid on   0x%08x\r\n",
                ImageHandle ));

      //
      //  Initialize the service layer
      //
      EslServiceLoad ( ImageHandle );

      //
      //  Make the socket serivces available to other drivers
      //  and applications
      //
      Status = EslDxeInstall ( &ImageHandle );
      if ( EFI_ERROR ( Status )) {
        //
        //  Disconnect from the network
        //
        EslServiceUnload ( );

        //
        //  Remove the driver bindings
        //
        gBS->UninstallMultipleProtocolInterfaces (
                ImageHandle,
                &gEfiDriverBindingProtocolGuid,
                &mDriverBinding,
                &gEfiComponentNameProtocolGuid,
                &mComponentName,
                &gEfiComponentName2ProtocolGuid,
                &mComponentName2,
                NULL
                );
        DEBUG (( DEBUG_POOL | DEBUG_INIT | DEBUG_INFO,
                "Removed:   gEfiComponentName2ProtocolGuid from 0x%08x\r\n",
                ImageHandle ));
        DEBUG (( DEBUG_POOL | DEBUG_INIT | DEBUG_INFO,
                  "Removed:   gEfiComponentNameProtocolGuid from 0x%08x\r\n",
                  ImageHandle ));
        DEBUG (( DEBUG_POOL | DEBUG_INIT | DEBUG_INFO,
                  "Removed:   gEfiDriverBindingProtocolGuid from 0x%08x\r\n",
                  ImageHandle ));
      }
    }
    else  {
      DEBUG (( DEBUG_ERROR | DEBUG_POOL | DEBUG_INIT,
                "ERROR - EfiLibInstallDriverBindingComponentName2 failed, Status: %r\r\n",
                Status ));
    }
  }
  DBG_EXIT_STATUS ( Status );
  return Status;
}


/**
  Socket layer's service binding protocol delcaration.
**/
CONST EFI_SERVICE_BINDING_PROTOCOL mEfiServiceBinding = {
  EslDxeCreateChild,
  EslDxeDestroyChild
};


/**
  The following entries disable the constructor and destructor
  for the SocketDxe driver.  Note that socket applications linking
  against EfiSocketLib use different redirection.
**/
PFN_ESL_xSTRUCTOR mpfnEslConstructor = NULL;  ///<  No EfiSocketLib constructor needed for SocketDxe
PFN_ESL_xSTRUCTOR mpfnEslDestructor = NULL;   ///<  No EfiSocketLib destructor needed for SocketDxe