BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

UserRevisionLine numberNew 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 "MBRBlockDevice.h"
borlanic 0:fbdae7e6d805 18 #include <algorithm>
borlanic 0:fbdae7e6d805 19
borlanic 0:fbdae7e6d805 20
borlanic 0:fbdae7e6d805 21 // On disk structures, all entries are little endian
borlanic 0:fbdae7e6d805 22 MBED_PACKED(struct) mbr_entry {
borlanic 0:fbdae7e6d805 23 uint8_t status;
borlanic 0:fbdae7e6d805 24 uint8_t chs_start[3];
borlanic 0:fbdae7e6d805 25 uint8_t type;
borlanic 0:fbdae7e6d805 26 uint8_t chs_stop[3];
borlanic 0:fbdae7e6d805 27 uint32_t lba_offset;
borlanic 0:fbdae7e6d805 28 uint32_t lba_size;
borlanic 0:fbdae7e6d805 29 };
borlanic 0:fbdae7e6d805 30
borlanic 0:fbdae7e6d805 31 MBED_PACKED(struct) mbr_table {
borlanic 0:fbdae7e6d805 32 struct mbr_entry entries[4];
borlanic 0:fbdae7e6d805 33 uint8_t signature[2];
borlanic 0:fbdae7e6d805 34 };
borlanic 0:fbdae7e6d805 35
borlanic 0:fbdae7e6d805 36 // Little-endian conversion, should compile to noop
borlanic 0:fbdae7e6d805 37 // if system is little-endian
borlanic 0:fbdae7e6d805 38 static inline uint32_t tole32(uint32_t a)
borlanic 0:fbdae7e6d805 39 {
borlanic 0:fbdae7e6d805 40 union {
borlanic 0:fbdae7e6d805 41 uint32_t u32;
borlanic 0:fbdae7e6d805 42 uint8_t u8[4];
borlanic 0:fbdae7e6d805 43 } w;
borlanic 0:fbdae7e6d805 44
borlanic 0:fbdae7e6d805 45 w.u8[0] = a >> 0;
borlanic 0:fbdae7e6d805 46 w.u8[1] = a >> 8;
borlanic 0:fbdae7e6d805 47 w.u8[2] = a >> 16;
borlanic 0:fbdae7e6d805 48 w.u8[3] = a >> 24;
borlanic 0:fbdae7e6d805 49
borlanic 0:fbdae7e6d805 50 return w.u32;
borlanic 0:fbdae7e6d805 51 }
borlanic 0:fbdae7e6d805 52
borlanic 0:fbdae7e6d805 53 static inline uint32_t fromle32(uint32_t a)
borlanic 0:fbdae7e6d805 54 {
borlanic 0:fbdae7e6d805 55 return tole32(a);
borlanic 0:fbdae7e6d805 56 }
borlanic 0:fbdae7e6d805 57
borlanic 0:fbdae7e6d805 58 static void tochs(uint32_t lba, uint8_t chs[3])
borlanic 0:fbdae7e6d805 59 {
borlanic 0:fbdae7e6d805 60 uint32_t sector = std::min<uint32_t>(lba, 0xfffffd)+1;
borlanic 0:fbdae7e6d805 61 chs[0] = (sector >> 6) & 0xff;
borlanic 0:fbdae7e6d805 62 chs[1] = ((sector >> 0) & 0x3f) | ((sector >> 16) & 0xc0);
borlanic 0:fbdae7e6d805 63 chs[2] = (sector >> 14) & 0xff;
borlanic 0:fbdae7e6d805 64 }
borlanic 0:fbdae7e6d805 65
borlanic 0:fbdae7e6d805 66
borlanic 0:fbdae7e6d805 67 // Partition after address are turned into absolute
borlanic 0:fbdae7e6d805 68 // addresses, assumes bd is initialized
borlanic 0:fbdae7e6d805 69 static int partition_absolute(
borlanic 0:fbdae7e6d805 70 BlockDevice *bd, int part, uint8_t type,
borlanic 0:fbdae7e6d805 71 bd_size_t offset, bd_size_t size)
borlanic 0:fbdae7e6d805 72 {
borlanic 0:fbdae7e6d805 73 // Allocate smallest buffer necessary to write MBR
borlanic 0:fbdae7e6d805 74 uint32_t buffer_size = std::max<uint32_t>(bd->get_program_size(), sizeof(struct mbr_table));
borlanic 0:fbdae7e6d805 75 uint8_t *buffer = new uint8_t[buffer_size];
borlanic 0:fbdae7e6d805 76
borlanic 0:fbdae7e6d805 77 // Check for existing MBR
borlanic 0:fbdae7e6d805 78 int err = bd->read(buffer, 512-buffer_size, buffer_size);
borlanic 0:fbdae7e6d805 79 if (err) {
borlanic 0:fbdae7e6d805 80 delete[] buffer;
borlanic 0:fbdae7e6d805 81 return err;
borlanic 0:fbdae7e6d805 82 }
borlanic 0:fbdae7e6d805 83
borlanic 0:fbdae7e6d805 84 struct mbr_table *table = reinterpret_cast<struct mbr_table*>(
borlanic 0:fbdae7e6d805 85 &buffer[buffer_size - sizeof(struct mbr_table)]);
borlanic 0:fbdae7e6d805 86 if (table->signature[0] != 0x55 || table->signature[1] != 0xaa) {
borlanic 0:fbdae7e6d805 87 // Setup default values for MBR
borlanic 0:fbdae7e6d805 88 table->signature[0] = 0x55;
borlanic 0:fbdae7e6d805 89 table->signature[1] = 0xaa;
borlanic 0:fbdae7e6d805 90 memset(table->entries, 0, sizeof(table->entries));
borlanic 0:fbdae7e6d805 91 }
borlanic 0:fbdae7e6d805 92
borlanic 0:fbdae7e6d805 93 // Setup new partition
borlanic 0:fbdae7e6d805 94 MBED_ASSERT(part >= 1 && part <= 4);
borlanic 0:fbdae7e6d805 95 table->entries[part-1].status = 0x00; // inactive (not bootable)
borlanic 0:fbdae7e6d805 96 table->entries[part-1].type = type;
borlanic 0:fbdae7e6d805 97
borlanic 0:fbdae7e6d805 98 // lba dimensions
borlanic 0:fbdae7e6d805 99 MBED_ASSERT(bd->is_valid_erase(offset, size));
borlanic 0:fbdae7e6d805 100 uint32_t sector = std::max<uint32_t>(bd->get_erase_size(), 512);
borlanic 0:fbdae7e6d805 101 uint32_t lba_offset = offset / sector;
borlanic 0:fbdae7e6d805 102 uint32_t lba_size = size / sector;
borlanic 0:fbdae7e6d805 103 table->entries[part-1].lba_offset = tole32(lba_offset);
borlanic 0:fbdae7e6d805 104 table->entries[part-1].lba_size = tole32(lba_size);
borlanic 0:fbdae7e6d805 105
borlanic 0:fbdae7e6d805 106 // chs dimensions
borlanic 0:fbdae7e6d805 107 tochs(lba_offset, table->entries[part-1].chs_start);
borlanic 0:fbdae7e6d805 108 tochs(lba_offset+lba_size-1, table->entries[part-1].chs_stop);
borlanic 0:fbdae7e6d805 109
borlanic 0:fbdae7e6d805 110 // Check that we don't overlap other entries
borlanic 0:fbdae7e6d805 111 for (int i = 1; i <= 4; i++) {
borlanic 0:fbdae7e6d805 112 if (i != part && table->entries[i-1].type != 0x00) {
borlanic 0:fbdae7e6d805 113 uint32_t neighbor_lba_offset = fromle32(table->entries[i-1].lba_offset);
borlanic 0:fbdae7e6d805 114 uint32_t neighbor_lba_size = fromle32(table->entries[i-1].lba_size);
borlanic 0:fbdae7e6d805 115 MBED_ASSERT(
borlanic 0:fbdae7e6d805 116 (lba_offset >= neighbor_lba_offset + neighbor_lba_size) ||
borlanic 0:fbdae7e6d805 117 (lba_offset + lba_size <= neighbor_lba_offset));
borlanic 0:fbdae7e6d805 118 (void)neighbor_lba_offset;
borlanic 0:fbdae7e6d805 119 (void)neighbor_lba_size;
borlanic 0:fbdae7e6d805 120 }
borlanic 0:fbdae7e6d805 121 }
borlanic 0:fbdae7e6d805 122
borlanic 0:fbdae7e6d805 123 // Write out MBR
borlanic 0:fbdae7e6d805 124 err = bd->erase(0, bd->get_erase_size());
borlanic 0:fbdae7e6d805 125 if (err) {
borlanic 0:fbdae7e6d805 126 delete[] buffer;
borlanic 0:fbdae7e6d805 127 return err;
borlanic 0:fbdae7e6d805 128 }
borlanic 0:fbdae7e6d805 129
borlanic 0:fbdae7e6d805 130 err = bd->program(buffer, 512-buffer_size, buffer_size);
borlanic 0:fbdae7e6d805 131 delete[] buffer;
borlanic 0:fbdae7e6d805 132 return err;
borlanic 0:fbdae7e6d805 133 }
borlanic 0:fbdae7e6d805 134
borlanic 0:fbdae7e6d805 135 int MBRBlockDevice::partition(BlockDevice *bd, int part, uint8_t type, bd_addr_t start)
borlanic 0:fbdae7e6d805 136 {
borlanic 0:fbdae7e6d805 137 int err = bd->init();
borlanic 0:fbdae7e6d805 138 if (err) {
borlanic 0:fbdae7e6d805 139 return err;
borlanic 0:fbdae7e6d805 140 }
borlanic 0:fbdae7e6d805 141
borlanic 0:fbdae7e6d805 142 // Calculate dimensions
borlanic 0:fbdae7e6d805 143 bd_size_t offset = ((int64_t)start < 0) ? -start : start;
borlanic 0:fbdae7e6d805 144 bd_size_t size = bd->size();
borlanic 0:fbdae7e6d805 145
borlanic 0:fbdae7e6d805 146 if (offset < 512) {
borlanic 0:fbdae7e6d805 147 offset += std::max<uint32_t>(bd->get_erase_size(), 512);
borlanic 0:fbdae7e6d805 148 }
borlanic 0:fbdae7e6d805 149
borlanic 0:fbdae7e6d805 150 size -= offset;
borlanic 0:fbdae7e6d805 151
borlanic 0:fbdae7e6d805 152 err = partition_absolute(bd, part, type, offset, size);
borlanic 0:fbdae7e6d805 153 if (err) {
borlanic 0:fbdae7e6d805 154 return err;
borlanic 0:fbdae7e6d805 155 }
borlanic 0:fbdae7e6d805 156
borlanic 0:fbdae7e6d805 157 err = bd->deinit();
borlanic 0:fbdae7e6d805 158 if (err) {
borlanic 0:fbdae7e6d805 159 return err;
borlanic 0:fbdae7e6d805 160 }
borlanic 0:fbdae7e6d805 161
borlanic 0:fbdae7e6d805 162 return 0;
borlanic 0:fbdae7e6d805 163 }
borlanic 0:fbdae7e6d805 164
borlanic 0:fbdae7e6d805 165 int MBRBlockDevice::partition(BlockDevice *bd, int part, uint8_t type,
borlanic 0:fbdae7e6d805 166 bd_addr_t start, bd_addr_t stop)
borlanic 0:fbdae7e6d805 167 {
borlanic 0:fbdae7e6d805 168 int err = bd->init();
borlanic 0:fbdae7e6d805 169 if (err) {
borlanic 0:fbdae7e6d805 170 return err;
borlanic 0:fbdae7e6d805 171 }
borlanic 0:fbdae7e6d805 172
borlanic 0:fbdae7e6d805 173 // Calculate dimensions
borlanic 0:fbdae7e6d805 174 bd_size_t offset = ((int64_t)start < 0) ? -start : start;
borlanic 0:fbdae7e6d805 175 bd_size_t size = ((int64_t)stop < 0) ? -stop : stop;
borlanic 0:fbdae7e6d805 176
borlanic 0:fbdae7e6d805 177 if (offset < 512) {
borlanic 0:fbdae7e6d805 178 offset += std::max<uint32_t>(bd->get_erase_size(), 512);
borlanic 0:fbdae7e6d805 179 }
borlanic 0:fbdae7e6d805 180
borlanic 0:fbdae7e6d805 181 size -= offset;
borlanic 0:fbdae7e6d805 182
borlanic 0:fbdae7e6d805 183 err = partition_absolute(bd, part, type, offset, size);
borlanic 0:fbdae7e6d805 184 if (err) {
borlanic 0:fbdae7e6d805 185 return err;
borlanic 0:fbdae7e6d805 186 }
borlanic 0:fbdae7e6d805 187
borlanic 0:fbdae7e6d805 188 err = bd->deinit();
borlanic 0:fbdae7e6d805 189 if (err) {
borlanic 0:fbdae7e6d805 190 return err;
borlanic 0:fbdae7e6d805 191 }
borlanic 0:fbdae7e6d805 192
borlanic 0:fbdae7e6d805 193 return 0;
borlanic 0:fbdae7e6d805 194 }
borlanic 0:fbdae7e6d805 195
borlanic 0:fbdae7e6d805 196 MBRBlockDevice::MBRBlockDevice(BlockDevice *bd, int part)
borlanic 0:fbdae7e6d805 197 : _bd(bd), _part(part)
borlanic 0:fbdae7e6d805 198 {
borlanic 0:fbdae7e6d805 199 MBED_ASSERT(_part >= 1 && _part <= 4);
borlanic 0:fbdae7e6d805 200 }
borlanic 0:fbdae7e6d805 201
borlanic 0:fbdae7e6d805 202 int MBRBlockDevice::init()
borlanic 0:fbdae7e6d805 203 {
borlanic 0:fbdae7e6d805 204 int err = _bd->init();
borlanic 0:fbdae7e6d805 205 if (err) {
borlanic 0:fbdae7e6d805 206 return err;
borlanic 0:fbdae7e6d805 207 }
borlanic 0:fbdae7e6d805 208
borlanic 0:fbdae7e6d805 209 // Allocate smallest buffer necessary to write MBR
borlanic 0:fbdae7e6d805 210 uint32_t buffer_size = std::max<uint32_t>(_bd->get_read_size(), sizeof(struct mbr_table));
borlanic 0:fbdae7e6d805 211 uint8_t *buffer = new uint8_t[buffer_size];
borlanic 0:fbdae7e6d805 212
borlanic 0:fbdae7e6d805 213 err = _bd->read(buffer, 512-buffer_size, buffer_size);
borlanic 0:fbdae7e6d805 214 if (err) {
borlanic 0:fbdae7e6d805 215 delete[] buffer;
borlanic 0:fbdae7e6d805 216 return err;
borlanic 0:fbdae7e6d805 217 }
borlanic 0:fbdae7e6d805 218
borlanic 0:fbdae7e6d805 219 // Check for valid table
borlanic 0:fbdae7e6d805 220 struct mbr_table *table = reinterpret_cast<struct mbr_table*>(
borlanic 0:fbdae7e6d805 221 &buffer[buffer_size - sizeof(struct mbr_table)]);
borlanic 0:fbdae7e6d805 222 if (table->signature[0] != 0x55 || table->signature[1] != 0xaa) {
borlanic 0:fbdae7e6d805 223 delete[] buffer;
borlanic 0:fbdae7e6d805 224 return BD_ERROR_INVALID_MBR;
borlanic 0:fbdae7e6d805 225 }
borlanic 0:fbdae7e6d805 226
borlanic 0:fbdae7e6d805 227 // Check for valid entry
borlanic 0:fbdae7e6d805 228 // 0x00 = no entry
borlanic 0:fbdae7e6d805 229 // 0x05, 0x0f = extended partitions, currently not supported
borlanic 0:fbdae7e6d805 230 if ((table->entries[_part-1].type == 0x00 ||
borlanic 0:fbdae7e6d805 231 table->entries[_part-1].type == 0x05 ||
borlanic 0:fbdae7e6d805 232 table->entries[_part-1].type == 0x0f)) {
borlanic 0:fbdae7e6d805 233 delete[] buffer;
borlanic 0:fbdae7e6d805 234 return BD_ERROR_INVALID_PARTITION;
borlanic 0:fbdae7e6d805 235 }
borlanic 0:fbdae7e6d805 236
borlanic 0:fbdae7e6d805 237 // Get partition attributes
borlanic 0:fbdae7e6d805 238 bd_size_t sector = std::max<uint32_t>(_bd->get_erase_size(), 512);
borlanic 0:fbdae7e6d805 239 _type = table->entries[_part-1].type;
borlanic 0:fbdae7e6d805 240 _offset = fromle32(table->entries[_part-1].lba_offset) * sector;
borlanic 0:fbdae7e6d805 241 _size = fromle32(table->entries[_part-1].lba_size) * sector;
borlanic 0:fbdae7e6d805 242
borlanic 0:fbdae7e6d805 243 // Check that block addresses are valid
borlanic 0:fbdae7e6d805 244 MBED_ASSERT(_bd->is_valid_erase(_offset, _size));
borlanic 0:fbdae7e6d805 245
borlanic 0:fbdae7e6d805 246 delete[] buffer;
borlanic 0:fbdae7e6d805 247 return 0;
borlanic 0:fbdae7e6d805 248 }
borlanic 0:fbdae7e6d805 249
borlanic 0:fbdae7e6d805 250 int MBRBlockDevice::deinit()
borlanic 0:fbdae7e6d805 251 {
borlanic 0:fbdae7e6d805 252 return _bd->deinit();
borlanic 0:fbdae7e6d805 253 }
borlanic 0:fbdae7e6d805 254
borlanic 0:fbdae7e6d805 255 int MBRBlockDevice::sync()
borlanic 0:fbdae7e6d805 256 {
borlanic 0:fbdae7e6d805 257 return _bd->sync();
borlanic 0:fbdae7e6d805 258 }
borlanic 0:fbdae7e6d805 259
borlanic 0:fbdae7e6d805 260 int MBRBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size)
borlanic 0:fbdae7e6d805 261 {
borlanic 0:fbdae7e6d805 262 MBED_ASSERT(is_valid_read(addr, size));
borlanic 0:fbdae7e6d805 263 return _bd->read(b, addr + _offset, size);
borlanic 0:fbdae7e6d805 264 }
borlanic 0:fbdae7e6d805 265
borlanic 0:fbdae7e6d805 266 int MBRBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size)
borlanic 0:fbdae7e6d805 267 {
borlanic 0:fbdae7e6d805 268 MBED_ASSERT(is_valid_program(addr, size));
borlanic 0:fbdae7e6d805 269 return _bd->program(b, addr + _offset, size);
borlanic 0:fbdae7e6d805 270 }
borlanic 0:fbdae7e6d805 271
borlanic 0:fbdae7e6d805 272 int MBRBlockDevice::erase(bd_addr_t addr, bd_size_t size)
borlanic 0:fbdae7e6d805 273 {
borlanic 0:fbdae7e6d805 274 MBED_ASSERT(is_valid_erase(addr, size));
borlanic 0:fbdae7e6d805 275 return _bd->erase(addr + _offset, size);
borlanic 0:fbdae7e6d805 276 }
borlanic 0:fbdae7e6d805 277
borlanic 0:fbdae7e6d805 278 bd_size_t MBRBlockDevice::get_read_size() const
borlanic 0:fbdae7e6d805 279 {
borlanic 0:fbdae7e6d805 280 return _bd->get_read_size();
borlanic 0:fbdae7e6d805 281 }
borlanic 0:fbdae7e6d805 282
borlanic 0:fbdae7e6d805 283 bd_size_t MBRBlockDevice::get_program_size() const
borlanic 0:fbdae7e6d805 284 {
borlanic 0:fbdae7e6d805 285 return _bd->get_program_size();
borlanic 0:fbdae7e6d805 286 }
borlanic 0:fbdae7e6d805 287
borlanic 0:fbdae7e6d805 288 bd_size_t MBRBlockDevice::get_erase_size() const
borlanic 0:fbdae7e6d805 289 {
borlanic 0:fbdae7e6d805 290 return _bd->get_erase_size();
borlanic 0:fbdae7e6d805 291 }
borlanic 0:fbdae7e6d805 292
borlanic 0:fbdae7e6d805 293 bd_size_t MBRBlockDevice::get_erase_size(bd_addr_t addr) const
borlanic 0:fbdae7e6d805 294 {
borlanic 0:fbdae7e6d805 295 return _bd->get_erase_size(_offset + addr);
borlanic 0:fbdae7e6d805 296 }
borlanic 0:fbdae7e6d805 297
borlanic 0:fbdae7e6d805 298 int MBRBlockDevice::get_erase_value() const
borlanic 0:fbdae7e6d805 299 {
borlanic 0:fbdae7e6d805 300 return _bd->get_erase_value();
borlanic 0:fbdae7e6d805 301 }
borlanic 0:fbdae7e6d805 302
borlanic 0:fbdae7e6d805 303 bd_size_t MBRBlockDevice::size() const
borlanic 0:fbdae7e6d805 304 {
borlanic 0:fbdae7e6d805 305 return _size;
borlanic 0:fbdae7e6d805 306 }
borlanic 0:fbdae7e6d805 307
borlanic 0:fbdae7e6d805 308 bd_size_t MBRBlockDevice::get_partition_start() const
borlanic 0:fbdae7e6d805 309 {
borlanic 0:fbdae7e6d805 310 return _offset;
borlanic 0:fbdae7e6d805 311 }
borlanic 0:fbdae7e6d805 312
borlanic 0:fbdae7e6d805 313 bd_size_t MBRBlockDevice::get_partition_stop() const
borlanic 0:fbdae7e6d805 314 {
borlanic 0:fbdae7e6d805 315 return _offset+_size;
borlanic 0:fbdae7e6d805 316 }
borlanic 0:fbdae7e6d805 317
borlanic 0:fbdae7e6d805 318 uint8_t MBRBlockDevice::get_partition_type() const
borlanic 0:fbdae7e6d805 319 {
borlanic 0:fbdae7e6d805 320 return _type;
borlanic 0:fbdae7e6d805 321 }
borlanic 0:fbdae7e6d805 322
borlanic 0:fbdae7e6d805 323 int MBRBlockDevice::get_partition_number() const
borlanic 0:fbdae7e6d805 324 {
borlanic 0:fbdae7e6d805 325 return _part;
borlanic 0:fbdae7e6d805 326 }