Statistics: Posted by AgentSmithers — Thu Nov 03, 2022 3:47 am
FrenkR wrote:Issue resolved. If you have partition of size 1MB or 2MB, then "irom0_0_seg" address-es are the same for both partitions (looks like 0x1000 or 0x101000 are the same?). If you have 512KB partitions, then you must create LD script with different ROM addresses same way as is in LD files for 1024KB OTA partition segmentation(e.g.eagle.app.v6.new.1024.app1.ld, eagle.app.v6.new.1024.app2.ld).
If you don't do that, then uprading from app1 to app2 with the same bin will work, but if you will empty segment1, bin2 will stop working (which makes sense).
I hope that I saved 3 days of hard work "hunting" all those zillion of exceptions.
Rgds,
Frenk
Statistics: Posted by backupluis — Sat Oct 08, 2022 7:10 pm
Statistics: Posted by backupluis — Sat Oct 08, 2022 6:43 pm
Statistics: Posted by FrenkR — Fri Oct 07, 2022 2:06 am
Code:
#define SYSTEM_PARTITION_OTA_SIZE 0x6A000
#define SYSTEM_PARTITION_OTA_2_ADDR 0x81000
#define SYSTEM_PARTITION_RF_CAL_ADDR 0x1fb000
#define SYSTEM_PARTITION_PHY_DATA_ADDR 0x1fc000
#define SYSTEM_PARTITION_SYSTEM_PARAMETER_ADDR 0x1fd000
#define SYSTEM_PARTITION_CUSTOMER_PRIV_PARAM_ADDR 0x7c000
Code:
Fatal exception (0):
epc1=0x40213e48, epc2=0x00000000, epc3=0x00000000, excvaddr=0x00000000, depc=0x00000000
Fatal exception (0):
epc1=0x40213e48, epc2=0x00000000, epc3=0x00000000, excvaddr=0x00000000, depc=0x00000000
Fatal exception (0):
epc1=0x40213e48, epc2=0x00000000, epc3=0x00000000, excvaddr=0x00000000, depc=0x00000000
Code:
40213e48 <init_bss_data>:
40213e48: fffe61 l32r a6, 40213e40 <print_system_param_sectors+0x18>
40213e4b: fffe41 l32r a4, 40213e44 <print_system_param_sectors+0x1c>
40213e4e: 050c movi.n a5, 0
40213e50: 07b467 bgeu a4, a6, 40213e5b <init_bss_data+0x13>
40213e53: 004452 s8i a5, a4, 0
40213e56: 441b addi.n a4, a4, 1
40213e58: f79467 bne a4, a6, 40213e53 <init_bss_data+0xb>
40213e5b: f00d ret.n
40213e5d: 00 .byte 00
40213e5e: 00 .byte 00
40213e5f: 00 .byte 00
40213e60: 08 90 fe 3f
40213e64: 10 90 fe 3f
40213e68: cc 24 00 40
Statistics: Posted by FrenkR — Wed Oct 05, 2022 9:30 am
Code:
#ifndef __httpPostFota__
#define __httpPostFota__
#include <upgrade.h>
//#define UPGRADE_DEBUG
#ifdef UPGRADE_DEBUG
#define UPGRADE_DBG os_printf
#else
#define UPGRADE_DBG
#endif
#define FOTAHTMLSIZE 8192
typedef void (*FOTAWebRecvFunctionCallback)(void *arg, char *pusrdata, unsigned short length);
FOTAWebRecvFunctionCallback FOTAWebRecvCallBack;
unsigned char * FOTAFormatedHTMLPageWithHttpHeader;
LOCAL struct espconn FOTAWebServerSocket;
static char *FOTAmsg_espconn_state[] =
{
"NONE",
"WAIT",
"LISTEN",
"CONNECT",
"WRITE",
"READ",
"CLOSE"
};
char * FOTAHttpHeader = "HTTP/1.1 200 OK\r\nContent-Length: %d\r\nConnection: close\r\n\r\n";
LOCAL void ICACHE_FLASH_ATTR FOTAHttpSendWithHeader(struct espconn * WifiScanConfigConnection, unsigned char * LFormatedHTMLPage) //This may be able to be merged
{
FOTAFormatedHTMLPageWithHttpHeader = (unsigned char*)os_zalloc(FOTAHTMLSIZE);
if (FOTAFormatedHTMLPageWithHttpHeader == 0x0)
{
os_printf("[%s][%s][%d] - Unable to Alloc memory, exiting\r\n", __FILE__ ,__func__, __LINE__);
return;
}
os_printf("Attaching header and sending\r\n");
os_memset(FOTAFormatedHTMLPageWithHttpHeader, 0, FOTAHTMLSIZE);
os_printf("Attaching header and sending\r\n");
os_sprintf(FOTAFormatedHTMLPageWithHttpHeader, FOTAHttpHeader, os_strlen(LFormatedHTMLPage));
os_printf("Attaching header and sending\r\n");
os_strcat(FOTAFormatedHTMLPageWithHttpHeader, LFormatedHTMLPage);
os_printf("Attaching header and sending\r\n");
espconn_send(WifiScanConfigConnection, (uint8*)FOTAFormatedHTMLPageWithHttpHeader, os_strlen(FOTAFormatedHTMLPageWithHttpHeader));
os_printf("HTTP Response sent bytes: %d\r\n", os_strlen(LFormatedHTMLPage));
os_free(FOTAFormatedHTMLPageWithHttpHeader);
}
LOCAL os_timer_t Firmware_Timer;
static ETSTimer http2spi_flash_reboot_timer;
LOCAL char* ICACHE_FLASH_ATTR http2spi_checkheader(void *buf, int which)
{
os_printf("[%s]\r\n", __func__);
uint8_t *cd = (uint8_t *)buf;
uint32_t *buf32 = buf;
os_printf("OTA hdr %p: %08lX %08lX %08lX %08lX\n", buf,
(long unsigned int)buf32[0],
(long unsigned int)buf32[1],
(long unsigned int)buf32[2],
(long unsigned int)buf32[3]);
if (cd[0] != 0xEA) return "IROM magic missing";
if (cd[1] != 4 || cd[2] > 3 || (cd[3]>>4) > 6) return "bad flash header";
//if (cd[3] < 3 && cd[3] != which) return "Wrong partition binary";
if (((uint16_t *)buf)[3] != 0x4010) return "Invalid entry addr";
if (((uint32_t *)buf)[2] != 0) return "Invalid start offset";
return NULL;
}
long gstaticContentLength = 0; //This keeps the Content length in mem for the life of the transactions
long gContentLength = 0; //This is a countdown of Binary Data left to transmit to the client, this includes the Mime header data! Anything after first CRLFCRLF basiclly
uint16 RemotePortsOpenedInDownload = 0; //Only One port at a time can upload a firmware
bool HeaderRead = false;
int dataparts = 0; //This track the file transfer progress in packets
char FOTAdatabuffer[4] = {0}; //Rolling Ring Buffer
uint32 FOTAdatabufferindex = 0;
uint32_t FOTAburnto = 0x0;
uint32_t gOffset = 0x0;
LOCAL void ICACHE_FLASH_ATTR FOTAWebServerConn_recv_callback(void *arg, char *pusrdata, unsigned short length)
{
bool unloadafter = false;
struct espconn * FOTApespconn = (struct espconn *) arg;
//os_printf("[%s] Client recv'ed from port: ", __func__);
/*
uint16_printf(FOTApespconn->proto.tcp->remote_port);
os_printf(" - ");
uint16_printf(FOTApespconn->proto.tcp->local_port);
/*
//os_printf(FOTApespconn->proto.tcp->client_port);
//os_printf(FOTApespconn->tcp.client_port); //How to identify which socket is triggering this WebServerCallback. What if multiple clients are connected?
os_printf("\r\n");
/*
char* getpost = strstr(pusrdata, "POST /upload");
if (getpost != NULL)
{
}
*/
if (RemotePortsOpenedInDownload == FOTApespconn->proto.tcp->remote_port)
{
//os_printf("Incoming file data gcontentlength = %u lCurrentIncomming: %u\r\n", gContentLength, length);
if (dataparts <= 6)
{
dataparts++;
//os_printf(pusrdata);
//hex_printf(pusrdata, length);
//os_printf("\r\n-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\r\n");
}
char * Ptr = pusrdata;
if (HeaderRead == false) //Move past the header
{
os_printf("Header removed\r\n");
while (!strstarts(Ptr, "\r\n\r\n"))
{
Ptr++; //Increment Ptr
//lengthleft--;
}
while (*Ptr == '\n' || *Ptr == '\r')
{
Ptr++; //Increment Ptr
//lengthleft--;
}
os_printf("%u - %u!\r\n", Ptr, pusrdata);
os_printf("Before %u!\r\n", length);
length -= Ptr-pusrdata;
gContentLength -= length;
os_printf("After removing headers %u! Updating gContentLength(%u)\r\n", length, gContentLength);
HeaderRead = true;
}
//os_printf("---");
//os_printf(Ptr);
//os_printf("gcontentlength(%d) - thispacketLength(%d) = %d remainding, DbufferIndex(%u)\r\n", gContentLength, length, (gContentLength - length), FOTAdatabufferindex);
unsigned short i,ii = 0;
for (i=0; i<length; i++)
{
/*
if (i == 0)
{
for (ii=0; ii<10; ii++)
{
os_printf("%x ", *(Ptr + ii));
}
os_printf("\r\n");
}
*/
if (gContentLength < 3000)
{
if (*(Ptr + i) == '-' && *(Ptr + i + 1) == '-' && *(Ptr + i + 2) == '-' && *(Ptr + i + 3) == '-' && *(Ptr + i + 4) == '-' && *(Ptr + i + 5) == '-')
{
//os_printf("Found Hyphens EOF -\r\n");
char * ResponseToSend = "<!DOCTYPE html><html><head><meta http-equiv=\"refresh\" content=\"3;URL='/'\"></head><body>File Uploaded</body></html>";
FOTAHttpSendWithHeader(FOTApespconn, ResponseToSend);
system_update_cpu_freq(80);
return;
}
}
//os_printf("i=%d", i);
system_soft_wdt_feed();
FOTAdatabuffer[FOTAdatabufferindex%4] = *(Ptr + i);
FOTAdatabufferindex++;
if (FOTAdatabufferindex%4 == 0)
{
system_soft_wdt_feed();
if (spi_flash_write(gOffset, (uint32 *)&FOTAdatabuffer, 4) == SPI_FLASH_RESULT_OK)
{
if (gOffset % 0x10000 == 0)
{
os_printf("%u of packet %u - %x %x %x %x Writen to offset 0x%x bin=0x%x bytesleft=%u!\r\n", i, length, FOTAdatabuffer[0], FOTAdatabuffer[1], FOTAdatabuffer[2], FOTAdatabuffer[3], gOffset, gOffset-FOTAburnto, gContentLength-FOTAdatabufferindex);
}
if (gstaticContentLength - FOTAdatabufferindex < 0x30)
{
//os_printf("%u of packet %u - %x %x %x %x Writen to offset 0x%x bin=0x%x bytesleft=%u!\r\n", i, length, databuffer[0], databuffer[1], databuffer[2], databuffer[3], gOffset, gOffset-FOTAburnto, gContentLength-FOTAdatabufferindex);
}
if (FOTAdatabufferindex > gContentLength - 0x10 || FOTAdatabufferindex < 0x10 )// || i < 0x100 //This does for every packet
{
//os_printf("%u of packet %u - %x %x %x %x Writen to offset 0x%x bin=0x%x bytesleft=%u\r\n", i, length, databuffer[0], databuffer[1], databuffer[2], databuffer[3], gOffset, gOffset-FOTAburnto, gContentLength-FOTAdatabufferindex);
}
gOffset += 4;
}
else
{
os_printf("failed writing length:%d\r\n", length);
}
}
if (FOTAdatabufferindex > 60)
{
//return;
}
}
gContentLength -= length;
//os_printf("Packet Processed ContentLength(%u) of packet FOTAdatabufferindex(%u) Difference(%u)\r\n", gstaticContentLength, FOTAdatabufferindex, (gstaticContentLength - FOTAdatabufferindex));
if (gContentLength == 0)
{
//espconn_disconnect(&FOTAWebServerSocket); //Webservers don't close sockets, clients do
char * ResponseToSend = "<!DOCTYPE html><html><body>File Uploaded</body></html>";
FOTAHttpSendWithHeader(FOTApespconn, ResponseToSend);
}
}
if (strstr(pusrdata, "POST /upload") != NULL)
{
system_update_cpu_freq(160);
dataparts = 1;
RemotePortsOpenedInDownload = FOTApespconn->proto.tcp->remote_port;
os_printf(pusrdata);
int index = 0;
char * Ptr = pusrdata;
while (!strstarts(Ptr, "Content-Length: "))
{
Ptr++; //Increment Potr
}
while (*Ptr != ' ')
{
Ptr++; //Increment Potr
}
Ptr++; //Increment to Content String
char ContentLength[8] = {0};
index = 0;
while (*Ptr != '\r' && *Ptr != '\n')
{
ContentLength[index] = *Ptr;
index++;
Ptr++; //Increment Potr
}
os_printf(ContentLength);
gContentLength = atoi(ContentLength);
gstaticContentLength = gContentLength;
Ptr = pusrdata; //Reset PTR
while (!strstarts(Ptr, "Content-Type: multipart/form-data; boundary="))
{
Ptr++; //Increment Potr
}
while (*Ptr != '=')
{
Ptr++; //Increment Potr
}
Ptr++; //Increment to Content String
char Boundary[64] = {0};
index = 0;
while (*Ptr != '\r' && *Ptr != '\n')
{
Boundary[index] = *Ptr;
index++;
Ptr++; //Increment Ptr
//os_printf("%u\r\n", Ptr);
}
Boundary[index] = 0; //This may not be needed by the way I declared boundary with = {0}
os_printf(Boundary);
os_printf("\r\n");
while (!strstarts(Ptr, "\r\n\r\n"))
{
Ptr++; //Increment Ptr
//os_printf("%u\r\n", Ptr);
}
Ptr += 4;
os_printf("HEAD Content Length of file: %d\r\n", gContentLength); //Start erasing the flash at a given address
if(system_upgrade_userbin_check() == 0)
{
FOTAburnto = 0x101000;
}
else
{
FOTAburnto = 0x1000;
}
gOffset = FOTAburnto;
uint32_t poffset = 0x0;
for (poffset=0x0; gContentLength-poffset>0; poffset++)
{
if ((FOTAburnto+poffset) % SPI_FLASH_SEC_SIZE == 0)
{
//os_printf("Erasing 0x%x\r\n", FOTAburnto+poffset);
spi_flash_erase_sector((FOTAburnto+poffset)/SPI_FLASH_SEC_SIZE); //Doing it here may not be fast enough.
}
}
HeaderRead = false; //SetHeader back to false to prep for incoming header to skip
/*
os_printf("%u\r\n", Ptr);
os_printf("%u\r\n", pusrdata);
os_printf("%u\r\n", (Ptr-pusrdata));
//os_printf("%u\r\n", (pusrdata-Ptr)); //This loops around the interger value giving a result back in the 4millons
os_printf("Length: %u\r\n", length);
os_printf("Logic:%u\r\n", length-(Ptr-pusrdata));
//os_printf("%u\r\n", (&Ptr-&pusrdata)); //We don't really care about the addresses
//os_printf("%u\r\n", (&pusrdata-&Ptr));
*/
if(length-(Ptr-pusrdata) > 0) //It's possible data can be fragmented from the first packet
{
unsigned short packetlen = length-(Ptr-pusrdata);
os_printf("Calling first packet remainding len:%u of %u\r\n", packetlen, length);
FOTAWebServerConn_recv_callback(arg, Ptr, packetlen);//This function normally dosn't get called by chrome being predictable during development, This may change later sadly if IE frags the packets differently
}
else
{
//This is not normally called.
}
}
if (strstr(pusrdata, "GET /favicon.ico HTTP/1.1") != NULL) //Skip FavIcon request
{
espconn_disconnect(&FOTAWebServerSocket);
return;
}
if (strstr(pusrdata, "GET / HTTP/1.1") != NULL) //Display Config request
{
os_printf("[%s]\r\n", __func__);
char * ResponseToSend = NULL;
os_printf("[%s]s\r\n", __func__);
if (system_get_userbin_addr() == 0x0)
{
os_printf("OTA Firmware is not detected, please load the OTA firmware\r\n");
ResponseToSend = "<!DOCTYPE html><html><body>OTA Firmware is not detected, please load the OTA firmware</body></html>";
}
else
{
if(system_upgrade_userbin_check() == 0)
{
ResponseToSend = "<!DOCTYPE html><html><body><form action=\"upload\" method=\"post\" enctype=\"multipart/form-data\">Select image to upload to address 0x101000:<input type=\"file\" name=\"fileToUpload\" id=\"fileToUpload\"><input type=\"submit\" value=\"Upload Image\" name=\"submit\"></form></body></html>";
}
else
{
ResponseToSend = "<!DOCTYPE html><html><body><form action=\"upload\" method=\"post\" enctype=\"multipart/form-data\">Select image to upload to address 0x1000:<input type=\"file\" name=\"fileToUpload\" id=\"fileToUpload\"><input type=\"submit\" value=\"Upload Image\" name=\"submit\"></form></body></html>";
}
}
os_printf("[%s]s1\r\n", __func__);
//espconn_send(FOTApespconn, ResponseToSend, os_strlen(ResponseToSend));
FOTAHttpSendWithHeader(FOTApespconn, ResponseToSend);
os_printf("[%s]s2\r\n", __func__);
}
}
LOCAL void ICACHE_FLASH_ATTR FOTAWebServerConn_connect_callback(void *arg)
{
os_printf("[%s] Client Connected from port: ", __func__);
struct espconn * FOTApespconn = (struct espconn *) arg;
uint16_printf(FOTApespconn->proto.tcp->remote_port);
os_printf(" - ");
uint16_printf(FOTApespconn->proto.tcp->local_port);
os_printf("\r\n");
return;
remot_info *premot = NULL;
uint8 reter = espconn_get_connection_info(FOTApespconn,&premot,0);
if (reter == ESPCONN_OK)
{
if(FOTApespconn->link_cnt)
{
os_printf("EspConn %u TCP connections:\r\n", FOTApespconn->link_cnt);
int i = 0;
while(i < FOTApespconn->link_cnt)
{
os_printf("%d) " IPSTR ":%u %s\r\n", i, IP2STR(premot[i].remote_ip), premot[i].remote_port, FOTAmsg_espconn_state[premot[i].state] );
i++;
};
}
}
else
{
os_printf("EspConn getting TCP connections failed: %d\r\n", reter);
}
}
static ETSTimer http2spi_flash_reboot_timer;
void ICACHE_FLASH_ATTR FOTAWebServerConn_disconnect_callback(void *arg)
{
os_printf("[%s] Client Disconnected from port: ", __func__);
struct espconn * FOTApespconn = (struct espconn *) arg;
uint16_printf(FOTApespconn->proto.tcp->remote_port);
os_printf(" - ");
uint16_printf(FOTApespconn->proto.tcp->local_port);
os_printf("\r\n");
if (RemotePortsOpenedInDownload == FOTApespconn->proto.tcp->remote_port)
{
os_printf("Last Status: gcontentlength(%d), DbufferIndex(%u)\r\n", gContentLength, FOTAdatabufferindex);
uint32 Http2SPI_buf[8];
spi_flash_read(FOTAburnto, Http2SPI_buf, sizeof(Http2SPI_buf));
uint8 id = system_upgrade_userbin_check();
char *err = http2spi_checkheader(Http2SPI_buf, 1-id);
if (err != NULL) {
os_printf("OTA Not a bootable bin, %s\n HTTP to SPI completed.\r\n", err);
return -1;
}
os_printf("Reboot needed, rebooting...");
// Schedule a reboot
system_upgrade_flag_set(UPGRADE_FLAG_FINISH);
os_timer_disarm(&http2spi_flash_reboot_timer);
os_timer_setfn(&http2spi_flash_reboot_timer, (os_timer_func_t *)system_upgrade_reboot, NULL);
os_timer_arm(&http2spi_flash_reboot_timer, 2000, 1);
}
}
void ICACHE_FLASH_ATTR FOTAWebServerConn_sent_callback(void *arg)
{
os_printf("[%s] Data Sent\r\n", __func__);
espconn_disconnect(&FOTAWebServerSocket);
}
void ICACHE_FLASH_ATTR init_FOTAwebserver(uint16 port, void * LFOTAWebRecvCallBack(void *arg, char *pusrdata, unsigned short length)) //LFOTAWebRecvCallBack may need to point to the next pointer of the function and not the parent recv function
{
os_printf("[%s]\r\n", __func__);
//FOTAFormatedHTMLPageWithHttpHeader = (unsigned char*)os_zalloc(FOTAHTMLSIZE); //Never alloc on init. only when the far is used then free it asap!
if (LFOTAWebRecvCallBack != NULL)
{
os_printf("Setting Callback for WebServer\r\n");
FOTAWebRecvCallBack = LFOTAWebRecvCallBack;
}
FOTAWebServerSocket.type = ESPCONN_TCP;
FOTAWebServerSocket.state = ESPCONN_NONE;
FOTAWebServerSocket.proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp));
if (FOTAWebServerSocket.proto.tcp == 0x0)
{
os_printf("[%s][%s][%d] - Unable to Alloc memory, exiting\r\n", __FILE__ ,__func__, __LINE__);
return;
}
FOTAWebServerSocket.proto.tcp->local_port = port; // ESP8266 tcp port
espconn_accept(&FOTAWebServerSocket); // create tcp
espconn_regist_connectcb(&FOTAWebServerSocket, FOTAWebServerConn_connect_callback);
espconn_regist_recvcb(&FOTAWebServerSocket, FOTAWebServerConn_recv_callback); // register a tcp packet receiving callback
espconn_regist_sentcb(&FOTAWebServerSocket, FOTAWebServerConn_sent_callback);
espconn_regist_disconcb(&FOTAWebServerSocket, FOTAWebServerConn_disconnect_callback);
}
#endif
Statistics: Posted by AgentSmithers — Thu Dec 09, 2021 5:09 pm
Statistics: Posted by AgentSmithers — Sun Feb 18, 2018 2:03 pm
Statistics: Posted by JulienInnovel — Mon Feb 12, 2018 6:55 pm
Statistics: Posted by Her Mary — Mon Feb 12, 2018 5:27 pm
Statistics: Posted by JulienInnovel — Thu Feb 08, 2018 5:28 pm
Statistics: Posted by JulienInnovel — Wed Feb 07, 2018 8:56 pm
Statistics: Posted by JulienInnovel — Tue Feb 06, 2018 1:01 am