xref: /freebsd/sys/geom/part/g_part_mbr.c (revision f8b865d1d62d17626ab993212963277c06cc25b8)
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, CTLFLAG_RW, 0,
56     "GEOM_PART_MBR Master Boot Record");
57 
58 static u_int enforce_chs = 0;
59 SYSCTL_UINT(_kern_geom_part_mbr, OID_AUTO, enforce_chs,
60     CTLFLAG_RWTUN, &enforce_chs, 0, "Enforce alignment to CHS addressing");
61 
62 #define	MBRSIZE		512
63 
64 struct g_part_mbr_table {
65 	struct g_part_table	base;
66 	u_char		mbr[MBRSIZE];
67 };
68 
69 struct g_part_mbr_entry {
70 	struct g_part_entry	base;
71 	struct dos_partition ent;
72 };
73 
74 static int g_part_mbr_add(struct g_part_table *, struct g_part_entry *,
75     struct g_part_parms *);
76 static int g_part_mbr_bootcode(struct g_part_table *, struct g_part_parms *);
77 static int g_part_mbr_create(struct g_part_table *, struct g_part_parms *);
78 static int g_part_mbr_destroy(struct g_part_table *, struct g_part_parms *);
79 static void g_part_mbr_dumpconf(struct g_part_table *, struct g_part_entry *,
80     struct sbuf *, const char *);
81 static int g_part_mbr_dumpto(struct g_part_table *, struct g_part_entry *);
82 static int g_part_mbr_modify(struct g_part_table *, struct g_part_entry *,
83     struct g_part_parms *);
84 static const char *g_part_mbr_name(struct g_part_table *, struct g_part_entry *,
85     char *, size_t);
86 static int g_part_mbr_probe(struct g_part_table *, struct g_consumer *);
87 static int g_part_mbr_read(struct g_part_table *, struct g_consumer *);
88 static int g_part_mbr_setunset(struct g_part_table *, struct g_part_entry *,
89     const char *, unsigned int);
90 static const char *g_part_mbr_type(struct g_part_table *, struct g_part_entry *,
91     char *, size_t);
92 static int g_part_mbr_write(struct g_part_table *, struct g_consumer *);
93 static int g_part_mbr_resize(struct g_part_table *, struct g_part_entry *,
94     struct g_part_parms *);
95 
96 static kobj_method_t g_part_mbr_methods[] = {
97 	KOBJMETHOD(g_part_add,		g_part_mbr_add),
98 	KOBJMETHOD(g_part_bootcode,	g_part_mbr_bootcode),
99 	KOBJMETHOD(g_part_create,	g_part_mbr_create),
100 	KOBJMETHOD(g_part_destroy,	g_part_mbr_destroy),
101 	KOBJMETHOD(g_part_dumpconf,	g_part_mbr_dumpconf),
102 	KOBJMETHOD(g_part_dumpto,	g_part_mbr_dumpto),
103 	KOBJMETHOD(g_part_modify,	g_part_mbr_modify),
104 	KOBJMETHOD(g_part_resize,	g_part_mbr_resize),
105 	KOBJMETHOD(g_part_name,		g_part_mbr_name),
106 	KOBJMETHOD(g_part_probe,	g_part_mbr_probe),
107 	KOBJMETHOD(g_part_read,		g_part_mbr_read),
108 	KOBJMETHOD(g_part_setunset,	g_part_mbr_setunset),
109 	KOBJMETHOD(g_part_type,		g_part_mbr_type),
110 	KOBJMETHOD(g_part_write,	g_part_mbr_write),
111 	{ 0, 0 }
112 };
113 
114 static struct g_part_scheme g_part_mbr_scheme = {
115 	"MBR",
116 	g_part_mbr_methods,
117 	sizeof(struct g_part_mbr_table),
118 	.gps_entrysz = sizeof(struct g_part_mbr_entry),
119 	.gps_minent = NDOSPART,
120 	.gps_maxent = NDOSPART,
121 	.gps_bootcodesz = MBRSIZE,
122 };
123 G_PART_SCHEME_DECLARE(g_part_mbr);
124 MODULE_VERSION(geom_part_mbr, 0);
125 
126 static struct g_part_mbr_alias {
127 	u_char		typ;
128 	int		alias;
129 } mbr_alias_match[] = {
130 	{ DOSPTYP_386BSD,	G_PART_ALIAS_FREEBSD },
131 	{ DOSPTYP_EXT,		G_PART_ALIAS_EBR },
132 	{ DOSPTYP_NTFS,		G_PART_ALIAS_MS_NTFS },
133 	{ DOSPTYP_FAT16,	G_PART_ALIAS_MS_FAT16 },
134 	{ DOSPTYP_FAT32,	G_PART_ALIAS_MS_FAT32 },
135 	{ DOSPTYP_EXTLBA,	G_PART_ALIAS_EBR },
136 	{ DOSPTYP_LDM,		G_PART_ALIAS_MS_LDM_DATA },
137 	{ DOSPTYP_LINSWP,	G_PART_ALIAS_LINUX_SWAP },
138 	{ DOSPTYP_LINUX,	G_PART_ALIAS_LINUX_DATA },
139 	{ DOSPTYP_LINLVM,	G_PART_ALIAS_LINUX_LVM },
140 	{ DOSPTYP_LINRAID,	G_PART_ALIAS_LINUX_RAID },
141 	{ DOSPTYP_PPCBOOT,	G_PART_ALIAS_PREP_BOOT },
142 	{ DOSPTYP_VMFS,		G_PART_ALIAS_VMFS },
143 	{ DOSPTYP_VMKDIAG,	G_PART_ALIAS_VMKDIAG },
144 	{ DOSPTYP_APPLE_UFS,	G_PART_ALIAS_APPLE_UFS },
145 	{ DOSPTYP_APPLE_BOOT,	G_PART_ALIAS_APPLE_BOOT },
146 	{ DOSPTYP_HFS,		G_PART_ALIAS_APPLE_HFS },
147 };
148 
149 static int
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
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
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
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
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
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)
276 		*(uint32_t *)(table->mbr + DOSDSNOFF) = dsn;
277 	return (0);
278 }
279 
280 static int
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
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
309 g_part_mbr_dumpconf(struct g_part_table *basetable, struct g_part_entry *baseentry,
310     struct sbuf *sb, const char *indent)
311 {
312 	struct g_part_mbr_entry *entry;
313 	struct g_part_mbr_table *table;
314 	uint32_t dsn;
315 
316 	table = (struct g_part_mbr_table *)basetable;
317 	entry = (struct g_part_mbr_entry *)baseentry;
318 	if (indent == NULL) {
319 		/* conftxt: libdisk compatibility */
320 		sbuf_printf(sb, " xs MBR xt %u", entry->ent.dp_typ);
321 	} else if (entry != NULL) {
322 		/* confxml: partition entry information */
323 		sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
324 		    entry->ent.dp_typ);
325 		if (entry->ent.dp_flag & 0x80)
326 			sbuf_printf(sb, "%s<attrib>active</attrib>\n", indent);
327 		dsn = le32dec(table->mbr + DOSDSNOFF);
328 		sbuf_printf(sb, "%s<efimedia>HD(%d,MBR,%#08x,%#jx,%#jx)", indent,
329 		    entry->base.gpe_index, dsn, (intmax_t)entry->base.gpe_start,
330 		    (intmax_t)(entry->base.gpe_end - entry->base.gpe_start + 1));
331 		sbuf_printf(sb, "</efimedia>\n");
332 	} else {
333 		/* confxml: scheme information */
334 	}
335 }
336 
337 static int
338 g_part_mbr_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
339 {
340 	struct g_part_mbr_entry *entry;
341 
342 	/* Allow dumping to a FreeBSD partition or Linux swap partition only. */
343 	entry = (struct g_part_mbr_entry *)baseentry;
344 	return ((entry->ent.dp_typ == DOSPTYP_386BSD ||
345 	    entry->ent.dp_typ == DOSPTYP_LINSWP) ? 1 : 0);
346 }
347 
348 static int
349 g_part_mbr_modify(struct g_part_table *basetable,
350     struct g_part_entry *baseentry, struct g_part_parms *gpp)
351 {
352 	struct g_part_mbr_entry *entry;
353 
354 	if (gpp->gpp_parms & G_PART_PARM_LABEL)
355 		return (EINVAL);
356 
357 	entry = (struct g_part_mbr_entry *)baseentry;
358 	if (gpp->gpp_parms & G_PART_PARM_TYPE)
359 		return (mbr_parse_type(gpp->gpp_type, &entry->ent.dp_typ));
360 	return (0);
361 }
362 
363 static int
364 g_part_mbr_resize(struct g_part_table *basetable,
365     struct g_part_entry *baseentry, struct g_part_parms *gpp)
366 {
367 	struct g_part_mbr_entry *entry;
368 	struct g_provider *pp;
369 	uint32_t size;
370 
371 	if (baseentry == NULL) {
372 		pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
373 		basetable->gpt_last = MIN(pp->mediasize / pp->sectorsize,
374 		    UINT32_MAX) - 1;
375 		return (0);
376 	}
377 	size = gpp->gpp_size;
378 	if (mbr_align(basetable, NULL, &size) != 0)
379 		return (EINVAL);
380 	/* XXX: prevent unexpected shrinking. */
381 	pp = baseentry->gpe_pp;
382 	if ((g_debugflags & 0x10) == 0 && size < gpp->gpp_size &&
383 	    pp->mediasize / pp->sectorsize > size)
384 		return (EBUSY);
385 	entry = (struct g_part_mbr_entry *)baseentry;
386 	baseentry->gpe_end = baseentry->gpe_start + size - 1;
387 	entry->ent.dp_size = size;
388 	mbr_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl,
389 	    &entry->ent.dp_ehd, &entry->ent.dp_esect);
390 	return (0);
391 }
392 
393 static const char *
394 g_part_mbr_name(struct g_part_table *table, struct g_part_entry *baseentry,
395     char *buf, size_t bufsz)
396 {
397 
398 	snprintf(buf, bufsz, "s%d", baseentry->gpe_index);
399 	return (buf);
400 }
401 
402 static int
403 g_part_mbr_probe(struct g_part_table *table, struct g_consumer *cp)
404 {
405 	char psn[8];
406 	struct g_provider *pp;
407 	u_char *buf, *p;
408 	int error, index, res, sum;
409 	uint16_t magic;
410 
411 	pp = cp->provider;
412 
413 	/* Sanity-check the provider. */
414 	if (pp->sectorsize < MBRSIZE || pp->mediasize < pp->sectorsize)
415 		return (ENOSPC);
416 	if (pp->sectorsize > 4096)
417 		return (ENXIO);
418 
419 	/* We don't nest under an MBR (see EBR instead). */
420 	error = g_getattr("PART::scheme", cp, &psn);
421 	if (error == 0 && strcmp(psn, g_part_mbr_scheme.name) == 0)
422 		return (ELOOP);
423 
424 	/* Check that there's a MBR. */
425 	buf = g_read_data(cp, 0L, pp->sectorsize, &error);
426 	if (buf == NULL)
427 		return (error);
428 
429 	/* We goto out on mismatch. */
430 	res = ENXIO;
431 
432 	magic = le16dec(buf + DOSMAGICOFFSET);
433 	if (magic != DOSMAGIC)
434 		goto out;
435 
436 	for (index = 0; index < NDOSPART; index++) {
437 		p = buf + DOSPARTOFF + index * DOSPARTSIZE;
438 		if (p[0] != 0 && p[0] != 0x80)
439 			goto out;
440 	}
441 
442 	/*
443 	 * If the partition table does not consist of all zeroes,
444 	 * assume we have a MBR. If it's all zeroes, we could have
445 	 * a boot sector. For example, a boot sector that doesn't
446 	 * have boot code -- common on non-i386 hardware. In that
447 	 * case we check if we have a possible BPB. If so, then we
448 	 * assume we have a boot sector instead.
449 	 */
450 	sum = 0;
451 	for (index = 0; index < NDOSPART * DOSPARTSIZE; index++)
452 		sum += buf[DOSPARTOFF + index];
453 	if (sum != 0 || !mbr_probe_bpb(buf + 0x0b))
454 		res = G_PART_PROBE_PRI_NORM;
455 
456  out:
457 	g_free(buf);
458 	return (res);
459 }
460 
461 static int
462 g_part_mbr_read(struct g_part_table *basetable, struct g_consumer *cp)
463 {
464 	struct dos_partition ent;
465 	struct g_provider *pp;
466 	struct g_part_mbr_table *table;
467 	struct g_part_mbr_entry *entry;
468 	u_char *buf, *p;
469 	off_t chs, msize, first;
470 	u_int sectors, heads;
471 	int error, index;
472 
473 	pp = cp->provider;
474 	table = (struct g_part_mbr_table *)basetable;
475 	first = basetable->gpt_sectors;
476 	msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
477 
478 	buf = g_read_data(cp, 0L, pp->sectorsize, &error);
479 	if (buf == NULL)
480 		return (error);
481 
482 	bcopy(buf, table->mbr, sizeof(table->mbr));
483 	for (index = NDOSPART - 1; index >= 0; index--) {
484 		p = buf + DOSPARTOFF + index * DOSPARTSIZE;
485 		ent.dp_flag = p[0];
486 		ent.dp_shd = p[1];
487 		ent.dp_ssect = p[2];
488 		ent.dp_scyl = p[3];
489 		ent.dp_typ = p[4];
490 		ent.dp_ehd = p[5];
491 		ent.dp_esect = p[6];
492 		ent.dp_ecyl = p[7];
493 		ent.dp_start = le32dec(p + 8);
494 		ent.dp_size = le32dec(p + 12);
495 		if (ent.dp_typ == 0 || ent.dp_typ == DOSPTYP_PMBR)
496 			continue;
497 		if (ent.dp_start == 0 || ent.dp_size == 0)
498 			continue;
499 		sectors = ent.dp_esect & 0x3f;
500 		if (sectors > basetable->gpt_sectors &&
501 		    !basetable->gpt_fixgeom) {
502 			g_part_geometry_heads(msize, sectors, &chs, &heads);
503 			if (chs != 0) {
504 				basetable->gpt_sectors = sectors;
505 				basetable->gpt_heads = heads;
506 			}
507 		}
508 		if (ent.dp_start < first)
509 			first = ent.dp_start;
510 		entry = (struct g_part_mbr_entry *)g_part_new_entry(basetable,
511 		    index + 1, ent.dp_start, ent.dp_start + ent.dp_size - 1);
512 		entry->ent = ent;
513 	}
514 
515 	basetable->gpt_entries = NDOSPART;
516 	basetable->gpt_first = basetable->gpt_sectors;
517 	basetable->gpt_last = msize - 1;
518 
519 	if (first < basetable->gpt_first)
520 		basetable->gpt_first = 1;
521 
522 	g_free(buf);
523 	return (0);
524 }
525 
526 static int
527 g_part_mbr_setunset(struct g_part_table *table, struct g_part_entry *baseentry,
528     const char *attrib, unsigned int set)
529 {
530 	struct g_part_entry *iter;
531 	struct g_part_mbr_entry *entry;
532 	int changed;
533 
534 	if (baseentry == NULL)
535 		return (ENODEV);
536 	if (strcasecmp(attrib, "active") != 0)
537 		return (EINVAL);
538 
539 	/* Only one entry can have the active attribute. */
540 	LIST_FOREACH(iter, &table->gpt_entry, gpe_entry) {
541 		if (iter->gpe_deleted)
542 			continue;
543 		changed = 0;
544 		entry = (struct g_part_mbr_entry *)iter;
545 		if (iter == baseentry) {
546 			if (set && (entry->ent.dp_flag & 0x80) == 0) {
547 				entry->ent.dp_flag |= 0x80;
548 				changed = 1;
549 			} else if (!set && (entry->ent.dp_flag & 0x80)) {
550 				entry->ent.dp_flag &= ~0x80;
551 				changed = 1;
552 			}
553 		} else {
554 			if (set && (entry->ent.dp_flag & 0x80)) {
555 				entry->ent.dp_flag &= ~0x80;
556 				changed = 1;
557 			}
558 		}
559 		if (changed && !iter->gpe_created)
560 			iter->gpe_modified = 1;
561 	}
562 	return (0);
563 }
564 
565 static const char *
566 g_part_mbr_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
567     char *buf, size_t bufsz)
568 {
569 	struct g_part_mbr_entry *entry;
570 	int i;
571 
572 	entry = (struct g_part_mbr_entry *)baseentry;
573 	for (i = 0; i < nitems(mbr_alias_match); i++) {
574 		if (mbr_alias_match[i].typ == entry->ent.dp_typ)
575 			return (g_part_alias_name(mbr_alias_match[i].alias));
576 	}
577 	snprintf(buf, bufsz, "!%d", entry->ent.dp_typ);
578 	return (buf);
579 }
580 
581 static int
582 g_part_mbr_write(struct g_part_table *basetable, struct g_consumer *cp)
583 {
584 	struct g_part_entry *baseentry;
585 	struct g_part_mbr_entry *entry;
586 	struct g_part_mbr_table *table;
587 	u_char *p;
588 	int error, index;
589 
590 	table = (struct g_part_mbr_table *)basetable;
591 	baseentry = LIST_FIRST(&basetable->gpt_entry);
592 	for (index = 1; index <= basetable->gpt_entries; index++) {
593 		p = table->mbr + DOSPARTOFF + (index - 1) * DOSPARTSIZE;
594 		entry = (baseentry != NULL && index == baseentry->gpe_index)
595 		    ? (struct g_part_mbr_entry *)baseentry : NULL;
596 		if (entry != NULL && !baseentry->gpe_deleted) {
597 			p[0] = entry->ent.dp_flag;
598 			p[1] = entry->ent.dp_shd;
599 			p[2] = entry->ent.dp_ssect;
600 			p[3] = entry->ent.dp_scyl;
601 			p[4] = entry->ent.dp_typ;
602 			p[5] = entry->ent.dp_ehd;
603 			p[6] = entry->ent.dp_esect;
604 			p[7] = entry->ent.dp_ecyl;
605 			le32enc(p + 8, entry->ent.dp_start);
606 			le32enc(p + 12, entry->ent.dp_size);
607 		} else
608 			bzero(p, DOSPARTSIZE);
609 
610 		if (entry != NULL)
611 			baseentry = LIST_NEXT(baseentry, gpe_entry);
612 	}
613 
614 	error = g_write_data(cp, 0, table->mbr, cp->provider->sectorsize);
615 	return (error);
616 }
617