2 Definitions for the TFTP server.
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
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 _TFTP_SERVER_H_
16 #define _TFTP_SERVER_H_
23 #include <Guid/EventGroup.h>
25 #include <Library/BaseMemoryLib.h>
26 #include <Library/DebugLib.h>
27 #include <Library/PcdLib.h>
28 #include <Library/UefiApplicationEntryPoint.h>
29 #include <Library/UefiBootServicesTableLib.h>
30 #include <Library/UefiLib.h>
31 #include <Protocol/BlockIo.h>
33 #include <netinet/in.h>
35 #include <sys/EfiSysCall.h>
37 #include <sys/socket.h>
39 //------------------------------------------------------------------------------
41 //------------------------------------------------------------------------------
43 #if defined(_MSC_VER) /* Handle Microsoft VC++ compiler specifics. */
44 #define DBG_ENTER() DEBUG (( DEBUG_INFO, "Entering " __FUNCTION__ "\n" )) ///< Display routine entry
45 #define DBG_EXIT() DEBUG (( DEBUG_INFO, "Exiting " __FUNCTION__ "\n" )) ///< Display routine exit
46 #define DBG_EXIT_DEC(Status) DEBUG (( DEBUG_INFO, "Exiting " __FUNCTION__ ", Status: %d\n", Status )) ///< Display routine exit with decimal value
47 #define DBG_EXIT_HEX(Status) DEBUG (( DEBUG_INFO, "Exiting " __FUNCTION__ ", Status: 0x%08x\n", Status )) ///< Display routine exit with hex value
48 #define DBG_EXIT_STATUS(Status) DEBUG (( DEBUG_INFO, "Exiting " __FUNCTION__ ", Status: %r\n", Status )) ///< Display routine exit with status value
49 #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_EXIT_DEC(Status)
54 #define DBG_EXIT_HEX(Status)
55 #define DBG_EXIT_STATUS(Status)
56 #define DBG_EXIT_TF(Status)
59 #define DIM(x) ( sizeof ( x ) / sizeof ( x[0] )) ///< Compute the number of entries in an array
61 //------------------------------------------------------------------------------
63 //------------------------------------------------------------------------------
65 #define DEBUG_SOCKET_POLL 0x40000000 ///< Display the socket poll messages
66 #define DEBUG_PORT_WORK 0x20000000 ///< Display the port work messages
67 #define DEBUG_SERVER_TIMER 0x10000000 ///< Display the socket poll messages
68 #define DEBUG_TFTP_PORT 0x08000000 ///< Display the TFTP port messages
69 #define DEBUG_TFTP_REQUEST 0x04000000 ///< Display the TFTP request messages
70 #define DEBUG_TX 0x02000000 ///< Display transmit messages
71 #define DEBUG_RX 0x01000000 ///< Display receive messages
72 #define DEBUG_TFTP_ACK 0x00800000 ///< Display the TFTP ACK messages
74 #define TFTP_PORT_POLL_DELAY ( 2 * 1000 ) ///< Delay in milliseconds for attempts to open the TFTP port
75 #define CLIENT_POLL_DELAY 50 ///< Delay in milliseconds between client polls
77 #define TPL_TFTP_SERVER TPL_CALLBACK ///< TPL for routine synchronization
82 This macro which is enabled when debug is enabled verifies that
83 the new TPL value is >= the current TPL value.
89 #if !defined(MDEPKG_NDEBUG)
91 #define VERIFY_TPL(tpl) \
93 EFI_TPL PreviousTpl; \
95 PreviousTpl = gBS->RaiseTPL ( TPL_HIGH_LEVEL ); \
96 gBS->RestoreTPL ( PreviousTpl ); \
97 if ( PreviousTpl > tpl ) { \
98 DEBUG (( DEBUG_ERROR, "Current TPL: %d, New TPL: %d\r\n", PreviousTpl, tpl )); \
99 ASSERT ( PreviousTpl <= tpl ); \
103 #else // MDEPKG_NDEBUG
105 #define VERIFY_TPL(tpl)
107 #endif // MDEPKG_NDEBUG
109 #define TFTP_SERVER_SIGNATURE SIGNATURE_32('T','F','T','P') ///< TSDT_TFTP_SERVER memory signature
112 // See: http://www.rfc-editor.org/rfc/pdfrfc/rfc1350.txt.pdf
117 #define TFTP_OP_READ_REQUEST 1 ///< Read request, zero terminated file name, zero terminated mode
118 #define TFTP_OP_WRITE_REQUEST 2 ///< Write request, zero terminated file name, zero terminated mode
119 #define TFTP_OP_DATA 3 ///< Data block, end-of-file indicated by short block
120 #define TFTP_OP_ACK 4 ///< ACK block number
121 #define TFTP_OP_ERROR 5 ///< Error number and explaination
122 #define TFTP_OP_OACK 6 ///< ACK the options
124 #define TFTP_MAX_BLOCK_SIZE 4096 ///< Maximum block size
126 //------------------------------------------------------------------------------
128 //------------------------------------------------------------------------------
131 Port control structure
133 typedef struct _TSDT_CONNECTION_CONTEXT TSDT_CONNECTION_CONTEXT
;
134 typedef struct _TSDT_CONNECTION_CONTEXT
{
136 // Remote connection management
138 TSDT_CONNECTION_CONTEXT
* pNext
; ///< Next context in the connection list
139 struct sockaddr_in RemoteAddress
; ///< Remote address
142 // TFTP management parameters
144 UINT16 AckNext
; ///< Next block to be received
145 BOOLEAN bExpectAck
; ///< TRUE for read, FALSE for write
146 UINT32 BlockSize
; ///< Negotiated block size
147 UINT32 Timeout
; ///< Number of seconds to wait before retransmission
150 // File management parameters
152 EFI_HANDLE File
; ///< NULL while file is closed
153 UINT64 LengthInBytes
; ///< Size of the file
154 UINT64 MaxTransferSize
; ///< Maximum transfer size
155 BOOLEAN bEofSent
; ///< End of file sent
156 UINT8
* pBuffer
; ///< Pointer into the file data
157 UINT8
* pEnd
; ///< End of the file data
158 UINT8 FileData
[ 64 * TFTP_MAX_BLOCK_SIZE
]; ///< File data to send
163 ssize_t TxBytes
; ///< Bytes in the TX buffer
164 UINT8 TxBuffer
[ 2 + 2 + TFTP_MAX_BLOCK_SIZE
]; ///< Transmit buffer
165 }GCC_TSDT_CONNECTION_CONTEXT
;
168 TFTP server control structure
171 UINTN Signature
; ///< Structure identification
176 EFI_HANDLE ImageHandle
; ///< Image handle
179 // TFTP port management
181 BOOLEAN bTimerRunning
; ///< Port creation timer status
182 EFI_EVENT TimerEvent
; ///< Timer to open TFTP port
183 struct pollfd TftpPort
; ///< Poll descriptor for the TFTP port
184 struct sockaddr_in TftpServerAddress
; ///< Address of the local TFTP server
187 // Request management
189 struct sockaddr_in RemoteAddress
; ///< Remote address
190 ssize_t RxBytes
; ///< Receive data length in bytes
191 UINT8 RxBuffer
[ 2 + 2 + TFTP_MAX_BLOCK_SIZE
]; ///< Receive buffer
194 // Client port management
196 TSDT_CONNECTION_CONTEXT
* pContextList
; ///< List of connection context structures
199 //#define SERVER_FROM_SERVICE(a) CR(a, TSDT_TFTP_SERVER, ServiceBinding, TFTP_SERVER_SIGNATURE) ///< Locate DT_LAYER from service binding
201 extern TSDT_TFTP_SERVER mTftpServer
;
203 //------------------------------------------------------------------------------
205 //------------------------------------------------------------------------------
208 Process the TFTP request
210 @param [in] pOption Address of the first zero terminated option string
211 @param [in] pValue Address to receive the value
213 @retval EFI_SUCCESS Option translated into a value
223 Process the TFTP request
225 @param [in] pTftpServer The TFTP server control structure address.
226 @param [in] pContext Connection context structure address
231 IN TSDT_TFTP_SERVER
* pTftpServer
,
232 IN TSDT_CONNECTION_CONTEXT
* pContext
236 Build and send an error packet
238 @param [in] pTftpServer The TFTP server control structure address.
239 @param [in] pContext The context structure address.
240 @param [in] Error Error number for the packet
241 @param [in] pError Zero terminated error string address
243 @retval EFI_SUCCESS Message processed successfully
248 IN TSDT_TFTP_SERVER
* pTftpServer
,
249 IN TSDT_CONNECTION_CONTEXT
* pContext
,
255 Send the next block of file system data
257 @param [in] pTftpServer The TFTP server control structure address.
258 @param [in] pContext The context structure address.
260 @retval EFI_SUCCESS Message processed successfully
265 IN TSDT_TFTP_SERVER
* pTftpServer
,
266 IN TSDT_CONNECTION_CONTEXT
* pContext
270 TFTP port creation timer routine
272 This routine polls the socket layer waiting for the initial network connection
273 which will enable the creation of the TFTP port. The socket layer will manage
274 the coming and going of the network connections after that until the last network
275 connection is broken.
277 @param [in] pTftpServer The TFTP server control structure address.
282 IN TSDT_TFTP_SERVER
* pTftpServer
286 Start the TFTP server port creation timer
288 @param [in] pTftpServer The TFTP server control structure address.
290 @retval EFI_SUCCESS The timer was successfully started.
291 @retval EFI_ALREADY_STARTED The timer is already running.
292 @retval Other The timer failed to start.
296 TftpServerTimerStart (
297 IN TSDT_TFTP_SERVER
* pTftpServer
301 Stop the TFTP server port creation timer
303 @param [in] pTftpServer The TFTP server control structure address.
305 @retval EFI_SUCCESS The TFTP port timer is stopped
306 @retval Other Failed to stop the TFTP port timer
310 TftpServerTimerStop (
311 IN TSDT_TFTP_SERVER
* pTftpServer
315 Send the next TFTP packet
317 @param [in] pTftpServer The TFTP server control structure address.
318 @param [in] pContext The context structure address.
320 @retval EFI_SUCCESS Message processed successfully
325 IN TSDT_TFTP_SERVER
* pTftpServer
,
326 IN TSDT_CONNECTION_CONTEXT
* pContext
329 //------------------------------------------------------------------------------
331 #endif // _TFTP_SERVER_H_