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.8 (Berkeley) 04/05/93 11 */ 12 13 #include <sys/param.h> 14 #include <sys/exec.h> 15 16 char line[1024]; 17 18 /* 19 * This gets arguments from the PROM, calls other routines to open 20 * and load the program to boot, and then transfers execution to that 21 * new program. 22 * Argv[0] should be something like "rz(0,0,0)vmunix" on a DECstation 3100. 23 * Argv[0,1] should be something like "boot 5/rz0/vmunix" on a DECstation 5000. 24 * The argument "-a" means vmunix should do an automatic reboot. 25 */ 26 void 27 main(argc, argv) 28 int argc; 29 char **argv; 30 { 31 register char *cp; 32 int ask, entry; 33 34 #ifdef JUSTASK 35 ask = 1; 36 #else 37 /* check for DS5000 boot */ 38 if (strcmp(argv[0], "boot") == 0) { 39 argc--; 40 argv++; 41 } 42 cp = *argv; 43 ask = 0; 44 #endif /* JUSTASK */ 45 for (;;) { 46 if (ask) { 47 printf("Boot: "); 48 gets(line); 49 if (line[0] == '\0') 50 continue; 51 cp = line; 52 argv[0] = cp; 53 argc = 1; 54 } else 55 printf("Boot: %s\n", cp); 56 entry = loadfile(cp); 57 if (entry != -1) 58 break; 59 ask = 1; 60 } 61 printf("Starting at 0x%x\n\n", entry); 62 ((void (*)())entry)(argc, argv); 63 } 64 65 /* 66 * Open 'filename', read in program and return the entry point or -1 if error. 67 */ 68 loadfile(fname) 69 register char *fname; 70 { 71 register struct devices *dp; 72 register int fd, i, n; 73 struct exec aout; 74 75 if ((fd = open(fname, 0)) < 0) { 76 goto err; 77 } 78 79 /* read the exec header */ 80 i = read(fd, (char *)&aout, sizeof(aout)); 81 if (i != sizeof(aout)) { 82 goto cerr; 83 } else if (aout.a_magic != OMAGIC) { 84 goto cerr; 85 } 86 87 /* read the code and initialized data */ 88 printf("Size: %d+%d", aout.a_text, aout.a_data); 89 if (lseek(fd, (off_t)N_TXTOFF(aout), 0) < 0) { 90 goto cerr; 91 } 92 i = aout.a_text + aout.a_data; 93 n = read(fd, (char *)aout.a_entry, i); 94 #ifndef SMALL 95 (void) close(fd); 96 #endif 97 if (n < 0) { 98 goto err; 99 } else if (n != i) { 100 goto err; 101 } 102 103 /* kernel will zero out its own bss */ 104 n = aout.a_bss; 105 printf("+%d\n", n); 106 107 return ((int)aout.a_entry); 108 109 cerr: 110 #ifndef SMALL 111 (void) close(fd); 112 #endif 113 err: 114 printf("Can't boot '%s'\n", fname); 115 return (-1); 116 } 117