1/*-
2 * Copyright (c) 2007-2009 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 "opt_geom.h"
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: releng/11.0/sys/geom/part/g_part_ebr.c 298354 2016-04-20 16:19:44Z pfg $");
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/part/g_part.h>
48
49#include "g_part_if.h"
50
51FEATURE(geom_part_ebr,
52    "GEOM partitioning class for extended boot records support");
53#if defined(GEOM_PART_EBR_COMPAT)
54FEATURE(geom_part_ebr_compat,
55    "GEOM EBR partitioning class: backward-compatible partition names");
56#endif
57
58#define	EBRSIZE		512
59
60struct g_part_ebr_table {
61	struct g_part_table	base;
62#ifndef GEOM_PART_EBR_COMPAT
63	u_char		ebr[EBRSIZE];
64#endif
65};
66
67struct g_part_ebr_entry {
68	struct g_part_entry	base;
69	struct dos_partition	ent;
70};
71
72static int g_part_ebr_add(struct g_part_table *, struct g_part_entry *,
73    struct g_part_parms *);
74static int g_part_ebr_create(struct g_part_table *, struct g_part_parms *);
75static int g_part_ebr_destroy(struct g_part_table *, struct g_part_parms *);
76static void g_part_ebr_dumpconf(struct g_part_table *, struct g_part_entry *,
77    struct sbuf *, const char *);
78static int g_part_ebr_dumpto(struct g_part_table *, struct g_part_entry *);
79#if defined(GEOM_PART_EBR_COMPAT)
80static void g_part_ebr_fullname(struct g_part_table *, struct g_part_entry *,
81    struct sbuf *, const char *);
82#endif
83static int g_part_ebr_modify(struct g_part_table *, struct g_part_entry *,
84    struct g_part_parms *);
85static const char *g_part_ebr_name(struct g_part_table *, struct g_part_entry *,
86    char *, size_t);
87static int g_part_ebr_precheck(struct g_part_table *, enum g_part_ctl,
88    struct g_part_parms *);
89static int g_part_ebr_probe(struct g_part_table *, struct g_consumer *);
90static int g_part_ebr_read(struct g_part_table *, struct g_consumer *);
91static int g_part_ebr_setunset(struct g_part_table *, struct g_part_entry *,
92    const char *, unsigned int);
93static const char *g_part_ebr_type(struct g_part_table *, struct g_part_entry *,
94    char *, size_t);
95static int g_part_ebr_write(struct g_part_table *, struct g_consumer *);
96static int g_part_ebr_resize(struct g_part_table *, struct g_part_entry *,
97    struct g_part_parms *);
98
99static kobj_method_t g_part_ebr_methods[] = {
100	KOBJMETHOD(g_part_add,		g_part_ebr_add),
101	KOBJMETHOD(g_part_create,	g_part_ebr_create),
102	KOBJMETHOD(g_part_destroy,	g_part_ebr_destroy),
103	KOBJMETHOD(g_part_dumpconf,	g_part_ebr_dumpconf),
104	KOBJMETHOD(g_part_dumpto,	g_part_ebr_dumpto),
105#if defined(GEOM_PART_EBR_COMPAT)
106	KOBJMETHOD(g_part_fullname,	g_part_ebr_fullname),
107#endif
108	KOBJMETHOD(g_part_modify,	g_part_ebr_modify),
109	KOBJMETHOD(g_part_name,		g_part_ebr_name),
110	KOBJMETHOD(g_part_precheck,	g_part_ebr_precheck),
111	KOBJMETHOD(g_part_probe,	g_part_ebr_probe),
112	KOBJMETHOD(g_part_read,		g_part_ebr_read),
113	KOBJMETHOD(g_part_resize,	g_part_ebr_resize),
114	KOBJMETHOD(g_part_setunset,	g_part_ebr_setunset),
115	KOBJMETHOD(g_part_type,		g_part_ebr_type),
116	KOBJMETHOD(g_part_write,	g_part_ebr_write),
117	{ 0, 0 }
118};
119
120static struct g_part_scheme g_part_ebr_scheme = {
121	"EBR",
122	g_part_ebr_methods,
123	sizeof(struct g_part_ebr_table),
124	.gps_entrysz = sizeof(struct g_part_ebr_entry),
125	.gps_minent = 1,
126	.gps_maxent = INT_MAX,
127};
128G_PART_SCHEME_DECLARE(g_part_ebr);
129
130static struct g_part_ebr_alias {
131	u_char		typ;
132	int		alias;
133} ebr_alias_match[] = {
134	{ DOSPTYP_386BSD,	G_PART_ALIAS_FREEBSD },
135	{ DOSPTYP_NTFS,		G_PART_ALIAS_MS_NTFS },
136	{ DOSPTYP_FAT32,	G_PART_ALIAS_MS_FAT32 },
137	{ DOSPTYP_LINSWP,	G_PART_ALIAS_LINUX_SWAP },
138	{ DOSPTYP_LINUX,	G_PART_ALIAS_LINUX_DATA },
139	{ DOSPTYP_LINLVM,	G_PART_ALIAS_LINUX_LVM },
140	{ DOSPTYP_LINRAID,	G_PART_ALIAS_LINUX_RAID },
141};
142
143static void ebr_set_chs(struct g_part_table *, uint32_t, u_char *, u_char *,
144    u_char *);
145
146static void
147ebr_entry_decode(const char *p, struct dos_partition *ent)
148{
149	ent->dp_flag = p[0];
150	ent->dp_shd = p[1];
151	ent->dp_ssect = p[2];
152	ent->dp_scyl = p[3];
153	ent->dp_typ = p[4];
154	ent->dp_ehd = p[5];
155	ent->dp_esect = p[6];
156	ent->dp_ecyl = p[7];
157	ent->dp_start = le32dec(p + 8);
158	ent->dp_size = le32dec(p + 12);
159}
160
161static void
162ebr_entry_link(struct g_part_table *table, uint32_t start, uint32_t end,
163   u_char *buf)
164{
165
166	buf[0] = 0 /* dp_flag */;
167	ebr_set_chs(table, start, &buf[3] /* dp_scyl */, &buf[1] /* dp_shd */,
168	    &buf[2] /* dp_ssect */);
169	buf[4] = 5 /* dp_typ */;
170	ebr_set_chs(table, end, &buf[7] /* dp_ecyl */, &buf[5] /* dp_ehd */,
171	    &buf[6] /* dp_esect */);
172	le32enc(buf + 8, start);
173	le32enc(buf + 12, end - start + 1);
174}
175
176static int
177ebr_parse_type(const char *type, u_char *dp_typ)
178{
179	const char *alias;
180	char *endp;
181	long lt;
182	int i;
183
184	if (type[0] == '!') {
185		lt = strtol(type + 1, &endp, 0);
186		if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256)
187			return (EINVAL);
188		*dp_typ = (u_char)lt;
189		return (0);
190	}
191	for (i = 0; i < nitems(ebr_alias_match); i++) {
192		alias = g_part_alias_name(ebr_alias_match[i].alias);
193		if (strcasecmp(type, alias) == 0) {
194			*dp_typ = ebr_alias_match[i].typ;
195			return (0);
196		}
197	}
198	return (EINVAL);
199}
200
201
202static void
203ebr_set_chs(struct g_part_table *table, uint32_t lba, u_char *cylp, u_char *hdp,
204    u_char *secp)
205{
206	uint32_t cyl, hd, sec;
207
208	sec = lba % table->gpt_sectors + 1;
209	lba /= table->gpt_sectors;
210	hd = lba % table->gpt_heads;
211	lba /= table->gpt_heads;
212	cyl = lba;
213	if (cyl > 1023)
214		sec = hd = cyl = ~0;
215
216	*cylp = cyl & 0xff;
217	*hdp = hd & 0xff;
218	*secp = (sec & 0x3f) | ((cyl >> 2) & 0xc0);
219}
220
221static int
222ebr_align(struct g_part_table *basetable, uint32_t *start, uint32_t *size)
223{
224	uint32_t sectors;
225
226	sectors = basetable->gpt_sectors;
227	if (*size < 2 * sectors)
228		return (EINVAL);
229	if (*start % sectors) {
230		*size += (*start % sectors) - sectors;
231		*start -= (*start % sectors) - sectors;
232	}
233	if (*size % sectors)
234		*size -= (*size % sectors);
235	if (*size < 2 * sectors)
236		return (EINVAL);
237	return (0);
238}
239
240
241static int
242g_part_ebr_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
243    struct g_part_parms *gpp)
244{
245	struct g_provider *pp;
246	struct g_part_ebr_entry *entry;
247	uint32_t start, size;
248
249	if (gpp->gpp_parms & G_PART_PARM_LABEL)
250		return (EINVAL);
251
252	pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
253	entry = (struct g_part_ebr_entry *)baseentry;
254	start = gpp->gpp_start;
255	size = gpp->gpp_size;
256	if (ebr_align(basetable, &start, &size) != 0)
257		return (EINVAL);
258	if (baseentry->gpe_deleted)
259		bzero(&entry->ent, sizeof(entry->ent));
260
261	KASSERT(baseentry->gpe_start <= start, ("%s", __func__));
262	KASSERT(baseentry->gpe_end >= start + size - 1, ("%s", __func__));
263	baseentry->gpe_index = (start / basetable->gpt_sectors) + 1;
264	baseentry->gpe_offset =
265	    (off_t)(start + basetable->gpt_sectors) * pp->sectorsize;
266	baseentry->gpe_start = start;
267	baseentry->gpe_end = start + size - 1;
268	entry->ent.dp_start = basetable->gpt_sectors;
269	entry->ent.dp_size = size - basetable->gpt_sectors;
270	ebr_set_chs(basetable, entry->ent.dp_start, &entry->ent.dp_scyl,
271	    &entry->ent.dp_shd, &entry->ent.dp_ssect);
272	ebr_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl,
273	    &entry->ent.dp_ehd, &entry->ent.dp_esect);
274	return (ebr_parse_type(gpp->gpp_type, &entry->ent.dp_typ));
275}
276
277static int
278g_part_ebr_create(struct g_part_table *basetable, struct g_part_parms *gpp)
279{
280	char type[64];
281	struct g_consumer *cp;
282	struct g_provider *pp;
283	uint32_t msize;
284	int error;
285
286	pp = gpp->gpp_provider;
287
288	if (pp->sectorsize < EBRSIZE)
289		return (ENOSPC);
290	if (pp->sectorsize > 4096)
291		return (ENXIO);
292
293	/* Check that we have a parent and that it's a MBR. */
294	if (basetable->gpt_depth == 0)
295		return (ENXIO);
296	cp = LIST_FIRST(&pp->consumers);
297	error = g_getattr("PART::scheme", cp, &type);
298	if (error != 0)
299		return (error);
300	if (strcmp(type, "MBR") != 0)
301		return (ENXIO);
302	error = g_getattr("PART::type", cp, &type);
303	if (error != 0)
304		return (error);
305	if (strcmp(type, "ebr") != 0)
306		return (ENXIO);
307
308	msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
309	basetable->gpt_first = 0;
310	basetable->gpt_last = msize - 1;
311	basetable->gpt_entries = msize / basetable->gpt_sectors;
312	return (0);
313}
314
315static int
316g_part_ebr_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
317{
318
319	/* Wipe the first sector to clear the partitioning. */
320	basetable->gpt_smhead |= 1;
321	return (0);
322}
323
324static void
325g_part_ebr_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
326    struct sbuf *sb, const char *indent)
327{
328	struct g_part_ebr_entry *entry;
329
330	entry = (struct g_part_ebr_entry *)baseentry;
331	if (indent == NULL) {
332		/* conftxt: libdisk compatibility */
333		sbuf_printf(sb, " xs MBREXT xt %u", entry->ent.dp_typ);
334	} else if (entry != NULL) {
335		/* confxml: partition entry information */
336		sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
337		    entry->ent.dp_typ);
338		if (entry->ent.dp_flag & 0x80)
339			sbuf_printf(sb, "%s<attrib>active</attrib>\n", indent);
340	} else {
341		/* confxml: scheme information */
342	}
343}
344
345static int
346g_part_ebr_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
347{
348	struct g_part_ebr_entry *entry;
349
350	/* Allow dumping to a FreeBSD partition or Linux swap partition only. */
351	entry = (struct g_part_ebr_entry *)baseentry;
352	return ((entry->ent.dp_typ == DOSPTYP_386BSD ||
353	    entry->ent.dp_typ == DOSPTYP_LINSWP) ? 1 : 0);
354}
355
356#if defined(GEOM_PART_EBR_COMPAT)
357static void
358g_part_ebr_fullname(struct g_part_table *table, struct g_part_entry *entry,
359    struct sbuf *sb, const char *pfx)
360{
361	struct g_part_entry *iter;
362	u_int idx;
363
364	idx = 5;
365	LIST_FOREACH(iter, &table->gpt_entry, gpe_entry) {
366		if (iter == entry)
367			break;
368		idx++;
369	}
370	sbuf_printf(sb, "%.*s%u", (int)strlen(pfx) - 1, pfx, idx);
371}
372#endif
373
374static int
375g_part_ebr_modify(struct g_part_table *basetable,
376    struct g_part_entry *baseentry, struct g_part_parms *gpp)
377{
378	struct g_part_ebr_entry *entry;
379
380	if (gpp->gpp_parms & G_PART_PARM_LABEL)
381		return (EINVAL);
382
383	entry = (struct g_part_ebr_entry *)baseentry;
384	if (gpp->gpp_parms & G_PART_PARM_TYPE)
385		return (ebr_parse_type(gpp->gpp_type, &entry->ent.dp_typ));
386	return (0);
387}
388
389static int
390g_part_ebr_resize(struct g_part_table *basetable,
391    struct g_part_entry *baseentry, struct g_part_parms *gpp)
392{
393	struct g_provider *pp;
394
395	if (baseentry != NULL)
396		return (EOPNOTSUPP);
397	pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
398	basetable->gpt_last = MIN(pp->mediasize / pp->sectorsize,
399	    UINT32_MAX) - 1;
400	return (0);
401}
402
403static const char *
404g_part_ebr_name(struct g_part_table *table, struct g_part_entry *entry,
405    char *buf, size_t bufsz)
406{
407
408	snprintf(buf, bufsz, "+%08u", entry->gpe_index);
409	return (buf);
410}
411
412static int
413g_part_ebr_precheck(struct g_part_table *table, enum g_part_ctl req,
414    struct g_part_parms *gpp)
415{
416#if defined(GEOM_PART_EBR_COMPAT)
417	if (req == G_PART_CTL_DESTROY)
418		return (0);
419	return (ECANCELED);
420#else
421	/*
422	 * The index is a function of the start of the partition.
423	 * This is not something the user can override, nor is it
424	 * something the common code will do right. We can set the
425	 * index now so that we get what we need.
426	 */
427	if (req == G_PART_CTL_ADD)
428		gpp->gpp_index = (gpp->gpp_start / table->gpt_sectors) + 1;
429	return (0);
430#endif
431}
432
433static int
434g_part_ebr_probe(struct g_part_table *table, struct g_consumer *cp)
435{
436	char type[64];
437	struct g_provider *pp;
438	u_char *buf, *p;
439	int error, index, res;
440	uint16_t magic;
441
442	pp = cp->provider;
443
444	/* Sanity-check the provider. */
445	if (pp->sectorsize < EBRSIZE || pp->mediasize < pp->sectorsize)
446		return (ENOSPC);
447	if (pp->sectorsize > 4096)
448		return (ENXIO);
449
450	/* Check that we have a parent and that it's a MBR. */
451	if (table->gpt_depth == 0)
452		return (ENXIO);
453	error = g_getattr("PART::scheme", cp, &type);
454	if (error != 0)
455		return (error);
456	if (strcmp(type, "MBR") != 0)
457		return (ENXIO);
458	/* Check that partition has type DOSPTYP_EBR. */
459	error = g_getattr("PART::type", cp, &type);
460	if (error != 0)
461		return (error);
462	if (strcmp(type, "ebr") != 0)
463		return (ENXIO);
464
465	/* Check that there's a EBR. */
466	buf = g_read_data(cp, 0L, pp->sectorsize, &error);
467	if (buf == NULL)
468		return (error);
469
470	/* We goto out on mismatch. */
471	res = ENXIO;
472
473	magic = le16dec(buf + DOSMAGICOFFSET);
474	if (magic != DOSMAGIC)
475		goto out;
476
477	for (index = 0; index < 2; index++) {
478		p = buf + DOSPARTOFF + index * DOSPARTSIZE;
479		if (p[0] != 0 && p[0] != 0x80)
480			goto out;
481	}
482	res = G_PART_PROBE_PRI_NORM;
483
484 out:
485	g_free(buf);
486	return (res);
487}
488
489static int
490g_part_ebr_read(struct g_part_table *basetable, struct g_consumer *cp)
491{
492	struct dos_partition ent[2];
493	struct g_provider *pp;
494	struct g_part_entry *baseentry;
495	struct g_part_ebr_table *table;
496	struct g_part_ebr_entry *entry;
497	u_char *buf;
498	off_t ofs, msize;
499	u_int lba;
500	int error, index;
501
502	pp = cp->provider;
503	table = (struct g_part_ebr_table *)basetable;
504	msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
505
506	lba = 0;
507	while (1) {
508		ofs = (off_t)lba * pp->sectorsize;
509		buf = g_read_data(cp, ofs, pp->sectorsize, &error);
510		if (buf == NULL)
511			return (error);
512
513		ebr_entry_decode(buf + DOSPARTOFF + 0 * DOSPARTSIZE, ent + 0);
514		ebr_entry_decode(buf + DOSPARTOFF + 1 * DOSPARTSIZE, ent + 1);
515
516		/* The 3rd & 4th entries should be zeroes. */
517		if (le64dec(buf + DOSPARTOFF + 2 * DOSPARTSIZE) +
518		    le64dec(buf + DOSPARTOFF + 3 * DOSPARTSIZE) != 0) {
519			basetable->gpt_corrupt = 1;
520			printf("GEOM: %s: invalid entries in the EBR ignored.\n",
521			    pp->name);
522		}
523#ifndef GEOM_PART_EBR_COMPAT
524		/* Save the first EBR, it can contain a boot code */
525		if (lba == 0)
526			bcopy(buf, table->ebr, sizeof(table->ebr));
527#endif
528		g_free(buf);
529
530		if (ent[0].dp_typ == 0)
531			break;
532
533		if (ent[0].dp_typ == 5 && ent[1].dp_typ == 0) {
534			lba = ent[0].dp_start;
535			continue;
536		}
537
538		index = (lba / basetable->gpt_sectors) + 1;
539		baseentry = (struct g_part_entry *)g_part_new_entry(basetable,
540		    index, lba, lba + ent[0].dp_start + ent[0].dp_size - 1);
541		baseentry->gpe_offset = (off_t)(lba + ent[0].dp_start) *
542		    pp->sectorsize;
543		entry = (struct g_part_ebr_entry *)baseentry;
544		entry->ent = ent[0];
545
546		if (ent[1].dp_typ == 0)
547			break;
548
549		lba = ent[1].dp_start;
550	}
551
552	basetable->gpt_entries = msize / basetable->gpt_sectors;
553	basetable->gpt_first = 0;
554	basetable->gpt_last = msize - 1;
555	return (0);
556}
557
558static int
559g_part_ebr_setunset(struct g_part_table *table, struct g_part_entry *baseentry,
560    const char *attrib, unsigned int set)
561{
562	struct g_part_entry *iter;
563	struct g_part_ebr_entry *entry;
564	int changed;
565
566	if (baseentry == NULL)
567		return (ENODEV);
568	if (strcasecmp(attrib, "active") != 0)
569		return (EINVAL);
570
571	/* Only one entry can have the active attribute. */
572	LIST_FOREACH(iter, &table->gpt_entry, gpe_entry) {
573		if (iter->gpe_deleted)
574			continue;
575		changed = 0;
576		entry = (struct g_part_ebr_entry *)iter;
577		if (iter == baseentry) {
578			if (set && (entry->ent.dp_flag & 0x80) == 0) {
579				entry->ent.dp_flag |= 0x80;
580				changed = 1;
581			} else if (!set && (entry->ent.dp_flag & 0x80)) {
582				entry->ent.dp_flag &= ~0x80;
583				changed = 1;
584			}
585		} else {
586			if (set && (entry->ent.dp_flag & 0x80)) {
587				entry->ent.dp_flag &= ~0x80;
588				changed = 1;
589			}
590		}
591		if (changed && !iter->gpe_created)
592			iter->gpe_modified = 1;
593	}
594	return (0);
595}
596
597static const char *
598g_part_ebr_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
599    char *buf, size_t bufsz)
600{
601	struct g_part_ebr_entry *entry;
602	int i;
603
604	entry = (struct g_part_ebr_entry *)baseentry;
605	for (i = 0; i < nitems(ebr_alias_match); i++) {
606		if (ebr_alias_match[i].typ == entry->ent.dp_typ)
607			return (g_part_alias_name(ebr_alias_match[i].alias));
608	}
609	snprintf(buf, bufsz, "!%d", entry->ent.dp_typ);
610	return (buf);
611}
612
613static int
614g_part_ebr_write(struct g_part_table *basetable, struct g_consumer *cp)
615{
616#ifndef GEOM_PART_EBR_COMPAT
617	struct g_part_ebr_table *table;
618#endif
619	struct g_provider *pp;
620	struct g_part_entry *baseentry, *next;
621	struct g_part_ebr_entry *entry;
622	u_char *buf;
623	u_char *p;
624	int error;
625
626	pp = cp->provider;
627	buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
628#ifndef GEOM_PART_EBR_COMPAT
629	table = (struct g_part_ebr_table *)basetable;
630	bcopy(table->ebr, buf, DOSPARTOFF);
631#endif
632	le16enc(buf + DOSMAGICOFFSET, DOSMAGIC);
633
634	baseentry = LIST_FIRST(&basetable->gpt_entry);
635	while (baseentry != NULL && baseentry->gpe_deleted)
636		baseentry = LIST_NEXT(baseentry, gpe_entry);
637
638	/* Wipe-out the first EBR when there are no slices. */
639	if (baseentry == NULL) {
640		error = g_write_data(cp, 0, buf, pp->sectorsize);
641		goto out;
642	}
643
644	/*
645	 * If the first partition is not in LBA 0, we need to
646	 * put a "link" EBR in LBA 0.
647	 */
648	if (baseentry->gpe_start != 0) {
649		ebr_entry_link(basetable, (uint32_t)baseentry->gpe_start,
650		    (uint32_t)baseentry->gpe_end, buf + DOSPARTOFF);
651		error = g_write_data(cp, 0, buf, pp->sectorsize);
652		if (error)
653			goto out;
654	}
655
656	do {
657		entry = (struct g_part_ebr_entry *)baseentry;
658
659		p = buf + DOSPARTOFF;
660		p[0] = entry->ent.dp_flag;
661		p[1] = entry->ent.dp_shd;
662		p[2] = entry->ent.dp_ssect;
663		p[3] = entry->ent.dp_scyl;
664		p[4] = entry->ent.dp_typ;
665		p[5] = entry->ent.dp_ehd;
666		p[6] = entry->ent.dp_esect;
667		p[7] = entry->ent.dp_ecyl;
668		le32enc(p + 8, entry->ent.dp_start);
669		le32enc(p + 12, entry->ent.dp_size);
670
671		next = LIST_NEXT(baseentry, gpe_entry);
672		while (next != NULL && next->gpe_deleted)
673			next = LIST_NEXT(next, gpe_entry);
674
675		p += DOSPARTSIZE;
676		if (next != NULL)
677			ebr_entry_link(basetable, (uint32_t)next->gpe_start,
678			    (uint32_t)next->gpe_end, p);
679		else
680			bzero(p, DOSPARTSIZE);
681
682		error = g_write_data(cp, baseentry->gpe_start * pp->sectorsize,
683		    buf, pp->sectorsize);
684#ifndef GEOM_PART_EBR_COMPAT
685		if (baseentry->gpe_start == 0)
686			bzero(buf, DOSPARTOFF);
687#endif
688		baseentry = next;
689	} while (!error && baseentry != NULL);
690
691 out:
692	g_free(buf);
693	return (error);
694}
695