Home | History | Annotate | Line # | Download | only in bootxx
bootxx.c revision 1.4
      1  1.4  jdolecek /*	$NetBSD: bootxx.c,v 1.4 2000/09/24 12:32:33 jdolecek 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.4  jdolecek #include <sys/boot_flag.h>
     46  1.1       leo #include <sys/exec.h>
     47  1.1       leo #include <sys/reboot.h>
     48  1.1       leo #include <machine/cpu.h>
     49  1.1       leo 
     50  1.1       leo #include "bootxx.h"
     51  1.1       leo 
     52  1.1       leo void	boot_BSD __P((kparb *)__attribute__((noreturn)));
     53  1.1       leo int	load_BSD __P((osdsc *));
     54  1.1       leo int	sys_info __P((osdsc *));
     55  1.1       leo int	usr_info __P((osdsc *));
     56  1.1       leo 
     57  1.1       leo int
     58  1.1       leo bootxx(readsector, disklabel, autoboot)
     59  1.1       leo 	void	*readsector,
     60  1.1       leo 		*disklabel;
     61  1.1       leo 	int	autoboot;
     62  1.1       leo {
     63  1.1       leo 	static osdsc	os_desc;
     64  1.1       leo 	extern char	end[], edata[];
     65  1.1       leo 	osdsc		*od = &os_desc;
     66  1.1       leo 
     67  1.1       leo 	bzero(edata, end - edata);
     68  1.1       leo 
     69  1.4  jdolecek 	printf("\033v\nNetBSD/Atari boot loader ($Revision: 1.4 $)\n\n");
     70  1.1       leo 
     71  1.1       leo 	if (init_dskio(readsector, disklabel, -1))
     72  1.1       leo 		return(-1);
     73  1.1       leo 
     74  1.1       leo 	if (sys_info(od))
     75  1.1       leo 		return(-2);
     76  1.1       leo 
     77  1.1       leo 	for (;;) {
     78  1.1       leo 		od->rootfs = 0;			/* partition a */
     79  1.1       leo 		od->osname = "/netbsd";
     80  1.1       leo 		od->ostype = &od->osname[1];
     81  1.1       leo 		od->boothowto = (RB_RDONLY);
     82  1.1       leo 
     83  1.1       leo 		if (!autoboot) {
     84  1.1       leo 			int	pref;
     85  1.1       leo 
     86  1.1       leo 			od->boothowto = (RB_RDONLY|RB_SINGLE);
     87  1.1       leo 			pref = usr_info(od);
     88  1.1       leo 			if (pref < 0)
     89  1.1       leo 				continue;
     90  1.1       leo 			if (pref > 0)
     91  1.1       leo 				return(pref);
     92  1.1       leo 		}
     93  1.1       leo 		autoboot = 0;			/* in case auto boot fails */
     94  1.1       leo 
     95  1.1       leo 		if (init_dskio(readsector, disklabel, od->rootfs))
     96  1.1       leo 			continue;
     97  1.1       leo 
     98  1.1       leo 		if (load_BSD(od))
     99  1.1       leo 			continue;
    100  1.1       leo 
    101  1.1       leo 		boot_BSD(&od->kp);
    102  1.1       leo 	}
    103  1.1       leo 	/* NOTREACHED */
    104  1.1       leo }
    105  1.1       leo 
    106  1.1       leo int
    107  1.1       leo sys_info(od)
    108  1.1       leo 	osdsc	*od;
    109  1.1       leo {
    110  1.1       leo 	long	*jar;
    111  1.1       leo 	int	tos;
    112  1.1       leo 
    113  1.1       leo 	od->stmem_size = *ADDR_PHYSTOP;
    114  1.1       leo 
    115  1.1       leo 	if (*ADDR_CHKRAMTOP == RAMTOP_MAGIC) {
    116  1.1       leo 		od->ttmem_size  = *ADDR_RAMTOP;
    117  1.1       leo 		if (od->ttmem_size > TTRAM_BASE) {
    118  1.1       leo 			od->ttmem_size  -= TTRAM_BASE;
    119  1.1       leo 			od->ttmem_start  = TTRAM_BASE;
    120  1.1       leo 		}
    121  1.1       leo 	}
    122  1.1       leo 
    123  1.1       leo 	tos = (*ADDR_SYSBASE)->os_version;
    124  1.1       leo 	if ((tos > 0x300) && (tos < 0x306))
    125  1.1       leo 		od->cputype = ATARI_CLKBROKEN;
    126  1.1       leo 
    127  1.1       leo 	if ((jar = *ADDR_P_COOKIE)) {
    128  1.1       leo 		while (jar[0]) {
    129  1.1       leo 			if (jar[0] == 0x5f435055L) { /* _CPU	*/
    130  1.1       leo 				switch(jar[1]) {
    131  1.1       leo 					case 0:
    132  1.1       leo 						od->cputype |= ATARI_68000;
    133  1.1       leo 						break;
    134  1.1       leo 					case 10:
    135  1.1       leo 						od->cputype |= ATARI_68010;
    136  1.1       leo 						break;
    137  1.1       leo 					case 20:
    138  1.1       leo 						od->cputype |= ATARI_68020;
    139  1.1       leo 						break;
    140  1.1       leo 					case 30:
    141  1.1       leo 						od->cputype |= ATARI_68030;
    142  1.1       leo 						break;
    143  1.1       leo 					case 40:
    144  1.1       leo 						od->cputype |= ATARI_68040;
    145  1.3       leo 						break;
    146  1.3       leo 					case 60:
    147  1.3       leo 						od->cputype |= ATARI_68060;
    148  1.1       leo 						break;
    149  1.1       leo 				}
    150  1.1       leo 			}
    151  1.1       leo 			jar += 2;
    152  1.1       leo 		}
    153  1.1       leo 	}
    154  1.1       leo 	if (!(od->cputype & ATARI_ANYCPU)) {
    155  1.1       leo 		printf("Unknown CPU-type.\n");
    156  1.1       leo 		return(-1);
    157  1.1       leo 	}
    158  1.1       leo 
    159  1.1       leo 	return(0);
    160  1.1       leo }
    161  1.1       leo 
    162  1.1       leo int
    163  1.1       leo usr_info(od)
    164  1.1       leo 	osdsc	*od;
    165  1.1       leo {
    166  1.1       leo 	static char	line[800];
    167  1.1       leo 	char		c, *p = line;
    168  1.1       leo 
    169  1.1       leo 	printf("\nEnter os-type [.%s] root-fs [:a] kernel [%s]"
    170  1.1       leo 	       " options [none]:\n\033e", od->ostype, od->osname);
    171  1.1       leo 	gets(p);
    172  1.1       leo 	printf("\033f");
    173  1.1       leo 
    174  1.1       leo 	for (;;) {
    175  1.1       leo 		while (isspace(*p))
    176  1.1       leo 			*p++ = '\0';
    177  1.1       leo 
    178  1.1       leo 		switch (*p++) {
    179  1.1       leo 		  case '\0':
    180  1.1       leo 			goto done;
    181  1.1       leo 		  case ':':
    182  1.1       leo 			if ((c = *p) >= 'a' && c <= 'z')
    183  1.1       leo 				od->rootfs = c - 'a';
    184  1.1       leo 			else if (c >= 'A' && c <= 'Z')
    185  1.1       leo 				od->rootfs = c - ('A' - 27);
    186  1.1       leo 			else return(-1);
    187  1.1       leo 
    188  1.1       leo 			if (!od->rootfs)
    189  1.1       leo 				break;
    190  1.1       leo 			*p = 'b';
    191  1.1       leo 			/* FALLTHROUGH */
    192  1.1       leo 		  case '-':
    193  1.1       leo 			if ((c = *p) == 'a')
    194  1.1       leo 				od->boothowto &= ~RB_SINGLE;
    195  1.1       leo 			else if (c == 'b')
    196  1.1       leo 				od->boothowto |= RB_ASKNAME;
    197  1.4  jdolecek 			else
    198  1.4  jdolecek 				BOOT_FLAG(c, od->boothowto);
    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