jeelabs / esp-link

esp8266 wifi-serial bridge, outbound TCP, and arduino/AVR/LPC/NXP programmer
Other
2.82k stars 720 forks source link

loss of data a possibility? #555

Open SaimShuja opened 1 year ago

SaimShuja commented 1 year ago

following code accumulate record data from hex file into the optibootdata page buffer

`// append record

uint16_t recLen = getHexValue(buf, 2);
for (uint16_t i=0; i<recLen; i++)
  optibootData->pageBuf[optibootData->pageLen++] = getHexValue(buf+8+2*i, 2);
// program page, if we have a full page
if (optibootData->pageLen >= optibootData->pgmSz) {
  //DBG("OB full\n");
  DBG("processRecord %d, call programPage() %08x\n", optibootData->pgmSz, optibootData->address + optibootData->segment);
  if (!programPage()) return false;
} `

reference

it expects that data length in one line of hex file be exactly 8*2 bytes, but if record length is not exactly 16 bytes for example is 14 bytes or 10 bytes then first record will be accumulated in page buffer , then optibootData->pageLen will be 2 bytes less then 128bytes which is the page size of MCU, thus making it read one more record into page buffer , but when it reads another record which is 16 bytes makeing optibootData->pageLen greater then 128 , rather 126+16 in case of a (14 byte record present). and it will full fill the condition (optibootData->pageLen >= optibootData->pgmSz) thus will program the page into the MCU , but when programing page it will only write first 128 bytes of page buffer as under.

uint16_t pgmLen = optibootData->pageLen; if (pgmLen > optibootData->pgmSz) pgmLen = optibootData->pgmSz;\ DBG("OB pgm %d@0x%x\n", pgmLen, optibootData->address); reference

which will cause discarding of bytes above 128 in the page buffer thus rendering the programmed firmware faulty, because then the firmware written to the MCU will not have those discarded bytes (bytes 129 - 142) .

this file works this file works because the less bytes record is at the end, so no extra bytes are read into the buffer this fails this fails because it reads more byte which are later discarded.