xref: /netbsd/sys/arch/mvme68k/stand/bootxx/bootxx.c (revision 6550d01e)
1 /*	$NetBSD: bootxx.c,v 1.16 2009/08/22 10:02:21 he 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * This is a generic "first-stage" boot program.
34  *
35  * Note that this program has absolutely no filesystem knowledge!
36  *
37  * Instead, this uses a table of disk block numbers that are
38  * filled in by the installboot program such that this program
39  * can load the "second-stage" boot program.
40  */
41 
42 #include <sys/param.h>
43 #include <sys/time.h>
44 #include <sys/exec.h>
45 #include <sys/exec_elf.h>
46 #include <sys/exec_aout.h>
47 #include <machine/prom.h>
48 
49 #include <lib/libsa/stand.h>
50 #include "libsa.h"
51 #include "bootxx.h"
52 
53 int copyboot(struct open_file *, u_long *);
54 
55 /*
56  * Boot device is derived from ROM provided information.
57  */
58 #define LOADADDR	0x11000 /* where to load level 2 bootstrap */
59 				/* (l2 must relocate itself) */
60 
61 extern int     	block_size;
62 extern int     	block_count;	/* length of table */
63 /* XXX ondisk32 */
64 extern int32_t 	block_table[];
65 
66 extern		char bootprog_name[], bootprog_rev[];
67 
68 int main(void);
69 
70 int
71 main(void)
72 {
73 	struct open_file f;
74 	u_long addr;
75 	char *foo;
76 	int error;
77 
78 	printf("Boot: bug device: ctrl=%d, dev=%d\n",
79 	    bugargs.ctrl_lun, bugargs.dev_lun);
80 	printf("\nbootxx: %s first level bootstrap program [%s]\n\n",
81 	    bootprog_name, bootprog_rev);
82 
83 	f.f_flags = F_RAW;
84 	if (devopen(&f, 0, &foo)) {
85 		printf("bootxx: open failed\n");
86 		_rtt();
87 	}
88 
89 	addr = LOADADDR;
90 	error = copyboot(&f, &addr);
91 	f.f_dev->dv_close(&f);
92 	if (error == 0)
93 		bugexec((void *)addr);
94 
95 	/* copyboot had a problem... */
96 	_rtt();
97 }
98 
99 int
100 copyboot(struct open_file *fp, u_long *addr)
101 {
102 	int	i, blknum;
103 	size_t	n;
104 	char	*laddr = (char *) *addr;
105 	union boothead {
106 		struct exec ah;
107 		Elf32_Ehdr eh;
108 	} *x;
109 
110 	x = (union boothead *)laddr;
111 
112 	if (!block_count) {
113 		printf("bootxx: no data!?!\n");
114 		return -1;
115 	}
116 
117 	for (i = 0; i < block_count; i++) {
118 		if ((blknum = block_table[i]) == 0)
119 			break;
120 #ifdef DEBUG
121 		printf("bootxx: read block # %d = %d\n", i, blknum);
122 #endif
123 		if ((fp->f_dev->dv_strategy)(fp->f_devdata, F_READ,
124 		    blknum, block_size, laddr, &n)) {
125 			printf("bootxx: read failed\n");
126 			return -1;
127 		}
128 		if (n != block_size) {
129 			printf("bootxx: short read\n");
130 			return -1;
131 		}
132 		laddr += block_size;
133 	}
134 
135 	if (N_GETMAGIC(x->ah) == OMAGIC)
136 		*addr += sizeof(x->ah);
137 	else if (memcmp(x->eh.e_ident, ELFMAG, SELFMAG) == 0) {
138 		Elf32_Phdr *ep = (Elf32_Phdr *)(*addr + x->eh.e_phoff);
139 		*addr += ep->p_offset;
140 	} else {
141 		printf("bootxx: secondary bootstrap isn't an executable\n");
142 		return -1;
143 	}
144 
145 	return 0;
146 }
147