ftp.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /*
  2. * lwftp.c : a lightweight FTP client using raw API of LWIP
  3. *
  4. * Copyright (c) 2014 GEZEDO
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted provided that the following conditions are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * 3. The name of the author may not be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  19. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  20. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  21. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  23. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  26. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  27. * OF SUCH DAMAGE.
  28. *
  29. * Author: Laurent GONZALEZ <lwip@gezedo.com>
  30. *
  31. */
  32. #pragma GCC diagnostic error "-Wall"
  33. #pragma GCC diagnostic error "-Wextra"
  34. #include <string.h>
  35. #include <stdbool.h>
  36. #include "ftp.h"
  37. #include "lwip/tcp.h"
  38. #include "stm32f4xx.h"
  39. #include "common_config.h"
  40. #include "spi_flash.h"
  41. /** Enable debugging for LWFTP */
  42. #ifndef LWFTP_DEBUG
  43. #define LWFTP_DEBUG LWIP_DBG_ON
  44. #endif
  45. #define LWFTP_TRACE (LWFTP_DEBUG|LWIP_DBG_TRACE)
  46. #define LWFTP_WARNING (LWFTP_DEBUG|LWIP_DBG_LEVEL_WARNING)
  47. #define LWFTP_SERIOUS (LWFTP_DEBUG|LWIP_DBG_LEVEL_SERIOUS)
  48. #define LWFTP_SEVERE (LWFTP_DEBUG|LWIP_DBG_LEVEL_SEVERE)
  49. #define PTRNLEN(s) s,(sizeof(s)-1)
  50. #define min(x,y) ((x)<(y)?(x):(y))
  51. /** Close control or data pcb
  52. * @param pointer to lwftp session data
  53. */
  54. static err_t lwftp_pcb_close(struct tcp_pcb *tpcb)
  55. {
  56. err_t error;
  57. tcp_err(tpcb, NULL);
  58. tcp_recv(tpcb, NULL);
  59. tcp_sent(tpcb, NULL);
  60. error = tcp_close(tpcb);
  61. if ( error != ERR_OK ) {
  62. LWIP_DEBUGF(LWFTP_SEVERE, ("lwftp:pcb close failure, not implemented\n"));
  63. }
  64. return ERR_OK;
  65. }
  66. /** Send data
  67. * @param pointer to lwftp session data
  68. * @param pointer to PCB
  69. * @param number of bytes sent
  70. */
  71. static err_t lwftp_send_next_data(lwftp_session_t *s)
  72. {
  73. const char *data;
  74. int len = 0;
  75. err_t error = ERR_OK;
  76. if (s->data_source) {
  77. len = s->data_source(s->data_handle, &data, s->data_pcb->mss);
  78. if (len) {
  79. LWIP_DEBUGF(LWFTP_TRACE, ("lwftp:sending %d bytes of data\n",len));
  80. error = tcp_write(s->data_pcb, data, len, 0);
  81. if (error!=ERR_OK) {
  82. LWIP_DEBUGF(LWFTP_SEVERE, ("lwftp:write failure (%s), not implemented\n",lwip_strerr(error)));
  83. }
  84. }
  85. }
  86. if (!len) {
  87. LWIP_DEBUGF(LWFTP_TRACE, ("lwftp:end of file\n"));
  88. lwftp_pcb_close(s->data_pcb);
  89. s->data_pcb = NULL;
  90. }
  91. return ERR_OK;
  92. }
  93. /** Handle data connection incoming data
  94. * @param pointer to lwftp session data
  95. * @param pointer to PCB
  96. * @param pointer to incoming pbuf
  97. * @param state of incoming process
  98. */
  99. static err_t lwftp_data_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
  100. {
  101. (void)err;
  102. lwftp_session_t *s = (lwftp_session_t*)arg;
  103. if (p) {
  104. if (s->data_sink) {
  105. struct pbuf *q;
  106. for (q=p; q; q=q->next) {
  107. s->data_sink(s->data_handle, q->payload, q->len);
  108. }
  109. } else {
  110. LWIP_DEBUGF(LWFTP_SEVERE, ("lwftp: sinking %d bytes\n",p->tot_len));
  111. }
  112. tcp_recved(tpcb, p->tot_len);
  113. pbuf_free(p);
  114. } else {
  115. // NULL pbuf shall lead to close the pcb. Close is postponed after
  116. // the session state machine updates. No need to close right here.
  117. // Instead we kindly tell data sink we are done
  118. if (s->data_sink) {
  119. s->data_sink(s->data_handle, NULL, 0);
  120. }
  121. }
  122. return ERR_OK;
  123. }
  124. /** Handle data connection acknowledge of sent data
  125. * @param pointer to lwftp session data
  126. * @param pointer to PCB
  127. * @param number of bytes sent
  128. */
  129. static err_t lwftp_data_sent(void *arg, struct tcp_pcb *tpcb, u16_t len)
  130. {
  131. (void)tpcb;
  132. lwftp_session_t *s = (lwftp_session_t*)arg;
  133. if ( s->data_source ) {
  134. s->data_source(s->data_handle, NULL, len);
  135. }
  136. return lwftp_send_next_data(s);
  137. }
  138. /** Handle data connection error
  139. * @param pointer to lwftp session data
  140. * @param state of connection
  141. */
  142. static void lwftp_data_err(void *arg, err_t err)
  143. {
  144. LWIP_UNUSED_ARG(err);
  145. if (arg != NULL) {
  146. lwftp_session_t *s = (lwftp_session_t*)arg;
  147. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:failed/error connecting for data to server (%s)\n",lwip_strerr(err)));
  148. s->control_state = LWFTP_QUIT; // gracefully exit on data error
  149. s->data_pcb = NULL; // No need to de-allocate PCB
  150. }
  151. }
  152. /** Process newly connected PCB
  153. * @param pointer to lwftp session data
  154. * @param pointer to PCB
  155. * @param state of connection
  156. */
  157. static err_t lwftp_data_connected(void *arg, struct tcp_pcb *tpcb, err_t err)
  158. {
  159. (void)tpcb;
  160. lwftp_session_t *s = (lwftp_session_t*)arg;
  161. if ( err == ERR_OK ) {
  162. LWIP_DEBUGF(LWFTP_TRACE, ("lwftp:connected for data to server\n"));
  163. s->data_state = LWFTP_CONNECTED;
  164. } else {
  165. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:err in data_connected (%s)\n",lwip_strerr(err)));
  166. }
  167. return err;
  168. }
  169. /** Open data connection for passive transfer
  170. * @param pointer to lwftp session data
  171. * @param pointer to incoming PASV response
  172. */
  173. static err_t lwftp_data_open(lwftp_session_t *s, struct pbuf *p)
  174. {
  175. err_t error;
  176. char *ptr;
  177. ip_addr_t data_server;
  178. u16_t data_port;
  179. // Find server connection parameter
  180. ptr = strchr(p->payload, '(');
  181. if (!ptr) return ERR_BUF;
  182. do {
  183. unsigned int a = strtoul(ptr+1,&ptr,10);
  184. unsigned int b = strtoul(ptr+1,&ptr,10);
  185. unsigned int c = strtoul(ptr+1,&ptr,10);
  186. unsigned int d = strtoul(ptr+1,&ptr,10);
  187. IP4_ADDR(&data_server,a,b,c,d);
  188. } while(0);
  189. data_port = strtoul(ptr+1,&ptr,10) << 8;
  190. data_port |= strtoul(ptr+1,&ptr,10) & 255;
  191. if (*ptr!=')') return ERR_BUF;
  192. // Open data session
  193. tcp_arg(s->data_pcb, s);
  194. tcp_err(s->data_pcb, lwftp_data_err);
  195. tcp_recv(s->data_pcb, lwftp_data_recv);
  196. tcp_sent(s->data_pcb, lwftp_data_sent);
  197. error = tcp_connect(s->data_pcb, &data_server, data_port, lwftp_data_connected);
  198. return error;
  199. }
  200. /** Send a message to control connection
  201. * @param pointer to lwftp session data
  202. * @param pointer to message string
  203. */
  204. static err_t lwftp_send_msg(lwftp_session_t *s, const char* msg, size_t len)
  205. {
  206. err_t error;
  207. LWIP_DEBUGF(LWFTP_TRACE,("lwftp:sending %s",msg));
  208. error = tcp_write(s->control_pcb, msg, len, 0);
  209. if ( error != ERR_OK ) {
  210. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:cannot write (%s)\n",lwip_strerr(error)));
  211. }
  212. return error;
  213. }
  214. /** Close control connection
  215. * @param pointer to lwftp session data
  216. * @param result to pass to callback fn (if called)
  217. */
  218. static void lwftp_control_close(lwftp_session_t *s, int result)
  219. {
  220. if (s->data_pcb) {
  221. lwftp_pcb_close(s->data_pcb);
  222. s->data_pcb = NULL;
  223. }
  224. if (s->control_pcb) {
  225. lwftp_pcb_close(s->control_pcb);
  226. s->control_pcb = NULL;
  227. }
  228. s->control_state = LWFTP_CLOSED;
  229. if ( (result >= 0) && s->done_fn ) {
  230. s->done_fn(s->data_handle, result);
  231. }
  232. }
  233. /** Main client state machine
  234. * @param pointer to lwftp session data
  235. * @param pointer to PCB
  236. * @param pointer to incoming data
  237. */
  238. static void lwftp_control_process(lwftp_session_t *s, struct tcp_pcb *tpcb, struct pbuf *p)
  239. {
  240. (void)tpcb;
  241. unsigned response = 0;
  242. int result = LWFTP_RESULT_ERR_SRVR_RESP;
  243. // Try to get response number
  244. if (p) {
  245. response = strtoul(p->payload, NULL, 10);
  246. LWIP_DEBUGF(LWFTP_TRACE, ("lwftp:got response %d\n",response));
  247. }
  248. switch (s->control_state) {
  249. case LWFTP_CONNECTED:
  250. if (response>0) {
  251. if (response==220) {
  252. lwftp_send_msg(s, PTRNLEN("USER "));
  253. lwftp_send_msg(s, s->settings->user, strlen(s->settings->user));
  254. lwftp_send_msg(s, PTRNLEN("\n"));
  255. s->control_state = LWFTP_USER_SENT;
  256. } else {
  257. s->control_state = LWFTP_QUIT;
  258. }
  259. }
  260. break;
  261. case LWFTP_USER_SENT:
  262. if (response>0) {
  263. if (response==331) {
  264. lwftp_send_msg(s, PTRNLEN("PASS "));
  265. lwftp_send_msg(s, s->settings->pass, strlen(s->settings->pass));
  266. lwftp_send_msg(s, PTRNLEN("\n"));
  267. s->control_state = LWFTP_PASS_SENT;
  268. } else if (response==230) {
  269. goto anonymous;
  270. } else {
  271. s->control_state = LWFTP_QUIT;
  272. }
  273. }
  274. break;
  275. case LWFTP_PASS_SENT:
  276. if (response>0) {
  277. if (response==230) {
  278. anonymous:
  279. lwftp_send_msg(s, PTRNLEN("TYPE I\n"));
  280. s->control_state = LWFTP_TYPE_SENT;
  281. } else {
  282. s->control_state = LWFTP_QUIT;
  283. }
  284. }
  285. break;
  286. case LWFTP_TYPE_SENT:
  287. if (response>0) {
  288. if (response==200) {
  289. lwftp_send_msg(s, PTRNLEN("PASV\n"));
  290. s->control_state = LWFTP_PASV_SENT;
  291. } else {
  292. s->control_state = LWFTP_QUIT;
  293. }
  294. }
  295. break;
  296. case LWFTP_PASV_SENT:
  297. if (response>0) {
  298. if (response==227) {
  299. lwftp_data_open(s,p);
  300. switch (s->target_state) {
  301. case LWFTP_STOR_SENT:
  302. lwftp_send_msg(s, PTRNLEN("STOR "));
  303. break;
  304. case LWFTP_RETR_SENT:
  305. lwftp_send_msg(s, PTRNLEN("RETR "));
  306. break;
  307. default:
  308. //LOG_ERROR("Unexpected internal state");
  309. s->target_state = LWFTP_QUIT;
  310. }
  311. lwftp_send_msg(s, s->settings->remote_path, strlen(s->settings->remote_path));
  312. lwftp_send_msg(s, PTRNLEN("\n"));
  313. s->control_state = s->target_state;
  314. } else {
  315. s->control_state = LWFTP_QUIT;
  316. }
  317. }
  318. break;
  319. case LWFTP_RETR_SENT:
  320. if (response>0) {
  321. if (response==150) {
  322. s->control_state = LWFTP_XFERING;
  323. } else if (response==550) {
  324. s->control_state = LWFTP_QUIT;
  325. //result = LWFTP_RESULT_ERR_FILENAME;
  326. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp: Failed to open file '%s'\n", s->settings->remote_path));
  327. }
  328. else {
  329. s->control_state = LWFTP_QUIT;
  330. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:expected 150, received %d\n",response));
  331. }
  332. }
  333. break;
  334. case LWFTP_STOR_SENT:
  335. if (response>0) {
  336. if (response==150) {
  337. s->control_state = LWFTP_XFERING;
  338. lwftp_data_sent(s,NULL,0);
  339. } else {
  340. s->control_state = LWFTP_QUIT;
  341. }
  342. }
  343. break;
  344. case LWFTP_XFERING:
  345. if (response>0) {
  346. if (response==226) {
  347. s->data_state = LWFTP_XFERING; // signal transfer OK
  348. } else {
  349. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:expected 226, received %d\n",response));
  350. }
  351. // Quit anyway after any message received during STOR
  352. s->control_state = LWFTP_QUIT;
  353. }
  354. break;
  355. case LWFTP_QUIT_SENT:
  356. if (response>0) {
  357. if (response==221) {
  358. if (s->data_state == LWFTP_XFERING){ // check for transfer OK
  359. result = LWFTP_RESULT_OK;
  360. }
  361. } else {
  362. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:expected 221, received %d\n",response));
  363. }
  364. s->control_state = LWFTP_CLOSING;
  365. }
  366. break;
  367. default:
  368. LWIP_DEBUGF(LWFTP_SEVERE, ("lwftp:unhandled state (%d)\n",s->control_state));
  369. }
  370. // Free receiving pbuf if any
  371. if (p) {
  372. pbuf_free(p);
  373. }
  374. // Handle second step in state machine
  375. switch ( s->control_state ) {
  376. case LWFTP_QUIT:
  377. lwftp_send_msg(s, PTRNLEN("QUIT\n"));
  378. s->control_state = LWFTP_QUIT_SENT;
  379. break;
  380. case LWFTP_CLOSING:
  381. // this function frees s, no use of s is allowed after
  382. return lwftp_control_close(s, result);
  383. default:;
  384. }
  385. }
  386. /** Handle control connection incoming data
  387. * @param pointer to lwftp session data
  388. * @param pointer to PCB
  389. * @param pointer to incoming pbuf
  390. * @param state of incoming process
  391. */
  392. static err_t lwftp_control_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
  393. {
  394. lwftp_session_t *s = (lwftp_session_t*)arg;
  395. if ( err == ERR_OK ) {
  396. if (p) {
  397. tcp_recved(tpcb, p->tot_len);
  398. lwftp_control_process(s, tpcb, p);
  399. } else {
  400. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:connection closed by remote host\n"));
  401. lwftp_control_close(s, LWFTP_RESULT_ERR_CLOSED);
  402. }
  403. } else {
  404. LWIP_DEBUGF(LWFTP_SERIOUS, ("lwftp:failed to receive (%s)\n",lwip_strerr(err)));
  405. lwftp_control_close(s, LWFTP_RESULT_ERR_UNKNOWN);
  406. }
  407. return err;
  408. }
  409. /** Handle control connection acknowledge of sent data
  410. * @param pointer to lwftp session data
  411. * @param pointer to PCB
  412. * @param number of bytes sent
  413. */
  414. static err_t lwftp_control_sent(void *arg, struct tcp_pcb *tpcb, u16_t len)
  415. {
  416. (void)arg;
  417. (void)tpcb;
  418. (void)len;
  419. LWIP_DEBUGF(LWFTP_TRACE, ("lwftp:successfully sent %d bytes\n",len));
  420. return ERR_OK;
  421. }
  422. /** Handle control connection error
  423. * @param pointer to lwftp session data
  424. * @param state of connection
  425. */
  426. static void lwftp_control_err(void *arg, err_t err)
  427. {
  428. LWIP_UNUSED_ARG(err);
  429. if (arg != NULL) {
  430. lwftp_session_t *s = (lwftp_session_t*)arg;
  431. int result;
  432. if( s->control_state == LWFTP_CLOSED ) {
  433. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:failed to connect to server (%s)\n",lwip_strerr(err)));
  434. result = LWFTP_RESULT_ERR_CONNECT;
  435. } else {
  436. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:connection closed by remote host\n"));
  437. result = LWFTP_RESULT_ERR_CLOSED;
  438. }
  439. s->control_pcb = NULL; // No need to de-allocate PCB
  440. lwftp_control_close(s, result);
  441. }
  442. }
  443. /** Process newly connected PCB
  444. * @param pointer to lwftp session data
  445. * @param pointer to PCB
  446. * @param state of connection
  447. */
  448. static err_t lwftp_control_connected(void *arg, struct tcp_pcb *tpcb, err_t err)
  449. {
  450. (void)tpcb;
  451. lwftp_session_t *s = (lwftp_session_t*)arg;
  452. if ( err == ERR_OK ) {
  453. LWIP_DEBUGF(LWFTP_TRACE, ("lwftp:connected to server\n"));
  454. s->control_state = LWFTP_CONNECTED;
  455. } else {
  456. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:err in control_connected (%s)\n",lwip_strerr(err)));
  457. }
  458. return err;
  459. }
  460. /** Store data to a remote file
  461. * @param Session structure
  462. */
  463. err_t lwftp_store(lwftp_session_t *s)
  464. {
  465. err_t error;
  466. // Check user supplied data
  467. if ( (s->control_state!=LWFTP_CLOSED) ||
  468. !s->settings->remote_path ||
  469. s->control_pcb ||
  470. s->data_pcb ||
  471. !s->settings->user ||
  472. !s->settings->pass )
  473. {
  474. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:invalid session data\n"));
  475. return ERR_ARG;
  476. }
  477. // Get sessions pcb
  478. s->control_pcb = tcp_new();
  479. if (!s->control_pcb) {
  480. LWIP_DEBUGF(LWFTP_SERIOUS, ("lwftp:cannot alloc control_pcb (low memory?)\n"));
  481. error = ERR_MEM;
  482. goto exit;
  483. }
  484. s->data_pcb = tcp_new();
  485. if (!s->data_pcb) {
  486. LWIP_DEBUGF(LWFTP_SERIOUS, ("lwftp:cannot alloc data_pcb (low memory?)\n"));
  487. error = ERR_MEM;
  488. goto close_pcb;
  489. }
  490. // Open control session
  491. tcp_arg(s->control_pcb, s);
  492. tcp_err(s->control_pcb, lwftp_control_err);
  493. tcp_recv(s->control_pcb, lwftp_control_recv);
  494. tcp_sent(s->control_pcb, lwftp_control_sent);
  495. error = tcp_connect(s->control_pcb, &s->settings->server_ip, s->settings->server_port, lwftp_control_connected);
  496. if ( error == ERR_OK ) goto exit;
  497. LWIP_DEBUGF(LWFTP_SERIOUS, ("lwftp:cannot connect control_pcb (%s)\n", lwip_strerr(error)));
  498. close_pcb:
  499. // Release pcbs in case of failure
  500. lwftp_control_close(s, -1);
  501. exit:
  502. return error;
  503. }
  504. err_t lwftp_retr(lwftp_session_t *s)
  505. {
  506. s->target_state = LWFTP_RETR_SENT;
  507. err_t error;
  508. // Check user supplied data
  509. if ( (s->control_state!=LWFTP_CLOSED) ||
  510. !s->settings->remote_path ||
  511. s->control_pcb ||
  512. s->data_pcb ||
  513. !s->settings->user ||
  514. !s->settings->pass )
  515. {
  516. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:invalid session data\n"));
  517. return ERR_ARG;
  518. }
  519. // Get sessions pcb
  520. s->control_pcb = tcp_new();
  521. if (!s->control_pcb) {
  522. LWIP_DEBUGF(LWFTP_SERIOUS, ("lwftp:cannot alloc control_pcb (low memory?)\n"));
  523. error = ERR_MEM;
  524. goto exit;
  525. }
  526. s->data_pcb = tcp_new();
  527. if (!s->data_pcb) {
  528. LWIP_DEBUGF(LWFTP_SERIOUS, ("lwftp:cannot alloc data_pcb (low memory?)\n"));
  529. error = ERR_MEM;
  530. goto close_pcb;
  531. }
  532. // Open control session
  533. tcp_arg(s->control_pcb, s);
  534. tcp_err(s->control_pcb, lwftp_control_err);
  535. tcp_recv(s->control_pcb, lwftp_control_recv);
  536. tcp_sent(s->control_pcb, lwftp_control_sent);
  537. error = tcp_connect(s->control_pcb, &s->settings->server_ip, s->settings->server_port, lwftp_control_connected);
  538. if ( error == ERR_OK ) goto exit;
  539. LWIP_DEBUGF(LWFTP_SERIOUS, ("lwftp:cannot connect control_pcb (%s)\n", lwip_strerr(error)));
  540. close_pcb:
  541. // Release pcbs in case of failure
  542. lwftp_control_close(s, -1);
  543. exit:
  544. return error;
  545. }
  546. //static void ftp_retr_callback(void *arg, int result)
  547. //{
  548. // lwftp_session_t *s = (lwftp_session_t *)arg;
  549. //
  550. // if (result != LWFTP_RESULT_OK) {
  551. // //LOG_ERROR("retr failed (%d)", result);
  552. // return lwftp_close(s);
  553. // }
  554. // lwftp_close(s);
  555. //}
  556. static bool validate_spif_firmware(uint32_t fw_len)
  557. {
  558. const uint32_t spif_firmware_offset = SPI_FLASH_SECTOR_SIZE * FIRMWARE_UPDATE_SECTOR_OFFSET;
  559. // check the firmware revision id
  560. char rev[HW_REV_LEN];
  561. spi_flash_read(spif_firmware_offset + HW_REV_OFFSET, &rev, sizeof(rev), 0);
  562. if (strncmp(rev, HW_REV, sizeof(rev))) {
  563. printf("HW revision mismatch: expected %s, found %s\r\n", HW_REV, rev);
  564. return false;
  565. }
  566. // check if the firmware length is correct
  567. if (fw_len != MAIN_FW_SIZE) {
  568. printf("ftp: firmware size mismatch: expected %d, got %ld\r\n", MAIN_FW_SIZE, fw_len);
  569. return false;
  570. }
  571. uint32_t expected_crc;
  572. const uint32_t crc_offset = USER_FLASH_CRC_ADDRESS - USER_FLASH_FIRST_PAGE_ADDRESS;
  573. spi_flash_read(spif_firmware_offset + crc_offset, &expected_crc, sizeof(expected_crc), 0);
  574. // calculate CRC
  575. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_CRC, ENABLE);
  576. CRC->CR = 1;
  577. for (uint32_t offset = 0; offset < crc_offset; offset += 4) {
  578. uint32_t data;
  579. spi_flash_read(offset, &data, sizeof(data), 0);
  580. CRC->DR = data;
  581. }
  582. uint32_t calculated_crc = CRC->DR;
  583. if (expected_crc != calculated_crc) {
  584. printf("ftp: calculated CRC (%lx) doesn't match the expected CRC (%lx)!\r\n", calculated_crc, expected_crc);
  585. return false;
  586. }
  587. return true;
  588. }
  589. static void erase_spif_firmware()
  590. {
  591. for (int sec = 0; sec < FIRMWARE_UPDATE_SECTOR_COUNT; ++sec) {
  592. spi_flash_erase_sector(SPI_FLASH_SECTOR_SIZE * (FIRMWARE_UPDATE_SECTOR_OFFSET + sec), 0);
  593. }
  594. }
  595. static unsigned data_sink(void *arg, const char* ptr, unsigned len)
  596. {
  597. (void)arg;
  598. static unsigned offset = 0;
  599. if (ptr) {
  600. // we received some data from the FTP server
  601. if (offset == 0) {
  602. // we need to erase the flash before we can write it
  603. erase_spif_firmware();
  604. }
  605. if (offset + len > MAIN_FW_SIZE) {
  606. // the firmware won't fit into the MCU flash
  607. return 0;
  608. }
  609. spi_flash_write(SPI_FLASH_SECTOR_SIZE * FIRMWARE_UPDATE_SECTOR_OFFSET + offset, ptr, len, 0);
  610. offset += len;
  611. } else {
  612. // all the data is in
  613. uint32_t fw_size = offset;
  614. // next time it comes, overwrite the existing SPI contents
  615. offset = 0;
  616. bool good_firmware = validate_spif_firmware(fw_size);
  617. if (good_firmware) {
  618. // TODO reboot
  619. } else {
  620. // erase it so the bootloader won't try to verify it every time
  621. erase_spif_firmware();
  622. }
  623. }
  624. return len;
  625. }
  626. void start_ftp_client(lwftp_session_t *s)
  627. {
  628. s->data_sink = data_sink;
  629. //s->done_fn = ftp_retr_callback;
  630. lwftp_retr(s);
  631. // FTP session will continue with the connection callback
  632. }