1/*	$OpenBSD: devopen.c,v 1.4 2023/01/10 17:10:57 miod Exp $	*/
2/*	$NetBSD: devopen.c,v 1.3 2013/01/16 15:46:20 tsutsui Exp $	*/
3
4/*
5 * Copyright (c) 1992 OMRON Corporation.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * OMRON Corporation.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the University of
21 *	California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 *	@(#)conf.c	8.1 (Berkeley) 6/10/93
39 */
40/*
41 * Copyright (c) 1992, 1993
42 *	The Regents of the University of California.  All rights reserved.
43 *
44 * This code is derived from software contributed to Berkeley by
45 * OMRON Corporation.
46 *
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
49 * are met:
50 * 1. Redistributions of source code must retain the above copyright
51 *    notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 *    notice, this list of conditions and the following disclaimer in the
54 *    documentation and/or other materials provided with the distribution.
55 * 3. Neither the name of the University nor the names of its contributors
56 *    may be used to endorse or promote products derived from this software
57 *    without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
71 *	@(#)conf.c	8.1 (Berkeley) 6/10/93
72 */
73
74#include <sys/reboot.h>
75#include <lib/libkern/libkern.h>
76#include <luna88k/stand/boot/samachdep.h>
77#include <machine/disklabel.h>
78
79#define MAXDEVNAME	16
80
81static int make_device(const char *, int *, int *, int *, char **);
82
83int
84devopen(struct open_file *f, const char *fname, char **file)
85{
86	int dev, unit, part;
87	int error;
88	struct devsw *dp;
89	int i;
90
91	if (make_device(fname, &dev, &unit, &part, file) != 0)
92		return ENXIO;
93
94#ifdef DEBUG
95	printf("%s: %s(%d,%d):%s\n", __func__,
96	    devsw[dev].dv_name, unit, part, *file);
97#endif
98	dp = &devsw[dev];
99	error = (*dp->dv_open)(f, unit, part);
100	if (error != 0) {
101#ifdef DEBUG
102		printf("%s: open %s(%d,%d):%s failed (%s)\n", __func__,
103		    devsw[dev].dv_name, unit, part, *file, strerror(error));
104#endif
105		return error;
106	}
107
108	for (i = 0; i < nfsys_disk; i++)
109		file_system[i] = file_system_disk[i];
110	nfsys = nfsys_disk;
111
112#ifdef SUPPORT_ETHERNET
113	if (strcmp(dp->dv_name, "le") == 0) {
114		/* XXX mixing local fs_ops on netboot could be troublesome */
115		file_system[0] = file_system_nfs[0];
116		nfsys = 1;
117	}
118#endif
119
120	f->f_dev = dp;
121
122	/* Save boot device information to pass to the kernel */
123	bootdev = MAKEBOOTDEV(dev, 0, unit / 10, 6 - unit % 10, part);
124
125	return 0;
126}
127
128int
129make_device(const char *str, int *devp, int *unitp, int *partp, char **fname)
130{
131	const char *cp;
132	struct devsw *dp;
133	int major, unit = 0, part = 0;
134	int i;
135	char devname[MAXDEVNAME + 1];
136
137	/*
138	 * parse path strings
139	 */
140				/* find end of dev type name */
141	for (cp = str, i = 0; *cp != '\0' && *cp != '(' && i < MAXDEVNAME; i++)
142			devname[i] = *cp++;
143	if (*cp != '(') {
144		return (-1);
145	}
146	devname[i] = '\0';
147				/* compare dev type name */
148	for (dp = devsw; dp->dv_name; dp++)
149		if (!strcmp(devname, dp->dv_name))
150			break;
151	cp++;
152	if (dp->dv_name == NULL) {
153		return (-1);
154	}
155	major = dp - devsw;
156				/* get mixed controller and unit number */
157	for (; *cp != ',' && *cp != ')'; cp++) {
158		if (*cp == '\0')
159			return -1;
160		if (*cp >= '0' && *cp <= '9')
161			unit = unit * 10 + *cp - '0';
162	}
163	if (unit < 0 || unit >= 20 || (unit % 10) > 7) {
164#ifdef DEBUG
165		printf("%s: invalid unit number (%d)\n", __func__, unit);
166#endif
167		return (-1);
168	}
169				/* get optional partition number */
170	if (*cp == ',')
171		cp++;
172
173	for (; /* *cp != ',' && */ *cp != ')'; cp++) {
174		if (*cp == '\0')
175			return -1;
176		if (*cp >= '0' && *cp <= '9')
177			part = part * 10 + *cp - '0';
178	}
179	if (part < 0 || part >= MAXPARTITIONS) {
180#ifdef DEBUG
181		printf("%s: invalid partition number (%d)\n", __func__, part);
182#endif
183		return (-1);
184	}
185				/* check out end of dev spec */
186	*devp  = major;
187	*unitp = unit;
188	*partp = part;
189	cp++;
190	if (*cp == ':')
191		cp++;
192	if (*cp == '\0')
193		*fname = "bsd";
194	else
195		*fname = (char *)cp;	/* XXX */
196#ifdef DEBUG
197	printf("%s(%s): major = %d, unit = %d, part = %d, fname = %s\n",
198	    __func__, str, major, unit, part, *fname);
199#endif
200
201	return 0;
202}
203