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