forked from rauc/rauc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmbr.c
399 lines (328 loc) · 9.85 KB
/
mbr.c
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include <errno.h>
#include <fcntl.h>
#include <glib/gstdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <linux/hdreg.h>
#include "mbr.h"
#include "update_handler.h"
/* partition entry in MBR partition table, the system boots from */
#define BOOT_PARTITION_ENTRY 0
#define MBR_NUMBER_OF_PARTITIONS 4
#define MBR_MAGIC_NUMBER_L 0x55
#define MBR_MAGIC_NUMBER_H 0xAA
#pragma pack(push,1)
struct mbr_chs_entry {
guint8 head;
guint8 sector;
guint8 cylinder;
};
G_STATIC_ASSERT(sizeof(struct mbr_chs_entry) == 3);
struct mbr_tbl_entry {
guint8 boot_indicator;
struct mbr_chs_entry chs_start;
guint8 type;
struct mbr_chs_entry chs_end;
guint32 partition_start_le;
guint32 partition_size_le;
};
G_STATIC_ASSERT(sizeof(struct mbr_tbl_entry) == 16);
struct mbr {
guint8 bootstrap_code[440];
guint32 disk_signature_le;
guint8 unused[2];
struct mbr_tbl_entry partition_table[MBR_NUMBER_OF_PARTITIONS];
guint8 magic_number[2];
};
G_STATIC_ASSERT(sizeof(struct mbr) == 512);
#pragma pack(pop)
static guint get_sectorsize(gint fd)
{
guint sector_size;
if (ioctl(fd, BLKSSZGET, §or_size) != 0)
return 512;
return sector_size;
}
static gboolean get_number_of_sectors(gint fd, guint *sectors,
GError **error)
{
g_return_val_if_fail(sectors, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
if (ioctl(fd, BLKGETSIZE, sectors) != 0) {
g_set_error(error, R_UPDATE_ERROR, R_UPDATE_ERROR_FAILED,
"ioctl command 0x%04x failed: %s",
BLKGETSIZE, g_strerror(errno));
return FALSE;
}
return TRUE;
}
static void get_hd_geometry(gint fd, guint8 *heads, guint8 *sectors)
{
struct hd_geometry geometry;
g_return_if_fail(heads);
g_return_if_fail(sectors);
if (ioctl(fd, HDIO_GETGEO, &geometry) == 0) {
*heads = geometry.heads;
*sectors = geometry.sectors;
} else {
g_message("Failed to get disk geometry, using LBA addressing: %s",
g_strerror(errno));
*heads = 255;
*sectors = 63;
}
}
static gboolean validate_region(gint fd, guint64 start, guint64 size,
guint sector_size, GError **error)
{
gboolean res = FALSE;
guint number_of_sectors;
GError *ierror = NULL;
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
if (start < sizeof(struct mbr) || size == 0) {
g_set_error(error, R_UPDATE_ERROR, R_UPDATE_ERROR_FAILED,
"no valid configuration for region");
goto out;
}
if ((start % sector_size) != 0) {
g_set_error(error, R_UPDATE_ERROR, R_UPDATE_ERROR_FAILED,
"Region start %"G_GINT64_MODIFIER "d is not aligned to the sector-size %d",
start, sector_size);
goto out;
}
if ((size % (2 * sector_size)) != 0) {
g_set_error(error, R_UPDATE_ERROR, R_UPDATE_ERROR_FAILED,
"Region size %"G_GINT64_MODIFIER "d is not aligned to the double sector-size %d",
size, 2 * sector_size);
goto out;
}
res = get_number_of_sectors(fd, &number_of_sectors, &ierror);
if (!res) {
g_propagate_error(error, ierror);
goto out;
}
if ((start + size) >= (guint64)number_of_sectors * sector_size) {
g_set_error(error, R_UPDATE_ERROR, R_UPDATE_ERROR_FAILED,
"Region configuration is bigger than device");
res = FALSE;
goto out;
}
out:
return res;
}
static gboolean read_mbr(gint fd, struct mbr *mbr, GError **error)
{
g_return_val_if_fail(mbr, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
if (read(fd, mbr, sizeof(*mbr)) != sizeof(*mbr)) {
g_set_error(error, R_UPDATE_ERROR, R_UPDATE_ERROR_FAILED,
"Read: %s", g_strerror(errno));
return FALSE;
}
if (mbr->magic_number[0] != MBR_MAGIC_NUMBER_L ||
mbr->magic_number[1] != MBR_MAGIC_NUMBER_H) {
g_set_error(error, R_UPDATE_ERROR, R_UPDATE_ERROR_FAILED,
"No valid master boot record found");
return FALSE;
}
return TRUE;
}
static gboolean is_region_free(guint64 region_start, guint64 region_size,
const struct mbr_tbl_entry *partition_tbl, guint sector_size,
GError **error)
{
guint64 p_start, p_end;
guint i;
g_return_val_if_fail(partition_tbl, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
for (i = 0; i < MBR_NUMBER_OF_PARTITIONS; i++) {
if (i == BOOT_PARTITION_ENTRY)
continue;
p_start = (guint64)GUINT32_FROM_LE(partition_tbl[i].partition_start_le) * sector_size;
p_end = (guint64)GUINT32_FROM_LE(partition_tbl[i].partition_size_le) * sector_size +
p_start - 1;
if (region_start >= p_start && region_start <= p_end) {
g_set_error(error, R_UPDATE_ERROR, R_UPDATE_ERROR_FAILED,
"Region start address 0x%"G_GINT64_MODIFIER "x is in area of "
"partition %d (0x%"G_GINT64_MODIFIER "x - 0x%"G_GINT64_MODIFIER "x)",
region_start, i+1, p_start, p_end);
break;
}
if (p_start >= region_start &&
p_start <= region_start + region_size - 1) {
g_set_error(error, R_UPDATE_ERROR, R_UPDATE_ERROR_FAILED,
"Region end address 0x%"G_GINT64_MODIFIER "x is in area of "
"partition %d (0x%"G_GINT64_MODIFIER "x - 0x%"G_GINT64_MODIFIER "x)",
region_start + region_size - 1, i+1, p_start,
p_end);
break;
}
}
if (i < MBR_NUMBER_OF_PARTITIONS)
return FALSE;
return TRUE;
}
/**
* Calculation of the CHS value from an LBA value
*
* The 3 CHS bytes in Partition are stored with following layout:
* - 8 bits for HEAD
* - upper 2 bits for CYLINDER
* - 6 bits for SECTOR
* - lower 8 bits for CYLINDER
*/
static void get_chs(struct mbr_chs_entry *chs, guint32 lba,
guint8 heads, guint8 sectors)
{
g_return_if_fail(chs);
g_return_if_fail(heads);
g_return_if_fail(sectors);
chs->sector = lba % sectors + 1;
lba /= sectors;
chs->head = lba % heads;
lba /= heads;
chs->cylinder = lba & 0xFF;
/* Move bit 8 & 9 of cylinder to bit 6 & 7 of sector */
chs->sector |= (lba >> 2) & 0xC0;
}
static gboolean get_raw_partition_entry(gint fd,
struct mbr_tbl_entry *raw_entry,
const struct boot_switch_partition *partition, GError **error)
{
gboolean res = FALSE;
guint32 start, size;
guint sector_size;
guint8 heads, sectors;
g_return_val_if_fail(raw_entry, FALSE);
g_return_val_if_fail(partition, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
sector_size = get_sectorsize(fd);
if (partition->start % sector_size || partition->size % sector_size) {
g_set_error(error, R_UPDATE_ERROR, R_UPDATE_ERROR_FAILED,
"Partition start address or size is not a multiple"
" of sector size %d", sector_size);
goto out;
}
start = partition->start / sector_size;
size = partition->size / sector_size;
raw_entry->partition_start_le = GUINT32_TO_LE(start);
raw_entry->partition_size_le = GUINT32_TO_LE(size);
get_hd_geometry(fd, &heads, §ors);
get_chs(&raw_entry->chs_start, start, heads, sectors);
get_chs(&raw_entry->chs_end, start + size - 1, heads, sectors);
res = TRUE;
out:
return res;
}
gboolean r_mbr_switch_get_inactive_partition(const gchar *device,
struct boot_switch_partition *partition,
guint64 region_start, guint64 region_size,
GError **error)
{
gboolean res = FALSE;
struct mbr mbr;
GError *ierror = NULL;
struct mbr_tbl_entry *boot_part;
guint sector_size;
gint fd;
g_return_val_if_fail(device, FALSE);
g_return_val_if_fail(partition, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
fd = g_open(device, O_RDONLY);
sector_size = get_sectorsize(fd);
res = validate_region(fd, region_start, region_size, sector_size, &ierror);
if (!res) {
g_propagate_error(error, ierror);
goto out;
}
res = read_mbr(fd, &mbr, &ierror);
if (!res) {
g_propagate_prefixed_error(error, ierror,
"Failed to read MBR:");
goto out;
}
/* check if region overlaps with any partition */
res = is_region_free(region_start, region_size, mbr.partition_table,
sector_size, &ierror);
if (!res) {
g_propagate_error(error, ierror);
goto out;
}
res = FALSE;
boot_part = &mbr.partition_table[BOOT_PARTITION_ENTRY];
if (GUINT32_FROM_LE(boot_part->partition_start_le) == 0) {
g_set_error(error, R_UPDATE_ERROR, R_UPDATE_ERROR_FAILED,
"No boot partition found in entry %d",
BOOT_PARTITION_ENTRY);
goto out;
}
if ((region_start / sector_size) ==
(guint64)GUINT32_FROM_LE(boot_part->partition_start_le)) {
partition->start = region_start + region_size / 2;
} else if (((region_start + region_size / 2) / sector_size) ==
(guint64)GUINT32_FROM_LE(boot_part->partition_start_le)) {
partition->start = region_start;
} else {
g_set_error(error, R_UPDATE_ERROR, R_UPDATE_ERROR_FAILED,
"Boot partition's start address does not match "
"region configuration");
goto out;
}
partition->size = region_size / 2;
res = TRUE;
out:
if (fd >= 0)
g_close(fd, NULL);
return res;
}
gboolean r_mbr_switch_set_boot_partition(const gchar *device,
const struct boot_switch_partition *partition,
GError **error)
{
gboolean res = FALSE;
struct mbr mbr;
struct mbr_tbl_entry *boot_part;
GError *ierror = NULL;
gint fd;
g_return_val_if_fail(device, FALSE);
g_return_val_if_fail(partition, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
fd = g_open(device, O_RDWR);
if (fd == -1) {
g_set_error(error, R_UPDATE_ERROR, R_UPDATE_ERROR_FAILED,
"Opening device failed: %s",
g_strerror(errno));
goto out;
}
res = read_mbr(fd, &mbr, &ierror);
if (!res) {
g_propagate_prefixed_error(error, ierror,
"Failed to read MBR:");
goto out;
}
boot_part = &mbr.partition_table[BOOT_PARTITION_ENTRY];
res = get_raw_partition_entry(fd, boot_part, partition, &ierror);
if (!res) {
g_propagate_prefixed_error(error, ierror,
"Failed to create new partition entry:");
goto out;
}
if (lseek(fd, 0, SEEK_SET) != 0) {
g_set_error(error, R_UPDATE_ERROR, R_UPDATE_ERROR_FAILED,
"Failed to seek to position 0");
res = FALSE;
goto out;
}
if (write(fd, &mbr, sizeof(mbr)) != sizeof(mbr)) {
g_set_error(error, R_UPDATE_ERROR, R_UPDATE_ERROR_FAILED,
"Could not write new MBR: %s",
g_strerror(errno));
res = FALSE;
goto out;
}
res = TRUE;
out:
if (fd >= 0)
g_close(fd, NULL);
return res;
}