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