summaryrefslogtreecommitdiffstats
path: root/net/tftp.c
blob: 6be8b8f623212332ecb9d068d833674a0fce5be3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
 *	Copyright 1994, 1995, 2000 Neil Russell.
 *	(See License)
 *	Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de
 */

#include <common.h>
#include <command.h>
#include <net.h>
#include <driver.h>
#include <clock.h>
#include <fs.h>
#include <errno.h>
#include <libgen.h>
#include <fcntl.h>
#include <progress.h>
#include <getopt.h>
#include <fs.h>
#include <linux/stat.h>
#include <linux/err.h>

#define TFTP_PORT	69		/* Well known TFTP port #		*/
#define TIMEOUT		5		/* Seconds to timeout for a lost pkt	*/

/*
 *	TFTP operations.
 */
#define TFTP_RRQ	1
#define TFTP_WRQ	2
#define TFTP_DATA	3
#define TFTP_ACK	4
#define TFTP_ERROR	5
#define TFTP_OACK	6


static int		tftp_server_port;	/* The UDP port at their end		*/
static unsigned int	tftp_block;		/* packet sequence number		*/
static unsigned int	tftp_last_block;	/* last packet sequence number received */
static int		tftp_state;
static uint64_t		tftp_timer_start;
static int		tftp_err;

#define STATE_RRQ	1
#define STATE_WRQ	2
#define STATE_RDATA	3
#define STATE_WDATA	4
#define STATE_OACK	5
#define STATE_LAST	6
#define STATE_DONE	7

#define TFTP_BLOCK_SIZE		512		    /* default TFTP block size	*/

static char *tftp_filename;
static struct net_connection *tftp_con;
static int tftp_fd;
static int tftp_size;

#ifdef CONFIG_NET_TFTP_PUSH
static int tftp_push;

static inline void do_tftp_push(int push)
{
	tftp_push = push;
}

#else

#define tftp_push	0

static inline void do_tftp_push(int push)
{
}
#endif

static int tftp_send(void)
{
	unsigned char *xp;
	int len = 0;
	uint16_t *s;
	unsigned char *pkt = net_udp_get_payload(tftp_con);
	int ret;
	static int last_len;

	switch (tftp_state) {
	case STATE_RRQ:
	case STATE_WRQ:
		xp = pkt;
		s = (uint16_t *)pkt;
		if (tftp_state == STATE_RRQ)
			*s++ = htons(TFTP_RRQ);
		else
			*s++ = htons(TFTP_WRQ);
		pkt = (unsigned char *)s;
		pkt += sprintf((unsigned char *)pkt, "%s%coctet%ctimeout%c%d",
				tftp_filename, 0, 0, 0, TIMEOUT) + 1;
		len = pkt - xp;
		break;

	case STATE_WDATA:
		if (!tftp_push)
			break;

		if (tftp_last_block == tftp_block) {
			len = last_len;
			break;
		}

		tftp_last_block = tftp_block;
		s = (uint16_t *)pkt;
		*s++ = htons(TFTP_DATA);
		*s++ = htons(tftp_block);
		len = read(tftp_fd, s, 512);
		if (len < 0) {
			perror("read");
			tftp_err = -errno;
			tftp_state = STATE_DONE;
			return tftp_err;
		}
		tftp_size += len;
		if (len < 512)
			tftp_state = STATE_LAST;
		len += 4;
		last_len = len;
		break;

	case STATE_RDATA:
	case STATE_OACK:
		xp = pkt;
		s = (uint16_t *)pkt;
		*s++ = htons(TFTP_ACK);
		*s++ = htons(tftp_block);
		pkt = (unsigned char *)s;
		len = pkt - xp;
		break;
	}

	tftp_timer_start = get_time_ns();
	show_progress(tftp_size);
	ret = net_udp_send(tftp_con, len);

	return ret;
}

static void tftp_handler(char *packet, unsigned len)
{
	uint16_t proto;
	uint16_t *s;
	char *pkt = net_eth_to_udp_payload(packet);
	struct udphdr *udp = net_eth_to_udphdr(packet);
	int ret;

	len = net_eth_to_udplen(packet);
	if (len < 2)
		return;

	len -= 2;

	s = (uint16_t *)pkt;
	proto = *s++;
	pkt = (unsigned char *)s;

	switch (ntohs(proto)) {
	case TFTP_RRQ:
	case TFTP_WRQ:
	default:
		break;
	case TFTP_ACK:
		if (!tftp_push)
			break;

		tftp_block = ntohs(*(uint16_t *)pkt);
		if (tftp_block != tftp_last_block) {
			debug("ack %d != %d\n", tftp_block, tftp_last_block);
			break;
		}
		tftp_block++;
		if (tftp_state == STATE_LAST) {
			tftp_state = STATE_DONE;
			break;
		}
		tftp_con->udp->uh_dport = udp->uh_sport;
		tftp_state = STATE_WDATA;
		tftp_send();
		break;

	case TFTP_OACK:
		debug("Got OACK: %s %s\n", pkt, pkt + strlen(pkt) + 1);
		tftp_server_port = ntohs(udp->uh_sport);
		tftp_con->udp->uh_dport = udp->uh_sport;

		if (tftp_push) {
			/* send first block */
			tftp_state = STATE_WDATA;
			tftp_block = 1;
		} else {
			/* send ACK */
			tftp_state = STATE_OACK;
			tftp_block = 0;
		}

		tftp_send();

		break;
	case TFTP_DATA:
		if (len < 2)
			return;
		len -= 2;
		tftp_block = ntohs(*(uint16_t *)pkt);

		if (tftp_state == STATE_RRQ)
			debug("Server did not acknowledge timeout option!\n");

		if (tftp_state == STATE_RRQ || tftp_state == STATE_OACK) {
			/* first block received */
			tftp_state = STATE_RDATA;
			tftp_con->udp->uh_dport = udp->uh_sport;
			tftp_server_port = ntohs(udp->uh_sport);
			tftp_last_block = 0;

			if (tftp_block != 1) {	/* Assertion */
				printf("error: First block is not block 1 (%ld)\n",
					tftp_block);
				tftp_err = -EINVAL;
				tftp_state = STATE_DONE;
				break;
			}
		}

		if (tftp_block == tftp_last_block)
			/* Same block again; ignore it. */
			break;

		tftp_last_block = tftp_block;

		if (!(tftp_block % 10))
			tftp_size++;

		ret = write(tftp_fd, pkt + 2, len);
		if (ret < 0) {
			perror("write");
			tftp_err = -errno;
			tftp_state = STATE_DONE;
			return;
		}

		/*
		 *	Acknowledge the block just received, which will prompt
		 *	the server for the next one.
		 */
		tftp_send();

		if (len < TFTP_BLOCK_SIZE)
			tftp_state = STATE_DONE;

		break;

	case TFTP_ERROR:
		debug("\nTFTP error: '%s' (%d)\n",
					pkt + 2, ntohs(*(uint16_t *)pkt));
		switch (ntohs(*(uint16_t *)pkt)) {
		case 1: tftp_err = -ENOENT; break;
		case 2: tftp_err = -EACCES; break;
		default: tftp_err = -EINVAL; break;
		}
		tftp_state = STATE_DONE;
		break;
	}
}

static int do_tftpb(struct command *cmdtp, int argc, char *argv[])
{
	char *localfile, *remotefile, *file1, *file2;
	char ip1[16];
	int opt;
	struct stat s;
	unsigned long flags;

	do_tftp_push(0);
	tftp_last_block = 0;
	tftp_size = 0;

	while((opt = getopt(argc, argv, "p")) > 0) {
		switch(opt) {
		case 'p':
			do_tftp_push(1);
			break;
		}
	}

	if (argc <= optind)
		return COMMAND_ERROR_USAGE;

	file1 = argv[optind++];

	if (argc == optind)
		file2 = basename(file1);
	else
		file2 = argv[optind];

	if (tftp_push) {
		localfile = file1;
		remotefile = file2;
		stat(localfile, &s);
		flags = O_RDONLY;
	} else {
		localfile = file2;
		remotefile = file1;
		flags = O_WRONLY | O_CREAT;
	}

	tftp_fd = open(localfile, flags);
	if (tftp_fd < 0) {
		perror("open");
		return 1;
	}

	tftp_con = net_udp_new(net_get_serverip(), TFTP_PORT, tftp_handler);
	if (IS_ERR(tftp_con)) {
		tftp_err = PTR_ERR(tftp_con);
		goto out_close;
	}

	tftp_filename = remotefile;

	printf("TFTP %s server %s ('%s' -> '%s')\n",
			tftp_push ? "to" : "from",
			ip_to_string(net_get_serverip(), ip1),
			file1, file2);

	init_progression_bar(tftp_push ? s.st_size : 0);

	tftp_timer_start = get_time_ns();
	tftp_state = tftp_push ? STATE_WRQ : STATE_RRQ;
	tftp_block = 1;

	tftp_err = tftp_send();
	if (tftp_err)
		goto out_unreg;

	while (tftp_state != STATE_DONE) {
		if (ctrlc()) {
			tftp_err = -EINTR;
			break;
		}
		net_poll();
		if (is_timeout(tftp_timer_start, SECOND)) {
			show_progress(-1);
			tftp_send();
		}
	}
out_unreg:
	net_unregister(tftp_con);
out_close:
	close(tftp_fd);

	if (tftp_err) {
		printf("\ntftp failed: %s\n", strerror(-tftp_err));
		if (!tftp_push)
			unlink(localfile);
	}

	printf("\n");

	return tftp_err == 0 ? 0 : 1;
}

BAREBOX_CMD_HELP_START(tftp)
#ifdef CONFIG_NET_TFTP_PUSH
BAREBOX_CMD_HELP_USAGE("tftp <remotefile> [localfile], tftp -p <localfile> [remotefile]\n")
BAREBOX_CMD_HELP_SHORT("Load a file from or upload to TFTP server.\n")
BAREBOX_CMD_HELP_END
#else
BAREBOX_CMD_HELP_USAGE("tftp <remotefile> [localfile]\n")
BAREBOX_CMD_HELP_SHORT("Load a file from a TFTP server.\n")
BAREBOX_CMD_HELP_END
#endif

/**
 * @page tftp_command

The second file argument can be skipped in which case the first filename
is used (without the directory part).

\<localfile> can be the local filename or a device file under /dev.
This also works for flash memory. Refer to \ref erase_command and \ref
unprotect_command for flash preparation.

\note This command is available only if enabled in menuconfig.
 */

BAREBOX_CMD_START(tftp)
	.cmd		= do_tftpb,
	.usage		=
#ifdef CONFIG_NET_TFTP_PUSH
			"(up-)"
#endif
			"Load file using tftp protocol",
	BAREBOX_CMD_HELP(cmd_tftp_help)
BAREBOX_CMD_END