Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:19

0001 /*  $NetBSD: pack_dev.c,v 1.10 2009/02/13 01:37:23 lukem Exp $  */
0002 
0003 /*-
0004  * Copyright (c) 1998, 2001 The NetBSD Foundation, Inc.
0005  * All rights reserved.
0006  *
0007  * This code is derived from software contributed to The NetBSD Foundation
0008  * by Charles M. Hannum.
0009  *
0010  * Redistribution and use in source and binary forms, with or without
0011  * modification, are permitted provided that the following conditions
0012  * are met:
0013  * 1. Redistributions of source code must retain the above copyright
0014  *    notice, this list of conditions and the following disclaimer.
0015  * 2. Redistributions in binary form must reproduce the above copyright
0016  *    notice, this list of conditions and the following disclaimer in the
0017  *    documentation and/or other materials provided with the distribution.
0018  *
0019  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
0020  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
0021  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0022  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
0023  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0024  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0025  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0027  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0028  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0029  * POSSIBILITY OF SUCH DAMAGE.
0030  */
0031 
0032 #ifdef HAVE_CONFIG_H
0033 #include "config.h"
0034 #endif
0035 
0036 #if HAVE_NBTOOL_CONFIG_H
0037 #include "nbtool_config.h"
0038 #endif
0039 
0040 #if 0
0041 #include <sys/cdefs.h>
0042 #if !defined(lint)
0043 __RCSID("$NetBSD: pack_dev.c,v 1.10 2009/02/13 01:37:23 lukem Exp $");
0044 #endif /* not lint */
0045 
0046 #include <sys/types.h>
0047 #include <sys/stat.h>
0048 
0049 #include <limits.h>
0050 #include <stdio.h>
0051 #include <stdlib.h>
0052 #include <string.h>
0053 #include <unistd.h>
0054 
0055 #include "pack_dev.h"
0056 #endif
0057 
0058 static  pack_t  pack_netbsd;
0059 static  pack_t  pack_freebsd;
0060 static  pack_t  pack_8_8;
0061 static  pack_t  pack_12_20;
0062 static  pack_t  pack_14_18;
0063 static  pack_t  pack_8_24;
0064 static  pack_t  pack_bsdos;
0065 static  int compare_format(const void *, const void *);
0066 
0067 static const char iMajorError[] = "invalid major number";
0068 static const char iMinorError[] = "invalid minor number";
0069 static const char tooManyFields[] = "too many fields for format";
0070 
0071 #define makedev(x,y) rtems_filesystem_make_dev_t(x,y)
0072 #define major(d)     rtems_filesystem_dev_major_t(d)
0073 #define minor(d)     rtems_filesystem_dev_minor_t(d)
0074 
0075     /* exported */
0076 portdev_t
0077 pack_native(int n, u_long numbers[], const char **error)
0078 {
0079     portdev_t dev = 0;
0080 
0081     if (n == 2) {
0082         dev = makedev(numbers[0], numbers[1]);
0083         if ((u_long)major(dev) != numbers[0])
0084             *error = iMajorError;
0085         else if ((u_long)minor(dev) != numbers[1])
0086             *error = iMinorError;
0087     } else
0088         *error = tooManyFields;
0089     return (dev);
0090 }
0091 
0092 
0093 static portdev_t
0094 pack_netbsd(int n, u_long numbers[], const char **error)
0095 {
0096     portdev_t dev = 0;
0097 
0098     if (n == 2) {
0099         dev = makedev_netbsd(numbers[0], numbers[1]);
0100         if ((u_long)major_netbsd(dev) != numbers[0])
0101             *error = iMajorError;
0102         else if ((u_long)minor_netbsd(dev) != numbers[1])
0103             *error = iMinorError;
0104     } else
0105         *error = tooManyFields;
0106     return (dev);
0107 }
0108 
0109 
0110 #define major_freebsd(x)    ((int32_t)(((x) & 0x0000ff00) >> 8))
0111 #define minor_freebsd(x)    ((int32_t)(((x) & 0xffff00ff) >> 0))
0112 #define makedev_freebsd(x,y)    ((portdev_t)((((x) << 8) & 0x0000ff00) | \
0113                      (((y) << 0) & 0xffff00ff)))
0114 
0115 static portdev_t
0116 pack_freebsd(int n, u_long numbers[], const char **error)
0117 {
0118     portdev_t dev = 0;
0119 
0120     if (n == 2) {
0121         dev = makedev_freebsd(numbers[0], numbers[1]);
0122         if ((u_long)major_freebsd(dev) != numbers[0])
0123             *error = iMajorError;
0124         if ((u_long)minor_freebsd(dev) != numbers[1])
0125             *error = iMinorError;
0126     } else
0127         *error = tooManyFields;
0128     return (dev);
0129 }
0130 
0131 
0132 #define major_8_8(x)        ((int32_t)(((x) & 0x0000ff00) >> 8))
0133 #define minor_8_8(x)        ((int32_t)(((x) & 0x000000ff) >> 0))
0134 #define makedev_8_8(x,y)    ((portdev_t)((((x) << 8) & 0x0000ff00) | \
0135                      (((y) << 0) & 0x000000ff)))
0136 
0137 static portdev_t
0138 pack_8_8(int n, u_long numbers[], const char **error)
0139 {
0140     portdev_t dev = 0;
0141 
0142     if (n == 2) {
0143         dev = makedev_8_8(numbers[0], numbers[1]);
0144         if ((u_long)major_8_8(dev) != numbers[0])
0145             *error = iMajorError;
0146         if ((u_long)minor_8_8(dev) != numbers[1])
0147             *error = iMinorError;
0148     } else
0149         *error = tooManyFields;
0150     return (dev);
0151 }
0152 
0153 
0154 #define major_12_20(x)      ((int32_t)(((x) & 0xfff00000) >> 20))
0155 #define minor_12_20(x)      ((int32_t)(((x) & 0x000fffff) >>  0))
0156 #define makedev_12_20(x,y)  ((portdev_t)((((x) << 20) & 0xfff00000) | \
0157                      (((y) <<  0) & 0x000fffff)))
0158 
0159 static portdev_t
0160 pack_12_20(int n, u_long numbers[], const char **error)
0161 {
0162     portdev_t dev = 0;
0163 
0164     if (n == 2) {
0165         dev = makedev_12_20(numbers[0], numbers[1]);
0166         if ((u_long)major_12_20(dev) != numbers[0])
0167             *error = iMajorError;
0168         if ((u_long)minor_12_20(dev) != numbers[1])
0169             *error = iMinorError;
0170     } else
0171         *error = tooManyFields;
0172     return (dev);
0173 }
0174 
0175 
0176 #define major_14_18(x)      ((int32_t)(((x) & 0xfffc0000) >> 18))
0177 #define minor_14_18(x)      ((int32_t)(((x) & 0x0003ffff) >>  0))
0178 #define makedev_14_18(x,y)  ((portdev_t)((((x) << 18) & 0xfffc0000) | \
0179                      (((y) <<  0) & 0x0003ffff)))
0180 
0181 static portdev_t
0182 pack_14_18(int n, u_long numbers[], const char **error)
0183 {
0184     portdev_t dev = 0;
0185 
0186     if (n == 2) {
0187         dev = makedev_14_18(numbers[0], numbers[1]);
0188         if ((u_long)major_14_18(dev) != numbers[0])
0189             *error = iMajorError;
0190         if ((u_long)minor_14_18(dev) != numbers[1])
0191             *error = iMinorError;
0192     } else
0193         *error = tooManyFields;
0194     return (dev);
0195 }
0196 
0197 
0198 #define major_8_24(x)       ((int32_t)(((x) & 0xff000000) >> 24))
0199 #define minor_8_24(x)       ((int32_t)(((x) & 0x00ffffff) >>  0))
0200 #define makedev_8_24(x,y)   ((portdev_t)((((x) << 24) & 0xff000000) | \
0201                      (((y) <<  0) & 0x00ffffff)))
0202 
0203 static portdev_t
0204 pack_8_24(int n, u_long numbers[], const char **error)
0205 {
0206     portdev_t dev = 0;
0207 
0208     if (n == 2) {
0209         dev = makedev_8_24(numbers[0], numbers[1]);
0210         if ((u_long)major_8_24(dev) != numbers[0])
0211             *error = iMajorError;
0212         if ((u_long)minor_8_24(dev) != numbers[1])
0213             *error = iMinorError;
0214     } else
0215         *error = tooManyFields;
0216     return (dev);
0217 }
0218 
0219 
0220 #define major_12_12_8(x)    ((int32_t)(((x) & 0xfff00000) >> 20))
0221 #define unit_12_12_8(x)     ((int32_t)(((x) & 0x000fff00) >>  8))
0222 #define subunit_12_12_8(x)  ((int32_t)(((x) & 0x000000ff) >>  0))
0223 #define makedev_12_12_8(x,y,z)  ((portdev_t)((((x) << 20) & 0xfff00000) | \
0224                      (((y) <<  8) & 0x000fff00) | \
0225                      (((z) <<  0) & 0x000000ff)))
0226 
0227 static portdev_t
0228 pack_bsdos(int n, u_long numbers[], const char **error)
0229 {
0230     portdev_t dev = 0;
0231 
0232     if (n == 2) {
0233         dev = makedev_12_20(numbers[0], numbers[1]);
0234         if ((u_long)major_12_20(dev) != numbers[0])
0235             *error = iMajorError;
0236         if ((u_long)minor_12_20(dev) != numbers[1])
0237             *error = iMinorError;
0238     } else if (n == 3) {
0239         dev = makedev_12_12_8(numbers[0], numbers[1], numbers[2]);
0240         if ((u_long)major_12_12_8(dev) != numbers[0])
0241             *error = iMajorError;
0242         if ((u_long)unit_12_12_8(dev) != numbers[1])
0243             *error = "invalid unit number";
0244         if ((u_long)subunit_12_12_8(dev) != numbers[2])
0245             *error = "invalid subunit number";
0246     } else
0247         *error = tooManyFields;
0248     return (dev);
0249 }
0250 
0251 
0252         /* list of formats and pack functions */
0253         /* this list must be sorted lexically */
0254 struct format {
0255     const char  *name;
0256     pack_t      *pack;
0257 } formats[] = {
0258     {"386bsd",  pack_8_8},
0259     {"4bsd",    pack_8_8},
0260     {"bsdos",   pack_bsdos},
0261     {"freebsd", pack_freebsd},
0262     {"hpux",    pack_8_24},
0263     {"isc",     pack_8_8},
0264     {"linux",   pack_8_8},
0265     {"native",  pack_native},
0266     {"netbsd",  pack_netbsd},
0267     {"osf1",    pack_12_20},
0268     {"sco",     pack_8_8},
0269     {"solaris", pack_14_18},
0270     {"sunos",   pack_8_8},
0271     {"svr3",    pack_8_8},
0272     {"svr4",    pack_14_18},
0273     {"ultrix",  pack_8_8},
0274 };
0275 
0276 static int
0277 compare_format(const void *key, const void *element)
0278 {
0279     const char      *name;
0280     const struct format *format;
0281 
0282     name = key;
0283     format = element;
0284 
0285     return (strcmp(name, format->name));
0286 }
0287 
0288 
0289 pack_t *
0290 pack_find(const char *name)
0291 {
0292     struct format   *format;
0293 
0294     format = bsearch(name, formats,
0295         sizeof(formats)/sizeof(formats[0]),
0296         sizeof(formats[0]), compare_format);
0297     if (format == 0)
0298         return (NULL);
0299     return (format->pack);
0300 }