From ba5e1427fc74b0f439a75d08a46cbe9233242b27 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Fri, 20 Jan 2017 13:26:35 -0600 Subject: [PATCH] bd: Added utility block device classes - ChainingBlockDevice - SlicingBlockDevice --- .../filesystem/util_block_device/main.cpp | 188 ++++++++++++++++ .../filesystem/bd/ChainingBlockDevice.cpp | 204 ++++++++++++++++++ features/filesystem/bd/ChainingBlockDevice.h | 153 +++++++++++++ features/filesystem/bd/SlicingBlockDevice.cpp | 125 +++++++++++ features/filesystem/bd/SlicingBlockDevice.h | 151 +++++++++++++ 5 files changed, 821 insertions(+) create mode 100644 features/TESTS/filesystem/util_block_device/main.cpp create mode 100644 features/filesystem/bd/ChainingBlockDevice.cpp create mode 100644 features/filesystem/bd/ChainingBlockDevice.h create mode 100644 features/filesystem/bd/SlicingBlockDevice.cpp create mode 100644 features/filesystem/bd/SlicingBlockDevice.h diff --git a/features/TESTS/filesystem/util_block_device/main.cpp b/features/TESTS/filesystem/util_block_device/main.cpp new file mode 100644 index 0000000000..0b0d4c47de --- /dev/null +++ b/features/TESTS/filesystem/util_block_device/main.cpp @@ -0,0 +1,188 @@ +/* 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 "mbed.h" +#include "greentea-client/test_env.h" +#include "unity.h" +#include "utest.h" + +#include "HeapBlockDevice.h" +#include "SlicingBlockDevice.h" +#include "ChainingBlockDevice.h" +#include + +using namespace utest::v1; + +#define BLOCK_COUNT 16 +#define BLOCK_SIZE 512 +uint8_t write_block[BLOCK_SIZE]; +uint8_t read_block[BLOCK_SIZE]; + +// Simple test which read/writes blocks on a sliced block device +void test_slicing() { + HeapBlockDevice bd(BLOCK_COUNT*BLOCK_SIZE, BLOCK_SIZE); + + // Test with first slice of block device + SlicingBlockDevice slice1(&bd, 0, (BLOCK_COUNT/2)*BLOCK_SIZE); + + int err = slice1.init(); + TEST_ASSERT_EQUAL(0, err); + + TEST_ASSERT_EQUAL(BLOCK_SIZE, slice1.get_write_size()); + TEST_ASSERT_EQUAL((BLOCK_COUNT/2)*BLOCK_SIZE, slice1.size()); + + // Fill with random sequence + srand(1); + for (int i = 0; i < BLOCK_SIZE; i++) { + write_block[i] = 0xff & rand(); + } + + // Write, sync, and read the block + err = slice1.write(write_block, 0, BLOCK_SIZE); + TEST_ASSERT_EQUAL(0, err); + + err = slice1.read(read_block, 0, BLOCK_SIZE); + TEST_ASSERT_EQUAL(0, err); + + // Check that the data was unmodified + srand(1); + for (int i = 0; i < BLOCK_SIZE; i++) { + TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]); + } + + // Check with original block device + err = bd.read(read_block, 0, BLOCK_SIZE); + TEST_ASSERT_EQUAL(0, err); + + // Check that the data was unmodified + srand(1); + for (int i = 0; i < BLOCK_SIZE; i++) { + TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]); + } + + err = slice1.deinit(); + TEST_ASSERT_EQUAL(0, err); + + + // Test with second slice of block device + SlicingBlockDevice slice2(&bd, -(BLOCK_COUNT/2)*BLOCK_SIZE); + + err = slice2.init(); + TEST_ASSERT_EQUAL(0, err); + + TEST_ASSERT_EQUAL(BLOCK_SIZE, slice2.get_write_size()); + TEST_ASSERT_EQUAL((BLOCK_COUNT/2)*BLOCK_SIZE, slice2.size()); + + // Fill with random sequence + srand(1); + for (int i = 0; i < BLOCK_SIZE; i++) { + write_block[i] = 0xff & rand(); + } + + // Write, sync, and read the block + err = slice2.write(write_block, 0, BLOCK_SIZE); + TEST_ASSERT_EQUAL(0, err); + + err = slice2.read(read_block, 0, BLOCK_SIZE); + TEST_ASSERT_EQUAL(0, err); + + // Check that the data was unmodified + srand(1); + for (int i = 0; i < BLOCK_SIZE; i++) { + TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]); + } + + // Check with original block device + err = bd.read(read_block, (BLOCK_COUNT/2)*BLOCK_SIZE, BLOCK_SIZE); + TEST_ASSERT_EQUAL(0, err); + + // Check that the data was unmodified + srand(1); + for (int i = 0; i < BLOCK_SIZE; i++) { + TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]); + } + + err = slice2.deinit(); + TEST_ASSERT_EQUAL(0, err); +} + +// Simple test which read/writes blocks on a chain of block devices +void test_chaining() { + HeapBlockDevice bd1((BLOCK_COUNT/2)*BLOCK_SIZE, BLOCK_SIZE); + HeapBlockDevice bd2((BLOCK_COUNT/2)*BLOCK_SIZE, BLOCK_SIZE); + + // Test with chain of block device + BlockDevice *bds[] = {&bd1, &bd2}; + ChainingBlockDevice chain(bds); + + int err = chain.init(); + TEST_ASSERT_EQUAL(0, err); + + TEST_ASSERT_EQUAL(BLOCK_SIZE, chain.get_write_size()); + TEST_ASSERT_EQUAL(BLOCK_COUNT*BLOCK_SIZE, chain.size()); + + // Fill with random sequence + srand(1); + for (int i = 0; i < BLOCK_SIZE; i++) { + write_block[i] = 0xff & rand(); + } + + // Write, sync, and read the block + err = chain.write(write_block, 0, BLOCK_SIZE); + TEST_ASSERT_EQUAL(0, err); + + err = chain.read(read_block, 0, BLOCK_SIZE); + TEST_ASSERT_EQUAL(0, err); + + // Check that the data was unmodified + srand(1); + for (int i = 0; i < BLOCK_SIZE; i++) { + TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]); + } + + // Write, sync, and read the block + err = chain.write(write_block, (BLOCK_COUNT/2)*BLOCK_SIZE, BLOCK_SIZE); + TEST_ASSERT_EQUAL(0, err); + + err = chain.read(read_block, (BLOCK_COUNT/2)*BLOCK_SIZE, BLOCK_SIZE); + TEST_ASSERT_EQUAL(0, err); + + // Check that the data was unmodified + srand(1); + for (int i = 0; i < BLOCK_SIZE; i++) { + TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]); + } + + err = chain.deinit(); + TEST_ASSERT_EQUAL(0, err); +} + + +// Test setup +utest::v1::status_t test_setup(const size_t number_of_cases) { + GREENTEA_SETUP(10, "default_auto"); + return verbose_test_setup_handler(number_of_cases); +} + +Case cases[] = { + Case("Testing slicing of a block device", test_slicing), + Case("Testing chaining of block devices", test_chaining), +}; + +Specification specification(test_setup, cases); + +int main() { + return !Harness::run(specification); +} diff --git a/features/filesystem/bd/ChainingBlockDevice.cpp b/features/filesystem/bd/ChainingBlockDevice.cpp new file mode 100644 index 0000000000..2f623b6f66 --- /dev/null +++ b/features/filesystem/bd/ChainingBlockDevice.cpp @@ -0,0 +1,204 @@ +/* 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 "ChainingBlockDevice.h" + + +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) +{ +} + +static bool is_aligned(uint64_t x, uint64_t alignment) +{ + return (x / alignment) * alignment == x; +} + +bd_error_t ChainingBlockDevice::init() +{ + _read_size = 0; + _program_size = 0; + _erase_size = 0; + _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++) { + bd_error_t err = _bds[i]->init(); + if (err) { + return err; + } + + bd_size_t read = _bds[i]->get_read_size(); + if (i == 0 || (read >= _read_size && is_aligned(read, _read_size))) { + _read_size = read; + } else if (!(_read_size > read && is_aligned(_read_size, read))) { + return BD_ERROR_PARAMETER; + } + + bd_size_t program = _bds[i]->get_program_size(); + if (i == 0 || (program >= _program_size && is_aligned(program, _program_size))) { + _program_size = program; + } else if (!(_program_size > program && is_aligned(_program_size, program))) { + return BD_ERROR_PARAMETER; + } + + bd_size_t erase = _bds[i]->get_erase_size(); + if (i == 0 || (erase >= _erase_size && is_aligned(erase, _erase_size))) { + _erase_size = erase; + } else if (!(_erase_size > erase && is_aligned(_erase_size, erase))) { + return BD_ERROR_PARAMETER; + } + + _size += _bds[i]->size(); + } + + return 0; +} + +bd_error_t ChainingBlockDevice::deinit() +{ + for (size_t i = 0; i < _bd_count; i++) { + bd_error_t err = _bds[i]->deinit(); + if (err) { + return err; + } + } + + return 0; +} + +bd_error_t ChainingBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size) +{ + if (!is_valid_read(addr, size)) { + return BD_ERROR_PARAMETER; + } + + uint8_t *buffer = static_cast(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; + } + + bd_error_t err = _bds[i]->read(buffer, addr, read); + if (err) { + return err; + } + + buffer += read; + addr += read; + size -= read; + } + + addr -= size; + } + + return 0; +} + +bd_error_t ChainingBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size) +{ + if (!is_valid_program(addr, size)) { + return BD_ERROR_PARAMETER; + } + + const uint8_t *buffer = static_cast(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; + } + + bd_error_t err = _bds[i]->program(buffer, addr, program); + if (err) { + return err; + } + + buffer += program; + addr += program; + size -= program; + } + + addr -= size; + } + + return 0; +} + +bd_error_t ChainingBlockDevice::erase(bd_addr_t addr, bd_size_t size) +{ + if (!is_valid_erase(addr, size)) { + return BD_ERROR_PARAMETER; + } + + // 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; + } + + bd_error_t err = _bds[i]->erase(addr, erase); + if (err) { + return err; + } + + addr += erase; + size -= erase; + } + + addr -= size; + } + + return 0; +} + +bd_size_t ChainingBlockDevice::get_read_size() +{ + return _read_size; +} + +bd_size_t ChainingBlockDevice::get_program_size() +{ + return _program_size; +} + +bd_size_t ChainingBlockDevice::get_erase_size() +{ + return _erase_size; +} + +bd_size_t ChainingBlockDevice::size() +{ + return _size; +} diff --git a/features/filesystem/bd/ChainingBlockDevice.h b/features/filesystem/bd/ChainingBlockDevice.h new file mode 100644 index 0000000000..0c021c9082 --- /dev/null +++ b/features/filesystem/bd/ChainingBlockDevice.h @@ -0,0 +1,153 @@ +/* mbed Microcontroller Library + * Copyright (c) 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. + */ +#ifndef MBED_CHAINING_BLOCK_DEVICE_H +#define MBED_CHAINING_BLOCK_DEVICE_H + +#include "BlockDevice.h" +#include "mbed.h" + + +/** Block device for chaining multiple block devices + * with the similar block sizes at sequential addresses + * + * @code + * #include "mbed.h" + * #include "HeapBlockDevice.h" + * #include "ChainingBlockDevice.h" + * + * // Create two smaller block devices with + * // 64 and 32 blocks of size 512 bytes + * HeapBlockDevice mem1(64*512, 512); + * HeapBlockDevice mem2(32*512, 512); + * + * // Create a block device backed by mem1 and mem2 + * // contains 96 blocks of size 512 bytes + * BlockDevice *bds[] = {&mem1, &mem2}; + * ChainingBlockDevice chainmem(bds); + */ +class ChainingBlockDevice : public BlockDevice { +public: + /** Lifetime of the memory block device + * + * @param bds Array of block devices to chain with sequential block addresses + * @param count Number of block devices to chain + * @note All block devices must have the same block size + */ + ChainingBlockDevice(BlockDevice **bds, size_t bd_count); + + /** Lifetime of the memory block device + * + * @param bds Array of block devices to chain with sequential block addresses + * @note All block devices must have the same block size + */ + template + ChainingBlockDevice(BlockDevice *(&bds)[Size]) + : _bds(bds), _bd_count(sizeof(bds) / sizeof(bds[0])) + , _read_size(0), _program_size(0), _erase_size(0), _size(0) { + } + + /** Lifetime of the memory block device + * + * @param bds Array of block devices to chain with sequential block addresses + * @note All block devices must have the same block size + */ + virtual ~ChainingBlockDevice() {} + + /** Initialize a block device + * + * @return 0 on success or a negative error code on failure + */ + virtual bd_error_t init(); + + /** Deinitialize a block device + * + * @return 0 on success or a negative error code on failure + */ + virtual bd_error_t deinit(); + + /** Read blocks from a block device + * + * @param buffer Buffer to write blocks to + * @param addr Address of block to begin reading from + * @param size Size to read in bytes, must be a multiple of read block size + * @return 0 on success, negative error code on failure + */ + virtual bd_error_t read(void *buffer, bd_addr_t addr, bd_size_t size); + + /** Program blocks to a block device + * + * The blocks must have been erased prior to being programmed + * + * @param buffer Buffer of data to write to blocks + * @param addr Address of block to begin writing to + * @param size Size to write in bytes, must be a multiple of program block size + * @return 0 on success, negative error code on failure + */ + virtual bd_error_t program(const void *buffer, bd_addr_t addr, bd_size_t size); + + /** Erase blocks on a block device + * + * The state of an erased block is undefined until it has been programmed + * + * @param addr Address of block to begin erasing + * @param size Size to erase in bytes, must be a multiple of erase block size + * @return 0 on success, negative error code on failure + */ + virtual bd_error_t erase(bd_addr_t addr, bd_size_t size); + + /** Get the size of a readable block + * + * @return Size of a readable block in bytes + */ + virtual bd_size_t get_read_size(); + + /** Get the size of a programable block + * + * @return Size of a programable block in bytes + * @note Must be a multiple of the read size + */ + virtual bd_size_t get_program_size(); + + /** Get the size of a eraseable block + * + * @return Size of a eraseable block in bytes + * @note Must be a multiple of the program size + */ + virtual bd_size_t get_erase_size(); + + /** Get the total size of the underlying device + * + * @return Size of the underlying device in bytes + */ + virtual bd_size_t size(); + +protected: + BlockDevice **_bds; + size_t _bd_count; + bd_size_t _read_size; + bd_size_t _program_size; + bd_size_t _erase_size; + bd_size_t _size; +}; + + +#endif diff --git a/features/filesystem/bd/SlicingBlockDevice.cpp b/features/filesystem/bd/SlicingBlockDevice.cpp new file mode 100644 index 0000000000..2c6450a43a --- /dev/null +++ b/features/filesystem/bd/SlicingBlockDevice.cpp @@ -0,0 +1,125 @@ +/* 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 "SlicingBlockDevice.h" + + +SlicingBlockDevice::SlicingBlockDevice(BlockDevice *bd, bd_addr_t start) + : _bd(bd) + , _start_from_end(false), _start(start) + , _stop_from_end(true), _stop(0) +{ + if ((int64_t)_start < 0) { + _start_from_end = true; + _start = -_start; + } +} + +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) +{ + if ((int64_t)_start < 0) { + _start_from_end = true; + _start = -_start; + } + + if ((int64_t)_stop < 0) { + _stop_from_end = true; + _stop = -_stop; + } +} + +bd_error_t SlicingBlockDevice::init() +{ + bd_error_t 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 (!_bd->is_valid_write(_start, _stop - _start)) { + return BD_ERROR_PARAMETER; + } + + return 0; +} + +bd_error_t SlicingBlockDevice::deinit() +{ + return _bd->deinit(); +} + +bd_error_t SlicingBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size) +{ + if (!is_valid_read(addr, size)) { + return BD_ERROR_PARAMETER; + } + + return _bd->read(b, addr + _start, size); +} + +bd_error_t SlicingBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size) +{ + if (!is_valid_program(addr, size)) { + return BD_ERROR_PARAMETER; + } + + return _bd->program(b, addr + _start, size); +} + +bd_error_t SlicingBlockDevice::erase(bd_addr_t addr, bd_size_t size) +{ + if (!is_valid_erase(addr, size)) { + return BD_ERROR_PARAMETER; + } + + return _bd->erase(addr + _start, size); +} + +bd_size_t SlicingBlockDevice::get_read_size() +{ + return _bd->get_read_size(); +} + +bd_size_t SlicingBlockDevice::get_program_size() +{ + return _bd->get_program_size(); +} + +bd_size_t SlicingBlockDevice::get_erase_size() +{ + return _bd->get_erase_size(); +} + +bd_size_t SlicingBlockDevice::size() +{ + return _stop - _start; +} diff --git a/features/filesystem/bd/SlicingBlockDevice.h b/features/filesystem/bd/SlicingBlockDevice.h new file mode 100644 index 0000000000..18b1cb6c63 --- /dev/null +++ b/features/filesystem/bd/SlicingBlockDevice.h @@ -0,0 +1,151 @@ +/* mbed Microcontroller Library + * Copyright (c) 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. + */ +#ifndef MBED_SLICING_BLOCK_DEVICE_H +#define MBED_SLICING_BLOCK_DEVICE_H + +#include "BlockDevice.h" +#include "mbed.h" + + +/** Block device for mapping to a slice of another block device + * + * @code + * #include "mbed.h" + * #include "HeapBlockDevice.h" + * #include "SlicingBlockDevice.h" + * + * // Create a block device with 64 blocks of size 512 + * HeapBlockDevice mem(64*512, 512); + * + * // Create a block device that maps to the first 32 blocks + * SlicingBlockDevice slice1(&mem, 0*512, 32*512); + * + * // Create a block device that maps to the last 32 blocks + * SlicingBlockDevice slice2(&mem, 32*512); + * + * // Create a block device that maps to the middle 32 blocks + * SlicingBlockDevice slice3(&mem, 16*512, -16*512); + */ +class SlicingBlockDevice : public BlockDevice { +public: + /** Lifetime of the memory block device + * + * @param bd Block device to back the SlicingBlockDevice + * @param start Start block address to map to block 0, negative addresses + * are calculated from the end of the underlying block device + * @note This is the same as SlicingBlockDevice(bd, start, bd->size()) + */ + SlicingBlockDevice(BlockDevice *bd, bd_addr_t start); + + /** Lifetime of the memory block device + * + * @param bd Block device to back the SlicingBlockDevice + * @param start Start block address to map to block 0, negative addresses + * are calculated from the end of the underlying block device + * @param stop End block address to mark the end of the block device, + * this block is not mapped, negative addresses are + * calculated from the end of the underlying block device + */ + SlicingBlockDevice(BlockDevice *bd, bd_addr_t start, bd_addr_t end); + + /** Lifetime of a block device + */ + virtual ~SlicingBlockDevice() {}; + + /** Initialize a block device + * + * @return 0 on success or a negative error code on failure + */ + virtual bd_error_t init(); + + /** Deinitialize a block device + * + * @return 0 on success or a negative error code on failure + */ + virtual bd_error_t deinit(); + + /** Read blocks from a block device + * + * @param buffer Buffer to write blocks to + * @param addr Address of block to begin reading from + * @param size Size to read in bytes, must be a multiple of read block size + * @return 0 on success, negative error code on failure + */ + virtual bd_error_t read(void *buffer, bd_addr_t addr, bd_size_t size); + + /** Program blocks to a block device + * + * The blocks must have been erased prior to being programmed + * + * @param buffer Buffer of data to write to blocks + * @param addr Address of block to begin writing to + * @param size Size to write in bytes, must be a multiple of program block size + * @return 0 on success, negative error code on failure + */ + virtual bd_error_t program(const void *buffer, bd_addr_t addr, bd_size_t size); + + /** Erase blocks on a block device + * + * The state of an erased block is undefined until it has been programmed + * + * @param addr Address of block to begin erasing + * @param size Size to erase in bytes, must be a multiple of erase block size + * @return 0 on success, negative error code on failure + */ + virtual bd_error_t erase(bd_addr_t addr, bd_size_t size); + + /** Get the size of a readable block + * + * @return Size of a readable block in bytes + */ + virtual bd_size_t get_read_size(); + + /** Get the size of a programable block + * + * @return Size of a programable block in bytes + * @note Must be a multiple of the read size + */ + virtual bd_size_t get_program_size(); + + /** Get the size of a eraseable block + * + * @return Size of a eraseable block in bytes + * @note Must be a multiple of the program size + */ + virtual bd_size_t get_erase_size(); + + /** Get the total size of the underlying device + * + * @return Size of the underlying device in bytes + */ + virtual bd_size_t size(); + +protected: + BlockDevice *_bd; + bool _start_from_end; + bd_size_t _start; + bool _stop_from_end; + bd_size_t _stop; +}; + + +#endif