xref: /original-bsd/sys/hp300/stand/boot.c (revision 6093a5ae)
1 /*-
2  * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)boot.c	7.5 (Berkeley) 06/18/92
8  */
9 
10 #include "sys/param.h"
11 #include "sys/reboot.h"
12 #include <a.out.h>
13 #include "saio.h"
14 
15 #ifndef INSECURE
16 #include "sys/stat.h"
17 struct stat sb;
18 #endif
19 
20 #define	PRTCPU		/* print out cpu type */
21 
22 /*
23  * Boot program... bits in `howto' determine whether boot stops to
24  * ask for system name.  Boot device is derived from ROM provided
25  * information.
26  */
27 
28 char line[100];
29 
30 extern	unsigned opendev;
31 extern	char *lowram;
32 extern	int noconsole;
33 extern	int howto, bootdev;
34 
35 #ifdef PRTCPU
36 #include "samachdep.h"
37 #endif
38 
39 main()
40 {
41 	register char *cp;
42 	int io, retry, type;
43 #ifdef PRTCPU
44 	extern int machineid;
45 
46 	printf("\nHP");
47 	switch (machineid) {
48 	case HP_320:
49 		cp = "320"; break;
50 	case HP_330:
51 		cp = "318/319/330"; break;
52 	case HP_340:
53 		cp = "340"; break;
54 	case HP_350:
55 		cp = "350"; break;
56 	case HP_360:
57 		cp = "360"; break;
58 	case HP_370:
59 		cp = "370"; break;
60 	case HP_375:
61 		cp = "345/375/400"; break;
62 	case HP_380:
63 		cp = "380/425"; break;
64 	default:
65 		cp = "???"; break;
66 	}
67 	printf("%s CPU\nBoot\n", cp);
68 #else
69 	printf("\nBoot\n");
70 #endif
71 #ifdef JUSTASK
72 	howto = RB_ASKNAME|RB_SINGLE;
73 #else
74 	if ((howto & RB_ASKNAME) == 0) {
75 		type = (bootdev >> B_TYPESHIFT) & B_TYPEMASK;
76 		if ((unsigned)type < ndevs && devsw[type].dv_name)
77 			strcpy(line, UNIX);
78 		else
79 			howto |= RB_SINGLE|RB_ASKNAME;
80 	}
81 #endif
82 	for (retry = 0;;) {
83 		if (!noconsole && (howto & RB_ASKNAME)) {
84 			printf(": ");
85 			gets(line);
86 			if (line[0] == 0) {
87 				strcpy(line, UNIX);
88 				printf(": %s\n", line);
89 			}
90 		} else
91 			printf(": %s\n", line);
92 		io = open(line, 0);
93 		if (io >= 0) {
94 #ifndef INSECURE
95 			(void) fstat(io, &sb);
96 			if (sb.st_uid || (sb.st_mode & 2)) {
97 				printf("non-secure file, will not load\n");
98 				close(io);
99 				howto = RB_SINGLE|RB_ASKNAME;
100 				continue;
101 			}
102 #endif
103 			copyunix(howto, opendev, io);
104 			close(io);
105 			howto |= RB_SINGLE|RB_ASKNAME;
106 		}
107 		if (++retry > 2)
108 			howto |= RB_SINGLE|RB_ASKNAME;
109 	}
110 }
111 
112 /*ARGSUSED*/
113 copyunix(howto, devtype, io)
114 	register int howto;	/* d7 contains boot flags */
115 	register u_int devtype;	/* d6 contains boot device */
116 	register int io;
117 {
118 	struct exec x;
119 	register int i;
120 	register char *load;	/* a5 contains load addr for unix */
121 	register char *addr;
122 
123 	i = read(io, (char *)&x, sizeof(x));
124 	if (i != sizeof(x) ||
125 	    (x.a_magic != OMAGIC && x.a_magic != ZMAGIC && x.a_magic != NMAGIC)) {
126 		printf("Bad format\n");
127 		return;
128 	}
129 	printf("%d", x.a_text);
130 	if (x.a_magic == ZMAGIC && lseek(io, 0x400, L_SET) == -1)
131 		goto shread;
132 	load = addr = lowram;
133 	if (read(io, (char *)addr, x.a_text) != x.a_text)
134 		goto shread;
135 	addr += x.a_text;
136 	if (x.a_magic == ZMAGIC || x.a_magic == NMAGIC)
137 		while ((int)addr & CLOFSET)
138 			*addr++ = 0;
139 	printf("+%d", x.a_data);
140 	if (read(io, addr, x.a_data) != x.a_data)
141 		goto shread;
142 	addr += x.a_data;
143 	printf("+%d", x.a_bss);
144 	x.a_bss += 128*512;	/* slop */
145 	for (i = 0; i < x.a_bss; i++)
146 		*addr++ = 0;
147 	x.a_entry += (int)lowram;
148 	printf(" start 0x%x\n", x.a_entry);
149 #ifdef __GNUC__
150 	asm("	movl %0,d7" : : "m" (howto));
151 	asm("	movl %0,d6" : : "m" (devtype));
152 	asm("	movl %0,a5" : : "a" (load));
153 #endif
154 	(*((int (*)()) x.a_entry))();
155 	return;
156 shread:
157 	printf("Short read\n");
158 	return;
159 }
160