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