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