Home | History | Annotate | Line # | Download | only in bootxx
bootxx.c revision 1.1
      1  1.1  chuck /*	$NetBSD: bootxx.c,v 1.1 1996/05/17 20:11:32 chuck Exp $ */
      2  1.1  chuck 
      3  1.1  chuck /*
      4  1.1  chuck  * Copyright (c) 1994 Paul Kranenburg
      5  1.1  chuck  * All rights reserved.
      6  1.1  chuck  *
      7  1.1  chuck  * Redistribution and use in source and binary forms, with or without
      8  1.1  chuck  * modification, are permitted provided that the following conditions
      9  1.1  chuck  * are met:
     10  1.1  chuck  * 1. Redistributions of source code must retain the above copyright
     11  1.1  chuck  *    notice, this list of conditions and the following disclaimer.
     12  1.1  chuck  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  chuck  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  chuck  *    documentation and/or other materials provided with the distribution.
     15  1.1  chuck  * 3. All advertising materials mentioning features or use of this software
     16  1.1  chuck  *    must display the following acknowledgement:
     17  1.1  chuck  *      This product includes software developed by Paul Kranenburg.
     18  1.1  chuck  * 4. The name of the author may not be used to endorse or promote products
     19  1.1  chuck  *    derived from this software without specific prior written permission
     20  1.1  chuck  *
     21  1.1  chuck  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.1  chuck  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.1  chuck  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.1  chuck  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.1  chuck  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  1.1  chuck  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  1.1  chuck  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  1.1  chuck  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  1.1  chuck  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  1.1  chuck  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.1  chuck  */
     32  1.1  chuck 
     33  1.1  chuck /*
     34  1.1  chuck  * This is a generic "first-stage" boot program.
     35  1.1  chuck  *
     36  1.1  chuck  * Note that this program has absolutely no filesystem knowledge!
     37  1.1  chuck  *
     38  1.1  chuck  * Instead, this uses a table of disk block numbers that are
     39  1.1  chuck  * filled in by the installboot program such that this program
     40  1.1  chuck  * can load the "second-stage" boot program.
     41  1.1  chuck  */
     42  1.1  chuck 
     43  1.1  chuck #include <sys/param.h>
     44  1.1  chuck #include <sys/time.h>
     45  1.1  chuck #include <sys/exec.h>
     46  1.1  chuck #include <machine/prom.h>
     47  1.1  chuck 
     48  1.1  chuck #include "stand.h"
     49  1.1  chuck #include "libsa.h"
     50  1.1  chuck 
     51  1.1  chuck /*
     52  1.1  chuck  * Boot device is derived from ROM provided information.
     53  1.1  chuck  */
     54  1.1  chuck #define LOADADDR	0x11000 /* where to load level 2 bootstrap */
     55  1.1  chuck 				/* (l2 must relocate itself) */
     56  1.1  chuck 
     57  1.1  chuck /* This determines the largest boot program we can load. */
     58  1.1  chuck #define MAXBLOCKNUM	64
     59  1.1  chuck 
     60  1.1  chuck /*
     61  1.1  chuck  * These three names are known by installboot.
     62  1.1  chuck  * The block_table contains starting block numbers,
     63  1.1  chuck  * in terms of 512-byte blocks.  Each non-zero value
     64  1.1  chuck  * will result in a read of block_size bytes.
     65  1.1  chuck  */
     66  1.1  chuck int     	block_size = 512;	/* default */
     67  1.1  chuck int     	block_count = MAXBLOCKNUM;	/* length of table */
     68  1.1  chuck daddr_t 	block_table[MAXBLOCKNUM] = { 0 };
     69  1.1  chuck 
     70  1.1  chuck extern		char *version;
     71  1.1  chuck 
     72  1.1  chuck 
     73  1.1  chuck main()
     74  1.1  chuck {
     75  1.1  chuck 	struct open_file	f;
     76  1.1  chuck 	char	*addr;
     77  1.1  chuck 	int n, error;
     78  1.1  chuck 
     79  1.1  chuck 	printf("Boot: bug device: ctrl=%d, dev=%d\n",
     80  1.1  chuck 		bugargs.ctrl_lun, bugargs.dev_lun);
     81  1.1  chuck 	printf("\nbootxx: first level bootstrap program [%s]\n\n", version);
     82  1.1  chuck 
     83  1.1  chuck 	f.f_flags = F_RAW;
     84  1.1  chuck 	if (devopen(&f, 0, &addr)) {
     85  1.1  chuck 		printf("bootxx: open failed\n");
     86  1.1  chuck 		_rtt();
     87  1.1  chuck 	}
     88  1.1  chuck 
     89  1.1  chuck 	addr = (char*)LOADADDR;
     90  1.1  chuck 	error = copyboot(&f, addr);
     91  1.1  chuck 	f.f_dev->dv_close(&f);
     92  1.1  chuck 	if (!error) {
     93  1.1  chuck 		bugexec((void (*)())addr);
     94  1.1  chuck 	}
     95  1.1  chuck 	/* copyboot had a problem... */
     96  1.1  chuck 	_rtt();
     97  1.1  chuck }
     98  1.1  chuck 
     99  1.1  chuck int
    100  1.1  chuck copyboot(fp, addr)
    101  1.1  chuck 	struct open_file	*fp;
    102  1.1  chuck 	char			*addr;
    103  1.1  chuck {
    104  1.1  chuck 	int	n, i, blknum;
    105  1.1  chuck 	struct exec *x;
    106  1.1  chuck 
    107  1.1  chuck 	addr -= sizeof(struct exec); /* assume OMAGIC, verify below */
    108  1.1  chuck 	x = (struct exec *)addr;
    109  1.1  chuck 
    110  1.1  chuck 	if (!block_count) {
    111  1.1  chuck 		printf("bootxx: no data!?!\n");
    112  1.1  chuck 		return -1;
    113  1.1  chuck 	}
    114  1.1  chuck 
    115  1.1  chuck 	for (i = 0; i < block_count; i++) {
    116  1.1  chuck 
    117  1.1  chuck 		if ((blknum = block_table[i]) == 0)
    118  1.1  chuck 			break;
    119  1.1  chuck 
    120  1.1  chuck #ifdef DEBUG
    121  1.1  chuck 		printf("bootxx: read block # %d = %d\n", i, blknum);
    122  1.1  chuck #endif
    123  1.1  chuck 		if ((fp->f_dev->dv_strategy)(fp->f_devdata, F_READ,
    124  1.1  chuck 					   blknum, block_size, addr, &n))
    125  1.1  chuck 		{
    126  1.1  chuck 			printf("bootxx: read failed\n");
    127  1.1  chuck 			return -1;
    128  1.1  chuck 		}
    129  1.1  chuck 		if (n != block_size) {
    130  1.1  chuck 			printf("bootxx: short read\n");
    131  1.1  chuck 			return -1;
    132  1.1  chuck 		}
    133  1.1  chuck 		addr += block_size;
    134  1.1  chuck 	}
    135  1.1  chuck 
    136  1.1  chuck 	if (N_GETMAGIC(*x) != OMAGIC) {
    137  1.1  chuck 		printf("bootxx: secondary bootstrap isn't in OMAGIC format\n");
    138  1.1  chuck 		return(-1);
    139  1.1  chuck 	}
    140  1.1  chuck 
    141  1.1  chuck 	return 0;
    142  1.1  chuck }
    143