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