Home | History | Annotate | Line # | Download | only in boot
boot.c revision 1.25
      1 /*	$NetBSD: boot.c,v 1.25 2016/06/19 09:23:16 isaki 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/libsa/ufs.h>
     35 #ifdef NETBOOT
     36 #include <lib/libsa/dev_net.h>
     37 #endif
     38 #include <lib/libkern/libkern.h>
     39 
     40 #include "libx68k.h"
     41 #include "iocs.h"
     42 #include "switch.h"
     43 
     44 #include "exec_image.h"
     45 
     46 
     47 #define HEAP_START	((void*) 0x00080000)
     48 #define HEAP_END	((void*) 0x000fffff)
     49 #define EXSCSI_BDID	((void*) 0x00ea0001)
     50 #define SRAM_MEMSIZE	(*((long*) 0x00ed0008))
     51 
     52 char default_kernel[20] =
     53 #ifndef NETBOOT
     54     "sd0a:netbsd";
     55 #else
     56     "nfs:netbsd";
     57 #endif
     58 int mpu;
     59 #ifndef NETBOOT
     60 int hostadaptor;
     61 #endif
     62 int console_device = -1;
     63 
     64 #ifdef DEBUG
     65 #ifdef NETBOOT
     66 int debug = 1;
     67 #endif
     68 #endif
     69 
     70 static void help(void);
     71 #ifndef NETBOOT
     72 static int get_scsi_host_adapter(void);
     73 #endif
     74 static void doboot(const char *, int);
     75 static void boot(char *);
     76 #ifndef NETBOOT
     77 static void cmd_ls(char *);
     78 #endif
     79 int bootmenu(void);
     80 void bootmain(int);
     81 extern int detectmpu(void);
     82 extern int badbaddr(void *);
     83 
     84 #ifndef NETBOOT
     85 /* from boot_ufs/bootmain.c */
     86 static int
     87 get_scsi_host_adapter(void)
     88 {
     89 	char *bootrom;
     90 	int ha;
     91 
     92 	bootrom = (char *) (IOCS_BOOTINF() & 0x00ffffe0);
     93 	/*
     94 	 * bootrom+0x24	"SCSIIN" ... Internal SCSI (spc@0)
     95 	 *		"SCSIEX" ... External SCSI (spc@1 or mha@0)
     96 	 */
     97 	if (*(u_short *)(bootrom + 0x24 + 4) == 0x494e) {	/* "IN" */
     98 		ha = (X68K_BOOT_SCSIIF_SPC << 4) | 0;
     99 	} else if (badbaddr(EXSCSI_BDID)) {
    100 		ha = (X68K_BOOT_SCSIIF_MHA << 4) | 0;
    101 	} else {
    102 		ha = (X68K_BOOT_SCSIIF_SPC << 4) | 1;
    103 	}
    104 
    105 	return ha;
    106 }
    107 #endif
    108 
    109 static void
    110 help(void)
    111 {
    112 	printf("Usage:\n");
    113 	printf("boot [dev:][file] -[flags]\n");
    114 #ifndef NETBOOT
    115 	printf(" dev:   sd<ID><PART>, ID=0-7, PART=a-p\n");
    116 	printf("        cd<ID>a, ID=0-7\n");
    117 	printf("        fd<UNIT>a, UNIT=0-3, format is detected.\n");
    118 #else
    119 	printf(" dev:   nfs, first probed NE2000 is used.\n");
    120 #endif
    121 	printf(" file:  netbsd, netbsd.gz, etc.\n");
    122 	printf(" flags: abdqsv\n");
    123 #ifndef NETBOOT
    124 	printf("ls [dev:][directory]\n");
    125 #endif
    126 	printf("switch [show | key=val]\n");
    127 	printf("halt\nreboot\n");
    128 }
    129 
    130 static void
    131 doboot(const char *file, int flags)
    132 {
    133 	u_long		marks[MARK_MAX];
    134 	int fd;
    135 	int dev;	/* device number in devspec[] */
    136 	int unit;
    137 	int part;
    138 	int bootdev;
    139 	char *name;
    140 	short *p;
    141 	int loadflag;
    142 
    143 	printf("Starting %s, flags 0x%x\n", file, flags);
    144 
    145 	loadflag = LOAD_KERNEL;
    146 	if (file[0] == 'f')
    147 		loadflag &= ~LOAD_BACKWARDS;
    148 
    149 	marks[MARK_START] = 0x100000;
    150 	if ((fd = loadfile(file, marks, loadflag)) == -1) {
    151 		printf("loadfile failed\n");
    152 		return;
    153 	}
    154 	close(fd);
    155 
    156 	if (devparse(file, &dev, &unit, &part, &name) != 0) {
    157 		printf("XXX: unknown corruption in /boot.\n");
    158 	}
    159 
    160 #ifdef DEBUG
    161 #ifndef NETBOOT
    162 	printf("dev = %x, unit = %d, part = %c, name = %s\n",
    163 	       dev, unit, part + 'a', name);
    164 #else
    165 	printf("dev = %x, unit = %d, name = %s\n",
    166 	       dev, unit, name);
    167 #endif
    168 #endif
    169 
    170 #ifndef NETBOOT
    171 	if (dev == 0) {		/* SCSI */
    172 		bootdev = X68K_MAKESCSIBOOTDEV(X68K_MAJOR_SD,
    173 					   hostadaptor >> 4,
    174 					   hostadaptor & 15,
    175 					   unit & 7, 0, 0);
    176 	} else {
    177 		bootdev = X68K_MAKEBOOTDEV(X68K_MAJOR_FD, unit & 3, 0);
    178 	}
    179 #else
    180 	bootdev = X68K_MAKEBOOTDEV(X68K_MAJOR_NE, unit, 0);
    181 #endif
    182 #ifdef DEBUG
    183 	printf("boot device = %x\n", bootdev);
    184 #ifndef NETBOOT
    185 	printf("if = %d, unit = %d, id = %d, lun = %d, part = %c\n",
    186 	       B_X68K_SCSI_IF(bootdev),
    187 	       B_X68K_SCSI_IF_UN(bootdev),
    188 	       B_X68K_SCSI_ID(bootdev),
    189 	       B_X68K_SCSI_LUN(bootdev),
    190 	       B_X68K_SCSI_PART(bootdev) + 'a');
    191 #else
    192 	printf("if = %d, unit = %d\n",
    193 	       B_X68K_SCSI_IF(bootdev),
    194 	       B_X68K_SCSI_IF_UN(bootdev));
    195 #endif
    196 #endif
    197 
    198 	p = ((short*) marks[MARK_ENTRY]) - 1;
    199 #ifdef DEBUG
    200 	printf("Kernel Version: 0x%x\n", *p);
    201 #endif
    202 	if (*p != 0x4e73 && *p != 0) {
    203 		/*
    204 		 * XXX temporary solution; compatibility loader
    205 		 * must be written.
    206 		 */
    207 		printf("This kernel is too new to be loaded by "
    208 		       "this version of /boot.\n");
    209 		return;
    210 	}
    211 
    212 	exec_image(marks[MARK_START], 0, marks[MARK_ENTRY]-marks[MARK_START],
    213 		   marks[MARK_END]-marks[MARK_START], bootdev, flags);
    214 
    215 	return;
    216 }
    217 
    218 static void
    219 boot(char *arg)
    220 {
    221 	char filename[80];
    222 	char *p;
    223 	int flags = 0;
    224 
    225 	if (*arg == 0 || *arg == '-') {
    226 		strcpy(filename, default_kernel);
    227 		if (*arg == '-')
    228 			if (parseopts(arg, &flags) == 0) {
    229 				help();
    230 				return;
    231 			}
    232 		doboot(filename, flags);
    233 		return;
    234 	} else {
    235 		p = gettrailer(arg);
    236 		if (strchr(arg, ':')) {
    237 			strcpy(filename, arg);
    238 			if (arg[strlen(arg) - 1] == ':')
    239 				strcat(filename, "netbsd");
    240 		} else {
    241 			strcpy(filename, default_kernel);
    242 			strcpy(strchr(filename, ':') + 1, arg);
    243 		}
    244 		if (*p == '-') {
    245 			if (parseopts(p, &flags) == 0)
    246 				return;
    247 		} else if (*p != 0) {
    248 			help();
    249 			return;
    250 		}
    251 
    252 		doboot(filename, flags);
    253 		return;
    254 	}
    255 }
    256 
    257 #ifndef NETBOOT
    258 static void
    259 cmd_ls(char *arg)
    260 {
    261 	char filename[80];
    262 
    263 	devopen_open_dir = 1;
    264 	if (*arg == 0) {
    265 		strcpy(filename, default_kernel);
    266 		strcpy(strchr(filename, ':')+1, "/");
    267 	} else if (strchr(arg, ':') == 0) {
    268 		strcpy(filename, default_kernel);
    269 		strcpy(strchr(filename, ':')+1, arg);
    270 	} else {
    271 		strcpy(filename, arg);
    272 		if (*(strchr(arg, ':')+1) == 0)
    273 			strcat(filename, "/");
    274 	}
    275 	ls(filename);
    276 	devopen_open_dir = 0;
    277 }
    278 #endif
    279 
    280 int
    281 bootmenu(void)
    282 {
    283 	char input[80];
    284 	int n = 5, c;
    285 
    286 	printf("Press return to boot now, any other key for boot menu\n");
    287 	printf("booting %s - starting in %d seconds. ",
    288 		default_kernel, n);
    289 	while (n-- > 0 && (c = awaitkey_1sec()) == 0) {
    290 		printf("\r");
    291 		printf("booting %s - starting in %d seconds. ",
    292 		       default_kernel, n);
    293 	}
    294 	printf("\r");
    295 	printf("booting %s - starting in %d seconds. ", default_kernel, 0);
    296 	printf("\n");
    297 
    298 	if (c == 0 || c == '\r') {
    299 		doboot(default_kernel, 0);
    300 		printf("Could not start %s; ", default_kernel);
    301 		strcat(default_kernel, ".gz");
    302 		printf("trying %s.\n", default_kernel);
    303 		doboot(default_kernel, 0);
    304 		printf("Could not start %s; ", default_kernel);
    305 	}
    306 
    307 	printf("Please use the absolute unit# (e.g. SCSI ID)"
    308 	       " instead of the NetBSD logical #.\n");
    309 	for (;;) {
    310 		char *p, *options;
    311 
    312 		printf("> ");
    313 		gets(input);
    314 
    315 		for (p = &input[0]; p - &input[0] < 80 && *p == ' '; p++)
    316 			;
    317 		options = gettrailer(p);
    318 		if (strcmp("boot", p) == 0)
    319 			boot(options);
    320 		else if (strcmp("help", p) == 0 ||
    321 			 strcmp("?", p) == 0)
    322 			help();
    323 		else if (strcmp("halt", p) == 0 ||
    324 			 strcmp("reboot", p) == 0)
    325 			exit(0);
    326 		else if (strcmp("switch", p) == 0)
    327 			cmd_switch(options);
    328 #ifndef NETBOOT
    329 		else if (strcmp("ls", p) == 0)
    330 			cmd_ls(options);
    331 #endif
    332 		else
    333 			printf("Unknown command %s\n", p);
    334 	}
    335 }
    336 
    337 static u_int
    338 checkmemsize(void)
    339 {
    340 	u_int m;
    341 
    342 #define MIN_MB 4
    343 #define MAX_MB 12
    344 
    345 	for (m = MIN_MB; m <= MAX_MB; m++) {
    346 		if (badbaddr((void *)(m * 1024 * 1024 - 1))) {
    347 			/* no memory */
    348 			break;
    349 		}
    350 	}
    351 
    352 	return (m - 1) * 1024 * 1024;
    353 }
    354 
    355 extern const char bootprog_rev[];
    356 extern const char bootprog_name[];
    357 
    358 /*
    359  * Arguments from the boot block:
    360  *   bootdev - specifies the device from which /boot was read, in
    361  *		bootdev format.
    362  */
    363 void
    364 bootmain(int bootdev)
    365 {
    366 	u_int sram_memsize;
    367 	u_int probed_memsize;
    368 
    369 #ifndef NETBOOT
    370 	hostadaptor = get_scsi_host_adapter();
    371 #else
    372 	rtc_offset = RTC_OFFSET;
    373 	try_bootp = 1;
    374 #endif
    375 	mpu = detectmpu();
    376 
    377 	if (mpu < 3) {		/* not tested on 68020 */
    378 		printf("This MPU cannot run NetBSD.\n");
    379 		exit(1);
    380 	}
    381 	sram_memsize = SRAM_MEMSIZE;
    382 	if (sram_memsize < 4*1024*1024) {
    383 		printf("Main memory too small.\n");
    384 		exit(1);
    385 	}
    386 
    387 	console_device = consio_init(console_device);
    388 	setheap(HEAP_START, HEAP_END);
    389 
    390 #ifndef NETBOOT
    391 	switch (B_TYPE(bootdev)) {
    392 	case X68K_MAJOR_FD:
    393 		default_kernel[0] = 'f';
    394 		default_kernel[2] = '0' + B_UNIT(bootdev);
    395 		default_kernel[3] = 'a';
    396 		break;
    397 	case X68K_MAJOR_SD:
    398 		default_kernel[2] = '0' + B_X68K_SCSI_ID(bootdev);
    399 		default_kernel[3] =
    400 			'a' + sd_getbsdpartition(B_X68K_SCSI_ID(bootdev),
    401 						 B_X68K_SCSI_PART(bootdev));
    402 		break;
    403 	case X68K_MAJOR_CD:
    404 		default_kernel[0] = 'c';
    405 		default_kernel[2] = '0' + B_X68K_SCSI_ID(bootdev);
    406 		default_kernel[3] = 'a';
    407 		break;
    408 	default:
    409 		printf("Warning: unknown boot device: %x\n", bootdev);
    410 	}
    411 #endif
    412 	print_title("%s, Revision %s\n", bootprog_name, bootprog_rev);
    413 
    414 	/* check actual memory size for machines with a dead SRAM battery */
    415 	probed_memsize = checkmemsize();
    416 	if (sram_memsize != probed_memsize) {
    417 		printf("\x1b[1mWarning: SRAM Memory Size (%d MB) "
    418 		    "is different from probed Memory Size (%d MB)\n"
    419 		    "         Check and reset SRAM values.\x1b[m\n\n",
    420 		    sram_memsize / (1024 * 1024),
    421 		    probed_memsize / (1024 * 1024));
    422 	}
    423 
    424 	bootmenu();
    425 }
    426