tftp.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (C) 2002-2007 by egnite Software GmbH
  3. * Copyright (C) 2010 by egnite GmbH
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. */
  35. /*
  36. * $Id: tftp.c 2954 2010-04-03 11:22:40Z haraldkipp $
  37. */
  38. #include <string.h>
  39. #include "dialog.h"
  40. #include "bootmon.h"
  41. #include "tftp.h"
  42. /*!
  43. * \addtogroup xgTftp
  44. */
  45. /*@{*/
  46. /*
  47. * Write the contents of a buffer to a specified program memory address.
  48. *
  49. * \param addr Program memory byte address to start writing.
  50. * \param data Points to a buffer which contains the data to be written.
  51. *
  52. * \todo Would be fine to verify the result and return a result to the
  53. * caller.
  54. */
  55. void StoreBlock(unsigned short page, unsigned char *data)
  56. {
  57. unsigned long i;
  58. unsigned long *addr;
  59. unsigned long val;
  60. addr = (unsigned long *) ((unsigned long) page << 9);
  61. for (i = 0; i < 512; i += 4) {
  62. val = *data++;
  63. val |= *data++ << 8;
  64. val |= *data++ << 16;
  65. val |= *data++ << 24;
  66. *addr++ = val;
  67. }
  68. }
  69. /*!
  70. * \brief Download a file from a TFTP server and burn it into the flash ROM.
  71. *
  72. * \return 0 on success, -1 otherwise.
  73. */
  74. int TftpRecv(void)
  75. {
  76. unsigned char retry;
  77. int rlen = 0;
  78. int slen;
  79. unsigned short tport = TPORT;
  80. unsigned short block = 0;
  81. unsigned char *cp;
  82. char *cp1;
  83. /*
  84. * Do nothing if there's no TFTP host configured.
  85. */
  86. if (confboot.cb_tftp_ip == 0) {
  87. DEBUG("TFTP skipped\n");
  88. return 0;
  89. }
  90. /*
  91. * Prepare the transmit buffer for a file request.
  92. */
  93. sframe.eth.udp.u.tftp.th_opcode = TFTP_RRQ;
  94. slen = 2;
  95. cp = (unsigned char*)sframe.eth.udp.u.tftp.th_u.tu_stuff;
  96. cp1 = (char *)confboot.cb_image;
  97. if (*cp1 == 0) {
  98. cp1 = "enut.bin";
  99. }
  100. DEBUG("Loading ");
  101. DEBUG(cp1);
  102. do {
  103. *cp = *cp1++;
  104. slen++;
  105. } while (*cp++);
  106. memcpy_(cp, (unsigned char*)"octet", 6);
  107. slen += 6;
  108. /*
  109. * Loop until we receive a packet with less than 512 bytes of data.
  110. */
  111. do {
  112. /*
  113. * Send file request or acknowledge and receive
  114. * a data block.
  115. */
  116. for (retry = 0; retry < 3; retry++) {
  117. DEBUG("[RQ TFTP]");
  118. if (UdpOutput(confboot.cb_tftp_ip, tport, SPORT, slen) >= 0) {
  119. if ((rlen = UdpInput(SPORT, 500)) >= 4)
  120. break;
  121. }
  122. }
  123. DEBUG("[GO-ON]");
  124. /*
  125. * Can't reach the TFTP server or got a malformed
  126. * repsonse.
  127. */
  128. if (retry >= 3 || rlen < 4) {
  129. return -1;
  130. }
  131. /*
  132. * Accept data blocks only. Anything else will stop
  133. * the transfer with an error.
  134. */
  135. if (ntohs(rframe.eth.udp.u.tftp.th_opcode) != TFTP_DATA)
  136. return -1;
  137. /*
  138. * If this was the first block we received, prepare
  139. * the send buffer for sending ACKs.
  140. */
  141. if (block == 0) {
  142. tport = rframe.eth.udp.udp_hdr.uh_sport;
  143. sframe.eth.udp.u.tftp.th_opcode = TFTP_ACK;
  144. slen = 4;
  145. }
  146. /*
  147. * If this block is out of sequence, we ignore it.
  148. * However, if we missed the first block, return
  149. * with an error.
  150. */
  151. if (ntohs(rframe.eth.udp.u.tftp.th_u.tu_block) != block + 1) {
  152. if (block == 0) {
  153. return -1;
  154. }
  155. continue;
  156. }
  157. /*
  158. * Burn the received data into the flash ROM.
  159. */
  160. if (rlen > 4) {
  161. StoreBlock(block, (unsigned char*)rframe.eth.udp.u.tftp.th_data);
  162. }
  163. /*
  164. * Update our block counter.
  165. */
  166. block++;
  167. sframe.eth.udp.u.tftp.th_u.tu_block = htons(block);
  168. } while (rlen >= 516);
  169. /*
  170. * Send the last ACK.
  171. */
  172. UdpOutput(confboot.cb_tftp_ip, tport, SPORT, slen);
  173. return 0;
  174. }
  175. /*@}*/