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