xref: /onnv-gate/usr/src/cmd/devinfo/devinfo.c (revision 7563:84ec90ffc3f7)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51871Ssommerfe  * Common Development and Distribution License (the "License").
61871Ssommerfe  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*7563SPrasad.Singamsetty@Sun.COM 
220Sstevel@tonic-gate /*
23*7563SPrasad.Singamsetty@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  *	Two output fields under the -i option will always be
320Sstevel@tonic-gate  *	output as zero, since they are not supported by Sun:
330Sstevel@tonic-gate  *		Software version, and
340Sstevel@tonic-gate  *		Drive id number.
350Sstevel@tonic-gate  *	AT&T filled these 2 fields with data from their "pdsector",
360Sstevel@tonic-gate  *	which Sun doesn't support per se.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include <stdio.h>
410Sstevel@tonic-gate #include <fcntl.h>
420Sstevel@tonic-gate #include <stdlib.h>
430Sstevel@tonic-gate #include <unistd.h>
440Sstevel@tonic-gate #include <string.h>
450Sstevel@tonic-gate #include <sys/types.h>
460Sstevel@tonic-gate #include <sys/stat.h>
470Sstevel@tonic-gate #include <sys/dkio.h>
480Sstevel@tonic-gate #include <sys/efi_partition.h>
490Sstevel@tonic-gate #include <sys/vtoc.h>
500Sstevel@tonic-gate #include <sys/mkdev.h>
510Sstevel@tonic-gate #include <errno.h>
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #define	DRERR	2
540Sstevel@tonic-gate #define	OPENERR	2
550Sstevel@tonic-gate 
560Sstevel@tonic-gate /*
570Sstevel@tonic-gate  * Standard I/O file descriptors.
580Sstevel@tonic-gate  */
590Sstevel@tonic-gate #define	STDOUT	1		/* Standard output */
600Sstevel@tonic-gate #define	STDERR	2		/* Standard error */
610Sstevel@tonic-gate 
620Sstevel@tonic-gate static	void	partinfo(int fd, char *device);
630Sstevel@tonic-gate static	void	devinfo(struct dk_geom *geom, int fd, char *device);
64*7563SPrasad.Singamsetty@Sun.COM static	int	readvtoc(int fd, char *name, struct extvtoc *vtoc);
650Sstevel@tonic-gate static	int	warn(char *what, char *why);
660Sstevel@tonic-gate static	void	usage(void);
670Sstevel@tonic-gate 
68239Sceastha int
main(int argc,char ** argv)690Sstevel@tonic-gate main(int argc, char **argv)
700Sstevel@tonic-gate {
710Sstevel@tonic-gate 	struct dk_geom  geom;
720Sstevel@tonic-gate 	int errflg, iflg, pflg, fd, c;
730Sstevel@tonic-gate 	char *device;
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	iflg = 0;
760Sstevel@tonic-gate 	pflg = 0;
770Sstevel@tonic-gate 	errflg = 0;
780Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "i:p:")) != EOF) {
790Sstevel@tonic-gate 		switch (c) {
800Sstevel@tonic-gate 			case 'i':
810Sstevel@tonic-gate 				iflg++;
820Sstevel@tonic-gate 				device = optarg;
830Sstevel@tonic-gate 				break;
840Sstevel@tonic-gate 			case 'p':
850Sstevel@tonic-gate 				pflg++;
860Sstevel@tonic-gate 				device = optarg;
870Sstevel@tonic-gate 				break;
880Sstevel@tonic-gate 			case '?':
890Sstevel@tonic-gate 				errflg++;
900Sstevel@tonic-gate 				break;
910Sstevel@tonic-gate 			default:
920Sstevel@tonic-gate 				errflg++;
930Sstevel@tonic-gate 				break;
940Sstevel@tonic-gate 		}
950Sstevel@tonic-gate 		if (errflg)
960Sstevel@tonic-gate 			usage();
970Sstevel@tonic-gate 	}
980Sstevel@tonic-gate 	if ((optind > argc) || (optind == 1) || (pflg && iflg))
990Sstevel@tonic-gate 		usage();
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	if ((fd = open(device, O_RDONLY)) < 0) {
1020Sstevel@tonic-gate 		(void) fprintf(stderr, "devinfo: %s: %s\n",
1030Sstevel@tonic-gate 			device, strerror(errno));
1040Sstevel@tonic-gate 		exit(OPENERR);
1050Sstevel@tonic-gate 	}
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	if (iflg) {
1080Sstevel@tonic-gate 		if (ioctl(fd, DKIOCGGEOM, &geom) == -1) {
1090Sstevel@tonic-gate 			if (errno == ENOTSUP) {
1100Sstevel@tonic-gate 				(void) warn(device,
1110Sstevel@tonic-gate "This operation is not supported on EFI labeled devices");
1120Sstevel@tonic-gate 			} else {
1130Sstevel@tonic-gate 				(void) warn(device,
1140Sstevel@tonic-gate "Unable to read Disk geometry");
1150Sstevel@tonic-gate 			}
1160Sstevel@tonic-gate 			(void) close(fd);
1170Sstevel@tonic-gate 			exit(DRERR);
1180Sstevel@tonic-gate 		}
1190Sstevel@tonic-gate 		devinfo(&geom, fd, device);
1200Sstevel@tonic-gate 	}
1210Sstevel@tonic-gate 	if (pflg)
1220Sstevel@tonic-gate 		partinfo(fd, device);
1230Sstevel@tonic-gate 	(void) close(fd);
1240Sstevel@tonic-gate 	return (0);
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate static void
partinfo(int fd,char * device)1280Sstevel@tonic-gate partinfo(int fd, char *device)
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate 	int i;
1310Sstevel@tonic-gate 	int	slice;
1320Sstevel@tonic-gate 	major_t maj;
1330Sstevel@tonic-gate 	minor_t min;
1340Sstevel@tonic-gate 	struct stat64 statbuf;
135*7563SPrasad.Singamsetty@Sun.COM 	struct extvtoc vtdata;
1360Sstevel@tonic-gate 	struct dk_gpt *efi;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	i = stat64(device, &statbuf);
1390Sstevel@tonic-gate 	if (i < 0)
1400Sstevel@tonic-gate 		exit(DRERR);
1410Sstevel@tonic-gate 	maj = major(statbuf.st_rdev);
1420Sstevel@tonic-gate 	min = minor(statbuf.st_rdev);
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	if ((slice = readvtoc(fd, device, &vtdata)) >= 0) {
1450Sstevel@tonic-gate 
146*7563SPrasad.Singamsetty@Sun.COM 		(void) printf("%s\t%0lx\t%0lx\t%llu\t%llu\t%x\t%x\n",
1470Sstevel@tonic-gate 			device, maj, min,
1480Sstevel@tonic-gate 			vtdata.v_part[slice].p_start,
1490Sstevel@tonic-gate 			vtdata.v_part[slice].p_size,
1500Sstevel@tonic-gate 			vtdata.v_part[slice].p_flag,
1510Sstevel@tonic-gate 			vtdata.v_part[slice].p_tag);
1520Sstevel@tonic-gate 	} else if ((slice == VT_ENOTSUP) &&
1530Sstevel@tonic-gate 	    (slice = efi_alloc_and_read(fd, &efi)) >= 0) {
1541871Ssommerfe 		(void) printf("%s\t%lx\t%lx\t%lld\t%lld\t%hx\t%hx\n",
1550Sstevel@tonic-gate 			device, maj, min,
1560Sstevel@tonic-gate 			efi->efi_parts[slice].p_start,
1570Sstevel@tonic-gate 			efi->efi_parts[slice].p_size,
1580Sstevel@tonic-gate 			efi->efi_parts[slice].p_flag,
1590Sstevel@tonic-gate 			efi->efi_parts[slice].p_tag);
1600Sstevel@tonic-gate 	} else {
1610Sstevel@tonic-gate 		exit(DRERR);
1620Sstevel@tonic-gate 	}
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate static void
devinfo(struct dk_geom * geom,int fd,char * device)1660Sstevel@tonic-gate devinfo(struct dk_geom *geom, int fd, char *device)
1670Sstevel@tonic-gate {
1680Sstevel@tonic-gate 	int i;
1690Sstevel@tonic-gate 	unsigned int nopartitions, sectorcyl, bytes;
170*7563SPrasad.Singamsetty@Sun.COM 	struct extvtoc vtdata;
1710Sstevel@tonic-gate /*
1720Sstevel@tonic-gate  *	unsigned int version = 0;
1730Sstevel@tonic-gate  *	unsigned int driveid = 0;
1740Sstevel@tonic-gate  */
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	nopartitions = 0;
1770Sstevel@tonic-gate 	sectorcyl = 0;
1780Sstevel@tonic-gate 	bytes = 0;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	if (readvtoc(fd, device, &vtdata) < 0)
1810Sstevel@tonic-gate 		exit(DRERR);
1820Sstevel@tonic-gate 	sectorcyl = geom->dkg_nhead  *  geom->dkg_nsect;
1830Sstevel@tonic-gate 	bytes = vtdata.v_sectorsz;
1840Sstevel@tonic-gate /*
1850Sstevel@tonic-gate  *	these are not supported by Sun.
1860Sstevel@tonic-gate  *
1870Sstevel@tonic-gate  *	driveid = osect0->newsect0.pdinfo.driveid;
1880Sstevel@tonic-gate  *	version = osect0->newsect0.pdinfo.version;
1890Sstevel@tonic-gate  */
1900Sstevel@tonic-gate 	for (i = 0; i < V_NUMPAR; i++)	{
1910Sstevel@tonic-gate 		if (vtdata.v_part[i].p_size != 0x00)
1920Sstevel@tonic-gate 			nopartitions++;
1930Sstevel@tonic-gate 	}
1940Sstevel@tonic-gate /*
1950Sstevel@tonic-gate  *	(void) printf("%s	%0x	%0x	%d	%d	%d\n",
1960Sstevel@tonic-gate  *		device, version, driveid, sectorcyl, bytes, nopartitions);
1970Sstevel@tonic-gate  */
1980Sstevel@tonic-gate 	(void) printf("%s	%0x	%0x	%d	%d	%d\n",
1990Sstevel@tonic-gate 		device, 0, 0, sectorcyl, bytes, nopartitions);
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate /*
2040Sstevel@tonic-gate  * readvtoc()
2050Sstevel@tonic-gate  *
2060Sstevel@tonic-gate  * Read a partition map.
2070Sstevel@tonic-gate  */
2080Sstevel@tonic-gate static int
readvtoc(int fd,char * name,struct extvtoc * vtoc)209*7563SPrasad.Singamsetty@Sun.COM readvtoc(int fd, char *name, struct extvtoc *vtoc)
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate 	int	retval;
2120Sstevel@tonic-gate 
213*7563SPrasad.Singamsetty@Sun.COM 	retval = read_extvtoc(fd, vtoc);
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	switch (retval) {
2160Sstevel@tonic-gate 		case (VT_ERROR):
2170Sstevel@tonic-gate 			return (warn(name, strerror(errno)));
2180Sstevel@tonic-gate 		case (VT_EIO):
2190Sstevel@tonic-gate 			return (warn(name, "I/O error accessing VTOC"));
2200Sstevel@tonic-gate 		case (VT_EINVAL):
2210Sstevel@tonic-gate 			return (warn(name, "Invalid field in VTOC"));
2220Sstevel@tonic-gate 		}
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	return (retval);
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate /*
2290Sstevel@tonic-gate  * warn()
2300Sstevel@tonic-gate  *
2310Sstevel@tonic-gate  * Print an error message. Always returns -1.
2320Sstevel@tonic-gate  */
2330Sstevel@tonic-gate static int
warn(char * what,char * why)2340Sstevel@tonic-gate warn(char *what, char *why)
2350Sstevel@tonic-gate {
2360Sstevel@tonic-gate 	static char	myname[]  = "devinfo";
2370Sstevel@tonic-gate 	static char	between[] = ": ";
2380Sstevel@tonic-gate 	static char	after[]   = "\n";
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	(void) write(STDERR, myname, (uint_t)strlen(myname));
2410Sstevel@tonic-gate 	(void) write(STDERR, between, (uint_t)strlen(between));
2420Sstevel@tonic-gate 	(void) write(STDERR, what, (uint_t)strlen(what));
2430Sstevel@tonic-gate 	(void) write(STDERR, between, (uint_t)strlen(between));
2440Sstevel@tonic-gate 	(void) write(STDERR, why, (uint_t)strlen(why));
2450Sstevel@tonic-gate 	(void) write(STDERR, after, (uint_t)strlen(after));
2460Sstevel@tonic-gate 	return (-1);
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate static void
usage(void)2500Sstevel@tonic-gate usage(void)
2510Sstevel@tonic-gate {
2520Sstevel@tonic-gate 	(void) fprintf(stderr, "Usage: devinfo -p device\n"
2530Sstevel@tonic-gate 		"       devinfo -i device \n");
2540Sstevel@tonic-gate 	exit(2);
2550Sstevel@tonic-gate }
256