xref: /titanic_53/usr/src/cmd/devinfo/devinfo.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate /*
32*7c478bd9Sstevel@tonic-gate  *	Two output fields under the -i option will always be
33*7c478bd9Sstevel@tonic-gate  *	output as zero, since they are not supported by Sun:
34*7c478bd9Sstevel@tonic-gate  *		Software version, and
35*7c478bd9Sstevel@tonic-gate  *		Drive id number.
36*7c478bd9Sstevel@tonic-gate  *	AT&T filled these 2 fields with data from their "pdsector",
37*7c478bd9Sstevel@tonic-gate  *	which Sun doesn't support per se.
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #include <stdio.h>
43*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
44*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
45*7c478bd9Sstevel@tonic-gate #include <unistd.h>
46*7c478bd9Sstevel@tonic-gate #include <string.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/efi_partition.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/vtoc.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
53*7c478bd9Sstevel@tonic-gate #include <errno.h>
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate #define	DRERR	2
56*7c478bd9Sstevel@tonic-gate #define	OPENERR	2
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate /*
59*7c478bd9Sstevel@tonic-gate  * Standard I/O file descriptors.
60*7c478bd9Sstevel@tonic-gate  */
61*7c478bd9Sstevel@tonic-gate #define	STDOUT	1		/* Standard output */
62*7c478bd9Sstevel@tonic-gate #define	STDERR	2		/* Standard error */
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate static	void	partinfo(int fd, char *device);
65*7c478bd9Sstevel@tonic-gate static	void	devinfo(struct dk_geom *geom, int fd, char *device);
66*7c478bd9Sstevel@tonic-gate static	int	readvtoc(int fd, char *name, struct vtoc *vtoc);
67*7c478bd9Sstevel@tonic-gate static	int	warn(char *what, char *why);
68*7c478bd9Sstevel@tonic-gate static	void	usage(void);
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
71*7c478bd9Sstevel@tonic-gate {
72*7c478bd9Sstevel@tonic-gate 	struct dk_geom  geom;
73*7c478bd9Sstevel@tonic-gate 	int errflg, iflg, pflg, fd, c;
74*7c478bd9Sstevel@tonic-gate 	char *device;
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 	iflg = 0;
77*7c478bd9Sstevel@tonic-gate 	pflg = 0;
78*7c478bd9Sstevel@tonic-gate 	errflg = 0;
79*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "i:p:")) != EOF) {
80*7c478bd9Sstevel@tonic-gate 		switch (c) {
81*7c478bd9Sstevel@tonic-gate 			case 'i':
82*7c478bd9Sstevel@tonic-gate 				iflg++;
83*7c478bd9Sstevel@tonic-gate 				device = optarg;
84*7c478bd9Sstevel@tonic-gate 				break;
85*7c478bd9Sstevel@tonic-gate 			case 'p':
86*7c478bd9Sstevel@tonic-gate 				pflg++;
87*7c478bd9Sstevel@tonic-gate 				device = optarg;
88*7c478bd9Sstevel@tonic-gate 				break;
89*7c478bd9Sstevel@tonic-gate 			case '?':
90*7c478bd9Sstevel@tonic-gate 				errflg++;
91*7c478bd9Sstevel@tonic-gate 				break;
92*7c478bd9Sstevel@tonic-gate 			default:
93*7c478bd9Sstevel@tonic-gate 				errflg++;
94*7c478bd9Sstevel@tonic-gate 				break;
95*7c478bd9Sstevel@tonic-gate 		}
96*7c478bd9Sstevel@tonic-gate 		if (errflg)
97*7c478bd9Sstevel@tonic-gate 			usage();
98*7c478bd9Sstevel@tonic-gate 	}
99*7c478bd9Sstevel@tonic-gate 	if ((optind > argc) || (optind == 1) || (pflg && iflg))
100*7c478bd9Sstevel@tonic-gate 		usage();
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	if ((fd = open(device, O_RDONLY)) < 0) {
103*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "devinfo: %s: %s\n",
104*7c478bd9Sstevel@tonic-gate 			device, strerror(errno));
105*7c478bd9Sstevel@tonic-gate 		exit(OPENERR);
106*7c478bd9Sstevel@tonic-gate 	}
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	if (iflg) {
109*7c478bd9Sstevel@tonic-gate 		if (ioctl(fd, DKIOCGGEOM, &geom) == -1) {
110*7c478bd9Sstevel@tonic-gate 			if (errno == ENOTSUP) {
111*7c478bd9Sstevel@tonic-gate 				(void) warn(device,
112*7c478bd9Sstevel@tonic-gate "This operation is not supported on EFI labeled devices");
113*7c478bd9Sstevel@tonic-gate 			} else {
114*7c478bd9Sstevel@tonic-gate 				(void) warn(device,
115*7c478bd9Sstevel@tonic-gate "Unable to read Disk geometry");
116*7c478bd9Sstevel@tonic-gate 			}
117*7c478bd9Sstevel@tonic-gate 			(void) close(fd);
118*7c478bd9Sstevel@tonic-gate 			exit(DRERR);
119*7c478bd9Sstevel@tonic-gate 		}
120*7c478bd9Sstevel@tonic-gate 		devinfo(&geom, fd, device);
121*7c478bd9Sstevel@tonic-gate 	}
122*7c478bd9Sstevel@tonic-gate 	if (pflg)
123*7c478bd9Sstevel@tonic-gate 		partinfo(fd, device);
124*7c478bd9Sstevel@tonic-gate 	(void) close(fd);
125*7c478bd9Sstevel@tonic-gate 	return (0);
126*7c478bd9Sstevel@tonic-gate }
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate static void
129*7c478bd9Sstevel@tonic-gate partinfo(int fd, char *device)
130*7c478bd9Sstevel@tonic-gate {
131*7c478bd9Sstevel@tonic-gate 	int i;
132*7c478bd9Sstevel@tonic-gate 	int	slice;
133*7c478bd9Sstevel@tonic-gate 	major_t maj;
134*7c478bd9Sstevel@tonic-gate 	minor_t min;
135*7c478bd9Sstevel@tonic-gate 	struct stat64 statbuf;
136*7c478bd9Sstevel@tonic-gate 	struct vtoc vtdata;
137*7c478bd9Sstevel@tonic-gate 	struct dk_gpt *efi;
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	i = stat64(device, &statbuf);
140*7c478bd9Sstevel@tonic-gate 	if (i < 0)
141*7c478bd9Sstevel@tonic-gate 		exit(DRERR);
142*7c478bd9Sstevel@tonic-gate 	maj = major(statbuf.st_rdev);
143*7c478bd9Sstevel@tonic-gate 	min = minor(statbuf.st_rdev);
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 	if ((slice = readvtoc(fd, device, &vtdata)) >= 0) {
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 		(void) printf("%s\t%0lx	%0lx\t%ld\t%ld\t%x\t%x\n",
148*7c478bd9Sstevel@tonic-gate 			device, maj, min,
149*7c478bd9Sstevel@tonic-gate 			vtdata.v_part[slice].p_start,
150*7c478bd9Sstevel@tonic-gate 			vtdata.v_part[slice].p_size,
151*7c478bd9Sstevel@tonic-gate 			vtdata.v_part[slice].p_flag,
152*7c478bd9Sstevel@tonic-gate 			vtdata.v_part[slice].p_tag);
153*7c478bd9Sstevel@tonic-gate 	} else if ((slice == VT_ENOTSUP) &&
154*7c478bd9Sstevel@tonic-gate 	    (slice = efi_alloc_and_read(fd, &efi)) >= 0) {
155*7c478bd9Sstevel@tonic-gate 		(void) printf("%s\t%lx  %lx\t%lld\t%lld\t%hx\t%hx\n",
156*7c478bd9Sstevel@tonic-gate 			device, maj, min,
157*7c478bd9Sstevel@tonic-gate 			efi->efi_parts[slice].p_start,
158*7c478bd9Sstevel@tonic-gate 			efi->efi_parts[slice].p_size,
159*7c478bd9Sstevel@tonic-gate 			efi->efi_parts[slice].p_flag,
160*7c478bd9Sstevel@tonic-gate 			efi->efi_parts[slice].p_tag);
161*7c478bd9Sstevel@tonic-gate 	} else {
162*7c478bd9Sstevel@tonic-gate 		exit(DRERR);
163*7c478bd9Sstevel@tonic-gate 	}
164*7c478bd9Sstevel@tonic-gate }
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate static void
167*7c478bd9Sstevel@tonic-gate devinfo(struct dk_geom *geom, int fd, char *device)
168*7c478bd9Sstevel@tonic-gate {
169*7c478bd9Sstevel@tonic-gate 	int i;
170*7c478bd9Sstevel@tonic-gate 	unsigned int nopartitions, sectorcyl, bytes;
171*7c478bd9Sstevel@tonic-gate 	struct vtoc vtdata;
172*7c478bd9Sstevel@tonic-gate /*
173*7c478bd9Sstevel@tonic-gate  *	unsigned int version = 0;
174*7c478bd9Sstevel@tonic-gate  *	unsigned int driveid = 0;
175*7c478bd9Sstevel@tonic-gate  */
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 	nopartitions = 0;
178*7c478bd9Sstevel@tonic-gate 	sectorcyl = 0;
179*7c478bd9Sstevel@tonic-gate 	bytes = 0;
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	if (readvtoc(fd, device, &vtdata) < 0)
182*7c478bd9Sstevel@tonic-gate 		exit(DRERR);
183*7c478bd9Sstevel@tonic-gate 	sectorcyl = geom->dkg_nhead  *  geom->dkg_nsect;
184*7c478bd9Sstevel@tonic-gate 	bytes = vtdata.v_sectorsz;
185*7c478bd9Sstevel@tonic-gate /*
186*7c478bd9Sstevel@tonic-gate  *	these are not supported by Sun.
187*7c478bd9Sstevel@tonic-gate  *
188*7c478bd9Sstevel@tonic-gate  *	driveid = osect0->newsect0.pdinfo.driveid;
189*7c478bd9Sstevel@tonic-gate  *	version = osect0->newsect0.pdinfo.version;
190*7c478bd9Sstevel@tonic-gate  */
191*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < V_NUMPAR; i++)	{
192*7c478bd9Sstevel@tonic-gate 		if (vtdata.v_part[i].p_size != 0x00)
193*7c478bd9Sstevel@tonic-gate 			nopartitions++;
194*7c478bd9Sstevel@tonic-gate 	}
195*7c478bd9Sstevel@tonic-gate /*
196*7c478bd9Sstevel@tonic-gate  *	(void) printf("%s	%0x	%0x	%d	%d	%d\n",
197*7c478bd9Sstevel@tonic-gate  *		device, version, driveid, sectorcyl, bytes, nopartitions);
198*7c478bd9Sstevel@tonic-gate  */
199*7c478bd9Sstevel@tonic-gate 	(void) printf("%s	%0x	%0x	%d	%d	%d\n",
200*7c478bd9Sstevel@tonic-gate 		device, 0, 0, sectorcyl, bytes, nopartitions);
201*7c478bd9Sstevel@tonic-gate }
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate /*
205*7c478bd9Sstevel@tonic-gate  * readvtoc()
206*7c478bd9Sstevel@tonic-gate  *
207*7c478bd9Sstevel@tonic-gate  * Read a partition map.
208*7c478bd9Sstevel@tonic-gate  */
209*7c478bd9Sstevel@tonic-gate static int
210*7c478bd9Sstevel@tonic-gate readvtoc(int fd, char *name, struct vtoc *vtoc)
211*7c478bd9Sstevel@tonic-gate {
212*7c478bd9Sstevel@tonic-gate 	int	retval;
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate 	retval = read_vtoc(fd, vtoc);
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 	switch (retval) {
217*7c478bd9Sstevel@tonic-gate 		case (VT_ERROR):
218*7c478bd9Sstevel@tonic-gate 			return (warn(name, strerror(errno)));
219*7c478bd9Sstevel@tonic-gate 		case (VT_EIO):
220*7c478bd9Sstevel@tonic-gate 			return (warn(name, "I/O error accessing VTOC"));
221*7c478bd9Sstevel@tonic-gate 		case (VT_EINVAL):
222*7c478bd9Sstevel@tonic-gate 			return (warn(name, "Invalid field in VTOC"));
223*7c478bd9Sstevel@tonic-gate 		}
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate 	return (retval);
226*7c478bd9Sstevel@tonic-gate }
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate /*
230*7c478bd9Sstevel@tonic-gate  * warn()
231*7c478bd9Sstevel@tonic-gate  *
232*7c478bd9Sstevel@tonic-gate  * Print an error message. Always returns -1.
233*7c478bd9Sstevel@tonic-gate  */
234*7c478bd9Sstevel@tonic-gate static int
235*7c478bd9Sstevel@tonic-gate warn(char *what, char *why)
236*7c478bd9Sstevel@tonic-gate {
237*7c478bd9Sstevel@tonic-gate 	static char	myname[]  = "devinfo";
238*7c478bd9Sstevel@tonic-gate 	static char	between[] = ": ";
239*7c478bd9Sstevel@tonic-gate 	static char	after[]   = "\n";
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate 	(void) write(STDERR, myname, (uint_t)strlen(myname));
242*7c478bd9Sstevel@tonic-gate 	(void) write(STDERR, between, (uint_t)strlen(between));
243*7c478bd9Sstevel@tonic-gate 	(void) write(STDERR, what, (uint_t)strlen(what));
244*7c478bd9Sstevel@tonic-gate 	(void) write(STDERR, between, (uint_t)strlen(between));
245*7c478bd9Sstevel@tonic-gate 	(void) write(STDERR, why, (uint_t)strlen(why));
246*7c478bd9Sstevel@tonic-gate 	(void) write(STDERR, after, (uint_t)strlen(after));
247*7c478bd9Sstevel@tonic-gate 	return (-1);
248*7c478bd9Sstevel@tonic-gate }
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate static void
251*7c478bd9Sstevel@tonic-gate usage(void)
252*7c478bd9Sstevel@tonic-gate {
253*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "Usage: devinfo -p device\n"
254*7c478bd9Sstevel@tonic-gate 		"       devinfo -i device \n");
255*7c478bd9Sstevel@tonic-gate 	exit(2);
256*7c478bd9Sstevel@tonic-gate }
257