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