summaryrefslogtreecommitdiff
path: root/sys/arch/sparc/stand/boot/boot.c
blob: 9040bdf0557418df237fa36c6eaae8addc09d50c (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*	$NetBSD: boot.c,v 1.30 2016/06/11 06:43:16 dholland Exp $ */

/*-
 * Copyright (c) 1982, 1986, 1990, 1993
 *	The Regents of the University of California.  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. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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.
 *
 * 	@(#)boot.c	8.1 (Berkeley) 6/10/93
 */

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

#include <lib/libsa/stand.h>
#include <lib/libsa/loadfile.h>
#include <lib/libkern/libkern.h>

#include <machine/promlib.h>
#include <sparc/stand/common/promdev.h>
#include <sparc/stand/common/isfloppy.h>

#include "bootinfo.h"

extern void	prom_patch(void);	/* prompatch.c */

static int	bootoptions(const char *);

int	boothowto;
int	debug;
int	netif_debug;

char	fbuf[80], dbuf[128];
paddr_t bstart, bend;	/* physical start & end address of the boot program */

int	compatmode = 0;		/* For loading older kernels */
u_long	loadaddrmask = -1UL;

extern char bootprog_name[], bootprog_rev[];

int	main(void);
typedef void (*entry_t)(void *, int, int, int, long, long);

/*
 * Boot device is derived from ROM provided information, or if there is none,
 * this list is used in sequence, to find a kernel.
 */
char *kernels[] = {
	"netbsd",
	"netbsd.gz",
	"netbsd.old",
	"netbsd.old.gz",
	"onetbsd",
	"onetbsd.gz",
	"vmunix",
#ifdef notyet
	"netbsd.pl",
	"netbsd.pl.gz",
	"netbsd.el",
	"netbsd.el.gz",
#endif
	NULL
};

int
bootoptions(const char *ap)
{
	int v = 0;
	if (ap == NULL || *ap++ != '-')
		return (0);

	while (*ap != '\0' && *ap != ' ' && *ap != '\t' && *ap != '\n') {
		BOOT_FLAG(*ap, v);
		if (*ap == 'C')
			compatmode = 1;
		ap++;
	}

	if ((v & RB_KDB) != 0)
		debug = 1;

	return (v);
}

static paddr_t
getphysmem(u_long size)
{
	struct	memarr *pmemarr;	/* physical memory regions */
	int	npmemarr;		/* number of entries in pmemarr */
	struct memarr *mp;
	int i;
	extern char start[];	/* top of stack (see srt0.S) */

	/*
	 * Find the physical memory area that's in use by the boot loader.
	 * Our stack grows down from label `start'; assume we need no more
	 * than 16K of stack space.
	 * The top of the boot loader is the next 4MB boundary.
	 */
	if (pmap_extract((vaddr_t)start - (16*1024), &bstart) != 0)
		return ((paddr_t)-1);

	bend = roundup(bstart, 0x400000);

	/*
	 * Get available physical memory from the prom.
	 */
	npmemarr = prom_makememarr(NULL, 0, MEMARR_AVAILPHYS);
	pmemarr = alloc(npmemarr*sizeof(struct memarr));
	if (pmemarr == NULL)
		return ((paddr_t)-1);
	npmemarr = prom_makememarr(pmemarr, npmemarr, MEMARR_AVAILPHYS);

	/*
	 * Find a suitable loading address.
	 */
	for (mp = pmemarr, i = npmemarr; --i >= 0; mp++) {
		paddr_t pa = (paddr_t)pmemarr[i].addr;
		u_long len = (u_long)pmemarr[i].len;

		/* Check whether it will fit in front of us */
		if (pa < bstart && len >= size && (bstart - pa) >= size)
			return (pa);

		/* Skip the boot program memory */
		if (pa < bend) {
			if (len < bend - pa)
				/* Not large enough */
				continue;

			/* Shrink this segment */
			len -=  bend - pa;
			pa = bend;
		}

		/* Does it fit in the remainder of this segment? */
		if (len >= size)
			return (pa);
	}
	return ((paddr_t)-1);
}

static int
loadk(char *kernel, u_long *marks)
{
	int fd, error;
	vaddr_t va;
	paddr_t pa;
	u_long size;
	int flags = LOAD_KERNEL;

	if ((fd = open(kernel, 0)) < 0)
		return (errno ? errno : ENOENT);

	marks[MARK_START] = 0;
	if ((error = fdloadfile(fd, marks, COUNT_KERNEL)) != 0)
		goto out;

	size = marks[MARK_END] - marks[MARK_START];

	/* We want that leading 16K in front of the kernel image */
	size += PROM_LOADADDR;
	va = marks[MARK_START] - PROM_LOADADDR;

	/*
	 * Extra space for bootinfo and kernel bootstrap.
	 * In compat mode, we get to re-use the space occupied by the
	 * boot program. Traditionally, we've silently assumed that
	 * is enough for the kernel to work with.
	 */
	size += BOOTINFO_SIZE;
	if (!compatmode)
		size += 512 * 1024;

	/* Get a physical load address */
	pa = getphysmem(size);
	if (pa == (paddr_t)-1) {
		error = EFBIG;
		goto out;
	}

	if (boothowto & AB_VERBOSE)
		printf("Loading at physical address %lx\n", pa);
	if (pmap_map(va, pa, size) != 0) {
		error = EFAULT;
		goto out;
	}

	/* XXX - to do: inspect kernel image and set compat mode */
	if (compatmode) {
		/* Double-map at VA 0 for compatibility */
		if (pa + size >= bstart) {
			printf("%s: too large for compat mode\n", kernel);
			error = EFBIG;
			goto out;
		}

		if (pa != 0 && pmap_map(0, pa, size) != 0) {
			error = EFAULT;
			goto out;
		}
		loadaddrmask = 0x07ffffffUL;
	}

	if (bootdev_isfloppy(prom_bootdevice))
		flags &= ~LOAD_BACKWARDS;

	marks[MARK_START] = 0;
	error = fdloadfile(fd, marks, flags);
out:
	close(fd);
	return (error);
}

int
main(void)
{
	int	error, i;
	char	kernel[MAX_PROM_PATH];
	const char *k;
	u_long	marks[MARK_MAX], bootinfo;
	struct btinfo_symtab bi_sym;
	struct btinfo_boothowto bi_howto;
	void	*arg;

#ifdef HEAP_VARIABLE
	{
		extern char end[];
		setheap((void *)ALIGN(end), (void *)0xffffffff);
	}
#endif
	prom_init();
	mmu_init();

	printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);

	/* massage machine prom */
	prom_patch();

	/*
	 * get default kernel.
	 */
	k = prom_getbootfile();
	if (k != NULL && *k != '\0') {
		i = -1;	/* not using the kernels */
		strcpy(kernel, k);
	} else {
		i = 0;
		strcpy(kernel, kernels[i]);
	}

	k = prom_getbootpath();
	if (k && *k)
		strcpy(prom_bootdevice, k);
	boothowto = bootoptions(prom_getbootargs());

	for (;;) {
		/*
		 * ask for a kernel first ..
		 */
		if (boothowto & RB_ASKNAME) {
			printf("device[%s] (\"halt\" to halt): ",
					prom_bootdevice);
			kgets(dbuf, sizeof(dbuf));
			if (strcmp(dbuf, "halt") == 0)
				_rtt();
			if (dbuf[0])
				strcpy(prom_bootdevice, dbuf);
			printf("boot (press RETURN to try default list): ");
			kgets(fbuf, sizeof(fbuf));
			if (fbuf[0])
				strcpy(kernel, fbuf);
			else {
				boothowto &= ~RB_ASKNAME;
				i = 0;
				strcpy(kernel, kernels[i]);
			}
		}

		printf("Booting %s\n", kernel);
		if ((error = loadk(kernel, marks)) == 0)
			break;

		if (error != ENOENT) {
			printf("Cannot load %s: error=%d\n", kernel, error);
			boothowto |= RB_ASKNAME;
		}

		/*
		 * if we have are not in askname mode, and we aren't using the
		 * prom bootfile, try the next one (if it exits).  otherwise,
		 * go into askname mode.
		 */
		if ((boothowto & RB_ASKNAME) == 0 &&
		    i != -1 && kernels[++i]) {
			strcpy(kernel, kernels[i]);
			printf(": trying %s...\n", kernel);
		} else {
			printf("\n");
			boothowto |= RB_ASKNAME;
		}
	}

	marks[MARK_END] = (((u_long)marks[MARK_END] + sizeof(u_long) - 1)) &
	    (-sizeof(u_long));
	arg = (prom_version() == PROM_OLDMON) ? (void *)PROM_LOADADDR : romp;

	/* Setup boot info structure at the end of the kernel image */
	bootinfo = bi_init(marks[MARK_END] & loadaddrmask);

	/* Add kernel symbols to bootinfo */
	bi_sym.nsym = marks[MARK_NSYM] & loadaddrmask;
	bi_sym.ssym = marks[MARK_SYM] & loadaddrmask;
	bi_sym.esym = marks[MARK_END] & loadaddrmask;
	bi_add(&bi_sym, BTINFO_SYMTAB, sizeof(bi_sym));

	/* Add boothowto */
	bi_howto.boothowto = boothowto;
	bi_add(&bi_howto, BTINFO_BOOTHOWTO, sizeof(bi_howto));

	/* Add kernel path to bootinfo */
	i = sizeof(struct btinfo_common) + strlen(kernel) + 1;
	/* Impose limit (somewhat arbitrary) */
	if (i < BOOTINFO_SIZE / 2) {
		union {
			struct btinfo_kernelfile bi_file;
			char x[BOOTINFO_SIZE / 2];
		} U;
		strcpy(U.bi_file.name, kernel);
		bi_add(&U.bi_file, BTINFO_KERNELFILE, i);
	}

	(*(entry_t)marks[MARK_ENTRY])(arg, 0, 0, 0, bootinfo, DDB_MAGIC2);
	_rtt();
}