1 /*- 2 * Copyright (c) 2007-2009 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 EBRSIZE 512 49 50 struct g_part_ebr_table { 51 struct g_part_table base; 52 }; 53 54 struct g_part_ebr_entry { 55 struct g_part_entry base; 56 struct dos_partition ent; 57 int alias; 58 }; 59 60 static int g_part_ebr_add(struct g_part_table *, struct g_part_entry *, 61 struct g_part_parms *); 62 static int g_part_ebr_create(struct g_part_table *, struct g_part_parms *); 63 static int g_part_ebr_destroy(struct g_part_table *, struct g_part_parms *); 64 static int g_part_ebr_devalias(struct g_part_table *, struct g_part_entry *, 65 char *, size_t); 66 static void g_part_ebr_dumpconf(struct g_part_table *, struct g_part_entry *, 67 struct sbuf *, const char *); 68 static int g_part_ebr_dumpto(struct g_part_table *, struct g_part_entry *); 69 static int g_part_ebr_modify(struct g_part_table *, struct g_part_entry *, 70 struct g_part_parms *); 71 static const char *g_part_ebr_name(struct g_part_table *, struct g_part_entry *, 72 char *, size_t); 73 static int g_part_ebr_precheck(struct g_part_table *, enum g_part_ctl, 74 struct g_part_parms *); 75 static int g_part_ebr_probe(struct g_part_table *, struct g_consumer *); 76 static int g_part_ebr_read(struct g_part_table *, struct g_consumer *); 77 static int g_part_ebr_setunset(struct g_part_table *, struct g_part_entry *, 78 const char *, unsigned int); 79 static const char *g_part_ebr_type(struct g_part_table *, struct g_part_entry *, 80 char *, size_t); 81 static int g_part_ebr_write(struct g_part_table *, struct g_consumer *); 82 83 static kobj_method_t g_part_ebr_methods[] = { 84 KOBJMETHOD(g_part_add, g_part_ebr_add), 85 KOBJMETHOD(g_part_create, g_part_ebr_create), 86 KOBJMETHOD(g_part_destroy, g_part_ebr_destroy), 87 KOBJMETHOD(g_part_devalias, g_part_ebr_devalias), 88 KOBJMETHOD(g_part_dumpconf, g_part_ebr_dumpconf), 89 KOBJMETHOD(g_part_dumpto, g_part_ebr_dumpto), 90 KOBJMETHOD(g_part_modify, g_part_ebr_modify), 91 KOBJMETHOD(g_part_name, g_part_ebr_name), 92 KOBJMETHOD(g_part_precheck, g_part_ebr_precheck), 93 KOBJMETHOD(g_part_probe, g_part_ebr_probe), 94 KOBJMETHOD(g_part_read, g_part_ebr_read), 95 KOBJMETHOD(g_part_setunset, g_part_ebr_setunset), 96 KOBJMETHOD(g_part_type, g_part_ebr_type), 97 KOBJMETHOD(g_part_write, g_part_ebr_write), 98 { 0, 0 } 99 }; 100 101 static struct g_part_scheme g_part_ebr_scheme = { 102 "EBR", 103 g_part_ebr_methods, 104 sizeof(struct g_part_ebr_table), 105 .gps_entrysz = sizeof(struct g_part_ebr_entry), 106 .gps_minent = 1, 107 .gps_maxent = INT_MAX, 108 }; 109 G_PART_SCHEME_DECLARE(g_part_ebr); 110 111 static void ebr_set_chs(struct g_part_table *, uint32_t, u_char *, u_char *, 112 u_char *); 113 114 static void 115 ebr_entry_decode(const char *p, struct dos_partition *ent) 116 { 117 ent->dp_flag = p[0]; 118 ent->dp_shd = p[1]; 119 ent->dp_ssect = p[2]; 120 ent->dp_scyl = p[3]; 121 ent->dp_typ = p[4]; 122 ent->dp_ehd = p[5]; 123 ent->dp_esect = p[6]; 124 ent->dp_ecyl = p[7]; 125 ent->dp_start = le32dec(p + 8); 126 ent->dp_size = le32dec(p + 12); 127 } 128 129 static void 130 ebr_entry_link(struct g_part_table *table, uint32_t start, uint32_t end, 131 u_char *buf) 132 { 133 134 buf[0] = 0 /* dp_flag */; 135 ebr_set_chs(table, start, &buf[3] /* dp_scyl */, &buf[1] /* dp_shd */, 136 &buf[2] /* dp_ssect */); 137 buf[4] = 5 /* dp_typ */; 138 ebr_set_chs(table, end, &buf[7] /* dp_ecyl */, &buf[5] /* dp_ehd */, 139 &buf[6] /* dp_esect */); 140 le32enc(buf + 8, start); 141 le32enc(buf + 12, end - start + 1); 142 } 143 144 static int 145 ebr_parse_type(const char *type, u_char *dp_typ) 146 { 147 const char *alias; 148 char *endp; 149 long lt; 150 151 if (type[0] == '!') { 152 lt = strtol(type + 1, &endp, 0); 153 if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256) 154 return (EINVAL); 155 *dp_typ = (u_char)lt; 156 return (0); 157 } 158 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD); 159 if (!strcasecmp(type, alias)) { 160 *dp_typ = DOSPTYP_386BSD; 161 return (0); 162 } 163 return (EINVAL); 164 } 165 166 static void 167 ebr_set_chs(struct g_part_table *table, uint32_t lba, u_char *cylp, u_char *hdp, 168 u_char *secp) 169 { 170 uint32_t cyl, hd, sec; 171 172 sec = lba % table->gpt_sectors + 1; 173 lba /= table->gpt_sectors; 174 hd = lba % table->gpt_heads; 175 lba /= table->gpt_heads; 176 cyl = lba; 177 if (cyl > 1023) 178 sec = hd = cyl = ~0; 179 180 *cylp = cyl & 0xff; 181 *hdp = hd & 0xff; 182 *secp = (sec & 0x3f) | ((cyl >> 2) & 0xc0); 183 } 184 185 static int 186 g_part_ebr_add(struct g_part_table *basetable, struct g_part_entry *baseentry, 187 struct g_part_parms *gpp) 188 { 189 struct g_geom *gp; 190 struct g_provider *pp; 191 struct g_part_ebr_entry *entry; 192 uint32_t start, size, sectors; 193 194 if (gpp->gpp_parms & G_PART_PARM_LABEL) 195 return (EINVAL); 196 197 gp = basetable->gpt_gp; 198 pp = LIST_FIRST(&gp->consumer)->provider; 199 sectors = basetable->gpt_sectors; 200 201 entry = (struct g_part_ebr_entry *)baseentry; 202 203 start = gpp->gpp_start; 204 size = gpp->gpp_size; 205 if (size < 2 * sectors) 206 return (EINVAL); 207 if (start % sectors) { 208 size = size - sectors + (start % sectors); 209 start = start - (start % sectors) + sectors; 210 } 211 if (size % sectors) 212 size = size - (size % sectors); 213 if (size < 2 * sectors) 214 return (EINVAL); 215 216 if (baseentry->gpe_deleted) 217 bzero(&entry->ent, sizeof(entry->ent)); 218 219 KASSERT(baseentry->gpe_start <= start, (__func__)); 220 KASSERT(baseentry->gpe_end >= start + size - 1, (__func__)); 221 baseentry->gpe_index = (start / sectors) + 1; 222 baseentry->gpe_offset = (off_t)(start + sectors) * pp->sectorsize; 223 baseentry->gpe_start = start; 224 baseentry->gpe_end = start + size - 1; 225 entry->ent.dp_start = sectors; 226 entry->ent.dp_size = size - sectors; 227 ebr_set_chs(basetable, entry->ent.dp_start, &entry->ent.dp_scyl, 228 &entry->ent.dp_shd, &entry->ent.dp_ssect); 229 ebr_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl, 230 &entry->ent.dp_ehd, &entry->ent.dp_esect); 231 return (ebr_parse_type(gpp->gpp_type, &entry->ent.dp_typ)); 232 } 233 234 static int 235 g_part_ebr_create(struct g_part_table *basetable, struct g_part_parms *gpp) 236 { 237 char psn[8]; 238 struct g_consumer *cp; 239 struct g_provider *pp; 240 uint64_t msize; 241 int error; 242 243 pp = gpp->gpp_provider; 244 245 if (pp->sectorsize < EBRSIZE) 246 return (ENOSPC); 247 if (pp->sectorsize > 4096) 248 return (ENXIO); 249 250 /* Check that we have a parent and that it's a MBR. */ 251 if (basetable->gpt_depth == 0) 252 return (ENXIO); 253 cp = LIST_FIRST(&pp->consumers); 254 error = g_getattr("PART::scheme", cp, &psn); 255 if (error) 256 return (error); 257 if (strcmp(psn, "MBR")) 258 return (ENXIO); 259 260 msize = pp->mediasize / pp->sectorsize; 261 basetable->gpt_entries = msize / basetable->gpt_sectors; 262 basetable->gpt_first = 0; 263 basetable->gpt_last = msize - (msize % basetable->gpt_sectors) - 1; 264 return (0); 265 } 266 267 static int 268 g_part_ebr_destroy(struct g_part_table *basetable, struct g_part_parms *gpp) 269 { 270 271 /* Wipe the first sector to clear the partitioning. */ 272 basetable->gpt_smhead |= 1; 273 return (0); 274 } 275 276 static int 277 g_part_ebr_devalias(struct g_part_table *table, struct g_part_entry *baseentry, 278 char *buf, size_t bufsz) 279 { 280 struct g_part_ebr_entry *entry; 281 size_t len; 282 283 entry = (struct g_part_ebr_entry *)baseentry; 284 if (entry->alias == 0) 285 return (ENOENT); 286 287 len = strlcpy(buf, table->gpt_gp->name, bufsz); 288 if (len == 0) 289 return (EINVAL); 290 291 snprintf(buf + len - 1, bufsz - len, "%d", entry->alias); 292 return (0); 293 } 294 295 static void 296 g_part_ebr_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry, 297 struct sbuf *sb, const char *indent) 298 { 299 struct g_part_ebr_entry *entry; 300 301 entry = (struct g_part_ebr_entry *)baseentry; 302 if (indent == NULL) { 303 /* conftxt: libdisk compatibility */ 304 sbuf_printf(sb, " xs MBREXT xt %u", entry->ent.dp_typ); 305 } else if (entry != NULL) { 306 /* confxml: partition entry information */ 307 sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent, 308 entry->ent.dp_typ); 309 if (entry->ent.dp_flag & 0x80) 310 sbuf_printf(sb, "%s<attrib>active</attrib>\n", indent); 311 } else { 312 /* confxml: scheme information */ 313 } 314 } 315 316 static int 317 g_part_ebr_dumpto(struct g_part_table *table, struct g_part_entry *baseentry) 318 { 319 struct g_part_ebr_entry *entry; 320 321 /* Allow dumping to a FreeBSD partition only. */ 322 entry = (struct g_part_ebr_entry *)baseentry; 323 return ((entry->ent.dp_typ == DOSPTYP_386BSD) ? 1 : 0); 324 } 325 326 static int 327 g_part_ebr_modify(struct g_part_table *basetable, 328 struct g_part_entry *baseentry, struct g_part_parms *gpp) 329 { 330 struct g_part_ebr_entry *entry; 331 332 if (gpp->gpp_parms & G_PART_PARM_LABEL) 333 return (EINVAL); 334 335 entry = (struct g_part_ebr_entry *)baseentry; 336 if (gpp->gpp_parms & G_PART_PARM_TYPE) 337 return (ebr_parse_type(gpp->gpp_type, &entry->ent.dp_typ)); 338 return (0); 339 } 340 341 static const char * 342 g_part_ebr_name(struct g_part_table *table, struct g_part_entry *entry, 343 char *buf, size_t bufsz) 344 { 345 346 snprintf(buf, bufsz, ".%08u", entry->gpe_index); 347 return (buf); 348 } 349 350 static int 351 g_part_ebr_precheck(struct g_part_table *table, enum g_part_ctl req, 352 struct g_part_parms *gpp) 353 { 354 355 /* 356 * The index is a function of the start of the partition. 357 * This is not something the user can override, nor is it 358 * something the common code will do right. We can set the 359 * index now so that we get what we need. 360 */ 361 if (req == G_PART_CTL_ADD) 362 gpp->gpp_index = (gpp->gpp_start / table->gpt_sectors) + 1; 363 364 return (0); 365 } 366 367 static int 368 g_part_ebr_probe(struct g_part_table *table, struct g_consumer *cp) 369 { 370 char psn[8]; 371 struct g_provider *pp; 372 u_char *buf, *p; 373 int error, index, res, sum; 374 uint16_t magic; 375 376 pp = cp->provider; 377 378 /* Sanity-check the provider. */ 379 if (pp->sectorsize < EBRSIZE || pp->mediasize < pp->sectorsize) 380 return (ENOSPC); 381 if (pp->sectorsize > 4096) 382 return (ENXIO); 383 384 /* Check that we have a parent and that it's a MBR. */ 385 if (table->gpt_depth == 0) 386 return (ENXIO); 387 error = g_getattr("PART::scheme", cp, &psn); 388 if (error) 389 return (error); 390 if (strcmp(psn, "MBR")) 391 return (ENXIO); 392 393 /* Check that there's a EBR. */ 394 buf = g_read_data(cp, 0L, pp->sectorsize, &error); 395 if (buf == NULL) 396 return (error); 397 398 /* We goto out on mismatch. */ 399 res = ENXIO; 400 401 magic = le16dec(buf + DOSMAGICOFFSET); 402 if (magic != DOSMAGIC) 403 goto out; 404 405 /* The sector is all zeroes, except for the partition entries. */ 406 sum = 0; 407 for (index = 0; index < DOSPARTOFF; index++) 408 sum += buf[index]; 409 if (sum != 0) 410 goto out; 411 412 for (index = 0; index < NDOSPART; index++) { 413 p = buf + DOSPARTOFF + index * DOSPARTSIZE; 414 if (p[0] != 0 && p[0] != 0x80) 415 goto out; 416 if (index < 2) 417 continue; 418 /* The 3rd & 4th entries are always zero. */ 419 if ((le64dec(p+0) + le64dec(p+8)) != 0) 420 goto out; 421 } 422 423 res = G_PART_PROBE_PRI_HIGH; 424 425 out: 426 g_free(buf); 427 return (res); 428 } 429 430 static int 431 g_part_ebr_read(struct g_part_table *basetable, struct g_consumer *cp) 432 { 433 struct dos_partition ent[2]; 434 struct g_provider *pp; 435 struct g_part_entry *baseentry; 436 struct g_part_ebr_table *table; 437 struct g_part_ebr_entry *entry; 438 u_char *buf; 439 off_t ofs, msize; 440 u_int lba; 441 int alias, error, index; 442 443 pp = cp->provider; 444 table = (struct g_part_ebr_table *)basetable; 445 msize = pp->mediasize / pp->sectorsize; 446 447 alias = 5; 448 lba = 0; 449 while (1) { 450 ofs = (off_t)lba * pp->sectorsize; 451 buf = g_read_data(cp, ofs, pp->sectorsize, &error); 452 if (buf == NULL) 453 return (error); 454 455 ebr_entry_decode(buf + DOSPARTOFF + 0 * DOSPARTSIZE, ent + 0); 456 ebr_entry_decode(buf + DOSPARTOFF + 1 * DOSPARTSIZE, ent + 1); 457 g_free(buf); 458 459 if (ent[0].dp_typ == 0) 460 break; 461 462 if (ent[0].dp_typ == 5 && ent[1].dp_typ == 0) { 463 lba = ent[0].dp_start; 464 continue; 465 } 466 467 index = (lba / basetable->gpt_sectors) + 1; 468 baseentry = (struct g_part_entry *)g_part_new_entry(basetable, 469 index, lba, lba + ent[0].dp_start + ent[0].dp_size - 1); 470 baseentry->gpe_offset = (off_t)(lba + ent[0].dp_start) * 471 pp->sectorsize; 472 entry = (struct g_part_ebr_entry *)baseentry; 473 entry->ent = ent[0]; 474 entry->alias = alias++; 475 476 if (ent[1].dp_typ == 0) 477 break; 478 479 lba = ent[1].dp_start; 480 } 481 482 basetable->gpt_entries = msize / basetable->gpt_sectors; 483 basetable->gpt_first = 0; 484 basetable->gpt_last = msize - (msize % basetable->gpt_sectors) - 1; 485 return (0); 486 } 487 488 static int 489 g_part_ebr_setunset(struct g_part_table *table, struct g_part_entry *baseentry, 490 const char *attrib, unsigned int set) 491 { 492 struct g_part_entry *iter; 493 struct g_part_ebr_entry *entry; 494 int changed; 495 496 if (strcasecmp(attrib, "active") != 0) 497 return (EINVAL); 498 499 /* Only one entry can have the active attribute. */ 500 LIST_FOREACH(iter, &table->gpt_entry, gpe_entry) { 501 if (iter->gpe_deleted) 502 continue; 503 changed = 0; 504 entry = (struct g_part_ebr_entry *)iter; 505 if (iter == baseentry) { 506 if (set && (entry->ent.dp_flag & 0x80) == 0) { 507 entry->ent.dp_flag |= 0x80; 508 changed = 1; 509 } else if (!set && (entry->ent.dp_flag & 0x80)) { 510 entry->ent.dp_flag &= ~0x80; 511 changed = 1; 512 } 513 } else { 514 if (set && (entry->ent.dp_flag & 0x80)) { 515 entry->ent.dp_flag &= ~0x80; 516 changed = 1; 517 } 518 } 519 if (changed && !iter->gpe_created) 520 iter->gpe_modified = 1; 521 } 522 return (0); 523 } 524 525 static const char * 526 g_part_ebr_type(struct g_part_table *basetable, struct g_part_entry *baseentry, 527 char *buf, size_t bufsz) 528 { 529 struct g_part_ebr_entry *entry; 530 int type; 531 532 entry = (struct g_part_ebr_entry *)baseentry; 533 type = entry->ent.dp_typ; 534 if (type == DOSPTYP_386BSD) 535 return (g_part_alias_name(G_PART_ALIAS_FREEBSD)); 536 snprintf(buf, bufsz, "!%d", type); 537 return (buf); 538 } 539 540 static int 541 g_part_ebr_write(struct g_part_table *basetable, struct g_consumer *cp) 542 { 543 struct g_provider *pp; 544 struct g_part_entry *baseentry, *next; 545 struct g_part_ebr_entry *entry; 546 u_char *buf; 547 u_char *p; 548 int error; 549 550 pp = cp->provider; 551 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO); 552 le16enc(buf + DOSMAGICOFFSET, DOSMAGIC); 553 554 baseentry = LIST_FIRST(&basetable->gpt_entry); 555 while (baseentry != NULL && baseentry->gpe_deleted) 556 baseentry = LIST_NEXT(baseentry, gpe_entry); 557 558 /* Wipe-out the the first EBR when there are no slices. */ 559 if (baseentry == NULL) { 560 error = g_write_data(cp, 0, buf, pp->sectorsize); 561 goto out; 562 } 563 564 /* 565 * If the first partition is not in LBA 0, we need to 566 * put a "link" EBR in LBA 0. 567 */ 568 if (baseentry->gpe_start != 0) { 569 ebr_entry_link(basetable, (uint32_t)baseentry->gpe_start, 570 (uint32_t)baseentry->gpe_end, buf + DOSPARTOFF); 571 error = g_write_data(cp, 0, buf, pp->sectorsize); 572 if (error) 573 goto out; 574 } 575 576 do { 577 entry = (struct g_part_ebr_entry *)baseentry; 578 579 p = buf + DOSPARTOFF; 580 p[0] = entry->ent.dp_flag; 581 p[1] = entry->ent.dp_shd; 582 p[2] = entry->ent.dp_ssect; 583 p[3] = entry->ent.dp_scyl; 584 p[4] = entry->ent.dp_typ; 585 p[5] = entry->ent.dp_ehd; 586 p[6] = entry->ent.dp_esect; 587 p[7] = entry->ent.dp_ecyl; 588 le32enc(p + 8, entry->ent.dp_start); 589 le32enc(p + 12, entry->ent.dp_size); 590 591 next = LIST_NEXT(baseentry, gpe_entry); 592 while (next != NULL && next->gpe_deleted) 593 next = LIST_NEXT(next, gpe_entry); 594 595 p += DOSPARTSIZE; 596 if (next != NULL) 597 ebr_entry_link(basetable, (uint32_t)next->gpe_start, 598 (uint32_t)next->gpe_end, p); 599 else 600 bzero(p, DOSPARTSIZE); 601 602 error = g_write_data(cp, baseentry->gpe_start * pp->sectorsize, 603 buf, pp->sectorsize); 604 605 baseentry = next; 606 } while (!error && baseentry != NULL); 607 608 out: 609 g_free(buf); 610 return (error); 611 } 612