1*3052da24Sjasper /* $OpenBSD: devopen.c,v 1.11 2014/07/13 09:26:08 jasper Exp $ */
28641b11fStom
38641b11fStom /*
4c28389d0Stom * Copyright (c) 2004 Tom Cosgrove
58641b11fStom * Copyright (c) 1996-1999 Michael Shalayeff
68641b11fStom * All rights reserved.
78641b11fStom *
88641b11fStom * Redistribution and use in source and binary forms, with or without
98641b11fStom * modification, are permitted provided that the following conditions
108641b11fStom * are met:
118641b11fStom * 1. Redistributions of source code must retain the above copyright
128641b11fStom * notice, this list of conditions and the following disclaimer.
138641b11fStom * 2. Redistributions in binary form must reproduce the above copyright
148641b11fStom * notice, this list of conditions and the following disclaimer in the
158641b11fStom * documentation and/or other materials provided with the distribution.
168641b11fStom *
178641b11fStom * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
188641b11fStom * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
198641b11fStom * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
208641b11fStom * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
218641b11fStom * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
228641b11fStom * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
238641b11fStom * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
248641b11fStom * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
258641b11fStom * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
268641b11fStom * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
278641b11fStom * THE POSSIBILITY OF SUCH DAMAGE.
288641b11fStom */
298641b11fStom
308641b11fStom #include "libsa.h"
318641b11fStom #include "biosdev.h"
328641b11fStom #include <sys/param.h>
338641b11fStom #include <dev/cons.h>
348641b11fStom
358641b11fStom extern int debug;
368641b11fStom
378641b11fStom extern char *fs_name[];
388641b11fStom extern int nfsname;
398641b11fStom extern struct devsw netsw[];
408641b11fStom
41c7471d74Stom extern char *bootmac; /* Gets passed to kernel for network boot */
42c7471d74Stom
438641b11fStom /* XXX use slot for 'rd' for 'hd' pseudo-device */
448641b11fStom const char bdevs[][4] = {
45d3d1d3b8Stedu "wd", "", "fd", "", "sd", "st", "cd", "",
463692301cStedu "", "", "", "", "", "", "", "", "", "hd", ""
478641b11fStom };
48cf92b8d0Sjasper const int nbdevs = nitems(bdevs);
498641b11fStom
508641b11fStom const char cdevs[][4] = {
518641b11fStom "cn", "", "", "", "", "", "", "",
528641b11fStom "com", "", "", "", "pc"
538641b11fStom };
54cf92b8d0Sjasper const int ncdevs = nitems(cdevs);
558641b11fStom
568641b11fStom /* pass dev_t to the open routines */
578641b11fStom int
devopen(struct open_file * f,const char * fname,char ** file)588641b11fStom devopen(struct open_file *f, const char *fname, char **file)
598641b11fStom {
608641b11fStom struct devsw *dp = devsw;
618641b11fStom char *p;
628641b11fStom char *stripdev;
638641b11fStom int i, l;
648641b11fStom int rc = 1;
658641b11fStom
668641b11fStom *file = (char *)fname;
678641b11fStom
688641b11fStom #ifdef DEBUG
698641b11fStom if (debug)
708641b11fStom printf("devopen(%s):", fname);
718641b11fStom #endif
728641b11fStom
738641b11fStom /* Make sure we have a prefix, e.g. hd0a: or tftp:. */
748641b11fStom for (p = (char *)fname; *p != ':' && *p != '\0'; ) p++;
758641b11fStom if (*p != ':')
768641b11fStom return 1;
778641b11fStom stripdev = p + 1;
788641b11fStom
798641b11fStom l = p - fname; /* Length of device prefix. */
808641b11fStom for (i = 0; i < nfsname; i++) {
818641b11fStom if ((fs_name[i] != NULL) &&
828641b11fStom (strncmp(fname, fs_name[i], l) == 0)) {
838641b11fStom
848641b11fStom /* Force oopen() etc to use this filesystem. */
858641b11fStom f->f_ops = &file_system[i];
868641b11fStom f->f_dev = dp = &netsw[0];
878641b11fStom
888641b11fStom rc = (*dp->dv_open)(f, NULL);
898641b11fStom if (rc == 0)
908641b11fStom *file = stripdev;
918641b11fStom else
928641b11fStom f->f_dev = NULL;
938641b11fStom #ifdef DEBUG
948641b11fStom if (debug)
958641b11fStom putchar('\n');
968641b11fStom #endif
978641b11fStom return rc;
988641b11fStom }
998641b11fStom }
1008641b11fStom
101c7471d74Stom /*
102c7471d74Stom * Assume that any network filesystems would be caught by the
103c7471d74Stom * code above, so that the next phase of devopen() is only for
104c7471d74Stom * local devices.
105c7471d74Stom *
106c7471d74Stom * Clear bootmac, to signal that we loaded this file from a
107c7471d74Stom * non-network device.
108c7471d74Stom */
109c7471d74Stom bootmac = NULL;
110c7471d74Stom
1118641b11fStom for (i = 0; i < ndevs && rc != 0; dp++, i++) {
1128641b11fStom #ifdef DEBUG
1138641b11fStom if (debug)
1148641b11fStom printf(" %s: ", dp->dv_name);
1158641b11fStom #endif
1168641b11fStom if ((rc = (*dp->dv_open)(f, file)) == 0) {
1178641b11fStom f->f_dev = dp;
1188641b11fStom return 0;
1198641b11fStom }
1208641b11fStom #ifdef DEBUG
1218641b11fStom else if (debug)
1228641b11fStom printf("%d", rc);
1238641b11fStom #endif
1248641b11fStom
1258641b11fStom }
1268641b11fStom #ifdef DEBUG
1278641b11fStom if (debug)
1288641b11fStom putchar('\n');
1298641b11fStom #endif
1308641b11fStom
1318641b11fStom if ((f->f_flags & F_NODEV) == 0)
1328641b11fStom f->f_dev = dp;
1338641b11fStom
1348641b11fStom return rc;
1358641b11fStom }
1368641b11fStom
1378641b11fStom void
devboot(dev_t bootdev,char * p)1388641b11fStom devboot(dev_t bootdev, char *p)
1398641b11fStom {
1408641b11fStom *p++ = 't';
1418641b11fStom *p++ = 'f';
1428641b11fStom *p++ = 't';
1438641b11fStom *p++ = 'p';
1448641b11fStom *p = '\0';
1458641b11fStom }
1468641b11fStom
1478641b11fStom char ttyname_buf[8];
1488641b11fStom
1498641b11fStom char *
ttyname(int fd)1508641b11fStom ttyname(int fd)
1518641b11fStom {
1528641b11fStom snprintf(ttyname_buf, sizeof ttyname_buf, "%s%d",
1538641b11fStom cdevs[major(cn_tab->cn_dev)], minor(cn_tab->cn_dev));
1548641b11fStom
1558641b11fStom return ttyname_buf;
1568641b11fStom }
1578641b11fStom
1588641b11fStom dev_t
ttydev(char * name)1598641b11fStom ttydev(char *name)
1608641b11fStom {
1618641b11fStom int i, unit = -1;
1628641b11fStom char *no = name + strlen(name) - 1;
1638641b11fStom
1648641b11fStom while (no >= name && *no >= '0' && *no <= '9')
1658641b11fStom unit = (unit < 0 ? 0 : (unit * 10)) + *no-- - '0';
1668641b11fStom if (no < name || unit < 0)
1678641b11fStom return NODEV;
1688641b11fStom for (i = 0; i < ncdevs; i++)
1698641b11fStom if (strncmp(name, cdevs[i], no - name + 1) == 0)
1708641b11fStom return (makedev(i, unit));
1718641b11fStom return NODEV;
1728641b11fStom }
1738641b11fStom
1748641b11fStom int
cnspeed(dev_t dev,int sp)1758641b11fStom cnspeed(dev_t dev, int sp)
1768641b11fStom {
1778641b11fStom if (major(dev) == 8) /* comN */
1788641b11fStom return (comspeed(dev, sp));
1798641b11fStom
1808641b11fStom /* pc0 and anything else */
1818641b11fStom return 9600;
1828641b11fStom }
183