xref: /netbsd-src/sys/arch/sun68k/stand/bootyy/bootyy.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: bootyy.c,v 1.3 2005/12/11 12:19:29 christos 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 <machine/mon.h>
53 
54 #include <stand.h>
55 #include "libsa.h"
56 
57 /* This determines the largest boot program we can load. */
58 #define MAXBLOCKNUM	64
59 
60 /*
61  * The block size used in the Sun Network Disk protocol.
62  */
63 #define SUNND_BSIZE (512)
64 
65 /*
66  * The first block number we request.  This is related to historical
67  * values; in 4.2BSD, the disklabel and the first stage boot program
68  * are together called the bootblocks, and take up the first BBSIZE
69  * bytes of the disk.  In 4.2BSD, BBSIZE was 8192 and the disk device
70  * block size was 512 bytes (see the definition of SUNND_BSIZE below),
71  * making the number of blocks taken up by the bootblocks (BBSIZE /
72  * SUNND_BSIZE).  This value for FIRSTBLOCKNUM assumes that the
73  * second-stage boot begins immediately after the bootblocks.  The
74  * second-stage boot always runs contiguously for MAXBLOCKNUM blocks.
75  * FIRSTBLOCKNUM can be increased, and MAXBLOCKNUM can be adjusted, as
76  * long as they match up with however you're putting together the disk
77  * image that is being served to this client.
78  */
79 #define FIRSTBLOCKNUM (8192 / SUNND_BSIZE)
80 
81 int     	block_size = SUNND_BSIZE;	/* default */
82 
83 int
84 main(void)
85 {
86 	struct open_file	f;
87 	void	*entry;
88 	char	*addr;
89 	int n, error;
90 
91 #ifdef DEBUG
92 	printf("bootyy: open...\n");
93 #endif
94 	f.f_flags = F_RAW;
95 	if (devopen(&f, 0, &addr)) {
96 		printf("bootyy: devopen failed\n");
97 		return;
98 	}
99 
100 	addr = (char *)KERN_LOADADDR;
101 	error = copyboot(&f, addr);
102 	f.f_dev->dv_close(&f);
103 	if (!error) {
104 #ifdef DEBUG
105 		printf("bootyy: start 0x%x\n", (long)addr);
106 #endif
107 		entry = addr;
108 		chain_to(entry);
109 	}
110 	/* copyboot had a problem... */
111 	return;
112 }
113 
114 int
115 copyboot(struct open_file *fp, char *addr)
116 {
117 	int	n, i, blknum;
118 	char *buf;
119 
120 	/* Need to use a buffer that can be mapped into DVMA space. */
121 	buf = alloc(block_size);
122 	if (!buf)
123 		panic("bootyy: alloc failed");
124 
125 	for (i = 0, blknum = FIRSTBLOCKNUM; i < MAXBLOCKNUM; i++, blknum++) {
126 
127 #ifdef DEBUG
128 		printf("bootyy: block # %d = %d\n", i, blknum);
129 #endif
130 		if ((fp->f_dev->dv_strategy)(fp->f_devdata, F_READ,
131 					   blknum, block_size, buf, &n))
132 		{
133 			printf("bootyy: read failed\n");
134 			return -1;
135 		}
136 		if (n != block_size) {
137 			printf("bootyy: short read\n");
138 			return -1;
139 		}
140 		memcpy(addr, buf, block_size);
141 		addr += block_size;
142 	}
143 
144 	return 0;
145 }
146