bootxx.c revision 1.3
11.3Slukem/*	$NetBSD: bootxx.c,v 1.3 2002/04/24 01:40:25 lukem Exp $ */
21.1Sfredette
31.1Sfredette/*-
41.1Sfredette * Copyright (c) 1998 The NetBSD Foundation, Inc.
51.1Sfredette * All rights reserved.
61.1Sfredette *
71.1Sfredette * This code is derived from software contributed to The NetBSD Foundation
81.1Sfredette * by Paul Kranenburg.
91.1Sfredette *
101.1Sfredette * Redistribution and use in source and binary forms, with or without
111.1Sfredette * modification, are permitted provided that the following conditions
121.1Sfredette * are met:
131.1Sfredette * 1. Redistributions of source code must retain the above copyright
141.1Sfredette *    notice, this list of conditions and the following disclaimer.
151.1Sfredette * 2. Redistributions in binary form must reproduce the above copyright
161.1Sfredette *    notice, this list of conditions and the following disclaimer in the
171.1Sfredette *    documentation and/or other materials provided with the distribution.
181.1Sfredette * 3. All advertising materials mentioning features or use of this software
191.1Sfredette *    must display the following acknowledgement:
201.1Sfredette *        This product includes software developed by the NetBSD
211.1Sfredette *        Foundation, Inc. and its contributors.
221.1Sfredette * 4. Neither the name of The NetBSD Foundation nor the names of its
231.1Sfredette *    contributors may be used to endorse or promote products derived
241.1Sfredette *    from this software without specific prior written permission.
251.1Sfredette *
261.1Sfredette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
271.1Sfredette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
281.1Sfredette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
291.1Sfredette * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
301.1Sfredette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
311.1Sfredette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
321.1Sfredette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
331.1Sfredette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
341.1Sfredette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
351.1Sfredette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
361.1Sfredette * POSSIBILITY OF SUCH DAMAGE.
371.1Sfredette */
381.1Sfredette
391.1Sfredette/*
401.1Sfredette * This is a generic "first-stage" boot program.
411.1Sfredette *
421.1Sfredette * Note that this program has absolutely no filesystem knowledge!
431.1Sfredette *
441.1Sfredette * Instead, this uses a table of disk block numbers that are
451.1Sfredette * filled in by the installboot program such that this program
461.1Sfredette * can load the "second-stage" boot program.
471.1Sfredette */
481.1Sfredette
491.1Sfredette#include <sys/param.h>
501.1Sfredette#include <machine/mon.h>
511.1Sfredette
521.3Slukem#include <dev/sun/sun_boot.h>
531.3Slukem
541.1Sfredette#include <stand.h>
551.1Sfredette#include "libsa.h"
561.1Sfredette
571.1Sfredette/*
581.1Sfredette * This is the address where we load the second-stage boot loader.
591.1Sfredette */
601.1Sfredette#define LOADADDR	0x4000
611.1Sfredette
621.1Sfredette/*
631.3Slukem * The contents of the sun68k_bbinfo below are set by installboot(8)
641.2Sfredette * to hold the filesystem data of the second-stage boot program
651.2Sfredette * (typically `/ufsboot'): filesystem block size, # of filesystem
661.2Sfredette * blocks and the block numbers themselves.
671.1Sfredette */
681.3Slukemstruct sun68k_bbinfo bbinfo = {
691.3Slukem	{ SUN68K_BBINFO_MAGIC },
701.2Sfredette	0,
711.2Sfredette	MAXBLOCKNUM,
721.2Sfredette	{ 0 }
731.2Sfredette};
741.1Sfredette
751.1Sfredetteint
761.1Sfredettemain()
771.1Sfredette{
781.1Sfredette	struct open_file	f;
791.1Sfredette	void	*entry;
801.1Sfredette	char	*addr;
811.1Sfredette	int n, error;
821.1Sfredette
831.1Sfredette#ifdef DEBUG
841.1Sfredette	printf("bootxx: open...\n");
851.1Sfredette#endif
861.1Sfredette	f.f_flags = F_RAW;
871.1Sfredette	if (devopen(&f, 0, &addr)) {
881.1Sfredette		printf("bootxx: devopen failed\n");
891.1Sfredette		return;
901.1Sfredette	}
911.1Sfredette
921.1Sfredette	addr = (char*)LOADADDR;
931.1Sfredette	error = copyboot(&f, addr);
941.1Sfredette	f.f_dev->dv_close(&f);
951.1Sfredette	if (!error) {
961.1Sfredette#ifdef DEBUG
971.1Sfredette		printf("bootxx: start 0x%x\n", (long)addr);
981.1Sfredette#endif
991.1Sfredette		entry = addr;
1001.1Sfredette		chain_to(entry);
1011.1Sfredette	}
1021.1Sfredette	/* copyboot had a problem... */
1031.1Sfredette	return;
1041.1Sfredette}
1051.1Sfredette
1061.1Sfredetteint
1071.1Sfredettecopyboot(fp, addr)
1081.1Sfredette	struct open_file	*fp;
1091.1Sfredette	char			*addr;
1101.1Sfredette{
1111.1Sfredette	int	n, i, blknum;
1121.1Sfredette	char *buf;
1131.1Sfredette
1141.1Sfredette	/* Need to use a buffer that can be mapped into DVMA space. */
1151.2Sfredette	buf = alloc(bbinfo.bbi_block_size);
1161.1Sfredette	if (!buf)
1171.1Sfredette		panic("bootxx: alloc failed");
1181.1Sfredette
1191.2Sfredette	for (i = 0; i < bbinfo.bbi_block_count; i++) {
1201.1Sfredette
1211.2Sfredette		if ((blknum = bbinfo.bbi_block_table[i]) == 0)
1221.1Sfredette			break;
1231.1Sfredette
1241.1Sfredette#ifdef DEBUG
1251.1Sfredette		printf("bootxx: block # %d = %d\n", i, blknum);
1261.1Sfredette#endif
1271.2Sfredette		if ((fp->f_dev->dv_strategy)(fp->f_devdata, F_READ, blknum,
1281.2Sfredette					   bbinfo.bbi_block_size, buf, &n))
1291.1Sfredette		{
1301.1Sfredette			printf("bootxx: read failed\n");
1311.1Sfredette			return -1;
1321.1Sfredette		}
1331.2Sfredette		if (n != bbinfo.bbi_block_size) {
1341.1Sfredette			printf("bootxx: short read\n");
1351.1Sfredette			return -1;
1361.1Sfredette		}
1371.2Sfredette		bcopy(buf, addr, bbinfo.bbi_block_size);
1381.2Sfredette		addr += bbinfo.bbi_block_size;
1391.1Sfredette	}
1401.1Sfredette
1411.1Sfredette	return 0;
1421.1Sfredette}
1431.1Sfredette
144