1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  23  * Copyright 2017 The MathWorks, Inc.  All rights reserved.
  24  */
  25 
  26 #include <stdio.h>
  27 #include <stdlib.h>
  28 #include <string.h>
  29 #include <strings.h>
  30 #include <unistd.h>
  31 #include <errno.h>
  32 #include <fcntl.h>
  33 #include <ctype.h>
  34 #include <sys/stat.h>
  35 #include <sys/types.h>
  36 #include <sys/param.h>
  37 #include <sys/systeminfo.h>
  38 #include <sys/efi_partition.h>
  39 #include <sys/byteorder.h>
  40 
  41 #include <sys/vtoc.h>
  42 #include <sys/tty.h>
  43 #include <sys/dktp/fdisk.h>
  44 #include <sys/dkio.h>
  45 #include <sys/mnttab.h>
  46 #include "libfdisk.h"
  47 
  48 #define DEFAULT_PATH_PREFIX     "/dev/rdsk/"
  49 
  50 static void fdisk_free_ld_nodes(ext_part_t *epp);
  51 static void fdisk_ext_place_in_sorted_list(ext_part_t *epp,
  52     logical_drive_t *newld);
  53 static void fdisk_ext_remove_from_sorted_list(ext_part_t *epp,
  54     logical_drive_t *delld);
  55 static int fdisk_ext_overlapping_parts(ext_part_t *epp, uint32_t begsec,
  56     uint32_t endsec);
  57 static int fdisk_read_extpart(ext_part_t *epp);
  58 static void fdisk_set_CHS_values(ext_part_t *epp, struct ipart *part);
  59 static int fdisk_init_master_part_table(ext_part_t *epp);
  60 static struct ipart *fdisk_alloc_part_table();
  61 static int fdisk_read_master_part_table(ext_part_t *epp);
  62 
  63 static int
  64 fdisk_init_disk_geom(ext_part_t *epp)
  65 {
  66         struct dk_geom disk_geom;
  67         struct dk_minfo disk_info;
  68         int no_virtgeom_ioctl = 0, no_physgeom_ioctl = 0;
  69 
  70         /* Get disk's HBA (virtual) geometry */
  71         errno = 0;
  72         if (ioctl(epp->dev_fd, DKIOCG_VIRTGEOM, &disk_geom)) {
  73                 if (errno == ENOTTY) {
  74                         no_virtgeom_ioctl = 1;
  75                 } else if (errno == EINVAL) {
  76                         /*
  77                          * This means that the ioctl exists, but
  78                          * is invalid for this disk, meaning the
  79                          * disk doesn't have an HBA geometry
  80                          * (like, say, it's larger than 8GB).
  81                          */
  82                         epp->disk_geom.virt_cyl = epp->disk_geom.virt_heads =
  83                             epp->disk_geom.virt_sec = 0;
  84                 } else {
  85                         return (FDISK_ENOVGEOM);
  86                 }
  87         } else {
  88                 /* save virtual geometry values obtained by ioctl */
  89                 epp->disk_geom.virt_cyl = disk_geom.dkg_ncyl;
  90                 epp->disk_geom.virt_heads = disk_geom.dkg_nhead;
  91                 epp->disk_geom.virt_sec = disk_geom.dkg_nsect;
  92         }
  93 
  94         errno = 0;
  95         if (ioctl(epp->dev_fd, DKIOCG_PHYGEOM, &disk_geom)) {
  96                 if (errno == ENOTTY) {
  97                         no_physgeom_ioctl = 1;
  98                 } else {
  99                         return (FDISK_ENOPGEOM);
 100                 }
 101         }
 102         /*
 103          * Call DKIOCGGEOM if the ioctls for physical and virtual
 104          * geometry fail. Get both from this generic call.
 105          */
 106         if (no_virtgeom_ioctl && no_physgeom_ioctl) {
 107                 errno = 0;
 108                 if (ioctl(epp->dev_fd, DKIOCGGEOM, &disk_geom)) {
 109                         return (FDISK_ENOLGEOM);
 110                 }
 111         }
 112 
 113         epp->disk_geom.phys_cyl = disk_geom.dkg_ncyl;
 114         epp->disk_geom.phys_heads = disk_geom.dkg_nhead;
 115         epp->disk_geom.phys_sec = disk_geom.dkg_nsect;
 116         epp->disk_geom.alt_cyl = disk_geom.dkg_acyl;
 117 
 118         /*
 119          * If DKIOCGMEDIAINFO ioctl succeeds, set the dki_lbsize as the
 120          * size of the sector, else default to 512
 121          */
 122         if (ioctl(epp->dev_fd, DKIOCGMEDIAINFO, (caddr_t)&disk_info) < 0) {
 123                 /* ioctl failed, falling back to default value of 512 bytes */
 124                 epp->disk_geom.sectsize = 512;
 125         } else {
 126                 epp->disk_geom.sectsize = ((disk_info.dki_lbsize) ?
 127                     disk_info.dki_lbsize : 512);
 128         }
 129 
 130         /*
 131          * if hba geometry was not set by DKIOC_VIRTGEOM
 132          * or we got an invalid hba geometry
 133          * then set hba geometry based on max values
 134          */
 135         if (no_virtgeom_ioctl || disk_geom.dkg_ncyl == 0 ||
 136             disk_geom.dkg_nhead == 0 || disk_geom.dkg_nsect == 0 ||
 137             disk_geom.dkg_ncyl > MAX_CYL || disk_geom.dkg_nhead > MAX_HEAD ||
 138             disk_geom.dkg_nsect > MAX_SECT) {
 139                 epp->disk_geom.virt_sec      = MAX_SECT;
 140                 epp->disk_geom.virt_heads    = MAX_HEAD + 1;
 141                 epp->disk_geom.virt_cyl      = (epp->disk_geom.phys_cyl *
 142                     epp->disk_geom.phys_heads * epp->disk_geom.phys_sec) /
 143                     (epp->disk_geom.virt_sec * epp->disk_geom.virt_heads);
 144         }
 145         return (FDISK_SUCCESS);
 146 }
 147 
 148 /*
 149  * Initialise important members of the ext_part_t structure and
 150  * other data structures vital to functionality of libfdisk
 151  */
 152 int
 153 libfdisk_init(ext_part_t **epp, char *devstr, struct ipart *parttab, int opflag)
 154 {
 155         ext_part_t *temp;
 156         struct stat sbuf;
 157         int rval = FDISK_SUCCESS;
 158         int found_bad_magic = 0;
 159 
 160         if ((temp = calloc(1, sizeof (ext_part_t))) == NULL) {
 161                 *epp = NULL;
 162                 return (ENOMEM);
 163         }
 164 
 165         (void) strncpy(temp->device_name, devstr,
 166             sizeof (temp->device_name));
 167 
 168         /* Try to stat the node as provided */
 169         if (stat(temp->device_name, &sbuf) != 0) {
 170 
 171                 /* Prefix /dev/rdsk/ and stat again */
 172                 (void) snprintf(temp->device_name, sizeof (temp->device_name),
 173                     "%s%s", DEFAULT_PATH_PREFIX, devstr);
 174 
 175                 if (stat(temp->device_name, &sbuf) != 0) {
 176 
 177                         /*
 178                          * In case of an EFI labeled disk, the device name
 179                          * could be cN[tN]dN. There is no pN. So we add "p0"
 180                          * at the end if we do not find it and stat again.
 181                          */
 182                         if (strrchr(temp->device_name, 'p') == NULL) {
 183                                 (void) strcat(temp->device_name, "p0");
 184                         }
 185 
 186                         if (stat(temp->device_name, &sbuf) != 0) {
 187 
 188                                 /* Failed all options, give up */
 189                                 rval = EINVAL;
 190                                 goto fail;
 191                         }
 192                 }
 193         }
 194 
 195         /* Make sure the device is a raw device */
 196         if ((sbuf.st_mode & S_IFMT) != S_IFCHR) {
 197                 rval = EINVAL;
 198                 goto fail;
 199         }
 200 
 201         temp->ld_head = NULL;
 202         temp->sorted_ld_head = NULL;
 203 
 204         if ((temp->dev_fd = open(temp->device_name, O_RDWR, 0666)) < 0) {
 205                 rval = EINVAL;
 206                 goto fail;
 207         }
 208 
 209         if ((temp->mtable = parttab) == NULL) {
 210                 if ((rval = fdisk_init_master_part_table(temp)) !=
 211                     FDISK_SUCCESS) {
 212                         /*
 213                          * When we have no fdisk magic 0xAA55 on the disk,
 214                          * we return FDISK_EBADMAGIC after successfully
 215                          * obtaining the disk geometry.
 216                          */
 217                         if (rval != FDISK_EBADMAGIC)
 218                                 goto fail;
 219                         else
 220                                 found_bad_magic = 1;
 221                 }
 222         }
 223 
 224         temp->op_flag = opflag;
 225 
 226         if ((rval = fdisk_init_disk_geom(temp)) != FDISK_SUCCESS) {
 227                 goto fail;
 228         }
 229 
 230         *epp = temp;
 231 
 232         if (found_bad_magic != 0) {
 233                 return (FDISK_EBADMAGIC);
 234         }
 235 
 236         if (opflag & FDISK_READ_DISK) {
 237                 rval = fdisk_read_extpart(*epp);
 238         }
 239         return (rval);
 240 
 241 fail:
 242         *epp = NULL;
 243         free(temp);
 244         return (rval);
 245 }
 246 
 247 int
 248 libfdisk_reset(ext_part_t *epp)
 249 {
 250         int rval = FDISK_SUCCESS;
 251 
 252         fdisk_free_ld_nodes(epp);
 253         epp->first_ebr_is_null = 1;
 254         epp->corrupt_logical_drives = 0;
 255         epp->logical_drive_count = 0;
 256         epp->invalid_bb_sig[0] = 0;
 257         if (epp->op_flag & FDISK_READ_DISK) {
 258                 rval = fdisk_read_extpart(epp);
 259         }
 260         return (rval);
 261 }
 262 
 263 void
 264 libfdisk_fini(ext_part_t **epp)
 265 {
 266         if (*epp == NULL)
 267                 return;
 268 
 269         fdisk_free_ld_nodes(*epp);
 270         (void) close((*epp)->dev_fd);
 271         free(*epp);
 272         *epp = NULL;
 273 }
 274 
 275 int
 276 fdisk_is_linux_swap(ext_part_t *epp, uint32_t part_start, uint64_t *lsm_offset)
 277 {
 278         int             i;
 279         int             rval = -1;
 280         off_t           seek_offset;
 281         uint32_t        linux_pg_size;
 282         char            *buf, *linux_swap_magic;
 283         int             sec_sz = fdisk_get_disk_geom(epp, PHYSGEOM, SSIZE);
 284         off_t           label_offset;
 285 
 286         /*
 287          * Known linux kernel page sizes
 288          * The linux swap magic is found as the last 10 bytes of a disk chunk
 289          * at the beginning of the linux swap partition whose size is that of
 290          * kernel page size.
 291          */
 292         uint32_t        linux_pg_size_arr[] = {4096, };
 293 
 294         if ((buf = calloc(1, sec_sz)) == NULL) {
 295                 return (ENOMEM);
 296         }
 297 
 298         /*
 299          * Check if there is a sane Solaris VTOC
 300          * If there is a valid vtoc, no need to lookup
 301          * for the linux swap signature.
 302          */
 303         label_offset = (part_start + DK_LABEL_LOC) * sec_sz;
 304         if (lseek(epp->dev_fd, label_offset, SEEK_SET) < 0) {
 305                 rval = EIO;
 306                 goto done;
 307         }
 308 
 309         if ((rval = read(epp->dev_fd, buf, sec_sz)) < sec_sz) {
 310                 rval = EIO;
 311                 goto done;
 312         }
 313 
 314 
 315         if ((((struct dk_label *)buf)->dkl_magic == DKL_MAGIC) &&
 316             (((struct dk_label *)buf)->dkl_vtoc.v_sanity == VTOC_SANE)) {
 317                 rval = -1;
 318                 goto done;
 319         }
 320 
 321         /* No valid vtoc, so check for linux swap signature */
 322         linux_swap_magic = buf + sec_sz - LINUX_SWAP_MAGIC_LENGTH;
 323 
 324         for (i = 0; i < sizeof (linux_pg_size_arr)/sizeof (uint32_t); i++) {
 325                 linux_pg_size = linux_pg_size_arr[i];
 326                 seek_offset = linux_pg_size/sec_sz - 1;
 327                 seek_offset += part_start;
 328                 seek_offset *= sec_sz;
 329 
 330                 if (lseek(epp->dev_fd, seek_offset, SEEK_SET) < 0) {
 331                         rval = EIO;
 332                         break;
 333                 }
 334 
 335                 if ((rval = read(epp->dev_fd, buf, sec_sz)) < sec_sz) {
 336                         rval = EIO;
 337                         break;
 338                 }
 339 
 340                 if ((strncmp(linux_swap_magic, "SWAP-SPACE",
 341                     LINUX_SWAP_MAGIC_LENGTH) == 0) ||
 342                     (strncmp(linux_swap_magic, "SWAPSPACE2",
 343                     LINUX_SWAP_MAGIC_LENGTH) == 0)) {
 344                         /* Found a linux swap */
 345                         rval = 0;
 346                         if (lsm_offset != NULL)
 347                                 *lsm_offset = (uint64_t)seek_offset;
 348                         break;
 349                 }
 350         }
 351 
 352 done:
 353         free(buf);
 354         return (rval);
 355 }
 356 
 357 int
 358 fdisk_get_solaris_part(ext_part_t *epp, int *pnum, uint32_t *begsec,
 359     uint32_t *numsec)
 360 {
 361         logical_drive_t *temp = fdisk_get_ld_head(epp);
 362         uint32_t part_start;
 363         int pno;
 364         int rval = -1;
 365 
 366         for (pno = 5; temp != NULL; temp = temp->next, pno++) {
 367                 if (fdisk_is_solaris_part(LE_8(temp->parts[0].systid))) {
 368                         part_start = temp->abs_secnum + temp->logdrive_offset;
 369                         if ((temp->parts[0].systid == SUNIXOS) &&
 370                             (fdisk_is_linux_swap(epp, part_start,
 371                             NULL) == 0)) {
 372                                 continue;
 373                         }
 374                         *pnum = pno;
 375                         *begsec = part_start;
 376                         *numsec = temp->numsect;
 377                         rval = FDISK_SUCCESS;
 378                 }
 379         }
 380         return (rval);
 381 }
 382 
 383 int
 384 fdisk_get_part_info(ext_part_t *epp, int pnum, uchar_t *sysid, uint32_t *begsec,
 385     uint32_t *numsec)
 386 {
 387         logical_drive_t *temp = fdisk_get_ld_head(epp);
 388         int pno;
 389 
 390         if ((pnum < 5) || (pnum >= MAX_EXT_PARTS + 5)) {
 391                 return (EINVAL);
 392         }
 393 
 394         for (pno = 5; (pno < pnum) && (temp != NULL); temp = temp->next, pno++)
 395                 ;
 396 
 397         if (temp == NULL) {
 398                 return (EINVAL);
 399         }
 400 
 401         *sysid = LE_8(temp->parts[0].systid);
 402         *begsec = temp->abs_secnum + temp->logdrive_offset;
 403         *numsec = temp->numsect;
 404         return (FDISK_SUCCESS);
 405 }
 406 
 407 /*
 408  * Allocate a node of type logical_drive_t and return the pointer to it
 409  */
 410 static logical_drive_t *
 411 fdisk_alloc_ld_node()
 412 {
 413         logical_drive_t *temp;
 414 
 415         if ((temp = calloc(1, sizeof (logical_drive_t))) == NULL) {
 416                 return (NULL);
 417         }
 418         temp->next = NULL;
 419         return (temp);
 420 }
 421 
 422 /*
 423  * Free all the logical_drive_t's allocated during the run
 424  */
 425 static void
 426 fdisk_free_ld_nodes(ext_part_t *epp)
 427 {
 428         logical_drive_t *temp;
 429 
 430         for (temp = epp->ld_head; temp != NULL; ) {
 431                 temp = epp->ld_head -> next;
 432                 free(epp->ld_head);
 433                 epp->ld_head = temp;
 434         }
 435         epp->ld_head = NULL;
 436         epp->sorted_ld_head = NULL;
 437 }
 438 
 439 /*
 440  * Find the first free sector within the extended partition
 441  */
 442 int
 443 fdisk_ext_find_first_free_sec(ext_part_t *epp, uint32_t *first_free_sec)
 444 {
 445         logical_drive_t *temp;
 446         uint32_t last_free_sec;
 447 
 448         *first_free_sec = epp->ext_beg_sec;
 449 
 450         if (epp->ld_head == NULL) {
 451                 return (FDISK_SUCCESS);
 452         }
 453 
 454         /*
 455          * When the first logical drive is out of order, we need to adjust
 456          * first_free_sec accordingly. In this case, the first extended
 457          * partition sector is not free even though the actual logical drive
 458          * does not occupy space from the beginning of the extended partition.
 459          * The next free sector would be the second sector of the extended
 460          * partition.
 461          */
 462         if (epp->ld_head->abs_secnum > epp->ext_beg_sec +
 463             MAX_LOGDRIVE_OFFSET) {
 464                 (*first_free_sec)++;
 465         }
 466 
 467         while (*first_free_sec <= epp->ext_end_sec) {
 468                 for (temp = epp->sorted_ld_head; temp != NULL; temp =
 469                     temp->sorted_next) {
 470                         if (temp->abs_secnum == *first_free_sec) {
 471                                 *first_free_sec = temp->abs_secnum +
 472                                     temp->logdrive_offset + temp->numsect;
 473                         }
 474                 }
 475 
 476                 last_free_sec = fdisk_ext_find_last_free_sec(epp,
 477                     *first_free_sec);
 478 
 479                 if ((last_free_sec - *first_free_sec) < MAX_LOGDRIVE_OFFSET) {
 480                         /*
 481                          * Minimum size of a partition assumed to be atleast one
 482                          * sector.
 483                          */
 484                         *first_free_sec = last_free_sec + 1;
 485                         continue;
 486                 }
 487 
 488                 break;
 489         }
 490 
 491         if (*first_free_sec > epp->ext_end_sec) {
 492                 return (FDISK_EOOBOUND);
 493         }
 494 
 495         return (FDISK_SUCCESS);
 496 }
 497 
 498 /*
 499  * Find the last free sector within the extended partition given, a beginning
 500  * sector (so that the range - "begsec to last_free_sec" is contiguous)
 501  */
 502 uint32_t
 503 fdisk_ext_find_last_free_sec(ext_part_t *epp, uint32_t begsec)
 504 {
 505         logical_drive_t *temp;
 506         uint32_t last_free_sec;
 507 
 508         last_free_sec = epp->ext_end_sec;
 509         for (temp = epp->sorted_ld_head; temp != NULL;
 510             temp = temp->sorted_next) {
 511                 if (temp->abs_secnum > begsec) {
 512                         last_free_sec = temp->abs_secnum - 1;
 513                         break;
 514                 }
 515         }
 516         return (last_free_sec);
 517 }
 518 
 519 /*
 520  * Place the given ext_part_t structure in a sorted list, sorted in the
 521  * ascending order of their beginning sectors.
 522  */
 523 static void
 524 fdisk_ext_place_in_sorted_list(ext_part_t *epp, logical_drive_t *newld)
 525 {
 526         logical_drive_t *pre, *cur;
 527 
 528         if (newld->abs_secnum < epp->sorted_ld_head->abs_secnum) {
 529                 newld->sorted_next = epp->sorted_ld_head;
 530                 epp->sorted_ld_head = newld;
 531                 return;
 532         }
 533         pre = cur = epp->sorted_ld_head;
 534 
 535         for (; cur != NULL; pre = cur, cur = cur->sorted_next) {
 536                 if (newld->abs_secnum < cur->abs_secnum) {
 537                         break;
 538                 }
 539         }
 540 
 541         newld->sorted_next = cur;
 542         pre->sorted_next = newld;
 543 }
 544 
 545 static void
 546 fdisk_ext_remove_from_sorted_list(ext_part_t *epp, logical_drive_t *delld)
 547 {
 548         logical_drive_t *pre, *cur;
 549 
 550         if (delld == epp->sorted_ld_head) {
 551                 epp->sorted_ld_head = delld->sorted_next;
 552                 return;
 553         }
 554 
 555         pre = cur = epp->sorted_ld_head;
 556 
 557         for (; cur != NULL; pre = cur, cur = cur->sorted_next) {
 558                 if (cur->abs_secnum == delld->abs_secnum) {
 559                         /* Found */
 560                         break;
 561                 }
 562         }
 563 
 564         pre->sorted_next = cur->sorted_next;
 565 }
 566 
 567 static int
 568 fdisk_ext_overlapping_parts(ext_part_t *epp, uint32_t begsec, uint32_t endsec)
 569 {
 570         logical_drive_t *temp;
 571         uint32_t firstsec, lastsec, last_free_sec;
 572 
 573         for (temp = epp->ld_head; temp != NULL; temp = temp->next) {
 574                 firstsec = temp->abs_secnum;
 575                 lastsec = firstsec + temp->logdrive_offset + temp->numsect - 1;
 576                 if ((begsec >= firstsec) &&
 577                     (begsec <= lastsec)) {
 578                         return (1);
 579                 }
 580         }
 581 
 582         /*
 583          * Find the maximum possible end sector value
 584          * given a beginning sector value
 585          */
 586         last_free_sec = fdisk_ext_find_last_free_sec(epp, begsec);
 587 
 588         if (endsec > last_free_sec) {
 589                 return (1);
 590         }
 591         return (0);
 592 }
 593 
 594 /*
 595  * Check if the logical drive boundaries are sane
 596  */
 597 int
 598 fdisk_validate_logical_drive(ext_part_t *epp, uint32_t begsec,
 599     uint32_t offset, uint32_t numsec)
 600 {
 601         uint32_t endsec;
 602 
 603         endsec = begsec + offset + numsec - 1;
 604         if (begsec < epp->ext_beg_sec ||
 605             begsec > epp->ext_end_sec ||
 606             endsec < epp->ext_beg_sec ||
 607             endsec > epp->ext_end_sec ||
 608             endsec < begsec ||
 609             fdisk_ext_overlapping_parts(epp, begsec, endsec)) {
 610                 return (1);
 611         }
 612 
 613         return (0);
 614 }
 615 
 616 /*
 617  * Procedure to walk through the extended partitions and build a Singly
 618  * Linked List out of the data.
 619  */
 620 static int
 621 fdisk_read_extpart(ext_part_t *epp)
 622 {
 623         struct ipart *fdp, *ext_fdp;
 624         int i = 0, j = 0, ext_part_found = 0, lpart = 5;
 625         off_t secnum, offset;
 626         logical_drive_t *temp, *ep_ptr;
 627         unsigned char *ext_buf;
 628         int sectsize = epp->disk_geom.sectsize;
 629 
 630         if ((ext_buf = (uchar_t *)malloc(sectsize)) == NULL) {
 631                 return (ENOMEM);
 632         }
 633         fdp = epp->mtable;
 634 
 635         for (i = 0; (i < FD_NUMPART) && (!ext_part_found); i++, fdp++) {
 636                 if (fdisk_is_dos_extended(LE_8(fdp->systid))) {
 637                         ext_part_found = 1;
 638                         secnum = LE_32(fdp->relsect);
 639                         offset = secnum * sectsize;
 640                         epp->ext_beg_sec = secnum;
 641                         epp->ext_end_sec = secnum + LE_32(fdp->numsect) - 1;
 642                         epp->ext_beg_cyl =
 643                             FDISK_SECT_TO_CYL(epp, epp->ext_beg_sec);
 644                         epp->ext_end_cyl =
 645                             FDISK_SECT_TO_CYL(epp, epp->ext_end_sec);
 646 
 647                         /*LINTED*/
 648                         while (B_TRUE) {
 649                                 if (lseek(epp->dev_fd, offset, SEEK_SET) < 0) {
 650                                         return (EIO);
 651                                 }
 652                                 if (read(epp->dev_fd, ext_buf, sectsize) <
 653                                     sectsize) {
 654                                         return (EIO);
 655                                 }
 656                                 /*LINTED*/
 657                                 ext_fdp = (struct ipart *)
 658                                     (&ext_buf[FDISK_PART_TABLE_START]);
 659                                 if ((LE_32(ext_fdp->relsect) == 0) &&
 660                                     (epp->logical_drive_count == 0)) {
 661                                         /* No logical drives defined */
 662                                         epp->first_ebr_is_null = 0;
 663                                         return (FDISK_ENOLOGDRIVE);
 664                                 }
 665 
 666                                 temp = fdisk_alloc_ld_node();
 667                                 temp->abs_secnum = secnum;
 668                                 temp->logdrive_offset =
 669                                     LE_32(ext_fdp->relsect);
 670                                 temp ->numsect = LE_32(ext_fdp->numsect);
 671                                 if (epp->ld_head == NULL) {
 672                                         /* adding first logical drive */
 673                                         if (temp->logdrive_offset >
 674                                             MAX_LOGDRIVE_OFFSET) {
 675                                                 /* out of order */
 676                                                 temp->abs_secnum +=
 677                                                     temp->logdrive_offset;
 678                                                 temp->logdrive_offset = 0;
 679                                         }
 680                                 }
 681                                 temp->begcyl =
 682                                     FDISK_SECT_TO_CYL(epp, temp->abs_secnum);
 683                                 temp->endcyl = FDISK_SECT_TO_CYL(epp,
 684                                     temp->abs_secnum +
 685                                     temp->logdrive_offset +
 686                                     temp->numsect - 1);
 687 
 688                                 /*
 689                                  * Check for sanity of logical drives
 690                                  */
 691                                 if (fdisk_validate_logical_drive(epp,
 692                                     temp->abs_secnum, temp->logdrive_offset,
 693                                     temp->numsect)) {
 694                                         epp->corrupt_logical_drives = 1;
 695                                         free(temp);
 696                                         return (FDISK_EBADLOGDRIVE);
 697                                 }
 698 
 699                                 temp->parts[0] = *ext_fdp;
 700                                 ext_fdp++;
 701                                 temp->parts[1] = *ext_fdp;
 702 
 703                                 if (epp->ld_head == NULL) {
 704                                         epp->ld_head = temp;
 705                                         epp->sorted_ld_head = temp;
 706                                         ep_ptr = temp;
 707                                         epp->logical_drive_count = 1;
 708                                 } else {
 709                                         ep_ptr->next = temp;
 710                                         ep_ptr = temp;
 711                                         fdisk_ext_place_in_sorted_list(epp,
 712                                             temp);
 713                                         epp->logical_drive_count++;
 714                                 }
 715 
 716                                 /*LINTED*/
 717                                 if (LE_16((*(uint16_t *)&ext_buf[510])) !=
 718                                     MBB_MAGIC) {
 719                                         epp->invalid_bb_sig[j++] = lpart;
 720                                         temp->modified = FDISK_MINOR_WRITE;
 721                                 }
 722 
 723                                 if (LE_32(ext_fdp->relsect) == 0)
 724                                         break;
 725                                 else {
 726                                         secnum = LE_32(fdp->relsect) +
 727                                             LE_32(ext_fdp->relsect);
 728                                         offset = secnum * sectsize;
 729                                 }
 730                                 lpart++;
 731                         }
 732                 }
 733         }
 734         return (FDISK_SUCCESS);
 735 }
 736 
 737 static int
 738 fdisk_init_master_part_table(ext_part_t *epp)
 739 {
 740         int rval;
 741         if ((epp->mtable = fdisk_alloc_part_table()) == NULL) {
 742                 return (ENOMEM);
 743         }
 744         rval = fdisk_read_master_part_table(epp);
 745         if (rval) {
 746                 return (rval);
 747         }
 748         return (FDISK_SUCCESS);
 749 }
 750 
 751 static struct ipart *
 752 fdisk_alloc_part_table()
 753 {
 754         int size = sizeof (struct ipart);
 755         struct ipart *table;
 756 
 757         if ((table = calloc(4, size)) == NULL) {
 758                 return (NULL);
 759         }
 760 
 761         return (table);
 762 }
 763 
 764 /*
 765  * Reads the master fdisk partition table from the device assuming that it has
 766  * a valid table.
 767  * MBR is supposed to be of 512 bytes no matter what the device block size is.
 768  */
 769 static int
 770 fdisk_read_master_part_table(ext_part_t *epp)
 771 {
 772         struct dk_minfo_ext dkmp_ext;
 773         uchar_t *buf;
 774         int sectsize;
 775         int size = sizeof (struct ipart);
 776         int cpcnt = FD_NUMPART * size;
 777 
 778         if (lseek(epp->dev_fd, 0, SEEK_SET) < 0) {
 779                 return (EIO);
 780         }
 781         if (ioctl(epp->dev_fd, DKIOCGMEDIAINFOEXT, &dkmp_ext) < 0) {
 782                 return (EIO);
 783         }
 784         if (dkmp_ext.dki_lbsize < 512) {
 785                 return (EIO);
 786         }
 787         sectsize = dkmp_ext.dki_lbsize;
 788         buf = calloc(sectsize, sizeof (uchar_t));
 789         if (buf == NULL) {
 790                 return (ENOMEM);
 791         }
 792         if (read(epp->dev_fd, buf, sectsize) < sectsize) {
 793                 free(buf);
 794                 return (EIO);
 795         }
 796 
 797         /*LINTED*/
 798         if (LE_16((*(uint16_t *)&buf[510])) != MBB_MAGIC) {
 799                 bzero(epp->mtable, cpcnt);
 800                 free(buf);
 801                 return (FDISK_EBADMAGIC);
 802         }
 803 
 804         bcopy(&buf[FDISK_PART_TABLE_START], epp->mtable, cpcnt);
 805         free(buf);
 806 
 807         return (FDISK_SUCCESS);
 808 }
 809 
 810 int
 811 fdisk_ext_part_exists(ext_part_t *epp)
 812 {
 813         int i;
 814         struct ipart *part_table = epp->mtable;
 815 
 816         if (part_table == NULL) {
 817                 /* No extended partition found */
 818                 return (0);
 819         }
 820 
 821         for (i = 0; i < FD_NUMPART; i++) {
 822                 if (fdisk_is_dos_extended(LE_8(part_table[i].systid))) {
 823                         break;
 824                 }
 825         }
 826 
 827         if (i == FD_NUMPART) {
 828                 /* No extended partition found */
 829                 return (0);
 830         }
 831         return (1);
 832 }
 833 
 834 int
 835 fdisk_ext_validate_part_start(ext_part_t *epp, uint32_t begcyl,
 836     uint32_t *begsec)
 837 {
 838         logical_drive_t *temp;
 839         uint32_t first_free_sec;
 840         uint32_t first_free_cyl;
 841         int rval;
 842 
 843         rval = fdisk_ext_find_first_free_sec(epp, &first_free_sec);
 844         if (rval != FDISK_SUCCESS) {
 845                 return (rval);
 846         }
 847 
 848         first_free_cyl = FDISK_SECT_TO_CYL(epp, first_free_sec);
 849         if (begcyl == first_free_cyl) {
 850                 *begsec = first_free_sec;
 851                 return (FDISK_SUCCESS);
 852         }
 853 
 854         /* Check if the cylinder number is beyond the extended partition */
 855         if ((begcyl < epp->ext_beg_cyl) || (begcyl > epp->ext_end_cyl)) {
 856                 return (FDISK_EOOBOUND);
 857         }
 858 
 859         for (temp = epp->ld_head; temp != NULL; temp = temp->next) {
 860                 if ((begcyl >= temp->begcyl) &&
 861                     (begcyl <= temp->endcyl)) {
 862                         return (FDISK_EOVERLAP);
 863                 }
 864         }
 865         *begsec = FDISK_CYL_TO_SECT(epp, begcyl);
 866 
 867         return (FDISK_SUCCESS);
 868 }
 869 
 870 void
 871 fdisk_change_logical_drive_id(ext_part_t *epp, int pno, uchar_t partid)
 872 {
 873         logical_drive_t *temp;
 874         int i;
 875 
 876         i = FD_NUMPART + 1;
 877         for (temp = epp->ld_head; i < pno; temp = temp->next, i++)
 878                 ;
 879 
 880         temp->parts[0].systid = LE_8(partid);
 881         temp->modified = FDISK_MAJOR_WRITE;
 882 }
 883 
 884 /*
 885  * A couple of special scenarios :
 886  * 1. Since the first logical drive's EBR is always at the beginning of the
 887  * extended partition, any specification that starts the first logical drive
 888  * out of order will need to address the following issue :
 889  * If the beginning of the drive is not coinciding with the beginning of the
 890  * extended partition  and :
 891  * a) The start is within MAX_LOGDRIVE_OFFSET, the offset changes from the
 892  *      default of 63 to less than 63.
 893  *      logdrive_offset is updated to keep track of the space between
 894  *      the beginning of the logical drive and extended partition. abs_secnum
 895  *      points to the beginning of the extended partition.
 896  * b) The start is greater than MAX_LOGDRIVE_OFFSET, the offset changes from
 897  *      the default of 63 to greater than 63.
 898  *      logdrive_offset is set to 0. abs_secnum points to the beginning of the
 899  *      logical drive, which is at an offset from the extended partition.
 900  */
 901 void
 902 fdisk_add_logical_drive(ext_part_t *epp, uint32_t begsec, uint32_t endsec,
 903     uchar_t partid)
 904 {
 905         logical_drive_t *temp, *pre, *cur;
 906         struct ipart *part;
 907 
 908         temp = fdisk_alloc_ld_node();
 909         temp->abs_secnum = begsec;
 910         temp->logdrive_offset = MAX_LOGDRIVE_OFFSET;
 911         temp->numsect = endsec - begsec + 1 - MAX_LOGDRIVE_OFFSET;
 912         temp->begcyl = FDISK_SECT_TO_CYL(epp, begsec);
 913         temp->endcyl = FDISK_SECT_TO_CYL(epp, endsec);
 914         temp->modified = FDISK_MAJOR_WRITE;
 915 
 916         part            = &temp->parts[0];
 917         part->bootid = 0;
 918         part->systid = LE_8(partid);
 919         part->relsect        = MAX_LOGDRIVE_OFFSET;
 920         part->numsect        = LE_32(temp->numsect);
 921 
 922         fdisk_set_CHS_values(epp, part);
 923 
 924         if (epp->ld_head == NULL) {
 925                 epp->corrupt_logical_drives = 0;
 926                 if (begsec != epp->ext_beg_sec) {
 927                         part->relsect = LE_32(begsec - epp->ext_beg_sec);
 928                         temp->numsect = endsec - begsec + 1;
 929                         part->numsect = LE_32(temp->numsect);
 930                         if (LE_32(part->relsect) > MAX_LOGDRIVE_OFFSET) {
 931                                 temp->logdrive_offset = 0;
 932                         } else {
 933                                 temp->abs_secnum = epp->ext_beg_sec;
 934                                 temp->logdrive_offset = LE_32(part->relsect);
 935                         }
 936                 }
 937                 epp->first_ebr_is_null = 0;
 938                 epp->ld_head = temp;
 939                 epp->sorted_ld_head = temp;
 940                 epp->logical_drive_count = 1;
 941                 return;
 942         }
 943 
 944         if (temp->abs_secnum == epp->ext_beg_sec) {
 945                 part->relsect = LE_32(LE_32(part->relsect) - 1);
 946                 temp->logdrive_offset--;
 947                 temp->abs_secnum++;
 948         }
 949 
 950         for (pre = cur = epp->ld_head; cur != NULL; pre = cur, cur = cur->next)
 951                 ;
 952 
 953         part = &pre->parts[1];
 954         part->bootid = 0;
 955         part->systid = LE_8(EXTDOS);
 956         part->relsect        = LE_32(temp->abs_secnum - epp->ext_beg_sec);
 957         part->numsect        = LE_32(temp->numsect + temp->logdrive_offset);
 958 
 959         fdisk_set_CHS_values(epp, part);
 960 
 961         pre->next = temp;
 962         pre->modified = FDISK_MAJOR_WRITE;
 963         epp->logical_drive_count++;
 964         fdisk_ext_place_in_sorted_list(epp, temp);
 965 }
 966 
 967 /*
 968  * There are 2 cases that need to be handled.
 969  * 1. Deleting the first extended partition :
 970  *      The peculiarity of this case is that the offset of the first extended
 971  *      partition is always indicated by the entry in the master boot record.
 972  *      (MBR). This never changes, unless the extended partition itself is
 973  *      deleted. Hence, the location of the first EBR is fixed.
 974  *      It is only the logical drive which is deleted. This first EBR now gives
 975  *      information of the next logical drive and the info about the subsequent
 976  *      extended partition. Hence the "relsect" of the first EBR is modified to
 977  *      point to the next logical drive.
 978  *
 979  * 2. Deleting an intermediate extended partition.
 980  *      This is quite normal and follows the semantics of a normal linked list
 981  *      delete operation. The node being deleted has the information about the
 982  *      logical drive that it houses and the location and the size of the next
 983  *      extended partition. This informationis transferred to the node previous
 984  *      to the node being deleted.
 985  *
 986  */
 987 
 988 void
 989 fdisk_delete_logical_drive(ext_part_t *epp, int pno)
 990 {
 991         logical_drive_t *pre, *cur;
 992         int i;
 993 
 994         i = FD_NUMPART + 1;
 995         pre = cur = epp->ld_head;
 996         for (; i < pno; i++) {
 997                 pre = cur;
 998                 cur = cur->next;
 999         }
1000 
1001         if (cur == epp->ld_head) {
1002                 /* Deleting the first logical drive */
1003                 if (cur->next == NULL) {
1004                         /* Deleting the only logical drive left */
1005                         free(cur);
1006                         epp->ld_head = NULL;
1007                         epp->sorted_ld_head = NULL;
1008                         epp->logical_drive_count = 0;
1009                         epp->first_ebr_is_null = 1;
1010                 } else {
1011                         pre = epp->ld_head;
1012                         cur = pre->next;
1013                         cur->parts[0].relsect =
1014                             LE_32(LE_32(cur->parts[0].relsect) +
1015                             LE_32(pre->parts[1].relsect));
1016                         /* Corner case when partitions are out of order */
1017                         if ((pre->abs_secnum != epp->ext_beg_sec) &&
1018                             (cur->abs_secnum == epp->ext_beg_sec + 1)) {
1019                                 cur->logdrive_offset++;
1020                                 cur->abs_secnum = epp->ext_beg_sec;
1021                         } else {
1022                                 cur->abs_secnum = LE_32(cur->parts[0].relsect) +
1023                                     epp->ext_beg_sec;
1024                                 cur->logdrive_offset = 0;
1025                         }
1026                         fdisk_ext_remove_from_sorted_list(epp, pre);
1027                         epp->ld_head = cur;
1028                         epp->ld_head->modified = FDISK_MAJOR_WRITE;
1029                         epp->logical_drive_count--;
1030                         free(pre);
1031                 }
1032         } else {
1033                 pre->parts[1] = cur->parts[1];
1034                 pre->next = cur->next;
1035                 fdisk_ext_remove_from_sorted_list(epp, cur);
1036                 pre->modified = FDISK_MAJOR_WRITE;
1037                 free(cur);
1038                 epp->logical_drive_count--;
1039         }
1040 }
1041 
1042 static void
1043 fdisk_set_CHS_values(ext_part_t *epp, struct ipart *part)
1044 {
1045         uint32_t        lba, cy, hd, sc;
1046         uint32_t        sectors = epp->disk_geom.virt_sec;
1047         uint32_t        heads = epp->disk_geom.virt_heads;
1048 
1049         lba = LE_32(part->relsect) + epp->ext_beg_sec;
1050         if (lba >= heads * sectors * MAX_CYL) {
1051                 /*
1052                  * the lba address cannot be expressed in CHS value
1053                  * so store the maximum CHS field values in the CHS fields.
1054                  */
1055                 cy = MAX_CYL + 1;
1056                 hd = MAX_HEAD;
1057                 sc = MAX_SECT;
1058         } else {
1059                 cy = lba / sectors / heads;
1060                 hd = lba / sectors % heads;
1061                 sc = lba % sectors + 1;
1062         }
1063 
1064         part->begcyl = cy & 0xff;
1065         part->beghead = (uchar_t)hd;
1066         part->begsect = (uchar_t)(((cy >> 2) & 0xc0) | sc);
1067 
1068         /*
1069          * This code is identical to the code above
1070          * except that it works on ending CHS values
1071          */
1072         lba += LE_32(part->numsect - 1);
1073         if (lba >= heads * sectors * MAX_CYL) {
1074                 cy = MAX_CYL + 1;
1075                 hd = MAX_HEAD;
1076                 sc = MAX_SECT;
1077         } else {
1078                 cy = lba / sectors / heads;
1079                 hd = lba / sectors % heads;
1080                 sc = lba % sectors + 1;
1081         }
1082         part->endcyl = cy & 0xff;
1083         part->endhead = (uchar_t)hd;
1084         part->endsect = (uchar_t)(((cy >> 2) & 0xc0) | sc);
1085 }
1086 
1087 static int
1088 read_modify_write_ebr(ext_part_t *epp, unsigned char *ebr_buf,
1089     struct ipart *ebr_tab, uint32_t sec_offset)
1090 {
1091         off_t seek_offset;
1092         int sectsize = epp->disk_geom.sectsize;
1093 
1094         seek_offset = (off_t)sec_offset * sectsize;
1095 
1096         if (lseek(epp->dev_fd, seek_offset, SEEK_SET) < 0) {
1097                 return (EIO);
1098         }
1099         if (read(epp->dev_fd, ebr_buf, sectsize) < sectsize) {
1100                 return (EIO);
1101         }
1102 
1103         bzero(&ebr_buf[FDISK_PART_TABLE_START], 4 * sizeof (struct ipart));
1104         if (ebr_tab != NULL) {
1105                 bcopy(ebr_tab, &ebr_buf[FDISK_PART_TABLE_START],
1106                     2 * sizeof (struct ipart));
1107         }
1108         ebr_buf[510] = 0x55;
1109         ebr_buf[511] = 0xAA;
1110         if (lseek(epp->dev_fd, seek_offset, SEEK_SET) < 0) {
1111                 return (EIO);
1112         }
1113         if (write(epp->dev_fd, ebr_buf, sectsize) < sectsize) {
1114                 return (EIO);
1115         }
1116         return (0);
1117 }
1118 
1119 /*
1120  * XXX - ZFS mounts not detected. Needs to come in as a feature.
1121  * Currently only /etc/mnttab entries are being checked
1122  */
1123 int
1124 fdisk_mounted_logical_drives(ext_part_t *epp)
1125 {
1126         char *part_str, *canonp;
1127         char compare_pdev_str[PATH_MAX];
1128         char compare_sdev_str[PATH_MAX];
1129         FILE *fp;
1130         struct mnttab mt;
1131         int part;
1132         int look_for_mounted_slices = 0;
1133         uint32_t begsec, numsec;
1134 
1135         /*
1136          * Do not check for mounted logical drives for
1137          * devices other than /dev/rdsk/
1138          */
1139         if (strstr(epp->device_name, DEFAULT_PATH_PREFIX) == NULL) {
1140                 return (0);
1141         }
1142 
1143         if ((fp = fopen(MNTTAB, "r")) == NULL) {
1144                 return (ENOENT);
1145         }
1146 
1147         canonp = epp->device_name + strlen(DEFAULT_PATH_PREFIX);
1148         (void) snprintf(compare_pdev_str, PATH_MAX, "%s%s", "/dev/dsk/",
1149             canonp);
1150         part_str = strrchr(compare_pdev_str, 'p');
1151         *(part_str + 1) = '\0';
1152         (void) strcpy(compare_sdev_str, compare_pdev_str);
1153         part_str = strrchr(compare_sdev_str, 'p');
1154         *part_str = 's';
1155 
1156         if (fdisk_get_solaris_part(epp, &part, &begsec, &numsec) ==
1157             FDISK_SUCCESS) {
1158                 if (part > FD_NUMPART) {
1159                         /*
1160                          * Solaris partition is on a logical drive. Look for
1161                          * mounted slices.
1162                          */
1163                         look_for_mounted_slices = 1;
1164                 }
1165         }
1166 
1167         while (getmntent(fp, &mt) == 0) {
1168                 if (strstr(mt.mnt_special, compare_pdev_str) == NULL) {
1169                         if (strstr(mt.mnt_special, compare_sdev_str) == NULL) {
1170                                 continue;
1171                         } else {
1172                                 if (look_for_mounted_slices) {
1173                                         return (FDISK_EMOUNTED);
1174                                 }
1175                         }
1176                 }
1177 
1178                 /*
1179                  * Get the partition number that is mounted, which would be
1180                  * found just beyond the last 'p' in the device string.
1181                  * For example, in /dev/dsk/c0t0d0p12, partition number 12
1182                  * is just beyond the last 'p'.
1183                  */
1184                 part_str = strrchr(mt.mnt_special, 'p');
1185                 if (part_str != NULL) {
1186                         part_str++;
1187                         part = atoi(part_str);
1188                         /* Extended partition numbers start from 5 */
1189                         if (part >= 5) {
1190                                 return (FDISK_EMOUNTED);
1191                         }
1192                 }
1193         }
1194         return (0);
1195 }
1196 
1197 int
1198 fdisk_commit_ext_part(ext_part_t *epp)
1199 {
1200         logical_drive_t *temp;
1201         int wflag = 0;          /* write flag */
1202         int rval;
1203         int sectsize = epp->disk_geom.sectsize;
1204         unsigned char *ebr_buf;
1205         int ld_count;
1206         uint32_t abs_secnum;
1207         int check_mounts = 0;
1208 
1209         if ((ebr_buf = (unsigned char *)malloc(sectsize)) == NULL) {
1210                 return (ENOMEM);
1211         }
1212 
1213         if (epp->first_ebr_is_null) {
1214                 /*
1215                  * Indicator that the extended partition as a whole was
1216                  * modifies (either created or deleted. Must check for mounts
1217                  * and must commit
1218                  */
1219                 check_mounts = 1;
1220         }
1221 
1222         /*
1223          * Pass1 through the logical drives to make sure that commit of minor
1224          * written block dont get held up due to mounts.
1225          */
1226         for (temp = epp->ld_head; temp != NULL; temp = temp->next) {
1227                 if (temp == epp->ld_head) {
1228                         abs_secnum = epp->ext_beg_sec;
1229                 } else {
1230                         abs_secnum = temp->abs_secnum;
1231                 }
1232                 if (temp->modified == FDISK_MINOR_WRITE) {
1233                         rval = read_modify_write_ebr(epp, ebr_buf,
1234                             temp->parts, abs_secnum);
1235                         if (rval) {
1236                                 goto error;
1237                         }
1238                         temp->modified = 0;
1239                 } else if (temp->modified == FDISK_MAJOR_WRITE) {
1240                         check_mounts = 1;
1241                 }
1242         }
1243 
1244         if (!check_mounts) {
1245                 goto skip_check_mounts;
1246         }
1247 
1248         if ((rval = fdisk_mounted_logical_drives(epp)) != 0) {
1249                 /* One/more extended partitions are mounted */
1250                 if (ebr_buf) {
1251                         free(ebr_buf);
1252                 }
1253                 return (rval);
1254         }
1255 
1256 skip_check_mounts:
1257 
1258         if (epp->first_ebr_is_null) {
1259                 rval = read_modify_write_ebr(epp, ebr_buf, NULL,
1260                     epp->ext_beg_sec);
1261                 if (rval) {
1262                         goto error;
1263                 }
1264                 wflag = 1;
1265                 ld_count = 0;
1266         } else {
1267                 if (epp->logical_drive_count == 0) {
1268                         /*
1269                          * Can hit this case when there is just an extended
1270                          * partition with no logical drives, and the user
1271                          * committed without making any changes
1272                          * We dont have anything to commit. Return success
1273                          */
1274                         if (ebr_buf) {
1275                                 free(ebr_buf);
1276                         }
1277                         return (FDISK_SUCCESS);
1278                 }
1279 
1280                 /*
1281                  * Make sure that the first EBR is written with the first
1282                  * logical drive's data, which might not be the first in disk
1283                  * order.
1284                  */
1285                 for (temp = epp->ld_head, ld_count = 0; temp != NULL;
1286                     temp = temp->next, ld_count++) {
1287                         if (ld_count == 0) {
1288                                 abs_secnum = epp->ext_beg_sec;
1289                         } else {
1290                                 abs_secnum = temp->abs_secnum;
1291                         }
1292                         if (temp->modified) {
1293                                 rval = read_modify_write_ebr(epp, ebr_buf,
1294                                     temp->parts, abs_secnum);
1295                                 if (rval) {
1296                                         if (ld_count) {
1297                                                 /*
1298                                                  * There was atleast one
1299                                                  * write to the disk before
1300                                                  * this failure. Make sure that
1301                                                  * the kernel is notified.
1302                                                  * Issue the ioctl.
1303                                                  */
1304                                                 break;
1305                                         }
1306                                         goto error;
1307                                 }
1308                                 if ((!wflag) && (temp->modified ==
1309                                     FDISK_MAJOR_WRITE)) {
1310                                         wflag = 1;
1311                                 }
1312                         }
1313                 }
1314 
1315                 if (wflag == 0) {
1316                         /* No changes made */
1317                         rval = FDISK_SUCCESS;
1318                         goto error;
1319                 }
1320         }
1321 
1322         /* Issue ioctl to the driver to update extended partition info */
1323         rval = ioctl(epp->dev_fd, DKIOCSETEXTPART);
1324 
1325         /*
1326          * Certain devices ex:lofi do not support DKIOCSETEXTPART.
1327          * Extended partitions are still created on these devices.
1328          */
1329         if (errno == ENOTTY)
1330                 rval = FDISK_SUCCESS;
1331 
1332 error:
1333         if (ebr_buf) {
1334                 free(ebr_buf);
1335         }
1336         return (rval);
1337 }
1338 
1339 int
1340 fdisk_init_ext_part(ext_part_t *epp, uint32_t rsect, uint32_t nsect)
1341 {
1342         epp->first_ebr_is_null = 1;
1343         epp->corrupt_logical_drives = 0;
1344         epp->logical_drive_count = 0;
1345         epp->ext_beg_sec = rsect;
1346         epp->ext_end_sec = rsect + nsect - 1;
1347         epp->ext_beg_cyl = FDISK_SECT_TO_CYL(epp, epp->ext_beg_sec);
1348         epp->ext_end_cyl = FDISK_SECT_TO_CYL(epp, epp->ext_end_sec);
1349         epp->invalid_bb_sig[0] = 0;
1350         return (0);
1351 }
1352 
1353 int
1354 fdisk_delete_ext_part(ext_part_t *epp)
1355 {
1356         epp->first_ebr_is_null = 1;
1357         /* Clear the logical drive information */
1358         fdisk_free_ld_nodes(epp);
1359         epp->logical_drive_count = 0;
1360         epp->corrupt_logical_drives = 0;
1361         epp->invalid_bb_sig[0] = 0;
1362         return (0);
1363 }
1364 
1365 int
1366 fdisk_get_disk_geom(ext_part_t *epp, int type, int what)
1367 {
1368         switch (type) {
1369                 case PHYSGEOM:
1370                         switch (what) {
1371                                 case NCYL:
1372                                         return ((int)epp->disk_geom.phys_cyl);
1373                                 case NHEADS:
1374                                         return ((int)epp->disk_geom.phys_heads);
1375                                 case NSECTPT:
1376                                         return ((int)epp->disk_geom.phys_sec);
1377                                 case SSIZE:
1378                                         return ((int)epp->disk_geom.sectsize);
1379                                 case ACYL:
1380                                         return ((int)epp->disk_geom.alt_cyl);
1381                                 default:
1382                                         return (EINVAL);
1383                         }
1384                 case VIRTGEOM:
1385                         switch (what) {
1386                                 case NCYL:
1387                                         return ((int)epp->disk_geom.virt_cyl);
1388                                 case NHEADS:
1389                                         return ((int)epp->disk_geom.virt_heads);
1390                                 case NSECTPT:
1391                                         return ((int)epp->disk_geom.virt_sec);
1392                                 case SSIZE:
1393                                         return ((int)epp->disk_geom.sectsize);
1394                                 case ACYL:
1395                                         return ((int)epp->disk_geom.alt_cyl);
1396                                 default:
1397                                         return (EINVAL);
1398                         }
1399                 default:
1400                         return (EINVAL);
1401         }
1402 }
1403 
1404 int
1405 fdisk_invalid_bb_sig(ext_part_t *epp, uchar_t **bbsig_arr)
1406 {
1407         *bbsig_arr = &(epp->invalid_bb_sig[0]);
1408         return (epp->invalid_bb_sig[0]);
1409 }