diff --git a/Makefile b/Makefile index f060dd9..6610c2e 100644 --- a/Makefile +++ b/Makefile @@ -76,7 +76,7 @@ LIBS = c gcc hal phy pp net80211 wpa main lwip # compiler flags using during compilation of source files CFLAGS = -Os -ggdb -std=c99 -Werror -Wpointer-arith -Wundef -Wall -Wl,-EL -fno-inline-functions \ -nostdlib -mlongcalls -mtext-section-literals -D__ets__ -DICACHE_FLASH -D_STDINT_H \ - -Wno-address -DESPFS_POS=$(ESPFS_POS) -DESPFS_SIZE=$(ESPFS_SIZE) + -Wno-address -DFIRMWARE_SIZE=$(ESP_FLASH_MAX) # linker flags used to generate the main object file LDFLAGS = -nostdlib -Wl,--no-check-sections -u call_user_start -Wl,-static @@ -235,11 +235,11 @@ build/eagle.esphttpd.v6.ld: $(SDK_LDDIR)/eagle.app.v6.ld $(SDK_LDDIR)/eagle.app.v6.ld >$@ build/eagle.esphttpd1.v6.ld: $(SDK_LDDIR)/eagle.app.v6.new.512.app1.ld $(Q) sed -e '/\.irom\.text/{' -e 'a . = ALIGN (4);' -e 'a *(.espfs)' -e '}' \ - -e '/^ irom0_0_seg/ s/2B000/32000/' \ + -e '/^ irom0_0_seg/ s/2B000/38000/' \ $(SDK_LDDIR)/eagle.app.v6.new.512.app1.ld >$@ build/eagle.esphttpd2.v6.ld: $(SDK_LDDIR)/eagle.app.v6.new.512.app2.ld $(Q) sed -e '/\.irom\.text/{' -e 'a . = ALIGN (4);' -e 'a *(.espfs)' -e '}' \ - -e '/^ irom0_0_seg/ s/2B000/32000/' \ + -e '/^ irom0_0_seg/ s/2B000/38000/' \ $(SDK_LDDIR)/eagle.app.v6.new.512.app2.ld >$@ blankflash: diff --git a/html/cats/junge-katze-iv.jpg b/html/cats/junge-katze-iv.jpg- similarity index 100% rename from html/cats/junge-katze-iv.jpg rename to html/cats/junge-katze-iv.jpg- diff --git a/html/cats/junge-katze-iv_01.jpg b/html/cats/junge-katze-iv_01.jpg new file mode 100644 index 0000000..0b2944e Binary files /dev/null and b/html/cats/junge-katze-iv_01.jpg differ diff --git a/html/index.tpl b/html/index.tpl index 2f9b18e..50c49cc 100644 --- a/html/index.tpl +++ b/html/index.tpl @@ -18,7 +18,7 @@ been loaded %counter% times.

And because we're on the Internets now, here are the required pictures of cats:
-
+

diff --git a/include/esp8266.h b/include/esp8266.h index 69e6c8a..ce92455 100644 --- a/include/esp8266.h +++ b/include/esp8266.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include "espmissingincludes.h" diff --git a/user/cgiflash.c b/user/cgiflash.c index db82e06..f5917a3 100644 --- a/user/cgiflash.c +++ b/user/cgiflash.c @@ -39,37 +39,89 @@ int ICACHE_FLASH_ATTR cgiReadFlash(HttpdConnData *connData) { if (*pos>=0x40200000+(512*1024)) return HTTPD_CGI_DONE; else return HTTPD_CGI_MORE; } -//Cgi that allows the ESPFS image to be replaced via http POST -int ICACHE_FLASH_ATTR cgiUploadEspfs(HttpdConnData *connData) { +// Return which firmware needs to be uploaded next +int ICACHE_FLASH_ATTR cgiGetFirmwareNext(HttpdConnData *connData) { if (connData->conn==NULL) { //Connection aborted. Clean up. return HTTPD_CGI_DONE; } - /* TODO: fix this check so it calculates the end of the irom segment minus the start of the espfs - if(connData->post->len > ESPFS_SIZE){ + + uint8 id = system_upgrade_userbin_check(); + httpdStartResponse(connData, 200); + httpdHeader(connData, "Content-Type", "text/plain"); + httpdEndHeaders(connData); + httpdSend(connData, (id == 1 ? "user1.bin" : "user2.bin"), -1); + os_printf("Next firmware: user%d.bin\n", 1-id); + + return HTTPD_CGI_DONE; +} + +//Cgi that allows the firmware to be replaced via http POST +int ICACHE_FLASH_ATTR cgiUploadFirmware(HttpdConnData *connData) { + if (connData->conn==NULL) { + //Connection aborted. Clean up. + return HTTPD_CGI_DONE; + } + + if(connData->post->len > FIRMWARE_SIZE){ // The uploaded file is too large - os_printf("ESPFS file too large\n"); - httpdSend(connData, "HTTP/1.0 500 Internal Server Error\r\nServer: esp8266-httpd/0.3\r\nConnection: close\r\nContent-Type: text/plain\r\nContent-Length: 24\r\n\r\nESPFS image loo large.\r\n", -1); + os_printf("Firmware image too large\n"); + httpdStartResponse(connData, 400); + httpdHeader(connData, "Content-Type", "text/plain"); + httpdEndHeaders(connData); + httpdSend(connData, "Firmware image loo large.\r\n", -1); return HTTPD_CGI_DONE; } - */ - // The source should be 4byte aligned, so go ahead and flash whatever we have - int address = ESPFS_POS + connData->post->received - connData->post->buffLen; + uint8 id = system_upgrade_userbin_check(); + + int address; + if (id == 1) { + address = 4*1024; // start after 4KB boot partition + } else { + // 4KB boot, firmware1, 16KB user param, 4KB reserved + address = 4*1024 + FIRMWARE_SIZE + 16*1024 + 4*1024; + } + address += connData->post->received - connData->post->buffLen; + if(address % SPI_FLASH_SEC_SIZE == 0){ // We need to erase this block - os_printf("Erasing flash at %d\n", address/SPI_FLASH_SEC_SIZE); + os_printf("Erasing flash at 0x%05x (id=%d)\n", address, 2-id); spi_flash_erase_sector(address/SPI_FLASH_SEC_SIZE); } + // Write the data - os_printf("Writing at: 0x%x\n", address); + os_printf("Writing %d bytes at 0x%05x (%d of %d)\n", connData->post->buffSize, address, + connData->post->received, connData->post->len); spi_flash_write(address, (uint32 *)connData->post->buff, connData->post->buffLen); - os_printf("Wrote %d bytes (%dB of %d)\n", connData->post->buffSize, connData->post->received, connData->post->len);//&connData->postBuff)); if (connData->post->received == connData->post->len){ - httpdSend(connData, "Finished uploading", -1); + // TODO: verify the firmware (is there a checksum or something???) + httpdStartResponse(connData, 200); + httpdEndHeaders(connData); return HTTPD_CGI_DONE; } else { return HTTPD_CGI_MORE; } } + +// Handle request to reboot into the new firmware +int ICACHE_FLASH_ATTR cgiRebootFirmware(HttpdConnData *connData) { + if (connData->conn==NULL) { + //Connection aborted. Clean up. + return HTTPD_CGI_DONE; + } + + // TODO: sanity-check that the 'next' partition actually contains something that looks like + // valid firmware + + // This hsould probably be forked into a separate task that waits a second to let the + // current HTTP request finish... + system_upgrade_flag_set(UPGRADE_FLAG_FINISH); + system_upgrade_reboot(); + httpdStartResponse(connData, 200); + httpdEndHeaders(connData); + return HTTPD_CGI_DONE; +} + + diff --git a/user/cgiflash.h b/user/cgiflash.h index 268a62f..15c60c2 100644 --- a/user/cgiflash.h +++ b/user/cgiflash.h @@ -4,6 +4,8 @@ #include "httpd.h" int cgiReadFlash(HttpdConnData *connData); -int cgiUploadEspfs(HttpdConnData *connData); +int cgiGetFirmwareNext(HttpdConnData *connData); +int cgiUploadFirmware(HttpdConnData *connData); +int cgiRebootFirmware(HttpdConnData *connData); -#endif \ No newline at end of file +#endif diff --git a/user/user_main.c b/user/user_main.c index c5a0aad..58ff4dd 100644 --- a/user/user_main.c +++ b/user/user_main.c @@ -53,11 +53,13 @@ should be placed above the URLs they protect. */ HttpdBuiltInUrl builtInUrls[]={ {"/", cgiRedirect, "/index.tpl"}, - {"/flash.bin", cgiReadFlash, NULL}, + {"/flash/download", cgiReadFlash, NULL}, + {"/flash/next", cgiGetFirmwareNext, NULL}, + {"/flash/upload", cgiUploadFirmware, NULL}, + {"/flash/reboot", cgiRebootFirmware, NULL}, {"/led.tpl", cgiEspFsTemplate, tplLed}, {"/index.tpl", cgiEspFsTemplate, tplCounter}, {"/led.cgi", cgiLed, NULL}, - {"/updateweb.cgi", cgiUploadEspfs, NULL}, //Routines to make the /wifi URL and everything beneath it work.