tftp.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (C) 2002-2004 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 4115 2012-04-12 21:06:13Z olereinhardt $
  37. */
  38. #include <avr/io.h>
  39. #include <avr/pgmspace.h>
  40. #include <string.h>
  41. #include "config.h"
  42. #include "flash.h"
  43. #include "eboot.h"
  44. #include "debug.h"
  45. #include "tftp.h"
  46. /*!
  47. * \addtogroup xgTftp
  48. */
  49. /*@{*/
  50. /*!
  51. * \brief Erase and program a page in the flash ROM.
  52. *
  53. * \param page The page number to program, 0..479. (0..991 on ATMega2561)
  54. * \param data Pointer to the new page contents.
  55. * \param len Number of bytes to program. If this is less than 256,
  56. * then the remaining bytes will be filled with 0xFF.
  57. */
  58. #if defined(__AVR_ATmega2561__)
  59. #define MAXPAGE 992
  60. #else
  61. #define MAXPAGE 480
  62. #endif
  63. static void FlashPage(u_short page, void *data, u_short len)
  64. {
  65. u_short i;
  66. u_short *wp = data;
  67. if (len > 256)
  68. len = 256;
  69. if (page >= MAXPAGE)
  70. return;
  71. RAMPZ = page >> 8; // page / 256 = RAMPZ
  72. page <<= 8;
  73. SpmCommand(page, (1 << PGERS) | (1 << SPMEN));
  74. SpmCommand(0, (1 << RWWSRE) | (1 << SPMEN));
  75. for (i = 0; i < len; i += 2, wp++)
  76. SpmBufferFill(i, *wp);
  77. for (; i < 256; i += 2)
  78. SpmBufferFill(i, 0xFFFF);
  79. SpmCommand(page, (1 << PGWRT) | (1 << SPMEN));
  80. SpmCommand(0, (1 << RWWSRE) | (1 << SPMEN));
  81. }
  82. /*!
  83. * \brief Download a file from a TFTP server and burn it into the flash ROM.
  84. *
  85. * \return 0 on success, -1 otherwise.
  86. */
  87. int TftpRecv(void)
  88. {
  89. u_char retry;
  90. int rlen = 0;
  91. int slen;
  92. u_short tport = 69;
  93. u_short block = 0;
  94. u_char *cp;
  95. u_char *cp1;
  96. /*
  97. * Prepare the transmit buffer for a file request.
  98. */
  99. sframe.u.tftp.th_opcode = htons(TFTP_RRQ);
  100. slen = 2;
  101. cp = (u_char *)sframe.u.tftp.th_u.tu_stuff;
  102. cp1 = confboot.cb_image;
  103. do {
  104. *cp = *cp1++;
  105. slen++;
  106. } while(*cp++);
  107. *cp++ = 'o';
  108. *cp++ = 'c';
  109. *cp++ = 't';
  110. *cp++ = 'e';
  111. *cp++ = 't';
  112. *cp++ = 0;
  113. slen += 6;
  114. /*
  115. * Lopp until we receive a packet with less than 512 bytes of data.
  116. */
  117. do {
  118. /*
  119. * Send file request or acknowledge and receive
  120. * a data block.
  121. */
  122. for (retry = 0; retry < 3; retry++) {
  123. if (UdpOutput(confboot.cb_tftp_ip, tport, SPORT, slen) >= 0) {
  124. if ((rlen = UdpInput(SPORT, 20000)) >= 4) {
  125. DEBUG(".");
  126. break;
  127. }
  128. }
  129. }
  130. /*
  131. * Can't reach the TFTP server or got a malformed
  132. * repsonse.
  133. */
  134. if ((retry >= 3) || (rlen < 4)) {
  135. DEBUG("[ErrTftp2]");
  136. return -1;
  137. }
  138. /*
  139. * Accept data blocks only. Anything else will stop
  140. * the transfer with an error.
  141. */
  142. if (ntohs(rframe.u.tftp.th_opcode) != TFTP_DATA) {
  143. DEBUG("[ErrTftp3]");
  144. return -1;
  145. }
  146. /*
  147. * If this was the first block we received, prepare
  148. * the send buffer for sending ACKs.
  149. */
  150. if (block == 0) {
  151. tport = ntohs(rframe.udp_hdr.uh_sport);
  152. sframe.u.tftp.th_opcode = htons(TFTP_ACK);
  153. slen = 4;
  154. }
  155. /*
  156. * If this block is out of sequence, we ignore it.
  157. * However, if we missed the first block, return
  158. * with an error.
  159. */
  160. if (ntohs(rframe.u.tftp.th_u.tu_block) != block + 1) {
  161. if (block == 0) {
  162. DEBUG("[ErrTftp4]");
  163. return -1;
  164. }
  165. continue;
  166. }
  167. /*
  168. * Burn the received data into the flash ROM.
  169. */
  170. if (rlen > 4) {
  171. FlashPage(block << 1, rframe.u.tftp.th_data, rlen - 4);
  172. if (rlen > 260)
  173. FlashPage((block << 1) + 1, &rframe.u.tftp.th_data[256], rlen - 260);
  174. }
  175. /*
  176. * Update our block counter.
  177. */
  178. block++;
  179. sframe.u.tftp.th_u.tu_block = htons(block);
  180. } while (rlen >= 516);
  181. /*
  182. * Send the last ACK.
  183. */
  184. UdpOutput(confboot.cb_tftp_ip, tport, SPORT, slen);
  185. return 0;
  186. }
  187. /*@}*/