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