xref: /titanic_50/usr/src/cmd/format/menu_fdisk.c (revision 554ff184129088135ad2643c1c9832174a17be88)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * This file contains functions that implement the fdisk menu commands.
31  */
32 #include "global.h"
33 #include <errno.h>
34 #include <sys/time.h>
35 #include <sys/resource.h>
36 #include <sys/wait.h>
37 #include <signal.h>
38 #include <string.h>
39 #include <fcntl.h>
40 #include <stdlib.h>
41 #include <sys/dktp/fdisk.h>
42 #include <sys/stat.h>
43 #include <sys/dklabel.h>
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 "checkmount.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  * Handling the alignment problem of struct ipart.
113  */
114 static void
115 fill_ipart(char *bootptr, struct ipart *partp)
116 {
117 #if defined(sparc)
118 	/*
119 	 * Sparc platform:
120 	 *
121 	 * Packing short/word for struct ipart to resolve
122 	 *	little endian on Sparc since it is not
123 	 *	properly aligned on Sparc.
124 	 */
125 	partp->bootid = getbyte((uchar_t **)&bootptr);
126 	partp->beghead = getbyte((uchar_t **)&bootptr);
127 	partp->begsect = getbyte((uchar_t **)&bootptr);
128 	partp->begcyl = getbyte((uchar_t **)&bootptr);
129 	partp->systid = getbyte((uchar_t **)&bootptr);
130 	partp->endhead = getbyte((uchar_t **)&bootptr);
131 	partp->endsect = getbyte((uchar_t **)&bootptr);
132 	partp->endcyl = getbyte((uchar_t **)&bootptr);
133 	partp->relsect = getlong((uchar_t **)&bootptr);
134 	partp->numsect = getlong((uchar_t **)&bootptr);
135 #elif defined(i386)
136 	/*
137 	 * i386 platform:
138 	 *
139 	 * The fdisk table does not begin on a 4-byte boundary within
140 	 * the master boot record; so, we need to recopy its contents
141 	 * to another data structure to avoid an alignment exception.
142 	 */
143 	(void) bcopy(bootptr, partp, sizeof (struct ipart));
144 #else
145 #error  No Platform defined
146 #endif /* defined(sparc) */
147 }
148 
149 /*
150  * Get a correct byte/short/word routines for Sparc platform.
151  */
152 #if defined(sparc)
153 static int
154 getbyte(uchar_t **bp)
155 {
156 	int	b;
157 
158 	b = **bp;
159 	*bp = *bp + 1;
160 	return (b);
161 }
162 
163 #ifdef DEADCODE
164 static int
165 getshort(uchar_t **bp)
166 {
167 	int	b;
168 
169 	b = ((**bp) << 8) | *(*bp + 1);
170 	*bp += 2;
171 	return (b);
172 }
173 #endif /* DEADCODE */
174 
175 static int
176 getlong(uchar_t **bp)
177 {
178 	int	b, bh, bl;
179 
180 	bh = ((**bp) << 8) | *(*bp + 1);
181 	*bp += 2;
182 	bl = ((**bp) << 8) | *(*bp + 1);
183 	*bp += 2;
184 
185 	b = (bh << 16) | bl;
186 	return (b);
187 }
188 #endif /* defined(sparc) */
189 
190 
191 /*
192  * Convert cn[tn]dnsn to cn[tn]dnp0 path
193  */
194 static void
195 get_pname(char *name)
196 {
197 	char		buf[MAXPATHLEN];
198 	char		*devp = "/dev/dsk";
199 	char		*rdevp = "/dev/rdsk";
200 	char		np[MAXNAMELEN];
201 	char		*npt;
202 
203 	/*
204 	 * If it is a full path /dev/[r]dsk/cn[tn]dnsn, use this path
205 	 */
206 	(void) strcpy(np, cur_disk->disk_name);
207 	if (strncmp(rdevp, cur_disk->disk_name, strlen(rdevp)) == 0 ||
208 	    strncmp(devp, cur_disk->disk_name, strlen(devp)) == 0) {
209 		/*
210 		 * Skip if the path is already included with pN
211 		 */
212 		if (strchr(np, 'p') == NULL) {
213 			npt = strrchr(np, 's');
214 			/* If sN is found, do not include it */
215 			if (isdigit(*++npt)) {
216 				*--npt = '\0';
217 			}
218 			(void) snprintf(buf, sizeof (buf), "%sp0", np);
219 		} else {
220 			(void) snprintf(buf, sizeof (buf), "%s", np);
221 		}
222 	} else {
223 		(void) snprintf(buf, sizeof (buf), "/dev/rdsk/%sp0", np);
224 	}
225 	(void) strcpy(name, buf);
226 }
227 
228 /*
229  * Open file descriptor for current disk (cur_file)
230  *	with "p0" path or cur_disk->disk_path
231  */
232 void
233 open_cur_file(int mode)
234 {
235 	char	*dkpath;
236 	char	pbuf[MAXPATHLEN];
237 
238 	switch (mode) {
239 	    case FD_USE_P0_PATH:
240 		(void) get_pname(&pbuf[0]);
241 		dkpath = pbuf;
242 		break;
243 	    case FD_USE_CUR_DISK_PATH:
244 		dkpath = cur_disk->disk_path;
245 		break;
246 	    default:
247 		err_print("Error: Invalid mode option for opening cur_file\n");
248 		fullabort();
249 	}
250 
251 	/* Close previous cur_file */
252 	(void) close(cur_file);
253 	/* Open cur_file with the required path dkpath */
254 	if ((cur_file = open_disk(dkpath, O_RDWR | O_NDELAY)) < 0) {
255 		err_print(
256 		    "Error: can't open selected disk '%s'.\n", dkpath);
257 		fullabort();
258 	}
259 }
260 
261 
262 /*
263  * This routine implements the 'fdisk' command.  It simply runs
264  * the fdisk command on the current disk.
265  * Use of this is restricted to interactive mode only.
266  */
267 int
268 c_fdisk()
269 {
270 
271 	char		buf[MAXPATHLEN];
272 	char		pbuf[MAXPATHLEN];
273 	struct stat	statbuf;
274 
275 	/*
276 	 * We must be in interactive mode to use the fdisk command
277 	 */
278 	if (option_f != (char *)NULL || isatty(0) != 1 || isatty(1) != 1) {
279 		err_print("Fdisk command is for interactive use only!\n");
280 		return (-1);
281 	}
282 
283 	/*
284 	 * There must be a current disk type and a current disk
285 	 */
286 	if (cur_dtype == NULL) {
287 		err_print("Current Disk Type is not set.\n");
288 		return (-1);
289 	}
290 
291 	/*
292 	 * If disk is larger than 1TB then an EFI label is required
293 	 * and there is no point in running fdisk
294 	 */
295 	if (cur_dtype->capacity > INFINITY) {
296 		err_print("This disk must use an EFI label.\n");
297 		return (-1);
298 	}
299 
300 	/*
301 	 * Before running the fdisk command, get file status of
302 	 *	/dev/rdsk/cn[tn]dnp0 path to see if this disk
303 	 *	supports fixed disk partition table.
304 	 */
305 	(void) get_pname(&pbuf[0]);
306 	if (stat(pbuf, (struct stat *)&statbuf) == -1 ||
307 	    !S_ISCHR(statbuf.st_mode)) {
308 		err_print(
309 		"Disk does not support fixed disk partition table\n");
310 		return (0);
311 	}
312 
313 	/*
314 	 * Run the fdisk program.
315 	 */
316 	(void) snprintf(buf, sizeof (buf), "fdisk %s\n", pbuf);
317 	(void) system(buf);
318 
319 	/*
320 	 * Open cur_file with "p0" path for accessing the fdisk table
321 	 */
322 	(void) open_cur_file(FD_USE_P0_PATH);
323 
324 	/*
325 	 * Get solaris partition information in the fdisk partition table
326 	 */
327 	if (get_solaris_part(cur_file, &cur_disk->fdisk_part) == -1) {
328 		err_print("No fdisk solaris partition found\n");
329 		cur_disk->fdisk_part.numsect = 0;  /* No Solaris */
330 	}
331 
332 	/*
333 	 * Restore cur_file with cur_disk->disk_path
334 	 */
335 	(void) open_cur_file(FD_USE_CUR_DISK_PATH);
336 
337 	return (0);
338 }
339 
340 /*
341  * Read MBR on the disk
342  * if the Solaris partition has changed,
343  *	reread the vtoc
344  */
345 #ifdef DEADCODE
346 static void
347 update_cur_parts()
348 {
349 
350 	int i;
351 	register struct partition_info *parts;
352 
353 	for (i = 0; i < NDKMAP; i++) {
354 #if defined(_SUNOS_VTOC_16)
355 		if (cur_parts->vtoc.v_part[i].p_tag &&
356 			cur_parts->vtoc.v_part[i].p_tag != V_ALTSCTR) {
357 			cur_parts->vtoc.v_part[i].p_start = 0;
358 			cur_parts->vtoc.v_part[i].p_size = 0;
359 
360 #endif
361 			cur_parts->pinfo_map[i].dkl_nblk = 0;
362 			cur_parts->pinfo_map[i].dkl_cylno = 0;
363 			cur_parts->vtoc.v_part[i].p_tag =
364 				default_vtoc_map[i].p_tag;
365 			cur_parts->vtoc.v_part[i].p_flag =
366 				default_vtoc_map[i].p_flag;
367 #if defined(_SUNOS_VTOC_16)
368 		}
369 #endif
370 	}
371 	cur_parts->pinfo_map[C_PARTITION].dkl_nblk = ncyl * spc();
372 
373 #if defined(_SUNOS_VTOC_16)
374 	/*
375 	 * Adjust for the boot partitions
376 	 */
377 	cur_parts->pinfo_map[I_PARTITION].dkl_nblk = spc();
378 	cur_parts->pinfo_map[I_PARTITION].dkl_cylno = 0;
379 	cur_parts->vtoc.v_part[C_PARTITION].p_start =
380 		cur_parts->pinfo_map[C_PARTITION].dkl_cylno * nhead * nsect;
381 	cur_parts->vtoc.v_part[C_PARTITION].p_size =
382 		cur_parts->pinfo_map[C_PARTITION].dkl_nblk;
383 
384 	cur_parts->vtoc.v_part[I_PARTITION].p_start =
385 			cur_parts->pinfo_map[I_PARTITION].dkl_cylno;
386 	cur_parts->vtoc.v_part[I_PARTITION].p_size =
387 			cur_parts->pinfo_map[I_PARTITION].dkl_nblk;
388 
389 #endif	/* defined(_SUNOS_VTOC_16) */
390 	parts = cur_dtype->dtype_plist;
391 	cur_dtype->dtype_ncyl = ncyl;
392 	cur_dtype->dtype_plist = cur_parts;
393 	parts->pinfo_name = cur_parts->pinfo_name;
394 	cur_disk->disk_parts = cur_parts;
395 	cur_ctype->ctype_dlist = cur_dtype;
396 
397 }
398 #endif /* DEADCODE */
399 
400 static int
401 get_solaris_part(int fd, struct ipart *ipart)
402 {
403 	int		i;
404 	struct ipart	ip;
405 	int		status;
406 	char		*bootptr;
407 
408 	(void) lseek(fd, 0, 0);
409 	status = read(fd, (caddr_t)&boot_sec, NBPSCTR);
410 
411 	if (status != NBPSCTR) {
412 		err_print("Bad read of fdisk partition. Status = %x\n", status);
413 		err_print("Cannot read fdisk partition information.\n");
414 		return (-1);
415 	}
416 
417 	for (i = 0; i < FD_NUMPART; i++) {
418 		int	ipc;
419 
420 		ipc = i * sizeof (struct ipart);
421 
422 		/* Handling the alignment problem of struct ipart */
423 		bootptr = &boot_sec.parts[ipc];
424 		(void) fill_ipart(bootptr, &ip);
425 
426 		/*
427 		 * we are interested in Solaris and EFI partition types
428 		 */
429 		if (ip.systid == SUNIXOS ||
430 		    ip.systid == SUNIXOS2 ||
431 		    ip.systid == EFI_PMBR) {
432 			/*
433 			 * if the disk has an EFI label, nhead and nsect may
434 			 * be zero.  This test protects us from FPE's, and
435 			 * format still seems to work fine
436 			 */
437 			if (nhead != 0 && nsect != 0) {
438 				pcyl = lel(ip.numsect) / (nhead * nsect);
439 				xstart = lel(ip.relsect) / (nhead * nsect);
440 				ncyl = pcyl - acyl;
441 			}
442 #ifdef DEBUG
443 			else {
444 				err_print("Critical geometry values are zero:\n"
445 					"\tnhead = %d; nsect = %d\n", nhead,
446 					nsect);
447 			}
448 #endif /* DEBUG */
449 
450 			solaris_offset = lel(ip.relsect);
451 			break;
452 		}
453 	}
454 
455 	if (i == FD_NUMPART) {
456 		err_print("Solaris fdisk partition not found\n");
457 		return (-1);
458 	}
459 
460 	/*
461 	 * compare the previous and current Solaris partition
462 	 * but don't use bootid in determination of Solaris partition changes
463 	 */
464 	ipart->bootid = ip.bootid;
465 	status = bcmp(&ip, ipart, sizeof (struct ipart));
466 
467 	bcopy(&ip, ipart, sizeof (struct ipart));
468 
469 	/* if the disk partitioning has changed - get the VTOC */
470 	if (status) {
471 		status = ioctl(fd, DKIOCGVTOC, &cur_parts->vtoc);
472 		if (status == -1) {
473 			i = errno;
474 			err_print("Bad ioctl DKIOCGVTOC.\n");
475 			err_print("errno=%d %s\n", i, strerror(i));
476 			err_print("Cannot read vtoc information.\n");
477 			return (-1);
478 		}
479 	}
480 	return (0);
481 }
482 
483 
484 int
485 copy_solaris_part(struct ipart *ipart)
486 {
487 
488 	int		status, i, fd;
489 	struct mboot	mboot;
490 	struct ipart	ip;
491 	char		buf[MAXPATHLEN];
492 	char		*bootptr;
493 	struct stat	statbuf;
494 
495 	(void) get_pname(&buf[0]);
496 	if (stat(buf, &statbuf) == -1 ||
497 	    !S_ISCHR(statbuf.st_mode) ||
498 	    ((cur_label == L_TYPE_EFI) &&
499 		(cur_disk->disk_flags & DSK_LABEL_DIRTY))) {
500 		/*
501 		 * Make sure to reset solaris_offset to zero if it is
502 		 *	previously set by a selected disk that
503 		 *	supports the fdisk table.
504 		 */
505 		solaris_offset = 0;
506 		/*
507 		 * Return if this disk does not support fdisk table or
508 		 * if it uses an EFI label but has not yet been labelled.
509 		 * If the EFI label has not been written then the open
510 		 * on the partition will fail.
511 		 */
512 		return (0);
513 	}
514 
515 	if ((fd = open(buf, O_RDONLY)) < 0) {
516 		err_print("Error: can't open disk '%s'.\n", buf);
517 		return (-1);
518 	}
519 
520 	status = read(fd, (caddr_t)&mboot, sizeof (struct mboot));
521 
522 	if (status != sizeof (struct mboot)) {
523 		err_print("Bad read of fdisk partition.\n");
524 		(void) close(fd);
525 		return (-1);
526 	}
527 
528 	for (i = 0; i < FD_NUMPART; i++) {
529 		int	ipc;
530 
531 		ipc = i * sizeof (struct ipart);
532 
533 		/* Handling the alignment problem of struct ipart */
534 		bootptr = &mboot.parts[ipc];
535 		(void) fill_ipart(bootptr, &ip);
536 
537 		if (ip.systid == SUNIXOS ||
538 		    ip.systid == SUNIXOS2 ||
539 		    ip.systid == EFI_PMBR) {
540 			solaris_offset = lel(ip.relsect);
541 			bcopy(&ip, ipart, sizeof (struct ipart));
542 
543 			/*
544 			 * if the disk has an EFI label, we typically won't
545 			 * have values for nhead and nsect.  format seems to
546 			 * work without them, and we need to protect ourselves
547 			 * from FPE's
548 			 */
549 			if (nhead != 0 && nsect != 0) {
550 				pcyl = lel(ip.numsect) / (nhead * nsect);
551 				ncyl = pcyl - acyl;
552 			}
553 #ifdef DEBUG
554 			else {
555 				err_print("Critical geometry values are zero:\n"
556 					"\tnhead = %d; nsect = %d\n", nhead,
557 					nsect);
558 			}
559 #endif /* DEBUG */
560 
561 			break;
562 		}
563 	}
564 
565 	(void) close(fd);
566 	return (0);
567 
568 }
569 
570 #if defined(_FIRMWARE_NEEDS_FDISK)
571 int
572 auto_solaris_part(struct dk_label *label)
573 {
574 
575 	int		status, i, fd;
576 	struct mboot	mboot;
577 	struct ipart	ip;
578 	char		buf[80];
579 	char		*bootptr;
580 
581 	(void) snprintf(buf, sizeof (buf), "/dev/rdsk/%sp0", x86_devname);
582 	if ((fd = open(buf, O_RDONLY)) < 0) {
583 		err_print("Error: can't open selected disk '%s'.\n", buf);
584 		return (-1);
585 	}
586 	status = read(fd, (caddr_t)&mboot, sizeof (struct mboot));
587 
588 	if (status != sizeof (struct mboot)) {
589 		err_print("Bad read of fdisk partition.\n");
590 		return (-1);
591 	}
592 
593 	for (i = 0; i < FD_NUMPART; i++) {
594 		int	ipc;
595 
596 		ipc = i * sizeof (struct ipart);
597 
598 		/* Handling the alignment problem of struct ipart */
599 		bootptr = &mboot.parts[ipc];
600 		(void) fill_ipart(bootptr, &ip);
601 
602 		/*
603 		 * if the disk has an EFI label, the nhead and nsect fields
604 		 * the label may be zero.  This protects us from FPE's, and
605 		 * format still seems to work happily
606 		 */
607 		if (ip.systid == SUNIXOS ||
608 		    ip.systid == SUNIXOS2 ||
609 		    ip.systid == EFI_PMBR) {
610 			if ((label->dkl_nhead != 0) &&
611 			    (label->dkl_nsect != 0)) {
612 				label->dkl_pcyl = lel(ip.numsect) /
613 				    (label->dkl_nhead * label->dkl_nsect);
614 				label->dkl_ncyl = label->dkl_pcyl -
615 				    label->dkl_acyl;
616 			}
617 #ifdef DEBUG
618 			else {
619 				err_print("Critical label fields aren't "
620 					"non-zero:\n"
621 					"\tlabel->dkl_nhead = %d; "
622 					"label->dkl_nsect = "
623 					"%d\n", label->dkl_nhead,
624 					label->dkl_nsect);
625 			}
626 #endif /* DEBUG */
627 
628 		solaris_offset = lel(ip.relsect);
629 		break;
630 		}
631 	}
632 
633 	(void) close(fd);
634 
635 	return (0);
636 }
637 #endif	/* defined(_FIRMWARE_NEEDS_FDISK) */
638 
639 
640 int
641 good_fdisk()
642 {
643 	char		buf[MAXPATHLEN];
644 	struct stat	statbuf;
645 
646 	(void) get_pname(&buf[0]);
647 	if (stat(buf, &statbuf) == -1 ||
648 	    !S_ISCHR(statbuf.st_mode) ||
649 	    cur_label == L_TYPE_EFI) {
650 		/*
651 		 * Return if this disk does not support fdisk table or
652 		 * if the disk is labeled with EFI.
653 		 */
654 		return (1);
655 	}
656 
657 	if (lel(cur_disk->fdisk_part.numsect) > 0)
658 		return (1);
659 	else
660 		return (0);
661 }
662