xref: /original-bsd/sys/hp300/stand/boot.c (revision 3705696b)
1 /*-
2  * Copyright (c) 1982, 1986, 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)boot.c	8.1 (Berkeley) 06/10/93
8  */
9 
10 #include <sys/param.h>
11 #include <sys/reboot.h>
12 #include <a.out.h>
13 #include <stand.att/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 <hp300/stand/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 	case HP_433:
65 		cp = "433"; break;
66 	default:
67 		cp = "???"; break;
68 	}
69 	printf("%s CPU\nBoot\n", cp);
70 #else
71 	printf("\nBoot\n");
72 #endif
73 #ifdef JUSTASK
74 	howto = RB_ASKNAME|RB_SINGLE;
75 #else
76 	if ((howto & RB_ASKNAME) == 0) {
77 		type = (bootdev >> B_TYPESHIFT) & B_TYPEMASK;
78 		if ((unsigned)type < ndevs && devsw[type].dv_name)
79 			strcpy(line, UNIX);
80 		else
81 			howto |= RB_SINGLE|RB_ASKNAME;
82 	}
83 #endif
84 	for (retry = 0;;) {
85 		if (!noconsole && (howto & RB_ASKNAME)) {
86 			printf(": ");
87 			gets(line);
88 			if (line[0] == 0) {
89 				strcpy(line, UNIX);
90 				printf(": %s\n", line);
91 			}
92 		} else
93 			printf(": %s\n", line);
94 		io = open(line, 0);
95 		if (io >= 0) {
96 #ifndef INSECURE
97 			(void) fstat(io, &sb);
98 			if (sb.st_uid || (sb.st_mode & 2)) {
99 				printf("non-secure file, will not load\n");
100 				close(io);
101 				howto = RB_SINGLE|RB_ASKNAME;
102 				continue;
103 			}
104 #endif
105 			copyunix(howto, opendev, io);
106 			close(io);
107 			howto |= RB_SINGLE|RB_ASKNAME;
108 		}
109 		if (++retry > 2)
110 			howto |= RB_SINGLE|RB_ASKNAME;
111 	}
112 }
113 
114 /*ARGSUSED*/
115 copyunix(howto, devtype, io)
116 	register int howto;	/* d7 contains boot flags */
117 	register u_int devtype;	/* d6 contains boot device */
118 	register int io;
119 {
120 	struct exec x;
121 	register int i;
122 	register char *load;	/* a5 contains load addr for unix */
123 	register char *addr;
124 
125 	i = read(io, (char *)&x, sizeof(x));
126 	if (i != sizeof(x) ||
127 	    (x.a_magic != OMAGIC && x.a_magic != ZMAGIC && x.a_magic != NMAGIC)) {
128 		printf("Bad format\n");
129 		return;
130 	}
131 	printf("%d", x.a_text);
132 	if (x.a_magic == ZMAGIC && lseek(io, 0x400, L_SET) == -1)
133 		goto shread;
134 	load = addr = lowram;
135 	if (read(io, (char *)addr, x.a_text) != x.a_text)
136 		goto shread;
137 	addr += x.a_text;
138 	if (x.a_magic == ZMAGIC || x.a_magic == NMAGIC)
139 		while ((int)addr & CLOFSET)
140 			*addr++ = 0;
141 	printf("+%d", x.a_data);
142 	if (read(io, addr, x.a_data) != x.a_data)
143 		goto shread;
144 	addr += x.a_data;
145 	printf("+%d", x.a_bss);
146 	x.a_bss += 128*512;	/* slop */
147 	for (i = 0; i < x.a_bss; i++)
148 		*addr++ = 0;
149 	x.a_entry += (int)lowram;
150 	printf(" start 0x%x\n", x.a_entry);
151 #ifdef __GNUC__
152 	asm("	movl %0,d7" : : "m" (howto));
153 	asm("	movl %0,d6" : : "m" (devtype));
154 	asm("	movl %0,a5" : : "a" (load));
155 #endif
156 	(*((int (*)()) x.a_entry))();
157 	return;
158 shread:
159 	printf("Short read\n");
160 	return;
161 }
162