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