ftp.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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 "common_config.h"
  35. #ifdef FTP_ENABLE
  36. #include <string.h>
  37. #include <stdbool.h>
  38. #include "ftp.h"
  39. #include "lwip/tcp.h"
  40. #include "sockets.h"
  41. #include "stm32f4xx.h"
  42. #include "spi_flash.h"
  43. #include "web_params_api.h"
  44. #include "FreeRTOS.h"
  45. #include "task.h"
  46. #include "hal.h"
  47. /** Enable debugging for LWFTP */
  48. #ifndef LWFTP_DEBUG
  49. #define LWFTP_DEBUG LWIP_DBG_ON
  50. #endif
  51. #define LWFTP_TRACE (LWFTP_DEBUG|LWIP_DBG_TRACE)
  52. #define LWFTP_WARNING (LWFTP_DEBUG|LWIP_DBG_LEVEL_WARNING)
  53. #define LWFTP_SERIOUS (LWFTP_DEBUG|LWIP_DBG_LEVEL_SERIOUS)
  54. #define LWFTP_SEVERE (LWFTP_DEBUG|LWIP_DBG_LEVEL_SEVERE)
  55. #define PTRNLEN(s) s,(sizeof(s)-1)
  56. #define min(x,y) ((x)<(y)?(x):(y))
  57. static unsigned received_bytes_count = 0;
  58. static char **error_ptr;
  59. /** Close control or data pcb
  60. * @param pointer to lwftp session data
  61. */
  62. static err_t lwftp_pcb_close(struct tcp_pcb *tpcb)
  63. {
  64. err_t error;
  65. tcp_err(tpcb, NULL);
  66. tcp_recv(tpcb, NULL);
  67. tcp_sent(tpcb, NULL);
  68. error = tcp_close(tpcb);
  69. if ( error != ERR_OK ) {
  70. LWIP_DEBUGF(LWFTP_SEVERE, ("lwftp:pcb close failure, not implemented\n"));
  71. }
  72. return ERR_OK;
  73. }
  74. /** Send data
  75. * @param pointer to lwftp session data
  76. * @param pointer to PCB
  77. * @param number of bytes sent
  78. */
  79. static err_t lwftp_send_next_data(lwftp_session_t *s)
  80. {
  81. const char *data;
  82. int len = 0;
  83. err_t error = ERR_OK;
  84. if (s->data_source) {
  85. len = s->data_source(s->data_handle, &data, s->data_pcb->mss);
  86. if (len) {
  87. LWIP_DEBUGF(LWFTP_TRACE, ("lwftp:sending %d bytes of data\n",len));
  88. error = tcp_write(s->data_pcb, data, len, 0);
  89. if (error!=ERR_OK) {
  90. LWIP_DEBUGF(LWFTP_SEVERE, ("lwftp:write failure (%s), not implemented\n",lwip_strerr(error)));
  91. }
  92. }
  93. }
  94. if (!len) {
  95. LWIP_DEBUGF(LWFTP_TRACE, ("lwftp:end of file\n"));
  96. lwftp_pcb_close(s->data_pcb);
  97. s->data_pcb = NULL;
  98. }
  99. return ERR_OK;
  100. }
  101. /** Handle data connection incoming data
  102. * @param pointer to lwftp session data
  103. * @param pointer to PCB
  104. * @param pointer to incoming pbuf
  105. * @param state of incoming process
  106. */
  107. static err_t lwftp_data_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
  108. {
  109. (void)err;
  110. lwftp_session_t *s = (lwftp_session_t*)arg;
  111. if (p) {
  112. if (s->data_sink) {
  113. struct pbuf *q;
  114. for (q=p; q; q=q->next) {
  115. s->data_sink(s->data_handle, q->payload, q->len);
  116. }
  117. } else {
  118. LWIP_DEBUGF(LWFTP_SEVERE, ("lwftp: sinking %d bytes\n",p->tot_len));
  119. }
  120. tcp_recved(tpcb, p->tot_len);
  121. pbuf_free(p);
  122. } else {
  123. if (s->data_sink) {
  124. s->data_sink(s->data_handle, NULL, 0);
  125. }
  126. // NULL pbuf shall lead to close the pcb.
  127. if (s->data_pcb) {
  128. lwftp_pcb_close(s->data_pcb);
  129. s->data_pcb = NULL;
  130. }
  131. }
  132. return ERR_OK;
  133. }
  134. /** Handle data connection acknowledge of sent data
  135. * @param pointer to lwftp session data
  136. * @param pointer to PCB
  137. * @param number of bytes sent
  138. */
  139. static err_t lwftp_data_sent(void *arg, struct tcp_pcb *tpcb, u16_t len)
  140. {
  141. (void)tpcb;
  142. lwftp_session_t *s = (lwftp_session_t*)arg;
  143. if ( s->data_source ) {
  144. s->data_source(s->data_handle, NULL, len);
  145. }
  146. return lwftp_send_next_data(s);
  147. }
  148. /** Handle data connection error
  149. * @param pointer to lwftp session data
  150. * @param state of connection
  151. */
  152. static void lwftp_data_err(void *arg, err_t err)
  153. {
  154. LWIP_UNUSED_ARG(err);
  155. if (arg != NULL) {
  156. lwftp_session_t *s = (lwftp_session_t*)arg;
  157. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:failed/error connecting for data to server (%s)\n",lwip_strerr(err)));
  158. s->control_state = LWFTP_QUIT; // gracefully exit on data error
  159. s->data_pcb = NULL; // No need to de-allocate PCB
  160. }
  161. }
  162. /** Process newly connected PCB
  163. * @param pointer to lwftp session data
  164. * @param pointer to PCB
  165. * @param state of connection
  166. */
  167. static err_t lwftp_data_connected(void *arg, struct tcp_pcb *tpcb, err_t err)
  168. {
  169. (void)tpcb;
  170. lwftp_session_t *s = (lwftp_session_t*)arg;
  171. if ( err == ERR_OK ) {
  172. LWIP_DEBUGF(LWFTP_TRACE, ("lwftp:connected for data to server\n"));
  173. s->data_state = LWFTP_CONNECTED;
  174. } else {
  175. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:err in data_connected (%s)\n",lwip_strerr(err)));
  176. }
  177. return err;
  178. }
  179. /** Open data connection for passive transfer
  180. * @param pointer to lwftp session data
  181. * @param pointer to incoming PASV response
  182. */
  183. static err_t lwftp_data_open(lwftp_session_t *s, struct pbuf *p)
  184. {
  185. err_t error;
  186. char *ptr;
  187. ip_addr_t data_server;
  188. u16_t data_port;
  189. // Find server connection parameter
  190. ptr = strchr(p->payload, '(');
  191. if (!ptr) return ERR_BUF;
  192. do {
  193. unsigned int a = strtoul(ptr+1,&ptr,10);
  194. unsigned int b = strtoul(ptr+1,&ptr,10);
  195. unsigned int c = strtoul(ptr+1,&ptr,10);
  196. unsigned int d = strtoul(ptr+1,&ptr,10);
  197. IP4_ADDR(&data_server,a,b,c,d);
  198. } while(0);
  199. data_port = strtoul(ptr+1,&ptr,10) << 8;
  200. data_port |= strtoul(ptr+1,&ptr,10) & 255;
  201. if (*ptr!=')') return ERR_BUF;
  202. // Open data session
  203. tcp_arg(s->data_pcb, s);
  204. tcp_err(s->data_pcb, lwftp_data_err);
  205. tcp_recv(s->data_pcb, lwftp_data_recv);
  206. tcp_sent(s->data_pcb, lwftp_data_sent);
  207. error = tcp_connect(s->data_pcb, &data_server, data_port, lwftp_data_connected);
  208. return error;
  209. }
  210. /** Send a message to control connection
  211. * @param pointer to lwftp session data
  212. * @param pointer to message string
  213. */
  214. static err_t lwftp_send_msg(lwftp_session_t *s, const char* msg, size_t len)
  215. {
  216. err_t error;
  217. LWIP_DEBUGF(LWFTP_TRACE,("lwftp:sending %s",msg));
  218. error = tcp_write(s->control_pcb, msg, len, 0);
  219. if ( error != ERR_OK ) {
  220. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:cannot write (%s)\n",lwip_strerr(error)));
  221. }
  222. return error;
  223. }
  224. /** Close control connection
  225. * @param pointer to lwftp session data
  226. * @param result to pass to callback fn (if called)
  227. */
  228. static void lwftp_control_close(lwftp_session_t *s, int result)
  229. {
  230. if (s->control_pcb) {
  231. lwftp_pcb_close(s->control_pcb);
  232. s->control_pcb = NULL;
  233. }
  234. s->control_state = LWFTP_CLOSED;
  235. if ( (result >= 0) && s->done_fn ) {
  236. s->done_fn(s->data_handle, result);
  237. }
  238. }
  239. static void set_timeout(struct tcp_pcb *tpcb, unsigned seconds)
  240. {
  241. struct timeval timeout;
  242. timeout.tv_sec = seconds;
  243. timeout.tv_usec = 0;
  244. setsockopt(tpcb, SOL_SOCKET, SO_RCVTIMEO, (const char *)&timeout, sizeof(timeout));
  245. }
  246. /** Main client state machine
  247. * @param pointer to lwftp session data
  248. * @param pointer to PCB
  249. * @param pointer to incoming data
  250. */
  251. static void lwftp_control_process(lwftp_session_t *s, struct tcp_pcb *tpcb, struct pbuf *p)
  252. {
  253. (void)tpcb;
  254. unsigned response = 0;
  255. int result = LWFTP_RESULT_ERR_SRVR_RESP;
  256. // Try to get response number
  257. if (p) {
  258. response = strtoul(p->payload, NULL, 10);
  259. LWIP_DEBUGF(LWFTP_TRACE, ("lwftp:got response %d\n",response));
  260. }
  261. switch (s->control_state) {
  262. case LWFTP_CONNECTED:
  263. if (response>0) {
  264. if (response==220) {
  265. lwftp_send_msg(s, PTRNLEN("USER "));
  266. lwftp_send_msg(s, s->settings->user, strlen(s->settings->user));
  267. lwftp_send_msg(s, PTRNLEN("\n"));
  268. s->control_state = LWFTP_USER_SENT;
  269. } else {
  270. s->error = "The server doesn't like us";
  271. s->control_state = LWFTP_QUIT;
  272. }
  273. } else {
  274. s->error = "The server doesn't greet us";
  275. s->control_state = LWFTP_QUIT;
  276. }
  277. break;
  278. case LWFTP_USER_SENT:
  279. if (response>0) {
  280. if (response==331) {
  281. lwftp_send_msg(s, PTRNLEN("PASS "));
  282. lwftp_send_msg(s, s->settings->pass, strlen(s->settings->pass));
  283. lwftp_send_msg(s, PTRNLEN("\n"));
  284. s->control_state = LWFTP_PASS_SENT;
  285. } else if (response==230) {
  286. goto anonymous;
  287. } else {
  288. s->error = "Wrong user name";
  289. s->control_state = LWFTP_QUIT;
  290. }
  291. }
  292. break;
  293. case LWFTP_PASS_SENT:
  294. if (response>0) {
  295. if (response==230) {
  296. anonymous:
  297. lwftp_send_msg(s, PTRNLEN("TYPE I\n"));
  298. s->control_state = LWFTP_TYPE_SENT;
  299. } else {
  300. s->error = "Wrong password";
  301. s->control_state = LWFTP_QUIT;
  302. }
  303. }
  304. break;
  305. case LWFTP_TYPE_SENT:
  306. if (response>0) {
  307. if (response==200) {
  308. lwftp_send_msg(s, PTRNLEN("PASV\n"));
  309. s->control_state = LWFTP_PASV_SENT;
  310. } else {
  311. s->error = "The server doesn't support binary files";
  312. s->control_state = LWFTP_QUIT;
  313. }
  314. }
  315. break;
  316. case LWFTP_PASV_SENT:
  317. if (response>0) {
  318. if (response==227) {
  319. lwftp_data_open(s,p);
  320. switch (s->target_state) {
  321. case LWFTP_STOR_SENT:
  322. lwftp_send_msg(s, PTRNLEN("STOR "));
  323. break;
  324. case LWFTP_RETR_SENT:
  325. lwftp_send_msg(s, PTRNLEN("RETR "));
  326. break;
  327. default:
  328. //LOG_ERROR("Unexpected internal state");
  329. s->target_state = LWFTP_QUIT;
  330. }
  331. lwftp_send_msg(s, s->settings->remote_path, strlen(s->settings->remote_path));
  332. lwftp_send_msg(s, PTRNLEN("\n"));
  333. s->control_state = s->target_state;
  334. } else {
  335. s->error = "The server doesn't support PASV";
  336. s->control_state = LWFTP_QUIT;
  337. }
  338. }
  339. break;
  340. case LWFTP_RETR_SENT:
  341. if (response>0) {
  342. if (response==150) {
  343. s->control_state = LWFTP_XFERING;
  344. set_timeout(s->control_pcb, 0);
  345. } else if (response==550) {
  346. s->control_state = LWFTP_QUIT;
  347. s->error = "Failed to open file";
  348. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp: %s '%s'\n", error, s->settings->remote_path));
  349. }
  350. else {
  351. s->error = "The server doesn't start sending the file";
  352. s->control_state = LWFTP_QUIT;
  353. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:expected 150, received %d\n",response));
  354. }
  355. }
  356. break;
  357. case LWFTP_STOR_SENT:
  358. if (response>0) {
  359. if (response==150) {
  360. s->control_state = LWFTP_XFERING;
  361. lwftp_data_sent(s,NULL,0);
  362. } else {
  363. s->control_state = LWFTP_QUIT;
  364. }
  365. }
  366. break;
  367. case LWFTP_XFERING:
  368. if (response>0) {
  369. if (response==226) {
  370. s->data_state = LWFTP_XFERING; // signal transfer OK
  371. } else {
  372. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:expected 226, received %d\n",response));
  373. }
  374. // Quit anyway after any message received during STOR
  375. s->control_state = LWFTP_QUIT;
  376. }
  377. break;
  378. case LWFTP_QUIT_SENT:
  379. if (response>0) {
  380. if (response==221) {
  381. if (s->data_state == LWFTP_XFERING){ // check for transfer OK
  382. result = LWFTP_RESULT_OK;
  383. }
  384. } else {
  385. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:expected 221, received %d\n",response));
  386. }
  387. s->control_state = LWFTP_CLOSING;
  388. }
  389. break;
  390. default:
  391. LWIP_DEBUGF(LWFTP_SEVERE, ("lwftp:unhandled state (%d)\n",s->control_state));
  392. }
  393. // Free receiving pbuf if any
  394. if (p) {
  395. pbuf_free(p);
  396. }
  397. // Handle second step in state machine
  398. switch ( s->control_state ) {
  399. case LWFTP_QUIT:
  400. lwftp_send_msg(s, PTRNLEN("QUIT\n"));
  401. s->control_state = LWFTP_QUIT_SENT;
  402. break;
  403. case LWFTP_CLOSING:
  404. // this function frees s, no use of s is allowed after
  405. return lwftp_control_close(s, result);
  406. default:;
  407. }
  408. }
  409. /** Handle control connection incoming data
  410. * @param pointer to lwftp session data
  411. * @param pointer to PCB
  412. * @param pointer to incoming pbuf
  413. * @param state of incoming process
  414. */
  415. static err_t lwftp_control_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
  416. {
  417. lwftp_session_t *s = (lwftp_session_t*)arg;
  418. if ( err == ERR_OK ) {
  419. if (p) {
  420. tcp_recved(tpcb, p->tot_len);
  421. lwftp_control_process(s, tpcb, p);
  422. } else {
  423. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:connection closed by remote host\n"));
  424. lwftp_control_close(s, LWFTP_RESULT_ERR_CLOSED);
  425. }
  426. } else {
  427. LWIP_DEBUGF(LWFTP_SERIOUS, ("lwftp:failed to receive (%s)\n",lwip_strerr(err)));
  428. lwftp_control_close(s, LWFTP_RESULT_ERR_UNKNOWN);
  429. }
  430. return err;
  431. }
  432. /** Handle control connection acknowledge of sent data
  433. * @param pointer to lwftp session data
  434. * @param pointer to PCB
  435. * @param number of bytes sent
  436. */
  437. static err_t lwftp_control_sent(void *arg, struct tcp_pcb *tpcb, u16_t len)
  438. {
  439. (void)arg;
  440. (void)tpcb;
  441. (void)len;
  442. LWIP_DEBUGF(LWFTP_TRACE, ("lwftp:successfully sent %d bytes\n",len));
  443. return ERR_OK;
  444. }
  445. /** Handle control connection error
  446. * @param pointer to lwftp session data
  447. * @param state of connection
  448. */
  449. static void lwftp_control_err(void *arg, err_t err)
  450. {
  451. LWIP_UNUSED_ARG(err);
  452. if (arg != NULL) {
  453. lwftp_session_t *s = (lwftp_session_t*)arg;
  454. int result;
  455. if( s->control_state == LWFTP_CLOSED ) {
  456. s->error = "failed to connect to server";
  457. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp: %s (%s)\n",lwip_strerr(err)));
  458. result = LWFTP_RESULT_ERR_CONNECT;
  459. } else {
  460. s->error = "connection closed by remote host";
  461. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:connection closed by remote host\n"));
  462. result = LWFTP_RESULT_ERR_CLOSED;
  463. }
  464. s->control_pcb = NULL; // No need to de-allocate PCB
  465. lwftp_control_close(s, result);
  466. }
  467. }
  468. /** Process newly connected PCB
  469. * @param pointer to lwftp session data
  470. * @param pointer to PCB
  471. * @param state of connection
  472. */
  473. static err_t lwftp_control_connected(void *arg, struct tcp_pcb *tpcb, err_t err)
  474. {
  475. (void)tpcb;
  476. lwftp_session_t *s = (lwftp_session_t*)arg;
  477. if ( err == ERR_OK ) {
  478. LWIP_DEBUGF(LWFTP_TRACE, ("lwftp:connected to server\n"));
  479. s->control_state = LWFTP_CONNECTED;
  480. } else {
  481. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:err in control_connected (%s)\n",lwip_strerr(err)));
  482. }
  483. return err;
  484. }
  485. /** Store data to a remote file
  486. * @param Session structure
  487. */
  488. err_t lwftp_store(lwftp_session_t *s)
  489. {
  490. err_t error;
  491. // Check user supplied data
  492. if ( (s->control_state!=LWFTP_CLOSED) ||
  493. !s->settings->remote_path ||
  494. s->control_pcb ||
  495. s->data_pcb ||
  496. !s->settings->user ||
  497. !s->settings->pass )
  498. {
  499. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:invalid session data\n"));
  500. return ERR_ARG;
  501. }
  502. // Get sessions pcb
  503. s->control_pcb = tcp_new();
  504. if (!s->control_pcb) {
  505. LWIP_DEBUGF(LWFTP_SERIOUS, ("lwftp:cannot alloc control_pcb (low memory?)\n"));
  506. error = ERR_MEM;
  507. goto exit;
  508. }
  509. s->data_pcb = tcp_new();
  510. if (!s->data_pcb) {
  511. LWIP_DEBUGF(LWFTP_SERIOUS, ("lwftp:cannot alloc data_pcb (low memory?)\n"));
  512. error = ERR_MEM;
  513. goto close_pcb;
  514. }
  515. set_timeout(s->control_pcb, 5);
  516. set_timeout(s->data_pcb, 5);
  517. // Open control session
  518. tcp_arg(s->control_pcb, s);
  519. tcp_err(s->control_pcb, lwftp_control_err);
  520. tcp_recv(s->control_pcb, lwftp_control_recv);
  521. tcp_sent(s->control_pcb, lwftp_control_sent);
  522. error = tcp_connect(s->control_pcb, &s->settings->server_ip, s->settings->server_port, lwftp_control_connected);
  523. if ( error == ERR_OK ) goto exit;
  524. LWIP_DEBUGF(LWFTP_SERIOUS, ("lwftp:cannot connect control_pcb (%s)\n", lwip_strerr(error)));
  525. close_pcb:
  526. // Release pcbs in case of failure
  527. lwftp_control_close(s, -1);
  528. exit:
  529. return error;
  530. }
  531. err_t lwftp_retr(lwftp_session_t *s)
  532. {
  533. s->target_state = LWFTP_RETR_SENT;
  534. err_t error;
  535. // Check user supplied data
  536. if (!s->settings->remote_path) {
  537. s->error = "empty remote path";
  538. return ERR_ARG;
  539. }
  540. if (!s->settings->user) {
  541. s->error = "empty user name";
  542. return ERR_ARG;
  543. }
  544. if (!s->settings->pass) {
  545. s->error = "empty password";
  546. return ERR_ARG;
  547. }
  548. if (s->control_state != LWFTP_CLOSED || s->control_pcb) {
  549. // previous connection is incomplete
  550. lwftp_control_close(s, -1);
  551. }
  552. if (s->data_pcb) {
  553. lwftp_pcb_close(s->data_pcb);
  554. }
  555. // Get sessions pcb
  556. s->control_pcb = tcp_new();
  557. if (!s->control_pcb) {
  558. s->error = "cannot alloc control_pcb (low memory?)";
  559. error = ERR_MEM;
  560. goto exit;
  561. }
  562. s->data_pcb = tcp_new();
  563. if (!s->data_pcb) {
  564. s->error = "cannot alloc data_pcb (low memory?)";
  565. error = ERR_MEM;
  566. goto close_pcb;
  567. }
  568. // Open control session
  569. tcp_arg(s->control_pcb, s);
  570. tcp_err(s->control_pcb, lwftp_control_err);
  571. tcp_recv(s->control_pcb, lwftp_control_recv);
  572. tcp_sent(s->control_pcb, lwftp_control_sent);
  573. error = tcp_connect(s->control_pcb, &s->settings->server_ip, s->settings->server_port, lwftp_control_connected);
  574. if ( error == ERR_OK ) goto exit;
  575. LWIP_DEBUGF(LWFTP_SERIOUS, ("lwftp:cannot connect control_pcb (%s)\n", lwip_strerr(error)));
  576. s->error = "cannot connet control_pcb";
  577. close_pcb:
  578. // Release pcbs in case of failure
  579. lwftp_control_close(s, -1);
  580. exit:
  581. return error;
  582. }
  583. //static void ftp_retr_callback(void *arg, int result)
  584. //{
  585. // lwftp_session_t *s = (lwftp_session_t *)arg;
  586. //
  587. // if (result != LWFTP_RESULT_OK) {
  588. // //LOG_ERROR("retr failed (%d)", result);
  589. // return lwftp_close(s);
  590. // }
  591. // lwftp_close(s);
  592. //}
  593. static bool validate_spif_firmware(uint32_t fw_len)
  594. {
  595. const uint32_t spif_firmware_offset = SPI_FLASH_SECTOR_SIZE * FIRMWARE_UPDATE_SECTOR_OFFSET;
  596. // check the firmware revision id
  597. char rev[HW_REV_LEN];
  598. spi_flash_read(spif_firmware_offset + HW_REV_OFFSET, &rev, sizeof(rev), 0);
  599. if (strncmp(rev, HW_REV, sizeof(rev))) {
  600. printf("HW revision mismatch: expected %s, found %s\r\n", HW_REV, rev);
  601. return false;
  602. }
  603. // check if the firmware length is correct
  604. if (fw_len != MAIN_FW_SIZE) {
  605. printf("ftp: firmware size mismatch: expected %d, got %ld\r\n", MAIN_FW_SIZE, fw_len);
  606. return false;
  607. }
  608. uint32_t expected_crc;
  609. const uint32_t crc_offset = USER_FLASH_CRC_ADDRESS - USER_FLASH_FIRST_PAGE_ADDRESS;
  610. spi_flash_read(spif_firmware_offset + crc_offset, &expected_crc, sizeof(expected_crc), 0);
  611. // calculate CRC
  612. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_CRC, ENABLE);
  613. CRC->CR = 1;
  614. for (uint32_t offset = 0; offset < crc_offset; offset += 4) {
  615. uint32_t data;
  616. spi_flash_read(spif_firmware_offset + offset, &data, sizeof(data), 0);
  617. CRC->DR = data;
  618. }
  619. uint32_t calculated_crc = CRC->DR;
  620. if (expected_crc != calculated_crc) {
  621. printf("ftp: calculated CRC (%lx) doesn't match the expected CRC (%lx)!\r\n", calculated_crc, expected_crc);
  622. return false;
  623. }
  624. return true;
  625. }
  626. static void erase_spif_firmware()
  627. {
  628. printf("ftp: erasing SPI firmware partition...\r\n");
  629. for (int sec = 0; sec < FIRMWARE_UPDATE_SECTOR_COUNT; ++sec) {
  630. spi_flash_erase_sector(SPI_FLASH_SECTOR_SIZE * (FIRMWARE_UPDATE_SECTOR_OFFSET + sec), 0);
  631. }
  632. }
  633. static unsigned data_sink(void *arg, const char* ptr, unsigned len)
  634. {
  635. (void)arg;
  636. if (ptr && len) {
  637. // we received some data from the FTP server
  638. if (received_bytes_count == 0) {
  639. // we need to erase the flash before we can write it
  640. erase_spif_firmware();
  641. }
  642. if (received_bytes_count + len > MAIN_FW_SIZE) {
  643. printf("ftp: the firmware is too big! aborting the download\r\n");
  644. return 0;
  645. }
  646. spi_flash_write(SPI_FLASH_SECTOR_SIZE * FIRMWARE_UPDATE_SECTOR_OFFSET + received_bytes_count, ptr, len, 0);
  647. received_bytes_count += len;
  648. } else {
  649. printf("ftp: the firmware is downloaded, verifying...\r\n");
  650. uint32_t fw_size = received_bytes_count;
  651. bool good_firmware = validate_spif_firmware(fw_size);
  652. if (good_firmware) {
  653. printf("ftp: the firmware is valid, rebooting...\r\n");
  654. set_act_source(FTP_ACT);
  655. HTTP_StartResetTask(true);
  656. } else {
  657. // erase it so the bootloader won't try to verify it every time
  658. erase_spif_firmware();
  659. }
  660. }
  661. return len;
  662. }
  663. void start_ftp_client(lwftp_session_t *s)
  664. {
  665. received_bytes_count = 0;
  666. s->error = NULL;
  667. error_ptr = &s->error;
  668. s->data_sink = data_sink;
  669. //s->done_fn = ftp_retr_callback;
  670. lwftp_retr(s);
  671. // FTP session will continue with the connection callback
  672. }
  673. char *get_ftp_progress()
  674. {
  675. if (*error_ptr) {
  676. return *error_ptr;
  677. } else {
  678. unsigned progress = received_bytes_count * 100 / MAIN_FW_SIZE;
  679. static char progress_str_buf[4];
  680. snprintf(progress_str_buf, sizeof(progress_str_buf), "%u", progress);
  681. return progress_str_buf;
  682. }
  683. }
  684. #endif // FTP_ENABLE