xref: /netbsd-src/sys/arch/pmax/stand/common/bootxx.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: bootxx.c,v 1.23 1999/11/27 06:34:06 simonb Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jonathan Stone, Michael Hitch and Simon Burge.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Copyright (c) 1992, 1993
41  *	The Regents of the University of California.  All rights reserved.
42  *
43  * This code is derived from software contributed to Berkeley by
44  * Ralph Campbell.
45  *
46  * Redistribution and use in source and binary forms, with or without
47  * modification, are permitted provided that the following conditions
48  * are met:
49  * 1. Redistributions of source code must retain the above copyright
50  *    notice, this list of conditions and the following disclaimer.
51  * 2. Redistributions in binary form must reproduce the above copyright
52  *    notice, this list of conditions and the following disclaimer in the
53  *    documentation and/or other materials provided with the distribution.
54  * 3. All advertising materials mentioning features or use of this software
55  *    must display the following acknowledgement:
56  *	This product includes software developed by the University of
57  *	California, Berkeley and its contributors.
58  * 4. Neither the name of the University nor the names of its contributors
59  *    may be used to endorse or promote products derived from this software
60  *    without specific prior written permission.
61  *
62  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72  * SUCH DAMAGE.
73  *
74  *	@(#)boot.c	8.1 (Berkeley) 6/10/93
75  */
76 
77 #include <sys/param.h>
78 #include <sys/exec_elf.h>
79 #include <lib/libsa/stand.h>
80 #include <machine/dec_prom.h>
81 
82 typedef void (*entrypt) __P((int, char **, int, const void *));
83 
84 int main __P((int, char **));
85 entrypt loadfile __P((char *path, char *name));
86 
87 extern int clear_cache __P((char *addr, int len));
88 extern int bcmp __P((const void *, const void *, size_t));	/* XXX */
89 
90 /*
91  * This gets arguments from the PROM, calls other routines to open
92  * and load the secondary boot loader called boot, and then transfers
93  * execution to that program.
94  *
95  * Argv[0] should be something like "rz(0,0,0)netbsd" on a DECstation 3100.
96  * Argv[0,1] should be something like "boot 5/rz0/netbsd" on a DECstation 5000.
97  * The argument "-a" means netbsd should do an automatic reboot.
98  */
99 int
100 main(argc, argv)
101 	int argc;
102 	char **argv;
103 {
104 	char *cp;
105 	entrypt entry;
106 
107 	/* check for DS5000 boot */
108 	if (strcmp(argv[0], "boot") == 0) {
109 		argc--;
110 		argv++;
111 	}
112 	cp = *argv;
113 
114         printf("\nNetBSD/pmax " NETBSD_VERS " " BOOTXX_FS_NAME " Primary Bootstrap\n");
115 
116 	entry = loadfile(cp, "/boot.pmax");
117 	if ((int)entry != -1)
118 		goto goodload;
119 
120 	/* Give old /boot a go... */
121 	entry = loadfile(cp, "/boot");
122 	if ((int)entry != -1)
123 		goto goodload;
124 
125 	/* Booting off an 8.3 filesystem? */
126 	entry = loadfile(cp, "/boot.pma");
127 	if ((int)entry != -1)
128 		goto goodload;
129 
130 	goto bad;
131 goodload:
132 
133 	clear_cache((char *)PRIMARY_LOAD_ADDRESS, 1024 * 1024);
134 	if (callv == &callvec)
135 		entry(argc, argv, 0, 0);
136 	else
137 		entry(argc, argv, DEC_PROM_MAGIC, callv);
138 bad:
139 	/* XXX would calling prom_halt here be cleaner? */
140 	return (1);
141 }
142 
143 /*
144  * Open 'filename', read in program and return the entry point or -1 if error.
145  */
146 entrypt
147 loadfile(path, name)
148 	char *path, *name;
149 {
150 	int fd, i;
151 	char c, *buf, bootfname[64];
152 	Elf32_Ehdr ehdr;
153 	Elf32_Phdr phdr;
154 
155 	strcpy(bootfname, path);
156 	buf = bootfname;
157 	while ((c = *buf++) != '\0') {
158 		if (c == ')')
159 			break;
160 		if (c != '/')
161 			continue;
162 		while ((c = *buf++) != '\0')
163 			if (c == '/')
164 				break;
165 		/*
166 		 * Make "N/rzY" with no trailing '/' valid by adding
167 		 * the extra '/' before appending 'bootpmax' to the path.
168 		 */
169 		if (c != '/') {
170 			buf--;
171 			*buf++ = '/';
172 			*buf = '\0';
173 		}
174 		break;
175 	}
176 	strcpy(buf, name);
177 	if ((fd = open(bootfname, 0)) < 0) {
178 		printf("open %s: %d\n", bootfname, errno);
179 		goto err;
180 	}
181 
182 	/* read the exec header */
183 	i = read(fd, (char *)&ehdr, sizeof(ehdr));
184 	if ((i != sizeof(ehdr)) ||
185 	    (bcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) ||
186 	    (ehdr.e_ident[EI_CLASS] != ELFCLASS32)) {
187 		printf("%s: No ELF header\n", bootfname);
188 		goto cerr;
189 	}
190 
191 	for (i = 0; i < ehdr.e_phnum; i++) {
192 		if (lseek(fd, (off_t) ehdr.e_phoff + i * sizeof(phdr), 0) < 0)
193 			goto cerr;
194 		if (read(fd, &phdr, sizeof(phdr)) != sizeof(phdr))
195 			goto cerr;
196 		if (phdr.p_type != PT_LOAD)
197 			continue;
198 		if (lseek(fd, (off_t)phdr.p_offset, 0) < 0)
199 			goto cerr;
200 		if (read(fd, (char *)phdr.p_paddr, phdr.p_filesz) != phdr.p_filesz)
201 			goto cerr;
202 	}
203 	return ((entrypt)ehdr.e_entry);
204 
205 cerr:
206 #ifndef LIBSA_NO_FS_CLOSE
207 	(void) close(fd);
208 #endif
209 err:
210 	printf("Can't load '%s'\n", bootfname);
211 	return ((entrypt)-1);
212 }
213