tftp.c 25 KB

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