HeapCrash / Challenges / OTA chunk writer

OTA chunk writer

OTA images arrive in 1 KB chunks, each with an offset. The writer erases a 4 KB sector when a chunk starts on a sector boundary, then programs the chunk. Some images fail to boot after a resumed download, a fuzzed update server bricked a unit by overwriting the bootloader, and one unit booted a half-downloaded image.

The code

Embedded C for Cortex-M. It compiles. There are 3 real bugs in it: the kind that pass a bench test and fail in the field.

#define SLOT_ADDR  0x08020000u
#define SECTOR     4096u

bool ota_chunk(uint32_t offset, const uint8_t *data, uint32_t len, bool last)
{
    uint32_t addr = SLOT_ADDR + offset;
    if (offset % SECTOR == 0) {
        flash_erase_sector(addr);
    }
    flash_program(addr, data, len);
    if (last) {
        ota_mark_pending();
    }
    return true;
}

What an interviewer expects

The bugs, reference fixes and the follow-up question are in the practice app, after you've tried it.

Companies that test this

Matched on the skills each company's embedded roles emphasise.

Related challenges

Flag the lines, say what breaks, then fix it and build it with clang in your browser.

Find the bugs