Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

fsp.c 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*********************************************************************\
  2. * Copyright (c) 2005 by Radim Kolar (hsn-sendmail.cz) *
  3. * *
  4. * You may copy or modify this file in any manner you wish, provided *
  5. * that this notice is always included, and that you hold the author *
  6. * harmless for any loss or damage resulting from the installation or *
  7. * use of this software. *
  8. * *
  9. * This file provides support for FSP v2 protocol written from scratch *
  10. * by Radim Kolar, FSP project leader. *
  11. * *
  12. * ABOUT FSP *
  13. * FSP is a lightweight file transfer protocol and is being used for *
  14. * booting, Internet firmware updates, embedded devices and in *
  15. * wireless applications. FSP is very easy to implement; contact Radim *
  16. * Kolar if you need hand optimized assembler FSP stacks for various *
  17. * microcontrollers, CPUs or consultations. *
  18. * http://fsp.sourceforge.net/ *
  19. * *
  20. * REVISION HISTORY *
  21. * 1.0 2005-03-17 rkolar Initial coding *
  22. * 1.1 2005-03-24 rkolar We really need to send CC_BYE to the server *
  23. * at end of transfer, because next stage boot *
  24. * loader is unable to contact FSP server *
  25. * until session timeouts. *
  26. * 1.2 2005-03-26 rkolar We need to query filesize in advance, *
  27. * because NBI loader do not reads file until *
  28. * eof is reached.
  29. * REMARKS *
  30. * there is no support for selecting port number of fsp server, maybe *
  31. * we should parse fsp:// URLs in boot image filename. *
  32. * this implementation has filename limit 255 chars. *
  33. \*********************************************************************/
  34. #ifdef DOWNLOAD_PROTO_FSP
  35. #define FSP_PORT 21
  36. /* FSP commands */
  37. #define CC_GET_FILE 0x42
  38. #define CC_BYE 0x4A
  39. #define CC_ERR 0x40
  40. #define CC_STAT 0x4D
  41. /* etherboot limits */
  42. #define FSP_MAXFILENAME 255
  43. struct fsp_info {
  44. in_addr server_ip;
  45. uint16_t server_port;
  46. uint16_t local_port;
  47. const char *filename;
  48. int (*fnc)(unsigned char *, unsigned int, unsigned int, int);
  49. };
  50. struct fsp_header {
  51. uint8_t cmd;
  52. uint8_t sum;
  53. uint16_t key;
  54. uint16_t seq;
  55. uint16_t len;
  56. uint32_t pos;
  57. } PACKED;
  58. #define FSP_MAXPAYLOAD (ETH_MAX_MTU - \
  59. (sizeof(struct iphdr) + sizeof(struct udphdr) + sizeof(struct fsp_header)))
  60. static struct fsp_request {
  61. struct iphdr ip;
  62. struct udphdr udp;
  63. struct fsp_header fsp;
  64. unsigned char data[FSP_MAXFILENAME + 1 + 2];
  65. } request;
  66. struct fsp_reply {
  67. struct iphdr ip;
  68. struct udphdr udp;
  69. struct fsp_header fsp;
  70. unsigned char data[FSP_MAXPAYLOAD];
  71. } PACKED;
  72. static int await_fsp(int ival, void *ptr, unsigned short ptype __unused,
  73. struct iphdr *ip, struct udphdr *udp)
  74. {
  75. if(!udp)
  76. return 0;
  77. if (ip->dest.s_addr != arptable[ARP_CLIENT].ipaddr.s_addr)
  78. return 0;
  79. if (ntohs(udp->dest) != ival)
  80. return 0;
  81. if (ntohs(udp->len) < 12+sizeof(struct udphdr))
  82. return 0;
  83. return 1;
  84. }
  85. static int proto_fsp(struct fsp_info *info)
  86. {
  87. uint32_t filepos;
  88. uint32_t filelength=0;
  89. int i,retry;
  90. uint16_t reqlen;
  91. struct fsp_reply *reply;
  92. int block=1;
  93. /* prepare FSP request packet */
  94. filepos=0;
  95. i=strlen(info->filename);
  96. if(i>FSP_MAXFILENAME)
  97. {
  98. printf("Boot filename is too long.\n");
  99. return 0;
  100. }
  101. strcpy(request.data,info->filename);
  102. *(uint16_t *)(request.data+i+1)=htons(FSP_MAXPAYLOAD);
  103. request.fsp.len=htons(i+1);
  104. reqlen=i+3+12;
  105. rx_qdrain();
  106. retry=0;
  107. /* main loop */
  108. for(;;) {
  109. int sum;
  110. long timeout;
  111. /* query filelength if not known */
  112. if(filelength == 0)
  113. request.fsp.cmd=CC_STAT;
  114. /* prepare request packet */
  115. request.fsp.pos=htonl(filepos);
  116. request.fsp.seq=random();
  117. request.fsp.sum=0;
  118. for(i=0,sum=reqlen;i<reqlen;i++)
  119. {
  120. sum += ((uint8_t *)&request.fsp)[i];
  121. }
  122. request.fsp.sum= sum + (sum >> 8);
  123. /* send request */
  124. if (!udp_transmit(info->server_ip.s_addr, info->local_port,
  125. info->server_port, sizeof(request.ip) +
  126. sizeof(request.udp) + reqlen, &request))
  127. return (0);
  128. /* wait for retry */
  129. #ifdef CONGESTED
  130. timeout =
  131. rfc2131_sleep_interval(filepos ? TFTP_REXMT : TIMEOUT, retry);
  132. #else
  133. timeout = rfc2131_sleep_interval(TIMEOUT, retry);
  134. #endif
  135. retry++;
  136. if (!await_reply(await_fsp, info->local_port, NULL, timeout))
  137. continue;
  138. reply=(struct fsp_reply *) &nic.packet[ETH_HLEN];
  139. /* check received packet */
  140. if (reply->fsp.seq != request.fsp.seq)
  141. continue;
  142. reply->udp.len=ntohs(reply->udp.len)-sizeof(struct udphdr);
  143. if(reply->udp.len < ntohs(reply->fsp.len) + 12 )
  144. continue;
  145. sum=-reply->fsp.sum;
  146. for(i=0;i<reply->udp.len;i++)
  147. {
  148. sum += ((uint8_t *)&(reply->fsp))[i];
  149. }
  150. sum = (sum + (sum >> 8)) & 0xff;
  151. if(sum != reply->fsp.sum)
  152. {
  153. printf("FSP checksum failed. computed %d, but packet has %d.\n",sum,reply->fsp.sum);
  154. continue;
  155. }
  156. if(reply->fsp.cmd == CC_ERR)
  157. {
  158. printf("\nFSP error: %s",info->filename);
  159. if(reply->fsp.len)
  160. printf(" [%s]",reply->data);
  161. printf("\n");
  162. return 0;
  163. }
  164. if(reply->fsp.cmd == CC_BYE && filelength == 1)
  165. {
  166. info->fnc(request.data,block,1,1);
  167. return 1;
  168. }
  169. if(reply->fsp.cmd == CC_STAT)
  170. {
  171. if(reply->data[8] == 0)
  172. {
  173. /* file not found, etc. */
  174. filelength=0xffffffff;
  175. } else
  176. {
  177. filelength= ntohl(*((uint32_t *)&reply->data[4]));
  178. }
  179. request.fsp.cmd = CC_GET_FILE;
  180. request.fsp.key = reply->fsp.key;
  181. retry=0;
  182. continue;
  183. }
  184. if(reply->fsp.cmd == CC_GET_FILE)
  185. {
  186. if(ntohl(reply->fsp.pos) != filepos)
  187. continue;
  188. request.fsp.key = reply->fsp.key;
  189. retry=0;
  190. i=ntohs(reply->fsp.len);
  191. if(i == 1)
  192. {
  193. request.fsp.cmd=CC_BYE;
  194. request.data[0]=reply->data[0];
  195. continue;
  196. }
  197. /* let last byte alone */
  198. if(i >= filelength)
  199. i = filelength - 1;
  200. if(!info->fnc(reply->data,block++,i,0))
  201. return 0;
  202. filepos += i;
  203. filelength -= i;
  204. }
  205. }
  206. return 0;
  207. }
  208. int url_fsp(const char *name, int (*fnc)(unsigned char *, unsigned int, unsigned int, int))
  209. {
  210. struct fsp_info info;
  211. /* Set the defaults */
  212. info.server_ip.s_addr = arptable[ARP_SERVER].ipaddr.s_addr;
  213. info.server_port = FSP_PORT;
  214. info.local_port = 1024 + random() & 0xfbff;
  215. info.fnc = fnc;
  216. /* Now parse the url */
  217. /* printf("fsp-URI: [%s]\n", name); */
  218. /* quick hack for now */
  219. info.filename=name;
  220. return proto_fsp(&info);
  221. }
  222. #endif