1/*	$NetBSD: bootxx.c,v 1.11 2008/04/28 20:23:38 martin 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/bootblock.h>
44#include <machine/mon.h>
45
46#include <stand.h>
47#include "libsa.h"
48
49int copyboot(struct open_file *, char *);
50
51/*
52 * This is the address where we load the second-stage boot loader.
53 */
54#define LOADADDR	0x4000
55
56/*
57 * The contents of the sun68k_bbinfo below are set by installboot(8)
58 * to hold the filesystem data of the second-stage boot program
59 * (typically `/ufsboot'): filesystem block size, # of filesystem
60 * blocks and the block numbers themselves.
61 */
62struct shared_bbinfo bbinfo = {
63	{ SUN68K_BBINFO_MAGIC },
64	0,
65	SHARED_BBINFO_MAXBLOCKS,
66	{ 0 }
67};
68
69int
70main(void)
71{
72	struct open_file	f;
73	void	*entry;
74	char	*addr;
75	int error;
76
77#ifdef DEBUG
78	printf("bootxx: open...\n");
79#endif
80	f.f_flags = F_RAW;
81	if (devopen(&f, 0, &addr)) {
82		putstr("bootxx: devopen failed\n");
83		return 1;
84	}
85
86	addr = (char*)LOADADDR;
87	error = copyboot(&f, addr);
88	f.f_dev->dv_close(&f);
89	if (!error) {
90#ifdef DEBUG
91		printf("bootxx: start 0x%x\n", (long)addr);
92#endif
93		entry = addr;
94		chain_to(entry);
95	}
96	/* copyboot had a problem... */
97	return 0;
98}
99
100int
101copyboot(struct open_file *fp, char *addr)
102{
103	size_t n;
104	int i, blknum;
105	char *buf;
106
107	/* Need to use a buffer that can be mapped into DVMA space. */
108	buf = alloc(bbinfo.bbi_block_size);
109	if (!buf)
110		panic("bootxx: alloc failed");
111
112	for (i = 0; i < bbinfo.bbi_block_count; i++) {
113
114		if ((blknum = bbinfo.bbi_block_table[i]) == 0)
115			break;
116
117#ifdef DEBUG
118		printf("bootxx: block # %d = %d\n", i, blknum);
119#endif
120		if ((fp->f_dev->dv_strategy)(fp->f_devdata, F_READ, blknum,
121					   bbinfo.bbi_block_size, buf, &n))
122		{
123			putstr("bootxx: read failed\n");
124			return -1;
125		}
126		if (n != bbinfo.bbi_block_size) {
127			putstr("bootxx: short read\n");
128			return -1;
129		}
130		memcpy(addr, buf, bbinfo.bbi_block_size);
131		addr += bbinfo.bbi_block_size;
132	}
133
134	return 0;
135}
136