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