]>
Commit | Line | Data |
---|---|---|
4684b66f | 1 | /** @file\r |
2 | Definitions for the TFTP server.\r | |
3 | \r | |
4 | Copyright (c) 2011, Intel Corporation\r | |
5 | All rights reserved. This program and the accompanying materials\r | |
6 | are licensed and made available under the terms and conditions of the BSD License\r | |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | \r | |
13 | **/\r | |
14 | \r | |
15 | #ifndef _TFTP_SERVER_H_\r | |
16 | #define _TFTP_SERVER_H_\r | |
17 | \r | |
18 | #include <errno.h>\r | |
19 | #include <stdlib.h>\r | |
20 | #include <string.h>\r | |
21 | #include <Uefi.h>\r | |
22 | \r | |
23 | #include <Guid/EventGroup.h>\r | |
24 | \r | |
25 | #include <Library/BaseMemoryLib.h>\r | |
26 | #include <Library/DebugLib.h>\r | |
27 | #include <Library/PcdLib.h>\r | |
28 | #include <Library/UefiApplicationEntryPoint.h>\r | |
29 | #include <Library/UefiBootServicesTableLib.h>\r | |
30 | #include <Library/UefiLib.h>\r | |
31 | #include <Protocol/BlockIo.h>\r | |
32 | \r | |
33 | #include <netinet/in.h>\r | |
34 | \r | |
35 | #include <sys/EfiSysCall.h>\r | |
36 | #include <sys/poll.h>\r | |
37 | #include <sys/socket.h>\r | |
38 | \r | |
39 | //------------------------------------------------------------------------------\r | |
40 | // Macros\r | |
41 | //------------------------------------------------------------------------------\r | |
42 | \r | |
43 | #if defined(_MSC_VER) /* Handle Microsoft VC++ compiler specifics. */\r | |
44 | #define DBG_ENTER() DEBUG (( DEBUG_INFO, "Entering " __FUNCTION__ "\n" )) ///< Display routine entry\r | |
45 | #define DBG_EXIT() DEBUG (( DEBUG_INFO, "Exiting " __FUNCTION__ "\n" )) ///< Display routine exit\r | |
46 | #define DBG_EXIT_DEC(Status) DEBUG (( DEBUG_INFO, "Exiting " __FUNCTION__ ", Status: %d\n", Status )) ///< Display routine exit with decimal value\r | |
47 | #define DBG_EXIT_HEX(Status) DEBUG (( DEBUG_INFO, "Exiting " __FUNCTION__ ", Status: 0x%08x\n", Status )) ///< Display routine exit with hex value\r | |
48 | #define DBG_EXIT_STATUS(Status) DEBUG (( DEBUG_INFO, "Exiting " __FUNCTION__ ", Status: %r\n", Status )) ///< Display routine exit with status value\r | |
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\r | |
50 | #else // _MSC_VER\r | |
51 | #define DBG_ENTER()\r | |
52 | #define DBG_EXIT()\r | |
53 | #define DBG_EXIT_DEC(Status)\r | |
54 | #define DBG_EXIT_HEX(Status)\r | |
55 | #define DBG_EXIT_STATUS(Status)\r | |
56 | #define DBG_EXIT_TF(Status)\r | |
57 | #endif // _MSC_VER\r | |
58 | \r | |
59 | #define DIM(x) ( sizeof ( x ) / sizeof ( x[0] )) ///< Compute the number of entries in an array\r | |
60 | \r | |
61 | //------------------------------------------------------------------------------\r | |
62 | // Constants\r | |
63 | //------------------------------------------------------------------------------\r | |
64 | \r | |
65 | #define DEBUG_SOCKET_POLL 0x40000000 ///< Display the socket poll messages\r | |
66 | #define DEBUG_PORT_WORK 0x20000000 ///< Display the port work messages\r | |
67 | #define DEBUG_SERVER_TIMER 0x10000000 ///< Display the socket poll messages\r | |
68 | #define DEBUG_TFTP_PORT 0x08000000 ///< Display the TFTP port messages\r | |
69 | #define DEBUG_TFTP_REQUEST 0x04000000 ///< Display the TFTP request messages\r | |
70 | #define DEBUG_TX 0x02000000 ///< Display transmit messages\r | |
71 | #define DEBUG_RX 0x01000000 ///< Display receive messages\r | |
72 | #define DEBUG_TFTP_ACK 0x00800000 ///< Display the TFTP ACK messages\r | |
73 | \r | |
74 | #define TFTP_PORT_POLL_DELAY ( 2 * 1000 ) ///< Delay in milliseconds for attempts to open the TFTP port\r | |
75 | #define CLIENT_POLL_DELAY 50 ///< Delay in milliseconds between client polls\r | |
76 | \r | |
77 | #define TPL_TFTP_SERVER TPL_CALLBACK ///< TPL for routine synchronization\r | |
78 | \r | |
79 | /**\r | |
80 | Verify new TPL value\r | |
81 | \r | |
82 | This macro which is enabled when debug is enabled verifies that\r | |
83 | the new TPL value is >= the current TPL value.\r | |
84 | **/\r | |
85 | #ifdef VERIFY_TPL\r | |
86 | #undef VERIFY_TPL\r | |
87 | #endif // VERIFY_TPL\r | |
88 | \r | |
89 | #if !defined(MDEPKG_NDEBUG)\r | |
90 | \r | |
91 | #define VERIFY_TPL(tpl) \\r | |
92 | { \\r | |
93 | EFI_TPL PreviousTpl; \\r | |
94 | \\r | |
95 | PreviousTpl = gBS->RaiseTPL ( TPL_HIGH_LEVEL ); \\r | |
96 | gBS->RestoreTPL ( PreviousTpl ); \\r | |
97 | if ( PreviousTpl > tpl ) { \\r | |
98 | DEBUG (( DEBUG_ERROR, "Current TPL: %d, New TPL: %d\r\n", PreviousTpl, tpl )); \\r | |
99 | ASSERT ( PreviousTpl <= tpl ); \\r | |
100 | } \\r | |
101 | }\r | |
102 | \r | |
103 | #else // MDEPKG_NDEBUG\r | |
104 | \r | |
105 | #define VERIFY_TPL(tpl)\r | |
106 | \r | |
107 | #endif // MDEPKG_NDEBUG\r | |
108 | \r | |
109 | #define TFTP_SERVER_SIGNATURE SIGNATURE_32('T','F','T','P') ///< TSDT_TFTP_SERVER memory signature\r | |
110 | \r | |
111 | //\r | |
112 | // See: http://www.rfc-editor.org/rfc/pdfrfc/rfc1350.txt.pdf\r | |
113 | //\r | |
114 | // TFTP Operations\r | |
115 | //\r | |
116 | \r | |
117 | #define TFTP_OP_READ_REQUEST 1 ///< Read request, zero terminated file name, zero terminated mode\r | |
118 | #define TFTP_OP_WRITE_REQUEST 2 ///< Write request, zero terminated file name, zero terminated mode\r | |
119 | #define TFTP_OP_DATA 3 ///< Data block, end-of-file indicated by short block\r | |
120 | #define TFTP_OP_ACK 4 ///< ACK block number\r | |
121 | #define TFTP_OP_ERROR 5 ///< Error number and explaination\r | |
122 | #define TFTP_OP_OACK 6 ///< ACK the options\r | |
123 | \r | |
124 | #define TFTP_MAX_BLOCK_SIZE 4096 ///< Maximum block size\r | |
125 | \r | |
126 | //------------------------------------------------------------------------------\r | |
127 | // Data Types\r | |
128 | //------------------------------------------------------------------------------\r | |
129 | \r | |
130 | /**\r | |
131 | Port control structure\r | |
132 | **/\r | |
133 | typedef struct _TSDT_CONNECTION_CONTEXT TSDT_CONNECTION_CONTEXT;\r | |
134 | typedef struct _TSDT_CONNECTION_CONTEXT {\r | |
135 | //\r | |
136 | // Remote connection management\r | |
137 | //\r | |
138 | TSDT_CONNECTION_CONTEXT * pNext; ///< Next context in the connection list\r | |
139 | struct sockaddr_in RemoteAddress; ///< Remote address\r | |
140 | \r | |
141 | //\r | |
142 | // TFTP management parameters\r | |
143 | //\r | |
144 | UINT16 AckNext; ///< Next block to be received\r | |
145 | BOOLEAN bExpectAck; ///< TRUE for read, FALSE for write\r | |
146 | UINT32 BlockSize; ///< Negotiated block size\r | |
147 | UINT32 Timeout; ///< Number of seconds to wait before retransmission\r | |
148 | \r | |
149 | //\r | |
150 | // File management parameters\r | |
151 | //\r | |
152 | EFI_HANDLE File; ///< NULL while file is closed\r | |
153 | UINT64 LengthInBytes; ///< Size of the file\r | |
154 | UINT64 MaxTransferSize; ///< Maximum transfer size\r | |
155 | BOOLEAN bEofSent; ///< End of file sent\r | |
156 | UINT8 * pBuffer; ///< Pointer into the file data\r | |
157 | UINT8 * pEnd; ///< End of the file data\r | |
158 | UINT8 FileData [ 64 * TFTP_MAX_BLOCK_SIZE ]; ///< File data to send\r | |
159 | \r | |
160 | //\r | |
161 | // Buffer management\r | |
162 | //\r | |
163 | ssize_t TxBytes; ///< Bytes in the TX buffer\r | |
164 | UINT8 TxBuffer [ 2 + 2 + TFTP_MAX_BLOCK_SIZE ]; ///< Transmit buffer\r | |
165 | }GCC_TSDT_CONNECTION_CONTEXT;\r | |
166 | \r | |
167 | /**\r | |
168 | TFTP server control structure\r | |
169 | **/\r | |
170 | typedef struct {\r | |
171 | UINTN Signature; ///< Structure identification\r | |
172 | \r | |
173 | //\r | |
174 | // Image attributes\r | |
175 | //\r | |
176 | EFI_HANDLE ImageHandle; ///< Image handle\r | |
177 | \r | |
178 | //\r | |
179 | // TFTP port management\r | |
180 | //\r | |
181 | BOOLEAN bTimerRunning; ///< Port creation timer status\r | |
182 | EFI_EVENT TimerEvent; ///< Timer to open TFTP port\r | |
183 | struct pollfd TftpPort; ///< Poll descriptor for the TFTP port\r | |
184 | struct sockaddr_in TftpServerAddress; ///< Address of the local TFTP server\r | |
185 | \r | |
186 | //\r | |
187 | // Request management\r | |
188 | //\r | |
189 | struct sockaddr_in RemoteAddress; ///< Remote address\r | |
190 | ssize_t RxBytes; ///< Receive data length in bytes\r | |
191 | UINT8 RxBuffer [ 2 + 2 + TFTP_MAX_BLOCK_SIZE ]; ///< Receive buffer\r | |
192 | \r | |
193 | //\r | |
194 | // Client port management\r | |
195 | //\r | |
196 | TSDT_CONNECTION_CONTEXT * pContextList; ///< List of connection context structures\r | |
197 | } TSDT_TFTP_SERVER;\r | |
198 | \r | |
199 | //#define SERVER_FROM_SERVICE(a) CR(a, TSDT_TFTP_SERVER, ServiceBinding, TFTP_SERVER_SIGNATURE) ///< Locate DT_LAYER from service binding\r | |
200 | \r | |
201 | extern TSDT_TFTP_SERVER mTftpServer;\r | |
202 | \r | |
203 | //------------------------------------------------------------------------------\r | |
204 | // Support routines\r | |
205 | //------------------------------------------------------------------------------\r | |
206 | \r | |
207 | /**\r | |
208 | Process the TFTP request\r | |
209 | \r | |
210 | @param [in] pOption Address of the first zero terminated option string\r | |
211 | @param [in] pValue Address to receive the value\r | |
212 | \r | |
213 | @retval EFI_SUCCESS Option translated into a value\r | |
214 | \r | |
215 | **/\r | |
216 | EFI_STATUS\r | |
217 | TftpOptionValue (\r | |
218 | IN UINT8 * pOption,\r | |
219 | IN INT32 * pValue\r | |
220 | );\r | |
221 | \r | |
222 | /**\r | |
223 | Process the TFTP request\r | |
224 | \r | |
225 | @param [in] pTftpServer The TFTP server control structure address.\r | |
226 | @param [in] pContext Connection context structure address\r | |
227 | \r | |
228 | **/\r | |
229 | VOID\r | |
230 | TftpProcessRequest (\r | |
231 | IN TSDT_TFTP_SERVER * pTftpServer,\r | |
232 | IN TSDT_CONNECTION_CONTEXT * pContext\r | |
233 | );\r | |
234 | \r | |
235 | /**\r | |
236 | Build and send an error packet\r | |
237 | \r | |
238 | @param [in] pTftpServer The TFTP server control structure address.\r | |
239 | @param [in] pContext The context structure address.\r | |
240 | @param [in] Error Error number for the packet\r | |
241 | @param [in] pError Zero terminated error string address\r | |
242 | \r | |
243 | @retval EFI_SUCCESS Message processed successfully\r | |
244 | \r | |
245 | **/\r | |
246 | EFI_STATUS\r | |
247 | TftpSendError (\r | |
248 | IN TSDT_TFTP_SERVER * pTftpServer,\r | |
249 | IN TSDT_CONNECTION_CONTEXT * pContext,\r | |
250 | IN UINT16 Error,\r | |
251 | IN UINT8 * pError\r | |
252 | );\r | |
253 | \r | |
254 | /**\r | |
255 | Send the next block of file system data\r | |
256 | \r | |
257 | @param [in] pTftpServer The TFTP server control structure address.\r | |
258 | @param [in] pContext The context structure address.\r | |
259 | \r | |
260 | @retval EFI_SUCCESS Message processed successfully\r | |
261 | \r | |
262 | **/\r | |
263 | EFI_STATUS\r | |
264 | TftpSendNextBlock (\r | |
265 | IN TSDT_TFTP_SERVER * pTftpServer,\r | |
266 | IN TSDT_CONNECTION_CONTEXT * pContext\r | |
267 | );\r | |
268 | \r | |
269 | /**\r | |
270 | TFTP port creation timer routine\r | |
271 | \r | |
272 | This routine polls the socket layer waiting for the initial network connection\r | |
273 | which will enable the creation of the TFTP port. The socket layer will manage\r | |
274 | the coming and going of the network connections after that until the last network\r | |
275 | connection is broken.\r | |
276 | \r | |
277 | @param [in] pTftpServer The TFTP server control structure address.\r | |
278 | \r | |
279 | **/\r | |
280 | VOID\r | |
281 | TftpServerTimer (\r | |
282 | IN TSDT_TFTP_SERVER * pTftpServer\r | |
283 | );\r | |
284 | \r | |
285 | /**\r | |
286 | Start the TFTP server port creation timer\r | |
287 | \r | |
288 | @param [in] pTftpServer The TFTP server control structure address.\r | |
289 | \r | |
290 | @retval EFI_SUCCESS The timer was successfully started.\r | |
291 | @retval EFI_ALREADY_STARTED The timer is already running.\r | |
292 | @retval Other The timer failed to start.\r | |
293 | \r | |
294 | **/\r | |
295 | EFI_STATUS\r | |
296 | TftpServerTimerStart (\r | |
297 | IN TSDT_TFTP_SERVER * pTftpServer\r | |
298 | );\r | |
299 | \r | |
300 | /**\r | |
301 | Stop the TFTP server port creation timer\r | |
302 | \r | |
303 | @param [in] pTftpServer The TFTP server control structure address.\r | |
304 | \r | |
305 | @retval EFI_SUCCESS The TFTP port timer is stopped\r | |
306 | @retval Other Failed to stop the TFTP port timer\r | |
307 | \r | |
308 | **/\r | |
309 | EFI_STATUS\r | |
310 | TftpServerTimerStop (\r | |
311 | IN TSDT_TFTP_SERVER * pTftpServer\r | |
312 | );\r | |
313 | \r | |
314 | /**\r | |
315 | Send the next TFTP packet\r | |
316 | \r | |
317 | @param [in] pTftpServer The TFTP server control structure address.\r | |
318 | @param [in] pContext The context structure address.\r | |
319 | \r | |
320 | @retval EFI_SUCCESS Message processed successfully\r | |
321 | \r | |
322 | **/\r | |
323 | EFI_STATUS\r | |
324 | TftpTxPacket (\r | |
325 | IN TSDT_TFTP_SERVER * pTftpServer,\r | |
326 | IN TSDT_CONNECTION_CONTEXT * pContext\r | |
327 | );\r | |
328 | \r | |
329 | //------------------------------------------------------------------------------\r | |
330 | \r | |
331 | #endif // _TFTP_SERVER_H_\r |