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