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