summaryrefslogtreecommitdiff
path: root/sys/arch/sun3/stand/tapeboot/boot.c
blob: c6a24287299688ac9c87a57d43c07c499b445e5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*	$NetBSD: boot.c,v 1.1.1.1 1995/10/13 21:27:30 gwr Exp $	*/

/*
 * Copyright (c) 1994 Paul Kranenburg
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by Paul Kranenburg.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * This is a generic "first-stage" boot program.
 *
 * Note that this program has absolutely no filesystem knowledge!
 *
 * Instead, this uses a table of disk block numbers that are
 * filled in by the installboot program such that this program
 * can load the "second-stage" boot program.
 */

#include <sys/param.h>
#include <sys/reboot.h>
#include <sys/exec.h>

#include <machine/mon.h>

#include "stand.h"
#include "promboot.h"

/*
 * Boot device is derived from ROM provided information.
 */
#define LOADADDR	0x4000

extern int debug;
extern char		*version;

char	line[80];

int block_size = 512;


main()
{
	struct open_file	f;
	struct bootparam *bp;
	void	(*entry)();
	char	*addr;
	int n, error;

	printf(">> NetBSD tapeboot [%s]\n", version);
	prom_get_boot_info();

	/*
	 * Set the tape file number to the next one, because
	 * the boot program is first, then the kernel.
	 */
	bp = *romp->bootParam;
	bp->partNum += 1;

	for (;;) {
		if (prom_boothow & RB_ASKNAME) {
			printf("tapeboot: segment? [%d] ", bp->partNum);
			gets(line);
			if (('0' <= line[0]) && (line[0] <= '9')) {
				bp->partNum = line[0] - '0';
			}
		}
		
		printf("tapeboot: opening segment %d\n", bp->partNum);
		f.f_flags = F_RAW;
		if ((error = devopen(&f, 0, &addr)) != 0) {
			printf("tapeboot: open failed, error=%d\n", error);
			goto ask;
		}

		addr = (char*)LOADADDR;
		error = loadfile(&f, addr);

		printf("tapeboot: close (rewind)...\n");
		f.f_dev->dv_close(&f);
		if (error == 0)
			break;

		printf("tapeboot: load failed, error=%d\n", error);
	ask:
		prom_boothow |= RB_ASKNAME;
	}

	if (debug) {
		printf("Debug mode - enter c to continue...");
		/* This will print "\nAbort at ...\n" */
		asm("	trap #0");
	}

	printf("Starting program at 0x%x\n", (long)addr);
	entry = (void (*)())addr;
	(*entry)();
}

int
loadfile(fp, addr)
	struct open_file	*fp;
	char			*addr;
{
	char *buf;
	int	n, blknum;
	int error = 0;
	/*
	 * Loading a kernel.  It WILL have an a.out header.
	 * XXX - This assumes OMAGIC format!
	 */
	addr -= sizeof(struct exec); /* XXX */

	/* Need to use a buffer that can be mapped into DVMA space. */
	buf = alloc(block_size);
	if (!buf)
		panic("tapeboot: alloc failed");

	printf("tapeboot: loading ... ");
	/* limit program size to < 2MB */
	for (blknum = 0; blknum < 4096; blknum++) {

		error = (fp->f_dev->dv_strategy)(fp->f_devdata, F_READ,
		                blknum, block_size, buf, &n);
		if (error) {
			printf("(error=%d)\n", error);
			return EIO;
		}
		if (n == 0)
			break;	/* end of tape */
		if (n != block_size) {
			printf(" (short read)\n");
			return EIO;
		}

		bcopy(buf, addr, block_size);
		addr += block_size;
	}

	printf("(%d blocks)\n", blknum);
	return 0;
}