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