xref: /freebsd/sys/geom/part/g_part_ebr.c (revision 884a2a699669ec61e2366e3e358342dbc94be24a)
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, UINT32_MAX);
292 	basetable->gpt_first = 0;
293 	basetable->gpt_last = msize - 1;
294 	basetable->gpt_entries = msize / basetable->gpt_sectors;
295 	return (0);
296 }
297 
298 static int
299 g_part_ebr_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
300 {
301 
302 	/* Wipe the first sector to clear the partitioning. */
303 	basetable->gpt_smhead |= 1;
304 	return (0);
305 }
306 
307 static void
308 g_part_ebr_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
309     struct sbuf *sb, const char *indent)
310 {
311 	struct g_part_ebr_entry *entry;
312 
313 	entry = (struct g_part_ebr_entry *)baseentry;
314 	if (indent == NULL) {
315 		/* conftxt: libdisk compatibility */
316 		sbuf_printf(sb, " xs MBREXT xt %u", entry->ent.dp_typ);
317 	} else if (entry != NULL) {
318 		/* confxml: partition entry information */
319 		sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
320 		    entry->ent.dp_typ);
321 		if (entry->ent.dp_flag & 0x80)
322 			sbuf_printf(sb, "%s<attrib>active</attrib>\n", indent);
323 	} else {
324 		/* confxml: scheme information */
325 	}
326 }
327 
328 static int
329 g_part_ebr_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
330 {
331 	struct g_part_ebr_entry *entry;
332 
333 	/* Allow dumping to a FreeBSD partition only. */
334 	entry = (struct g_part_ebr_entry *)baseentry;
335 	return ((entry->ent.dp_typ == DOSPTYP_386BSD) ? 1 : 0);
336 }
337 
338 #if defined(GEOM_PART_EBR_COMPAT)
339 static void
340 g_part_ebr_fullname(struct g_part_table *table, struct g_part_entry *entry,
341     struct sbuf *sb, const char *pfx)
342 {
343 	struct g_part_entry *iter;
344 	u_int idx;
345 
346 	idx = 5;
347 	LIST_FOREACH(iter, &table->gpt_entry, gpe_entry) {
348 		if (iter == entry)
349 			break;
350 		idx++;
351 	}
352 	sbuf_printf(sb, "%.*s%u", (int)strlen(pfx) - 1, pfx, idx);
353 }
354 #endif
355 
356 static int
357 g_part_ebr_modify(struct g_part_table *basetable,
358     struct g_part_entry *baseentry, struct g_part_parms *gpp)
359 {
360 	struct g_part_ebr_entry *entry;
361 
362 	if (gpp->gpp_parms & G_PART_PARM_LABEL)
363 		return (EINVAL);
364 
365 	entry = (struct g_part_ebr_entry *)baseentry;
366 	if (gpp->gpp_parms & G_PART_PARM_TYPE)
367 		return (ebr_parse_type(gpp->gpp_type, &entry->ent.dp_typ));
368 	return (0);
369 }
370 
371 static const char *
372 g_part_ebr_name(struct g_part_table *table, struct g_part_entry *entry,
373     char *buf, size_t bufsz)
374 {
375 
376 	snprintf(buf, bufsz, "+%08u", entry->gpe_index);
377 	return (buf);
378 }
379 
380 static int
381 g_part_ebr_precheck(struct g_part_table *table, enum g_part_ctl req,
382     struct g_part_parms *gpp)
383 {
384 #if defined(GEOM_PART_EBR_COMPAT)
385 	if (req == G_PART_CTL_DESTROY)
386 		return (0);
387 	return (ECANCELED);
388 #else
389 	/*
390 	 * The index is a function of the start of the partition.
391 	 * This is not something the user can override, nor is it
392 	 * something the common code will do right. We can set the
393 	 * index now so that we get what we need.
394 	 */
395 	if (req == G_PART_CTL_ADD)
396 		gpp->gpp_index = (gpp->gpp_start / table->gpt_sectors) + 1;
397 	return (0);
398 #endif
399 }
400 
401 static int
402 g_part_ebr_probe(struct g_part_table *table, struct g_consumer *cp)
403 {
404 	char psn[8];
405 	struct g_provider *pp;
406 	u_char *buf, *p;
407 	int error, index, res;
408 	uint16_t magic;
409 
410 	pp = cp->provider;
411 
412 	/* Sanity-check the provider. */
413 	if (pp->sectorsize < EBRSIZE || pp->mediasize < pp->sectorsize)
414 		return (ENOSPC);
415 	if (pp->sectorsize > 4096)
416 		return (ENXIO);
417 
418 	/* Check that we have a parent and that it's a MBR. */
419 	if (table->gpt_depth == 0)
420 		return (ENXIO);
421 	error = g_getattr("PART::scheme", cp, &psn);
422 	if (error)
423 		return (error);
424 	if (strcmp(psn, "MBR"))
425 		return (ENXIO);
426 
427 	/* Check that there's a EBR. */
428 	buf = g_read_data(cp, 0L, pp->sectorsize, &error);
429 	if (buf == NULL)
430 		return (error);
431 
432 	/* We goto out on mismatch. */
433 	res = ENXIO;
434 
435 	magic = le16dec(buf + DOSMAGICOFFSET);
436 	if (magic != DOSMAGIC)
437 		goto out;
438 
439 	for (index = 0; index < 2; index++) {
440 		p = buf + DOSPARTOFF + index * DOSPARTSIZE;
441 		if (p[0] != 0 && p[0] != 0x80)
442 			goto out;
443 	}
444 	res = G_PART_PROBE_PRI_NORM;
445 
446  out:
447 	g_free(buf);
448 	return (res);
449 }
450 
451 static int
452 g_part_ebr_read(struct g_part_table *basetable, struct g_consumer *cp)
453 {
454 	struct dos_partition ent[2];
455 	struct g_provider *pp;
456 	struct g_part_entry *baseentry;
457 	struct g_part_ebr_table *table;
458 	struct g_part_ebr_entry *entry;
459 	u_char *buf;
460 	off_t ofs, msize;
461 	u_int lba;
462 	int error, index, sum;
463 
464 	pp = cp->provider;
465 	table = (struct g_part_ebr_table *)basetable;
466 	msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
467 
468 	lba = 0;
469 	while (1) {
470 		ofs = (off_t)lba * pp->sectorsize;
471 		buf = g_read_data(cp, ofs, pp->sectorsize, &error);
472 		if (buf == NULL)
473 			return (error);
474 
475 		ebr_entry_decode(buf + DOSPARTOFF + 0 * DOSPARTSIZE, ent + 0);
476 		ebr_entry_decode(buf + DOSPARTOFF + 1 * DOSPARTSIZE, ent + 1);
477 
478 		/* The 3rd & 4th entries should be zeroes. */
479 		if (le64dec(buf + DOSPARTOFF + 2 * DOSPARTSIZE) +
480 		    le64dec(buf + DOSPARTOFF + 3 * DOSPARTSIZE) != 0) {
481 			basetable->gpt_corrupt = 1;
482 			printf("GEOM: %s: invalid entries in the EBR ignored.\n",
483 			    pp->name);
484 		}
485 		/* We do not support bootcode for EBR. If bootcode area is
486 		 * not zeroes, then mark this EBR as corrupt to do not break
487 		 * anything for another OS'es.
488 		 */
489 		if (lba == 0) {
490 			sum = 0;
491 			for (index = 0; index < DOSPARTOFF; index++)
492 				sum += buf[index];
493 			if (sum != 0) {
494 				basetable->gpt_corrupt = 1;
495 				printf("GEOM: %s: EBR has non empty bootcode.\n",
496 				    pp->name);
497 			}
498 		}
499 		g_free(buf);
500 
501 		if (ent[0].dp_typ == 0)
502 			break;
503 
504 		if (ent[0].dp_typ == 5 && ent[1].dp_typ == 0) {
505 			lba = ent[0].dp_start;
506 			continue;
507 		}
508 
509 		index = (lba / basetable->gpt_sectors) + 1;
510 		baseentry = (struct g_part_entry *)g_part_new_entry(basetable,
511 		    index, lba, lba + ent[0].dp_start + ent[0].dp_size - 1);
512 		baseentry->gpe_offset = (off_t)(lba + ent[0].dp_start) *
513 		    pp->sectorsize;
514 		entry = (struct g_part_ebr_entry *)baseentry;
515 		entry->ent = ent[0];
516 
517 		if (ent[1].dp_typ == 0)
518 			break;
519 
520 		lba = ent[1].dp_start;
521 	}
522 
523 	basetable->gpt_entries = msize / basetable->gpt_sectors;
524 	basetable->gpt_first = 0;
525 	basetable->gpt_last = msize - 1;
526 	return (0);
527 }
528 
529 static int
530 g_part_ebr_setunset(struct g_part_table *table, struct g_part_entry *baseentry,
531     const char *attrib, unsigned int set)
532 {
533 	struct g_part_entry *iter;
534 	struct g_part_ebr_entry *entry;
535 	int changed;
536 
537 	if (strcasecmp(attrib, "active") != 0)
538 		return (EINVAL);
539 
540 	/* Only one entry can have the active attribute. */
541 	LIST_FOREACH(iter, &table->gpt_entry, gpe_entry) {
542 		if (iter->gpe_deleted)
543 			continue;
544 		changed = 0;
545 		entry = (struct g_part_ebr_entry *)iter;
546 		if (iter == baseentry) {
547 			if (set && (entry->ent.dp_flag & 0x80) == 0) {
548 				entry->ent.dp_flag |= 0x80;
549 				changed = 1;
550 			} else if (!set && (entry->ent.dp_flag & 0x80)) {
551 				entry->ent.dp_flag &= ~0x80;
552 				changed = 1;
553 			}
554 		} else {
555 			if (set && (entry->ent.dp_flag & 0x80)) {
556 				entry->ent.dp_flag &= ~0x80;
557 				changed = 1;
558 			}
559 		}
560 		if (changed && !iter->gpe_created)
561 			iter->gpe_modified = 1;
562 	}
563 	return (0);
564 }
565 
566 static const char *
567 g_part_ebr_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
568     char *buf, size_t bufsz)
569 {
570 	struct g_part_ebr_entry *entry;
571 	int i;
572 
573 	entry = (struct g_part_ebr_entry *)baseentry;
574 	for (i = 0;
575 	    i < sizeof(ebr_alias_match) / sizeof(ebr_alias_match[0]); i++) {
576 		if (ebr_alias_match[i].typ == entry->ent.dp_typ)
577 			return (g_part_alias_name(ebr_alias_match[i].alias));
578 	}
579 	snprintf(buf, bufsz, "!%d", entry->ent.dp_typ);
580 	return (buf);
581 }
582 
583 static int
584 g_part_ebr_write(struct g_part_table *basetable, struct g_consumer *cp)
585 {
586 	struct g_provider *pp;
587 	struct g_part_entry *baseentry, *next;
588 	struct g_part_ebr_entry *entry;
589 	u_char *buf;
590 	u_char *p;
591 	int error;
592 
593 	pp = cp->provider;
594 	buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
595 	le16enc(buf + DOSMAGICOFFSET, DOSMAGIC);
596 
597 	baseentry = LIST_FIRST(&basetable->gpt_entry);
598 	while (baseentry != NULL && baseentry->gpe_deleted)
599 		baseentry = LIST_NEXT(baseentry, gpe_entry);
600 
601 	/* Wipe-out the first EBR when there are no slices. */
602 	if (baseentry == NULL) {
603 		error = g_write_data(cp, 0, buf, pp->sectorsize);
604 		goto out;
605 	}
606 
607 	/*
608 	 * If the first partition is not in LBA 0, we need to
609 	 * put a "link" EBR in LBA 0.
610 	 */
611 	if (baseentry->gpe_start != 0) {
612 		ebr_entry_link(basetable, (uint32_t)baseentry->gpe_start,
613 		    (uint32_t)baseentry->gpe_end, buf + DOSPARTOFF);
614 		error = g_write_data(cp, 0, buf, pp->sectorsize);
615 		if (error)
616 			goto out;
617 	}
618 
619 	do {
620 		entry = (struct g_part_ebr_entry *)baseentry;
621 
622 		p = buf + DOSPARTOFF;
623 		p[0] = entry->ent.dp_flag;
624 		p[1] = entry->ent.dp_shd;
625 		p[2] = entry->ent.dp_ssect;
626 		p[3] = entry->ent.dp_scyl;
627 		p[4] = entry->ent.dp_typ;
628 		p[5] = entry->ent.dp_ehd;
629 		p[6] = entry->ent.dp_esect;
630 		p[7] = entry->ent.dp_ecyl;
631 		le32enc(p + 8, entry->ent.dp_start);
632 		le32enc(p + 12, entry->ent.dp_size);
633 
634 		next = LIST_NEXT(baseentry, gpe_entry);
635 		while (next != NULL && next->gpe_deleted)
636 			next = LIST_NEXT(next, gpe_entry);
637 
638 		p += DOSPARTSIZE;
639 		if (next != NULL)
640 			ebr_entry_link(basetable, (uint32_t)next->gpe_start,
641 			    (uint32_t)next->gpe_end, p);
642 		else
643 			bzero(p, DOSPARTSIZE);
644 
645 		error = g_write_data(cp, baseentry->gpe_start * pp->sectorsize,
646 		    buf, pp->sectorsize);
647 
648 		baseentry = next;
649 	} while (!error && baseentry != NULL);
650 
651  out:
652 	g_free(buf);
653 	return (error);
654 }
655