11.12Stsutsui/*	$NetBSD: bootxx.c,v 1.12 2009/01/12 07:00:59 tsutsui 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 *
191.1Sfredette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.1Sfredette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.1Sfredette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.1Sfredette * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.1Sfredette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.1Sfredette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.1Sfredette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.1Sfredette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.1Sfredette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.1Sfredette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.1Sfredette * POSSIBILITY OF SUCH DAMAGE.
301.1Sfredette */
311.1Sfredette
321.1Sfredette/*
331.1Sfredette * This is a generic "first-stage" boot program.
341.1Sfredette *
351.1Sfredette * Note that this program has absolutely no filesystem knowledge!
361.1Sfredette *
371.1Sfredette * Instead, this uses a table of disk block numbers that are
381.1Sfredette * filled in by the installboot program such that this program
391.1Sfredette * can load the "second-stage" boot program.
401.1Sfredette */
411.1Sfredette
421.1Sfredette#include <sys/param.h>
431.6Slukem#include <sys/bootblock.h>
441.1Sfredette#include <machine/mon.h>
451.3Slukem
461.1Sfredette#include <stand.h>
471.1Sfredette#include "libsa.h"
481.1Sfredette
491.12Stsutsuiint copyboot(struct open_file *, char *);
501.12Stsutsui
511.1Sfredette/*
521.1Sfredette * This is the address where we load the second-stage boot loader.
531.1Sfredette */
541.1Sfredette#define LOADADDR	0x4000
551.1Sfredette
561.1Sfredette/*
571.3Slukem * The contents of the sun68k_bbinfo below are set by installboot(8)
581.2Sfredette * to hold the filesystem data of the second-stage boot program
591.2Sfredette * (typically `/ufsboot'): filesystem block size, # of filesystem
601.2Sfredette * blocks and the block numbers themselves.
611.1Sfredette */
621.8Slukemstruct shared_bbinfo bbinfo = {
631.3Slukem	{ SUN68K_BBINFO_MAGIC },
641.2Sfredette	0,
651.8Slukem	SHARED_BBINFO_MAXBLOCKS,
661.2Sfredette	{ 0 }
671.2Sfredette};
681.1Sfredette
691.9Schsint
701.9Schsmain(void)
711.1Sfredette{
721.1Sfredette	struct open_file	f;
731.1Sfredette	void	*entry;
741.1Sfredette	char	*addr;
751.12Stsutsui	int error;
761.1Sfredette
771.1Sfredette#ifdef DEBUG
781.1Sfredette	printf("bootxx: open...\n");
791.1Sfredette#endif
801.1Sfredette	f.f_flags = F_RAW;
811.1Sfredette	if (devopen(&f, 0, &addr)) {
821.7Slukem		putstr("bootxx: devopen failed\n");
831.12Stsutsui		return 1;
841.1Sfredette	}
851.1Sfredette
861.1Sfredette	addr = (char*)LOADADDR;
871.1Sfredette	error = copyboot(&f, addr);
881.1Sfredette	f.f_dev->dv_close(&f);
891.1Sfredette	if (!error) {
901.1Sfredette#ifdef DEBUG
911.1Sfredette		printf("bootxx: start 0x%x\n", (long)addr);
921.1Sfredette#endif
931.1Sfredette		entry = addr;
941.1Sfredette		chain_to(entry);
951.1Sfredette	}
961.1Sfredette	/* copyboot had a problem... */
971.12Stsutsui	return 0;
981.1Sfredette}
991.1Sfredette
1001.9Schsint
1011.9Schscopyboot(struct open_file *fp, char *addr)
1021.1Sfredette{
1031.12Stsutsui	size_t n;
1041.12Stsutsui	int i, blknum;
1051.1Sfredette	char *buf;
1061.1Sfredette
1071.1Sfredette	/* Need to use a buffer that can be mapped into DVMA space. */
1081.2Sfredette	buf = alloc(bbinfo.bbi_block_size);
1091.1Sfredette	if (!buf)
1101.1Sfredette		panic("bootxx: alloc failed");
1111.1Sfredette
1121.2Sfredette	for (i = 0; i < bbinfo.bbi_block_count; i++) {
1131.1Sfredette
1141.2Sfredette		if ((blknum = bbinfo.bbi_block_table[i]) == 0)
1151.1Sfredette			break;
1161.1Sfredette
1171.1Sfredette#ifdef DEBUG
1181.1Sfredette		printf("bootxx: block # %d = %d\n", i, blknum);
1191.1Sfredette#endif
1201.2Sfredette		if ((fp->f_dev->dv_strategy)(fp->f_devdata, F_READ, blknum,
1211.2Sfredette					   bbinfo.bbi_block_size, buf, &n))
1221.1Sfredette		{
1231.7Slukem			putstr("bootxx: read failed\n");
1241.1Sfredette			return -1;
1251.1Sfredette		}
1261.2Sfredette		if (n != bbinfo.bbi_block_size) {
1271.7Slukem			putstr("bootxx: short read\n");
1281.1Sfredette			return -1;
1291.1Sfredette		}
1301.9Schs		memcpy(addr, buf, bbinfo.bbi_block_size);
1311.2Sfredette		addr += bbinfo.bbi_block_size;
1321.1Sfredette	}
1331.1Sfredette
1341.1Sfredette	return 0;
1351.1Sfredette}
136