Home | History | Annotate | Line # | Download | only in bootyy
bootyy.c revision 1.1.32.1
      1  1.1.32.1      kent /*	$NetBSD: bootyy.c,v 1.1.32.1 2005/04/29 11:28:27 kent Exp $ */
      2       1.1  fredette 
      3       1.1  fredette /*-
      4       1.1  fredette  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5       1.1  fredette  * All rights reserved.
      6       1.1  fredette  *
      7       1.1  fredette  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1  fredette  * by Paul Kranenburg.
      9       1.1  fredette  *
     10       1.1  fredette  * Redistribution and use in source and binary forms, with or without
     11       1.1  fredette  * modification, are permitted provided that the following conditions
     12       1.1  fredette  * are met:
     13       1.1  fredette  * 1. Redistributions of source code must retain the above copyright
     14       1.1  fredette  *    notice, this list of conditions and the following disclaimer.
     15       1.1  fredette  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1  fredette  *    notice, this list of conditions and the following disclaimer in the
     17       1.1  fredette  *    documentation and/or other materials provided with the distribution.
     18       1.1  fredette  * 3. All advertising materials mentioning features or use of this software
     19       1.1  fredette  *    must display the following acknowledgement:
     20       1.1  fredette  *        This product includes software developed by the NetBSD
     21       1.1  fredette  *        Foundation, Inc. and its contributors.
     22       1.1  fredette  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.1  fredette  *    contributors may be used to endorse or promote products derived
     24       1.1  fredette  *    from this software without specific prior written permission.
     25       1.1  fredette  *
     26       1.1  fredette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.1  fredette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.1  fredette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.1  fredette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.1  fredette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.1  fredette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.1  fredette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.1  fredette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.1  fredette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.1  fredette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.1  fredette  * POSSIBILITY OF SUCH DAMAGE.
     37       1.1  fredette  */
     38       1.1  fredette 
     39       1.1  fredette /*
     40       1.1  fredette  * This is a generic "first-stage" boot program.
     41       1.1  fredette  *
     42       1.1  fredette  * Note that this program has absolutely no filesystem knowledge!
     43       1.1  fredette  *
     44       1.1  fredette  * Instead, this uses a table of disk block numbers that are
     45       1.1  fredette  * filled in by the installboot program such that this program
     46       1.1  fredette  * can load the "second-stage" boot program.
     47       1.1  fredette  */
     48       1.1  fredette 
     49       1.1  fredette #include <sys/param.h>
     50       1.1  fredette #include <sys/time.h>
     51       1.1  fredette #include <sys/exec.h>
     52       1.1  fredette #include <machine/mon.h>
     53       1.1  fredette 
     54       1.1  fredette #include <stand.h>
     55       1.1  fredette #include "libsa.h"
     56       1.1  fredette 
     57       1.1  fredette /* This determines the largest boot program we can load. */
     58       1.1  fredette #define MAXBLOCKNUM	64
     59       1.1  fredette 
     60       1.1  fredette /*
     61       1.1  fredette  * The block size used in the Sun Network Disk protocol.
     62       1.1  fredette  */
     63       1.1  fredette #define SUNND_BSIZE (512)
     64       1.1  fredette 
     65       1.1  fredette /*
     66       1.1  fredette  * The first block number we request.  This is related to historical
     67       1.1  fredette  * values; in 4.2BSD, the disklabel and the first stage boot program
     68       1.1  fredette  * are together called the bootblocks, and take up the first BBSIZE
     69       1.1  fredette  * bytes of the disk.  In 4.2BSD, BBSIZE was 8192 and the disk device
     70       1.1  fredette  * block size was 512 bytes (see the definition of SUNND_BSIZE below),
     71       1.1  fredette  * making the number of blocks taken up by the bootblocks (BBSIZE /
     72       1.1  fredette  * SUNND_BSIZE).  This value for FIRSTBLOCKNUM assumes that the
     73       1.1  fredette  * second-stage boot begins immediately after the bootblocks.  The
     74       1.1  fredette  * second-stage boot always runs contiguously for MAXBLOCKNUM blocks.
     75       1.1  fredette  * FIRSTBLOCKNUM can be increased, and MAXBLOCKNUM can be adjusted, as
     76       1.1  fredette  * long as they match up with however you're putting together the disk
     77       1.1  fredette  * image that is being served to this client.
     78       1.1  fredette  */
     79       1.1  fredette #define FIRSTBLOCKNUM (8192 / SUNND_BSIZE)
     80       1.1  fredette 
     81       1.1  fredette int     	block_size = SUNND_BSIZE;	/* default */
     82       1.1  fredette 
     83  1.1.32.1      kent int
     84  1.1.32.1      kent main(void)
     85       1.1  fredette {
     86       1.1  fredette 	struct open_file	f;
     87       1.1  fredette 	void	*entry;
     88       1.1  fredette 	char	*addr;
     89       1.1  fredette 	int n, error;
     90       1.1  fredette 
     91       1.1  fredette #ifdef DEBUG
     92       1.1  fredette 	printf("bootyy: open...\n");
     93       1.1  fredette #endif
     94       1.1  fredette 	f.f_flags = F_RAW;
     95       1.1  fredette 	if (devopen(&f, 0, &addr)) {
     96       1.1  fredette 		printf("bootyy: devopen failed\n");
     97       1.1  fredette 		return;
     98       1.1  fredette 	}
     99       1.1  fredette 
    100  1.1.32.1      kent 	addr = (char *)KERN_LOADADDR;
    101       1.1  fredette 	error = copyboot(&f, addr);
    102       1.1  fredette 	f.f_dev->dv_close(&f);
    103       1.1  fredette 	if (!error) {
    104       1.1  fredette #ifdef DEBUG
    105       1.1  fredette 		printf("bootyy: start 0x%x\n", (long)addr);
    106       1.1  fredette #endif
    107       1.1  fredette 		entry = addr;
    108       1.1  fredette 		chain_to(entry);
    109       1.1  fredette 	}
    110       1.1  fredette 	/* copyboot had a problem... */
    111       1.1  fredette 	return;
    112       1.1  fredette }
    113       1.1  fredette 
    114  1.1.32.1      kent int
    115  1.1.32.1      kent copyboot(struct open_file *fp, char *addr)
    116       1.1  fredette {
    117       1.1  fredette 	int	n, i, blknum;
    118       1.1  fredette 	char *buf;
    119       1.1  fredette 
    120       1.1  fredette 	/* Need to use a buffer that can be mapped into DVMA space. */
    121       1.1  fredette 	buf = alloc(block_size);
    122       1.1  fredette 	if (!buf)
    123       1.1  fredette 		panic("bootyy: alloc failed");
    124       1.1  fredette 
    125       1.1  fredette 	for (i = 0, blknum = FIRSTBLOCKNUM; i < MAXBLOCKNUM; i++, blknum++) {
    126       1.1  fredette 
    127       1.1  fredette #ifdef DEBUG
    128       1.1  fredette 		printf("bootyy: block # %d = %d\n", i, blknum);
    129       1.1  fredette #endif
    130       1.1  fredette 		if ((fp->f_dev->dv_strategy)(fp->f_devdata, F_READ,
    131       1.1  fredette 					   blknum, block_size, buf, &n))
    132       1.1  fredette 		{
    133       1.1  fredette 			printf("bootyy: read failed\n");
    134       1.1  fredette 			return -1;
    135       1.1  fredette 		}
    136       1.1  fredette 		if (n != block_size) {
    137       1.1  fredette 			printf("bootyy: short read\n");
    138       1.1  fredette 			return -1;
    139       1.1  fredette 		}
    140  1.1.32.1      kent 		memcpy(addr, buf, block_size);
    141       1.1  fredette 		addr += block_size;
    142       1.1  fredette 	}
    143       1.1  fredette 
    144       1.1  fredette 	return 0;
    145       1.1  fredette }
    146