xref: /openbsd/sbin/fdisk/disk.c (revision 898184e3)
1 /*	$OpenBSD: disk.c,v 1.34 2013/03/21 18:45:58 deraadt 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 #include "disk.h"
41 #include "misc.h"
42 
43 struct disklabel dl;
44 
45 DISK_metrics *DISK_getlabelmetrics(char *name);
46 
47 int
48 DISK_open(char *disk, int mode)
49 {
50 	int fd;
51 	struct stat st;
52 
53 	fd = opendev(disk, mode, OPENDEV_PART, NULL);
54 	if (fd == -1)
55 		err(1, "%s", disk);
56 	if (fstat(fd, &st) == -1)
57 		err(1, "%s", disk);
58 	if (!S_ISCHR(st.st_mode) && !S_ISREG(st.st_mode))
59 		err(1, "%s is not a character device or a regular file", disk);
60 	return (fd);
61 }
62 
63 /* Routine to go after the disklabel for geometry
64  * information.  This should work everywhere, but
65  * in the land of PC, things are not always what
66  * they seem.
67  */
68 DISK_metrics *
69 DISK_getlabelmetrics(char *name)
70 {
71 	DISK_metrics *lm = NULL;
72 	u_int64_t sz, spc;
73 	int fd;
74 
75 	/* Get label metrics */
76 	if ((fd = DISK_open(name, O_RDONLY)) != -1) {
77 		lm = malloc(sizeof(DISK_metrics));
78 		if (lm == NULL)
79 			err(1, NULL);
80 
81 		if (ioctl(fd, DIOCGPDINFO, &dl) == -1) {
82 			warn("DIOCGPDINFO");
83 			free(lm);
84 			lm = NULL;
85 		} else {
86 			lm->cylinders = dl.d_ncylinders;
87 			lm->heads = dl.d_ntracks;
88 			lm->sectors = dl.d_nsectors;
89 			/* MBR handles only first UINT32_MAX sectors. */
90 			spc = (u_int64_t)lm->heads * lm->sectors;
91 			sz = DL_GETDSIZE(&dl);
92 			if (sz > UINT32_MAX) {
93 				lm->cylinders = UINT32_MAX / spc;
94 				lm->size = lm->cylinders * spc;
95 				warnx("disk too large (%llu sectors)."
96 				    " size truncated.", sz);
97 			} else
98 				lm->size = sz;
99 			unit_types[SECTORS].conversion = dl.d_secsize;
100 		}
101 		close(fd);
102 	}
103 
104 	return (lm);
105 }
106 
107 /* This is ugly, and convoluted.  All the magic
108  * for disk geo/size happens here.  Basically,
109  * the real size is the one we will use in the
110  * rest of the program, the label size is what we
111  * got from the disklabel.  If the disklabel fails,
112  * we assume we are working with a normal file,
113  * and should request the user to specify the
114  * geometry he/she wishes to use.
115  */
116 int
117 DISK_getmetrics(disk_t *disk, DISK_metrics *user)
118 {
119 
120 	disk->label = DISK_getlabelmetrics(disk->name);
121 
122 	/* If user supplied, use that */
123 	if (user) {
124 		disk->real = user;
125 		return (0);
126 	}
127 
128 	/* If we have a label, use that */
129 	if (disk->label) {
130 		disk->real = disk->label;
131 		return (0);
132 	}
133 
134 	/* Can not get geometry, punt */
135 	disk->real = NULL;
136 	return (1);
137 }
138 
139 /*
140  * Print the disk geometry information. Take an optional modifier
141  * to indicate the units that should be used for display.
142  */
143 int
144 DISK_printmetrics(disk_t *disk, char *units)
145 {
146 	const int secsize = unit_types[SECTORS].conversion;
147 	double size;
148 	int i;
149 
150 	i = unit_lookup(units);
151 	size = ((double)disk->real->size * secsize) / unit_types[i].conversion;
152 	printf("Disk: %s\t", disk->name);
153 	if (disk->real) {
154 		printf("geometry: %d/%d/%d [%.0f ",
155 		    disk->real->cylinders, disk->real->heads,
156 		    disk->real->sectors, size);
157 		if (i == SECTORS && secsize != DEV_BSIZE)
158 			printf("%d-byte ", secsize);
159 		printf("%s]\n", unit_types[i].lname);
160 	} else
161 		printf("geometry: <none>\n");
162 
163 	return (0);
164 }
165 
166