tftp.c 6.39 KB
Newer Older
Gabe Black's avatar
Gabe Black committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
 * Copyright 2013 Google Inc.
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but without any warranty; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <assert.h>
#include <endian.h>
#include <libpayload.h>
#include <stdint.h>

23
#include "drivers/net/net.h"
24
#include "net/net.h"
Gabe Black's avatar
Gabe Black committed
25
#include "net/uip.h"
26
#include "net/uip_udp_packet.h"
Gabe Black's avatar
Gabe Black committed
27
28
29
30
31
32
33
34
35
36
37
38
#include "netboot/tftp.h"

typedef enum TftpStatus
{
	TftpPending = 0,
	TftpSuccess = 1,
	TftpFailure = 2
} TftpStatus;

static TftpStatus tftp_status;

static uint8_t *tftp_dest;
39
static int tftp_got_response;
Gabe Black's avatar
Gabe Black committed
40
static int tftp_blocknum;
41
static uint32_t tftp_total_size;
42
static uint32_t tftp_max_size;
Gabe Black's avatar
Gabe Black committed
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

typedef struct TftpAckPacket
{
	uint16_t opcode;
	uint16_t block;
} TftpAckPacket;

static void tftp_print_error_pkt(void)
{
	if (uip_datalen() >= 4) {
		uint16_t error;
		memcpy(&error, (uint8_t *)uip_appdata + 2,
			sizeof(error));
		error = ntohw(error);
		printf("Error code %d", error);
		switch (error) {
		case TftpUndefined:
			printf(" (Undefined)\n");
			break;
		case TftpFileNotFound:
			printf(" (File not found)\n");
			break;
		case TftpAccessViolation:
			printf(" (Access violation)\n");
			break;
		case TftpNoSpace:
			printf(" (Not enough space)\n");
			break;
		case TftpIllegalOp:
			printf(" (Illegal operation)\n");
			break;
		case TftpUnknownId:
			printf(" (Unknown transfer ID)\n");
			break;
		case TftpFileExists:
			printf(" (File already exists)\n");
			break;
		case TftpNoSuchUser:
			printf(" (No such user)\n");
			break;
		default:
			printf("\n");
		}
	}
	if (uip_datalen() > 4) {
		// Copy out the error message so we can null terminate it.
		int message_len = uip_datalen() - 4 + 1;
90
		char *message = xmalloc(message_len);
Gabe Black's avatar
Gabe Black committed
91
92
93
94
95
96
97
98

		message[message_len - 1] = 0;
		memcpy(message, uip_appdata, message_len - 1);
		printf("Error message: %s\n", message);
		free(message);
	}
}

99
static void tftp_callback(void)
Gabe Black's avatar
Gabe Black committed
100
101
102
103
104
105
106
107
108
109
{
	// If there isn't at least an opcode, ignore the packet.
	if (!uip_newdata())
		return;
	if (uip_datalen() < 2)
		return;

	if (!uip_udp_conn->rport) {
		int srcport_offset = offsetof(struct uip_udpip_hdr, srcport);
		memcpy(&uip_udp_conn->rport,
110
			&uip_buf[CONFIG_UIP_LLH_LEN + srcport_offset],
Gabe Black's avatar
Gabe Black committed
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
			sizeof(uip_udp_conn->rport));
	}

	// Extract the opcode.
	uint16_t opcode;
	memcpy(&opcode, uip_appdata, sizeof(opcode));
	opcode = ntohw(opcode);

	// If there was an error, report it and stop the transfer.
	if (opcode == TftpError) {
		tftp_status = TftpFailure;
		printf(" error!\n");
		tftp_print_error_pkt();
		return;
	}

	// We should only get data packets. Those are at least 4 bytes long.
	if (opcode != TftpData || uip_datalen() < 4)
		return;

	// Get the block number.
	uint16_t blocknum;
	memcpy(&blocknum, (uint8_t *)uip_appdata + 2, sizeof(blocknum));
	blocknum = ntohw(blocknum);

136
137
138
	// Ignore blocks which are duplicated or out of order, taking into
	// account 16-bit block number overflow.
	if (blocknum != (tftp_blocknum & 0xFFFF))
Gabe Black's avatar
Gabe Black committed
139
140
141
142
143
144
145
146
147
		return;

	void *new_data = (uint8_t *)uip_appdata + 4;
	int new_data_len = uip_datalen() - 4;

	// If the block is too big, reject it.
	if (new_data_len > TftpMaxBlockSize)
		return;

148
149
150
151
152
153
154
	// If we're out of space give up.
	if (new_data_len > tftp_max_size - tftp_total_size) {
		tftp_status = TftpFailure;
		printf("TFTP transfer too large.\n");
		return;
	}

155
156
157
158
159
160
161
162
163
164
#if 1  /* |<-- DEBUG ---- */
	const int blocknum_debug = tftp_blocknum - 1;

	if (!(blocknum_debug % 1024))
		printf("\n%08x ", tftp_total_size);

	if (!(blocknum_debug % 16))
		printf("#");
#endif  /* ---- DEBUG -->| */

Gabe Black's avatar
Gabe Black committed
165
166
167
168
169
	// If there's any data, copy it in.
	if (new_data_len) {
		memcpy(tftp_dest, new_data, new_data_len);
		tftp_dest += new_data_len;
	}
170
	tftp_total_size += new_data_len;
Gabe Black's avatar
Gabe Black committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188

	// If this block was less than the maximum size, the transfer is done.
	if (new_data_len < TftpMaxBlockSize) {
		tftp_status = TftpSuccess;
		return;
	}

	// Prepare an ack.
	TftpAckPacket ack = {
		htonw(TftpAck),
		htonw(tftp_blocknum)
	};
	memcpy(uip_appdata, &ack, sizeof(ack));
	uip_udp_send(sizeof(ack));

	// Move on to the next block.
	tftp_blocknum++;

189
	tftp_got_response = 1;
Gabe Black's avatar
Gabe Black committed
190
191
}

192
int tftp_read(void *dest, uip_ipaddr_t *server_ip, const char *bootfile,
193
	uint32_t *size, uint32_t max_size)
Gabe Black's avatar
Gabe Black committed
194
195
196
197
198
199
200
{
	// Build the read request packet.
	uint16_t opcode = htonw(TftpReadReq);
	int opcode_len = sizeof(opcode);

	int name_len = strlen(bootfile) + 1;

201
	const char mode[] = "Octet";
Gabe Black's avatar
Gabe Black committed
202
203
204
	int mode_len = sizeof(mode);

	int read_req_len = opcode_len + name_len + mode_len;
205
	uint8_t *read_req = xmalloc(read_req_len);
Gabe Black's avatar
Gabe Black committed
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229

	memcpy(read_req, &opcode, opcode_len);
	memcpy(read_req + opcode_len, bootfile, name_len);
	memcpy(read_req + opcode_len + name_len, mode, mode_len);

	// Set up the UDP connection.
	struct uip_udp_conn *conn = uip_udp_new(server_ip, htonw(TftpPort));
	if (!conn) {
		printf("Failed to set up UDP connection.\n");
		free(read_req);
		return -1;
	}

	// Send the request.
	printf("Sending tftp read request... ");
	uip_udp_packet_send(conn, read_req, read_req_len);
	conn->rport = 0;
	printf("done.\n");

	// Prepare for the transfer.
	printf("Waiting for the transfer... ");
	tftp_status = TftpPending;
	tftp_dest = dest;
	tftp_blocknum = 1;
230
	tftp_total_size = 0;
231
	tftp_max_size = max_size;
Gabe Black's avatar
Gabe Black committed
232
233
234

	// Poll the network driver until the transaction is done.

235
	net_set_callback(&tftp_callback);
236
237
238
239
240
241
242
	while (tftp_status == TftpPending) {
		tftp_got_response = 0;
		net_poll();
		if (tftp_got_response)
			continue;

		// No response. Resend our last packet and try again.
Gabe Black's avatar
Gabe Black committed
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
		if (tftp_blocknum == 1) {
			// Resend the read request.
			conn->rport = htonw(TftpPort);
			uip_udp_packet_send(conn, read_req, read_req_len);
			conn->rport = 0;
		} else {
			// Resend the last ack.
			TftpAckPacket ack = {
				htonw(TftpAck),
				htonw(tftp_blocknum - 1)
			};
			uip_udp_packet_send(conn, &ack, sizeof(ack));
		}
	}
	uip_udp_remove(conn);
	free(read_req);
259
	net_set_callback(NULL);
Gabe Black's avatar
Gabe Black committed
260
261
262
263
264
265

	// See what happened.
	if (tftp_status == TftpFailure) {
		// The error was printed when it was received.
		return -1;
	} else {
266
267
		if (size)
			*size = tftp_total_size;
Gabe Black's avatar
Gabe Black committed
268
269
270
271
		printf(" done.\n");
		return 0;
	}
}