tftp.c 23 KB

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