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