bootxx.c revision 1.9.2.1 1 /* $NetBSD: bootxx.c,v 1.9.2.1 2000/07/27 16:48:26 matt 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. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)boot.c 7.15 (Berkeley) 5/4/91
35 */
36
37 #include "sys/param.h"
38 #include "sys/reboot.h"
39 #include "sys/disklabel.h"
40 #include "sys/exec.h"
41 #include "sys/exec_elf.h"
42
43 #include "lib/libsa/stand.h"
44 #include "lib/libsa/ufs.h"
45 #include "lib/libsa/cd9660.h"
46
47 #include "lib/libkern/libkern.h"
48
49 #include "machine/pte.h"
50 #include "machine/sid.h"
51 #include "machine/mtpr.h"
52 #include "machine/reg.h"
53 #include "machine/rpb.h"
54 #include "../vax/gencons.h"
55
56 #include "../mba/mbareg.h"
57 #include "../mba/hpreg.h"
58
59 #define NRSP 1 /* Kludge */
60 #define NCMD 1 /* Kludge */
61
62 #include "dev/mscp/mscp.h"
63 #include "dev/mscp/mscpreg.h"
64
65 #include "../boot/data.h"
66
67 void Xmain(void);
68 void hoppabort(int);
69 void romread_uvax(int lbn, int size, void *buf, struct rpb *rpb);
70 void hpread(int block);
71 int read750(int block, int *regs);
72 int unit_init(int, struct rpb *, int);
73
74 struct open_file file;
75
76 unsigned *bootregs;
77 struct rpb *rpb;
78 struct bqo *bqo;
79 int vax_cputype;
80 int vax_load_failure;
81 struct udadevice {u_short udaip;u_short udasa;};
82 volatile struct udadevice *csr;
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
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 if (rpb->devtyp == BDEV_SDN)
116 rpb->devtyp = BDEV_SD; /* XXX until driver fixed */
117 } else {
118 bzero(rpb, 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, (caddr_t)(&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 *) ph.p_paddr, ph.p_filesz);
171 memset((void *) (ph.p_paddr + 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 { ufs_open, 0, ufs_read, 0, 0, ufs_stat },
189 { cd9660_open, 0, cd9660_read, 0, 0, cd9660_stat },
190 #if 0
191 { ustarfs_open, 0, ustarfs_read, 0, 0, ustarfs_stat },
192 #endif
193 };
194
195 int nfsys = (sizeof(file_system) / sizeof(struct fs_ops));
196
197 #if 0
198 int tar_open(char *path, struct open_file *f);
199 ssize_t tar_read(struct open_file *f, void *buf, size_t size, size_t *resid);
200
201 int
202 tar_open(path, f)
203 char *path;
204 struct open_file *f;
205 {
206 char *buf = alloc(512);
207
208 bzero(buf, 512);
209 romstrategy(0, 0, 8192, 512, buf, 0);
210 if (bcmp(buf, "boot", 5) || bcmp(&buf[257], "ustar", 5))
211 return EINVAL; /* Not a ustarfs with "boot" first */
212 return 0;
213 }
214
215 ssize_t
216 tar_read(f, buf, size, resid)
217 struct open_file *f;
218 void *buf;
219 size_t size;
220 size_t *resid;
221 {
222 romstrategy(0, 0, (8192+512), size, buf, 0);
223 *resid = size;
224 return 0; /* XXX */
225 }
226 #endif
227
228
229 int
230 devopen(f, fname, file)
231 struct open_file *f;
232 const char *fname;
233 char **file;
234 {
235 *file = (char *)fname;
236
237 if (from == FROM750)
238 return 0;
239 /*
240 * Reinit the VMB boot device.
241 */
242 if (bqo->unit_init) {
243 int initfn;
244
245 initfn = rpb->iovec + bqo->unit_init;
246 if (rpb->devtyp == BDEV_UDA || rpb->devtyp == BDEV_TK) {
247 /*
248 * This reset do not seem to be done in the
249 * ROM routines, so we have to do it manually.
250 */
251 csr = (struct udadevice *)rpb->csrphy;
252 csr->udaip = 0;
253 while ((csr->udasa & MP_STEP1) == 0)
254 ;
255 }
256 /*
257 * AP (R12) have a pointer to the VMB argument list,
258 * wanted by bqo->unit_init.
259 */
260 unit_init(initfn, rpb, bootregs[12]);
261 }
262 return 0;
263 }
264
265 int
266 romstrategy(sc, func, dblk, size, buf, rsize)
267 void *sc;
268 int func;
269 daddr_t dblk;
270 size_t size;
271 void *buf;
272 size_t *rsize;
273 {
274 int block = dblk;
275 int nsize = size;
276
277 if (from == FROMMV) {
278 romread_uvax(block, size, buf, rpb);
279 } else /* if (from == FROM750) */ {
280 while (size > 0) {
281 if (rpb->devtyp == BDEV_HP)
282 hpread(block);
283 else
284 read750(block, bootregs);
285 bcopy(0, buf, 512);
286 size -= 512;
287 (char *)buf += 512;
288 block++;
289 }
290 }
291
292 if (rsize)
293 *rsize = nsize;
294 return 0;
295 }
296
297 /*
298 * The 11/750 boot ROM for Massbus disks doesn't seen to have layout info
299 * for all RP disks (not RP07 at least) so therefore a very small and dumb
300 * device driver is used. It assumes that there is a label on the disk
301 * already that has valid layout info. If there is no label, we can't boot
302 * anyway.
303 */
304
305 #define MBA_WCSR(reg, val) \
306 ((void)(*(volatile u_int32_t *)((adpadr) + (reg)) = (val)));
307 #define MBA_RCSR(reg) \
308 (*(volatile u_int32_t *)((adpadr) + (reg)))
309 #define HP_WCSR(reg, val) \
310 ((void)(*(volatile u_int32_t *)((unitadr) + (reg)) = (val)));
311 #define HP_RCSR(reg) \
312 (*(volatile u_int32_t *)((unitadr) + (reg)))
313
314 void
315 hpread(int bn)
316 {
317 int adpadr = bootregs[1];
318 int unitadr = adpadr + MUREG(bootregs[3], 0);
319 u_int cn, sn, tn;
320 struct disklabel *dp;
321 extern char start;
322
323 dp = (struct disklabel *)(LABELOFFSET + &start);
324 MBA_WCSR(MAPREG(0), PG_V);
325
326 MBA_WCSR(MBA_VAR, 0);
327 MBA_WCSR(MBA_BC, (~512) + 1);
328 #ifdef __GNUC__
329 /*
330 * Avoid four subroutine calls by using hardware division.
331 */
332 asm("clrl r1;movl %3,r0;ediv %4,r0,%0,%1;movl %1,r0;ediv %5,r0,%2,%1"
333 : "=g"(cn),"=g"(sn),"=g"(tn)
334 : "g"(bn),"g"(dp->d_secpercyl),"g"(dp->d_nsectors)
335 : "r0","r1","cc");
336 #else
337 cn = bn / dp->d_secpercyl;
338 sn = bn % dp->d_secpercyl;
339 tn = sn / dp->d_nsectors;
340 sn = sn % dp->d_nsectors;
341 #endif
342 HP_WCSR(HP_DC, cn);
343 HP_WCSR(HP_DA, (tn << 8) | sn);
344 HP_WCSR(HP_CS1, HPCS_READ);
345
346 while (MBA_RCSR(MBA_SR) & MBASR_DTBUSY)
347 ;
348 return;
349 }
350
351 extern char end[];
352 static char *top = (char*)end;
353
354 void *
355 alloc(size)
356 unsigned size;
357 {
358 void *ut = top;
359 top += size;
360 return ut;
361 }
362
363 void
364 free(ptr, size)
365 void *ptr;
366 unsigned size;
367 {
368 }
369
370 int
371 romclose(f)
372 struct open_file *f;
373 {
374 return 0;
375 }
376