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