1 /* $NetBSD: boot.c,v 1.31 2011/01/22 19:19:24 joerg Exp $ */ 2 /*- 3 * Copyright (c) 1982, 1986 The Regents of the University of California. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Neither the name of the University nor the names of its contributors 15 * may be used to endorse or promote products derived from this software 16 * without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * @(#)boot.c 7.15 (Berkeley) 5/4/91 31 */ 32 33 #include <sys/param.h> 34 #include <sys/reboot.h> 35 #include <sys/boot_flag.h> 36 37 #include <lib/libsa/stand.h> 38 #include <lib/libsa/net.h> 39 #include <lib/libsa/loadfile.h> 40 #include <lib/libkern/libkern.h> 41 42 #define V750UCODE(x) ((x>>8)&255) 43 44 #include "machine/rpb.h" 45 46 #include "vaxstand.h" 47 48 /* 49 * Boot program... arguments passed in r10 and r11 determine 50 * whether boot stops to ask for system name and which device 51 * boot comes from. 52 */ 53 54 char line[100]; 55 int bootdev, debug; 56 extern unsigned opendev; 57 58 void usage(char *), boot(char *), halt(char *); 59 void Xmain(void); 60 void autoconf(void); 61 int setjmp(int *); 62 int testkey(void); 63 void loadpcs(void); 64 65 const struct vals { 66 char *namn; 67 void (*func)(char *); 68 char *info; 69 } val[] = { 70 {"?", usage, "Show this help menu"}, 71 {"help", usage, "Same as '?'"}, 72 {"boot", boot, "Load and execute file"}, 73 {"halt", halt, "Halts the system"}, 74 {0, 0}, 75 }; 76 77 static struct { 78 char name[12]; 79 int quiet; 80 } filelist[] = { 81 { "netbsd.vax", 1 }, 82 { "netbsd", 0 }, 83 { "netbsd.gz", 0 }, 84 { "netbsd.old", 0 }, 85 { "gennetbsd", 0 }, 86 { "", 0 }, 87 }; 88 89 int jbuf[10]; 90 int sluttid, senast, skip, askname; 91 struct rpb bootrpb; 92 93 void 94 Xmain(void) 95 { 96 int io; 97 int j, nu; 98 u_long marks[MARK_MAX]; 99 extern const char bootprog_rev[]; 100 101 io = 0; 102 skip = 1; 103 autoconf(); 104 105 askname = bootrpb.rpb_bootr5 & RB_ASKNAME; 106 printf("\n\r>> NetBSD/vax boot [%s] <<\n", bootprog_rev); 107 printf(">> Press any key to abort autoboot "); 108 sluttid = getsecs() + 5; 109 senast = 0; 110 skip = 0; 111 setjmp(jbuf); 112 for (;;) { 113 nu = sluttid - getsecs(); 114 if (senast != nu) 115 printf("%c%d", 8, nu); 116 if (nu <= 0) 117 break; 118 senast = nu; 119 if ((j = (testkey() & 0177))) { 120 skip = 1; 121 if (j != 10 && j != 13) { 122 printf("\nPress '?' for help"); 123 askname = 1; 124 } 125 break; 126 } 127 } 128 skip = 1; 129 printf("\n"); 130 if (setjmp(jbuf)) 131 askname = 1; 132 133 /* First try to autoboot */ 134 if (askname == 0) { 135 int fileindex; 136 for (fileindex = 0; filelist[fileindex].name[0] != '\0'; 137 fileindex++) { 138 int err; 139 errno = 0; 140 if (!filelist[fileindex].quiet) 141 printf("> boot %s\n", filelist[fileindex].name); 142 marks[MARK_START] = 0; 143 err = loadfile(filelist[fileindex].name, marks, 144 LOAD_KERNEL|COUNT_KERNEL); 145 if (err == 0) { 146 machdep_start((char *)marks[MARK_ENTRY], 147 marks[MARK_NSYM], 148 (void *)marks[MARK_START], 149 (void *)marks[MARK_SYM], 150 (void *)marks[MARK_END]); 151 } 152 if (!filelist[fileindex].quiet) 153 printf("%s: boot failed: %s\n", 154 filelist[fileindex].name, strerror(errno)); 155 #if 0 /* Will hang VAX 4000 machines */ 156 if (testkey()) 157 break; 158 #endif 159 } 160 } 161 162 /* If any key pressed, go to conversational boot */ 163 for (;;) { 164 const struct vals *v = &val[0]; 165 char *c, *d; 166 167 printf("> "); 168 gets(line); 169 170 c = line; 171 while (*c == ' ') 172 c++; 173 174 if (c[0] == 0) 175 continue; 176 177 if ((d = strchr(c, ' '))) 178 *d++ = 0; 179 180 while (v->namn) { 181 if (strcmp(v->namn, c) == 0) 182 break; 183 v++; 184 } 185 if (v->namn) 186 (*v->func)(d); 187 else 188 printf("Unknown command: %s\n", c); 189 } 190 } 191 192 void 193 halt(char *hej) 194 { 195 __asm("halt"); 196 } 197 198 void 199 boot(char *arg) 200 { 201 char *fn = "netbsd"; 202 int howto, fl, err; 203 u_long marks[MARK_MAX]; 204 205 if (arg) { 206 while (*arg == ' ') 207 arg++; 208 209 if (*arg != '-') { 210 fn = arg; 211 if ((arg = strchr(arg, ' '))) { 212 *arg++ = 0; 213 while (*arg == ' ') 214 arg++; 215 } else 216 goto load; 217 } 218 if (*arg != '-') { 219 fail: printf("usage: boot [filename] [-asdqv]\n"); 220 return; 221 } 222 223 howto = 0; 224 while (*++arg) { 225 fl = 0; 226 BOOT_FLAG(*arg, fl); 227 if (!fl) 228 goto fail; 229 howto |= fl; 230 } 231 bootrpb.rpb_bootr5 = howto; 232 } 233 load: 234 marks[MARK_START] = 0; 235 err = loadfile(fn, marks, LOAD_KERNEL|COUNT_KERNEL); 236 if (err == 0) { 237 machdep_start((char *)marks[MARK_ENTRY], 238 marks[MARK_NSYM], 239 (void *)marks[MARK_START], 240 (void *)marks[MARK_SYM], 241 (void *)marks[MARK_END]); 242 } 243 printf("Boot failed: %s\n", strerror(errno)); 244 } 245 246 /* 750 Patchable Control Store magic */ 247 248 #include "../include/mtpr.h" 249 #include "../include/cpu.h" 250 #include "../include/sid.h" 251 #define PCS_BITCNT 0x2000 /* number of patchbits */ 252 #define PCS_MICRONUM 0x400 /* number of ucode locs */ 253 #define PCS_PATCHADDR 0xf00000 /* start addr of patchbits */ 254 #define PCS_PCSADDR (PCS_PATCHADDR+0x8000) /* start addr of pcs */ 255 #define PCS_PATCHBIT (PCS_PATCHADDR+0xc000) /* patchbits enable reg */ 256 #define PCS_ENABLE 0xfff00000 /* enable bits for pcs */ 257 258 #define extzv(one, two, three,four) \ 259 ({ \ 260 __asm volatile ("extzv %0,%3,%1,%2" \ 261 : \ 262 : "g"(one),"m"(two),"mo>"(three),"g"(four)); \ 263 }) 264 265 266 void 267 loadpcs(void) 268 { 269 static int pcsdone = 0; 270 int mid = mfpr(PR_SID); 271 int i, j, *ip, *jp; 272 char pcs[100]; 273 char *cp; 274 275 if ((mid >> 24) != VAX_750 || ((mid >> 8) & 255) < 95 || pcsdone) 276 return; 277 printf("Updating 11/750 microcode: "); 278 for (cp = line; *cp; cp++) 279 if (*cp == ')' || *cp == ':') 280 break; 281 if (*cp) { 282 memcpy(pcs, line, 99); 283 pcs[99] = 0; 284 i = cp - line + 1; 285 } else 286 i = 0; 287 strcpy(pcs + i, "pcs750.bin"); 288 i = open(pcs, 0); 289 if (i < 0) { 290 printf("bad luck - missing pcs750.bin :-(\n"); 291 return; 292 } 293 /* 294 * We ask for more than we need to be sure we get only what we expect. 295 * After read: 296 * locs 0 - 1023 packed patchbits 297 * 1024 - 11264 packed microcode 298 */ 299 if (read(i, (char *)0, 23*512) != 22*512) { 300 printf("Error reading %s\n", pcs); 301 close(i); 302 return; 303 } 304 close(i); 305 306 /* 307 * Enable patchbit loading and load the bits one at a time. 308 */ 309 *((int *)PCS_PATCHBIT) = 1; 310 ip = (int *)PCS_PATCHADDR; 311 jp = (int *)0; 312 for (i=0; i < PCS_BITCNT; i++) { 313 extzv(i,*jp,*ip++,1); 314 } 315 *((int *)PCS_PATCHBIT) = 0; 316 317 /* 318 * Load PCS microcode 20 bits at a time. 319 */ 320 ip = (int *)PCS_PCSADDR; 321 jp = (int *)1024; 322 for (i=j=0; j < PCS_MICRONUM * 4; i+=20, j++) { 323 extzv(i,*jp,*ip++,20); 324 } 325 326 /* 327 * Enable PCS. 328 */ 329 i = *jp; /* get 1st 20 bits of microcode again */ 330 i &= 0xfffff; 331 i |= PCS_ENABLE; /* reload these bits with PCS enable set */ 332 *((int *)PCS_PCSADDR) = i; 333 334 mid = mfpr(PR_SID); 335 printf("new rev level=%d\n", V750UCODE(mid)); 336 pcsdone = 1; 337 } 338 339 void 340 usage(char *hej) 341 { 342 const struct vals *v = &val[0]; 343 344 printf("Commands:\n"); 345 while (v->namn) { 346 printf("%s\t%s\n", v->namn, v->info); 347 v++; 348 } 349 } 350