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