xref: /netbsd-src/sys/arch/atari/stand/bootxx/bootxx.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: bootxx.c,v 1.12 2005/12/11 12:17:00 christos 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 <string.h>
38 #include <libkern.h>
39 #include <tosdefs.h>
40 #include <sys/boot_flag.h>
41 #include <sys/exec.h>
42 #include <sys/reboot.h>
43 #include <machine/cpu.h>
44 
45 typedef int      (*bxxx_t)(void *, void *, struct osdsc *);
46 
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 int
53 bootxx(void *readsector, void *disklabel, int autoboot)
54 {
55 	static osdsc_t	os_desc;
56 	extern char	end[], edata[];
57 	osdsc_t		*od = &os_desc;
58 	bxxx_t		bootxxx = (bxxx_t)(LOADADDR3);
59 
60 	bzero(edata, end - edata);
61 	setheap(end, (void*)(LOADADDR3 - 4));
62 
63 	printf("\033v\nNetBSD/atari secondary bootloader"
64 						" ($Revision: 1.12 $)\n\n");
65 
66 	if (init_dskio(readsector, disklabel, -1))
67 		return -1;
68 
69 	for (;;) {
70 		od->rootfs = 0;			/* partition a */
71 		od->osname = "/netbsd";
72 		od->ostype = &od->osname[1];
73 		od->boothowto = (RB_RDONLY);
74 
75 		if (!autoboot) {
76 			int	pref;
77 
78 			od->boothowto = (RB_RDONLY|RB_SINGLE);
79 			pref = usr_info(od);
80 			if (pref < 0)
81 				continue;
82 			if (pref > 0)
83 				return pref;
84 		}
85 		autoboot = 0;			/* in case auto boot fails */
86 
87 		if (init_dskio(readsector, disklabel, od->rootfs))
88 			continue;
89 
90 		if (load_booter(od))
91 			continue;
92 
93 		(*bootxxx)(readsector, disklabel, od);
94 	}
95 	/* NOTREACHED */
96 }
97 
98 
99 int
100 usr_info(osdsc_t *od)
101 {
102 	static char	line[800];
103 	char		c, *p = line;
104 
105 	printf("\nEnter os-type [.%s] root-fs [:a] kernel [%s]"
106 	       " options [none]:\n\033e", od->ostype, od->osname);
107 	gets(p);
108 	printf("\033f");
109 
110 	for (;;) {
111 		while (isspace(*p))
112 			*p++ = '\0';
113 
114 		switch (*p++) {
115 		case '\0':
116 			goto done;
117 		case ':':
118 			if ((c = *p) >= 'a' && c <= 'z')
119 				od->rootfs = c - 'a';
120 			else if (c >= 'A' && c <= 'Z')
121 				od->rootfs = c - ('A' - 27);
122 			else
123 				return -1;
124 
125 			if (!od->rootfs)
126 				break;
127 			*p = 'b';
128 			/* FALLTHROUGH */
129 		  case '-':
130 			if ((c = *p) == 'a')
131 				od->boothowto &= ~RB_SINGLE;
132 			else if (c == 'b')
133 				od->boothowto |= RB_ASKNAME;
134 			else
135 				BOOT_FLAG(c, od->boothowto);
136 			break;
137 		  case '.':
138 			od->ostype = p;
139 			break;
140 		  case '/':
141 			od->osname = --p;
142 			break;
143 		  default:
144 			return -1;
145 		}
146 
147 		while ((c = *p) && !isspace(c))
148 			p += 1;
149 	}
150 
151 done:
152 	c = od->ostype[0];
153 	if (isupper(c))
154 		c = tolower(c);
155 
156 	switch (c) {
157 	case 'n':		/* NetBSD */
158 		return 0;
159 	case 'l':		/* Linux  */
160 		return 0x10;
161 	case 'a':		/* ASV    */
162 		return 0x40;
163 	case 't':		/* TOS    */
164 		return 0x80;
165 	default:
166 		return -1;
167 	}
168 }
169 
170 int
171 load_booter(osdsc_t *od)
172 {
173 	int		fd = -1;
174 	u_char		*bstart = (u_char *)(LOADADDR3);
175 	int		bsize;
176 	char		*fname;
177 	char		*boot_names[] = {	/* 3rd level boot names	  */
178 				"/boot.atari",	/* in order of preference */
179 				"/boot",
180 				"/boot.ata",
181 				NULL };		/* NULL terminated!	  */
182 
183 	/*
184 	 * Read booter's exec-header.
185 	 */
186 	for (fname = boot_names[0]; fname != NULL; fname++) {
187 		if ((fd = open(fname, 0)) < 0)
188 			printf("Cannot open '%s'\n", fname);
189 		else
190 			break;
191 	}
192 	if (fd < 0)
193 		return -1;
194 	while((bsize = read(fd, bstart, 1024)) > 0) {
195 		bstart += bsize;
196 	}
197 	close(fd);
198 	return 0;
199 }
200 
201 void
202 _rtt(void)
203 {
204 	printf("Halting...\n");
205 	for(;;)
206 		;
207 }
208