2 * Copyright (c) 2007, 2008 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 <sys/sysctl.h>
44 #include <geom/geom.h>
45 #include <geom/geom_int.h>
46 #include <geom/part/g_part.h>
48 #include "g_part_if.h"
50 FEATURE(geom_part_mbr, "GEOM partitioning class for MBR support");
52 SYSCTL_DECL(_kern_geom_part);
53 static SYSCTL_NODE(_kern_geom_part, OID_AUTO, mbr, CTLFLAG_RW, 0,
54 "GEOM_PART_MBR Master Boot Record");
56 static u_int enforce_chs = 0;
57 SYSCTL_UINT(_kern_geom_part_mbr, OID_AUTO, enforce_chs,
58 CTLFLAG_RWTUN, &enforce_chs, 0, "Enforce alignment to CHS addressing");
62 struct g_part_mbr_table {
63 struct g_part_table base;
67 struct g_part_mbr_entry {
68 struct g_part_entry base;
69 struct dos_partition ent;
72 static int g_part_mbr_add(struct g_part_table *, struct g_part_entry *,
73 struct g_part_parms *);
74 static int g_part_mbr_bootcode(struct g_part_table *, struct g_part_parms *);
75 static int g_part_mbr_create(struct g_part_table *, struct g_part_parms *);
76 static int g_part_mbr_destroy(struct g_part_table *, struct g_part_parms *);
77 static void g_part_mbr_dumpconf(struct g_part_table *, struct g_part_entry *,
78 struct sbuf *, const char *);
79 static int g_part_mbr_dumpto(struct g_part_table *, struct g_part_entry *);
80 static int g_part_mbr_modify(struct g_part_table *, struct g_part_entry *,
81 struct g_part_parms *);
82 static const char *g_part_mbr_name(struct g_part_table *, struct g_part_entry *,
84 static int g_part_mbr_probe(struct g_part_table *, struct g_consumer *);
85 static int g_part_mbr_read(struct g_part_table *, struct g_consumer *);
86 static int g_part_mbr_setunset(struct g_part_table *, struct g_part_entry *,
87 const char *, unsigned int);
88 static const char *g_part_mbr_type(struct g_part_table *, struct g_part_entry *,
90 static int g_part_mbr_write(struct g_part_table *, struct g_consumer *);
91 static int g_part_mbr_resize(struct g_part_table *, struct g_part_entry *,
92 struct g_part_parms *);
94 static kobj_method_t g_part_mbr_methods[] = {
95 KOBJMETHOD(g_part_add, g_part_mbr_add),
96 KOBJMETHOD(g_part_bootcode, g_part_mbr_bootcode),
97 KOBJMETHOD(g_part_create, g_part_mbr_create),
98 KOBJMETHOD(g_part_destroy, g_part_mbr_destroy),
99 KOBJMETHOD(g_part_dumpconf, g_part_mbr_dumpconf),
100 KOBJMETHOD(g_part_dumpto, g_part_mbr_dumpto),
101 KOBJMETHOD(g_part_modify, g_part_mbr_modify),
102 KOBJMETHOD(g_part_resize, g_part_mbr_resize),
103 KOBJMETHOD(g_part_name, g_part_mbr_name),
104 KOBJMETHOD(g_part_probe, g_part_mbr_probe),
105 KOBJMETHOD(g_part_read, g_part_mbr_read),
106 KOBJMETHOD(g_part_setunset, g_part_mbr_setunset),
107 KOBJMETHOD(g_part_type, g_part_mbr_type),
108 KOBJMETHOD(g_part_write, g_part_mbr_write),
112 static struct g_part_scheme g_part_mbr_scheme = {
115 sizeof(struct g_part_mbr_table),
116 .gps_entrysz = sizeof(struct g_part_mbr_entry),
117 .gps_minent = NDOSPART,
118 .gps_maxent = NDOSPART,
119 .gps_bootcodesz = MBRSIZE,
121 G_PART_SCHEME_DECLARE(g_part_mbr);
123 static struct g_part_mbr_alias {
126 } mbr_alias_match[] = {
127 { DOSPTYP_386BSD, G_PART_ALIAS_FREEBSD },
128 { DOSPTYP_EXT, G_PART_ALIAS_EBR },
129 { DOSPTYP_NTFS, G_PART_ALIAS_MS_NTFS },
130 { DOSPTYP_FAT16, G_PART_ALIAS_MS_FAT16 },
131 { DOSPTYP_FAT32, G_PART_ALIAS_MS_FAT32 },
132 { DOSPTYP_EXTLBA, G_PART_ALIAS_EBR },
133 { DOSPTYP_LDM, G_PART_ALIAS_MS_LDM_DATA },
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 { DOSPTYP_PPCBOOT, G_PART_ALIAS_PREP_BOOT },
139 { DOSPTYP_VMFS, G_PART_ALIAS_VMFS },
140 { DOSPTYP_VMKDIAG, G_PART_ALIAS_VMKDIAG },
141 { DOSPTYP_APPLE_UFS, G_PART_ALIAS_APPLE_UFS },
142 { DOSPTYP_APPLE_BOOT, G_PART_ALIAS_APPLE_BOOT },
143 { DOSPTYP_HFS, G_PART_ALIAS_APPLE_HFS },
147 mbr_parse_type(const char *type, u_char *dp_typ)
154 if (type[0] == '!') {
155 lt = strtol(type + 1, &endp, 0);
156 if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256)
158 *dp_typ = (u_char)lt;
161 for (i = 0; i < nitems(mbr_alias_match); i++) {
162 alias = g_part_alias_name(mbr_alias_match[i].alias);
163 if (strcasecmp(type, alias) == 0) {
164 *dp_typ = mbr_alias_match[i].typ;
172 mbr_probe_bpb(u_char *bpb)
177 #define PO2(x) ((x & (x - 1)) == 0)
178 secsz = le16dec(bpb);
179 if (secsz < 512 || secsz > 4096 || !PO2(secsz))
182 if (clstsz < 1 || clstsz > 128 || !PO2(clstsz))
190 mbr_set_chs(struct g_part_table *table, uint32_t lba, u_char *cylp, u_char *hdp,
193 uint32_t cyl, hd, sec;
195 sec = lba % table->gpt_sectors + 1;
196 lba /= table->gpt_sectors;
197 hd = lba % table->gpt_heads;
198 lba /= table->gpt_heads;
205 *secp = (sec & 0x3f) | ((cyl >> 2) & 0xc0);
209 mbr_align(struct g_part_table *basetable, uint32_t *start, uint32_t *size)
213 if (enforce_chs == 0)
215 sectors = basetable->gpt_sectors;
218 if (start != NULL && (*start % sectors)) {
219 *size += (*start % sectors) - sectors;
220 *start -= (*start % sectors) - sectors;
223 *size -= (*size % sectors);
230 g_part_mbr_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
231 struct g_part_parms *gpp)
233 struct g_part_mbr_entry *entry;
234 uint32_t start, size;
236 if (gpp->gpp_parms & G_PART_PARM_LABEL)
239 entry = (struct g_part_mbr_entry *)baseentry;
240 start = gpp->gpp_start;
241 size = gpp->gpp_size;
242 if (mbr_align(basetable, &start, &size) != 0)
244 if (baseentry->gpe_deleted)
245 bzero(&entry->ent, sizeof(entry->ent));
247 KASSERT(baseentry->gpe_start <= start, ("%s", __func__));
248 KASSERT(baseentry->gpe_end >= start + size - 1, ("%s", __func__));
249 baseentry->gpe_start = start;
250 baseentry->gpe_end = start + size - 1;
251 entry->ent.dp_start = start;
252 entry->ent.dp_size = size;
253 mbr_set_chs(basetable, baseentry->gpe_start, &entry->ent.dp_scyl,
254 &entry->ent.dp_shd, &entry->ent.dp_ssect);
255 mbr_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl,
256 &entry->ent.dp_ehd, &entry->ent.dp_esect);
257 return (mbr_parse_type(gpp->gpp_type, &entry->ent.dp_typ));
261 g_part_mbr_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
263 struct g_part_mbr_table *table;
266 if (gpp->gpp_codesize != MBRSIZE)
269 table = (struct g_part_mbr_table *)basetable;
270 dsn = *(uint32_t *)(table->mbr + DOSDSNOFF);
271 bcopy(gpp->gpp_codeptr, table->mbr, DOSPARTOFF);
273 *(uint32_t *)(table->mbr + DOSDSNOFF) = dsn;
278 g_part_mbr_create(struct g_part_table *basetable, struct g_part_parms *gpp)
280 struct g_provider *pp;
281 struct g_part_mbr_table *table;
283 pp = gpp->gpp_provider;
284 if (pp->sectorsize < MBRSIZE)
287 basetable->gpt_first = basetable->gpt_sectors;
288 basetable->gpt_last = MIN(pp->mediasize / pp->sectorsize,
291 table = (struct g_part_mbr_table *)basetable;
292 le16enc(table->mbr + DOSMAGICOFFSET, DOSMAGIC);
297 g_part_mbr_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
300 /* Wipe the first sector to clear the partitioning. */
301 basetable->gpt_smhead |= 1;
306 g_part_mbr_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
307 struct sbuf *sb, const char *indent)
309 struct g_part_mbr_entry *entry;
311 entry = (struct g_part_mbr_entry *)baseentry;
312 if (indent == NULL) {
313 /* conftxt: libdisk compatibility */
314 sbuf_printf(sb, " xs MBR xt %u", entry->ent.dp_typ);
315 } else if (entry != NULL) {
316 /* confxml: partition entry information */
317 sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
319 if (entry->ent.dp_flag & 0x80)
320 sbuf_printf(sb, "%s<attrib>active</attrib>\n", indent);
322 /* confxml: scheme information */
327 g_part_mbr_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
329 struct g_part_mbr_entry *entry;
331 /* Allow dumping to a FreeBSD partition or Linux swap partition only. */
332 entry = (struct g_part_mbr_entry *)baseentry;
333 return ((entry->ent.dp_typ == DOSPTYP_386BSD ||
334 entry->ent.dp_typ == DOSPTYP_LINSWP) ? 1 : 0);
338 g_part_mbr_modify(struct g_part_table *basetable,
339 struct g_part_entry *baseentry, struct g_part_parms *gpp)
341 struct g_part_mbr_entry *entry;
343 if (gpp->gpp_parms & G_PART_PARM_LABEL)
346 entry = (struct g_part_mbr_entry *)baseentry;
347 if (gpp->gpp_parms & G_PART_PARM_TYPE)
348 return (mbr_parse_type(gpp->gpp_type, &entry->ent.dp_typ));
353 g_part_mbr_resize(struct g_part_table *basetable,
354 struct g_part_entry *baseentry, struct g_part_parms *gpp)
356 struct g_part_mbr_entry *entry;
357 struct g_provider *pp;
360 if (baseentry == NULL) {
361 pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
362 basetable->gpt_last = MIN(pp->mediasize / pp->sectorsize,
366 size = gpp->gpp_size;
367 if (mbr_align(basetable, NULL, &size) != 0)
369 /* XXX: prevent unexpected shrinking. */
370 pp = baseentry->gpe_pp;
371 if ((g_debugflags & 0x10) == 0 && size < gpp->gpp_size &&
372 pp->mediasize / pp->sectorsize > size)
374 entry = (struct g_part_mbr_entry *)baseentry;
375 baseentry->gpe_end = baseentry->gpe_start + size - 1;
376 entry->ent.dp_size = size;
377 mbr_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl,
378 &entry->ent.dp_ehd, &entry->ent.dp_esect);
383 g_part_mbr_name(struct g_part_table *table, struct g_part_entry *baseentry,
384 char *buf, size_t bufsz)
387 snprintf(buf, bufsz, "s%d", baseentry->gpe_index);
392 g_part_mbr_probe(struct g_part_table *table, struct g_consumer *cp)
395 struct g_provider *pp;
397 int error, index, res, sum;
402 /* Sanity-check the provider. */
403 if (pp->sectorsize < MBRSIZE || pp->mediasize < pp->sectorsize)
405 if (pp->sectorsize > 4096)
408 /* We don't nest under an MBR (see EBR instead). */
409 error = g_getattr("PART::scheme", cp, &psn);
410 if (error == 0 && strcmp(psn, g_part_mbr_scheme.name) == 0)
413 /* Check that there's a MBR. */
414 buf = g_read_data(cp, 0L, pp->sectorsize, &error);
418 /* We goto out on mismatch. */
421 magic = le16dec(buf + DOSMAGICOFFSET);
422 if (magic != DOSMAGIC)
425 for (index = 0; index < NDOSPART; index++) {
426 p = buf + DOSPARTOFF + index * DOSPARTSIZE;
427 if (p[0] != 0 && p[0] != 0x80)
432 * If the partition table does not consist of all zeroes,
433 * assume we have a MBR. If it's all zeroes, we could have
434 * a boot sector. For example, a boot sector that doesn't
435 * have boot code -- common on non-i386 hardware. In that
436 * case we check if we have a possible BPB. If so, then we
437 * assume we have a boot sector instead.
440 for (index = 0; index < NDOSPART * DOSPARTSIZE; index++)
441 sum += buf[DOSPARTOFF + index];
442 if (sum != 0 || !mbr_probe_bpb(buf + 0x0b))
443 res = G_PART_PROBE_PRI_NORM;
451 g_part_mbr_read(struct g_part_table *basetable, struct g_consumer *cp)
453 struct dos_partition ent;
454 struct g_provider *pp;
455 struct g_part_mbr_table *table;
456 struct g_part_mbr_entry *entry;
458 off_t chs, msize, first;
459 u_int sectors, heads;
463 table = (struct g_part_mbr_table *)basetable;
464 first = basetable->gpt_sectors;
465 msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
467 buf = g_read_data(cp, 0L, pp->sectorsize, &error);
471 bcopy(buf, table->mbr, sizeof(table->mbr));
472 for (index = NDOSPART - 1; index >= 0; index--) {
473 p = buf + DOSPARTOFF + index * DOSPARTSIZE;
482 ent.dp_start = le32dec(p + 8);
483 ent.dp_size = le32dec(p + 12);
484 if (ent.dp_typ == 0 || ent.dp_typ == DOSPTYP_PMBR)
486 if (ent.dp_start == 0 || ent.dp_size == 0)
488 sectors = ent.dp_esect & 0x3f;
489 if (sectors > basetable->gpt_sectors &&
490 !basetable->gpt_fixgeom) {
491 g_part_geometry_heads(msize, sectors, &chs, &heads);
493 basetable->gpt_sectors = sectors;
494 basetable->gpt_heads = heads;
497 if (ent.dp_start < first)
498 first = ent.dp_start;
499 entry = (struct g_part_mbr_entry *)g_part_new_entry(basetable,
500 index + 1, ent.dp_start, ent.dp_start + ent.dp_size - 1);
504 basetable->gpt_entries = NDOSPART;
505 basetable->gpt_first = basetable->gpt_sectors;
506 basetable->gpt_last = msize - 1;
508 if (first < basetable->gpt_first)
509 basetable->gpt_first = 1;
516 g_part_mbr_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_mbr_entry *entry;
523 if (baseentry == NULL)
525 if (strcasecmp(attrib, "active") != 0)
528 /* Only one entry can have the active attribute. */
529 LIST_FOREACH(iter, &table->gpt_entry, gpe_entry) {
530 if (iter->gpe_deleted)
533 entry = (struct g_part_mbr_entry *)iter;
534 if (iter == baseentry) {
535 if (set && (entry->ent.dp_flag & 0x80) == 0) {
536 entry->ent.dp_flag |= 0x80;
538 } else if (!set && (entry->ent.dp_flag & 0x80)) {
539 entry->ent.dp_flag &= ~0x80;
543 if (set && (entry->ent.dp_flag & 0x80)) {
544 entry->ent.dp_flag &= ~0x80;
548 if (changed && !iter->gpe_created)
549 iter->gpe_modified = 1;
555 g_part_mbr_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
556 char *buf, size_t bufsz)
558 struct g_part_mbr_entry *entry;
561 entry = (struct g_part_mbr_entry *)baseentry;
562 for (i = 0; i < nitems(mbr_alias_match); i++) {
563 if (mbr_alias_match[i].typ == entry->ent.dp_typ)
564 return (g_part_alias_name(mbr_alias_match[i].alias));
566 snprintf(buf, bufsz, "!%d", entry->ent.dp_typ);
571 g_part_mbr_write(struct g_part_table *basetable, struct g_consumer *cp)
573 struct g_part_entry *baseentry;
574 struct g_part_mbr_entry *entry;
575 struct g_part_mbr_table *table;
579 table = (struct g_part_mbr_table *)basetable;
580 baseentry = LIST_FIRST(&basetable->gpt_entry);
581 for (index = 1; index <= basetable->gpt_entries; index++) {
582 p = table->mbr + DOSPARTOFF + (index - 1) * DOSPARTSIZE;
583 entry = (baseentry != NULL && index == baseentry->gpe_index)
584 ? (struct g_part_mbr_entry *)baseentry : NULL;
585 if (entry != NULL && !baseentry->gpe_deleted) {
586 p[0] = entry->ent.dp_flag;
587 p[1] = entry->ent.dp_shd;
588 p[2] = entry->ent.dp_ssect;
589 p[3] = entry->ent.dp_scyl;
590 p[4] = entry->ent.dp_typ;
591 p[5] = entry->ent.dp_ehd;
592 p[6] = entry->ent.dp_esect;
593 p[7] = entry->ent.dp_ecyl;
594 le32enc(p + 8, entry->ent.dp_start);
595 le32enc(p + 12, entry->ent.dp_size);
597 bzero(p, DOSPARTSIZE);
600 baseentry = LIST_NEXT(baseentry, gpe_entry);
603 error = g_write_data(cp, 0, table->mbr, cp->provider->sectorsize);