HeapCrash / Challenges / Startup flash CRC self-test

Startup flash CRC self-test

For a Class B safety requirement, firmware checks its flash image at start-up against a CRC-32 (the zlib/Ethernet variant) that a post-link step appends right after the image. The check failed on every board, so someone made it 'warn only'. Months later a unit with a corrupted flash byte kept running and misbehaved in the field.

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.

extern uint32_t __app_start, __app_end;    /* linker-script symbols */
extern const uint32_t __app_crc;           /* CRC word stored right after the image */

static uint32_t crc32(const uint8_t *p, uint32_t n)
{
    uint32_t c = 0;
    while (n--) {
        c ^= *p++;
        for (int k = 0; k < 8; k++) c = (c >> 1) ^ (0xEDB88320u & (0u - (c & 1u)));
    }
    return c;
}

bool flash_selftest(void)
{
    uint32_t len = __app_end - __app_start;
    uint32_t crc = crc32((const uint8_t *)__app_start, len);
    if (crc != __app_crc) {
        log_warn("flash CRC mismatch");
    }
    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