xref: /illumos-gate/usr/src/cmd/format/menu_fdisk.c (revision c26dc428cee0f025b14a5ad03a7722f2a52f8383)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * This file contains functions that implement the fdisk menu commands.
28  */
29 #include "global.h"
30 #include <errno.h>
31 #include <sys/time.h>
32 #include <sys/resource.h>
33 #include <sys/wait.h>
34 #include <signal.h>
35 #include <string.h>
36 #include <fcntl.h>
37 #include <stdlib.h>
38 #include <sys/dktp/fdisk.h>
39 #include <sys/stat.h>
40 #include <sys/dklabel.h>
41 #ifdef i386
42 #include <libfdisk.h>
43 #endif
44 
45 #include "main.h"
46 #include "analyze.h"
47 #include "menu.h"
48 #include "menu_command.h"
49 #include "menu_defect.h"
50 #include "menu_partition.h"
51 #include "menu_fdisk.h"
52 #include "param.h"
53 #include "misc.h"
54 #include "label.h"
55 #include "startup.h"
56 #include "partition.h"
57 #include "prompts.h"
58 #include "checkdev.h"
59 #include "io.h"
60 #include "ctlr_scsi.h"
61 #include "auto_sense.h"
62 
63 extern	struct menu_item menu_fdisk[];
64 
65 /*
66  * Byte swapping macros for accessing struct ipart
67  *	to resolve little endian on Sparc.
68  */
69 #if defined(sparc)
70 #define	les(val)	((((val)&0xFF)<<8)|(((val)>>8)&0xFF))
71 #define	lel(val)	(((unsigned)(les((val)&0x0000FFFF))<<16) | \
72 			(les((unsigned)((val)&0xffff0000)>>16)))
73 
74 #elif	defined(i386)
75 
76 #define	les(val)	(val)
77 #define	lel(val)	(val)
78 
79 #else	/* defined(sparc) */
80 
81 #error	No Platform defined
82 
83 #endif	/* defined(sparc) */
84 
85 
86 /* Function prototypes */
87 #ifdef	__STDC__
88 
89 #if	defined(sparc)
90 
91 static int getbyte(uchar_t **);
92 static int getlong(uchar_t **);
93 
94 #endif	/* defined(sparc) */
95 
96 static int get_solaris_part(int fd, struct ipart *ipart);
97 
98 #else	/* __STDC__ */
99 
100 #if	defined(sparc)
101 
102 static int getbyte();
103 static int getlong();
104 
105 #endif	/* defined(sparc) */
106 
107 static int get_solaris_part();
108 
109 #endif	/* __STDC__ */
110 
111 
112 #ifdef i386
113 int extpart_init(ext_part_t **epp);
114 #endif
115 /*
116  * Handling the alignment problem of struct ipart.
117  */
118 static void
119 fill_ipart(char *bootptr, struct ipart *partp)
120 {
121 #if defined(sparc)
122 	/*
123 	 * Sparc platform:
124 	 *
125 	 * Packing short/word for struct ipart to resolve
126 	 *	little endian on Sparc since it is not
127 	 *	properly aligned on Sparc.
128 	 */
129 	partp->bootid = getbyte((uchar_t **)&bootptr);
130 	partp->beghead = getbyte((uchar_t **)&bootptr);
131 	partp->begsect = getbyte((uchar_t **)&bootptr);
132 	partp->begcyl = getbyte((uchar_t **)&bootptr);
133 	partp->systid = getbyte((uchar_t **)&bootptr);
134 	partp->endhead = getbyte((uchar_t **)&bootptr);
135 	partp->endsect = getbyte((uchar_t **)&bootptr);
136 	partp->endcyl = getbyte((uchar_t **)&bootptr);
137 	partp->relsect = getlong((uchar_t **)&bootptr);
138 	partp->numsect = getlong((uchar_t **)&bootptr);
139 #elif defined(i386)
140 	/*
141 	 * i386 platform:
142 	 *
143 	 * The fdisk table does not begin on a 4-byte boundary within
144 	 * the master boot record; so, we need to recopy its contents
145 	 * to another data structure to avoid an alignment exception.
146 	 */
147 	(void) bcopy(bootptr, partp, sizeof (struct ipart));
148 #else
149 #error  No Platform defined
150 #endif /* defined(sparc) */
151 }
152 
153 /*
154  * Get a correct byte/short/word routines for Sparc platform.
155  */
156 #if defined(sparc)
157 static int
158 getbyte(uchar_t **bp)
159 {
160 	int	b;
161 
162 	b = **bp;
163 	*bp = *bp + 1;
164 	return (b);
165 }
166 
167 #ifdef DEADCODE
168 static int
169 getshort(uchar_t **bp)
170 {
171 	int	b;
172 
173 	b = ((**bp) << 8) | *(*bp + 1);
174 	*bp += 2;
175 	return (b);
176 }
177 #endif /* DEADCODE */
178 
179 static int
180 getlong(uchar_t **bp)
181 {
182 	int	b, bh, bl;
183 
184 	bh = ((**bp) << 8) | *(*bp + 1);
185 	*bp += 2;
186 	bl = ((**bp) << 8) | *(*bp + 1);
187 	*bp += 2;
188 
189 	b = (bh << 16) | bl;
190 	return (b);
191 }
192 #endif /* defined(sparc) */
193 
194 /*
195  * Convert cn[tn]dn to cn[tn]dns2 path
196  */
197 static void
198 get_sname(char *name)
199 {
200 	char		buf[MAXPATHLEN];
201 	char		*devp = "/dev/dsk";
202 	char		*rdevp = "/dev/rdsk";
203 	char		np[MAXNAMELEN];
204 	char		*npt;
205 
206 	/*
207 	 * If it is a full path /dev/[r]dsk/cn[tn]dn, use this path
208 	 */
209 	(void) strcpy(np, cur_disk->disk_name);
210 	if (strncmp(rdevp, cur_disk->disk_name, strlen(rdevp)) == 0 ||
211 	    strncmp(devp, cur_disk->disk_name, strlen(devp)) == 0) {
212 		/*
213 		 * Skip if the path is already included with sN
214 		 */
215 		if (strchr(np, 's') == strrchr(np, 's')) {
216 			npt = strrchr(np, 'p');
217 			/* If pN is found, do not include it */
218 			if (npt != NULL) {
219 				*npt = '\0';
220 			}
221 			(void) snprintf(buf, sizeof (buf), "%ss2", np);
222 		} else {
223 			(void) snprintf(buf, sizeof (buf), "%s", np);
224 		}
225 	} else {
226 		(void) snprintf(buf, sizeof (buf), "/dev/rdsk/%ss2", np);
227 	}
228 	(void) strcpy(name, buf);
229 }
230 
231 /*
232  * Convert cn[tn]dnsn to cn[tn]dnp0 path
233  */
234 static void
235 get_pname(char *name)
236 {
237 	char		buf[MAXPATHLEN];
238 	char		*devp = "/dev/dsk";
239 	char		*rdevp = "/dev/rdsk";
240 	char		np[MAXNAMELEN];
241 	char		*npt;
242 
243 	/*
244 	 * If it is a full path /dev/[r]dsk/cn[tn]dnsn, use this path
245 	 */
246 	if (cur_disk == NULL) {
247 		(void) strcpy(np, x86_devname);
248 	} else {
249 		(void) strcpy(np, cur_disk->disk_name);
250 	}
251 
252 	if (strncmp(rdevp, np, strlen(rdevp)) == 0 ||
253 	    strncmp(devp, np, strlen(devp)) == 0) {
254 		/*
255 		 * Skip if the path is already included with pN
256 		 */
257 		if (strchr(np, 'p') == NULL) {
258 			npt = strrchr(np, 's');
259 			/* If sN is found, do not include it */
260 			if (isdigit(*++npt)) {
261 				*--npt = '\0';
262 			}
263 			(void) snprintf(buf, sizeof (buf), "%sp0", np);
264 		} else {
265 			(void) snprintf(buf, sizeof (buf), "%s", np);
266 		}
267 	} else {
268 		(void) snprintf(buf, sizeof (buf), "/dev/rdsk/%sp0", np);
269 	}
270 	(void) strcpy(name, buf);
271 }
272 
273 /*
274  * Open file descriptor for current disk (cur_file)
275  *	with "p0" path or cur_disk->disk_path
276  */
277 void
278 open_cur_file(int mode)
279 {
280 	char	*dkpath;
281 	char	pbuf[MAXPATHLEN];
282 
283 	switch (mode) {
284 		case FD_USE_P0_PATH:
285 			(void) get_pname(&pbuf[0]);
286 			dkpath = pbuf;
287 			break;
288 		case FD_USE_CUR_DISK_PATH:
289 			if (cur_disk->fdisk_part.systid == SUNIXOS ||
290 			    cur_disk->fdisk_part.systid == SUNIXOS2) {
291 				(void) get_sname(&pbuf[0]);
292 				dkpath = pbuf;
293 			} else {
294 				dkpath = cur_disk->disk_path;
295 			}
296 			break;
297 		default:
298 			err_print("Error: Invalid mode option for opening "
299 			    "cur_file\n");
300 			fullabort();
301 	}
302 
303 	/* Close previous cur_file */
304 	(void) close(cur_file);
305 	/* Open cur_file with the required path dkpath */
306 	if ((cur_file = open_disk(dkpath, O_RDWR | O_NDELAY)) < 0) {
307 		err_print(
308 		    "Error: can't open selected disk '%s'.\n", dkpath);
309 		fullabort();
310 	}
311 }
312 
313 
314 /*
315  * This routine implements the 'fdisk' command.  It simply runs
316  * the fdisk command on the current disk.
317  * Use of this is restricted to interactive mode only.
318  */
319 int
320 c_fdisk()
321 {
322 
323 	char		buf[MAXPATHLEN];
324 	char		pbuf[MAXPATHLEN];
325 	struct stat	statbuf;
326 
327 	/*
328 	 * We must be in interactive mode to use the fdisk command
329 	 */
330 	if (option_f != (char *)NULL || isatty(0) != 1 || isatty(1) != 1) {
331 		err_print("Fdisk command is for interactive use only!\n");
332 		return (-1);
333 	}
334 
335 	/*
336 	 * There must be a current disk type and a current disk
337 	 */
338 	if (cur_dtype == NULL) {
339 		err_print("Current Disk Type is not set.\n");
340 		return (-1);
341 	}
342 
343 	/*
344 	 * Before running the fdisk command, get file status of
345 	 *	/dev/rdsk/cn[tn]dnp0 path to see if this disk
346 	 *	supports fixed disk partition table.
347 	 */
348 	(void) get_pname(&pbuf[0]);
349 	if (stat(pbuf, (struct stat *)&statbuf) == -1 ||
350 	    !S_ISCHR(statbuf.st_mode)) {
351 		err_print(
352 		"Disk does not support fixed disk partition table\n");
353 		return (0);
354 	}
355 
356 	/*
357 	 * Run the fdisk program.
358 	 */
359 	(void) snprintf(buf, sizeof (buf), "fdisk %s\n", pbuf);
360 	(void) system(buf);
361 
362 	/*
363 	 * Open cur_file with "p0" path for accessing the fdisk table
364 	 */
365 	(void) open_cur_file(FD_USE_P0_PATH);
366 
367 	/*
368 	 * Get solaris partition information in the fdisk partition table
369 	 */
370 	if (get_solaris_part(cur_file, &cur_disk->fdisk_part) == -1) {
371 		err_print("No fdisk solaris partition found\n");
372 		cur_disk->fdisk_part.numsect = 0;  /* No Solaris */
373 	}
374 
375 	/*
376 	 * Restore cur_file with cur_disk->disk_path
377 	 */
378 	(void) open_cur_file(FD_USE_CUR_DISK_PATH);
379 
380 	return (0);
381 }
382 
383 /*
384  * Read MBR on the disk
385  * if the Solaris partition has changed,
386  *	reread the vtoc
387  */
388 #ifdef DEADCODE
389 static void
390 update_cur_parts()
391 {
392 
393 	int i;
394 	register struct partition_info *parts;
395 
396 	for (i = 0; i < NDKMAP; i++) {
397 #if defined(_SUNOS_VTOC_16)
398 		if (cur_parts->vtoc.v_part[i].p_tag &&
399 		    cur_parts->vtoc.v_part[i].p_tag != V_ALTSCTR) {
400 			cur_parts->vtoc.v_part[i].p_start = 0;
401 			cur_parts->vtoc.v_part[i].p_size = 0;
402 
403 #endif
404 			cur_parts->pinfo_map[i].dkl_nblk = 0;
405 			cur_parts->pinfo_map[i].dkl_cylno = 0;
406 			cur_parts->vtoc.v_part[i].p_tag =
407 			    default_vtoc_map[i].p_tag;
408 			cur_parts->vtoc.v_part[i].p_flag =
409 			    default_vtoc_map[i].p_flag;
410 #if defined(_SUNOS_VTOC_16)
411 		}
412 #endif
413 	}
414 	cur_parts->pinfo_map[C_PARTITION].dkl_nblk = ncyl * spc();
415 
416 #if defined(_SUNOS_VTOC_16)
417 	/*
418 	 * Adjust for the boot partitions
419 	 */
420 	cur_parts->pinfo_map[I_PARTITION].dkl_nblk = spc();
421 	cur_parts->pinfo_map[I_PARTITION].dkl_cylno = 0;
422 	cur_parts->vtoc.v_part[C_PARTITION].p_start =
423 	    cur_parts->pinfo_map[C_PARTITION].dkl_cylno * nhead * nsect;
424 	cur_parts->vtoc.v_part[C_PARTITION].p_size =
425 	    cur_parts->pinfo_map[C_PARTITION].dkl_nblk;
426 
427 	cur_parts->vtoc.v_part[I_PARTITION].p_start =
428 	    cur_parts->pinfo_map[I_PARTITION].dkl_cylno;
429 	cur_parts->vtoc.v_part[I_PARTITION].p_size =
430 	    cur_parts->pinfo_map[I_PARTITION].dkl_nblk;
431 
432 #endif	/* defined(_SUNOS_VTOC_16) */
433 	parts = cur_dtype->dtype_plist;
434 	cur_dtype->dtype_ncyl = ncyl;
435 	cur_dtype->dtype_plist = cur_parts;
436 	parts->pinfo_name = cur_parts->pinfo_name;
437 	cur_disk->disk_parts = cur_parts;
438 	cur_ctype->ctype_dlist = cur_dtype;
439 
440 }
441 #endif /* DEADCODE */
442 
443 static int
444 get_solaris_part(int fd, struct ipart *ipart)
445 {
446 	int		i;
447 	struct ipart	ip;
448 	int		status;
449 	char		*bootptr;
450 	struct dk_label	update_label;
451 	ushort_t	found = 0;
452 #ifdef i386
453 	uint32_t	relsec, numsec;
454 	int		pno, rval, ext_part_found = 0;
455 	ext_part_t	*epp;
456 #endif
457 
458 	(void) lseek(fd, 0, 0);
459 	status = read(fd, (caddr_t)&boot_sec, NBPSCTR);
460 
461 	if (status != NBPSCTR) {
462 		err_print("Bad read of fdisk partition. Status = %x\n", status);
463 		err_print("Cannot read fdisk partition information.\n");
464 		return (-1);
465 	}
466 
467 	for (i = 0; i < FD_NUMPART; i++) {
468 		int	ipc;
469 
470 		ipc = i * sizeof (struct ipart);
471 
472 		/* Handling the alignment problem of struct ipart */
473 		bootptr = &boot_sec.parts[ipc];
474 		(void) fill_ipart(bootptr, &ip);
475 
476 #ifdef i386
477 		if (fdisk_is_dos_extended(ip.systid) && (ext_part_found == 0)) {
478 			/* We support only one extended partition per disk */
479 			ext_part_found = 1;
480 			(void) extpart_init(&epp);
481 			rval = fdisk_get_solaris_part(epp, &pno, &relsec,
482 			    &numsec);
483 			if (rval == FDISK_SUCCESS) {
484 				/*
485 				 * Found a solaris partition inside the
486 				 * extended partition. Update the statistics.
487 				 */
488 				if (nhead != 0 && nsect != 0) {
489 					pcyl = numsec / (nhead * nsect);
490 					xstart = relsec / (nhead * nsect);
491 					ncyl = pcyl - acyl;
492 				}
493 				solaris_offset = relsec;
494 				found = 2;
495 				ip.bootid = 0;
496 				ip.beghead = ip.begsect = ip.begcyl = 0xff;
497 				ip.endhead = ip.endsect = ip.endcyl = 0xff;
498 				ip.systid = SUNIXOS2;
499 				ip.relsect = relsec;
500 				ip.numsect = numsec;
501 				ipart->bootid = ip.bootid;
502 				status = bcmp(&ip, ipart,
503 				    sizeof (struct ipart));
504 				bcopy(&ip, ipart, sizeof (struct ipart));
505 			}
506 			libfdisk_fini(&epp);
507 			continue;
508 		}
509 #endif
510 
511 		/*
512 		 * we are interested in Solaris and EFI partition types
513 		 */
514 		if (ip.systid == SUNIXOS ||
515 		    ip.systid == SUNIXOS2 ||
516 		    ip.systid == EFI_PMBR) {
517 			/*
518 			 * if the disk has an EFI label, nhead and nsect may
519 			 * be zero.  This test protects us from FPE's, and
520 			 * format still seems to work fine
521 			 */
522 			if (nhead != 0 && nsect != 0) {
523 				pcyl = lel(ip.numsect) / (nhead * nsect);
524 				xstart = lel(ip.relsect) / (nhead * nsect);
525 				ncyl = pcyl - acyl;
526 			}
527 #ifdef DEBUG
528 			else {
529 				err_print("Critical geometry values are zero:\n"
530 				    "\tnhead = %d; nsect = %d\n", nhead, nsect);
531 			}
532 #endif /* DEBUG */
533 
534 			solaris_offset = (uint_t)lel(ip.relsect);
535 			found = 1;
536 			break;
537 		}
538 	}
539 
540 	if (!found) {
541 		err_print("Solaris fdisk partition not found\n");
542 		return (-1);
543 	} else if (found == 1) {
544 		/*
545 		 * Found a primary solaris partition.
546 		 * compare the previous and current Solaris partition
547 		 * but don't use bootid in determination of Solaris partition
548 		 * changes
549 		 */
550 		ipart->bootid = ip.bootid;
551 		status = bcmp(&ip, ipart, sizeof (struct ipart));
552 
553 		bcopy(&ip, ipart, sizeof (struct ipart));
554 	}
555 
556 	/* if the disk partitioning has changed - get the VTOC */
557 	if (status) {
558 		struct extvtoc exvtoc;
559 		struct vtoc vtoc;
560 
561 		status = ioctl(fd, DKIOCGEXTVTOC, &exvtoc);
562 		if (status == -1) {
563 			i = errno;
564 			/* Try the old ioctl DKIOCGVTOC */
565 			status = ioctl(fd, DKIOCGVTOC, &vtoc);
566 			if (status == -1) {
567 				err_print("Bad ioctl DKIOCGEXTVTOC.\n");
568 				err_print("errno=%d %s\n", i, strerror(i));
569 				err_print("Cannot read vtoc information.\n");
570 				return (-1);
571 			}
572 		}
573 
574 		status = read_label(fd, &update_label);
575 		if (status == -1) {
576 			err_print("Cannot read label information.\n");
577 			return (-1);
578 		}
579 
580 		/* copy vtoc information */
581 		cur_parts->vtoc = update_label.dkl_vtoc;
582 
583 #if defined(_SUNOS_VTOC_16)
584 		/*
585 		 * this is to update the slice table on x86
586 		 * we don't care about VTOC8 here
587 		 */
588 		for (i = 0; i < NDKMAP; i ++) {
589 			cur_parts->pinfo_map[i].dkl_cylno =
590 			    update_label.dkl_vtoc.v_part[i].p_start /
591 			    ((int)(update_label.dkl_nhead *
592 			    update_label.dkl_nsect));
593 			cur_parts->pinfo_map[i].dkl_nblk =
594 			    update_label.dkl_vtoc.v_part[i].p_size;
595 		}
596 #endif /* defined(_SUNOS_VTOC_16) */
597 
598 		cur_dtype->dtype_ncyl = update_label.dkl_ncyl;
599 		cur_dtype->dtype_pcyl = update_label.dkl_pcyl;
600 		cur_dtype->dtype_acyl = update_label.dkl_acyl;
601 		cur_dtype->dtype_nhead = update_label.dkl_nhead;
602 		cur_dtype->dtype_nsect = update_label.dkl_nsect;
603 		ncyl = cur_dtype->dtype_ncyl;
604 		acyl = cur_dtype->dtype_acyl;
605 		pcyl = cur_dtype->dtype_pcyl;
606 		nsect = cur_dtype->dtype_nsect;
607 		nhead = cur_dtype->dtype_nhead;
608 	}
609 	return (0);
610 }
611 
612 
613 int
614 copy_solaris_part(struct ipart *ipart)
615 {
616 
617 	int		status, i, fd;
618 	struct mboot	mboot;
619 	struct ipart	ip;
620 	char		buf[MAXPATHLEN];
621 	char		*bootptr;
622 	struct stat	statbuf;
623 #ifdef i386
624 	uint32_t	relsec, numsec;
625 	int		pno, rval, ext_part_found = 0;
626 	ext_part_t	*epp;
627 #endif
628 
629 	(void) get_pname(&buf[0]);
630 	if (stat(buf, &statbuf) == -1 ||
631 	    !S_ISCHR(statbuf.st_mode) ||
632 	    ((cur_label == L_TYPE_EFI) &&
633 	    (cur_disk->disk_flags & DSK_LABEL_DIRTY))) {
634 		/*
635 		 * Make sure to reset solaris_offset to zero if it is
636 		 *	previously set by a selected disk that
637 		 *	supports the fdisk table.
638 		 */
639 		solaris_offset = 0;
640 		/*
641 		 * Return if this disk does not support fdisk table or
642 		 * if it uses an EFI label but has not yet been labelled.
643 		 * If the EFI label has not been written then the open
644 		 * on the partition will fail.
645 		 */
646 		return (0);
647 	}
648 
649 	if ((fd = open(buf, O_RDONLY)) < 0) {
650 		err_print("Error: can't open disk '%s'.\n", buf);
651 		return (-1);
652 	}
653 
654 	status = read(fd, (caddr_t)&mboot, sizeof (struct mboot));
655 
656 	if (status != sizeof (struct mboot)) {
657 		err_print("Bad read of fdisk partition.\n");
658 		(void) close(fd);
659 		return (-1);
660 	}
661 
662 	for (i = 0; i < FD_NUMPART; i++) {
663 		int	ipc;
664 
665 		ipc = i * sizeof (struct ipart);
666 
667 		/* Handling the alignment problem of struct ipart */
668 		bootptr = &mboot.parts[ipc];
669 		(void) fill_ipart(bootptr, &ip);
670 
671 #ifdef i386
672 		if (fdisk_is_dos_extended(ip.systid) && (ext_part_found == 0)) {
673 			/* We support only one extended partition per disk */
674 			ext_part_found = 1;
675 			(void) extpart_init(&epp);
676 			rval = fdisk_get_solaris_part(epp, &pno, &relsec,
677 			    &numsec);
678 			if (rval == FDISK_SUCCESS) {
679 				/*
680 				 * Found a solaris partition inside the
681 				 * extended partition. Update the statistics.
682 				 */
683 				if (nhead != 0 && nsect != 0) {
684 					pcyl = numsec / (nhead * nsect);
685 					ncyl = pcyl - acyl;
686 				}
687 				solaris_offset = relsec;
688 				ip.bootid = 0;
689 				ip.beghead = ip.begsect = ip.begcyl = 0xff;
690 				ip.endhead = ip.endsect = ip.endcyl = 0xff;
691 				ip.systid = SUNIXOS2;
692 				ip.relsect = relsec;
693 				ip.numsect = numsec;
694 				bcopy(&ip, ipart, sizeof (struct ipart));
695 			}
696 			libfdisk_fini(&epp);
697 			continue;
698 		}
699 #endif
700 
701 		if (ip.systid == SUNIXOS ||
702 		    ip.systid == SUNIXOS2 ||
703 		    ip.systid == EFI_PMBR) {
704 			solaris_offset = lel(ip.relsect);
705 			bcopy(&ip, ipart, sizeof (struct ipart));
706 
707 			/*
708 			 * if the disk has an EFI label, we typically won't
709 			 * have values for nhead and nsect.  format seems to
710 			 * work without them, and we need to protect ourselves
711 			 * from FPE's
712 			 */
713 			if (nhead != 0 && nsect != 0) {
714 				pcyl = lel(ip.numsect) / (nhead * nsect);
715 				ncyl = pcyl - acyl;
716 			}
717 #ifdef DEBUG
718 			else {
719 				err_print("Critical geometry values are zero:\n"
720 				    "\tnhead = %d; nsect = %d\n", nhead, nsect);
721 			}
722 #endif /* DEBUG */
723 
724 			break;
725 		}
726 	}
727 
728 	(void) close(fd);
729 	return (0);
730 
731 }
732 
733 #if defined(_FIRMWARE_NEEDS_FDISK)
734 int
735 auto_solaris_part(struct dk_label *label)
736 {
737 
738 	int		status, i, fd;
739 	struct mboot	mboot;
740 	struct ipart	ip;
741 	char		*bootptr;
742 	char		pbuf[MAXPATHLEN];
743 #ifdef i386
744 	uint32_t	relsec, numsec;
745 	int		pno, rval, ext_part_found = 0;
746 	ext_part_t	*epp;
747 #endif
748 
749 	(void) get_pname(&pbuf[0]);
750 	if ((fd = open_disk(pbuf, O_RDONLY)) < 0) {
751 		err_print("Error: can't open selected disk '%s'.\n", pbuf);
752 		return (-1);
753 	}
754 
755 	status = read(fd, (caddr_t)&mboot, sizeof (struct mboot));
756 
757 	if (status != sizeof (struct mboot)) {
758 		err_print("Bad read of fdisk partition.\n");
759 		return (-1);
760 	}
761 
762 	for (i = 0; i < FD_NUMPART; i++) {
763 		int	ipc;
764 
765 		ipc = i * sizeof (struct ipart);
766 
767 		/* Handling the alignment problem of struct ipart */
768 		bootptr = &mboot.parts[ipc];
769 		(void) fill_ipart(bootptr, &ip);
770 
771 #ifdef i386
772 		if (fdisk_is_dos_extended(ip.systid) && (ext_part_found == 0)) {
773 			/* We support only one extended partition per disk */
774 			ext_part_found = 1;
775 			(void) extpart_init(&epp);
776 			rval = fdisk_get_solaris_part(epp, &pno, &relsec,
777 			    &numsec);
778 			if (rval == FDISK_SUCCESS) {
779 				/*
780 				 * Found a solaris partition inside the
781 				 * extended partition. Update the statistics.
782 				 */
783 				if ((label->dkl_nhead != 0) &&
784 				    (label->dkl_nsect != 0)) {
785 					label->dkl_pcyl =
786 					    numsec / (label->dkl_nhead *
787 					    label->dkl_nsect);
788 					label->dkl_ncyl = label->dkl_pcyl -
789 					    label->dkl_acyl;
790 				}
791 				solaris_offset = relsec;
792 			}
793 			libfdisk_fini(&epp);
794 			continue;
795 		}
796 #endif
797 
798 		/*
799 		 * if the disk has an EFI label, the nhead and nsect fields
800 		 * the label may be zero.  This protects us from FPE's, and
801 		 * format still seems to work happily
802 		 */
803 		if (ip.systid == SUNIXOS ||
804 		    ip.systid == SUNIXOS2 ||
805 		    ip.systid == EFI_PMBR) {
806 			if ((label->dkl_nhead != 0) &&
807 			    (label->dkl_nsect != 0)) {
808 				label->dkl_pcyl = lel(ip.numsect) /
809 				    (label->dkl_nhead * label->dkl_nsect);
810 				label->dkl_ncyl = label->dkl_pcyl -
811 				    label->dkl_acyl;
812 			}
813 #ifdef DEBUG
814 			else {
815 				err_print("Critical label fields aren't "
816 				    "non-zero:\n"
817 				    "\tlabel->dkl_nhead = %d; "
818 				    "label->dkl_nsect = "
819 				    "%d\n", label->dkl_nhead,
820 				    label->dkl_nsect);
821 			}
822 #endif /* DEBUG */
823 
824 		solaris_offset = lel(ip.relsect);
825 		break;
826 		}
827 	}
828 
829 	(void) close(fd);
830 
831 	return (0);
832 }
833 #endif	/* defined(_FIRMWARE_NEEDS_FDISK) */
834 
835 
836 int
837 good_fdisk()
838 {
839 	char		buf[MAXPATHLEN];
840 	struct stat	statbuf;
841 
842 	(void) get_pname(&buf[0]);
843 	if (stat(buf, &statbuf) == -1 ||
844 	    !S_ISCHR(statbuf.st_mode) ||
845 	    cur_label == L_TYPE_EFI) {
846 		/*
847 		 * Return if this disk does not support fdisk table or
848 		 * if the disk is labeled with EFI.
849 		 */
850 		return (1);
851 	}
852 
853 	if (lel(cur_disk->fdisk_part.numsect) > 0) {
854 		return (1);
855 	} else {
856 		err_print("WARNING - ");
857 		err_print("This disk may be in use by an application "
858 		    "that has\n\t  modified the fdisk table. Ensure "
859 		    "that this disk is\n\t  not currently in use "
860 		    "before proceeding to use fdisk.\n");
861 		return (0);
862 	}
863 }
864 
865 
866 #ifdef i386
867 int
868 extpart_init(ext_part_t **epp)
869 {
870 	int		rval, lf_op_flag = 0;
871 	char		p0_path[MAXPATHLEN];
872 
873 	get_pname(&p0_path[0]);
874 	lf_op_flag |= FDISK_READ_DISK;
875 	if ((rval = libfdisk_init(epp, p0_path, NULL, lf_op_flag)) !=
876 	    FDISK_SUCCESS) {
877 		switch (rval) {
878 			/*
879 			 * FDISK_EBADLOGDRIVE and FDISK_ENOLOGDRIVE can
880 			 * be considered as soft errors and hence
881 			 * we do not exit
882 			 */
883 			case FDISK_EBADLOGDRIVE:
884 				break;
885 			case FDISK_ENOLOGDRIVE:
886 				break;
887 			case FDISK_ENOVGEOM:
888 				err_print("Could not get virtual geometry for"
889 				    " this device\n");
890 				fullabort();
891 				break;
892 			case FDISK_ENOPGEOM:
893 				err_print("Could not get physical geometry for"
894 				    " this device\n");
895 				fullabort();
896 				break;
897 			case FDISK_ENOLGEOM:
898 				err_print("Could not get label geometry for "
899 				    " this device\n");
900 				fullabort();
901 				break;
902 			default:
903 				err_print("Failed to initialise libfdisk.\n");
904 				fullabort();
905 				break;
906 		}
907 	}
908 	return (0);
909 }
910 #endif
911