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