tftp.c 23 KB

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