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