Home | History | Annotate | Line # | Download | only in boot
boot.c revision 1.1
      1 /*	$NetBSD: boot.c,v 1.1 2001/09/27 10:14:49 minoura Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001 Minoura Makoto
      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  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/param.h>
     30 #include <machine/bootinfo.h>
     31 
     32 #include <lib/libsa/stand.h>
     33 #include <lib/libsa/loadfile.h>
     34 #include <lib/libkern/libkern.h>
     35 
     36 #include "libx68k.h"
     37 #include "iocs.h"
     38 
     39 #include "exec_image.h"
     40 
     41 
     42 #define HEAP_START	((void*) 0x00080000)
     43 #define HEAP_END	((void*) 0x000fffff)
     44 #define EXSCSI_BDID	((void*) 0x00ea0001)
     45 #define SRAM_MEMSIZE	(*((long*) 0x00ed0008))
     46 /* check whether the bootinf is SCSI or floppy */
     47 #define BINF_ISFD(pbinf)	(*((char *)(pbinf) + 1) == 0)
     48 
     49 char default_kernel[20] = "sd0a:netbsd";
     50 int mpu, bootdev, hostadaptor;
     51 int console_device = -1;
     52 
     53 static void help(void);
     54 static int get_scsi_host_adapter(void);
     55 static void doboot(const char *, int);
     56 static void boot(char *);
     57 int bootmenu(void);
     58 void bootmain(int);
     59 extern int detectmpu(void);
     60 extern int badbaddr(caddr_t);
     61 
     62 /* from boot_ufs/bootmain.c */
     63 static int
     64 get_scsi_host_adapter(void)
     65 {
     66 	char *bootrom;
     67 	int ha;
     68 
     69 	bootrom = (char *) (IOCS_BOOTINF() & 0x00ffffe0);
     70 	/*
     71 	 * bootrom+0x24	"SCSIIN" ... Internal SCSI (spc@0)
     72 	 *		"SCSIEX" ... External SCSI (spc@1 or mha@0)
     73 	 */
     74 	if (*(u_short *)(bootrom + 0x24 + 4) == 0x494e) {	/* "IN" */
     75 		ha = (X68K_BOOT_SCSIIF_SPC << 4) | 0;
     76 	} else if (badbaddr(EXSCSI_BDID)) {
     77 		ha = (X68K_BOOT_SCSIIF_MHA << 4) | 0;
     78 	} else {
     79 		ha = (X68K_BOOT_SCSIIF_SPC << 4) | 1;
     80 	}
     81 
     82 	return ha;
     83 }
     84 
     85 
     86 static void
     87 help(void)
     88 {
     89 	printf ("Usage:\n");
     90 	printf ("boot [dev:][file] -[flags]\n");
     91 	printf (" dev:   sd<ID><PART>, ID=0-7, PART=a-p\n");
     92 	printf ("        cd<ID>a, ID=0-7\n");
     93 	printf ("        fd<UNIT>a, UNIT=0-3, format is detected.\n");
     94 	printf (" file:  netbsd, netbsd.gz, etc.\n");
     95 	printf (" flags: abdqsv\n");
     96 }
     97 
     98 static void
     99 doboot (const char *file, int flags)
    100 {
    101 	u_long		marks[MARK_MAX];
    102 	int fd;
    103 	int dev, unit, part;
    104 	char *name;
    105 
    106 	printf ("Starting %s, flags 0x%x\n", file, flags);
    107 	marks[MARK_START] = 0x100000;
    108 	if ((fd = loadfile(file, marks, LOAD_KERNEL)) == -1)
    109 		return;
    110 	close(fd);
    111 
    112 	if (devparse(file, &dev, &unit, &part, &name) != 0) {
    113 		printf ("XXX: unknown corruption in /boot.\n");
    114 	}
    115 
    116 	printf ("dev = %x, unit = %d, part = %c, name = %s\n",
    117 		dev, unit, part + 'a', name);
    118 
    119 	if (dev == 0) {		/* SCSI */
    120 		dev = X68K_MAKESCSIBOOTDEV(X68K_MAJOR_SD,
    121 					   hostadaptor >> 4,
    122 					   hostadaptor & 15,
    123 					   unit & 7, 0, 0);
    124 	} else {
    125 		dev = X68K_MAKEBOOTDEV(X68K_MAJOR_FD, unit & 3, 0);
    126 	}
    127 	printf ("boot device = %x\n", dev);
    128 	printf ("if = %d, unit = %d, id = %d, lun = %d, part = %c\n",
    129 		B_X68K_SCSI_IF (dev),
    130 		B_X68K_SCSI_IF_UN (dev),
    131 		B_X68K_SCSI_ID (dev),
    132 		B_X68K_SCSI_LUN (dev),
    133 		B_X68K_SCSI_PART (dev) + 'a');
    134 
    135 	{
    136 		short *p = ((short*) marks[MARK_ENTRY]) - 1;
    137 		printf ("Kernel Version: 0x%x\n", *p);
    138 		if (*p != 0x4e73 && *p != 0) {
    139 			/*
    140 			 * XXX temporary solution; compatibility loader
    141 			 * must be written.
    142 			 */
    143 			printf ("This kernel is too new to be loaded by "
    144 				"this version of /boot.\n");
    145 			return;
    146 		}
    147 	}
    148 
    149 	printf("marks[MARK_START]=%x\n", marks[MARK_START]);
    150 	printf("marks[MARK_ENTRY]=%x\n", marks[MARK_ENTRY]);
    151 	printf("marks[MARK_NSYM]=%x\n", marks[MARK_NSYM]);
    152 	printf("marks[MARK_SYM]=%x\n", marks[MARK_SYM]);
    153 	printf("marks[MARK_END]=%x\n", marks[MARK_END]);
    154 	printf("marks[MARK_MAX]=%x\n", marks[MARK_MAX]);
    155 
    156 	exec_image(marks[MARK_START], 0, marks[MARK_ENTRY]-marks[MARK_START],
    157 		   marks[MARK_END]-marks[MARK_START], dev, flags);
    158 
    159 	return;
    160 }
    161 
    162 static void
    163 boot(char *arg)
    164 {
    165 	char filename[80];
    166 	char *p;
    167 	int flags = 0;
    168 
    169 	if (*arg == 0 || *arg == '-') {
    170 		strcpy (filename, default_kernel);
    171 		if (*arg == '-')
    172 			if (parseopts(arg, &flags) == 0) {
    173 				help ();
    174 				return;
    175 			}
    176 		doboot (filename, flags);
    177 		return;
    178 	} else {
    179 		p = gettrailer (arg);
    180 		if (strchr (arg, ':')) {
    181 			strcpy (filename, arg);
    182 			if (arg[strlen(arg) - 1] == ':')
    183 				strcat (filename, "netbsd");
    184 		} else {
    185 			strcpy (filename, default_kernel);
    186 			strcpy (strchr (filename, ':') + 1, arg);
    187 		}
    188 		if (*p == '-') {
    189 			if (parseopts(p, &flags) == 0)
    190 				return;
    191 		} else if (*p != 0) {
    192 			help ();
    193 			return;
    194 		}
    195 
    196 		doboot (filename, flags);
    197 		return;
    198 	}
    199 }
    200 
    201 
    202 int
    203 bootmenu(void)
    204 {
    205 	char input[80];
    206 	int n = 5, c;
    207 
    208 	printf("booting %s - starting in %d seconds. ",
    209 		default_kernel, n);
    210 	while (n-- > 0 && (c = awaitkey_1sec()) == 0) {
    211 		printf("\r");
    212 		printf("booting %s - starting in %d seconds. ",
    213 		       default_kernel, n);
    214 	}
    215 	printf("\r");
    216 	printf("booting %s - starting in %d seconds. ", default_kernel, 0);
    217 	printf("\n");
    218 
    219 	if (c == 0 || c == '\r') {
    220 		doboot(default_kernel, 0);
    221 		printf("Could not start %s; ", default_kernel);
    222 		strcat(default_kernel, ".gz");
    223 		printf("trying %s.\n", default_kernel);
    224 		doboot(default_kernel, 0);
    225 		printf("Could not start %s; ", default_kernel);
    226 		exit(1);
    227 	}
    228 
    229 	for (;;) {
    230 		char *p, *options;
    231 
    232 		printf("> ");
    233 		gets(input);
    234 
    235 		for (p = &input[0]; p - &input[0] < 80 && *p == ' '; p++);
    236 		options = gettrailer(p);
    237 		if (strcmp("boot", p) == 0)
    238 			boot(options);
    239 		else if (strcmp("help", p) == 0 ||
    240 			 strcmp("?", p) == 0)
    241 			help();
    242 		else if (strcmp("reboot", p) == 0)
    243 			exit(0);
    244 		else
    245 			printf("Unknown command %s\n", p);
    246 	}
    247 }
    248 
    249 
    250 /*
    251  * Arguments from the boot block:
    252  *   bootdev - specifies the device from which /boot was read, in
    253  *		bootdev format.
    254  */
    255 void
    256 bootmain(int bootdev)
    257 {
    258 	hostadaptor = get_scsi_host_adapter();
    259 	mpu = detectmpu();
    260 
    261 	if (mpu < 3) {		/* not tested on 68020 */
    262 		printf("This MPU cannot run NetBSD.\n");
    263 		exit(1);
    264 	}
    265 	if (SRAM_MEMSIZE < 4*1024*1024) {
    266 		printf("Main memory too small.\n");
    267 		exit(1);
    268 	}
    269 
    270 	console_device = consio_init(console_device);
    271 	setheap(HEAP_START, HEAP_END);
    272 
    273 	if (BINF_ISFD(&bootdev)) {
    274 		default_kernel[0] = 'f';
    275 		default_kernel[2] = '0' + B_UNIT(bootdev);
    276 		default_kernel[3] = 'a';
    277 	} else {
    278 		default_kernel[2] = '0' + B_X68K_SCSI_ID(bootdev);
    279 		default_kernel[3] = 'a';
    280 	}
    281 	printf("NetBSD/x68k bootstrap loader version %s\n\n", BOOT_VERS);
    282 	bootmenu();
    283 }
    284