ftp.c 20 KB

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