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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/diskmbr.h>
35 #include <sys/endian.h>
36 #include <sys/kernel.h>
38 #include <sys/limits.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
42 #include <sys/queue.h>
44 #include <sys/systm.h>
45 #include <geom/geom.h>
46 #include <geom/part/g_part.h>
48 #include "g_part_if.h"
52 struct g_part_ebr_table {
53 struct g_part_table base;
54 #ifndef GEOM_PART_EBR_COMPAT
59 struct g_part_ebr_entry {
60 struct g_part_entry base;
61 struct dos_partition ent;
64 static int g_part_ebr_add(struct g_part_table *, struct g_part_entry *,
65 struct g_part_parms *);
66 static int g_part_ebr_create(struct g_part_table *, struct g_part_parms *);
67 static int g_part_ebr_destroy(struct g_part_table *, struct g_part_parms *);
68 static void g_part_ebr_dumpconf(struct g_part_table *, struct g_part_entry *,
69 struct sbuf *, const char *);
70 static int g_part_ebr_dumpto(struct g_part_table *, struct g_part_entry *);
71 #if defined(GEOM_PART_EBR_COMPAT)
72 static void g_part_ebr_fullname(struct g_part_table *, struct g_part_entry *,
73 struct sbuf *, const char *);
75 static int g_part_ebr_modify(struct g_part_table *, struct g_part_entry *,
76 struct g_part_parms *);
77 static const char *g_part_ebr_name(struct g_part_table *, struct g_part_entry *,
79 static int g_part_ebr_precheck(struct g_part_table *, enum g_part_ctl,
80 struct g_part_parms *);
81 static int g_part_ebr_probe(struct g_part_table *, struct g_consumer *);
82 static int g_part_ebr_read(struct g_part_table *, struct g_consumer *);
83 static int g_part_ebr_setunset(struct g_part_table *, struct g_part_entry *,
84 const char *, unsigned int);
85 static const char *g_part_ebr_type(struct g_part_table *, struct g_part_entry *,
87 static int g_part_ebr_write(struct g_part_table *, struct g_consumer *);
89 static kobj_method_t g_part_ebr_methods[] = {
90 KOBJMETHOD(g_part_add, g_part_ebr_add),
91 KOBJMETHOD(g_part_create, g_part_ebr_create),
92 KOBJMETHOD(g_part_destroy, g_part_ebr_destroy),
93 KOBJMETHOD(g_part_dumpconf, g_part_ebr_dumpconf),
94 KOBJMETHOD(g_part_dumpto, g_part_ebr_dumpto),
95 #if defined(GEOM_PART_EBR_COMPAT)
96 KOBJMETHOD(g_part_fullname, g_part_ebr_fullname),
98 KOBJMETHOD(g_part_modify, g_part_ebr_modify),
99 KOBJMETHOD(g_part_name, g_part_ebr_name),
100 KOBJMETHOD(g_part_precheck, g_part_ebr_precheck),
101 KOBJMETHOD(g_part_probe, g_part_ebr_probe),
102 KOBJMETHOD(g_part_read, g_part_ebr_read),
103 KOBJMETHOD(g_part_setunset, g_part_ebr_setunset),
104 KOBJMETHOD(g_part_type, g_part_ebr_type),
105 KOBJMETHOD(g_part_write, g_part_ebr_write),
109 static struct g_part_scheme g_part_ebr_scheme = {
112 sizeof(struct g_part_ebr_table),
113 .gps_entrysz = sizeof(struct g_part_ebr_entry),
115 .gps_maxent = INT_MAX,
117 G_PART_SCHEME_DECLARE(g_part_ebr);
119 static struct g_part_ebr_alias {
122 } ebr_alias_match[] = {
123 { DOSPTYP_386BSD, G_PART_ALIAS_FREEBSD },
124 { DOSPTYP_NTFS, G_PART_ALIAS_MS_NTFS },
125 { DOSPTYP_FAT32, G_PART_ALIAS_MS_FAT32 },
126 { DOSPTYP_LINSWP, G_PART_ALIAS_LINUX_SWAP },
127 { DOSPTYP_LINUX, G_PART_ALIAS_LINUX_DATA },
128 { DOSPTYP_LINLVM, G_PART_ALIAS_LINUX_LVM },
129 { DOSPTYP_LINRAID, G_PART_ALIAS_LINUX_RAID },
132 static void ebr_set_chs(struct g_part_table *, uint32_t, u_char *, u_char *,
136 ebr_entry_decode(const char *p, struct dos_partition *ent)
140 ent->dp_ssect = p[2];
144 ent->dp_esect = p[6];
146 ent->dp_start = le32dec(p + 8);
147 ent->dp_size = le32dec(p + 12);
151 ebr_entry_link(struct g_part_table *table, uint32_t start, uint32_t end,
155 buf[0] = 0 /* dp_flag */;
156 ebr_set_chs(table, start, &buf[3] /* dp_scyl */, &buf[1] /* dp_shd */,
157 &buf[2] /* dp_ssect */);
158 buf[4] = 5 /* dp_typ */;
159 ebr_set_chs(table, end, &buf[7] /* dp_ecyl */, &buf[5] /* dp_ehd */,
160 &buf[6] /* dp_esect */);
161 le32enc(buf + 8, start);
162 le32enc(buf + 12, end - start + 1);
166 ebr_parse_type(const char *type, u_char *dp_typ)
173 if (type[0] == '!') {
174 lt = strtol(type + 1, &endp, 0);
175 if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256)
177 *dp_typ = (u_char)lt;
181 i < sizeof(ebr_alias_match) / sizeof(ebr_alias_match[0]); i++) {
182 alias = g_part_alias_name(ebr_alias_match[i].alias);
183 if (strcasecmp(type, alias) == 0) {
184 *dp_typ = ebr_alias_match[i].typ;
193 ebr_set_chs(struct g_part_table *table, uint32_t lba, u_char *cylp, u_char *hdp,
196 uint32_t cyl, hd, sec;
198 sec = lba % table->gpt_sectors + 1;
199 lba /= table->gpt_sectors;
200 hd = lba % table->gpt_heads;
201 lba /= table->gpt_heads;
208 *secp = (sec & 0x3f) | ((cyl >> 2) & 0xc0);
212 g_part_ebr_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
213 struct g_part_parms *gpp)
216 struct g_provider *pp;
217 struct g_part_ebr_entry *entry;
218 uint32_t start, size, sectors;
220 if (gpp->gpp_parms & G_PART_PARM_LABEL)
223 gp = basetable->gpt_gp;
224 pp = LIST_FIRST(&gp->consumer)->provider;
225 sectors = basetable->gpt_sectors;
227 entry = (struct g_part_ebr_entry *)baseentry;
229 start = gpp->gpp_start;
230 size = gpp->gpp_size;
231 if (size < 2 * sectors)
233 if (start % sectors) {
234 size = size - sectors + (start % sectors);
235 start = start - (start % sectors) + sectors;
238 size = size - (size % sectors);
239 if (size < 2 * sectors)
242 if (baseentry->gpe_deleted)
243 bzero(&entry->ent, sizeof(entry->ent));
245 KASSERT(baseentry->gpe_start <= start, (__func__));
246 KASSERT(baseentry->gpe_end >= start + size - 1, (__func__));
247 baseentry->gpe_index = (start / sectors) + 1;
248 baseentry->gpe_offset = (off_t)(start + sectors) * pp->sectorsize;
249 baseentry->gpe_start = start;
250 baseentry->gpe_end = start + size - 1;
251 entry->ent.dp_start = sectors;
252 entry->ent.dp_size = size - sectors;
253 ebr_set_chs(basetable, entry->ent.dp_start, &entry->ent.dp_scyl,
254 &entry->ent.dp_shd, &entry->ent.dp_ssect);
255 ebr_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl,
256 &entry->ent.dp_ehd, &entry->ent.dp_esect);
257 return (ebr_parse_type(gpp->gpp_type, &entry->ent.dp_typ));
261 g_part_ebr_create(struct g_part_table *basetable, struct g_part_parms *gpp)
264 struct g_consumer *cp;
265 struct g_provider *pp;
269 pp = gpp->gpp_provider;
271 if (pp->sectorsize < EBRSIZE)
273 if (pp->sectorsize > 4096)
276 /* Check that we have a parent and that it's a MBR. */
277 if (basetable->gpt_depth == 0)
279 cp = LIST_FIRST(&pp->consumers);
280 error = g_getattr("PART::scheme", cp, &psn);
283 if (strcmp(psn, "MBR"))
286 msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
287 msize -= msize % basetable->gpt_sectors;
288 basetable->gpt_first = 0;
289 basetable->gpt_last = msize - 1;
290 basetable->gpt_entries = msize / basetable->gpt_sectors;
295 g_part_ebr_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
298 /* Wipe the first sector to clear the partitioning. */
299 basetable->gpt_smhead |= 1;
304 g_part_ebr_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
305 struct sbuf *sb, const char *indent)
307 struct g_part_ebr_entry *entry;
309 entry = (struct g_part_ebr_entry *)baseentry;
310 if (indent == NULL) {
311 /* conftxt: libdisk compatibility */
312 sbuf_printf(sb, " xs MBREXT xt %u", entry->ent.dp_typ);
313 } else if (entry != NULL) {
314 /* confxml: partition entry information */
315 sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
317 if (entry->ent.dp_flag & 0x80)
318 sbuf_printf(sb, "%s<attrib>active</attrib>\n", indent);
320 /* confxml: scheme information */
325 g_part_ebr_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
327 struct g_part_ebr_entry *entry;
329 /* Allow dumping to a FreeBSD partition or Linux swap partition only. */
330 entry = (struct g_part_ebr_entry *)baseentry;
331 return ((entry->ent.dp_typ == DOSPTYP_386BSD ||
332 entry->ent.dp_typ == DOSPTYP_LINSWP) ? 1 : 0);
335 #if defined(GEOM_PART_EBR_COMPAT)
337 g_part_ebr_fullname(struct g_part_table *table, struct g_part_entry *entry,
338 struct sbuf *sb, const char *pfx)
340 struct g_part_entry *iter;
344 LIST_FOREACH(iter, &table->gpt_entry, gpe_entry) {
349 sbuf_printf(sb, "%.*s%u", (int)strlen(pfx) - 1, pfx, idx);
354 g_part_ebr_modify(struct g_part_table *basetable,
355 struct g_part_entry *baseentry, struct g_part_parms *gpp)
357 struct g_part_ebr_entry *entry;
359 if (gpp->gpp_parms & G_PART_PARM_LABEL)
362 entry = (struct g_part_ebr_entry *)baseentry;
363 if (gpp->gpp_parms & G_PART_PARM_TYPE)
364 return (ebr_parse_type(gpp->gpp_type, &entry->ent.dp_typ));
369 g_part_ebr_name(struct g_part_table *table, struct g_part_entry *entry,
370 char *buf, size_t bufsz)
373 snprintf(buf, bufsz, "+%08u", entry->gpe_index);
378 g_part_ebr_precheck(struct g_part_table *table, enum g_part_ctl req,
379 struct g_part_parms *gpp)
381 #if defined(GEOM_PART_EBR_COMPAT)
385 * The index is a function of the start of the partition.
386 * This is not something the user can override, nor is it
387 * something the common code will do right. We can set the
388 * index now so that we get what we need.
390 if (req == G_PART_CTL_ADD)
391 gpp->gpp_index = (gpp->gpp_start / table->gpt_sectors) + 1;
397 g_part_ebr_probe(struct g_part_table *table, struct g_consumer *cp)
400 struct g_provider *pp;
402 int error, index, res;
407 /* Sanity-check the provider. */
408 if (pp->sectorsize < EBRSIZE || pp->mediasize < pp->sectorsize)
410 if (pp->sectorsize > 4096)
413 /* Check that we have a parent and that it's a MBR. */
414 if (table->gpt_depth == 0)
416 error = g_getattr("PART::scheme", cp, &psn);
419 if (strcmp(psn, "MBR"))
422 /* Check that there's a EBR. */
423 buf = g_read_data(cp, 0L, pp->sectorsize, &error);
427 /* We goto out on mismatch. */
430 magic = le16dec(buf + DOSMAGICOFFSET);
431 if (magic != DOSMAGIC)
434 for (index = 0; index < 2; index++) {
435 p = buf + DOSPARTOFF + index * DOSPARTSIZE;
436 if (p[0] != 0 && p[0] != 0x80)
439 res = G_PART_PROBE_PRI_NORM;
447 g_part_ebr_read(struct g_part_table *basetable, struct g_consumer *cp)
449 struct dos_partition ent[2];
450 struct g_provider *pp;
451 struct g_part_entry *baseentry;
452 struct g_part_ebr_table *table;
453 struct g_part_ebr_entry *entry;
460 table = (struct g_part_ebr_table *)basetable;
461 msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
465 ofs = (off_t)lba * pp->sectorsize;
466 buf = g_read_data(cp, ofs, pp->sectorsize, &error);
470 ebr_entry_decode(buf + DOSPARTOFF + 0 * DOSPARTSIZE, ent + 0);
471 ebr_entry_decode(buf + DOSPARTOFF + 1 * DOSPARTSIZE, ent + 1);
473 /* The 3rd & 4th entries should be zeroes. */
474 if (le64dec(buf + DOSPARTOFF + 2 * DOSPARTSIZE) +
475 le64dec(buf + DOSPARTOFF + 3 * DOSPARTSIZE) != 0) {
476 basetable->gpt_corrupt = 1;
477 printf("GEOM: %s: invalid entries in the EBR ignored.\n",
480 #ifndef GEOM_PART_EBR_COMPAT
481 /* Save the first EBR, it can contain a boot code */
483 bcopy(buf, table->ebr, sizeof(table->ebr));
487 if (ent[0].dp_typ == 0)
490 if (ent[0].dp_typ == 5 && ent[1].dp_typ == 0) {
491 lba = ent[0].dp_start;
495 index = (lba / basetable->gpt_sectors) + 1;
496 baseentry = (struct g_part_entry *)g_part_new_entry(basetable,
497 index, lba, lba + ent[0].dp_start + ent[0].dp_size - 1);
498 baseentry->gpe_offset = (off_t)(lba + ent[0].dp_start) *
500 entry = (struct g_part_ebr_entry *)baseentry;
503 if (ent[1].dp_typ == 0)
506 lba = ent[1].dp_start;
509 basetable->gpt_entries = msize / basetable->gpt_sectors;
510 basetable->gpt_first = 0;
511 basetable->gpt_last = msize - (msize % basetable->gpt_sectors) - 1;
516 g_part_ebr_setunset(struct g_part_table *table, struct g_part_entry *baseentry,
517 const char *attrib, unsigned int set)
519 struct g_part_entry *iter;
520 struct g_part_ebr_entry *entry;
523 if (strcasecmp(attrib, "active") != 0)
526 /* Only one entry can have the active attribute. */
527 LIST_FOREACH(iter, &table->gpt_entry, gpe_entry) {
528 if (iter->gpe_deleted)
531 entry = (struct g_part_ebr_entry *)iter;
532 if (iter == baseentry) {
533 if (set && (entry->ent.dp_flag & 0x80) == 0) {
534 entry->ent.dp_flag |= 0x80;
536 } else if (!set && (entry->ent.dp_flag & 0x80)) {
537 entry->ent.dp_flag &= ~0x80;
541 if (set && (entry->ent.dp_flag & 0x80)) {
542 entry->ent.dp_flag &= ~0x80;
546 if (changed && !iter->gpe_created)
547 iter->gpe_modified = 1;
553 g_part_ebr_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
554 char *buf, size_t bufsz)
556 struct g_part_ebr_entry *entry;
559 entry = (struct g_part_ebr_entry *)baseentry;
561 i < sizeof(ebr_alias_match) / sizeof(ebr_alias_match[0]); i++) {
562 if (ebr_alias_match[i].typ == entry->ent.dp_typ)
563 return (g_part_alias_name(ebr_alias_match[i].alias));
565 snprintf(buf, bufsz, "!%d", entry->ent.dp_typ);
570 g_part_ebr_write(struct g_part_table *basetable, struct g_consumer *cp)
572 #ifndef GEOM_PART_EBR_COMPAT
573 struct g_part_ebr_table *table;
575 struct g_provider *pp;
576 struct g_part_entry *baseentry, *next;
577 struct g_part_ebr_entry *entry;
583 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
584 #ifndef GEOM_PART_EBR_COMPAT
585 table = (struct g_part_ebr_table *)basetable;
586 bcopy(table->ebr, buf, DOSPARTOFF);
588 le16enc(buf + DOSMAGICOFFSET, DOSMAGIC);
590 baseentry = LIST_FIRST(&basetable->gpt_entry);
591 while (baseentry != NULL && baseentry->gpe_deleted)
592 baseentry = LIST_NEXT(baseentry, gpe_entry);
594 /* Wipe-out the first EBR when there are no slices. */
595 if (baseentry == NULL) {
596 error = g_write_data(cp, 0, buf, pp->sectorsize);
601 * If the first partition is not in LBA 0, we need to
602 * put a "link" EBR in LBA 0.
604 if (baseentry->gpe_start != 0) {
605 ebr_entry_link(basetable, (uint32_t)baseentry->gpe_start,
606 (uint32_t)baseentry->gpe_end, buf + DOSPARTOFF);
607 error = g_write_data(cp, 0, buf, pp->sectorsize);
613 entry = (struct g_part_ebr_entry *)baseentry;
615 p = buf + DOSPARTOFF;
616 p[0] = entry->ent.dp_flag;
617 p[1] = entry->ent.dp_shd;
618 p[2] = entry->ent.dp_ssect;
619 p[3] = entry->ent.dp_scyl;
620 p[4] = entry->ent.dp_typ;
621 p[5] = entry->ent.dp_ehd;
622 p[6] = entry->ent.dp_esect;
623 p[7] = entry->ent.dp_ecyl;
624 le32enc(p + 8, entry->ent.dp_start);
625 le32enc(p + 12, entry->ent.dp_size);
627 next = LIST_NEXT(baseentry, gpe_entry);
628 while (next != NULL && next->gpe_deleted)
629 next = LIST_NEXT(next, gpe_entry);
633 ebr_entry_link(basetable, (uint32_t)next->gpe_start,
634 (uint32_t)next->gpe_end, p);
636 bzero(p, DOSPARTSIZE);
638 error = g_write_data(cp, baseentry->gpe_start * pp->sectorsize,
639 buf, pp->sectorsize);
640 #ifndef GEOM_PART_EBR_COMPAT
641 if (baseentry->gpe_start == 0)
642 bzero(buf, DOSPARTOFF);
645 } while (!error && baseentry != NULL);