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