Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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