xref: /openbsd-src/sys/arch/alpha/stand/netboot/devopen.c (revision 0cfa65d9e55f8922eeb5ab08972672f6efb8db40)
1*0cfa65d9Smiod /*	$OpenBSD: devopen.c,v 1.5 2011/06/05 21:49:36 miod Exp $	*/
2dfe00690Smillert /*	$NetBSD: devopen.c,v 1.3 1997/04/06 08:41:25 cgd Exp $	*/
350ce9ee0Sniklas 
450ce9ee0Sniklas /*-
550ce9ee0Sniklas  * Copyright (c) 1992, 1993
650ce9ee0Sniklas  *	The Regents of the University of California.  All rights reserved.
750ce9ee0Sniklas  *
850ce9ee0Sniklas  * This code is derived from software contributed to Berkeley by
950ce9ee0Sniklas  * Ralph Campbell.
1050ce9ee0Sniklas  *
1150ce9ee0Sniklas  * Redistribution and use in source and binary forms, with or without
1250ce9ee0Sniklas  * modification, are permitted provided that the following conditions
1350ce9ee0Sniklas  * are met:
1450ce9ee0Sniklas  * 1. Redistributions of source code must retain the above copyright
1550ce9ee0Sniklas  *    notice, this list of conditions and the following disclaimer.
1650ce9ee0Sniklas  * 2. Redistributions in binary form must reproduce the above copyright
1750ce9ee0Sniklas  *    notice, this list of conditions and the following disclaimer in the
1850ce9ee0Sniklas  *    documentation and/or other materials provided with the distribution.
1929295d1cSmillert  * 3. Neither the name of the University nor the names of its contributors
2050ce9ee0Sniklas  *    may be used to endorse or promote products derived from this software
2150ce9ee0Sniklas  *    without specific prior written permission.
2250ce9ee0Sniklas  *
2350ce9ee0Sniklas  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2450ce9ee0Sniklas  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2550ce9ee0Sniklas  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2650ce9ee0Sniklas  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2750ce9ee0Sniklas  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2850ce9ee0Sniklas  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2950ce9ee0Sniklas  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3050ce9ee0Sniklas  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3150ce9ee0Sniklas  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3250ce9ee0Sniklas  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3350ce9ee0Sniklas  * SUCH DAMAGE.
3450ce9ee0Sniklas  *
3550ce9ee0Sniklas  *	@(#)devopen.c	8.1 (Berkeley) 6/10/93
3650ce9ee0Sniklas  */
3750ce9ee0Sniklas 
38dfe00690Smillert #include <lib/libsa/stand.h>
3950ce9ee0Sniklas 
4050ce9ee0Sniklas /*
4150ce9ee0Sniklas  * Decode the string 'fname', open the device and return the remaining
4250ce9ee0Sniklas  * file name if any.
4350ce9ee0Sniklas  */
44*0cfa65d9Smiod int
devopen(f,fname,file)4550ce9ee0Sniklas devopen(f, fname, file)
4650ce9ee0Sniklas 	struct open_file *f;
4750ce9ee0Sniklas 	const char *fname;
4850ce9ee0Sniklas 	char **file;	/* out */
4950ce9ee0Sniklas {
5050ce9ee0Sniklas 	register char *cp;
5150ce9ee0Sniklas 	register char *ncp;
5250ce9ee0Sniklas 	register struct devsw *dp;
53*0cfa65d9Smiod #if 0
5450ce9ee0Sniklas 	register int c, i;
55*0cfa65d9Smiod #endif
5650ce9ee0Sniklas 	int ctlr = 0, unit = 0, part = 0;
5750ce9ee0Sniklas 	char namebuf[20];
5850ce9ee0Sniklas 	int rc;
5950ce9ee0Sniklas 
6050ce9ee0Sniklas 	cp = (char *)fname;
6150ce9ee0Sniklas 	ncp = namebuf;
6250ce9ee0Sniklas 
6350ce9ee0Sniklas #if 0
6450ce9ee0Sniklas 	/* look for a string like '5/rz0/vmunix' or '5/rz3f/vmunix */
6550ce9ee0Sniklas 	if ((c = *cp) >= '0' && c <= '9') {
6650ce9ee0Sniklas 		ctlr = c - '0';
6750ce9ee0Sniklas 		/* skip the '/' */
6850ce9ee0Sniklas 		if (*++cp != '/')
6950ce9ee0Sniklas 			return (ENXIO);
7050ce9ee0Sniklas 		cp++;
7150ce9ee0Sniklas 		while ((c = *cp) != '\0') {
7250ce9ee0Sniklas 			if (c == '/')
7350ce9ee0Sniklas 				break;
7450ce9ee0Sniklas 			if (c >= '0' && c <= '9') {
7550ce9ee0Sniklas 				/* read unit number */
7650ce9ee0Sniklas 				unit = c - '0';
7750ce9ee0Sniklas 
7850ce9ee0Sniklas 				/* look for a partition */
7950ce9ee0Sniklas 				if ((c = *++cp) >= 'a' && c <= 'h') {
8050ce9ee0Sniklas 					part = c - 'a';
8150ce9ee0Sniklas 					c = *++cp;
8250ce9ee0Sniklas 				}
8350ce9ee0Sniklas 				if (c != '/')
8450ce9ee0Sniklas 					return (ENXIO);
8550ce9ee0Sniklas 				break;
8650ce9ee0Sniklas 			}
8750ce9ee0Sniklas 			if (ncp < namebuf + sizeof(namebuf) - 1)
8850ce9ee0Sniklas 				*ncp++ = c;
8950ce9ee0Sniklas 			cp++;
9050ce9ee0Sniklas 		}
9150ce9ee0Sniklas 		*ncp = '\0';
9250ce9ee0Sniklas 	} else if (strchr(cp, '(')) {
9350ce9ee0Sniklas 		/* expect a string like 'rz(0,0,0)vmunix' */
9450ce9ee0Sniklas 		while ((c = *cp) != '\0') {
9550ce9ee0Sniklas 			if (c == '(') {
9650ce9ee0Sniklas 				cp++;
9750ce9ee0Sniklas 				break;
9850ce9ee0Sniklas 			}
9950ce9ee0Sniklas 			if (ncp < namebuf + sizeof(namebuf) - 1)
10050ce9ee0Sniklas 				*ncp++ = c;
10150ce9ee0Sniklas 			cp++;
10250ce9ee0Sniklas 		}
10350ce9ee0Sniklas 
10450ce9ee0Sniklas 		/* get controller number */
10550ce9ee0Sniklas 		if ((c = *cp) >= '0' && c <= '9') {
10650ce9ee0Sniklas 			ctlr = c - '0';
10750ce9ee0Sniklas 			c = *++cp;
10850ce9ee0Sniklas 		}
10950ce9ee0Sniklas 
11050ce9ee0Sniklas 		if (c == ',') {
11150ce9ee0Sniklas 			/* get SCSI device number */
11250ce9ee0Sniklas 			if ((c = *++cp) >= '0' && c <= '9') {
11350ce9ee0Sniklas 				unit = c - '0';
11450ce9ee0Sniklas 				c = *++cp;
11550ce9ee0Sniklas 			}
11650ce9ee0Sniklas 
11750ce9ee0Sniklas 			if (c == ',') {
11850ce9ee0Sniklas 				/* get partition number */
11950ce9ee0Sniklas 				if ((c = *++cp) >= '0' && c <= '9') {
12050ce9ee0Sniklas 					part = c - '0';
12150ce9ee0Sniklas 					c = *++cp;
12250ce9ee0Sniklas 				}
12350ce9ee0Sniklas 			}
12450ce9ee0Sniklas 		}
12550ce9ee0Sniklas 		if (c != ')')
12650ce9ee0Sniklas 			return (ENXIO);
12750ce9ee0Sniklas 		cp++;
12850ce9ee0Sniklas 		*ncp = '\0';
12950ce9ee0Sniklas 	} else {
13050ce9ee0Sniklas #endif
13150ce9ee0Sniklas 		dp = devsw;
13250ce9ee0Sniklas 		ctlr = unit = part = 0;
13350ce9ee0Sniklas 		goto fnd;
13450ce9ee0Sniklas #if 0
13550ce9ee0Sniklas 	}
13650ce9ee0Sniklas 
13750ce9ee0Sniklas 	for (dp = devsw, i = 0; i < ndevs; dp++, i++)
13850ce9ee0Sniklas 		if (dp->dv_name && strcmp(namebuf, dp->dv_name) == 0)
13950ce9ee0Sniklas 			goto fnd;
14050ce9ee0Sniklas 	printf("Unknown device '%s'\nKnown devices are:", namebuf);
14150ce9ee0Sniklas 	for (dp = devsw, i = 0; i < ndevs; dp++, i++)
14250ce9ee0Sniklas 		if (dp->dv_name)
14350ce9ee0Sniklas 			printf(" %s", dp->dv_name);
14450ce9ee0Sniklas 	printf("\n");
14550ce9ee0Sniklas 	return (ENXIO);
14650ce9ee0Sniklas #endif
14750ce9ee0Sniklas 
14850ce9ee0Sniklas fnd:
14950ce9ee0Sniklas 	rc = (dp->dv_open)(f, ctlr, unit, part);
15050ce9ee0Sniklas 	if (rc)
15150ce9ee0Sniklas 		return (rc);
15250ce9ee0Sniklas 
15350ce9ee0Sniklas 	f->f_dev = dp;
15450ce9ee0Sniklas 	if (file && *cp != '\0')
15550ce9ee0Sniklas 		*file = cp;
15650ce9ee0Sniklas 	return (0);
15750ce9ee0Sniklas }
158