![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
BBR 1 Ebene
mbed-os/features/filesystem/bd/ChainingBlockDevice.cpp@0:fbdae7e6d805, 2018-05-14 (annotated)
- Committer:
- borlanic
- Date:
- Mon May 14 11:29:06 2018 +0000
- Revision:
- 0:fbdae7e6d805
BBR
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
borlanic | 0:fbdae7e6d805 | 1 | /* mbed Microcontroller Library |
borlanic | 0:fbdae7e6d805 | 2 | * Copyright (c) 2017 ARM Limited |
borlanic | 0:fbdae7e6d805 | 3 | * |
borlanic | 0:fbdae7e6d805 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
borlanic | 0:fbdae7e6d805 | 5 | * you may not use this file except in compliance with the License. |
borlanic | 0:fbdae7e6d805 | 6 | * You may obtain a copy of the License at |
borlanic | 0:fbdae7e6d805 | 7 | * |
borlanic | 0:fbdae7e6d805 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
borlanic | 0:fbdae7e6d805 | 9 | * |
borlanic | 0:fbdae7e6d805 | 10 | * Unless required by applicable law or agreed to in writing, software |
borlanic | 0:fbdae7e6d805 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
borlanic | 0:fbdae7e6d805 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
borlanic | 0:fbdae7e6d805 | 13 | * See the License for the specific language governing permissions and |
borlanic | 0:fbdae7e6d805 | 14 | * limitations under the License. |
borlanic | 0:fbdae7e6d805 | 15 | */ |
borlanic | 0:fbdae7e6d805 | 16 | |
borlanic | 0:fbdae7e6d805 | 17 | #include "ChainingBlockDevice.h" |
borlanic | 0:fbdae7e6d805 | 18 | |
borlanic | 0:fbdae7e6d805 | 19 | |
borlanic | 0:fbdae7e6d805 | 20 | ChainingBlockDevice::ChainingBlockDevice(BlockDevice **bds, size_t bd_count) |
borlanic | 0:fbdae7e6d805 | 21 | : _bds(bds), _bd_count(bd_count) |
borlanic | 0:fbdae7e6d805 | 22 | , _read_size(0), _program_size(0), _erase_size(0), _size(0) |
borlanic | 0:fbdae7e6d805 | 23 | , _erase_value(-1) |
borlanic | 0:fbdae7e6d805 | 24 | { |
borlanic | 0:fbdae7e6d805 | 25 | } |
borlanic | 0:fbdae7e6d805 | 26 | |
borlanic | 0:fbdae7e6d805 | 27 | static bool is_aligned(uint64_t x, uint64_t alignment) |
borlanic | 0:fbdae7e6d805 | 28 | { |
borlanic | 0:fbdae7e6d805 | 29 | return (x / alignment) * alignment == x; |
borlanic | 0:fbdae7e6d805 | 30 | } |
borlanic | 0:fbdae7e6d805 | 31 | |
borlanic | 0:fbdae7e6d805 | 32 | int ChainingBlockDevice::init() |
borlanic | 0:fbdae7e6d805 | 33 | { |
borlanic | 0:fbdae7e6d805 | 34 | _read_size = 0; |
borlanic | 0:fbdae7e6d805 | 35 | _program_size = 0; |
borlanic | 0:fbdae7e6d805 | 36 | _erase_size = 0; |
borlanic | 0:fbdae7e6d805 | 37 | _erase_value = -1; |
borlanic | 0:fbdae7e6d805 | 38 | _size = 0; |
borlanic | 0:fbdae7e6d805 | 39 | |
borlanic | 0:fbdae7e6d805 | 40 | // Initialize children block devices, find all sizes and |
borlanic | 0:fbdae7e6d805 | 41 | // assert that block sizes are similar. We can't do this in |
borlanic | 0:fbdae7e6d805 | 42 | // the constructor since some block devices may need to be |
borlanic | 0:fbdae7e6d805 | 43 | // initialized before they know their block size/count |
borlanic | 0:fbdae7e6d805 | 44 | for (size_t i = 0; i < _bd_count; i++) { |
borlanic | 0:fbdae7e6d805 | 45 | int err = _bds[i]->init(); |
borlanic | 0:fbdae7e6d805 | 46 | if (err) { |
borlanic | 0:fbdae7e6d805 | 47 | return err; |
borlanic | 0:fbdae7e6d805 | 48 | } |
borlanic | 0:fbdae7e6d805 | 49 | |
borlanic | 0:fbdae7e6d805 | 50 | bd_size_t read = _bds[i]->get_read_size(); |
borlanic | 0:fbdae7e6d805 | 51 | if (i == 0 || (read >= _read_size && is_aligned(read, _read_size))) { |
borlanic | 0:fbdae7e6d805 | 52 | _read_size = read; |
borlanic | 0:fbdae7e6d805 | 53 | } else { |
borlanic | 0:fbdae7e6d805 | 54 | MBED_ASSERT(_read_size > read && is_aligned(_read_size, read)); |
borlanic | 0:fbdae7e6d805 | 55 | } |
borlanic | 0:fbdae7e6d805 | 56 | |
borlanic | 0:fbdae7e6d805 | 57 | bd_size_t program = _bds[i]->get_program_size(); |
borlanic | 0:fbdae7e6d805 | 58 | if (i == 0 || (program >= _program_size && is_aligned(program, _program_size))) { |
borlanic | 0:fbdae7e6d805 | 59 | _program_size = program; |
borlanic | 0:fbdae7e6d805 | 60 | } else { |
borlanic | 0:fbdae7e6d805 | 61 | MBED_ASSERT(_program_size > program && is_aligned(_program_size, program)); |
borlanic | 0:fbdae7e6d805 | 62 | } |
borlanic | 0:fbdae7e6d805 | 63 | |
borlanic | 0:fbdae7e6d805 | 64 | bd_size_t erase = _bds[i]->get_erase_size(); |
borlanic | 0:fbdae7e6d805 | 65 | if (i == 0 || (erase >= _erase_size && is_aligned(erase, _erase_size))) { |
borlanic | 0:fbdae7e6d805 | 66 | _erase_size = erase; |
borlanic | 0:fbdae7e6d805 | 67 | } else { |
borlanic | 0:fbdae7e6d805 | 68 | MBED_ASSERT(_erase_size > erase && is_aligned(_erase_size, erase)); |
borlanic | 0:fbdae7e6d805 | 69 | } |
borlanic | 0:fbdae7e6d805 | 70 | |
borlanic | 0:fbdae7e6d805 | 71 | int value = _bds[i]->get_erase_value(); |
borlanic | 0:fbdae7e6d805 | 72 | if (i == 0 || value == _erase_value) { |
borlanic | 0:fbdae7e6d805 | 73 | _erase_value = value; |
borlanic | 0:fbdae7e6d805 | 74 | } else { |
borlanic | 0:fbdae7e6d805 | 75 | _erase_value = -1; |
borlanic | 0:fbdae7e6d805 | 76 | } |
borlanic | 0:fbdae7e6d805 | 77 | |
borlanic | 0:fbdae7e6d805 | 78 | _size += _bds[i]->size(); |
borlanic | 0:fbdae7e6d805 | 79 | } |
borlanic | 0:fbdae7e6d805 | 80 | |
borlanic | 0:fbdae7e6d805 | 81 | return 0; |
borlanic | 0:fbdae7e6d805 | 82 | } |
borlanic | 0:fbdae7e6d805 | 83 | |
borlanic | 0:fbdae7e6d805 | 84 | int ChainingBlockDevice::deinit() |
borlanic | 0:fbdae7e6d805 | 85 | { |
borlanic | 0:fbdae7e6d805 | 86 | for (size_t i = 0; i < _bd_count; i++) { |
borlanic | 0:fbdae7e6d805 | 87 | int err = _bds[i]->deinit(); |
borlanic | 0:fbdae7e6d805 | 88 | if (err) { |
borlanic | 0:fbdae7e6d805 | 89 | return err; |
borlanic | 0:fbdae7e6d805 | 90 | } |
borlanic | 0:fbdae7e6d805 | 91 | } |
borlanic | 0:fbdae7e6d805 | 92 | |
borlanic | 0:fbdae7e6d805 | 93 | return 0; |
borlanic | 0:fbdae7e6d805 | 94 | } |
borlanic | 0:fbdae7e6d805 | 95 | |
borlanic | 0:fbdae7e6d805 | 96 | int ChainingBlockDevice::sync() |
borlanic | 0:fbdae7e6d805 | 97 | { |
borlanic | 0:fbdae7e6d805 | 98 | for (size_t i = 0; i < _bd_count; i++) { |
borlanic | 0:fbdae7e6d805 | 99 | int err = _bds[i]->sync(); |
borlanic | 0:fbdae7e6d805 | 100 | if (err) { |
borlanic | 0:fbdae7e6d805 | 101 | return err; |
borlanic | 0:fbdae7e6d805 | 102 | } |
borlanic | 0:fbdae7e6d805 | 103 | } |
borlanic | 0:fbdae7e6d805 | 104 | |
borlanic | 0:fbdae7e6d805 | 105 | return 0; |
borlanic | 0:fbdae7e6d805 | 106 | } |
borlanic | 0:fbdae7e6d805 | 107 | |
borlanic | 0:fbdae7e6d805 | 108 | int ChainingBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size) |
borlanic | 0:fbdae7e6d805 | 109 | { |
borlanic | 0:fbdae7e6d805 | 110 | MBED_ASSERT(is_valid_read(addr, size)); |
borlanic | 0:fbdae7e6d805 | 111 | uint8_t *buffer = static_cast<uint8_t*>(b); |
borlanic | 0:fbdae7e6d805 | 112 | |
borlanic | 0:fbdae7e6d805 | 113 | // Find block devices containing blocks, may span multiple block devices |
borlanic | 0:fbdae7e6d805 | 114 | for (size_t i = 0; i < _bd_count && size > 0; i++) { |
borlanic | 0:fbdae7e6d805 | 115 | bd_size_t bdsize = _bds[i]->size(); |
borlanic | 0:fbdae7e6d805 | 116 | |
borlanic | 0:fbdae7e6d805 | 117 | if (addr < bdsize) { |
borlanic | 0:fbdae7e6d805 | 118 | bd_size_t read = size; |
borlanic | 0:fbdae7e6d805 | 119 | if (addr + read > bdsize) { |
borlanic | 0:fbdae7e6d805 | 120 | read = bdsize - addr; |
borlanic | 0:fbdae7e6d805 | 121 | } |
borlanic | 0:fbdae7e6d805 | 122 | |
borlanic | 0:fbdae7e6d805 | 123 | int err = _bds[i]->read(buffer, addr, read); |
borlanic | 0:fbdae7e6d805 | 124 | if (err) { |
borlanic | 0:fbdae7e6d805 | 125 | return err; |
borlanic | 0:fbdae7e6d805 | 126 | } |
borlanic | 0:fbdae7e6d805 | 127 | |
borlanic | 0:fbdae7e6d805 | 128 | buffer += read; |
borlanic | 0:fbdae7e6d805 | 129 | addr += read; |
borlanic | 0:fbdae7e6d805 | 130 | size -= read; |
borlanic | 0:fbdae7e6d805 | 131 | } |
borlanic | 0:fbdae7e6d805 | 132 | |
borlanic | 0:fbdae7e6d805 | 133 | addr -= bdsize; |
borlanic | 0:fbdae7e6d805 | 134 | } |
borlanic | 0:fbdae7e6d805 | 135 | |
borlanic | 0:fbdae7e6d805 | 136 | return 0; |
borlanic | 0:fbdae7e6d805 | 137 | } |
borlanic | 0:fbdae7e6d805 | 138 | |
borlanic | 0:fbdae7e6d805 | 139 | int ChainingBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size) |
borlanic | 0:fbdae7e6d805 | 140 | { |
borlanic | 0:fbdae7e6d805 | 141 | MBED_ASSERT(is_valid_program(addr, size)); |
borlanic | 0:fbdae7e6d805 | 142 | const uint8_t *buffer = static_cast<const uint8_t*>(b); |
borlanic | 0:fbdae7e6d805 | 143 | |
borlanic | 0:fbdae7e6d805 | 144 | // Find block devices containing blocks, may span multiple block devices |
borlanic | 0:fbdae7e6d805 | 145 | for (size_t i = 0; i < _bd_count && size > 0; i++) { |
borlanic | 0:fbdae7e6d805 | 146 | bd_size_t bdsize = _bds[i]->size(); |
borlanic | 0:fbdae7e6d805 | 147 | |
borlanic | 0:fbdae7e6d805 | 148 | if (addr < bdsize) { |
borlanic | 0:fbdae7e6d805 | 149 | bd_size_t program = size; |
borlanic | 0:fbdae7e6d805 | 150 | if (addr + program > bdsize) { |
borlanic | 0:fbdae7e6d805 | 151 | program = bdsize - addr; |
borlanic | 0:fbdae7e6d805 | 152 | } |
borlanic | 0:fbdae7e6d805 | 153 | |
borlanic | 0:fbdae7e6d805 | 154 | int err = _bds[i]->program(buffer, addr, program); |
borlanic | 0:fbdae7e6d805 | 155 | if (err) { |
borlanic | 0:fbdae7e6d805 | 156 | return err; |
borlanic | 0:fbdae7e6d805 | 157 | } |
borlanic | 0:fbdae7e6d805 | 158 | |
borlanic | 0:fbdae7e6d805 | 159 | buffer += program; |
borlanic | 0:fbdae7e6d805 | 160 | addr += program; |
borlanic | 0:fbdae7e6d805 | 161 | size -= program; |
borlanic | 0:fbdae7e6d805 | 162 | } |
borlanic | 0:fbdae7e6d805 | 163 | |
borlanic | 0:fbdae7e6d805 | 164 | addr -= bdsize; |
borlanic | 0:fbdae7e6d805 | 165 | } |
borlanic | 0:fbdae7e6d805 | 166 | |
borlanic | 0:fbdae7e6d805 | 167 | return 0; |
borlanic | 0:fbdae7e6d805 | 168 | } |
borlanic | 0:fbdae7e6d805 | 169 | |
borlanic | 0:fbdae7e6d805 | 170 | int ChainingBlockDevice::erase(bd_addr_t addr, bd_size_t size) |
borlanic | 0:fbdae7e6d805 | 171 | { |
borlanic | 0:fbdae7e6d805 | 172 | MBED_ASSERT(is_valid_erase(addr, size)); |
borlanic | 0:fbdae7e6d805 | 173 | |
borlanic | 0:fbdae7e6d805 | 174 | // Find block devices containing blocks, may span multiple block devices |
borlanic | 0:fbdae7e6d805 | 175 | for (size_t i = 0; i < _bd_count && size > 0; i++) { |
borlanic | 0:fbdae7e6d805 | 176 | bd_size_t bdsize = _bds[i]->size(); |
borlanic | 0:fbdae7e6d805 | 177 | |
borlanic | 0:fbdae7e6d805 | 178 | if (addr < bdsize) { |
borlanic | 0:fbdae7e6d805 | 179 | bd_size_t erase = size; |
borlanic | 0:fbdae7e6d805 | 180 | if (addr + erase > bdsize) { |
borlanic | 0:fbdae7e6d805 | 181 | erase = bdsize - addr; |
borlanic | 0:fbdae7e6d805 | 182 | } |
borlanic | 0:fbdae7e6d805 | 183 | |
borlanic | 0:fbdae7e6d805 | 184 | int err = _bds[i]->erase(addr, erase); |
borlanic | 0:fbdae7e6d805 | 185 | if (err) { |
borlanic | 0:fbdae7e6d805 | 186 | return err; |
borlanic | 0:fbdae7e6d805 | 187 | } |
borlanic | 0:fbdae7e6d805 | 188 | |
borlanic | 0:fbdae7e6d805 | 189 | addr += erase; |
borlanic | 0:fbdae7e6d805 | 190 | size -= erase; |
borlanic | 0:fbdae7e6d805 | 191 | } |
borlanic | 0:fbdae7e6d805 | 192 | |
borlanic | 0:fbdae7e6d805 | 193 | addr -= bdsize; |
borlanic | 0:fbdae7e6d805 | 194 | } |
borlanic | 0:fbdae7e6d805 | 195 | |
borlanic | 0:fbdae7e6d805 | 196 | return 0; |
borlanic | 0:fbdae7e6d805 | 197 | } |
borlanic | 0:fbdae7e6d805 | 198 | |
borlanic | 0:fbdae7e6d805 | 199 | bd_size_t ChainingBlockDevice::get_read_size() const |
borlanic | 0:fbdae7e6d805 | 200 | { |
borlanic | 0:fbdae7e6d805 | 201 | return _read_size; |
borlanic | 0:fbdae7e6d805 | 202 | } |
borlanic | 0:fbdae7e6d805 | 203 | |
borlanic | 0:fbdae7e6d805 | 204 | bd_size_t ChainingBlockDevice::get_program_size() const |
borlanic | 0:fbdae7e6d805 | 205 | { |
borlanic | 0:fbdae7e6d805 | 206 | return _program_size; |
borlanic | 0:fbdae7e6d805 | 207 | } |
borlanic | 0:fbdae7e6d805 | 208 | |
borlanic | 0:fbdae7e6d805 | 209 | bd_size_t ChainingBlockDevice::get_erase_size() const |
borlanic | 0:fbdae7e6d805 | 210 | { |
borlanic | 0:fbdae7e6d805 | 211 | return _erase_size; |
borlanic | 0:fbdae7e6d805 | 212 | } |
borlanic | 0:fbdae7e6d805 | 213 | |
borlanic | 0:fbdae7e6d805 | 214 | bd_size_t ChainingBlockDevice::get_erase_size(bd_addr_t addr) const |
borlanic | 0:fbdae7e6d805 | 215 | { |
borlanic | 0:fbdae7e6d805 | 216 | bd_addr_t bd_start_addr = 0; |
borlanic | 0:fbdae7e6d805 | 217 | for (size_t i = 0; i < _bd_count; i++) { |
borlanic | 0:fbdae7e6d805 | 218 | bd_size_t bdsize = _bds[i]->size(); |
borlanic | 0:fbdae7e6d805 | 219 | if (addr < (bd_start_addr + bdsize)) { |
borlanic | 0:fbdae7e6d805 | 220 | return _bds[i]->get_erase_size(addr - bd_start_addr); |
borlanic | 0:fbdae7e6d805 | 221 | } |
borlanic | 0:fbdae7e6d805 | 222 | bd_start_addr += bdsize; |
borlanic | 0:fbdae7e6d805 | 223 | } |
borlanic | 0:fbdae7e6d805 | 224 | |
borlanic | 0:fbdae7e6d805 | 225 | // Getting here implies an illegal address |
borlanic | 0:fbdae7e6d805 | 226 | MBED_ASSERT(0); |
borlanic | 0:fbdae7e6d805 | 227 | return 0; // satisfy compiler |
borlanic | 0:fbdae7e6d805 | 228 | } |
borlanic | 0:fbdae7e6d805 | 229 | |
borlanic | 0:fbdae7e6d805 | 230 | int ChainingBlockDevice::get_erase_value() const |
borlanic | 0:fbdae7e6d805 | 231 | { |
borlanic | 0:fbdae7e6d805 | 232 | return _erase_value; |
borlanic | 0:fbdae7e6d805 | 233 | } |
borlanic | 0:fbdae7e6d805 | 234 | |
borlanic | 0:fbdae7e6d805 | 235 | bd_size_t ChainingBlockDevice::size() const |
borlanic | 0:fbdae7e6d805 | 236 | { |
borlanic | 0:fbdae7e6d805 | 237 | return _size; |
borlanic | 0:fbdae7e6d805 | 238 | } |