xref: /netbsd-src/sys/arch/emips/stand/common/bootxx.c (revision fad88a189bc738ae57de078fa5bb457df7ffa219)
1*fad88a18Stsutsui /*	$NetBSD: bootxx.c,v 1.2 2019/12/14 02:58:20 tsutsui Exp $	*/
25f7e80a8Spooka 
35f7e80a8Spooka /*-
45f7e80a8Spooka  * Copyright (c) 1999 The NetBSD Foundation, Inc.
55f7e80a8Spooka  * All rights reserved.
65f7e80a8Spooka  *
75f7e80a8Spooka  * This code is derived from software contributed to The NetBSD Foundation
85f7e80a8Spooka  * by Jonathan Stone, Michael Hitch and Simon Burge.
95f7e80a8Spooka  *
105f7e80a8Spooka  * Redistribution and use in source and binary forms, with or without
115f7e80a8Spooka  * modification, are permitted provided that the following conditions
125f7e80a8Spooka  * are met:
135f7e80a8Spooka  * 1. Redistributions of source code must retain the above copyright
145f7e80a8Spooka  *    notice, this list of conditions and the following disclaimer.
155f7e80a8Spooka  * 2. Redistributions in binary form must reproduce the above copyright
165f7e80a8Spooka  *    notice, this list of conditions and the following disclaimer in the
175f7e80a8Spooka  *    documentation and/or other materials provided with the distribution.
185f7e80a8Spooka  *
195f7e80a8Spooka  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
205f7e80a8Spooka  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
215f7e80a8Spooka  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
225f7e80a8Spooka  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
235f7e80a8Spooka  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
245f7e80a8Spooka  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
255f7e80a8Spooka  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
265f7e80a8Spooka  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
275f7e80a8Spooka  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
285f7e80a8Spooka  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
295f7e80a8Spooka  * POSSIBILITY OF SUCH DAMAGE.
305f7e80a8Spooka  */
315f7e80a8Spooka 
325f7e80a8Spooka /*
335f7e80a8Spooka  * Copyright (c) 1992, 1993
345f7e80a8Spooka  *	The Regents of the University of California.  All rights reserved.
355f7e80a8Spooka  *
365f7e80a8Spooka  * This code is derived from software contributed to Berkeley by
375f7e80a8Spooka  * Ralph Campbell.
385f7e80a8Spooka  *
395f7e80a8Spooka  * Redistribution and use in source and binary forms, with or without
405f7e80a8Spooka  * modification, are permitted provided that the following conditions
415f7e80a8Spooka  * are met:
425f7e80a8Spooka  * 1. Redistributions of source code must retain the above copyright
435f7e80a8Spooka  *    notice, this list of conditions and the following disclaimer.
445f7e80a8Spooka  * 2. Redistributions in binary form must reproduce the above copyright
455f7e80a8Spooka  *    notice, this list of conditions and the following disclaimer in the
465f7e80a8Spooka  *    documentation and/or other materials provided with the distribution.
475f7e80a8Spooka  * 3. Neither the name of the University nor the names of its contributors
485f7e80a8Spooka  *    may be used to endorse or promote products derived from this software
495f7e80a8Spooka  *    without specific prior written permission.
505f7e80a8Spooka  *
515f7e80a8Spooka  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
525f7e80a8Spooka  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
535f7e80a8Spooka  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
545f7e80a8Spooka  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
555f7e80a8Spooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
565f7e80a8Spooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
575f7e80a8Spooka  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
585f7e80a8Spooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
595f7e80a8Spooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
605f7e80a8Spooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
615f7e80a8Spooka  * SUCH DAMAGE.
625f7e80a8Spooka  *
635f7e80a8Spooka  *	@(#)boot.c	8.1 (Berkeley) 6/10/93
645f7e80a8Spooka  */
655f7e80a8Spooka 
665f7e80a8Spooka #include <sys/param.h>
675f7e80a8Spooka #include <sys/exec_elf.h>
685f7e80a8Spooka #include <lib/libsa/stand.h>
695f7e80a8Spooka #include <lib/libkern/libkern.h>
705f7e80a8Spooka 
715f7e80a8Spooka typedef void (entrypt) (int, char **, int, const void *, int, char *);
725f7e80a8Spooka 
735f7e80a8Spooka entrypt main;
745f7e80a8Spooka entrypt *loadfile (char *path, char *name);
755f7e80a8Spooka 
765f7e80a8Spooka /*
775f7e80a8Spooka  * This gets arguments from the PROM, calls other routines to open
785f7e80a8Spooka  * and load the secondary boot loader called boot, and then transfers
795f7e80a8Spooka  * execution to that program.
805f7e80a8Spooka  */
815f7e80a8Spooka void
main(int argc,char ** argv,int code,const void * cv,int bim,char * bip)825f7e80a8Spooka main(int argc, char **argv, int code, const void *cv, int bim, char *bip)
835f7e80a8Spooka {
845f7e80a8Spooka 	char *cp;
855f7e80a8Spooka 	entrypt *entry;
865f7e80a8Spooka 
875f7e80a8Spooka 	cp = *argv;
885f7e80a8Spooka 
895f7e80a8Spooka     printf("\nNetBSD/emips " NETBSD_VERS " " BOOTXX_FS_NAME " Bootstrap\n");
905f7e80a8Spooka 
915f7e80a8Spooka 	entry = loadfile(cp, "netbsd");
925f7e80a8Spooka 	if ((int)entry != -1)
935f7e80a8Spooka 		goto goodload;
945f7e80a8Spooka 
955f7e80a8Spooka 	/* Give old /boot a go... */
965f7e80a8Spooka 	entry = loadfile(cp, "/boot");
975f7e80a8Spooka 	if ((int)entry != -1)
985f7e80a8Spooka 		goto goodload;
995f7e80a8Spooka 
1005f7e80a8Spooka 	goto bad;
1015f7e80a8Spooka goodload:
1025f7e80a8Spooka 
1035f7e80a8Spooka     entry(argc, argv, code, cv, bim, bip);
1045f7e80a8Spooka bad:
1055f7e80a8Spooka     printf("Boot failed.\n");
1065f7e80a8Spooka }
1075f7e80a8Spooka 
1085f7e80a8Spooka /*
1095f7e80a8Spooka  * Open 'filename', read in program and return the entry point or -1 if error.
1105f7e80a8Spooka  */
1115f7e80a8Spooka entrypt *
loadfile(char * path,char * name)1125f7e80a8Spooka loadfile(char *path, char *name)
1135f7e80a8Spooka {
1145f7e80a8Spooka 	int fd, i;
1155f7e80a8Spooka 	char c, *buf, bootfname[64];
1165f7e80a8Spooka 	Elf32_Ehdr ehdr;
1175f7e80a8Spooka 	Elf32_Phdr phdr;
1185f7e80a8Spooka 
1195f7e80a8Spooka 	strcpy(bootfname, path);
1205f7e80a8Spooka 	buf = bootfname;
1215f7e80a8Spooka 	while ((c = *buf++) != '\0') {
1225f7e80a8Spooka 		if (c == ')')
1235f7e80a8Spooka 			break;
1245f7e80a8Spooka 		if (c != '/')
1255f7e80a8Spooka 			continue;
1265f7e80a8Spooka 		while ((c = *buf++) != '\0')
1275f7e80a8Spooka 			if (c == '/')
1285f7e80a8Spooka 				break;
1295f7e80a8Spooka 		/*
1305f7e80a8Spooka 		 * Make "N/rzY" with no trailing '/' valid by adding
1315f7e80a8Spooka 		 * the extra '/' before appending 'bootemips' to the path.
1325f7e80a8Spooka 		 */
1335f7e80a8Spooka 		if (c != '/') {
1345f7e80a8Spooka 			buf--;
1355f7e80a8Spooka 			*buf++ = '/';
1365f7e80a8Spooka 			*buf = '\0';
1375f7e80a8Spooka 		}
1385f7e80a8Spooka 		break;
1395f7e80a8Spooka 	}
1405f7e80a8Spooka 	strcpy(buf, name);
1415f7e80a8Spooka 	if ((fd = open(bootfname, 0)) < 0) {
1425f7e80a8Spooka 		printf("open %s: %d\n", bootfname, errno);
1435f7e80a8Spooka 		goto err;
1445f7e80a8Spooka 	}
1455f7e80a8Spooka 
1465f7e80a8Spooka 	/* read the exec header */
1475f7e80a8Spooka 	i = read(fd, (char *)&ehdr, sizeof(ehdr));
1485f7e80a8Spooka 	if ((i != sizeof(ehdr)) ||
1495f7e80a8Spooka 	    (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) ||
1505f7e80a8Spooka 	    (ehdr.e_ident[EI_CLASS] != ELFCLASS32)) {
1515f7e80a8Spooka 		printf("%s: No ELF header\n", bootfname);
1525f7e80a8Spooka 		goto cerr;
1535f7e80a8Spooka 	}
1545f7e80a8Spooka 
1555f7e80a8Spooka 	for (i = 0; i < ehdr.e_phnum; i++) {
1565f7e80a8Spooka 		if (lseek(fd, (off_t) ehdr.e_phoff + i * sizeof(phdr), 0) < 0)
1575f7e80a8Spooka 			goto cerr;
1585f7e80a8Spooka 		if (read(fd, &phdr, sizeof(phdr)) != sizeof(phdr))
1595f7e80a8Spooka 			goto cerr;
1605f7e80a8Spooka 		if (phdr.p_type != PT_LOAD)
1615f7e80a8Spooka 			continue;
1625f7e80a8Spooka 		if (lseek(fd, (off_t)phdr.p_offset, 0) < 0)
1635f7e80a8Spooka 			goto cerr;
1645f7e80a8Spooka 		if (read(fd, (char *)phdr.p_paddr, phdr.p_filesz) != phdr.p_filesz)
1655f7e80a8Spooka 			goto cerr;
1665f7e80a8Spooka 	}
1675f7e80a8Spooka 	return ((entrypt*)ehdr.e_entry);
1685f7e80a8Spooka 
1695f7e80a8Spooka cerr:
1705f7e80a8Spooka #ifndef LIBSA_NO_FS_CLOSE
1715f7e80a8Spooka 	(void) close(fd);
1725f7e80a8Spooka #endif
1735f7e80a8Spooka err:
1745f7e80a8Spooka 	printf("Can't load '%s'\n", bootfname);
1755f7e80a8Spooka 	return ((entrypt*)-1);
1765f7e80a8Spooka }
177