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