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