Protocol Handshake with system_upgrade_start()

AgentSmithers
Posts: 123
Joined: Sat Apr 01, 2017 1:21 am
Contact:

Protocol Handshake with system_upgrade_start()

Postby AgentSmithers » Tue Jun 26, 2018 10:13 am

For anyone who has gotten this to work, My understanding is that the framework supports HTTP firmware upgrade natively without having to do SPI WRITE/ERASE logic on our own, I may be missing a serverside requirement but I wanted to confirm with everyone here that I can go this route with NONOS sdk.

Code: Select all

static void ICACHE_FLASH_ATTR ota_finished_callback(void *arg)
{
   struct upgrade_server_info *update = arg;
   if (update->upgrade_flag == true)
   {
   os_printf("[OTA]success; rebooting!\n");
   system_upgrade_reboot();
   }
   else
   {
   os_printf("[OTA]failed!\n");
   }

   os_free(update->pespconn);
   os_free(update->url);
   os_free(update);
}

static void ICACHE_FLASH_ATTR handleUpgrade(uint8_t serverVersion, ip_addr_t * server_ip, uint16_t port, const char *path)
{
   const char* file;
   uint8_t userBin = system_upgrade_userbin_check();
   switch (userBin)
   {
      case UPGRADE_FW_BIN1: file = "user2.bin"; break;
      case UPGRADE_FW_BIN2: file = "user1.bin"; break;
      default:
         os_printf("[OTA]Invalid userbin number!\n");
      return;
   }

   uint16_t version=1;
   if (serverVersion <= version)
   {
      os_printf("[OTA]No update. Server version:%d, local version %d\n", serverVersion, version);
      return;
   }

   os_printf("[OTA]Upgrade available version: %d\n", serverVersion);

   struct upgrade_server_info* update = (struct upgrade_server_info *)os_zalloc(sizeof(struct upgrade_server_info));
   update->pespconn = (struct espconn *)os_zalloc(sizeof(struct espconn));

   os_memcpy(update->ip, server_ip, 4);
   update->port = port;

   os_printf("[OTA]Server "IPSTR":%d. Path: %s%s\n", IP2STR(update->ip), update->port, path, file);

   update->check_cb = ota_finished_callback;
   update->check_times = 10000;
   update->url = (uint8 *)os_zalloc(512);

   os_sprintf((char*)update->url,"GET %s%s HTTP/1.1\r\n""Host: "IPSTR":%d\r\n""Connection: close\r\n""\r\n", path, file, IP2STR(update->ip), update->port);

   if (system_upgrade_start(update) == false)
   {
      os_printf("[OTA]Could not start upgrade\n");

      os_free(update->pespconn);
      os_free(update->url);
      os_free(update);
   }
   else
   {
      os_printf("[OTA]Upgrading...\n");
   }
}

ip_addr_t test_ip;
IP4_ADDR(&test_ip, 255,255,255,255);
handleUpgrade(2, &test_ip, 80, "/");



connected with YourSSID, channel 1
dhcp client start...
ip:192.168.1.155,mask:255.255.255.0,gw:192.168.1.1
[network_check_ip]

Current Details
Time: 13487456
Free Heap: 47416
Station MAC Address a0-20-a6-04-07-08
AP MAC Address a2-20-a6-04-07-08
Connect status - 5
IP Address: 192.168.1.155
Netmask : 255.255.255.0
Gateway : 192.168.1.1
Calling myConnectToStronestWifiCallback callback
[StationCallBackTimeInit]
[OTA]Upgrade available version: 2
[OTA]Server 184.168.156.1:80. Path: /user2.bin
system_upgrade_start
upgrade_connect 46712
[OTA]Upgrading...
upgrade_connect_cb
pusrdata = HTTP/1.1 200 OK
Date: Tue, 26 Jun 2018 02:03:16 GMT
Server: Apache
Last-Modified: Mon, 29 Jul 2013 16:45:15 GMT
Accept-Ranges: bytes
Content-Length: 76
Vary: Accept-Encoding
Connection: close
Content-Type: text/html


sumlength = 76
sec_block 1
.erase sector=257 ok

ALL=1 sectors erase ok!
upgrade_get_sum_disconcb 46208
upgrade_connect_cb
sumlength = 76
upgrade file download start.
error bin file
upgrade_check
[OTA]failed!
pm open,type:0 0

Her Majesty
Posts: 345
Joined: Mon Oct 27, 2014 11:09 am

Re: Protocol Handshake with system_upgrade_start()

Postby Her Majesty » Wed Jun 27, 2018 3:28 pm

Content-Length: 76


It seems not be a valid bin according to the length 76, maybe you should check the url first.

AgentSmithers
Posts: 123
Joined: Sat Apr 01, 2017 1:21 am
Contact:

Re: Protocol Handshake with system_upgrade_start()

Postby AgentSmithers » Thu Jun 28, 2018 6:09 am

Her Majesty wrote:
Content-Length: 76


It seems not be a valid bin according to the length 76, maybe you should check the url first.


I spotted that too. Testing the URL I got this. Maybe the next step is setting up a LAN server and wiresharking it.

267 KB (273,696 bytes)

AgentSmithers
Posts: 123
Joined: Sat Apr 01, 2017 1:21 am
Contact:

Re: Protocol Handshake with system_upgrade_start()

Postby AgentSmithers » Thu Jul 05, 2018 5:02 am

Code: Select all

[OTA]Upgrade available version: 2
[OTA]Server 192.168.1.5:80. Path: /user2.bin
Requesting for GET /user2.bin HTTP/1.1
Host: Website.net
Connection: close


system_upgrade_start
upgrade_connect 46680
[OTA]Upgrading...
upgrade_connect_cb
pusrdata = HTTP/1.1 200 OK
Date: Wed, 04 Jul 2018 20:54:55 GMT
Server: Apache
Last-Modified: Tue, 26 Jun 2018 00:38:32 GMT
ETag: "42d20-56f80b7b483b3"
Accept-Ranges: bytes
Content-Length: 273696
Connection: close
Content-Type: application/octet-stream


sumlength = 273696
sec_block 67
...................................................................upgrade_get_sum_disconcb 44248
erase sector=257 ok
erase sector=258 ok
erase sector=259 ok
erase sector=260 ok
erase sector=261 ok
erase sector=262 ok
erase sector=263 ok
erase sector=264 ok
erase sector=265 ok
erase sector=266 ok
erase sector=267 ok
erase sector=268 ok
erase sector=269 ok
erase sector=270 ok
erase sector=271 ok
erase sector=272 ok
erase sector=273 ok
erase sector=274 ok
erase sector=274 ok
erase sector=275 ok
erase sector=276 ok
erase sector=277 ok
erase sector=278 ok
erase sector=279 ok
erase sector=280 ok
erase sector=281 ok
erase sector=282 ok
erase sector=283 ok
erase sector=284 ok
erase sector=285 ok
erase sector=286 ok
erase sector=287 ok
erase sector=288 ok
erase sector=289 ok
erase sector=290 ok
erase sector=291 ok
erase sector=292 ok
erase sector=293 ok
erase sector=294 ok
erase sector=295 ok
erase sector=296 ok
erase sector=297 ok
erase sector=298 ok
erase sector=299 ok
erase sector=300 ok
erase sector=301 ok
erase sector=302 ok
erase sector=303 ok
erase sector=304 ok
erase sector=305 ok
erase sector=306 ok
erase sector=307 ok
erase sector=308 ok
erase sector=309 ok
erase sector=310 ok
erase sector=311 ok
erase sector=312 ok
erase sector=313 ok
erase sector=314 ok
erase sector=315 ok
erase sector=316 ok
erase sector=317 ok
erase sector=318 ok
erase sector=319 ok
erase sector=320 ok
erase sector=321 ok
erase sector=322 ok
erase sector=323 ok

ALL=67 sectors erase ok!
upgrade_connect_cb
sumlength = 273696
upgrade file download start.
please check the bin file
totallen = 2668
totallen = 4128
upgrade_check
[OTA]failed!
pm open,type:0 0


Okay, The issue I had was that my host is on "Shared" hosting so I had to put my Hostname in the HTTP GET request directly instead of the IP:PORT as the "HOST:" entry. Once I did that It found the length of the actual Bin file.

Now at this point, I think it's trying to do a 2nd get request and is unable to get to the actual bin file. If I am correct is there a way to override the "GET" request the firmware is making when reaching out for the new .BIN file? am I in the right direction?


UPDATE: I also found this, I am unsure it is related. Is there something in the .BIN file that will identify the Bin file it is 1 vs. 2?
https://stackoverflow.com/questions/471 ... e-bin-file

Attached is a PCAP capture I did on a local webserver showing the same result. I think something is up with the firmware somehow, But I've tested the .BIN in my own custom FOTA and it works fine, Now that I am using the Built in HTTP method I am having the issue with the same verified FOTA .BIN file.

Update: I may be hitting a 10second timeout?

https://github.com/willemwouters/ESP826 ... /upgrade.c

Code: Select all

LOCAL void ICACHE_FLASH_ATTR
upgrade_check(struct upgrade_server_info *server)
{
   UPGRADE_DBG("upgrade_check\n");

    if (system_upgrade_flag_check() != UPGRADE_FLAG_FINISH) {
       totallength = 0;
       sumlength = 0;
        os_timer_disarm(&upgrade_timer);
        system_upgrade_flag_set(UPGRADE_FLAG_IDLE);
        upgrade_deinit();
        server->upgrade_flag = false;

        if (server->check_cb != NULL) {
            server->check_cb(server);
        }
    } else {
        os_timer_disarm(&upgrade_timer);
        upgrade_deinit();
        server->upgrade_flag = true;

        if (server->check_cb != NULL) {
            server->check_cb(server);
        }
    }
#ifdef UPGRADE_SSL_ENABLE
    espconn_secure_disconnect(upgrade_conn);
#else
    espconn_disconnect(upgrade_conn);

#endif
}


Post Script: Does this work at all with Custom Firmware? Or is this function only for AT+ firmware?
Attachments
EspFirmware.zip
PCAP Capture
(7.68 KiB) Downloaded 20 times

Who is online

Users browsing this forum: Google [Bot] and 17 guests