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