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