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