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