1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2007, 2008 Marcel Moolenaar
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/param.h>
30 #include <sys/bio.h>
31 #include <sys/diskmbr.h>
32 #include <sys/endian.h>
33 #include <sys/kernel.h>
34 #include <sys/kobj.h>
35 #include <sys/limits.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/queue.h>
40 #include <sys/sbuf.h>
41 #include <sys/systm.h>
42 #include <sys/sysctl.h>
43 #include <geom/geom.h>
44 #include <geom/geom_int.h>
45 #include <geom/part/g_part.h>
46
47 #include "g_part_if.h"
48
49 FEATURE(geom_part_mbr, "GEOM partitioning class for MBR support");
50
51 SYSCTL_DECL(_kern_geom_part);
52 static SYSCTL_NODE(_kern_geom_part, OID_AUTO, mbr,
53 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
54 "GEOM_PART_MBR Master Boot Record");
55
56 static u_int enforce_chs = 0;
57 SYSCTL_UINT(_kern_geom_part_mbr, OID_AUTO, enforce_chs,
58 CTLFLAG_RWTUN, &enforce_chs, 0, "Enforce alignment to CHS addressing");
59
60 #define MBRSIZE 512
61
62 struct g_part_mbr_table {
63 struct g_part_table base;
64 u_char mbr[MBRSIZE];
65 };
66
67 struct g_part_mbr_entry {
68 struct g_part_entry base;
69 struct dos_partition ent;
70 };
71
72 static int g_part_mbr_add(struct g_part_table *, struct g_part_entry *,
73 struct g_part_parms *);
74 static int g_part_mbr_bootcode(struct g_part_table *, struct g_part_parms *);
75 static int g_part_mbr_create(struct g_part_table *, struct g_part_parms *);
76 static int g_part_mbr_destroy(struct g_part_table *, struct g_part_parms *);
77 static void g_part_mbr_dumpconf(struct g_part_table *, struct g_part_entry *,
78 struct sbuf *, const char *);
79 static int g_part_mbr_dumpto(struct g_part_table *, struct g_part_entry *);
80 static int g_part_mbr_modify(struct g_part_table *, struct g_part_entry *,
81 struct g_part_parms *);
82 static const char *g_part_mbr_name(struct g_part_table *, struct g_part_entry *,
83 char *, size_t);
84 static int g_part_mbr_probe(struct g_part_table *, struct g_consumer *);
85 static int g_part_mbr_read(struct g_part_table *, struct g_consumer *);
86 static int g_part_mbr_setunset(struct g_part_table *, struct g_part_entry *,
87 const char *, unsigned int);
88 static const char *g_part_mbr_type(struct g_part_table *, struct g_part_entry *,
89 char *, size_t);
90 static int g_part_mbr_write(struct g_part_table *, struct g_consumer *);
91 static int g_part_mbr_resize(struct g_part_table *, struct g_part_entry *,
92 struct g_part_parms *);
93
94 static kobj_method_t g_part_mbr_methods[] = {
95 KOBJMETHOD(g_part_add, g_part_mbr_add),
96 KOBJMETHOD(g_part_bootcode, g_part_mbr_bootcode),
97 KOBJMETHOD(g_part_create, g_part_mbr_create),
98 KOBJMETHOD(g_part_destroy, g_part_mbr_destroy),
99 KOBJMETHOD(g_part_dumpconf, g_part_mbr_dumpconf),
100 KOBJMETHOD(g_part_dumpto, g_part_mbr_dumpto),
101 KOBJMETHOD(g_part_modify, g_part_mbr_modify),
102 KOBJMETHOD(g_part_resize, g_part_mbr_resize),
103 KOBJMETHOD(g_part_name, g_part_mbr_name),
104 KOBJMETHOD(g_part_probe, g_part_mbr_probe),
105 KOBJMETHOD(g_part_read, g_part_mbr_read),
106 KOBJMETHOD(g_part_setunset, g_part_mbr_setunset),
107 KOBJMETHOD(g_part_type, g_part_mbr_type),
108 KOBJMETHOD(g_part_write, g_part_mbr_write),
109 { 0, 0 }
110 };
111
112 static struct g_part_scheme g_part_mbr_scheme = {
113 "MBR",
114 g_part_mbr_methods,
115 sizeof(struct g_part_mbr_table),
116 .gps_entrysz = sizeof(struct g_part_mbr_entry),
117 .gps_minent = NDOSPART,
118 .gps_maxent = NDOSPART,
119 .gps_bootcodesz = MBRSIZE,
120 };
121 G_PART_SCHEME_DECLARE(g_part_mbr);
122 MODULE_VERSION(geom_part_mbr, 0);
123
124 static struct g_part_mbr_alias {
125 u_char typ;
126 int alias;
127 } mbr_alias_match[] = {
128 { DOSPTYP_386BSD, G_PART_ALIAS_FREEBSD },
129 { DOSPTYP_APPLE_BOOT, G_PART_ALIAS_APPLE_BOOT },
130 { DOSPTYP_APPLE_UFS, G_PART_ALIAS_APPLE_UFS },
131 { DOSPTYP_EFI, G_PART_ALIAS_EFI },
132 { DOSPTYP_EXT, G_PART_ALIAS_EBR },
133 { DOSPTYP_EXTLBA, G_PART_ALIAS_EBR },
134 { DOSPTYP_FAT16, G_PART_ALIAS_MS_FAT16 },
135 { DOSPTYP_FAT32, G_PART_ALIAS_MS_FAT32 },
136 { DOSPTYP_FAT32LBA, G_PART_ALIAS_MS_FAT32LBA },
137 { DOSPTYP_HFS, G_PART_ALIAS_APPLE_HFS },
138 { DOSPTYP_LDM, G_PART_ALIAS_MS_LDM_DATA },
139 { DOSPTYP_LINLVM, G_PART_ALIAS_LINUX_LVM },
140 { DOSPTYP_LINRAID, G_PART_ALIAS_LINUX_RAID },
141 { DOSPTYP_LINSWP, G_PART_ALIAS_LINUX_SWAP },
142 { DOSPTYP_LINUX, G_PART_ALIAS_LINUX_DATA },
143 { DOSPTYP_NTFS, G_PART_ALIAS_MS_NTFS },
144 { DOSPTYP_PPCBOOT, G_PART_ALIAS_PREP_BOOT },
145 { DOSPTYP_VMFS, G_PART_ALIAS_VMFS },
146 { DOSPTYP_VMKDIAG, G_PART_ALIAS_VMKDIAG },
147 };
148
149 static int
mbr_parse_type(const char * type,u_char * dp_typ)150 mbr_parse_type(const char *type, u_char *dp_typ)
151 {
152 const char *alias;
153 char *endp;
154 long lt;
155 int i;
156
157 if (type[0] == '!') {
158 lt = strtol(type + 1, &endp, 0);
159 if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256)
160 return (EINVAL);
161 *dp_typ = (u_char)lt;
162 return (0);
163 }
164 for (i = 0; i < nitems(mbr_alias_match); i++) {
165 alias = g_part_alias_name(mbr_alias_match[i].alias);
166 if (strcasecmp(type, alias) == 0) {
167 *dp_typ = mbr_alias_match[i].typ;
168 return (0);
169 }
170 }
171 return (EINVAL);
172 }
173
174 static int
mbr_probe_bpb(u_char * bpb)175 mbr_probe_bpb(u_char *bpb)
176 {
177 uint16_t secsz;
178 uint8_t clstsz;
179
180 #define PO2(x) ((x & (x - 1)) == 0)
181 secsz = le16dec(bpb);
182 if (secsz < 512 || secsz > 4096 || !PO2(secsz))
183 return (0);
184 clstsz = bpb[2];
185 if (clstsz < 1 || clstsz > 128 || !PO2(clstsz))
186 return (0);
187 #undef PO2
188
189 return (1);
190 }
191
192 static void
mbr_set_chs(struct g_part_table * table,uint32_t lba,u_char * cylp,u_char * hdp,u_char * secp)193 mbr_set_chs(struct g_part_table *table, uint32_t lba, u_char *cylp, u_char *hdp,
194 u_char *secp)
195 {
196 uint32_t cyl, hd, sec;
197
198 sec = lba % table->gpt_sectors + 1;
199 lba /= table->gpt_sectors;
200 hd = lba % table->gpt_heads;
201 lba /= table->gpt_heads;
202 cyl = lba;
203 if (cyl > 1023)
204 sec = hd = cyl = ~0;
205
206 *cylp = cyl & 0xff;
207 *hdp = hd & 0xff;
208 *secp = (sec & 0x3f) | ((cyl >> 2) & 0xc0);
209 }
210
211 static int
mbr_align(struct g_part_table * basetable,uint32_t * start,uint32_t * size)212 mbr_align(struct g_part_table *basetable, uint32_t *start, uint32_t *size)
213 {
214 uint32_t sectors;
215
216 if (enforce_chs == 0)
217 return (0);
218 sectors = basetable->gpt_sectors;
219 if (*size < sectors)
220 return (EINVAL);
221 if (start != NULL && (*start % sectors)) {
222 *size += (*start % sectors) - sectors;
223 *start -= (*start % sectors) - sectors;
224 }
225 if (*size % sectors)
226 *size -= (*size % sectors);
227 if (*size < sectors)
228 return (EINVAL);
229 return (0);
230 }
231
232 static int
g_part_mbr_add(struct g_part_table * basetable,struct g_part_entry * baseentry,struct g_part_parms * gpp)233 g_part_mbr_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
234 struct g_part_parms *gpp)
235 {
236 struct g_part_mbr_entry *entry;
237 uint32_t start, size;
238
239 if (gpp->gpp_parms & G_PART_PARM_LABEL)
240 return (EINVAL);
241
242 entry = (struct g_part_mbr_entry *)baseentry;
243 start = gpp->gpp_start;
244 size = gpp->gpp_size;
245 if (mbr_align(basetable, &start, &size) != 0)
246 return (EINVAL);
247 if (baseentry->gpe_deleted)
248 bzero(&entry->ent, sizeof(entry->ent));
249
250 KASSERT(baseentry->gpe_start <= start, ("%s", __func__));
251 KASSERT(baseentry->gpe_end >= start + size - 1, ("%s", __func__));
252 baseentry->gpe_start = start;
253 baseentry->gpe_end = start + size - 1;
254 entry->ent.dp_start = start;
255 entry->ent.dp_size = size;
256 mbr_set_chs(basetable, baseentry->gpe_start, &entry->ent.dp_scyl,
257 &entry->ent.dp_shd, &entry->ent.dp_ssect);
258 mbr_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl,
259 &entry->ent.dp_ehd, &entry->ent.dp_esect);
260 return (mbr_parse_type(gpp->gpp_type, &entry->ent.dp_typ));
261 }
262
263 static int
g_part_mbr_bootcode(struct g_part_table * basetable,struct g_part_parms * gpp)264 g_part_mbr_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
265 {
266 struct g_part_mbr_table *table;
267 uint32_t dsn;
268
269 if (gpp->gpp_codesize != MBRSIZE)
270 return (ENODEV);
271
272 table = (struct g_part_mbr_table *)basetable;
273 dsn = *(uint32_t *)(table->mbr + DOSDSNOFF);
274 bcopy(gpp->gpp_codeptr, table->mbr, DOSPARTOFF);
275 if (dsn != 0 && !gpp->gpp_skip_dsn)
276 *(uint32_t *)(table->mbr + DOSDSNOFF) = dsn;
277 return (0);
278 }
279
280 static int
g_part_mbr_create(struct g_part_table * basetable,struct g_part_parms * gpp)281 g_part_mbr_create(struct g_part_table *basetable, struct g_part_parms *gpp)
282 {
283 struct g_provider *pp;
284 struct g_part_mbr_table *table;
285
286 pp = gpp->gpp_provider;
287 if (pp->sectorsize < MBRSIZE)
288 return (ENOSPC);
289
290 basetable->gpt_first = basetable->gpt_sectors;
291 basetable->gpt_last = MIN(pp->mediasize / pp->sectorsize,
292 UINT32_MAX) - 1;
293
294 table = (struct g_part_mbr_table *)basetable;
295 le16enc(table->mbr + DOSMAGICOFFSET, DOSMAGIC);
296 return (0);
297 }
298
299 static int
g_part_mbr_destroy(struct g_part_table * basetable,struct g_part_parms * gpp)300 g_part_mbr_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
301 {
302
303 /* Wipe the first sector to clear the partitioning. */
304 basetable->gpt_smhead |= 1;
305 return (0);
306 }
307
308 static void
g_part_mbr_efimedia(struct g_part_mbr_table * table,struct g_part_mbr_entry * entry,struct sbuf * sb)309 g_part_mbr_efimedia(struct g_part_mbr_table *table, struct g_part_mbr_entry *entry,
310 struct sbuf *sb)
311 {
312 uint32_t dsn;
313
314 dsn = le32dec(table->mbr + DOSDSNOFF);
315 sbuf_printf(sb, "HD(%d,MBR,%#08x,%#jx,%#jx)",
316 entry->base.gpe_index, dsn, (intmax_t)entry->base.gpe_start,
317 (intmax_t)(entry->base.gpe_end - entry->base.gpe_start + 1));
318 }
319
320 static void
g_part_mbr_dumpconf(struct g_part_table * basetable,struct g_part_entry * baseentry,struct sbuf * sb,const char * indent)321 g_part_mbr_dumpconf(struct g_part_table *basetable, struct g_part_entry *baseentry,
322 struct sbuf *sb, const char *indent)
323 {
324 struct g_part_mbr_entry *entry;
325 struct g_part_mbr_table *table;
326
327 table = (struct g_part_mbr_table *)basetable;
328 entry = (struct g_part_mbr_entry *)baseentry;
329 if (indent == NULL) {
330 /* conftxt: libdisk compatibility */
331 sbuf_printf(sb, " xs MBR xt %u", entry->ent.dp_typ);
332 } else if (entry != NULL) {
333 /* confxml: partition entry information */
334 sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
335 entry->ent.dp_typ);
336 if (entry->ent.dp_flag & 0x80)
337 sbuf_printf(sb, "%s<attrib>active</attrib>\n", indent);
338 sbuf_printf(sb, "%s<efimedia>", indent);
339 g_part_mbr_efimedia(table, entry, sb);
340 sbuf_cat(sb, "</efimedia>\n");
341 } else {
342 /* confxml: scheme information */
343 }
344 }
345
346 static int
g_part_mbr_dumpto(struct g_part_table * table,struct g_part_entry * baseentry)347 g_part_mbr_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
348 {
349 struct g_part_mbr_entry *entry;
350
351 /* Allow dumping to a FreeBSD partition or Linux swap partition only. */
352 entry = (struct g_part_mbr_entry *)baseentry;
353 return ((entry->ent.dp_typ == DOSPTYP_386BSD ||
354 entry->ent.dp_typ == DOSPTYP_LINSWP) ? 1 : 0);
355 }
356
357 static int
g_part_mbr_modify(struct g_part_table * basetable,struct g_part_entry * baseentry,struct g_part_parms * gpp)358 g_part_mbr_modify(struct g_part_table *basetable,
359 struct g_part_entry *baseentry, struct g_part_parms *gpp)
360 {
361 struct g_part_mbr_entry *entry;
362
363 if (gpp->gpp_parms & G_PART_PARM_LABEL)
364 return (EINVAL);
365
366 entry = (struct g_part_mbr_entry *)baseentry;
367 if (gpp->gpp_parms & G_PART_PARM_TYPE)
368 return (mbr_parse_type(gpp->gpp_type, &entry->ent.dp_typ));
369 return (0);
370 }
371
372 static int
g_part_mbr_resize(struct g_part_table * basetable,struct g_part_entry * baseentry,struct g_part_parms * gpp)373 g_part_mbr_resize(struct g_part_table *basetable,
374 struct g_part_entry *baseentry, struct g_part_parms *gpp)
375 {
376 struct g_part_mbr_entry *entry;
377 struct g_provider *pp;
378 uint32_t size;
379
380 if (baseentry == NULL) {
381 pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
382 basetable->gpt_last = MIN(pp->mediasize / pp->sectorsize,
383 UINT32_MAX) - 1;
384 return (0);
385 }
386 size = gpp->gpp_size;
387 if (mbr_align(basetable, NULL, &size) != 0)
388 return (EINVAL);
389 /* XXX: prevent unexpected shrinking. */
390 pp = baseentry->gpe_pp;
391 if ((g_debugflags & G_F_FOOTSHOOTING) == 0 && size < gpp->gpp_size &&
392 pp->mediasize / pp->sectorsize > size)
393 return (EBUSY);
394 entry = (struct g_part_mbr_entry *)baseentry;
395 baseentry->gpe_end = baseentry->gpe_start + size - 1;
396 entry->ent.dp_size = size;
397 mbr_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl,
398 &entry->ent.dp_ehd, &entry->ent.dp_esect);
399 return (0);
400 }
401
402 static const char *
g_part_mbr_name(struct g_part_table * table,struct g_part_entry * baseentry,char * buf,size_t bufsz)403 g_part_mbr_name(struct g_part_table *table, struct g_part_entry *baseentry,
404 char *buf, size_t bufsz)
405 {
406
407 snprintf(buf, bufsz, "s%d", baseentry->gpe_index);
408 return (buf);
409 }
410
411 static int
g_part_mbr_probe(struct g_part_table * table,struct g_consumer * cp)412 g_part_mbr_probe(struct g_part_table *table, struct g_consumer *cp)
413 {
414 char psn[8];
415 struct g_provider *pp;
416 u_char *buf, *p;
417 int error, index, res, sum;
418 uint16_t magic;
419
420 pp = cp->provider;
421
422 /* Sanity-check the provider. */
423 if (pp->sectorsize < MBRSIZE || pp->mediasize < pp->sectorsize)
424 return (ENOSPC);
425 if (pp->sectorsize > 4096)
426 return (ENXIO);
427
428 /* We don't nest under an MBR (see EBR instead). */
429 error = g_getattr("PART::scheme", cp, &psn);
430 if (error == 0 && strcmp(psn, g_part_mbr_scheme.name) == 0)
431 return (ELOOP);
432
433 /* Check that there's a MBR. */
434 buf = g_read_data(cp, 0L, pp->sectorsize, &error);
435 if (buf == NULL)
436 return (error);
437
438 /* We goto out on mismatch. */
439 res = ENXIO;
440
441 magic = le16dec(buf + DOSMAGICOFFSET);
442 if (magic != DOSMAGIC)
443 goto out;
444
445 for (index = 0; index < NDOSPART; index++) {
446 p = buf + DOSPARTOFF + index * DOSPARTSIZE;
447 if (p[0] != 0 && p[0] != 0x80)
448 goto out;
449 }
450
451 /*
452 * If the partition table does not consist of all zeroes,
453 * assume we have a MBR. If it's all zeroes, we could have
454 * a boot sector. For example, a boot sector that doesn't
455 * have boot code -- common on non-i386 hardware. In that
456 * case we check if we have a possible BPB. If so, then we
457 * assume we have a boot sector instead.
458 */
459 sum = 0;
460 for (index = 0; index < NDOSPART * DOSPARTSIZE; index++)
461 sum += buf[DOSPARTOFF + index];
462 if (sum != 0 || !mbr_probe_bpb(buf + 0x0b))
463 res = G_PART_PROBE_PRI_NORM;
464
465 out:
466 g_free(buf);
467 return (res);
468 }
469
470 static int
g_part_mbr_read(struct g_part_table * basetable,struct g_consumer * cp)471 g_part_mbr_read(struct g_part_table *basetable, struct g_consumer *cp)
472 {
473 struct dos_partition ent;
474 struct g_provider *pp;
475 struct g_part_mbr_table *table;
476 struct g_part_mbr_entry *entry;
477 u_char *buf, *p;
478 off_t chs, msize, first;
479 u_int sectors, heads;
480 int error, index;
481
482 pp = cp->provider;
483 table = (struct g_part_mbr_table *)basetable;
484 first = basetable->gpt_sectors;
485 msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
486
487 buf = g_read_data(cp, 0L, pp->sectorsize, &error);
488 if (buf == NULL)
489 return (error);
490
491 bcopy(buf, table->mbr, sizeof(table->mbr));
492 for (index = NDOSPART - 1; index >= 0; index--) {
493 p = buf + DOSPARTOFF + index * DOSPARTSIZE;
494 ent.dp_flag = p[0];
495 ent.dp_shd = p[1];
496 ent.dp_ssect = p[2];
497 ent.dp_scyl = p[3];
498 ent.dp_typ = p[4];
499 ent.dp_ehd = p[5];
500 ent.dp_esect = p[6];
501 ent.dp_ecyl = p[7];
502 ent.dp_start = le32dec(p + 8);
503 ent.dp_size = le32dec(p + 12);
504 if (ent.dp_typ == 0 || ent.dp_typ == DOSPTYP_PMBR)
505 continue;
506 if (ent.dp_start == 0 || ent.dp_size == 0)
507 continue;
508 sectors = ent.dp_esect & 0x3f;
509 if (sectors > basetable->gpt_sectors &&
510 !basetable->gpt_fixgeom) {
511 g_part_geometry_heads(msize, sectors, &chs, &heads);
512 if (chs != 0) {
513 basetable->gpt_sectors = sectors;
514 basetable->gpt_heads = heads;
515 }
516 }
517 if (ent.dp_start < first)
518 first = ent.dp_start;
519 entry = (struct g_part_mbr_entry *)g_part_new_entry(basetable,
520 index + 1, ent.dp_start, ent.dp_start + ent.dp_size - 1);
521 entry->ent = ent;
522 }
523
524 basetable->gpt_entries = NDOSPART;
525 basetable->gpt_first = basetable->gpt_sectors;
526 basetable->gpt_last = msize - 1;
527
528 if (first < basetable->gpt_first)
529 basetable->gpt_first = 1;
530
531 g_free(buf);
532 return (0);
533 }
534
535 static int
g_part_mbr_setunset(struct g_part_table * table,struct g_part_entry * baseentry,const char * attrib,unsigned int set)536 g_part_mbr_setunset(struct g_part_table *table, struct g_part_entry *baseentry,
537 const char *attrib, unsigned int set)
538 {
539 struct g_part_entry *iter;
540 struct g_part_mbr_entry *entry;
541 int changed;
542
543 if (baseentry == NULL)
544 return (ENODEV);
545 if (strcasecmp(attrib, "active") != 0)
546 return (EINVAL);
547
548 /* Only one entry can have the active attribute. */
549 LIST_FOREACH(iter, &table->gpt_entry, gpe_entry) {
550 if (iter->gpe_deleted)
551 continue;
552 changed = 0;
553 entry = (struct g_part_mbr_entry *)iter;
554 if (iter == baseentry) {
555 if (set && (entry->ent.dp_flag & 0x80) == 0) {
556 entry->ent.dp_flag |= 0x80;
557 changed = 1;
558 } else if (!set && (entry->ent.dp_flag & 0x80)) {
559 entry->ent.dp_flag &= ~0x80;
560 changed = 1;
561 }
562 } else {
563 if (set && (entry->ent.dp_flag & 0x80)) {
564 entry->ent.dp_flag &= ~0x80;
565 changed = 1;
566 }
567 }
568 if (changed && !iter->gpe_created)
569 iter->gpe_modified = 1;
570 }
571 return (0);
572 }
573
574 static const char *
g_part_mbr_type(struct g_part_table * basetable,struct g_part_entry * baseentry,char * buf,size_t bufsz)575 g_part_mbr_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
576 char *buf, size_t bufsz)
577 {
578 struct g_part_mbr_entry *entry;
579 int i;
580
581 entry = (struct g_part_mbr_entry *)baseentry;
582 for (i = 0; i < nitems(mbr_alias_match); i++) {
583 if (mbr_alias_match[i].typ == entry->ent.dp_typ)
584 return (g_part_alias_name(mbr_alias_match[i].alias));
585 }
586 snprintf(buf, bufsz, "!%d", entry->ent.dp_typ);
587 return (buf);
588 }
589
590 static int
g_part_mbr_write(struct g_part_table * basetable,struct g_consumer * cp)591 g_part_mbr_write(struct g_part_table *basetable, struct g_consumer *cp)
592 {
593 struct g_part_entry *baseentry;
594 struct g_part_mbr_entry *entry;
595 struct g_part_mbr_table *table;
596 u_char *p;
597 int error, index;
598
599 table = (struct g_part_mbr_table *)basetable;
600 baseentry = LIST_FIRST(&basetable->gpt_entry);
601 for (index = 1; index <= basetable->gpt_entries; index++) {
602 p = table->mbr + DOSPARTOFF + (index - 1) * DOSPARTSIZE;
603 entry = (baseentry != NULL && index == baseentry->gpe_index)
604 ? (struct g_part_mbr_entry *)baseentry : NULL;
605 if (entry != NULL && !baseentry->gpe_deleted) {
606 p[0] = entry->ent.dp_flag;
607 p[1] = entry->ent.dp_shd;
608 p[2] = entry->ent.dp_ssect;
609 p[3] = entry->ent.dp_scyl;
610 p[4] = entry->ent.dp_typ;
611 p[5] = entry->ent.dp_ehd;
612 p[6] = entry->ent.dp_esect;
613 p[7] = entry->ent.dp_ecyl;
614 le32enc(p + 8, entry->ent.dp_start);
615 le32enc(p + 12, entry->ent.dp_size);
616 } else
617 bzero(p, DOSPARTSIZE);
618
619 if (entry != NULL)
620 baseentry = LIST_NEXT(baseentry, gpe_entry);
621 }
622
623 error = g_write_data(cp, 0, table->mbr, cp->provider->sectorsize);
624 return (error);
625 }
626