1 /* $NetBSD: pack_dev.c,v 1.12 2013/06/14 16:28:20 tsutsui Exp $ */
4 * Copyright (c) 1998, 2001 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
36 #include <sys/types.h>
47 static pack_t pack_netbsd
;
48 static pack_t pack_freebsd
;
49 static pack_t pack_8_8
;
50 static pack_t pack_12_20
;
51 static pack_t pack_14_18
;
52 static pack_t pack_8_24
;
53 static pack_t pack_bsdos
;
54 static int compare_format(const void *, const void *);
56 static const char iMajorError
[] = "invalid major number";
57 static const char iMinorError
[] = "invalid minor number";
58 static const char tooManyFields
[] = "too many fields for format";
62 pack_native(int n
, u_long numbers
[], const char **error
)
67 dev
= makedev(numbers
[0], numbers
[1]);
68 if ((u_long
)major(dev
) != numbers
[0])
70 else if ((u_long
)minor(dev
) != numbers
[1])
73 *error
= tooManyFields
;
79 pack_netbsd(int n
, u_long numbers
[], const char **error
)
84 dev
= makedev_netbsd(numbers
[0], numbers
[1]);
85 if ((u_long
)major_netbsd(dev
) != numbers
[0])
87 else if ((u_long
)minor_netbsd(dev
) != numbers
[1])
90 *error
= tooManyFields
;
95 #define major_freebsd(x) ((int32_t)(((x) & 0x0000ff00) >> 8))
96 #define minor_freebsd(x) ((int32_t)(((x) & 0xffff00ff) >> 0))
97 #define makedev_freebsd(x,y) ((dev_t)((((x) << 8) & 0x0000ff00) | \
98 (((y) << 0) & 0xffff00ff)))
101 pack_freebsd(int n
, u_long numbers
[], const char **error
)
106 dev
= makedev_freebsd(numbers
[0], numbers
[1]);
107 if ((u_long
)major_freebsd(dev
) != numbers
[0])
108 *error
= iMajorError
;
109 if ((u_long
)minor_freebsd(dev
) != numbers
[1])
110 *error
= iMinorError
;
112 *error
= tooManyFields
;
117 #define major_8_8(x) ((int32_t)(((x) & 0x0000ff00) >> 8))
118 #define minor_8_8(x) ((int32_t)(((x) & 0x000000ff) >> 0))
119 #define makedev_8_8(x,y) ((dev_t)((((x) << 8) & 0x0000ff00) | \
120 (((y) << 0) & 0x000000ff)))
123 pack_8_8(int n
, u_long numbers
[], const char **error
)
128 dev
= makedev_8_8(numbers
[0], numbers
[1]);
129 if ((u_long
)major_8_8(dev
) != numbers
[0])
130 *error
= iMajorError
;
131 if ((u_long
)minor_8_8(dev
) != numbers
[1])
132 *error
= iMinorError
;
134 *error
= tooManyFields
;
139 #define major_12_20(x) ((int32_t)(((x) & 0xfff00000) >> 20))
140 #define minor_12_20(x) ((int32_t)(((x) & 0x000fffff) >> 0))
141 #define makedev_12_20(x,y) ((dev_t)((((x) << 20) & 0xfff00000) | \
142 (((y) << 0) & 0x000fffff)))
145 pack_12_20(int n
, u_long numbers
[], const char **error
)
150 dev
= makedev_12_20(numbers
[0], numbers
[1]);
151 if ((u_long
)major_12_20(dev
) != numbers
[0])
152 *error
= iMajorError
;
153 if ((u_long
)minor_12_20(dev
) != numbers
[1])
154 *error
= iMinorError
;
156 *error
= tooManyFields
;
161 #define major_14_18(x) ((int32_t)(((x) & 0xfffc0000) >> 18))
162 #define minor_14_18(x) ((int32_t)(((x) & 0x0003ffff) >> 0))
163 #define makedev_14_18(x,y) ((dev_t)((((x) << 18) & 0xfffc0000) | \
164 (((y) << 0) & 0x0003ffff)))
167 pack_14_18(int n
, u_long numbers
[], const char **error
)
172 dev
= makedev_14_18(numbers
[0], numbers
[1]);
173 if ((u_long
)major_14_18(dev
) != numbers
[0])
174 *error
= iMajorError
;
175 if ((u_long
)minor_14_18(dev
) != numbers
[1])
176 *error
= iMinorError
;
178 *error
= tooManyFields
;
183 #define major_8_24(x) ((int32_t)(((x) & 0xff000000) >> 24))
184 #define minor_8_24(x) ((int32_t)(((x) & 0x00ffffff) >> 0))
185 #define makedev_8_24(x,y) ((dev_t)((((x) << 24) & 0xff000000) | \
186 (((y) << 0) & 0x00ffffff)))
189 pack_8_24(int n
, u_long numbers
[], const char **error
)
194 dev
= makedev_8_24(numbers
[0], numbers
[1]);
195 if ((u_long
)major_8_24(dev
) != numbers
[0])
196 *error
= iMajorError
;
197 if ((u_long
)minor_8_24(dev
) != numbers
[1])
198 *error
= iMinorError
;
200 *error
= tooManyFields
;
205 #define major_12_12_8(x) ((int32_t)(((x) & 0xfff00000) >> 20))
206 #define unit_12_12_8(x) ((int32_t)(((x) & 0x000fff00) >> 8))
207 #define subunit_12_12_8(x) ((int32_t)(((x) & 0x000000ff) >> 0))
208 #define makedev_12_12_8(x,y,z) ((dev_t)((((x) << 20) & 0xfff00000) | \
209 (((y) << 8) & 0x000fff00) | \
210 (((z) << 0) & 0x000000ff)))
213 pack_bsdos(int n
, u_long numbers
[], const char **error
)
218 dev
= makedev_12_20(numbers
[0], numbers
[1]);
219 if ((u_long
)major_12_20(dev
) != numbers
[0])
220 *error
= iMajorError
;
221 if ((u_long
)minor_12_20(dev
) != numbers
[1])
222 *error
= iMinorError
;
224 dev
= makedev_12_12_8(numbers
[0], numbers
[1], numbers
[2]);
225 if ((u_long
)major_12_12_8(dev
) != numbers
[0])
226 *error
= iMajorError
;
227 if ((u_long
)unit_12_12_8(dev
) != numbers
[1])
228 *error
= "invalid unit number";
229 if ((u_long
)subunit_12_12_8(dev
) != numbers
[2])
230 *error
= "invalid subunit number";
232 *error
= tooManyFields
;
237 /* list of formats and pack functions */
238 /* this list must be sorted lexically */
239 static struct format
{
243 {"386bsd", pack_8_8
},
245 {"bsdos", pack_bsdos
},
246 {"freebsd", pack_freebsd
},
250 {"native", pack_native
},
251 {"netbsd", pack_netbsd
},
252 {"osf1", pack_12_20
},
254 {"solaris", pack_14_18
},
257 {"svr4", pack_14_18
},
258 {"ultrix", pack_8_8
},
262 compare_format(const void *key
, const void *element
)
265 const struct format
*format
;
270 return (strcmp(name
, format
->name
));
275 pack_find(const char *name
)
277 struct format
*format
;
279 format
= bsearch(name
, formats
,
280 sizeof(formats
)/sizeof(formats
[0]),
281 sizeof(formats
[0]), compare_format
);
284 return (format
->pack
);