xref: /freebsd/sys/geom/part/g_part_ebr.c (revision e17f5b1d)
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 
214 static void
215 ebr_set_chs(struct g_part_table *table, uint32_t lba, u_char *cylp, u_char *hdp,
216     u_char *secp)
217 {
218 	uint32_t cyl, hd, sec;
219 
220 	sec = lba % table->gpt_sectors + 1;
221 	lba /= table->gpt_sectors;
222 	hd = lba % table->gpt_heads;
223 	lba /= table->gpt_heads;
224 	cyl = lba;
225 	if (cyl > 1023)
226 		sec = hd = cyl = ~0;
227 
228 	*cylp = cyl & 0xff;
229 	*hdp = hd & 0xff;
230 	*secp = (sec & 0x3f) | ((cyl >> 2) & 0xc0);
231 }
232 
233 static int
234 ebr_align(struct g_part_table *basetable, uint32_t *start, uint32_t *size)
235 {
236 	uint32_t sectors;
237 
238 	sectors = basetable->gpt_sectors;
239 	if (*size < 2 * sectors)
240 		return (EINVAL);
241 	if (*start % sectors) {
242 		*size += (*start % sectors) - sectors;
243 		*start -= (*start % sectors) - sectors;
244 	}
245 	if (*size % sectors)
246 		*size -= (*size % sectors);
247 	if (*size < 2 * sectors)
248 		return (EINVAL);
249 	return (0);
250 }
251 
252 
253 static int
254 g_part_ebr_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
255     struct g_part_parms *gpp)
256 {
257 	struct g_provider *pp;
258 	struct g_part_ebr_entry *entry;
259 	struct g_part_entry *iter;
260 	uint32_t start, size;
261 	u_int idx;
262 
263 	if (gpp->gpp_parms & G_PART_PARM_LABEL)
264 		return (EINVAL);
265 
266 	pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
267 	entry = (struct g_part_ebr_entry *)baseentry;
268 	start = gpp->gpp_start;
269 	size = gpp->gpp_size;
270 	if (ebr_align(basetable, &start, &size) != 0)
271 		return (EINVAL);
272 	if (baseentry->gpe_deleted)
273 		bzero(&entry->ent, sizeof(entry->ent));
274 
275 	KASSERT(baseentry->gpe_start <= start, ("%s", __func__));
276 	KASSERT(baseentry->gpe_end >= start + size - 1, ("%s", __func__));
277 	baseentry->gpe_index = (start / basetable->gpt_sectors) + 1;
278 	baseentry->gpe_offset =
279 	    (off_t)(start + basetable->gpt_sectors) * pp->sectorsize;
280 	baseentry->gpe_start = start;
281 	baseentry->gpe_end = start + size - 1;
282 	entry->ent.dp_start = basetable->gpt_sectors;
283 	entry->ent.dp_size = size - basetable->gpt_sectors;
284 	ebr_set_chs(basetable, entry->ent.dp_start, &entry->ent.dp_scyl,
285 	    &entry->ent.dp_shd, &entry->ent.dp_ssect);
286 	ebr_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl,
287 	    &entry->ent.dp_ehd, &entry->ent.dp_esect);
288 
289 	if (compat_aliases) {
290 		idx = 5;
291 		LIST_FOREACH(iter, &basetable->gpt_entry, gpe_entry)
292 			idx++;
293 		entry->ebr_compat_idx = idx;
294 	}
295 	return (ebr_parse_type(gpp->gpp_type, &entry->ent.dp_typ));
296 }
297 
298 static void
299 g_part_ebr_add_alias(struct g_part_table *table, struct g_provider *pp,
300     struct g_part_entry *baseentry, const char *pfx)
301 {
302 	struct g_part_ebr_entry *entry;
303 
304 	g_provider_add_alias(pp, "%s%s" EBRNAMFMT, pfx, g_part_separator,
305 	    baseentry->gpe_index);
306 	if (compat_aliases) {
307 		entry = (struct g_part_ebr_entry *)baseentry;
308 		g_provider_add_alias(pp, "%.*s%u", (int)strlen(pfx) - 1, pfx,
309 		    entry->ebr_compat_idx);
310 	}
311 }
312 
313 static struct g_provider *
314 g_part_ebr_new_provider(struct g_part_table *table, struct g_geom *gp,
315     struct g_part_entry *baseentry, const char *pfx)
316 {
317 	struct g_part_ebr_entry *entry;
318 	struct g_provider *pp;
319 
320 	pp = g_new_providerf(gp, "%s%s" EBRNAMFMT, pfx, g_part_separator,
321 	    baseentry->gpe_index);
322 	if (compat_aliases) {
323 		entry = (struct g_part_ebr_entry *)baseentry;
324 		g_provider_add_alias(pp, "%.*s%u", (int)strlen(pfx) - 1, pfx,
325 		    entry->ebr_compat_idx);
326 	}
327 	return (pp);
328 }
329 
330 static int
331 g_part_ebr_create(struct g_part_table *basetable, struct g_part_parms *gpp)
332 {
333 	char type[64];
334 	struct g_consumer *cp;
335 	struct g_provider *pp;
336 	uint32_t msize;
337 	int error;
338 
339 	pp = gpp->gpp_provider;
340 
341 	if (pp->sectorsize < EBRSIZE)
342 		return (ENOSPC);
343 	if (pp->sectorsize > 4096)
344 		return (ENXIO);
345 
346 	/* Check that we have a parent and that it's a MBR. */
347 	if (basetable->gpt_depth == 0)
348 		return (ENXIO);
349 	cp = LIST_FIRST(&pp->consumers);
350 	error = g_getattr("PART::scheme", cp, &type);
351 	if (error != 0)
352 		return (error);
353 	if (strcmp(type, "MBR") != 0)
354 		return (ENXIO);
355 	error = g_getattr("PART::type", cp, &type);
356 	if (error != 0)
357 		return (error);
358 	if (strcmp(type, "ebr") != 0)
359 		return (ENXIO);
360 
361 	msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
362 	basetable->gpt_first = 0;
363 	basetable->gpt_last = msize - 1;
364 	basetable->gpt_entries = msize / basetable->gpt_sectors;
365 	return (0);
366 }
367 
368 static int
369 g_part_ebr_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
370 {
371 
372 	/* Wipe the first sector to clear the partitioning. */
373 	basetable->gpt_smhead |= 1;
374 	return (0);
375 }
376 
377 static void
378 g_part_ebr_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
379     struct sbuf *sb, const char *indent)
380 {
381 	struct g_part_ebr_entry *entry;
382 
383 	entry = (struct g_part_ebr_entry *)baseentry;
384 	if (indent == NULL) {
385 		/* conftxt: libdisk compatibility */
386 		sbuf_printf(sb, " xs MBREXT xt %u", entry->ent.dp_typ);
387 	} else if (entry != NULL) {
388 		/* confxml: partition entry information */
389 		sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
390 		    entry->ent.dp_typ);
391 		if (entry->ent.dp_flag & 0x80)
392 			sbuf_printf(sb, "%s<attrib>active</attrib>\n", indent);
393 	} else {
394 		/* confxml: scheme information */
395 	}
396 }
397 
398 static int
399 g_part_ebr_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
400 {
401 	struct g_part_ebr_entry *entry;
402 
403 	/* Allow dumping to a FreeBSD partition or Linux swap partition only. */
404 	entry = (struct g_part_ebr_entry *)baseentry;
405 	return ((entry->ent.dp_typ == DOSPTYP_386BSD ||
406 	    entry->ent.dp_typ == DOSPTYP_LINSWP) ? 1 : 0);
407 }
408 
409 static int
410 g_part_ebr_modify(struct g_part_table *basetable,
411     struct g_part_entry *baseentry, struct g_part_parms *gpp)
412 {
413 	struct g_part_ebr_entry *entry;
414 
415 	if (gpp->gpp_parms & G_PART_PARM_LABEL)
416 		return (EINVAL);
417 
418 	entry = (struct g_part_ebr_entry *)baseentry;
419 	if (gpp->gpp_parms & G_PART_PARM_TYPE)
420 		return (ebr_parse_type(gpp->gpp_type, &entry->ent.dp_typ));
421 	return (0);
422 }
423 
424 static int
425 g_part_ebr_resize(struct g_part_table *basetable,
426     struct g_part_entry *baseentry, struct g_part_parms *gpp)
427 {
428 	struct g_provider *pp;
429 
430 	if (baseentry != NULL)
431 		return (EOPNOTSUPP);
432 	pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
433 	basetable->gpt_last = MIN(pp->mediasize / pp->sectorsize,
434 	    UINT32_MAX) - 1;
435 	return (0);
436 }
437 
438 static const char *
439 g_part_ebr_name(struct g_part_table *table, struct g_part_entry *entry,
440     char *buf, size_t bufsz)
441 {
442 	snprintf(buf, bufsz, EBRNAMFMT, entry->gpe_index);
443 	return (buf);
444 }
445 
446 static int
447 g_part_ebr_precheck(struct g_part_table *table, enum g_part_ctl req,
448     struct g_part_parms *gpp)
449 {
450 	/*
451 	 * The index is a function of the start of the partition.
452 	 * This is not something the user can override, nor is it
453 	 * something the common code will do right. We can set the
454 	 * index now so that we get what we need.
455 	 */
456 	if (req == G_PART_CTL_ADD)
457 		gpp->gpp_index = (gpp->gpp_start / table->gpt_sectors) + 1;
458 	return (0);
459 }
460 
461 static int
462 g_part_ebr_probe(struct g_part_table *table, struct g_consumer *cp)
463 {
464 	char type[64];
465 	struct g_provider *pp;
466 	u_char *buf, *p;
467 	int error, index, res;
468 	uint16_t magic;
469 
470 	pp = cp->provider;
471 
472 	/* Sanity-check the provider. */
473 	if (pp->sectorsize < EBRSIZE || pp->mediasize < pp->sectorsize)
474 		return (ENOSPC);
475 	if (pp->sectorsize > 4096)
476 		return (ENXIO);
477 
478 	/* Check that we have a parent and that it's a MBR. */
479 	if (table->gpt_depth == 0)
480 		return (ENXIO);
481 	error = g_getattr("PART::scheme", cp, &type);
482 	if (error != 0)
483 		return (error);
484 	if (strcmp(type, "MBR") != 0)
485 		return (ENXIO);
486 	/* Check that partition has type DOSPTYP_EBR. */
487 	error = g_getattr("PART::type", cp, &type);
488 	if (error != 0)
489 		return (error);
490 	if (strcmp(type, "ebr") != 0)
491 		return (ENXIO);
492 
493 	/* Check that there's a EBR. */
494 	buf = g_read_data(cp, 0L, pp->sectorsize, &error);
495 	if (buf == NULL)
496 		return (error);
497 
498 	/* We goto out on mismatch. */
499 	res = ENXIO;
500 
501 	magic = le16dec(buf + DOSMAGICOFFSET);
502 	if (magic != DOSMAGIC)
503 		goto out;
504 
505 	for (index = 0; index < 2; index++) {
506 		p = buf + DOSPARTOFF + index * DOSPARTSIZE;
507 		if (p[0] != 0 && p[0] != 0x80)
508 			goto out;
509 	}
510 	res = G_PART_PROBE_PRI_NORM;
511 
512  out:
513 	g_free(buf);
514 	return (res);
515 }
516 
517 static int
518 g_part_ebr_read(struct g_part_table *basetable, struct g_consumer *cp)
519 {
520 	struct dos_partition ent[2];
521 	struct g_provider *pp;
522 	struct g_part_entry *baseentry;
523 	struct g_part_ebr_table *table;
524 	struct g_part_ebr_entry *entry;
525 	u_char *buf;
526 	off_t ofs, msize;
527 	u_int lba, idx;
528 	int error, index;
529 
530 	idx = 5;
531 	pp = cp->provider;
532 	table = (struct g_part_ebr_table *)basetable;
533 	msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
534 
535 	lba = 0;
536 	while (1) {
537 		ofs = (off_t)lba * pp->sectorsize;
538 		buf = g_read_data(cp, ofs, pp->sectorsize, &error);
539 		if (buf == NULL)
540 			return (error);
541 
542 		ebr_entry_decode(buf + DOSPARTOFF + 0 * DOSPARTSIZE, ent + 0);
543 		ebr_entry_decode(buf + DOSPARTOFF + 1 * DOSPARTSIZE, ent + 1);
544 
545 		/* The 3rd & 4th entries should be zeroes. */
546 		if (le64dec(buf + DOSPARTOFF + 2 * DOSPARTSIZE) +
547 		    le64dec(buf + DOSPARTOFF + 3 * DOSPARTSIZE) != 0) {
548 			basetable->gpt_corrupt = 1;
549 			printf("GEOM: %s: invalid entries in the EBR ignored.\n",
550 			    pp->name);
551 		}
552 		/*
553 		 * Preserve EBR, it can contain boot code or other metadata we
554 		 * are ignorant of.
555 		 */
556 		if (lba == 0)
557 			memcpy(table->lba0_ebr, buf, sizeof(table->lba0_ebr));
558 
559 		if (ent[0].dp_typ == 0) {
560 			g_free(buf);
561 			break;
562 		}
563 
564 		if (ent[0].dp_typ == 5 && ent[1].dp_typ == 0) {
565 			lba = ent[0].dp_start;
566 			g_free(buf);
567 			continue;
568 		}
569 
570 		index = (lba / basetable->gpt_sectors) + 1;
571 		baseentry = (struct g_part_entry *)g_part_new_entry(basetable,
572 		    index, lba, lba + ent[0].dp_start + ent[0].dp_size - 1);
573 		baseentry->gpe_offset = (off_t)(lba + ent[0].dp_start) *
574 		    pp->sectorsize;
575 		entry = (struct g_part_ebr_entry *)baseentry;
576 		entry->ent = ent[0];
577 		memcpy(entry->ebr, buf, sizeof(entry->ebr));
578 		if (compat_aliases)
579 			entry->ebr_compat_idx = idx++;
580 		g_free(buf);
581 
582 		if (ent[1].dp_typ == 0)
583 			break;
584 
585 		lba = ent[1].dp_start;
586 	}
587 
588 	basetable->gpt_entries = msize / basetable->gpt_sectors;
589 	basetable->gpt_first = 0;
590 	basetable->gpt_last = msize - 1;
591 	return (0);
592 }
593 
594 static int
595 g_part_ebr_setunset(struct g_part_table *table, struct g_part_entry *baseentry,
596     const char *attrib, unsigned int set)
597 {
598 	struct g_part_entry *iter;
599 	struct g_part_ebr_entry *entry;
600 	int changed;
601 
602 	if (baseentry == NULL)
603 		return (ENODEV);
604 	if (strcasecmp(attrib, "active") != 0)
605 		return (EINVAL);
606 
607 	/* Only one entry can have the active attribute. */
608 	LIST_FOREACH(iter, &table->gpt_entry, gpe_entry) {
609 		if (iter->gpe_deleted)
610 			continue;
611 		changed = 0;
612 		entry = (struct g_part_ebr_entry *)iter;
613 		if (iter == baseentry) {
614 			if (set && (entry->ent.dp_flag & 0x80) == 0) {
615 				entry->ent.dp_flag |= 0x80;
616 				changed = 1;
617 			} else if (!set && (entry->ent.dp_flag & 0x80)) {
618 				entry->ent.dp_flag &= ~0x80;
619 				changed = 1;
620 			}
621 		} else {
622 			if (set && (entry->ent.dp_flag & 0x80)) {
623 				entry->ent.dp_flag &= ~0x80;
624 				changed = 1;
625 			}
626 		}
627 		if (changed && !iter->gpe_created)
628 			iter->gpe_modified = 1;
629 	}
630 	return (0);
631 }
632 
633 static const char *
634 g_part_ebr_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
635     char *buf, size_t bufsz)
636 {
637 	struct g_part_ebr_entry *entry;
638 	int i;
639 
640 	entry = (struct g_part_ebr_entry *)baseentry;
641 	for (i = 0; i < nitems(ebr_alias_match); i++) {
642 		if (ebr_alias_match[i].typ == entry->ent.dp_typ)
643 			return (g_part_alias_name(ebr_alias_match[i].alias));
644 	}
645 	snprintf(buf, bufsz, "!%d", entry->ent.dp_typ);
646 	return (buf);
647 }
648 
649 static int
650 g_part_ebr_write(struct g_part_table *basetable, struct g_consumer *cp)
651 {
652 	struct g_part_ebr_table *table;
653 	struct g_provider *pp;
654 	struct g_part_entry *baseentry, *next;
655 	struct g_part_ebr_entry *entry;
656 	u_char *buf;
657 	u_char *p;
658 	int error;
659 
660 	pp = cp->provider;
661 	buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
662 	table = (struct g_part_ebr_table *)basetable;
663 
664 	_Static_assert(DOSPARTOFF <= sizeof(table->lba0_ebr), "");
665 	memcpy(buf, table->lba0_ebr, DOSPARTOFF);
666 	le16enc(buf + DOSMAGICOFFSET, DOSMAGIC);
667 
668 	baseentry = LIST_FIRST(&basetable->gpt_entry);
669 	while (baseentry != NULL && baseentry->gpe_deleted)
670 		baseentry = LIST_NEXT(baseentry, gpe_entry);
671 
672 	/* Wipe-out the first EBR when there are no slices. */
673 	if (baseentry == NULL) {
674 		error = g_write_data(cp, 0, buf, pp->sectorsize);
675 		goto out;
676 	}
677 
678 	/*
679 	 * If the first partition is not in LBA 0, we need to
680 	 * put a "link" EBR in LBA 0.
681 	 */
682 	if (baseentry->gpe_start != 0) {
683 		ebr_entry_link(basetable, (uint32_t)baseentry->gpe_start,
684 		    (uint32_t)baseentry->gpe_end, buf + DOSPARTOFF);
685 		error = g_write_data(cp, 0, buf, pp->sectorsize);
686 		if (error)
687 			goto out;
688 	}
689 
690 	do {
691 		entry = (struct g_part_ebr_entry *)baseentry;
692 
693 		_Static_assert(DOSPARTOFF <= sizeof(entry->ebr), "");
694 		memcpy(buf, entry->ebr, DOSPARTOFF);
695 
696 		p = buf + DOSPARTOFF;
697 		p[0] = entry->ent.dp_flag;
698 		p[1] = entry->ent.dp_shd;
699 		p[2] = entry->ent.dp_ssect;
700 		p[3] = entry->ent.dp_scyl;
701 		p[4] = entry->ent.dp_typ;
702 		p[5] = entry->ent.dp_ehd;
703 		p[6] = entry->ent.dp_esect;
704 		p[7] = entry->ent.dp_ecyl;
705 		le32enc(p + 8, entry->ent.dp_start);
706 		le32enc(p + 12, entry->ent.dp_size);
707 
708 		next = LIST_NEXT(baseentry, gpe_entry);
709 		while (next != NULL && next->gpe_deleted)
710 			next = LIST_NEXT(next, gpe_entry);
711 
712 		p += DOSPARTSIZE;
713 		if (next != NULL)
714 			ebr_entry_link(basetable, (uint32_t)next->gpe_start,
715 			    (uint32_t)next->gpe_end, p);
716 		else
717 			bzero(p, DOSPARTSIZE);
718 
719 		error = g_write_data(cp, baseentry->gpe_start * pp->sectorsize,
720 		    buf, pp->sectorsize);
721 		baseentry = next;
722 	} while (!error && baseentry != NULL);
723 
724  out:
725 	g_free(buf);
726 	return (error);
727 }
728