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