diff --git a/storage/blockdevice/COMPONENT_OSPIF/source/OSPIFBlockDevice.cpp b/storage/blockdevice/COMPONENT_OSPIF/source/OSPIFBlockDevice.cpp index 8952927557..dc482ba497 100644 --- a/storage/blockdevice/COMPONENT_OSPIF/source/OSPIFBlockDevice.cpp +++ b/storage/blockdevice/COMPONENT_OSPIF/source/OSPIFBlockDevice.cpp @@ -460,8 +460,6 @@ exit_point: int OSPIFBlockDevice::erase(bd_addr_t addr, bd_size_t in_size) { int type = 0; - uint32_t offset = 0; - uint32_t chunk = 4096; ospi_inst_t cur_erase_inst = OSPI_NO_INST; int size = (int)in_size; bool erase_failed = false; @@ -500,11 +498,16 @@ int OSPIFBlockDevice::erase(bd_addr_t addr, bd_size_t in_size) cur_erase_inst = _sfdp_info.bptbl.legacy_erase_instruction; eu_size = OSPIF_DEFAULT_SE_SIZE; } - offset = addr % eu_size; - chunk = ((offset + size) < eu_size) ? size : (eu_size - offset); - tr_debug("Erase - addr: %llu, size:%d, Inst: 0x%xh, chunk: %lu ", - addr, size, cur_erase_inst, chunk); + if (addr % eu_size != 0 || addr + size < eu_size) { + // Should not happen if the erase table parsing + // and alignment checks were performed correctly + tr_error("internal error: address %llu not aligned to erase size %llu (type %d)", + addr, eu_size, type); + } + + tr_debug("Erase - addr: %llu, size:%d, Inst: 0x%xh, erase size: %lu ", + addr, size, cur_erase_inst, eu_size); tr_debug("Erase - Region: %d, Type:%d ", region, type); @@ -524,8 +527,8 @@ int OSPIFBlockDevice::erase(bd_addr_t addr, bd_size_t in_size) goto exit_point; } - addr += chunk; - size -= chunk; + addr += eu_size; + size -= eu_size; if ((size > 0) && (addr > _sfdp_info.smptbl.region_high_boundary[region])) { // erase crossed to next region diff --git a/storage/blockdevice/COMPONENT_QSPIF/source/QSPIFBlockDevice.cpp b/storage/blockdevice/COMPONENT_QSPIF/source/QSPIFBlockDevice.cpp index e3c962f38b..07c90ae315 100644 --- a/storage/blockdevice/COMPONENT_QSPIF/source/QSPIFBlockDevice.cpp +++ b/storage/blockdevice/COMPONENT_QSPIF/source/QSPIFBlockDevice.cpp @@ -388,8 +388,6 @@ exit_point: int QSPIFBlockDevice::erase(bd_addr_t addr, bd_size_t in_size) { int type = 0; - uint32_t offset = 0; - uint32_t chunk = 4096; qspi_inst_t cur_erase_inst = QSPI_NO_INST; int size = (int)in_size; bool erase_failed = false; @@ -427,11 +425,16 @@ int QSPIFBlockDevice::erase(bd_addr_t addr, bd_size_t in_size) cur_erase_inst = _sfdp_info.bptbl.legacy_erase_instruction; eu_size = QSPIF_DEFAULT_SE_SIZE; } - offset = addr % eu_size; - chunk = ((offset + size) < eu_size) ? size : (eu_size - offset); - tr_debug("Erase - addr: %llu, size:%d, Inst: 0x%xh, chunk: %lu ", - addr, size, cur_erase_inst, chunk); + if (addr % eu_size != 0 || addr + size < eu_size) { + // Should not happen if the erase table parsing + // and alignment checks were performed correctly + tr_error("internal error: address %llu not aligned to erase size %llu (type %d)", + addr, eu_size, type); + } + + tr_debug("Erase - addr: %llu, size:%d, Inst: 0x%xh, erase size: %lu ", + addr, size, cur_erase_inst, eu_size); tr_debug("Erase - Region: %d, Type:%d ", region, type); @@ -451,8 +454,8 @@ int QSPIFBlockDevice::erase(bd_addr_t addr, bd_size_t in_size) goto exit_point; } - addr += chunk; - size -= chunk; + addr += eu_size; + size -= eu_size; if ((size > 0) && (addr > _sfdp_info.smptbl.region_high_boundary[region])) { // erase crossed to next region diff --git a/storage/blockdevice/COMPONENT_SPIF/source/SPIFBlockDevice.cpp b/storage/blockdevice/COMPONENT_SPIF/source/SPIFBlockDevice.cpp index 870cb161b6..cdc27926c8 100644 --- a/storage/blockdevice/COMPONENT_SPIF/source/SPIFBlockDevice.cpp +++ b/storage/blockdevice/COMPONENT_SPIF/source/SPIFBlockDevice.cpp @@ -305,9 +305,8 @@ int SPIFBlockDevice::erase(bd_addr_t addr, bd_size_t in_size) } int type = 0; - uint32_t offset = 0; - uint32_t chunk = 4096; int cur_erase_inst = _erase_instruction; + unsigned int curr_erase_size = 0; int size = (int)in_size; bool erase_failed = false; int status = SPIF_BD_ERROR_OK; @@ -339,12 +338,16 @@ int SPIFBlockDevice::erase(bd_addr_t addr, bd_size_t in_size) // find the matching instruction and erase size chunk for that type. type = sfdp_iterate_next_largest_erase_type(bitfield, size, (unsigned int)addr, region, _sfdp_info.smptbl); cur_erase_inst = _sfdp_info.smptbl.erase_type_inst_arr[type]; - offset = addr % _sfdp_info.smptbl.erase_type_size_arr[type]; - chunk = ((offset + size) < _sfdp_info.smptbl.erase_type_size_arr[type]) ? - size : (_sfdp_info.smptbl.erase_type_size_arr[type] - offset); + curr_erase_size = _sfdp_info.smptbl.erase_type_size_arr[type]; + if (addr % curr_erase_size != 0 || addr + size < curr_erase_size) { + // Should not happen if the erase table parsing + // and alignment checks were performed correctly + tr_error("internal error: address %llu not aligned to erase size %llu (type %d)", + addr, curr_erase_size, type); + } - tr_debug("erase - addr: %llu, size:%d, Inst: 0x%xh, chunk: %" PRIu32 " , ", - addr, size, cur_erase_inst, chunk); + tr_debug("erase - addr: %llu, size:%d, Inst: 0x%xh, erase size: %" PRIu32 " , ", + addr, size, cur_erase_inst, curr_erase_size); tr_debug("erase - Region: %d, Type:%d", region, type); @@ -359,8 +362,8 @@ int SPIFBlockDevice::erase(bd_addr_t addr, bd_size_t in_size) _spi_send_erase_command(cur_erase_inst, addr, size); - addr += chunk; - size -= chunk; + addr += curr_erase_size; + size -= curr_erase_size; if ((size > 0) && (addr > _sfdp_info.smptbl.region_high_boundary[region])) { // erase crossed to next region