pack_dev.c revision 1.8 1 1.8 christos /* $NetBSD: pack_dev.c,v 1.8 2004/05/11 17:09:58 christos Exp $ */
2 1.1 lukem
3 1.1 lukem /*-
4 1.1 lukem * Copyright (c) 1998, 2001 The NetBSD Foundation, Inc.
5 1.1 lukem * All rights reserved.
6 1.1 lukem *
7 1.1 lukem * This code is derived from software contributed to The NetBSD Foundation
8 1.1 lukem * by Charles M. Hannum.
9 1.1 lukem *
10 1.1 lukem * Redistribution and use in source and binary forms, with or without
11 1.1 lukem * modification, are permitted provided that the following conditions
12 1.1 lukem * are met:
13 1.1 lukem * 1. Redistributions of source code must retain the above copyright
14 1.1 lukem * notice, this list of conditions and the following disclaimer.
15 1.1 lukem * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 lukem * notice, this list of conditions and the following disclaimer in the
17 1.1 lukem * documentation and/or other materials provided with the distribution.
18 1.1 lukem * 3. All advertising materials mentioning features or use of this software
19 1.1 lukem * must display the following acknowledgement:
20 1.1 lukem * This product includes software developed by the NetBSD
21 1.1 lukem * Foundation, Inc. and its contributors.
22 1.1 lukem * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 lukem * contributors may be used to endorse or promote products derived
24 1.1 lukem * from this software without specific prior written permission.
25 1.1 lukem *
26 1.1 lukem * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 lukem * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 lukem * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 lukem * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 lukem * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 lukem * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 lukem * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 lukem * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 lukem * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 lukem * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 lukem * POSSIBILITY OF SUCH DAMAGE.
37 1.1 lukem */
38 1.1 lukem
39 1.6 lukem #if HAVE_NBTOOL_CONFIG_H
40 1.6 lukem #include "nbtool_config.h"
41 1.6 lukem #endif
42 1.6 lukem
43 1.1 lukem #include <sys/cdefs.h>
44 1.6 lukem #if !defined(lint)
45 1.8 christos __RCSID("$NetBSD: pack_dev.c,v 1.8 2004/05/11 17:09:58 christos Exp $");
46 1.1 lukem #endif /* not lint */
47 1.1 lukem
48 1.1 lukem #include <sys/types.h>
49 1.1 lukem #include <sys/stat.h>
50 1.1 lukem
51 1.1 lukem #include <limits.h>
52 1.1 lukem #include <stdio.h>
53 1.1 lukem #include <stdlib.h>
54 1.2 tv #include <string.h>
55 1.1 lukem #include <unistd.h>
56 1.1 lukem
57 1.1 lukem #include "pack_dev.h"
58 1.1 lukem
59 1.1 lukem static pack_t pack_netbsd;
60 1.1 lukem static pack_t pack_freebsd;
61 1.1 lukem static pack_t pack_8_8;
62 1.1 lukem static pack_t pack_12_20;
63 1.1 lukem static pack_t pack_14_18;
64 1.1 lukem static pack_t pack_8_24;
65 1.1 lukem static pack_t pack_bsdos;
66 1.1 lukem static int compare_format(const void *, const void *);
67 1.1 lukem
68 1.8 christos static const char iMajorError[] = "invalid major number";
69 1.8 christos static const char iMinorError[] = "invalid minor number";
70 1.8 christos static const char tooManyFields[] = "too many fields for format";
71 1.1 lukem
72 1.1 lukem /* exported */
73 1.5 christos portdev_t
74 1.8 christos pack_native(int n, u_long numbers[], const char **error)
75 1.1 lukem {
76 1.7 ross portdev_t dev = 0;
77 1.1 lukem
78 1.1 lukem if (n == 2) {
79 1.1 lukem dev = makedev(numbers[0], numbers[1]);
80 1.1 lukem if (major(dev) != numbers[0])
81 1.7 ross *error = iMajorError;
82 1.7 ross else if (minor(dev) != numbers[1])
83 1.7 ross *error = iMinorError;
84 1.1 lukem } else
85 1.7 ross *error = tooManyFields;
86 1.1 lukem return (dev);
87 1.1 lukem }
88 1.1 lukem
89 1.1 lukem
90 1.5 christos static portdev_t
91 1.8 christos pack_netbsd(int n, u_long numbers[], const char **error)
92 1.1 lukem {
93 1.7 ross portdev_t dev = 0;
94 1.1 lukem
95 1.1 lukem if (n == 2) {
96 1.1 lukem dev = makedev_netbsd(numbers[0], numbers[1]);
97 1.1 lukem if (major_netbsd(dev) != numbers[0])
98 1.7 ross *error = iMajorError;
99 1.7 ross else if (minor_netbsd(dev) != numbers[1])
100 1.7 ross *error = iMinorError;
101 1.1 lukem } else
102 1.7 ross *error = tooManyFields;
103 1.1 lukem return (dev);
104 1.1 lukem }
105 1.1 lukem
106 1.1 lukem
107 1.1 lukem #define major_freebsd(x) ((int32_t)(((x) & 0x0000ff00) >> 8))
108 1.1 lukem #define minor_freebsd(x) ((int32_t)(((x) & 0xffff00ff) >> 0))
109 1.5 christos #define makedev_freebsd(x,y) ((portdev_t)((((x) << 8) & 0x0000ff00) | \
110 1.1 lukem (((y) << 0) & 0xffff00ff)))
111 1.1 lukem
112 1.5 christos static portdev_t
113 1.8 christos pack_freebsd(int n, u_long numbers[], const char **error)
114 1.1 lukem {
115 1.7 ross portdev_t dev = 0;
116 1.1 lukem
117 1.1 lukem if (n == 2) {
118 1.1 lukem dev = makedev_freebsd(numbers[0], numbers[1]);
119 1.1 lukem if (major_freebsd(dev) != numbers[0])
120 1.7 ross *error = iMajorError;
121 1.1 lukem if (minor_freebsd(dev) != numbers[1])
122 1.7 ross *error = iMinorError;
123 1.1 lukem } else
124 1.7 ross *error = tooManyFields;
125 1.1 lukem return (dev);
126 1.1 lukem }
127 1.1 lukem
128 1.1 lukem
129 1.1 lukem #define major_8_8(x) ((int32_t)(((x) & 0x0000ff00) >> 8))
130 1.1 lukem #define minor_8_8(x) ((int32_t)(((x) & 0x000000ff) >> 0))
131 1.5 christos #define makedev_8_8(x,y) ((portdev_t)((((x) << 8) & 0x0000ff00) | \
132 1.1 lukem (((y) << 0) & 0x000000ff)))
133 1.1 lukem
134 1.5 christos static portdev_t
135 1.8 christos pack_8_8(int n, u_long numbers[], const char **error)
136 1.1 lukem {
137 1.7 ross portdev_t dev = 0;
138 1.1 lukem
139 1.1 lukem if (n == 2) {
140 1.1 lukem dev = makedev_8_8(numbers[0], numbers[1]);
141 1.1 lukem if (major_8_8(dev) != numbers[0])
142 1.7 ross *error = iMajorError;
143 1.1 lukem if (minor_8_8(dev) != numbers[1])
144 1.7 ross *error = iMinorError;
145 1.1 lukem } else
146 1.7 ross *error = tooManyFields;
147 1.1 lukem return (dev);
148 1.1 lukem }
149 1.1 lukem
150 1.1 lukem
151 1.1 lukem #define major_12_20(x) ((int32_t)(((x) & 0xfff00000) >> 20))
152 1.1 lukem #define minor_12_20(x) ((int32_t)(((x) & 0x000fffff) >> 0))
153 1.5 christos #define makedev_12_20(x,y) ((portdev_t)((((x) << 20) & 0xfff00000) | \
154 1.1 lukem (((y) << 0) & 0x000fffff)))
155 1.1 lukem
156 1.5 christos static portdev_t
157 1.8 christos pack_12_20(int n, u_long numbers[], const char **error)
158 1.1 lukem {
159 1.7 ross portdev_t dev = 0;
160 1.1 lukem
161 1.1 lukem if (n == 2) {
162 1.1 lukem dev = makedev_12_20(numbers[0], numbers[1]);
163 1.1 lukem if (major_12_20(dev) != numbers[0])
164 1.7 ross *error = iMajorError;
165 1.1 lukem if (minor_12_20(dev) != numbers[1])
166 1.7 ross *error = iMinorError;
167 1.1 lukem } else
168 1.7 ross *error = tooManyFields;
169 1.1 lukem return (dev);
170 1.1 lukem }
171 1.1 lukem
172 1.1 lukem
173 1.1 lukem #define major_14_18(x) ((int32_t)(((x) & 0xfffc0000) >> 18))
174 1.1 lukem #define minor_14_18(x) ((int32_t)(((x) & 0x0003ffff) >> 0))
175 1.5 christos #define makedev_14_18(x,y) ((portdev_t)((((x) << 18) & 0xfffc0000) | \
176 1.1 lukem (((y) << 0) & 0x0003ffff)))
177 1.1 lukem
178 1.5 christos static portdev_t
179 1.8 christos pack_14_18(int n, u_long numbers[], const char **error)
180 1.1 lukem {
181 1.7 ross portdev_t dev = 0;
182 1.1 lukem
183 1.1 lukem if (n == 2) {
184 1.1 lukem dev = makedev_14_18(numbers[0], numbers[1]);
185 1.1 lukem if (major_14_18(dev) != numbers[0])
186 1.7 ross *error = iMajorError;
187 1.1 lukem if (minor_14_18(dev) != numbers[1])
188 1.7 ross *error = iMinorError;
189 1.1 lukem } else
190 1.7 ross *error = tooManyFields;
191 1.1 lukem return (dev);
192 1.1 lukem }
193 1.1 lukem
194 1.1 lukem
195 1.1 lukem #define major_8_24(x) ((int32_t)(((x) & 0xff000000) >> 24))
196 1.1 lukem #define minor_8_24(x) ((int32_t)(((x) & 0x00ffffff) >> 0))
197 1.5 christos #define makedev_8_24(x,y) ((portdev_t)((((x) << 24) & 0xff000000) | \
198 1.1 lukem (((y) << 0) & 0x00ffffff)))
199 1.1 lukem
200 1.5 christos static portdev_t
201 1.8 christos pack_8_24(int n, u_long numbers[], const char **error)
202 1.1 lukem {
203 1.7 ross portdev_t dev = 0;
204 1.1 lukem
205 1.1 lukem if (n == 2) {
206 1.1 lukem dev = makedev_8_24(numbers[0], numbers[1]);
207 1.1 lukem if (major_8_24(dev) != numbers[0])
208 1.7 ross *error = iMajorError;
209 1.1 lukem if (minor_8_24(dev) != numbers[1])
210 1.7 ross *error = iMinorError;
211 1.1 lukem } else
212 1.7 ross *error = tooManyFields;
213 1.1 lukem return (dev);
214 1.1 lukem }
215 1.1 lukem
216 1.1 lukem
217 1.1 lukem #define major_12_12_8(x) ((int32_t)(((x) & 0xfff00000) >> 20))
218 1.1 lukem #define unit_12_12_8(x) ((int32_t)(((x) & 0x000fff00) >> 8))
219 1.1 lukem #define subunit_12_12_8(x) ((int32_t)(((x) & 0x000000ff) >> 0))
220 1.5 christos #define makedev_12_12_8(x,y,z) ((portdev_t)((((x) << 20) & 0xfff00000) | \
221 1.1 lukem (((y) << 8) & 0x000fff00) | \
222 1.1 lukem (((z) << 0) & 0x000000ff)))
223 1.1 lukem
224 1.5 christos static portdev_t
225 1.8 christos pack_bsdos(int n, u_long numbers[], const char **error)
226 1.1 lukem {
227 1.7 ross portdev_t dev = 0;
228 1.1 lukem
229 1.1 lukem if (n == 2) {
230 1.1 lukem dev = makedev_12_20(numbers[0], numbers[1]);
231 1.1 lukem if (major_12_20(dev) != numbers[0])
232 1.7 ross *error = iMajorError;
233 1.1 lukem if (minor_12_20(dev) != numbers[1])
234 1.7 ross *error = iMinorError;
235 1.1 lukem } else if (n == 3) {
236 1.1 lukem dev = makedev_12_12_8(numbers[0], numbers[1], numbers[2]);
237 1.1 lukem if (major_12_12_8(dev) != numbers[0])
238 1.7 ross *error = iMajorError;
239 1.1 lukem if (unit_12_12_8(dev) != numbers[1])
240 1.7 ross *error = "invalid unit number";
241 1.1 lukem if (subunit_12_12_8(dev) != numbers[2])
242 1.7 ross *error = "invalid subunit number";
243 1.1 lukem } else
244 1.7 ross *error = tooManyFields;
245 1.1 lukem return (dev);
246 1.1 lukem }
247 1.1 lukem
248 1.1 lukem
249 1.1 lukem /* list of formats and pack functions */
250 1.1 lukem /* this list must be sorted lexically */
251 1.1 lukem struct format {
252 1.1 lukem const char *name;
253 1.1 lukem pack_t *pack;
254 1.1 lukem } formats[] = {
255 1.1 lukem {"386bsd", pack_8_8},
256 1.1 lukem {"4bsd", pack_8_8},
257 1.1 lukem {"bsdos", pack_bsdos},
258 1.1 lukem {"freebsd", pack_freebsd},
259 1.1 lukem {"hpux", pack_8_24},
260 1.1 lukem {"isc", pack_8_8},
261 1.1 lukem {"linux", pack_8_8},
262 1.1 lukem {"native", pack_native},
263 1.1 lukem {"netbsd", pack_netbsd},
264 1.1 lukem {"osf1", pack_12_20},
265 1.1 lukem {"sco", pack_8_8},
266 1.1 lukem {"solaris", pack_14_18},
267 1.1 lukem {"sunos", pack_8_8},
268 1.1 lukem {"svr3", pack_8_8},
269 1.1 lukem {"svr4", pack_14_18},
270 1.1 lukem {"ultrix", pack_8_8},
271 1.1 lukem };
272 1.1 lukem
273 1.1 lukem static int
274 1.1 lukem compare_format(const void *key, const void *element)
275 1.1 lukem {
276 1.1 lukem const char *name;
277 1.1 lukem const struct format *format;
278 1.1 lukem
279 1.1 lukem name = key;
280 1.1 lukem format = element;
281 1.1 lukem
282 1.1 lukem return (strcmp(name, format->name));
283 1.1 lukem }
284 1.1 lukem
285 1.1 lukem
286 1.1 lukem pack_t *
287 1.1 lukem pack_find(const char *name)
288 1.1 lukem {
289 1.1 lukem struct format *format;
290 1.1 lukem
291 1.1 lukem format = bsearch(name, formats,
292 1.1 lukem sizeof(formats)/sizeof(formats[0]),
293 1.1 lukem sizeof(formats[0]), compare_format);
294 1.1 lukem if (format == 0)
295 1.1 lukem return (NULL);
296 1.1 lukem return (format->pack);
297 1.1 lukem }
298