xref: /dragonfly/lib/libc/gen/disktab.c (revision ec21d9fb)
1 /*
2  * Copyright (c) 2007 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
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  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 /*
35  * Copyright (c) 1983, 1987, 1993
36  *	The Regents of the University of California.  All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. Neither the name of the University nor the names of its contributors
47  *    may be used to endorse or promote products derived from this software
48  *    without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60  * SUCH DAMAGE.
61  */
62 /*
63  * $DragonFly: src/lib/libc/gen/disktab.c,v 1.11 2007/06/17 23:50:13 dillon Exp $
64  * @(#)disklabel.c     8.2 (Berkeley) 5/3/95
65  * $FreeBSD: src/lib/libc/gen/disklabel.c,v 1.9.2.1 2001/03/05 08:40:47 obrien
66  */
67 
68 #include <sys/param.h>
69 #define DKTYPENAMES
70 #include <sys/disklabel.h>
71 #include <sys/dtype.h>
72 #include <vfs/ufs/dinode.h>
73 #include <vfs/ufs/fs.h>
74 
75 #include <ctype.h>
76 #include <stdio.h>
77 #include <stdlib.h>
78 #include <stdarg.h>
79 #include <unistd.h>
80 #include <string.h>
81 #include <disktab.h>
82 
83 static int	gettype (const char *, const char **);
84 
85 struct disktab *
86 getdisktabbyname(const char *name)
87 {
88 	static struct disktab dtab;
89 	struct disktab *dt = &dtab;
90 	struct dt_partition *pp;
91 	char	*buf;
92 	char  	*db_array[2] = { _PATH_DISKTAB, 0 };
93 	char	*cp, *cq;	/* can't be register */
94 	char	p, max, psize[3], pbsize[3],
95 		pfsize[3], poffset[3], ptype[3];
96 #if 0
97 	u_int32_t *dx;
98 #endif
99 
100 	if (cgetent(&buf, db_array, (char *)name) < 0)
101 		return NULL;
102 
103 	bzero(dt, sizeof(*dt));
104 	/*
105 	 * typename
106 	 */
107 	cq = dt->d_typename;
108 	cp = buf;
109 	while (cq < dt->d_typename + sizeof(dt->d_typename) - 1 &&
110 	    (*cq = *cp) && *cq != '|' && *cq != ':')
111 		cq++, cp++;
112 	*cq = '\0';
113 
114 #define getnumdflt(field, dname, dflt) \
115         { long f; (field) = (cgetnum(buf, dname, &f) == -1) ? (dflt) : f; }
116 
117 	getnumdflt(dt->d_media_blksize, "se", DEV_BSIZE);
118 	getnumdflt(dt->d_nheads, "nt", 0);
119 	getnumdflt(dt->d_secpertrack, "ns", 0);
120 	getnumdflt(dt->d_ncylinders, "nc", 0);
121 
122 	if (cgetstr(buf, "dt", &cq) > 0)
123 		dt->d_typeid = gettype(cq, dktypenames);
124 	else
125 		getnumdflt(dt->d_typeid, "dt", 0);
126 	getnumdflt(dt->d_secpercyl, "sc", dt->d_secpertrack * dt->d_nheads);
127 	getnumdflt(dt->d_media_blocks, "su", dt->d_secpercyl * dt->d_ncylinders);
128 	getnumdflt(dt->d_rpm, "rm", 3600);
129 	getnumdflt(dt->d_interleave, "il", 1);
130 	getnumdflt(dt->d_trackskew, "sk", 0);
131 	getnumdflt(dt->d_cylskew, "cs", 0);
132 	getnumdflt(dt->d_headswitch, "hs", 0);
133 	getnumdflt(dt->d_trkseek, "ts", 0);
134 	getnumdflt(dt->d_bbsize, "bs", BBSIZE);
135 	getnumdflt(dt->d_sbsize, "sb", SBSIZE);
136 	strcpy(psize, "px");
137 	strcpy(pbsize, "bx");
138 	strcpy(pfsize, "fx");
139 	strcpy(poffset, "ox");
140 	strcpy(ptype, "tx");
141 	max = 'a' - 1;
142 	pp = &dt->d_partitions[0];
143 	for (p = 'a'; p < 'a' + MAXDTPARTITIONS; p++, pp++) {
144 		long l;
145 		psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p;
146 		if (cgetnum(buf, psize, &l) == -1) {
147 			pp->p_size = 0;
148 		} else {
149 			pp->p_size = l;
150 			cgetnum(buf, poffset, &l);
151 			pp->p_offset = l;
152 			getnumdflt(pp->p_fsize, pfsize, 0);
153 			if (pp->p_fsize) {
154 				long bsize;
155 
156 				if (cgetnum(buf, pbsize, &bsize) == 0)
157 					pp->p_frag = bsize / pp->p_fsize;
158 				else
159 					pp->p_frag = 8;
160 			}
161 			getnumdflt(pp->p_fstype, ptype, 0);
162 			if (pp->p_fstype == 0 && cgetstr(buf, ptype, &cq) > 0) {
163 				pp->p_fstype = gettype(cq, fstypenames);
164 				snprintf(pp->p_fstypestr,
165 					 sizeof(pp->p_fstypestr), "%s", cq);
166 			} else if (pp->p_fstype >= 0 &&
167 				   pp->p_fstype < FSMAXTYPES) {
168 				snprintf(pp->p_fstypestr,
169 					 sizeof(pp->p_fstypestr), "%s",
170 					 fstypenames[pp->p_fstype]);
171 			}
172 			max = p;
173 		}
174 	}
175 	dt->d_npartitions = max + 1 - 'a';
176 #if 0
177 	strcpy(psize, "dx");
178 	dx = dt->d_drivedata;
179 	for (p = '0'; p < '0' + NDDATA; p++, dx++) {
180 		psize[1] = p;
181 		getnumdflt(*dx, psize, 0);
182 	}
183 	dp->d_magic = DISKMAGIC;
184 	dp->d_magic2 = DISKMAGIC;
185 #endif
186 	free(buf);
187 	return (dt);
188 }
189 
190 static int
191 gettype(const char *t, const char **names)
192 {
193 	const char **nm;
194 
195 	for (nm = names; *nm; nm++)
196 		if (strcasecmp(t, *nm) == 0)
197 			return (nm - names);
198 	if (isdigit((unsigned char)*t))
199 		return (atoi(t));
200 	return (0);
201 }
202