Home | History | Annotate | Line # | Download | only in bootxx
bootxx.c revision 1.1
      1 /*	$NetBSD: bootxx.c,v 1.1 1996/02/29 11:35:05 leo 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 asm("	.text
     34 	.even
     35 start:
     36 	bras	_bootxx
     37 ");
     38 #define	boot_BSD	bsd_startup
     39 
     40 #include <stand.h>
     41 #include <string.h>
     42 #include <libkern.h>
     43 #include <kparamb.h>
     44 #include <sys/exec.h>
     45 #include <sys/reboot.h>
     46 #include <machine/cpu.h>
     47 
     48 #include "bootxx.h"
     49 
     50 void	boot_BSD __P((kparb *)__attribute__((noreturn)));
     51 int	load_BSD __P((osdsc *));
     52 int	sys_info __P((osdsc *));
     53 int	usr_info __P((osdsc *));
     54 
     55 int
     56 bootxx(readsector, disklabel, autoboot)
     57 	void	*readsector,
     58 		*disklabel;
     59 	int	autoboot;
     60 {
     61 	static osdsc	os_desc;
     62 	extern char	end[], edata[];
     63 	osdsc		*od = &os_desc;
     64 
     65 	bzero(edata, end - edata);
     66 
     67 	printf("\033v\nNetBSD/Atari boot loader ($Revision: 1.1 $)\n\n");
     68 
     69 	if (init_dskio(readsector, disklabel, -1))
     70 		return(-1);
     71 
     72 	if (sys_info(od))
     73 		return(-2);
     74 
     75 	for (;;) {
     76 		od->rootfs = 0;			/* partition a */
     77 		od->osname = "/netbsd";
     78 		od->ostype = &od->osname[1];
     79 		od->boothowto = (RB_RDONLY);
     80 
     81 		if (!autoboot) {
     82 			int	pref;
     83 
     84 			od->boothowto = (RB_RDONLY|RB_SINGLE);
     85 			pref = usr_info(od);
     86 			if (pref < 0)
     87 				continue;
     88 			if (pref > 0)
     89 				return(pref);
     90 		}
     91 		autoboot = 0;			/* in case auto boot fails */
     92 
     93 		if (init_dskio(readsector, disklabel, od->rootfs))
     94 			continue;
     95 
     96 		if (load_BSD(od))
     97 			continue;
     98 
     99 		boot_BSD(&od->kp);
    100 	}
    101 	/* NOTREACHED */
    102 }
    103 
    104 int
    105 sys_info(od)
    106 	osdsc	*od;
    107 {
    108 	long	*jar;
    109 	int	tos;
    110 
    111 	od->stmem_size = *ADDR_PHYSTOP;
    112 
    113 	if (*ADDR_CHKRAMTOP == RAMTOP_MAGIC) {
    114 		od->ttmem_size  = *ADDR_RAMTOP;
    115 		if (od->ttmem_size > TTRAM_BASE) {
    116 			od->ttmem_size  -= TTRAM_BASE;
    117 			od->ttmem_start  = TTRAM_BASE;
    118 		}
    119 	}
    120 
    121 	tos = (*ADDR_SYSBASE)->os_version;
    122 	if ((tos > 0x300) && (tos < 0x306))
    123 		od->cputype = ATARI_CLKBROKEN;
    124 
    125 	if ((jar = *ADDR_P_COOKIE)) {
    126 		while (jar[0]) {
    127 			if (jar[0] == 0x5f435055L) { /* _CPU	*/
    128 				switch(jar[1]) {
    129 					case 0:
    130 						od->cputype |= ATARI_68000;
    131 						break;
    132 					case 10:
    133 						od->cputype |= ATARI_68010;
    134 						break;
    135 					case 20:
    136 						od->cputype |= ATARI_68020;
    137 						break;
    138 					case 30:
    139 						od->cputype |= ATARI_68030;
    140 						break;
    141 					case 40:
    142 						od->cputype |= ATARI_68040;
    143 						break;
    144 				}
    145 			}
    146 			jar += 2;
    147 		}
    148 	}
    149 	if (!(od->cputype & ATARI_ANYCPU)) {
    150 		printf("Unknown CPU-type.\n");
    151 		return(-1);
    152 	}
    153 
    154 	return(0);
    155 }
    156 
    157 int
    158 usr_info(od)
    159 	osdsc	*od;
    160 {
    161 	static char	line[800];
    162 	char		c, *p = line;
    163 
    164 	printf("\nEnter os-type [.%s] root-fs [:a] kernel [%s]"
    165 	       " options [none]:\n\033e", od->ostype, od->osname);
    166 	gets(p);
    167 	printf("\033f");
    168 
    169 	for (;;) {
    170 		while (isspace(*p))
    171 			*p++ = '\0';
    172 
    173 		switch (*p++) {
    174 		  case '\0':
    175 			goto done;
    176 		  case ':':
    177 			if ((c = *p) >= 'a' && c <= 'z')
    178 				od->rootfs = c - 'a';
    179 			else if (c >= 'A' && c <= 'Z')
    180 				od->rootfs = c - ('A' - 27);
    181 			else return(-1);
    182 
    183 			if (!od->rootfs)
    184 				break;
    185 			*p = 'b';
    186 			/* FALLTHROUGH */
    187 		  case '-':
    188 			if ((c = *p) == 'a')
    189 				od->boothowto &= ~RB_SINGLE;
    190 			else if (c == 'b')
    191 				od->boothowto |= RB_ASKNAME;
    192 			else if (c == 'd')
    193 				od->boothowto |= RB_KDB;
    194 			else return(-1);
    195 			break;
    196 		  case '.':
    197 			od->ostype = p;
    198 			break;
    199 		  case '/':
    200 			od->osname = --p;
    201 			break;
    202 		  default:
    203 			return(-1);
    204 		}
    205 
    206 		while ((c = *p) && !isspace(c))
    207 			p += 1;
    208 	}
    209 
    210 done:
    211 	c = od->ostype[0];
    212 	if (isupper(c))
    213 		c = tolower(c);
    214 
    215 	switch (c) {
    216 	  case 'n':		/* NetBSD */
    217 		return(0);
    218 	  case 'l':		/* Linux  */
    219 		return(0x10);
    220 	  case 'a':		/* ASV    */
    221 		return(0x40);
    222 	  case 't':		/* TOS    */
    223 		return(0x80);
    224 	  default:
    225 		return(-1);
    226 	}
    227 }
    228 
    229 int
    230 load_BSD(od)
    231 	osdsc		*od;
    232 {
    233 	struct exec	hdr;
    234 	int		err, fd;
    235 	u_int		textsz, strsz;
    236 
    237 	/*
    238 	 * Read kernel's exec-header.
    239 	 */
    240 	err = 1;
    241 	if ((fd = open(od->osname, 0)) < 0)
    242 		goto error;
    243 	err = 2;
    244 	if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr))
    245 		goto error;
    246 	err = 3;
    247 	if (N_GETMAGIC(hdr) != NMAGIC)
    248 		goto error;
    249 
    250 	/*
    251 	 * Extract sizes from kernel executable.
    252 	 */
    253 	textsz     = (hdr.a_text + __LDPGSZ - 1) & ~(__LDPGSZ - 1);
    254 	od->ksize  = textsz + hdr.a_data + hdr.a_bss;
    255 	od->kentry = hdr.a_entry;
    256 	od->kstart = NULL;
    257 	od->k_esym = 0;
    258 
    259 	if (hdr.a_syms) {
    260 	    u_int x = sizeof(hdr) + hdr.a_text + hdr.a_data + hdr.a_syms;
    261 	    err = 8;
    262 	    if (lseek(fd, (off_t)x, SEEK_SET) != x)
    263 		goto error;
    264 	    err = 9;
    265 	    if (read(fd, &strsz, sizeof(strsz)) != sizeof(strsz))
    266 		goto error;
    267 	    err = 10;
    268 	    if (lseek(fd, (off_t)sizeof(hdr), SEEK_SET) != sizeof(hdr))
    269 		goto error;
    270 	    od->ksize += sizeof(strsz) + hdr.a_syms + strsz;
    271 	}
    272 
    273 	/*
    274 	 * Read text & data, clear bss
    275 	 */
    276 	err = 16;
    277 	if ((od->kstart = alloc(od->ksize)) == NULL)
    278 		goto error;
    279 	err = 17;
    280 	if (read(fd, od->kstart, hdr.a_text) != hdr.a_text)
    281 		goto error;
    282 	err = 18;
    283 	if (read(fd, od->kstart + textsz, hdr.a_data) != hdr.a_data)
    284 		goto error;
    285 	bzero(od->kstart + textsz + hdr.a_data, hdr.a_bss);
    286 
    287 	/*
    288 	 * Read symbol and string table
    289 	 */
    290 	if (hdr.a_syms) {
    291 		char *p = od->kstart + textsz + hdr.a_data + hdr.a_bss;
    292 		*((u_int32_t *)p)++ = hdr.a_syms;
    293 		err = 24;
    294 		if (read(fd, p, hdr.a_syms) != hdr.a_syms)
    295 			goto error;
    296 		p += hdr.a_syms;
    297 		err = 25;
    298 		if (read(fd, p, strsz) != strsz)
    299 			goto error;
    300 		od->k_esym = (p - (char *)od->kstart) + strsz;
    301 	}
    302 
    303 	return(0);
    304 
    305 error:
    306 	if (fd >= 0) {
    307 		if (od->kstart)
    308 			free(od->kstart, od->ksize);
    309 		close(fd);
    310 	}
    311 	printf("Error %d in load_BSD.\n", err);
    312 	return(-1);
    313 }
    314