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