]>
Commit | Line | Data |
---|---|---|
fe8c2806 | 1 | /* |
2f09413f LC |
2 | * Copyright 1994, 1995, 2000 Neil Russell. |
3 | * (See License) | |
4 | * Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de | |
fe8c2806 WD |
5 | */ |
6 | ||
7 | #include <common.h> | |
8 | #include <command.h> | |
9 | #include <net.h> | |
10 | #include "tftp.h" | |
11 | #include "bootp.h" | |
12 | ||
2f09413f LC |
13 | /* Well known TFTP port # */ |
14 | #define WELL_KNOWN_PORT 69 | |
15 | /* Millisecs to timeout for lost pkt */ | |
16 | #define TIMEOUT 5000UL | |
fe8c2806 | 17 | #ifndef CONFIG_NET_RETRY_COUNT |
2f09413f LC |
18 | /* # of timeouts before giving up */ |
19 | # define TIMEOUT_COUNT 10 | |
fe8c2806 WD |
20 | #else |
21 | # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2) | |
22 | #endif | |
2f09413f LC |
23 | /* Number of "loading" hashes per line (for checking the image size) */ |
24 | #define HASHES_PER_LINE 65 | |
fe8c2806 WD |
25 | |
26 | /* | |
27 | * TFTP operations. | |
28 | */ | |
29 | #define TFTP_RRQ 1 | |
30 | #define TFTP_WRQ 2 | |
31 | #define TFTP_DATA 3 | |
32 | #define TFTP_ACK 4 | |
33 | #define TFTP_ERROR 5 | |
fbe4b5cb | 34 | #define TFTP_OACK 6 |
fe8c2806 | 35 | |
e83cc063 BS |
36 | static ulong TftpTimeoutMSecs = TIMEOUT; |
37 | static int TftpTimeoutCountMax = TIMEOUT_COUNT; | |
38 | ||
39 | /* | |
40 | * These globals govern the timeout behavior when attempting a connection to a | |
41 | * TFTP server. TftpRRQTimeoutMSecs specifies the number of milliseconds to | |
42 | * wait for the server to respond to initial connection. Second global, | |
43 | * TftpRRQTimeoutCountMax, gives the number of such connection retries. | |
44 | * TftpRRQTimeoutCountMax must be non-negative and TftpRRQTimeoutMSecs must be | |
45 | * positive. The globals are meant to be set (and restored) by code needing | |
46 | * non-standard timeout behavior when initiating a TFTP transfer. | |
47 | */ | |
48 | ulong TftpRRQTimeoutMSecs = TIMEOUT; | |
49 | int TftpRRQTimeoutCountMax = TIMEOUT_COUNT; | |
50 | ||
aafda38f RB |
51 | enum { |
52 | TFTP_ERR_UNDEFINED = 0, | |
53 | TFTP_ERR_FILE_NOT_FOUND = 1, | |
54 | TFTP_ERR_ACCESS_DENIED = 2, | |
55 | TFTP_ERR_DISK_FULL = 3, | |
56 | TFTP_ERR_UNEXPECTED_OPCODE = 4, | |
57 | TFTP_ERR_UNKNOWN_TRANSFER_ID = 5, | |
58 | TFTP_ERR_FILE_ALREADY_EXISTS = 6, | |
59 | }; | |
60 | ||
a93907c4 | 61 | static IPaddr_t TftpServerIP; |
2f09413f LC |
62 | /* The UDP port at their end */ |
63 | static int TftpServerPort; | |
64 | /* The UDP port at our end */ | |
65 | static int TftpOurPort; | |
fe8c2806 | 66 | static int TftpTimeoutCount; |
2f09413f LC |
67 | /* packet sequence number */ |
68 | static ulong TftpBlock; | |
69 | /* last packet sequence number received */ | |
70 | static ulong TftpLastBlock; | |
71 | /* count of sequence number wraparounds */ | |
72 | static ulong TftpBlockWrap; | |
73 | /* memory offset due to wrapping */ | |
74 | static ulong TftpBlockWrapOffset; | |
fe8c2806 | 75 | static int TftpState; |
4fccb818 | 76 | #ifdef CONFIG_TFTP_TSIZE |
2f09413f LC |
77 | /* The file size reported by the server */ |
78 | static int TftpTsize; | |
79 | /* The number of hashes we printed */ | |
80 | static short TftpNumchars; | |
4fccb818 | 81 | #endif |
3f85ce27 | 82 | |
fe8c2806 WD |
83 | #define STATE_RRQ 1 |
84 | #define STATE_DATA 2 | |
85 | #define STATE_TOO_LARGE 3 | |
86 | #define STATE_BAD_MAGIC 4 | |
fbe4b5cb | 87 | #define STATE_OACK 5 |
fe8c2806 | 88 | |
2f09413f LC |
89 | /* default TFTP block size */ |
90 | #define TFTP_BLOCK_SIZE 512 | |
91 | /* sequence number is 16 bit */ | |
92 | #define TFTP_SEQUENCE_SIZE ((ulong)(1<<16)) | |
3f85ce27 | 93 | |
fe8c2806 WD |
94 | #define DEFAULT_NAME_LEN (8 + 4 + 1) |
95 | static char default_filename[DEFAULT_NAME_LEN]; | |
a93907c4 JCPV |
96 | |
97 | #ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN | |
98 | #define MAX_LEN 128 | |
99 | #else | |
100 | #define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN | |
101 | #endif | |
102 | ||
103 | static char tftp_filename[MAX_LEN]; | |
fe8c2806 | 104 | |
6d0f6bcf | 105 | #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP |
e6f2e902 | 106 | extern flash_info_t flash_info[]; |
fe8c2806 WD |
107 | #endif |
108 | ||
85eb5caf WD |
109 | /* 512 is poor choice for ethernet, MTU is typically 1500. |
110 | * Minus eth.hdrs thats 1468. Can get 2x better throughput with | |
53a5c424 | 111 | * almost-MTU block sizes. At least try... fall back to 512 if need be. |
89ba81d1 | 112 | * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file) |
53a5c424 | 113 | */ |
89ba81d1 AR |
114 | #ifdef CONFIG_TFTP_BLOCKSIZE |
115 | #define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE | |
116 | #else | |
53a5c424 | 117 | #define TFTP_MTU_BLOCKSIZE 1468 |
89ba81d1 AR |
118 | #endif |
119 | ||
c718b143 LC |
120 | static unsigned short TftpBlkSize = TFTP_BLOCK_SIZE; |
121 | static unsigned short TftpBlkSizeOption = TFTP_MTU_BLOCKSIZE; | |
53a5c424 DU |
122 | |
123 | #ifdef CONFIG_MCAST_TFTP | |
124 | #include <malloc.h> | |
125 | #define MTFTP_BITMAPSIZE 0x1000 | |
126 | static unsigned *Bitmap; | |
c718b143 | 127 | static int PrevBitmapHole, Mapsize = MTFTP_BITMAPSIZE; |
9bb0a1bf LC |
128 | static uchar ProhibitMcast, MasterClient; |
129 | static uchar Multicast; | |
53a5c424 DU |
130 | extern IPaddr_t Mcast_addr; |
131 | static int Mcast_port; | |
132 | static ulong TftpEndingBlock; /* can get 'last' block before done..*/ | |
133 | ||
c718b143 | 134 | static void parse_multicast_oack(char *pkt, int len); |
53a5c424 DU |
135 | |
136 | static void | |
137 | mcast_cleanup(void) | |
138 | { | |
6d2231e8 LC |
139 | if (Mcast_addr) |
140 | eth_mcast_join(Mcast_addr, 0); | |
141 | if (Bitmap) | |
142 | free(Bitmap); | |
c718b143 | 143 | Bitmap = NULL; |
53a5c424 DU |
144 | Mcast_addr = Multicast = Mcast_port = 0; |
145 | TftpEndingBlock = -1; | |
146 | } | |
147 | ||
148 | #endif /* CONFIG_MCAST_TFTP */ | |
149 | ||
fe8c2806 | 150 | static __inline__ void |
2e320257 | 151 | store_block(unsigned block, uchar *src, unsigned len) |
fe8c2806 | 152 | { |
53a5c424 | 153 | ulong offset = block * TftpBlkSize + TftpBlockWrapOffset; |
3f85ce27 | 154 | ulong newsize = offset + len; |
6d0f6bcf | 155 | #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP |
fe8c2806 WD |
156 | int i, rc = 0; |
157 | ||
c718b143 | 158 | for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) { |
fe8c2806 | 159 | /* start address in flash? */ |
be1b0d27 JF |
160 | if (flash_info[i].flash_id == FLASH_UNKNOWN) |
161 | continue; | |
fe8c2806 WD |
162 | if (load_addr + offset >= flash_info[i].start[0]) { |
163 | rc = 1; | |
164 | break; | |
165 | } | |
166 | } | |
167 | ||
168 | if (rc) { /* Flash is destination for this packet */ | |
c718b143 | 169 | rc = flash_write((char *)src, (ulong)(load_addr+offset), len); |
fe8c2806 | 170 | if (rc) { |
c718b143 | 171 | flash_perror(rc); |
fe8c2806 WD |
172 | NetState = NETLOOP_FAIL; |
173 | return; | |
174 | } | |
175 | } | |
176 | else | |
6d0f6bcf | 177 | #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */ |
fe8c2806 WD |
178 | { |
179 | (void)memcpy((void *)(load_addr + offset), src, len); | |
180 | } | |
53a5c424 DU |
181 | #ifdef CONFIG_MCAST_TFTP |
182 | if (Multicast) | |
183 | ext2_set_bit(block, Bitmap); | |
184 | #endif | |
fe8c2806 WD |
185 | |
186 | if (NetBootFileXferSize < newsize) | |
187 | NetBootFileXferSize = newsize; | |
188 | } | |
189 | ||
c718b143 LC |
190 | static void TftpSend(void); |
191 | static void TftpTimeout(void); | |
fe8c2806 WD |
192 | |
193 | /**********************************************************************/ | |
194 | ||
195 | static void | |
c718b143 | 196 | TftpSend(void) |
fe8c2806 | 197 | { |
2e320257 LC |
198 | volatile uchar *pkt; |
199 | volatile uchar *xp; | |
200 | int len = 0; | |
7bc5ee07 | 201 | volatile ushort *s; |
fe8c2806 | 202 | |
85eb5caf | 203 | #ifdef CONFIG_MCAST_TFTP |
53a5c424 | 204 | /* Multicast TFTP.. non-MasterClients do not ACK data. */ |
85eb5caf WD |
205 | if (Multicast |
206 | && (TftpState == STATE_DATA) | |
207 | && (MasterClient == 0)) | |
53a5c424 DU |
208 | return; |
209 | #endif | |
fe8c2806 WD |
210 | /* |
211 | * We will always be sending some sort of packet, so | |
212 | * cobble together the packet headers now. | |
213 | */ | |
a3d991bd | 214 | pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE; |
fe8c2806 WD |
215 | |
216 | switch (TftpState) { | |
217 | ||
218 | case STATE_RRQ: | |
219 | xp = pkt; | |
7bc5ee07 WD |
220 | s = (ushort *)pkt; |
221 | *s++ = htons(TFTP_RRQ); | |
222 | pkt = (uchar *)s; | |
c718b143 | 223 | strcpy((char *)pkt, tftp_filename); |
fe8c2806 | 224 | pkt += strlen(tftp_filename) + 1; |
c718b143 | 225 | strcpy((char *)pkt, "octet"); |
fe8c2806 | 226 | pkt += 5 /*strlen("octet")*/ + 1; |
c718b143 | 227 | strcpy((char *)pkt, "timeout"); |
fbe4b5cb | 228 | pkt += 7 /*strlen("timeout")*/ + 1; |
c96f86ee | 229 | sprintf((char *)pkt, "%lu", TftpTimeoutMSecs / 1000); |
0ebf04c6 | 230 | debug("send option \"timeout %s\"\n", (char *)pkt); |
fbe4b5cb | 231 | pkt += strlen((char *)pkt) + 1; |
4fccb818 RG |
232 | #ifdef CONFIG_TFTP_TSIZE |
233 | memcpy((char *)pkt, "tsize\0000\0", 8); | |
234 | pkt += 8; | |
235 | #endif | |
53a5c424 | 236 | /* try for more effic. blk size */ |
c718b143 LC |
237 | pkt += sprintf((char *)pkt, "blksize%c%d%c", |
238 | 0, TftpBlkSizeOption, 0); | |
85eb5caf | 239 | #ifdef CONFIG_MCAST_TFTP |
53a5c424 | 240 | /* Check all preconditions before even trying the option */ |
85eb5caf | 241 | if (!ProhibitMcast |
c718b143 | 242 | && (Bitmap = malloc(Mapsize)) |
53a5c424 DU |
243 | && eth_get_dev()->mcast) { |
244 | free(Bitmap); | |
c718b143 LC |
245 | Bitmap = NULL; |
246 | pkt += sprintf((char *)pkt, "multicast%c%c", 0, 0); | |
53a5c424 DU |
247 | } |
248 | #endif /* CONFIG_MCAST_TFTP */ | |
fe8c2806 WD |
249 | len = pkt - xp; |
250 | break; | |
251 | ||
fbe4b5cb | 252 | case STATE_OACK: |
53a5c424 DU |
253 | #ifdef CONFIG_MCAST_TFTP |
254 | /* My turn! Start at where I need blocks I missed.*/ | |
255 | if (Multicast) | |
c718b143 LC |
256 | TftpBlock = ext2_find_next_zero_bit(Bitmap, |
257 | (Mapsize*8), 0); | |
53a5c424 DU |
258 | /*..falling..*/ |
259 | #endif | |
260 | case STATE_DATA: | |
fe8c2806 | 261 | xp = pkt; |
7bc5ee07 WD |
262 | s = (ushort *)pkt; |
263 | *s++ = htons(TFTP_ACK); | |
264 | *s++ = htons(TftpBlock); | |
265 | pkt = (uchar *)s; | |
fe8c2806 WD |
266 | len = pkt - xp; |
267 | break; | |
268 | ||
269 | case STATE_TOO_LARGE: | |
270 | xp = pkt; | |
7bc5ee07 WD |
271 | s = (ushort *)pkt; |
272 | *s++ = htons(TFTP_ERROR); | |
273 | *s++ = htons(3); | |
274 | pkt = (uchar *)s; | |
c718b143 | 275 | strcpy((char *)pkt, "File too large"); |
fe8c2806 WD |
276 | pkt += 14 /*strlen("File too large")*/ + 1; |
277 | len = pkt - xp; | |
278 | break; | |
279 | ||
280 | case STATE_BAD_MAGIC: | |
281 | xp = pkt; | |
7bc5ee07 WD |
282 | s = (ushort *)pkt; |
283 | *s++ = htons(TFTP_ERROR); | |
284 | *s++ = htons(2); | |
285 | pkt = (uchar *)s; | |
c718b143 | 286 | strcpy((char *)pkt, "File has bad magic"); |
fe8c2806 WD |
287 | pkt += 18 /*strlen("File has bad magic")*/ + 1; |
288 | len = pkt - xp; | |
289 | break; | |
290 | } | |
291 | ||
2f09413f LC |
292 | NetSendUDPPacket(NetServerEther, TftpServerIP, TftpServerPort, |
293 | TftpOurPort, len); | |
fe8c2806 WD |
294 | } |
295 | ||
296 | ||
297 | static void | |
03eb129f LC |
298 | TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, |
299 | unsigned len) | |
fe8c2806 WD |
300 | { |
301 | ushort proto; | |
7bc5ee07 | 302 | ushort *s; |
ff13ac8c | 303 | int i; |
fe8c2806 WD |
304 | |
305 | if (dest != TftpOurPort) { | |
53a5c424 | 306 | #ifdef CONFIG_MCAST_TFTP |
85eb5caf | 307 | if (Multicast |
53a5c424 DU |
308 | && (!Mcast_port || (dest != Mcast_port))) |
309 | #endif | |
0bdd8acc | 310 | return; |
fe8c2806 | 311 | } |
7bc325a1 | 312 | if (TftpState != STATE_RRQ && src != TftpServerPort) |
fe8c2806 | 313 | return; |
fe8c2806 | 314 | |
7bc325a1 | 315 | if (len < 2) |
fe8c2806 | 316 | return; |
fe8c2806 WD |
317 | len -= 2; |
318 | /* warning: don't use increment (++) in ntohs() macros!! */ | |
7bc5ee07 WD |
319 | s = (ushort *)pkt; |
320 | proto = *s++; | |
321 | pkt = (uchar *)s; | |
fe8c2806 WD |
322 | switch (ntohs(proto)) { |
323 | ||
324 | case TFTP_RRQ: | |
325 | case TFTP_WRQ: | |
326 | case TFTP_ACK: | |
327 | break; | |
328 | default: | |
329 | break; | |
330 | ||
fbe4b5cb | 331 | case TFTP_OACK: |
d371708a WD |
332 | debug("Got OACK: %s %s\n", |
333 | pkt, | |
334 | pkt + strlen((char *)pkt) + 1); | |
fbe4b5cb WD |
335 | TftpState = STATE_OACK; |
336 | TftpServerPort = src; | |
60174746 WD |
337 | /* |
338 | * Check for 'blksize' option. | |
339 | * Careful: "i" is signed, "len" is unsigned, thus | |
340 | * something like "len-8" may give a *huge* number | |
341 | */ | |
c718b143 | 342 | for (i = 0; i+8 < len; i++) { |
2e320257 | 343 | if (strcmp((char *)pkt+i, "blksize") == 0) { |
ff13ac8c | 344 | TftpBlkSize = (unsigned short) |
2e320257 | 345 | simple_strtoul((char *)pkt+i+8, NULL, |
c718b143 | 346 | 10); |
0ebf04c6 | 347 | debug("Blocksize ack: %s, %d\n", |
2e320257 | 348 | (char *)pkt+i+8, TftpBlkSize); |
ff13ac8c | 349 | } |
4fccb818 | 350 | #ifdef CONFIG_TFTP_TSIZE |
2e320257 LC |
351 | if (strcmp((char *)pkt+i, "tsize") == 0) { |
352 | TftpTsize = simple_strtoul((char *)pkt+i+6, | |
2f09413f | 353 | NULL, 10); |
4fccb818 | 354 | debug("size = %s, %d\n", |
2e320257 | 355 | (char *)pkt+i+6, TftpTsize); |
4fccb818 RG |
356 | } |
357 | #endif | |
53a5c424 DU |
358 | } |
359 | #ifdef CONFIG_MCAST_TFTP | |
c718b143 | 360 | parse_multicast_oack((char *)pkt, len-1); |
85eb5caf | 361 | if ((Multicast) && (!MasterClient)) |
53a5c424 DU |
362 | TftpState = STATE_DATA; /* passive.. */ |
363 | else | |
364 | #endif | |
c718b143 | 365 | TftpSend(); /* Send ACK */ |
fbe4b5cb | 366 | break; |
fe8c2806 WD |
367 | case TFTP_DATA: |
368 | if (len < 2) | |
369 | return; | |
370 | len -= 2; | |
371 | TftpBlock = ntohs(*(ushort *)pkt); | |
3f85ce27 WD |
372 | |
373 | /* | |
cd0a9de6 WD |
374 | * RFC1350 specifies that the first data packet will |
375 | * have sequence number 1. If we receive a sequence | |
376 | * number of 0 this means that there was a wrap | |
377 | * around of the (16 bit) counter. | |
3f85ce27 WD |
378 | */ |
379 | if (TftpBlock == 0) { | |
380 | TftpBlockWrap++; | |
2f09413f LC |
381 | TftpBlockWrapOffset += |
382 | TftpBlkSize * TFTP_SEQUENCE_SIZE; | |
c718b143 | 383 | printf("\n\t %lu MB received\n\t ", |
2f09413f | 384 | TftpBlockWrapOffset>>20); |
4fccb818 RG |
385 | } |
386 | #ifdef CONFIG_TFTP_TSIZE | |
387 | else if (TftpTsize) { | |
2f09413f LC |
388 | while (TftpNumchars < |
389 | NetBootFileXferSize * 50 / TftpTsize) { | |
4fccb818 RG |
390 | putc('#'); |
391 | TftpNumchars++; | |
392 | } | |
393 | } | |
394 | #endif | |
395 | else { | |
7bc325a1 | 396 | if (((TftpBlock - 1) % 10) == 0) |
c718b143 | 397 | putc('#'); |
7bc325a1 | 398 | else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0) |
c718b143 | 399 | puts("\n\t "); |
fe8c2806 WD |
400 | } |
401 | ||
0ebf04c6 RG |
402 | if (TftpState == STATE_RRQ) |
403 | debug("Server did not acknowledge timeout option!\n"); | |
fbe4b5cb WD |
404 | |
405 | if (TftpState == STATE_RRQ || TftpState == STATE_OACK) { | |
3f85ce27 | 406 | /* first block received */ |
fe8c2806 WD |
407 | TftpState = STATE_DATA; |
408 | TftpServerPort = src; | |
409 | TftpLastBlock = 0; | |
3f85ce27 WD |
410 | TftpBlockWrap = 0; |
411 | TftpBlockWrapOffset = 0; | |
fe8c2806 | 412 | |
53a5c424 DU |
413 | #ifdef CONFIG_MCAST_TFTP |
414 | if (Multicast) { /* start!=1 common if mcast */ | |
415 | TftpLastBlock = TftpBlock - 1; | |
416 | } else | |
417 | #endif | |
fe8c2806 | 418 | if (TftpBlock != 1) { /* Assertion */ |
c718b143 LC |
419 | printf("\nTFTP error: " |
420 | "First block is not block 1 (%ld)\n" | |
421 | "Starting again\n\n", | |
fe8c2806 | 422 | TftpBlock); |
c718b143 | 423 | NetStartAgain(); |
fe8c2806 WD |
424 | break; |
425 | } | |
426 | } | |
427 | ||
428 | if (TftpBlock == TftpLastBlock) { | |
429 | /* | |
430 | * Same block again; ignore it. | |
431 | */ | |
432 | break; | |
433 | } | |
434 | ||
435 | TftpLastBlock = TftpBlock; | |
e83cc063 | 436 | TftpTimeoutCountMax = TIMEOUT_COUNT; |
c718b143 | 437 | NetSetTimeout(TftpTimeoutMSecs, TftpTimeout); |
fe8c2806 | 438 | |
c718b143 | 439 | store_block(TftpBlock - 1, pkt + 2, len); |
fe8c2806 WD |
440 | |
441 | /* | |
442 | * Acknoledge the block just received, which will prompt | |
443 | * the server for the next one. | |
444 | */ | |
53a5c424 | 445 | #ifdef CONFIG_MCAST_TFTP |
85eb5caf WD |
446 | /* if I am the MasterClient, actively calculate what my next |
447 | * needed block is; else I'm passive; not ACKING | |
a93907c4 | 448 | */ |
53a5c424 DU |
449 | if (Multicast) { |
450 | if (len < TftpBlkSize) { | |
451 | TftpEndingBlock = TftpBlock; | |
452 | } else if (MasterClient) { | |
85eb5caf | 453 | TftpBlock = PrevBitmapHole = |
53a5c424 DU |
454 | ext2_find_next_zero_bit( |
455 | Bitmap, | |
456 | (Mapsize*8), | |
457 | PrevBitmapHole); | |
458 | if (TftpBlock > ((Mapsize*8) - 1)) { | |
c718b143 | 459 | printf("tftpfile too big\n"); |
53a5c424 | 460 | /* try to double it and retry */ |
c718b143 | 461 | Mapsize <<= 1; |
53a5c424 | 462 | mcast_cleanup(); |
c718b143 | 463 | NetStartAgain(); |
53a5c424 DU |
464 | return; |
465 | } | |
466 | TftpLastBlock = TftpBlock; | |
467 | } | |
468 | } | |
469 | #endif | |
c718b143 | 470 | TftpSend(); |
fe8c2806 | 471 | |
53a5c424 DU |
472 | #ifdef CONFIG_MCAST_TFTP |
473 | if (Multicast) { | |
474 | if (MasterClient && (TftpBlock >= TftpEndingBlock)) { | |
c718b143 | 475 | puts("\nMulticast tftp done\n"); |
53a5c424 DU |
476 | mcast_cleanup(); |
477 | NetState = NETLOOP_SUCCESS; | |
85eb5caf | 478 | } |
53a5c424 DU |
479 | } |
480 | else | |
481 | #endif | |
482 | if (len < TftpBlkSize) { | |
fe8c2806 WD |
483 | /* |
484 | * We received the whole thing. Try to | |
485 | * run it. | |
486 | */ | |
4fccb818 | 487 | #ifdef CONFIG_TFTP_TSIZE |
2f09413f | 488 | /* Print hash marks for the last packet received */ |
4fccb818 RG |
489 | while (TftpTsize && TftpNumchars < 49) { |
490 | putc('#'); | |
491 | TftpNumchars++; | |
492 | } | |
493 | #endif | |
c718b143 | 494 | puts("\ndone\n"); |
fe8c2806 WD |
495 | NetState = NETLOOP_SUCCESS; |
496 | } | |
497 | break; | |
498 | ||
499 | case TFTP_ERROR: | |
c718b143 LC |
500 | printf("\nTFTP error: '%s' (%d)\n", |
501 | pkt + 2, ntohs(*(ushort *)pkt)); | |
aafda38f RB |
502 | |
503 | switch (ntohs(*(ushort *)pkt)) { | |
504 | case TFTP_ERR_FILE_NOT_FOUND: | |
505 | case TFTP_ERR_ACCESS_DENIED: | |
506 | puts("Not retrying...\n"); | |
507 | eth_halt(); | |
508 | NetState = NETLOOP_FAIL; | |
509 | break; | |
510 | case TFTP_ERR_UNDEFINED: | |
511 | case TFTP_ERR_DISK_FULL: | |
512 | case TFTP_ERR_UNEXPECTED_OPCODE: | |
513 | case TFTP_ERR_UNKNOWN_TRANSFER_ID: | |
514 | case TFTP_ERR_FILE_ALREADY_EXISTS: | |
515 | default: | |
516 | puts("Starting again\n\n"); | |
53a5c424 | 517 | #ifdef CONFIG_MCAST_TFTP |
aafda38f | 518 | mcast_cleanup(); |
53a5c424 | 519 | #endif |
aafda38f RB |
520 | NetStartAgain(); |
521 | break; | |
522 | } | |
fe8c2806 WD |
523 | break; |
524 | } | |
525 | } | |
526 | ||
527 | ||
528 | static void | |
c718b143 | 529 | TftpTimeout(void) |
fe8c2806 | 530 | { |
e83cc063 | 531 | if (++TftpTimeoutCount > TftpTimeoutCountMax) { |
c718b143 | 532 | puts("\nRetry count exceeded; starting again\n"); |
53a5c424 DU |
533 | #ifdef CONFIG_MCAST_TFTP |
534 | mcast_cleanup(); | |
535 | #endif | |
c718b143 | 536 | NetStartAgain(); |
fe8c2806 | 537 | } else { |
c718b143 LC |
538 | puts("T "); |
539 | NetSetTimeout(TftpTimeoutMSecs, TftpTimeout); | |
540 | TftpSend(); | |
fe8c2806 WD |
541 | } |
542 | } | |
543 | ||
544 | ||
545 | void | |
c718b143 | 546 | TftpStart(void) |
fe8c2806 | 547 | { |
ecb0ccd9 | 548 | char *ep; /* Environment pointer */ |
89ba81d1 | 549 | |
c96f86ee WD |
550 | /* |
551 | * Allow the user to choose TFTP blocksize and timeout. | |
552 | * TFTP protocol has a minimal timeout of 1 second. | |
553 | */ | |
2cb53608 LC |
554 | ep = getenv("tftpblocksize"); |
555 | if (ep != NULL) | |
89ba81d1 | 556 | TftpBlkSizeOption = simple_strtol(ep, NULL, 10); |
c96f86ee | 557 | |
2cb53608 LC |
558 | ep = getenv("tftptimeout"); |
559 | if (ep != NULL) | |
c96f86ee WD |
560 | TftpTimeoutMSecs = simple_strtol(ep, NULL, 10); |
561 | ||
562 | if (TftpTimeoutMSecs < 1000) { | |
563 | printf("TFTP timeout (%ld ms) too low, " | |
564 | "set minimum = 1000 ms\n", | |
565 | TftpTimeoutMSecs); | |
566 | TftpTimeoutMSecs = 1000; | |
567 | } | |
568 | ||
569 | debug("TFTP blocksize = %i, timeout = %ld ms\n", | |
570 | TftpBlkSizeOption, TftpTimeoutMSecs); | |
ecb0ccd9 | 571 | |
a93907c4 | 572 | TftpServerIP = NetServerIP; |
fe8c2806 | 573 | if (BootFile[0] == '\0') { |
fe8c2806 | 574 | sprintf(default_filename, "%02lX%02lX%02lX%02lX.img", |
c43352cc WD |
575 | NetOurIP & 0xFF, |
576 | (NetOurIP >> 8) & 0xFF, | |
577 | (NetOurIP >> 16) & 0xFF, | |
c718b143 | 578 | (NetOurIP >> 24) & 0xFF); |
a93907c4 JCPV |
579 | |
580 | strncpy(tftp_filename, default_filename, MAX_LEN); | |
581 | tftp_filename[MAX_LEN-1] = 0; | |
fe8c2806 | 582 | |
c718b143 | 583 | printf("*** Warning: no boot file name; using '%s'\n", |
fe8c2806 WD |
584 | tftp_filename); |
585 | } else { | |
c718b143 | 586 | char *p = strchr(BootFile, ':'); |
a93907c4 JCPV |
587 | |
588 | if (p == NULL) { | |
589 | strncpy(tftp_filename, BootFile, MAX_LEN); | |
590 | tftp_filename[MAX_LEN-1] = 0; | |
591 | } else { | |
c718b143 | 592 | TftpServerIP = string_to_ip(BootFile); |
6a86bb6c | 593 | strncpy(tftp_filename, p + 1, MAX_LEN); |
a93907c4 JCPV |
594 | tftp_filename[MAX_LEN-1] = 0; |
595 | } | |
fe8c2806 WD |
596 | } |
597 | ||
5bb226e8 | 598 | #if defined(CONFIG_NET_MULTI) |
c718b143 | 599 | printf("Using %s device\n", eth_get_name()); |
5bb226e8 | 600 | #endif |
b6446b67 MF |
601 | printf("TFTP from server %pI4" |
602 | "; our IP address is %pI4", &TftpServerIP, &NetOurIP); | |
fe8c2806 WD |
603 | |
604 | /* Check if we need to send across this subnet */ | |
605 | if (NetOurGatewayIP && NetOurSubnetMask) { | |
0bdd8acc LC |
606 | IPaddr_t OurNet = NetOurIP & NetOurSubnetMask; |
607 | IPaddr_t ServerNet = TftpServerIP & NetOurSubnetMask; | |
fe8c2806 | 608 | |
0bdd8acc LC |
609 | if (OurNet != ServerNet) |
610 | printf("; sending through gateway %pI4", | |
611 | &NetOurGatewayIP); | |
fe8c2806 | 612 | } |
c718b143 | 613 | putc('\n'); |
fe8c2806 | 614 | |
c718b143 | 615 | printf("Filename '%s'.", tftp_filename); |
fe8c2806 WD |
616 | |
617 | if (NetBootFileSize) { | |
c718b143 LC |
618 | printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9); |
619 | print_size(NetBootFileSize<<9, ""); | |
fe8c2806 WD |
620 | } |
621 | ||
c718b143 | 622 | putc('\n'); |
fe8c2806 | 623 | |
c718b143 | 624 | printf("Load address: 0x%lx\n", load_addr); |
fe8c2806 | 625 | |
c718b143 | 626 | puts("Loading: *\b"); |
fe8c2806 | 627 | |
e83cc063 BS |
628 | TftpTimeoutCountMax = TftpRRQTimeoutCountMax; |
629 | ||
c718b143 LC |
630 | NetSetTimeout(TftpTimeoutMSecs, TftpTimeout); |
631 | NetSetHandler(TftpHandler); | |
fe8c2806 WD |
632 | |
633 | TftpServerPort = WELL_KNOWN_PORT; | |
634 | TftpTimeoutCount = 0; | |
635 | TftpState = STATE_RRQ; | |
ecb0ccd9 | 636 | /* Use a pseudo-random port unless a specific port is set */ |
fe8c2806 | 637 | TftpOurPort = 1024 + (get_timer(0) % 3072); |
53a5c424 | 638 | |
ecb0ccd9 | 639 | #ifdef CONFIG_TFTP_PORT |
2cb53608 | 640 | ep = getenv("tftpdstp"); |
7bc325a1 | 641 | if (ep != NULL) |
28cb9375 | 642 | TftpServerPort = simple_strtol(ep, NULL, 10); |
2cb53608 | 643 | ep = getenv("tftpsrcp"); |
7bc325a1 | 644 | if (ep != NULL) |
c718b143 | 645 | TftpOurPort = simple_strtol(ep, NULL, 10); |
ecb0ccd9 | 646 | #endif |
fbe4b5cb | 647 | TftpBlock = 0; |
fe8c2806 | 648 | |
73a8b27c WD |
649 | /* zero out server ether in case the server ip has changed */ |
650 | memset(NetServerEther, 0, 6); | |
53a5c424 DU |
651 | /* Revert TftpBlkSize to dflt */ |
652 | TftpBlkSize = TFTP_BLOCK_SIZE; | |
653 | #ifdef CONFIG_MCAST_TFTP | |
a93907c4 | 654 | mcast_cleanup(); |
53a5c424 | 655 | #endif |
4fccb818 RG |
656 | #ifdef CONFIG_TFTP_TSIZE |
657 | TftpTsize = 0; | |
658 | TftpNumchars = 0; | |
659 | #endif | |
73a8b27c | 660 | |
c718b143 | 661 | TftpSend(); |
fe8c2806 WD |
662 | } |
663 | ||
53a5c424 DU |
664 | #ifdef CONFIG_MCAST_TFTP |
665 | /* Credits: atftp project. | |
666 | */ | |
667 | ||
668 | /* pick up BcastAddr, Port, and whether I am [now] the master-client. * | |
669 | * Frame: | |
670 | * +-------+-----------+---+-------~~-------+---+ | |
671 | * | opc | multicast | 0 | addr, port, mc | 0 | | |
672 | * +-------+-----------+---+-------~~-------+---+ | |
673 | * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then | |
674 | * I am the new master-client so must send ACKs to DataBlocks. If I am not | |
675 | * master-client, I'm a passive client, gathering what DataBlocks I may and | |
676 | * making note of which ones I got in my bitmask. | |
677 | * In theory, I never go from master->passive.. | |
678 | * .. this comes in with pkt already pointing just past opc | |
679 | */ | |
680 | static void parse_multicast_oack(char *pkt, int len) | |
681 | { | |
c718b143 LC |
682 | int i; |
683 | IPaddr_t addr; | |
684 | char *mc_adr, *port, *mc; | |
53a5c424 | 685 | |
c718b143 | 686 | mc_adr = port = mc = NULL; |
53a5c424 DU |
687 | /* march along looking for 'multicast\0', which has to start at least |
688 | * 14 bytes back from the end. | |
689 | */ | |
c718b143 LC |
690 | for (i = 0; i < len-14; i++) |
691 | if (strcmp(pkt+i, "multicast") == 0) | |
53a5c424 DU |
692 | break; |
693 | if (i >= (len-14)) /* non-Multicast OACK, ign. */ | |
694 | return; | |
85eb5caf | 695 | |
c718b143 | 696 | i += 10; /* strlen multicast */ |
53a5c424 | 697 | mc_adr = pkt+i; |
c718b143 | 698 | for (; i < len; i++) { |
53a5c424 DU |
699 | if (*(pkt+i) == ',') { |
700 | *(pkt+i) = '\0'; | |
701 | if (port) { | |
702 | mc = pkt+i+1; | |
703 | break; | |
704 | } else { | |
705 | port = pkt+i+1; | |
706 | } | |
707 | } | |
708 | } | |
6d2231e8 LC |
709 | if (!port || !mc_adr || !mc) |
710 | return; | |
53a5c424 | 711 | if (Multicast && MasterClient) { |
c718b143 | 712 | printf("I got a OACK as master Client, WRONG!\n"); |
53a5c424 DU |
713 | return; |
714 | } | |
715 | /* ..I now accept packets destined for this MCAST addr, port */ | |
716 | if (!Multicast) { | |
717 | if (Bitmap) { | |
c718b143 | 718 | printf("Internal failure! no mcast.\n"); |
53a5c424 | 719 | free(Bitmap); |
c718b143 LC |
720 | Bitmap = NULL; |
721 | ProhibitMcast = 1; | |
53a5c424 DU |
722 | return ; |
723 | } | |
724 | /* I malloc instead of pre-declare; so that if the file ends | |
85eb5caf WD |
725 | * up being too big for this bitmap I can retry |
726 | */ | |
2cb53608 LC |
727 | Bitmap = malloc(Mapsize); |
728 | if (!Bitmap) { | |
c718b143 LC |
729 | printf("No Bitmap, no multicast. Sorry.\n"); |
730 | ProhibitMcast = 1; | |
53a5c424 DU |
731 | return; |
732 | } | |
c718b143 | 733 | memset(Bitmap, 0, Mapsize); |
53a5c424 DU |
734 | PrevBitmapHole = 0; |
735 | Multicast = 1; | |
736 | } | |
737 | addr = string_to_ip(mc_adr); | |
738 | if (Mcast_addr != addr) { | |
739 | if (Mcast_addr) | |
740 | eth_mcast_join(Mcast_addr, 0); | |
2cb53608 LC |
741 | Mcast_addr = addr; |
742 | if (eth_mcast_join(Mcast_addr, 1)) { | |
c718b143 LC |
743 | printf("Fail to set mcast, revert to TFTP\n"); |
744 | ProhibitMcast = 1; | |
53a5c424 DU |
745 | mcast_cleanup(); |
746 | NetStartAgain(); | |
747 | } | |
748 | } | |
c718b143 LC |
749 | MasterClient = (unsigned char)simple_strtoul((char *)mc, NULL, 10); |
750 | Mcast_port = (unsigned short)simple_strtoul(port, NULL, 10); | |
751 | printf("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient); | |
53a5c424 DU |
752 | return; |
753 | } | |
754 | ||
755 | #endif /* Multicast TFTP */ |