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