1 /* 2 * Copyright (c) 1992 Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Ralph Campbell. 7 * 8 * %sccs.include.redist.c% 9 * 10 * @(#)boot.c 7.2 (Berkeley) 03/01/92 11 */ 12 13 #include "param.h" 14 #include "reboot.h" 15 #include "exec.h" 16 17 #ifndef TEST 18 #define DEF_MONFUNCS 19 #include "../include/machMon.h" 20 #endif 21 22 char line[1024]; 23 24 /* 25 * This gets arguments from the PROM, calls other routines to open 26 * and load the program to boot, and then transfers execution to that 27 * new program. 28 * Argv[0] should be something like "rz(0,0,0)vmunix" on a DECstation 3100. 29 * Argv[0,1] should be something like "boot 5/rz0/vmunix" on a DECstation 5000. 30 * The argument "-a" means vmunix should do an automatic reboot. 31 */ 32 void 33 main(argc, argv, argenv) 34 int argc; 35 char **argv; 36 char **argenv; 37 { 38 register char *cp; 39 int howto, entry; 40 char *boot = "boot"; 41 42 #ifdef JUSTASK 43 howto = RB_ASKNAME; 44 #else 45 if (argc > 0 && strcmp(argv[0], boot) == 0) { 46 argc--; 47 argv++; 48 argv[0] = getenv(boot); 49 printf("boot '%s'\n", argv[0]); /* XXX */ 50 } 51 howto = 0; 52 for (cp = argv[0]; *cp; cp++) { 53 if (*cp == ')' && cp[1]) { 54 cp = argv[0]; 55 goto fnd; 56 } 57 } 58 howto |= RB_ASKNAME; 59 fnd: 60 ; 61 #endif 62 for (;;) { 63 if (howto & RB_ASKNAME) { 64 printf("Boot: "); 65 gets(line); 66 if (line[0] == '\0') 67 continue; 68 cp = line; 69 argv[0] = cp; 70 argc = 1; 71 } else 72 printf("Boot: %s\n", cp); 73 entry = loadfile(cp); 74 if (entry != -1) 75 break; 76 howto = RB_ASKNAME; 77 } 78 #ifndef TEST 79 Boot_Transfer(argc, argv, argenv, entry); 80 #endif 81 } 82 83 /* 84 * Open 'filename', read in program and return the entry point or -1 if error. 85 */ 86 loadfile(fname) 87 register char *fname; 88 { 89 register struct devices *dp; 90 register int fd, i, n; 91 struct exec aout; 92 93 if ((fd = Open(fname, 0)) < 0) 94 goto err; 95 96 /* read the COFF header */ 97 i = Read(fd, (char *)&aout, sizeof(aout)); 98 if (i != sizeof(aout)) { 99 printf("No a.out header\n"); 100 goto cerr; 101 } else if (aout.a_magic != OMAGIC) { 102 printf("A.out? magic 0%o size %d+%d+%d\n", aout.a_magic, 103 aout.a_text, aout.a_data, aout.a_bss); 104 goto cerr; 105 } 106 107 /* read the code and initialized data */ 108 printf("Size: %d+%d", aout.a_text, aout.a_data); 109 if (Lseek(fd, N_TXTOFF(aout), 0) < 0) { 110 printf("\nSeek error\n"); 111 goto cerr; 112 } 113 i = aout.a_text + aout.a_data; 114 #ifndef TEST 115 n = Read(fd, (char *)aout.ex_aout.codeStart, i); 116 #else 117 n = i; 118 #endif 119 (void) Close(fd); 120 if (n < 0) { 121 printf("\nRead error\n"); 122 goto err; 123 } else if (n != i) { 124 printf("\nShort read (%d)\n", n); 125 goto err; 126 } 127 128 /* kernel will zero out its own bss */ 129 n = aout.a_bss; 130 printf("+%d\n", n); 131 132 return ((int)aout.a_entry); 133 134 cerr: 135 (void) Close(fd); 136 err: 137 return (-1); 138 } 139