xref: /csrg-svn/sys/tahoe/stand/boot.c (revision 30177)
1 /*	boot.c	1.3	86/11/25	*/
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 /* Types in r10 specifying major device */
21 char	devname[][2] = {
22 	0, 0,		/* 0 = ud */
23 	'd','k',	/* 1 = vd/dk */
24 	0, 0,		/* 2 = xp */
25 	'c','y',	/* 3 = cy */
26 };
27 #define	MAXTYPE	(sizeof(devname) / sizeof(devname[0]))
28 #define	DEV_DFLT	1		/* vd/dk */
29 
30 #define	UNIX	"vmunix"
31 char line[100];
32 
33 int	retry = 0;
34 
35 main()
36 {
37 	register dummy;		/* skip r12 */
38 	register howto, devtype;	/* howto=r11, devtype=r10 */
39 	int io, i;
40 	register type, part, unit;
41 	register char *cp;
42 	long atol();
43 
44 
45 #ifdef lint
46 	howto = 0; devtype = 0;
47 #endif
48 #ifdef JUSTASK
49 	howto = RB_ASKNAME|RB_SINGLE;
50 #else
51 	if ((devtype & B_MAGICMASK) != B_DEVMAGIC)
52 		devtype = DEV_DFLT << B_TYPESHIFT;	/* unit, partition 0 */
53 	type = (devtype >> B_TYPESHIFT) & B_TYPEMASK;
54 	unit = (devtype >> B_UNITSHIFT) & B_UNITMASK;
55 	unit += 8 * ((devtype >> B_ADAPTORSHIFT) & B_ADAPTORMASK);
56 	part = (devtype >> B_PARTITIONSHIFT) & B_PARTITIONMASK;
57 	if ((howto & RB_ASKNAME) == 0) {
58 		if (type >= 0 && type <= MAXTYPE && devname[type][0]) {
59 			cp = line;
60 			*cp++ = devname[type][0];
61 			*cp++ = devname[type][1];
62 			*cp++ = '(';
63 			if (unit >= 10)
64 				*cp++ = unit / 10 + '0';
65 			*cp++ = unit % 10 + '0';
66 			*cp++ = ',';
67 			if (part >= 10)
68 				*cp++ = part / 10 + '0';
69 			*cp++ = part % 10 + '0';
70 			*cp++ = ')';
71 			strcpy(cp, UNIX);
72 		} else
73 			howto = RB_SINGLE|RB_ASKNAME;
74 	}
75 #endif
76 	for (;;) {
77 		printf("\nBoot\n");
78 		if (howto & RB_ASKNAME) {
79 			printf(": ");
80 			gets(line);
81 		} else
82 			printf(": %s\n", line);
83 		io = open(line, 0);
84 		if (io >= 0) {
85 			if (howto & RB_ASKNAME) {
86 				/*
87 				 * Build up devtype register to pass on to
88 				 * booted program.
89 				 */
90 				cp = line;
91 				for (i = 0; i <= MAXTYPE; i++)
92 					if ((devname[i][0] == cp[0]) &&
93 					    (devname[i][1] == cp[1]))
94 					    	break;
95 				if (i <= MAXTYPE) {
96 					devtype = i << B_TYPESHIFT;
97 					cp += 3;
98 					i = *cp++ - '0';
99 					if (*cp >= '0' && *cp <= '9')
100 						i = i * 10 + *cp++ - '0';
101 					cp++;
102 					devtype |= ((i % 8) << B_UNITSHIFT);
103 					devtype |= ((i / 8) << B_ADAPTORSHIFT);
104 					devtype |= atol(cp) << B_PARTITIONSHIFT;
105 				}
106 			}
107 			devtype |= B_DEVMAGIC;
108 			copyunix(howto, devtype, io);
109 			close(io);
110 			howto = RB_SINGLE|RB_ASKNAME;
111 		}
112 		if (++retry > 2)
113 			howto |= RB_SINGLE|RB_ASKNAME;
114 	}
115 }
116 
117 /*ARGSUSED*/
118 copyunix(howto, devtype, io)
119 	register io, howto, devtype;	/* NOTE ORDER */
120 {
121 	struct exec x;
122 	register int i;
123 	register char *addr;
124 
125 	i = read(io, (char *)&x, sizeof x);
126 	if (i != sizeof x ||
127 	    (x.a_magic != 0407 && x.a_magic != 0413 && x.a_magic != 0410))
128 		_stop("Bad format\n");
129 	printf("%d", x.a_text);
130 	if (x.a_magic == 0413 && lseek(io, 0x400, 0) == -1)
131 		goto shread;
132 	if (read(io, (char *)0x800, x.a_text) != x.a_text)
133 		goto shread;
134 	addr = (char *)(x.a_text + 0x800);
135 	if (x.a_magic == 0413 || x.a_magic == 0410)
136 		while ((int)addr & CLOFSET)
137 			*addr++ = 0;
138 	printf("+%d", x.a_data);
139 	if (read(io, addr, x.a_data) != x.a_data)
140 		goto shread;
141 	addr += x.a_data;
142 	printf("+%d", x.a_bss);
143 	x.a_bss += 32*1024;	/* slop */
144 	for (i = 0; i < x.a_bss; i++)
145 		*addr++ = 0;
146 	x.a_entry &= 0x1fffffff;
147 	printf(" start 0x%x\n", x.a_entry);
148 	mtpr(PADC, 0);		/* Purge data cache */
149 	mtpr(PACC, 0);		/* Purge code cache */
150 	mtpr(DCR, 1);		/* Enable data cache */
151 	(*((int (*)()) x.a_entry))();
152 	return;
153 shread:
154 	_stop("Short read\n");
155 }
156