xref: /netbsd-src/sbin/mknod/pack_dev.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: pack_dev.c,v 1.8 2004/05/11 17:09:58 christos 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.8 2004/05/11 17:09:58 christos Exp $");
46 #endif /* not lint */
47 
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 
51 #include <limits.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 
57 #include "pack_dev.h"
58 
59 static	pack_t	pack_netbsd;
60 static	pack_t	pack_freebsd;
61 static	pack_t	pack_8_8;
62 static	pack_t	pack_12_20;
63 static	pack_t	pack_14_18;
64 static	pack_t	pack_8_24;
65 static	pack_t	pack_bsdos;
66 static	int	compare_format(const void *, const void *);
67 
68 static const char iMajorError[] = "invalid major number";
69 static const char iMinorError[] = "invalid minor number";
70 static const char tooManyFields[] = "too many fields for format";
71 
72 	/* exported */
73 portdev_t
74 pack_native(int n, u_long numbers[], const char **error)
75 {
76 	portdev_t dev = 0;
77 
78 	if (n == 2) {
79 		dev = makedev(numbers[0], numbers[1]);
80 		if (major(dev) != numbers[0])
81 			*error = iMajorError;
82 		else if (minor(dev) != numbers[1])
83 			*error = iMinorError;
84 	} else
85 		*error = tooManyFields;
86 	return (dev);
87 }
88 
89 
90 static portdev_t
91 pack_netbsd(int n, u_long numbers[], const char **error)
92 {
93 	portdev_t dev = 0;
94 
95 	if (n == 2) {
96 		dev = makedev_netbsd(numbers[0], numbers[1]);
97 		if (major_netbsd(dev) != numbers[0])
98 			*error = iMajorError;
99 		else if (minor_netbsd(dev) != numbers[1])
100 			*error = iMinorError;
101 	} else
102 		*error = tooManyFields;
103 	return (dev);
104 }
105 
106 
107 #define	major_freebsd(x)	((int32_t)(((x) & 0x0000ff00) >> 8))
108 #define	minor_freebsd(x)	((int32_t)(((x) & 0xffff00ff) >> 0))
109 #define	makedev_freebsd(x,y)	((portdev_t)((((x) << 8) & 0x0000ff00) | \
110 					 (((y) << 0) & 0xffff00ff)))
111 
112 static portdev_t
113 pack_freebsd(int n, u_long numbers[], const char **error)
114 {
115 	portdev_t dev = 0;
116 
117 	if (n == 2) {
118 		dev = makedev_freebsd(numbers[0], numbers[1]);
119 		if (major_freebsd(dev) != numbers[0])
120 			*error = iMajorError;
121 		if (minor_freebsd(dev) != numbers[1])
122 			*error = iMinorError;
123 	} else
124 		*error = tooManyFields;
125 	return (dev);
126 }
127 
128 
129 #define	major_8_8(x)		((int32_t)(((x) & 0x0000ff00) >> 8))
130 #define	minor_8_8(x)		((int32_t)(((x) & 0x000000ff) >> 0))
131 #define	makedev_8_8(x,y)	((portdev_t)((((x) << 8) & 0x0000ff00) | \
132 					 (((y) << 0) & 0x000000ff)))
133 
134 static portdev_t
135 pack_8_8(int n, u_long numbers[], const char **error)
136 {
137 	portdev_t dev = 0;
138 
139 	if (n == 2) {
140 		dev = makedev_8_8(numbers[0], numbers[1]);
141 		if (major_8_8(dev) != numbers[0])
142 			*error = iMajorError;
143 		if (minor_8_8(dev) != numbers[1])
144 			*error = iMinorError;
145 	} else
146 		*error = tooManyFields;
147 	return (dev);
148 }
149 
150 
151 #define	major_12_20(x)		((int32_t)(((x) & 0xfff00000) >> 20))
152 #define	minor_12_20(x)		((int32_t)(((x) & 0x000fffff) >>  0))
153 #define	makedev_12_20(x,y)	((portdev_t)((((x) << 20) & 0xfff00000) | \
154 					 (((y) <<  0) & 0x000fffff)))
155 
156 static portdev_t
157 pack_12_20(int n, u_long numbers[], const char **error)
158 {
159 	portdev_t dev = 0;
160 
161 	if (n == 2) {
162 		dev = makedev_12_20(numbers[0], numbers[1]);
163 		if (major_12_20(dev) != numbers[0])
164 			*error = iMajorError;
165 		if (minor_12_20(dev) != numbers[1])
166 			*error = iMinorError;
167 	} else
168 		*error = tooManyFields;
169 	return (dev);
170 }
171 
172 
173 #define	major_14_18(x)		((int32_t)(((x) & 0xfffc0000) >> 18))
174 #define	minor_14_18(x)		((int32_t)(((x) & 0x0003ffff) >>  0))
175 #define	makedev_14_18(x,y)	((portdev_t)((((x) << 18) & 0xfffc0000) | \
176 					 (((y) <<  0) & 0x0003ffff)))
177 
178 static portdev_t
179 pack_14_18(int n, u_long numbers[], const char **error)
180 {
181 	portdev_t dev = 0;
182 
183 	if (n == 2) {
184 		dev = makedev_14_18(numbers[0], numbers[1]);
185 		if (major_14_18(dev) != numbers[0])
186 			*error = iMajorError;
187 		if (minor_14_18(dev) != numbers[1])
188 			*error = iMinorError;
189 	} else
190 		*error = tooManyFields;
191 	return (dev);
192 }
193 
194 
195 #define	major_8_24(x)		((int32_t)(((x) & 0xff000000) >> 24))
196 #define	minor_8_24(x)		((int32_t)(((x) & 0x00ffffff) >>  0))
197 #define	makedev_8_24(x,y)	((portdev_t)((((x) << 24) & 0xff000000) | \
198 					 (((y) <<  0) & 0x00ffffff)))
199 
200 static portdev_t
201 pack_8_24(int n, u_long numbers[], const char **error)
202 {
203 	portdev_t dev = 0;
204 
205 	if (n == 2) {
206 		dev = makedev_8_24(numbers[0], numbers[1]);
207 		if (major_8_24(dev) != numbers[0])
208 			*error = iMajorError;
209 		if (minor_8_24(dev) != numbers[1])
210 			*error = iMinorError;
211 	} else
212 		*error = tooManyFields;
213 	return (dev);
214 }
215 
216 
217 #define	major_12_12_8(x)	((int32_t)(((x) & 0xfff00000) >> 20))
218 #define	unit_12_12_8(x)		((int32_t)(((x) & 0x000fff00) >>  8))
219 #define	subunit_12_12_8(x)	((int32_t)(((x) & 0x000000ff) >>  0))
220 #define	makedev_12_12_8(x,y,z)	((portdev_t)((((x) << 20) & 0xfff00000) | \
221 					 (((y) <<  8) & 0x000fff00) | \
222 					 (((z) <<  0) & 0x000000ff)))
223 
224 static portdev_t
225 pack_bsdos(int n, u_long numbers[], const char **error)
226 {
227 	portdev_t dev = 0;
228 
229 	if (n == 2) {
230 		dev = makedev_12_20(numbers[0], numbers[1]);
231 		if (major_12_20(dev) != numbers[0])
232 			*error = iMajorError;
233 		if (minor_12_20(dev) != numbers[1])
234 			*error = iMinorError;
235 	} else if (n == 3) {
236 		dev = makedev_12_12_8(numbers[0], numbers[1], numbers[2]);
237 		if (major_12_12_8(dev) != numbers[0])
238 			*error = iMajorError;
239 		if (unit_12_12_8(dev) != numbers[1])
240 			*error = "invalid unit number";
241 		if (subunit_12_12_8(dev) != numbers[2])
242 			*error = "invalid subunit number";
243 	} else
244 		*error = tooManyFields;
245 	return (dev);
246 }
247 
248 
249 		/* list of formats and pack functions */
250 		/* this list must be sorted lexically */
251 struct format {
252 	const char	*name;
253 	pack_t		*pack;
254 } formats[] = {
255 	{"386bsd",  pack_8_8},
256 	{"4bsd",    pack_8_8},
257 	{"bsdos",   pack_bsdos},
258 	{"freebsd", pack_freebsd},
259 	{"hpux",    pack_8_24},
260 	{"isc",     pack_8_8},
261 	{"linux",   pack_8_8},
262 	{"native",  pack_native},
263 	{"netbsd",  pack_netbsd},
264 	{"osf1",    pack_12_20},
265 	{"sco",     pack_8_8},
266 	{"solaris", pack_14_18},
267 	{"sunos",   pack_8_8},
268 	{"svr3",    pack_8_8},
269 	{"svr4",    pack_14_18},
270 	{"ultrix",  pack_8_8},
271 };
272 
273 static int
274 compare_format(const void *key, const void *element)
275 {
276 	const char		*name;
277 	const struct format	*format;
278 
279 	name = key;
280 	format = element;
281 
282 	return (strcmp(name, format->name));
283 }
284 
285 
286 pack_t *
287 pack_find(const char *name)
288 {
289 	struct format	*format;
290 
291 	format = bsearch(name, formats,
292 	    sizeof(formats)/sizeof(formats[0]),
293 	    sizeof(formats[0]), compare_format);
294 	if (format == 0)
295 		return (NULL);
296 	return (format->pack);
297 }
298