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