xref: /original-bsd/sys/tahoe/stand/boot.c (revision c7ce21e7)
1 /*	boot.c	1.7	88/03/04	*/
2 
3 #include "../machine/mtpr.h"
4 
5 #include "param.h"
6 #include "inode.h"
7 #include "fs.h"
8 #include "vm.h"
9 #include "saio.h"
10 #include "reboot.h"
11 
12 #include <a.out.h>
13 
14 /*
15  * Boot program... arguments passed in r10 and r11 determine
16  * whether boot stops to ask for system name and which device
17  * boot comes from.
18  */
19 
20 #define	DEV_DFLT	1		/* vd/dk */
21 
22 char line[100];
23 
24 extern	unsigned opendev;
25 extern	unsigned bootdev;
26 
27 main()
28 {
29 	register char *cp;		/* skip r12 */
30 	register u_int howto, devtype;	/* howto=r11, devtype=r10 */
31 	int io, retry, type;
32 
33 #ifdef lint
34 	howto = 0; devtype = 0;
35 #endif
36 	if ((devtype & B_MAGICMASK) != B_DEVMAGIC)
37 		devtype = DEV_DFLT << B_TYPESHIFT;	/* unit, partition 0 */
38 	bootdev = devtype;
39 	printf("\nBoot\n");
40 #ifdef JUSTASK
41 	howto = RB_ASKNAME|RB_SINGLE;
42 #else
43 	if ((howto & RB_ASKNAME) == 0) {
44 		type = (devtype >> B_TYPESHIFT) & B_TYPEMASK;
45 		if ((unsigned)type < ndevs && devsw[type].dv_name)
46 			strcpy(line, UNIX);
47 		else
48 			howto |= RB_SINGLE|RB_ASKNAME;
49 	}
50 #endif
51 	for (retry = 0;;) {
52 		if (howto & RB_ASKNAME) {
53 			printf(": ");
54 			gets(line);
55 			if (line[0] == 0) {
56 				strcpy(line, UNIX);
57 				printf(": %s\n", line);
58 			}
59 		} else
60 			printf(": %s\n", line);
61 		io = open(line, 0);
62 		if (io >= 0) {
63 			copyunix(howto, opendev, io);
64 			close(io);
65 			howto |= RB_SINGLE|RB_ASKNAME;
66 		}
67 		if (++retry > 2)
68 			howto |= RB_SINGLE|RB_ASKNAME;
69 	}
70 }
71 
72 /*ARGSUSED*/
73 copyunix(howto, devtype, io)
74 	register io, howto, devtype;	/* NOTE ORDER */
75 {
76 	register int esym;		/* must be r9 */
77 	register int i;
78 	register char *addr;
79 	struct exec x;
80 
81 	i = read(io, (char *)&x, sizeof x);
82 	if (i != sizeof x ||
83 	    (x.a_magic != 0407 && x.a_magic != 0413 && x.a_magic != 0410)) {
84 		printf("Bad format\n");
85 		return;
86 	}
87 	printf("%d", x.a_text);
88 	if (x.a_magic == 0413 && lseek(io, 0x400, 0) == -1)
89 		goto shread;
90 	if (read(io, (char *)RELOC, x.a_text) != x.a_text)
91 		goto shread;
92 	addr = (char *)(x.a_text + RELOC);
93 	if (x.a_magic == 0413 || x.a_magic == 0410)
94 		while ((int)addr & CLOFSET)
95 			*addr++ = 0;
96 	printf("+%d", x.a_data);
97 	if (read(io, addr, x.a_data) != x.a_data)
98 		goto shread;
99 	addr += x.a_data;
100 	printf("+%d", x.a_bss);
101 	if (howto & RB_KDB && x.a_syms) {
102 		for (i = 0; i < x.a_bss; i++)
103 			*addr++ = 0;
104 		*(int *)addr = x.a_syms;		/* symbol table size */
105 		addr += sizeof (int);
106 		printf("[+%d", x.a_syms);
107 		if (read(io, addr, x.a_syms) != x.a_syms)
108 			goto shread;
109 		addr += x.a_syms;
110 		if (read(io, addr, sizeof (int)) != sizeof (int))
111 			goto shread;
112 		i = *(int *)addr - sizeof (int);	/* string table size */
113 		addr += sizeof (int);
114 		printf("+%d]", i);
115 		if (read(io, addr, i) != i)
116 			goto shread;
117 		addr += i;
118 		esym = roundup((int)addr, sizeof (int));
119 		x.a_bss = 0;
120 	} else
121 		howto &= ~RB_KDB;
122 	x.a_bss += 32*1024;	/* slop */
123 	for (i = 0; i < x.a_bss; i++)
124 		*addr++ = 0;
125 	x.a_entry &= 0x1fffffff;
126 	printf(" start 0x%x\n", x.a_entry);
127 	mtpr(PADC, 0);		/* Purge data cache */
128 	mtpr(PACC, 0);		/* Purge code cache */
129 	mtpr(DCR, 1);		/* Enable data cache */
130 	(*((int (*)()) x.a_entry))();
131 	return;
132 shread:
133 	printf("Short read\n");
134 	return;
135 }
136