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