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/MemoryAllocationLib.h>
23 #include <Library/UefiBootServicesTableLib.h>
24 #include <Library/UefiLib.h>
26 #include <Protocol/EfiSocket.h>
27 #include <Protocol/ServiceBinding.h>
28 #include <Protocol/Tcp4.h>
29 #include <Protocol/Udp4.h>
33 //------------------------------------------------------------------------------
35 //------------------------------------------------------------------------------
37 #define DEBUG_TPL 0x40000000 ///< Display TPL change messages
39 #define TPL_SOCKETS TPL_CALLBACK ///< TPL for routine synchronization
41 //------------------------------------------------------------------------------
43 //------------------------------------------------------------------------------
45 #if defined(_MSC_VER) /* Handle Microsoft VC++ compiler specifics. */
46 #define DBG_ENTER() DEBUG (( DEBUG_INFO, "Entering " __FUNCTION__ "\n" )) ///< Display routine entry
47 #define DBG_EXIT() DEBUG (( DEBUG_INFO, "Exiting " __FUNCTION__ "\n" )) ///< Display routine exit
48 #define DBG_EXIT_DEC(Status) DEBUG (( DEBUG_INFO, "Exiting " __FUNCTION__ ", Status: %d\n", Status )) ///< Display routine exit with decimal value
49 #define DBG_EXIT_HEX(Status) DEBUG (( DEBUG_INFO, "Exiting " __FUNCTION__ ", Status: 0x%08x\n", Status )) ///< Display routine exit with hex value
50 #define DBG_EXIT_STATUS(Status) DEBUG (( DEBUG_INFO, "Exiting " __FUNCTION__ ", Status: %r\n", Status )) ///< Display routine exit with status value
51 #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
53 #define DBG_ENTER() ///< Display routine entry
54 #define DBG_EXIT() ///< Display routine exit
55 #define DBG_EXIT_DEC(Status) ///< Display routine exit with decimal value
56 #define DBG_EXIT_HEX(Status) ///< Display routine exit with hex value
57 #define DBG_EXIT_STATUS(Status) ///< Display routine exit with status value
58 #define DBG_EXIT_TF(Status) ///< Display routine with TRUE/FALSE value
61 #define DIM(x) ( sizeof ( x ) / sizeof ( x[0] )) ///< Compute the number of entries in an array
66 This macro which is enabled when debug is enabled verifies that
67 the new TPL value is >= the current TPL value.
73 #if !defined(MDEPKG_NDEBUG)
76 Verify that the TPL is at the correct level
78 #define VERIFY_AT_TPL(tpl) \
80 EFI_TPL PreviousTpl; \
82 PreviousTpl = EfiGetCurrentTpl ( ); \
83 if ( PreviousTpl != tpl ) { \
84 DEBUG (( DEBUG_ERROR | DEBUG_TPL, \
85 "Current TPL: %d, New TPL: %d\r\n", \
86 PreviousTpl, tpl )); \
87 ASSERT ( PreviousTpl == tpl ); \
91 #define VERIFY_TPL(tpl) \
93 EFI_TPL PreviousTpl; \
95 PreviousTpl = EfiGetCurrentTpl ( ); \
96 if ( PreviousTpl > tpl ) { \
97 DEBUG (( DEBUG_ERROR | DEBUG_TPL, \
98 "Current TPL: %d, New TPL: %d\r\n", \
99 PreviousTpl, tpl )); \
100 ASSERT ( PreviousTpl <= tpl ); \
104 #else // MDEPKG_NDEBUG
106 #define VERIFY_AT_TPL(tpl) ///< Verify that the TPL is at the correct level
107 #define VERIFY_TPL(tpl) ///< Verify that the TPL is at the correct level
109 #endif // MDEPKG_NDEBUG
112 Raise TPL to the specified level
114 #define RAISE_TPL(PreviousTpl, tpl) \
115 VERIFY_TPL ( tpl ); \
116 PreviousTpl = gBS->RaiseTPL ( tpl ); \
117 DEBUG (( DEBUG_TPL | DEBUG_TPL, \
122 Restore the TPL to the previous value
124 #define RESTORE_TPL(tpl) \
125 DEBUG (( DEBUG_TPL | DEBUG_TPL, \
128 gBS->RestoreTPL ( tpl )
130 //------------------------------------------------------------------------------
132 //------------------------------------------------------------------------------
134 typedef struct _ESL_SERVICE ESL_SERVICE
; ///< Forward delcaration
137 Protocol binding and installation control structure
139 The driver uses this structure to simplify the driver binding processing.
142 CHAR16
* pName
; ///< Protocol name
143 EFI_GUID
* pNetworkBinding
; ///< Network service binding protocol for socket support
144 EFI_GUID
* pNetworkProtocolGuid
;///< Network protocol GUID
145 CONST EFI_GUID
* pTagGuid
; ///< Tag to mark protocol in use
146 UINTN ServiceListOffset
; ///< Offset in ::ESL_LAYER for the list of services
147 UINTN RxIo
; ///< Number of receive ESL_IO_MGMT structures for data
148 UINTN TxIoNormal
; ///< Number of transmit ESL_IO_MGMT structures for normal data
149 UINTN TxIoUrgent
; ///< Number of transmit ESL_IO_MGMT structures for urgent data
150 } ESL_SOCKET_BINDING
;
152 //------------------------------------------------------------------------------
154 //------------------------------------------------------------------------------
156 extern CONST EFI_GUID mEslIp4ServiceGuid
; ///< Tag GUID for the IPv4 layer
157 extern CONST EFI_GUID mEslTcp4ServiceGuid
; ///< Tag GUID for the TCPv4 layer
158 extern CONST EFI_GUID mEslUdp4ServiceGuid
; ///< Tag GUID for the UDPv4 layer
160 //------------------------------------------------------------------------------
162 //------------------------------------------------------------------------------
164 extern CONST ESL_SOCKET_BINDING cEslSocketBinding
[];///< List of network service bindings
165 extern CONST UINTN cEslSocketBindingEntries
; ///< Number of network service bindings
167 //------------------------------------------------------------------------------
168 // DXE Support Routines
169 //------------------------------------------------------------------------------
172 Creates a child handle and installs a protocol.
174 When the socket application is linked against UseSocketDxe, the ::socket
175 routine indirectly calls this routine in SocketDxe to create a child
176 handle if necessary and install the socket protocol on the handle.
177 Upon return, EslServiceGetProtocol in UseSocketLib returns the
178 ::EFI_SOCKET_PROTOCOL address to the socket routine.
180 @param [in] pThis Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
181 @param [in] pChildHandle Pointer to the handle of the child to create. If it is NULL,
182 then a new handle is created. If it is a pointer to an existing UEFI handle,
183 then the protocol is added to the existing UEFI handle.
185 @retval EFI_SUCCESS The protocol was added to ChildHandle.
186 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
187 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
189 @retval other The child handle was not created
195 IN EFI_SERVICE_BINDING_PROTOCOL
* pThis
,
196 IN OUT EFI_HANDLE
* pChildHandle
200 Destroys a child handle with a protocol installed on it.
202 When the socket application is linked against UseSocketDxe, the ::close
203 routine indirectly calls this routine in SocketDxe to undo the operations
204 done by the ::EslDxeCreateChild routine. This routine removes the socket
205 protocol from the handle and then destroys the child handle if there are
206 no other protocols attached.
208 @param [in] pThis Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
209 @param [in] ChildHandle Handle of the child to destroy
211 @retval EFI_SUCCESS The protocol was removed from ChildHandle.
212 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
213 @retval EFI_INVALID_PARAMETER Child handle is not a valid UEFI Handle.
214 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
215 because its services are being used.
216 @retval other The child handle was not destroyed
222 IN EFI_SERVICE_BINDING_PROTOCOL
* pThis
,
223 IN EFI_HANDLE ChildHandle
227 Install the socket service
229 SocketDxe uses this routine to announce the socket interface to
232 @param [in] pImageHandle Address of the image handle
234 @retval EFI_SUCCESS Service installed successfully
239 IN EFI_HANDLE
* pImageHandle
243 Uninstall the socket service
245 SocketDxe uses this routine to notify EFI that the socket layer
246 is no longer available.
248 @param [in] ImageHandle Handle for the image.
250 @retval EFI_SUCCESS Service installed successfully
255 IN EFI_HANDLE ImageHandle
258 //------------------------------------------------------------------------------
259 // Service Support Routines
260 //------------------------------------------------------------------------------
263 Connect to the network service bindings
265 Walk the network service protocols on the controller handle and
266 locate any that are not in use. Create ::ESL_SERVICE structures to
267 manage the network layer interfaces for the socket driver. Tag
268 each of the network interfaces that are being used. Finally, this
269 routine calls ESL_SOCKET_BINDING::pfnInitialize to prepare the network
270 interface for use by the socket layer.
272 @param [in] BindingHandle Handle for protocol binding.
273 @param [in] Controller Handle of device to work with.
275 @retval EFI_SUCCESS This driver is added to Controller.
276 @retval other This driver does not support this device.
282 IN EFI_HANDLE BindingHandle
,
283 IN EFI_HANDLE Controller
287 Shutdown the connections to the network layer by locating the
288 tags on the network interfaces established by ::EslServiceConnect.
289 This routine calls ESL_SOCKET_BINDING::pfnShutdown to shutdown the any
290 activity on the network interface and then free the ::ESL_SERVICE
293 @param [in] BindingHandle Handle for protocol binding.
294 @param [in] Controller Handle of device to stop driver on.
296 @retval EFI_SUCCESS This driver is removed Controller.
297 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
298 @retval other This driver was not removed from this device.
303 EslServiceDisconnect (
304 IN EFI_HANDLE BindingHandle
,
305 IN EFI_HANDLE Controller
309 Initialize the service layer
311 @param [in] ImageHandle Handle for the image.
317 IN EFI_HANDLE ImageHandle
321 Shutdown the service layer
330 //------------------------------------------------------------------------------
331 // Socket Protocol Routines
332 //------------------------------------------------------------------------------
335 Bind a name to a socket.
337 This routine calls the network specific layer to save the network
338 address of the local connection point.
340 The ::bind routine calls this routine to connect a name
341 (network address and port) to a socket on the local machine.
343 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
345 @param [in] pSockAddr Address of a sockaddr structure that contains the
346 connection point on the local machine. An IPv4 address
347 of INADDR_ANY specifies that the connection is made to
348 all of the network stacks on the platform. Specifying a
349 specific IPv4 address restricts the connection to the
350 network stack supporting that address. Specifying zero
351 for the port causes the network layer to assign a port
352 number from the dynamic range. Specifying a specific
353 port number causes the network layer to use that port.
355 @param [in] SockAddrLength Specifies the length in bytes of the sockaddr structure.
357 @param [out] pErrno Address to receive the errno value upon completion.
359 @retval EFI_SUCCESS - Socket successfully created
364 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
365 IN CONST
struct sockaddr
* pSockAddr
,
366 IN socklen_t SockAddrLength
,
371 Determine if the socket is closed
373 This routine checks the state of the socket to determine if
374 the network specific layer has completed the close operation.
376 The ::close routine polls this routine to determine when the
377 close operation is complete. The close operation needs to
378 reverse the operations of the ::EslSocketAllocate routine.
380 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
381 @param [out] pErrno Address to receive the errno value upon completion.
383 @retval EFI_SUCCESS Socket successfully closed
384 @retval EFI_NOT_READY Close still in progress
385 @retval EFI_ALREADY Close operation already in progress
386 @retval Other Failed to close the socket
391 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
396 Start the close operation on the socket
398 This routine calls the network specific layer to initiate the
399 close state machine. This routine then calls the network
400 specific layer to determine if the close state machine has gone
401 to completion. The result from this poll is returned to the
404 The ::close routine calls this routine to start the close
405 operation which reverses the operations of the
406 ::EslSocketAllocate routine. The close routine then polls
407 the ::EslSocketClosePoll routine to determine when the
410 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
411 @param [in] bCloseNow Boolean to control close behavior
412 @param [out] pErrno Address to receive the errno value upon completion.
414 @retval EFI_SUCCESS Socket successfully closed
415 @retval EFI_NOT_READY Close still in progress
416 @retval EFI_ALREADY Close operation already in progress
417 @retval Other Failed to close the socket
421 EslSocketCloseStart (
422 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
423 IN BOOLEAN bCloseNow
,
428 Connect to a remote system via the network.
430 This routine calls the network specific layer to establish
431 the remote system address and establish the connection to
434 The ::connect routine calls this routine to establish a
435 connection with the specified remote system. This routine
436 is designed to be polled by the connect routine for completion
437 of the network connection.
439 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
441 @param [in] pSockAddr Network address of the remote system.
443 @param [in] SockAddrLength Length in bytes of the network address.
445 @param [out] pErrno Address to receive the errno value upon completion.
447 @retval EFI_SUCCESS The connection was successfully established.
448 @retval EFI_NOT_READY The connection is in progress, call this routine again.
449 @retval Others The connection attempt failed.
454 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
455 IN
const struct sockaddr
* pSockAddr
,
456 IN socklen_t SockAddrLength
,
461 Get the local address.
463 This routine calls the network specific layer to get the network
464 address of the local host connection point.
466 The ::getsockname routine calls this routine to obtain the network
467 address associated with the local host connection point.
469 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
471 @param [out] pAddress Network address to receive the local system address
473 @param [in,out] pAddressLength Length of the local network address structure
475 @param [out] pErrno Address to receive the errno value upon completion.
477 @retval EFI_SUCCESS - Local address successfully returned
481 EslSocketGetLocalAddress (
482 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
483 OUT
struct sockaddr
* pAddress
,
484 IN OUT socklen_t
* pAddressLength
,
489 Get the peer address.
491 This routine calls the network specific layer to get the remote
492 system connection point.
494 The ::getpeername routine calls this routine to obtain the network
495 address of the remote connection point.
497 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
499 @param [out] pAddress Network address to receive the remote system address
501 @param [in,out] pAddressLength Length of the remote network address structure
503 @param [out] pErrno Address to receive the errno value upon completion.
505 @retval EFI_SUCCESS - Remote address successfully returned
509 EslSocketGetPeerAddress (
510 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
511 OUT
struct sockaddr
* pAddress
,
512 IN OUT socklen_t
* pAddressLength
,
517 Establish the known port to listen for network connections.
519 This routine calls into the network protocol layer to establish
520 a handler that is called upon connection completion. The handler
521 is responsible for inserting the connection into the FIFO.
523 The ::listen routine indirectly calls this routine to place the
524 socket into a state that enables connection attempts. Connections
525 are placed in a FIFO that is serviced by the application. The
526 application calls the ::accept (::EslSocketAccept) routine to
527 remove the next connection from the FIFO and get the associated
530 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
532 @param [in] Backlog Backlog specifies the maximum FIFO depth for
533 the connections waiting for the application
534 to call accept. Connection attempts received
535 while the queue is full are refused.
537 @param [out] pErrno Address to receive the errno value upon completion.
539 @retval EFI_SUCCESS - Socket successfully created
540 @retval Other - Failed to enable the socket for listen
545 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
551 Get the socket options
553 This routine handles the socket level options and passes the
554 others to the network specific layer.
556 The ::getsockopt routine calls this routine to retrieve the
557 socket options one at a time by name.
559 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
560 @param [in] level Option protocol level
561 @param [in] OptionName Name of the option
562 @param [out] pOptionValue Buffer to receive the option value
563 @param [in,out] pOptionLength Length of the buffer in bytes,
564 upon return length of the option value in bytes
565 @param [out] pErrno Address to receive the errno value upon completion.
567 @retval EFI_SUCCESS - Socket data successfully received
572 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
575 OUT
void * __restrict option_value
,
576 IN OUT socklen_t
* __restrict option_len
,
581 Set the socket options
583 This routine handles the socket level options and passes the
584 others to the network specific layer.
586 The ::setsockopt routine calls this routine to adjust the socket
587 options one at a time by name.
589 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
590 @param [in] level Option protocol level
591 @param [in] OptionName Name of the option
592 @param [in] pOptionValue Buffer containing the option value
593 @param [in] OptionLength Length of the buffer in bytes
594 @param [out] pErrno Address to receive the errno value upon completion.
596 @retval EFI_SUCCESS - Option successfully set
601 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
604 IN CONST
void * option_value
,
605 IN socklen_t option_len
,
610 Poll a socket for pending activity.
612 This routine builds a detected event mask which is returned to
613 the caller in the buffer provided.
615 The ::poll routine calls this routine to determine if the socket
616 needs to be serviced as a result of connection, error, receive or
619 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
621 @param [in] Events Events of interest for this socket
623 @param [in] pEvents Address to receive the detected events
625 @param [out] pErrno Address to receive the errno value upon completion.
627 @retval EFI_SUCCESS - Socket successfully polled
628 @retval EFI_INVALID_PARAMETER - When pEvents is NULL
633 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
640 Receive data from a network connection.
642 This routine calls the network specific routine to remove the
643 next portion of data from the receive queue and return it to the
646 The ::recvfrom routine calls this routine to determine if any data
647 is received from the remote system. Note that the other routines
648 ::recv and ::read are layered on top of ::recvfrom.
650 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
652 @param [in] Flags Message control flags
654 @param [in] BufferLength Length of the the buffer
656 @param [in] pBuffer Address of a buffer to receive the data.
658 @param [in] pDataLength Number of received data bytes in the buffer.
660 @param [out] pAddress Network address to receive the remote system address
662 @param [in,out] pAddressLength Length of the remote network address structure
664 @param [out] pErrno Address to receive the errno value upon completion.
666 @retval EFI_SUCCESS - Socket data successfully received
671 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
673 IN
size_t BufferLength
,
675 OUT
size_t * pDataLength
,
676 OUT
struct sockaddr
* pAddress
,
677 IN OUT socklen_t
* pAddressLength
,
682 Shutdown the socket receive and transmit operations
684 This routine sets a flag to stop future transmissions and calls
685 the network specific layer to cancel the pending receive operation.
687 The ::shutdown routine calls this routine to stop receive and transmit
688 operations on the socket.
690 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
692 @param [in] How Which operations to stop
694 @param [out] pErrno Address to receive the errno value upon completion.
696 @retval EFI_SUCCESS - Socket operations successfully shutdown
701 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
707 Send data using a network connection.
709 This routine calls the network specific layer to queue the data
710 for transmission. Eventually the buffer will reach the head of
711 the queue and will get transmitted over the network. For datagram
712 sockets there is no guarantee that the data reaches the application
713 running on the remote system.
715 The ::sendto routine calls this routine to send data to the remote
716 system. Note that ::send and ::write are layered on top of ::sendto.
718 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
720 @param [in] Flags Message control flags
722 @param [in] BufferLength Length of the the buffer
724 @param [in] pBuffer Address of a buffer containing the data to send
726 @param [in] pDataLength Address to receive the number of data bytes sent
728 @param [in] pAddress Network address of the remote system address
730 @param [in] AddressLength Length of the remote network address structure
732 @param [out] pErrno Address to receive the errno value upon completion.
734 @retval EFI_SUCCESS - Socket data successfully queued for transmit
739 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
741 IN
size_t BufferLength
,
742 IN CONST UINT8
* pBuffer
,
743 OUT
size_t * pDataLength
,
744 IN
const struct sockaddr
* pAddress
,
745 IN socklen_t AddressLength
,
749 //------------------------------------------------------------------------------
751 #endif // _EFI_SOCKET_LIB_H_