bootxx.c revision 1.6
11.6Spk/*	$NetBSD: bootxx.c,v 1.6 1999/06/12 12:49:25 pk Exp $ */
21.1Smrg
31.3Spk/*-
41.3Spk * Copyright (c) 1998 The NetBSD Foundation, Inc.
51.1Smrg * All rights reserved.
61.1Smrg *
71.3Spk * This code is derived from software contributed to The NetBSD Foundation
81.3Spk * by Paul Kranenburg.
91.3Spk *
101.1Smrg * Redistribution and use in source and binary forms, with or without
111.1Smrg * modification, are permitted provided that the following conditions
121.1Smrg * are met:
131.1Smrg * 1. Redistributions of source code must retain the above copyright
141.1Smrg *    notice, this list of conditions and the following disclaimer.
151.1Smrg * 2. Redistributions in binary form must reproduce the above copyright
161.1Smrg *    notice, this list of conditions and the following disclaimer in the
171.1Smrg *    documentation and/or other materials provided with the distribution.
181.1Smrg * 3. All advertising materials mentioning features or use of this software
191.1Smrg *    must display the following acknowledgement:
201.3Spk *        This product includes software developed by the NetBSD
211.3Spk *        Foundation, Inc. and its contributors.
221.3Spk * 4. Neither the name of The NetBSD Foundation nor the names of its
231.3Spk *    contributors may be used to endorse or promote products derived
241.3Spk *    from this software without specific prior written permission.
251.1Smrg *
261.3Spk * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
271.3Spk * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
281.3Spk * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
291.3Spk * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
301.3Spk * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
311.3Spk * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
321.3Spk * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
331.3Spk * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
341.3Spk * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
351.3Spk * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
361.3Spk * POSSIBILITY OF SUCH DAMAGE.
371.1Smrg */
381.1Smrg
391.1Smrg#include <sys/param.h>
401.1Smrg#include <sys/time.h>
411.1Smrg#include <a.out.h>
421.1Smrg
431.1Smrg#include <lib/libsa/stand.h>
441.1Smrg
451.4Spk#include <machine/promlib.h>
461.1Smrg#include <sparc/stand/common/promdev.h>
471.1Smrg
481.1Smrgint debug;
491.1Smrgint netif_debug;
501.1Smrg
511.1Smrg/*
521.1Smrg * Boot device is derived from ROM provided information.
531.1Smrg */
541.1Smrgconst char		progname[] = "bootxx";
551.1Smrgstruct open_file	io;
561.1Smrg
571.1Smrg/*
581.1Smrg * The contents of the block_* variables below is set by installboot(8)
591.1Smrg * to hold the the filesystem data of the second-stage boot program
601.1Smrg * (typically `/boot'): filesystem block size, # of filesystem blocks and
611.1Smrg * the block numbers themselves.
621.1Smrg */
631.1Smrg#define MAXBLOCKNUM	256	/* enough for a 2MB boot program (bs 8K) */
641.1Smrgint32_t			block_size = 0;
651.1Smrgint32_t			block_count = MAXBLOCKNUM;
661.1Smrgdaddr_t			block_table[MAXBLOCKNUM] = { 0 };
671.1Smrg
681.1Smrg
691.4Spkint	main __P((void));
701.1Smrgvoid	loadboot __P((struct open_file *, caddr_t));
711.1Smrg
721.1Smrgint
731.1Smrgmain()
741.1Smrg{
751.1Smrg	char	*dummy;
761.4Spk	void (*entry)__P((void *)) = (void (*)__P((void *)))PROM_LOADADDR;
771.4Spk	void	*arg;
781.1Smrg
791.6Spk#ifdef HEAP_VARIABLE
801.6Spk	{
811.6Spk		extern char end[];
821.6Spk		setheap((void *)ALIGN(end), (void *)0xffffffff);
831.6Spk	}
841.6Spk#endif
851.1Smrg	prom_init();
861.4Spk	prom_bootdevice = prom_getbootpath();
871.1Smrg	io.f_flags = F_RAW;
881.1Smrg	if (devopen(&io, 0, &dummy)) {
891.4Spk		panic("%s: can't open device `%s'", progname,
901.4Spk			prom_bootdevice != NULL ? prom_bootdevice : "unknown");
911.1Smrg	}
921.1Smrg
931.5Schristos	(void)loadboot(&io, (caddr_t)PROM_LOADADDR);
941.2Spk	(io.f_dev->dv_close)(&io);
951.4Spk
961.5Schristos	arg = (prom_version() == PROM_OLDMON) ? (caddr_t)PROM_LOADADDR : romp;
971.4Spk	(*entry)(arg);
981.1Smrg	_rtt();
991.1Smrg}
1001.1Smrg
1011.1Smrgvoid
1021.1Smrgloadboot(f, addr)
1031.4Spk	struct open_file	*f;
1041.4Spk	char			*addr;
1051.1Smrg{
1061.4Spk	int	i;
1071.4Spk	char	*buf;
1081.1Smrg	size_t		n;
1091.1Smrg	daddr_t		blk;
1101.1Smrg
1111.1Smrg	/*
1121.1Smrg	 * Allocate a buffer that we can map into DVMA space; only
1131.1Smrg	 * needed for sun4 architecture, but use it for all machines
1141.1Smrg	 * to keep code size down as much as possible.
1151.1Smrg	 */
1161.1Smrg	buf = alloc(block_size);
1171.1Smrg	if (buf == NULL)
1181.1Smrg		panic("%s: alloc failed", progname);
1191.1Smrg
1201.1Smrg	for (i = 0; i < block_count; i++) {
1211.1Smrg		if ((blk = block_table[i]) == 0)
1221.1Smrg			panic("%s: block table corrupt", progname);
1231.1Smrg
1241.1Smrg#ifdef DEBUG
1251.1Smrg		printf("%s: block # %d = %d\n", progname, i, blk);
1261.1Smrg#endif
1271.1Smrg		if ((f->f_dev->dv_strategy)(f->f_devdata, F_READ,
1281.1Smrg					    blk, block_size, buf, &n)) {
1291.4Spk			printf("%s: read failure", progname);
1301.4Spk			_rtt();
1311.1Smrg		}
1321.1Smrg		bcopy(buf, addr, block_size);
1331.1Smrg		if (n != block_size)
1341.1Smrg			panic("%s: short read", progname);
1351.1Smrg		if (i == 0) {
1361.4Spk			int m = N_GETMAGIC(*(struct exec *)addr);
1371.1Smrg			if (m == ZMAGIC || m == NMAGIC || m == OMAGIC) {
1381.1Smrg				/* Move exec header out of the way */
1391.1Smrg				bcopy(addr, addr - sizeof(struct exec), n);
1401.1Smrg				addr -= sizeof(struct exec);
1411.1Smrg			}
1421.1Smrg		}
1431.1Smrg		addr += n;
1441.1Smrg	}
1451.1Smrg
1461.4Spk}
1471.4Spk
1481.4Spk/*
1491.4Spk * We don't need the overlap handling feature that the libkern version
1501.4Spk * of bcopy() provides. We DO need code compactness..
1511.4Spk */
1521.4Spkvoid
1531.4Spkbcopy(src, dst, n)
1541.4Spk	const void *src;
1551.4Spk	void *dst;
1561.4Spk	size_t n;
1571.4Spk{
1581.4Spk	const char *p = src;
1591.4Spk	char *q = dst;
1601.4Spk
1611.4Spk	while (n-- > 0)
1621.4Spk		*q++ = *p++;
1631.1Smrg}
164