Home | History | Annotate | Line # | Download | only in xxboot
bootxx.c revision 1.12
      1 /* $NetBSD: bootxx.c,v 1.12 2000/11/02 17:01:47 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 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 		if (rpb->devtyp == BDEV_SDN)
    117 			rpb->devtyp = BDEV_SD;	/* XXX until driver fixed */
    118 	} else {
    119 		bzero(rpb, sizeof(struct rpb));
    120 		rpb->devtyp = bootregs[0];
    121 		rpb->unit = bootregs[3];
    122 		rpb->rpb_bootr5 = bootregs[5];
    123 		rpb->csrphy = bootregs[2];
    124 		rpb->adpphy = bootregs[1];	/* BI node on 8200 */
    125 		if (rpb->devtyp != BDEV_HP && vax_cputype == VAX_TYP_750)
    126 			rpb->adpphy =
    127 			    (bootregs[1] == 0xffe000 ? 0xf30000 : 0xf32000);
    128         }
    129 	rpb->rpb_base = rpb;
    130 	rpb->iovec = (int)bqo;
    131 
    132 	io = open("/boot.vax", 0);
    133 	if (io < 0)
    134 		io = open("/boot", 0);
    135 	if (io < 0)
    136 		asm("halt");
    137 
    138 	read(io, (void *)&hdr.aout, sizeof(hdr.aout));
    139 	if (N_GETMAGIC(hdr.aout) == OMAGIC && N_GETMID(hdr.aout) == MID_VAX) {
    140 		vax_load_failure++;
    141 		entry = hdr.aout.a_entry;
    142 		if (entry < sizeof(hdr.aout))
    143 			entry = sizeof(hdr.aout);
    144 		read(io, (void *) entry, hdr.aout.a_text + hdr.aout.a_data);
    145 		memset((void *) (entry + hdr.aout.a_text + hdr.aout.a_data),
    146 		       0, hdr.aout.a_bss);
    147 	} else if (memcmp(hdr.elf.e_ident, ELFMAG, SELFMAG) == 0) {
    148 		Elf32_Phdr ph;
    149 		size_t off = sizeof(hdr.elf);
    150 		vax_load_failure += 2;
    151 		read(io, (caddr_t)(&hdr.elf) + sizeof(hdr.aout),
    152 		     sizeof(hdr.elf) - sizeof(hdr.aout));
    153 		if (hdr.elf.e_machine != EM_VAX || hdr.elf.e_type != ET_EXEC
    154 		    || hdr.elf.e_phnum != 1)
    155 			goto die;
    156 		vax_load_failure++;
    157 		entry = hdr.elf.e_entry;
    158 		if (hdr.elf.e_phoff != sizeof(hdr.elf))
    159 			goto die;
    160 		vax_load_failure++;
    161 		read(io, &ph, sizeof(ph));
    162 		off += sizeof(ph);
    163 		if (ph.p_type != PT_LOAD)
    164 			goto die;
    165 		vax_load_failure++;
    166 		while (off < ph.p_offset) {
    167 			u_int32_t tmp;
    168 			read(io, &tmp, sizeof(tmp));
    169 			off += sizeof(tmp);
    170 		}
    171 		read(io, (void *) ph.p_paddr, ph.p_filesz);
    172 		memset((void *) (ph.p_paddr + ph.p_filesz), 0,
    173 		       ph.p_memsz - ph.p_filesz);
    174 	} else {
    175 		goto die;
    176 	}
    177 	hoppabort(entry);
    178 die:
    179 	asm("halt");
    180 }
    181 
    182 /*
    183  * Write an extremely limited version of a (us)tar filesystem, suitable
    184  * for loading secondary-stage boot loader.
    185  * - Can only load file "boot".
    186  * - Must be the first file on tape.
    187  */
    188 struct fs_ops file_system[] = {
    189 	{ ufs_open, 0, ufs_read, 0, 0, ufs_stat },
    190 	{ cd9660_open, 0, cd9660_read, 0, 0, cd9660_stat },
    191 #if 0
    192 	{ ustarfs_open, 0, ustarfs_read, 0, 0, ustarfs_stat },
    193 #endif
    194 };
    195 
    196 int nfsys = (sizeof(file_system) / sizeof(struct fs_ops));
    197 
    198 #if 0
    199 int tar_open(char *path, struct open_file *f);
    200 ssize_t tar_read(struct open_file *f, void *buf, size_t size, size_t *resid);
    201 
    202 int
    203 tar_open(path, f)
    204 	char *path;
    205 	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(f, buf, size, resid)
    218 	struct open_file *f;
    219 	void *buf;
    220 	size_t size;
    221 	size_t *resid;
    222 {
    223 	romstrategy(0, 0, (8192+512), size, buf, 0);
    224 	*resid = size;
    225 	return 0; /* XXX */
    226 }
    227 #endif
    228 
    229 
    230 int
    231 devopen(f, fname, file)
    232 	struct open_file *f;
    233 	const char    *fname;
    234 	char          **file;
    235 {
    236 	*file = (char *)fname;
    237 
    238 	if (from == FROM750)
    239 		return 0;
    240 	/*
    241 	 * Reinit the VMB boot device.
    242 	 */
    243 	if (bqo->unit_init && (moved++ == 0)) {
    244 		int initfn;
    245 
    246 		initfn = rpb->iovec + bqo->unit_init;
    247 		if (rpb->devtyp == BDEV_UDA || rpb->devtyp == BDEV_TK) {
    248 			/*
    249 			 * This reset do not seem to be done in the
    250 			 * ROM routines, so we have to do it manually.
    251 			 */
    252 			csr = (struct udadevice *)rpb->csrphy;
    253 			csr->udaip = 0;
    254 			while ((csr->udasa & MP_STEP1) == 0)
    255 				;
    256 		}
    257 		/*
    258 		 * AP (R12) have a pointer to the VMB argument list,
    259 		 * wanted by bqo->unit_init.
    260 		 */
    261 		unit_init(initfn, rpb, bootregs[12]);
    262 	}
    263 	return 0;
    264 }
    265 
    266 extern struct disklabel romlabel;
    267 
    268 int
    269 romstrategy(sc, func, dblk, size, buf, rsize)
    270 	void    *sc;
    271 	int     func;
    272 	daddr_t dblk;
    273 	size_t	size;
    274 	void    *buf;
    275 	size_t	*rsize;
    276 {
    277 	int	block = dblk;
    278 	int     nsize = size;
    279 
    280 	if (romlabel.d_magic == DISKLABEL && romlabel.d_magic2 == DISKLABEL) {
    281 		if (romlabel.d_npartitions > 1) {
    282 			block += romlabel.d_partitions[0].p_offset;
    283 			if (romlabel.d_partitions[0].p_type == FS_RAID) {
    284 				block += 64; /* XXX */
    285 			}
    286 		}
    287 	}
    288 
    289 	if (from == FROMMV) {
    290 		romread_uvax(block, size, buf, rpb);
    291 	} else /* if (from == FROM750) */ {
    292 		while (size > 0) {
    293 			if (rpb->devtyp == BDEV_HP)
    294 				hpread(block);
    295 			else
    296 				read750(block, bootregs);
    297 			bcopy(0, buf, 512);
    298 			size -= 512;
    299 			(char *)buf += 512;
    300 			block++;
    301 		}
    302 	}
    303 
    304 	if (rsize)
    305 		*rsize = nsize;
    306 	return 0;
    307 }
    308 
    309 /*
    310  * The 11/750 boot ROM for Massbus disks doesn't seen to have layout info
    311  * for all RP disks (not RP07 at least) so therefore a very small and dumb
    312  * device driver is used. It assumes that there is a label on the disk
    313  * already that has valid layout info. If there is no label, we can't boot
    314  * anyway.
    315  */
    316 
    317 #define MBA_WCSR(reg, val) \
    318 	((void)(*(volatile u_int32_t *)((adpadr) + (reg)) = (val)));
    319 #define MBA_RCSR(reg) \
    320 	(*(volatile u_int32_t *)((adpadr) + (reg)))
    321 #define HP_WCSR(reg, val) \
    322 	((void)(*(volatile u_int32_t *)((unitadr) + (reg)) = (val)));
    323 #define HP_RCSR(reg) \
    324 	(*(volatile u_int32_t *)((unitadr) + (reg)))
    325 
    326 void
    327 hpread(int bn)
    328 {
    329 	int adpadr = bootregs[1];
    330 	int unitadr = adpadr + MUREG(bootregs[3], 0);
    331 	u_int cn, sn, tn;
    332 	struct disklabel *dp;
    333 	extern char start;
    334 
    335 	dp = (struct disklabel *)(LABELOFFSET + &start);
    336 	MBA_WCSR(MAPREG(0), PG_V);
    337 
    338 	MBA_WCSR(MBA_VAR, 0);
    339 	MBA_WCSR(MBA_BC, (~512) + 1);
    340 #ifdef __GNUC__
    341 	/*
    342 	 * Avoid four subroutine calls by using hardware division.
    343 	 */
    344 	asm("clrl r1;movl %3,r0;ediv %4,r0,%0,%1;movl %1,r0;ediv %5,r0,%2,%1"
    345 	    : "=g"(cn),"=g"(sn),"=g"(tn)
    346 	    : "g"(bn),"g"(dp->d_secpercyl),"g"(dp->d_nsectors)
    347 	    : "r0","r1","cc");
    348 #else
    349 	cn = bn / dp->d_secpercyl;
    350 	sn = bn % dp->d_secpercyl;
    351 	tn = sn / dp->d_nsectors;
    352 	sn = sn % dp->d_nsectors;
    353 #endif
    354 	HP_WCSR(HP_DC, cn);
    355 	HP_WCSR(HP_DA, (tn << 8) | sn);
    356 	HP_WCSR(HP_CS1, HPCS_READ);
    357 
    358 	while (MBA_RCSR(MBA_SR) & MBASR_DTBUSY)
    359 		;
    360 	return;
    361 }
    362 
    363 extern char end[];
    364 static char *top = (char*)end;
    365 
    366 void *
    367 alloc(size)
    368         unsigned size;
    369 {
    370 	void *ut = top;
    371 	top += size;
    372 	return ut;
    373 }
    374 
    375 void
    376 free(ptr, size)
    377         void *ptr;
    378         unsigned size;
    379 {
    380 }
    381 
    382 int
    383 romclose(f)
    384 	struct open_file *f;
    385 {
    386 	return 0;
    387 }
    388 
    389 #ifdef USE_PRINTF
    390 void
    391 putchar(int ch)
    392 {
    393 	/*
    394 	 * On KA88 we may get C-S/C-Q from the console.
    395 	 * Must obey it.
    396 	 */
    397 	while (mfpr(PR_RXCS) & GC_DON) {
    398 		if ((mfpr(PR_RXDB) & 0x7f) == 19) {
    399 			while (1) {
    400 				while ((mfpr(PR_RXCS) & GC_DON) == 0)
    401 					;
    402 				if ((mfpr(PR_RXDB) & 0x7f) == 17)
    403 					break;
    404 			}
    405 		}
    406 	}
    407 
    408 	while ((mfpr(PR_TXCS) & GC_RDY) == 0)
    409 		;
    410 	mtpr(0, PR_TXCS);
    411 	mtpr(ch & 0377, PR_TXDB);
    412 	if (ch == 10)
    413 		putchar(13);
    414 }
    415 #endif
    416