Home | History | Annotate | Line # | Download | only in bootxx
bootxx.c revision 1.17
      1 /*	$NetBSD: bootxx.c,v 1.17 2016/06/11 06:28:07 dholland Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Waldi Ravens.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *        This product includes software developed by Waldi Ravens.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #define	boot_BSD	bsd_startup
     34 
     35 #include <lib/libsa/stand.h>
     36 #include <atari_stand.h>
     37 #include <libkern.h>
     38 #include <tosdefs.h>
     39 #include <sys/boot_flag.h>
     40 #include <sys/exec.h>
     41 #include <sys/reboot.h>
     42 #include <machine/cpu.h>
     43 
     44 typedef int      (*bxxx_t)(void *, void *, struct osdsc *);
     45 
     46 int	bootxx(void *, void *, int);
     47 void	boot_BSD(struct kparamb *) __attribute__((noreturn));
     48 int	bootxxx(void *, void *, struct osdsc *);
     49 int	load_booter(struct osdsc *);
     50 int	usr_info(struct osdsc *);
     51 
     52 #define	BOOTXXX_MAXSIZE	(64 * 1024)
     53 #define	HEAPSIZE	(64 * 1024)	/* should be >32KB for ffs blocksize */
     54 #define	HEAPSTART	(LOADADDR3 + BOOTXXX_MAXSIZE)
     55 #define	HEAPEND		(HEAPSTART + HEAPSIZE)
     56 
     57 int
     58 bootxx(void *readsector, void *disklabel, int autoboot)
     59 {
     60 	static osdsc_t	os_desc;
     61 	extern char	end[], edata[];
     62 	osdsc_t		*od = &os_desc;
     63 	bxxx_t		bootxxx = (bxxx_t)(LOADADDR3);
     64 
     65 	memset(edata, 0, end - edata);
     66 	setheap((void *)HEAPSTART, (void *)HEAPEND);
     67 
     68 	printf("\033v\nNetBSD/atari secondary bootloader"
     69 						" ($Revision: 1.17 $)\n\n");
     70 
     71 	if (init_dskio(readsector, disklabel, -1))
     72 		return -1;
     73 
     74 	for (;;) {
     75 		od->rootfs = 0;			/* partition a */
     76 		od->osname = "/netbsd";
     77 		od->ostype = &od->osname[1];
     78 		od->boothowto = (RB_RDONLY);
     79 
     80 		if (!autoboot) {
     81 			int	pref;
     82 
     83 			od->boothowto = (RB_RDONLY|RB_SINGLE);
     84 			pref = usr_info(od);
     85 			if (pref < 0)
     86 				continue;
     87 			if (pref > 0)
     88 				return pref;
     89 		}
     90 		autoboot = 0;			/* in case auto boot fails */
     91 
     92 		if (init_dskio(readsector, disklabel, od->rootfs))
     93 			continue;
     94 
     95 		if (load_booter(od))
     96 			continue;
     97 
     98 		(*bootxxx)(readsector, disklabel, od);
     99 	}
    100 	/* NOTREACHED */
    101 }
    102 
    103 
    104 int
    105 usr_info(osdsc_t *od)
    106 {
    107 	static char	line[800];
    108 	char		c, *p = line;
    109 
    110 	printf("\nEnter os-type [.%s] root-fs [:a] kernel [%s]"
    111 	       " options [none]:\n\033e", od->ostype, od->osname);
    112 	kgets(p, sizeof(line));
    113 	printf("\033f");
    114 
    115 	for (;;) {
    116 		while (isspace(*p))
    117 			*p++ = '\0';
    118 
    119 		switch (*p++) {
    120 		case '\0':
    121 			goto done;
    122 		case ':':
    123 			if ((c = *p) >= 'a' && c <= 'z')
    124 				od->rootfs = c - 'a';
    125 			else if (c >= 'A' && c <= 'Z')
    126 				od->rootfs = c - ('A' - 27);
    127 			else
    128 				return -1;
    129 
    130 			if (!od->rootfs)
    131 				break;
    132 			*p = 'b';
    133 			/* FALLTHROUGH */
    134 		  case '-':
    135 			if ((c = *p) == 'a')
    136 				od->boothowto &= ~RB_SINGLE;
    137 			else if (c == 'b')
    138 				od->boothowto |= RB_ASKNAME;
    139 			else
    140 				BOOT_FLAG(c, od->boothowto);
    141 			break;
    142 		  case '.':
    143 			od->ostype = p;
    144 			break;
    145 		  case '/':
    146 			od->osname = --p;
    147 			break;
    148 		  default:
    149 			return -1;
    150 		}
    151 
    152 		while ((c = *p) && !isspace(c))
    153 			p += 1;
    154 	}
    155 
    156 done:
    157 	c = od->ostype[0];
    158 	if (isupper(c))
    159 		c = tolower(c);
    160 
    161 	switch (c) {
    162 	case 'n':		/* NetBSD */
    163 		return 0;
    164 	case 'l':		/* Linux  */
    165 		return 0x10;
    166 	case 'a':		/* ASV    */
    167 		return 0x40;
    168 	case 't':		/* TOS    */
    169 		return 0x80;
    170 	default:
    171 		return -1;
    172 	}
    173 }
    174 
    175 int
    176 load_booter(osdsc_t *od)
    177 {
    178 	int		fd = -1;
    179 	u_char		*bstart = (u_char *)(LOADADDR3);
    180 	int		bsize;
    181 	char		*fname;
    182 	char		*boot_names[] = {	/* 3rd level boot names	  */
    183 				"/boot.atari",	/* in order of preference */
    184 				"/boot",
    185 				"/boot.ata",
    186 				NULL };		/* NULL terminated!	  */
    187 
    188 	/*
    189 	 * Read booter's exec-header.
    190 	 */
    191 	for (fname = boot_names[0]; fname != NULL; fname++) {
    192 		if ((fd = open(fname, 0)) < 0)
    193 			printf("Cannot open '%s'\n", fname);
    194 		else
    195 			break;
    196 	}
    197 	if (fd < 0)
    198 		return -1;
    199 	while ((bsize = read(fd, bstart, 1024)) > 0) {
    200 		bstart += bsize;
    201 	}
    202 	close(fd);
    203 	return 0;
    204 }
    205 
    206 void
    207 _rtt(void)
    208 {
    209 
    210 	printf("Halting...\n");
    211 	for (;;)
    212 		;
    213 }
    214