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