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