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