xref: /freebsd/sys/geom/part/g_part_ebr.c (revision f8b865d1d62d17626ab993212963277c06cc25b8)
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_NTFS,		G_PART_ALIAS_MS_NTFS },
139 	{ DOSPTYP_FAT32,	G_PART_ALIAS_MS_FAT32 },
140 	{ DOSPTYP_LINSWP,	G_PART_ALIAS_LINUX_SWAP },
141 	{ DOSPTYP_LINUX,	G_PART_ALIAS_LINUX_DATA },
142 	{ DOSPTYP_LINLVM,	G_PART_ALIAS_LINUX_LVM },
143 	{ DOSPTYP_LINRAID,	G_PART_ALIAS_LINUX_RAID },
144 };
145 
146 static void ebr_set_chs(struct g_part_table *, uint32_t, u_char *, u_char *,
147     u_char *);
148 
149 static void
150 ebr_entry_decode(const char *p, struct dos_partition *ent)
151 {
152 	ent->dp_flag = p[0];
153 	ent->dp_shd = p[1];
154 	ent->dp_ssect = p[2];
155 	ent->dp_scyl = p[3];
156 	ent->dp_typ = p[4];
157 	ent->dp_ehd = p[5];
158 	ent->dp_esect = p[6];
159 	ent->dp_ecyl = p[7];
160 	ent->dp_start = le32dec(p + 8);
161 	ent->dp_size = le32dec(p + 12);
162 }
163 
164 static void
165 ebr_entry_link(struct g_part_table *table, uint32_t start, uint32_t end,
166    u_char *buf)
167 {
168 
169 	buf[0] = 0 /* dp_flag */;
170 	ebr_set_chs(table, start, &buf[3] /* dp_scyl */, &buf[1] /* dp_shd */,
171 	    &buf[2] /* dp_ssect */);
172 	buf[4] = 5 /* dp_typ */;
173 	ebr_set_chs(table, end, &buf[7] /* dp_ecyl */, &buf[5] /* dp_ehd */,
174 	    &buf[6] /* dp_esect */);
175 	le32enc(buf + 8, start);
176 	le32enc(buf + 12, end - start + 1);
177 }
178 
179 static int
180 ebr_parse_type(const char *type, u_char *dp_typ)
181 {
182 	const char *alias;
183 	char *endp;
184 	long lt;
185 	int i;
186 
187 	if (type[0] == '!') {
188 		lt = strtol(type + 1, &endp, 0);
189 		if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256)
190 			return (EINVAL);
191 		*dp_typ = (u_char)lt;
192 		return (0);
193 	}
194 	for (i = 0; i < nitems(ebr_alias_match); i++) {
195 		alias = g_part_alias_name(ebr_alias_match[i].alias);
196 		if (strcasecmp(type, alias) == 0) {
197 			*dp_typ = ebr_alias_match[i].typ;
198 			return (0);
199 		}
200 	}
201 	return (EINVAL);
202 }
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 
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 	uint32_t start, size;
251 
252 	if (gpp->gpp_parms & G_PART_PARM_LABEL)
253 		return (EINVAL);
254 
255 	pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
256 	entry = (struct g_part_ebr_entry *)baseentry;
257 	start = gpp->gpp_start;
258 	size = gpp->gpp_size;
259 	if (ebr_align(basetable, &start, &size) != 0)
260 		return (EINVAL);
261 	if (baseentry->gpe_deleted)
262 		bzero(&entry->ent, sizeof(entry->ent));
263 
264 	KASSERT(baseentry->gpe_start <= start, ("%s", __func__));
265 	KASSERT(baseentry->gpe_end >= start + size - 1, ("%s", __func__));
266 	baseentry->gpe_index = (start / basetable->gpt_sectors) + 1;
267 	baseentry->gpe_offset =
268 	    (off_t)(start + basetable->gpt_sectors) * pp->sectorsize;
269 	baseentry->gpe_start = start;
270 	baseentry->gpe_end = start + size - 1;
271 	entry->ent.dp_start = basetable->gpt_sectors;
272 	entry->ent.dp_size = size - basetable->gpt_sectors;
273 	ebr_set_chs(basetable, entry->ent.dp_start, &entry->ent.dp_scyl,
274 	    &entry->ent.dp_shd, &entry->ent.dp_ssect);
275 	ebr_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl,
276 	    &entry->ent.dp_ehd, &entry->ent.dp_esect);
277 	return (ebr_parse_type(gpp->gpp_type, &entry->ent.dp_typ));
278 }
279 
280 static int
281 g_part_ebr_create(struct g_part_table *basetable, struct g_part_parms *gpp)
282 {
283 	char type[64];
284 	struct g_consumer *cp;
285 	struct g_provider *pp;
286 	uint32_t msize;
287 	int error;
288 
289 	pp = gpp->gpp_provider;
290 
291 	if (pp->sectorsize < EBRSIZE)
292 		return (ENOSPC);
293 	if (pp->sectorsize > 4096)
294 		return (ENXIO);
295 
296 	/* Check that we have a parent and that it's a MBR. */
297 	if (basetable->gpt_depth == 0)
298 		return (ENXIO);
299 	cp = LIST_FIRST(&pp->consumers);
300 	error = g_getattr("PART::scheme", cp, &type);
301 	if (error != 0)
302 		return (error);
303 	if (strcmp(type, "MBR") != 0)
304 		return (ENXIO);
305 	error = g_getattr("PART::type", cp, &type);
306 	if (error != 0)
307 		return (error);
308 	if (strcmp(type, "ebr") != 0)
309 		return (ENXIO);
310 
311 	msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
312 	basetable->gpt_first = 0;
313 	basetable->gpt_last = msize - 1;
314 	basetable->gpt_entries = msize / basetable->gpt_sectors;
315 	return (0);
316 }
317 
318 static int
319 g_part_ebr_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
320 {
321 
322 	/* Wipe the first sector to clear the partitioning. */
323 	basetable->gpt_smhead |= 1;
324 	return (0);
325 }
326 
327 static void
328 g_part_ebr_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
329     struct sbuf *sb, const char *indent)
330 {
331 	struct g_part_ebr_entry *entry;
332 
333 	entry = (struct g_part_ebr_entry *)baseentry;
334 	if (indent == NULL) {
335 		/* conftxt: libdisk compatibility */
336 		sbuf_printf(sb, " xs MBREXT xt %u", entry->ent.dp_typ);
337 	} else if (entry != NULL) {
338 		/* confxml: partition entry information */
339 		sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
340 		    entry->ent.dp_typ);
341 		if (entry->ent.dp_flag & 0x80)
342 			sbuf_printf(sb, "%s<attrib>active</attrib>\n", indent);
343 	} else {
344 		/* confxml: scheme information */
345 	}
346 }
347 
348 static int
349 g_part_ebr_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
350 {
351 	struct g_part_ebr_entry *entry;
352 
353 	/* Allow dumping to a FreeBSD partition or Linux swap partition only. */
354 	entry = (struct g_part_ebr_entry *)baseentry;
355 	return ((entry->ent.dp_typ == DOSPTYP_386BSD ||
356 	    entry->ent.dp_typ == DOSPTYP_LINSWP) ? 1 : 0);
357 }
358 
359 #if defined(GEOM_PART_EBR_COMPAT)
360 static void
361 g_part_ebr_fullname(struct g_part_table *table, struct g_part_entry *entry,
362     struct sbuf *sb, const char *pfx)
363 {
364 	struct g_part_entry *iter;
365 	u_int idx;
366 
367 	idx = 5;
368 	LIST_FOREACH(iter, &table->gpt_entry, gpe_entry) {
369 		if (iter == entry)
370 			break;
371 		idx++;
372 	}
373 	sbuf_printf(sb, "%.*s%u", (int)strlen(pfx) - 1, pfx, idx);
374 }
375 #endif
376 
377 static int
378 g_part_ebr_modify(struct g_part_table *basetable,
379     struct g_part_entry *baseentry, struct g_part_parms *gpp)
380 {
381 	struct g_part_ebr_entry *entry;
382 
383 	if (gpp->gpp_parms & G_PART_PARM_LABEL)
384 		return (EINVAL);
385 
386 	entry = (struct g_part_ebr_entry *)baseentry;
387 	if (gpp->gpp_parms & G_PART_PARM_TYPE)
388 		return (ebr_parse_type(gpp->gpp_type, &entry->ent.dp_typ));
389 	return (0);
390 }
391 
392 static int
393 g_part_ebr_resize(struct g_part_table *basetable,
394     struct g_part_entry *baseentry, struct g_part_parms *gpp)
395 {
396 	struct g_provider *pp;
397 
398 	if (baseentry != NULL)
399 		return (EOPNOTSUPP);
400 	pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
401 	basetable->gpt_last = MIN(pp->mediasize / pp->sectorsize,
402 	    UINT32_MAX) - 1;
403 	return (0);
404 }
405 
406 static const char *
407 g_part_ebr_name(struct g_part_table *table, struct g_part_entry *entry,
408     char *buf, size_t bufsz)
409 {
410 
411 	snprintf(buf, bufsz, "+%08u", entry->gpe_index);
412 	return (buf);
413 }
414 
415 static int
416 g_part_ebr_precheck(struct g_part_table *table, enum g_part_ctl req,
417     struct g_part_parms *gpp)
418 {
419 #if defined(GEOM_PART_EBR_COMPAT)
420 	if (req == G_PART_CTL_DESTROY)
421 		return (0);
422 	return (ECANCELED);
423 #else
424 	/*
425 	 * The index is a function of the start of the partition.
426 	 * This is not something the user can override, nor is it
427 	 * something the common code will do right. We can set the
428 	 * index now so that we get what we need.
429 	 */
430 	if (req == G_PART_CTL_ADD)
431 		gpp->gpp_index = (gpp->gpp_start / table->gpt_sectors) + 1;
432 	return (0);
433 #endif
434 }
435 
436 static int
437 g_part_ebr_probe(struct g_part_table *table, struct g_consumer *cp)
438 {
439 	char type[64];
440 	struct g_provider *pp;
441 	u_char *buf, *p;
442 	int error, index, res;
443 	uint16_t magic;
444 
445 	pp = cp->provider;
446 
447 	/* Sanity-check the provider. */
448 	if (pp->sectorsize < EBRSIZE || pp->mediasize < pp->sectorsize)
449 		return (ENOSPC);
450 	if (pp->sectorsize > 4096)
451 		return (ENXIO);
452 
453 	/* Check that we have a parent and that it's a MBR. */
454 	if (table->gpt_depth == 0)
455 		return (ENXIO);
456 	error = g_getattr("PART::scheme", cp, &type);
457 	if (error != 0)
458 		return (error);
459 	if (strcmp(type, "MBR") != 0)
460 		return (ENXIO);
461 	/* Check that partition has type DOSPTYP_EBR. */
462 	error = g_getattr("PART::type", cp, &type);
463 	if (error != 0)
464 		return (error);
465 	if (strcmp(type, "ebr") != 0)
466 		return (ENXIO);
467 
468 	/* Check that there's a EBR. */
469 	buf = g_read_data(cp, 0L, pp->sectorsize, &error);
470 	if (buf == NULL)
471 		return (error);
472 
473 	/* We goto out on mismatch. */
474 	res = ENXIO;
475 
476 	magic = le16dec(buf + DOSMAGICOFFSET);
477 	if (magic != DOSMAGIC)
478 		goto out;
479 
480 	for (index = 0; index < 2; index++) {
481 		p = buf + DOSPARTOFF + index * DOSPARTSIZE;
482 		if (p[0] != 0 && p[0] != 0x80)
483 			goto out;
484 	}
485 	res = G_PART_PROBE_PRI_NORM;
486 
487  out:
488 	g_free(buf);
489 	return (res);
490 }
491 
492 static int
493 g_part_ebr_read(struct g_part_table *basetable, struct g_consumer *cp)
494 {
495 	struct dos_partition ent[2];
496 	struct g_provider *pp;
497 	struct g_part_entry *baseentry;
498 	struct g_part_ebr_table *table;
499 	struct g_part_ebr_entry *entry;
500 	u_char *buf;
501 	off_t ofs, msize;
502 	u_int lba;
503 	int error, index;
504 
505 	pp = cp->provider;
506 	table = (struct g_part_ebr_table *)basetable;
507 	msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
508 
509 	lba = 0;
510 	while (1) {
511 		ofs = (off_t)lba * pp->sectorsize;
512 		buf = g_read_data(cp, ofs, pp->sectorsize, &error);
513 		if (buf == NULL)
514 			return (error);
515 
516 		ebr_entry_decode(buf + DOSPARTOFF + 0 * DOSPARTSIZE, ent + 0);
517 		ebr_entry_decode(buf + DOSPARTOFF + 1 * DOSPARTSIZE, ent + 1);
518 
519 		/* The 3rd & 4th entries should be zeroes. */
520 		if (le64dec(buf + DOSPARTOFF + 2 * DOSPARTSIZE) +
521 		    le64dec(buf + DOSPARTOFF + 3 * DOSPARTSIZE) != 0) {
522 			basetable->gpt_corrupt = 1;
523 			printf("GEOM: %s: invalid entries in the EBR ignored.\n",
524 			    pp->name);
525 		}
526 #ifndef GEOM_PART_EBR_COMPAT
527 		/* Save the first EBR, it can contain a boot code */
528 		if (lba == 0)
529 			bcopy(buf, table->ebr, sizeof(table->ebr));
530 #endif
531 		g_free(buf);
532 
533 		if (ent[0].dp_typ == 0)
534 			break;
535 
536 		if (ent[0].dp_typ == 5 && ent[1].dp_typ == 0) {
537 			lba = ent[0].dp_start;
538 			continue;
539 		}
540 
541 		index = (lba / basetable->gpt_sectors) + 1;
542 		baseentry = (struct g_part_entry *)g_part_new_entry(basetable,
543 		    index, lba, lba + ent[0].dp_start + ent[0].dp_size - 1);
544 		baseentry->gpe_offset = (off_t)(lba + ent[0].dp_start) *
545 		    pp->sectorsize;
546 		entry = (struct g_part_ebr_entry *)baseentry;
547 		entry->ent = ent[0];
548 
549 		if (ent[1].dp_typ == 0)
550 			break;
551 
552 		lba = ent[1].dp_start;
553 	}
554 
555 	basetable->gpt_entries = msize / basetable->gpt_sectors;
556 	basetable->gpt_first = 0;
557 	basetable->gpt_last = msize - 1;
558 	return (0);
559 }
560 
561 static int
562 g_part_ebr_setunset(struct g_part_table *table, struct g_part_entry *baseentry,
563     const char *attrib, unsigned int set)
564 {
565 	struct g_part_entry *iter;
566 	struct g_part_ebr_entry *entry;
567 	int changed;
568 
569 	if (baseentry == NULL)
570 		return (ENODEV);
571 	if (strcasecmp(attrib, "active") != 0)
572 		return (EINVAL);
573 
574 	/* Only one entry can have the active attribute. */
575 	LIST_FOREACH(iter, &table->gpt_entry, gpe_entry) {
576 		if (iter->gpe_deleted)
577 			continue;
578 		changed = 0;
579 		entry = (struct g_part_ebr_entry *)iter;
580 		if (iter == baseentry) {
581 			if (set && (entry->ent.dp_flag & 0x80) == 0) {
582 				entry->ent.dp_flag |= 0x80;
583 				changed = 1;
584 			} else if (!set && (entry->ent.dp_flag & 0x80)) {
585 				entry->ent.dp_flag &= ~0x80;
586 				changed = 1;
587 			}
588 		} else {
589 			if (set && (entry->ent.dp_flag & 0x80)) {
590 				entry->ent.dp_flag &= ~0x80;
591 				changed = 1;
592 			}
593 		}
594 		if (changed && !iter->gpe_created)
595 			iter->gpe_modified = 1;
596 	}
597 	return (0);
598 }
599 
600 static const char *
601 g_part_ebr_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
602     char *buf, size_t bufsz)
603 {
604 	struct g_part_ebr_entry *entry;
605 	int i;
606 
607 	entry = (struct g_part_ebr_entry *)baseentry;
608 	for (i = 0; i < nitems(ebr_alias_match); i++) {
609 		if (ebr_alias_match[i].typ == entry->ent.dp_typ)
610 			return (g_part_alias_name(ebr_alias_match[i].alias));
611 	}
612 	snprintf(buf, bufsz, "!%d", entry->ent.dp_typ);
613 	return (buf);
614 }
615 
616 static int
617 g_part_ebr_write(struct g_part_table *basetable, struct g_consumer *cp)
618 {
619 #ifndef GEOM_PART_EBR_COMPAT
620 	struct g_part_ebr_table *table;
621 #endif
622 	struct g_provider *pp;
623 	struct g_part_entry *baseentry, *next;
624 	struct g_part_ebr_entry *entry;
625 	u_char *buf;
626 	u_char *p;
627 	int error;
628 
629 	pp = cp->provider;
630 	buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
631 #ifndef GEOM_PART_EBR_COMPAT
632 	table = (struct g_part_ebr_table *)basetable;
633 	bcopy(table->ebr, buf, DOSPARTOFF);
634 #endif
635 	le16enc(buf + DOSMAGICOFFSET, DOSMAGIC);
636 
637 	baseentry = LIST_FIRST(&basetable->gpt_entry);
638 	while (baseentry != NULL && baseentry->gpe_deleted)
639 		baseentry = LIST_NEXT(baseentry, gpe_entry);
640 
641 	/* Wipe-out the first EBR when there are no slices. */
642 	if (baseentry == NULL) {
643 		error = g_write_data(cp, 0, buf, pp->sectorsize);
644 		goto out;
645 	}
646 
647 	/*
648 	 * If the first partition is not in LBA 0, we need to
649 	 * put a "link" EBR in LBA 0.
650 	 */
651 	if (baseentry->gpe_start != 0) {
652 		ebr_entry_link(basetable, (uint32_t)baseentry->gpe_start,
653 		    (uint32_t)baseentry->gpe_end, buf + DOSPARTOFF);
654 		error = g_write_data(cp, 0, buf, pp->sectorsize);
655 		if (error)
656 			goto out;
657 	}
658 
659 	do {
660 		entry = (struct g_part_ebr_entry *)baseentry;
661 
662 		p = buf + DOSPARTOFF;
663 		p[0] = entry->ent.dp_flag;
664 		p[1] = entry->ent.dp_shd;
665 		p[2] = entry->ent.dp_ssect;
666 		p[3] = entry->ent.dp_scyl;
667 		p[4] = entry->ent.dp_typ;
668 		p[5] = entry->ent.dp_ehd;
669 		p[6] = entry->ent.dp_esect;
670 		p[7] = entry->ent.dp_ecyl;
671 		le32enc(p + 8, entry->ent.dp_start);
672 		le32enc(p + 12, entry->ent.dp_size);
673 
674 		next = LIST_NEXT(baseentry, gpe_entry);
675 		while (next != NULL && next->gpe_deleted)
676 			next = LIST_NEXT(next, gpe_entry);
677 
678 		p += DOSPARTSIZE;
679 		if (next != NULL)
680 			ebr_entry_link(basetable, (uint32_t)next->gpe_start,
681 			    (uint32_t)next->gpe_end, p);
682 		else
683 			bzero(p, DOSPARTSIZE);
684 
685 		error = g_write_data(cp, baseentry->gpe_start * pp->sectorsize,
686 		    buf, pp->sectorsize);
687 #ifndef GEOM_PART_EBR_COMPAT
688 		if (baseentry->gpe_start == 0)
689 			bzero(buf, DOSPARTOFF);
690 #endif
691 		baseentry = next;
692 	} while (!error && baseentry != NULL);
693 
694  out:
695 	g_free(buf);
696 	return (error);
697 }
698