xref: /titanic_52/usr/src/cmd/format/menu_fdisk.c (revision 4a5d661a82b942b6538acd26209d959ce98b593a)
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) 1993, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright 2016 Toomas Soome <tsoome@me.com>
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 #ifdef i386
112 int extpart_init(ext_part_t **epp);
113 #endif
114 /*
115  * Handling the alignment problem of struct ipart.
116  */
117 static void
118 fill_ipart(char *bootptr, struct ipart *partp)
119 {
120 #if defined(sparc)
121 	/*
122 	 * Sparc platform:
123 	 *
124 	 * Packing short/word for struct ipart to resolve
125 	 *	little endian on Sparc since it is not
126 	 *	properly aligned on Sparc.
127 	 */
128 	partp->bootid = getbyte((uchar_t **)&bootptr);
129 	partp->beghead = getbyte((uchar_t **)&bootptr);
130 	partp->begsect = getbyte((uchar_t **)&bootptr);
131 	partp->begcyl = getbyte((uchar_t **)&bootptr);
132 	partp->systid = getbyte((uchar_t **)&bootptr);
133 	partp->endhead = getbyte((uchar_t **)&bootptr);
134 	partp->endsect = getbyte((uchar_t **)&bootptr);
135 	partp->endcyl = getbyte((uchar_t **)&bootptr);
136 	partp->relsect = getlong((uchar_t **)&bootptr);
137 	partp->numsect = getlong((uchar_t **)&bootptr);
138 #elif defined(i386)
139 	/*
140 	 * i386 platform:
141 	 *
142 	 * The fdisk table does not begin on a 4-byte boundary within
143 	 * the master boot record; so, we need to recopy its contents
144 	 * to another data structure to avoid an alignment exception.
145 	 */
146 	(void) bcopy(bootptr, partp, sizeof (struct ipart));
147 #else
148 #error  No Platform defined
149 #endif /* defined(sparc) */
150 }
151 
152 /*
153  * Get a correct byte/short/word routines for Sparc platform.
154  */
155 #if defined(sparc)
156 static int
157 getbyte(uchar_t **bp)
158 {
159 	int	b;
160 
161 	b = **bp;
162 	*bp = *bp + 1;
163 	return (b);
164 }
165 
166 #ifdef DEADCODE
167 static int
168 getshort(uchar_t **bp)
169 {
170 	int	b;
171 
172 	b = ((**bp) << 8) | *(*bp + 1);
173 	*bp += 2;
174 	return (b);
175 }
176 #endif /* DEADCODE */
177 
178 static int
179 getlong(uchar_t **bp)
180 {
181 	int	b, bh, bl;
182 
183 	bh = ((**bp) << 8) | *(*bp + 1);
184 	*bp += 2;
185 	bl = ((**bp) << 8) | *(*bp + 1);
186 	*bp += 2;
187 
188 	b = (bh << 16) | bl;
189 	return (b);
190 }
191 #endif /* defined(sparc) */
192 
193 #ifdef i386
194 /*
195  * Convert emcpowerN[a-p,p0,p1,p2,p3,p4] to emcpowerNp0 path,
196  * this is specific for emc powerpath driver.
197  */
198 static void
199 get_emcpower_pname(char *name, char *devname)
200 {
201 	char	*emcp = "emcpower";
202 	char	*npt = NULL;
203 	char	np[MAXNAMELEN];
204 	int	i = strlen(emcp);
205 
206 	(void) strcpy(np, devname);
207 	npt = strstr(np, emcp);
208 	while ((i < strlen(npt)) && (isdigit(npt[i])))
209 		i++;
210 	npt[i] = '\0';
211 	(void) snprintf(name, MAXNAMELEN, "/dev/rdsk/%sp0", npt);
212 }
213 #endif
214 
215 /*
216  * Convert cn[tn]dn to cn[tn]dns2 path
217  */
218 static void
219 get_sname(char *name)
220 {
221 	char		buf[MAXPATHLEN];
222 	char		*devp = "/dev/dsk";
223 	char		*rdevp = "/dev/rdsk";
224 	char		np[MAXNAMELEN];
225 	char		*npt;
226 
227 #ifdef i386
228 	if (emcpower_name(cur_disk->disk_name)) {
229 		get_emcpower_pname(name, cur_disk->disk_name);
230 		return;
231 	}
232 #endif
233 
234 	/*
235 	 * If it is a full path /dev/[r]dsk/cn[tn]dn, use this path
236 	 */
237 	(void) strcpy(np, cur_disk->disk_name);
238 	if (strncmp(rdevp, cur_disk->disk_name, strlen(rdevp)) == 0 ||
239 	    strncmp(devp, cur_disk->disk_name, strlen(devp)) == 0) {
240 		/*
241 		 * Skip if the path is already included with sN
242 		 */
243 		if (strchr(np, 's') == strrchr(np, 's')) {
244 			npt = strrchr(np, 'p');
245 			/* If pN is found, do not include it */
246 			if (npt != NULL) {
247 				*npt = '\0';
248 			}
249 			(void) snprintf(buf, sizeof (buf), "%ss2", np);
250 		} else {
251 			(void) snprintf(buf, sizeof (buf), "%s", np);
252 		}
253 	} else {
254 		(void) snprintf(buf, sizeof (buf), "/dev/rdsk/%ss2", np);
255 	}
256 	(void) strcpy(name, buf);
257 }
258 
259 /*
260  * Convert cn[tn]dnsn to cn[tn]dnp0 path
261  */
262 static void
263 get_pname(char *name)
264 {
265 	char		buf[MAXPATHLEN];
266 	char		*devp = "/dev/dsk";
267 	char		*rdevp = "/dev/rdsk";
268 	char		np[MAXNAMELEN];
269 	char		*npt;
270 
271 	/*
272 	 * If it is a full path /dev/[r]dsk/cn[tn]dnsn, use this path
273 	 */
274 	if (cur_disk == NULL) {
275 		(void) strcpy(np, x86_devname);
276 	} else {
277 		(void) strcpy(np, cur_disk->disk_name);
278 	}
279 
280 #ifdef i386
281 	if (emcpower_name(np)) {
282 		get_emcpower_pname(name, np);
283 		return;
284 	}
285 #endif
286 
287 	if (strncmp(rdevp, np, strlen(rdevp)) == 0 ||
288 	    strncmp(devp, np, strlen(devp)) == 0) {
289 		/*
290 		 * Skip if the path is already included with pN
291 		 */
292 		if (strchr(np, 'p') == NULL) {
293 			npt = strrchr(np, 's');
294 			/* If sN is found, do not include it */
295 			if (isdigit(*++npt)) {
296 				*--npt = '\0';
297 			}
298 			(void) snprintf(buf, sizeof (buf), "%sp0", np);
299 		} else {
300 			(void) snprintf(buf, sizeof (buf), "%s", np);
301 		}
302 	} else {
303 		(void) snprintf(buf, sizeof (buf), "/dev/rdsk/%sp0", np);
304 	}
305 	(void) strcpy(name, buf);
306 }
307 
308 /*
309  * Open file descriptor for current disk (cur_file)
310  *	with "p0" path or cur_disk->disk_path
311  */
312 void
313 open_cur_file(int mode)
314 {
315 	char	*dkpath;
316 	char	pbuf[MAXPATHLEN];
317 
318 	switch (mode) {
319 		case FD_USE_P0_PATH:
320 			(void) get_pname(&pbuf[0]);
321 			dkpath = pbuf;
322 			break;
323 		case FD_USE_CUR_DISK_PATH:
324 			if (cur_disk->fdisk_part.systid == SUNIXOS ||
325 			    cur_disk->fdisk_part.systid == SUNIXOS2) {
326 				(void) get_sname(&pbuf[0]);
327 				dkpath = pbuf;
328 			} else {
329 				dkpath = cur_disk->disk_path;
330 			}
331 			break;
332 		default:
333 			err_print("Error: Invalid mode option for opening "
334 			    "cur_file\n");
335 			fullabort();
336 	}
337 
338 	/* Close previous cur_file */
339 	(void) close(cur_file);
340 	/* Open cur_file with the required path dkpath */
341 	if ((cur_file = open_disk(dkpath, O_RDWR | O_NDELAY)) < 0) {
342 		err_print(
343 		    "Error: can't open selected disk '%s'.\n", dkpath);
344 		fullabort();
345 	}
346 }
347 
348 
349 /*
350  * This routine implements the 'fdisk' command.  It simply runs
351  * the fdisk command on the current disk.
352  * Use of this is restricted to interactive mode only.
353  */
354 int
355 c_fdisk()
356 {
357 
358 	char		buf[MAXPATHLEN];
359 	char		pbuf[MAXPATHLEN];
360 	struct stat	statbuf;
361 
362 	/*
363 	 * We must be in interactive mode to use the fdisk command
364 	 */
365 	if (option_f != (char *)NULL || isatty(0) != 1 || isatty(1) != 1) {
366 		err_print("Fdisk command is for interactive use only!\n");
367 		return (-1);
368 	}
369 
370 	/*
371 	 * There must be a current disk type and a current disk
372 	 */
373 	if (cur_dtype == NULL) {
374 		err_print("Current Disk Type is not set.\n");
375 		return (-1);
376 	}
377 
378 	/*
379 	 * Before running the fdisk command, get file status of
380 	 *	/dev/rdsk/cn[tn]dnp0 path to see if this disk
381 	 *	supports fixed disk partition table.
382 	 */
383 	(void) get_pname(&pbuf[0]);
384 	if (stat(pbuf, (struct stat *)&statbuf) == -1 ||
385 	    !S_ISCHR(statbuf.st_mode)) {
386 		err_print(
387 		"Disk does not support fixed disk partition table\n");
388 		return (0);
389 	}
390 
391 	/*
392 	 * Run the fdisk program.
393 	 */
394 	(void) snprintf(buf, sizeof (buf), "fdisk %s\n", pbuf);
395 	(void) system(buf);
396 
397 	/*
398 	 * Open cur_file with "p0" path for accessing the fdisk table
399 	 */
400 	(void) open_cur_file(FD_USE_P0_PATH);
401 
402 	/*
403 	 * Get solaris partition information in the fdisk partition table
404 	 */
405 	if (get_solaris_part(cur_file, &cur_disk->fdisk_part) == -1) {
406 		err_print("No fdisk solaris partition found\n");
407 		cur_disk->fdisk_part.numsect = 0;  /* No Solaris */
408 	}
409 
410 	/*
411 	 * Restore cur_file with cur_disk->disk_path
412 	 */
413 	(void) open_cur_file(FD_USE_CUR_DISK_PATH);
414 
415 	return (0);
416 }
417 
418 /*
419  * Read MBR on the disk
420  * if the Solaris partition has changed,
421  *	reread the vtoc
422  */
423 #ifdef DEADCODE
424 static void
425 update_cur_parts()
426 {
427 
428 	int i;
429 	register struct partition_info *parts;
430 
431 	for (i = 0; i < NDKMAP; i++) {
432 #if defined(_SUNOS_VTOC_16)
433 		if (cur_parts->vtoc.v_part[i].p_tag &&
434 		    cur_parts->vtoc.v_part[i].p_tag != V_ALTSCTR) {
435 			cur_parts->vtoc.v_part[i].p_start = 0;
436 			cur_parts->vtoc.v_part[i].p_size = 0;
437 
438 #endif
439 			cur_parts->pinfo_map[i].dkl_nblk = 0;
440 			cur_parts->pinfo_map[i].dkl_cylno = 0;
441 			cur_parts->vtoc.v_part[i].p_tag =
442 			    default_vtoc_map[i].p_tag;
443 			cur_parts->vtoc.v_part[i].p_flag =
444 			    default_vtoc_map[i].p_flag;
445 #if defined(_SUNOS_VTOC_16)
446 		}
447 #endif
448 	}
449 	cur_parts->pinfo_map[C_PARTITION].dkl_nblk = ncyl * spc();
450 
451 #if defined(_SUNOS_VTOC_16)
452 	/*
453 	 * Adjust for the boot partitions
454 	 */
455 	cur_parts->pinfo_map[I_PARTITION].dkl_nblk = spc();
456 	cur_parts->pinfo_map[I_PARTITION].dkl_cylno = 0;
457 	cur_parts->vtoc.v_part[C_PARTITION].p_start =
458 	    cur_parts->pinfo_map[C_PARTITION].dkl_cylno * nhead * nsect;
459 	cur_parts->vtoc.v_part[C_PARTITION].p_size =
460 	    cur_parts->pinfo_map[C_PARTITION].dkl_nblk;
461 
462 	cur_parts->vtoc.v_part[I_PARTITION].p_start =
463 	    cur_parts->pinfo_map[I_PARTITION].dkl_cylno;
464 	cur_parts->vtoc.v_part[I_PARTITION].p_size =
465 	    cur_parts->pinfo_map[I_PARTITION].dkl_nblk;
466 
467 #endif	/* defined(_SUNOS_VTOC_16) */
468 	parts = cur_dtype->dtype_plist;
469 	cur_dtype->dtype_ncyl = ncyl;
470 	cur_dtype->dtype_plist = cur_parts;
471 	parts->pinfo_name = cur_parts->pinfo_name;
472 	cur_disk->disk_parts = cur_parts;
473 	cur_ctype->ctype_dlist = cur_dtype;
474 
475 }
476 #endif /* DEADCODE */
477 
478 static int
479 get_solaris_part(int fd, struct ipart *ipart)
480 {
481 	int		i;
482 	struct ipart	ip;
483 	int		status;
484 	char		*mbr;
485 	char		*bootptr;
486 	struct dk_label	update_label;
487 	ushort_t	found = 0;
488 #ifdef i386
489 	uint32_t	relsec, numsec;
490 	int		pno, rval, ext_part_found = 0;
491 	ext_part_t	*epp;
492 #endif
493 
494 	(void) lseek(fd, 0, 0);
495 
496 	/*
497 	 * We may get mbr of different size, but the first 512 bytes
498 	 * are valid information.
499 	 */
500 	mbr = malloc(cur_blksz);
501 	if (mbr == NULL) {
502 		err_print("No memory available.\n");
503 		return (-1);
504 	}
505 	status = read(fd, mbr, cur_blksz);
506 
507 	if (status != cur_blksz) {
508 		err_print("Bad read of fdisk partition. Status = %x\n", status);
509 		err_print("Cannot read fdisk partition information.\n");
510 		free(mbr);
511 		return (-1);
512 	}
513 
514 	(void) memcpy(&boot_sec, mbr, sizeof (struct mboot));
515 	free(mbr);
516 
517 #ifdef i386
518 	(void) extpart_init(&epp);
519 #endif
520 	for (i = 0; i < FD_NUMPART; i++) {
521 		int	ipc;
522 
523 		ipc = i * sizeof (struct ipart);
524 
525 		/* Handling the alignment problem of struct ipart */
526 		bootptr = &boot_sec.parts[ipc];
527 		(void) fill_ipart(bootptr, &ip);
528 
529 #ifdef i386
530 		if (fdisk_is_dos_extended(ip.systid) && (ext_part_found == 0)) {
531 			/* We support only one extended partition per disk */
532 			ext_part_found = 1;
533 			rval = fdisk_get_solaris_part(epp, &pno, &relsec,
534 			    &numsec);
535 			if (rval == FDISK_SUCCESS) {
536 				/*
537 				 * Found a solaris partition inside the
538 				 * extended partition. Update the statistics.
539 				 */
540 				if (nhead != 0 && nsect != 0) {
541 					pcyl = numsec / (nhead * nsect);
542 					xstart = relsec / (nhead * nsect);
543 					ncyl = pcyl - acyl;
544 				}
545 				solaris_offset = relsec;
546 				found = 2;
547 				ip.bootid = 0;
548 				ip.beghead = ip.begsect = ip.begcyl = 0xff;
549 				ip.endhead = ip.endsect = ip.endcyl = 0xff;
550 				ip.systid = SUNIXOS2;
551 				ip.relsect = relsec;
552 				ip.numsect = numsec;
553 				ipart->bootid = ip.bootid;
554 				status = bcmp(&ip, ipart,
555 				    sizeof (struct ipart));
556 				bcopy(&ip, ipart, sizeof (struct ipart));
557 			}
558 			continue;
559 		}
560 #endif
561 
562 		/*
563 		 * we are interested in Solaris and EFI partition types
564 		 */
565 #ifdef i386
566 		if ((ip.systid == SUNIXOS &&
567 		    (fdisk_is_linux_swap(epp, lel(ip.relsect), NULL) != 0)) ||
568 		    ip.systid == SUNIXOS2 ||
569 		    ip.systid == EFI_PMBR) {
570 #else
571 		if (ip.systid == SUNIXOS ||
572 		    ip.systid == SUNIXOS2 ||
573 		    ip.systid == EFI_PMBR) {
574 #endif
575 			/*
576 			 * if the disk has an EFI label, nhead and nsect may
577 			 * be zero.  This test protects us from FPE's, and
578 			 * format still seems to work fine
579 			 */
580 			if (nhead != 0 && nsect != 0) {
581 				pcyl = lel(ip.numsect) / (nhead * nsect);
582 				xstart = lel(ip.relsect) / (nhead * nsect);
583 				ncyl = pcyl - acyl;
584 			}
585 #ifdef DEBUG
586 			else {
587 				err_print("Critical geometry values are zero:\n"
588 				    "\tnhead = %d; nsect = %d\n", nhead, nsect);
589 			}
590 #endif /* DEBUG */
591 
592 			solaris_offset = (uint_t)lel(ip.relsect);
593 			found = 1;
594 			break;
595 		}
596 	}
597 
598 #ifdef i386
599 	libfdisk_fini(&epp);
600 #endif
601 
602 	if (!found) {
603 		err_print("Solaris fdisk partition not found\n");
604 		return (-1);
605 	} else if (found == 1) {
606 		/*
607 		 * Found a primary solaris partition.
608 		 * compare the previous and current Solaris partition
609 		 * but don't use bootid in determination of Solaris partition
610 		 * changes
611 		 */
612 		ipart->bootid = ip.bootid;
613 		status = bcmp(&ip, ipart, sizeof (struct ipart));
614 
615 		bcopy(&ip, ipart, sizeof (struct ipart));
616 	}
617 
618 	/* if the disk partitioning has changed - get the VTOC */
619 	if (status) {
620 		struct extvtoc exvtoc;
621 		struct vtoc vtoc;
622 
623 		status = ioctl(fd, DKIOCGEXTVTOC, &exvtoc);
624 		if (status == -1) {
625 			i = errno;
626 			/* Try the old ioctl DKIOCGVTOC */
627 			status = ioctl(fd, DKIOCGVTOC, &vtoc);
628 			if (status == -1) {
629 				err_print("Bad ioctl DKIOCGEXTVTOC.\n");
630 				err_print("errno=%d %s\n", i, strerror(i));
631 				err_print("Cannot read vtoc information.\n");
632 				return (-1);
633 			}
634 		}
635 
636 		status = read_label(fd, &update_label);
637 		if (status == -1) {
638 			err_print("Cannot read label information.\n");
639 			return (-1);
640 		}
641 
642 		/* copy vtoc information */
643 		cur_parts->vtoc = update_label.dkl_vtoc;
644 
645 #if defined(_SUNOS_VTOC_16)
646 		/*
647 		 * this is to update the slice table on x86
648 		 * we don't care about VTOC8 here
649 		 */
650 		for (i = 0; i < NDKMAP; i ++) {
651 			cur_parts->pinfo_map[i].dkl_cylno =
652 			    update_label.dkl_vtoc.v_part[i].p_start /
653 			    ((int)(update_label.dkl_nhead *
654 			    update_label.dkl_nsect));
655 			cur_parts->pinfo_map[i].dkl_nblk =
656 			    update_label.dkl_vtoc.v_part[i].p_size;
657 		}
658 #endif /* defined(_SUNOS_VTOC_16) */
659 
660 		cur_dtype->dtype_ncyl = update_label.dkl_ncyl;
661 		cur_dtype->dtype_pcyl = update_label.dkl_pcyl;
662 		cur_dtype->dtype_acyl = update_label.dkl_acyl;
663 		cur_dtype->dtype_nhead = update_label.dkl_nhead;
664 		cur_dtype->dtype_nsect = update_label.dkl_nsect;
665 		ncyl = cur_dtype->dtype_ncyl;
666 		acyl = cur_dtype->dtype_acyl;
667 		pcyl = cur_dtype->dtype_pcyl;
668 		nsect = cur_dtype->dtype_nsect;
669 		nhead = cur_dtype->dtype_nhead;
670 	}
671 	return (0);
672 }
673 
674 
675 int
676 copy_solaris_part(struct ipart *ipart)
677 {
678 
679 	int		status, i, fd;
680 	struct mboot	mboot;
681 	char		*mbr;
682 	struct ipart	ip;
683 	char		buf[MAXPATHLEN];
684 	char		*bootptr;
685 	struct stat	statbuf;
686 #ifdef i386
687 	uint32_t	relsec, numsec;
688 	int		pno, rval, ext_part_found = 0;
689 	ext_part_t	*epp;
690 #endif
691 
692 	(void) get_pname(&buf[0]);
693 	if (stat(buf, &statbuf) == -1 ||
694 	    !S_ISCHR(statbuf.st_mode) ||
695 	    ((cur_label == L_TYPE_EFI) &&
696 	    (cur_disk->disk_flags & DSK_LABEL_DIRTY))) {
697 		/*
698 		 * Make sure to reset solaris_offset to zero if it is
699 		 *	previously set by a selected disk that
700 		 *	supports the fdisk table.
701 		 */
702 		solaris_offset = 0;
703 		/*
704 		 * Return if this disk does not support fdisk table or
705 		 * if it uses an EFI label but has not yet been labelled.
706 		 * If the EFI label has not been written then the open
707 		 * on the partition will fail.
708 		 */
709 		return (0);
710 	}
711 
712 	if ((fd = open(buf, O_RDONLY)) < 0) {
713 		/*
714 		 * Labeled device support in lofi provides p0 partition on
715 		 * both x86 and sparc. However, sparc does not implement fdisk
716 		 * partitioning. This workaround will allow format
717 		 * to ignore fdisk errors in case of lofi devices.
718 		 */
719 		if (strcmp(cur_disk->disk_dkinfo.dki_cname, "lofi") == 0) {
720 			solaris_offset = 0;
721 			return (0);
722 		}
723 		err_print("Error: can't open disk '%s'.\n", buf);
724 		return (-1);
725 	}
726 
727 	/*
728 	 * We may get mbr of different size, but the first 512 bytes
729 	 * are valid information.
730 	 */
731 	mbr = malloc(cur_blksz);
732 	if (mbr == NULL) {
733 		err_print("No memory available.\n");
734 		return (-1);
735 	}
736 	status = read(fd, mbr, cur_blksz);
737 
738 	if (status != cur_blksz) {
739 		err_print("Bad read of fdisk partition.\n");
740 		(void) close(fd);
741 		free(mbr);
742 		return (-1);
743 	}
744 
745 	(void) memcpy(&mboot, mbr, sizeof (struct mboot));
746 
747 #ifdef i386
748 	(void) extpart_init(&epp);
749 #endif
750 	for (i = 0; i < FD_NUMPART; i++) {
751 		int	ipc;
752 
753 		ipc = i * sizeof (struct ipart);
754 
755 		/* Handling the alignment problem of struct ipart */
756 		bootptr = &mboot.parts[ipc];
757 		(void) fill_ipart(bootptr, &ip);
758 
759 #ifdef i386
760 		if (fdisk_is_dos_extended(ip.systid) && (ext_part_found == 0)) {
761 			/* We support only one extended partition per disk */
762 			ext_part_found = 1;
763 			rval = fdisk_get_solaris_part(epp, &pno, &relsec,
764 			    &numsec);
765 			if (rval == FDISK_SUCCESS) {
766 				/*
767 				 * Found a solaris partition inside the
768 				 * extended partition. Update the statistics.
769 				 */
770 				if (nhead != 0 && nsect != 0) {
771 					pcyl = numsec / (nhead * nsect);
772 					ncyl = pcyl - acyl;
773 				}
774 				solaris_offset = relsec;
775 				ip.bootid = 0;
776 				ip.beghead = ip.begsect = ip.begcyl = 0xff;
777 				ip.endhead = ip.endsect = ip.endcyl = 0xff;
778 				ip.systid = SUNIXOS2;
779 				ip.relsect = relsec;
780 				ip.numsect = numsec;
781 				bcopy(&ip, ipart, sizeof (struct ipart));
782 			}
783 			continue;
784 		}
785 #endif
786 
787 
788 #ifdef i386
789 		if ((ip.systid == SUNIXOS &&
790 		    (fdisk_is_linux_swap(epp, lel(ip.relsect), NULL) != 0)) ||
791 		    ip.systid == SUNIXOS2 ||
792 		    ip.systid == EFI_PMBR) {
793 #else
794 		if (ip.systid == SUNIXOS ||
795 		    ip.systid == SUNIXOS2 ||
796 		    ip.systid == EFI_PMBR) {
797 #endif
798 			solaris_offset = lel(ip.relsect);
799 			bcopy(&ip, ipart, sizeof (struct ipart));
800 
801 			/*
802 			 * if the disk has an EFI label, we typically won't
803 			 * have values for nhead and nsect.  format seems to
804 			 * work without them, and we need to protect ourselves
805 			 * from FPE's
806 			 */
807 			if (nhead != 0 && nsect != 0) {
808 				pcyl = lel(ip.numsect) / (nhead * nsect);
809 				ncyl = pcyl - acyl;
810 			}
811 #ifdef DEBUG
812 			else {
813 				err_print("Critical geometry values are zero:\n"
814 				    "\tnhead = %d; nsect = %d\n", nhead, nsect);
815 			}
816 #endif /* DEBUG */
817 
818 			break;
819 		}
820 	}
821 #ifdef i386
822 	libfdisk_fini(&epp);
823 #endif
824 
825 	(void) close(fd);
826 	free(mbr);
827 	return (0);
828 }
829 
830 #if defined(_FIRMWARE_NEEDS_FDISK)
831 int
832 auto_solaris_part(struct dk_label *label)
833 {
834 
835 	int		status, i, fd;
836 	struct mboot	mboot;
837 	char		*mbr;
838 	struct ipart	ip;
839 	char		*bootptr;
840 	char		pbuf[MAXPATHLEN];
841 #ifdef i386
842 	uint32_t	relsec, numsec;
843 	int		pno, rval, ext_part_found = 0;
844 	ext_part_t	*epp;
845 #endif
846 
847 	(void) get_pname(&pbuf[0]);
848 	if ((fd = open_disk(pbuf, O_RDONLY)) < 0) {
849 		err_print("Error: can't open selected disk '%s'.\n", pbuf);
850 		return (-1);
851 	}
852 
853 	/*
854 	 * We may get mbr of different size, but the first 512 bytes
855 	 * are valid information.
856 	 */
857 	mbr = malloc(cur_blksz);
858 	if (mbr == NULL) {
859 		err_print("No memory available.\n");
860 		return (-1);
861 	}
862 	status = read(fd, mbr, cur_blksz);
863 
864 	if (status != cur_blksz) {
865 		err_print("Bad read of fdisk partition.\n");
866 		free(mbr);
867 		return (-1);
868 	}
869 
870 	(void) memcpy(&mboot, mbr, sizeof (struct mboot));
871 
872 #ifdef i386
873 	(void) extpart_init(&epp);
874 #endif
875 	for (i = 0; i < FD_NUMPART; i++) {
876 		int	ipc;
877 
878 		ipc = i * sizeof (struct ipart);
879 
880 		/* Handling the alignment problem of struct ipart */
881 		bootptr = &mboot.parts[ipc];
882 		(void) fill_ipart(bootptr, &ip);
883 
884 #ifdef i386
885 		if (fdisk_is_dos_extended(ip.systid) && (ext_part_found == 0)) {
886 			/* We support only one extended partition per disk */
887 			ext_part_found = 1;
888 			rval = fdisk_get_solaris_part(epp, &pno, &relsec,
889 			    &numsec);
890 			if (rval == FDISK_SUCCESS) {
891 				/*
892 				 * Found a solaris partition inside the
893 				 * extended partition. Update the statistics.
894 				 */
895 				if ((label->dkl_nhead != 0) &&
896 				    (label->dkl_nsect != 0)) {
897 					label->dkl_pcyl =
898 					    numsec / (label->dkl_nhead *
899 					    label->dkl_nsect);
900 					label->dkl_ncyl = label->dkl_pcyl -
901 					    label->dkl_acyl;
902 				}
903 				solaris_offset = relsec;
904 			}
905 			continue;
906 		}
907 #endif
908 
909 		/*
910 		 * if the disk has an EFI label, the nhead and nsect fields
911 		 * the label may be zero.  This protects us from FPE's, and
912 		 * format still seems to work happily
913 		 */
914 
915 
916 #ifdef i386
917 		if ((ip.systid == SUNIXOS &&
918 		    (fdisk_is_linux_swap(epp, lel(ip.relsect), NULL) != 0)) ||
919 		    ip.systid == SUNIXOS2 ||
920 		    ip.systid == EFI_PMBR) {
921 #else
922 		if (ip.systid == SUNIXOS ||
923 		    ip.systid == SUNIXOS2 ||
924 		    ip.systid == EFI_PMBR) {
925 #endif
926 			if ((label->dkl_nhead != 0) &&
927 			    (label->dkl_nsect != 0)) {
928 				label->dkl_pcyl = lel(ip.numsect) /
929 				    (label->dkl_nhead * label->dkl_nsect);
930 				label->dkl_ncyl = label->dkl_pcyl -
931 				    label->dkl_acyl;
932 			}
933 #ifdef DEBUG
934 			else {
935 				err_print("Critical label fields aren't "
936 				    "non-zero:\n"
937 				    "\tlabel->dkl_nhead = %d; "
938 				    "label->dkl_nsect = "
939 				    "%d\n", label->dkl_nhead,
940 				    label->dkl_nsect);
941 			}
942 #endif /* DEBUG */
943 
944 		solaris_offset = lel(ip.relsect);
945 		break;
946 		}
947 	}
948 
949 #ifdef i386
950 	libfdisk_fini(&epp);
951 #endif
952 	(void) close(fd);
953 	free(mbr);
954 	return (0);
955 }
956 #endif	/* defined(_FIRMWARE_NEEDS_FDISK) */
957 
958 
959 int
960 good_fdisk()
961 {
962 	char		buf[MAXPATHLEN];
963 	struct stat	statbuf;
964 
965 	(void) get_pname(&buf[0]);
966 	if (stat(buf, &statbuf) == -1 ||
967 	    !S_ISCHR(statbuf.st_mode) ||
968 	    cur_label == L_TYPE_EFI) {
969 		/*
970 		 * Return if this disk does not support fdisk table or
971 		 * if the disk is labeled with EFI.
972 		 */
973 		return (1);
974 	}
975 
976 	if (lel(cur_disk->fdisk_part.numsect) > 0) {
977 		return (1);
978 	} else {
979 		/*
980 		 * Labeled device support in lofi provides p0 partition on
981 		 * both x86 and sparc. However, sparc does not implement fdisk
982 		 * partitioning. This workaround will allow format
983 		 * to ignore fdisk errors in case of lofi devices.
984 		 */
985 		if (strcmp(cur_disk->disk_dkinfo.dki_cname, "lofi") == 0) {
986 			return (1);
987 		}
988 		err_print("WARNING - ");
989 		err_print("This disk may be in use by an application "
990 		    "that has\n\t  modified the fdisk table. Ensure "
991 		    "that this disk is\n\t  not currently in use "
992 		    "before proceeding to use fdisk.\n");
993 		return (0);
994 	}
995 }
996 
997 #ifdef i386
998 int
999 extpart_init(ext_part_t **epp)
1000 {
1001 	int		rval, lf_op_flag = 0;
1002 	char		p0_path[MAXPATHLEN];
1003 
1004 	get_pname(&p0_path[0]);
1005 	lf_op_flag |= FDISK_READ_DISK;
1006 	if ((rval = libfdisk_init(epp, p0_path, NULL, lf_op_flag)) !=
1007 	    FDISK_SUCCESS) {
1008 		switch (rval) {
1009 			/*
1010 			 * FDISK_EBADLOGDRIVE, FDISK_ENOLOGDRIVE
1011 			 * and FDISK_EBADMAGIC can be considered
1012 			 * as soft errors and hence we do not exit.
1013 			 */
1014 			case FDISK_EBADLOGDRIVE:
1015 				break;
1016 			case FDISK_ENOLOGDRIVE:
1017 				break;
1018 			case FDISK_EBADMAGIC:
1019 				break;
1020 			case FDISK_ENOVGEOM:
1021 				err_print("Could not get virtual geometry for"
1022 				    " this device\n");
1023 				fullabort();
1024 				break;
1025 			case FDISK_ENOPGEOM:
1026 				err_print("Could not get physical geometry for"
1027 				    " this device\n");
1028 				fullabort();
1029 				break;
1030 			case FDISK_ENOLGEOM:
1031 				err_print("Could not get label geometry for "
1032 				    " this device\n");
1033 				fullabort();
1034 				break;
1035 			default:
1036 				err_print("Failed to initialise libfdisk.\n");
1037 				fullabort();
1038 				break;
1039 		}
1040 	}
1041 	return (0);
1042 }
1043 #endif
1044