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