]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/UseSocketDxe/UseSocketDxe.c
Add Socket Libraries.
[mirror_edk2.git] / StdLib / UseSocketDxe / UseSocketDxe.c
1 /** @file
2 Implement the connection to the socket driver
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 <Uefi.h>
16
17 #include <Library/DebugLib.h>
18 #include <Library/UefiBootServicesTableLib.h>
19 #include <Library/UefiLib.h>
20
21 #include <Protocol/EfiSocket.h>
22 #include <Protocol/ServiceBinding.h>
23
24
25 /**
26 Connect to the socket driver
27
28 @param [in] ppSocketProtocol Address to receive the socket protocol address
29
30 @retval 0 Successfully returned the socket protocol
31 @retval other Value for errno
32 **/
33 int
34 EslServiceGetProtocol (
35 IN EFI_SOCKET_PROTOCOL ** ppSocketProtocol
36 )
37 {
38 EFI_SERVICE_BINDING_PROTOCOL * pServiceBinding;
39 int RetVal;
40 EFI_HANDLE SocketHandle;
41 EFI_STATUS Status;
42
43 //
44 // Locate the socket protocol
45 //
46 Status = gBS->LocateProtocol ( &gEfiSocketServiceBindingProtocolGuid,
47 NULL,
48 (VOID **)&pServiceBinding );
49 if ( !EFI_ERROR ( Status )) {
50 //
51 // Create a new socket
52 //
53 SocketHandle = NULL;
54 Status = pServiceBinding->CreateChild ( pServiceBinding,
55 &SocketHandle );
56 if ( !EFI_ERROR ( Status )) {
57 //
58 // Get the socket protocol
59 //
60 Status = gBS->OpenProtocol ( SocketHandle,
61 &gEfiSocketProtocolGuid,
62 (VOID **)ppSocketProtocol,
63 NULL,
64 NULL,
65 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL );
66 if ( !EFI_ERROR ( Status )) {
67 //
68 // Success!
69 //
70 RetVal = 0;
71 }
72 else {
73 DEBUG (( DEBUG_ERROR,
74 "ERROR - No socket protocol on 0x%08x, Status: %r\r\n",
75 SocketHandle,
76 Status ));
77 RetVal = ENODEV;
78 }
79 }
80 else {
81 //
82 // Translate the error
83 //
84 DEBUG (( DEBUG_ERROR,
85 "ERROR - CreateChild failed, Status: %r\r\n",
86 Status ));
87 switch ( Status ) {
88 case EFI_SUCCESS:
89 RetVal = 0;
90 break;
91
92 case EFI_ACCESS_DENIED:
93 case EFI_WRITE_PROTECTED:
94 RetVal = EACCES;
95 break;
96
97 case EFI_NO_RESPONSE:
98 RetVal = EHOSTUNREACH;
99 break;
100
101 case EFI_BAD_BUFFER_SIZE:
102 case EFI_BUFFER_TOO_SMALL:
103 case EFI_INVALID_PARAMETER:
104 RetVal = EINVAL;
105 break;
106
107 case EFI_DEVICE_ERROR:
108 case EFI_MEDIA_CHANGED:
109 case EFI_NO_MEDIA:
110 case EFI_VOLUME_CORRUPTED:
111 RetVal = EIO;
112 break;
113
114 case EFI_NOT_FOUND:
115 RetVal = ENOENT;
116 break;
117
118 default:
119 case EFI_OUT_OF_RESOURCES:
120 RetVal = ENOMEM;
121 break;
122
123 case EFI_VOLUME_FULL:
124 RetVal = ENOSPC;
125 break;
126
127 case EFI_UNSUPPORTED:
128 RetVal = ENOSYS;
129 break;
130
131 case EFI_NO_MAPPING:
132 RetVal = ENXIO;
133 break;
134
135 case EFI_LOAD_ERROR:
136 RetVal = ESRCH;
137 break;
138
139 case EFI_TIMEOUT:
140 RetVal = ETIMEDOUT;
141 break;
142
143 case EFI_NOT_READY:
144 RetVal = EWOULDBLOCK;
145 break;
146 }
147 }
148 }
149 else {
150 DEBUG (( DEBUG_ERROR,
151 "ERROR - No socket service binding protocol, Status: %r\r\n",
152 Status ));
153 RetVal = ENODEV;
154 }
155
156 //
157 // Return the operation status
158 //
159 return RetVal;
160 }