1 /* $NetBSD: bootxx.c,v 1.11 2002/05/15 09:44:54 lukem 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 #include <sys/param.h> 40 #include <sys/time.h> 41 #include <sys/exec.h> 42 #include <sys/bootblock.h> 43 44 #include <lib/libsa/stand.h> 45 46 #include <machine/promlib.h> 47 #include <sparc/stand/common/promdev.h> 48 49 int debug; 50 int netif_debug; 51 52 /* 53 * Boot device is derived from ROM provided information. 54 */ 55 const char progname[] = "bootxx"; 56 struct open_file io; 57 58 /* 59 * The contents of the bbinfo below are set by installboot(8) 60 * to hold the filesystem data of the second-stage boot program 61 * (typically `/boot'): filesystem block size, # of filesystem 62 * blocks and the block numbers themselves. 63 */ 64 struct shared_bbinfo bbinfo = { 65 { SPARC_BBINFO_MAGIC }, 66 0, 67 SHARED_BBINFO_MAXBLOCKS, 68 { 0 } 69 }; 70 71 int main __P((void)); 72 void loadboot __P((struct open_file *, caddr_t)); 73 74 int 75 main() 76 { 77 char *dummy; 78 void (*entry)__P((void *)) = (void (*)__P((void *)))PROM_LOADADDR; 79 void *arg; 80 81 #ifdef HEAP_VARIABLE 82 { 83 extern char end[]; 84 setheap((void *)ALIGN(end), (void *)0xffffffff); 85 } 86 #endif 87 prom_init(); 88 prom_bootdevice = prom_getbootpath(); 89 io.f_flags = F_RAW; 90 if (devopen(&io, 0, &dummy)) { 91 panic("%s: can't open device `%s'", progname, 92 prom_bootdevice != NULL ? prom_bootdevice : "unknown"); 93 } 94 95 (void)loadboot(&io, (caddr_t)PROM_LOADADDR); 96 (io.f_dev->dv_close)(&io); 97 98 arg = (prom_version() == PROM_OLDMON) ? (caddr_t)PROM_LOADADDR : romp; 99 (*entry)(arg); 100 _rtt(); 101 } 102 103 void 104 loadboot(f, addr) 105 struct open_file *f; 106 char *addr; 107 { 108 int i; 109 char *buf; 110 size_t n; 111 daddr_t blk; 112 113 /* 114 * Allocate a buffer that we can map into DVMA space; only 115 * needed for sun4 architecture, but use it for all machines 116 * to keep code size down as much as possible. 117 */ 118 buf = alloc(bbinfo.bbi_block_size); 119 if (buf == NULL) 120 panic("%s: alloc failed", progname); 121 122 for (i = 0; i < bbinfo.bbi_block_count; i++) { 123 if ((blk = bbinfo.bbi_block_table[i]) == 0) 124 panic("%s: block table corrupt", progname); 125 126 #ifdef DEBUG 127 printf("%s: block # %d = %d\n", progname, i, blk); 128 #endif 129 if ((f->f_dev->dv_strategy)(f->f_devdata, F_READ, blk, 130 bbinfo.bbi_block_size, buf, &n)) { 131 printf("%s: read failure", progname); 132 _rtt(); 133 } 134 bcopy(buf, addr, bbinfo.bbi_block_size); 135 if (n != bbinfo.bbi_block_size) 136 panic("%s: short read", progname); 137 if (i == 0) { 138 int m = N_GETMAGIC(*(struct exec *)addr); 139 if (m == ZMAGIC || m == NMAGIC || m == OMAGIC) { 140 /* Move exec header out of the way */ 141 bcopy(addr, addr - sizeof(struct exec), n); 142 addr -= sizeof(struct exec); 143 } 144 } 145 addr += n; 146 } 147 148 } 149 150 /* 151 * We don't need the overlap handling feature that the libkern version 152 * of bcopy() provides. We DO need code compactness.. 153 */ 154 void 155 bcopy(src, dst, n) 156 const void *src; 157 void *dst; 158 size_t n; 159 { 160 const char *p = src; 161 char *q = dst; 162 163 while (n-- > 0) 164 *q++ = *p++; 165 } 166