1 /* $OpenBSD: autoconf.c,v 1.30 2024/10/22 21:50:02 jsg Exp $ */
2 /*
3 * Copyright (c) 1998 Steve Murphree, Jr.
4 * Copyright (c) 1996 Nivas Madhur
5 * Copyright (c) 1994 Christian E. Hopps
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Christian E. Hopps.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/buf.h>
37 #include <sys/reboot.h>
38 #include <sys/conf.h>
39 #include <sys/device.h>
40 #include <sys/disklabel.h>
41 #include <sys/kernel.h>
42
43 #include <uvm/uvm.h>
44
45 #include <machine/asm_macro.h> /* enable/disable interrupts */
46 #include <machine/autoconf.h>
47 #include <machine/cpu.h>
48 #include <machine/vmparam.h>
49
50 #include <scsi/scsi_all.h>
51 #include <scsi/scsiconf.h>
52
53 #include <dev/cons.h>
54
55 /*
56 * The following several variables are related to
57 * the configuration process, and are used in initializing
58 * the machine.
59 */
60
61 void dumpconf(void);
62 void get_autoboot_device(void);
63
64 int cold = 1; /* 1 if still booting */
65 dev_t bootdev; /* set by bootloader, retrieved in locore0.S */
66 struct device *bootdv; /* set by device drivers (if found) */
67
68 /*
69 * called at boot time, configure all devices on the system.
70 */
71 void
cpu_configure()72 cpu_configure()
73 {
74 softintr_init();
75
76 if (config_rootfound("mainbus", "mainbus") == 0)
77 panic("no mainbus found");
78
79 /*
80 * Switch to our final trap vectors, and unmap the PROM data area.
81 */
82 set_vbr(kernel_vbr);
83 pmap_unmap_firmware();
84
85 cold = 0;
86
87 /*
88 * Turn external interrupts on.
89 */
90 set_psr(get_psr() & ~PSR_IND);
91 spl0();
92 }
93
94 void
diskconf(void)95 diskconf(void)
96 {
97 printf("boot device: %s\n",
98 (bootdv) ? bootdv->dv_xname : "<unknown>");
99 setroot(bootdv, 0, RB_USERREQ);
100 dumpconf();
101 }
102
103 /*
104 * Get 'auto-boot' information
105 *
106 * XXX Right now we can not handle network boot.
107 */
108 struct autoboot_t {
109 char cont[16];
110 int targ;
111 int part;
112 } autoboot;
113
114 void
get_autoboot_device(void)115 get_autoboot_device(void)
116 {
117 char *value, c;
118 int i, len, part;
119 extern char *nvram_by_symbol(char *); /* machdep.c */
120
121 if ((bootdev & B_MAGICMASK) == B_DEVMAGIC) {
122 #ifdef DEBUG
123 printf("bootdev = 0x%08x (t:%d, a:%d, c:%d, u:%d, p:%d)\n",
124 bootdev, B_TYPE(bootdev), B_ADAPTOR(bootdev),
125 B_CONTROLLER(bootdev), B_UNIT(bootdev),
126 B_PARTITION(bootdev));
127 #endif
128 switch (B_TYPE(bootdev)) {
129 case 0:
130 snprintf(autoboot.cont, sizeof(autoboot.cont),
131 "spc%d", B_CONTROLLER(bootdev));
132 break;
133 #if 0 /* not yet */
134 case 1:
135 snprintf(autoboot.cont, sizeof(autoboot.cont),
136 "le%d", B_CONTROLLER(bootdev));
137 break;
138 #endif
139 default:
140 goto use_nvram_info;
141 }
142 autoboot.targ = B_UNIT(bootdev);
143 autoboot.part = B_PARTITION(bootdev);
144 return;
145 }
146
147 use_nvram_info:
148 /*
149 * Use old method if we can not get bootdev information
150 */
151 printf("%s: no bootdev information, use NVRAM setting\n", __func__);
152
153 /* Assume default controller is internal spc (spc0) */
154 strlcpy(autoboot.cont, "spc0", sizeof(autoboot.cont));
155
156 /* Get boot controller and SCSI target from NVRAM */
157 value = nvram_by_symbol("boot_unit");
158 if (value != NULL) {
159 len = strlen(value);
160 if (len == 1) {
161 c = value[0];
162 } else if (len == 2 && value[0] == '1') {
163 /* External spc (spc1) */
164 strlcpy(autoboot.cont, "spc1", sizeof(autoboot.cont));
165 c = value[1];
166 } else
167 c = -1;
168
169 if ((c >= '0') && (c <= '6'))
170 autoboot.targ = 6 - (c - '0');
171 }
172
173 /* Get partition number from NVRAM */
174 value = nvram_by_symbol("boot_partition");
175 if (value != NULL) {
176 len = strlen(value);
177 part = 0;
178 for (i = 0; i < len; i++)
179 part = part * 10 + (value[i] - '0');
180 autoboot.part = part;
181 }
182 }
183
184 void
device_register(struct device * dev,void * aux)185 device_register(struct device *dev, void *aux)
186 {
187 /*
188 * scsi: sd,cd XXX: Can LUNA-88K boot from CD-ROM?
189 */
190 if (strcmp("sd", dev->dv_cfdata->cf_driver->cd_name) == 0 ||
191 strcmp("cd", dev->dv_cfdata->cf_driver->cd_name) == 0) {
192 struct scsi_attach_args *sa = aux;
193 struct device *spcsc;
194
195 spcsc = dev->dv_parent->dv_parent;
196
197 if (strcmp(spcsc->dv_xname, autoboot.cont) == 0 &&
198 sa->sa_sc_link->target == autoboot.targ &&
199 sa->sa_sc_link->lun == 0) {
200 bootdv = dev;
201 return;
202 }
203 }
204 }
205
206 const struct nam2blk nam2blk[] = {
207 { "sd", 4 },
208 { "cd", 6 },
209 { "rd", 7 },
210 { "vnd", 8 },
211 { "wd", 9 },
212 { NULL, -1 }
213 };
214