bootxx.c revision 1.12
11.12Smartin/*	$NetBSD: bootxx.c,v 1.12 2005/04/28 12:47:45 martin 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.7Spk#include <sys/exec.h>
421.10Slukem#include <sys/bootblock.h>
431.1Smrg
441.1Smrg#include <lib/libsa/stand.h>
451.12Smartin#include <string.h>
461.1Smrg
471.4Spk#include <machine/promlib.h>
481.1Smrg#include <sparc/stand/common/promdev.h>
491.1Smrg
501.1Smrgint debug;
511.1Smrgint netif_debug;
521.1Smrg
531.1Smrg/*
541.1Smrg * Boot device is derived from ROM provided information.
551.1Smrg */
561.1Smrgconst char		progname[] = "bootxx";
571.1Smrgstruct open_file	io;
581.1Smrg
591.1Smrg/*
601.9Sthorpej * The contents of the bbinfo below are set by installboot(8)
611.8Ssoren * to hold the filesystem data of the second-stage boot program
621.8Ssoren * (typically `/boot'): filesystem block size, # of filesystem
631.8Ssoren * blocks and the block numbers themselves.
641.1Smrg */
651.11Slukemstruct shared_bbinfo bbinfo = {
661.10Slukem	{ SPARC_BBINFO_MAGIC },
671.9Sthorpej	0,
681.11Slukem	SHARED_BBINFO_MAXBLOCKS,
691.9Sthorpej	{ 0 }
701.9Sthorpej};
711.1Smrg
721.4Spkint	main __P((void));
731.1Smrgvoid	loadboot __P((struct open_file *, caddr_t));
741.1Smrg
751.1Smrgint
761.1Smrgmain()
771.1Smrg{
781.1Smrg	char	*dummy;
791.4Spk	void (*entry)__P((void *)) = (void (*)__P((void *)))PROM_LOADADDR;
801.4Spk	void	*arg;
811.1Smrg
821.6Spk#ifdef HEAP_VARIABLE
831.6Spk	{
841.6Spk		extern char end[];
851.6Spk		setheap((void *)ALIGN(end), (void *)0xffffffff);
861.6Spk	}
871.6Spk#endif
881.1Smrg	prom_init();
891.12Smartin	dummy = prom_getbootpath();
901.12Smartin	if (dummy && *dummy != '\0')
911.12Smartin		strcpy(prom_bootdevice, dummy);
921.1Smrg	io.f_flags = F_RAW;
931.1Smrg	if (devopen(&io, 0, &dummy)) {
941.4Spk		panic("%s: can't open device `%s'", progname,
951.4Spk			prom_bootdevice != NULL ? prom_bootdevice : "unknown");
961.1Smrg	}
971.1Smrg
981.5Schristos	(void)loadboot(&io, (caddr_t)PROM_LOADADDR);
991.2Spk	(io.f_dev->dv_close)(&io);
1001.4Spk
1011.5Schristos	arg = (prom_version() == PROM_OLDMON) ? (caddr_t)PROM_LOADADDR : romp;
1021.4Spk	(*entry)(arg);
1031.1Smrg	_rtt();
1041.1Smrg}
1051.1Smrg
1061.1Smrgvoid
1071.1Smrgloadboot(f, addr)
1081.4Spk	struct open_file	*f;
1091.4Spk	char			*addr;
1101.1Smrg{
1111.4Spk	int	i;
1121.4Spk	char	*buf;
1131.1Smrg	size_t		n;
1141.1Smrg	daddr_t		blk;
1151.1Smrg
1161.1Smrg	/*
1171.1Smrg	 * Allocate a buffer that we can map into DVMA space; only
1181.1Smrg	 * needed for sun4 architecture, but use it for all machines
1191.1Smrg	 * to keep code size down as much as possible.
1201.1Smrg	 */
1211.9Sthorpej	buf = alloc(bbinfo.bbi_block_size);
1221.1Smrg	if (buf == NULL)
1231.1Smrg		panic("%s: alloc failed", progname);
1241.1Smrg
1251.9Sthorpej	for (i = 0; i < bbinfo.bbi_block_count; i++) {
1261.9Sthorpej		if ((blk = bbinfo.bbi_block_table[i]) == 0)
1271.1Smrg			panic("%s: block table corrupt", progname);
1281.1Smrg
1291.1Smrg#ifdef DEBUG
1301.1Smrg		printf("%s: block # %d = %d\n", progname, i, blk);
1311.1Smrg#endif
1321.9Sthorpej		if ((f->f_dev->dv_strategy)(f->f_devdata, F_READ, blk,
1331.9Sthorpej		    bbinfo.bbi_block_size, buf, &n)) {
1341.4Spk			printf("%s: read failure", progname);
1351.4Spk			_rtt();
1361.1Smrg		}
1371.9Sthorpej		bcopy(buf, addr, bbinfo.bbi_block_size);
1381.9Sthorpej		if (n != bbinfo.bbi_block_size)
1391.1Smrg			panic("%s: short read", progname);
1401.1Smrg		if (i == 0) {
1411.4Spk			int m = N_GETMAGIC(*(struct exec *)addr);
1421.1Smrg			if (m == ZMAGIC || m == NMAGIC || m == OMAGIC) {
1431.1Smrg				/* Move exec header out of the way */
1441.1Smrg				bcopy(addr, addr - sizeof(struct exec), n);
1451.1Smrg				addr -= sizeof(struct exec);
1461.1Smrg			}
1471.1Smrg		}
1481.1Smrg		addr += n;
1491.1Smrg	}
1501.1Smrg
1511.4Spk}
1521.4Spk
1531.4Spk/*
1541.4Spk * We don't need the overlap handling feature that the libkern version
1551.4Spk * of bcopy() provides. We DO need code compactness..
1561.4Spk */
1571.4Spkvoid
1581.4Spkbcopy(src, dst, n)
1591.4Spk	const void *src;
1601.4Spk	void *dst;
1611.4Spk	size_t n;
1621.4Spk{
1631.4Spk	const char *p = src;
1641.4Spk	char *q = dst;
1651.4Spk
1661.4Spk	while (n-- > 0)
1671.4Spk		*q++ = *p++;
1681.1Smrg}
169