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