xref: /netbsd-src/sys/arch/mipsco/stand/common/bootxx.c (revision 82357f6d420792564499086cb6440ad2fcce8410)
1*82357f6dSdsl /*	$NetBSD: bootxx.c,v 1.9 2009/03/14 21:04:12 dsl Exp $	*/
2a3ec1726Swdk 
3a3ec1726Swdk /*-
4a3ec1726Swdk  * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
5a3ec1726Swdk  * All rights reserved.
6a3ec1726Swdk  *
7a3ec1726Swdk  * This code is derived from software contributed to The NetBSD Foundation
8a3ec1726Swdk  * by Jonathan Stone, Michael Hitch, Simon Burge and Wayne Knowles.
9a3ec1726Swdk  *
10a3ec1726Swdk  * Redistribution and use in source and binary forms, with or without
11a3ec1726Swdk  * modification, are permitted provided that the following conditions
12a3ec1726Swdk  * are met:
13a3ec1726Swdk  * 1. Redistributions of source code must retain the above copyright
14a3ec1726Swdk  *    notice, this list of conditions and the following disclaimer.
15a3ec1726Swdk  * 2. Redistributions in binary form must reproduce the above copyright
16a3ec1726Swdk  *    notice, this list of conditions and the following disclaimer in the
17a3ec1726Swdk  *    documentation and/or other materials provided with the distribution.
18a3ec1726Swdk  *
19a3ec1726Swdk  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20a3ec1726Swdk  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21a3ec1726Swdk  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22a3ec1726Swdk  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23a3ec1726Swdk  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24a3ec1726Swdk  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25a3ec1726Swdk  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26a3ec1726Swdk  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27a3ec1726Swdk  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28a3ec1726Swdk  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29a3ec1726Swdk  * POSSIBILITY OF SUCH DAMAGE.
30a3ec1726Swdk  */
31a3ec1726Swdk 
32a3ec1726Swdk /*
33a3ec1726Swdk  * Copyright (c) 1992, 1993
34a3ec1726Swdk  *	The Regents of the University of California.  All rights reserved.
35a3ec1726Swdk  *
36a3ec1726Swdk  * This code is derived from software contributed to Berkeley by
37a3ec1726Swdk  * Ralph Campbell.
38a3ec1726Swdk  *
39a3ec1726Swdk  * Redistribution and use in source and binary forms, with or without
40a3ec1726Swdk  * modification, are permitted provided that the following conditions
41a3ec1726Swdk  * are met:
42a3ec1726Swdk  * 1. Redistributions of source code must retain the above copyright
43a3ec1726Swdk  *    notice, this list of conditions and the following disclaimer.
44a3ec1726Swdk  * 2. Redistributions in binary form must reproduce the above copyright
45a3ec1726Swdk  *    notice, this list of conditions and the following disclaimer in the
46a3ec1726Swdk  *    documentation and/or other materials provided with the distribution.
47aad01611Sagc  * 3. Neither the name of the University nor the names of its contributors
48a3ec1726Swdk  *    may be used to endorse or promote products derived from this software
49a3ec1726Swdk  *    without specific prior written permission.
50a3ec1726Swdk  *
51a3ec1726Swdk  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52a3ec1726Swdk  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53a3ec1726Swdk  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54a3ec1726Swdk  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55a3ec1726Swdk  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56a3ec1726Swdk  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57a3ec1726Swdk  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58a3ec1726Swdk  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59a3ec1726Swdk  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60a3ec1726Swdk  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61a3ec1726Swdk  * SUCH DAMAGE.
62a3ec1726Swdk  *
63a3ec1726Swdk  *	@(#)boot.c	8.1 (Berkeley) 6/10/93
64a3ec1726Swdk  */
65a3ec1726Swdk 
66a3ec1726Swdk #include <sys/param.h>
67a3ec1726Swdk #include <sys/exec_elf.h>
68a3ec1726Swdk #include <lib/libsa/stand.h>
69a3ec1726Swdk #include <machine/prom.h>
70a3ec1726Swdk 
7102cdf4d2Sdsl typedef void (*entrypt)(int, char **, int, const void *);
72a3ec1726Swdk 
7302cdf4d2Sdsl int main(int, char **);
7402cdf4d2Sdsl entrypt loadfile(char *path, char *name);
75a3ec1726Swdk 
76a3ec1726Swdk /*
77a3ec1726Swdk  * This gets arguments from the PROM, calls other routines to open
78a3ec1726Swdk  * and load the secondary boot loader called boot, and then transfers
79a3ec1726Swdk  * execution to that program.
80a3ec1726Swdk  */
81a3ec1726Swdk int
main(int argc,char ** argv)82454af1c0Sdsl main(int argc, char **argv)
83a3ec1726Swdk {
84a3ec1726Swdk 	entrypt entry;
85a3ec1726Swdk 	char *cp;
8602cdf4d2Sdsl 	extern void prom_init(void);
87a3ec1726Swdk 
88a3ec1726Swdk 	prom_init();
89a3ec1726Swdk 
9050e120c1Swdk 	cp = *argv;
91a3ec1726Swdk 
92a3ec1726Swdk         printf("\nNetBSD/mipsco " NETBSD_VERS " " BOOTXX_FS_NAME " Primary Bootstrap\n");
93a3ec1726Swdk 
94a3ec1726Swdk 	entry = loadfile(cp, "/boot");
95a3ec1726Swdk 	if ((int)entry != -1)
96a3ec1726Swdk 		goto goodload;
97a3ec1726Swdk 
98a3ec1726Swdk 	entry = loadfile(cp, "/boot.mipsco");
99a3ec1726Swdk 	if ((int)entry != -1)
100a3ec1726Swdk 		goto goodload;
101a3ec1726Swdk 
102a3ec1726Swdk 	goto bad;
103a3ec1726Swdk 
104a3ec1726Swdk  goodload:
105a3ec1726Swdk 	MIPS_PROM(flushcache)();
106a3ec1726Swdk 	entry(argc, argv, 0, 0);
107a3ec1726Swdk 
108a3ec1726Swdk  bad:
109a3ec1726Swdk 	return (1);
110a3ec1726Swdk }
111a3ec1726Swdk 
112a3ec1726Swdk /*
113a3ec1726Swdk  * Open 'filename', read in program and return the entry point or -1 if error.
114a3ec1726Swdk  */
115a3ec1726Swdk entrypt
loadfile(char * path,char * name)116*82357f6dSdsl loadfile(char *path, char *name)
117a3ec1726Swdk {
118a3ec1726Swdk 	int fd, i;
119a3ec1726Swdk 	char *src, *dst, bootfname[64];
120a3ec1726Swdk 	Elf32_Ehdr ehdr;
121a3ec1726Swdk 	Elf32_Phdr phdr;
122a3ec1726Swdk 
12350e120c1Swdk 	dst = bootfname;
12450e120c1Swdk 	for (src = path; *src;/**/)
12550e120c1Swdk 		if ((*dst++ = *src++) == ')')
12650e120c1Swdk 			break;
12750e120c1Swdk 	for (src = name; *src;/**/)
12850e120c1Swdk 		*dst++ = *src++;
12950e120c1Swdk 	*dst = (char) 0;
130a3ec1726Swdk 
131a3ec1726Swdk 	if ((fd = open(bootfname, 0)) < 0) {
132a3ec1726Swdk 		printf("open %s: %d\n", bootfname, errno);
133a3ec1726Swdk 		goto err;
134a3ec1726Swdk 	}
135a3ec1726Swdk 
136a3ec1726Swdk 	/* read the exec header */
137a3ec1726Swdk 	i = read(fd, (char *)&ehdr, sizeof(ehdr));
138a3ec1726Swdk 	if ((i != sizeof(ehdr)) ||
139fa993060Swdk 	    (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) ||
140a3ec1726Swdk 	    (ehdr.e_ident[EI_CLASS] != ELFCLASS32)) {
141a3ec1726Swdk 		printf("%s: No ELF header\n", bootfname);
142a3ec1726Swdk 		goto cerr;
143a3ec1726Swdk 	}
144a3ec1726Swdk 
145a3ec1726Swdk 	for (i = 0; i < ehdr.e_phnum; i++) {
146a3ec1726Swdk 		if (lseek(fd, (off_t) ehdr.e_phoff + i * sizeof(phdr), 0) < 0)
147a3ec1726Swdk 			goto cerr;
148a3ec1726Swdk 		if (read(fd, &phdr, sizeof(phdr)) != sizeof(phdr))
149a3ec1726Swdk 			goto cerr;
150a3ec1726Swdk 		if (phdr.p_type != PT_LOAD)
151a3ec1726Swdk 			continue;
152a3ec1726Swdk 		if (lseek(fd, (off_t)phdr.p_offset, 0) < 0)
153a3ec1726Swdk 			goto cerr;
154a3ec1726Swdk 		if (read(fd, (char *)phdr.p_paddr, phdr.p_filesz) != phdr.p_filesz)
155a3ec1726Swdk 			goto cerr;
156a3ec1726Swdk 	}
157a3ec1726Swdk 	return ((entrypt)ehdr.e_entry);
158a3ec1726Swdk 
159a3ec1726Swdk  cerr:
160a3ec1726Swdk #ifndef LIBSA_NO_FS_CLOSE
161a3ec1726Swdk 	(void) close(fd);
162a3ec1726Swdk #endif
163a3ec1726Swdk  err:
164a3ec1726Swdk 	printf("Can't load '%s'\n", bootfname);
165a3ec1726Swdk 	return ((entrypt)-1);
166a3ec1726Swdk }
167