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