tftp.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. /*
  2. * Copyright 1994, 1995, 2000 Neil Russell.
  3. * (See License)
  4. * Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de
  5. * Copyright 2011 Comelit Group SpA,
  6. * Luca Ceresoli <luca.ceresoli@comelit.it>
  7. */
  8. #include <common.h>
  9. #include <command.h>
  10. #include <efi_loader.h>
  11. #include <env.h>
  12. #include <image.h>
  13. #include <lmb.h>
  14. #include <log.h>
  15. #include <mapmem.h>
  16. #include <net.h>
  17. #include <asm/global_data.h>
  18. #include <net/tftp.h>
  19. #include "bootp.h"
  20. #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
  21. #include <flash.h>
  22. #endif
  23. DECLARE_GLOBAL_DATA_PTR;
  24. /* Well known TFTP port # */
  25. #define WELL_KNOWN_PORT 69
  26. /* Millisecs to timeout for lost pkt */
  27. #define TIMEOUT 5000UL
  28. #ifndef CONFIG_NET_RETRY_COUNT
  29. /* # of timeouts before giving up */
  30. # define TIMEOUT_COUNT 10
  31. #else
  32. # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
  33. #endif
  34. /* Number of "loading" hashes per line (for checking the image size) */
  35. #define HASHES_PER_LINE 65
  36. /*
  37. * TFTP operations.
  38. */
  39. #define TFTP_RRQ 1
  40. #define TFTP_WRQ 2
  41. #define TFTP_DATA 3
  42. #define TFTP_ACK 4
  43. #define TFTP_ERROR 5
  44. #define TFTP_OACK 6
  45. static ulong timeout_ms = TIMEOUT;
  46. static int timeout_count_max = TIMEOUT_COUNT;
  47. static ulong time_start; /* Record time we started tftp */
  48. /*
  49. * These globals govern the timeout behavior when attempting a connection to a
  50. * TFTP server. tftp_timeout_ms specifies the number of milliseconds to
  51. * wait for the server to respond to initial connection. Second global,
  52. * tftp_timeout_count_max, gives the number of such connection retries.
  53. * tftp_timeout_count_max must be non-negative and tftp_timeout_ms must be
  54. * positive. The globals are meant to be set (and restored) by code needing
  55. * non-standard timeout behavior when initiating a TFTP transfer.
  56. */
  57. ulong tftp_timeout_ms = TIMEOUT;
  58. int tftp_timeout_count_max = TIMEOUT_COUNT;
  59. enum {
  60. TFTP_ERR_UNDEFINED = 0,
  61. TFTP_ERR_FILE_NOT_FOUND = 1,
  62. TFTP_ERR_ACCESS_DENIED = 2,
  63. TFTP_ERR_DISK_FULL = 3,
  64. TFTP_ERR_UNEXPECTED_OPCODE = 4,
  65. TFTP_ERR_UNKNOWN_TRANSFER_ID = 5,
  66. TFTP_ERR_FILE_ALREADY_EXISTS = 6,
  67. TFTP_ERR_OPTION_NEGOTIATION = 8,
  68. };
  69. static struct in_addr tftp_remote_ip;
  70. /* The UDP port at their end */
  71. static int tftp_remote_port;
  72. /* The UDP port at our end */
  73. static int tftp_our_port;
  74. static int timeout_count;
  75. /* packet sequence number */
  76. static ulong tftp_cur_block;
  77. /* last packet sequence number received */
  78. static ulong tftp_prev_block;
  79. /* count of sequence number wraparounds */
  80. static ulong tftp_block_wrap;
  81. /* memory offset due to wrapping */
  82. static ulong tftp_block_wrap_offset;
  83. static int tftp_state;
  84. static ulong tftp_load_addr;
  85. #ifdef CONFIG_LMB
  86. static ulong tftp_load_size;
  87. #endif
  88. #ifdef CONFIG_TFTP_TSIZE
  89. /* The file size reported by the server */
  90. static int tftp_tsize;
  91. /* The number of hashes we printed */
  92. static short tftp_tsize_num_hash;
  93. #endif
  94. /* The window size negotiated */
  95. static ushort tftp_windowsize;
  96. /* Next block to send ack to */
  97. static ushort tftp_next_ack;
  98. /* Last nack block we send */
  99. static ushort tftp_last_nack;
  100. #ifdef CONFIG_CMD_TFTPPUT
  101. /* 1 if writing, else 0 */
  102. static int tftp_put_active;
  103. /* 1 if we have sent the last block */
  104. static int tftp_put_final_block_sent;
  105. #else
  106. #define tftp_put_active 0
  107. #endif
  108. #define STATE_SEND_RRQ 1
  109. #define STATE_DATA 2
  110. #define STATE_TOO_LARGE 3
  111. #define STATE_BAD_MAGIC 4
  112. #define STATE_OACK 5
  113. #define STATE_RECV_WRQ 6
  114. #define STATE_SEND_WRQ 7
  115. #define STATE_INVALID_OPTION 8
  116. /* default TFTP block size */
  117. #define TFTP_BLOCK_SIZE 512
  118. /* sequence number is 16 bit */
  119. #define TFTP_SEQUENCE_SIZE ((ulong)(1<<16))
  120. #define DEFAULT_NAME_LEN (8 + 4 + 1)
  121. static char default_filename[DEFAULT_NAME_LEN];
  122. #ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
  123. #define MAX_LEN 128
  124. #else
  125. #define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
  126. #endif
  127. static char tftp_filename[MAX_LEN];
  128. /* 512 is poor choice for ethernet, MTU is typically 1500.
  129. * Minus eth.hdrs thats 1468. Can get 2x better throughput with
  130. * almost-MTU block sizes. At least try... fall back to 512 if need be.
  131. * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
  132. */
  133. /* When windowsize is defined to 1,
  134. * tftp behaves the same way as it was
  135. * never declared
  136. */
  137. #ifdef CONFIG_TFTP_WINDOWSIZE
  138. #define TFTP_WINDOWSIZE CONFIG_TFTP_WINDOWSIZE
  139. #else
  140. #define TFTP_WINDOWSIZE 1
  141. #endif
  142. static unsigned short tftp_block_size = TFTP_BLOCK_SIZE;
  143. static unsigned short tftp_block_size_option = CONFIG_TFTP_BLOCKSIZE;
  144. static unsigned short tftp_window_size_option = TFTP_WINDOWSIZE;
  145. static inline int store_block(int block, uchar *src, unsigned int len)
  146. {
  147. ulong offset = block * tftp_block_size + tftp_block_wrap_offset -
  148. tftp_block_size;
  149. ulong newsize = offset + len;
  150. ulong store_addr = tftp_load_addr + offset;
  151. #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
  152. int i, rc = 0;
  153. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  154. /* start address in flash? */
  155. if (flash_info[i].flash_id == FLASH_UNKNOWN)
  156. continue;
  157. if (store_addr >= flash_info[i].start[0]) {
  158. rc = 1;
  159. break;
  160. }
  161. }
  162. if (rc) { /* Flash is destination for this packet */
  163. rc = flash_write((char *)src, store_addr, len);
  164. if (rc) {
  165. flash_perror(rc);
  166. return rc;
  167. }
  168. } else
  169. #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
  170. {
  171. void *ptr;
  172. #ifdef CONFIG_LMB
  173. ulong end_addr = tftp_load_addr + tftp_load_size;
  174. if (!end_addr)
  175. end_addr = ULONG_MAX;
  176. if (store_addr < tftp_load_addr ||
  177. store_addr + len > end_addr) {
  178. puts("\nTFTP error: ");
  179. puts("trying to overwrite reserved memory...\n");
  180. return -1;
  181. }
  182. #endif
  183. ptr = map_sysmem(store_addr, len);
  184. memcpy(ptr, src, len);
  185. unmap_sysmem(ptr);
  186. }
  187. if (net_boot_file_size < newsize)
  188. net_boot_file_size = newsize;
  189. return 0;
  190. }
  191. /* Clear our state ready for a new transfer */
  192. static void new_transfer(void)
  193. {
  194. tftp_prev_block = 0;
  195. tftp_block_wrap = 0;
  196. tftp_block_wrap_offset = 0;
  197. #ifdef CONFIG_CMD_TFTPPUT
  198. tftp_put_final_block_sent = 0;
  199. #endif
  200. }
  201. #ifdef CONFIG_CMD_TFTPPUT
  202. /**
  203. * Load the next block from memory to be sent over tftp.
  204. *
  205. * @param block Block number to send
  206. * @param dst Destination buffer for data
  207. * @param len Number of bytes in block (this one and every other)
  208. * @return number of bytes loaded
  209. */
  210. static int load_block(unsigned block, uchar *dst, unsigned len)
  211. {
  212. /* We may want to get the final block from the previous set */
  213. ulong offset = block * tftp_block_size + tftp_block_wrap_offset -
  214. tftp_block_size;
  215. ulong tosend = len;
  216. tosend = min(net_boot_file_size - offset, tosend);
  217. (void)memcpy(dst, (void *)(image_save_addr + offset), tosend);
  218. debug("%s: block=%u, offset=%lu, len=%u, tosend=%lu\n", __func__,
  219. block, offset, len, tosend);
  220. return tosend;
  221. }
  222. #endif
  223. static void tftp_send(void);
  224. static void tftp_timeout_handler(void);
  225. /**********************************************************************/
  226. static void show_block_marker(void)
  227. {
  228. ulong pos;
  229. #ifdef CONFIG_TFTP_TSIZE
  230. if (tftp_tsize) {
  231. pos = tftp_cur_block * tftp_block_size +
  232. tftp_block_wrap_offset;
  233. if (pos > tftp_tsize)
  234. pos = tftp_tsize;
  235. while (tftp_tsize_num_hash < pos * 50 / tftp_tsize) {
  236. putc('#');
  237. tftp_tsize_num_hash++;
  238. }
  239. } else
  240. #endif
  241. {
  242. pos = (tftp_cur_block - 1) +
  243. (tftp_block_wrap * TFTP_SEQUENCE_SIZE);
  244. if ((pos % 10) == 0)
  245. putc('#');
  246. else if (((pos + 1) % (10 * HASHES_PER_LINE)) == 0)
  247. puts("\n\t ");
  248. }
  249. }
  250. /**
  251. * restart the current transfer due to an error
  252. *
  253. * @param msg Message to print for user
  254. */
  255. static void restart(const char *msg)
  256. {
  257. printf("\n%s; starting again\n", msg);
  258. net_start_again();
  259. }
  260. /*
  261. * Check if the block number has wrapped, and update progress
  262. *
  263. * TODO: The egregious use of global variables in this file should be tidied.
  264. */
  265. static void update_block_number(void)
  266. {
  267. /*
  268. * RFC1350 specifies that the first data packet will
  269. * have sequence number 1. If we receive a sequence
  270. * number of 0 this means that there was a wrap
  271. * around of the (16 bit) counter.
  272. */
  273. if (tftp_cur_block == 0 && tftp_prev_block != 0) {
  274. tftp_block_wrap++;
  275. tftp_block_wrap_offset += tftp_block_size * TFTP_SEQUENCE_SIZE;
  276. timeout_count = 0; /* we've done well, reset the timeout */
  277. }
  278. show_block_marker();
  279. }
  280. /* The TFTP get or put is complete */
  281. static void tftp_complete(void)
  282. {
  283. #ifdef CONFIG_TFTP_TSIZE
  284. /* Print hash marks for the last packet received */
  285. while (tftp_tsize && tftp_tsize_num_hash < 49) {
  286. putc('#');
  287. tftp_tsize_num_hash++;
  288. }
  289. puts(" ");
  290. print_size(tftp_tsize, "");
  291. #endif
  292. time_start = get_timer(time_start);
  293. if (time_start > 0) {
  294. puts("\n\t "); /* Line up with "Loading: " */
  295. print_size(net_boot_file_size /
  296. time_start * 1000, "/s");
  297. }
  298. puts("\ndone\n");
  299. if (IS_ENABLED(CONFIG_CMD_BOOTEFI)) {
  300. if (!tftp_put_active)
  301. efi_set_bootdev("Net", "", tftp_filename,
  302. map_sysmem(tftp_load_addr, 0),
  303. net_boot_file_size);
  304. }
  305. net_set_state(NETLOOP_SUCCESS);
  306. }
  307. static void tftp_send(void)
  308. {
  309. uchar *pkt;
  310. uchar *xp;
  311. int len = 0;
  312. ushort *s;
  313. bool err_pkt = false;
  314. /*
  315. * We will always be sending some sort of packet, so
  316. * cobble together the packet headers now.
  317. */
  318. pkt = net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
  319. switch (tftp_state) {
  320. case STATE_SEND_RRQ:
  321. case STATE_SEND_WRQ:
  322. xp = pkt;
  323. s = (ushort *)pkt;
  324. #ifdef CONFIG_CMD_TFTPPUT
  325. *s++ = htons(tftp_state == STATE_SEND_RRQ ? TFTP_RRQ :
  326. TFTP_WRQ);
  327. #else
  328. *s++ = htons(TFTP_RRQ);
  329. #endif
  330. pkt = (uchar *)s;
  331. strcpy((char *)pkt, tftp_filename);
  332. pkt += strlen(tftp_filename) + 1;
  333. strcpy((char *)pkt, "octet");
  334. pkt += 5 /*strlen("octet")*/ + 1;
  335. strcpy((char *)pkt, "timeout");
  336. pkt += 7 /*strlen("timeout")*/ + 1;
  337. sprintf((char *)pkt, "%lu", timeout_ms / 1000);
  338. debug("send option \"timeout %s\"\n", (char *)pkt);
  339. pkt += strlen((char *)pkt) + 1;
  340. #ifdef CONFIG_TFTP_TSIZE
  341. pkt += sprintf((char *)pkt, "tsize%c%u%c",
  342. 0, net_boot_file_size, 0);
  343. #endif
  344. /* try for more effic. blk size */
  345. pkt += sprintf((char *)pkt, "blksize%c%d%c",
  346. 0, tftp_block_size_option, 0);
  347. /* try for more effic. window size.
  348. * Implemented only for tftp get.
  349. * Don't bother sending if it's 1
  350. */
  351. if (tftp_state == STATE_SEND_RRQ && tftp_window_size_option > 1)
  352. pkt += sprintf((char *)pkt, "windowsize%c%d%c",
  353. 0, tftp_window_size_option, 0);
  354. len = pkt - xp;
  355. break;
  356. case STATE_OACK:
  357. case STATE_RECV_WRQ:
  358. case STATE_DATA:
  359. xp = pkt;
  360. s = (ushort *)pkt;
  361. s[0] = htons(TFTP_ACK);
  362. s[1] = htons(tftp_cur_block);
  363. pkt = (uchar *)(s + 2);
  364. #ifdef CONFIG_CMD_TFTPPUT
  365. if (tftp_put_active) {
  366. int toload = tftp_block_size;
  367. int loaded = load_block(tftp_cur_block, pkt, toload);
  368. s[0] = htons(TFTP_DATA);
  369. pkt += loaded;
  370. tftp_put_final_block_sent = (loaded < toload);
  371. }
  372. #endif
  373. len = pkt - xp;
  374. break;
  375. case STATE_TOO_LARGE:
  376. xp = pkt;
  377. s = (ushort *)pkt;
  378. *s++ = htons(TFTP_ERROR);
  379. *s++ = htons(3);
  380. pkt = (uchar *)s;
  381. strcpy((char *)pkt, "File too large");
  382. pkt += 14 /*strlen("File too large")*/ + 1;
  383. len = pkt - xp;
  384. err_pkt = true;
  385. break;
  386. case STATE_BAD_MAGIC:
  387. xp = pkt;
  388. s = (ushort *)pkt;
  389. *s++ = htons(TFTP_ERROR);
  390. *s++ = htons(2);
  391. pkt = (uchar *)s;
  392. strcpy((char *)pkt, "File has bad magic");
  393. pkt += 18 /*strlen("File has bad magic")*/ + 1;
  394. len = pkt - xp;
  395. err_pkt = true;
  396. break;
  397. case STATE_INVALID_OPTION:
  398. xp = pkt;
  399. s = (ushort *)pkt;
  400. *s++ = htons(TFTP_ERROR);
  401. *s++ = htons(TFTP_ERR_OPTION_NEGOTIATION);
  402. pkt = (uchar *)s;
  403. strcpy((char *)pkt, "Option Negotiation Failed");
  404. /* strlen("Option Negotiation Failed") + NULL*/
  405. pkt += 25 + 1;
  406. len = pkt - xp;
  407. err_pkt = true;
  408. break;
  409. }
  410. net_send_udp_packet(net_server_ethaddr, tftp_remote_ip,
  411. tftp_remote_port, tftp_our_port, len);
  412. if (err_pkt)
  413. net_set_state(NETLOOP_FAIL);
  414. }
  415. #ifdef CONFIG_CMD_TFTPPUT
  416. static void icmp_handler(unsigned type, unsigned code, unsigned dest,
  417. struct in_addr sip, unsigned src, uchar *pkt,
  418. unsigned len)
  419. {
  420. if (type == ICMP_NOT_REACH && code == ICMP_NOT_REACH_PORT) {
  421. /* Oh dear the other end has gone away */
  422. restart("TFTP server died");
  423. }
  424. }
  425. #endif
  426. static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
  427. unsigned src, unsigned len)
  428. {
  429. __be16 proto;
  430. __be16 *s;
  431. int i;
  432. u16 timeout_val_rcvd;
  433. if (dest != tftp_our_port) {
  434. return;
  435. }
  436. if (tftp_state != STATE_SEND_RRQ && src != tftp_remote_port &&
  437. tftp_state != STATE_RECV_WRQ && tftp_state != STATE_SEND_WRQ)
  438. return;
  439. if (len < 2)
  440. return;
  441. len -= 2;
  442. /* warning: don't use increment (++) in ntohs() macros!! */
  443. s = (__be16 *)pkt;
  444. proto = *s++;
  445. pkt = (uchar *)s;
  446. switch (ntohs(proto)) {
  447. case TFTP_RRQ:
  448. break;
  449. case TFTP_ACK:
  450. #ifdef CONFIG_CMD_TFTPPUT
  451. if (tftp_put_active) {
  452. if (tftp_put_final_block_sent) {
  453. tftp_complete();
  454. } else {
  455. /*
  456. * Move to the next block. We want our block
  457. * count to wrap just like the other end!
  458. */
  459. int block = ntohs(*s);
  460. int ack_ok = (tftp_cur_block == block);
  461. tftp_prev_block = tftp_cur_block;
  462. tftp_cur_block = (unsigned short)(block + 1);
  463. update_block_number();
  464. if (ack_ok)
  465. tftp_send(); /* Send next data block */
  466. }
  467. }
  468. #endif
  469. break;
  470. default:
  471. break;
  472. #ifdef CONFIG_CMD_TFTPSRV
  473. case TFTP_WRQ:
  474. debug("Got WRQ\n");
  475. tftp_remote_ip = sip;
  476. tftp_remote_port = src;
  477. tftp_our_port = 1024 + (get_timer(0) % 3072);
  478. new_transfer();
  479. tftp_send(); /* Send ACK(0) */
  480. break;
  481. #endif
  482. case TFTP_OACK:
  483. debug("Got OACK: ");
  484. for (i = 0; i < len; i++) {
  485. if (pkt[i] == '\0')
  486. debug(" ");
  487. else
  488. debug("%c", pkt[i]);
  489. }
  490. debug("\n");
  491. tftp_state = STATE_OACK;
  492. tftp_remote_port = src;
  493. /*
  494. * Check for 'blksize' option.
  495. * Careful: "i" is signed, "len" is unsigned, thus
  496. * something like "len-8" may give a *huge* number
  497. */
  498. for (i = 0; i+8 < len; i++) {
  499. if (strcasecmp((char *)pkt + i, "blksize") == 0) {
  500. tftp_block_size = (unsigned short)
  501. dectoul((char *)pkt + i + 8, NULL);
  502. debug("Blocksize oack: %s, %d\n",
  503. (char *)pkt + i + 8, tftp_block_size);
  504. if (tftp_block_size > tftp_block_size_option) {
  505. printf("Invalid blk size(=%d)\n",
  506. tftp_block_size);
  507. tftp_state = STATE_INVALID_OPTION;
  508. }
  509. }
  510. if (strcasecmp((char *)pkt + i, "timeout") == 0) {
  511. timeout_val_rcvd = (unsigned short)
  512. dectoul((char *)pkt + i + 8, NULL);
  513. debug("Timeout oack: %s, %d\n",
  514. (char *)pkt + i + 8, timeout_val_rcvd);
  515. if (timeout_val_rcvd != (timeout_ms / 1000)) {
  516. printf("Invalid timeout val(=%d s)\n",
  517. timeout_val_rcvd);
  518. tftp_state = STATE_INVALID_OPTION;
  519. }
  520. }
  521. #ifdef CONFIG_TFTP_TSIZE
  522. if (strcasecmp((char *)pkt + i, "tsize") == 0) {
  523. tftp_tsize = dectoul((char *)pkt + i + 6,
  524. NULL);
  525. debug("size = %s, %d\n",
  526. (char *)pkt + i + 6, tftp_tsize);
  527. }
  528. #endif
  529. if (strcasecmp((char *)pkt + i, "windowsize") == 0) {
  530. tftp_windowsize =
  531. dectoul((char *)pkt + i + 11, NULL);
  532. debug("windowsize = %s, %d\n",
  533. (char *)pkt + i + 11, tftp_windowsize);
  534. }
  535. }
  536. tftp_next_ack = tftp_windowsize;
  537. #ifdef CONFIG_CMD_TFTPPUT
  538. if (tftp_put_active && tftp_state == STATE_OACK) {
  539. /* Get ready to send the first block */
  540. tftp_state = STATE_DATA;
  541. tftp_cur_block++;
  542. }
  543. #endif
  544. tftp_send(); /* Send ACK or first data block */
  545. break;
  546. case TFTP_DATA:
  547. if (len < 2)
  548. return;
  549. len -= 2;
  550. if (ntohs(*(__be16 *)pkt) != (ushort)(tftp_cur_block + 1)) {
  551. debug("Received unexpected block: %d, expected: %d\n",
  552. ntohs(*(__be16 *)pkt),
  553. (ushort)(tftp_cur_block + 1));
  554. /*
  555. * If one packet is dropped most likely
  556. * all other buffers in the window
  557. * that will arrive will cause a sending NACK.
  558. * This just overwellms the server, let's just send one.
  559. */
  560. if (tftp_last_nack != tftp_cur_block) {
  561. tftp_send();
  562. tftp_last_nack = tftp_cur_block;
  563. tftp_next_ack = (ushort)(tftp_cur_block +
  564. tftp_windowsize);
  565. }
  566. break;
  567. }
  568. tftp_cur_block++;
  569. tftp_cur_block %= TFTP_SEQUENCE_SIZE;
  570. if (tftp_state == STATE_SEND_RRQ) {
  571. debug("Server did not acknowledge any options!\n");
  572. tftp_next_ack = tftp_windowsize;
  573. }
  574. if (tftp_state == STATE_SEND_RRQ || tftp_state == STATE_OACK ||
  575. tftp_state == STATE_RECV_WRQ) {
  576. /* first block received */
  577. tftp_state = STATE_DATA;
  578. tftp_remote_port = src;
  579. new_transfer();
  580. if (tftp_cur_block != 1) { /* Assertion */
  581. puts("\nTFTP error: ");
  582. printf("First block is not block 1 (%ld)\n",
  583. tftp_cur_block);
  584. puts("Starting again\n\n");
  585. net_start_again();
  586. break;
  587. }
  588. }
  589. if (tftp_cur_block == tftp_prev_block) {
  590. /* Same block again; ignore it. */
  591. break;
  592. }
  593. update_block_number();
  594. tftp_prev_block = tftp_cur_block;
  595. timeout_count_max = tftp_timeout_count_max;
  596. net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
  597. if (store_block(tftp_cur_block, pkt + 2, len)) {
  598. eth_halt();
  599. net_set_state(NETLOOP_FAIL);
  600. break;
  601. }
  602. if (len < tftp_block_size) {
  603. tftp_send();
  604. tftp_complete();
  605. break;
  606. }
  607. /*
  608. * Acknowledge the block just received, which will prompt
  609. * the remote for the next one.
  610. */
  611. if (tftp_cur_block == tftp_next_ack) {
  612. tftp_send();
  613. tftp_next_ack += tftp_windowsize;
  614. }
  615. break;
  616. case TFTP_ERROR:
  617. printf("\nTFTP error: '%s' (%d)\n",
  618. pkt + 2, ntohs(*(__be16 *)pkt));
  619. switch (ntohs(*(__be16 *)pkt)) {
  620. case TFTP_ERR_FILE_NOT_FOUND:
  621. case TFTP_ERR_ACCESS_DENIED:
  622. puts("Not retrying...\n");
  623. eth_halt();
  624. net_set_state(NETLOOP_FAIL);
  625. break;
  626. case TFTP_ERR_UNDEFINED:
  627. case TFTP_ERR_DISK_FULL:
  628. case TFTP_ERR_UNEXPECTED_OPCODE:
  629. case TFTP_ERR_UNKNOWN_TRANSFER_ID:
  630. case TFTP_ERR_FILE_ALREADY_EXISTS:
  631. default:
  632. puts("Starting again\n\n");
  633. net_start_again();
  634. break;
  635. }
  636. break;
  637. }
  638. }
  639. static void tftp_timeout_handler(void)
  640. {
  641. if (++timeout_count > timeout_count_max) {
  642. restart("Retry count exceeded");
  643. } else {
  644. puts("T ");
  645. net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
  646. if (tftp_state != STATE_RECV_WRQ)
  647. tftp_send();
  648. }
  649. }
  650. /* Initialize tftp_load_addr and tftp_load_size from image_load_addr and lmb */
  651. static int tftp_init_load_addr(void)
  652. {
  653. #ifdef CONFIG_LMB
  654. struct lmb lmb;
  655. phys_size_t max_size;
  656. lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
  657. max_size = lmb_get_free_size(&lmb, image_load_addr);
  658. if (!max_size)
  659. return -1;
  660. tftp_load_size = max_size;
  661. #endif
  662. tftp_load_addr = image_load_addr;
  663. return 0;
  664. }
  665. void tftp_start(enum proto_t protocol)
  666. {
  667. #if CONFIG_NET_TFTP_VARS
  668. char *ep; /* Environment pointer */
  669. /*
  670. * Allow the user to choose TFTP blocksize and timeout.
  671. * TFTP protocol has a minimal timeout of 1 second.
  672. */
  673. ep = env_get("tftpblocksize");
  674. if (ep != NULL)
  675. tftp_block_size_option = simple_strtol(ep, NULL, 10);
  676. ep = env_get("tftpwindowsize");
  677. if (ep != NULL)
  678. tftp_window_size_option = simple_strtol(ep, NULL, 10);
  679. ep = env_get("tftptimeout");
  680. if (ep != NULL)
  681. timeout_ms = simple_strtol(ep, NULL, 10);
  682. if (timeout_ms < 1000) {
  683. printf("TFTP timeout (%ld ms) too low, set min = 1000 ms\n",
  684. timeout_ms);
  685. timeout_ms = 1000;
  686. }
  687. ep = env_get("tftptimeoutcountmax");
  688. if (ep != NULL)
  689. tftp_timeout_count_max = simple_strtol(ep, NULL, 10);
  690. if (tftp_timeout_count_max < 0) {
  691. printf("TFTP timeout count max (%d ms) negative, set to 0\n",
  692. tftp_timeout_count_max);
  693. tftp_timeout_count_max = 0;
  694. }
  695. #endif
  696. debug("TFTP blocksize = %i, TFTP windowsize = %d timeout = %ld ms\n",
  697. tftp_block_size_option, tftp_window_size_option, timeout_ms);
  698. tftp_remote_ip = net_server_ip;
  699. if (!net_parse_bootfile(&tftp_remote_ip, tftp_filename, MAX_LEN)) {
  700. sprintf(default_filename, "%02X%02X%02X%02X.img",
  701. net_ip.s_addr & 0xFF,
  702. (net_ip.s_addr >> 8) & 0xFF,
  703. (net_ip.s_addr >> 16) & 0xFF,
  704. (net_ip.s_addr >> 24) & 0xFF);
  705. strncpy(tftp_filename, default_filename, DEFAULT_NAME_LEN);
  706. tftp_filename[DEFAULT_NAME_LEN - 1] = 0;
  707. printf("*** Warning: no boot file name; using '%s'\n",
  708. tftp_filename);
  709. }
  710. printf("Using %s device\n", eth_get_name());
  711. printf("TFTP %s server %pI4; our IP address is %pI4",
  712. #ifdef CONFIG_CMD_TFTPPUT
  713. protocol == TFTPPUT ? "to" : "from",
  714. #else
  715. "from",
  716. #endif
  717. &tftp_remote_ip, &net_ip);
  718. /* Check if we need to send across this subnet */
  719. if (net_gateway.s_addr && net_netmask.s_addr) {
  720. struct in_addr our_net;
  721. struct in_addr remote_net;
  722. our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
  723. remote_net.s_addr = tftp_remote_ip.s_addr & net_netmask.s_addr;
  724. if (our_net.s_addr != remote_net.s_addr)
  725. printf("; sending through gateway %pI4", &net_gateway);
  726. }
  727. putc('\n');
  728. printf("Filename '%s'.", tftp_filename);
  729. if (net_boot_file_expected_size_in_blocks) {
  730. printf(" Size is 0x%x Bytes = ",
  731. net_boot_file_expected_size_in_blocks << 9);
  732. print_size(net_boot_file_expected_size_in_blocks << 9, "");
  733. }
  734. putc('\n');
  735. #ifdef CONFIG_CMD_TFTPPUT
  736. tftp_put_active = (protocol == TFTPPUT);
  737. if (tftp_put_active) {
  738. printf("Save address: 0x%lx\n", image_save_addr);
  739. printf("Save size: 0x%lx\n", image_save_size);
  740. net_boot_file_size = image_save_size;
  741. puts("Saving: *\b");
  742. tftp_state = STATE_SEND_WRQ;
  743. new_transfer();
  744. } else
  745. #endif
  746. {
  747. if (tftp_init_load_addr()) {
  748. eth_halt();
  749. net_set_state(NETLOOP_FAIL);
  750. puts("\nTFTP error: ");
  751. puts("trying to overwrite reserved memory...\n");
  752. return;
  753. }
  754. printf("Load address: 0x%lx\n", tftp_load_addr);
  755. puts("Loading: *\b");
  756. tftp_state = STATE_SEND_RRQ;
  757. }
  758. time_start = get_timer(0);
  759. timeout_count_max = tftp_timeout_count_max;
  760. net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
  761. net_set_udp_handler(tftp_handler);
  762. #ifdef CONFIG_CMD_TFTPPUT
  763. net_set_icmp_handler(icmp_handler);
  764. #endif
  765. tftp_remote_port = WELL_KNOWN_PORT;
  766. timeout_count = 0;
  767. /* Use a pseudo-random port unless a specific port is set */
  768. tftp_our_port = 1024 + (get_timer(0) % 3072);
  769. #ifdef CONFIG_TFTP_PORT
  770. ep = env_get("tftpdstp");
  771. if (ep != NULL)
  772. tftp_remote_port = simple_strtol(ep, NULL, 10);
  773. ep = env_get("tftpsrcp");
  774. if (ep != NULL)
  775. tftp_our_port = simple_strtol(ep, NULL, 10);
  776. #endif
  777. tftp_cur_block = 0;
  778. tftp_windowsize = 1;
  779. tftp_last_nack = 0;
  780. /* zero out server ether in case the server ip has changed */
  781. memset(net_server_ethaddr, 0, 6);
  782. /* Revert tftp_block_size to dflt */
  783. tftp_block_size = TFTP_BLOCK_SIZE;
  784. #ifdef CONFIG_TFTP_TSIZE
  785. tftp_tsize = 0;
  786. tftp_tsize_num_hash = 0;
  787. #endif
  788. tftp_send();
  789. }
  790. #ifdef CONFIG_CMD_TFTPSRV
  791. void tftp_start_server(void)
  792. {
  793. tftp_filename[0] = 0;
  794. if (tftp_init_load_addr()) {
  795. eth_halt();
  796. net_set_state(NETLOOP_FAIL);
  797. puts("\nTFTP error: trying to overwrite reserved memory...\n");
  798. return;
  799. }
  800. printf("Using %s device\n", eth_get_name());
  801. printf("Listening for TFTP transfer on %pI4\n", &net_ip);
  802. printf("Load address: 0x%lx\n", tftp_load_addr);
  803. puts("Loading: *\b");
  804. timeout_count_max = tftp_timeout_count_max;
  805. timeout_count = 0;
  806. timeout_ms = TIMEOUT;
  807. net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
  808. /* Revert tftp_block_size to dflt */
  809. tftp_block_size = TFTP_BLOCK_SIZE;
  810. tftp_cur_block = 0;
  811. tftp_our_port = WELL_KNOWN_PORT;
  812. #ifdef CONFIG_TFTP_TSIZE
  813. tftp_tsize = 0;
  814. tftp_tsize_num_hash = 0;
  815. #endif
  816. tftp_state = STATE_RECV_WRQ;
  817. net_set_udp_handler(tftp_handler);
  818. /* zero out server ether in case the server ip has changed */
  819. memset(net_server_ethaddr, 0, 6);
  820. }
  821. #endif /* CONFIG_CMD_TFTPSRV */