Skip to content

Uncorrected read of STM32 internal_flash cache #7615

@innopoltech

Description

@innopoltech

In the "ports/stm/supervisor/internal_flash.c" file
In the "supervisor_flash_read_blocks" function

uint32_t count = (sector_size - (src - sector_start_addr)) / FILESYSTEM_BLOCK_SIZE;
    count = MIN(num_blocks, count);

    if (count < num_blocks && _cache_flash_addr == sector_start_addr) {
        // Read is contained in the cache, so just read cache
        memcpy(dest, (_flash_cache + (src - sector_start_addr)), FILESYSTEM_BLOCK_SIZE * num_blocks);
    } else {
        // The read spans multiple sectors or is in another sector
        // Must write out anything in cache before trying to read.
        supervisor_flash_flush();
        memcpy(dest, (uint8_t *)src, FILESYSTEM_BLOCK_SIZE * num_blocks);
    }

If the sector for the block and the cache sector match, if the block size is 1, then the reading will still be done from the flash, because from
count = MIN(num_blocks, count);
count =1 and then (count <num_blocks ) == false

In the other case, when there is a read at the sector boundary, e.g.
uint32_t count = (sector_size - (src - sector_start_addr)) / FILESYSTEM_BLOCK_SIZE =1 and num_blocks = 2, then from
count = MIN(num_blocks, count);
count = 1, and then (count <num_blocks ) == true

memcpy(dest, (_flash_cache + (src - sector_start_addr)), FILESYSTEM_BLOCK_SIZE * num_blocks); will read outside the cache boundaries.

Maybe I didn't understand something?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions