boot.c revision 1.3 1 /* $NetBSD: boot.c,v 1.3 2001/09/29 01:42:26 minoura Exp $ */
2
3 /*
4 * Copyright (c) 2001 Minoura Makoto
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 AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/param.h>
30 #include <machine/bootinfo.h>
31
32 #include <lib/libsa/stand.h>
33 #include <lib/libsa/loadfile.h>
34 #include <lib/libkern/libkern.h>
35
36 #include "libx68k.h"
37 #include "iocs.h"
38
39 #include "exec_image.h"
40
41
42 #define HEAP_START ((void*) 0x00080000)
43 #define HEAP_END ((void*) 0x000fffff)
44 #define EXSCSI_BDID ((void*) 0x00ea0001)
45 #define SRAM_MEMSIZE (*((long*) 0x00ed0008))
46 /* check whether the bootinf is SCSI or floppy */
47 #define BINF_ISFD(pbinf) (*((char *)(pbinf) + 1) == 0)
48
49 char default_kernel[20] = "sd0a:netbsd";
50 int mpu, bootdev, hostadaptor;
51 int console_device = -1;
52
53 static void help(void);
54 static int get_scsi_host_adapter(void);
55 static void doboot(const char *, int);
56 static void boot(char *);
57 int bootmenu(void);
58 void bootmain(int);
59 extern int detectmpu(void);
60 extern int badbaddr(caddr_t);
61
62 /* from boot_ufs/bootmain.c */
63 static int
64 get_scsi_host_adapter(void)
65 {
66 char *bootrom;
67 int ha;
68
69 bootrom = (char *) (IOCS_BOOTINF() & 0x00ffffe0);
70 /*
71 * bootrom+0x24 "SCSIIN" ... Internal SCSI (spc@0)
72 * "SCSIEX" ... External SCSI (spc@1 or mha@0)
73 */
74 if (*(u_short *)(bootrom + 0x24 + 4) == 0x494e) { /* "IN" */
75 ha = (X68K_BOOT_SCSIIF_SPC << 4) | 0;
76 } else if (badbaddr(EXSCSI_BDID)) {
77 ha = (X68K_BOOT_SCSIIF_MHA << 4) | 0;
78 } else {
79 ha = (X68K_BOOT_SCSIIF_SPC << 4) | 1;
80 }
81
82 return ha;
83 }
84
85
86 static void
87 help(void)
88 {
89 printf("Usage:\n");
90 printf("boot [dev:][file] -[flags]\n");
91 printf(" dev: sd<ID><PART>, ID=0-7, PART=a-p\n");
92 printf(" cd<ID>a, ID=0-7\n");
93 printf(" fd<UNIT>a, UNIT=0-3, format is detected.\n");
94 printf(" file: netbsd, netbsd.gz, etc.\n");
95 printf(" flags: abdqsv\n");
96 }
97
98 static void
99 doboot (const char *file, int flags)
100 {
101 u_long marks[MARK_MAX];
102 int fd;
103 int dev, unit, part;
104 char *name;
105
106 printf("Starting %s, flags 0x%x\n", file, flags);
107 marks[MARK_START] = 0x100000;
108 if ((fd = loadfile(file, marks, LOAD_KERNEL)) == -1)
109 return;
110 close(fd);
111
112 if (devparse(file, &dev, &unit, &part, &name) != 0) {
113 printf("XXX: unknown corruption in /boot.\n");
114 }
115
116 printf("dev = %x, unit = %d, part = %c, name = %s\n",
117 dev, unit, part + 'a', name);
118
119 if (dev == 0) { /* SCSI */
120 dev = X68K_MAKESCSIBOOTDEV(X68K_MAJOR_SD,
121 hostadaptor >> 4,
122 hostadaptor & 15,
123 unit & 7, 0, 0);
124 } else {
125 dev = X68K_MAKEBOOTDEV(X68K_MAJOR_FD, unit & 3, 0);
126 }
127 printf("boot device = %x\n", dev);
128 printf("if = %d, unit = %d, id = %d, lun = %d, part = %c\n",
129 B_X68K_SCSI_IF(dev),
130 B_X68K_SCSI_IF_UN(dev),
131 B_X68K_SCSI_ID(dev),
132 B_X68K_SCSI_LUN(dev),
133 B_X68K_SCSI_PART(dev) + 'a');
134
135 {
136 short *p = ((short*) marks[MARK_ENTRY]) - 1;
137 printf("Kernel Version: 0x%x\n", *p);
138 if (*p != 0x4e73 && *p != 0) {
139 /*
140 * XXX temporary solution; compatibility loader
141 * must be written.
142 */
143 printf("This kernel is too new to be loaded by "
144 "this version of /boot.\n");
145 return;
146 }
147 }
148
149 exec_image(marks[MARK_START], 0, marks[MARK_ENTRY]-marks[MARK_START],
150 marks[MARK_END]-marks[MARK_START], dev, flags);
151
152 return;
153 }
154
155 static void
156 boot(char *arg)
157 {
158 char filename[80];
159 char *p;
160 int flags = 0;
161
162 if (*arg == 0 || *arg == '-') {
163 strcpy(filename, default_kernel);
164 if (*arg == '-')
165 if (parseopts(arg, &flags) == 0) {
166 help();
167 return;
168 }
169 doboot(filename, flags);
170 return;
171 } else {
172 p = gettrailer(arg);
173 if (strchr(arg, ':')) {
174 strcpy(filename, arg);
175 if (arg[strlen(arg) - 1] == ':')
176 strcat(filename, "netbsd");
177 } else {
178 strcpy(filename, default_kernel);
179 strcpy(strchr(filename, ':') + 1, arg);
180 }
181 if (*p == '-') {
182 if (parseopts(p, &flags) == 0)
183 return;
184 } else if (*p != 0) {
185 help();
186 return;
187 }
188
189 doboot(filename, flags);
190 return;
191 }
192 }
193
194 int
195 bootmenu(void)
196 {
197 char input[80];
198 int n = 5, c;
199
200 printf("booting %s - starting in %d seconds. ",
201 default_kernel, n);
202 while (n-- > 0 && (c = awaitkey_1sec()) == 0) {
203 printf("\r");
204 printf("booting %s - starting in %d seconds. ",
205 default_kernel, n);
206 }
207 printf("\r");
208 printf("booting %s - starting in %d seconds. ", default_kernel, 0);
209 printf("\n");
210
211 if (c == 0 || c == '\r') {
212 doboot(default_kernel, 0);
213 printf("Could not start %s; ", default_kernel);
214 strcat(default_kernel, ".gz");
215 printf("trying %s.\n", default_kernel);
216 doboot(default_kernel, 0);
217 printf("Could not start %s; ", default_kernel);
218 exit(1);
219 }
220
221 printf("Please use the absolute unit# (e.g. SCSI ID) instead of the NetBSD ones.\n");
222 for (;;) {
223 char *p, *options;
224
225 printf("> ");
226 gets(input);
227
228 for (p = &input[0]; p - &input[0] < 80 && *p == ' '; p++);
229 options = gettrailer(p);
230 if (strcmp("boot", p) == 0)
231 boot(options);
232 else if (strcmp("help", p) == 0 ||
233 strcmp("?", p) == 0)
234 help();
235 else if (strcmp("reboot", p) == 0)
236 exit(0);
237 else
238 printf("Unknown command %s\n", p);
239 }
240 }
241
242
243 /*
244 * Arguments from the boot block:
245 * bootdev - specifies the device from which /boot was read, in
246 * bootdev format.
247 */
248 void
249 bootmain(int bootdev)
250 {
251 hostadaptor = get_scsi_host_adapter();
252 mpu = detectmpu();
253
254 if (mpu < 3) { /* not tested on 68020 */
255 printf("This MPU cannot run NetBSD.\n");
256 exit(1);
257 }
258 if (SRAM_MEMSIZE < 4*1024*1024) {
259 printf("Main memory too small.\n");
260 exit(1);
261 }
262
263 console_device = consio_init(console_device);
264 setheap(HEAP_START, HEAP_END);
265
266 if (BINF_ISFD(&bootdev)) {
267 default_kernel[0] = 'f';
268 default_kernel[2] = '0' + B_UNIT(bootdev);
269 default_kernel[3] = 'a';
270 } else {
271 default_kernel[2] = '0' + B_X68K_SCSI_ID(bootdev);
272 default_kernel[3] = 'a';
273 }
274 print_title("NetBSD/x68k bootstrap loader version %s", BOOT_VERS);
275 bootmenu();
276 }
277