bootxx.c revision 1.8
11.8Slukem/*	$NetBSD: bootxx.c,v 1.8 2002/05/15 09:44:55 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.6Slukem#include <sys/bootblock.h>
511.1Sfredette#include <machine/mon.h>
521.3Slukem
531.1Sfredette#include <stand.h>
541.1Sfredette#include "libsa.h"
551.1Sfredette
561.1Sfredette/*
571.1Sfredette * This is the address where we load the second-stage boot loader.
581.1Sfredette */
591.1Sfredette#define LOADADDR	0x4000
601.1Sfredette
611.1Sfredette/*
621.3Slukem * The contents of the sun68k_bbinfo below are set by installboot(8)
631.2Sfredette * to hold the filesystem data of the second-stage boot program
641.2Sfredette * (typically `/ufsboot'): filesystem block size, # of filesystem
651.2Sfredette * blocks and the block numbers themselves.
661.1Sfredette */
671.8Slukemstruct shared_bbinfo bbinfo = {
681.3Slukem	{ SUN68K_BBINFO_MAGIC },
691.2Sfredette	0,
701.8Slukem	SHARED_BBINFO_MAXBLOCKS,
711.2Sfredette	{ 0 }
721.2Sfredette};
731.1Sfredette
741.1Sfredetteint
751.1Sfredettemain()
761.1Sfredette{
771.1Sfredette	struct open_file	f;
781.1Sfredette	void	*entry;
791.1Sfredette	char	*addr;
801.1Sfredette	int n, error;
811.1Sfredette
821.1Sfredette#ifdef DEBUG
831.1Sfredette	printf("bootxx: open...\n");
841.1Sfredette#endif
851.1Sfredette	f.f_flags = F_RAW;
861.1Sfredette	if (devopen(&f, 0, &addr)) {
871.7Slukem		putstr("bootxx: devopen failed\n");
881.1Sfredette		return;
891.1Sfredette	}
901.1Sfredette
911.1Sfredette	addr = (char*)LOADADDR;
921.1Sfredette	error = copyboot(&f, addr);
931.1Sfredette	f.f_dev->dv_close(&f);
941.1Sfredette	if (!error) {
951.1Sfredette#ifdef DEBUG
961.1Sfredette		printf("bootxx: start 0x%x\n", (long)addr);
971.1Sfredette#endif
981.1Sfredette		entry = addr;
991.1Sfredette		chain_to(entry);
1001.1Sfredette	}
1011.1Sfredette	/* copyboot had a problem... */
1021.1Sfredette	return;
1031.1Sfredette}
1041.1Sfredette
1051.1Sfredetteint
1061.1Sfredettecopyboot(fp, addr)
1071.1Sfredette	struct open_file	*fp;
1081.1Sfredette	char			*addr;
1091.1Sfredette{
1101.1Sfredette	int	n, i, blknum;
1111.1Sfredette	char *buf;
1121.1Sfredette
1131.1Sfredette	/* Need to use a buffer that can be mapped into DVMA space. */
1141.2Sfredette	buf = alloc(bbinfo.bbi_block_size);
1151.1Sfredette	if (!buf)
1161.1Sfredette		panic("bootxx: alloc failed");
1171.1Sfredette
1181.2Sfredette	for (i = 0; i < bbinfo.bbi_block_count; i++) {
1191.1Sfredette
1201.2Sfredette		if ((blknum = bbinfo.bbi_block_table[i]) == 0)
1211.1Sfredette			break;
1221.1Sfredette
1231.1Sfredette#ifdef DEBUG
1241.1Sfredette		printf("bootxx: block # %d = %d\n", i, blknum);
1251.1Sfredette#endif
1261.2Sfredette		if ((fp->f_dev->dv_strategy)(fp->f_devdata, F_READ, blknum,
1271.2Sfredette					   bbinfo.bbi_block_size, buf, &n))
1281.1Sfredette		{
1291.7Slukem			putstr("bootxx: read failed\n");
1301.1Sfredette			return -1;
1311.1Sfredette		}
1321.2Sfredette		if (n != bbinfo.bbi_block_size) {
1331.7Slukem			putstr("bootxx: short read\n");
1341.1Sfredette			return -1;
1351.1Sfredette		}
1361.2Sfredette		bcopy(buf, addr, bbinfo.bbi_block_size);
1371.2Sfredette		addr += bbinfo.bbi_block_size;
1381.1Sfredette	}
1391.1Sfredette
1401.1Sfredette	return 0;
1411.1Sfredette}
1421.1Sfredette
143