1 /* $NetBSD: boot.c,v 1.29 2009/03/18 17:06:47 cegger 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[], bootprog_date[]; 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 %s] <<\n", bootprog_rev, 107 bootprog_date); 108 printf(">> Press any key to abort autoboot "); 109 sluttid = getsecs() + 5; 110 senast = 0; 111 skip = 0; 112 setjmp(jbuf); 113 for (;;) { 114 nu = sluttid - getsecs(); 115 if (senast != nu) 116 printf("%c%d", 8, nu); 117 if (nu <= 0) 118 break; 119 senast = nu; 120 if ((j = (testkey() & 0177))) { 121 skip = 1; 122 if (j != 10 && j != 13) { 123 printf("\nPress '?' for help"); 124 askname = 1; 125 } 126 break; 127 } 128 } 129 skip = 1; 130 printf("\n"); 131 if (setjmp(jbuf)) 132 askname = 1; 133 134 /* First try to autoboot */ 135 if (askname == 0) { 136 int fileindex; 137 for (fileindex = 0; filelist[fileindex].name[0] != '\0'; 138 fileindex++) { 139 int err; 140 errno = 0; 141 if (!filelist[fileindex].quiet) 142 printf("> boot %s\n", filelist[fileindex].name); 143 marks[MARK_START] = 0; 144 err = loadfile(filelist[fileindex].name, marks, 145 LOAD_KERNEL|COUNT_KERNEL); 146 if (err == 0) { 147 machdep_start((char *)marks[MARK_ENTRY], 148 marks[MARK_NSYM], 149 (void *)marks[MARK_START], 150 (void *)marks[MARK_SYM], 151 (void *)marks[MARK_END]); 152 } 153 if (!filelist[fileindex].quiet) 154 printf("%s: boot failed: %s\n", 155 filelist[fileindex].name, strerror(errno)); 156 #if 0 /* Will hang VAX 4000 machines */ 157 if (testkey()) 158 break; 159 #endif 160 } 161 } 162 163 /* If any key pressed, go to conversational boot */ 164 for (;;) { 165 const struct vals *v = &val[0]; 166 char *c, *d; 167 168 printf("> "); 169 gets(line); 170 171 c = line; 172 while (*c == ' ') 173 c++; 174 175 if (c[0] == 0) 176 continue; 177 178 if ((d = strchr(c, ' '))) 179 *d++ = 0; 180 181 while (v->namn) { 182 if (strcmp(v->namn, c) == 0) 183 break; 184 v++; 185 } 186 if (v->namn) 187 (*v->func)(d); 188 else 189 printf("Unknown command: %s\n", c); 190 } 191 } 192 193 void 194 halt(char *hej) 195 { 196 __asm("halt"); 197 } 198 199 void 200 boot(char *arg) 201 { 202 char *fn = "netbsd"; 203 int howto, fl, err; 204 u_long marks[MARK_MAX]; 205 206 if (arg) { 207 while (*arg == ' ') 208 arg++; 209 210 if (*arg != '-') { 211 fn = arg; 212 if ((arg = strchr(arg, ' '))) { 213 *arg++ = 0; 214 while (*arg == ' ') 215 arg++; 216 } else 217 goto load; 218 } 219 if (*arg != '-') { 220 fail: printf("usage: boot [filename] [-asdqv]\n"); 221 return; 222 } 223 224 howto = 0; 225 while (*++arg) { 226 fl = 0; 227 BOOT_FLAG(*arg, fl); 228 if (!fl) 229 goto fail; 230 howto |= fl; 231 } 232 bootrpb.rpb_bootr5 = howto; 233 } 234 load: 235 marks[MARK_START] = 0; 236 err = loadfile(fn, marks, LOAD_KERNEL|COUNT_KERNEL); 237 if (err == 0) { 238 machdep_start((char *)marks[MARK_ENTRY], 239 marks[MARK_NSYM], 240 (void *)marks[MARK_START], 241 (void *)marks[MARK_SYM], 242 (void *)marks[MARK_END]); 243 } 244 printf("Boot failed: %s\n", strerror(errno)); 245 } 246 247 /* 750 Patchable Control Store magic */ 248 249 #include "../include/mtpr.h" 250 #include "../include/cpu.h" 251 #include "../include/sid.h" 252 #define PCS_BITCNT 0x2000 /* number of patchbits */ 253 #define PCS_MICRONUM 0x400 /* number of ucode locs */ 254 #define PCS_PATCHADDR 0xf00000 /* start addr of patchbits */ 255 #define PCS_PCSADDR (PCS_PATCHADDR+0x8000) /* start addr of pcs */ 256 #define PCS_PATCHBIT (PCS_PATCHADDR+0xc000) /* patchbits enable reg */ 257 #define PCS_ENABLE 0xfff00000 /* enable bits for pcs */ 258 259 #define extzv(one, two, three,four) \ 260 ({ \ 261 __asm volatile ("extzv %0,%3,%1,%2" \ 262 : \ 263 : "g"(one),"m"(two),"mo>"(three),"g"(four)); \ 264 }) 265 266 267 void 268 loadpcs(void) 269 { 270 static int pcsdone = 0; 271 int mid = mfpr(PR_SID); 272 int i, j, *ip, *jp; 273 char pcs[100]; 274 char *cp; 275 276 if ((mid >> 24) != VAX_750 || ((mid >> 8) & 255) < 95 || pcsdone) 277 return; 278 printf("Updating 11/750 microcode: "); 279 for (cp = line; *cp; cp++) 280 if (*cp == ')' || *cp == ':') 281 break; 282 if (*cp) { 283 memcpy( pcs, line, 99); 284 pcs[99] = 0; 285 i = cp - line + 1; 286 } else 287 i = 0; 288 strcpy(pcs + i, "pcs750.bin"); 289 i = open(pcs, 0); 290 if (i < 0) { 291 printf("bad luck - missing pcs750.bin :-(\n"); 292 return; 293 } 294 /* 295 * We ask for more than we need to be sure we get only what we expect. 296 * After read: 297 * locs 0 - 1023 packed patchbits 298 * 1024 - 11264 packed microcode 299 */ 300 if (read(i, (char *)0, 23*512) != 22*512) { 301 printf("Error reading %s\n", pcs); 302 close(i); 303 return; 304 } 305 close(i); 306 307 /* 308 * Enable patchbit loading and load the bits one at a time. 309 */ 310 *((int *)PCS_PATCHBIT) = 1; 311 ip = (int *)PCS_PATCHADDR; 312 jp = (int *)0; 313 for (i=0; i < PCS_BITCNT; i++) { 314 extzv(i,*jp,*ip++,1); 315 } 316 *((int *)PCS_PATCHBIT) = 0; 317 318 /* 319 * Load PCS microcode 20 bits at a time. 320 */ 321 ip = (int *)PCS_PCSADDR; 322 jp = (int *)1024; 323 for (i=j=0; j < PCS_MICRONUM * 4; i+=20, j++) { 324 extzv(i,*jp,*ip++,20); 325 } 326 327 /* 328 * Enable PCS. 329 */ 330 i = *jp; /* get 1st 20 bits of microcode again */ 331 i &= 0xfffff; 332 i |= PCS_ENABLE; /* reload these bits with PCS enable set */ 333 *((int *)PCS_PCSADDR) = i; 334 335 mid = mfpr(PR_SID); 336 printf("new rev level=%d\n", V750UCODE(mid)); 337 pcsdone = 1; 338 } 339 340 void 341 usage(char *hej) 342 { 343 const struct vals *v = &val[0]; 344 345 printf("Commands:\n"); 346 while (v->namn) { 347 printf("%s\t%s\n", v->namn, v->info); 348 v++; 349 } 350 } 351