tftp.c 21 KB

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