xref: /openbsd-src/lib/libc/gen/disklabel.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*
2  * Copyright (c) 1983, 1987, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char rcsid[] = "$OpenBSD: disklabel.c,v 1.9 2003/06/11 21:03:10 deraadt Exp $";
32 #endif /* LIBC_SCCS and not lint */
33 
34 #include <sys/param.h>
35 #define DKTYPENAMES
36 #include <sys/disklabel.h>
37 #include <ufs/ffs/fs.h>
38 
39 #include <ctype.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 
47 static int	gettype(char *, char **);
48 
49 struct disklabel *
50 getdiskbyname(const char *name)
51 {
52 	static struct	disklabel disk;
53 	register struct	disklabel *dp = &disk;
54 	register struct partition *pp;
55 	char	*buf;
56 	char  	*db_array[2] = { _PATH_DISKTAB, 0 };
57 	char	*cp, *cq;	/* can't be register */
58 	char	p, max, psize[3], pbsize[3],
59 		pfsize[3], poffset[3], ptype[3];
60 	u_int32_t *dx;
61 
62 	if (cgetent(&buf, db_array, (char *) name) < 0)
63 		return NULL;
64 
65 	bzero((char *)&disk, sizeof(disk));
66 	/*
67 	 * typename
68 	 */
69 	cq = dp->d_typename;
70 	cp = buf;
71 	while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 &&
72 	    (*cq = *cp) && *cq != '|' && *cq != ':')
73 		cq++, cp++;
74 	*cq = '\0';
75 	/*
76 	 * boot name (optional)  xxboot, bootxx
77 	 */
78 	cgetstr(buf, "b0", &dp->d_boot0);
79 	cgetstr(buf, "b1", &dp->d_boot1);
80 
81 	if (cgetstr(buf, "ty", &cq) > 0 && strcmp(cq, "removable") == 0)
82 		dp->d_flags |= D_REMOVABLE;
83 	else  if (cq && strcmp(cq, "simulated") == 0)
84 		dp->d_flags |= D_RAMDISK;
85 	if (cgetcap(buf, "sf", ':') != NULL)
86 		dp->d_flags |= D_BADSECT;
87 
88 #define getnumdflt(field, dname, dflt) \
89 	{ long f; (field) = (cgetnum(buf, dname, &f) == -1) ? (dflt) : f; }
90 #define	getnum(field, dname) \
91 	{ long f; cgetnum(buf, dname, &f); field = f; }
92 
93 	getnumdflt(dp->d_secsize, "se", DEV_BSIZE);
94 	getnum(dp->d_ntracks, "nt");
95 	getnum(dp->d_nsectors, "ns");
96 	getnum(dp->d_ncylinders, "nc");
97 
98 	if (cgetstr(buf, "dt", &cq) > 0)
99 		dp->d_type = gettype(cq, dktypenames);
100 	else
101 		getnumdflt(dp->d_type, "dt", 0);
102 	getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks);
103 	getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders);
104 	getnumdflt(dp->d_rpm, "rm", 3600);
105 	getnumdflt(dp->d_interleave, "il", 1);
106 	getnumdflt(dp->d_trackskew, "sk", 0);
107 	getnumdflt(dp->d_cylskew, "cs", 0);
108 	getnumdflt(dp->d_headswitch, "hs", 0);
109 	getnumdflt(dp->d_trkseek, "ts", 0);
110 	getnumdflt(dp->d_bbsize, "bs", BBSIZE);
111 	getnumdflt(dp->d_sbsize, "sb", SBSIZE);
112 	strlcpy(psize, "px", sizeof psize);
113 	strlcpy(pbsize, "bx", sizeof pbsize);
114 	strlcpy(pfsize, "fx", sizeof pfsize);
115 	strlcpy(poffset, "ox", sizeof poffset);
116 	strlcpy(ptype, "tx", sizeof ptype);
117 	max = 'a' - 1;
118 	pp = &dp->d_partitions[0];
119 	for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) {
120 		long f;
121 
122 		psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p;
123 		if (cgetnum(buf, psize, &f) == -1)
124 			pp->p_size = 0;
125 		else {
126 			pp->p_size = f;
127 			getnum(pp->p_offset, poffset);
128 			getnumdflt(pp->p_fsize, pfsize, 0);
129 			if (pp->p_fsize) {
130 				long bsize;
131 
132 				if (cgetnum(buf, pbsize, &bsize) == 0)
133 					pp->p_frag = bsize / pp->p_fsize;
134 				else
135 					pp->p_frag = 8;
136 			}
137 			getnumdflt(pp->p_fstype, ptype, 0);
138 			if (pp->p_fstype == 0 && cgetstr(buf, ptype, &cq) > 0)
139 				pp->p_fstype = gettype(cq, fstypenames);
140 			max = p;
141 		}
142 	}
143 	dp->d_npartitions = max + 1 - 'a';
144 	(void)strlcpy(psize, "dx", sizeof psize);
145 	dx = dp->d_drivedata;
146 	for (p = '0'; p < '0' + NDDATA; p++, dx++) {
147 		psize[1] = p;
148 		getnumdflt(*dx, psize, 0);
149 	}
150 	dp->d_magic = DISKMAGIC;
151 	dp->d_magic2 = DISKMAGIC;
152 	free(buf);
153 	return (dp);
154 }
155 
156 static int
157 gettype(char *t, char **names)
158 {
159 	register char **nm;
160 
161 	for (nm = names; *nm; nm++)
162 		if (strcasecmp(t, *nm) == 0)
163 			return (nm - names);
164 	if (isdigit((u_char)*t))
165 		return (atoi(t));
166 	return (0);
167 }
168