2 * Copyright (c) 2007-2009 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/diskmbr.h>
33 #include <sys/endian.h>
34 #include <sys/kernel.h>
36 #include <sys/limits.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
40 #include <sys/queue.h>
42 #include <sys/systm.h>
43 #include <geom/geom.h>
44 #include <geom/part/g_part.h>
46 #include "g_part_if.h"
50 struct g_part_ebr_table {
51 struct g_part_table base;
54 struct g_part_ebr_entry {
55 struct g_part_entry base;
56 struct dos_partition ent;
60 static int g_part_ebr_add(struct g_part_table *, struct g_part_entry *,
61 struct g_part_parms *);
62 static int g_part_ebr_create(struct g_part_table *, struct g_part_parms *);
63 static int g_part_ebr_destroy(struct g_part_table *, struct g_part_parms *);
64 static int g_part_ebr_devalias(struct g_part_table *, struct g_part_entry *,
66 static void g_part_ebr_dumpconf(struct g_part_table *, struct g_part_entry *,
67 struct sbuf *, const char *);
68 static int g_part_ebr_dumpto(struct g_part_table *, struct g_part_entry *);
69 static int g_part_ebr_modify(struct g_part_table *, struct g_part_entry *,
70 struct g_part_parms *);
71 static const char *g_part_ebr_name(struct g_part_table *, struct g_part_entry *,
73 static int g_part_ebr_precheck(struct g_part_table *, enum g_part_ctl,
74 struct g_part_parms *);
75 static int g_part_ebr_probe(struct g_part_table *, struct g_consumer *);
76 static int g_part_ebr_read(struct g_part_table *, struct g_consumer *);
77 static int g_part_ebr_setunset(struct g_part_table *, struct g_part_entry *,
78 const char *, unsigned int);
79 static const char *g_part_ebr_type(struct g_part_table *, struct g_part_entry *,
81 static int g_part_ebr_write(struct g_part_table *, struct g_consumer *);
83 static kobj_method_t g_part_ebr_methods[] = {
84 KOBJMETHOD(g_part_add, g_part_ebr_add),
85 KOBJMETHOD(g_part_create, g_part_ebr_create),
86 KOBJMETHOD(g_part_destroy, g_part_ebr_destroy),
87 KOBJMETHOD(g_part_devalias, g_part_ebr_devalias),
88 KOBJMETHOD(g_part_dumpconf, g_part_ebr_dumpconf),
89 KOBJMETHOD(g_part_dumpto, g_part_ebr_dumpto),
90 KOBJMETHOD(g_part_modify, g_part_ebr_modify),
91 KOBJMETHOD(g_part_name, g_part_ebr_name),
92 KOBJMETHOD(g_part_precheck, g_part_ebr_precheck),
93 KOBJMETHOD(g_part_probe, g_part_ebr_probe),
94 KOBJMETHOD(g_part_read, g_part_ebr_read),
95 KOBJMETHOD(g_part_setunset, g_part_ebr_setunset),
96 KOBJMETHOD(g_part_type, g_part_ebr_type),
97 KOBJMETHOD(g_part_write, g_part_ebr_write),
101 static struct g_part_scheme g_part_ebr_scheme = {
104 sizeof(struct g_part_ebr_table),
105 .gps_entrysz = sizeof(struct g_part_ebr_entry),
107 .gps_maxent = INT_MAX,
109 G_PART_SCHEME_DECLARE(g_part_ebr);
111 static void ebr_set_chs(struct g_part_table *, uint32_t, u_char *, u_char *,
115 ebr_entry_decode(const char *p, struct dos_partition *ent)
119 ent->dp_ssect = p[2];
123 ent->dp_esect = p[6];
125 ent->dp_start = le32dec(p + 8);
126 ent->dp_size = le32dec(p + 12);
130 ebr_entry_link(struct g_part_table *table, uint32_t start, uint32_t end,
134 buf[0] = 0 /* dp_flag */;
135 ebr_set_chs(table, start, &buf[3] /* dp_scyl */, &buf[1] /* dp_shd */,
136 &buf[2] /* dp_ssect */);
137 buf[4] = 5 /* dp_typ */;
138 ebr_set_chs(table, end, &buf[7] /* dp_ecyl */, &buf[5] /* dp_ehd */,
139 &buf[6] /* dp_esect */);
140 le32enc(buf + 8, start);
141 le32enc(buf + 12, end - start + 1);
145 ebr_parse_type(const char *type, u_char *dp_typ)
151 if (type[0] == '!') {
152 lt = strtol(type + 1, &endp, 0);
153 if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256)
155 *dp_typ = (u_char)lt;
158 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD);
159 if (!strcasecmp(type, alias)) {
160 *dp_typ = DOSPTYP_386BSD;
167 ebr_set_chs(struct g_part_table *table, uint32_t lba, u_char *cylp, u_char *hdp,
170 uint32_t cyl, hd, sec;
172 sec = lba % table->gpt_sectors + 1;
173 lba /= table->gpt_sectors;
174 hd = lba % table->gpt_heads;
175 lba /= table->gpt_heads;
182 *secp = (sec & 0x3f) | ((cyl >> 2) & 0xc0);
186 g_part_ebr_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
187 struct g_part_parms *gpp)
190 struct g_provider *pp;
191 struct g_part_ebr_entry *entry;
192 uint32_t start, size, sectors;
194 if (gpp->gpp_parms & G_PART_PARM_LABEL)
197 gp = basetable->gpt_gp;
198 pp = LIST_FIRST(&gp->consumer)->provider;
199 sectors = basetable->gpt_sectors;
201 entry = (struct g_part_ebr_entry *)baseentry;
203 start = gpp->gpp_start;
204 size = gpp->gpp_size;
205 if (size < 2 * sectors)
207 if (start % sectors) {
208 size = size - sectors + (start % sectors);
209 start = start - (start % sectors) + sectors;
212 size = size - (size % sectors);
213 if (size < 2 * sectors)
216 if (baseentry->gpe_deleted)
217 bzero(&entry->ent, sizeof(entry->ent));
219 KASSERT(baseentry->gpe_start <= start, (__func__));
220 KASSERT(baseentry->gpe_end >= start + size - 1, (__func__));
221 baseentry->gpe_index = (start / sectors) + 1;
222 baseentry->gpe_offset = (off_t)(start + sectors) * pp->sectorsize;
223 baseentry->gpe_start = start;
224 baseentry->gpe_end = start + size - 1;
225 entry->ent.dp_start = sectors;
226 entry->ent.dp_size = size - sectors;
227 ebr_set_chs(basetable, entry->ent.dp_start, &entry->ent.dp_scyl,
228 &entry->ent.dp_shd, &entry->ent.dp_ssect);
229 ebr_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl,
230 &entry->ent.dp_ehd, &entry->ent.dp_esect);
231 return (ebr_parse_type(gpp->gpp_type, &entry->ent.dp_typ));
235 g_part_ebr_create(struct g_part_table *basetable, struct g_part_parms *gpp)
238 struct g_consumer *cp;
239 struct g_provider *pp;
243 pp = gpp->gpp_provider;
245 if (pp->sectorsize < EBRSIZE)
247 if (pp->sectorsize > 4096)
250 /* Check that we have a parent and that it's a MBR. */
251 if (basetable->gpt_depth == 0)
253 cp = LIST_FIRST(&pp->consumers);
254 error = g_getattr("PART::scheme", cp, &psn);
257 if (strcmp(psn, "MBR"))
260 msize = pp->mediasize / pp->sectorsize;
261 basetable->gpt_entries = msize / basetable->gpt_sectors;
262 basetable->gpt_first = 0;
263 basetable->gpt_last = msize - (msize % basetable->gpt_sectors) - 1;
268 g_part_ebr_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
271 /* Wipe the first sector to clear the partitioning. */
272 basetable->gpt_smhead |= 1;
277 g_part_ebr_devalias(struct g_part_table *table, struct g_part_entry *baseentry,
278 char *buf, size_t bufsz)
280 struct g_part_ebr_entry *entry;
283 entry = (struct g_part_ebr_entry *)baseentry;
284 if (entry->alias == 0)
287 len = strlcpy(buf, table->gpt_gp->name, bufsz);
291 snprintf(buf + len - 1, bufsz - len, "%d", entry->alias);
296 g_part_ebr_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
297 struct sbuf *sb, const char *indent)
299 struct g_part_ebr_entry *entry;
301 entry = (struct g_part_ebr_entry *)baseentry;
302 if (indent == NULL) {
303 /* conftxt: libdisk compatibility */
304 sbuf_printf(sb, " xs MBREXT xt %u", entry->ent.dp_typ);
305 } else if (entry != NULL) {
306 /* confxml: partition entry information */
307 sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
309 if (entry->ent.dp_flag & 0x80)
310 sbuf_printf(sb, "%s<attrib>active</attrib>\n", indent);
312 /* confxml: scheme information */
317 g_part_ebr_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
319 struct g_part_ebr_entry *entry;
321 /* Allow dumping to a FreeBSD partition only. */
322 entry = (struct g_part_ebr_entry *)baseentry;
323 return ((entry->ent.dp_typ == DOSPTYP_386BSD) ? 1 : 0);
327 g_part_ebr_modify(struct g_part_table *basetable,
328 struct g_part_entry *baseentry, struct g_part_parms *gpp)
330 struct g_part_ebr_entry *entry;
332 if (gpp->gpp_parms & G_PART_PARM_LABEL)
335 entry = (struct g_part_ebr_entry *)baseentry;
336 if (gpp->gpp_parms & G_PART_PARM_TYPE)
337 return (ebr_parse_type(gpp->gpp_type, &entry->ent.dp_typ));
342 g_part_ebr_name(struct g_part_table *table, struct g_part_entry *entry,
343 char *buf, size_t bufsz)
346 snprintf(buf, bufsz, ".%08u", entry->gpe_index);
351 g_part_ebr_precheck(struct g_part_table *table, enum g_part_ctl req,
352 struct g_part_parms *gpp)
356 * The index is a function of the start of the partition.
357 * This is not something the user can override, nor is it
358 * something the common code will do right. We can set the
359 * index now so that we get what we need.
361 if (req == G_PART_CTL_ADD)
362 gpp->gpp_index = (gpp->gpp_start / table->gpt_sectors) + 1;
368 g_part_ebr_probe(struct g_part_table *table, struct g_consumer *cp)
371 struct g_provider *pp;
373 int error, index, res, sum;
378 /* Sanity-check the provider. */
379 if (pp->sectorsize < EBRSIZE || pp->mediasize < pp->sectorsize)
381 if (pp->sectorsize > 4096)
384 /* Check that we have a parent and that it's a MBR. */
385 if (table->gpt_depth == 0)
387 error = g_getattr("PART::scheme", cp, &psn);
390 if (strcmp(psn, "MBR"))
393 /* Check that there's a EBR. */
394 buf = g_read_data(cp, 0L, pp->sectorsize, &error);
398 /* We goto out on mismatch. */
401 magic = le16dec(buf + DOSMAGICOFFSET);
402 if (magic != DOSMAGIC)
405 /* The sector is all zeroes, except for the partition entries. */
407 for (index = 0; index < DOSPARTOFF; index++)
412 for (index = 0; index < NDOSPART; index++) {
413 p = buf + DOSPARTOFF + index * DOSPARTSIZE;
414 if (p[0] != 0 && p[0] != 0x80)
418 /* The 3rd & 4th entries are always zero. */
419 if ((le64dec(p+0) + le64dec(p+8)) != 0)
423 res = G_PART_PROBE_PRI_HIGH;
431 g_part_ebr_read(struct g_part_table *basetable, struct g_consumer *cp)
433 struct dos_partition ent[2];
434 struct g_provider *pp;
435 struct g_part_entry *baseentry;
436 struct g_part_ebr_table *table;
437 struct g_part_ebr_entry *entry;
441 int alias, error, index;
444 table = (struct g_part_ebr_table *)basetable;
445 msize = pp->mediasize / pp->sectorsize;
450 ofs = (off_t)lba * pp->sectorsize;
451 buf = g_read_data(cp, ofs, pp->sectorsize, &error);
455 ebr_entry_decode(buf + DOSPARTOFF + 0 * DOSPARTSIZE, ent + 0);
456 ebr_entry_decode(buf + DOSPARTOFF + 1 * DOSPARTSIZE, ent + 1);
459 if (ent[0].dp_typ == 0)
462 if (ent[0].dp_typ == 5 && ent[1].dp_typ == 0) {
463 lba = ent[0].dp_start;
467 index = (lba / basetable->gpt_sectors) + 1;
468 baseentry = (struct g_part_entry *)g_part_new_entry(basetable,
469 index, lba, lba + ent[0].dp_start + ent[0].dp_size - 1);
470 baseentry->gpe_offset = (off_t)(lba + ent[0].dp_start) *
472 entry = (struct g_part_ebr_entry *)baseentry;
474 entry->alias = alias++;
476 if (ent[1].dp_typ == 0)
479 lba = ent[1].dp_start;
482 basetable->gpt_entries = msize / basetable->gpt_sectors;
483 basetable->gpt_first = 0;
484 basetable->gpt_last = msize - (msize % basetable->gpt_sectors) - 1;
489 g_part_ebr_setunset(struct g_part_table *table, struct g_part_entry *baseentry,
490 const char *attrib, unsigned int set)
492 struct g_part_entry *iter;
493 struct g_part_ebr_entry *entry;
496 if (strcasecmp(attrib, "active") != 0)
499 /* Only one entry can have the active attribute. */
500 LIST_FOREACH(iter, &table->gpt_entry, gpe_entry) {
501 if (iter->gpe_deleted)
504 entry = (struct g_part_ebr_entry *)iter;
505 if (iter == baseentry) {
506 if (set && (entry->ent.dp_flag & 0x80) == 0) {
507 entry->ent.dp_flag |= 0x80;
509 } else if (!set && (entry->ent.dp_flag & 0x80)) {
510 entry->ent.dp_flag &= ~0x80;
514 if (set && (entry->ent.dp_flag & 0x80)) {
515 entry->ent.dp_flag &= ~0x80;
519 if (changed && !iter->gpe_created)
520 iter->gpe_modified = 1;
526 g_part_ebr_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
527 char *buf, size_t bufsz)
529 struct g_part_ebr_entry *entry;
532 entry = (struct g_part_ebr_entry *)baseentry;
533 type = entry->ent.dp_typ;
534 if (type == DOSPTYP_386BSD)
535 return (g_part_alias_name(G_PART_ALIAS_FREEBSD));
536 snprintf(buf, bufsz, "!%d", type);
541 g_part_ebr_write(struct g_part_table *basetable, struct g_consumer *cp)
543 struct g_provider *pp;
544 struct g_part_entry *baseentry, *next;
545 struct g_part_ebr_entry *entry;
551 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
552 le16enc(buf + DOSMAGICOFFSET, DOSMAGIC);
554 baseentry = LIST_FIRST(&basetable->gpt_entry);
555 while (baseentry != NULL && baseentry->gpe_deleted)
556 baseentry = LIST_NEXT(baseentry, gpe_entry);
558 /* Wipe-out the the first EBR when there are no slices. */
559 if (baseentry == NULL) {
560 error = g_write_data(cp, 0, buf, pp->sectorsize);
565 * If the first partition is not in LBA 0, we need to
566 * put a "link" EBR in LBA 0.
568 if (baseentry->gpe_start != 0) {
569 ebr_entry_link(basetable, (uint32_t)baseentry->gpe_start,
570 (uint32_t)baseentry->gpe_end, buf + DOSPARTOFF);
571 error = g_write_data(cp, 0, buf, pp->sectorsize);
577 entry = (struct g_part_ebr_entry *)baseentry;
579 p = buf + DOSPARTOFF;
580 p[0] = entry->ent.dp_flag;
581 p[1] = entry->ent.dp_shd;
582 p[2] = entry->ent.dp_ssect;
583 p[3] = entry->ent.dp_scyl;
584 p[4] = entry->ent.dp_typ;
585 p[5] = entry->ent.dp_ehd;
586 p[6] = entry->ent.dp_esect;
587 p[7] = entry->ent.dp_ecyl;
588 le32enc(p + 8, entry->ent.dp_start);
589 le32enc(p + 12, entry->ent.dp_size);
591 next = LIST_NEXT(baseentry, gpe_entry);
592 while (next != NULL && next->gpe_deleted)
593 next = LIST_NEXT(next, gpe_entry);
597 ebr_entry_link(basetable, (uint32_t)next->gpe_start,
598 (uint32_t)next->gpe_end, p);
600 bzero(p, DOSPARTSIZE);
602 error = g_write_data(cp, baseentry->gpe_start * pp->sectorsize,
603 buf, pp->sectorsize);
606 } while (!error && baseentry != NULL);