stm32/flash: Update flash_get_sector_info to return -1 on invalid addr.

So the caller can tell when an invalid address is used and can take
appropriate action.
pull/6211/head
Andrew Leech 2020-06-30 16:33:32 +10:00 zatwierdzone przez Damien George
rodzic 8bbaa20227
commit 40006813c3
4 zmienionych plików z 18 dodań i 15 usunięć

Wyświetl plik

@ -151,7 +151,7 @@ bool flash_is_valid_addr(uint32_t addr) {
return flash_layout[0].base_address <= addr && addr < end_of_flash;
}
uint32_t flash_get_sector_info(uint32_t addr, uint32_t *start_addr, uint32_t *size) {
int32_t flash_get_sector_info(uint32_t addr, uint32_t *start_addr, uint32_t *size) {
if (addr >= flash_layout[0].base_address) {
uint32_t sector_index = 0;
for (int i = 0; i < MP_ARRAY_SIZE(flash_layout); ++i) {
@ -172,7 +172,7 @@ uint32_t flash_get_sector_info(uint32_t addr, uint32_t *start_addr, uint32_t *si
}
}
}
return 0;
return -1;
}
int flash_erase(uint32_t flash_dest, uint32_t num_word32) {

Wyświetl plik

@ -27,7 +27,7 @@
#define MICROPY_INCLUDED_STM32_FLASH_H
bool flash_is_valid_addr(uint32_t addr);
uint32_t flash_get_sector_info(uint32_t addr, uint32_t *start_addr, uint32_t *size);
int32_t flash_get_sector_info(uint32_t addr, uint32_t *start_addr, uint32_t *size);
int flash_erase(uint32_t flash_dest, uint32_t num_word32);
int flash_write(uint32_t flash_dest, const uint32_t *src, uint32_t num_word32);

Wyświetl plik

@ -181,7 +181,7 @@ int32_t flash_bdev_ioctl(uint32_t op, uint32_t arg) {
static uint8_t *flash_cache_get_addr_for_write(uint32_t flash_addr) {
uint32_t flash_sector_start;
uint32_t flash_sector_size;
uint32_t flash_sector_id = flash_get_sector_info(flash_addr, &flash_sector_start, &flash_sector_size);
int32_t flash_sector_id = flash_get_sector_info(flash_addr, &flash_sector_start, &flash_sector_size);
if (flash_sector_size > FLASH_SECTOR_SIZE_MAX) {
flash_sector_size = FLASH_SECTOR_SIZE_MAX;
}
@ -201,7 +201,7 @@ static uint8_t *flash_cache_get_addr_for_write(uint32_t flash_addr) {
static uint8_t *flash_cache_get_addr_for_read(uint32_t flash_addr) {
uint32_t flash_sector_start;
uint32_t flash_sector_size;
uint32_t flash_sector_id = flash_get_sector_info(flash_addr, &flash_sector_start, &flash_sector_size);
int32_t flash_sector_id = flash_get_sector_info(flash_addr, &flash_sector_start, &flash_sector_size);
if (flash_cache_sector_id == flash_sector_id) {
// in cache, copy from there
return (uint8_t *)CACHE_MEM_START_ADDR + flash_addr - flash_sector_start;

Wyświetl plik

@ -476,25 +476,27 @@ static int mboot_flash_mass_erase(void) {
static int mboot_flash_page_erase(uint32_t addr, uint32_t *next_addr) {
uint32_t sector_size = 0;
uint32_t sector = flash_get_sector_info(addr, NULL, &sector_size);
if (sector == 0) {
// Don't allow to erase the sector with this bootloader in it
uint32_t sector_start = 0;
int32_t sector = flash_get_sector_info(addr, &sector_start, &sector_size);
if (sector <= 0) {
// Don't allow to erase the sector with this bootloader in it, or invalid sectors
dfu_context.status = DFU_STATUS_ERROR_ADDRESS;
dfu_context.error = MBOOT_ERROR_STR_OVERWRITE_BOOTLOADER_IDX;
dfu_context.error = (sector == 0) ? MBOOT_ERROR_STR_OVERWRITE_BOOTLOADER_IDX
: MBOOT_ERROR_STR_INVALID_ADDRESS_IDX;
return -1;
}
*next_addr = addr + sector_size;
*next_addr = sector_start + sector_size;
// Erase the flash page.
int ret = flash_erase(addr, sector_size / sizeof(uint32_t));
int ret = flash_erase(sector_start, sector_size / sizeof(uint32_t));
if (ret != 0) {
return ret;
}
// Check the erase set bits to 1, at least for the first 256 bytes
for (int i = 0; i < 64; ++i) {
if (((volatile uint32_t*)addr)[i] != 0xffffffff) {
if (((volatile uint32_t*)sector_start)[i] != 0xffffffff) {
return -2;
}
}
@ -503,11 +505,12 @@ static int mboot_flash_page_erase(uint32_t addr, uint32_t *next_addr) {
}
static int mboot_flash_write(uint32_t addr, const uint8_t *src8, size_t len) {
uint32_t sector = flash_get_sector_info(addr, NULL, NULL);
if (sector == 0) {
int32_t sector = flash_get_sector_info(addr, NULL, NULL);
if (sector <= 0) {
// Don't allow to write the sector with this bootloader in it
dfu_context.status = DFU_STATUS_ERROR_ADDRESS;
dfu_context.error = MBOOT_ERROR_STR_OVERWRITE_BOOTLOADER_IDX;
dfu_context.error = (sector == 0) ? MBOOT_ERROR_STR_OVERWRITE_BOOTLOADER_IDX
: MBOOT_ERROR_STR_INVALID_ADDRESS_IDX;
return -1;
}