bootxx.c revision 1.1
1/*	$NetBSD: bootxx.c,v 1.1 2001/06/14 12:57:12 fredette 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 <machine/mon.h>
51
52#include <stand.h>
53#include "libsa.h"
54
55/*
56 * This is the address where we load the second-stage boot loader.
57 */
58#define LOADADDR	0x4000
59
60/* This determines the largest boot program we can load. */
61#define MAXBLOCKNUM	64
62
63/*
64 * These three names are known by installboot.
65 * The block_table contains starting block numbers,
66 * in terms of 512-byte blocks.  Each non-zero value
67 * will result in a read of block_size bytes.
68 */
69int     	block_size = 512;	/* default */
70int     	block_count = MAXBLOCKNUM;	/* length of table */
71daddr_t 	block_table[MAXBLOCKNUM] = { 0 };
72
73
74int
75main()
76{
77	struct open_file	f;
78	void	*entry;
79	char	*addr;
80	int n, error;
81
82#ifdef DEBUG
83	printf("bootxx: open...\n");
84#endif
85	f.f_flags = F_RAW;
86	if (devopen(&f, 0, &addr)) {
87		printf("bootxx: devopen failed\n");
88		return;
89	}
90
91	addr = (char*)LOADADDR;
92	error = copyboot(&f, addr);
93	f.f_dev->dv_close(&f);
94	if (!error) {
95#ifdef DEBUG
96		printf("bootxx: start 0x%x\n", (long)addr);
97#endif
98		entry = addr;
99		chain_to(entry);
100	}
101	/* copyboot had a problem... */
102	return;
103}
104
105int
106copyboot(fp, addr)
107	struct open_file	*fp;
108	char			*addr;
109{
110	int	n, i, blknum;
111	char *buf;
112
113	/* Need to use a buffer that can be mapped into DVMA space. */
114	buf = alloc(block_size);
115	if (!buf)
116		panic("bootxx: alloc failed");
117
118	for (i = 0; i < block_count; i++) {
119
120		if ((blknum = block_table[i]) == 0)
121			break;
122
123#ifdef DEBUG
124		printf("bootxx: block # %d = %d\n", i, blknum);
125#endif
126		if ((fp->f_dev->dv_strategy)(fp->f_devdata, F_READ,
127					   blknum, block_size, buf, &n))
128		{
129			printf("bootxx: read failed\n");
130			return -1;
131		}
132		if (n != block_size) {
133			printf("bootxx: short read\n");
134			return -1;
135		}
136		bcopy(buf, addr, block_size);
137		addr += block_size;
138	}
139
140	return 0;
141}
142
143