xref: /original-bsd/sys/pmax/stand/boot.c (revision 94e7bb75)
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.3 (Berkeley) 03/15/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 	}
50 	howto = 0;
51 	for (cp = argv[0]; *cp; cp++) {
52 		if (*cp == ')' && cp[1]) {
53 			cp = argv[0];
54 			goto fnd;
55 		}
56 	}
57 	howto |= RB_ASKNAME;
58 fnd:
59 	;
60 #endif
61 	for (;;) {
62 		if (howto & RB_ASKNAME) {
63 			printf("Boot: ");
64 			gets(line);
65 			if (line[0] == '\0')
66 				continue;
67 			cp = line;
68 			argv[0] = cp;
69 			argc = 1;
70 		} else
71 			printf("Boot: %s\n", cp);
72 		entry = loadfile(cp);
73 		if (entry != -1)
74 			break;
75 		howto = RB_ASKNAME;
76 	}
77 #ifndef TEST
78 	Boot_Transfer(argc, argv, argenv, entry);
79 #endif
80 }
81 
82 /*
83  * Open 'filename', read in program and return the entry point or -1 if error.
84  */
85 loadfile(fname)
86 	register char *fname;
87 {
88 	register struct devices *dp;
89 	register int fd, i, n;
90 	struct exec aout;
91 
92 	if ((fd = Open(fname, 0)) < 0)
93 		goto err;
94 
95 	/* read the COFF header */
96 	i = Read(fd, (char *)&aout, sizeof(aout));
97 	if (i != sizeof(aout)) {
98 		printf("No a.out header\n");
99 		goto cerr;
100 	} else if (aout.a_magic != OMAGIC) {
101 		printf("A.out? magic 0%o size %d+%d+%d\n", aout.a_magic,
102 			aout.a_text, aout.a_data, aout.a_bss);
103 		goto cerr;
104 	}
105 
106 	/* read the code and initialized data */
107 	printf("Size: %d+%d", aout.a_text, aout.a_data);
108 	if (Lseek(fd, N_TXTOFF(aout), 0) < 0) {
109 		printf("\nSeek error\n");
110 		goto cerr;
111 	}
112 	i = aout.a_text + aout.a_data;
113 #ifndef TEST
114 	n = Read(fd, (char *)aout.ex_aout.codeStart, i);
115 #else
116 	n = i;
117 #endif
118 	(void) Close(fd);
119 	if (n < 0) {
120 		printf("\nRead error\n");
121 		goto err;
122 	} else if (n != i) {
123 		printf("\nShort read (%d)\n", n);
124 		goto err;
125 	}
126 
127 	/* kernel will zero out its own bss */
128 	n = aout.a_bss;
129 	printf("+%d\n", n);
130 
131 	return ((int)aout.a_entry);
132 
133 cerr:
134 	(void) Close(fd);
135 err:
136 	return (-1);
137 }
138