xref: /netbsd/sys/arch/mvme68k/stand/bootxx/bootxx.c (revision bf9ec67e)
1 /*	$NetBSD: bootxx.c,v 1.8 2002/05/05 20:11:37 jdolecek Exp $ */
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Paul Kranenburg.
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  * This is a generic "first-stage" boot program.
41  *
42  * Note that this program has absolutely no filesystem knowledge!
43  *
44  * Instead, this uses a table of disk block numbers that are
45  * filled in by the installboot program such that this program
46  * can load the "second-stage" boot program.
47  */
48 
49 #include <sys/param.h>
50 #include <sys/time.h>
51 #include <sys/exec.h>
52 #include <sys/exec_elf.h>
53 #include <machine/prom.h>
54 
55 #include "stand.h"
56 #include "libsa.h"
57 #include "bootxx.h"
58 
59 int copyboot __P((struct open_file *, u_long *));
60 
61 /*
62  * Boot device is derived from ROM provided information.
63  */
64 #define LOADADDR	0x11000 /* where to load level 2 bootstrap */
65 				/* (l2 must relocate itself) */
66 
67 extern int     	block_size;
68 extern int     	block_count;	/* length of table */
69 extern daddr_t 	block_table[];
70 
71 extern		char bootprog_name[], bootprog_rev[];
72 
73 int main(void);
74 
75 int
76 main()
77 {
78 	struct open_file	f;
79 	u_long	addr;
80 	char *foo;
81 	int error;
82 
83 	printf("Boot: bug device: ctrl=%d, dev=%d\n",
84 		bugargs.ctrl_lun, bugargs.dev_lun);
85 	printf("\nbootxx: %s first level bootstrap program [%s]\n\n",
86 		bootprog_name, bootprog_rev);
87 
88 	f.f_flags = F_RAW;
89 	if (devopen(&f, 0, &foo)) {
90 		printf("bootxx: open failed\n");
91 		_rtt();
92 	}
93 
94 	addr = LOADADDR;
95 	error = copyboot(&f, &addr);
96 	f.f_dev->dv_close(&f);
97 	if (!error)
98 		bugexec((void *)addr);
99 
100 	/* copyboot had a problem... */
101 	_rtt();
102 }
103 
104 int
105 copyboot(fp, addr)
106 	struct open_file	*fp;
107 	u_long			*addr;
108 {
109 	int	n, i, blknum;
110 	char	*laddr = (char *) *addr;
111 	union boothead {
112 		struct exec ah;
113 		Elf32_Ehdr eh;
114 	} *x;
115 
116 	x = (union boothead *)laddr;
117 
118 	if (!block_count) {
119 		printf("bootxx: no data!?!\n");
120 		return -1;
121 	}
122 
123 	for (i = 0; i < block_count; i++) {
124 
125 		if ((blknum = block_table[i]) == 0)
126 			break;
127 
128 #ifdef DEBUG
129 		printf("bootxx: read block # %d = %d\n", i, blknum);
130 #endif
131 		if ((fp->f_dev->dv_strategy)(fp->f_devdata, F_READ,
132 					   blknum, block_size, laddr, &n))
133 		{
134 			printf("bootxx: read failed\n");
135 			return -1;
136 		}
137 		if (n != block_size) {
138 			printf("bootxx: short read\n");
139 			return -1;
140 		}
141 		laddr += block_size;
142 	}
143 
144 	if (N_GETMAGIC(x->ah) == OMAGIC)
145 		*addr += sizeof(x->ah);
146 	else
147 	if (memcmp(x->eh.e_ident, ELFMAG, SELFMAG) == 0) {
148 		Elf32_Phdr *ep = (Elf32_Phdr *)(*addr + x->eh.e_phoff);
149 		*addr += ep->p_offset;
150 	} else {
151 		printf("bootxx: secondary bootstrap isn't an executable\n");
152 		return(-1);
153 	}
154 
155 	return (0);
156 }
157