tftp.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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. net_set_state(NETLOOP_SUCCESS);
  299. }
  300. static void tftp_send(void)
  301. {
  302. uchar *pkt;
  303. uchar *xp;
  304. int len = 0;
  305. ushort *s;
  306. bool err_pkt = false;
  307. /*
  308. * We will always be sending some sort of packet, so
  309. * cobble together the packet headers now.
  310. */
  311. pkt = net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
  312. switch (tftp_state) {
  313. case STATE_SEND_RRQ:
  314. case STATE_SEND_WRQ:
  315. xp = pkt;
  316. s = (ushort *)pkt;
  317. #ifdef CONFIG_CMD_TFTPPUT
  318. *s++ = htons(tftp_state == STATE_SEND_RRQ ? TFTP_RRQ :
  319. TFTP_WRQ);
  320. #else
  321. *s++ = htons(TFTP_RRQ);
  322. #endif
  323. pkt = (uchar *)s;
  324. strcpy((char *)pkt, tftp_filename);
  325. pkt += strlen(tftp_filename) + 1;
  326. strcpy((char *)pkt, "octet");
  327. pkt += 5 /*strlen("octet")*/ + 1;
  328. strcpy((char *)pkt, "timeout");
  329. pkt += 7 /*strlen("timeout")*/ + 1;
  330. sprintf((char *)pkt, "%lu", timeout_ms / 1000);
  331. debug("send option \"timeout %s\"\n", (char *)pkt);
  332. pkt += strlen((char *)pkt) + 1;
  333. #ifdef CONFIG_TFTP_TSIZE
  334. pkt += sprintf((char *)pkt, "tsize%c%u%c",
  335. 0, net_boot_file_size, 0);
  336. #endif
  337. /* try for more effic. blk size */
  338. pkt += sprintf((char *)pkt, "blksize%c%d%c",
  339. 0, tftp_block_size_option, 0);
  340. /* try for more effic. window size.
  341. * Implemented only for tftp get.
  342. * Don't bother sending if it's 1
  343. */
  344. if (tftp_state == STATE_SEND_RRQ && tftp_window_size_option > 1)
  345. pkt += sprintf((char *)pkt, "windowsize%c%d%c",
  346. 0, tftp_window_size_option, 0);
  347. len = pkt - xp;
  348. break;
  349. case STATE_OACK:
  350. case STATE_RECV_WRQ:
  351. case STATE_DATA:
  352. xp = pkt;
  353. s = (ushort *)pkt;
  354. s[0] = htons(TFTP_ACK);
  355. s[1] = htons(tftp_cur_block);
  356. pkt = (uchar *)(s + 2);
  357. #ifdef CONFIG_CMD_TFTPPUT
  358. if (tftp_put_active) {
  359. int toload = tftp_block_size;
  360. int loaded = load_block(tftp_cur_block, pkt, toload);
  361. s[0] = htons(TFTP_DATA);
  362. pkt += loaded;
  363. tftp_put_final_block_sent = (loaded < toload);
  364. }
  365. #endif
  366. len = pkt - xp;
  367. break;
  368. case STATE_TOO_LARGE:
  369. xp = pkt;
  370. s = (ushort *)pkt;
  371. *s++ = htons(TFTP_ERROR);
  372. *s++ = htons(3);
  373. pkt = (uchar *)s;
  374. strcpy((char *)pkt, "File too large");
  375. pkt += 14 /*strlen("File too large")*/ + 1;
  376. len = pkt - xp;
  377. err_pkt = true;
  378. break;
  379. case STATE_BAD_MAGIC:
  380. xp = pkt;
  381. s = (ushort *)pkt;
  382. *s++ = htons(TFTP_ERROR);
  383. *s++ = htons(2);
  384. pkt = (uchar *)s;
  385. strcpy((char *)pkt, "File has bad magic");
  386. pkt += 18 /*strlen("File has bad magic")*/ + 1;
  387. len = pkt - xp;
  388. err_pkt = true;
  389. break;
  390. case STATE_INVALID_OPTION:
  391. xp = pkt;
  392. s = (ushort *)pkt;
  393. *s++ = htons(TFTP_ERROR);
  394. *s++ = htons(TFTP_ERR_OPTION_NEGOTIATION);
  395. pkt = (uchar *)s;
  396. strcpy((char *)pkt, "Option Negotiation Failed");
  397. /* strlen("Option Negotiation Failed") + NULL*/
  398. pkt += 25 + 1;
  399. len = pkt - xp;
  400. err_pkt = true;
  401. break;
  402. }
  403. net_send_udp_packet(net_server_ethaddr, tftp_remote_ip,
  404. tftp_remote_port, tftp_our_port, len);
  405. if (err_pkt)
  406. net_set_state(NETLOOP_FAIL);
  407. }
  408. #ifdef CONFIG_CMD_TFTPPUT
  409. static void icmp_handler(unsigned type, unsigned code, unsigned dest,
  410. struct in_addr sip, unsigned src, uchar *pkt,
  411. unsigned len)
  412. {
  413. if (type == ICMP_NOT_REACH && code == ICMP_NOT_REACH_PORT) {
  414. /* Oh dear the other end has gone away */
  415. restart("TFTP server died");
  416. }
  417. }
  418. #endif
  419. static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
  420. unsigned src, unsigned len)
  421. {
  422. __be16 proto;
  423. __be16 *s;
  424. int i;
  425. u16 timeout_val_rcvd;
  426. if (dest != tftp_our_port) {
  427. return;
  428. }
  429. if (tftp_state != STATE_SEND_RRQ && src != tftp_remote_port &&
  430. tftp_state != STATE_RECV_WRQ && tftp_state != STATE_SEND_WRQ)
  431. return;
  432. if (len < 2)
  433. return;
  434. len -= 2;
  435. /* warning: don't use increment (++) in ntohs() macros!! */
  436. s = (__be16 *)pkt;
  437. proto = *s++;
  438. pkt = (uchar *)s;
  439. switch (ntohs(proto)) {
  440. case TFTP_RRQ:
  441. break;
  442. case TFTP_ACK:
  443. #ifdef CONFIG_CMD_TFTPPUT
  444. if (tftp_put_active) {
  445. if (tftp_put_final_block_sent) {
  446. tftp_complete();
  447. } else {
  448. /*
  449. * Move to the next block. We want our block
  450. * count to wrap just like the other end!
  451. */
  452. int block = ntohs(*s);
  453. int ack_ok = (tftp_cur_block == block);
  454. tftp_prev_block = tftp_cur_block;
  455. tftp_cur_block = (unsigned short)(block + 1);
  456. update_block_number();
  457. if (ack_ok)
  458. tftp_send(); /* Send next data block */
  459. }
  460. }
  461. #endif
  462. break;
  463. default:
  464. break;
  465. #ifdef CONFIG_CMD_TFTPSRV
  466. case TFTP_WRQ:
  467. debug("Got WRQ\n");
  468. tftp_remote_ip = sip;
  469. tftp_remote_port = src;
  470. tftp_our_port = 1024 + (get_timer(0) % 3072);
  471. new_transfer();
  472. tftp_send(); /* Send ACK(0) */
  473. break;
  474. #endif
  475. case TFTP_OACK:
  476. debug("Got OACK: ");
  477. for (i = 0; i < len; i++) {
  478. if (pkt[i] == '\0')
  479. debug(" ");
  480. else
  481. debug("%c", pkt[i]);
  482. }
  483. debug("\n");
  484. tftp_state = STATE_OACK;
  485. tftp_remote_port = src;
  486. /*
  487. * Check for 'blksize' option.
  488. * Careful: "i" is signed, "len" is unsigned, thus
  489. * something like "len-8" may give a *huge* number
  490. */
  491. for (i = 0; i+8 < len; i++) {
  492. if (strcasecmp((char *)pkt + i, "blksize") == 0) {
  493. tftp_block_size = (unsigned short)
  494. simple_strtoul((char *)pkt + i + 8,
  495. NULL, 10);
  496. debug("Blocksize oack: %s, %d\n",
  497. (char *)pkt + i + 8, tftp_block_size);
  498. if (tftp_block_size > tftp_block_size_option) {
  499. printf("Invalid blk size(=%d)\n",
  500. tftp_block_size);
  501. tftp_state = STATE_INVALID_OPTION;
  502. }
  503. }
  504. if (strcasecmp((char *)pkt + i, "timeout") == 0) {
  505. timeout_val_rcvd = (unsigned short)
  506. simple_strtoul((char *)pkt + i + 8,
  507. NULL, 10);
  508. debug("Timeout oack: %s, %d\n",
  509. (char *)pkt + i + 8, timeout_val_rcvd);
  510. if (timeout_val_rcvd != (timeout_ms / 1000)) {
  511. printf("Invalid timeout val(=%d s)\n",
  512. timeout_val_rcvd);
  513. tftp_state = STATE_INVALID_OPTION;
  514. }
  515. }
  516. #ifdef CONFIG_TFTP_TSIZE
  517. if (strcasecmp((char *)pkt + i, "tsize") == 0) {
  518. tftp_tsize = simple_strtoul((char *)pkt + i + 6,
  519. NULL, 10);
  520. debug("size = %s, %d\n",
  521. (char *)pkt + i + 6, tftp_tsize);
  522. }
  523. #endif
  524. if (strcasecmp((char *)pkt + i, "windowsize") == 0) {
  525. tftp_windowsize =
  526. simple_strtoul((char *)pkt + i + 11,
  527. NULL, 10);
  528. debug("windowsize = %s, %d\n",
  529. (char *)pkt + i + 11, tftp_windowsize);
  530. }
  531. }
  532. tftp_next_ack = tftp_windowsize;
  533. #ifdef CONFIG_CMD_TFTPPUT
  534. if (tftp_put_active && tftp_state == STATE_OACK) {
  535. /* Get ready to send the first block */
  536. tftp_state = STATE_DATA;
  537. tftp_cur_block++;
  538. }
  539. #endif
  540. tftp_send(); /* Send ACK or first data block */
  541. break;
  542. case TFTP_DATA:
  543. if (len < 2)
  544. return;
  545. len -= 2;
  546. if (ntohs(*(__be16 *)pkt) != (ushort)(tftp_cur_block + 1)) {
  547. debug("Received unexpected block: %d, expected: %d\n",
  548. ntohs(*(__be16 *)pkt),
  549. (ushort)(tftp_cur_block + 1));
  550. /*
  551. * If one packet is dropped most likely
  552. * all other buffers in the window
  553. * that will arrive will cause a sending NACK.
  554. * This just overwellms the server, let's just send one.
  555. */
  556. if (tftp_last_nack != tftp_cur_block) {
  557. tftp_send();
  558. tftp_last_nack = tftp_cur_block;
  559. tftp_next_ack = (ushort)(tftp_cur_block +
  560. tftp_windowsize);
  561. }
  562. break;
  563. }
  564. tftp_cur_block++;
  565. tftp_cur_block %= TFTP_SEQUENCE_SIZE;
  566. if (tftp_state == STATE_SEND_RRQ)
  567. debug("Server did not acknowledge any options!\n");
  568. if (tftp_state == STATE_SEND_RRQ || tftp_state == STATE_OACK ||
  569. tftp_state == STATE_RECV_WRQ) {
  570. /* first block received */
  571. tftp_state = STATE_DATA;
  572. tftp_remote_port = src;
  573. new_transfer();
  574. if (tftp_cur_block != 1) { /* Assertion */
  575. puts("\nTFTP error: ");
  576. printf("First block is not block 1 (%ld)\n",
  577. tftp_cur_block);
  578. puts("Starting again\n\n");
  579. net_start_again();
  580. break;
  581. }
  582. }
  583. if (tftp_cur_block == tftp_prev_block) {
  584. /* Same block again; ignore it. */
  585. break;
  586. }
  587. update_block_number();
  588. tftp_prev_block = tftp_cur_block;
  589. timeout_count_max = tftp_timeout_count_max;
  590. net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
  591. if (store_block(tftp_cur_block, pkt + 2, len)) {
  592. eth_halt();
  593. net_set_state(NETLOOP_FAIL);
  594. break;
  595. }
  596. /*
  597. * Acknowledge the block just received, which will prompt
  598. * the remote for the next one.
  599. */
  600. if (tftp_cur_block == tftp_next_ack) {
  601. tftp_send();
  602. tftp_next_ack += tftp_windowsize;
  603. }
  604. if (len < tftp_block_size) {
  605. tftp_send();
  606. tftp_complete();
  607. }
  608. break;
  609. case TFTP_ERROR:
  610. printf("\nTFTP error: '%s' (%d)\n",
  611. pkt + 2, ntohs(*(__be16 *)pkt));
  612. switch (ntohs(*(__be16 *)pkt)) {
  613. case TFTP_ERR_FILE_NOT_FOUND:
  614. case TFTP_ERR_ACCESS_DENIED:
  615. puts("Not retrying...\n");
  616. eth_halt();
  617. net_set_state(NETLOOP_FAIL);
  618. break;
  619. case TFTP_ERR_UNDEFINED:
  620. case TFTP_ERR_DISK_FULL:
  621. case TFTP_ERR_UNEXPECTED_OPCODE:
  622. case TFTP_ERR_UNKNOWN_TRANSFER_ID:
  623. case TFTP_ERR_FILE_ALREADY_EXISTS:
  624. default:
  625. puts("Starting again\n\n");
  626. net_start_again();
  627. break;
  628. }
  629. break;
  630. }
  631. }
  632. static void tftp_timeout_handler(void)
  633. {
  634. if (++timeout_count > timeout_count_max) {
  635. restart("Retry count exceeded");
  636. } else {
  637. puts("T ");
  638. net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
  639. if (tftp_state != STATE_RECV_WRQ)
  640. tftp_send();
  641. }
  642. }
  643. /* Initialize tftp_load_addr and tftp_load_size from image_load_addr and lmb */
  644. static int tftp_init_load_addr(void)
  645. {
  646. #ifdef CONFIG_LMB
  647. struct lmb lmb;
  648. phys_size_t max_size;
  649. lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
  650. max_size = lmb_get_free_size(&lmb, image_load_addr);
  651. if (!max_size)
  652. return -1;
  653. tftp_load_size = max_size;
  654. #endif
  655. tftp_load_addr = image_load_addr;
  656. return 0;
  657. }
  658. void tftp_start(enum proto_t protocol)
  659. {
  660. #if CONFIG_NET_TFTP_VARS
  661. char *ep; /* Environment pointer */
  662. /*
  663. * Allow the user to choose TFTP blocksize and timeout.
  664. * TFTP protocol has a minimal timeout of 1 second.
  665. */
  666. ep = env_get("tftpblocksize");
  667. if (ep != NULL)
  668. tftp_block_size_option = simple_strtol(ep, NULL, 10);
  669. ep = env_get("tftpwindowsize");
  670. if (ep != NULL)
  671. tftp_window_size_option = simple_strtol(ep, NULL, 10);
  672. ep = env_get("tftptimeout");
  673. if (ep != NULL)
  674. timeout_ms = simple_strtol(ep, NULL, 10);
  675. if (timeout_ms < 1000) {
  676. printf("TFTP timeout (%ld ms) too low, set min = 1000 ms\n",
  677. timeout_ms);
  678. timeout_ms = 1000;
  679. }
  680. ep = env_get("tftptimeoutcountmax");
  681. if (ep != NULL)
  682. tftp_timeout_count_max = simple_strtol(ep, NULL, 10);
  683. if (tftp_timeout_count_max < 0) {
  684. printf("TFTP timeout count max (%d ms) negative, set to 0\n",
  685. tftp_timeout_count_max);
  686. tftp_timeout_count_max = 0;
  687. }
  688. #endif
  689. debug("TFTP blocksize = %i, TFTP windowsize = %d timeout = %ld ms\n",
  690. tftp_block_size_option, tftp_window_size_option, timeout_ms);
  691. tftp_remote_ip = net_server_ip;
  692. if (!net_parse_bootfile(&tftp_remote_ip, tftp_filename, MAX_LEN)) {
  693. sprintf(default_filename, "%02X%02X%02X%02X.img",
  694. net_ip.s_addr & 0xFF,
  695. (net_ip.s_addr >> 8) & 0xFF,
  696. (net_ip.s_addr >> 16) & 0xFF,
  697. (net_ip.s_addr >> 24) & 0xFF);
  698. strncpy(tftp_filename, default_filename, DEFAULT_NAME_LEN);
  699. tftp_filename[DEFAULT_NAME_LEN - 1] = 0;
  700. printf("*** Warning: no boot file name; using '%s'\n",
  701. tftp_filename);
  702. }
  703. printf("Using %s device\n", eth_get_name());
  704. printf("TFTP %s server %pI4; our IP address is %pI4",
  705. #ifdef CONFIG_CMD_TFTPPUT
  706. protocol == TFTPPUT ? "to" : "from",
  707. #else
  708. "from",
  709. #endif
  710. &tftp_remote_ip, &net_ip);
  711. /* Check if we need to send across this subnet */
  712. if (net_gateway.s_addr && net_netmask.s_addr) {
  713. struct in_addr our_net;
  714. struct in_addr remote_net;
  715. our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
  716. remote_net.s_addr = tftp_remote_ip.s_addr & net_netmask.s_addr;
  717. if (our_net.s_addr != remote_net.s_addr)
  718. printf("; sending through gateway %pI4", &net_gateway);
  719. }
  720. putc('\n');
  721. printf("Filename '%s'.", tftp_filename);
  722. if (net_boot_file_expected_size_in_blocks) {
  723. printf(" Size is 0x%x Bytes = ",
  724. net_boot_file_expected_size_in_blocks << 9);
  725. print_size(net_boot_file_expected_size_in_blocks << 9, "");
  726. }
  727. putc('\n');
  728. #ifdef CONFIG_CMD_TFTPPUT
  729. tftp_put_active = (protocol == TFTPPUT);
  730. if (tftp_put_active) {
  731. printf("Save address: 0x%lx\n", image_save_addr);
  732. printf("Save size: 0x%lx\n", image_save_size);
  733. net_boot_file_size = image_save_size;
  734. puts("Saving: *\b");
  735. tftp_state = STATE_SEND_WRQ;
  736. new_transfer();
  737. } else
  738. #endif
  739. {
  740. if (tftp_init_load_addr()) {
  741. eth_halt();
  742. net_set_state(NETLOOP_FAIL);
  743. puts("\nTFTP error: ");
  744. puts("trying to overwrite reserved memory...\n");
  745. return;
  746. }
  747. printf("Load address: 0x%lx\n", tftp_load_addr);
  748. puts("Loading: *\b");
  749. tftp_state = STATE_SEND_RRQ;
  750. #ifdef CONFIG_CMD_BOOTEFI
  751. efi_set_bootdev("Net", "", tftp_filename);
  752. #endif
  753. }
  754. time_start = get_timer(0);
  755. timeout_count_max = tftp_timeout_count_max;
  756. net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
  757. net_set_udp_handler(tftp_handler);
  758. #ifdef CONFIG_CMD_TFTPPUT
  759. net_set_icmp_handler(icmp_handler);
  760. #endif
  761. tftp_remote_port = WELL_KNOWN_PORT;
  762. timeout_count = 0;
  763. /* Use a pseudo-random port unless a specific port is set */
  764. tftp_our_port = 1024 + (get_timer(0) % 3072);
  765. #ifdef CONFIG_TFTP_PORT
  766. ep = env_get("tftpdstp");
  767. if (ep != NULL)
  768. tftp_remote_port = simple_strtol(ep, NULL, 10);
  769. ep = env_get("tftpsrcp");
  770. if (ep != NULL)
  771. tftp_our_port = simple_strtol(ep, NULL, 10);
  772. #endif
  773. tftp_cur_block = 0;
  774. tftp_windowsize = 1;
  775. tftp_last_nack = 0;
  776. /* zero out server ether in case the server ip has changed */
  777. memset(net_server_ethaddr, 0, 6);
  778. /* Revert tftp_block_size to dflt */
  779. tftp_block_size = TFTP_BLOCK_SIZE;
  780. #ifdef CONFIG_TFTP_TSIZE
  781. tftp_tsize = 0;
  782. tftp_tsize_num_hash = 0;
  783. #endif
  784. tftp_send();
  785. }
  786. #ifdef CONFIG_CMD_TFTPSRV
  787. void tftp_start_server(void)
  788. {
  789. tftp_filename[0] = 0;
  790. if (tftp_init_load_addr()) {
  791. eth_halt();
  792. net_set_state(NETLOOP_FAIL);
  793. puts("\nTFTP error: trying to overwrite reserved memory...\n");
  794. return;
  795. }
  796. printf("Using %s device\n", eth_get_name());
  797. printf("Listening for TFTP transfer on %pI4\n", &net_ip);
  798. printf("Load address: 0x%lx\n", tftp_load_addr);
  799. puts("Loading: *\b");
  800. timeout_count_max = tftp_timeout_count_max;
  801. timeout_count = 0;
  802. timeout_ms = TIMEOUT;
  803. net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
  804. /* Revert tftp_block_size to dflt */
  805. tftp_block_size = TFTP_BLOCK_SIZE;
  806. tftp_cur_block = 0;
  807. tftp_our_port = WELL_KNOWN_PORT;
  808. #ifdef CONFIG_TFTP_TSIZE
  809. tftp_tsize = 0;
  810. tftp_tsize_num_hash = 0;
  811. #endif
  812. tftp_state = STATE_RECV_WRQ;
  813. net_set_udp_handler(tftp_handler);
  814. /* zero out server ether in case the server ip has changed */
  815. memset(net_server_ethaddr, 0, 6);
  816. }
  817. #endif /* CONFIG_CMD_TFTPSRV */