summaryrefslogtreecommitdiffstats
path: root/StdLib/UseSocketDxe/UseSocketDxe.c
blob: 16da9c6919ea3e5ee36ebf8d54e32a2bb80297d7 (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
/** @file
  Implement the connection to the socket driver

  Copyright (c) 2011, Intel Corporation. All rights reserved.
  SPDX-License-Identifier: BSD-2-Clause-Patent

**/

#include <Uefi.h>

#include <Library/DebugLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiLib.h>

#include <Protocol/EfiSocket.h>
#include <Protocol/ServiceBinding.h>


/**
  Free the socket resources

  This releases the socket resources allocated by calling
  EslServiceGetProtocol.

  This routine is called from the ::close routine in BsdSocketLib
  to release the socket resources.

  @param [in] pSocketProtocol   Address of an ::EFI_SOCKET_PROTOCOL
                                structure

  @return       Value for ::errno, zero (0) indicates success.

 **/
int
EslServiceFreeProtocol (
  IN EFI_SOCKET_PROTOCOL * pSocketProtocol
  )
{
  EFI_SERVICE_BINDING_PROTOCOL * pServiceBinding;
  int RetVal;
  EFI_STATUS Status;

  //
  //  Assume success
  //
  RetVal = 0;

  //
  //  Locate the socket protocol
  //
  Status = gBS->LocateProtocol ( &gEfiSocketServiceBindingProtocolGuid,
                                 NULL,
                                 (VOID **) &pServiceBinding );
  if ( !EFI_ERROR ( Status )) {
    //
    //  Release the handle
    //
    Status = pServiceBinding->DestroyChild ( pServiceBinding,
                                             pSocketProtocol->SocketHandle );
  }
  if ( EFI_ERROR ( Status )) {
    RetVal = EIO;
  }

  //
  //  Return the operation status
  //
  return RetVal;
}


/**
  Connect to the EFI socket library

  This routine establishes a connection to the socket driver
  and returns the API (::EFI_SOCKET_PROTOCOL address) to the
  socket file system layer in BsdSocketLib.  This routine looks for
  the gEfiSocketServiceBindingProtocolGuid to locate the socket
  driver.  This routine then creates a child handle and locates
  the gEfiSocketProtocolGuid protocol on that handle to get the
  ::EFI_SOCKET_PROTOCOL structure address.

  This routine is called from the ::socket routine in BsdSocketLib
  to create the data structure and initialize the API for a socket.
  Note that this implementation is only used by socket applications
  that link directly to UseSocketDxe.

  @param [in] ppSocketProtocol  Address to receive the ::EFI_SOCKET_PROTOCOL
                                structure address

  @return       Value for ::errno, zero (0) indicates success.

 **/
int
EslServiceGetProtocol (
  IN EFI_SOCKET_PROTOCOL ** ppSocketProtocol
  )
{
  EFI_SERVICE_BINDING_PROTOCOL * pServiceBinding;
  int RetVal;
  EFI_HANDLE SocketHandle;
  EFI_STATUS Status;

  //
  //  Locate the socket protocol
  //
  Status = gBS->LocateProtocol ( &gEfiSocketServiceBindingProtocolGuid,
                                 NULL,
                                 (VOID **)&pServiceBinding );
  if ( !EFI_ERROR ( Status )) {
    //
    //  Create a new socket
    //
    SocketHandle = NULL;
    Status = pServiceBinding->CreateChild ( pServiceBinding,
                                            &SocketHandle );
    if ( !EFI_ERROR ( Status )) {
      //
      //  Get the socket protocol
      //
      Status = gBS->OpenProtocol ( SocketHandle,
                                   &gEfiSocketProtocolGuid,
                                   (VOID **)ppSocketProtocol,
                                   NULL,
                                   NULL,
                                   EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL );
      if ( !EFI_ERROR ( Status )) {
        //
        //  Success!
        //
        RetVal = 0;
      }
      else {
        DEBUG (( DEBUG_ERROR,
                  "ERROR - No socket protocol on 0x%08x, Status: %r\r\n",
                  SocketHandle,
                  Status ));
        RetVal = ENODEV;
      }
    }
    else {
      //
      //  Translate the error
      //
      DEBUG (( DEBUG_ERROR,
                "ERROR - CreateChild failed, Status: %r\r\n",
                Status ));
      switch ( Status ) {
      case EFI_SUCCESS:
        RetVal = 0;
        break;

      case EFI_ACCESS_DENIED:
      case EFI_WRITE_PROTECTED:
        RetVal = EACCES;
        break;

      case EFI_NO_RESPONSE:
        RetVal = EHOSTUNREACH;
        break;

      case EFI_BAD_BUFFER_SIZE:
      case EFI_BUFFER_TOO_SMALL:
      case EFI_INVALID_PARAMETER:
        RetVal = EINVAL;
        break;

      case EFI_DEVICE_ERROR:
      case EFI_MEDIA_CHANGED:
      case EFI_NO_MEDIA:
      case EFI_VOLUME_CORRUPTED:
        RetVal = EIO;
        break;

      case EFI_NOT_FOUND:
        RetVal = ENOENT;
        break;

      default:
      case EFI_OUT_OF_RESOURCES:
        RetVal = ENOMEM;
        break;

      case EFI_VOLUME_FULL:
        RetVal = ENOSPC;
        break;

      case EFI_UNSUPPORTED:
        RetVal = ENOSYS;
        break;

      case EFI_NO_MAPPING:
        RetVal = ENXIO;
        break;

      case EFI_LOAD_ERROR:
        RetVal = ESRCH;
        break;

      case EFI_TIMEOUT:
        RetVal = ETIMEDOUT;
        break;

      case EFI_NOT_READY:
        RetVal = EWOULDBLOCK;
        break;
      }
    }
  }
  else {
    DEBUG (( DEBUG_ERROR,
              "ERROR - Socket driver not loaded, Status: %r\r\n",
              Status ));
    RetVal = ENODEV;
  }

  //
  //  Return the operation status
  //
  return RetVal;
}