Home | History | Annotate | Line # | Download | only in bootxx
bootxx.c revision 1.2.8.1
      1  1.2.8.1  cgd /*	$NetBSD: bootxx.c,v 1.2.8.1 1998/10/30 17:21:35 cgd Exp $	*/
      2      1.1  leo 
      3      1.1  leo /*
      4      1.1  leo  * Copyright (c) 1995 Waldi Ravens.
      5      1.1  leo  * All rights reserved.
      6      1.1  leo  *
      7      1.1  leo  * Redistribution and use in source and binary forms, with or without
      8      1.1  leo  * modification, are permitted provided that the following conditions
      9      1.1  leo  * are met:
     10      1.1  leo  * 1. Redistributions of source code must retain the above copyright
     11      1.1  leo  *    notice, this list of conditions and the following disclaimer.
     12      1.1  leo  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1  leo  *    notice, this list of conditions and the following disclaimer in the
     14      1.1  leo  *    documentation and/or other materials provided with the distribution.
     15      1.1  leo  * 3. All advertising materials mentioning features or use of this software
     16      1.1  leo  *    must display the following acknowledgement:
     17      1.1  leo  *        This product includes software developed by Waldi Ravens.
     18      1.1  leo  * 4. The name of the author may not be used to endorse or promote products
     19      1.1  leo  *    derived from this software without specific prior written permission
     20      1.1  leo  *
     21      1.1  leo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22      1.1  leo  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23      1.1  leo  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24      1.1  leo  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25      1.1  leo  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26      1.1  leo  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27      1.1  leo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28      1.1  leo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29      1.1  leo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30      1.1  leo  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31      1.1  leo  */
     32      1.1  leo 
     33      1.1  leo asm("	.text
     34      1.1  leo 	.even
     35      1.1  leo start:
     36      1.1  leo 	bras	_bootxx
     37      1.1  leo ");
     38      1.1  leo #define	boot_BSD	bsd_startup
     39      1.1  leo 
     40      1.1  leo #include <stand.h>
     41      1.2  leo #include <atari_stand.h>
     42      1.1  leo #include <string.h>
     43      1.1  leo #include <libkern.h>
     44      1.1  leo #include <kparamb.h>
     45      1.1  leo #include <sys/exec.h>
     46      1.1  leo #include <sys/reboot.h>
     47      1.1  leo #include <machine/cpu.h>
     48      1.1  leo 
     49      1.1  leo #include "bootxx.h"
     50      1.1  leo 
     51      1.1  leo void	boot_BSD __P((kparb *)__attribute__((noreturn)));
     52      1.1  leo int	load_BSD __P((osdsc *));
     53      1.1  leo int	sys_info __P((osdsc *));
     54      1.1  leo int	usr_info __P((osdsc *));
     55      1.1  leo 
     56      1.1  leo int
     57      1.1  leo bootxx(readsector, disklabel, autoboot)
     58      1.1  leo 	void	*readsector,
     59      1.1  leo 		*disklabel;
     60      1.1  leo 	int	autoboot;
     61      1.1  leo {
     62      1.1  leo 	static osdsc	os_desc;
     63      1.1  leo 	extern char	end[], edata[];
     64      1.1  leo 	osdsc		*od = &os_desc;
     65      1.1  leo 
     66      1.1  leo 	bzero(edata, end - edata);
     67      1.1  leo 
     68  1.2.8.1  cgd 	printf("\033v\nNetBSD/Atari boot loader ($Revision: 1.2.8.1 $)\n\n");
     69      1.1  leo 
     70      1.1  leo 	if (init_dskio(readsector, disklabel, -1))
     71      1.1  leo 		return(-1);
     72      1.1  leo 
     73      1.1  leo 	if (sys_info(od))
     74      1.1  leo 		return(-2);
     75      1.1  leo 
     76      1.1  leo 	for (;;) {
     77      1.1  leo 		od->rootfs = 0;			/* partition a */
     78      1.1  leo 		od->osname = "/netbsd";
     79      1.1  leo 		od->ostype = &od->osname[1];
     80      1.1  leo 		od->boothowto = (RB_RDONLY);
     81      1.1  leo 
     82      1.1  leo 		if (!autoboot) {
     83      1.1  leo 			int	pref;
     84      1.1  leo 
     85      1.1  leo 			od->boothowto = (RB_RDONLY|RB_SINGLE);
     86      1.1  leo 			pref = usr_info(od);
     87      1.1  leo 			if (pref < 0)
     88      1.1  leo 				continue;
     89      1.1  leo 			if (pref > 0)
     90      1.1  leo 				return(pref);
     91      1.1  leo 		}
     92      1.1  leo 		autoboot = 0;			/* in case auto boot fails */
     93      1.1  leo 
     94      1.1  leo 		if (init_dskio(readsector, disklabel, od->rootfs))
     95      1.1  leo 			continue;
     96      1.1  leo 
     97      1.1  leo 		if (load_BSD(od))
     98      1.1  leo 			continue;
     99      1.1  leo 
    100      1.1  leo 		boot_BSD(&od->kp);
    101      1.1  leo 	}
    102      1.1  leo 	/* NOTREACHED */
    103      1.1  leo }
    104      1.1  leo 
    105      1.1  leo int
    106      1.1  leo sys_info(od)
    107      1.1  leo 	osdsc	*od;
    108      1.1  leo {
    109      1.1  leo 	long	*jar;
    110      1.1  leo 	int	tos;
    111      1.1  leo 
    112      1.1  leo 	od->stmem_size = *ADDR_PHYSTOP;
    113      1.1  leo 
    114      1.1  leo 	if (*ADDR_CHKRAMTOP == RAMTOP_MAGIC) {
    115      1.1  leo 		od->ttmem_size  = *ADDR_RAMTOP;
    116      1.1  leo 		if (od->ttmem_size > TTRAM_BASE) {
    117      1.1  leo 			od->ttmem_size  -= TTRAM_BASE;
    118      1.1  leo 			od->ttmem_start  = TTRAM_BASE;
    119      1.1  leo 		}
    120      1.1  leo 	}
    121      1.1  leo 
    122      1.1  leo 	tos = (*ADDR_SYSBASE)->os_version;
    123      1.1  leo 	if ((tos > 0x300) && (tos < 0x306))
    124      1.1  leo 		od->cputype = ATARI_CLKBROKEN;
    125      1.1  leo 
    126      1.1  leo 	if ((jar = *ADDR_P_COOKIE)) {
    127      1.1  leo 		while (jar[0]) {
    128      1.1  leo 			if (jar[0] == 0x5f435055L) { /* _CPU	*/
    129      1.1  leo 				switch(jar[1]) {
    130      1.1  leo 					case 0:
    131      1.1  leo 						od->cputype |= ATARI_68000;
    132      1.1  leo 						break;
    133      1.1  leo 					case 10:
    134      1.1  leo 						od->cputype |= ATARI_68010;
    135      1.1  leo 						break;
    136      1.1  leo 					case 20:
    137      1.1  leo 						od->cputype |= ATARI_68020;
    138      1.1  leo 						break;
    139      1.1  leo 					case 30:
    140      1.1  leo 						od->cputype |= ATARI_68030;
    141      1.1  leo 						break;
    142      1.1  leo 					case 40:
    143      1.1  leo 						od->cputype |= ATARI_68040;
    144  1.2.8.1  cgd 						break;
    145  1.2.8.1  cgd 					case 60:
    146  1.2.8.1  cgd 						od->cputype |= ATARI_68060;
    147      1.1  leo 						break;
    148      1.1  leo 				}
    149      1.1  leo 			}
    150      1.1  leo 			jar += 2;
    151      1.1  leo 		}
    152      1.1  leo 	}
    153      1.1  leo 	if (!(od->cputype & ATARI_ANYCPU)) {
    154      1.1  leo 		printf("Unknown CPU-type.\n");
    155      1.1  leo 		return(-1);
    156      1.1  leo 	}
    157      1.1  leo 
    158      1.1  leo 	return(0);
    159      1.1  leo }
    160      1.1  leo 
    161      1.1  leo int
    162      1.1  leo usr_info(od)
    163      1.1  leo 	osdsc	*od;
    164      1.1  leo {
    165      1.1  leo 	static char	line[800];
    166      1.1  leo 	char		c, *p = line;
    167      1.1  leo 
    168      1.1  leo 	printf("\nEnter os-type [.%s] root-fs [:a] kernel [%s]"
    169      1.1  leo 	       " options [none]:\n\033e", od->ostype, od->osname);
    170      1.1  leo 	gets(p);
    171      1.1  leo 	printf("\033f");
    172      1.1  leo 
    173      1.1  leo 	for (;;) {
    174      1.1  leo 		while (isspace(*p))
    175      1.1  leo 			*p++ = '\0';
    176      1.1  leo 
    177      1.1  leo 		switch (*p++) {
    178      1.1  leo 		  case '\0':
    179      1.1  leo 			goto done;
    180      1.1  leo 		  case ':':
    181      1.1  leo 			if ((c = *p) >= 'a' && c <= 'z')
    182      1.1  leo 				od->rootfs = c - 'a';
    183      1.1  leo 			else if (c >= 'A' && c <= 'Z')
    184      1.1  leo 				od->rootfs = c - ('A' - 27);
    185      1.1  leo 			else return(-1);
    186      1.1  leo 
    187      1.1  leo 			if (!od->rootfs)
    188      1.1  leo 				break;
    189      1.1  leo 			*p = 'b';
    190      1.1  leo 			/* FALLTHROUGH */
    191      1.1  leo 		  case '-':
    192      1.1  leo 			if ((c = *p) == 'a')
    193      1.1  leo 				od->boothowto &= ~RB_SINGLE;
    194      1.1  leo 			else if (c == 'b')
    195      1.1  leo 				od->boothowto |= RB_ASKNAME;
    196      1.1  leo 			else if (c == 'd')
    197      1.1  leo 				od->boothowto |= RB_KDB;
    198      1.1  leo 			else return(-1);
    199      1.1  leo 			break;
    200      1.1  leo 		  case '.':
    201      1.1  leo 			od->ostype = p;
    202      1.1  leo 			break;
    203      1.1  leo 		  case '/':
    204      1.1  leo 			od->osname = --p;
    205      1.1  leo 			break;
    206      1.1  leo 		  default:
    207      1.1  leo 			return(-1);
    208      1.1  leo 		}
    209      1.1  leo 
    210      1.1  leo 		while ((c = *p) && !isspace(c))
    211      1.1  leo 			p += 1;
    212      1.1  leo 	}
    213      1.1  leo 
    214      1.1  leo done:
    215      1.1  leo 	c = od->ostype[0];
    216      1.1  leo 	if (isupper(c))
    217      1.1  leo 		c = tolower(c);
    218      1.1  leo 
    219      1.1  leo 	switch (c) {
    220      1.1  leo 	  case 'n':		/* NetBSD */
    221      1.1  leo 		return(0);
    222      1.1  leo 	  case 'l':		/* Linux  */
    223      1.1  leo 		return(0x10);
    224      1.1  leo 	  case 'a':		/* ASV    */
    225      1.1  leo 		return(0x40);
    226      1.1  leo 	  case 't':		/* TOS    */
    227      1.1  leo 		return(0x80);
    228      1.1  leo 	  default:
    229      1.1  leo 		return(-1);
    230      1.1  leo 	}
    231      1.1  leo }
    232      1.1  leo 
    233      1.1  leo int
    234      1.1  leo load_BSD(od)
    235      1.1  leo 	osdsc		*od;
    236      1.1  leo {
    237      1.1  leo 	struct exec	hdr;
    238      1.1  leo 	int		err, fd;
    239      1.1  leo 	u_int		textsz, strsz;
    240      1.1  leo 
    241      1.1  leo 	/*
    242      1.1  leo 	 * Read kernel's exec-header.
    243      1.1  leo 	 */
    244      1.1  leo 	err = 1;
    245      1.1  leo 	if ((fd = open(od->osname, 0)) < 0)
    246      1.1  leo 		goto error;
    247      1.1  leo 	err = 2;
    248      1.1  leo 	if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr))
    249      1.1  leo 		goto error;
    250      1.1  leo 	err = 3;
    251      1.1  leo 	if (N_GETMAGIC(hdr) != NMAGIC)
    252      1.1  leo 		goto error;
    253      1.1  leo 
    254      1.1  leo 	/*
    255      1.1  leo 	 * Extract sizes from kernel executable.
    256      1.1  leo 	 */
    257      1.1  leo 	textsz     = (hdr.a_text + __LDPGSZ - 1) & ~(__LDPGSZ - 1);
    258      1.1  leo 	od->ksize  = textsz + hdr.a_data + hdr.a_bss;
    259      1.1  leo 	od->kentry = hdr.a_entry;
    260      1.1  leo 	od->kstart = NULL;
    261      1.1  leo 	od->k_esym = 0;
    262      1.1  leo 
    263      1.1  leo 	if (hdr.a_syms) {
    264      1.1  leo 	    u_int x = sizeof(hdr) + hdr.a_text + hdr.a_data + hdr.a_syms;
    265      1.1  leo 	    err = 8;
    266      1.1  leo 	    if (lseek(fd, (off_t)x, SEEK_SET) != x)
    267      1.1  leo 		goto error;
    268      1.1  leo 	    err = 9;
    269      1.1  leo 	    if (read(fd, &strsz, sizeof(strsz)) != sizeof(strsz))
    270      1.1  leo 		goto error;
    271      1.1  leo 	    err = 10;
    272      1.1  leo 	    if (lseek(fd, (off_t)sizeof(hdr), SEEK_SET) != sizeof(hdr))
    273      1.1  leo 		goto error;
    274      1.1  leo 	    od->ksize += sizeof(strsz) + hdr.a_syms + strsz;
    275      1.1  leo 	}
    276      1.1  leo 
    277      1.1  leo 	/*
    278      1.1  leo 	 * Read text & data, clear bss
    279      1.1  leo 	 */
    280      1.1  leo 	err = 16;
    281      1.1  leo 	if ((od->kstart = alloc(od->ksize)) == NULL)
    282      1.1  leo 		goto error;
    283      1.1  leo 	err = 17;
    284      1.1  leo 	if (read(fd, od->kstart, hdr.a_text) != hdr.a_text)
    285      1.1  leo 		goto error;
    286      1.1  leo 	err = 18;
    287      1.1  leo 	if (read(fd, od->kstart + textsz, hdr.a_data) != hdr.a_data)
    288      1.1  leo 		goto error;
    289      1.1  leo 	bzero(od->kstart + textsz + hdr.a_data, hdr.a_bss);
    290      1.1  leo 
    291      1.1  leo 	/*
    292      1.1  leo 	 * Read symbol and string table
    293      1.1  leo 	 */
    294      1.1  leo 	if (hdr.a_syms) {
    295      1.1  leo 		char *p = od->kstart + textsz + hdr.a_data + hdr.a_bss;
    296      1.1  leo 		*((u_int32_t *)p)++ = hdr.a_syms;
    297      1.1  leo 		err = 24;
    298      1.1  leo 		if (read(fd, p, hdr.a_syms) != hdr.a_syms)
    299      1.1  leo 			goto error;
    300      1.1  leo 		p += hdr.a_syms;
    301      1.1  leo 		err = 25;
    302      1.1  leo 		if (read(fd, p, strsz) != strsz)
    303      1.1  leo 			goto error;
    304      1.1  leo 		od->k_esym = (p - (char *)od->kstart) + strsz;
    305      1.1  leo 	}
    306      1.1  leo 
    307      1.1  leo 	return(0);
    308      1.1  leo 
    309      1.1  leo error:
    310      1.1  leo 	if (fd >= 0) {
    311      1.1  leo 		if (od->kstart)
    312      1.1  leo 			free(od->kstart, od->ksize);
    313      1.1  leo 		close(fd);
    314      1.1  leo 	}
    315      1.1  leo 	printf("Error %d in load_BSD.\n", err);
    316      1.1  leo 	return(-1);
    317      1.1  leo }
    318