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