Import Mbed OS hard-float snapshot
This commit is contained in:
355
storage/blockdevice/source/BufferedBlockDevice.cpp
Normal file
355
storage/blockdevice/source/BufferedBlockDevice.cpp
Normal file
@@ -0,0 +1,355 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2018 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "blockdevice/BufferedBlockDevice.h"
|
||||
#include "platform/mbed_assert.h"
|
||||
#include "platform/mbed_atomic.h"
|
||||
#include <algorithm>
|
||||
#include <string.h>
|
||||
|
||||
namespace mbed {
|
||||
|
||||
static inline uint32_t align_down(bd_size_t val, bd_size_t size)
|
||||
{
|
||||
return val / size * size;
|
||||
}
|
||||
|
||||
BufferedBlockDevice::BufferedBlockDevice(BlockDevice *bd)
|
||||
: _bd(bd), _bd_program_size(0), _bd_read_size(0), _bd_size(0), _write_cache_addr(0), _write_cache_valid(false),
|
||||
_write_cache(0), _read_buf(0), _init_ref_count(0), _is_initialized(false)
|
||||
{
|
||||
MBED_ASSERT(_bd);
|
||||
}
|
||||
|
||||
BufferedBlockDevice::~BufferedBlockDevice()
|
||||
{
|
||||
deinit();
|
||||
}
|
||||
|
||||
int BufferedBlockDevice::init()
|
||||
{
|
||||
uint32_t val = core_util_atomic_incr_u32(&_init_ref_count, 1);
|
||||
|
||||
if (val != 1) {
|
||||
return BD_ERROR_OK;
|
||||
}
|
||||
|
||||
int err = _bd->init();
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
_bd_read_size = _bd->get_read_size();
|
||||
_bd_program_size = _bd->get_program_size();
|
||||
_bd_size = _bd->size();
|
||||
|
||||
if (!_write_cache) {
|
||||
_write_cache = new uint8_t[_bd_program_size];
|
||||
}
|
||||
|
||||
if (!_read_buf) {
|
||||
_read_buf = new uint8_t[_bd_read_size];
|
||||
}
|
||||
|
||||
invalidate_write_cache();
|
||||
|
||||
_is_initialized = true;
|
||||
return BD_ERROR_OK;
|
||||
}
|
||||
|
||||
int BufferedBlockDevice::deinit()
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_OK;
|
||||
}
|
||||
|
||||
// Flush out all data from buffers
|
||||
int err = sync();
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
uint32_t val = core_util_atomic_decr_u32(&_init_ref_count, 1);
|
||||
|
||||
if (val) {
|
||||
return BD_ERROR_OK;
|
||||
}
|
||||
|
||||
delete[] _write_cache;
|
||||
_write_cache = 0;
|
||||
delete[] _read_buf;
|
||||
_read_buf = 0;
|
||||
_is_initialized = false;
|
||||
return _bd->deinit();
|
||||
}
|
||||
|
||||
int BufferedBlockDevice::flush()
|
||||
{
|
||||
MBED_ASSERT(_write_cache);
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
if (_write_cache_valid) {
|
||||
int ret = _bd->program(_write_cache, _write_cache_addr, _bd_program_size);
|
||||
if (ret) {
|
||||
return ret;
|
||||
}
|
||||
invalidate_write_cache();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void BufferedBlockDevice::invalidate_write_cache()
|
||||
{
|
||||
_write_cache_addr = _bd_size;
|
||||
_write_cache_valid = false;
|
||||
}
|
||||
|
||||
int BufferedBlockDevice::sync()
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
MBED_ASSERT(_write_cache);
|
||||
int ret = flush();
|
||||
if (ret) {
|
||||
return ret;
|
||||
}
|
||||
return _bd->sync();
|
||||
}
|
||||
|
||||
int BufferedBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
MBED_ASSERT(_write_cache && _read_buf);
|
||||
|
||||
if (!is_valid_read(addr, size)) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
// Common case - no need to involve write cache or read buffer
|
||||
if (_bd->is_valid_read(addr, size) &&
|
||||
((addr + size <= _write_cache_addr) || (addr > _write_cache_addr + _bd_program_size))) {
|
||||
return _bd->read(b, addr, size);
|
||||
}
|
||||
|
||||
uint8_t *buf = static_cast<uint8_t *>(b);
|
||||
|
||||
// Read logic: Split read to chunks, according to whether we cross the write cache
|
||||
while (size) {
|
||||
bd_size_t chunk;
|
||||
bool read_from_bd = true;
|
||||
if (_write_cache_valid && addr < _write_cache_addr) {
|
||||
chunk = std::min(size, _write_cache_addr - addr);
|
||||
} else if (_write_cache_valid && (addr >= _write_cache_addr) && (addr < _write_cache_addr + _bd_program_size)) {
|
||||
// One case we need to take our data from cache
|
||||
chunk = std::min(size, _bd_program_size - addr % _bd_program_size);
|
||||
memcpy(buf, _write_cache + addr % _bd_program_size, chunk);
|
||||
read_from_bd = false;
|
||||
} else {
|
||||
chunk = size;
|
||||
}
|
||||
|
||||
// Now, in case we read from the BD, make sure we are aligned with its read size.
|
||||
// If not, use read buffer as a helper.
|
||||
if (read_from_bd) {
|
||||
bd_size_t offs_in_read_buf = addr % _bd_read_size;
|
||||
int ret;
|
||||
if (offs_in_read_buf || (chunk < _bd_read_size)) {
|
||||
chunk = std::min(chunk, _bd_read_size - offs_in_read_buf);
|
||||
ret = _bd->read(_read_buf, addr - offs_in_read_buf, _bd_read_size);
|
||||
memcpy(buf, _read_buf + offs_in_read_buf, chunk);
|
||||
} else {
|
||||
chunk = align_down(chunk, _bd_read_size);
|
||||
ret = _bd->read(buf, addr, chunk);
|
||||
}
|
||||
if (ret) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
buf += chunk;
|
||||
addr += chunk;
|
||||
size -= chunk;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int BufferedBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
MBED_ASSERT(_write_cache);
|
||||
|
||||
int ret;
|
||||
|
||||
bd_addr_t aligned_addr = align_down(addr, _bd_program_size);
|
||||
|
||||
const uint8_t *buf = static_cast <const uint8_t *>(b);
|
||||
|
||||
// Need to flush if moved to another program unit
|
||||
if (aligned_addr != _write_cache_addr) {
|
||||
ret = flush();
|
||||
if (ret) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
// Write logic: Keep data in cache as long as we don't reach the end of the program unit.
|
||||
// Otherwise, program to the underlying BD.
|
||||
while (size) {
|
||||
_write_cache_addr = align_down(addr, _bd_program_size);
|
||||
bd_addr_t offs_in_buf = addr - _write_cache_addr;
|
||||
bd_size_t chunk;
|
||||
if (offs_in_buf) {
|
||||
chunk = std::min(_bd_program_size - offs_in_buf, size);
|
||||
} else if (size >= _bd_program_size) {
|
||||
chunk = align_down(size, _bd_program_size);
|
||||
} else {
|
||||
chunk = size;
|
||||
}
|
||||
|
||||
const uint8_t *prog_buf;
|
||||
if (chunk < _bd_program_size) {
|
||||
// If cache not valid, and program doesn't cover an entire unit, it means we need to
|
||||
// read it from the underlying BD
|
||||
if (!_write_cache_valid) {
|
||||
ret = _bd->read(_write_cache, _write_cache_addr, _bd_program_size);
|
||||
if (ret) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
memcpy(_write_cache + offs_in_buf, buf, chunk);
|
||||
prog_buf = _write_cache;
|
||||
} else {
|
||||
prog_buf = buf;
|
||||
}
|
||||
|
||||
// Only program if we reached the end of a program unit
|
||||
if (!((offs_in_buf + chunk) % _bd_program_size)) {
|
||||
ret = _bd->program(prog_buf, _write_cache_addr, std::max(chunk, _bd_program_size));
|
||||
if (ret) {
|
||||
return ret;
|
||||
}
|
||||
invalidate_write_cache();
|
||||
ret = _bd->sync();
|
||||
if (ret) {
|
||||
return ret;
|
||||
}
|
||||
} else {
|
||||
_write_cache_valid = true;
|
||||
}
|
||||
|
||||
buf += chunk;
|
||||
addr += chunk;
|
||||
size -= chunk;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int BufferedBlockDevice::erase(bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
if (!is_valid_erase(addr, size)) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
if ((_write_cache_addr >= addr) && (_write_cache_addr <= addr + size)) {
|
||||
invalidate_write_cache();
|
||||
}
|
||||
return _bd->erase(addr, size);
|
||||
}
|
||||
|
||||
int BufferedBlockDevice::trim(bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
if (!is_valid_erase(addr, size)) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
if ((_write_cache_addr >= addr) && (_write_cache_addr <= addr + size)) {
|
||||
invalidate_write_cache();
|
||||
}
|
||||
return _bd->trim(addr, size);
|
||||
}
|
||||
|
||||
bd_size_t BufferedBlockDevice::get_read_size() const
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
bd_size_t BufferedBlockDevice::get_program_size() const
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
bd_size_t BufferedBlockDevice::get_erase_size() const
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _bd->get_erase_size();
|
||||
}
|
||||
|
||||
bd_size_t BufferedBlockDevice::get_erase_size(bd_addr_t addr) const
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _bd->get_erase_size(addr);
|
||||
}
|
||||
|
||||
int BufferedBlockDevice::get_erase_value() const
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
return _bd->get_erase_value();
|
||||
}
|
||||
|
||||
bd_size_t BufferedBlockDevice::size() const
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _bd_size;
|
||||
}
|
||||
|
||||
const char *BufferedBlockDevice::get_type() const
|
||||
{
|
||||
return _bd->get_type();
|
||||
}
|
||||
|
||||
} // namespace mbed
|
||||
300
storage/blockdevice/source/ChainingBlockDevice.cpp
Normal file
300
storage/blockdevice/source/ChainingBlockDevice.cpp
Normal file
@@ -0,0 +1,300 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2017 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "blockdevice/ChainingBlockDevice.h"
|
||||
#include "platform/mbed_atomic.h"
|
||||
#include "platform/mbed_assert.h"
|
||||
|
||||
namespace mbed {
|
||||
|
||||
ChainingBlockDevice::ChainingBlockDevice(BlockDevice **bds, size_t bd_count)
|
||||
: _bds(bds), _bd_count(bd_count)
|
||||
, _read_size(0), _program_size(0), _erase_size(0), _size(0)
|
||||
, _erase_value(-1), _init_ref_count(0), _is_initialized(false)
|
||||
{
|
||||
}
|
||||
|
||||
static bool is_aligned(uint64_t x, uint64_t alignment)
|
||||
{
|
||||
return (x / alignment) * alignment == x;
|
||||
}
|
||||
|
||||
int ChainingBlockDevice::init()
|
||||
{
|
||||
int err;
|
||||
uint32_t val = core_util_atomic_incr_u32(&_init_ref_count, 1);
|
||||
|
||||
if (val != 1) {
|
||||
return BD_ERROR_OK;
|
||||
}
|
||||
|
||||
_read_size = 0;
|
||||
_program_size = 0;
|
||||
_erase_size = 0;
|
||||
_erase_value = -1;
|
||||
_size = 0;
|
||||
|
||||
// Initialize children block devices, find all sizes and
|
||||
// assert that block sizes are similar. We can't do this in
|
||||
// the constructor since some block devices may need to be
|
||||
// initialized before they know their block size/count
|
||||
for (size_t i = 0; i < _bd_count; i++) {
|
||||
err = _bds[i]->init();
|
||||
if (err) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
bd_size_t read = _bds[i]->get_read_size();
|
||||
if (i == 0 || (read >= _read_size && is_aligned(read, _read_size))) {
|
||||
_read_size = read;
|
||||
} else {
|
||||
MBED_ASSERT(_read_size > read && is_aligned(_read_size, read));
|
||||
}
|
||||
|
||||
bd_size_t program = _bds[i]->get_program_size();
|
||||
if (i == 0 || (program >= _program_size && is_aligned(program, _program_size))) {
|
||||
_program_size = program;
|
||||
} else {
|
||||
MBED_ASSERT(_program_size > program && is_aligned(_program_size, program));
|
||||
}
|
||||
|
||||
bd_size_t erase = _bds[i]->get_erase_size();
|
||||
if (i == 0 || (erase >= _erase_size && is_aligned(erase, _erase_size))) {
|
||||
_erase_size = erase;
|
||||
} else {
|
||||
MBED_ASSERT(_erase_size > erase && is_aligned(_erase_size, erase));
|
||||
}
|
||||
|
||||
int value = _bds[i]->get_erase_value();
|
||||
if (i == 0 || value == _erase_value) {
|
||||
_erase_value = value;
|
||||
} else {
|
||||
_erase_value = -1;
|
||||
}
|
||||
|
||||
_size += _bds[i]->size();
|
||||
}
|
||||
|
||||
_is_initialized = true;
|
||||
return BD_ERROR_OK;
|
||||
|
||||
fail:
|
||||
_is_initialized = false;
|
||||
_init_ref_count = 0;
|
||||
return err;
|
||||
}
|
||||
|
||||
int ChainingBlockDevice::deinit()
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_OK;
|
||||
}
|
||||
|
||||
uint32_t val = core_util_atomic_decr_u32(&_init_ref_count, 1);
|
||||
|
||||
if (val) {
|
||||
return BD_ERROR_OK;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < _bd_count; i++) {
|
||||
int err = _bds[i]->deinit();
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
_is_initialized = false;
|
||||
return BD_ERROR_OK;
|
||||
}
|
||||
|
||||
int ChainingBlockDevice::sync()
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < _bd_count; i++) {
|
||||
int err = _bds[i]->sync();
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ChainingBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
if (!is_valid_read(addr, size)) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
uint8_t *buffer = static_cast<uint8_t *>(b);
|
||||
|
||||
// Find block devices containing blocks, may span multiple block devices
|
||||
for (size_t i = 0; i < _bd_count && size > 0; i++) {
|
||||
bd_size_t bdsize = _bds[i]->size();
|
||||
|
||||
if (addr < bdsize) {
|
||||
bd_size_t read = size;
|
||||
if (addr + read > bdsize) {
|
||||
read = bdsize - addr;
|
||||
}
|
||||
|
||||
int err = _bds[i]->read(buffer, addr, read);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
buffer += read;
|
||||
addr += read;
|
||||
size -= read;
|
||||
}
|
||||
|
||||
addr -= bdsize;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ChainingBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
if (!is_valid_program(addr, size)) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
const uint8_t *buffer = static_cast<const uint8_t *>(b);
|
||||
|
||||
// Find block devices containing blocks, may span multiple block devices
|
||||
for (size_t i = 0; i < _bd_count && size > 0; i++) {
|
||||
bd_size_t bdsize = _bds[i]->size();
|
||||
|
||||
if (addr < bdsize) {
|
||||
bd_size_t program = size;
|
||||
if (addr + program > bdsize) {
|
||||
program = bdsize - addr;
|
||||
}
|
||||
|
||||
int err = _bds[i]->program(buffer, addr, program);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
buffer += program;
|
||||
addr += program;
|
||||
size -= program;
|
||||
}
|
||||
|
||||
addr -= bdsize;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ChainingBlockDevice::erase(bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
if (!is_valid_erase(addr, size)) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
// Find block devices containing blocks, may span multiple block devices
|
||||
for (size_t i = 0; i < _bd_count && size > 0; i++) {
|
||||
bd_size_t bdsize = _bds[i]->size();
|
||||
|
||||
if (addr < bdsize) {
|
||||
bd_size_t erase = size;
|
||||
if (addr + erase > bdsize) {
|
||||
erase = bdsize - addr;
|
||||
}
|
||||
|
||||
int err = _bds[i]->erase(addr, erase);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
addr += erase;
|
||||
size -= erase;
|
||||
}
|
||||
|
||||
addr -= bdsize;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bd_size_t ChainingBlockDevice::get_read_size() const
|
||||
{
|
||||
return _read_size;
|
||||
}
|
||||
|
||||
bd_size_t ChainingBlockDevice::get_program_size() const
|
||||
{
|
||||
return _program_size;
|
||||
}
|
||||
|
||||
bd_size_t ChainingBlockDevice::get_erase_size() const
|
||||
{
|
||||
return _erase_size;
|
||||
}
|
||||
|
||||
bd_size_t ChainingBlockDevice::get_erase_size(bd_addr_t addr) const
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
bd_addr_t bd_start_addr = 0;
|
||||
for (size_t i = 0; i < _bd_count; i++) {
|
||||
bd_size_t bdsize = _bds[i]->size();
|
||||
if (addr < (bd_start_addr + bdsize)) {
|
||||
return _bds[i]->get_erase_size(addr - bd_start_addr);
|
||||
}
|
||||
bd_start_addr += bdsize;
|
||||
}
|
||||
|
||||
// Getting here implies an illegal address
|
||||
MBED_ASSERT(0);
|
||||
return 0; // satisfy compiler
|
||||
}
|
||||
|
||||
int ChainingBlockDevice::get_erase_value() const
|
||||
{
|
||||
return _erase_value;
|
||||
}
|
||||
|
||||
bd_size_t ChainingBlockDevice::size() const
|
||||
{
|
||||
return _size;
|
||||
}
|
||||
|
||||
const char *ChainingBlockDevice::get_type() const
|
||||
{
|
||||
return "CHAINING";
|
||||
}
|
||||
|
||||
} // namespace mbed
|
||||
211
storage/blockdevice/source/ExhaustibleBlockDevice.cpp
Normal file
211
storage/blockdevice/source/ExhaustibleBlockDevice.cpp
Normal file
@@ -0,0 +1,211 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2017 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "blockdevice/ExhaustibleBlockDevice.h"
|
||||
#include "platform/mbed_atomic.h"
|
||||
#include "platform/mbed_assert.h"
|
||||
|
||||
namespace mbed {
|
||||
|
||||
ExhaustibleBlockDevice::ExhaustibleBlockDevice(BlockDevice *bd, uint32_t erase_cycles)
|
||||
: _bd(bd), _erase_array(NULL), _erase_cycles(erase_cycles), _init_ref_count(0), _is_initialized(false)
|
||||
{
|
||||
}
|
||||
|
||||
ExhaustibleBlockDevice::~ExhaustibleBlockDevice()
|
||||
{
|
||||
delete[] _erase_array;
|
||||
}
|
||||
|
||||
int ExhaustibleBlockDevice::init()
|
||||
{
|
||||
int err;
|
||||
uint32_t val = core_util_atomic_incr_u32(&_init_ref_count, 1);
|
||||
|
||||
if (val != 1) {
|
||||
return BD_ERROR_OK;
|
||||
}
|
||||
|
||||
err = _bd->init();
|
||||
if (err) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (!_erase_array) {
|
||||
// can only be allocated after initialization
|
||||
_erase_array = new uint32_t[_bd->size() / _bd->get_erase_size()];
|
||||
for (size_t i = 0; i < _bd->size() / _bd->get_erase_size(); i++) {
|
||||
_erase_array[i] = _erase_cycles;
|
||||
}
|
||||
}
|
||||
|
||||
_is_initialized = true;
|
||||
return BD_ERROR_OK;
|
||||
|
||||
fail:
|
||||
_is_initialized = false;
|
||||
_init_ref_count = 0;
|
||||
return err;
|
||||
}
|
||||
|
||||
int ExhaustibleBlockDevice::deinit()
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_OK;
|
||||
}
|
||||
|
||||
core_util_atomic_decr_u32(&_init_ref_count, 1);
|
||||
|
||||
if (_init_ref_count) {
|
||||
return BD_ERROR_OK;
|
||||
}
|
||||
|
||||
// _erase_array is lazily cleaned up in destructor to allow
|
||||
// data to live across de/reinitialization
|
||||
_is_initialized = false;
|
||||
return _bd->deinit();
|
||||
}
|
||||
|
||||
int ExhaustibleBlockDevice::sync()
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
return _bd->sync();
|
||||
}
|
||||
|
||||
int ExhaustibleBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
return _bd->read(buffer, addr, size);
|
||||
}
|
||||
|
||||
int ExhaustibleBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
if (!is_valid_program(addr, size)) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
if (_erase_array[addr / get_erase_size()] == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _bd->program(buffer, addr, size);
|
||||
}
|
||||
|
||||
int ExhaustibleBlockDevice::erase(bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
if (!is_valid_erase(addr, size)) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
bd_size_t eu_size = get_erase_size();
|
||||
while (size) {
|
||||
// use an erase cycle
|
||||
if (_erase_array[addr / eu_size] > 0) {
|
||||
_erase_array[addr / eu_size] -= 1;
|
||||
}
|
||||
|
||||
if (_erase_array[addr / eu_size] > 0) {
|
||||
int err = _bd->erase(addr, eu_size);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
addr += eu_size;
|
||||
size -= eu_size;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bd_size_t ExhaustibleBlockDevice::get_read_size() const
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _bd->get_read_size();
|
||||
}
|
||||
|
||||
bd_size_t ExhaustibleBlockDevice::get_program_size() const
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _bd->get_program_size();
|
||||
}
|
||||
|
||||
bd_size_t ExhaustibleBlockDevice::get_erase_size() const
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _bd->get_erase_size();
|
||||
}
|
||||
|
||||
bd_size_t ExhaustibleBlockDevice::get_erase_size(bd_addr_t addr) const
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _bd->get_erase_size(addr);
|
||||
}
|
||||
|
||||
int ExhaustibleBlockDevice::get_erase_value() const
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
return _bd->get_erase_value();
|
||||
}
|
||||
|
||||
bd_size_t ExhaustibleBlockDevice::size() const
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _bd->size();
|
||||
}
|
||||
|
||||
const char *ExhaustibleBlockDevice::get_type() const
|
||||
{
|
||||
if (_bd != NULL) {
|
||||
return _bd->get_type();
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
} // namespace mbed
|
||||
232
storage/blockdevice/source/FlashSimBlockDevice.cpp
Normal file
232
storage/blockdevice/source/FlashSimBlockDevice.cpp
Normal file
@@ -0,0 +1,232 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2018 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "blockdevice/FlashSimBlockDevice.h"
|
||||
#include "platform/mbed_assert.h"
|
||||
#include "platform/mbed_atomic.h"
|
||||
#include <algorithm>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "mbed_assert.h"
|
||||
|
||||
namespace mbed {
|
||||
|
||||
static const bd_size_t min_blank_buf_size = 32;
|
||||
|
||||
static inline uint32_t align_up(bd_size_t val, bd_size_t size)
|
||||
{
|
||||
return (((val - 1) / size) + 1) * size;
|
||||
}
|
||||
|
||||
FlashSimBlockDevice::FlashSimBlockDevice(BlockDevice *bd, uint8_t erase_value) :
|
||||
_erase_value(erase_value), _blank_buf_size(0),
|
||||
_blank_buf(0), _bd(bd), _init_ref_count(0), _is_initialized(false)
|
||||
{
|
||||
MBED_ASSERT(bd);
|
||||
}
|
||||
|
||||
FlashSimBlockDevice::~FlashSimBlockDevice()
|
||||
{
|
||||
deinit();
|
||||
delete[] _blank_buf;
|
||||
}
|
||||
|
||||
int FlashSimBlockDevice::init()
|
||||
{
|
||||
int err;
|
||||
uint32_t val = core_util_atomic_incr_u32(&_init_ref_count, 1);
|
||||
|
||||
if (val != 1) {
|
||||
return BD_ERROR_OK;
|
||||
}
|
||||
|
||||
err = _bd->init();
|
||||
if (err) {
|
||||
goto fail;
|
||||
}
|
||||
_blank_buf_size = align_up(min_blank_buf_size, _bd->get_program_size());
|
||||
if (!_blank_buf) {
|
||||
_blank_buf = new uint8_t[_blank_buf_size];
|
||||
memset(_blank_buf, 0, _blank_buf_size);
|
||||
MBED_ASSERT(_blank_buf);
|
||||
}
|
||||
|
||||
_is_initialized = true;
|
||||
return BD_ERROR_OK;
|
||||
|
||||
fail:
|
||||
_is_initialized = false;
|
||||
_init_ref_count = 0;
|
||||
return err;
|
||||
}
|
||||
|
||||
int FlashSimBlockDevice::deinit()
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_OK;
|
||||
}
|
||||
|
||||
uint32_t val = core_util_atomic_decr_u32(&_init_ref_count, 1);
|
||||
|
||||
if (val) {
|
||||
return BD_ERROR_OK;
|
||||
}
|
||||
|
||||
_is_initialized = false;
|
||||
return _bd->deinit();
|
||||
}
|
||||
|
||||
int FlashSimBlockDevice::sync()
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
return _bd->sync();
|
||||
}
|
||||
|
||||
bd_size_t FlashSimBlockDevice::get_read_size() const
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _bd->get_read_size();
|
||||
}
|
||||
|
||||
bd_size_t FlashSimBlockDevice::get_program_size() const
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _bd->get_program_size();
|
||||
}
|
||||
|
||||
bd_size_t FlashSimBlockDevice::get_erase_size() const
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _bd->get_erase_size();
|
||||
}
|
||||
|
||||
bd_size_t FlashSimBlockDevice::get_erase_size(bd_addr_t addr) const
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _bd->get_erase_size(addr);
|
||||
}
|
||||
|
||||
bd_size_t FlashSimBlockDevice::size() const
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _bd->size();
|
||||
}
|
||||
|
||||
int FlashSimBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
return _bd->read(b, addr, size);
|
||||
}
|
||||
|
||||
int FlashSimBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
if (!is_valid_program(addr, size)) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
bd_addr_t curr_addr = addr;
|
||||
bd_size_t curr_size = size;
|
||||
|
||||
const uint8_t *buf = (const uint8_t *) b;
|
||||
while (curr_size) {
|
||||
bd_size_t read_size = std::min(_blank_buf_size, curr_size);
|
||||
int ret = _bd->read(_blank_buf, curr_addr, read_size);
|
||||
if (ret) {
|
||||
return ret;
|
||||
}
|
||||
for (bd_size_t i = 0; i < read_size; i++) {
|
||||
// Allow either programming on blanks or programming the same value
|
||||
// (as real flash devices do)
|
||||
if ((_blank_buf[i] != _erase_value) && (_blank_buf[i] != *buf)) {
|
||||
return BD_ERROR_NOT_ERASED;
|
||||
}
|
||||
buf++;
|
||||
}
|
||||
curr_addr += read_size;
|
||||
curr_size -= read_size;
|
||||
}
|
||||
|
||||
return _bd->program(b, addr, size);
|
||||
}
|
||||
|
||||
int FlashSimBlockDevice::erase(bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
if (!is_valid_erase(addr, size)) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
bd_addr_t curr_addr = addr;
|
||||
bd_size_t curr_size = size;
|
||||
|
||||
memset(_blank_buf, _erase_value, (unsigned int) _blank_buf_size);
|
||||
|
||||
while (curr_size) {
|
||||
bd_size_t prog_size = std::min(_blank_buf_size, curr_size);
|
||||
int ret = _bd->program(_blank_buf, curr_addr, prog_size);
|
||||
if (ret) {
|
||||
return ret;
|
||||
}
|
||||
curr_addr += prog_size;
|
||||
curr_size -= prog_size;
|
||||
}
|
||||
|
||||
return BD_ERROR_OK;
|
||||
}
|
||||
|
||||
int FlashSimBlockDevice::get_erase_value() const
|
||||
{
|
||||
return _erase_value;
|
||||
}
|
||||
|
||||
const char *FlashSimBlockDevice::get_type() const
|
||||
{
|
||||
if (_bd != NULL) {
|
||||
return _bd->get_type();
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
} // namespace mbed
|
||||
190
storage/blockdevice/source/HeapBlockDevice.cpp
Normal file
190
storage/blockdevice/source/HeapBlockDevice.cpp
Normal file
@@ -0,0 +1,190 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2017 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "blockdevice/HeapBlockDevice.h"
|
||||
#include "platform/mbed_atomic.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace mbed {
|
||||
|
||||
HeapBlockDevice::HeapBlockDevice(bd_size_t size, bd_size_t block)
|
||||
: _read_size(block), _program_size(block), _erase_size(block)
|
||||
, _count(size / block), _blocks(0), _init_ref_count(0), _is_initialized(false)
|
||||
{
|
||||
MBED_ASSERT(_count * _erase_size == size);
|
||||
}
|
||||
|
||||
HeapBlockDevice::HeapBlockDevice(bd_size_t size, bd_size_t read, bd_size_t program, bd_size_t erase)
|
||||
: _read_size(read), _program_size(program), _erase_size(erase)
|
||||
, _count(size / erase), _blocks(0), _init_ref_count(0), _is_initialized(false)
|
||||
{
|
||||
MBED_ASSERT(_count * _erase_size == size);
|
||||
}
|
||||
|
||||
HeapBlockDevice::~HeapBlockDevice()
|
||||
{
|
||||
if (_blocks) {
|
||||
for (size_t i = 0; i < _count; i++) {
|
||||
free(_blocks[i]);
|
||||
}
|
||||
|
||||
delete[] _blocks;
|
||||
_blocks = 0;
|
||||
}
|
||||
}
|
||||
|
||||
int HeapBlockDevice::init()
|
||||
{
|
||||
uint32_t val = core_util_atomic_incr_u32(&_init_ref_count, 1);
|
||||
|
||||
if (val != 1) {
|
||||
return BD_ERROR_OK;
|
||||
}
|
||||
|
||||
if (!_blocks) {
|
||||
_blocks = new uint8_t *[_count];
|
||||
for (size_t i = 0; i < _count; i++) {
|
||||
_blocks[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
_is_initialized = true;
|
||||
return BD_ERROR_OK;
|
||||
}
|
||||
|
||||
int HeapBlockDevice::deinit()
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_OK;
|
||||
}
|
||||
|
||||
uint32_t val = core_util_atomic_decr_u32(&_init_ref_count, 1);
|
||||
|
||||
if (val) {
|
||||
return BD_ERROR_OK;
|
||||
}
|
||||
|
||||
MBED_ASSERT(_blocks != NULL);
|
||||
// Memory is lazily cleaned up in destructor to allow
|
||||
// data to live across de/reinitialization
|
||||
_is_initialized = false;
|
||||
return BD_ERROR_OK;
|
||||
}
|
||||
|
||||
bd_size_t HeapBlockDevice::get_read_size() const
|
||||
{
|
||||
return _read_size;
|
||||
}
|
||||
|
||||
bd_size_t HeapBlockDevice::get_program_size() const
|
||||
{
|
||||
return _program_size;
|
||||
}
|
||||
|
||||
bd_size_t HeapBlockDevice::get_erase_size() const
|
||||
{
|
||||
return _erase_size;
|
||||
}
|
||||
|
||||
bd_size_t HeapBlockDevice::get_erase_size(bd_addr_t addr) const
|
||||
{
|
||||
return _erase_size;
|
||||
}
|
||||
|
||||
bd_size_t HeapBlockDevice::size() const
|
||||
{
|
||||
return _count * _erase_size;
|
||||
}
|
||||
|
||||
int HeapBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
if (!is_valid_read(addr, size)) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
uint8_t *buffer = static_cast<uint8_t *>(b);
|
||||
|
||||
while (size > 0) {
|
||||
bd_addr_t hi = addr / _erase_size;
|
||||
bd_addr_t lo = addr % _erase_size;
|
||||
|
||||
if (_blocks[hi]) {
|
||||
memcpy(buffer, &_blocks[hi][lo], _read_size);
|
||||
} else {
|
||||
memset(buffer, 0, _read_size);
|
||||
}
|
||||
|
||||
buffer += _read_size;
|
||||
addr += _read_size;
|
||||
size -= _read_size;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int HeapBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
if (!is_valid_program(addr, size)) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
const uint8_t *buffer = static_cast<const uint8_t *>(b);
|
||||
|
||||
while (size > 0) {
|
||||
bd_addr_t hi = addr / _erase_size;
|
||||
bd_addr_t lo = addr % _erase_size;
|
||||
|
||||
if (!_blocks[hi]) {
|
||||
_blocks[hi] = (uint8_t *)malloc(_erase_size);
|
||||
if (!_blocks[hi]) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
memcpy(&_blocks[hi][lo], buffer, _program_size);
|
||||
|
||||
buffer += _program_size;
|
||||
addr += _program_size;
|
||||
size -= _program_size;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int HeapBlockDevice::erase(bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
if (!is_valid_erase(addr, size)) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char *HeapBlockDevice::get_type() const
|
||||
{
|
||||
return "HEAP";
|
||||
}
|
||||
|
||||
} // namespace mbed
|
||||
455
storage/blockdevice/source/MBRBlockDevice.cpp
Normal file
455
storage/blockdevice/source/MBRBlockDevice.cpp
Normal file
@@ -0,0 +1,455 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2017 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "blockdevice/MBRBlockDevice.h"
|
||||
#include "platform/mbed_atomic.h"
|
||||
#include "platform/mbed_toolchain.h"
|
||||
#include "platform/mbed_assert.h"
|
||||
#include <algorithm>
|
||||
#include <string.h>
|
||||
|
||||
namespace mbed {
|
||||
|
||||
// On disk structures, all entries are little endian
|
||||
MBED_PACKED(struct) mbr_entry {
|
||||
uint8_t status;
|
||||
uint8_t chs_start[3];
|
||||
uint8_t type;
|
||||
uint8_t chs_stop[3];
|
||||
uint32_t lba_offset;
|
||||
uint32_t lba_size;
|
||||
};
|
||||
|
||||
MBED_PACKED(struct) mbr_table {
|
||||
struct mbr_entry entries[4];
|
||||
uint8_t signature[2];
|
||||
};
|
||||
|
||||
// Little-endian conversion, should compile to noop
|
||||
// if system is little-endian
|
||||
static inline uint32_t tole32(uint32_t a)
|
||||
{
|
||||
union {
|
||||
uint32_t u32;
|
||||
uint8_t u8[4];
|
||||
} w;
|
||||
|
||||
w.u8[0] = a >> 0;
|
||||
w.u8[1] = a >> 8;
|
||||
w.u8[2] = a >> 16;
|
||||
w.u8[3] = a >> 24;
|
||||
|
||||
return w.u32;
|
||||
}
|
||||
|
||||
static inline uint32_t fromle32(uint32_t a)
|
||||
{
|
||||
return tole32(a);
|
||||
}
|
||||
|
||||
static void tochs(uint32_t lba, uint8_t chs[3])
|
||||
{
|
||||
uint32_t sector = std::min<uint32_t>(lba, 0xfffffd) + 1;
|
||||
chs[0] = (sector >> 6) & 0xff;
|
||||
chs[1] = ((sector >> 0) & 0x3f) | ((sector >> 16) & 0xc0);
|
||||
chs[2] = (sector >> 14) & 0xff;
|
||||
}
|
||||
|
||||
|
||||
// Partition after address are turned into absolute
|
||||
// addresses, assumes bd is initialized
|
||||
static int partition_absolute(
|
||||
BlockDevice *bd, int part, uint8_t type,
|
||||
bd_size_t offset, bd_size_t size)
|
||||
{
|
||||
// Allocate smallest buffer necessary to write MBR
|
||||
uint32_t buffer_size = std::max<uint32_t>(bd->get_program_size(), sizeof(struct mbr_table));
|
||||
|
||||
// Prevent alignment issues
|
||||
if (buffer_size % bd->get_program_size() != 0) {
|
||||
buffer_size += bd->get_program_size() - (buffer_size % bd->get_program_size());
|
||||
}
|
||||
|
||||
uint8_t *buffer = new uint8_t[buffer_size];
|
||||
|
||||
// Check for existing MBR
|
||||
int err = bd->read(buffer, 512 - buffer_size, buffer_size);
|
||||
if (err) {
|
||||
delete[] buffer;
|
||||
return err;
|
||||
}
|
||||
|
||||
uint32_t table_start_offset = buffer_size - sizeof(struct mbr_table);
|
||||
struct mbr_table *table = reinterpret_cast<struct mbr_table *>(
|
||||
&buffer[table_start_offset]);
|
||||
if (table->signature[0] != 0x55 || table->signature[1] != 0xaa) {
|
||||
// Setup default values for MBR
|
||||
table->signature[0] = 0x55;
|
||||
table->signature[1] = 0xaa;
|
||||
memset(table->entries, 0, sizeof(table->entries));
|
||||
}
|
||||
|
||||
// For Windows-formatted SD card, it is not partitioned (no MBR), but its PBR has the
|
||||
// same boot signature (0xaa55) as MBR. We would easily mis-recognize this SD card has valid
|
||||
// partitions if we only check partition type. We add check by only accepting 0x00 (inactive)
|
||||
// /0x80 (active) for valid partition status.
|
||||
for (int i = 1; i <= 4; i++) {
|
||||
if (table->entries[i - 1].status != 0x00 &&
|
||||
table->entries[i - 1].status != 0x80) {
|
||||
memset(table->entries, 0, sizeof(table->entries));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Setup new partition
|
||||
MBED_ASSERT(part >= 1 && part <= 4);
|
||||
table->entries[part - 1].status = 0x00; // inactive (not bootable)
|
||||
table->entries[part - 1].type = type;
|
||||
|
||||
// lba dimensions
|
||||
MBED_ASSERT(bd->is_valid_erase(offset, size));
|
||||
uint32_t sector = std::max<uint32_t>(bd->get_erase_size(), 512);
|
||||
uint32_t lba_offset = offset / sector;
|
||||
uint32_t lba_size = size / sector;
|
||||
table->entries[part - 1].lba_offset = tole32(lba_offset);
|
||||
table->entries[part - 1].lba_size = tole32(lba_size);
|
||||
|
||||
// chs dimensions
|
||||
tochs(lba_offset, table->entries[part - 1].chs_start);
|
||||
tochs(lba_offset + lba_size - 1, table->entries[part - 1].chs_stop);
|
||||
|
||||
// Check that we don't overlap other entries
|
||||
for (int i = 1; i <= 4; i++) {
|
||||
if (i != part && table->entries[i - 1].type != 0x00) {
|
||||
uint32_t neighbor_lba_offset = fromle32(table->entries[i - 1].lba_offset);
|
||||
uint32_t neighbor_lba_size = fromle32(table->entries[i - 1].lba_size);
|
||||
MBED_ASSERT(
|
||||
(lba_offset >= neighbor_lba_offset + neighbor_lba_size) ||
|
||||
(lba_offset + lba_size <= neighbor_lba_offset));
|
||||
(void)neighbor_lba_offset;
|
||||
(void)neighbor_lba_size;
|
||||
}
|
||||
}
|
||||
|
||||
// As the erase operation may do nothing, erase remainder of the buffer, to eradicate
|
||||
// any remaining programmed data (such as previously programmed file systems).
|
||||
if (table_start_offset > 0) {
|
||||
memset(buffer, 0xFF, table_start_offset);
|
||||
}
|
||||
if (table_start_offset + sizeof(struct mbr_table) < buffer_size) {
|
||||
memset(buffer + table_start_offset + sizeof(struct mbr_table), 0xFF,
|
||||
buffer_size - (table_start_offset + sizeof(struct mbr_table)));
|
||||
}
|
||||
|
||||
// Write out MBR
|
||||
err = bd->erase(0, bd->get_erase_size());
|
||||
if (err) {
|
||||
delete[] buffer;
|
||||
return err;
|
||||
}
|
||||
|
||||
err = bd->program(buffer, 512 - buffer_size, buffer_size);
|
||||
delete[] buffer;
|
||||
return err;
|
||||
}
|
||||
|
||||
int MBRBlockDevice::partition(BlockDevice *bd, int part, uint8_t type, bd_addr_t start)
|
||||
{
|
||||
int err = bd->init();
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
// Calculate dimensions
|
||||
bd_size_t size = ((int64_t)start < 0) ? -start : start;
|
||||
bd_size_t offset = bd->size();
|
||||
|
||||
if (size < 512) {
|
||||
size += std::max<uint32_t>(bd->get_erase_size(), 512);
|
||||
}
|
||||
|
||||
offset -= size;
|
||||
|
||||
err = partition_absolute(bd, part, type, offset, size);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
err = bd->deinit();
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int MBRBlockDevice::partition(BlockDevice *bd, int part, uint8_t type,
|
||||
bd_addr_t start, bd_addr_t stop)
|
||||
{
|
||||
int err = bd->init();
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
// Calculate dimensions
|
||||
bd_size_t offset = ((int64_t)start < 0) ? -start : start;
|
||||
bd_size_t size = ((int64_t)stop < 0) ? -stop : stop;
|
||||
|
||||
if (offset < 512) {
|
||||
offset += std::max<uint32_t>(bd->get_erase_size(), 512);
|
||||
}
|
||||
|
||||
size -= offset;
|
||||
|
||||
err = partition_absolute(bd, part, type, offset, size);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
err = bd->deinit();
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
MBRBlockDevice::MBRBlockDevice(BlockDevice *bd, int part)
|
||||
: _bd(bd), _offset(0), _size(0), _type(0), _part(part), _init_ref_count(0), _is_initialized(false)
|
||||
{
|
||||
MBED_ASSERT(_part >= 1 && _part <= 4);
|
||||
}
|
||||
|
||||
int MBRBlockDevice::init()
|
||||
{
|
||||
uint32_t buffer_size;
|
||||
uint8_t *buffer = 0;
|
||||
struct mbr_table *table;
|
||||
bd_size_t sector;
|
||||
int err;
|
||||
|
||||
uint32_t val = core_util_atomic_incr_u32(&_init_ref_count, 1);
|
||||
|
||||
if (val != 1) {
|
||||
return BD_ERROR_OK;
|
||||
}
|
||||
|
||||
err = _bd->init();
|
||||
if (err) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
// Allocate smallest buffer necessary to write MBR
|
||||
buffer_size = std::max<uint32_t>(_bd->get_read_size(), sizeof(struct mbr_table));
|
||||
buffer = new uint8_t[buffer_size];
|
||||
|
||||
err = _bd->read(buffer, 512 - buffer_size, buffer_size);
|
||||
if (err) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
// Check for valid table
|
||||
table = reinterpret_cast<struct mbr_table *>(&buffer[buffer_size - sizeof(struct mbr_table)]);
|
||||
if (table->signature[0] != 0x55 || table->signature[1] != 0xaa) {
|
||||
err = BD_ERROR_INVALID_MBR;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
// Check for valid partition status
|
||||
// Same reason as in partition_absolute regarding Windows-formatted SD card
|
||||
if (table->entries[_part - 1].status != 0x00 &&
|
||||
table->entries[_part - 1].status != 0x80) {
|
||||
err = BD_ERROR_INVALID_PARTITION;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
// Check for valid entry
|
||||
// 0x00 = no entry
|
||||
// 0x05, 0x0f = extended partitions, currently not supported
|
||||
if ((table->entries[_part - 1].type == 0x00 ||
|
||||
table->entries[_part - 1].type == 0x05 ||
|
||||
table->entries[_part - 1].type == 0x0f)) {
|
||||
err = BD_ERROR_INVALID_PARTITION;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
// Get partition attributes
|
||||
sector = std::max<uint32_t>(_bd->get_erase_size(), 512);
|
||||
_type = table->entries[_part - 1].type;
|
||||
_offset = fromle32(table->entries[_part - 1].lba_offset) * sector;
|
||||
_size = fromle32(table->entries[_part - 1].lba_size) * sector;
|
||||
|
||||
// Check that block addresses are valid
|
||||
if (!_bd->is_valid_erase(_offset, _size)) {
|
||||
err = BD_ERROR_INVALID_PARTITION;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
_is_initialized = true;
|
||||
delete[] buffer;
|
||||
return BD_ERROR_OK;
|
||||
|
||||
fail:
|
||||
delete[] buffer;
|
||||
_is_initialized = false;
|
||||
_init_ref_count = 0;
|
||||
return err;
|
||||
}
|
||||
|
||||
int MBRBlockDevice::deinit()
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_OK;
|
||||
}
|
||||
|
||||
uint32_t val = core_util_atomic_decr_u32(&_init_ref_count, 1);
|
||||
|
||||
if (val) {
|
||||
return BD_ERROR_OK;
|
||||
}
|
||||
|
||||
_is_initialized = false;
|
||||
return _bd->deinit();
|
||||
}
|
||||
|
||||
int MBRBlockDevice::sync()
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
return _bd->sync();
|
||||
}
|
||||
|
||||
int MBRBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
if (!is_valid_read(addr, size)) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
return _bd->read(b, addr + _offset, size);
|
||||
}
|
||||
|
||||
int MBRBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
if (!is_valid_program(addr, size)) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
return _bd->program(b, addr + _offset, size);
|
||||
}
|
||||
|
||||
int MBRBlockDevice::erase(bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
if (!is_valid_erase(addr, size)) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
return _bd->erase(addr + _offset, size);
|
||||
}
|
||||
|
||||
bd_size_t MBRBlockDevice::get_read_size() const
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _bd->get_read_size();
|
||||
}
|
||||
|
||||
bd_size_t MBRBlockDevice::get_program_size() const
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _bd->get_program_size();
|
||||
}
|
||||
|
||||
bd_size_t MBRBlockDevice::get_erase_size() const
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _bd->get_erase_size();
|
||||
}
|
||||
|
||||
bd_size_t MBRBlockDevice::get_erase_size(bd_addr_t addr) const
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _bd->get_erase_size(_offset + addr);
|
||||
}
|
||||
|
||||
int MBRBlockDevice::get_erase_value() const
|
||||
{
|
||||
if (!_is_initialized) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
return _bd->get_erase_value();
|
||||
}
|
||||
|
||||
bd_size_t MBRBlockDevice::size() const
|
||||
{
|
||||
return _size;
|
||||
}
|
||||
|
||||
bd_size_t MBRBlockDevice::get_partition_start() const
|
||||
{
|
||||
return _offset;
|
||||
}
|
||||
|
||||
bd_size_t MBRBlockDevice::get_partition_stop() const
|
||||
{
|
||||
return _offset + _size;
|
||||
}
|
||||
|
||||
uint8_t MBRBlockDevice::get_partition_type() const
|
||||
{
|
||||
return _type;
|
||||
}
|
||||
|
||||
int MBRBlockDevice::get_partition_number() const
|
||||
{
|
||||
return _part;
|
||||
}
|
||||
|
||||
const char *MBRBlockDevice::get_type() const
|
||||
{
|
||||
if (_bd != NULL) {
|
||||
return _bd->get_type();
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
} // namespace mbed
|
||||
123
storage/blockdevice/source/ObservingBlockDevice.cpp
Normal file
123
storage/blockdevice/source/ObservingBlockDevice.cpp
Normal file
@@ -0,0 +1,123 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2017-2017 ARM Limited
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "blockdevice/ObservingBlockDevice.h"
|
||||
#include "blockdevice/ReadOnlyBlockDevice.h"
|
||||
|
||||
namespace mbed {
|
||||
|
||||
ObservingBlockDevice::ObservingBlockDevice(BlockDevice *bd)
|
||||
: _bd(bd)
|
||||
{
|
||||
// Does nothing
|
||||
}
|
||||
|
||||
ObservingBlockDevice::~ObservingBlockDevice()
|
||||
{
|
||||
// Does nothing
|
||||
}
|
||||
|
||||
void ObservingBlockDevice::attach(mbed::Callback<void(BlockDevice *)> cb)
|
||||
{
|
||||
_change = cb;
|
||||
}
|
||||
|
||||
int ObservingBlockDevice::init()
|
||||
{
|
||||
return _bd->init();
|
||||
}
|
||||
|
||||
int ObservingBlockDevice::deinit()
|
||||
{
|
||||
return _bd->deinit();
|
||||
}
|
||||
|
||||
int ObservingBlockDevice::sync()
|
||||
{
|
||||
return _bd->sync();
|
||||
}
|
||||
|
||||
int ObservingBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
return _bd->read(buffer, addr, size);
|
||||
}
|
||||
|
||||
int ObservingBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
int res = _bd->program(buffer, addr, size);
|
||||
if (_change) {
|
||||
ReadOnlyBlockDevice dev(_bd);
|
||||
_change(&dev);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
int ObservingBlockDevice::erase(bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
int res = _bd->erase(addr, size);
|
||||
if (_change) {
|
||||
ReadOnlyBlockDevice dev(_bd);
|
||||
_change(&dev);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
bd_size_t ObservingBlockDevice::get_read_size() const
|
||||
{
|
||||
return _bd->get_read_size();
|
||||
}
|
||||
|
||||
bd_size_t ObservingBlockDevice::get_program_size() const
|
||||
{
|
||||
return _bd->get_program_size();
|
||||
}
|
||||
|
||||
bd_size_t ObservingBlockDevice::get_erase_size() const
|
||||
{
|
||||
return _bd->get_erase_size();
|
||||
}
|
||||
|
||||
bd_size_t ObservingBlockDevice::get_erase_size(bd_addr_t addr) const
|
||||
{
|
||||
return _bd->get_erase_size(addr);
|
||||
}
|
||||
|
||||
int ObservingBlockDevice::get_erase_value() const
|
||||
{
|
||||
return _bd->get_erase_value();
|
||||
}
|
||||
|
||||
bd_size_t ObservingBlockDevice::size() const
|
||||
{
|
||||
return _bd->size();
|
||||
}
|
||||
|
||||
const char *ObservingBlockDevice::get_type() const
|
||||
{
|
||||
if (_bd != NULL) {
|
||||
return _bd->get_type();
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
} // namespace mbed
|
||||
133
storage/blockdevice/source/ProfilingBlockDevice.cpp
Normal file
133
storage/blockdevice/source/ProfilingBlockDevice.cpp
Normal file
@@ -0,0 +1,133 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2017 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "blockdevice/ProfilingBlockDevice.h"
|
||||
#include "stddef.h"
|
||||
|
||||
namespace mbed {
|
||||
|
||||
ProfilingBlockDevice::ProfilingBlockDevice(BlockDevice *bd)
|
||||
: _bd(bd)
|
||||
, _read_count(0)
|
||||
, _program_count(0)
|
||||
, _erase_count(0)
|
||||
{
|
||||
}
|
||||
|
||||
int ProfilingBlockDevice::init()
|
||||
{
|
||||
return _bd->init();
|
||||
}
|
||||
|
||||
int ProfilingBlockDevice::deinit()
|
||||
{
|
||||
return _bd->deinit();
|
||||
}
|
||||
|
||||
int ProfilingBlockDevice::sync()
|
||||
{
|
||||
return _bd->sync();
|
||||
}
|
||||
|
||||
int ProfilingBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
int err = _bd->read(b, addr, size);
|
||||
if (!err) {
|
||||
_read_count += size;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
int ProfilingBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
int err = _bd->program(b, addr, size);
|
||||
if (!err) {
|
||||
_program_count += size;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
int ProfilingBlockDevice::erase(bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
int err = _bd->erase(addr, size);
|
||||
if (!err) {
|
||||
_erase_count += size;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
bd_size_t ProfilingBlockDevice::get_read_size() const
|
||||
{
|
||||
return _bd->get_read_size();
|
||||
}
|
||||
|
||||
bd_size_t ProfilingBlockDevice::get_program_size() const
|
||||
{
|
||||
return _bd->get_program_size();
|
||||
}
|
||||
|
||||
bd_size_t ProfilingBlockDevice::get_erase_size() const
|
||||
{
|
||||
return _bd->get_erase_size();
|
||||
}
|
||||
|
||||
bd_size_t ProfilingBlockDevice::get_erase_size(bd_addr_t addr) const
|
||||
{
|
||||
return _bd->get_erase_size(addr);
|
||||
}
|
||||
|
||||
int ProfilingBlockDevice::get_erase_value() const
|
||||
{
|
||||
return _bd->get_erase_value();
|
||||
}
|
||||
|
||||
bd_size_t ProfilingBlockDevice::size() const
|
||||
{
|
||||
return _bd->size();
|
||||
}
|
||||
|
||||
void ProfilingBlockDevice::reset()
|
||||
{
|
||||
_read_count = 0;
|
||||
_program_count = 0;
|
||||
_erase_count = 0;
|
||||
}
|
||||
|
||||
bd_size_t ProfilingBlockDevice::get_read_count() const
|
||||
{
|
||||
return _read_count;
|
||||
}
|
||||
|
||||
bd_size_t ProfilingBlockDevice::get_program_count() const
|
||||
{
|
||||
return _program_count;
|
||||
}
|
||||
|
||||
bd_size_t ProfilingBlockDevice::get_erase_count() const
|
||||
{
|
||||
return _erase_count;
|
||||
}
|
||||
|
||||
const char *ProfilingBlockDevice::get_type() const
|
||||
{
|
||||
if (_bd != NULL) {
|
||||
return _bd->get_type();
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
} // namespace mbed
|
||||
113
storage/blockdevice/source/ReadOnlyBlockDevice.cpp
Normal file
113
storage/blockdevice/source/ReadOnlyBlockDevice.cpp
Normal file
@@ -0,0 +1,113 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2017-2017 ARM Limited
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
/** \addtogroup storage */
|
||||
/** @{*/
|
||||
|
||||
#include "blockdevice/ReadOnlyBlockDevice.h"
|
||||
#include "platform/mbed_error.h"
|
||||
|
||||
namespace mbed {
|
||||
|
||||
ReadOnlyBlockDevice::ReadOnlyBlockDevice(BlockDevice *bd)
|
||||
: _bd(bd)
|
||||
{
|
||||
// Does nothing
|
||||
}
|
||||
|
||||
ReadOnlyBlockDevice::~ReadOnlyBlockDevice()
|
||||
{
|
||||
// Does nothing
|
||||
}
|
||||
|
||||
int ReadOnlyBlockDevice::init()
|
||||
{
|
||||
return _bd->init();
|
||||
}
|
||||
|
||||
int ReadOnlyBlockDevice::deinit()
|
||||
{
|
||||
return _bd->deinit();
|
||||
}
|
||||
|
||||
int ReadOnlyBlockDevice::sync()
|
||||
{
|
||||
return _bd->sync();
|
||||
}
|
||||
|
||||
int ReadOnlyBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
return _bd->read(buffer, addr, size);
|
||||
}
|
||||
|
||||
int ReadOnlyBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
return MBED_ERROR_WRITE_PROTECTED;
|
||||
}
|
||||
|
||||
int ReadOnlyBlockDevice::erase(bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
return MBED_ERROR_WRITE_PROTECTED;
|
||||
}
|
||||
|
||||
bd_size_t ReadOnlyBlockDevice::get_read_size() const
|
||||
{
|
||||
return _bd->get_read_size();
|
||||
}
|
||||
|
||||
bd_size_t ReadOnlyBlockDevice::get_program_size() const
|
||||
{
|
||||
return _bd->get_program_size();
|
||||
}
|
||||
|
||||
bd_size_t ReadOnlyBlockDevice::get_erase_size() const
|
||||
{
|
||||
return _bd->get_erase_size();
|
||||
}
|
||||
|
||||
bd_size_t ReadOnlyBlockDevice::get_erase_size(bd_addr_t addr) const
|
||||
{
|
||||
return _bd->get_erase_size(addr);
|
||||
}
|
||||
|
||||
int ReadOnlyBlockDevice::get_erase_value() const
|
||||
{
|
||||
return _bd->get_erase_value();
|
||||
}
|
||||
|
||||
bd_size_t ReadOnlyBlockDevice::size() const
|
||||
{
|
||||
return _bd->size();
|
||||
}
|
||||
|
||||
const char *ReadOnlyBlockDevice::get_type() const
|
||||
{
|
||||
if (_bd != NULL) {
|
||||
return _bd->get_type();
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
/** @}*/
|
||||
158
storage/blockdevice/source/SlicingBlockDevice.cpp
Normal file
158
storage/blockdevice/source/SlicingBlockDevice.cpp
Normal file
@@ -0,0 +1,158 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2017 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "blockdevice/SlicingBlockDevice.h"
|
||||
#include "platform/mbed_assert.h"
|
||||
#include "stddef.h"
|
||||
#include <stdio.h>
|
||||
|
||||
namespace mbed {
|
||||
|
||||
|
||||
SlicingBlockDevice::SlicingBlockDevice(BlockDevice *bd, bd_addr_t start, bd_addr_t stop)
|
||||
: _bd(bd)
|
||||
, _start_from_end(false), _start(start)
|
||||
, _stop_from_end(false), _stop(stop)
|
||||
{
|
||||
MBED_ASSERT(bd);
|
||||
// SlicingBlockDevice(bd, 0,0) would use the full block, which does not make sense, it must be a programming eror.
|
||||
// SlicingBlockDevice(bd, 100,100) would have no size, which is also a programming error.
|
||||
MBED_ASSERT(start != stop);
|
||||
|
||||
if ((int64_t)_start < 0) {
|
||||
_start_from_end = true;
|
||||
_start = -_start;
|
||||
}
|
||||
|
||||
if ((int64_t)_stop <= 0) {
|
||||
_stop_from_end = true;
|
||||
_stop = -_stop;
|
||||
}
|
||||
}
|
||||
|
||||
int SlicingBlockDevice::init()
|
||||
{
|
||||
int err = _bd->init();
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
bd_size_t size = _bd->size();
|
||||
|
||||
// Calculate from_end values
|
||||
if (_start_from_end) {
|
||||
_start_from_end = false;
|
||||
_start = size - _start;
|
||||
}
|
||||
|
||||
if (_stop_from_end) {
|
||||
_stop_from_end = false;
|
||||
_stop = size - _stop;
|
||||
}
|
||||
|
||||
// Check that block addresses are valid
|
||||
if (!is_valid_erase(0, _stop - _start)) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int SlicingBlockDevice::deinit()
|
||||
{
|
||||
return _bd->deinit();
|
||||
}
|
||||
|
||||
int SlicingBlockDevice::sync()
|
||||
{
|
||||
return _bd->sync();
|
||||
}
|
||||
|
||||
int SlicingBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
if (!is_valid_read(addr, size)) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
return _bd->read(b, addr + _start, size);
|
||||
}
|
||||
|
||||
int SlicingBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
if (!is_valid_program(addr, size)) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
return _bd->program(b, addr + _start, size);
|
||||
}
|
||||
|
||||
int SlicingBlockDevice::erase(bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
if (!is_valid_erase(addr, size)) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
return _bd->erase(addr + _start, size);
|
||||
}
|
||||
|
||||
bool SlicingBlockDevice::is_valid_read(bd_addr_t addr, bd_size_t size) const
|
||||
{
|
||||
return _bd->is_valid_read(_start + addr, size) && _start + addr + size <= _stop;
|
||||
}
|
||||
|
||||
bool SlicingBlockDevice::is_valid_program(bd_addr_t addr, bd_size_t size) const
|
||||
{
|
||||
return _bd->is_valid_program(_start + addr, size) && _start + addr + size <= _stop;
|
||||
}
|
||||
|
||||
bool SlicingBlockDevice::is_valid_erase(bd_addr_t addr, bd_size_t size) const
|
||||
{
|
||||
return _bd->is_valid_erase(_start + addr, size) && _start + addr + size <= _stop;
|
||||
}
|
||||
|
||||
bd_size_t SlicingBlockDevice::get_read_size() const
|
||||
{
|
||||
return _bd->get_read_size();
|
||||
}
|
||||
|
||||
bd_size_t SlicingBlockDevice::get_program_size() const
|
||||
{
|
||||
return _bd->get_program_size();
|
||||
}
|
||||
|
||||
bd_size_t SlicingBlockDevice::get_erase_size() const
|
||||
{
|
||||
return _bd->get_erase_size(_start);
|
||||
}
|
||||
|
||||
bd_size_t SlicingBlockDevice::get_erase_size(bd_addr_t addr) const
|
||||
{
|
||||
return _bd->get_erase_size(_start + addr);
|
||||
}
|
||||
|
||||
int SlicingBlockDevice::get_erase_value() const
|
||||
{
|
||||
return _bd->get_erase_value();
|
||||
}
|
||||
|
||||
bd_size_t SlicingBlockDevice::size() const
|
||||
{
|
||||
return _stop - _start;
|
||||
}
|
||||
|
||||
const char *SlicingBlockDevice::get_type() const
|
||||
{
|
||||
return _bd->get_type();
|
||||
}
|
||||
|
||||
} // namespace mbed
|
||||
Reference in New Issue
Block a user