From 38cc0eeb7b9045f7b1bb4eb1c7b568ab25f9fd85 Mon Sep 17 00:00:00 2001 From: George Hotz Date: Fri, 12 Jan 2018 10:12:43 -0800 Subject: [PATCH 1/2] add wifi_secure_mode, boots in insecure mode --- boardesp/proxy.c | 80 +++++++++++++++++++++++++------------------- boardesp/webserver.c | 12 ++++--- 2 files changed, 52 insertions(+), 40 deletions(-) diff --git a/boardesp/proxy.c b/boardesp/proxy.c index 02e3473a53634f..35493869fde893 100644 --- a/boardesp/proxy.c +++ b/boardesp/proxy.c @@ -20,6 +20,9 @@ _a > _b ? _a : _b; }) char ssid[32]; +char password[] = "testing123"; +int wifi_secure_mode = 0; + static const int pin = 2; // Structure holding the TCP connection information. @@ -218,11 +221,50 @@ void ICACHE_FLASH_ATTR inter_recv_cb(void *arg, char *pusrdata, unsigned short l } } +void ICACHE_FLASH_ATTR wifi_configure(int secure) { + wifi_secure_mode = secure; + + // start wifi AP + wifi_set_opmode(SOFTAP_MODE); + struct softap_config config = {0}; + wifi_softap_get_config(&config); + strcpy(config.ssid, ssid); + if (wifi_secure_mode == 0) strcat(config.ssid, "-pair"); + strcpy(config.password, password); + config.ssid_len = strlen(config.ssid); + config.authmode = wifi_secure_mode ? AUTH_WPA2_PSK : AUTH_OPEN; + config.beacon_interval = 100; + config.max_connection = 4; + wifi_softap_set_config(&config); + + if (wifi_secure_mode) { + // setup tcp server + tcp_proto.local_port = 1337; + tcp_conn.type = ESPCONN_TCP; + tcp_conn.state = ESPCONN_NONE; + tcp_conn.proto.tcp = &tcp_proto; + espconn_regist_connectcb(&tcp_conn, tcp_connect_cb); + espconn_accept(&tcp_conn); + espconn_regist_time(&tcp_conn, 60, 0); // 60s timeout for all connections + + // setup inter server + inter_proto.local_port = 1338; + const char udp_remote_ip[4] = {255, 255, 255, 255}; + os_memcpy(inter_proto.remote_ip, udp_remote_ip, 4); + inter_proto.remote_port = 1338; + + inter_conn.type = ESPCONN_UDP; + inter_conn.proto.udp = &inter_proto; + + espconn_regist_recvcb(&inter_conn, inter_recv_cb); + espconn_create(&inter_conn); + } +} + void ICACHE_FLASH_ATTR wifi_init() { // default ssid and password memset(ssid, 0, 32); os_sprintf(ssid, "panda-%08x-BROKEN", system_get_chip_id()); - char password[] = "testing123"; // fetch secure ssid and password // update, try 20 times, for 1 second @@ -242,19 +284,7 @@ void ICACHE_FLASH_ATTR wifi_init() { os_delay_us(50000); } - // start wifi AP - wifi_set_opmode(SOFTAP_MODE); - struct softap_config config; - wifi_softap_get_config(&config); - strcpy(config.ssid, ssid); - strcpy(config.password, password); - config.ssid_len = strlen(ssid); - config.authmode = AUTH_WPA2_PSK; - config.beacon_interval = 100; - config.max_connection = 4; - wifi_softap_set_config(&config); - - //set IP + // set IP wifi_softap_dhcps_stop(); //stop DHCP before setting static IP struct ip_info ip_config; IP4_ADDR(&ip_config.ip, 192, 168, 0, 10); @@ -265,27 +295,7 @@ void ICACHE_FLASH_ATTR wifi_init() { wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, &stupid_gateway); wifi_softap_dhcps_start(); - // setup tcp server - tcp_proto.local_port = 1337; - tcp_conn.type = ESPCONN_TCP; - tcp_conn.state = ESPCONN_NONE; - tcp_conn.proto.tcp = &tcp_proto; - espconn_regist_connectcb(&tcp_conn, tcp_connect_cb); - espconn_accept(&tcp_conn); - espconn_regist_time(&tcp_conn, 60, 0); // 60s timeout for all connections - - // setup inter server - inter_proto.local_port = 1338; - const char udp_remote_ip[4] = {255, 255, 255, 255}; - os_memcpy(inter_proto.remote_ip, udp_remote_ip, 4); - inter_proto.remote_port = 1338; - - inter_conn.type = ESPCONN_UDP; - inter_conn.proto.udp = &inter_proto; - - espconn_regist_recvcb(&inter_conn, inter_recv_cb); - - espconn_create(&inter_conn); + wifi_configure(0); } #define LOOP_PRIO 2 diff --git a/boardesp/webserver.c b/boardesp/webserver.c index 66b1185fefd719..53778f44cc7d50 100644 --- a/boardesp/webserver.c +++ b/boardesp/webserver.c @@ -40,6 +40,7 @@ char OK_header[] = "HTTP/1.0 200 OK\nContent-Type: text/html\n\n"; static struct espconn web_conn; static esp_tcp web_proto; extern char ssid[]; +extern int wifi_secure_mode; char *st_firmware; int real_content_length, content_length = 0; @@ -215,8 +216,9 @@ static void ICACHE_FLASH_ATTR web_rx_cb(void *arg, char *data, uint16_t len) { espconn_send_string(&web_conn, resp); espconn_disconnect(conn); - - } else if (memcmp(data, "GET /set_property?usb_mode=", 27) == 0) { + } else if (memcmp(data, "GET /secure", 11) == 0 && !wifi_secure_mode) { + wifi_configure(1); + } else if (memcmp(data, "GET /set_property?usb_mode=", 27) == 0 && wifi_secure_mode) { char mode_value = data[27] - '0'; if (mode_value >= '\x00' && mode_value <= '\x02') { memset(resp, 0, MAX_RESP); @@ -228,7 +230,7 @@ static void ICACHE_FLASH_ATTR web_rx_cb(void *arg, char *data, uint16_t len) { espconn_send_string(&web_conn, resp); espconn_disconnect(conn); } - } else if (memcmp(data, "PUT /stupdate ", 14) == 0) { + } else if (memcmp(data, "PUT /stupdate ", 14) == 0 && wifi_secure_mode) { os_printf("init st firmware\n"); char *cl = strstr(data, "Content-Length: "); if (cl != NULL) { @@ -244,8 +246,8 @@ static void ICACHE_FLASH_ATTR web_rx_cb(void *arg, char *data, uint16_t len) { state = RECEIVING_ST_FIRMWARE; } - } else if ((memcmp(data, "PUT /espupdate1 ", 16) == 0) || - (memcmp(data, "PUT /espupdate2 ", 16) == 0)) { + } else if (((memcmp(data, "PUT /espupdate1 ", 16) == 0) || + (memcmp(data, "PUT /espupdate2 ", 16) == 0)) && wifi_secure_mode) { // 0x1000 = user1.bin // 0x81000 = user2.bin // 0x3FE000 = blank.bin From c6653cacbd3e4e39bf34c4df1c27f93b391f5177 Mon Sep 17 00:00:00 2001 From: George Hotz Date: Thu, 18 Jan 2018 21:30:32 -0800 Subject: [PATCH 2/2] from python import --- board/build.mk | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/board/build.mk b/board/build.mk index 7b3e271915ec12..069a00e1a04058 100644 --- a/board/build.mk +++ b/board/build.mk @@ -17,7 +17,7 @@ DFU_UTIL = "dfu-util" # this no longer pushes the bootstub flash: obj/$(PROJ_NAME).bin - PYTHONPATH=../ python -c "from panda import Panda; Panda().flash('obj/$(PROJ_NAME).bin')" + PYTHONPATH=../ python -c "from python import Panda; Panda().flash('obj/$(PROJ_NAME).bin')" ota: obj/$(PROJ_NAME).bin curl http://192.168.0.10/stupdate --upload-file $< @@ -26,7 +26,7 @@ bin: obj/$(PROJ_NAME).bin # this flashes everything recover: obj/bootstub.$(PROJ_NAME).bin obj/$(PROJ_NAME).bin - -PYTHONPATH=../ python -c "from panda import Panda; Panda().reset(enter_bootloader=True)" + -PYTHONPATH=../ python -c "from python import Panda; Panda().reset(enter_bootloader=True)" sleep 1.0 $(DFU_UTIL) -d 0483:df11 -a 0 -s 0x08004000 -D obj/$(PROJ_NAME).bin $(DFU_UTIL) -d 0483:df11 -a 0 -s 0x08000000:leave -D obj/bootstub.$(PROJ_NAME).bin