Moved the flash cgis out into their own file and made the uploadEspfs cgi work properly
This commit is contained in:
-59
@@ -81,62 +81,3 @@ int ICACHE_FLASH_ATTR tplCounter(HttpdConnData *connData, char *token, void **ar
|
|||||||
httpdSend(connData, buff, -1);
|
httpdSend(connData, buff, -1);
|
||||||
return HTTPD_CGI_DONE;
|
return HTTPD_CGI_DONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//Cgi that reads the SPI flash. Assumes 512KByte flash.
|
|
||||||
int ICACHE_FLASH_ATTR cgiReadFlash(HttpdConnData *connData) {
|
|
||||||
int *pos=(int *)&connData->cgiData;
|
|
||||||
if (connData->conn==NULL) {
|
|
||||||
//Connection aborted. Clean up.
|
|
||||||
return HTTPD_CGI_DONE;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (*pos==0) {
|
|
||||||
os_printf("Start flash download.\n");
|
|
||||||
httpdStartResponse(connData, 200);
|
|
||||||
httpdHeader(connData, "Content-Type", "application/bin");
|
|
||||||
httpdEndHeaders(connData);
|
|
||||||
*pos=0x40200000;
|
|
||||||
return HTTPD_CGI_MORE;
|
|
||||||
}
|
|
||||||
//Send 1K of flash per call. We will get called again if we haven't sent 512K yet.
|
|
||||||
espconn_sent(connData->conn, (uint8 *)(*pos), 1024);
|
|
||||||
*pos+=1024;
|
|
||||||
if (*pos>=0x40200000+(512*1024)) return HTTPD_CGI_DONE; else return HTTPD_CGI_MORE;
|
|
||||||
}
|
|
||||||
|
|
||||||
int ICACHE_FLASH_ATTR cgiUploadEspfs(HttpdConnData *connData) {
|
|
||||||
if (connData->conn==NULL) {
|
|
||||||
//Connection aborted. Clean up.
|
|
||||||
return HTTPD_CGI_DONE;
|
|
||||||
}
|
|
||||||
SpiFlashOpResult ret;
|
|
||||||
int x;
|
|
||||||
int flashOff = ESPFS_POS;
|
|
||||||
int flashSize = ESPFS_SIZE;
|
|
||||||
|
|
||||||
//If this is the first time, erase the flash sector
|
|
||||||
if (connData->post->received == 0){
|
|
||||||
os_printf("Erasing flash at 0x%x...\n", flashOff);
|
|
||||||
// Which segment are we flashing?
|
|
||||||
for (x=0; x<flashSize; x+=4096){
|
|
||||||
spi_flash_erase_sector((flashOff+x)/0x1000);
|
|
||||||
}
|
|
||||||
os_printf("Done erasing.\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
// The source should be 4byte aligned, so go ahead an flash whatever we have
|
|
||||||
ret=spi_flash_write((flashOff + connData->post->received), (uint32 *)connData->post->buff, connData->post->buffLen);
|
|
||||||
os_printf("Flash return %d\n", ret);
|
|
||||||
|
|
||||||
// Count bytes for data
|
|
||||||
connData->post->received += connData->post->buffSize;//connData->postBuff);
|
|
||||||
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);
|
|
||||||
return HTTPD_CGI_DONE;
|
|
||||||
} else {
|
|
||||||
return HTTPD_CGI_MORE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -5,8 +5,6 @@
|
|||||||
|
|
||||||
int cgiLed(HttpdConnData *connData);
|
int cgiLed(HttpdConnData *connData);
|
||||||
int tplLed(HttpdConnData *connData, char *token, void **arg);
|
int tplLed(HttpdConnData *connData, char *token, void **arg);
|
||||||
int cgiReadFlash(HttpdConnData *connData);
|
|
||||||
int tplCounter(HttpdConnData *connData, char *token, void **arg);
|
int tplCounter(HttpdConnData *connData, char *token, void **arg);
|
||||||
int cgiUploadEspfs(HttpdConnData *connData);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
/*
|
||||||
|
Some flash handling cgi routines. Used for reading the existing flash and updating the ESPFS image.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ----------------------------------------------------------------------------
|
||||||
|
* "THE BEER-WARE LICENSE" (Revision 42):
|
||||||
|
* Jeroen Domburg <jeroen@spritesmods.com> wrote this file. As long as you retain
|
||||||
|
* this notice you can do whatever you want with this stuff. If we meet some day,
|
||||||
|
* and you think this stuff is worth it, you can buy me a beer in return.
|
||||||
|
* ----------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include <osapi.h>
|
||||||
|
#include "user_interface.h"
|
||||||
|
#include "mem.h"
|
||||||
|
#include "httpd.h"
|
||||||
|
#include "cgiflash.h"
|
||||||
|
#include "io.h"
|
||||||
|
#include <ip_addr.h>
|
||||||
|
#include "espmissingincludes.h"
|
||||||
|
#include "../include/httpdconfig.h"
|
||||||
|
|
||||||
|
|
||||||
|
//Cgi that reads the SPI flash. Assumes 512KByte flash.
|
||||||
|
int ICACHE_FLASH_ATTR cgiReadFlash(HttpdConnData *connData) {
|
||||||
|
int *pos=(int *)&connData->cgiData;
|
||||||
|
if (connData->conn==NULL) {
|
||||||
|
//Connection aborted. Clean up.
|
||||||
|
return HTTPD_CGI_DONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (*pos==0) {
|
||||||
|
os_printf("Start flash download.\n");
|
||||||
|
httpdStartResponse(connData, 200);
|
||||||
|
httpdHeader(connData, "Content-Type", "application/bin");
|
||||||
|
httpdEndHeaders(connData);
|
||||||
|
*pos=0x40200000;
|
||||||
|
return HTTPD_CGI_MORE;
|
||||||
|
}
|
||||||
|
//Send 1K of flash per call. We will get called again if we haven't sent 512K yet.
|
||||||
|
espconn_sent(connData->conn, (uint8 *)(*pos), 1024);
|
||||||
|
*pos+=1024;
|
||||||
|
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) {
|
||||||
|
if (connData->conn==NULL) {
|
||||||
|
//Connection aborted. Clean up.
|
||||||
|
return HTTPD_CGI_DONE;
|
||||||
|
}
|
||||||
|
if(connData->post->len > ESPFS_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\nContent-Type: text/plain\r\nContent-Length: 24\r\n\r\nESPFS 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;
|
||||||
|
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);
|
||||||
|
spi_flash_erase_sector(address/SPI_FLASH_SEC_SIZE);
|
||||||
|
}
|
||||||
|
// Write the data
|
||||||
|
os_printf("Writing at: 0x%x\n", address);
|
||||||
|
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);
|
||||||
|
return HTTPD_CGI_DONE;
|
||||||
|
} else {
|
||||||
|
return HTTPD_CGI_MORE;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
#ifndef CGIFLASH_H
|
||||||
|
#define CGIFLASH_H
|
||||||
|
|
||||||
|
#include "httpd.h"
|
||||||
|
|
||||||
|
int cgiReadFlash(HttpdConnData *connData);
|
||||||
|
int cgiUploadEspfs(HttpdConnData *connData);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -18,6 +18,7 @@
|
|||||||
#include "httpdespfs.h"
|
#include "httpdespfs.h"
|
||||||
#include "cgi.h"
|
#include "cgi.h"
|
||||||
#include "cgiwifi.h"
|
#include "cgiwifi.h"
|
||||||
|
#include "cgiflash.h"
|
||||||
#include "stdout.h"
|
#include "stdout.h"
|
||||||
#include "auth.h"
|
#include "auth.h"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user