1 /*- 2 * Copyright (c) 2007, 2008 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 <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/bio.h> 32 #include <sys/diskmbr.h> 33 #include <sys/endian.h> 34 #include <sys/kernel.h> 35 #include <sys/kobj.h> 36 #include <sys/limits.h> 37 #include <sys/lock.h> 38 #include <sys/malloc.h> 39 #include <sys/mutex.h> 40 #include <sys/queue.h> 41 #include <sys/sbuf.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> 47 48 #include "g_part_if.h" 49 50 FEATURE(geom_part_mbr, "GEOM partitioning class for MBR support"); 51 52 #define MBRSIZE 512 53 54 struct g_part_mbr_table { 55 struct g_part_table base; 56 u_char mbr[MBRSIZE]; 57 }; 58 59 struct g_part_mbr_entry { 60 struct g_part_entry base; 61 struct dos_partition ent; 62 }; 63 64 static int g_part_mbr_add(struct g_part_table *, struct g_part_entry *, 65 struct g_part_parms *); 66 static int g_part_mbr_bootcode(struct g_part_table *, struct g_part_parms *); 67 static int g_part_mbr_create(struct g_part_table *, struct g_part_parms *); 68 static int g_part_mbr_destroy(struct g_part_table *, struct g_part_parms *); 69 static void g_part_mbr_dumpconf(struct g_part_table *, struct g_part_entry *, 70 struct sbuf *, const char *); 71 static int g_part_mbr_dumpto(struct g_part_table *, struct g_part_entry *); 72 static int g_part_mbr_modify(struct g_part_table *, struct g_part_entry *, 73 struct g_part_parms *); 74 static const char *g_part_mbr_name(struct g_part_table *, struct g_part_entry *, 75 char *, size_t); 76 static int g_part_mbr_probe(struct g_part_table *, struct g_consumer *); 77 static int g_part_mbr_read(struct g_part_table *, struct g_consumer *); 78 static int g_part_mbr_setunset(struct g_part_table *, struct g_part_entry *, 79 const char *, unsigned int); 80 static const char *g_part_mbr_type(struct g_part_table *, struct g_part_entry *, 81 char *, size_t); 82 static int g_part_mbr_write(struct g_part_table *, struct g_consumer *); 83 static int g_part_mbr_resize(struct g_part_table *, struct g_part_entry *, 84 struct g_part_parms *); 85 86 static kobj_method_t g_part_mbr_methods[] = { 87 KOBJMETHOD(g_part_add, g_part_mbr_add), 88 KOBJMETHOD(g_part_bootcode, g_part_mbr_bootcode), 89 KOBJMETHOD(g_part_create, g_part_mbr_create), 90 KOBJMETHOD(g_part_destroy, g_part_mbr_destroy), 91 KOBJMETHOD(g_part_dumpconf, g_part_mbr_dumpconf), 92 KOBJMETHOD(g_part_dumpto, g_part_mbr_dumpto), 93 KOBJMETHOD(g_part_modify, g_part_mbr_modify), 94 KOBJMETHOD(g_part_resize, g_part_mbr_resize), 95 KOBJMETHOD(g_part_name, g_part_mbr_name), 96 KOBJMETHOD(g_part_probe, g_part_mbr_probe), 97 KOBJMETHOD(g_part_read, g_part_mbr_read), 98 KOBJMETHOD(g_part_setunset, g_part_mbr_setunset), 99 KOBJMETHOD(g_part_type, g_part_mbr_type), 100 KOBJMETHOD(g_part_write, g_part_mbr_write), 101 { 0, 0 } 102 }; 103 104 static struct g_part_scheme g_part_mbr_scheme = { 105 "MBR", 106 g_part_mbr_methods, 107 sizeof(struct g_part_mbr_table), 108 .gps_entrysz = sizeof(struct g_part_mbr_entry), 109 .gps_minent = NDOSPART, 110 .gps_maxent = NDOSPART, 111 .gps_bootcodesz = MBRSIZE, 112 }; 113 G_PART_SCHEME_DECLARE(g_part_mbr); 114 115 static struct g_part_mbr_alias { 116 u_char typ; 117 int alias; 118 } mbr_alias_match[] = { 119 { DOSPTYP_386BSD, G_PART_ALIAS_FREEBSD }, 120 { DOSPTYP_EXT, G_PART_ALIAS_EBR }, 121 { DOSPTYP_NTFS, G_PART_ALIAS_MS_NTFS }, 122 { DOSPTYP_FAT16, G_PART_ALIAS_MS_FAT16 }, 123 { DOSPTYP_FAT32, G_PART_ALIAS_MS_FAT32 }, 124 { DOSPTYP_EXTLBA, G_PART_ALIAS_EBR }, 125 { DOSPTYP_LDM, G_PART_ALIAS_MS_LDM_DATA }, 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 }, 130 { DOSPTYP_PPCBOOT, G_PART_ALIAS_PREP_BOOT }, 131 { DOSPTYP_VMFS, G_PART_ALIAS_VMFS }, 132 { DOSPTYP_VMKDIAG, G_PART_ALIAS_VMKDIAG }, 133 }; 134 135 static int 136 mbr_parse_type(const char *type, u_char *dp_typ) 137 { 138 const char *alias; 139 char *endp; 140 long lt; 141 int i; 142 143 if (type[0] == '!') { 144 lt = strtol(type + 1, &endp, 0); 145 if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256) 146 return (EINVAL); 147 *dp_typ = (u_char)lt; 148 return (0); 149 } 150 for (i = 0; 151 i < sizeof(mbr_alias_match) / sizeof(mbr_alias_match[0]); i++) { 152 alias = g_part_alias_name(mbr_alias_match[i].alias); 153 if (strcasecmp(type, alias) == 0) { 154 *dp_typ = mbr_alias_match[i].typ; 155 return (0); 156 } 157 } 158 return (EINVAL); 159 } 160 161 static int 162 mbr_probe_bpb(u_char *bpb) 163 { 164 uint16_t secsz; 165 uint8_t clstsz; 166 167 #define PO2(x) ((x & (x - 1)) == 0) 168 secsz = le16dec(bpb); 169 if (secsz < 512 || secsz > 4096 || !PO2(secsz)) 170 return (0); 171 clstsz = bpb[2]; 172 if (clstsz < 1 || clstsz > 128 || !PO2(clstsz)) 173 return (0); 174 #undef PO2 175 176 return (1); 177 } 178 179 static void 180 mbr_set_chs(struct g_part_table *table, uint32_t lba, u_char *cylp, u_char *hdp, 181 u_char *secp) 182 { 183 uint32_t cyl, hd, sec; 184 185 sec = lba % table->gpt_sectors + 1; 186 lba /= table->gpt_sectors; 187 hd = lba % table->gpt_heads; 188 lba /= table->gpt_heads; 189 cyl = lba; 190 if (cyl > 1023) 191 sec = hd = cyl = ~0; 192 193 *cylp = cyl & 0xff; 194 *hdp = hd & 0xff; 195 *secp = (sec & 0x3f) | ((cyl >> 2) & 0xc0); 196 } 197 198 static int 199 mbr_align(struct g_part_table *basetable, uint32_t *start, uint32_t *size) 200 { 201 uint32_t sectors; 202 203 sectors = basetable->gpt_sectors; 204 if (*size < sectors) 205 return (EINVAL); 206 if (start != NULL && (*start % sectors)) { 207 *size += (*start % sectors) - sectors; 208 *start -= (*start % sectors) - sectors; 209 } 210 if (*size % sectors) 211 *size -= (*size % sectors); 212 if (*size < sectors) 213 return (EINVAL); 214 return (0); 215 } 216 217 static int 218 g_part_mbr_add(struct g_part_table *basetable, struct g_part_entry *baseentry, 219 struct g_part_parms *gpp) 220 { 221 struct g_part_mbr_entry *entry; 222 uint32_t start, size; 223 224 if (gpp->gpp_parms & G_PART_PARM_LABEL) 225 return (EINVAL); 226 227 entry = (struct g_part_mbr_entry *)baseentry; 228 start = gpp->gpp_start; 229 size = gpp->gpp_size; 230 if (mbr_align(basetable, &start, &size) != 0) 231 return (EINVAL); 232 if (baseentry->gpe_deleted) 233 bzero(&entry->ent, sizeof(entry->ent)); 234 235 KASSERT(baseentry->gpe_start <= start, ("%s", __func__)); 236 KASSERT(baseentry->gpe_end >= start + size - 1, ("%s", __func__)); 237 baseentry->gpe_start = start; 238 baseentry->gpe_end = start + size - 1; 239 entry->ent.dp_start = start; 240 entry->ent.dp_size = size; 241 mbr_set_chs(basetable, baseentry->gpe_start, &entry->ent.dp_scyl, 242 &entry->ent.dp_shd, &entry->ent.dp_ssect); 243 mbr_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl, 244 &entry->ent.dp_ehd, &entry->ent.dp_esect); 245 return (mbr_parse_type(gpp->gpp_type, &entry->ent.dp_typ)); 246 } 247 248 static int 249 g_part_mbr_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp) 250 { 251 struct g_part_mbr_table *table; 252 uint32_t dsn; 253 254 if (gpp->gpp_codesize != MBRSIZE) 255 return (ENODEV); 256 257 table = (struct g_part_mbr_table *)basetable; 258 dsn = *(uint32_t *)(table->mbr + DOSDSNOFF); 259 bcopy(gpp->gpp_codeptr, table->mbr, DOSPARTOFF); 260 if (dsn != 0) 261 *(uint32_t *)(table->mbr + DOSDSNOFF) = dsn; 262 return (0); 263 } 264 265 static int 266 g_part_mbr_create(struct g_part_table *basetable, struct g_part_parms *gpp) 267 { 268 struct g_provider *pp; 269 struct g_part_mbr_table *table; 270 271 pp = gpp->gpp_provider; 272 if (pp->sectorsize < MBRSIZE) 273 return (ENOSPC); 274 275 basetable->gpt_first = basetable->gpt_sectors; 276 basetable->gpt_last = MIN(pp->mediasize / pp->sectorsize, 277 UINT32_MAX) - 1; 278 279 table = (struct g_part_mbr_table *)basetable; 280 le16enc(table->mbr + DOSMAGICOFFSET, DOSMAGIC); 281 return (0); 282 } 283 284 static int 285 g_part_mbr_destroy(struct g_part_table *basetable, struct g_part_parms *gpp) 286 { 287 288 /* Wipe the first sector to clear the partitioning. */ 289 basetable->gpt_smhead |= 1; 290 return (0); 291 } 292 293 static void 294 g_part_mbr_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry, 295 struct sbuf *sb, const char *indent) 296 { 297 struct g_part_mbr_entry *entry; 298 299 entry = (struct g_part_mbr_entry *)baseentry; 300 if (indent == NULL) { 301 /* conftxt: libdisk compatibility */ 302 sbuf_printf(sb, " xs MBR xt %u", entry->ent.dp_typ); 303 } else if (entry != NULL) { 304 /* confxml: partition entry information */ 305 sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent, 306 entry->ent.dp_typ); 307 if (entry->ent.dp_flag & 0x80) 308 sbuf_printf(sb, "%s<attrib>active</attrib>\n", indent); 309 } else { 310 /* confxml: scheme information */ 311 } 312 } 313 314 static int 315 g_part_mbr_dumpto(struct g_part_table *table, struct g_part_entry *baseentry) 316 { 317 struct g_part_mbr_entry *entry; 318 319 /* Allow dumping to a FreeBSD partition or Linux swap partition only. */ 320 entry = (struct g_part_mbr_entry *)baseentry; 321 return ((entry->ent.dp_typ == DOSPTYP_386BSD || 322 entry->ent.dp_typ == DOSPTYP_LINSWP) ? 1 : 0); 323 } 324 325 static int 326 g_part_mbr_modify(struct g_part_table *basetable, 327 struct g_part_entry *baseentry, struct g_part_parms *gpp) 328 { 329 struct g_part_mbr_entry *entry; 330 331 if (gpp->gpp_parms & G_PART_PARM_LABEL) 332 return (EINVAL); 333 334 entry = (struct g_part_mbr_entry *)baseentry; 335 if (gpp->gpp_parms & G_PART_PARM_TYPE) 336 return (mbr_parse_type(gpp->gpp_type, &entry->ent.dp_typ)); 337 return (0); 338 } 339 340 static int 341 g_part_mbr_resize(struct g_part_table *basetable, 342 struct g_part_entry *baseentry, struct g_part_parms *gpp) 343 { 344 struct g_part_mbr_entry *entry; 345 struct g_provider *pp; 346 uint32_t size; 347 348 if (baseentry == NULL) { 349 pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider; 350 basetable->gpt_last = MIN(pp->mediasize / pp->sectorsize, 351 UINT32_MAX) - 1; 352 return (0); 353 } 354 size = gpp->gpp_size; 355 if (mbr_align(basetable, NULL, &size) != 0) 356 return (EINVAL); 357 /* XXX: prevent unexpected shrinking. */ 358 pp = baseentry->gpe_pp; 359 if ((g_debugflags & 0x10) == 0 && size < gpp->gpp_size && 360 pp->mediasize / pp->sectorsize > size) 361 return (EBUSY); 362 entry = (struct g_part_mbr_entry *)baseentry; 363 baseentry->gpe_end = baseentry->gpe_start + size - 1; 364 entry->ent.dp_size = size; 365 mbr_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl, 366 &entry->ent.dp_ehd, &entry->ent.dp_esect); 367 return (0); 368 } 369 370 static const char * 371 g_part_mbr_name(struct g_part_table *table, struct g_part_entry *baseentry, 372 char *buf, size_t bufsz) 373 { 374 375 snprintf(buf, bufsz, "s%d", baseentry->gpe_index); 376 return (buf); 377 } 378 379 static int 380 g_part_mbr_probe(struct g_part_table *table, struct g_consumer *cp) 381 { 382 char psn[8]; 383 struct g_provider *pp; 384 u_char *buf, *p; 385 int error, index, res, sum; 386 uint16_t magic; 387 388 pp = cp->provider; 389 390 /* Sanity-check the provider. */ 391 if (pp->sectorsize < MBRSIZE || pp->mediasize < pp->sectorsize) 392 return (ENOSPC); 393 if (pp->sectorsize > 4096) 394 return (ENXIO); 395 396 /* We don't nest under an MBR (see EBR instead). */ 397 error = g_getattr("PART::scheme", cp, &psn); 398 if (error == 0 && strcmp(psn, g_part_mbr_scheme.name) == 0) 399 return (ELOOP); 400 401 /* Check that there's a MBR. */ 402 buf = g_read_data(cp, 0L, pp->sectorsize, &error); 403 if (buf == NULL) 404 return (error); 405 406 /* We goto out on mismatch. */ 407 res = ENXIO; 408 409 magic = le16dec(buf + DOSMAGICOFFSET); 410 if (magic != DOSMAGIC) 411 goto out; 412 413 for (index = 0; index < NDOSPART; index++) { 414 p = buf + DOSPARTOFF + index * DOSPARTSIZE; 415 if (p[0] != 0 && p[0] != 0x80) 416 goto out; 417 } 418 419 /* 420 * If the partition table does not consist of all zeroes, 421 * assume we have a MBR. If it's all zeroes, we could have 422 * a boot sector. For example, a boot sector that doesn't 423 * have boot code -- common on non-i386 hardware. In that 424 * case we check if we have a possible BPB. If so, then we 425 * assume we have a boot sector instead. 426 */ 427 sum = 0; 428 for (index = 0; index < NDOSPART * DOSPARTSIZE; index++) 429 sum += buf[DOSPARTOFF + index]; 430 if (sum != 0 || !mbr_probe_bpb(buf + 0x0b)) 431 res = G_PART_PROBE_PRI_NORM; 432 433 out: 434 g_free(buf); 435 return (res); 436 } 437 438 static int 439 g_part_mbr_read(struct g_part_table *basetable, struct g_consumer *cp) 440 { 441 struct dos_partition ent; 442 struct g_provider *pp; 443 struct g_part_mbr_table *table; 444 struct g_part_mbr_entry *entry; 445 u_char *buf, *p; 446 off_t chs, msize, first; 447 u_int sectors, heads; 448 int error, index; 449 450 pp = cp->provider; 451 table = (struct g_part_mbr_table *)basetable; 452 first = basetable->gpt_sectors; 453 msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX); 454 455 buf = g_read_data(cp, 0L, pp->sectorsize, &error); 456 if (buf == NULL) 457 return (error); 458 459 bcopy(buf, table->mbr, sizeof(table->mbr)); 460 for (index = NDOSPART - 1; index >= 0; index--) { 461 p = buf + DOSPARTOFF + index * DOSPARTSIZE; 462 ent.dp_flag = p[0]; 463 ent.dp_shd = p[1]; 464 ent.dp_ssect = p[2]; 465 ent.dp_scyl = p[3]; 466 ent.dp_typ = p[4]; 467 ent.dp_ehd = p[5]; 468 ent.dp_esect = p[6]; 469 ent.dp_ecyl = p[7]; 470 ent.dp_start = le32dec(p + 8); 471 ent.dp_size = le32dec(p + 12); 472 if (ent.dp_typ == 0 || ent.dp_typ == DOSPTYP_PMBR) 473 continue; 474 if (ent.dp_start == 0 || ent.dp_size == 0) 475 continue; 476 sectors = ent.dp_esect & 0x3f; 477 if (sectors > basetable->gpt_sectors && 478 !basetable->gpt_fixgeom) { 479 g_part_geometry_heads(msize, sectors, &chs, &heads); 480 if (chs != 0) { 481 basetable->gpt_sectors = sectors; 482 basetable->gpt_heads = heads; 483 } 484 } 485 if (ent.dp_start < first) 486 first = ent.dp_start; 487 entry = (struct g_part_mbr_entry *)g_part_new_entry(basetable, 488 index + 1, ent.dp_start, ent.dp_start + ent.dp_size - 1); 489 entry->ent = ent; 490 } 491 492 basetable->gpt_entries = NDOSPART; 493 basetable->gpt_first = basetable->gpt_sectors; 494 basetable->gpt_last = msize - 1; 495 496 if (first < basetable->gpt_first) 497 basetable->gpt_first = 1; 498 499 g_free(buf); 500 return (0); 501 } 502 503 static int 504 g_part_mbr_setunset(struct g_part_table *table, struct g_part_entry *baseentry, 505 const char *attrib, unsigned int set) 506 { 507 struct g_part_entry *iter; 508 struct g_part_mbr_entry *entry; 509 int changed; 510 511 if (baseentry == NULL) 512 return (ENODEV); 513 if (strcasecmp(attrib, "active") != 0) 514 return (EINVAL); 515 516 /* Only one entry can have the active attribute. */ 517 LIST_FOREACH(iter, &table->gpt_entry, gpe_entry) { 518 if (iter->gpe_deleted) 519 continue; 520 changed = 0; 521 entry = (struct g_part_mbr_entry *)iter; 522 if (iter == baseentry) { 523 if (set && (entry->ent.dp_flag & 0x80) == 0) { 524 entry->ent.dp_flag |= 0x80; 525 changed = 1; 526 } else if (!set && (entry->ent.dp_flag & 0x80)) { 527 entry->ent.dp_flag &= ~0x80; 528 changed = 1; 529 } 530 } else { 531 if (set && (entry->ent.dp_flag & 0x80)) { 532 entry->ent.dp_flag &= ~0x80; 533 changed = 1; 534 } 535 } 536 if (changed && !iter->gpe_created) 537 iter->gpe_modified = 1; 538 } 539 return (0); 540 } 541 542 static const char * 543 g_part_mbr_type(struct g_part_table *basetable, struct g_part_entry *baseentry, 544 char *buf, size_t bufsz) 545 { 546 struct g_part_mbr_entry *entry; 547 int i; 548 549 entry = (struct g_part_mbr_entry *)baseentry; 550 for (i = 0; 551 i < sizeof(mbr_alias_match) / sizeof(mbr_alias_match[0]); i++) { 552 if (mbr_alias_match[i].typ == entry->ent.dp_typ) 553 return (g_part_alias_name(mbr_alias_match[i].alias)); 554 } 555 snprintf(buf, bufsz, "!%d", entry->ent.dp_typ); 556 return (buf); 557 } 558 559 static int 560 g_part_mbr_write(struct g_part_table *basetable, struct g_consumer *cp) 561 { 562 struct g_part_entry *baseentry; 563 struct g_part_mbr_entry *entry; 564 struct g_part_mbr_table *table; 565 u_char *p; 566 int error, index; 567 568 table = (struct g_part_mbr_table *)basetable; 569 baseentry = LIST_FIRST(&basetable->gpt_entry); 570 for (index = 1; index <= basetable->gpt_entries; index++) { 571 p = table->mbr + DOSPARTOFF + (index - 1) * DOSPARTSIZE; 572 entry = (baseentry != NULL && index == baseentry->gpe_index) 573 ? (struct g_part_mbr_entry *)baseentry : NULL; 574 if (entry != NULL && !baseentry->gpe_deleted) { 575 p[0] = entry->ent.dp_flag; 576 p[1] = entry->ent.dp_shd; 577 p[2] = entry->ent.dp_ssect; 578 p[3] = entry->ent.dp_scyl; 579 p[4] = entry->ent.dp_typ; 580 p[5] = entry->ent.dp_ehd; 581 p[6] = entry->ent.dp_esect; 582 p[7] = entry->ent.dp_ecyl; 583 le32enc(p + 8, entry->ent.dp_start); 584 le32enc(p + 12, entry->ent.dp_size); 585 } else 586 bzero(p, DOSPARTSIZE); 587 588 if (entry != NULL) 589 baseentry = LIST_NEXT(baseentry, gpe_entry); 590 } 591 592 error = g_write_data(cp, 0, table->mbr, cp->provider->sectorsize); 593 return (error); 594 } 595