1/*	$NetBSD: bootxx.c,v 1.25 2009/10/26 19:16:57 cegger 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#include <sys/param.h>
33#include <sys/exec.h>
34#include <sys/exec_aout.h>
35#include <sys/bootblock.h>
36
37#include <lib/libkern/libkern.h>
38#include <lib/libsa/stand.h>
39
40#include <machine/promlib.h>
41#include <sparc/stand/common/promdev.h>
42
43int debug;
44int netif_debug;
45
46/*
47 * Boot device is derived from ROM provided information.
48 */
49const char		progname[] = "bootxx";
50struct open_file	io;
51
52/*
53 * The contents of the bbinfo below are set by installboot(8)
54 * to hold the filesystem data of the second-stage boot program
55 * (typically `/boot'): filesystem block size, # of filesystem
56 * blocks and the block numbers themselves.
57 */
58struct shared_bbinfo bbinfo = {
59	{ SPARC_BBINFO_MAGIC },
60	0,
61	SHARED_BBINFO_MAXBLOCKS,
62	{ 0 }
63};
64
65int	main(void);
66void	loadboot(struct open_file *, char *);
67
68int
69main(void)
70{
71	char	*dummy1;
72	const char	*dummy;
73	void (*entry)(void *) = (void (*)(void *))PROM_LOADADDR;
74	void	*arg;
75
76#ifdef HEAP_VARIABLE
77	{
78		extern char end[];
79		setheap((void *)ALIGN(end), (void *)0xffffffff);
80	}
81#endif
82	prom_init();
83	dummy = prom_getbootpath();
84	if (dummy && *dummy != '\0')
85		strcpy(prom_bootdevice, dummy);
86	io.f_flags = F_RAW;
87	if (devopen(&io, 0, &dummy1)) {
88		panic("%s: can't open device `%s'", progname,
89			prom_bootdevice[0] ? prom_bootdevice : "unknown");
90	}
91
92	(void)loadboot(&io, (void *)PROM_LOADADDR);
93	(io.f_dev->dv_close)(&io);
94
95	arg = (prom_version() == PROM_OLDMON) ? (void *)PROM_LOADADDR : romp;
96	(*entry)(arg);
97	_rtt();
98}
99
100void
101loadboot(struct open_file *f, char *addr)
102{
103	int	i;
104	char	*buf;
105	size_t		n;
106	daddr_t		blk;
107
108	/*
109	 * Allocate a buffer that we can map into DVMA space; only
110	 * needed for sun4 architecture, but use it for all machines
111	 * to keep code size down as much as possible.
112	 */
113	buf = alloc(bbinfo.bbi_block_size);
114	if (buf == NULL)
115		panic("%s: alloc failed", progname);
116
117	for (i = 0; i < bbinfo.bbi_block_count; i++) {
118		if ((blk = bbinfo.bbi_block_table[i]) == 0)
119			panic("%s: block table corrupt", progname);
120
121#ifdef DEBUG
122		printf("%s: block # %d = %d\n", progname, i, blk);
123#endif
124		if ((f->f_dev->dv_strategy)(f->f_devdata, F_READ, blk,
125		    bbinfo.bbi_block_size, buf, &n)) {
126			printf("%s: read failure", progname);
127			_rtt();
128		}
129		memcpy(addr, buf, bbinfo.bbi_block_size);
130		if (n != bbinfo.bbi_block_size)
131			panic("%s: short read", progname);
132		if (i == 0) {
133			int m = N_GETMAGIC(*(struct exec *)addr);
134			if (m == ZMAGIC || m == NMAGIC || m == OMAGIC) {
135				/* Move exec header out of the way */
136				memcpy(addr - sizeof(struct exec), addr, n);
137				addr -= sizeof(struct exec);
138			}
139		}
140		addr += n;
141	}
142
143}
144