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