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