bootxx.c revision 1.3
1/*	$NetBSD: bootxx.c,v 1.3 1998/09/05 15:12:26 pk 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#include <sys/param.h>
40#include <sys/time.h>
41#include <a.out.h>
42
43#include <lib/libsa/stand.h>
44
45#include <sparc/stand/common/promdev.h>
46
47int debug;
48int netif_debug;
49
50/*
51 * Boot device is derived from ROM provided information.
52 */
53const char		progname[] = "bootxx";
54struct open_file	io;
55
56/*
57 * The contents of the block_* variables below is set by installboot(8)
58 * to hold the the filesystem data of the second-stage boot program
59 * (typically `/boot'): filesystem block size, # of filesystem blocks and
60 * the block numbers themselves.
61 */
62#define MAXBLOCKNUM	256	/* enough for a 2MB boot program (bs 8K) */
63int32_t			block_size = 0;
64int32_t			block_count = MAXBLOCKNUM;
65daddr_t			block_table[MAXBLOCKNUM] = { 0 };
66
67
68void	loadboot __P((struct open_file *, caddr_t));
69
70int
71main()
72{
73	char	*dummy;
74	size_t	n;
75	register void (*entry)__P((caddr_t)) = (void (*)__P((caddr_t)))LOADADDR;
76
77	prom_init();
78	io.f_flags = F_RAW;
79	if (devopen(&io, 0, &dummy)) {
80		panic("%s: can't open device", progname);
81	}
82
83	(void)loadboot(&io, LOADADDR);
84	(io.f_dev->dv_close)(&io);
85	(*entry)(cputyp == CPU_SUN4 ? LOADADDR : (caddr_t)promvec);
86	_rtt();
87}
88
89void
90loadboot(f, addr)
91	register struct open_file	*f;
92	register char			*addr;
93{
94	register int	i;
95	register char	*buf;
96	size_t		n;
97	daddr_t		blk;
98
99	/*
100	 * Allocate a buffer that we can map into DVMA space; only
101	 * needed for sun4 architecture, but use it for all machines
102	 * to keep code size down as much as possible.
103	 */
104	buf = alloc(block_size);
105	if (buf == NULL)
106		panic("%s: alloc failed", progname);
107
108	for (i = 0; i < block_count; i++) {
109		if ((blk = block_table[i]) == 0)
110			panic("%s: block table corrupt", progname);
111
112#ifdef DEBUG
113		printf("%s: block # %d = %d\n", progname, i, blk);
114#endif
115		if ((f->f_dev->dv_strategy)(f->f_devdata, F_READ,
116					    blk, block_size, buf, &n)) {
117			panic("%s: read failure", progname);
118		}
119		bcopy(buf, addr, block_size);
120		if (n != block_size)
121			panic("%s: short read", progname);
122		if (i == 0) {
123			register int m = N_GETMAGIC(*(struct exec *)addr);
124			if (m == ZMAGIC || m == NMAGIC || m == OMAGIC) {
125				/* Move exec header out of the way */
126				bcopy(addr, addr - sizeof(struct exec), n);
127				addr -= sizeof(struct exec);
128			}
129		}
130		addr += n;
131	}
132
133}
134