bootxx.c revision 1.38 1 /* $NetBSD: bootxx.c,v 1.38 2018/05/01 07:03:47 ragge Exp $ */
2
3 /*-
4 * Copyright (c) 1982, 1986 The Regents of the University of California.
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 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)boot.c 7.15 (Berkeley) 5/4/91
32 */
33
34 #include <sys/param.h>
35 #include <sys/reboot.h>
36 #include <sys/disklabel.h>
37 #include <sys/exec.h>
38 #include <sys/exec_elf.h>
39 #include <sys/exec_aout.h>
40
41 #include <lib/libsa/stand.h>
42 #include <lib/libsa/ufs.h>
43 #include <lib/libsa/cd9660.h>
44 #include <lib/libsa/ustarfs.h>
45
46 #include <lib/libkern/libkern.h>
47
48 #include <machine/pte.h>
49 #include <machine/sid.h>
50 #include <machine/mtpr.h>
51 #include <machine/reg.h>
52 #include <machine/rpb.h>
53 #include "../vax/gencons.h"
54
55 #include "../mba/mbareg.h"
56 #include "../mba/hpreg.h"
57
58 #define NRSP 1 /* Kludge */
59 #define NCMD 1 /* Kludge */
60
61 #include <dev/mscp/mscp.h>
62 #include <dev/mscp/mscpreg.h>
63
64 #include "../boot/data.h"
65
66 #define RF_PROTECTED_SECTORS 64 /* XXX refer to <.../rf_optnames.h> */
67
68 void Xmain(void);
69 void hoppabort(int);
70 void romread_uvax(int lbn, int size, void *buf, struct rpb *rpb);
71 void hpread(int block);
72 int read750(int block, int *regs);
73 int unit_init(int, struct rpb *, int);
74
75 struct open_file file;
76
77 unsigned *bootregs;
78 struct rpb *rpb;
79 struct bqo *bqo;
80 int vax_cputype;
81 int vax_load_failure;
82 struct udadevice {u_short udaip;u_short udasa;};
83 volatile struct udadevice *csr;
84 static int moved;
85
86 extern int from;
87 #define FROM750 1
88 #define FROMMV 2
89 #define FROMVMB 4
90
91 /*
92 * The boot block are used by 11/750, 8200, MicroVAX II/III, VS2000,
93 * VS3100/??, VS4000 and VAX6000/???, and only when booting from disk.
94 */
95 void
96 Xmain(void)
97 {
98 union {
99 struct exec aout;
100 Elf32_Ehdr elf;
101 } hdr;
102 int io;
103 u_long entry;
104
105 vax_cputype = (mfpr(PR_SID) >> 24) & 0xFF;
106 moved = 0;
107 /*
108 */
109 rpb = (void *)0xf0000; /* Safe address right now */
110 bqo = (void *)0xf1000;
111 if (from == FROMMV) {
112 /*
113 * now relocate rpb/bqo (which are used by ROM-routines)
114 */
115 bcopy ((void *)bootregs[11], rpb, sizeof(struct rpb));
116 bcopy ((void*)rpb->iovec, bqo, rpb->iovecsz);
117 } else {
118 memset(rpb, 0, sizeof(struct rpb));
119 rpb->devtyp = bootregs[0];
120 rpb->unit = bootregs[3];
121 rpb->rpb_bootr5 = bootregs[5];
122 rpb->csrphy = bootregs[2];
123 rpb->adpphy = bootregs[1]; /* BI node on 8200 */
124 if (rpb->devtyp != BDEV_HP && vax_cputype == VAX_TYP_750)
125 rpb->adpphy =
126 (bootregs[1] == 0xffe000 ? 0xf30000 : 0xf32000);
127 }
128 rpb->rpb_base = rpb;
129 rpb->iovec = (int)bqo;
130
131 io = open("/boot.vax", 0);
132 if (io < 0)
133 io = open("/boot", 0);
134 if (io < 0)
135 __asm("halt");
136
137 read(io, (void *)&hdr.aout, sizeof(hdr.aout));
138 if (N_GETMAGIC(hdr.aout) == OMAGIC && N_GETMID(hdr.aout) == MID_VAX) {
139 vax_load_failure++;
140 entry = hdr.aout.a_entry;
141 if (entry < sizeof(hdr.aout))
142 entry = sizeof(hdr.aout);
143 read(io, (void *) entry, hdr.aout.a_text + hdr.aout.a_data);
144 memset((void *) (entry + hdr.aout.a_text + hdr.aout.a_data),
145 0, hdr.aout.a_bss);
146 } else if (memcmp(hdr.elf.e_ident, ELFMAG, SELFMAG) == 0) {
147 Elf32_Phdr ph;
148 size_t off = sizeof(hdr.elf);
149 vax_load_failure += 2;
150 read(io, (char *)&hdr.elf + sizeof(hdr.aout),
151 sizeof(hdr.elf) - sizeof(hdr.aout));
152 if (hdr.elf.e_machine != EM_VAX || hdr.elf.e_type != ET_EXEC
153 || hdr.elf.e_phnum != 1)
154 goto die;
155 vax_load_failure++;
156 entry = hdr.elf.e_entry;
157 if (hdr.elf.e_phoff != sizeof(hdr.elf))
158 goto die;
159 vax_load_failure++;
160 read(io, &ph, sizeof(ph));
161 off += sizeof(ph);
162 if (ph.p_type != PT_LOAD)
163 goto die;
164 vax_load_failure++;
165 while (off < ph.p_offset) {
166 u_int32_t tmp;
167 read(io, &tmp, sizeof(tmp));
168 off += sizeof(tmp);
169 }
170 read(io, (void *) hdr.elf.e_entry, ph.p_filesz);
171 memset((void *) (hdr.elf.e_entry + ph.p_filesz), 0,
172 ph.p_memsz - ph.p_filesz);
173 } else {
174 goto die;
175 }
176 hoppabort(entry);
177 die:
178 __asm("halt");
179 }
180
181 /*
182 * Write an extremely limited version of a (us)tar filesystem, suitable
183 * for loading secondary-stage boot loader.
184 * - Can only load file "boot".
185 * - Must be the first file on tape.
186 */
187 struct fs_ops file_system[] = {
188 #ifdef NEED_UFS
189 { ffsv1_open, 0, ffsv1_read, 0, 0, ffsv1_stat },
190 { ffsv2_open, 0, ffsv2_read, 0, 0, ffsv2_stat },
191 #endif
192 #ifdef NEED_CD9660
193 { cd9660_open, 0, cd9660_read, 0, 0, cd9660_stat },
194 #endif
195 #ifdef NEED_USTARFS
196 { ustarfs_open, 0, ustarfs_read, 0, 0, ustarfs_stat },
197 #endif
198 };
199
200 int nfsys = __arraycount(file_system);
201
202 #if 0
203 int tar_open(char *path, struct open_file *f);
204 ssize_t tar_read(struct open_file *f, void *buf, size_t size, size_t *resid);
205
206 int
207 tar_open(char *path, struct open_file *f)
208 {
209 char *buf = alloc(512);
210
211 memset(buf, 0, 512);
212 romstrategy(0, 0, 8192, 512, buf, 0);
213 if (memcmp(buf, "boot", 5) || memcmp(&buf[257], "ustar", 5))
214 return EINVAL; /* Not a ustarfs with "boot" first */
215 return 0;
216 }
217
218 ssize_t
219 tar_read(struct open_file *f, void *buf, size_t size, size_t *resid)
220 {
221 romstrategy(0, 0, (8192+512), size, buf, 0);
222 *resid = size;
223 return 0; /* XXX */
224 }
225 #endif
226
227
228 int
229 devopen(struct open_file *f, const char *fname, char **file)
230 {
231 *file = (char *)fname;
232
233 if (from == FROM750)
234 return 0;
235 /*
236 * Reinit the VMB boot device.
237 */
238 if (bqo->unit_init && (moved++ == 0)) {
239 int initfn;
240
241 initfn = rpb->iovec + bqo->unit_init;
242 if (rpb->devtyp == BDEV_UDA || rpb->devtyp == BDEV_TK) {
243 /*
244 * This reset do not seem to be done in the
245 * ROM routines, so we have to do it manually.
246 */
247 csr = (struct udadevice *)rpb->csrphy;
248 csr->udaip = 0;
249 while ((csr->udasa & MP_STEP1) == 0)
250 ;
251 }
252 /*
253 * AP (R12) have a pointer to the VMB argument list,
254 * wanted by bqo->unit_init.
255 */
256 unit_init(initfn, rpb, bootregs[12]);
257 }
258 return 0;
259 }
260
261 extern struct disklabel romlabel;
262
263 int
264 romstrategy(void *sc, int func, daddr_t dblk, size_t size, void *buf, size_t *rsize)
265 {
266 int block = dblk;
267 int nsize = size;
268 char *cbuf;
269
270 cbuf = (char *)buf;
271
272 if (romlabel.d_magic == DISKMAGIC && romlabel.d_magic2 == DISKMAGIC) {
273 if (romlabel.d_npartitions > 1) {
274 block += romlabel.d_partitions[0].p_offset;
275 if (romlabel.d_partitions[0].p_fstype == FS_RAID) {
276 block += RF_PROTECTED_SECTORS;
277 }
278 }
279 }
280
281 if (from == FROMMV) {
282 romread_uvax(block, size, cbuf, rpb);
283 } else /* if (from == FROM750) */ {
284 while (size > 0) {
285 if (rpb->devtyp == BDEV_HP)
286 hpread(block);
287 else
288 read750(block, (int *)bootregs);
289 memcpy(cbuf, 0, 512);
290 size -= 512;
291 cbuf += 512;
292 block++;
293 }
294 }
295
296 if (rsize)
297 *rsize = nsize;
298 return 0;
299 }
300
301 /*
302 * The 11/750 boot ROM for Massbus disks doesn't seen to have layout info
303 * for all RP disks (not RP07 at least) so therefore a very small and dumb
304 * device driver is used. It assumes that there is a label on the disk
305 * already that has valid layout info. If there is no label, we can't boot
306 * anyway.
307 */
308
309 #define MBA_WCSR(reg, val) \
310 ((void)(*(volatile u_int32_t *)((adpadr) + (reg)) = (val)));
311 #define MBA_RCSR(reg) \
312 (*(volatile u_int32_t *)((adpadr) + (reg)))
313 #define HP_WCSR(reg, val) \
314 ((void)(*(volatile u_int32_t *)((unitadr) + (reg)) = (val)));
315 #define HP_RCSR(reg) \
316 (*(volatile u_int32_t *)((unitadr) + (reg)))
317
318 void
319 hpread(int bn)
320 {
321 int adpadr = bootregs[1];
322 int unitadr = adpadr + MUREG(bootregs[3], 0);
323 u_int cn, sn, tn;
324 struct disklabel *dp;
325 extern char start;
326
327 dp = (struct disklabel *)(LABELOFFSET + &start);
328 MBA_WCSR(MAPREG(0), PG_V);
329
330 MBA_WCSR(MBA_VAR, 0);
331 MBA_WCSR(MBA_BC, (~512) + 1);
332 #ifdef __GNUC__
333 /*
334 * Avoid four subroutine calls by using hardware division.
335 */
336 __asm("clrl %%r1;"
337 "movl %3,%%r0;"
338 "ediv %4,%%r0,%0,%1;"
339 "movl %1,%%r0;"
340 "ediv %5,%%r0,%2,%1"
341 : "=g"(cn),"=g"(sn),"=g"(tn)
342 : "g"(bn),"g"(dp->d_secpercyl),"g"(dp->d_nsectors)
343 : "r0","r1","cc");
344 #else
345 cn = bn / dp->d_secpercyl;
346 sn = bn % dp->d_secpercyl;
347 tn = sn / dp->d_nsectors;
348 sn = sn % dp->d_nsectors;
349 #endif
350 HP_WCSR(HP_DC, cn);
351 HP_WCSR(HP_DA, (tn << 8) | sn);
352 HP_WCSR(HP_CS1, HPCS_READ);
353
354 while (MBA_RCSR(MBA_SR) & MBASR_DTBUSY)
355 ;
356 return;
357 }
358
359 extern char end[];
360 static char *top = (char*)end;
361
362 void *
363 alloc(size_t size)
364 {
365 void *ut = top;
366 top += size;
367 return ut;
368 }
369
370 void
371 dealloc(void *ptr, size_t size)
372 {
373 }
374
375 int
376 romclose(struct open_file *f)
377 {
378 return 0;
379 }
380
381 #ifdef USE_PRINTF
382 void
383 putchar(int ch)
384 {
385 /*
386 * On KA88 we may get C-S/C-Q from the console.
387 * Must obey it.
388 */
389 while (mfpr(PR_RXCS) & GC_DON) {
390 if ((mfpr(PR_RXDB) & 0x7f) == 19) {
391 while (1) {
392 while ((mfpr(PR_RXCS) & GC_DON) == 0)
393 ;
394 if ((mfpr(PR_RXDB) & 0x7f) == 17)
395 break;
396 }
397 }
398 }
399
400 while ((mfpr(PR_TXCS) & GC_RDY) == 0)
401 ;
402 mtpr(0, PR_TXCS);
403 mtpr(ch & 0377, PR_TXDB);
404 if (ch == 10)
405 putchar(13);
406 }
407 #endif
408