1 /* $NetBSD: boot.c,v 1.7 2011/01/22 19:19:16 joerg Exp $ */ 2 3 /*- 4 * Copyright (c) 1999 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jonathan Stone, Michael Hitch and Simon Burge. 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 * Copyright (c) 1992, 1993 34 * The Regents of the University of California. All rights reserved. 35 * 36 * This code is derived from software contributed to Berkeley by 37 * Ralph Campbell. 38 * 39 * Redistribution and use in source and binary forms, with or without 40 * modification, are permitted provided that the following conditions 41 * are met: 42 * 1. Redistributions of source code must retain the above copyright 43 * notice, this list of conditions and the following disclaimer. 44 * 2. Redistributions in binary form must reproduce the above copyright 45 * notice, this list of conditions and the following disclaimer in the 46 * documentation and/or other materials provided with the distribution. 47 * 3. Neither the name of the University nor the names of its contributors 48 * may be used to endorse or promote products derived from this software 49 * without specific prior written permission. 50 * 51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 61 * SUCH DAMAGE. 62 * 63 * @(#)boot.c 8.1 (Berkeley) 6/10/93 64 */ 65 66 #include <lib/libsa/stand.h> 67 #include <lib/libsa/loadfile.h> 68 #include <lib/libkern/libkern.h> 69 70 #include <sys/param.h> 71 #include <sys/exec.h> 72 #include <sys/exec_elf.h> 73 #include <sys/boot_flag.h> 74 75 #include <dev/arcbios/arcbios.h> 76 77 #include "common.h" 78 #include "bootinfo.h" 79 80 #ifdef BOOT_DEBUG 81 #define DPRINTF if (debug) printf 82 #else 83 #define DPRINTF while (/*CONSTCOND*/0) printf 84 #endif 85 86 /* 87 * We won't go overboard with gzip'd kernel names. After all we can 88 * still boot a gzip'd kernel called "netbsd.arc" - it doesn't need 89 * the .gz suffix. 90 * 91 * For arcane reasons, the first byte of the first element of this struct will 92 * contain a zero. We therefore start from one. 93 */ 94 95 char *kernelnames[] = { 96 "placekeeper", 97 "netbsd.arc", 98 "netbsd", 99 "netbsd.gz", 100 "netbsd.bak", 101 "netbsd.old", 102 "onetbsd", 103 "gennetbsd", 104 NULL 105 }; 106 107 static int debug = 0; 108 static char **environment; 109 110 /* Storage must be static. */ 111 struct btinfo_symtab bi_syms; 112 struct btinfo_bootpath bi_bpath; 113 114 static char bootinfo[BOOTINFO_SIZE]; 115 116 extern const struct arcbios_fv *ARCBIOS; 117 118 int main(int, char **); 119 static char *firmware_getenv(char *); 120 121 /* 122 * This gets arguments from the ARCS monitor, calls ARCS routines to open 123 * and load the program to boot, then transfers execution to the new program. 124 * 125 * argv[0] will be the ARCS path to the bootloader (i.e., 126 * "scsi(0)disk(2)rdisk(0)partition(1)\boot"). 127 * 128 * argv[1] through argv[n] will contain arguments passed from the PROM, if any. 129 */ 130 131 int 132 main(int argc, char **argv) 133 { 134 const char *kernel = NULL; 135 const char *bootpath = NULL; 136 char bootfile[PATH_MAX]; 137 void (*entry)(int, char *[], u_int, void *); 138 u_long marks[MARK_MAX]; 139 int win = 0; 140 int i; 141 int ch; 142 143 /* print a banner */ 144 printf("\n"); 145 printf("%s Bootstrap, Revision %s\n", bootprog_name, bootprog_rev); 146 147 memset(marks, 0, sizeof marks); 148 149 /* initialise bootinfo structure early */ 150 bi_init(bootinfo); 151 152 #ifdef BOOT_DEBUG 153 for (i = 0; i < argc; i++) 154 printf("argv[%d] = %s\n", i, argv[i]); 155 #endif 156 157 /* Parse arguments, if present. */ 158 while ((ch = getopt(argc, argv, "v")) != -1) { 159 switch (ch) { 160 case 'v': 161 debug = 1; 162 break; 163 } 164 } 165 166 environment = &argv[1]; 167 168 bootpath = firmware_getenv("OSLoadPartition"); 169 if (bootpath == NULL) 170 bootpath = 171 (*ARCBIOS->GetEnvironmentVariable)("OSLoadPartition"); 172 173 if (bootpath == NULL) { 174 /* XXX need to actually do the fixup */ 175 printf("OSLoadPartition is not specified.\n"); 176 return 0; 177 } 178 DPRINTF("bootpath = %s\n", bootpath); 179 180 /* 181 * Grab OSLoadFilename from ARCS. 182 */ 183 184 kernel = firmware_getenv("OSLoadFilename"); 185 if (kernel == NULL) 186 kernel = (*ARCBIOS->GetEnvironmentVariable)("OSLoadFilename"); 187 188 DPRINTF("kernel = %s\n", kernel ? kernel : "<null>"); 189 190 /* 191 * The first arg is assumed to contain the name of the kernel to boot, 192 * if it a) does not start with a hyphen and b) does not contain 193 * an equals sign. 194 */ 195 196 for (i = 1; i < argc; i++) { 197 if (((strchr(argv[i], '=')) == NULL) && (argv[i][0] != '-')) { 198 kernel = argv[i]; 199 break; 200 } 201 } 202 203 if (kernel != NULL) { 204 /* 205 * if the name contains parenthesis, we assume that it 206 * contains the bootpath and ignore anything passed through 207 * the environment 208 */ 209 if (strchr(kernel, '(')) 210 win = loadfile(kernel, marks, LOAD_KERNEL); 211 else { 212 strcpy(bootfile, bootpath); 213 strcat(bootfile, kernel); 214 win = loadfile(bootfile, marks, LOAD_KERNEL); 215 } 216 217 } else { 218 i = 1; 219 while (kernelnames[i] != NULL) { 220 strcpy(bootfile, bootpath); 221 strcat(bootfile, kernelnames[i]); 222 kernel = kernelnames[i]; 223 win = loadfile(bootfile, marks, LOAD_KERNEL); 224 if (win != -1) 225 break; 226 i++; 227 } 228 229 } 230 231 if (win < 0) { 232 printf("Boot failed! Halting...\n"); 233 (void)getchar(); 234 return 0; 235 } 236 237 strlcpy(bi_bpath.bootpath, kernel, BTINFO_BOOTPATH_LEN); 238 bi_add(&bi_bpath, BTINFO_BOOTPATH, sizeof(bi_bpath)); 239 240 bi_syms.nsym = marks[MARK_NSYM]; 241 bi_syms.ssym = marks[MARK_SYM]; 242 bi_syms.esym = marks[MARK_END]; 243 bi_add(&bi_syms, BTINFO_SYMTAB, sizeof(bi_syms)); 244 245 entry = (void *)marks[MARK_ENTRY]; 246 247 if (debug) { 248 printf("Starting at %p\n\n", entry); 249 printf("nsym 0x%lx ssym 0x%lx esym 0x%lx\n", marks[MARK_NSYM], 250 marks[MARK_SYM], marks[MARK_END]); 251 } 252 (*entry)(argc, argv, BOOTINFO_MAGIC, bootinfo); 253 254 printf("Kernel returned! Halting...\n"); 255 return 0; 256 } 257 258 char * 259 firmware_getenv(char *envname) 260 { 261 char **env; 262 int len; 263 264 len = strlen(envname); 265 266 for (env = environment; env[0]; env++) { 267 if (strncasecmp(envname, env[0], len) == 0 && 268 env[0][len] == '=') { 269 return &env[0][len + 1]; 270 } 271 } 272 return NULL; 273 } 274 275 void 276 _rtt(void) 277 { 278 279 (*ARCBIOS->Halt)(); 280 } 281