1*82357f6dSdsl /* $NetBSD: devopen.c,v 1.6 2009/03/14 21:04:12 dsl Exp $ */
2a3ec1726Swdk
3a3ec1726Swdk /*-
4a3ec1726Swdk * Copyright (c) 1992, 1993
5a3ec1726Swdk * The Regents of the University of California. All rights reserved.
6a3ec1726Swdk *
7a3ec1726Swdk * This code is derived from software contributed to Berkeley by
8a3ec1726Swdk * Ralph Campbell.
9a3ec1726Swdk *
10a3ec1726Swdk * Redistribution and use in source and binary forms, with or without
11a3ec1726Swdk * modification, are permitted provided that the following conditions
12a3ec1726Swdk * are met:
13a3ec1726Swdk * 1. Redistributions of source code must retain the above copyright
14a3ec1726Swdk * notice, this list of conditions and the following disclaimer.
15a3ec1726Swdk * 2. Redistributions in binary form must reproduce the above copyright
16a3ec1726Swdk * notice, this list of conditions and the following disclaimer in the
17a3ec1726Swdk * documentation and/or other materials provided with the distribution.
18aad01611Sagc * 3. Neither the name of the University nor the names of its contributors
19a3ec1726Swdk * may be used to endorse or promote products derived from this software
20a3ec1726Swdk * without specific prior written permission.
21a3ec1726Swdk *
22a3ec1726Swdk * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23a3ec1726Swdk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24a3ec1726Swdk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25a3ec1726Swdk * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26a3ec1726Swdk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27a3ec1726Swdk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28a3ec1726Swdk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29a3ec1726Swdk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30a3ec1726Swdk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31a3ec1726Swdk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32a3ec1726Swdk * SUCH DAMAGE.
33a3ec1726Swdk *
34a3ec1726Swdk * @(#)devopen.c 8.1 (Berkeley) 6/10/93
35a3ec1726Swdk */
36a3ec1726Swdk
37a3ec1726Swdk #include <lib/libsa/stand.h>
38e1445fedSwdk #include <lib/libkern/libkern.h>
39a3ec1726Swdk
40a3ec1726Swdk /*
41a3ec1726Swdk * Decode the string 'fname', open the device and return the remaining
42a3ec1726Swdk * file name if any.
43a3ec1726Swdk */
44a3ec1726Swdk int
devopen(struct open_file * f,const char * fname,char ** file)45*82357f6dSdsl devopen(struct open_file *f, const char *fname, char **file)
46*82357f6dSdsl /* file: out */
47a3ec1726Swdk {
48a3ec1726Swdk int ctlr = 0, unit = 0, part = 0;
49a3ec1726Swdk int c, rc;
50a3ec1726Swdk char namebuf[20];
51a3ec1726Swdk const char *cp;
52a3ec1726Swdk char *ncp;
53a3ec1726Swdk #if !defined(LIBSA_SINGLE_DEVICE)
54a3ec1726Swdk int i;
55a3ec1726Swdk struct devsw *dp;
56a3ec1726Swdk #endif
57a3ec1726Swdk
58a3ec1726Swdk cp = fname;
59a3ec1726Swdk ncp = namebuf;
60a3ec1726Swdk
61a3ec1726Swdk /* expect a string like 'dksd(0,0,0)netbsd' */
62a3ec1726Swdk while ((c = *cp) != '\0') {
63a3ec1726Swdk if (c == '(') {
64a3ec1726Swdk cp++;
65a3ec1726Swdk break;
66a3ec1726Swdk }
67a3ec1726Swdk if (ncp < namebuf + sizeof(namebuf) - 1)
68a3ec1726Swdk *ncp++ = c;
69a3ec1726Swdk cp++;
70a3ec1726Swdk }
71a3ec1726Swdk
72a3ec1726Swdk /* get controller number */
73a3ec1726Swdk if ((c = *cp) >= '0' && c <= '9') {
74a3ec1726Swdk ctlr = c - '0';
75a3ec1726Swdk c = *++cp;
76a3ec1726Swdk }
77a3ec1726Swdk
78a3ec1726Swdk if (c == ',') {
79a3ec1726Swdk /* get SCSI device number */
80a3ec1726Swdk if ((c = *++cp) >= '0' && c <= '9') {
81a3ec1726Swdk unit = c - '0';
82a3ec1726Swdk c = *++cp;
83a3ec1726Swdk }
84a3ec1726Swdk
85a3ec1726Swdk if (c == ',') {
86a3ec1726Swdk /* get partition number */
87a3ec1726Swdk if ((c = *++cp) >= '0' && c <= '9') {
88a3ec1726Swdk part = c - '0';
8950e120c1Swdk /* dksd(,,8)boot -> dksd(,,) */
9050e120c1Swdk if (part >= 8)
9150e120c1Swdk part = 0;
92a3ec1726Swdk c = *++cp;
93a3ec1726Swdk }
94a3ec1726Swdk }
95a3ec1726Swdk }
96a3ec1726Swdk if (c != ')')
97a3ec1726Swdk return (ENXIO);
98a3ec1726Swdk cp++;
99a3ec1726Swdk
100a3ec1726Swdk *ncp = '\0';
101a3ec1726Swdk
102a3ec1726Swdk #ifdef LIBSA_SINGLE_DEVICE
103a3ec1726Swdk rc = DEV_OPEN(dp)(f, ctlr, unit, part);
104a3ec1726Swdk #else /* !LIBSA_SINGLE_DEVICE */
105a3ec1726Swdk for (dp = devsw, i = 0; i < ndevs; dp++, i++)
106a3ec1726Swdk if (dp->dv_name && strcmp(namebuf, dp->dv_name) == 0)
107a3ec1726Swdk goto fnd;
108a3ec1726Swdk printf("Unknown device '%s'\nKnown devices are:", namebuf);
109a3ec1726Swdk for (dp = devsw, i = 0; i < ndevs; dp++, i++)
110a3ec1726Swdk if (dp->dv_name)
111a3ec1726Swdk printf(" %s", dp->dv_name);
112a3ec1726Swdk printf("\n");
113a3ec1726Swdk return (ENXIO);
114a3ec1726Swdk
115a3ec1726Swdk fnd:
116a3ec1726Swdk #ifdef BOOTNET
117a3ec1726Swdk if (strcmp(namebuf, "tftp") == 0)
118a3ec1726Swdk rc = (dp->dv_open)(f, namebuf);
119a3ec1726Swdk else
120a3ec1726Swdk #endif /* BOOTNET */
121a3ec1726Swdk rc = (dp->dv_open)(f, ctlr, unit, part);
122a3ec1726Swdk #endif /* !LIBSA_SINGLE_DEVICE */
123a3ec1726Swdk if (rc)
124a3ec1726Swdk return (rc);
125a3ec1726Swdk
126a3ec1726Swdk #ifndef LIBSA_SINGLE_DEVICE
127a3ec1726Swdk f->f_dev = dp;
128a3ec1726Swdk #endif
129a3ec1726Swdk if (file && *cp != '\0')
130a3ec1726Swdk *file = (char *)cp; /* XXX */
131a3ec1726Swdk return (0);
132a3ec1726Swdk }
133