1 /* $OpenBSD: disk.c,v 1.42 2014/03/31 22:03:29 krw Exp $ */ 2 3 /* 4 * Copyright (c) 1997, 2001 Tobias Weingartner 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <sys/types.h> 29 #include <sys/fcntl.h> 30 #include <sys/ioctl.h> 31 #include <sys/dkio.h> 32 #include <sys/stdint.h> 33 #include <sys/stat.h> 34 #include <sys/disklabel.h> 35 #include <err.h> 36 #include <util.h> 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <unistd.h> 40 41 #include "disk.h" 42 #include "misc.h" 43 44 struct disklabel dl; 45 46 int 47 DISK_open(char *disk, int mode) 48 { 49 int fd; 50 struct stat st; 51 52 fd = opendev(disk, mode, OPENDEV_PART, NULL); 53 if (fd == -1) 54 err(1, "%s", disk); 55 if (fstat(fd, &st) == -1) 56 err(1, "%s", disk); 57 if (!S_ISCHR(st.st_mode) && !S_ISREG(st.st_mode)) 58 errx(1, "%s is not a character device or a regular file", disk); 59 return (fd); 60 } 61 62 void 63 DISK_getlabelgeometry(struct disk *disk) 64 { 65 u_int64_t sz, spc; 66 int fd; 67 68 /* Get label geometry. */ 69 if ((fd = DISK_open(disk->name, O_RDONLY)) != -1) { 70 if (ioctl(fd, DIOCGPDINFO, &dl) == -1) { 71 warn("DIOCGPDINFO"); 72 } else { 73 disk->cylinders = dl.d_ncylinders; 74 disk->heads = dl.d_ntracks; 75 disk->sectors = dl.d_nsectors; 76 /* MBR handles only first UINT32_MAX sectors. */ 77 spc = (u_int64_t)disk->heads * disk->sectors; 78 sz = DL_GETDSIZE(&dl); 79 if (sz > UINT32_MAX) { 80 disk->cylinders = UINT32_MAX / spc; 81 disk->size = disk->cylinders * spc; 82 warnx("disk too large (%llu sectors)." 83 " size truncated.", sz); 84 } else 85 disk->size = sz; 86 unit_types[SECTORS].conversion = dl.d_secsize; 87 } 88 close(fd); 89 } 90 } 91 92 /* 93 * Print the disk geometry information. Take an optional modifier 94 * to indicate the units that should be used for display. 95 */ 96 int 97 DISK_printgeometry(struct disk *disk, char *units) 98 { 99 const int secsize = unit_types[SECTORS].conversion; 100 double size; 101 int i; 102 103 i = unit_lookup(units); 104 size = ((double)disk->size * secsize) / unit_types[i].conversion; 105 printf("Disk: %s\t", disk->name); 106 if (disk->size) { 107 printf("geometry: %d/%d/%d [%.0f ", disk->cylinders, 108 disk->heads, disk->sectors, size); 109 if (i == SECTORS && secsize != sizeof(struct dos_mbr)) 110 printf("%d-byte ", secsize); 111 printf("%s]\n", unit_types[i].lname); 112 } else 113 printf("geometry: <none>\n"); 114 115 return (0); 116 } 117