xref: /openbsd/sys/arch/i386/stand/libsa/dev_i386.c (revision cecf84d4)
1 /*	$OpenBSD: dev_i386.c,v 1.40 2014/07/13 09:26:08 jasper Exp $	*/
2 
3 /*
4  * Copyright (c) 1996-1999 Michael Shalayeff
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26  * THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/queue.h>
31 #include <sys/disklabel.h>
32 #include <dev/cons.h>
33 
34 #include "libsa.h"
35 #include "biosdev.h"
36 #include "disk.h"
37 
38 #ifdef SOFTRAID
39 #include <dev/biovar.h>
40 #include <dev/softraidvar.h>
41 #include "softraid.h"
42 #endif
43 
44 extern int debug;
45 
46 /* XXX use slot for 'rd' for 'hd' pseudo-device */
47 const char bdevs[][4] = {
48 	"wd", "", "fd", "", "sd", "st", "cd", "",
49 	"", "", "", "", "", "", "", "", "", "hd", ""
50 };
51 const int nbdevs = nitems(bdevs);
52 
53 const char cdevs[][4] = {
54 	"cn", "", "", "", "", "", "", "",
55 	"com", "", "", "", "pc"
56 };
57 const int ncdevs = nitems(cdevs);
58 
59 /* pass dev_t to the open routines */
60 int
61 devopen(struct open_file *f, const char *fname, char **file)
62 {
63 	struct devsw *dp = devsw;
64 	register int i, rc = 1;
65 
66 	*file = (char *)fname;
67 
68 #ifdef DEBUG
69 	if (debug)
70 		printf("devopen:");
71 #endif
72 
73 	for (i = 0; i < ndevs && rc != 0; dp++, i++) {
74 #ifdef DEBUG
75 		if (debug)
76 			printf(" %s: ", dp->dv_name);
77 #endif
78 		if ((rc = (*dp->dv_open)(f, file)) == 0) {
79 			f->f_dev = dp;
80 			return 0;
81 		}
82 #ifdef DEBUG
83 		else if (debug)
84 			printf("%d", rc);
85 #endif
86 
87 	}
88 #ifdef DEBUG
89 	if (debug)
90 		putchar('\n');
91 #endif
92 
93 	if ((f->f_flags & F_NODEV) == 0)
94 		f->f_dev = dp;
95 
96 	return rc;
97 }
98 
99 void
100 devboot(dev_t bootdev, char *p)
101 {
102 #ifdef SOFTRAID
103 	struct sr_boot_volume *bv;
104 	struct sr_boot_chunk *bc;
105 	struct diskinfo *dip = NULL;
106 #endif
107 	int sr_boot_vol = -1;
108 	int part_type = FS_UNUSED;
109 
110 #ifdef _TEST
111 	*p++ = '/';
112 	*p++ = 'd';
113 	*p++ = 'e';
114 	*p++ = 'v';
115 	*p++ = '/';
116 	*p++ = 'r';
117 #endif
118 
119 #ifdef SOFTRAID
120 	/*
121 	 * Determine the partition type for the 'a' partition of the
122 	 * boot device.
123 	 */
124 	TAILQ_FOREACH(dip, &disklist, list)
125 		if (dip->bios_info.bios_number == bootdev &&
126 		    (dip->bios_info.flags & BDI_BADLABEL) == 0)
127 			part_type = dip->disklabel.d_partitions[0].p_fstype;
128 
129 	/*
130 	 * See if we booted from a disk that is a member of a bootable
131 	 * softraid volume.
132 	 */
133 	SLIST_FOREACH(bv, &sr_volumes, sbv_link) {
134 		if (bv->sbv_flags & BIOC_SCBOOTABLE)
135 			SLIST_FOREACH(bc, &bv->sbv_chunks, sbc_link)
136 				if (bc->sbc_disk == bootdev)
137 					sr_boot_vol = bv->sbv_unit;
138 		if (sr_boot_vol != -1)
139 			break;
140 	}
141 #endif
142 
143 	if (sr_boot_vol != -1 && part_type != FS_BSDFFS) {
144 		*p++ = 's';
145 		*p++ = 'r';
146 		*p++ = '0' + sr_boot_vol;
147 	} else if (bootdev & 0x100) {
148 		*p++ = 'c';
149 		*p++ = 'd';
150 		*p++ = '0';
151 	} else {
152 		if (bootdev & 0x80)
153 			*p++ = 'h';
154 		else
155 			*p++ = 'f';
156 		*p++ = 'd';
157 		*p++ = '0' + (bootdev & 0x7f);
158 	}
159 	*p++ = 'a';
160 	*p = '\0';
161 }
162 
163 char ttyname_buf[8];
164 
165 char *
166 ttyname(int fd)
167 {
168 	snprintf(ttyname_buf, sizeof ttyname_buf, "%s%d",
169 	    cdevs[major(cn_tab->cn_dev)], minor(cn_tab->cn_dev));
170 
171 	return ttyname_buf;
172 }
173 
174 dev_t
175 ttydev(char *name)
176 {
177 	int i, unit = -1;
178 	char *no = name + strlen(name) - 1;
179 
180 	while (no >= name && *no >= '0' && *no <= '9')
181 		unit = (unit < 0 ? 0 : (unit * 10)) + *no-- - '0';
182 	if (no < name || unit < 0)
183 		return NODEV;
184 	for (i = 0; i < ncdevs; i++)
185 		if (strncmp(name, cdevs[i], no - name + 1) == 0)
186 			return makedev(i, unit);
187 	return NODEV;
188 }
189 
190 int
191 cnspeed(dev_t dev, int sp)
192 {
193 	if (major(dev) == 8)	/* comN */
194 		return comspeed(dev, sp);
195 
196 	/* pc0 and anything else */
197 	return 9600;
198 }
199