Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing SLIP Escaping in command transfers #1

Open
BatListener opened this issue Oct 1, 2020 · 0 comments
Open

Missing SLIP Escaping in command transfers #1

BatListener opened this issue Oct 1, 2020 · 0 comments

Comments

@BatListener
Copy link

Thanks a lot for sharing, that helped me a lot!
One topic I stumbled on:
There is no SLIP escaping when sending commands to the nRF bootloader. This can create problems when a command parameter contains the EOM character (0xC0). The bootloader on the nRF side then prematurely cuts the commad block, leaving possibly invalid parameters for its upper levels.
I had that problem with a specific payload length of a block to download. The last CREATE OBJECT command will use the remaining size of the block instead of the maximum block size. If that remainig blocksize generates a 0xC0 in the length parameter of the CREATE OBJECT command, the command will fail. I had that with a remaining block size of 2240 bytes, which results in a length encoding of 0xC0, 0x08, 0x00, 0x00.
Correction is easy:
Just add SLIP escaping in fwuPrepareSendBuffer(). I did it this way:

static void fwuPrepareSendBuffer(TFwu *fwu, uint8_t *data, uint8_t len)
{
    // TODO assert privateCommandState == FWU_CS_IDLE | _DONE | _FAIL
    // TODO assert len <= FWU_REQUEST_BUF_SIZE
    
    uint8_t i;
    uint8_t *p = &fwu->privateRequestBuf[0];

    fwu->privateRequestIx = 0;
    fwu->privateRequestLen = len + 1;
    fwu->privateResponseLen = 0;

    // Copy the data into our internal buffer.
    // Handle SLIP escaping.
    for (i = 0; i < len; i++) {
        // SLIP escape characters: C0->DBDC, DB->DBDD
        if ((*data == 0xC0) || (*data == 0xDB))  {
            *p++ = 0xDB;
            *p++ = (*data++ == 0xC0) ? 0xDC : 0xDD;
            fwu->privateRequestLen++;
        } else {
            *p++ = *data++;
        }
    }
    
    // Add the end-of-message marker.
    *p = FWU_EOM;
    
    // Ready to send!
    fwu->privateCommandRequest = FWU_CR_SEND;
}

Regards and many thanks for sharing!
Michael

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant