xref: /openbsd-src/sys/arch/i386/stand/pxeboot/devopen.c (revision 3052da2458a13a0594a388815c223e864f79d20b)
1*3052da24Sjasper /*	$OpenBSD: devopen.c,v 1.11 2014/07/13 09:26:08 jasper Exp $	*/
2ef7aef7bStom 
3ef7aef7bStom /*
4c28389d0Stom  * Copyright (c) 2004 Tom Cosgrove
5ef7aef7bStom  * Copyright (c) 1996-1999 Michael Shalayeff
6ef7aef7bStom  * All rights reserved.
7ef7aef7bStom  *
8ef7aef7bStom  * Redistribution and use in source and binary forms, with or without
9ef7aef7bStom  * modification, are permitted provided that the following conditions
10ef7aef7bStom  * are met:
11ef7aef7bStom  * 1. Redistributions of source code must retain the above copyright
12ef7aef7bStom  *    notice, this list of conditions and the following disclaimer.
13ef7aef7bStom  * 2. Redistributions in binary form must reproduce the above copyright
14ef7aef7bStom  *    notice, this list of conditions and the following disclaimer in the
15ef7aef7bStom  *    documentation and/or other materials provided with the distribution.
16ef7aef7bStom  *
17ef7aef7bStom  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18ef7aef7bStom  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19ef7aef7bStom  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20ef7aef7bStom  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
21ef7aef7bStom  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22ef7aef7bStom  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23ef7aef7bStom  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24ef7aef7bStom  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25ef7aef7bStom  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26ef7aef7bStom  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27ef7aef7bStom  * THE POSSIBILITY OF SUCH DAMAGE.
28ef7aef7bStom  */
29ef7aef7bStom 
30ef7aef7bStom #include "libsa.h"
31ef7aef7bStom #include "biosdev.h"
32ef7aef7bStom #include <sys/param.h>
33ef7aef7bStom #include <dev/cons.h>
34ef7aef7bStom 
35ef7aef7bStom extern int debug;
36ef7aef7bStom 
37ef7aef7bStom extern char *fs_name[];
38ef7aef7bStom extern int nfsname;
39ef7aef7bStom extern struct devsw netsw[];
40ef7aef7bStom 
418314303bStom extern char *bootmac;		/* Gets passed to kernel for network boot */
428314303bStom 
43ef7aef7bStom /* XXX use slot for 'rd' for 'hd' pseudo-device */
44ef7aef7bStom const char bdevs[][4] = {
45d3d1d3b8Stedu 	"wd", "", "fd", "", "sd", "st", "cd", "",
463692301cStedu 	"", "", "", "", "", "", "", "", "", "hd", ""
47ef7aef7bStom };
48cf92b8d0Sjasper const int nbdevs = nitems(bdevs);
49ef7aef7bStom 
50ef7aef7bStom const char cdevs[][4] = {
51ef7aef7bStom 	"cn", "", "", "", "", "", "", "",
52ef7aef7bStom 	"com", "", "", "", "pc"
53ef7aef7bStom };
54cf92b8d0Sjasper const int ncdevs = nitems(cdevs);
55ef7aef7bStom 
56ef7aef7bStom /* pass dev_t to the open routines */
57ef7aef7bStom int
devopen(struct open_file * f,const char * fname,char ** file)58ef7aef7bStom devopen(struct open_file *f, const char *fname, char **file)
59ef7aef7bStom {
60ef7aef7bStom 	struct devsw *dp = devsw;
61ef7aef7bStom 	char *p;
62ef7aef7bStom 	char *stripdev;
63ef7aef7bStom 	int i, l;
64ef7aef7bStom 	int rc = 1;
65ef7aef7bStom 
66ef7aef7bStom 	*file = (char *)fname;
67ef7aef7bStom 
68ef7aef7bStom #ifdef DEBUG
69ef7aef7bStom 	if (debug)
70ef7aef7bStom 		printf("devopen(%s):", fname);
71ef7aef7bStom #endif
72ef7aef7bStom 
73ef7aef7bStom 	/* Make sure we have a prefix, e.g. hd0a: or tftp:. */
74ef7aef7bStom 	for (p = (char *)fname; *p != ':' && *p != '\0'; ) p++;
75ef7aef7bStom 	if (*p != ':')
76ef7aef7bStom 		return 1;
77ef7aef7bStom 	stripdev = p + 1;
78ef7aef7bStom 
79ef7aef7bStom 	l = p - fname;			/* Length of device prefix. */
80ef7aef7bStom 	for (i = 0; i < nfsname; i++) {
81ef7aef7bStom 		if ((fs_name[i] != NULL) &&
82ef7aef7bStom 		    (strncmp(fname, fs_name[i], l) == 0)) {
83ef7aef7bStom 
84ef7aef7bStom 			/* Force oopen() etc to use this filesystem. */
85ef7aef7bStom 			f->f_ops = &file_system[i];
86ef7aef7bStom 			f->f_dev = dp = &netsw[0];
87ef7aef7bStom 
88ef7aef7bStom 			rc = (*dp->dv_open)(f, NULL);
89ef7aef7bStom 			if (rc == 0)
90ef7aef7bStom 				*file = stripdev;
91ef7aef7bStom 			else
92ef7aef7bStom 				f->f_dev = NULL;
93ef7aef7bStom #ifdef DEBUG
94ef7aef7bStom 			if (debug)
95ef7aef7bStom 				putchar('\n');
96ef7aef7bStom #endif
97ef7aef7bStom 			return rc;
98ef7aef7bStom 		}
99ef7aef7bStom 	}
100ef7aef7bStom 
1018314303bStom 	/*
1028314303bStom 	 * Assume that any network filesystems would be caught by the
1038314303bStom 	 * code above, so that the next phase of devopen() is only for
1048314303bStom 	 * local devices.
1058314303bStom 	 *
1068314303bStom 	 * Clear bootmac, to signal that we loaded this file from a
1078314303bStom 	 * non-network device.
1088314303bStom 	 */
1098314303bStom 	bootmac = NULL;
1108314303bStom 
111ef7aef7bStom 	for (i = 0; i < ndevs && rc != 0; dp++, i++) {
112ef7aef7bStom #ifdef DEBUG
113ef7aef7bStom 		if (debug)
114ef7aef7bStom 			printf(" %s: ", dp->dv_name);
115ef7aef7bStom #endif
116ef7aef7bStom 		if ((rc = (*dp->dv_open)(f, file)) == 0) {
117ef7aef7bStom 			f->f_dev = dp;
118ef7aef7bStom 			return 0;
119ef7aef7bStom 		}
120ef7aef7bStom #ifdef DEBUG
121ef7aef7bStom 		else if (debug)
122ef7aef7bStom 			printf("%d", rc);
123ef7aef7bStom #endif
124ef7aef7bStom 
125ef7aef7bStom 	}
126ef7aef7bStom #ifdef DEBUG
127ef7aef7bStom 	if (debug)
128ef7aef7bStom 		putchar('\n');
129ef7aef7bStom #endif
130ef7aef7bStom 
131ef7aef7bStom 	if ((f->f_flags & F_NODEV) == 0)
132ef7aef7bStom 		f->f_dev = dp;
133ef7aef7bStom 
134ef7aef7bStom 	return rc;
135ef7aef7bStom }
136ef7aef7bStom 
137ef7aef7bStom void
devboot(dev_t bootdev,char * p)138ef7aef7bStom devboot(dev_t bootdev, char *p)
139ef7aef7bStom {
140ef7aef7bStom 	*p++ = 't';
141ef7aef7bStom 	*p++ = 'f';
142ef7aef7bStom 	*p++ = 't';
143ef7aef7bStom 	*p++ = 'p';
144ef7aef7bStom 	*p = '\0';
145ef7aef7bStom }
146ef7aef7bStom 
147ef7aef7bStom char ttyname_buf[8];
148ef7aef7bStom 
149ef7aef7bStom char *
ttyname(int fd)150ef7aef7bStom ttyname(int fd)
151ef7aef7bStom {
152ef7aef7bStom 	snprintf(ttyname_buf, sizeof ttyname_buf, "%s%d",
153ef7aef7bStom 	    cdevs[major(cn_tab->cn_dev)], minor(cn_tab->cn_dev));
154ef7aef7bStom 
155ef7aef7bStom 	return ttyname_buf;
156ef7aef7bStom }
157ef7aef7bStom 
158ef7aef7bStom dev_t
ttydev(char * name)159ef7aef7bStom ttydev(char *name)
160ef7aef7bStom {
161ef7aef7bStom 	int i, unit = -1;
162ef7aef7bStom 	char *no = name + strlen(name) - 1;
163ef7aef7bStom 
164ef7aef7bStom 	while (no >= name && *no >= '0' && *no <= '9')
165ef7aef7bStom 		unit = (unit < 0 ? 0 : (unit * 10)) + *no-- - '0';
166ef7aef7bStom 	if (no < name || unit < 0)
167ef7aef7bStom 		return NODEV;
168ef7aef7bStom 	for (i = 0; i < ncdevs; i++)
169ef7aef7bStom 		if (strncmp(name, cdevs[i], no - name + 1) == 0)
170ef7aef7bStom 			return (makedev(i, unit));
171ef7aef7bStom 	return NODEV;
172ef7aef7bStom }
173ef7aef7bStom 
174ef7aef7bStom int
cnspeed(dev_t dev,int sp)175ef7aef7bStom cnspeed(dev_t dev, int sp)
176ef7aef7bStom {
177ef7aef7bStom 	if (major(dev) == 8)	/* comN */
178ef7aef7bStom 		return (comspeed(dev, sp));
179ef7aef7bStom 
180ef7aef7bStom 	/* pc0 and anything else */
181ef7aef7bStom 	return 9600;
182ef7aef7bStom }
183