xref: /titanic_53/usr/src/cmd/devinfo/devinfo.c (revision 0d8b53344490faab75b127936edb53a2f1e0dc01)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*0d8b5334Sceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
30*0d8b5334Sceastha #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  *	Two output fields under the -i option will always be
347c478bd9Sstevel@tonic-gate  *	output as zero, since they are not supported by Sun:
357c478bd9Sstevel@tonic-gate  *		Software version, and
367c478bd9Sstevel@tonic-gate  *		Drive id number.
377c478bd9Sstevel@tonic-gate  *	AT&T filled these 2 fields with data from their "pdsector",
387c478bd9Sstevel@tonic-gate  *	which Sun doesn't support per se.
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <stdio.h>
437c478bd9Sstevel@tonic-gate #include <fcntl.h>
447c478bd9Sstevel@tonic-gate #include <stdlib.h>
457c478bd9Sstevel@tonic-gate #include <unistd.h>
467c478bd9Sstevel@tonic-gate #include <string.h>
477c478bd9Sstevel@tonic-gate #include <sys/types.h>
487c478bd9Sstevel@tonic-gate #include <sys/stat.h>
497c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
507c478bd9Sstevel@tonic-gate #include <sys/efi_partition.h>
517c478bd9Sstevel@tonic-gate #include <sys/vtoc.h>
527c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
537c478bd9Sstevel@tonic-gate #include <errno.h>
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #define	DRERR	2
567c478bd9Sstevel@tonic-gate #define	OPENERR	2
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate  * Standard I/O file descriptors.
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate #define	STDOUT	1		/* Standard output */
627c478bd9Sstevel@tonic-gate #define	STDERR	2		/* Standard error */
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate static	void	partinfo(int fd, char *device);
657c478bd9Sstevel@tonic-gate static	void	devinfo(struct dk_geom *geom, int fd, char *device);
667c478bd9Sstevel@tonic-gate static	int	readvtoc(int fd, char *name, struct vtoc *vtoc);
677c478bd9Sstevel@tonic-gate static	int	warn(char *what, char *why);
687c478bd9Sstevel@tonic-gate static	void	usage(void);
697c478bd9Sstevel@tonic-gate 
70*0d8b5334Sceastha int
717c478bd9Sstevel@tonic-gate main(int argc, char **argv)
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate 	struct dk_geom  geom;
747c478bd9Sstevel@tonic-gate 	int errflg, iflg, pflg, fd, c;
757c478bd9Sstevel@tonic-gate 	char *device;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	iflg = 0;
787c478bd9Sstevel@tonic-gate 	pflg = 0;
797c478bd9Sstevel@tonic-gate 	errflg = 0;
807c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "i:p:")) != EOF) {
817c478bd9Sstevel@tonic-gate 		switch (c) {
827c478bd9Sstevel@tonic-gate 			case 'i':
837c478bd9Sstevel@tonic-gate 				iflg++;
847c478bd9Sstevel@tonic-gate 				device = optarg;
857c478bd9Sstevel@tonic-gate 				break;
867c478bd9Sstevel@tonic-gate 			case 'p':
877c478bd9Sstevel@tonic-gate 				pflg++;
887c478bd9Sstevel@tonic-gate 				device = optarg;
897c478bd9Sstevel@tonic-gate 				break;
907c478bd9Sstevel@tonic-gate 			case '?':
917c478bd9Sstevel@tonic-gate 				errflg++;
927c478bd9Sstevel@tonic-gate 				break;
937c478bd9Sstevel@tonic-gate 			default:
947c478bd9Sstevel@tonic-gate 				errflg++;
957c478bd9Sstevel@tonic-gate 				break;
967c478bd9Sstevel@tonic-gate 		}
977c478bd9Sstevel@tonic-gate 		if (errflg)
987c478bd9Sstevel@tonic-gate 			usage();
997c478bd9Sstevel@tonic-gate 	}
1007c478bd9Sstevel@tonic-gate 	if ((optind > argc) || (optind == 1) || (pflg && iflg))
1017c478bd9Sstevel@tonic-gate 		usage();
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	if ((fd = open(device, O_RDONLY)) < 0) {
1047c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "devinfo: %s: %s\n",
1057c478bd9Sstevel@tonic-gate 			device, strerror(errno));
1067c478bd9Sstevel@tonic-gate 		exit(OPENERR);
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	if (iflg) {
1107c478bd9Sstevel@tonic-gate 		if (ioctl(fd, DKIOCGGEOM, &geom) == -1) {
1117c478bd9Sstevel@tonic-gate 			if (errno == ENOTSUP) {
1127c478bd9Sstevel@tonic-gate 				(void) warn(device,
1137c478bd9Sstevel@tonic-gate "This operation is not supported on EFI labeled devices");
1147c478bd9Sstevel@tonic-gate 			} else {
1157c478bd9Sstevel@tonic-gate 				(void) warn(device,
1167c478bd9Sstevel@tonic-gate "Unable to read Disk geometry");
1177c478bd9Sstevel@tonic-gate 			}
1187c478bd9Sstevel@tonic-gate 			(void) close(fd);
1197c478bd9Sstevel@tonic-gate 			exit(DRERR);
1207c478bd9Sstevel@tonic-gate 		}
1217c478bd9Sstevel@tonic-gate 		devinfo(&geom, fd, device);
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 	if (pflg)
1247c478bd9Sstevel@tonic-gate 		partinfo(fd, device);
1257c478bd9Sstevel@tonic-gate 	(void) close(fd);
1267c478bd9Sstevel@tonic-gate 	return (0);
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate static void
1307c478bd9Sstevel@tonic-gate partinfo(int fd, char *device)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate 	int i;
1337c478bd9Sstevel@tonic-gate 	int	slice;
1347c478bd9Sstevel@tonic-gate 	major_t maj;
1357c478bd9Sstevel@tonic-gate 	minor_t min;
1367c478bd9Sstevel@tonic-gate 	struct stat64 statbuf;
1377c478bd9Sstevel@tonic-gate 	struct vtoc vtdata;
1387c478bd9Sstevel@tonic-gate 	struct dk_gpt *efi;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	i = stat64(device, &statbuf);
1417c478bd9Sstevel@tonic-gate 	if (i < 0)
1427c478bd9Sstevel@tonic-gate 		exit(DRERR);
1437c478bd9Sstevel@tonic-gate 	maj = major(statbuf.st_rdev);
1447c478bd9Sstevel@tonic-gate 	min = minor(statbuf.st_rdev);
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	if ((slice = readvtoc(fd, device, &vtdata)) >= 0) {
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 		(void) printf("%s\t%0lx	%0lx\t%ld\t%ld\t%x\t%x\n",
1497c478bd9Sstevel@tonic-gate 			device, maj, min,
1507c478bd9Sstevel@tonic-gate 			vtdata.v_part[slice].p_start,
1517c478bd9Sstevel@tonic-gate 			vtdata.v_part[slice].p_size,
1527c478bd9Sstevel@tonic-gate 			vtdata.v_part[slice].p_flag,
1537c478bd9Sstevel@tonic-gate 			vtdata.v_part[slice].p_tag);
1547c478bd9Sstevel@tonic-gate 	} else if ((slice == VT_ENOTSUP) &&
1557c478bd9Sstevel@tonic-gate 	    (slice = efi_alloc_and_read(fd, &efi)) >= 0) {
1567c478bd9Sstevel@tonic-gate 		(void) printf("%s\t%lx  %lx\t%lld\t%lld\t%hx\t%hx\n",
1577c478bd9Sstevel@tonic-gate 			device, maj, min,
1587c478bd9Sstevel@tonic-gate 			efi->efi_parts[slice].p_start,
1597c478bd9Sstevel@tonic-gate 			efi->efi_parts[slice].p_size,
1607c478bd9Sstevel@tonic-gate 			efi->efi_parts[slice].p_flag,
1617c478bd9Sstevel@tonic-gate 			efi->efi_parts[slice].p_tag);
1627c478bd9Sstevel@tonic-gate 	} else {
1637c478bd9Sstevel@tonic-gate 		exit(DRERR);
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate static void
1687c478bd9Sstevel@tonic-gate devinfo(struct dk_geom *geom, int fd, char *device)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	int i;
1717c478bd9Sstevel@tonic-gate 	unsigned int nopartitions, sectorcyl, bytes;
1727c478bd9Sstevel@tonic-gate 	struct vtoc vtdata;
1737c478bd9Sstevel@tonic-gate /*
1747c478bd9Sstevel@tonic-gate  *	unsigned int version = 0;
1757c478bd9Sstevel@tonic-gate  *	unsigned int driveid = 0;
1767c478bd9Sstevel@tonic-gate  */
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	nopartitions = 0;
1797c478bd9Sstevel@tonic-gate 	sectorcyl = 0;
1807c478bd9Sstevel@tonic-gate 	bytes = 0;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	if (readvtoc(fd, device, &vtdata) < 0)
1837c478bd9Sstevel@tonic-gate 		exit(DRERR);
1847c478bd9Sstevel@tonic-gate 	sectorcyl = geom->dkg_nhead  *  geom->dkg_nsect;
1857c478bd9Sstevel@tonic-gate 	bytes = vtdata.v_sectorsz;
1867c478bd9Sstevel@tonic-gate /*
1877c478bd9Sstevel@tonic-gate  *	these are not supported by Sun.
1887c478bd9Sstevel@tonic-gate  *
1897c478bd9Sstevel@tonic-gate  *	driveid = osect0->newsect0.pdinfo.driveid;
1907c478bd9Sstevel@tonic-gate  *	version = osect0->newsect0.pdinfo.version;
1917c478bd9Sstevel@tonic-gate  */
1927c478bd9Sstevel@tonic-gate 	for (i = 0; i < V_NUMPAR; i++)	{
1937c478bd9Sstevel@tonic-gate 		if (vtdata.v_part[i].p_size != 0x00)
1947c478bd9Sstevel@tonic-gate 			nopartitions++;
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate /*
1977c478bd9Sstevel@tonic-gate  *	(void) printf("%s	%0x	%0x	%d	%d	%d\n",
1987c478bd9Sstevel@tonic-gate  *		device, version, driveid, sectorcyl, bytes, nopartitions);
1997c478bd9Sstevel@tonic-gate  */
2007c478bd9Sstevel@tonic-gate 	(void) printf("%s	%0x	%0x	%d	%d	%d\n",
2017c478bd9Sstevel@tonic-gate 		device, 0, 0, sectorcyl, bytes, nopartitions);
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate /*
2067c478bd9Sstevel@tonic-gate  * readvtoc()
2077c478bd9Sstevel@tonic-gate  *
2087c478bd9Sstevel@tonic-gate  * Read a partition map.
2097c478bd9Sstevel@tonic-gate  */
2107c478bd9Sstevel@tonic-gate static int
2117c478bd9Sstevel@tonic-gate readvtoc(int fd, char *name, struct vtoc *vtoc)
2127c478bd9Sstevel@tonic-gate {
2137c478bd9Sstevel@tonic-gate 	int	retval;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	retval = read_vtoc(fd, vtoc);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	switch (retval) {
2187c478bd9Sstevel@tonic-gate 		case (VT_ERROR):
2197c478bd9Sstevel@tonic-gate 			return (warn(name, strerror(errno)));
2207c478bd9Sstevel@tonic-gate 		case (VT_EIO):
2217c478bd9Sstevel@tonic-gate 			return (warn(name, "I/O error accessing VTOC"));
2227c478bd9Sstevel@tonic-gate 		case (VT_EINVAL):
2237c478bd9Sstevel@tonic-gate 			return (warn(name, "Invalid field in VTOC"));
2247c478bd9Sstevel@tonic-gate 		}
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	return (retval);
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate /*
2317c478bd9Sstevel@tonic-gate  * warn()
2327c478bd9Sstevel@tonic-gate  *
2337c478bd9Sstevel@tonic-gate  * Print an error message. Always returns -1.
2347c478bd9Sstevel@tonic-gate  */
2357c478bd9Sstevel@tonic-gate static int
2367c478bd9Sstevel@tonic-gate warn(char *what, char *why)
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate 	static char	myname[]  = "devinfo";
2397c478bd9Sstevel@tonic-gate 	static char	between[] = ": ";
2407c478bd9Sstevel@tonic-gate 	static char	after[]   = "\n";
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	(void) write(STDERR, myname, (uint_t)strlen(myname));
2437c478bd9Sstevel@tonic-gate 	(void) write(STDERR, between, (uint_t)strlen(between));
2447c478bd9Sstevel@tonic-gate 	(void) write(STDERR, what, (uint_t)strlen(what));
2457c478bd9Sstevel@tonic-gate 	(void) write(STDERR, between, (uint_t)strlen(between));
2467c478bd9Sstevel@tonic-gate 	(void) write(STDERR, why, (uint_t)strlen(why));
2477c478bd9Sstevel@tonic-gate 	(void) write(STDERR, after, (uint_t)strlen(after));
2487c478bd9Sstevel@tonic-gate 	return (-1);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate static void
2527c478bd9Sstevel@tonic-gate usage(void)
2537c478bd9Sstevel@tonic-gate {
2547c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "Usage: devinfo -p device\n"
2557c478bd9Sstevel@tonic-gate 		"       devinfo -i device \n");
2567c478bd9Sstevel@tonic-gate 	exit(2);
2577c478bd9Sstevel@tonic-gate }
258