1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2007-2009 Marcel Moolenaar 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 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. 16 * 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. 27 */ 28 29 #include "opt_geom.h" 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/param.h> 35 #include <sys/bio.h> 36 #include <sys/diskmbr.h> 37 #include <sys/endian.h> 38 #include <sys/kernel.h> 39 #include <sys/kobj.h> 40 #include <sys/limits.h> 41 #include <sys/lock.h> 42 #include <sys/malloc.h> 43 #include <sys/mutex.h> 44 #include <sys/queue.h> 45 #include <sys/sbuf.h> 46 #include <sys/systm.h> 47 #include <sys/sysctl.h> 48 #include <geom/geom.h> 49 #include <geom/part/g_part.h> 50 51 #include "g_part_if.h" 52 53 FEATURE(geom_part_ebr, 54 "GEOM partitioning class for extended boot records support"); 55 #if defined(GEOM_PART_EBR_COMPAT) 56 FEATURE(geom_part_ebr_compat, 57 "GEOM EBR partitioning class: backward-compatible partition names"); 58 #endif 59 60 #define EBRSIZE 512 61 62 struct g_part_ebr_table { 63 struct g_part_table base; 64 #ifndef GEOM_PART_EBR_COMPAT 65 u_char ebr[EBRSIZE]; 66 #endif 67 }; 68 69 struct g_part_ebr_entry { 70 struct g_part_entry base; 71 struct dos_partition ent; 72 }; 73 74 static int g_part_ebr_add(struct g_part_table *, struct g_part_entry *, 75 struct g_part_parms *); 76 static int g_part_ebr_create(struct g_part_table *, struct g_part_parms *); 77 static int g_part_ebr_destroy(struct g_part_table *, struct g_part_parms *); 78 static void g_part_ebr_dumpconf(struct g_part_table *, struct g_part_entry *, 79 struct sbuf *, const char *); 80 static int g_part_ebr_dumpto(struct g_part_table *, struct g_part_entry *); 81 #if defined(GEOM_PART_EBR_COMPAT) 82 static void g_part_ebr_fullname(struct g_part_table *, struct g_part_entry *, 83 struct sbuf *, const char *); 84 #endif 85 static int g_part_ebr_modify(struct g_part_table *, struct g_part_entry *, 86 struct g_part_parms *); 87 static const char *g_part_ebr_name(struct g_part_table *, struct g_part_entry *, 88 char *, size_t); 89 static int g_part_ebr_precheck(struct g_part_table *, enum g_part_ctl, 90 struct g_part_parms *); 91 static int g_part_ebr_probe(struct g_part_table *, struct g_consumer *); 92 static int g_part_ebr_read(struct g_part_table *, struct g_consumer *); 93 static int g_part_ebr_setunset(struct g_part_table *, struct g_part_entry *, 94 const char *, unsigned int); 95 static const char *g_part_ebr_type(struct g_part_table *, struct g_part_entry *, 96 char *, size_t); 97 static int g_part_ebr_write(struct g_part_table *, struct g_consumer *); 98 static int g_part_ebr_resize(struct g_part_table *, struct g_part_entry *, 99 struct g_part_parms *); 100 101 static kobj_method_t g_part_ebr_methods[] = { 102 KOBJMETHOD(g_part_add, g_part_ebr_add), 103 KOBJMETHOD(g_part_create, g_part_ebr_create), 104 KOBJMETHOD(g_part_destroy, g_part_ebr_destroy), 105 KOBJMETHOD(g_part_dumpconf, g_part_ebr_dumpconf), 106 KOBJMETHOD(g_part_dumpto, g_part_ebr_dumpto), 107 #if defined(GEOM_PART_EBR_COMPAT) 108 KOBJMETHOD(g_part_fullname, g_part_ebr_fullname), 109 #endif 110 KOBJMETHOD(g_part_modify, g_part_ebr_modify), 111 KOBJMETHOD(g_part_name, g_part_ebr_name), 112 KOBJMETHOD(g_part_precheck, g_part_ebr_precheck), 113 KOBJMETHOD(g_part_probe, g_part_ebr_probe), 114 KOBJMETHOD(g_part_read, g_part_ebr_read), 115 KOBJMETHOD(g_part_resize, g_part_ebr_resize), 116 KOBJMETHOD(g_part_setunset, g_part_ebr_setunset), 117 KOBJMETHOD(g_part_type, g_part_ebr_type), 118 KOBJMETHOD(g_part_write, g_part_ebr_write), 119 { 0, 0 } 120 }; 121 122 static struct g_part_scheme g_part_ebr_scheme = { 123 "EBR", 124 g_part_ebr_methods, 125 sizeof(struct g_part_ebr_table), 126 .gps_entrysz = sizeof(struct g_part_ebr_entry), 127 .gps_minent = 1, 128 .gps_maxent = INT_MAX, 129 }; 130 G_PART_SCHEME_DECLARE(g_part_ebr); 131 MODULE_VERSION(geom_part_ebr, 0); 132 133 static struct g_part_ebr_alias { 134 u_char typ; 135 int alias; 136 } ebr_alias_match[] = { 137 { DOSPTYP_386BSD, G_PART_ALIAS_FREEBSD }, 138 { DOSPTYP_NTFS, G_PART_ALIAS_MS_NTFS }, 139 { DOSPTYP_FAT32, G_PART_ALIAS_MS_FAT32 }, 140 { DOSPTYP_FAT32LBA, G_PART_ALIAS_MS_FAT32LBA }, 141 { DOSPTYP_LINSWP, G_PART_ALIAS_LINUX_SWAP }, 142 { DOSPTYP_LINUX, G_PART_ALIAS_LINUX_DATA }, 143 { DOSPTYP_LINLVM, G_PART_ALIAS_LINUX_LVM }, 144 { DOSPTYP_LINRAID, G_PART_ALIAS_LINUX_RAID }, 145 }; 146 147 static void ebr_set_chs(struct g_part_table *, uint32_t, u_char *, u_char *, 148 u_char *); 149 150 static void 151 ebr_entry_decode(const char *p, struct dos_partition *ent) 152 { 153 ent->dp_flag = p[0]; 154 ent->dp_shd = p[1]; 155 ent->dp_ssect = p[2]; 156 ent->dp_scyl = p[3]; 157 ent->dp_typ = p[4]; 158 ent->dp_ehd = p[5]; 159 ent->dp_esect = p[6]; 160 ent->dp_ecyl = p[7]; 161 ent->dp_start = le32dec(p + 8); 162 ent->dp_size = le32dec(p + 12); 163 } 164 165 static void 166 ebr_entry_link(struct g_part_table *table, uint32_t start, uint32_t end, 167 u_char *buf) 168 { 169 170 buf[0] = 0 /* dp_flag */; 171 ebr_set_chs(table, start, &buf[3] /* dp_scyl */, &buf[1] /* dp_shd */, 172 &buf[2] /* dp_ssect */); 173 buf[4] = 5 /* dp_typ */; 174 ebr_set_chs(table, end, &buf[7] /* dp_ecyl */, &buf[5] /* dp_ehd */, 175 &buf[6] /* dp_esect */); 176 le32enc(buf + 8, start); 177 le32enc(buf + 12, end - start + 1); 178 } 179 180 static int 181 ebr_parse_type(const char *type, u_char *dp_typ) 182 { 183 const char *alias; 184 char *endp; 185 long lt; 186 int i; 187 188 if (type[0] == '!') { 189 lt = strtol(type + 1, &endp, 0); 190 if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256) 191 return (EINVAL); 192 *dp_typ = (u_char)lt; 193 return (0); 194 } 195 for (i = 0; i < nitems(ebr_alias_match); i++) { 196 alias = g_part_alias_name(ebr_alias_match[i].alias); 197 if (strcasecmp(type, alias) == 0) { 198 *dp_typ = ebr_alias_match[i].typ; 199 return (0); 200 } 201 } 202 return (EINVAL); 203 } 204 205 206 static void 207 ebr_set_chs(struct g_part_table *table, uint32_t lba, u_char *cylp, u_char *hdp, 208 u_char *secp) 209 { 210 uint32_t cyl, hd, sec; 211 212 sec = lba % table->gpt_sectors + 1; 213 lba /= table->gpt_sectors; 214 hd = lba % table->gpt_heads; 215 lba /= table->gpt_heads; 216 cyl = lba; 217 if (cyl > 1023) 218 sec = hd = cyl = ~0; 219 220 *cylp = cyl & 0xff; 221 *hdp = hd & 0xff; 222 *secp = (sec & 0x3f) | ((cyl >> 2) & 0xc0); 223 } 224 225 static int 226 ebr_align(struct g_part_table *basetable, uint32_t *start, uint32_t *size) 227 { 228 uint32_t sectors; 229 230 sectors = basetable->gpt_sectors; 231 if (*size < 2 * sectors) 232 return (EINVAL); 233 if (*start % sectors) { 234 *size += (*start % sectors) - sectors; 235 *start -= (*start % sectors) - sectors; 236 } 237 if (*size % sectors) 238 *size -= (*size % sectors); 239 if (*size < 2 * sectors) 240 return (EINVAL); 241 return (0); 242 } 243 244 245 static int 246 g_part_ebr_add(struct g_part_table *basetable, struct g_part_entry *baseentry, 247 struct g_part_parms *gpp) 248 { 249 struct g_provider *pp; 250 struct g_part_ebr_entry *entry; 251 uint32_t start, size; 252 253 if (gpp->gpp_parms & G_PART_PARM_LABEL) 254 return (EINVAL); 255 256 pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider; 257 entry = (struct g_part_ebr_entry *)baseentry; 258 start = gpp->gpp_start; 259 size = gpp->gpp_size; 260 if (ebr_align(basetable, &start, &size) != 0) 261 return (EINVAL); 262 if (baseentry->gpe_deleted) 263 bzero(&entry->ent, sizeof(entry->ent)); 264 265 KASSERT(baseentry->gpe_start <= start, ("%s", __func__)); 266 KASSERT(baseentry->gpe_end >= start + size - 1, ("%s", __func__)); 267 baseentry->gpe_index = (start / basetable->gpt_sectors) + 1; 268 baseentry->gpe_offset = 269 (off_t)(start + basetable->gpt_sectors) * pp->sectorsize; 270 baseentry->gpe_start = start; 271 baseentry->gpe_end = start + size - 1; 272 entry->ent.dp_start = basetable->gpt_sectors; 273 entry->ent.dp_size = size - basetable->gpt_sectors; 274 ebr_set_chs(basetable, entry->ent.dp_start, &entry->ent.dp_scyl, 275 &entry->ent.dp_shd, &entry->ent.dp_ssect); 276 ebr_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl, 277 &entry->ent.dp_ehd, &entry->ent.dp_esect); 278 return (ebr_parse_type(gpp->gpp_type, &entry->ent.dp_typ)); 279 } 280 281 static int 282 g_part_ebr_create(struct g_part_table *basetable, struct g_part_parms *gpp) 283 { 284 char type[64]; 285 struct g_consumer *cp; 286 struct g_provider *pp; 287 uint32_t msize; 288 int error; 289 290 pp = gpp->gpp_provider; 291 292 if (pp->sectorsize < EBRSIZE) 293 return (ENOSPC); 294 if (pp->sectorsize > 4096) 295 return (ENXIO); 296 297 /* Check that we have a parent and that it's a MBR. */ 298 if (basetable->gpt_depth == 0) 299 return (ENXIO); 300 cp = LIST_FIRST(&pp->consumers); 301 error = g_getattr("PART::scheme", cp, &type); 302 if (error != 0) 303 return (error); 304 if (strcmp(type, "MBR") != 0) 305 return (ENXIO); 306 error = g_getattr("PART::type", cp, &type); 307 if (error != 0) 308 return (error); 309 if (strcmp(type, "ebr") != 0) 310 return (ENXIO); 311 312 msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX); 313 basetable->gpt_first = 0; 314 basetable->gpt_last = msize - 1; 315 basetable->gpt_entries = msize / basetable->gpt_sectors; 316 return (0); 317 } 318 319 static int 320 g_part_ebr_destroy(struct g_part_table *basetable, struct g_part_parms *gpp) 321 { 322 323 /* Wipe the first sector to clear the partitioning. */ 324 basetable->gpt_smhead |= 1; 325 return (0); 326 } 327 328 static void 329 g_part_ebr_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry, 330 struct sbuf *sb, const char *indent) 331 { 332 struct g_part_ebr_entry *entry; 333 334 entry = (struct g_part_ebr_entry *)baseentry; 335 if (indent == NULL) { 336 /* conftxt: libdisk compatibility */ 337 sbuf_printf(sb, " xs MBREXT xt %u", entry->ent.dp_typ); 338 } else if (entry != NULL) { 339 /* confxml: partition entry information */ 340 sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent, 341 entry->ent.dp_typ); 342 if (entry->ent.dp_flag & 0x80) 343 sbuf_printf(sb, "%s<attrib>active</attrib>\n", indent); 344 } else { 345 /* confxml: scheme information */ 346 } 347 } 348 349 static int 350 g_part_ebr_dumpto(struct g_part_table *table, struct g_part_entry *baseentry) 351 { 352 struct g_part_ebr_entry *entry; 353 354 /* Allow dumping to a FreeBSD partition or Linux swap partition only. */ 355 entry = (struct g_part_ebr_entry *)baseentry; 356 return ((entry->ent.dp_typ == DOSPTYP_386BSD || 357 entry->ent.dp_typ == DOSPTYP_LINSWP) ? 1 : 0); 358 } 359 360 #if defined(GEOM_PART_EBR_COMPAT) 361 static void 362 g_part_ebr_fullname(struct g_part_table *table, struct g_part_entry *entry, 363 struct sbuf *sb, const char *pfx) 364 { 365 struct g_part_entry *iter; 366 u_int idx; 367 368 idx = 5; 369 LIST_FOREACH(iter, &table->gpt_entry, gpe_entry) { 370 if (iter == entry) 371 break; 372 idx++; 373 } 374 sbuf_printf(sb, "%.*s%u", (int)strlen(pfx) - 1, pfx, idx); 375 } 376 #endif 377 378 static int 379 g_part_ebr_modify(struct g_part_table *basetable, 380 struct g_part_entry *baseentry, struct g_part_parms *gpp) 381 { 382 struct g_part_ebr_entry *entry; 383 384 if (gpp->gpp_parms & G_PART_PARM_LABEL) 385 return (EINVAL); 386 387 entry = (struct g_part_ebr_entry *)baseentry; 388 if (gpp->gpp_parms & G_PART_PARM_TYPE) 389 return (ebr_parse_type(gpp->gpp_type, &entry->ent.dp_typ)); 390 return (0); 391 } 392 393 static int 394 g_part_ebr_resize(struct g_part_table *basetable, 395 struct g_part_entry *baseentry, struct g_part_parms *gpp) 396 { 397 struct g_provider *pp; 398 399 if (baseentry != NULL) 400 return (EOPNOTSUPP); 401 pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider; 402 basetable->gpt_last = MIN(pp->mediasize / pp->sectorsize, 403 UINT32_MAX) - 1; 404 return (0); 405 } 406 407 static const char * 408 g_part_ebr_name(struct g_part_table *table, struct g_part_entry *entry, 409 char *buf, size_t bufsz) 410 { 411 412 snprintf(buf, bufsz, "+%08u", entry->gpe_index); 413 return (buf); 414 } 415 416 static int 417 g_part_ebr_precheck(struct g_part_table *table, enum g_part_ctl req, 418 struct g_part_parms *gpp) 419 { 420 #if defined(GEOM_PART_EBR_COMPAT) 421 if (req == G_PART_CTL_DESTROY) 422 return (0); 423 return (ECANCELED); 424 #else 425 /* 426 * The index is a function of the start of the partition. 427 * This is not something the user can override, nor is it 428 * something the common code will do right. We can set the 429 * index now so that we get what we need. 430 */ 431 if (req == G_PART_CTL_ADD) 432 gpp->gpp_index = (gpp->gpp_start / table->gpt_sectors) + 1; 433 return (0); 434 #endif 435 } 436 437 static int 438 g_part_ebr_probe(struct g_part_table *table, struct g_consumer *cp) 439 { 440 char type[64]; 441 struct g_provider *pp; 442 u_char *buf, *p; 443 int error, index, res; 444 uint16_t magic; 445 446 pp = cp->provider; 447 448 /* Sanity-check the provider. */ 449 if (pp->sectorsize < EBRSIZE || pp->mediasize < pp->sectorsize) 450 return (ENOSPC); 451 if (pp->sectorsize > 4096) 452 return (ENXIO); 453 454 /* Check that we have a parent and that it's a MBR. */ 455 if (table->gpt_depth == 0) 456 return (ENXIO); 457 error = g_getattr("PART::scheme", cp, &type); 458 if (error != 0) 459 return (error); 460 if (strcmp(type, "MBR") != 0) 461 return (ENXIO); 462 /* Check that partition has type DOSPTYP_EBR. */ 463 error = g_getattr("PART::type", cp, &type); 464 if (error != 0) 465 return (error); 466 if (strcmp(type, "ebr") != 0) 467 return (ENXIO); 468 469 /* Check that there's a EBR. */ 470 buf = g_read_data(cp, 0L, pp->sectorsize, &error); 471 if (buf == NULL) 472 return (error); 473 474 /* We goto out on mismatch. */ 475 res = ENXIO; 476 477 magic = le16dec(buf + DOSMAGICOFFSET); 478 if (magic != DOSMAGIC) 479 goto out; 480 481 for (index = 0; index < 2; index++) { 482 p = buf + DOSPARTOFF + index * DOSPARTSIZE; 483 if (p[0] != 0 && p[0] != 0x80) 484 goto out; 485 } 486 res = G_PART_PROBE_PRI_NORM; 487 488 out: 489 g_free(buf); 490 return (res); 491 } 492 493 static int 494 g_part_ebr_read(struct g_part_table *basetable, struct g_consumer *cp) 495 { 496 struct dos_partition ent[2]; 497 struct g_provider *pp; 498 struct g_part_entry *baseentry; 499 struct g_part_ebr_table *table; 500 struct g_part_ebr_entry *entry; 501 u_char *buf; 502 off_t ofs, msize; 503 u_int lba; 504 int error, index; 505 506 pp = cp->provider; 507 table = (struct g_part_ebr_table *)basetable; 508 msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX); 509 510 lba = 0; 511 while (1) { 512 ofs = (off_t)lba * pp->sectorsize; 513 buf = g_read_data(cp, ofs, pp->sectorsize, &error); 514 if (buf == NULL) 515 return (error); 516 517 ebr_entry_decode(buf + DOSPARTOFF + 0 * DOSPARTSIZE, ent + 0); 518 ebr_entry_decode(buf + DOSPARTOFF + 1 * DOSPARTSIZE, ent + 1); 519 520 /* The 3rd & 4th entries should be zeroes. */ 521 if (le64dec(buf + DOSPARTOFF + 2 * DOSPARTSIZE) + 522 le64dec(buf + DOSPARTOFF + 3 * DOSPARTSIZE) != 0) { 523 basetable->gpt_corrupt = 1; 524 printf("GEOM: %s: invalid entries in the EBR ignored.\n", 525 pp->name); 526 } 527 #ifndef GEOM_PART_EBR_COMPAT 528 /* Save the first EBR, it can contain a boot code */ 529 if (lba == 0) 530 bcopy(buf, table->ebr, sizeof(table->ebr)); 531 #endif 532 g_free(buf); 533 534 if (ent[0].dp_typ == 0) 535 break; 536 537 if (ent[0].dp_typ == 5 && ent[1].dp_typ == 0) { 538 lba = ent[0].dp_start; 539 continue; 540 } 541 542 index = (lba / basetable->gpt_sectors) + 1; 543 baseentry = (struct g_part_entry *)g_part_new_entry(basetable, 544 index, lba, lba + ent[0].dp_start + ent[0].dp_size - 1); 545 baseentry->gpe_offset = (off_t)(lba + ent[0].dp_start) * 546 pp->sectorsize; 547 entry = (struct g_part_ebr_entry *)baseentry; 548 entry->ent = ent[0]; 549 550 if (ent[1].dp_typ == 0) 551 break; 552 553 lba = ent[1].dp_start; 554 } 555 556 basetable->gpt_entries = msize / basetable->gpt_sectors; 557 basetable->gpt_first = 0; 558 basetable->gpt_last = msize - 1; 559 return (0); 560 } 561 562 static int 563 g_part_ebr_setunset(struct g_part_table *table, struct g_part_entry *baseentry, 564 const char *attrib, unsigned int set) 565 { 566 struct g_part_entry *iter; 567 struct g_part_ebr_entry *entry; 568 int changed; 569 570 if (baseentry == NULL) 571 return (ENODEV); 572 if (strcasecmp(attrib, "active") != 0) 573 return (EINVAL); 574 575 /* Only one entry can have the active attribute. */ 576 LIST_FOREACH(iter, &table->gpt_entry, gpe_entry) { 577 if (iter->gpe_deleted) 578 continue; 579 changed = 0; 580 entry = (struct g_part_ebr_entry *)iter; 581 if (iter == baseentry) { 582 if (set && (entry->ent.dp_flag & 0x80) == 0) { 583 entry->ent.dp_flag |= 0x80; 584 changed = 1; 585 } else if (!set && (entry->ent.dp_flag & 0x80)) { 586 entry->ent.dp_flag &= ~0x80; 587 changed = 1; 588 } 589 } else { 590 if (set && (entry->ent.dp_flag & 0x80)) { 591 entry->ent.dp_flag &= ~0x80; 592 changed = 1; 593 } 594 } 595 if (changed && !iter->gpe_created) 596 iter->gpe_modified = 1; 597 } 598 return (0); 599 } 600 601 static const char * 602 g_part_ebr_type(struct g_part_table *basetable, struct g_part_entry *baseentry, 603 char *buf, size_t bufsz) 604 { 605 struct g_part_ebr_entry *entry; 606 int i; 607 608 entry = (struct g_part_ebr_entry *)baseentry; 609 for (i = 0; i < nitems(ebr_alias_match); i++) { 610 if (ebr_alias_match[i].typ == entry->ent.dp_typ) 611 return (g_part_alias_name(ebr_alias_match[i].alias)); 612 } 613 snprintf(buf, bufsz, "!%d", entry->ent.dp_typ); 614 return (buf); 615 } 616 617 static int 618 g_part_ebr_write(struct g_part_table *basetable, struct g_consumer *cp) 619 { 620 #ifndef GEOM_PART_EBR_COMPAT 621 struct g_part_ebr_table *table; 622 #endif 623 struct g_provider *pp; 624 struct g_part_entry *baseentry, *next; 625 struct g_part_ebr_entry *entry; 626 u_char *buf; 627 u_char *p; 628 int error; 629 630 pp = cp->provider; 631 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO); 632 #ifndef GEOM_PART_EBR_COMPAT 633 table = (struct g_part_ebr_table *)basetable; 634 bcopy(table->ebr, buf, DOSPARTOFF); 635 #endif 636 le16enc(buf + DOSMAGICOFFSET, DOSMAGIC); 637 638 baseentry = LIST_FIRST(&basetable->gpt_entry); 639 while (baseentry != NULL && baseentry->gpe_deleted) 640 baseentry = LIST_NEXT(baseentry, gpe_entry); 641 642 /* Wipe-out the first EBR when there are no slices. */ 643 if (baseentry == NULL) { 644 error = g_write_data(cp, 0, buf, pp->sectorsize); 645 goto out; 646 } 647 648 /* 649 * If the first partition is not in LBA 0, we need to 650 * put a "link" EBR in LBA 0. 651 */ 652 if (baseentry->gpe_start != 0) { 653 ebr_entry_link(basetable, (uint32_t)baseentry->gpe_start, 654 (uint32_t)baseentry->gpe_end, buf + DOSPARTOFF); 655 error = g_write_data(cp, 0, buf, pp->sectorsize); 656 if (error) 657 goto out; 658 } 659 660 do { 661 entry = (struct g_part_ebr_entry *)baseentry; 662 663 p = buf + DOSPARTOFF; 664 p[0] = entry->ent.dp_flag; 665 p[1] = entry->ent.dp_shd; 666 p[2] = entry->ent.dp_ssect; 667 p[3] = entry->ent.dp_scyl; 668 p[4] = entry->ent.dp_typ; 669 p[5] = entry->ent.dp_ehd; 670 p[6] = entry->ent.dp_esect; 671 p[7] = entry->ent.dp_ecyl; 672 le32enc(p + 8, entry->ent.dp_start); 673 le32enc(p + 12, entry->ent.dp_size); 674 675 next = LIST_NEXT(baseentry, gpe_entry); 676 while (next != NULL && next->gpe_deleted) 677 next = LIST_NEXT(next, gpe_entry); 678 679 p += DOSPARTSIZE; 680 if (next != NULL) 681 ebr_entry_link(basetable, (uint32_t)next->gpe_start, 682 (uint32_t)next->gpe_end, p); 683 else 684 bzero(p, DOSPARTSIZE); 685 686 error = g_write_data(cp, baseentry->gpe_start * pp->sectorsize, 687 buf, pp->sectorsize); 688 #ifndef GEOM_PART_EBR_COMPAT 689 if (baseentry->gpe_start == 0) 690 bzero(buf, DOSPARTOFF); 691 #endif 692 baseentry = next; 693 } while (!error && baseentry != NULL); 694 695 out: 696 g_free(buf); 697 return (error); 698 } 699