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/cdefs.h>
38 __RCSID("$NetBSD: pack_dev.c,v 1.12 2013/06/14 16:28:20 tsutsui Exp $");
41 #include <sys/types.h>
52 static pack_t pack_netbsd
;
53 static pack_t pack_freebsd
;
54 static pack_t pack_8_8
;
55 static pack_t pack_12_20
;
56 static pack_t pack_14_18
;
57 static pack_t pack_8_24
;
58 static pack_t pack_bsdos
;
59 static int compare_format(const void *, const void *);
61 static const char iMajorError
[] = "invalid major number";
62 static const char iMinorError
[] = "invalid minor number";
63 static const char tooManyFields
[] = "too many fields for format";
67 pack_native(int n
, u_long numbers
[], const char **error
)
72 dev
= makedev(numbers
[0], numbers
[1]);
73 if ((u_long
)major(dev
) != numbers
[0])
75 else if ((u_long
)minor(dev
) != numbers
[1])
78 *error
= tooManyFields
;
84 pack_netbsd(int n
, u_long numbers
[], const char **error
)
89 dev
= makedev_netbsd(numbers
[0], numbers
[1]);
90 if ((u_long
)major_netbsd(dev
) != numbers
[0])
92 else if ((u_long
)minor_netbsd(dev
) != numbers
[1])
95 *error
= tooManyFields
;
100 #define major_freebsd(x) ((int32_t)(((x) & 0x0000ff00) >> 8))
101 #define minor_freebsd(x) ((int32_t)(((x) & 0xffff00ff) >> 0))
102 #define makedev_freebsd(x,y) ((dev_t)((((x) << 8) & 0x0000ff00) | \
103 (((y) << 0) & 0xffff00ff)))
106 pack_freebsd(int n
, u_long numbers
[], const char **error
)
111 dev
= makedev_freebsd(numbers
[0], numbers
[1]);
112 if ((u_long
)major_freebsd(dev
) != numbers
[0])
113 *error
= iMajorError
;
114 if ((u_long
)minor_freebsd(dev
) != numbers
[1])
115 *error
= iMinorError
;
117 *error
= tooManyFields
;
122 #define major_8_8(x) ((int32_t)(((x) & 0x0000ff00) >> 8))
123 #define minor_8_8(x) ((int32_t)(((x) & 0x000000ff) >> 0))
124 #define makedev_8_8(x,y) ((dev_t)((((x) << 8) & 0x0000ff00) | \
125 (((y) << 0) & 0x000000ff)))
128 pack_8_8(int n
, u_long numbers
[], const char **error
)
133 dev
= makedev_8_8(numbers
[0], numbers
[1]);
134 if ((u_long
)major_8_8(dev
) != numbers
[0])
135 *error
= iMajorError
;
136 if ((u_long
)minor_8_8(dev
) != numbers
[1])
137 *error
= iMinorError
;
139 *error
= tooManyFields
;
144 #define major_12_20(x) ((int32_t)(((x) & 0xfff00000) >> 20))
145 #define minor_12_20(x) ((int32_t)(((x) & 0x000fffff) >> 0))
146 #define makedev_12_20(x,y) ((dev_t)((((x) << 20) & 0xfff00000) | \
147 (((y) << 0) & 0x000fffff)))
150 pack_12_20(int n
, u_long numbers
[], const char **error
)
155 dev
= makedev_12_20(numbers
[0], numbers
[1]);
156 if ((u_long
)major_12_20(dev
) != numbers
[0])
157 *error
= iMajorError
;
158 if ((u_long
)minor_12_20(dev
) != numbers
[1])
159 *error
= iMinorError
;
161 *error
= tooManyFields
;
166 #define major_14_18(x) ((int32_t)(((x) & 0xfffc0000) >> 18))
167 #define minor_14_18(x) ((int32_t)(((x) & 0x0003ffff) >> 0))
168 #define makedev_14_18(x,y) ((dev_t)((((x) << 18) & 0xfffc0000) | \
169 (((y) << 0) & 0x0003ffff)))
172 pack_14_18(int n
, u_long numbers
[], const char **error
)
177 dev
= makedev_14_18(numbers
[0], numbers
[1]);
178 if ((u_long
)major_14_18(dev
) != numbers
[0])
179 *error
= iMajorError
;
180 if ((u_long
)minor_14_18(dev
) != numbers
[1])
181 *error
= iMinorError
;
183 *error
= tooManyFields
;
188 #define major_8_24(x) ((int32_t)(((x) & 0xff000000) >> 24))
189 #define minor_8_24(x) ((int32_t)(((x) & 0x00ffffff) >> 0))
190 #define makedev_8_24(x,y) ((dev_t)((((x) << 24) & 0xff000000) | \
191 (((y) << 0) & 0x00ffffff)))
194 pack_8_24(int n
, u_long numbers
[], const char **error
)
199 dev
= makedev_8_24(numbers
[0], numbers
[1]);
200 if ((u_long
)major_8_24(dev
) != numbers
[0])
201 *error
= iMajorError
;
202 if ((u_long
)minor_8_24(dev
) != numbers
[1])
203 *error
= iMinorError
;
205 *error
= tooManyFields
;
210 #define major_12_12_8(x) ((int32_t)(((x) & 0xfff00000) >> 20))
211 #define unit_12_12_8(x) ((int32_t)(((x) & 0x000fff00) >> 8))
212 #define subunit_12_12_8(x) ((int32_t)(((x) & 0x000000ff) >> 0))
213 #define makedev_12_12_8(x,y,z) ((dev_t)((((x) << 20) & 0xfff00000) | \
214 (((y) << 8) & 0x000fff00) | \
215 (((z) << 0) & 0x000000ff)))
218 pack_bsdos(int n
, u_long numbers
[], const char **error
)
223 dev
= makedev_12_20(numbers
[0], numbers
[1]);
224 if ((u_long
)major_12_20(dev
) != numbers
[0])
225 *error
= iMajorError
;
226 if ((u_long
)minor_12_20(dev
) != numbers
[1])
227 *error
= iMinorError
;
229 dev
= makedev_12_12_8(numbers
[0], numbers
[1], numbers
[2]);
230 if ((u_long
)major_12_12_8(dev
) != numbers
[0])
231 *error
= iMajorError
;
232 if ((u_long
)unit_12_12_8(dev
) != numbers
[1])
233 *error
= "invalid unit number";
234 if ((u_long
)subunit_12_12_8(dev
) != numbers
[2])
235 *error
= "invalid subunit number";
237 *error
= tooManyFields
;
242 /* list of formats and pack functions */
243 /* this list must be sorted lexically */
244 static struct format
{
248 {"386bsd", pack_8_8
},
250 {"bsdos", pack_bsdos
},
251 {"freebsd", pack_freebsd
},
255 {"native", pack_native
},
256 {"netbsd", pack_netbsd
},
257 {"osf1", pack_12_20
},
259 {"solaris", pack_14_18
},
262 {"svr4", pack_14_18
},
263 {"ultrix", pack_8_8
},
267 compare_format(const void *key
, const void *element
)
270 const struct format
*format
;
275 return (strcmp(name
, format
->name
));
280 pack_find(const char *name
)
282 struct format
*format
;
284 format
= bsearch(name
, formats
,
285 sizeof(formats
)/sizeof(formats
[0]),
286 sizeof(formats
[0]), compare_format
);
289 return (format
->pack
);