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