xref: /csrg-svn/sys/pmax/stand/boot.c (revision 58835)
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*58835Sralph  *	@(#)boot.c	7.7 (Berkeley) 03/27/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
3756638Sralph 	ask = 0;
3856638Sralph #ifdef DS3100
3952132Smckusick 	for (cp = argv[0]; *cp; cp++) {
4052132Smckusick 		if (*cp == ')' && cp[1]) {
4152132Smckusick 			cp = argv[0];
4252132Smckusick 			goto fnd;
4352132Smckusick 		}
4452132Smckusick 	}
4556638Sralph #endif
4656638Sralph #ifdef DS5000
4756638Sralph 	if (argc > 1) {
4856638Sralph 		argc--;
4956638Sralph 		argv++;
5056638Sralph 		/* look for second '/' as in '5/rz0/vmunix' */
5156638Sralph 		for (cp = argv[0]; *cp; cp++) {
5256638Sralph 			if (*cp == '/') {
5356638Sralph 				while (*++cp) {
5456638Sralph 					if (*cp == '/' && cp[1]) {
5556638Sralph 						cp = argv[0];
5656638Sralph 						goto fnd;
5756638Sralph 					}
5856638Sralph 				}
5956638Sralph 			}
6056638Sralph 		}
6156638Sralph 	}
6256638Sralph #endif
6356638Sralph 	ask = 1;
6452132Smckusick fnd:
6552132Smckusick 	;
6656638Sralph #endif /* JUSTASK */
6752132Smckusick 	for (;;) {
6856638Sralph 		if (ask) {
6952132Smckusick 			printf("Boot: ");
7052132Smckusick 			gets(line);
7152132Smckusick 			if (line[0] == '\0')
7252132Smckusick 				continue;
7352132Smckusick 			cp = line;
7452765Sralph 			argv[0] = cp;
7552765Sralph 			argc = 1;
7652132Smckusick 		} else
7752132Smckusick 			printf("Boot: %s\n", cp);
7852132Smckusick 		entry = loadfile(cp);
7952132Smckusick 		if (entry != -1)
8052132Smckusick 			break;
8156638Sralph 		ask = 1;
8252132Smckusick 	}
8356638Sralph 	printf("Starting at 0x%x\n\n", entry);
8456638Sralph 	((void (*)())entry)(argc, argv);
8552132Smckusick }
8652132Smckusick 
8752132Smckusick /*
8852132Smckusick  * Open 'filename', read in program and return the entry point or -1 if error.
8952132Smckusick  */
9052132Smckusick loadfile(fname)
9152132Smckusick 	register char *fname;
9252132Smckusick {
9352132Smckusick 	register struct devices *dp;
9452132Smckusick 	register int fd, i, n;
9552132Smckusick 	struct exec aout;
9652132Smckusick 
9756638Sralph 	if ((fd = open(fname, 0)) < 0) {
9852132Smckusick 		goto err;
9956638Sralph 	}
10052132Smckusick 
10158007Sralph 	/* read the exec header */
10256638Sralph 	i = read(fd, (char *)&aout, sizeof(aout));
10352132Smckusick 	if (i != sizeof(aout)) {
10452132Smckusick 		goto cerr;
10552132Smckusick 	} else if (aout.a_magic != OMAGIC) {
10652132Smckusick 		goto cerr;
10752132Smckusick 	}
10852132Smckusick 
10952132Smckusick 	/* read the code and initialized data */
11052132Smckusick 	printf("Size: %d+%d", aout.a_text, aout.a_data);
11156638Sralph 	if (lseek(fd, (off_t)N_TXTOFF(aout), 0) < 0) {
11252132Smckusick 		goto cerr;
11352132Smckusick 	}
11452132Smckusick 	i = aout.a_text + aout.a_data;
11558007Sralph 	n = read(fd, (char *)aout.a_entry, i);
116*58835Sralph #ifndef SMALL
117*58835Sralph 	(void) close(fd);
11852132Smckusick #endif
11952132Smckusick 	if (n < 0) {
12052132Smckusick 		goto err;
12152132Smckusick 	} else if (n != i) {
12252132Smckusick 		goto err;
12352132Smckusick 	}
12452132Smckusick 
12552132Smckusick 	/* kernel will zero out its own bss */
12652132Smckusick 	n = aout.a_bss;
12752132Smckusick 	printf("+%d\n", n);
12852132Smckusick 
12952132Smckusick 	return ((int)aout.a_entry);
13052132Smckusick 
13152132Smckusick cerr:
132*58835Sralph #ifndef SMALL
13356638Sralph 	(void) close(fd);
134*58835Sralph #endif
13552132Smckusick err:
136*58835Sralph 	printf("Can't boot '%s'\n", fname);
13752132Smckusick 	return (-1);
13852132Smckusick }
139