1/*	$OpenBSD: dev_i386.c,v 1.1 2019/05/10 21:20:42 mlarkin 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 <lib/libsa/softraid.h>
42#include "softraid_amd64.h"
43#endif
44
45extern int debug;
46
47/* XXX use slot for 'rd' for 'hd' pseudo-device */
48const char bdevs[][4] = {
49	"wd", "", "fd", "", "sd", "st", "cd", "",
50	"", "", "", "", "", "", "", "", "", "hd", ""
51};
52const int nbdevs = nitems(bdevs);
53
54const char cdevs[][4] = {
55	"cn", "", "", "", "", "", "", "",
56	"com", "", "", "", "pc"
57};
58const int ncdevs = nitems(cdevs);
59
60/* pass dev_t to the open routines */
61int
62devopen(struct open_file *f, const char *fname, char **file)
63{
64	struct devsw *dp = devsw;
65	register int i, rc = 1;
66
67	*file = (char *)fname;
68
69#ifdef DEBUG
70	if (debug)
71		printf("devopen:");
72#endif
73
74	for (i = 0; i < ndevs && rc != 0; dp++, i++) {
75#ifdef DEBUG
76		if (debug)
77			printf(" %s: ", dp->dv_name);
78#endif
79		if ((rc = (*dp->dv_open)(f, file)) == 0) {
80			f->f_dev = dp;
81			if (strcmp("TFTP", dp->dv_name) != 0) {
82				/*
83				 * Clear bootmac, to signal that we loaded
84				 * this file from a non-network device.
85				 */
86				extern char *bootmac;
87				bootmac = NULL;
88			}
89			return 0;
90		}
91#ifdef DEBUG
92		else if (debug)
93			printf("%d", rc);
94#endif
95
96	}
97#ifdef DEBUG
98	if (debug)
99		putchar('\n');
100#endif
101
102	if ((f->f_flags & F_NODEV) == 0)
103		f->f_dev = dp;
104
105	return rc;
106}
107
108void
109devboot(dev_t bootdev, char *p)
110{
111#ifdef SOFTRAID
112	struct sr_boot_volume *bv;
113	struct sr_boot_chunk *bc;
114	struct diskinfo *dip = NULL;
115#endif
116	int sr_boot_vol = -1;
117	int part_type = FS_UNUSED;
118
119	if (!bootdev) {
120		*p++ = 't';
121		*p++ = 'f';
122		*p++ = 't';
123		*p++ = 'p';
124		*p = '\0';
125		return;
126	}
127
128#ifdef SOFTRAID
129	/*
130	 * Determine the partition type for the 'a' partition of the
131	 * boot device.
132	 */
133	TAILQ_FOREACH(dip, &disklist, list)
134		if (dip->bios_info.bios_number == bootdev &&
135		    (dip->bios_info.flags & BDI_BADLABEL) == 0)
136			part_type = dip->disklabel.d_partitions[0].p_fstype;
137
138	/*
139	 * See if we booted from a disk that is a member of a bootable
140	 * softraid volume.
141	 */
142	SLIST_FOREACH(bv, &sr_volumes, sbv_link) {
143		if (bv->sbv_flags & BIOC_SCBOOTABLE)
144			SLIST_FOREACH(bc, &bv->sbv_chunks, sbc_link)
145				if (bc->sbc_disk == bootdev)
146					sr_boot_vol = bv->sbv_unit;
147		if (sr_boot_vol != -1)
148			break;
149	}
150#endif
151
152	if (sr_boot_vol != -1 && part_type != FS_BSDFFS) {
153		*p++ = 's';
154		*p++ = 'r';
155		*p++ = '0' + sr_boot_vol;
156	} else if (bootdev & 0x100) {
157		*p++ = 'c';
158		*p++ = 'd';
159		*p++ = '0';
160	} else {
161		if (bootdev & 0x80)
162			*p++ = 'h';
163		else
164			*p++ = 'f';
165		*p++ = 'd';
166		*p++ = '0' + (bootdev & 0x7f);
167	}
168	*p++ = 'a';
169	*p = '\0';
170}
171
172char ttyname_buf[8];
173
174char *
175ttyname(int fd)
176{
177	snprintf(ttyname_buf, sizeof ttyname_buf, "%s%d",
178	    cdevs[major(cn_tab->cn_dev)], minor(cn_tab->cn_dev));
179
180	return ttyname_buf;
181}
182
183dev_t
184ttydev(char *name)
185{
186	int i, unit = -1;
187	char *no = name + strlen(name) - 1;
188
189	while (no >= name && *no >= '0' && *no <= '9')
190		unit = (unit < 0 ? 0 : (unit * 10)) + *no-- - '0';
191	if (no < name || unit < 0)
192		return NODEV;
193	for (i = 0; i < ncdevs; i++)
194		if (strncmp(name, cdevs[i], no - name + 1) == 0)
195			return makedev(i, unit);
196	return NODEV;
197}
198
199int
200cnspeed(dev_t dev, int sp)
201{
202	if (major(dev) == 8)	/* comN */
203		return comspeed(dev, sp);
204
205	/* pc0 and anything else */
206	return 9600;
207}
208