152132Smckusick /* 252132Smckusick * Copyright (c) 1992 Regents of the University of California. 352132Smckusick * All rights reserved. 452132Smckusick * 552132Smckusick * This code is derived from software contributed to Berkeley by 652132Smckusick * Ralph Campbell. 752132Smckusick * 852132Smckusick * %sccs.include.redist.c% 952132Smckusick * 10*58981Sralph * @(#)boot.c 7.8 (Berkeley) 04/05/93 1152132Smckusick */ 1252132Smckusick 1356526Sbostic #include <sys/param.h> 1456526Sbostic #include <sys/exec.h> 1552132Smckusick 1652132Smckusick char line[1024]; 1752132Smckusick 1852132Smckusick /* 1952132Smckusick * This gets arguments from the PROM, calls other routines to open 2052132Smckusick * and load the program to boot, and then transfers execution to that 2152132Smckusick * new program. 2252765Sralph * Argv[0] should be something like "rz(0,0,0)vmunix" on a DECstation 3100. 2352765Sralph * Argv[0,1] should be something like "boot 5/rz0/vmunix" on a DECstation 5000. 2452765Sralph * The argument "-a" means vmunix should do an automatic reboot. 2552132Smckusick */ 2652132Smckusick void 2756638Sralph main(argc, argv) 2852132Smckusick int argc; 2952132Smckusick char **argv; 3052132Smckusick { 3152132Smckusick register char *cp; 3256638Sralph int ask, entry; 3352132Smckusick 3452132Smckusick #ifdef JUSTASK 3556638Sralph ask = 1; 3652132Smckusick #else 37*58981Sralph /* check for DS5000 boot */ 38*58981Sralph if (strcmp(argv[0], "boot") == 0) { 3956638Sralph argc--; 4056638Sralph argv++; 4156638Sralph } 42*58981Sralph cp = *argv; 43*58981Sralph ask = 0; 4456638Sralph #endif /* JUSTASK */ 4552132Smckusick for (;;) { 4656638Sralph if (ask) { 4752132Smckusick printf("Boot: "); 4852132Smckusick gets(line); 4952132Smckusick if (line[0] == '\0') 5052132Smckusick continue; 5152132Smckusick cp = line; 5252765Sralph argv[0] = cp; 5352765Sralph argc = 1; 5452132Smckusick } else 5552132Smckusick printf("Boot: %s\n", cp); 5652132Smckusick entry = loadfile(cp); 5752132Smckusick if (entry != -1) 5852132Smckusick break; 5956638Sralph ask = 1; 6052132Smckusick } 6156638Sralph printf("Starting at 0x%x\n\n", entry); 6256638Sralph ((void (*)())entry)(argc, argv); 6352132Smckusick } 6452132Smckusick 6552132Smckusick /* 6652132Smckusick * Open 'filename', read in program and return the entry point or -1 if error. 6752132Smckusick */ 6852132Smckusick loadfile(fname) 6952132Smckusick register char *fname; 7052132Smckusick { 7152132Smckusick register struct devices *dp; 7252132Smckusick register int fd, i, n; 7352132Smckusick struct exec aout; 7452132Smckusick 7556638Sralph if ((fd = open(fname, 0)) < 0) { 7652132Smckusick goto err; 7756638Sralph } 7852132Smckusick 7958007Sralph /* read the exec header */ 8056638Sralph i = read(fd, (char *)&aout, sizeof(aout)); 8152132Smckusick if (i != sizeof(aout)) { 8252132Smckusick goto cerr; 8352132Smckusick } else if (aout.a_magic != OMAGIC) { 8452132Smckusick goto cerr; 8552132Smckusick } 8652132Smckusick 8752132Smckusick /* read the code and initialized data */ 8852132Smckusick printf("Size: %d+%d", aout.a_text, aout.a_data); 8956638Sralph if (lseek(fd, (off_t)N_TXTOFF(aout), 0) < 0) { 9052132Smckusick goto cerr; 9152132Smckusick } 9252132Smckusick i = aout.a_text + aout.a_data; 9358007Sralph n = read(fd, (char *)aout.a_entry, i); 9458835Sralph #ifndef SMALL 9558835Sralph (void) close(fd); 9652132Smckusick #endif 9752132Smckusick if (n < 0) { 9852132Smckusick goto err; 9952132Smckusick } else if (n != i) { 10052132Smckusick goto err; 10152132Smckusick } 10252132Smckusick 10352132Smckusick /* kernel will zero out its own bss */ 10452132Smckusick n = aout.a_bss; 10552132Smckusick printf("+%d\n", n); 10652132Smckusick 10752132Smckusick return ((int)aout.a_entry); 10852132Smckusick 10952132Smckusick cerr: 11058835Sralph #ifndef SMALL 11156638Sralph (void) close(fd); 11258835Sralph #endif 11352132Smckusick err: 11458835Sralph printf("Can't boot '%s'\n", fname); 11552132Smckusick return (-1); 11652132Smckusick } 117