11.8Sdholland/*	$NetBSD: boot.c,v 1.8 2016/06/11 06:49:46 dholland Exp $ */
21.1Sfredette
31.1Sfredette/*-
41.1Sfredette * Copyright (c) 1982, 1986, 1990, 1993
51.1Sfredette *	The Regents of the University of California.  All rights reserved.
61.1Sfredette *
71.1Sfredette * Redistribution and use in source and binary forms, with or without
81.1Sfredette * modification, are permitted provided that the following conditions
91.1Sfredette * are met:
101.1Sfredette * 1. Redistributions of source code must retain the above copyright
111.1Sfredette *    notice, this list of conditions and the following disclaimer.
121.1Sfredette * 2. Redistributions in binary form must reproduce the above copyright
131.1Sfredette *    notice, this list of conditions and the following disclaimer in the
141.1Sfredette *    documentation and/or other materials provided with the distribution.
151.3Sagc * 3. Neither the name of the University nor the names of its contributors
161.1Sfredette *    may be used to endorse or promote products derived from this software
171.1Sfredette *    without specific prior written permission.
181.1Sfredette *
191.1Sfredette * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
201.1Sfredette * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
211.1Sfredette * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
221.1Sfredette * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
231.1Sfredette * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
241.1Sfredette * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
251.1Sfredette * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
261.1Sfredette * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
271.1Sfredette * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
281.1Sfredette * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
291.1Sfredette * SUCH DAMAGE.
301.1Sfredette *
311.1Sfredette * 	@(#)boot.c	8.1 (Berkeley) 6/10/93
321.1Sfredette */
331.1Sfredette
341.1Sfredette#include <sys/param.h>
351.1Sfredette#include <sys/reboot.h>
361.1Sfredette#include <machine/mon.h>
371.1Sfredette
381.1Sfredette#include <stand.h>
391.1Sfredette#include <loadfile.h>
401.1Sfredette#include "libsa.h"
411.1Sfredette
421.1Sfredette/*
431.1Sfredette * Default the name (really tape segment number).
441.1Sfredette * The defaults assume the following tape layout:
451.1Sfredette *   segment 0:  tapeboot
461.1Sfredette *   segment 1:  netbsd.sun3  (RAMDISK3)
471.1Sfredette *   segment 2:  netbsd.sun3x (RAMDISK3X)
481.1Sfredette *   segment 3:  miniroot image
491.2Sfredette *   segment 4:  netbsd.sun2  (RAMDISK)
501.2Sfredette * Therefore, the default name is "1" or "2" or "4"
511.2Sfredette * for sun3, sun3x, and sun2, respectively.
521.1Sfredette */
531.1Sfredette
541.1Sfredettechar	defname[32] = "1";
551.1Sfredettechar	line[80];
561.1Sfredette
571.4Schsint
581.4Schsmain(void)
591.1Sfredette{
601.1Sfredette	char *cp, *file;
611.1Sfredette	void *entry;
621.1Sfredette	u_long marks[MARK_MAX];
631.2Sfredette	u_long mark_start;
641.1Sfredette	int fd;
651.1Sfredette
661.1Sfredette	printf(">> %s tapeboot [%s]\n", bootprog_name, bootprog_rev);
671.1Sfredette	prom_get_boot_info();
681.1Sfredette
691.1Sfredette	/*
701.1Sfredette	 * Can not hold open the tape device as is done
711.1Sfredette	 * in the other boot programs because it does
721.1Sfredette	 * its position-to-segment on open.
731.1Sfredette	 */
741.1Sfredette
751.2Sfredette	/* Assume the Sun3/Sun3x load start. */
761.2Sfredette	memset(marks, 0, sizeof(marks));
771.2Sfredette	mark_start = 0;
781.2Sfredette
791.1Sfredette	/* If running on a Sun3X, use segment 2. */
801.1Sfredette	if (_is3x)
811.1Sfredette		defname[0] = '2';
821.2Sfredette
831.2Sfredette	/*
841.2Sfredette	 * If running on a Sun2, use segment 4 and
851.2Sfredette	 * do the special MMU setup.
861.2Sfredette	 */
871.2Sfredette	else if (_is2) {
881.2Sfredette		defname[0] = '4';
891.2Sfredette		mark_start = sun2_map_mem_load();
901.2Sfredette	}
911.2Sfredette
921.1Sfredette	file = defname;
931.1Sfredette
941.1Sfredette	cp = prom_bootfile;
951.1Sfredette	if (cp && *cp)
961.1Sfredette		file = cp;
971.1Sfredette
981.1Sfredette	for (;;) {
991.1Sfredette		if (prom_boothow & RB_ASKNAME) {
1001.1Sfredette			printf("tapeboot: segment? [%s]: ", defname);
1011.8Sdholland			kgets(line, sizeof(line));
1021.1Sfredette			if (line[0])
1031.1Sfredette				file = line;
1041.1Sfredette			else
1051.1Sfredette				file = defname;
1061.1Sfredette		} else
1071.1Sfredette			printf("tapeboot: loading segment %s\n", file);
1081.1Sfredette
1091.2Sfredette		marks[MARK_START] = mark_start;
1101.7Stsutsui		if ((fd = loadfile(file, marks,
1111.7Stsutsui		    LOAD_KERNEL & ~LOAD_BACKWARDS)) != -1) {
1121.1Sfredette			break;
1131.1Sfredette		}
1141.1Sfredette		printf("tapeboot: segment %s: %s\n", file, strerror(errno));
1151.1Sfredette		prom_boothow |= RB_ASKNAME;
1161.1Sfredette	}
1171.1Sfredette	close(fd);
1181.1Sfredette
1191.1Sfredette	entry = (void *)marks[MARK_ENTRY];
1201.2Sfredette	if (_is2) {
1211.2Sfredette		printf("relocating program...");
1221.2Sfredette		entry = sun2_map_mem_run(entry);
1231.2Sfredette	}
1241.6Stsutsui	printf("Starting program at 0x%x\n", (u_int)entry);
1251.1Sfredette	chain_to(entry);
1261.6Stsutsui
1271.6Stsutsui	return 0;
1281.1Sfredette}
129