Erste version der Software für der Prototyp

Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

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