2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2007-2009 Marcel Moolenaar
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
36 #include <sys/diskmbr.h>
37 #include <sys/endian.h>
38 #include <sys/kernel.h>
40 #include <sys/limits.h>
42 #include <sys/malloc.h>
43 #include <sys/mutex.h>
44 #include <sys/queue.h>
46 #include <sys/systm.h>
47 #include <sys/sysctl.h>
48 #include <geom/geom.h>
49 #include <geom/part/g_part.h>
51 #include "g_part_if.h"
53 FEATURE(geom_part_ebr,
54 "GEOM partitioning class for extended boot records support");
55 FEATURE(geom_part_ebr_compat,
56 "GEOM EBR partitioning class: backward-compatible partition names");
58 SYSCTL_DECL(_kern_geom_part);
59 static SYSCTL_NODE(_kern_geom_part, OID_AUTO, ebr, CTLFLAG_RW | CTLFLAG_MPSAFE,
60 0, "GEOM_PART_EBR Extended Boot Record");
62 static bool compat_aliases = true;
63 SYSCTL_BOOL(_kern_geom_part_ebr, OID_AUTO, compat_aliases,
64 CTLFLAG_RDTUN, &compat_aliases, 0,
65 "Set non-zero to enable EBR compatibility alias names (e.g., ada0p5)");
67 #define EBRNAMFMT "+%08u"
70 struct g_part_ebr_table {
71 struct g_part_table base;
72 u_char lba0_ebr[EBRSIZE];
75 struct g_part_ebr_entry {
76 struct g_part_entry base;
77 struct dos_partition ent;
82 static int g_part_ebr_add(struct g_part_table *, struct g_part_entry *,
83 struct g_part_parms *);
84 static void g_part_ebr_add_alias(struct g_part_table *, struct g_provider *,
85 struct g_part_entry *, const char *);
86 static int g_part_ebr_create(struct g_part_table *, struct g_part_parms *);
87 static int g_part_ebr_destroy(struct g_part_table *, struct g_part_parms *);
88 static void g_part_ebr_dumpconf(struct g_part_table *, struct g_part_entry *,
89 struct sbuf *, const char *);
90 static int g_part_ebr_dumpto(struct g_part_table *, struct g_part_entry *);
91 static int g_part_ebr_modify(struct g_part_table *, struct g_part_entry *,
92 struct g_part_parms *);
93 static const char *g_part_ebr_name(struct g_part_table *, struct g_part_entry *,
95 static struct g_provider *g_part_ebr_new_provider(struct g_part_table *,
96 struct g_geom *, struct g_part_entry *, const char *);
97 static int g_part_ebr_precheck(struct g_part_table *, enum g_part_ctl,
98 struct g_part_parms *);
99 static int g_part_ebr_probe(struct g_part_table *, struct g_consumer *);
100 static int g_part_ebr_read(struct g_part_table *, struct g_consumer *);
101 static int g_part_ebr_setunset(struct g_part_table *, struct g_part_entry *,
102 const char *, unsigned int);
103 static const char *g_part_ebr_type(struct g_part_table *, struct g_part_entry *,
105 static int g_part_ebr_write(struct g_part_table *, struct g_consumer *);
106 static int g_part_ebr_resize(struct g_part_table *, struct g_part_entry *,
107 struct g_part_parms *);
109 static kobj_method_t g_part_ebr_methods[] = {
110 KOBJMETHOD(g_part_add, g_part_ebr_add),
111 KOBJMETHOD(g_part_add_alias, g_part_ebr_add_alias),
112 KOBJMETHOD(g_part_create, g_part_ebr_create),
113 KOBJMETHOD(g_part_destroy, g_part_ebr_destroy),
114 KOBJMETHOD(g_part_dumpconf, g_part_ebr_dumpconf),
115 KOBJMETHOD(g_part_dumpto, g_part_ebr_dumpto),
116 KOBJMETHOD(g_part_modify, g_part_ebr_modify),
117 KOBJMETHOD(g_part_name, g_part_ebr_name),
118 KOBJMETHOD(g_part_new_provider, g_part_ebr_new_provider),
119 KOBJMETHOD(g_part_precheck, g_part_ebr_precheck),
120 KOBJMETHOD(g_part_probe, g_part_ebr_probe),
121 KOBJMETHOD(g_part_read, g_part_ebr_read),
122 KOBJMETHOD(g_part_resize, g_part_ebr_resize),
123 KOBJMETHOD(g_part_setunset, g_part_ebr_setunset),
124 KOBJMETHOD(g_part_type, g_part_ebr_type),
125 KOBJMETHOD(g_part_write, g_part_ebr_write),
129 static struct g_part_scheme g_part_ebr_scheme = {
132 sizeof(struct g_part_ebr_table),
133 .gps_entrysz = sizeof(struct g_part_ebr_entry),
135 .gps_maxent = INT_MAX,
137 G_PART_SCHEME_DECLARE(g_part_ebr);
138 MODULE_VERSION(geom_part_ebr, 0);
140 static struct g_part_ebr_alias {
143 } ebr_alias_match[] = {
144 { DOSPTYP_386BSD, G_PART_ALIAS_FREEBSD },
145 { DOSPTYP_EFI, G_PART_ALIAS_EFI },
146 { DOSPTYP_FAT32, G_PART_ALIAS_MS_FAT32 },
147 { DOSPTYP_FAT32LBA, G_PART_ALIAS_MS_FAT32LBA },
148 { DOSPTYP_LINLVM, G_PART_ALIAS_LINUX_LVM },
149 { DOSPTYP_LINRAID, G_PART_ALIAS_LINUX_RAID },
150 { DOSPTYP_LINSWP, G_PART_ALIAS_LINUX_SWAP },
151 { DOSPTYP_LINUX, G_PART_ALIAS_LINUX_DATA },
152 { DOSPTYP_NTFS, G_PART_ALIAS_MS_NTFS },
155 static void ebr_set_chs(struct g_part_table *, uint32_t, u_char *, u_char *,
159 ebr_entry_decode(const char *p, struct dos_partition *ent)
163 ent->dp_ssect = p[2];
167 ent->dp_esect = p[6];
169 ent->dp_start = le32dec(p + 8);
170 ent->dp_size = le32dec(p + 12);
174 ebr_entry_link(struct g_part_table *table, uint32_t start, uint32_t end,
178 buf[0] = 0 /* dp_flag */;
179 ebr_set_chs(table, start, &buf[3] /* dp_scyl */, &buf[1] /* dp_shd */,
180 &buf[2] /* dp_ssect */);
181 buf[4] = DOSPTYP_EXT /* dp_typ */;
182 ebr_set_chs(table, end, &buf[7] /* dp_ecyl */, &buf[5] /* dp_ehd */,
183 &buf[6] /* dp_esect */);
184 le32enc(buf + 8, start);
185 le32enc(buf + 12, end - start + 1);
189 ebr_parse_type(const char *type, u_char *dp_typ)
196 if (type[0] == '!') {
197 lt = strtol(type + 1, &endp, 0);
198 if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256)
200 *dp_typ = (u_char)lt;
203 for (i = 0; i < nitems(ebr_alias_match); i++) {
204 alias = g_part_alias_name(ebr_alias_match[i].alias);
205 if (strcasecmp(type, alias) == 0) {
206 *dp_typ = ebr_alias_match[i].typ;
214 ebr_set_chs(struct g_part_table *table, uint32_t lba, u_char *cylp, u_char *hdp,
217 uint32_t cyl, hd, sec;
219 sec = lba % table->gpt_sectors + 1;
220 lba /= table->gpt_sectors;
221 hd = lba % table->gpt_heads;
222 lba /= table->gpt_heads;
229 *secp = (sec & 0x3f) | ((cyl >> 2) & 0xc0);
233 ebr_align(struct g_part_table *basetable, uint32_t *start, uint32_t *size)
237 sectors = basetable->gpt_sectors;
238 if (*size < 2 * sectors)
240 if (*start % sectors) {
241 *size += (*start % sectors) - sectors;
242 *start -= (*start % sectors) - sectors;
245 *size -= (*size % sectors);
246 if (*size < 2 * sectors)
252 g_part_ebr_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
253 struct g_part_parms *gpp)
255 struct g_provider *pp;
256 struct g_part_ebr_entry *entry;
257 struct g_part_entry *iter;
258 uint32_t start, size;
261 if (gpp->gpp_parms & G_PART_PARM_LABEL)
264 pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
265 entry = (struct g_part_ebr_entry *)baseentry;
266 start = gpp->gpp_start;
267 size = gpp->gpp_size;
268 if (ebr_align(basetable, &start, &size) != 0)
270 if (baseentry->gpe_deleted)
271 bzero(&entry->ent, sizeof(entry->ent));
273 KASSERT(baseentry->gpe_start <= start, ("%s", __func__));
274 KASSERT(baseentry->gpe_end >= start + size - 1, ("%s", __func__));
275 baseentry->gpe_index = (start / basetable->gpt_sectors) + 1;
276 baseentry->gpe_offset =
277 (off_t)(start + basetable->gpt_sectors) * pp->sectorsize;
278 baseentry->gpe_start = start;
279 baseentry->gpe_end = start + size - 1;
280 entry->ent.dp_start = basetable->gpt_sectors;
281 entry->ent.dp_size = size - basetable->gpt_sectors;
282 ebr_set_chs(basetable, entry->ent.dp_start, &entry->ent.dp_scyl,
283 &entry->ent.dp_shd, &entry->ent.dp_ssect);
284 ebr_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl,
285 &entry->ent.dp_ehd, &entry->ent.dp_esect);
287 if (compat_aliases) {
289 LIST_FOREACH(iter, &basetable->gpt_entry, gpe_entry)
291 entry->ebr_compat_idx = idx;
293 return (ebr_parse_type(gpp->gpp_type, &entry->ent.dp_typ));
297 g_part_ebr_add_alias(struct g_part_table *table, struct g_provider *pp,
298 struct g_part_entry *baseentry, const char *pfx)
300 struct g_part_ebr_entry *entry;
302 g_provider_add_alias(pp, "%s%s" EBRNAMFMT, pfx, g_part_separator,
303 baseentry->gpe_index);
304 if (compat_aliases) {
305 entry = (struct g_part_ebr_entry *)baseentry;
306 g_provider_add_alias(pp, "%.*s%u", (int)strlen(pfx) - 1, pfx,
307 entry->ebr_compat_idx);
311 static struct g_provider *
312 g_part_ebr_new_provider(struct g_part_table *table, struct g_geom *gp,
313 struct g_part_entry *baseentry, const char *pfx)
315 struct g_part_ebr_entry *entry;
316 struct g_provider *pp;
318 pp = g_new_providerf(gp, "%s%s" EBRNAMFMT, pfx, g_part_separator,
319 baseentry->gpe_index);
320 if (compat_aliases) {
321 entry = (struct g_part_ebr_entry *)baseentry;
322 g_provider_add_alias(pp, "%.*s%u", (int)strlen(pfx) - 1, pfx,
323 entry->ebr_compat_idx);
329 g_part_ebr_create(struct g_part_table *basetable, struct g_part_parms *gpp)
332 struct g_consumer *cp;
333 struct g_provider *pp;
337 pp = gpp->gpp_provider;
339 if (pp->sectorsize < EBRSIZE)
341 if (pp->sectorsize > 4096)
344 /* Check that we have a parent and that it's a MBR. */
345 if (basetable->gpt_depth == 0)
347 cp = LIST_FIRST(&pp->consumers);
348 error = g_getattr("PART::scheme", cp, &type);
351 if (strcmp(type, "MBR") != 0)
353 error = g_getattr("PART::type", cp, &type);
356 if (strcmp(type, "ebr") != 0)
359 msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
360 basetable->gpt_first = 0;
361 basetable->gpt_last = msize - 1;
362 basetable->gpt_entries = msize / basetable->gpt_sectors;
367 g_part_ebr_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
370 /* Wipe the first sector to clear the partitioning. */
371 basetable->gpt_smhead |= 1;
376 g_part_ebr_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
377 struct sbuf *sb, const char *indent)
379 struct g_part_ebr_entry *entry;
381 entry = (struct g_part_ebr_entry *)baseentry;
382 if (indent == NULL) {
383 /* conftxt: libdisk compatibility */
384 sbuf_printf(sb, " xs MBREXT xt %u", entry->ent.dp_typ);
385 } else if (entry != NULL) {
386 /* confxml: partition entry information */
387 sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
389 if (entry->ent.dp_flag & 0x80)
390 sbuf_printf(sb, "%s<attrib>active</attrib>\n", indent);
392 /* confxml: scheme information */
397 g_part_ebr_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
399 struct g_part_ebr_entry *entry;
401 /* Allow dumping to a FreeBSD partition or Linux swap partition only. */
402 entry = (struct g_part_ebr_entry *)baseentry;
403 return ((entry->ent.dp_typ == DOSPTYP_386BSD ||
404 entry->ent.dp_typ == DOSPTYP_LINSWP) ? 1 : 0);
408 g_part_ebr_modify(struct g_part_table *basetable,
409 struct g_part_entry *baseentry, struct g_part_parms *gpp)
411 struct g_part_ebr_entry *entry;
413 if (gpp->gpp_parms & G_PART_PARM_LABEL)
416 entry = (struct g_part_ebr_entry *)baseentry;
417 if (gpp->gpp_parms & G_PART_PARM_TYPE)
418 return (ebr_parse_type(gpp->gpp_type, &entry->ent.dp_typ));
423 g_part_ebr_resize(struct g_part_table *basetable,
424 struct g_part_entry *baseentry, struct g_part_parms *gpp)
426 struct g_provider *pp;
428 if (baseentry != NULL)
430 pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
431 basetable->gpt_last = MIN(pp->mediasize / pp->sectorsize,
437 g_part_ebr_name(struct g_part_table *table, struct g_part_entry *entry,
438 char *buf, size_t bufsz)
440 snprintf(buf, bufsz, EBRNAMFMT, entry->gpe_index);
445 g_part_ebr_precheck(struct g_part_table *table, enum g_part_ctl req,
446 struct g_part_parms *gpp)
449 * The index is a function of the start of the partition.
450 * This is not something the user can override, nor is it
451 * something the common code will do right. We can set the
452 * index now so that we get what we need.
454 if (req == G_PART_CTL_ADD)
455 gpp->gpp_index = (gpp->gpp_start / table->gpt_sectors) + 1;
460 g_part_ebr_probe(struct g_part_table *table, struct g_consumer *cp)
463 struct g_provider *pp;
465 int error, index, res;
470 /* Sanity-check the provider. */
471 if (pp->sectorsize < EBRSIZE || pp->mediasize < pp->sectorsize)
473 if (pp->sectorsize > 4096)
476 /* Check that we have a parent and that it's a MBR. */
477 if (table->gpt_depth == 0)
479 error = g_getattr("PART::scheme", cp, &type);
482 if (strcmp(type, "MBR") != 0)
484 /* Check that partition has type DOSPTYP_EBR. */
485 error = g_getattr("PART::type", cp, &type);
488 if (strcmp(type, "ebr") != 0)
491 /* Check that there's a EBR. */
492 buf = g_read_data(cp, 0L, pp->sectorsize, &error);
496 /* We goto out on mismatch. */
499 magic = le16dec(buf + DOSMAGICOFFSET);
500 if (magic != DOSMAGIC)
503 for (index = 0; index < 2; index++) {
504 p = buf + DOSPARTOFF + index * DOSPARTSIZE;
505 if (p[0] != 0 && p[0] != 0x80)
508 res = G_PART_PROBE_PRI_NORM;
516 g_part_ebr_read(struct g_part_table *basetable, struct g_consumer *cp)
518 struct dos_partition ent[2];
519 struct g_provider *pp;
520 struct g_part_entry *baseentry;
521 struct g_part_ebr_table *table;
522 struct g_part_ebr_entry *entry;
530 table = (struct g_part_ebr_table *)basetable;
531 msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
535 ofs = (off_t)lba * pp->sectorsize;
536 buf = g_read_data(cp, ofs, pp->sectorsize, &error);
540 ebr_entry_decode(buf + DOSPARTOFF + 0 * DOSPARTSIZE, ent + 0);
541 ebr_entry_decode(buf + DOSPARTOFF + 1 * DOSPARTSIZE, ent + 1);
543 /* The 3rd & 4th entries should be zeroes. */
544 if (le64dec(buf + DOSPARTOFF + 2 * DOSPARTSIZE) +
545 le64dec(buf + DOSPARTOFF + 3 * DOSPARTSIZE) != 0) {
546 basetable->gpt_corrupt = 1;
547 printf("GEOM: %s: invalid entries in the EBR ignored.\n",
551 * Preserve EBR, it can contain boot code or other metadata we
555 memcpy(table->lba0_ebr, buf, sizeof(table->lba0_ebr));
557 if (ent[0].dp_typ == 0) {
562 if (ent[0].dp_typ == 5 && ent[1].dp_typ == 0) {
563 lba = ent[0].dp_start;
568 index = (lba / basetable->gpt_sectors) + 1;
569 baseentry = (struct g_part_entry *)g_part_new_entry(basetable,
570 index, lba, lba + ent[0].dp_start + ent[0].dp_size - 1);
571 baseentry->gpe_offset = (off_t)(lba + ent[0].dp_start) *
573 entry = (struct g_part_ebr_entry *)baseentry;
575 memcpy(entry->ebr, buf, sizeof(entry->ebr));
577 entry->ebr_compat_idx = idx++;
580 if (ent[1].dp_typ == 0)
583 lba = ent[1].dp_start;
586 basetable->gpt_entries = msize / basetable->gpt_sectors;
587 basetable->gpt_first = 0;
588 basetable->gpt_last = msize - 1;
593 g_part_ebr_setunset(struct g_part_table *table, struct g_part_entry *baseentry,
594 const char *attrib, unsigned int set)
596 struct g_part_entry *iter;
597 struct g_part_ebr_entry *entry;
600 if (baseentry == NULL)
602 if (strcasecmp(attrib, "active") != 0)
605 /* Only one entry can have the active attribute. */
606 LIST_FOREACH(iter, &table->gpt_entry, gpe_entry) {
607 if (iter->gpe_deleted)
610 entry = (struct g_part_ebr_entry *)iter;
611 if (iter == baseentry) {
612 if (set && (entry->ent.dp_flag & 0x80) == 0) {
613 entry->ent.dp_flag |= 0x80;
615 } else if (!set && (entry->ent.dp_flag & 0x80)) {
616 entry->ent.dp_flag &= ~0x80;
620 if (set && (entry->ent.dp_flag & 0x80)) {
621 entry->ent.dp_flag &= ~0x80;
625 if (changed && !iter->gpe_created)
626 iter->gpe_modified = 1;
632 g_part_ebr_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
633 char *buf, size_t bufsz)
635 struct g_part_ebr_entry *entry;
638 entry = (struct g_part_ebr_entry *)baseentry;
639 for (i = 0; i < nitems(ebr_alias_match); i++) {
640 if (ebr_alias_match[i].typ == entry->ent.dp_typ)
641 return (g_part_alias_name(ebr_alias_match[i].alias));
643 snprintf(buf, bufsz, "!%d", entry->ent.dp_typ);
648 g_part_ebr_write(struct g_part_table *basetable, struct g_consumer *cp)
650 struct g_part_ebr_table *table;
651 struct g_provider *pp;
652 struct g_part_entry *baseentry, *next;
653 struct g_part_ebr_entry *entry;
659 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
660 table = (struct g_part_ebr_table *)basetable;
662 _Static_assert(DOSPARTOFF <= sizeof(table->lba0_ebr), "");
663 memcpy(buf, table->lba0_ebr, DOSPARTOFF);
664 le16enc(buf + DOSMAGICOFFSET, DOSMAGIC);
666 baseentry = LIST_FIRST(&basetable->gpt_entry);
667 while (baseentry != NULL && baseentry->gpe_deleted)
668 baseentry = LIST_NEXT(baseentry, gpe_entry);
670 /* Wipe-out the first EBR when there are no slices. */
671 if (baseentry == NULL) {
672 error = g_write_data(cp, 0, buf, pp->sectorsize);
677 * If the first partition is not in LBA 0, we need to
678 * put a "link" EBR in LBA 0.
680 if (baseentry->gpe_start != 0) {
681 ebr_entry_link(basetable, (uint32_t)baseentry->gpe_start,
682 (uint32_t)baseentry->gpe_end, buf + DOSPARTOFF);
683 error = g_write_data(cp, 0, buf, pp->sectorsize);
689 entry = (struct g_part_ebr_entry *)baseentry;
691 _Static_assert(DOSPARTOFF <= sizeof(entry->ebr), "");
692 memcpy(buf, entry->ebr, DOSPARTOFF);
694 p = buf + DOSPARTOFF;
695 p[0] = entry->ent.dp_flag;
696 p[1] = entry->ent.dp_shd;
697 p[2] = entry->ent.dp_ssect;
698 p[3] = entry->ent.dp_scyl;
699 p[4] = entry->ent.dp_typ;
700 p[5] = entry->ent.dp_ehd;
701 p[6] = entry->ent.dp_esect;
702 p[7] = entry->ent.dp_ecyl;
703 le32enc(p + 8, entry->ent.dp_start);
704 le32enc(p + 12, entry->ent.dp_size);
706 next = LIST_NEXT(baseentry, gpe_entry);
707 while (next != NULL && next->gpe_deleted)
708 next = LIST_NEXT(next, gpe_entry);
712 ebr_entry_link(basetable, (uint32_t)next->gpe_start,
713 (uint32_t)next->gpe_end, p);
715 bzero(p, DOSPARTSIZE);
717 error = g_write_data(cp, baseentry->gpe_start * pp->sectorsize,
718 buf, pp->sectorsize);
720 } while (!error && baseentry != NULL);