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