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