Annotation of sys/arch/armish/stand/boot/devopen.c, Revision 1.1
1.1 ! nbrk 1: /* $OpenBSD: devopen.c,v 1.2 2006/07/29 15:01:49 kettenis Exp $ */
! 2: /* $NetBSD: devopen.c,v 1.1 2003/06/25 17:24:22 cdi Exp $ */
! 3:
! 4: /*-
! 5: * Copyright (c) 2003 The NetBSD Foundation, Inc.
! 6: * All rights reserved.
! 7: *
! 8: * This code is derived from software contributed to The NetBSD Foundation
! 9: * by Rolf Grossmann.
! 10: *
! 11: * Redistribution and use in source and binary forms, with or without
! 12: * modification, are permitted provided that the following conditions
! 13: * are met:
! 14: * 1. Redistributions of source code must retain the above copyright
! 15: * notice, this list of conditions and the following disclaimer.
! 16: * 2. Redistributions in binary form must reproduce the above copyright
! 17: * notice, this list of conditions and the following disclaimer in the
! 18: * documentation and/or other materials provided with the distribution.
! 19: * 3. All advertising materials mentioning features or use of this software
! 20: * must display the following acknowledgement:
! 21: * This product includes software developed by the NetBSD
! 22: * Foundation, Inc. and its contributors.
! 23: * 4. Neither the name of The NetBSD Foundation nor the names of its
! 24: * contributors may be used to endorse or promote products derived
! 25: * from this software without specific prior written permission.
! 26: *
! 27: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
! 28: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
! 29: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
! 30: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
! 31: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
! 32: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
! 33: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
! 34: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
! 35: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
! 36: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
! 37: * POSSIBILITY OF SUCH DAMAGE.
! 38: */
! 39:
! 40: #include "libsa.h"
! 41:
! 42: #define MAXDEVNAME 16
! 43:
! 44: /*
! 45: * Parse a device spec.
! 46: *
! 47: * [A-Za-z]*[0-9]*[A-Za-z]:file
! 48: * dev uint part
! 49: */
! 50: int
! 51: devparse(const char *fname, int *dev, int *unit, int *part, const char **file)
! 52: {
! 53: const char *s;
! 54:
! 55: *unit = 0; /* default to wd0a */
! 56: *part = 0;
! 57: *dev = 0;
! 58:
! 59: s = strchr(fname, ':');
! 60: if (s != NULL) {
! 61: int devlen;
! 62: int i, u, p;
! 63: struct devsw *dp;
! 64: char devname[MAXDEVNAME];
! 65:
! 66: devlen = s - fname;
! 67: if (devlen > MAXDEVNAME)
! 68: return (EINVAL);
! 69:
! 70: /* extract device name */
! 71: for (i = 0; isalpha(fname[i]) && (i < devlen); i++)
! 72: devname[i] = fname[i];
! 73: devname[i] = 0;
! 74:
! 75: if (!isdigit(fname[i]))
! 76: return (EUNIT);
! 77:
! 78: /* device number */
! 79: for (u = 0; isdigit(fname[i]) && (i < devlen); i++)
! 80: u = u * 10 + (fname[i] - '0');
! 81:
! 82: if (!isalpha(fname[i]))
! 83: return (EPART);
! 84:
! 85: /* partition number */
! 86: if (i < devlen)
! 87: p = fname[i++] - 'a';
! 88:
! 89: if (i != devlen)
! 90: return (ENXIO);
! 91:
! 92: /* check device name */
! 93: for (dp = devsw, i = 0; i < ndevs; dp++, i++) {
! 94: if (dp->dv_name && !strcmp(devname, dp->dv_name))
! 95: break;
! 96: }
! 97:
! 98: if (i >= ndevs)
! 99: return (ENXIO);
! 100:
! 101: *unit = u;
! 102: *part = p;
! 103: *dev = i;
! 104: fname = ++s;
! 105: }
! 106:
! 107: *file = fname;
! 108:
! 109: return (0);
! 110: }
! 111:
! 112: int
! 113: devopen(struct open_file *f, const char *fname, char **file)
! 114: {
! 115: struct devsw *dp;
! 116: int dev, unit, part, error;
! 117:
! 118: error = devparse(fname, &dev, &unit, &part, (const char **)file);
! 119: if (error)
! 120: return (error);
! 121:
! 122: dp = &devsw[dev];
! 123: if ((void *)dp->dv_open == (void *)nodev)
! 124: return (ENXIO);
! 125:
! 126: f->f_dev = dp;
! 127:
! 128: return (*dp->dv_open)(f, unit, part);
! 129: }
CVSweb