Home | History | Annotate | Line # | Download | only in bootyy
bootyy.c revision 1.3.78.1
      1 /*	$NetBSD: bootyy.c,v 1.3.78.1 2008/05/16 02:23:23 yamt 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 <machine/mon.h>
     46 
     47 #include <stand.h>
     48 #include "libsa.h"
     49 
     50 /* This determines the largest boot program we can load. */
     51 #define MAXBLOCKNUM	64
     52 
     53 /*
     54  * The block size used in the Sun Network Disk protocol.
     55  */
     56 #define SUNND_BSIZE (512)
     57 
     58 /*
     59  * The first block number we request.  This is related to historical
     60  * values; in 4.2BSD, the disklabel and the first stage boot program
     61  * are together called the bootblocks, and take up the first BBSIZE
     62  * bytes of the disk.  In 4.2BSD, BBSIZE was 8192 and the disk device
     63  * block size was 512 bytes (see the definition of SUNND_BSIZE below),
     64  * making the number of blocks taken up by the bootblocks (BBSIZE /
     65  * SUNND_BSIZE).  This value for FIRSTBLOCKNUM assumes that the
     66  * second-stage boot begins immediately after the bootblocks.  The
     67  * second-stage boot always runs contiguously for MAXBLOCKNUM blocks.
     68  * FIRSTBLOCKNUM can be increased, and MAXBLOCKNUM can be adjusted, as
     69  * long as they match up with however you're putting together the disk
     70  * image that is being served to this client.
     71  */
     72 #define FIRSTBLOCKNUM (8192 / SUNND_BSIZE)
     73 
     74 int     	block_size = SUNND_BSIZE;	/* default */
     75 
     76 int
     77 main(void)
     78 {
     79 	struct open_file	f;
     80 	void	*entry;
     81 	char	*addr;
     82 	int n, error;
     83 
     84 #ifdef DEBUG
     85 	printf("bootyy: open...\n");
     86 #endif
     87 	f.f_flags = F_RAW;
     88 	if (devopen(&f, 0, &addr)) {
     89 		printf("bootyy: devopen failed\n");
     90 		return;
     91 	}
     92 
     93 	addr = (char *)KERN_LOADADDR;
     94 	error = copyboot(&f, addr);
     95 	f.f_dev->dv_close(&f);
     96 	if (!error) {
     97 #ifdef DEBUG
     98 		printf("bootyy: start 0x%x\n", (long)addr);
     99 #endif
    100 		entry = addr;
    101 		chain_to(entry);
    102 	}
    103 	/* copyboot had a problem... */
    104 	return;
    105 }
    106 
    107 int
    108 copyboot(struct open_file *fp, char *addr)
    109 {
    110 	int	n, i, blknum;
    111 	char *buf;
    112 
    113 	/* Need to use a buffer that can be mapped into DVMA space. */
    114 	buf = alloc(block_size);
    115 	if (!buf)
    116 		panic("bootyy: alloc failed");
    117 
    118 	for (i = 0, blknum = FIRSTBLOCKNUM; i < MAXBLOCKNUM; i++, blknum++) {
    119 
    120 #ifdef DEBUG
    121 		printf("bootyy: block # %d = %d\n", i, blknum);
    122 #endif
    123 		if ((fp->f_dev->dv_strategy)(fp->f_devdata, F_READ,
    124 					   blknum, block_size, buf, &n))
    125 		{
    126 			printf("bootyy: read failed\n");
    127 			return -1;
    128 		}
    129 		if (n != block_size) {
    130 			printf("bootyy: short read\n");
    131 			return -1;
    132 		}
    133 		memcpy(addr, buf, block_size);
    134 		addr += block_size;
    135 	}
    136 
    137 	return 0;
    138 }
    139