Skip to content

Commit 4ab3c8b

Browse files
author
Seppo Takalo
committed
IOTSTOR-953: Fix address calculations from SlicingBlockDevice
* Change MBED_ASSERTS() to return valid error code, so that checks are not bypassed on release builds. * Fix starting address calculations so that "addr" parameter is always relative to SlicingDevice and "_start" is only added when calls to underlying storage block is made. * Bypass BlockDevice:is_valid_*() to underlying block device. Slicingblockdevice was just verifying addresses independently, without verifying those from underlying block storage.
1 parent 333b281 commit 4ab3c8b

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

features/storage/blockdevice/SlicingBlockDevice.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "SlicingBlockDevice.h"
1818
#include "platform/mbed_assert.h"
1919
#include "stddef.h"
20+
#include <stdio.h>
2021

2122
namespace mbed {
2223

@@ -58,7 +59,9 @@ int SlicingBlockDevice::init()
5859
}
5960

6061
// Check that block addresses are valid
61-
MBED_ASSERT(is_valid_erase(_start, _stop - _start));
62+
if (!is_valid_erase(0, _stop - _start)) {
63+
return BD_ERROR_DEVICE_ERROR;
64+
}
6265

6366
return 0;
6467
}
@@ -75,44 +78,41 @@ int SlicingBlockDevice::sync()
7578

7679
int SlicingBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size)
7780
{
78-
MBED_ASSERT(is_valid_read(addr + _start, size));
81+
if (!is_valid_read(addr, size)) {
82+
return BD_ERROR_DEVICE_ERROR;
83+
}
7984
return _bd->read(b, addr + _start, size);
8085
}
8186

8287
int SlicingBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size)
8388
{
84-
MBED_ASSERT(is_valid_program(addr + _start, size));
89+
if (!is_valid_program(addr, size)) {
90+
return BD_ERROR_DEVICE_ERROR;
91+
}
8592
return _bd->program(b, addr + _start, size);
8693
}
8794

8895
int SlicingBlockDevice::erase(bd_addr_t addr, bd_size_t size)
8996
{
90-
MBED_ASSERT(is_valid_erase(addr + _start, size));
97+
if (!is_valid_erase(addr, size)) {
98+
return BD_ERROR_DEVICE_ERROR;
99+
}
91100
return _bd->erase(addr + _start, size);
92101
}
93102

94103
bool SlicingBlockDevice::is_valid_read(bd_addr_t addr, bd_size_t size) const
95104
{
96-
return (
97-
addr % get_read_size() == 0 &&
98-
size % get_read_size() == 0 &&
99-
addr + size <= (this->size() + _start));
105+
return _bd->is_valid_read(_start + addr, size) && _start + addr + size <= _stop;
100106
}
101107

102108
bool SlicingBlockDevice::is_valid_program(bd_addr_t addr, bd_size_t size) const
103109
{
104-
return (
105-
addr % get_program_size() == 0 &&
106-
size % get_program_size() == 0 &&
107-
addr + size <= (this->size() + _start));
110+
return _bd->is_valid_program(_start + addr, size) && _start + addr + size <= _stop;
108111
}
109112

110113
bool SlicingBlockDevice::is_valid_erase(bd_addr_t addr, bd_size_t size) const
111114
{
112-
return (
113-
addr % get_erase_size(addr) == 0 &&
114-
(addr + size) % get_erase_size(addr + size - 1) == 0 &&
115-
addr + size <= (this->size() + _start));
115+
return _bd->is_valid_erase(_start + addr, size) && _start + addr + size <= _stop;
116116
}
117117

118118
bd_size_t SlicingBlockDevice::get_read_size() const
@@ -127,7 +127,7 @@ bd_size_t SlicingBlockDevice::get_program_size() const
127127

128128
bd_size_t SlicingBlockDevice::get_erase_size() const
129129
{
130-
return _bd->get_erase_size();
130+
return _bd->get_erase_size(_start);
131131
}
132132

133133
bd_size_t SlicingBlockDevice::get_erase_size(bd_addr_t addr) const

0 commit comments

Comments
 (0)