Home | History | Annotate | Line # | Download | only in bootxx
      1 /*	$NetBSD: bootxx.c,v 1.17 2026/03/30 03:32:22 thorpej 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 <sys/exec_aout.h>
     47 #include <machine/prom.h>
     48 
     49 #include <lib/libsa/stand.h>
     50 #include "libsa.h"
     51 #include "bootxx.h"
     52 
     53 int copyboot(struct open_file *, u_long *);
     54 
     55 /*
     56  * Boot device is derived from ROM provided information.
     57  */
     58 #define LOADADDR	0x11000 /* where to load level 2 bootstrap */
     59 				/* (l2 must relocate itself) */
     60 
     61 extern int     	block_size;
     62 extern int     	block_count;	/* length of table */
     63 /* XXX ondisk32 */
     64 extern int32_t 	block_table[];
     65 
     66 extern		char bootprog_name[], bootprog_rev[];
     67 
     68 int main(void);
     69 
     70 int
     71 main(void)
     72 {
     73 	struct open_file f;
     74 	u_long addr;
     75 	char *foo;
     76 	int error;
     77 	int targ = (bugargs.dev_lun >> 4) & 0xf;
     78 	int lun  =  bugargs.dev_lun       & 0xf;
     79 
     80 	printf("Boot: bug device: ctrl=%d, dev=%d.%d\n",
     81 	    bugargs.ctrl_lun, targ, lun);
     82 	printf("\nbootxx: %s first level bootstrap program [%s]\n\n",
     83 	    bootprog_name, bootprog_rev);
     84 
     85 	f.f_flags = F_RAW;
     86 	if (devopen(&f, 0, &foo)) {
     87 		printf("bootxx: open failed\n");
     88 		_rtt();
     89 	}
     90 
     91 	addr = LOADADDR;
     92 	error = copyboot(&f, &addr);
     93 	f.f_dev->dv_close(&f);
     94 	if (error == 0)
     95 		bugexec((void *)addr);
     96 
     97 	/* copyboot had a problem... */
     98 	_rtt();
     99 }
    100 
    101 int
    102 copyboot(struct open_file *fp, u_long *addr)
    103 {
    104 	int	i, blknum;
    105 	size_t	n;
    106 	char	*laddr = (char *) *addr;
    107 	union boothead {
    108 		struct exec ah;
    109 		Elf32_Ehdr eh;
    110 	} *x;
    111 
    112 	x = (union boothead *)laddr;
    113 
    114 	if (!block_count) {
    115 		printf("bootxx: no data!?!\n");
    116 		return -1;
    117 	}
    118 
    119 	for (i = 0; i < block_count; i++) {
    120 		if ((blknum = block_table[i]) == 0)
    121 			break;
    122 #ifdef DEBUG
    123 		printf("bootxx: read block # %d = %d\n", i, blknum);
    124 #endif
    125 		if ((fp->f_dev->dv_strategy)(fp->f_devdata, F_READ,
    126 		    blknum, block_size, laddr, &n)) {
    127 			printf("bootxx: read failed\n");
    128 			return -1;
    129 		}
    130 		if (n != block_size) {
    131 			printf("bootxx: short read\n");
    132 			return -1;
    133 		}
    134 		laddr += block_size;
    135 	}
    136 
    137 	if (N_GETMAGIC(x->ah) == OMAGIC)
    138 		*addr += sizeof(x->ah);
    139 	else if (memcmp(x->eh.e_ident, ELFMAG, SELFMAG) == 0) {
    140 		Elf32_Phdr *ep = (Elf32_Phdr *)(*addr + x->eh.e_phoff);
    141 		*addr += ep->p_offset;
    142 	} else {
    143 		printf("bootxx: secondary bootstrap isn't an executable\n");
    144 		return -1;
    145 	}
    146 
    147 	return 0;
    148 }
    149