2 Definitions for the EFI Socket layer library.
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
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.
15 #ifndef _EFI_SOCKET_LIB_H_
16 #define _EFI_SOCKET_LIB_H_
20 #include <Library/BaseMemoryLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/UefiBootServicesTableLib.h>
23 #include <Library/UefiLib.h>
25 #include <Protocol/EfiSocket.h>
26 #include <Protocol/ServiceBinding.h>
27 #include <Protocol/Tcp4.h>
28 #include <Protocol/Udp4.h>
32 //------------------------------------------------------------------------------
34 //------------------------------------------------------------------------------
36 #define DEBUG_TPL 0x40000000 ///< Display TPL change messages
38 #define TPL_SOCKETS TPL_CALLBACK ///< TPL for routine synchronization
40 //------------------------------------------------------------------------------
42 //------------------------------------------------------------------------------
44 #if defined(_MSC_VER) /* Handle Microsoft VC++ compiler specifics. */
45 #define DBG_ENTER() DEBUG (( DEBUG_INFO, "Entering " __FUNCTION__ "\n" )) ///< Display routine entry
46 #define DBG_EXIT() DEBUG (( DEBUG_INFO, "Exiting " __FUNCTION__ "\n" )) ///< Display routine exit
47 #define DBG_EXIT_DEC(Status) DEBUG (( DEBUG_INFO, "Exiting " __FUNCTION__ ", Status: %d\n", Status )) ///< Display routine exit with decimal value
48 #define DBG_EXIT_HEX(Status) DEBUG (( DEBUG_INFO, "Exiting " __FUNCTION__ ", Status: 0x%08x\n", Status )) ///< Display routine exit with hex value
49 #define DBG_EXIT_STATUS(Status) DEBUG (( DEBUG_INFO, "Exiting " __FUNCTION__ ", Status: %r\n", Status )) ///< Display routine exit with status value
50 #define DBG_EXIT_TF(Status) DEBUG (( DEBUG_INFO, "Exiting " __FUNCTION__ ", returning %s\n", (FALSE == Status) ? L"FALSE" : L"TRUE" )) ///< Display routine with TRUE/FALSE value
54 #define DBG_EXIT_DEC(Status)
55 #define DBG_EXIT_HEX(Status)
56 #define DBG_EXIT_STATUS(Status)
57 #define DBG_EXIT_TF(Status)
60 #define DIM(x) ( sizeof ( x ) / sizeof ( x[0] )) ///< Compute the number of entries in an array
65 This macro which is enabled when debug is enabled verifies that
66 the new TPL value is >= the current TPL value.
72 #if !defined(MDEPKG_NDEBUG)
74 #define VERIFY_TPL(tpl) \
76 EFI_TPL PreviousTpl; \
78 PreviousTpl = gBS->RaiseTPL ( TPL_HIGH_LEVEL ); \
79 gBS->RestoreTPL ( PreviousTpl ); \
80 if ( PreviousTpl > tpl ) { \
81 DEBUG (( DEBUG_ERROR | DEBUG_TPL, \
82 "Current TPL: %d, New TPL: %d\r\n", \
83 PreviousTpl, tpl )); \
84 ASSERT ( PreviousTpl <= tpl ); \
88 #else // MDEPKG_NDEBUG
90 #define VERIFY_TPL(tpl)
92 #endif // MDEPKG_NDEBUG
94 #define RAISE_TPL(PreviousTpl, tpl) \
96 PreviousTpl = gBS->RaiseTPL ( tpl ); \
97 DEBUG (( DEBUG_TPL | DEBUG_TPL, \
101 #define RESTORE_TPL(tpl) \
102 gBS->RestoreTPL ( tpl ); \
103 DEBUG (( DEBUG_TPL | DEBUG_TPL, \
107 //------------------------------------------------------------------------------
109 //------------------------------------------------------------------------------
111 typedef struct _DT_SERVICE DT_SERVICE
; ///< Forward delcaration
115 (EFIAPI
* PFN_SB_INITIALIZE
) (
116 DT_SERVICE
* pService
121 (EFIAPI
* PFN_SB_SHUTDOWN
) (
122 DT_SERVICE
* pService
126 Protocol binding and installation control structure
128 The driver uses this structure to simplify the driver binding processing.
131 CHAR16
* pName
; ///< Protocol name
132 EFI_GUID
* pNetworkBinding
; ///< Network service binding protocol for socket support
133 CONST EFI_GUID
* pTagGuid
; ///< Tag to mark protocol in use
134 PFN_SB_INITIALIZE pfnInitialize
;///< Routine to initialize the service
135 PFN_SB_SHUTDOWN pfnShutdown
; ///< Routine to shutdown the service
138 //------------------------------------------------------------------------------
140 //------------------------------------------------------------------------------
142 extern CONST EFI_GUID mEslRawServiceGuid
;
143 extern CONST EFI_GUID mEslTcp4ServiceGuid
;
144 extern CONST EFI_GUID mEslUdp4ServiceGuid
;
146 //------------------------------------------------------------------------------
148 //------------------------------------------------------------------------------
150 extern CONST DT_SOCKET_BINDING cEslSocketBinding
[];
151 extern CONST UINTN cEslSocketBindingEntries
;
153 //------------------------------------------------------------------------------
154 // Service Support Routines
155 //------------------------------------------------------------------------------
158 Connect to the network service bindings
160 Walk the network service protocols on the controller handle and
161 locate any that are not in use. Create service structures to
162 manage the service binding for the socket driver.
164 @param [in] BindingHandle Handle for protocol binding.
165 @param [in] Controller Handle of device to work with.
167 @retval EFI_SUCCESS This driver is added to Controller.
168 @retval other This driver does not support this device.
174 IN EFI_HANDLE BindingHandle
,
175 IN EFI_HANDLE Controller
179 Shutdown the network connections to this controller by removing
180 NetworkInterfaceIdentifier protocol and closing the DevicePath
181 and PciIo protocols on Controller.
183 @param [in] BindingHandle Handle for protocol binding.
184 @param [in] Controller Handle of device to stop driver on.
186 @retval EFI_SUCCESS This driver is removed Controller.
187 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
188 @retval other This driver was not removed from this device.
193 EslServiceDisconnect (
194 IN EFI_HANDLE BindingHandle
,
195 IN EFI_HANDLE Controller
199 Install the socket service
201 @param [in] pImageHandle Address of the image handle
203 @retval EFI_SUCCESS Service installed successfully
208 IN EFI_HANDLE
* pImageHandle
212 Initialize the service layer
214 @param [in] ImageHandle Handle for the image.
220 IN EFI_HANDLE ImageHandle
224 Uninstall the socket service
226 @param [in] ImageHandle Handle for the image.
228 @retval EFI_SUCCESS Service installed successfully
232 EslServiceUninstall (
233 IN EFI_HANDLE ImageHandle
237 Shutdown the service layer
246 //------------------------------------------------------------------------------
247 // Socket Service Binding Protocol Routines
248 //------------------------------------------------------------------------------
251 Creates a child handle and installs a protocol.
253 The CreateChild() function installs a protocol on ChildHandle.
254 If pChildHandle is a pointer to NULL, then a new handle is created and returned in pChildHandle.
255 If pChildHandle is not a pointer to NULL, then the protocol installs on the existing pChildHandle.
257 @param [in] pThis Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
258 @param [in] pChildHandle Pointer to the handle of the child to create. If it is NULL,
259 then a new handle is created. If it is a pointer to an existing UEFI handle,
260 then the protocol is added to the existing UEFI handle.
262 @retval EFI_SUCCES The protocol was added to ChildHandle.
263 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
264 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
266 @retval other The child handle was not created
271 EslSocketCreateChild (
272 IN EFI_SERVICE_BINDING_PROTOCOL
* pThis
,
273 IN OUT EFI_HANDLE
* pChildHandle
277 Destroys a child handle with a protocol installed on it.
279 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
280 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
281 last protocol on ChildHandle, then ChildHandle is destroyed.
283 @param [in] pThis Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
284 @param [in] ChildHandle Handle of the child to destroy
286 @retval EFI_SUCCES The protocol was removed from ChildHandle.
287 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
288 @retval EFI_INVALID_PARAMETER Child handle is not a valid UEFI Handle.
289 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
290 because its services are being used.
291 @retval other The child handle was not destroyed
296 EslSocketDestroyChild (
297 IN EFI_SERVICE_BINDING_PROTOCOL
* pThis
,
298 IN EFI_HANDLE ChildHandle
301 //------------------------------------------------------------------------------
302 // Socket Protocol Routines
303 //------------------------------------------------------------------------------
306 Bind a name to a socket.
308 The ::SocketBind routine connects a name to a socket on the local machine. The
309 <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/bind.html">POSIX</a>
310 documentation for the bind routine is available online for reference.
312 @param [in] pSocketProtocol Address of the socket protocol structure.
314 @param [in] pSockAddr Address of a sockaddr structure that contains the
315 connection point on the local machine. An IPv4 address
316 of INADDR_ANY specifies that the connection is made to
317 all of the network stacks on the platform. Specifying a
318 specific IPv4 address restricts the connection to the
319 network stack supporting that address. Specifying zero
320 for the port causes the network layer to assign a port
321 number from the dynamic range. Specifying a specific
322 port number causes the network layer to use that port.
324 @param [in] SockAddrLen Specifies the length in bytes of the sockaddr structure.
326 @param [out] pErrno Address to receive the errno value upon completion.
328 @retval EFI_SUCCESS - Socket successfully created
333 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
334 IN
const struct sockaddr
* pSockAddr
,
335 IN socklen_t SockAddrLength
,
340 Determine if the socket is closed
342 Reverses the operations of the ::SocketAllocate() routine.
344 @param [in] pSocketProtocol Address of the socket protocol structure.
345 @param [out] pErrno Address to receive the errno value upon completion.
347 @retval EFI_SUCCESS Socket successfully closed
348 @retval EFI_NOT_READY Close still in progress
349 @retval EFI_ALREADY Close operation already in progress
350 @retval Other Failed to close the socket
355 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
360 Start the close operation on the socket
362 Start closing the socket by closing all of the ports. Upon
363 completion, the ::SocketPoll() routine finishes closing the
366 @param [in] pSocketProtocol Address of the socket protocol structure.
367 @param [in] bCloseNow Boolean to control close behavior
368 @param [out] pErrno Address to receive the errno value upon completion.
370 @retval EFI_SUCCESS Socket successfully closed
371 @retval EFI_NOT_READY Close still in progress
372 @retval EFI_ALREADY Close operation already in progress
373 @retval Other Failed to close the socket
377 EslSocketCloseStart (
378 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
379 IN BOOLEAN bCloseNow
,
384 Connect to a remote system via the network.
386 The ::SocketConnect routine attempts to establish a connection to a
387 socket on the local or remote system using the specified address.
389 <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/connect.html">connect</a>
390 documentation is available online.
392 There are three states associated with a connection:
394 <li>Not connected</li>
395 <li>Connection in progress</li>
398 In the "Not connected" state, calls to ::connect start the connection
399 processing and update the state to "Connection in progress". During
400 the "Connection in progress" state, connect polls for connection completion
401 and moves the state to "Connected" after the connection is established.
402 Note that these states are only visible when the file descriptor is marked
403 with O_NONBLOCK. Also, the POLL_WRITE bit is set when the connection
404 completes and may be used by poll or select as an indicator to call
407 @param [in] pSocketProtocol Address of the socket protocol structure.
409 @param [in] pSockAddr Network address of the remote system.
411 @param [in] SockAddrLength Length in bytes of the network address.
413 @param [out] pErrno Address to receive the errno value upon completion.
415 @retval EFI_SUCCESS The connection was successfully established.
416 @retval EFI_NOT_READY The connection is in progress, call this routine again.
417 @retval Others The connection attempt failed.
422 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
423 IN
const struct sockaddr
* pSockAddr
,
424 IN socklen_t SockAddrLength
,
429 Get the local address.
431 @param [in] pSocketProtocol Address of the socket protocol structure.
433 @param [out] pAddress Network address to receive the local system address
435 @param [in,out] pAddressLength Length of the local network address structure
437 @param [out] pErrno Address to receive the errno value upon completion.
439 @retval EFI_SUCCESS - Local address successfully returned
443 EslSocketGetLocalAddress (
444 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
445 OUT
struct sockaddr
* pAddress
,
446 IN OUT socklen_t
* pAddressLength
,
451 Get the peer address.
453 @param [in] pSocketProtocol Address of the socket protocol structure.
455 @param [out] pAddress Network address to receive the remote system address
457 @param [in,out] pAddressLength Length of the remote network address structure
459 @param [out] pErrno Address to receive the errno value upon completion.
461 @retval EFI_SUCCESS - Remote address successfully returned
465 EslSocketGetPeerAddress (
466 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
467 OUT
struct sockaddr
* pAddress
,
468 IN OUT socklen_t
* pAddressLength
,
473 Establish the known port to listen for network connections.
475 The ::SocketListen routine places the port into a state that enables connection
476 attempts. Connections are placed into FIFO order in a queue to be serviced
477 by the application. The application calls the ::SocketAccept routine to remove
478 the next connection from the queue and get the associated socket. The
479 <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/listen.html">POSIX</a>
480 documentation for the bind routine is available online for reference.
482 @param [in] pSocketProtocol Address of the socket protocol structure.
484 @param [in] Backlog Backlog specifies the maximum FIFO depth for
485 the connections waiting for the application
486 to call accept. Connection attempts received
487 while the queue is full are refused.
489 @param [out] pErrno Address to receive the errno value upon completion.
491 @retval EFI_SUCCESS - Socket successfully created
492 @retval Other - Failed to enable the socket for listen
497 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
503 Get the socket options
505 Retrieve the socket options one at a time by name. The
506 <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/getsockopt.html">POSIX</a>
507 documentation is available online.
509 @param [in] pSocketProtocol Address of the socket protocol structure.
510 @param [in] level Option protocol level
511 @param [in] option_name Name of the option
512 @param [out] option_value Buffer to receive the option value
513 @param [in,out] option_len Length of the buffer in bytes,
514 upon return length of the option value in bytes
515 @param [out] pErrno Address to receive the errno value upon completion.
517 @retval EFI_SUCCESS - Socket data successfully received
522 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
525 OUT
void * __restrict option_value
,
526 IN OUT socklen_t
* __restrict option_len
,
531 Set the socket options
533 Adjust the socket options one at a time by name. The
534 <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/setsockopt.html">POSIX</a>
535 documentation is available online.
537 @param [in] pSocketProtocol Address of the socket protocol structure.
538 @param [in] level Option protocol level
539 @param [in] option_name Name of the option
540 @param [in] option_value Buffer containing the option value
541 @param [in] option_len Length of the buffer in bytes
542 @param [out] pErrno Address to receive the errno value upon completion.
544 @retval EFI_SUCCESS - Socket data successfully received
549 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
552 IN CONST
void * option_value
,
553 IN socklen_t option_len
,
558 Poll a socket for pending activity.
560 The SocketPoll routine checks a socket for pending activity associated
561 with the event mask. Activity is returned in the detected event buffer.
563 @param [in] pSocketProtocol Address of the socket protocol structure.
565 @param [in] Events Events of interest for this socket
567 @param [in] pEvents Address to receive the detected events
569 @param [out] pErrno Address to receive the errno value upon completion.
571 @retval EFI_SUCCESS - Socket successfully polled
572 @retval EFI_INVALID_PARAMETER - When pEvents is NULL
577 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
584 Receive data from a network connection.
587 @param [in] pSocketProtocol Address of the socket protocol structure.
589 @param [in] Flags Message control flags
591 @param [in] BufferLength Length of the the buffer
593 @param [in] pBuffer Address of a buffer to receive the data.
595 @param [in] pDataLength Number of received data bytes in the buffer.
597 @param [out] pAddress Network address to receive the remote system address
599 @param [in,out] pAddressLength Length of the remote network address structure
601 @param [out] pErrno Address to receive the errno value upon completion.
603 @retval EFI_SUCCESS - Socket data successfully received
608 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
610 IN
size_t BufferLength
,
612 OUT
size_t * pDataLength
,
613 OUT
struct sockaddr
* pAddress
,
614 IN OUT socklen_t
* pAddressLength
,
619 Shutdown the socket receive and transmit operations
621 The SocketShutdown routine stops the socket receive and transmit
624 @param [in] pSocketProtocol Address of the socket protocol structure.
626 @param [in] How Which operations to stop
628 @param [out] pErrno Address to receive the errno value upon completion.
630 @retval EFI_SUCCESS - Socket operations successfully shutdown
635 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
641 Send data using a network connection.
643 The SocketTransmit routine queues the data for transmission to the
644 remote network connection.
646 @param [in] pSocketProtocol Address of the socket protocol structure.
648 @param [in] Flags Message control flags
650 @param [in] BufferLength Length of the the buffer
652 @param [in] pBuffer Address of a buffer containing the data to send
654 @param [in] pDataLength Address to receive the number of data bytes sent
656 @param [in] pAddress Network address of the remote system address
658 @param [in] AddressLength Length of the remote network address structure
660 @param [out] pErrno Address to receive the errno value upon completion.
662 @retval EFI_SUCCESS - Socket data successfully received
667 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
669 IN
size_t BufferLength
,
670 IN CONST UINT8
* pBuffer
,
671 OUT
size_t * pDataLength
,
672 IN
const struct sockaddr
* pAddress
,
673 IN socklen_t AddressLength
,
677 //------------------------------------------------------------------------------
679 #endif // _EFI_SOCKET_LIB_H_