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