1*85e3fbd2Sdholland /* $NetBSD: bootxx.c,v 1.17 2016/06/11 06:28:07 dholland Exp $ */
2e538eed2Sleo
3e538eed2Sleo /*
4e538eed2Sleo * Copyright (c) 1995 Waldi Ravens.
5e538eed2Sleo * All rights reserved.
6e538eed2Sleo *
7e538eed2Sleo * Redistribution and use in source and binary forms, with or without
8e538eed2Sleo * modification, are permitted provided that the following conditions
9e538eed2Sleo * are met:
10e538eed2Sleo * 1. Redistributions of source code must retain the above copyright
11e538eed2Sleo * notice, this list of conditions and the following disclaimer.
12e538eed2Sleo * 2. Redistributions in binary form must reproduce the above copyright
13e538eed2Sleo * notice, this list of conditions and the following disclaimer in the
14e538eed2Sleo * documentation and/or other materials provided with the distribution.
15e538eed2Sleo * 3. All advertising materials mentioning features or use of this software
16e538eed2Sleo * must display the following acknowledgement:
17e538eed2Sleo * This product includes software developed by Waldi Ravens.
18e538eed2Sleo * 4. The name of the author may not be used to endorse or promote products
19e538eed2Sleo * derived from this software without specific prior written permission
20e538eed2Sleo *
21e538eed2Sleo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22e538eed2Sleo * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23e538eed2Sleo * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24e538eed2Sleo * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25e538eed2Sleo * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26e538eed2Sleo * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27e538eed2Sleo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28e538eed2Sleo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29e538eed2Sleo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30e538eed2Sleo * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31e538eed2Sleo */
32e538eed2Sleo
33e538eed2Sleo #define boot_BSD bsd_startup
34e538eed2Sleo
35e63501d2Sjunyoung #include <lib/libsa/stand.h>
361f92b460Sleo #include <atari_stand.h>
37e538eed2Sleo #include <libkern.h>
3875b42ecbSleo #include <tosdefs.h>
3949c105ffSjdolecek #include <sys/boot_flag.h>
40e538eed2Sleo #include <sys/exec.h>
41e538eed2Sleo #include <sys/reboot.h>
42e538eed2Sleo #include <machine/cpu.h>
43e538eed2Sleo
4497412aceSjunyoung typedef int (*bxxx_t)(void *, void *, struct osdsc *);
45e538eed2Sleo
46fe5a481bStsutsui int bootxx(void *, void *, int);
4797412aceSjunyoung void boot_BSD(struct kparamb *) __attribute__((noreturn));
4897412aceSjunyoung int bootxxx(void *, void *, struct osdsc *);
4997412aceSjunyoung int load_booter(struct osdsc *);
5097412aceSjunyoung int usr_info(struct osdsc *);
51e538eed2Sleo
521d361e78Stsutsui #define BOOTXXX_MAXSIZE (64 * 1024)
531d361e78Stsutsui #define HEAPSIZE (64 * 1024) /* should be >32KB for ffs blocksize */
541d361e78Stsutsui #define HEAPSTART (LOADADDR3 + BOOTXXX_MAXSIZE)
551d361e78Stsutsui #define HEAPEND (HEAPSTART + HEAPSIZE)
561d361e78Stsutsui
57e538eed2Sleo int
bootxx(void * readsector,void * disklabel,int autoboot)5897412aceSjunyoung bootxx(void *readsector, void *disklabel, int autoboot)
59e538eed2Sleo {
6075b42ecbSleo static osdsc_t os_desc;
61e538eed2Sleo extern char end[], edata[];
6275b42ecbSleo osdsc_t *od = &os_desc;
6375b42ecbSleo bxxx_t bootxxx = (bxxx_t)(LOADADDR3);
64e538eed2Sleo
65c363a9cbScegger memset(edata, 0, end - edata);
661d361e78Stsutsui setheap((void *)HEAPSTART, (void *)HEAPEND);
67e538eed2Sleo
6897412aceSjunyoung printf("\033v\nNetBSD/atari secondary bootloader"
69*85e3fbd2Sdholland " ($Revision: 1.17 $)\n\n");
70e538eed2Sleo
71e538eed2Sleo if (init_dskio(readsector, disklabel, -1))
7297412aceSjunyoung return -1;
73e538eed2Sleo
74e538eed2Sleo for (;;) {
75e538eed2Sleo od->rootfs = 0; /* partition a */
76e538eed2Sleo od->osname = "/netbsd";
77e538eed2Sleo od->ostype = &od->osname[1];
78e538eed2Sleo od->boothowto = (RB_RDONLY);
79e538eed2Sleo
80e538eed2Sleo if (!autoboot) {
81e538eed2Sleo int pref;
82e538eed2Sleo
83e538eed2Sleo od->boothowto = (RB_RDONLY|RB_SINGLE);
84e538eed2Sleo pref = usr_info(od);
85e538eed2Sleo if (pref < 0)
86e538eed2Sleo continue;
87e538eed2Sleo if (pref > 0)
8897412aceSjunyoung return pref;
89e538eed2Sleo }
90e538eed2Sleo autoboot = 0; /* in case auto boot fails */
91e538eed2Sleo
92e538eed2Sleo if (init_dskio(readsector, disklabel, od->rootfs))
93e538eed2Sleo continue;
94e538eed2Sleo
9575b42ecbSleo if (load_booter(od))
96e538eed2Sleo continue;
97e538eed2Sleo
9875b42ecbSleo (*bootxxx)(readsector, disklabel, od);
99e538eed2Sleo }
100e538eed2Sleo /* NOTREACHED */
101e538eed2Sleo }
102e538eed2Sleo
103e538eed2Sleo
104e538eed2Sleo int
usr_info(osdsc_t * od)10597412aceSjunyoung usr_info(osdsc_t *od)
106e538eed2Sleo {
107e538eed2Sleo static char line[800];
108e538eed2Sleo char c, *p = line;
109e538eed2Sleo
110e538eed2Sleo printf("\nEnter os-type [.%s] root-fs [:a] kernel [%s]"
111e538eed2Sleo " options [none]:\n\033e", od->ostype, od->osname);
112*85e3fbd2Sdholland kgets(p, sizeof(line));
113e538eed2Sleo printf("\033f");
114e538eed2Sleo
115e538eed2Sleo for (;;) {
116e538eed2Sleo while (isspace(*p))
117e538eed2Sleo *p++ = '\0';
118e538eed2Sleo
119e538eed2Sleo switch (*p++) {
120e538eed2Sleo case '\0':
121e538eed2Sleo goto done;
122e538eed2Sleo case ':':
123e538eed2Sleo if ((c = *p) >= 'a' && c <= 'z')
124e538eed2Sleo od->rootfs = c - 'a';
125e538eed2Sleo else if (c >= 'A' && c <= 'Z')
126e538eed2Sleo od->rootfs = c - ('A' - 27);
12797412aceSjunyoung else
12897412aceSjunyoung return -1;
129e538eed2Sleo
130e538eed2Sleo if (!od->rootfs)
131e538eed2Sleo break;
132e538eed2Sleo *p = 'b';
133e538eed2Sleo /* FALLTHROUGH */
134e538eed2Sleo case '-':
135e538eed2Sleo if ((c = *p) == 'a')
136e538eed2Sleo od->boothowto &= ~RB_SINGLE;
137e538eed2Sleo else if (c == 'b')
138e538eed2Sleo od->boothowto |= RB_ASKNAME;
13949c105ffSjdolecek else
14049c105ffSjdolecek BOOT_FLAG(c, od->boothowto);
141e538eed2Sleo break;
142e538eed2Sleo case '.':
143e538eed2Sleo od->ostype = p;
144e538eed2Sleo break;
145e538eed2Sleo case '/':
146e538eed2Sleo od->osname = --p;
147e538eed2Sleo break;
148e538eed2Sleo default:
14997412aceSjunyoung return -1;
150e538eed2Sleo }
151e538eed2Sleo
152e538eed2Sleo while ((c = *p) && !isspace(c))
153e538eed2Sleo p += 1;
154e538eed2Sleo }
155e538eed2Sleo
156e538eed2Sleo done:
157e538eed2Sleo c = od->ostype[0];
158e538eed2Sleo if (isupper(c))
159e538eed2Sleo c = tolower(c);
160e538eed2Sleo
161e538eed2Sleo switch (c) {
162e538eed2Sleo case 'n': /* NetBSD */
16397412aceSjunyoung return 0;
164e538eed2Sleo case 'l': /* Linux */
16597412aceSjunyoung return 0x10;
166e538eed2Sleo case 'a': /* ASV */
16797412aceSjunyoung return 0x40;
168e538eed2Sleo case 't': /* TOS */
16997412aceSjunyoung return 0x80;
170e538eed2Sleo default:
17197412aceSjunyoung return -1;
172e538eed2Sleo }
173e538eed2Sleo }
174e538eed2Sleo
175e538eed2Sleo int
load_booter(osdsc_t * od)17697412aceSjunyoung load_booter(osdsc_t *od)
177e538eed2Sleo {
1781913310cSleo int fd = -1;
17975b42ecbSleo u_char *bstart = (u_char *)(LOADADDR3);
18075b42ecbSleo int bsize;
18156252d54Sleo char *fname;
18256252d54Sleo char *boot_names[] = { /* 3rd level boot names */
18356252d54Sleo "/boot.atari", /* in order of preference */
18456252d54Sleo "/boot",
18556252d54Sleo "/boot.ata",
18656252d54Sleo NULL }; /* NULL terminated! */
18756252d54Sleo
188e538eed2Sleo /*
18975b42ecbSleo * Read booter's exec-header.
190e538eed2Sleo */
19156252d54Sleo for (fname = boot_names[0]; fname != NULL; fname++) {
19256252d54Sleo if ((fd = open(fname, 0)) < 0)
19356252d54Sleo printf("Cannot open '%s'\n", fname);
19497412aceSjunyoung else
19597412aceSjunyoung break;
19656252d54Sleo }
19756252d54Sleo if (fd < 0)
19897412aceSjunyoung return -1;
19975b42ecbSleo while ((bsize = read(fd, bstart, 1024)) > 0) {
20075b42ecbSleo bstart += bsize;
20135ad82f0Sthomas }
20235ad82f0Sthomas close(fd);
20375b42ecbSleo return 0;
20435ad82f0Sthomas }
20535ad82f0Sthomas
20675b42ecbSleo void
_rtt(void)20797412aceSjunyoung _rtt(void)
20875b42ecbSleo {
209360f84f6Stsutsui
21075b42ecbSleo printf("Halting...\n");
21175b42ecbSleo for (;;)
21275b42ecbSleo ;
21335ad82f0Sthomas }
214