bootxx.c revision 1.21
11.21Scegger/*	$NetBSD: bootxx.c,v 1.21 2009/03/18 17:06:46 cegger 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 *
191.3Spk * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.3Spk * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.3Spk * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.3Spk * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.3Spk * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.3Spk * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.3Spk * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.3Spk * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.3Spk * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.3Spk * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.3Spk * POSSIBILITY OF SUCH DAMAGE.
301.1Smrg */
311.1Smrg
321.1Smrg#include <sys/param.h>
331.7Spk#include <sys/exec.h>
341.10Slukem#include <sys/bootblock.h>
351.1Smrg
361.15Suwe#include <lib/libkern/libkern.h>
371.1Smrg#include <lib/libsa/stand.h>
381.1Smrg
391.4Spk#include <machine/promlib.h>
401.1Smrg#include <sparc/stand/common/promdev.h>
411.1Smrg
421.1Smrgint debug;
431.1Smrgint netif_debug;
441.1Smrg
451.1Smrg/*
461.1Smrg * Boot device is derived from ROM provided information.
471.1Smrg */
481.1Smrgconst char		progname[] = "bootxx";
491.1Smrgstruct open_file	io;
501.1Smrg
511.1Smrg/*
521.9Sthorpej * The contents of the bbinfo below are set by installboot(8)
531.8Ssoren * to hold the filesystem data of the second-stage boot program
541.8Ssoren * (typically `/boot'): filesystem block size, # of filesystem
551.8Ssoren * blocks and the block numbers themselves.
561.1Smrg */
571.11Slukemstruct shared_bbinfo bbinfo = {
581.10Slukem	{ SPARC_BBINFO_MAGIC },
591.9Sthorpej	0,
601.11Slukem	SHARED_BBINFO_MAXBLOCKS,
611.9Sthorpej	{ 0 }
621.9Sthorpej};
631.1Smrg
641.17Suweint	main(void);
651.19Shevoid	loadboot(struct open_file *, char *);
661.1Smrg
671.1Smrgint
681.17Suwemain(void)
691.1Smrg{
701.13Smartin	char	*dummy1;
711.13Smartin	const char	*dummy;
721.17Suwe	void (*entry)(void *) = (void (*)(void *))PROM_LOADADDR;
731.4Spk	void	*arg;
741.1Smrg
751.6Spk#ifdef HEAP_VARIABLE
761.6Spk	{
771.6Spk		extern char end[];
781.6Spk		setheap((void *)ALIGN(end), (void *)0xffffffff);
791.6Spk	}
801.6Spk#endif
811.1Smrg	prom_init();
821.12Smartin	dummy = prom_getbootpath();
831.12Smartin	if (dummy && *dummy != '\0')
841.12Smartin		strcpy(prom_bootdevice, dummy);
851.1Smrg	io.f_flags = F_RAW;
861.13Smartin	if (devopen(&io, 0, &dummy1)) {
871.4Spk		panic("%s: can't open device `%s'", progname,
881.4Spk			prom_bootdevice != NULL ? prom_bootdevice : "unknown");
891.1Smrg	}
901.1Smrg
911.18Schristos	(void)loadboot(&io, (void *)PROM_LOADADDR);
921.2Spk	(io.f_dev->dv_close)(&io);
931.4Spk
941.18Schristos	arg = (prom_version() == PROM_OLDMON) ? (void *)PROM_LOADADDR : romp;
951.4Spk	(*entry)(arg);
961.1Smrg	_rtt();
971.1Smrg}
981.1Smrg
991.1Smrgvoid
1001.17Suweloadboot(struct open_file *f, char *addr)
1011.1Smrg{
1021.4Spk	int	i;
1031.4Spk	char	*buf;
1041.1Smrg	size_t		n;
1051.1Smrg	daddr_t		blk;
1061.1Smrg
1071.1Smrg	/*
1081.1Smrg	 * Allocate a buffer that we can map into DVMA space; only
1091.1Smrg	 * needed for sun4 architecture, but use it for all machines
1101.1Smrg	 * to keep code size down as much as possible.
1111.1Smrg	 */
1121.9Sthorpej	buf = alloc(bbinfo.bbi_block_size);
1131.1Smrg	if (buf == NULL)
1141.1Smrg		panic("%s: alloc failed", progname);
1151.1Smrg
1161.9Sthorpej	for (i = 0; i < bbinfo.bbi_block_count; i++) {
1171.9Sthorpej		if ((blk = bbinfo.bbi_block_table[i]) == 0)
1181.1Smrg			panic("%s: block table corrupt", progname);
1191.1Smrg
1201.1Smrg#ifdef DEBUG
1211.1Smrg		printf("%s: block # %d = %d\n", progname, i, blk);
1221.1Smrg#endif
1231.9Sthorpej		if ((f->f_dev->dv_strategy)(f->f_devdata, F_READ, blk,
1241.9Sthorpej		    bbinfo.bbi_block_size, buf, &n)) {
1251.4Spk			printf("%s: read failure", progname);
1261.4Spk			_rtt();
1271.1Smrg		}
1281.21Scegger		memcpy( addr, buf, bbinfo.bbi_block_size);
1291.9Sthorpej		if (n != bbinfo.bbi_block_size)
1301.1Smrg			panic("%s: short read", progname);
1311.1Smrg		if (i == 0) {
1321.4Spk			int m = N_GETMAGIC(*(struct exec *)addr);
1331.1Smrg			if (m == ZMAGIC || m == NMAGIC || m == OMAGIC) {
1341.1Smrg				/* Move exec header out of the way */
1351.21Scegger				memcpy( addr - sizeof(struct exec), addr, n);
1361.1Smrg				addr -= sizeof(struct exec);
1371.1Smrg			}
1381.1Smrg		}
1391.1Smrg		addr += n;
1401.1Smrg	}
1411.1Smrg
1421.4Spk}
1431.4Spk
1441.4Spk/*
1451.4Spk * We don't need the overlap handling feature that the libkern version
1461.4Spk * of bcopy() provides. We DO need code compactness..
1471.4Spk */
1481.4Spkvoid
1491.17Suwebcopy(const void *src, void *dst, size_t n)
1501.4Spk{
1511.4Spk	const char *p = src;
1521.4Spk	char *q = dst;
1531.4Spk
1541.4Spk	while (n-- > 0)
1551.4Spk		*q++ = *p++;
1561.1Smrg}
157