devopen.c revision 1.2.2.2 1 1.2.2.2 kent /* $NetBSD: devopen.c,v 1.2.2.2 2005/04/29 11:28:02 kent Exp $ */
2 1.2.2.2 kent
3 1.2.2.2 kent /*-
4 1.2.2.2 kent * Copyright (c) 1992, 1993
5 1.2.2.2 kent * The Regents of the University of California. All rights reserved.
6 1.2.2.2 kent *
7 1.2.2.2 kent * This code is derived from software contributed to Berkeley by
8 1.2.2.2 kent * Ralph Campbell.
9 1.2.2.2 kent *
10 1.2.2.2 kent * Redistribution and use in source and binary forms, with or without
11 1.2.2.2 kent * modification, are permitted provided that the following conditions
12 1.2.2.2 kent * are met:
13 1.2.2.2 kent * 1. Redistributions of source code must retain the above copyright
14 1.2.2.2 kent * notice, this list of conditions and the following disclaimer.
15 1.2.2.2 kent * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.2.2 kent * notice, this list of conditions and the following disclaimer in the
17 1.2.2.2 kent * documentation and/or other materials provided with the distribution.
18 1.2.2.2 kent * 3. Neither the name of the University nor the names of its contributors
19 1.2.2.2 kent * may be used to endorse or promote products derived from this software
20 1.2.2.2 kent * without specific prior written permission.
21 1.2.2.2 kent *
22 1.2.2.2 kent * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.2.2.2 kent * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.2.2.2 kent * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.2.2.2 kent * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.2.2.2 kent * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.2.2.2 kent * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.2.2.2 kent * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.2.2.2 kent * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.2.2.2 kent * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.2.2.2 kent * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.2.2.2 kent * SUCH DAMAGE.
33 1.2.2.2 kent *
34 1.2.2.2 kent * @(#)devopen.c 8.1 (Berkeley) 6/10/93
35 1.2.2.2 kent */
36 1.2.2.2 kent
37 1.2.2.2 kent #include <lib/libsa/stand.h>
38 1.2.2.2 kent #include <lib/libkern/libkern.h>
39 1.2.2.2 kent
40 1.2.2.2 kent /*
41 1.2.2.2 kent * Decode the string 'fname', open the device and return the remaining
42 1.2.2.2 kent * file name if any.
43 1.2.2.2 kent */
44 1.2.2.2 kent int
45 1.2.2.2 kent devopen(struct open_file *f, const char *fname, char **file)
46 1.2.2.2 kent {
47 1.2.2.2 kent int error;
48 1.2.2.2 kent char namebuf[128];
49 1.2.2.2 kent char devtype[16];
50 1.2.2.2 kent const char *cp;
51 1.2.2.2 kent char *ncp;
52 1.2.2.2 kent #if !defined(LIBSA_SINGLE_DEVICE)
53 1.2.2.2 kent int i;
54 1.2.2.2 kent struct devsw *dp;
55 1.2.2.2 kent #endif
56 1.2.2.2 kent
57 1.2.2.2 kent cp = fname;
58 1.2.2.2 kent ncp = (char *)fname;
59 1.2.2.2 kent
60 1.2.2.2 kent #ifdef arc
61 1.2.2.2 kent /*
62 1.2.2.2 kent * Look for "fdisk" (floppy disk?) or "rdisk" (rigid disk?) strings
63 1.2.2.2 kent * in OSLOADPARTITION like the following:
64 1.2.2.2 kent * 'scsi(0)disk(0)rdisk()partition(2)netbsd' (jazzio scsi disk)
65 1.2.2.2 kent * 'scsi(1)cdrom(1)fdisk()netbsd.arc' (jazzio scsi cdrom)
66 1.2.2.2 kent * 'multi(0)disk(0)fdisk()netbsd' (floppy disk)
67 1.2.2.2 kent * 'eisa(0)scsi(0)disk(0)rdisk()partition(2)netbsd' (eisa raid)
68 1.2.2.2 kent * etc. (the file can either be a relative path or an abosolute path).
69 1.2.2.2 kent */
70 1.2.2.2 kent for (;;) {
71 1.2.2.2 kent if (strncmp(cp, "rdisk", 5) == 0 ||
72 1.2.2.2 kent strncmp(cp, "fdisk", 5) == 0) {
73 1.2.2.2 kent strcpy(devtype, "disk");
74 1.2.2.2 kent break;
75 1.2.2.2 kent }
76 1.2.2.2 kent while (*ncp && *ncp++ != ')')
77 1.2.2.2 kent ;
78 1.2.2.2 kent if (*ncp)
79 1.2.2.2 kent cp = ncp;
80 1.2.2.2 kent else
81 1.2.2.2 kent return ENXIO;
82 1.2.2.2 kent }
83 1.2.2.2 kent #endif
84 1.2.2.2 kent #ifdef sgimips
85 1.2.2.2 kent /*
86 1.2.2.2 kent * If device starts with a PCI bus specifier, skip past it so the
87 1.2.2.2 kent * device-matching code below gets the actual device type. Leave
88 1.2.2.2 kent * fname as is, since it'll be passed back to ARCS to open the
89 1.2.2.2 kent * device. This is necessary for the IP32.
90 1.2.2.2 kent */
91 1.2.2.2 kent if (strncmp(cp, "pci", 3) == 0) {
92 1.2.2.2 kent while (*ncp && *ncp++ != ')')
93 1.2.2.2 kent ;
94 1.2.2.2 kent if (*ncp)
95 1.2.2.2 kent cp = ncp;
96 1.2.2.2 kent }
97 1.2.2.2 kent
98 1.2.2.2 kent /*
99 1.2.2.2 kent * Look for a string like 'scsi(0)disk(0)rdisk(0)partition(0)netbsd'
100 1.2.2.2 kent * or 'dksc(0,0,0)/netbsd' (the file can either be a relative path
101 1.2.2.2 kent * or an abosolute path).
102 1.2.2.2 kent */
103 1.2.2.2 kent if (strncmp(cp, "scsi", 4) == 0) {
104 1.2.2.2 kent strcpy(devtype, "disk");
105 1.2.2.2 kent } else if (strncmp(cp, "dksc", 4) == 0) {
106 1.2.2.2 kent strcpy(devtype, "disk");
107 1.2.2.2 kent } else
108 1.2.2.2 kent return ENXIO;
109 1.2.2.2 kent #endif
110 1.2.2.2 kent
111 1.2.2.2 kent while (ncp != NULL && *ncp != 0) {
112 1.2.2.2 kent while (*ncp && *ncp++ != ')')
113 1.2.2.2 kent ;
114 1.2.2.2 kent if (*ncp)
115 1.2.2.2 kent cp = ncp;
116 1.2.2.2 kent }
117 1.2.2.2 kent
118 1.2.2.2 kent strncpy(namebuf, fname, sizeof(namebuf));
119 1.2.2.2 kent namebuf[cp - fname] = 0;
120 1.2.2.2 kent
121 1.2.2.2 kent printf("devopen: %s type %s file %s\n", namebuf, devtype, cp);
122 1.2.2.2 kent #ifdef LIBSA_SINGLE_DEVICE
123 1.2.2.2 kent error = DEV_OPEN(dp)(f, fname);
124 1.2.2.2 kent #else /* !LIBSA_SINGLE_DEVICE */
125 1.2.2.2 kent for (dp = devsw, i = 0; i < ndevs; dp++, i++)
126 1.2.2.2 kent if (dp->dv_name && strcmp(devtype, dp->dv_name) == 0)
127 1.2.2.2 kent goto found;
128 1.2.2.2 kent printf("Unknown device '%s'\nKnown devices are:", devtype);
129 1.2.2.2 kent for (dp = devsw, i = 0; i < ndevs; dp++, i++)
130 1.2.2.2 kent if (dp->dv_name)
131 1.2.2.2 kent printf(" %s", dp->dv_name);
132 1.2.2.2 kent printf("\n");
133 1.2.2.2 kent return ENXIO;
134 1.2.2.2 kent
135 1.2.2.2 kent found:
136 1.2.2.2 kent error = (dp->dv_open)(f, namebuf);
137 1.2.2.2 kent #endif /* !LIBSA_SINGLE_DEVICE */
138 1.2.2.2 kent if (error)
139 1.2.2.2 kent return error;
140 1.2.2.2 kent
141 1.2.2.2 kent #ifndef LIBSA_SINGLE_DEVICE
142 1.2.2.2 kent f->f_dev = dp;
143 1.2.2.2 kent #endif
144 1.2.2.2 kent if (file && *cp != '\0')
145 1.2.2.2 kent *file = (char *)cp; /* XXX */
146 1.2.2.2 kent return 0;
147 1.2.2.2 kent }
148